SettingsController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. namespace Typemill\Controllers;
  3. use \Symfony\Component\Yaml\Yaml;
  4. use Typemill\Models\Write;
  5. use Typemill\Models\Fields;
  6. use Typemill\Models\Validation;
  7. use Typemill\Models\User;
  8. use Typemill\Models\ProcessFile;
  9. use Typemill\Models\ProcessImage;
  10. class SettingsController extends Controller
  11. {
  12. /*********************
  13. ** BASIC SETTINGS **
  14. *********************/
  15. public function showSettings($request, $response, $args)
  16. {
  17. $user = new User();
  18. $settings = $this->c->get('settings');
  19. $defaultSettings = \Typemill\Settings::getDefaultSettings();
  20. $copyright = $this->getCopyright();
  21. $languages = $this->getLanguages();
  22. $locale = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2) : 'en';
  23. $users = $user->getUsers();
  24. $route = $request->getAttribute('route');
  25. return $this->render($response, 'settings/system.twig', array('settings' => $settings, 'copyright' => $copyright, 'languages' => $languages, 'locale' => $locale, 'formats' => $defaultSettings['formats'] ,'users' => $users, 'route' => $route->getName() ));
  26. }
  27. public function saveSettings($request, $response, $args)
  28. {
  29. if($request->isPost())
  30. {
  31. $referer = $request->getHeader('HTTP_REFERER');
  32. $uri = $request->getUri();
  33. $base_url = $uri->getBaseUrl();
  34. # security, users should not be able to fake post with settings from other typemill pages.
  35. if(!isset($referer[0]) OR $referer[0] !== $base_url . '/tm/settings' )
  36. {
  37. $this->c->flash->addMessage('error', 'illegal referer');
  38. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  39. }
  40. $settings = \Typemill\Settings::getUserSettings();
  41. $defaultSettings = \Typemill\Settings::getDefaultSettings();
  42. $params = $request->getParams();
  43. $files = $request->getUploadedFiles();
  44. $newSettings = isset($params['settings']) ? $params['settings'] : false;
  45. $validate = new Validation();
  46. $processFiles = new ProcessFile();
  47. if($newSettings)
  48. {
  49. /* make sure only allowed fields are stored */
  50. $newSettings = array(
  51. 'title' => $newSettings['title'],
  52. 'author' => $newSettings['author'],
  53. 'copyright' => $newSettings['copyright'],
  54. 'year' => $newSettings['year'],
  55. 'language' => $newSettings['language'],
  56. 'langattr' => $newSettings['langattr'],
  57. 'editor' => $newSettings['editor'],
  58. 'formats' => $newSettings['formats'],
  59. 'headlineanchors' => isset($newSettings['headlineanchors']) ? $newSettings['headlineanchors'] : null,
  60. );
  61. # https://www.slimframework.com/docs/v3/cookbook/uploading-files.html;
  62. $copyright = $this->getCopyright();
  63. $validate->settings($newSettings, $copyright, $defaultSettings['formats'], 'settings');
  64. }
  65. else
  66. {
  67. $this->c->flash->addMessage('error', 'Wrong Input');
  68. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  69. }
  70. if(isset($_SESSION['errors']))
  71. {
  72. $this->c->flash->addMessage('error', 'Please correct the errors');
  73. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  74. }
  75. if(!$processFiles->checkFolders())
  76. {
  77. $this->c->flash->addMessage('error', 'Please make sure that your media folder exists and is writable.');
  78. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  79. }
  80. # handle single input with single file upload
  81. $logo = $files['settings']['logo'];
  82. if($logo->getError() === UPLOAD_ERR_OK)
  83. {
  84. $allowed = ['jpg', 'jpeg', 'png', 'svg'];
  85. $extension = pathinfo($logo->getClientFilename(), PATHINFO_EXTENSION);
  86. if(!in_array(strtolower($extension), $allowed))
  87. {
  88. $_SESSION['errors']['settings']['logo'] = array('Only jpg, jpeg, png and svg allowed');
  89. $this->c->flash->addMessage('error', 'Please correct the errors');
  90. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  91. }
  92. $processFiles->deleteFileWithName('logo');
  93. $newSettings['logo'] = $processFiles->moveUploadedFile($logo, $overwrite = true, $name = 'logo');
  94. }
  95. elseif(isset($params['settings']['deletelogo']) && $params['settings']['deletelogo'] == 'delete')
  96. {
  97. $processFiles->deleteFileWithName('logo');
  98. $newSettings['logo'] = '';
  99. }
  100. else
  101. {
  102. $newSettings['logo'] = isset($settings['logo']) ? $settings['logo'] : '';
  103. }
  104. # handle single input with single file upload
  105. $favicon = $files['settings']['favicon'];
  106. if ($favicon->getError() === UPLOAD_ERR_OK)
  107. {
  108. $extension = pathinfo($favicon->getClientFilename(), PATHINFO_EXTENSION);
  109. if(strtolower($extension) != 'png')
  110. {
  111. $_SESSION['errors']['settings']['favicon'] = array('Only .png-files allowed');
  112. $this->c->flash->addMessage('error', 'Please correct the errors');
  113. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  114. }
  115. $processImage = new ProcessImage([
  116. '16' => ['width' => 16, 'height' => 16],
  117. '32' => ['width' => 32, 'height' => 32],
  118. '72' => ['width' => 72, 'height' => 72],
  119. '114' => ['width' => 114, 'height' => 114],
  120. '144' => ['width' => 144, 'height' => 144],
  121. '180' => ['width' => 180, 'height' => 180],
  122. ]);
  123. $favicons = $processImage->generateSizesFromImageFile('favicon.png', $favicon->file);
  124. foreach($favicons as $key => $favicon)
  125. {
  126. imagepng( $favicon, $processFiles->fileFolder . 'favicon-' . $key . '.png' );
  127. # $processFiles->moveUploadedFile($favicon, $overwrite = true, $name = 'favicon-' . $key);
  128. }
  129. $newSettings['favicon'] = 'favicon';
  130. }
  131. elseif(isset($params['settings']['deletefav']) && $params['settings']['deletefav'] == 'delete')
  132. {
  133. $processFiles->deleteFileWithName('favicon');
  134. $newSettings['favicon'] = '';
  135. }
  136. else
  137. {
  138. $newSettings['favicon'] = isset($settings['favicon']) ? $settings['favicon'] : '';
  139. }
  140. # store updated settings
  141. \Typemill\Settings::updateSettings(array_merge($settings, $newSettings));
  142. $this->c->flash->addMessage('info', 'Settings are stored');
  143. return $response->withRedirect($this->c->router->pathFor('settings.show'));
  144. }
  145. }
  146. /*********************
  147. ** THEME SETTINGS **
  148. *********************/
  149. public function showThemes($request, $response, $args)
  150. {
  151. $userSettings = $this->c->get('settings');
  152. $themes = $this->getThemes();
  153. $themedata = array();
  154. $fieldsModel = new Fields();
  155. foreach($themes as $themeName)
  156. {
  157. /* if theme is active, list it first */
  158. if($userSettings['theme'] == $themeName)
  159. {
  160. $themedata = array_merge(array($themeName => null), $themedata);
  161. }
  162. else
  163. {
  164. $themedata[$themeName] = null;
  165. }
  166. $themeSettings = \Typemill\Settings::getObjectSettings('themes', $themeName);
  167. # add standard-textarea for custom css
  168. $themeSettings['forms']['fields']['customcss'] = ['type' => 'textarea', 'label' => 'Custom CSS', 'rows' => 10, 'class' => 'codearea', 'description' => 'You can overwrite the theme-css with your own css here.'];
  169. # load custom css-file
  170. $write = new write();
  171. $customcss = $write->getFile('cache', $themeName . '-custom.css');
  172. $themeSettings['settings']['customcss'] = $customcss;
  173. if($themeSettings)
  174. {
  175. /* store them as default theme data with author, year, default settings and field-definitions */
  176. $themedata[$themeName] = $themeSettings;
  177. }
  178. if(isset($themeSettings['forms']['fields']))
  179. {
  180. $fields = $fieldsModel->getFields($userSettings, 'themes', $themeName, $themeSettings);
  181. /* overwrite original theme form definitions with enhanced form objects */
  182. $themedata[$themeName]['forms']['fields'] = $fields;
  183. }
  184. /* add the preview image */
  185. $img = getcwd() . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $themeName . DIRECTORY_SEPARATOR . $themeName;
  186. $image = false;
  187. if(file_exists($img . '.jpg'))
  188. {
  189. $image = $themeName . '.jpg';
  190. }
  191. if(file_exists($img . '.png'))
  192. {
  193. $image = $themeName . '.png';
  194. }
  195. $themedata[$themeName]['img'] = $image;
  196. }
  197. /* add the users for navigation */
  198. $user = new User();
  199. $users = $user->getUsers();
  200. $route = $request->getAttribute('route');
  201. return $this->render($response, 'settings/themes.twig', array('settings' => $userSettings, 'themes' => $themedata, 'users' => $users, 'route' => $route->getName() ));
  202. }
  203. public function showPlugins($request, $response, $args)
  204. {
  205. $userSettings = $this->c->get('settings');
  206. $plugins = array();
  207. $fieldsModel = new Fields();
  208. $fields = array();
  209. /* iterate through the plugins in the stored user settings */
  210. foreach($userSettings['plugins'] as $pluginName => $pluginUserSettings)
  211. {
  212. /* add plugin to plugin Data, if active, set it first */
  213. /* if plugin is active, list it first */
  214. if($userSettings['plugins'][$pluginName]['active'] == true)
  215. {
  216. $plugins = array_merge(array($pluginName => null), $plugins);
  217. }
  218. else
  219. {
  220. $plugins[$pluginName] = Null;
  221. }
  222. /* Check if the user has deleted a plugin. Then delete it in the settings and store the updated settings. */
  223. if(!is_dir($userSettings['rootPath'] . 'plugins' . DIRECTORY_SEPARATOR . $pluginName))
  224. {
  225. /* remove the plugin settings and store updated settings */
  226. \Typemill\Settings::removePluginSettings($pluginName);
  227. continue;
  228. }
  229. /* load the original plugin definitions from the plugin folder (author, version and stuff) */
  230. $pluginOriginalSettings = \Typemill\Settings::getObjectSettings('plugins', $pluginName);
  231. if($pluginOriginalSettings)
  232. {
  233. /* store them as default plugin data with plugin author, plugin year, default settings and field-definitions */
  234. $plugins[$pluginName] = $pluginOriginalSettings;
  235. }
  236. /* check, if the plugin has been disabled in the form-session-data */
  237. if(isset($_SESSION['old']) && !isset($_SESSION['old'][$pluginName]['active']))
  238. {
  239. $plugins[$pluginName]['settings']['active'] = false;
  240. }
  241. /* if the plugin defines forms and fields, so that the user can edit the plugin settings in the frontend */
  242. if(isset($pluginOriginalSettings['forms']['fields']))
  243. {
  244. # if the plugin defines frontend fields
  245. if(isset($pluginOriginalSettings['public']))
  246. {
  247. $pluginOriginalSettings['forms']['fields']['recaptcha'] = ['type' => 'checkbox', 'label' => 'Google Recaptcha', 'checkboxlabel' => 'Activate Recaptcha' ];
  248. $pluginOriginalSettings['forms']['fields']['recaptcha_webkey'] = ['type' => 'text', 'label' => 'Recaptcha Website Key', 'help' => 'Add the recaptcha website key here. You can get the key from the recaptcha website.', 'description' => 'The website key is mandatory if you activate the recaptcha field'];
  249. $pluginOriginalSettings['forms']['fields']['recaptcha_secretkey'] = ['type' => 'text', 'label' => 'Recaptcha Secret Key', 'help' => 'Add the recaptcha secret key here. You can get the key from the recaptcha website.', 'description' => 'The secret key is mandatory if you activate the recaptcha field'];
  250. }
  251. /* get all the fields and prefill them with the dafault-data, the user-data or old input data */
  252. $fields = $fieldsModel->getFields($userSettings, 'plugins', $pluginName, $pluginOriginalSettings);
  253. /* overwrite original plugin form definitions with enhanced form objects */
  254. $plugins[$pluginName]['forms']['fields'] = $fields;
  255. }
  256. }
  257. $user = new User();
  258. $users = $user->getUsers();
  259. $route = $request->getAttribute('route');
  260. return $this->render($response, 'settings/plugins.twig', array('settings' => $userSettings, 'plugins' => $plugins, 'users' => $users, 'route' => $route->getName() ));
  261. }
  262. /*************************************
  263. ** SAVE THEME- AND PLUGIN-SETTINGS **
  264. *************************************/
  265. public function saveThemes($request, $response, $args)
  266. {
  267. if($request->isPost())
  268. {
  269. $referer = $request->getHeader('HTTP_REFERER');
  270. $uri = $request->getUri();
  271. $base_url = $uri->getBaseUrl();
  272. # users should not be able to fake post with settings from other typemill pages.
  273. if(!isset($referer[0]) OR $referer[0] !== $base_url . '/tm/themes' )
  274. {
  275. $this->c->flash->addMessage('error', 'illegal referer');
  276. return $response->withRedirect($this->c->router->pathFor('themes.show'));
  277. }
  278. $userSettings = \Typemill\Settings::getUserSettings();
  279. $params = $request->getParams();
  280. $themeName = isset($params['theme']) ? $params['theme'] : false;
  281. $userInput = isset($params[$themeName]) ? $params[$themeName] : false;
  282. $validate = new Validation();
  283. $themeSettings = \Typemill\Settings::getObjectSettings('themes', $themeName);
  284. if(isset($themeSettings['settings']['images']))
  285. {
  286. # get the default settings
  287. $defaultSettings = \Typemill\Settings::getDefaultSettings();
  288. # merge the default image settings with the theme image settings, delete all others (image settings from old theme)
  289. $userSettings['images'] = array_merge($defaultSettings['images'], $themeSettings['settings']['images']);
  290. }
  291. /* set theme name and delete theme settings from user settings for the case, that the new theme has no settings */
  292. $userSettings['theme'] = $themeName;
  293. # extract the custom css from user input
  294. $customcss = isset($userInput['customcss']) ? $userInput['customcss'] : false;
  295. # delete custom css from userinput
  296. unset($userInput['customcss']);
  297. $write = new write();
  298. # make sure no file is set if there is no custom css
  299. if(!$customcss OR $customcss == '')
  300. {
  301. # delete the css file if exists
  302. $write->deleteFileWithPath('cache' . DIRECTORY_SEPARATOR . $themeName . '-custom.css');
  303. }
  304. else
  305. {
  306. if ( $customcss != strip_tags($customcss) )
  307. {
  308. $_SESSION['errors'][$themeName]['customcss'][] = 'custom css contains html';
  309. }
  310. else
  311. {
  312. # store css
  313. $write = new write();
  314. $write->writeFile('cache', $themeName . '-custom.css', $customcss);
  315. }
  316. }
  317. if($userInput)
  318. {
  319. /* validate the user-input */
  320. $this->validateInput('themes', $themeName, $userInput, $validate);
  321. /* set user input as theme settings */
  322. $userSettings['themes'][$themeName] = $userInput;
  323. }
  324. /* check for errors and redirect to path, if errors found */
  325. if(isset($_SESSION['errors']))
  326. {
  327. $this->c->flash->addMessage('error', 'Please correct the errors');
  328. return $response->withRedirect($this->c->router->pathFor('themes.show'));
  329. }
  330. /* store updated settings */
  331. \Typemill\Settings::updateSettings($userSettings);
  332. $this->c->flash->addMessage('info', 'Settings are stored');
  333. return $response->withRedirect($this->c->router->pathFor('themes.show'));
  334. }
  335. }
  336. public function savePlugins($request, $response, $args)
  337. {
  338. if($request->isPost())
  339. {
  340. $referer = $request->getHeader('HTTP_REFERER');
  341. $uri = $request->getUri();
  342. $base_url = $uri->getBaseUrl();
  343. # security, users should not be able to fake post with settings from other typemill pages.
  344. if(!isset($referer[0]) OR $referer[0] !== $base_url . '/tm/plugins' )
  345. {
  346. $this->c->flash->addMessage('error', 'illegal referer');
  347. return $response->withRedirect($this->c->router->pathFor('plugins.show'));
  348. }
  349. $userSettings = \Typemill\Settings::getUserSettings();
  350. $pluginSettings = array();
  351. $userInput = $request->getParams();
  352. $validate = new Validation();
  353. /* use the stored user settings and iterate over all original plugin settings, so we do not forget any... */
  354. foreach($userSettings['plugins'] as $pluginName => $pluginUserSettings)
  355. {
  356. /* if there are no input-data for this plugin, then use the stored plugin settings */
  357. if(!isset($userInput[$pluginName]))
  358. {
  359. $pluginSettings[$pluginName] = $pluginUserSettings;
  360. }
  361. else
  362. {
  363. /* validate the user-input */
  364. $this->validateInput('plugins', $pluginName, $userInput[$pluginName], $validate);
  365. /* use the input data */
  366. $pluginSettings[$pluginName] = $userInput[$pluginName];
  367. }
  368. /* deactivate the plugin, if there is no active flag */
  369. if(!isset($userInput[$pluginName]['active']))
  370. {
  371. $pluginSettings[$pluginName]['active'] = false;
  372. }
  373. }
  374. if(isset($_SESSION['errors']))
  375. {
  376. $this->c->flash->addMessage('error', 'Please correct the errors below');
  377. }
  378. else
  379. {
  380. /* if everything is valid, add plugin settings to base settings again */
  381. $userSettings['plugins'] = $pluginSettings;
  382. /* store updated settings */
  383. \Typemill\Settings::updateSettings($userSettings);
  384. $this->c->flash->addMessage('info', 'Settings are stored');
  385. }
  386. return $response->withRedirect($this->c->router->pathFor('plugins.show'));
  387. }
  388. }
  389. private function validateInput($objectType, $objectName, $userInput, $validate)
  390. {
  391. /* fetch the original settings from the folder (plugin or theme) to get the field definitions */
  392. $originalSettings = \Typemill\Settings::getObjectSettings($objectType, $objectName);
  393. if(isset($originalSettings['forms']['fields']))
  394. {
  395. /* flaten the multi-dimensional array with fieldsets to a one-dimensional array */
  396. $originalFields = array();
  397. foreach($originalSettings['forms']['fields'] as $fieldName => $fieldValue)
  398. {
  399. if(isset($fieldValue['fields']))
  400. {
  401. foreach($fieldValue['fields'] as $subFieldName => $subFieldValue)
  402. {
  403. $originalFields[$subFieldName] = $subFieldValue;
  404. }
  405. }
  406. else
  407. {
  408. $originalFields[$fieldName] = $fieldValue;
  409. }
  410. }
  411. # if the plugin defines frontend fields
  412. if(isset($originalSettings['public']))
  413. {
  414. $originalFields['recaptcha'] = ['type' => 'checkbox', 'label' => 'Google Recaptcha', 'checkboxlabel' => 'Activate Recaptcha' ];
  415. $originalFields['recaptcha_webkey'] = ['type' => 'text', 'label' => 'Recaptcha Website Key', 'help' => 'Add the recaptcha website key here. You can get the key from the recaptcha website.', 'description' => 'The website key is mandatory if you activate the recaptcha field'];
  416. $originalFields['recaptcha_secretkey'] = ['type' => 'text', 'label' => 'Recaptcha Secret Key', 'help' => 'Add the recaptcha secret key here. You can get the key from the recaptcha website.', 'description' => 'The secret key is mandatory if you activate the recaptcha field'];
  417. }
  418. # if plugin is not active, then skip required
  419. $skiprequired = false;
  420. if($objectType == 'plugins' && !isset($userInput['active']))
  421. {
  422. $skiprequired = true;
  423. }
  424. /* take the user input data and iterate over all fields and values */
  425. foreach($userInput as $fieldName => $fieldValue)
  426. {
  427. /* get the corresponding field definition from original plugin settings */
  428. $fieldDefinition = isset($originalFields[$fieldName]) ? $originalFields[$fieldName] : false;
  429. if($fieldDefinition)
  430. {
  431. /* validate user input for this field */
  432. $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition, $skiprequired);
  433. }
  434. if(!$fieldDefinition && $fieldName != 'active')
  435. {
  436. $_SESSION['errors'][$objectName][$fieldName] = array('This field is not defined!');
  437. }
  438. }
  439. }
  440. }
  441. /***********************
  442. ** USER MANAGEMENT **
  443. ***********************/
  444. public function showUser($request, $response, $args)
  445. {
  446. if($_SESSION['role'] == 'editor' && $_SESSION['user'] !== $args['username'])
  447. {
  448. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $_SESSION['user']] ));
  449. }
  450. $validate = new Validation();
  451. if($validate->username($args['username']))
  452. {
  453. $user = new User();
  454. $users = $user->getUsers();
  455. $userrole = $user->getUserroles();
  456. $userdata = $user->getUser($args['username']);
  457. $settings = $this->c->get('settings');
  458. if($userdata)
  459. {
  460. return $this->render($response, 'settings/user.twig', array('settings' => $settings, 'users' => $users, 'userdata' => $userdata, 'userrole' => $userrole, 'username' => $args['username'] ));
  461. }
  462. }
  463. $this->c->flash->addMessage('error', 'User does not exists');
  464. return $response->withRedirect($this->c->router->pathFor('user.list'));
  465. }
  466. public function listUser($request, $response)
  467. {
  468. $user = new User();
  469. $users = $user->getUsers();
  470. $userdata = array();
  471. $route = $request->getAttribute('route');
  472. $settings = $this->c->get('settings');
  473. foreach($users as $username)
  474. {
  475. $userdata[] = $user->getUser($username);
  476. }
  477. return $this->render($response, 'settings/userlist.twig', array('settings' => $settings, 'users' => $users, 'userdata' => $userdata, 'route' => $route->getName() ));
  478. }
  479. public function newUser($request, $response, $args)
  480. {
  481. $user = new User();
  482. $users = $user->getUsers();
  483. $userrole = $user->getUserroles();
  484. $route = $request->getAttribute('route');
  485. $settings = $this->c->get('settings');
  486. return $this->render($response, 'settings/usernew.twig', array('settings' => $settings, 'users' => $users, 'userrole' => $userrole, 'route' => $route->getName() ));
  487. }
  488. public function createUser($request, $response, $args)
  489. {
  490. if($request->isPost())
  491. {
  492. $referer = $request->getHeader('HTTP_REFERER');
  493. $uri = $request->getUri();
  494. $base_url = $uri->getBaseUrl();
  495. # security, users should not be able to fake post with settings from other typemill pages.
  496. if(!isset($referer[0]) OR $referer[0] !== $base_url . '/tm/user/new' )
  497. {
  498. $this->c->flash->addMessage('error', 'illegal referer');
  499. return $response->withRedirect($this->c->router->pathFor('user.new'));
  500. }
  501. $params = $request->getParams();
  502. $user = new User();
  503. $userroles = $user->getUserroles();
  504. $validate = new Validation();
  505. if($validate->newUser($params, $userroles))
  506. {
  507. $userdata = array('username' => $params['username'], 'firstname' => $params['firstname'], 'lastname' => $params['lastname'], 'email' => $params['email'], 'userrole' => $params['userrole'], 'password' => $params['password']);
  508. $user->createUser($userdata);
  509. $this->c->flash->addMessage('info', 'Welcome, there is a new user!');
  510. return $response->withRedirect($this->c->router->pathFor('user.list'));
  511. }
  512. $this->c->flash->addMessage('error', 'Please correct your input');
  513. return $response->withRedirect($this->c->router->pathFor('user.new'));
  514. }
  515. }
  516. public function updateUser($request, $response, $args)
  517. {
  518. if($request->isPost())
  519. {
  520. $referer = $request->getHeader('HTTP_REFERER');
  521. $uri = $request->getUri();
  522. $base_url = $uri->getBaseUrl();
  523. # security, users should not be able to fake post with settings from other typemill pages.
  524. if(!isset($referer[0]) OR strpos($referer[0], $base_url . '/tm/user/') === false )
  525. {
  526. $this->c->flash->addMessage('error', 'illegal referer');
  527. return $response->withRedirect($this->c->router->pathFor('user.list'));
  528. }
  529. $params = $request->getParams();
  530. $user = new User();
  531. $userroles = $user->getUserroles();
  532. $validate = new Validation();
  533. /* non admins have different update rights */
  534. if($_SESSION['role'] !== 'administrator')
  535. {
  536. /* if an editor tries to update other userdata than its own */
  537. if($_SESSION['user'] !== $params['username'])
  538. {
  539. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $_SESSION['user']] ));
  540. }
  541. /* non admins cannot change his userrole */
  542. $params['userrole'] = $_SESSION['role'];
  543. }
  544. if($validate->existingUser($params, $userroles))
  545. {
  546. $userdata = array('username' => $params['username'], 'firstname' => $params['firstname'], 'lastname' => $params['lastname'], 'email' => $params['email'], 'userrole' => $params['userrole']);
  547. if(empty($params['password']) AND empty($params['newpassword']))
  548. {
  549. $user->updateUser($userdata);
  550. $this->c->flash->addMessage('info', 'Saved all changes');
  551. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  552. }
  553. elseif($validate->newPassword($params))
  554. {
  555. $userdata['password'] = $params['newpassword'];
  556. $user->updateUser($userdata);
  557. $this->c->flash->addMessage('info', 'Saved all changes');
  558. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  559. }
  560. }
  561. $this->c->flash->addMessage('error', 'Please correct your input');
  562. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  563. }
  564. }
  565. public function deleteUser($request, $response, $args)
  566. {
  567. if($request->isPost())
  568. {
  569. $referer = $request->getHeader('HTTP_REFERER');
  570. $uri = $request->getUri();
  571. $base_url = $uri->getBaseUrl();
  572. # security, users should not be able to fake post with settings from other typemill pages.
  573. if(!isset($referer[0]) OR strpos($referer[0], $base_url . '/tm/user/') === false )
  574. {
  575. $this->c->flash->addMessage('error', 'illegal referer');
  576. return $response->withRedirect($this->c->router->pathFor('user.list'));
  577. }
  578. $params = $request->getParams();
  579. $validate = new Validation();
  580. $user = new User();
  581. /* non admins have different update rights */
  582. if($_SESSION['role'] !== 'administrator')
  583. {
  584. /* if an editor tries to delete other user than its own */
  585. if($_SESSION['user'] !== $params['username'])
  586. {
  587. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $_SESSION['user']] ));
  588. }
  589. }
  590. if($validate->username($params['username']))
  591. {
  592. $user->deleteUser($params['username']);
  593. # if user deleted his own account
  594. if($_SESSION['user'] == $params['username'])
  595. {
  596. session_destroy();
  597. return $response->withRedirect($this->c->router->pathFor('auth.show'));
  598. }
  599. $this->c->flash->addMessage('info', 'Say goodbye, the user is gone!');
  600. return $response->withRedirect($this->c->router->pathFor('user.list'));
  601. }
  602. $this->c->flash->addMessage('error', 'Ups, we did not find that user');
  603. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  604. }
  605. }
  606. private function getThemes()
  607. {
  608. $themeFolder = $this->c->get('settings')['rootPath'] . $this->c->get('settings')['themeFolder'];
  609. $themeFolderC = scandir($themeFolder);
  610. $themes = array();
  611. foreach ($themeFolderC as $key => $theme)
  612. {
  613. if (!in_array($theme, array(".","..")))
  614. {
  615. if (is_dir($themeFolder . DIRECTORY_SEPARATOR . $theme))
  616. {
  617. $themes[] = $theme;
  618. }
  619. }
  620. }
  621. return $themes;
  622. }
  623. private function getCopyright()
  624. {
  625. return array(
  626. "©",
  627. "CC-BY",
  628. "CC-BY-NC",
  629. "CC-BY-NC-ND",
  630. "CC-BY-NC-SA",
  631. "CC-BY-ND",
  632. "CC-BY-SA",
  633. "None"
  634. );
  635. }
  636. private function getLanguages()
  637. {
  638. return array(
  639. 'en' => 'English',
  640. 'ru' => 'Russian',
  641. 'nl' => 'Dutch, Flemish',
  642. 'de' => 'German',
  643. 'it' => 'Italian',
  644. );
  645. }
  646. }