SettingsController.php 27 KB

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