settings.lib.php 55 KB

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