SettingsController.php 27 KB

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