SetupController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. namespace Typemill\Controllers;
  3. use \Symfony\Component\Yaml\Yaml;
  4. use Typemill\Models\Field;
  5. use Typemill\Models\Validation;
  6. class SetupController extends Controller
  7. {
  8. public function setup($request, $response, $args)
  9. {
  10. $settings = $this->c->get('settings');
  11. $themes = $this->getThemes();
  12. $copyright = $this->getCopyright();
  13. $languages = $this->getLanguages();
  14. $locale = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
  15. $locale = $locale[0];
  16. $plugins = array();
  17. $fields = array();
  18. /* iterate through the plugins in the stored user settings */
  19. foreach($settings['plugins'] as $pluginName => $pluginUserSettings)
  20. {
  21. /* add plugin to plugin Data */
  22. $plugins[$pluginName] = Null;
  23. /* Check if the user has deleted a plugin. Then delete it in the settings and store the updated settings. */
  24. if(!is_dir($settings['rootPath'] . 'plugins' . DIRECTORY_SEPARATOR . $pluginName))
  25. {
  26. /* remove the plugin settings and store updated settings */
  27. \Typemill\Settings::removePluginSettings($pluginName);
  28. continue;
  29. }
  30. /* load the original plugin definitions from the plugin folder (author, version and stuff) */
  31. $pluginOriginalSettings = \Typemill\Settings::getPluginSettings($pluginName);
  32. if($pluginOriginalSettings)
  33. {
  34. /* store them as default plugin data with plugin author, plugin year, default settings and field-definitions */
  35. $plugins[$pluginName] = $pluginOriginalSettings;
  36. }
  37. /* overwrite the original plugin settings with the stored user settings, if they exist */
  38. if($pluginUserSettings)
  39. {
  40. $plugins[$pluginName]['settings'] = $pluginUserSettings;
  41. }
  42. /* check, if the plugin has been disabled in the form-session-data */
  43. /* TODO: Works only, if there is at least one plugin with settings */
  44. if(isset($_SESSION['old']) && !isset($_SESSION['old'][$pluginName]['active']))
  45. {
  46. $plugins[$pluginName]['settings']['active'] = false;
  47. }
  48. /* if the plugin defines forms and fields, so that the user can edit the plugin settings in the frontend */
  49. if(isset($pluginOriginalSettings['forms']))
  50. {
  51. $fields = array();
  52. /* then iterate through the fields */
  53. foreach($pluginOriginalSettings['forms']['fields'] as $fieldName => $fieldConfigs)
  54. {
  55. /* and create a new field object with the field name and the field configurations. */
  56. $field = new Field($fieldName, $fieldConfigs);
  57. /* now you have the configurations of the field. Time to set the values */
  58. /* At first, get the value for the field from the stored user settings */
  59. // $userValue = isset($pluginUserSettings[$fieldName]) ? $pluginUserSettings[$fieldName] : NULL;
  60. $userValue = isset($plugins[$pluginName]['settings'][$fieldName]) ? $plugins[$pluginName]['settings'][$fieldName] : NULL;
  61. /* Then overwrite the value, if there are old input values for the field in the session */
  62. $userValue = isset($_SESSION['old'][$pluginName][$fieldName]) ? $_SESSION['old'][$pluginName][$fieldName] : $userValue;
  63. if($field->getType() == "textarea")
  64. {
  65. if($userValue)
  66. {
  67. $field->setContent($userValue);
  68. }
  69. }
  70. elseIf($field->getType() != "checkbox")
  71. {
  72. $field->setAttributeValue('value', $userValue);
  73. }
  74. /* add the field to the field-List with the plugin-name as key */
  75. $fields[] = $field;
  76. }
  77. /* overwrite original plugin form definitions with enhanced form objects */
  78. $plugins[$pluginName]['forms']['fields'] = $fields;
  79. }
  80. }
  81. $this->c->view->render($response, '/setup.twig', array('settings' => $settings, 'themes' => $themes,'copyright' => $copyright,'plugins' => $plugins, 'languages' => $languages, 'locale' => $locale));
  82. }
  83. public function save($request, $response, $args)
  84. {
  85. if($request->isPost())
  86. {
  87. $settings = $this->c->get('settings');
  88. $pluginSettings = array();
  89. $params = $request->getParams();
  90. $validate = new Validation();
  91. /* extract the settings for the basic application and validate them */
  92. $appSettings = isset($params['settings']) ? $params['settings'] : false;
  93. if($appSettings)
  94. {
  95. $copyright = $this->getCopyright();
  96. $themes = $this->getThemes();
  97. $appSettings['startpage'] = isset($appSettings['startpage']) ? true : false;
  98. $validate->settings($appSettings, $themes, $copyright, 'settings');
  99. }
  100. $themeSettings = isset($params['themesettings']) ? $params['themesettings'] : false;
  101. if($themeSettings)
  102. {
  103. // load theme definitions by theme name
  104. $themeOriginalSettings = \Typemill\Settings::getThemeSettings($appSettings['theme']);
  105. // validate input with field definitions
  106. if($themeOriginalSettings)
  107. {
  108. foreach($themeSettings as $fieldName => $fieldValue)
  109. {
  110. $fieldDefinition = isset($themeOriginalSettings['forms']['fields'][$fieldName]) ? $themeOriginalSettings['forms']['fields'][$fieldName] : false;
  111. if($fieldDefinition)
  112. {
  113. /* validate user input for this field */
  114. $validate->pluginField($fieldName, $fieldValue, $appSettings['theme'], $fieldDefinition);
  115. }
  116. }
  117. }
  118. $appSettings['themesettings'] = $themeSettings;
  119. }
  120. /* use the stored user settings and iterate over all original plugin settings, so we do not forget any... */
  121. foreach($settings['plugins'] as $pluginName => $pluginUserSettings)
  122. {
  123. /* if there are no input-data for this plugin, then use the stored plugin settings */
  124. if(!isset($params[$pluginName]))
  125. {
  126. $pluginSettings[$pluginName] = $pluginUserSettings;
  127. }
  128. else
  129. {
  130. /* now fetch the original plugin settings from the plugin folder to get the field definitions */
  131. $pluginOriginalSettings = \Typemill\Settings::getPluginSettings($pluginName);
  132. if($pluginOriginalSettings)
  133. {
  134. /* take the user input data and iterate over all fields and values */
  135. foreach($params[$pluginName] as $fieldName => $fieldValue)
  136. {
  137. /* get the corresponding field definition from original plugin settings */
  138. $fieldDefinition = isset($pluginOriginalSettings['forms']['fields'][$fieldName]) ? $pluginOriginalSettings['forms']['fields'][$fieldName] : false;
  139. if($fieldDefinition)
  140. {
  141. /* validate user input for this field */
  142. $validate->pluginField($fieldName, $fieldValue, $pluginName, $fieldDefinition);
  143. }
  144. }
  145. }
  146. /* use the input data */
  147. $pluginSettings[$pluginName] = $params[$pluginName];
  148. }
  149. /* deactivate the plugin, if there is no active flag */
  150. if(!isset($params[$pluginName]['active']))
  151. {
  152. $pluginSettings[$pluginName]['active'] = false;
  153. }
  154. }
  155. if(!is_writable($this->c->get('settings')['settingsPath']))
  156. {
  157. $_SESSION['errors']['folder'] = 'Your settings-folder is not writable';
  158. }
  159. if(isset($_SESSION['errors']))
  160. {
  161. return $response->withRedirect($this->c->router->pathFor('setup'));
  162. }
  163. /* if everything is valid, add plugin settings to base settings again */
  164. $appSettings['plugins'] = $pluginSettings;
  165. /* store updated settings */
  166. \Typemill\Settings::updateSettings($appSettings);
  167. unset($_SESSION['old']);
  168. $this->c->view->render($response, '/welcome.twig', $appSettings);
  169. }
  170. }
  171. private function getCopyright()
  172. {
  173. return array(
  174. "©",
  175. "CC-BY",
  176. "CC-BY-NC",
  177. "CC-BY-NC-ND",
  178. "CC-BY-NC-SA",
  179. "CC-BY-ND",
  180. "CC-BY-SA",
  181. "None"
  182. );
  183. }
  184. private function getLanguages()
  185. {
  186. return array(
  187. 'ab' => 'Abkhazian',
  188. 'aa' => 'Afar',
  189. 'af' => 'Afrikaans',
  190. 'ak' => 'Akan',
  191. 'sq' => 'Albanian',
  192. 'am' => 'Amharic',
  193. 'ar' => 'Arabic',
  194. 'an' => 'Aragonese',
  195. 'hy' => 'Armenian',
  196. 'as' => 'Assamese',
  197. 'av' => 'Avaric',
  198. 'ae' => 'Avestan',
  199. 'ay' => 'Aymara',
  200. 'az' => 'Azerbaijani',
  201. 'bm' => 'Bambara',
  202. 'ba' => 'Bashkir',
  203. 'eu' => 'Basque',
  204. 'be' => 'Belarusian',
  205. 'bn' => 'Bengali',
  206. 'bh' => 'Bihari languages',
  207. 'bi' => 'Bislama',
  208. 'bs' => 'Bosnian',
  209. 'br' => 'Breton',
  210. 'bg' => 'Bulgarian',
  211. 'my' => 'Burmese',
  212. 'ca' => 'Catalan, Valencian',
  213. 'km' => 'Central Khmer',
  214. 'ch' => 'Chamorro',
  215. 'ce' => 'Chechen',
  216. 'ny' => 'Chichewa, Chewa, Nyanja',
  217. 'zh' => 'Chinese',
  218. 'cu' => 'Church Slavonic, Old Bulgarian, Old Church Slavonic',
  219. 'cv' => 'Chuvash',
  220. 'kw' => 'Cornish',
  221. 'co' => 'Corsican',
  222. 'cr' => 'Cree',
  223. 'hr' => 'Croatian',
  224. 'cs' => 'Czech',
  225. 'da' => 'Danish',
  226. 'dv' => 'Divehi, Dhivehi, Maldivian',
  227. 'nl' => 'Dutch, Flemish',
  228. 'dz' => 'Dzongkha',
  229. 'en' => 'English',
  230. 'eo' => 'Esperanto',
  231. 'et' => 'Estonian',
  232. 'ee' => 'Ewe',
  233. 'fo' => 'Faroese',
  234. 'fj' => 'Fijian',
  235. 'fi' => 'Finnish',
  236. 'fr' => 'French',
  237. 'ff' => 'Fulah',
  238. 'gd' => 'Gaelic, Scottish Gaelic',
  239. 'gl' => 'Galician',
  240. 'lg' => 'Ganda',
  241. 'ka' => 'Georgian',
  242. 'de' => 'German',
  243. 'ki' => 'Gikuyu, Kikuyu',
  244. 'el' => 'Greek (Modern)',
  245. 'kl' => 'Greenlandic, Kalaallisut',
  246. 'gn' => 'Guarani',
  247. 'gu' => 'Gujarati',
  248. 'ht' => 'Haitian, Haitian Creole',
  249. 'ha' => 'Hausa',
  250. 'he' => 'Hebrew',
  251. 'hz' => 'Herero',
  252. 'hi' => 'Hindi',
  253. 'ho' => 'Hiri Motu',
  254. 'hu' => 'Hungarian',
  255. 'is' => 'Icelandic',
  256. 'io' => 'Ido',
  257. 'ig' => 'Igbo',
  258. 'id' => 'Indonesian',
  259. 'ia' => 'Interlingua (International Auxiliary Language Association)',
  260. 'ie' => 'Interlingue',
  261. 'iu' => 'Inuktitut',
  262. 'ik' => 'Inupiaq',
  263. 'ga' => 'Irish',
  264. 'it' => 'Italian',
  265. 'ja' => 'Japanese',
  266. 'jv' => 'Javanese',
  267. 'kn' => 'Kannada',
  268. 'kr' => 'Kanuri',
  269. 'ks' => 'Kashmiri',
  270. 'kk' => 'Kazakh',
  271. 'rw' => 'Kinyarwanda',
  272. 'kv' => 'Komi',
  273. 'kg' => 'Kongo',
  274. 'ko' => 'Korean',
  275. 'kj' => 'Kwanyama, Kuanyama',
  276. 'ku' => 'Kurdish',
  277. 'ky' => 'Kyrgyz',
  278. 'lo' => 'Lao',
  279. 'la' => 'Latin',
  280. 'lv' => 'Latvian',
  281. 'lb' => 'Letzeburgesch, Luxembourgish',
  282. 'li' => 'Limburgish, Limburgan, Limburger',
  283. 'ln' => 'Lingala',
  284. 'lt' => 'Lithuanian',
  285. 'lu' => 'Luba-Katanga',
  286. 'mk' => 'Macedonian',
  287. 'mg' => 'Malagasy',
  288. 'ms' => 'Malay',
  289. 'ml' => 'Malayalam',
  290. 'mt' => 'Maltese',
  291. 'gv' => 'Manx',
  292. 'mi' => 'Maori',
  293. 'mr' => 'Marathi',
  294. 'mh' => 'Marshallese',
  295. 'ro' => 'Moldovan, Moldavian, Romanian',
  296. 'mn' => 'Mongolian',
  297. 'na' => 'Nauru',
  298. 'nv' => 'Navajo, Navaho',
  299. 'nd' => 'Northern Ndebele',
  300. 'ng' => 'Ndonga',
  301. 'ne' => 'Nepali',
  302. 'se' => 'Northern Sami',
  303. 'no' => 'Norwegian',
  304. 'nb' => 'Norwegian Bokmål',
  305. 'nn' => 'Norwegian Nynorsk',
  306. 'ii' => 'Nuosu, Sichuan Yi',
  307. 'oc' => 'Occitan (post 1500)',
  308. 'oj' => 'Ojibwa',
  309. 'or' => 'Oriya',
  310. 'om' => 'Oromo',
  311. 'os' => 'Ossetian, Ossetic',
  312. 'pi' => 'Pali',
  313. 'pa' => 'Panjabi, Punjabi',
  314. 'ps' => 'Pashto, Pushto',
  315. 'fa' => 'Persian',
  316. 'pl' => 'Polish',
  317. 'pt' => 'Portuguese',
  318. 'qu' => 'Quechua',
  319. 'rm' => 'Romansh',
  320. 'rn' => 'Rundi',
  321. 'ru' => 'Russian',
  322. 'sm' => 'Samoan',
  323. 'sg' => 'Sango',
  324. 'sa' => 'Sanskrit',
  325. 'sc' => 'Sardinian',
  326. 'sr' => 'Serbian',
  327. 'sn' => 'Shona',
  328. 'sd' => 'Sindhi',
  329. 'si' => 'Sinhala, Sinhalese',
  330. 'sk' => 'Slovak',
  331. 'sl' => 'Slovenian',
  332. 'so' => 'Somali',
  333. 'st' => 'Sotho, Southern',
  334. 'nr' => 'South Ndebele',
  335. 'es' => 'Spanish, Castilian',
  336. 'su' => 'Sundanese',
  337. 'sw' => 'Swahili',
  338. 'ss' => 'Swati',
  339. 'sv' => 'Swedish',
  340. 'tl' => 'Tagalog',
  341. 'ty' => 'Tahitian',
  342. 'tg' => 'Tajik',
  343. 'ta' => 'Tamil',
  344. 'tt' => 'Tatar',
  345. 'te' => 'Telugu',
  346. 'th' => 'Thai',
  347. 'bo' => 'Tibetan',
  348. 'ti' => 'Tigrinya',
  349. 'to' => 'Tonga (Tonga Islands)',
  350. 'ts' => 'Tsonga',
  351. 'tn' => 'Tswana',
  352. 'tr' => 'Turkish',
  353. 'tk' => 'Turkmen',
  354. 'tw' => 'Twi',
  355. 'ug' => 'Uighur, Uyghur',
  356. 'uk' => 'Ukrainian',
  357. 'ur' => 'Urdu',
  358. 'uz' => 'Uzbek',
  359. 've' => 'Venda',
  360. 'vi' => 'Vietnamese',
  361. 'vo' => 'Volap_k',
  362. 'wa' => 'Walloon',
  363. 'cy' => 'Welsh',
  364. 'fy' => 'Western Frisian',
  365. 'wo' => 'Wolof',
  366. 'xh' => 'Xhosa',
  367. 'yi' => 'Yiddish',
  368. 'yo' => 'Yoruba',
  369. 'za' => 'Zhuang, Chuang',
  370. 'zu' => 'Zulu'
  371. );
  372. }
  373. private function getThemes()
  374. {
  375. $themeFolder = $this->c->get('settings')['rootPath'] . $this->c->get('settings')['themeFolder'];
  376. $themeFolderC = scandir($themeFolder);
  377. $themes = array();
  378. foreach ($themeFolderC as $key => $theme)
  379. {
  380. if (!in_array($theme, array(".","..")))
  381. {
  382. if (is_dir($themeFolder . DIRECTORY_SEPARATOR . $theme))
  383. {
  384. $themes[] = $theme;
  385. }
  386. }
  387. }
  388. return $themes;
  389. }
  390. public function themes($request, $response)
  391. {
  392. /* Extract the parameters from get-call */
  393. $params = $request->getParams();
  394. $theme = isset($params['theme']) ? $params['theme'] : false;
  395. if($theme && preg_match('/^[A-Za-z0-9 _\-\+]+$/', $theme))
  396. {
  397. $themeSettings = \Typemill\Settings::getThemeSettings($theme);
  398. if($themeSettings)
  399. {
  400. return $response->withJson($themeSettings, 200);
  401. }
  402. }
  403. return $response->withJson(['error' => 'no data found'], 404);
  404. }
  405. }