SettingsController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 and return image-fields if they are defined
  320. $imageFields = $this->validateInput('themes', $themeName, $userInput, $validate);
  321. /* set user input as theme settings */
  322. $userSettings['themes'][$themeName] = $userInput;
  323. }
  324. # handle images
  325. $images = $request->getUploadedFiles();
  326. if(!isset($_SESSION['errors']) && isset($images[$themeName]))
  327. {
  328. $userInput = $this->saveImages($imageFields, $userInput, $userSettings, $images[$themeName]);
  329. # set user input as theme settings
  330. $userSettings['themes'][$themeName] = $userInput;
  331. }
  332. /* check for errors and redirect to path, if errors found */
  333. if(isset($_SESSION['errors']))
  334. {
  335. $this->c->flash->addMessage('error', 'Please correct the errors');
  336. return $response->withRedirect($this->c->router->pathFor('themes.show'));
  337. }
  338. /* store updated settings */
  339. \Typemill\Settings::updateSettings($userSettings);
  340. $this->c->flash->addMessage('info', 'Settings are stored');
  341. return $response->withRedirect($this->c->router->pathFor('themes.show'));
  342. }
  343. }
  344. public function savePlugins($request, $response, $args)
  345. {
  346. if($request->isPost())
  347. {
  348. $referer = $request->getHeader('HTTP_REFERER');
  349. $uri = $request->getUri();
  350. $base_url = $uri->getBaseUrl();
  351. # security, users should not be able to fake post with settings from other typemill pages.
  352. if(!isset($referer[0]) OR $referer[0] !== $base_url . '/tm/plugins' )
  353. {
  354. $this->c->flash->addMessage('error', 'illegal referer');
  355. return $response->withRedirect($this->c->router->pathFor('plugins.show'));
  356. }
  357. $userSettings = \Typemill\Settings::getUserSettings();
  358. $pluginSettings = array();
  359. $userInput = $request->getParams();
  360. $validate = new Validation();
  361. /* use the stored user settings and iterate over all original plugin settings, so we do not forget any... */
  362. foreach($userSettings['plugins'] as $pluginName => $pluginUserSettings)
  363. {
  364. /* if there are no input-data for this plugin, then use the stored plugin settings */
  365. if(!isset($userInput[$pluginName]))
  366. {
  367. $pluginSettings[$pluginName] = $pluginUserSettings;
  368. }
  369. else
  370. {
  371. /* validate the user-input */
  372. $imageFields = $this->validateInput('plugins', $pluginName, $userInput[$pluginName], $validate);
  373. /* use the input data */
  374. $pluginSettings[$pluginName] = $userInput[$pluginName];
  375. }
  376. # handle images
  377. $images = $request->getUploadedFiles();
  378. if(!isset($_SESSION['errors']) && isset($images[$pluginName]))
  379. {
  380. $userInput[$pluginName] = $this->saveImages($imageFields, $userInput[$pluginName], $userSettings, $images[$pluginName]);
  381. # set user input as theme settings
  382. $pluginSettings[$pluginName] = $userInput[$pluginName];
  383. }
  384. /* deactivate the plugin, if there is no active flag */
  385. if(!isset($userInput[$pluginName]['active']))
  386. {
  387. $pluginSettings[$pluginName]['active'] = false;
  388. }
  389. }
  390. if(isset($_SESSION['errors']))
  391. {
  392. $this->c->flash->addMessage('error', 'Please correct the errors below');
  393. }
  394. else
  395. {
  396. /* if everything is valid, add plugin settings to base settings again */
  397. $userSettings['plugins'] = $pluginSettings;
  398. /* store updated settings */
  399. \Typemill\Settings::updateSettings($userSettings);
  400. $this->c->flash->addMessage('info', 'Settings are stored');
  401. }
  402. return $response->withRedirect($this->c->router->pathFor('plugins.show'));
  403. }
  404. }
  405. private function validateInput($objectType, $objectName, $userInput, $validate)
  406. {
  407. /* fetch the original settings from the folder (plugin or theme) to get the field definitions */
  408. $originalSettings = \Typemill\Settings::getObjectSettings($objectType, $objectName);
  409. # images get special treatment
  410. $imageFieldDefinitions = array();
  411. if(isset($originalSettings['forms']['fields']))
  412. {
  413. /* flaten the multi-dimensional array with fieldsets to a one-dimensional array */
  414. $originalFields = array();
  415. foreach($originalSettings['forms']['fields'] as $fieldName => $fieldValue)
  416. {
  417. if(isset($fieldValue['fields']))
  418. {
  419. foreach($fieldValue['fields'] as $subFieldName => $subFieldValue)
  420. {
  421. $originalFields[$subFieldName] = $subFieldValue;
  422. }
  423. }
  424. else
  425. {
  426. $originalFields[$fieldName] = $fieldValue;
  427. }
  428. }
  429. # if the plugin defines frontend fields
  430. if(isset($originalSettings['public']))
  431. {
  432. $originalFields['recaptcha'] = ['type' => 'checkbox', 'label' => 'Google Recaptcha', 'checkboxlabel' => 'Activate Recaptcha' ];
  433. $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'];
  434. $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'];
  435. }
  436. # if plugin is not active, then skip required
  437. $skiprequired = false;
  438. if($objectType == 'plugins' && !isset($userInput['active']))
  439. {
  440. $skiprequired = true;
  441. }
  442. /* take the user input data and iterate over all fields and values */
  443. foreach($userInput as $fieldName => $fieldValue)
  444. {
  445. /* get the corresponding field definition from original plugin settings */
  446. $fieldDefinition = isset($originalFields[$fieldName]) ? $originalFields[$fieldName] : false;
  447. if($fieldDefinition)
  448. {
  449. /* validate user input for this field */
  450. $validate->objectField($fieldName, $fieldValue, $objectName, $fieldDefinition, $skiprequired);
  451. if($fieldDefinition['type'] == 'image')
  452. {
  453. # we want to return all images-fields for further processing
  454. $imageFieldDefinitions[$fieldName] = $fieldDefinition;
  455. }
  456. }
  457. if(!$fieldDefinition && $fieldName != 'active')
  458. {
  459. $_SESSION['errors'][$objectName][$fieldName] = array('This field is not defined!');
  460. }
  461. }
  462. }
  463. return $imageFieldDefinitions;
  464. }
  465. protected function saveImages($imageFields, $userInput, $userSettings, $files)
  466. {
  467. # initiate image processor with standard image sizes
  468. $processImages = new ProcessImage($userSettings['images']);
  469. if(!$processImages->checkFolders())
  470. {
  471. $this->c->flash->addMessage('error', 'Please make sure that your media folder exists and is writable.');
  472. return false;
  473. }
  474. foreach($imageFields as $fieldName => $imageField)
  475. {
  476. if(isset($userInput[$fieldName]))
  477. {
  478. # handle single input with single file upload
  479. $image = $files[$fieldName];
  480. if($image->getError() === UPLOAD_ERR_OK)
  481. {
  482. # not the most elegant, but createImage expects a base64-encoded string.
  483. $imageContent = $image->getStream()->getContents();
  484. $imageData = base64_encode($imageContent);
  485. $imageSrc = 'data: ' . $image->getClientMediaType() . ';base64,' . $imageData;
  486. if($processImages->createImage($imageSrc, $image->getClientFilename(), $userSettings['images'], $overwrite = NULL))
  487. {
  488. # returns image path to media library
  489. $userInput[$fieldName] = $processImages->publishImage();
  490. }
  491. }
  492. }
  493. }
  494. return $userInput;
  495. }
  496. /***********************
  497. ** USER MANAGEMENT **
  498. ***********************/
  499. public function showUser($request, $response, $args)
  500. {
  501. if($_SESSION['role'] == 'editor' && $_SESSION['user'] !== $args['username'])
  502. {
  503. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $_SESSION['user']] ));
  504. }
  505. $validate = new Validation();
  506. if($validate->username($args['username']))
  507. {
  508. $user = new User();
  509. $users = $user->getUsers();
  510. $userrole = $user->getUserroles();
  511. $userdata = $user->getUser($args['username']);
  512. $settings = $this->c->get('settings');
  513. if($userdata)
  514. {
  515. return $this->render($response, 'settings/user.twig', array('settings' => $settings, 'users' => $users, 'userdata' => $userdata, 'userrole' => $userrole, 'username' => $args['username'] ));
  516. }
  517. }
  518. $this->c->flash->addMessage('error', 'User does not exists');
  519. return $response->withRedirect($this->c->router->pathFor('user.list'));
  520. }
  521. public function listUser($request, $response)
  522. {
  523. $user = new User();
  524. $users = $user->getUsers();
  525. $userdata = array();
  526. $route = $request->getAttribute('route');
  527. $settings = $this->c->get('settings');
  528. foreach($users as $username)
  529. {
  530. $userdata[] = $user->getUser($username);
  531. }
  532. return $this->render($response, 'settings/userlist.twig', array('settings' => $settings, 'users' => $users, 'userdata' => $userdata, 'route' => $route->getName() ));
  533. }
  534. public function newUser($request, $response, $args)
  535. {
  536. $user = new User();
  537. $users = $user->getUsers();
  538. $userrole = $user->getUserroles();
  539. $route = $request->getAttribute('route');
  540. $settings = $this->c->get('settings');
  541. return $this->render($response, 'settings/usernew.twig', array('settings' => $settings, 'users' => $users, 'userrole' => $userrole, 'route' => $route->getName() ));
  542. }
  543. public function createUser($request, $response, $args)
  544. {
  545. if($request->isPost())
  546. {
  547. $referer = $request->getHeader('HTTP_REFERER');
  548. $uri = $request->getUri();
  549. $base_url = $uri->getBaseUrl();
  550. # security, users should not be able to fake post with settings from other typemill pages.
  551. if(!isset($referer[0]) OR $referer[0] !== $base_url . '/tm/user/new' )
  552. {
  553. $this->c->flash->addMessage('error', 'illegal referer');
  554. return $response->withRedirect($this->c->router->pathFor('user.new'));
  555. }
  556. $params = $request->getParams();
  557. $user = new User();
  558. $userroles = $user->getUserroles();
  559. $validate = new Validation();
  560. if($validate->newUser($params, $userroles))
  561. {
  562. $userdata = array('username' => $params['username'], 'firstname' => $params['firstname'], 'lastname' => $params['lastname'], 'email' => $params['email'], 'userrole' => $params['userrole'], 'password' => $params['password']);
  563. $user->createUser($userdata);
  564. $this->c->flash->addMessage('info', 'Welcome, there is a new user!');
  565. return $response->withRedirect($this->c->router->pathFor('user.list'));
  566. }
  567. $this->c->flash->addMessage('error', 'Please correct your input');
  568. return $response->withRedirect($this->c->router->pathFor('user.new'));
  569. }
  570. }
  571. public function updateUser($request, $response, $args)
  572. {
  573. if($request->isPost())
  574. {
  575. $referer = $request->getHeader('HTTP_REFERER');
  576. $uri = $request->getUri();
  577. $base_url = $uri->getBaseUrl();
  578. # security, users should not be able to fake post with settings from other typemill pages.
  579. if(!isset($referer[0]) OR strpos($referer[0], $base_url . '/tm/user/') === false )
  580. {
  581. $this->c->flash->addMessage('error', 'illegal referer');
  582. return $response->withRedirect($this->c->router->pathFor('user.list'));
  583. }
  584. $params = $request->getParams();
  585. $user = new User();
  586. $userroles = $user->getUserroles();
  587. $validate = new Validation();
  588. /* non admins have different update rights */
  589. if($_SESSION['role'] !== 'administrator')
  590. {
  591. /* if an editor tries to update other userdata than its own */
  592. if($_SESSION['user'] !== $params['username'])
  593. {
  594. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $_SESSION['user']] ));
  595. }
  596. /* non admins cannot change his userrole */
  597. $params['userrole'] = $_SESSION['role'];
  598. }
  599. if($validate->existingUser($params, $userroles))
  600. {
  601. $userdata = array('username' => $params['username'], 'firstname' => $params['firstname'], 'lastname' => $params['lastname'], 'email' => $params['email'], 'userrole' => $params['userrole']);
  602. if(empty($params['password']) AND empty($params['newpassword']))
  603. {
  604. $user->updateUser($userdata);
  605. $this->c->flash->addMessage('info', 'Saved all changes');
  606. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  607. }
  608. elseif($validate->newPassword($params))
  609. {
  610. $userdata['password'] = $params['newpassword'];
  611. $user->updateUser($userdata);
  612. $this->c->flash->addMessage('info', 'Saved all changes');
  613. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  614. }
  615. }
  616. $this->c->flash->addMessage('error', 'Please correct your input');
  617. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  618. }
  619. }
  620. public function deleteUser($request, $response, $args)
  621. {
  622. if($request->isPost())
  623. {
  624. $referer = $request->getHeader('HTTP_REFERER');
  625. $uri = $request->getUri();
  626. $base_url = $uri->getBaseUrl();
  627. # security, users should not be able to fake post with settings from other typemill pages.
  628. if(!isset($referer[0]) OR strpos($referer[0], $base_url . '/tm/user/') === false )
  629. {
  630. $this->c->flash->addMessage('error', 'illegal referer');
  631. return $response->withRedirect($this->c->router->pathFor('user.list'));
  632. }
  633. $params = $request->getParams();
  634. $validate = new Validation();
  635. $user = new User();
  636. /* non admins have different update rights */
  637. if($_SESSION['role'] !== 'administrator')
  638. {
  639. /* if an editor tries to delete other user than its own */
  640. if($_SESSION['user'] !== $params['username'])
  641. {
  642. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $_SESSION['user']] ));
  643. }
  644. }
  645. if($validate->username($params['username']))
  646. {
  647. $user->deleteUser($params['username']);
  648. # if user deleted his own account
  649. if($_SESSION['user'] == $params['username'])
  650. {
  651. session_destroy();
  652. return $response->withRedirect($this->c->router->pathFor('auth.show'));
  653. }
  654. $this->c->flash->addMessage('info', 'Say goodbye, the user is gone!');
  655. return $response->withRedirect($this->c->router->pathFor('user.list'));
  656. }
  657. $this->c->flash->addMessage('error', 'Ups, we did not find that user');
  658. return $response->withRedirect($this->c->router->pathFor('user.show', ['username' => $params['username']]));
  659. }
  660. }
  661. private function getThemes()
  662. {
  663. $themeFolder = $this->c->get('settings')['rootPath'] . $this->c->get('settings')['themeFolder'];
  664. $themeFolderC = scandir($themeFolder);
  665. $themes = array();
  666. foreach ($themeFolderC as $key => $theme)
  667. {
  668. if (!in_array($theme, array(".","..")))
  669. {
  670. if (is_dir($themeFolder . DIRECTORY_SEPARATOR . $theme))
  671. {
  672. $themes[] = $theme;
  673. }
  674. }
  675. }
  676. return $themes;
  677. }
  678. private function getCopyright()
  679. {
  680. return array(
  681. "©",
  682. "CC-BY",
  683. "CC-BY-NC",
  684. "CC-BY-NC-ND",
  685. "CC-BY-NC-SA",
  686. "CC-BY-ND",
  687. "CC-BY-SA",
  688. "None"
  689. );
  690. }
  691. private function getLanguages()
  692. {
  693. return array(
  694. 'en' => 'English',
  695. 'ru' => 'Russian',
  696. 'nl' => 'Dutch, Flemish',
  697. 'de' => 'German',
  698. 'it' => 'Italian',
  699. 'fr' => 'French',
  700. );
  701. }
  702. }