settings.lib.php 60 KB

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