settings.lib.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Library of the settings.php file
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @author Guillaume Viguier <guillaume@viguierjust.com>
  8. *
  9. * @since Chamilo 1.8.7
  10. * @package chamilo.admin
  11. */
  12. define('CSS_UPLOAD_PATH', api_get_path(SYS_APP_PATH) . 'Resources/public/css/themes/');
  13. use Symfony\Component\Filesystem\Filesystem;
  14. /**
  15. * This function allows easy activating and inactivating of regions
  16. * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  17. */
  18. function handle_regions()
  19. {
  20. if (isset($_POST['submit_plugins'])) {
  21. store_regions();
  22. // Add event to the system log.
  23. $user_id = api_get_user_id();
  24. $category = $_GET['category'];
  25. Event::addEvent(
  26. LOG_CONFIGURATION_SETTINGS_CHANGE,
  27. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  28. $category,
  29. api_get_utc_datetime(),
  30. $user_id
  31. );
  32. Display:: display_confirmation_message(get_lang('SettingsStored'));
  33. }
  34. $plugin_obj = new AppPlugin();
  35. $possible_plugins = $plugin_obj->read_plugins_from_path();
  36. $installed_plugins = $plugin_obj->get_installed_plugins();
  37. echo '<form name="plugins" method="post" action="' . api_get_self() . '?category=' . Security::remove_XSS($_GET['category']) . '">';
  38. echo '<table class="data_table">';
  39. echo '<tr>';
  40. echo '<th width="400px">';
  41. echo get_lang('Plugin');
  42. echo '</th><th>';
  43. echo get_lang('Regions');
  44. echo '</th>';
  45. echo '</th>';
  46. echo '</tr>';
  47. /* We display all the possible plugins and the checkboxes */
  48. $plugin_region_list = array();
  49. $my_plugin_list = $plugin_obj->get_plugin_regions();
  50. foreach ($my_plugin_list as $plugin_item) {
  51. $plugin_region_list[$plugin_item] = $plugin_item;
  52. }
  53. // Removing course tool
  54. unset($plugin_region_list['course_tool_plugin']);
  55. foreach ($installed_plugins as $plugin) {
  56. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH) . $plugin . '/plugin.php';
  57. if (file_exists($plugin_info_file)) {
  58. $plugin_info = array();
  59. require $plugin_info_file;
  60. if (isset($_GET['name']) && $_GET['name'] == $plugin) {
  61. echo '<tr class="row_selected">';
  62. } else {
  63. echo '<tr>';
  64. }
  65. echo '<td>';
  66. echo '<h4>' . $plugin_info['title'] . ' <small>v' . $plugin_info['version'] . '</small></h4>';
  67. echo '<p>' . $plugin_info['comment'] . '</p>';
  68. echo '</td><td>';
  69. $selected_plugins = $plugin_obj->get_areas_by_plugin($plugin);
  70. $region_list = [];
  71. $isAdminPlugin = isset($plugin_info['is_admin_plugin']) && $plugin_info['is_admin_plugin'];
  72. $isCoursePlugin = isset($plugin_info['is_course_plugin']) && $plugin_info['is_course_plugin'];
  73. if (!$isAdminPlugin && !$isCoursePlugin) {
  74. $region_list = $plugin_region_list;
  75. } else {
  76. if ($isAdminPlugin) {
  77. $region_list['menu_administrator'] = 'menu_administrator';
  78. }
  79. if ($isCoursePlugin) {
  80. $region_list['course_tool_plugin'] = 'course_tool_plugin';
  81. }
  82. }
  83. echo Display::select('plugin_' . $plugin . '[]', $region_list, $selected_plugins,
  84. array('multiple' => 'multiple', 'style' => 'width:500px'), true, get_lang('None'));
  85. echo '</td></tr>';
  86. }
  87. }
  88. echo '</table>';
  89. echo '<br />';
  90. echo '<button class="btn btn-success" type="submit" name="submit_plugins">' . get_lang('EnablePlugins') . '</button></form>';
  91. }
  92. function handle_extensions()
  93. {
  94. echo Display::page_subheader(get_lang('ConfigureExtensions'));
  95. echo '<a class="btn btn-success" href="configure_extensions.php?display=ppt2lp" role="button">' . get_lang('Ppt2lp') . '</a>';
  96. }
  97. /**
  98. * This function allows easy activating and inactivating of plugins
  99. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  100. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  101. * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  102. */
  103. function handle_plugins()
  104. {
  105. $plugin_obj = new AppPlugin();
  106. $token = Security::get_token();
  107. if (isset($_POST['submit_plugins'])) {
  108. store_plugins();
  109. // Add event to the system log.
  110. $user_id = api_get_user_id();
  111. $category = $_GET['category'];
  112. Event::addEvent(
  113. LOG_CONFIGURATION_SETTINGS_CHANGE,
  114. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  115. $category,
  116. api_get_utc_datetime(),
  117. $user_id
  118. );
  119. Display:: display_confirmation_message(get_lang('SettingsStored'));
  120. }
  121. $all_plugins = $plugin_obj->read_plugins_from_path();
  122. $installed_plugins = $plugin_obj->get_installed_plugins();
  123. //Plugins NOT installed
  124. echo Display::page_subheader(get_lang('Plugins'));
  125. echo '<form class="form-horizontal" name="plugins" method="post" action="' . api_get_self() . '?category=' . Security::remove_XSS($_GET['category']) . '&sec_token=' . $token . '">';
  126. echo '<table class="data_table">';
  127. echo '<tr>';
  128. echo '<th width="20px">';
  129. echo get_lang('Action');
  130. echo '</th><th>';
  131. echo get_lang('Description');
  132. echo '</th>';
  133. echo '</tr>';
  134. $plugin_list = array();
  135. $my_plugin_list = $plugin_obj->get_plugin_regions();
  136. foreach ($my_plugin_list as $plugin_item) {
  137. $plugin_list[$plugin_item] = $plugin_item;
  138. }
  139. foreach ($all_plugins as $plugin) {
  140. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH) . $plugin . '/plugin.php';
  141. if (file_exists($plugin_info_file)) {
  142. $plugin_info = array();
  143. require $plugin_info_file;
  144. if (in_array($plugin, $installed_plugins)) {
  145. echo '<tr class="row_selected">';
  146. } else {
  147. echo '<tr>';
  148. }
  149. echo '<td>';
  150. //Checkbox
  151. if (in_array($plugin, $installed_plugins)) {
  152. echo '<input type="checkbox" name="plugin_' . $plugin . '[]" checked="checked">';
  153. } else {
  154. echo '<input type="checkbox" name="plugin_' . $plugin . '[]">';
  155. }
  156. echo '</td><td>';
  157. echo '<h4>' . $plugin_info['title'] . ' <small>v ' . $plugin_info['version'] . '</small></h4>';
  158. echo '<p>' . $plugin_info['comment'] . '</p>';
  159. echo '<p>' . get_lang('Author') . ': ' . $plugin_info['author'] . '</p>';
  160. echo '<div class="btn-group">';
  161. if (in_array($plugin, $installed_plugins)) {
  162. echo Display::url('<em class="fa fa-cogs"></em> ' . get_lang('Configure'),
  163. 'configure_plugin.php?name=' . $plugin, array('class' => 'btn btn-default'));
  164. echo Display::url('<em class="fa fa-th-large"></em> ' . get_lang('Regions'),
  165. 'settings.php?category=Regions&name=' . $plugin, array('class' => 'btn btn-default'));
  166. }
  167. if (file_exists(api_get_path(SYS_PLUGIN_PATH) . $plugin . '/readme.txt')) {
  168. echo Display::url(
  169. "<em class='fa fa-file-text-o'></em> readme.txt",
  170. api_get_path(WEB_PLUGIN_PATH) . $plugin . "/readme.txt",
  171. [
  172. 'class' => 'btn btn-default ajax',
  173. 'data-title' => $plugin_info['title'],
  174. 'data-size' => 'lg',
  175. '_target' => '_blank'
  176. ]
  177. );
  178. }
  179. echo '</div>';
  180. echo '</td></tr>';
  181. }
  182. }
  183. echo '</table>';
  184. echo '<div class="form-actions bottom_actions">';
  185. echo '<button class="btn btn-success" type="submit" name="submit_plugins">' .
  186. get_lang('EnablePlugins') . '</button>';
  187. echo '</div>';
  188. echo '</form>';
  189. }
  190. /**
  191. * This function allows the platform admin to choose the default stylesheet
  192. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  193. * @author Julio Montoya <gugli100@gmail.com>, Chamilo
  194. */
  195. function handle_stylesheets()
  196. {
  197. global $_configuration;
  198. // Current style.
  199. $currentstyle = api_get_setting('stylesheets');
  200. $is_style_changeable = false;
  201. if ($_configuration['access_url'] != 1) {
  202. $style_info = api_get_settings('stylesheets', '', 1, 0);
  203. $url_info = api_get_access_url($_configuration['access_url']);
  204. if ($style_info[0]['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  205. $is_style_changeable = true;
  206. }
  207. } else {
  208. $is_style_changeable = true;
  209. }
  210. $form = new FormValidator(
  211. 'stylesheet_upload',
  212. 'post',
  213. 'settings.php?category=Stylesheets#tabs-3'
  214. );
  215. $form->addElement('text', 'name_stylesheet', get_lang('NameStylesheet'),
  216. array('size' => '40', 'maxlength' => '40'));
  217. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  218. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  219. $allowed_file_types = array(
  220. 'css',
  221. 'zip',
  222. 'jpeg',
  223. 'jpg',
  224. 'png',
  225. 'gif',
  226. 'ico',
  227. 'psd',
  228. 'xcf',
  229. 'svg',
  230. 'webp',
  231. 'woff',
  232. 'woff2'
  233. );
  234. $form->addRule('new_stylesheet', get_lang('InvalidExtension') . ' (' . implode(',', $allowed_file_types) . ')',
  235. 'filetype', $allowed_file_types);
  236. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  237. $form->addButtonUpload(get_lang('Upload'), 'stylesheet_upload');
  238. $show_upload_form = false;
  239. if (!is_writable(CSS_UPLOAD_PATH)) {
  240. Display::display_error_message(CSS_UPLOAD_PATH . get_lang('IsNotWritable'));
  241. } else {
  242. // Uploading a new stylesheet.
  243. if ($_configuration['access_url'] == 1) {
  244. $show_upload_form = true;
  245. } else {
  246. if ($is_style_changeable) {
  247. $show_upload_form = true;
  248. }
  249. }
  250. }
  251. // Stylesheet upload.
  252. if (isset($_POST['stylesheet_upload'])) {
  253. if ($form->validate()) {
  254. $values = $form->exportValues();
  255. $picture_element = $form->getElement('new_stylesheet');
  256. $picture = $picture_element->getValue();
  257. $result = upload_stylesheet($values, $picture);
  258. // Add event to the system log.
  259. $user_id = api_get_user_id();
  260. $category = $_GET['category'];
  261. Event::addEvent(
  262. LOG_CONFIGURATION_SETTINGS_CHANGE,
  263. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  264. $category,
  265. api_get_utc_datetime(),
  266. $user_id
  267. );
  268. if ($result) {
  269. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  270. }
  271. }
  272. }
  273. $form_change = new FormValidator(
  274. 'stylesheet_upload',
  275. 'post',
  276. api_get_self() . '?category=Stylesheets',
  277. null,
  278. array('id' => 'stylesheets_id')
  279. );
  280. $list_of_names = array();
  281. $selected = '';
  282. $dirpath = '';
  283. $safe_style_dir = '';
  284. if ($handle = @opendir(CSS_UPLOAD_PATH)) {
  285. $counter = 1;
  286. while (false !== ($style_dir = readdir($handle))) {
  287. if (substr($style_dir, 0, 1) == '.') {
  288. // Skip directories starting with a '.'
  289. continue;
  290. }
  291. $dirpath = CSS_UPLOAD_PATH . $style_dir;
  292. if (is_dir($dirpath)) {
  293. if ($style_dir != '.' && $style_dir != '..') {
  294. if (isset($_POST['style']) &&
  295. (isset($_POST['preview']) || isset($_POST['download'])) &&
  296. $_POST['style'] == $style_dir
  297. ) {
  298. $safe_style_dir = $style_dir;
  299. } else {
  300. if ($currentstyle == $style_dir || ($style_dir == 'chamilo' && !$currentstyle)) {
  301. if (isset($_POST['style'])) {
  302. $selected = Database::escape_string($_POST['style']);
  303. } else {
  304. $selected = $style_dir;
  305. }
  306. }
  307. }
  308. $show_name = ucwords(str_replace('_', ' ', $style_dir));
  309. if ($is_style_changeable) {
  310. $list_of_names[$style_dir] = $show_name;
  311. }
  312. $counter++;
  313. }
  314. }
  315. }
  316. closedir($handle);
  317. }
  318. // Sort styles in alphabetical order.
  319. asort($list_of_names);
  320. $select_list = array();
  321. foreach ($list_of_names as $style_dir => $item) {
  322. $select_list[$style_dir] = $item;
  323. }
  324. $styles = &$form_change->addElement('select', 'style', get_lang('NameStylesheet'), $select_list);
  325. $styles->setSelected($selected);
  326. if ($form_change->validate()) {
  327. // Submit stylesheets.
  328. if (isset($_POST['save'])) {
  329. store_stylesheets();
  330. Display::display_normal_message(get_lang('Saved'));
  331. }
  332. if (isset($_POST['download'])) {
  333. $arch = api_get_path(SYS_ARCHIVE_PATH) . $safe_style_dir . '.zip';
  334. $dir = api_get_path(SYS_CSS_PATH) . 'themes/' . $safe_style_dir;
  335. if (is_dir($dir)) {
  336. $zip = new PclZip($arch);
  337. // Remove path prefix except the style name and put file on disk
  338. $zip->create($dir, PCLZIP_OPT_REMOVE_PATH, substr($dir, 0, -strlen($safe_style_dir)));
  339. //@TODO: use more generic script to download.
  340. $str = '<a class="btn btn-primary btn-large" href="' . api_get_path(WEB_CODE_PATH) . 'course_info/download.php?archive=' . str_replace(api_get_path(SYS_ARCHIVE_PATH),
  341. '', $arch) . '">' . get_lang('ClickHereToDownloadTheFile') . '</a>';
  342. Display::display_normal_message($str, false);
  343. } else {
  344. Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
  345. }
  346. }
  347. }
  348. $logoForm = new FormValidator(
  349. 'logo_upload',
  350. 'post',
  351. 'settings.php?category=Stylesheets#tabs-2'
  352. );
  353. $logoForm->addHtml(Display::return_message(sprintf(get_lang('TheLogoMustBeSizeXAndFormatY'), '250 x 70', 'PNG'),
  354. 'info'));
  355. $dir = api_get_path(SYS_PUBLIC_PATH) . 'css/themes/' . $selected . '/images/';
  356. $url = api_get_path(WEB_CSS_PATH) . 'themes/' . $selected . '/images/';
  357. $logoFileName = 'header-logo.png';
  358. $newLogoFileName = 'header-logo-custom.png';
  359. if (is_file($dir . $newLogoFileName)) {
  360. $logoForm->addLabel(get_lang('CurrentLogo'),
  361. '<img id="header-logo-custom" src="' . $url . $newLogoFileName . '?' . time() . '">');
  362. } else {
  363. $logoForm->addLabel(get_lang('CurrentLogo'),
  364. '<img id="header-logo-custom" src="' . $url . $logoFileName . '?' . time() . '">');
  365. }
  366. $logoForm->addFile('new_logo', get_lang('UpdateLogo'));
  367. $allowedFileTypes = ['png'];
  368. if (isset($_POST['logo_reset'])) {
  369. if (is_file($dir . $newLogoFileName)) {
  370. unlink($dir . $newLogoFileName);
  371. Display::display_normal_message(get_lang('ResetToTheOriginalLogo'));
  372. echo '<script>'
  373. . '$("#header-logo").attr("src","' . $url . $logoFileName . '");'
  374. . '</script>';
  375. }
  376. } elseif (isset($_POST['logo_upload'])) {
  377. $logoForm->addRule('new_logo', get_lang('InvalidExtension') . ' (' . implode(',', $allowedFileTypes) . ')',
  378. 'filetype', $allowedFileTypes);
  379. $logoForm->addRule('new_logo', get_lang('ThisFieldIsRequired'), 'required');
  380. if ($logoForm->validate()) {
  381. $imageInfo = getimagesize($_FILES['new_logo']['tmp_name']);
  382. $width = $imageInfo[0];
  383. $height = $imageInfo[1];
  384. if ($width <= 250 && $height <= 70) {
  385. if (is_file($dir . $newLogoFileName)) {
  386. unlink($dir . $newLogoFileName);
  387. }
  388. $status = move_uploaded_file($_FILES['new_logo']['tmp_name'], $dir . $newLogoFileName);
  389. if ($status) {
  390. Display::display_normal_message(get_lang('NewLogoUpdated'));
  391. echo '<script>'
  392. . '$("#header-logo").attr("src","' . $url . $newLogoFileName . '");'
  393. . '</script>';
  394. } else {
  395. Display::display_error_message('Error - ' . get_lang('UplNoFileUploaded'));
  396. }
  397. } else {
  398. Display::display_error_message('Error - ' . get_lang('InvalidImageDimensions'));
  399. }
  400. }
  401. }
  402. if ($is_style_changeable) {
  403. $group = [
  404. $form_change->addButtonSave(get_lang('SaveSettings'), 'save', true),
  405. $form_change->addButtonPreview(get_lang('Preview'), 'preview', true),
  406. $form_change->addButtonDownload(get_lang('Download'), 'download', true)
  407. ];
  408. $form_change->addGroup($group);
  409. $logoGroup = [
  410. $logoForm->addButtonUpload(get_lang('Upload'), 'logo_upload', true),
  411. $logoForm->addButtonCancel(get_lang('Reset'), 'logo_reset', true)
  412. ];
  413. $logoForm->addGroup($logoGroup);
  414. if ($show_upload_form) {
  415. echo '<script>
  416. $(function() {
  417. $( "#tabs" ).tabs();
  418. });
  419. </script>';
  420. echo Display::tabs(
  421. array(get_lang('Update'), get_lang('UpdateLogo'), get_lang('UploadNewStylesheet')),
  422. array($form_change->return_form(), $logoForm->return_form(), $form->return_form())
  423. );
  424. } else {
  425. $form_change->display();
  426. }
  427. //Little hack to update the logo image in update form when submiting
  428. if (isset($_POST['logo_reset'])) {
  429. echo '<script>'
  430. . '$("#header-logo-custom").attr("src","' . $url . $logoFileName . '");'
  431. . '</script>';
  432. } elseif (isset($_POST['logo_upload']) && is_file($dir . $newLogoFileName)) {
  433. echo '<script>'
  434. . '$("#header-logo-custom").attr("src","' . $url . $newLogoFileName . '");'
  435. . '</script>';
  436. }
  437. } else {
  438. $form_change->freeze();
  439. }
  440. }
  441. /**
  442. * Creates the folder (if needed) and uploads the stylesheet in it
  443. *
  444. * @param array $values the values of the form
  445. * @param array $picture the values of the uploaded file
  446. *
  447. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  448. * @version May 2008
  449. * @since Dokeos 1.8.5
  450. */
  451. function upload_stylesheet($values, $picture)
  452. {
  453. $result = false;
  454. // Valid name for the stylesheet folder.
  455. $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
  456. $cssToUpload = CSS_UPLOAD_PATH;
  457. // Create the folder if needed.
  458. if (!is_dir($cssToUpload . $style_name . '/')) {
  459. mkdir($cssToUpload . $style_name . '/', api_get_permissions_for_new_directories());
  460. }
  461. $info = pathinfo($picture['name']);
  462. if ($info['extension'] == 'zip') {
  463. // Try to open the file and extract it in the theme.
  464. $zip = new ZipArchive();
  465. if ($zip->open($picture['tmp_name'])) {
  466. // Make sure all files inside the zip are images or css.
  467. $num_files = $zip->numFiles;
  468. $valid = true;
  469. $single_directory = true;
  470. $invalid_files = array();
  471. $allowedFiles = array(
  472. 'jpg',
  473. 'jpeg',
  474. 'png',
  475. 'gif',
  476. 'css',
  477. 'ico',
  478. 'psd',
  479. 'woff',
  480. 'woff2',
  481. 'xcf',
  482. 'svg',
  483. 'webp'
  484. );
  485. for ($i = 0; $i < $num_files; $i++) {
  486. $file = $zip->statIndex($i);
  487. if (substr($file['name'], -1) != '/') {
  488. $path_parts = pathinfo($file['name']);
  489. if (!in_array($path_parts['extension'], $allowedFiles)) {
  490. $valid = false;
  491. $invalid_files[] = $file['name'];
  492. }
  493. }
  494. if (strpos($file['name'], '/') === false) {
  495. $single_directory = false;
  496. }
  497. }
  498. if (!$valid) {
  499. $error_string = '<ul>';
  500. foreach ($invalid_files as $invalid_file) {
  501. $error_string .= '<li>' . $invalid_file . '</li>';
  502. }
  503. $error_string .= '</ul>';
  504. Display::display_error_message(
  505. get_lang('ErrorStylesheetFilesExtensionsInsideZip') . $error_string,
  506. false
  507. );
  508. } else {
  509. // If the zip does not contain a single directory, extract it.
  510. if (!$single_directory) {
  511. // Extract zip file.
  512. $zip->extractTo($cssToUpload . $style_name . '/');
  513. $result = true;
  514. } else {
  515. $extraction_path = $cssToUpload . $style_name . '/';
  516. for ($i = 0; $i < $num_files; $i++) {
  517. $entry = $zip->getNameIndex($i);
  518. if (substr($entry, -1) == '/') {
  519. continue;
  520. }
  521. $pos_slash = strpos($entry, '/');
  522. $entry_without_first_dir = substr($entry, $pos_slash + 1);
  523. // If there is still a slash, we need to make sure the directories are created.
  524. if (strpos($entry_without_first_dir, '/') !== false) {
  525. if (!is_dir($extraction_path . dirname($entry_without_first_dir))) {
  526. // Create it.
  527. @mkdir($extraction_path . dirname($entry_without_first_dir), $mode = 0777, true);
  528. }
  529. }
  530. $fp = $zip->getStream($entry);
  531. $ofp = fopen($extraction_path . dirname($entry_without_first_dir) . '/' . basename($entry),
  532. 'w');
  533. while (!feof($fp)) {
  534. fwrite($ofp, fread($fp, 8192));
  535. }
  536. fclose($fp);
  537. fclose($ofp);
  538. }
  539. $result = true;
  540. }
  541. }
  542. $zip->close();
  543. } else {
  544. Display::display_error_message(get_lang('ErrorReadingZip') . $info['extension'], false);
  545. }
  546. } else {
  547. // Simply move the file.
  548. move_uploaded_file($picture['tmp_name'], $cssToUpload . $style_name . '/' . $picture['name']);
  549. $result = true;
  550. }
  551. if ($result) {
  552. $fs = new Filesystem();
  553. $fs->mirror($cssToUpload, api_get_path(SYS_PATH) . 'web/css/themes/');
  554. }
  555. return $result;
  556. }
  557. /**
  558. * Store plugin regions.
  559. */
  560. function store_regions()
  561. {
  562. $plugin_obj = new AppPlugin();
  563. // Get a list of all current 'Plugins' settings
  564. $installed_plugins = $plugin_obj->get_installed_plugins();
  565. $shortlist_installed = array();
  566. if (!empty($installed_plugins)) {
  567. foreach ($installed_plugins as $plugin) {
  568. if (isset($plugin['subkey'])) {
  569. $shortlist_installed[] = $plugin['subkey'];
  570. }
  571. }
  572. }
  573. $shortlist_installed = array_flip(array_flip($shortlist_installed));
  574. $plugin_list = $plugin_obj->read_plugins_from_path();
  575. foreach ($plugin_list as $plugin) {
  576. if (isset($_POST['plugin_' . $plugin])) {
  577. $areas_to_installed = $_POST['plugin_' . $plugin];
  578. if (!empty($areas_to_installed)) {
  579. $plugin_obj->remove_all_regions($plugin);
  580. foreach ($areas_to_installed as $region) {
  581. if (!empty($region) && $region != '-1') {
  582. $plugin_obj->add_to_region($plugin, $region);
  583. }
  584. }
  585. }
  586. }
  587. }
  588. }
  589. /**
  590. * This function allows easy activating and inactivating of plugins
  591. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  592. */
  593. function store_plugins()
  594. {
  595. $appPlugin = new AppPlugin();
  596. // Get a list of all current 'Plugins' settings
  597. $plugin_list = $appPlugin->read_plugins_from_path();
  598. $installed_plugins = array();
  599. foreach ($plugin_list as $plugin) {
  600. if (isset($_POST['plugin_' . $plugin])) {
  601. $appPlugin->install($plugin);
  602. $installed_plugins[] = $plugin;
  603. }
  604. }
  605. if (!empty($installed_plugins)) {
  606. $remove_plugins = array_diff($plugin_list, $installed_plugins);
  607. } else {
  608. $remove_plugins = $plugin_list;
  609. }
  610. foreach ($remove_plugins as $plugin) {
  611. $appPlugin->uninstall($plugin);
  612. }
  613. }
  614. /**
  615. * This function allows the platform admin to choose which should be the default stylesheet
  616. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  617. */
  618. function store_stylesheets()
  619. {
  620. // Insert the stylesheet.
  621. if (is_style($_POST['style'])) {
  622. api_set_setting(
  623. 'stylesheets',
  624. $_POST['style'],
  625. null,
  626. 'stylesheets',
  627. api_get_current_access_url_id()
  628. );
  629. }
  630. return true;
  631. }
  632. /**
  633. * This function checks if the given style is a recognize style that exists in the css directory as
  634. * a standalone directory.
  635. * @param string Style
  636. * @return bool True if this style is recognized, false otherwise
  637. */
  638. function is_style($style)
  639. {
  640. $dir = CSS_UPLOAD_PATH;
  641. $dirs = scandir($dir);
  642. $style = str_replace(array('/', '\\'), array('', ''), $style); // Avoid slashes or backslashes.
  643. if (in_array($style, $dirs) && is_dir($dir . $style)) {
  644. return true;
  645. }
  646. return false;
  647. }
  648. /**
  649. * Search options
  650. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  651. * @author Marco Villegas <marvil07@gmail.com>
  652. */
  653. function handle_search()
  654. {
  655. global $SettingsStored, $_configuration;
  656. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  657. $search_enabled = api_get_setting('search_enabled');
  658. $form = new FormValidator('search-options', 'post', api_get_self() . '?category=Search');
  659. $values = api_get_settings_options('search_enabled');
  660. $form->addElement('header', null, get_lang('SearchEnabledTitle'));
  661. $group = array();
  662. if (is_array($values)) {
  663. foreach ($values as $key => $value) {
  664. $element = &$form->createElement('radio', 'search_enabled', '', get_lang($value['display_text']),
  665. $value['value']);
  666. $group[] = $element;
  667. }
  668. }
  669. //SearchEnabledComment
  670. $form->addGroup($group, 'search_enabled', array(get_lang('SearchEnabledTitle'), get_lang('SearchEnabledComment')),
  671. '<br />', false);
  672. $search_enabled = api_get_setting('search_enabled');
  673. if ($form->validate()) {
  674. $formvalues = $form->exportValues();
  675. $r = api_set_settings_category('Search', 'false', $_configuration['access_url']);
  676. // Save the settings.
  677. foreach ($formvalues as $key => $value) {
  678. $result = api_set_setting($key, $value, null, null);
  679. }
  680. $search_enabled = $formvalues['search_enabled'];
  681. Display::display_confirmation_message($SettingsStored);
  682. }
  683. $specific_fields = get_specific_field_list();
  684. if ($search_enabled == 'true') {
  685. $values = api_get_settings_options('search_show_unlinked_results');
  686. $group = array();
  687. foreach ($values as $key => $value) {
  688. $element = &$form->createElement('radio', 'search_show_unlinked_results', '',
  689. get_lang($value['display_text']), $value['value']);
  690. $group[] = $element;
  691. }
  692. $form->addGroup($group, 'search_show_unlinked_results',
  693. array(get_lang('SearchShowUnlinkedResultsTitle'), get_lang('SearchShowUnlinkedResultsComment')), '', false);
  694. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  695. $sf_values = array();
  696. foreach ($specific_fields as $sf) {
  697. $sf_values[$sf['code']] = $sf['name'];
  698. }
  699. $group = array();
  700. $url = Display::div(Display::url(get_lang('AddSpecificSearchField'), 'specific_fields.php'),
  701. array('class' => 'sectioncomment'));
  702. if (empty($sf_values)) {
  703. $form->addElement('label', [get_lang('SearchPrefilterPrefix'), $url]);
  704. } else {
  705. $form->addElement('select', 'search_prefilter_prefix', array(get_lang('SearchPrefilterPrefix'), $url),
  706. $sf_values, '');
  707. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  708. }
  709. }
  710. $default_values['search_enabled'] = $search_enabled;
  711. $form->addButtonSave(get_lang('Save'));
  712. $form->setDefaults($default_values);
  713. echo '<div id="search-options-form">';
  714. $form->display();
  715. echo '</div>';
  716. if ($search_enabled == 'true') {
  717. $xapian_path = api_get_path(SYS_UPLOAD_PATH) . 'plugins/xapian/searchdb';
  718. /*
  719. @todo Test the Xapian connection
  720. if (extension_loaded('xapian')) {
  721. require_once 'xapian.php';
  722. try {
  723. $db = new XapianDatabase($xapian_path.'/');
  724. } catch (Exception $e) {
  725. var_dump($e->getMessage());
  726. }
  727. require_once api_get_path(LIBRARY_PATH) . 'search/DokeosIndexer.class.php';
  728. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  729. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  730. $indexable = new IndexableChunk();
  731. $indexable->addValue("content", 'Test');
  732. $di = new DokeosIndexer();
  733. $di->connectDb(NULL, NULL, 'english');
  734. $di->addChunk($indexable);
  735. $did = $di->index();
  736. }
  737. */
  738. $xapian_loaded = Display::return_icon('bullet_green.png', get_lang('Ok'));
  739. $dir_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
  740. $dir_is_writable = Display::return_icon('bullet_green.png', get_lang('Ok'));
  741. $specific_fields_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
  742. //Testing specific fields
  743. if (empty($specific_fields)) {
  744. $specific_fields_exists = Display::return_icon('bullet_red.png', get_lang('AddSpecificSearchField'));
  745. }
  746. //Testing xapian extension
  747. if (!extension_loaded('xapian')) {
  748. $xapian_loaded = Display::return_icon('bullet_red.png', get_lang('Error'));
  749. }
  750. //Testing xapian searchdb path
  751. if (!is_dir($xapian_path)) {
  752. $dir_exists = Display::return_icon('bullet_red.png', get_lang('Error'));
  753. }
  754. //Testing xapian searchdb path is writable
  755. if (!is_writable($xapian_path)) {
  756. $dir_is_writable = Display::return_icon('bullet_red.png', get_lang('Error'));
  757. }
  758. $data[] = array(get_lang('XapianModuleInstalled'), $xapian_loaded);
  759. $data[] = array(get_lang('DirectoryExists') . ' - ' . $xapian_path, $dir_exists);
  760. $data[] = array(get_lang('IsWritable') . ' - ' . $xapian_path, $dir_is_writable);
  761. $data[] = array(get_lang('SpecificSearchFieldsAvailable'), $specific_fields_exists);
  762. echo Display::tag('h3', get_lang('Settings'));
  763. $table = new SortableTableFromArray($data);
  764. $table->set_header(0, get_lang('Setting'), false);
  765. $table->set_header(1, get_lang('Status'), false);
  766. echo $table->display();
  767. //@todo windows support
  768. if (api_is_windows_os() == false) {
  769. $list_of_programs = array('pdftotext', 'ps2pdf', 'catdoc', 'html2text', 'unrtf', 'catppt', 'xls2csv');
  770. foreach ($list_of_programs as $program) {
  771. $output = [];
  772. $ret_val = null;
  773. exec("which $program", $output, $ret_val);
  774. if (!$output) {
  775. $output[] = '';
  776. }
  777. $icon = Display::return_icon('bullet_red.png', get_lang('NotInstalled'));
  778. if (!empty($output[0])) {
  779. $icon = Display::return_icon('bullet_green.png', get_lang('Installed'));
  780. }
  781. $data2[] = array($program, $output[0], $icon);
  782. }
  783. echo Display::tag('h3', get_lang('ProgramsNeededToConvertFiles'));
  784. $table = new SortableTableFromArray($data2);
  785. $table->set_header(0, get_lang('Program'), false);
  786. $table->set_header(1, get_lang('Path'), false);
  787. $table->set_header(2, get_lang('Status'), false);
  788. echo $table->display();
  789. } else {
  790. Display::display_warning_message(
  791. get_lang('YouAreUsingChamiloInAWindowsPlatformSadlyYouCantConvertDocumentsInOrderToSearchTheContentUsingThisTool')
  792. );
  793. }
  794. }
  795. }
  796. /**
  797. * Wrapper for the templates
  798. *
  799. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  800. * @author Julio Montoya.
  801. * @version August 2008
  802. * @since Dokeos 1.8.6
  803. */
  804. function handle_templates()
  805. {
  806. /* Drive-by fix to avoid undefined var warnings, without repeating
  807. * isset() combos all over the place. */
  808. $action = isset($_GET['action']) ? $_GET['action'] : "invalid";
  809. if ($action != 'add') {
  810. echo '<div class="actions" style="margin-left: 1px;">';
  811. echo '<a href="settings.php?category=Templates&action=add">' .
  812. Display::return_icon('new_template.png', get_lang('AddTemplate'), '', ICON_SIZE_MEDIUM) . '</a>';
  813. echo '</div>';
  814. }
  815. if ($action == 'add' || ($action == 'edit' && is_numeric($_GET['id']))) {
  816. add_edit_template();
  817. // Add event to the system log.
  818. $user_id = api_get_user_id();
  819. $category = $_GET['category'];
  820. Event::addEvent(
  821. LOG_CONFIGURATION_SETTINGS_CHANGE,
  822. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  823. $category,
  824. api_get_utc_datetime(),
  825. $user_id
  826. );
  827. } else {
  828. if ($action == 'delete' && is_numeric($_GET['id'])) {
  829. delete_template($_GET['id']);
  830. // Add event to the system log
  831. $user_id = api_get_user_id();
  832. $category = $_GET['category'];
  833. Event::addEvent(
  834. LOG_CONFIGURATION_SETTINGS_CHANGE,
  835. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  836. $category,
  837. api_get_utc_datetime(),
  838. $user_id
  839. );
  840. }
  841. display_templates();
  842. }
  843. }
  844. /**
  845. * Display a sortable table with all the templates that the platform administrator has defined.
  846. *
  847. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  848. * @version August 2008
  849. * @since Dokeos 1.8.6
  850. */
  851. function display_templates()
  852. {
  853. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data', 1);
  854. $table->set_additional_parameters(array('category' => Security::remove_XSS($_GET['category'])));
  855. $table->set_header(0, get_lang('Image'), true, array('style' => 'width: 101px;'));
  856. $table->set_header(1, get_lang('Title'));
  857. $table->set_header(2, get_lang('Actions'), false, array('style' => 'width:50px;'));
  858. $table->set_column_filter(2, 'actions_filter');
  859. $table->set_column_filter(0, 'image_filter');
  860. $table->display();
  861. }
  862. /**
  863. * Gets the number of templates that are defined by the platform admin.
  864. *
  865. * @return integer
  866. *
  867. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  868. * @version August 2008
  869. * @since Dokeos 1.8.6
  870. */
  871. function get_number_of_templates()
  872. {
  873. // Database table definition.
  874. $table_system_template = Database:: get_main_table('system_template');
  875. // The sql statement.
  876. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  877. $result = Database::query($sql);
  878. $row = Database::fetch_array($result);
  879. // Returning the number of templates.
  880. return $row['total'];
  881. }
  882. /**
  883. * Gets all the template data for the sortable table.
  884. *
  885. * @param integer $from the start of the limit statement
  886. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  887. * @param integer $column the column that is
  888. * @param string $direction the sorting direction (ASC or DESC�
  889. * @return array
  890. *
  891. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  892. * @version August 2008
  893. * @since Dokeos 1.8.6
  894. */
  895. function get_template_data($from, $number_of_items, $column, $direction)
  896. {
  897. // Database table definition.
  898. $table_system_template = Database:: get_main_table('system_template');
  899. // The sql statement.
  900. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  901. $sql .= " ORDER BY col$column $direction ";
  902. $sql .= " LIMIT $from,$number_of_items";
  903. $result = Database::query($sql);
  904. $return = array();
  905. while ($row = Database::fetch_array($result)) {
  906. $row['1'] = get_lang($row['1']);
  907. $return[] = $row;
  908. }
  909. // Returning all the information for the sortable table.
  910. return $return;
  911. }
  912. /**
  913. * display the edit and delete icons in the sortable table
  914. *
  915. * @param integer $id the id of the template
  916. * @return string code for the link to edit and delete the template
  917. *
  918. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  919. * @version August 2008
  920. * @since Dokeos 1.8.6
  921. */
  922. function actions_filter($id)
  923. {
  924. $return = '<a href="settings.php?category=Templates&action=edit&id=' . Security::remove_XSS($id) . '">' . Display::return_icon('edit.png',
  925. get_lang('Edit'), '', ICON_SIZE_SMALL) . '</a>';
  926. $return .= '<a href="settings.php?category=Templates&action=delete&id=' . Security::remove_XSS($id) . '" onClick="javascript:if(!confirm(' . "'" . get_lang('ConfirmYourChoice') . "'" . ')) return false;">' . Display::return_icon('delete.png',
  927. get_lang('Delete'), '', ICON_SIZE_SMALL) . '</a>';
  928. return $return;
  929. }
  930. /**
  931. * Display the image of the template in the sortable table
  932. *
  933. * @param string $image the image
  934. * @return string code for the image
  935. *
  936. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  937. * @version August 2008
  938. * @since Dokeos 1.8.6
  939. */
  940. function image_filter($image)
  941. {
  942. if (!empty($image)) {
  943. return '<img src="' . api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/' . $image . '" alt="' . get_lang('TemplatePreview') . '"/>';
  944. } else {
  945. return '<img src="' . api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/noimage.gif" alt="' . get_lang('NoTemplatePreview') . '"/>';
  946. }
  947. }
  948. /**
  949. * Add (or edit) a template. This function displays the form and also takes
  950. * care of uploading the image and storing the information in the database
  951. *
  952. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  953. * @version August 2008
  954. * @since Dokeos 1.8.6
  955. */
  956. function add_edit_template()
  957. {
  958. // Initialize the object.
  959. $id = isset($_GET['id']) ? '&id=' . Security::remove_XSS($_GET['id']) : '';
  960. $form = new FormValidator('template', 'post',
  961. 'settings.php?category=Templates&action=' . Security::remove_XSS($_GET['action']) . $id);
  962. // Setting the form elements: the header.
  963. if ($_GET['action'] == 'add') {
  964. $title = get_lang('AddTemplate');
  965. } else {
  966. $title = get_lang('EditTemplate');
  967. }
  968. $form->addElement('header', '', $title);
  969. // Setting the form elements: the title of the template.
  970. $form->addText('title', get_lang('Title'), false);
  971. // Setting the form elements: the content of the template (wysiwyg editor).
  972. $form->addElement('html_editor', 'template_text', get_lang('Text'), null,
  973. array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  974. // Setting the form elements: the form to upload an image to be used with the template.
  975. $form->addElement('file', 'template_image', get_lang('Image'), '');
  976. // Setting the form elements: a little bit information about the template image.
  977. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  978. // Getting all the information of the template when editing a template.
  979. if ($_GET['action'] == 'edit') {
  980. // Database table definition.
  981. $table_system_template = Database:: get_main_table('system_template');
  982. $sql = "SELECT * FROM $table_system_template WHERE id = " . intval($_GET['id']) . "";
  983. $result = Database::query($sql);
  984. $row = Database::fetch_array($result);
  985. $defaults['template_id'] = intval($_GET['id']);
  986. $defaults['template_text'] = $row['content'];
  987. // Forcing get_lang().
  988. $defaults['title'] = get_lang($row['title']);
  989. // Adding an extra field: a hidden field with the id of the template we are editing.
  990. $form->addElement('hidden', 'template_id');
  991. // Adding an extra field: a preview of the image that is currently used.
  992. if (!empty($row['image'])) {
  993. $form->addElement('static', 'template_image_preview', '',
  994. '<img src="' . api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/' . $row['image'] . '" alt="' . get_lang('TemplatePreview') . '"/>');
  995. } else {
  996. $form->addElement('static', 'template_image_preview', '',
  997. '<img src="' . api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/noimage.gif" alt="' . get_lang('NoTemplatePreview') . '"/>');
  998. }
  999. // Setting the information of the template that we are editing.
  1000. $form->setDefaults($defaults);
  1001. }
  1002. // Setting the form elements: the submit button.
  1003. $form->addButtonSave(get_lang('Ok'), 'submit');
  1004. // Setting the rules: the required fields.
  1005. $form->addRule('template_image', get_lang('ThisFieldIsRequired'), 'required');
  1006. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  1007. $form->addRule('template_text', get_lang('ThisFieldIsRequired'), 'required');
  1008. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  1009. if ($form->validate()) {
  1010. $check = Security::check_token('post');
  1011. if ($check) {
  1012. // Exporting the values.
  1013. $values = $form->exportValues();
  1014. // Upload the file.
  1015. if (!empty($_FILES['template_image']['name'])) {
  1016. $upload_ok = process_uploaded_file($_FILES['template_image']);
  1017. if ($upload_ok) {
  1018. // Try to add an extension to the file if it hasn't one.
  1019. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']),
  1020. $_FILES['template_image']['type']);
  1021. // The upload directory.
  1022. $upload_dir = api_get_path(SYS_APP_PATH) . 'home/default_platform_document/template_thumb/';
  1023. // Create the directory if it does not exist.
  1024. if (!is_dir($upload_dir)) {
  1025. mkdir($upload_dir, api_get_permissions_for_new_directories());
  1026. }
  1027. // Resize the preview image to max default and upload.
  1028. $temp = new Image($_FILES['template_image']['tmp_name']);
  1029. $picture_info = $temp->get_image_info();
  1030. $max_width_for_picture = 100;
  1031. if ($picture_info['width'] > $max_width_for_picture) {
  1032. $temp->resize($max_width_for_picture);
  1033. }
  1034. $temp->send_image($upload_dir . $new_file_name);
  1035. }
  1036. }
  1037. // Store the information in the database (as insert or as update).
  1038. $table_system_template = Database:: get_main_table('system_template');
  1039. if ($_GET['action'] == 'add') {
  1040. $content_template = Security::remove_XSS($values['template_text'], COURSEMANAGERLOWSECURITY);
  1041. $params = [
  1042. 'title' => $values['title'],
  1043. 'content' => $content_template,
  1044. 'image' => $new_file_name
  1045. ];
  1046. Database::insert($table_system_template, $params);
  1047. // Display a feedback message.
  1048. Display::display_confirmation_message(get_lang('TemplateAdded'));
  1049. echo '<a href="settings.php?category=Templates&action=add">' . Display::return_icon('new_template.png',
  1050. get_lang('AddTemplate'), '', ICON_SIZE_MEDIUM) . '</a>';
  1051. } else {
  1052. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>' . Database::escape_string($values['template_text']) . '</body>';
  1053. $sql = "UPDATE $table_system_template set title = '" . Database::escape_string($values['title']) . "', content = '" . $content_template . "'";
  1054. if (!empty($new_file_name)) {
  1055. $sql .= ", image = '" . Database::escape_string($new_file_name) . "'";
  1056. }
  1057. $sql .= " WHERE id = " . intval($_GET['id']) . "";
  1058. Database::query($sql);
  1059. // Display a feedback message.
  1060. Display::display_confirmation_message(get_lang('TemplateEdited'));
  1061. }
  1062. }
  1063. Security::clear_token();
  1064. display_templates();
  1065. } else {
  1066. $token = Security::get_token();
  1067. $form->addElement('hidden', 'sec_token');
  1068. $form->setConstants(array('sec_token' => $token));
  1069. // Display the form.
  1070. $form->display();
  1071. }
  1072. }
  1073. /**
  1074. * Delete a template
  1075. *
  1076. * @param integer $id the id of the template that has to be deleted
  1077. *
  1078. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1079. * @version August 2008
  1080. * @since Dokeos 1.8.6
  1081. */
  1082. function delete_template($id)
  1083. {
  1084. // First we remove the image.
  1085. $table_system_template = Database:: get_main_table('system_template');
  1086. $sql = "SELECT * FROM $table_system_template WHERE id = " . intval($id) . "";
  1087. $result = Database::query($sql);
  1088. $row = Database::fetch_array($result);
  1089. if (!empty($row['image'])) {
  1090. @unlink(api_get_path(SYS_APP_PATH) . 'home/default_platform_document/template_thumb/' . $row['image']);
  1091. }
  1092. // Now we remove it from the database.
  1093. $sql = "DELETE FROM $table_system_template WHERE id = " . intval($id) . "";
  1094. Database::query($sql);
  1095. // Display a feedback message.
  1096. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  1097. }
  1098. /**
  1099. * Returns the list of timezone identifiers used to populate the select
  1100. * This function is called through a call_user_func() in the generate_settings_form function.
  1101. * @return array List of timezone identifiers
  1102. *
  1103. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1104. * @since Chamilo 1.8.7
  1105. */
  1106. function select_timezone_value()
  1107. {
  1108. return api_get_timezones();
  1109. }
  1110. /**
  1111. * Returns an array containing the list of options used to populate the gradebook_number_decimals variable
  1112. * This function is called through a call_user_func() in the generate_settings_form function.
  1113. * @return string[] List of gradebook_number_decimals options
  1114. *
  1115. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1116. */
  1117. function select_gradebook_number_decimals()
  1118. {
  1119. return array('0', '1', '2');
  1120. }
  1121. function select_gradebook_default_grade_model_id()
  1122. {
  1123. $grade_model = new GradeModel();
  1124. $models = $grade_model->get_all();
  1125. $options = array();
  1126. $options[-1] = get_lang('None');
  1127. if (!empty($models)) {
  1128. foreach ($models as $model) {
  1129. $options[$model['id']] = $model['name'];
  1130. }
  1131. }
  1132. return $options;
  1133. }
  1134. /**
  1135. * Updates the gradebook score custom values using the scoredisplay class of the
  1136. * gradebook module
  1137. *
  1138. * @param array List of gradebook score custom values
  1139. *
  1140. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1141. */
  1142. function update_gradebook_score_display_custom_values($values)
  1143. {
  1144. $scoredisplay = ScoreDisplay::instance();
  1145. $scores = $values['gradebook_score_display_custom_values_endscore'];
  1146. $displays = $values['gradebook_score_display_custom_values_displaytext'];
  1147. $nr_displays = count($displays);
  1148. $final = array();
  1149. for ($i = 1; $i < $nr_displays; $i++) {
  1150. if (!empty($scores[$i]) && !empty($displays[$i])) {
  1151. $final[$i]['score'] = $scores[$i];
  1152. $final[$i]['display'] = $displays[$i];
  1153. }
  1154. }
  1155. $scoredisplay->update_custom_score_display_settings($final);
  1156. }
  1157. function generate_settings_form($settings, $settings_by_access_list)
  1158. {
  1159. global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list;
  1160. $table_settings_current = Database:: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1161. $form = new FormValidator('settings', 'post', 'settings.php?category=' . Security::remove_XSS($_GET['category']));
  1162. $form->addElement('hidden', 'search_field',
  1163. (!empty($_GET['search_field']) ? Security::remove_XSS($_GET['search_field']) : null));
  1164. $url_id = api_get_current_access_url_id();
  1165. if (!empty($_configuration['multiple_access_urls']) && api_is_global_platform_admin() && $url_id == 1) {
  1166. $group = array();
  1167. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  1168. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  1169. $form->addGroup($group, 'buttons_in_action_right');
  1170. }
  1171. $default_values = array();
  1172. $url_info = api_get_access_url($url_id);
  1173. $i = 0;
  1174. $addedSettings = [];
  1175. foreach ($settings as $row) {
  1176. if (in_array($row['variable'], array_keys($settings_to_avoid))) {
  1177. continue;
  1178. }
  1179. if (in_array($row['variable'], $addedSettings)) {
  1180. continue;
  1181. }
  1182. $addedSettings[] = $row['variable'];
  1183. if (!empty($_configuration['multiple_access_urls'])) {
  1184. if (api_is_global_platform_admin()) {
  1185. if ($row['access_url_locked'] == 0) {
  1186. if ($url_id == 1) {
  1187. if ($row['access_url_changeable'] == '1') {
  1188. $form->addElement('html',
  1189. '<div style="float: right;"><a class="share_this_setting" data_status = "0" data_to_send = "' . $row['variable'] . '" href="javascript:void(0);">' .
  1190. Display::return_icon('shared_setting.png',
  1191. get_lang('ChangeSharedSetting')) . '</a></div>');
  1192. } else {
  1193. $form->addElement('html',
  1194. '<div style="float: right;"><a class="share_this_setting" data_status = "1" data_to_send = "' . $row['variable'] . '" href="javascript:void(0);">' .
  1195. Display::return_icon('shared_setting_na.png',
  1196. get_lang('ChangeSharedSetting')) . '</a></div>');
  1197. }
  1198. } else {
  1199. if ($row['access_url_changeable'] == '1') {
  1200. $form->addElement('html', '<div style="float: right;">' .
  1201. Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')) . '</div>');
  1202. } else {
  1203. $form->addElement('html', '<div style="float: right;">' .
  1204. Display::return_icon('shared_setting_na.png',
  1205. get_lang('ChangeSharedSetting')) . '</div>');
  1206. }
  1207. }
  1208. }
  1209. }
  1210. }
  1211. $hideme = array();
  1212. $hide_element = false;
  1213. if ($_configuration['access_url'] != 1) {
  1214. if ($row['access_url_changeable'] == 0) {
  1215. // We hide the element in other cases (checkbox, radiobutton) we 'freeze' the element.
  1216. $hide_element = true;
  1217. $hideme = array('disabled');
  1218. } elseif ($url_info['active'] == 1) {
  1219. // We show the elements.
  1220. if (empty($row['variable'])) {
  1221. $row['variable'] = 0;
  1222. }
  1223. if (empty($row['subkey'])) {
  1224. $row['subkey'] = 0;
  1225. }
  1226. if (empty($row['category'])) {
  1227. $row['category'] = 0;
  1228. }
  1229. if (is_array($settings_by_access_list[$row['variable']] [$row['subkey']] [$row['category']])) {
  1230. // We are sure that the other site have a selected value.
  1231. if ($settings_by_access_list[$row['variable']] [$row['subkey']] [$row['category']]['selected_value'] != '') {
  1232. $row['selected_value'] = $settings_by_access_list[$row['variable']] [$row['subkey']] [$row['category']]['selected_value'];
  1233. }
  1234. }
  1235. // There is no else{} statement because we load the default $row['selected_value'] of the main Chamilo site.
  1236. }
  1237. }
  1238. switch ($row['type']) {
  1239. case 'textfield':
  1240. if (in_array($row['variable'], $convert_byte_to_mega_list)) {
  1241. $form->addElement(
  1242. 'text',
  1243. $row['variable'],
  1244. array(
  1245. get_lang($row['title']),
  1246. get_lang($row['comment']),
  1247. get_lang('MB'),
  1248. ),
  1249. array('maxlength' => '8')
  1250. );
  1251. $form->applyFilter($row['variable'], 'html_filter');
  1252. $default_values[$row['variable']] = round($row['selected_value'] / 1024 / 1024, 1);
  1253. } elseif ($row['variable'] == 'account_valid_duration') {
  1254. $form->addElement(
  1255. 'text',
  1256. $row['variable'],
  1257. array(
  1258. get_lang($row['title']),
  1259. get_lang($row['comment']),
  1260. ),
  1261. array('maxlength' => '5')
  1262. );
  1263. $form->applyFilter($row['variable'], 'html_filter');
  1264. $default_values[$row['variable']] = $row['selected_value'];
  1265. // For platform character set selection: Conversion of the textfield to a select box with valid values.
  1266. } elseif ($row['variable'] == 'platform_charset') {
  1267. continue;
  1268. } else {
  1269. $hideme['class'] = 'col-md-4';
  1270. $form->addElement(
  1271. 'text',
  1272. $row['variable'],
  1273. array(
  1274. get_lang($row['title']),
  1275. get_lang($row['comment']),
  1276. ),
  1277. $hideme
  1278. );
  1279. $form->applyFilter($row['variable'], 'html_filter');
  1280. $default_values[$row['variable']] = $row['selected_value'];
  1281. }
  1282. break;
  1283. case 'textarea':
  1284. if ($row['variable'] == 'header_extra_content') {
  1285. $file = api_get_path(SYS_PATH) . api_get_home_path() . 'header_extra_content.txt';
  1286. $value = '';
  1287. if (file_exists($file)) {
  1288. $value = file_get_contents($file);
  1289. }
  1290. $form->addElement('textarea', $row['variable'],
  1291. array(get_lang($row['title']), get_lang($row['comment'])), array('rows' => '10'), $hideme);
  1292. $default_values[$row['variable']] = $value;
  1293. } elseif ($row['variable'] == 'footer_extra_content') {
  1294. $file = api_get_path(SYS_PATH) . api_get_home_path() . 'footer_extra_content.txt';
  1295. $value = '';
  1296. if (file_exists($file)) {
  1297. $value = file_get_contents($file);
  1298. }
  1299. $form->addElement('textarea', $row['variable'],
  1300. array(get_lang($row['title']), get_lang($row['comment'])), array('rows' => '10'), $hideme);
  1301. $default_values[$row['variable']] = $value;
  1302. } else {
  1303. $form->addElement('textarea', $row['variable'],
  1304. array(get_lang($row['title']), get_lang($row['comment'])), array('rows' => '10'), $hideme);
  1305. $default_values[$row['variable']] = $row['selected_value'];
  1306. }
  1307. break;
  1308. case 'radio':
  1309. $values = api_get_settings_options($row['variable']);
  1310. $group = array();
  1311. if (is_array($values)) {
  1312. foreach ($values as $key => $value) {
  1313. $element = &$form->createElement(
  1314. 'radio',
  1315. $row['variable'],
  1316. '',
  1317. get_lang($value['display_text']),
  1318. $value['value']
  1319. );
  1320. if ($hide_element) {
  1321. $element->freeze();
  1322. }
  1323. $group[] = $element;
  1324. }
  1325. }
  1326. $form->addGroup(
  1327. $group,
  1328. $row['variable'],
  1329. array(get_lang($row['title']), get_lang($row['comment'])),
  1330. '',
  1331. false
  1332. );
  1333. $default_values[$row['variable']] = $row['selected_value'];
  1334. break;
  1335. case 'checkbox';
  1336. // 1. We collect all the options of this variable.
  1337. $sql = "SELECT * FROM $table_settings_current
  1338. WHERE variable='" . $row['variable'] . "' AND access_url = 1";
  1339. $result = Database::query($sql);
  1340. $group = array();
  1341. while ($rowkeys = Database::fetch_array($result)) {
  1342. // Profile tab option should be hidden when the social tool is enabled.
  1343. if (api_get_setting('allow_social_tool') == 'true') {
  1344. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_profile') {
  1345. continue;
  1346. }
  1347. }
  1348. // Hiding the gradebook option.
  1349. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_gradebook') {
  1350. continue;
  1351. }
  1352. $element = &$form->createElement(
  1353. 'checkbox',
  1354. $rowkeys['subkey'],
  1355. '',
  1356. get_lang($rowkeys['subkeytext'])
  1357. );
  1358. if ($row['access_url_changeable'] == 1) {
  1359. // 2. We look into the DB if there is a setting for a specific access_url.
  1360. $access_url = $_configuration['access_url'];
  1361. if (empty($access_url)) {
  1362. $access_url = 1;
  1363. }
  1364. $sql = "SELECT selected_value FROM $table_settings_current
  1365. WHERE
  1366. variable='" . $rowkeys['variable'] . "' AND
  1367. subkey='" . $rowkeys['subkey'] . "' AND
  1368. subkeytext='" . $rowkeys['subkeytext'] . "' AND
  1369. access_url = $access_url";
  1370. $result_access = Database::query($sql);
  1371. $row_access = Database::fetch_array($result_access);
  1372. if ($row_access['selected_value'] == 'true' && !$form->isSubmitted()) {
  1373. $element->setChecked(true);
  1374. }
  1375. } else {
  1376. if ($rowkeys['selected_value'] == 'true' && !$form->isSubmitted()) {
  1377. $element->setChecked(true);
  1378. }
  1379. }
  1380. if ($hide_element) {
  1381. $element->freeze();
  1382. }
  1383. $group[] = $element;
  1384. }
  1385. $form->addGroup(
  1386. $group,
  1387. $row['variable'],
  1388. array(get_lang($row['title']), get_lang($row['comment'])),
  1389. ''
  1390. );
  1391. break;
  1392. case 'link':
  1393. $form->addElement('static', null, array(get_lang($row['title']), get_lang($row['comment'])),
  1394. get_lang('CurrentValue') . ' : ' . $row['selected_value'], $hideme);
  1395. break;
  1396. case 'select':
  1397. /*
  1398. * To populate the list of options, the select type dynamically calls a function that must be called select_ + the name of the variable being displayed.
  1399. * The functions being called must be added to the file settings.lib.php.
  1400. */
  1401. $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])),
  1402. call_user_func('select_' . $row['variable']), $hideme);
  1403. $default_values[$row['variable']] = $row['selected_value'];
  1404. break;
  1405. case 'custom':
  1406. break;
  1407. }
  1408. switch ($row['variable']) {
  1409. case 'pdf_export_watermark_enable':
  1410. $url = PDF::get_watermark(null);
  1411. if ($url != false) {
  1412. $delete_url = '<a href="?delete_watermark">' . get_lang('DelImage') . ' ' . Display::return_icon('delete.png',
  1413. get_lang('DelImage')) . '</a>';
  1414. $form->addElement('html',
  1415. '<div style="max-height:100px; max-width:100px; margin-left:162px; margin-bottom:10px; clear:both;"><img src="' . $url . '" style="margin-bottom:10px;" />' . $delete_url . '</div>');
  1416. }
  1417. $form->addElement('file', 'pdf_export_watermark_path', get_lang('AddWaterMark'));
  1418. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  1419. $form->addRule('pdf_export_watermark_path',
  1420. get_lang('OnlyImagesAllowed') . ' (' . implode(',', $allowed_picture_types) . ')', 'filetype',
  1421. $allowed_picture_types);
  1422. break;
  1423. case 'timezone_value':
  1424. $timezone = $row['selected_value'];
  1425. if (empty($timezone)) {
  1426. $timezone = _api_get_timezone();
  1427. }
  1428. $form->addElement('html',
  1429. sprintf(get_lang('LocalTimeUsingPortalTimezoneXIsY'), $timezone, api_get_local_time()));
  1430. break;
  1431. }
  1432. } // end for
  1433. if (!empty($settings)) {
  1434. $form->setDefaults($default_values);
  1435. }
  1436. $form->addHtml('<div class="bottom_actions">');
  1437. $form->addButtonSave(get_lang('SaveSettings'));
  1438. $form->addHtml('</div>');
  1439. return $form;
  1440. }
  1441. /**
  1442. * Searches a platform setting in all categories except from the Plugins category
  1443. * @param string $search
  1444. * @return array
  1445. */
  1446. function search_setting($search)
  1447. {
  1448. if (empty($search)) {
  1449. return array();
  1450. }
  1451. $table_settings_current = Database:: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1452. $sql = "SELECT * FROM $table_settings_current
  1453. WHERE category <> 'Plugins' ORDER BY id ASC ";
  1454. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1455. $settings = array();
  1456. $search = api_strtolower($search);
  1457. if (!empty($result)) {
  1458. foreach ($result as $setting) {
  1459. $found = false;
  1460. $title = api_strtolower(get_lang($setting['title']));
  1461. // try the title
  1462. if (strpos($title, $search) === false) {
  1463. $comment = api_strtolower(get_lang($setting['comment']));
  1464. //Try the comment
  1465. if (strpos($comment, $search) === false) {
  1466. //Try the variable name
  1467. if (strpos($setting['variable'], $search) === false) {
  1468. continue;
  1469. } else {
  1470. $found = true;
  1471. }
  1472. } else {
  1473. $found = true;
  1474. }
  1475. } else {
  1476. $found = true;
  1477. }
  1478. if ($found) {
  1479. $settings[] = $setting;
  1480. }
  1481. }
  1482. }
  1483. return $settings;
  1484. }