settings.lib.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. }