settings.lib.php 58 KB

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