settings.lib.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. * The function that retrieves all the possible settings for a certain config setting
  14. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  15. */
  16. function get_settings_options($var) {
  17. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  18. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  19. $result = Database::query($sql);
  20. while ($row = Database::fetch_array($result)) {
  21. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  22. $settings_options_array[] = $temp_array;
  23. }
  24. return $settings_options_array;
  25. }
  26. /**
  27. * This function allows easy activating and inactivating of plugins
  28. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  29. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  30. */
  31. function handle_plugins() {
  32. global $SettingsStored;
  33. $userplugins = array();
  34. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  35. if (isset($_POST['submit_plugins'])) {
  36. store_plugins();
  37. // Add event to the system log.
  38. $time = time();
  39. $user_id = api_get_user_id();
  40. $category = $_GET['category'];
  41. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  42. Display :: display_confirmation_message(get_lang('SettingsStored'));
  43. }
  44. //echo get_lang('AvailablePlugins').'<br />';
  45. echo '<br />';
  46. /* We scan the plugin directory. Each folder is a potential plugin. */
  47. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  48. $handle = @opendir($pluginpath);
  49. while (false !== ($file = readdir($handle))) {
  50. if ($file != '.' && $file != '..' && is_dir(api_get_path(SYS_PLUGIN_PATH).$file)) {
  51. $possibleplugins[] = $file;
  52. }
  53. }
  54. @closedir($handle);
  55. /* For each of the possible plugin directories we check whether a file named "plugin.php" exists
  56. (it contains all the needed information about this plugin).
  57. This "plugin.php" file looks like:
  58. $plugin_info['title'] = 'The title of the plugin';
  59. $plugin_info['comment'] = 'Some comment about the plugin';
  60. $plugin_info['location'] = array('loginpage_menu', 'campushomepage_menu', 'banner'); // The possible locations where the plugins can be used.
  61. $plugin_info['version'] = '0.1 alpha'; // The version number of the plugin.
  62. $plugin_info['author'] = 'Patrick Cool'; // The author of the plugin.
  63. */
  64. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  65. echo '<table class="data_table">';
  66. echo '<tr>';
  67. echo '<th>';
  68. echo get_lang('Plugin');
  69. echo '</th><th>';
  70. echo get_lang('LoginPageMainArea');
  71. echo '</th><th>';
  72. echo get_lang('LoginPageMenu');
  73. echo '</th><th>';
  74. echo get_lang('CampusHomepageMainArea');
  75. echo '</th><th>';
  76. echo get_lang('CampusHomepageMenu');
  77. echo '</th><th>';
  78. echo get_lang('MyCoursesMainArea');
  79. echo '</th><th>';
  80. echo get_lang('MyCoursesMenu');
  81. echo '</th><th>';
  82. echo get_lang('Header');
  83. echo '</th><th>';
  84. echo get_lang('Footer');
  85. echo '</th><th>';
  86. echo get_lang('CourseTool');
  87. echo '</th>';
  88. echo '</tr>';
  89. /* We retrieve all the active plugins. */
  90. //$sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  91. //$result = Database::query($sql);
  92. $result = api_get_settings('Plugins');
  93. //while ($row = Database::fetch_array($result))
  94. foreach ($result as $row) {
  95. $usedplugins[$row['variable']][] = $row['selected_value'];
  96. }
  97. /* We display all the possible plugins and the checkboxes */
  98. foreach ($possibleplugins as $testplugin) {
  99. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin.'/plugin.php';
  100. if (file_exists($plugin_info_file)) {
  101. $plugin_info = array();
  102. include ($plugin_info_file);
  103. echo '<tr>';
  104. echo '<td>';
  105. foreach ($plugin_info as $key => $value) {
  106. if ($key != 'location') {
  107. if ($key == 'title') {
  108. $value = '<strong>'.$value.'</strong>';
  109. }
  110. echo get_lang(ucwords($key)).': '.$value.'<br />';
  111. }
  112. }
  113. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt')) {
  114. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  115. }
  116. echo '</td>';
  117. // column: LoginPageMainArea
  118. if (empty($usedplugins)) {
  119. $usedplugins = array();
  120. }
  121. display_plugin_cell('loginpage_main', $plugin_info, $testplugin, $usedplugins);
  122. display_plugin_cell('loginpage_menu', $plugin_info, $testplugin, $usedplugins);
  123. display_plugin_cell('campushomepage_main', $plugin_info, $testplugin, $usedplugins);
  124. display_plugin_cell('campushomepage_menu', $plugin_info, $testplugin, $usedplugins);
  125. display_plugin_cell('mycourses_main', $plugin_info, $testplugin, $usedplugins);
  126. display_plugin_cell('mycourses_menu', $plugin_info, $testplugin, $usedplugins);
  127. display_plugin_cell('header', $plugin_info, $testplugin, $usedplugins);
  128. display_plugin_cell('footer', $plugin_info, $testplugin, $usedplugins);
  129. display_plugin_cell('course_tool_plugin', $plugin_info, $testplugin, $usedplugins);
  130. echo '</tr>';
  131. }
  132. }
  133. echo '</table>';
  134. echo '<br />';
  135. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  136. echo '<br />';
  137. }
  138. function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins) {
  139. echo '<td align="center">';
  140. if (in_array($location, $plugin_info['location'])) {
  141. if (isset($active_plugins[$location]) && is_array($active_plugins[$location])
  142. && in_array($current_plugin, $active_plugins[$location])) {
  143. $checked = 'checked';
  144. } else {
  145. $checked = '';
  146. }
  147. echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
  148. }
  149. echo "</td>";
  150. }
  151. /**
  152. * This function allows the platform admin to choose the default stylesheet
  153. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  154. * @author Julio Montoya <gugli100@gmail.com>, Chamilo
  155. */
  156. function handle_stylesheets() {
  157. global $_configuration;
  158. // Current style.
  159. $currentstyle = api_get_setting('stylesheets');
  160. $is_style_changeable = false;
  161. if ($_configuration['access_url'] != 1) {
  162. $style_info = api_get_settings('stylesheets', '', 1, 0);
  163. $url_info = api_get_access_url($_configuration['access_url']);
  164. if ($style_info[0]['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  165. $is_style_changeable = true;
  166. echo '<div class="actions" id="stylesheetuploadlink">';
  167. Display::display_icon('upload_stylesheets.png',get_lang('UploadNewStylesheet'),'','32');
  168. echo '<a href="" onclick="javascript: document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\'; return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  169. echo '</div>';
  170. }
  171. } else {
  172. $is_style_changeable = true;
  173. echo '<div class="actions" id="stylesheetuploadlink">';
  174. Display::display_icon('upload_stylesheets.png',get_lang('UploadNewStylesheet'),'','32');
  175. echo '<a href="" onclick="javascript: document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\'; return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  176. echo '</div>';
  177. }
  178. $form = new FormValidator('stylesheet_upload', 'post', 'settings.php?category=stylesheets&showuploadform=true');
  179. $form->addElement('text', 'name_stylesheet', get_lang('NameStylesheet'), array('size' => '40', 'maxlength' => '40'));
  180. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  181. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  182. $allowed_file_types = array('css', 'zip', 'jpeg', 'jpg', 'png', 'gif');
  183. $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  184. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  185. $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
  186. if ($form->validate() && is_writable(api_get_path(SYS_CODE_PATH).'css/')) {
  187. $values = $form->exportValues();
  188. $picture_element = & $form->getElement('new_stylesheet');
  189. $picture = $picture_element->getValue();
  190. upload_stylesheet($values, $picture);
  191. // Add event to the system log.
  192. $time = time();
  193. $user_id = api_get_user_id();
  194. $category = $_GET['category'];
  195. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  196. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  197. } else {
  198. if (!is_writable(api_get_path(SYS_CODE_PATH).'css/')) {
  199. Display::display_error_message(api_get_path(SYS_CODE_PATH).'css/'.get_lang('IsNotWritable'));
  200. } else {
  201. if ($_GET['showuploadform'] == 'true') {
  202. echo '<div id="newstylesheetform">';
  203. } else {
  204. echo '<div id="newstylesheetform" style="display: none;">';
  205. }
  206. // Uploading a new stylesheet.
  207. if ($_configuration['access_url'] == 1) {
  208. $form->display();
  209. } else {
  210. if ($is_style_changeable) {
  211. $form->display();
  212. }
  213. }
  214. echo '</div>';
  215. }
  216. }
  217. // Preview of the stylesheet.
  218. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  219. ?>
  220. <script type="text/javascript">
  221. function load_preview(selectobj){
  222. var style_dir = selectobj.options[selectobj.selectedIndex].value;
  223. parent.preview.location='style_preview.php?style=' + style_dir;
  224. }
  225. </script>
  226. <?php
  227. echo '<form name="stylesheets" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  228. echo '<br /><select name="style" onChange="load_preview(this)" >';
  229. $list_of_styles = array();
  230. $list_of_names = array();
  231. if ($handle = @opendir(api_get_path(SYS_PATH).'main/css/')) {
  232. $counter = 1;
  233. while (false !== ($style_dir = readdir($handle))) {
  234. if (substr($style_dir, 0, 1) == '.') { // Skip directories starting with a '.'
  235. continue;
  236. }
  237. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  238. if (is_dir($dirpath)) {
  239. if ($style_dir != '.' && $style_dir != '..') {
  240. if ($currentstyle == $style_dir || ($style_dir == 'chamilo' && !$currentstyle)) {
  241. $selected = 'selected="true"';
  242. } else {
  243. $selected = '';
  244. }
  245. $show_name = ucwords(str_replace('_', ' ', $style_dir));
  246. if ($is_style_changeable) {
  247. $list_of_styles[$style_dir] = "<option value=\"".$style_dir."\" ".$selected." /> $show_name </option>";
  248. $list_of_names[$style_dir] = $show_name;
  249. //echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  250. //echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  251. } else {
  252. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  253. }
  254. echo '<br />';
  255. $counter++;
  256. }
  257. }
  258. }
  259. @closedir($handle);
  260. }
  261. //Sort styles in alphabetical order
  262. asort($list_of_names);
  263. foreach($list_of_names as $style_dir=>$item) {
  264. echo $list_of_styles[$style_dir];
  265. }
  266. //echo '</select><br />';
  267. echo '</select>&nbsp;&nbsp;';
  268. //var_dump($list_of_names);
  269. if ($is_style_changeable){
  270. echo '<button class="save" type="submit" name="submit_stylesheets"> '.get_lang('SaveSettings').' </button></form>';
  271. }
  272. }
  273. /**
  274. * Creates the folder (if needed) and uploads the stylesheet in it
  275. *
  276. * @param array $values the values of the form
  277. * @param array $picture the values of the uploaded file
  278. *
  279. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  280. * @version May 2008
  281. * @since Dokeos 1.8.5
  282. */
  283. function upload_stylesheet($values, $picture) {
  284. // Valid name for the stylesheet folder.
  285. $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
  286. // Create the folder if needed.
  287. if (!is_dir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/')) {
  288. mkdir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/', api_get_permissions_for_new_directories());
  289. }
  290. $info = pathinfo($picture['name']);
  291. if ($info['extension'] == 'zip') {
  292. // Try to open the file and extract it in the theme.
  293. $zip = new ZipArchive();
  294. if ($zip->open($picture['tmp_name'])) {
  295. // Make sure all files inside the zip are images or css.
  296. $num_files = $zip->numFiles;
  297. $valid = true;
  298. $single_directory = true;
  299. $invalid_files = array();
  300. for ($i = 0; $i < $num_files; $i++) {
  301. $file = $zip->statIndex($i);
  302. if (substr($file['name'], -1) != '/') {
  303. $path_parts = pathinfo($file['name']);
  304. if (!in_array($path_parts['extension'], array('jpg', 'jpeg', 'png', 'gif', 'css'))) {
  305. $valid = false;
  306. $invalid_files[] = $file['name'];
  307. }
  308. }
  309. if (strpos($file['name'], '/') === false) {
  310. $single_directory = false;
  311. }
  312. }
  313. if (!$valid) {
  314. $error_string = '<ul>';
  315. foreach ($invalid_files as $invalid_file) {
  316. $error_string .= '<li>'.$invalid_file.'</li>';
  317. }
  318. $error_string .= '</ul>';
  319. Display::display_error_message(get_lang('ErrorStylesheetFilesExtensionsInsideZip').$error_string, false);
  320. } else {
  321. // If the zip does not contain a single directory, extract it.
  322. if (!$single_directory) {
  323. // Extract zip file.
  324. $zip->extractTo(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/');
  325. } else {
  326. $extraction_path = api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/';
  327. for ($i = 0; $i < $num_files; $i++) {
  328. $entry = $zip->getNameIndex($i);
  329. if (substr($entry, -1) == '/') continue;
  330. $pos_slash = strpos($entry, '/');
  331. $entry_without_first_dir = substr($entry, $pos_slash + 1);
  332. // If there is still a slash, we need to make sure the directories are created.
  333. if (strpos($entry_without_first_dir, '/') !== false) {
  334. if (!is_dir($extraction_path.dirname($entry_without_first_dir))) {
  335. // Create it.
  336. @mkdir($extraction_path.dirname($entry_without_first_dir), $mode = 0777, true);
  337. }
  338. }
  339. $fp = $zip->getStream($entry);
  340. $ofp = fopen( $extraction_path. dirname($entry_without_first_dir).'/'.basename($entry), 'w');
  341. while (!feof($fp)) {
  342. fwrite($ofp, fread($fp, 8192));
  343. }
  344. fclose($fp);
  345. fclose($ofp);
  346. }
  347. }
  348. }
  349. $zip->close();
  350. } else {
  351. Display::display_error_message(get_lang('ErrorReadingZip').$info['extension'], false);
  352. }
  353. } else {
  354. // Simply move the file.
  355. move_uploaded_file($picture['tmp_name'], api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'.$picture['name']);
  356. }
  357. }
  358. /**
  359. * This function allows easy activating and inactivating of plugins
  360. * @todo: A similar function needs to be written to activate or inactivate additional tools.
  361. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  362. */
  363. function store_plugins() {
  364. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  365. global $_configuration;
  366. // Get a list of all current 'Plugins' settings
  367. $installed_plugins = api_get_settings('Plugins','list',$_configuration['access_url']);
  368. $shortlist_installed = array();
  369. foreach ($installed_plugins as $plugin) {
  370. $shortlist_installed[] = $plugin['subkey'];
  371. }
  372. $shortlist_installed = array_flip(array_flip($shortlist_installed));
  373. // Step 1 : We remove all the plugins.
  374. //$sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  375. //Database::query($sql);
  376. $r = api_delete_category_settings('Plugins', $_configuration['access_url']);
  377. $shortlist_required = array();
  378. // Step 2: Looping through all the post values we only store these which are really a valid plugin location.
  379. foreach ($_POST as $form_name => $formvalue) {
  380. $form_name_elements = explode('-', $form_name);
  381. if (is_valid_plugin_location($form_name_elements[1])) {
  382. $shortlist_required[] = $form_name_elements[0];
  383. //$sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  384. //Database::query($sql);
  385. api_add_setting($form_name_elements['0'], $form_name_elements['1'], $form_name_elements['0'], null, 'Plugins', $form_name_elements['0'], null, null, null, $_configuration['access_url'], 1);
  386. // check if there is an install procedure
  387. $pluginpath = api_get_path(SYS_PLUGIN_PATH).$form_name_elements[0].'/install.php';
  388. if (is_file($pluginpath) && is_readable($pluginpath)) {
  389. //execute the install procedure
  390. include $pluginpath;
  391. }
  392. }
  393. }
  394. foreach ($shortlist_installed as $plugin) {
  395. // if one plugin was really deleted, execute the uninstall script
  396. if (!in_array($plugin,$shortlist_required)) {
  397. // check if there is an install procedure
  398. $pluginpath = api_get_path(SYS_PLUGIN_PATH).$plugin.'/uninstall.php';
  399. if (is_file($pluginpath) && is_readable($pluginpath)) {
  400. //execute the install procedure
  401. include $pluginpath;
  402. }
  403. }
  404. }
  405. }
  406. /**
  407. * Check if the post information is really a valid plugin location.
  408. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  409. */
  410. function is_valid_plugin_location($location) {
  411. static $valid_locations = array('loginpage_main', 'loginpage_menu', 'campushomepage_main', 'campushomepage_menu', 'mycourses_main', 'mycourses_menu', 'header', 'footer', 'course_tool_plugin');
  412. return in_array($location, $valid_locations);
  413. }
  414. /**
  415. * This function allows the platform admin to choose which should be the default stylesheet
  416. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  417. */
  418. function store_stylesheets() {
  419. global $_configuration;
  420. // Database table definitions.
  421. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  422. // Insert the stylesheet.
  423. $style = Database::escape_string($_POST['style']);
  424. if (is_style($style)) {
  425. /*
  426. $sql = 'UPDATE '.$table_settings_current.' SET
  427. selected_value = "'.$style.'"
  428. WHERE variable = "stylesheets"
  429. AND category = "stylesheets"';
  430. Database::query($sql);
  431. */
  432. api_set_setting('stylesheets', $style, null, 'stylesheets', $_configuration['access_url']);
  433. }
  434. return true;
  435. }
  436. /**
  437. * This function checks if the given style is a recognize style that exists in the css directory as
  438. * a standalone directory.
  439. * @param string Style
  440. * @return bool True if this style is recognized, false otherwise
  441. */
  442. function is_style($style) {
  443. $dir = api_get_path(SYS_PATH).'main/css/';
  444. $dirs = scandir($dir);
  445. $style = str_replace(array('/', '\\'), array('', ''), $style); // Avoid slashes or backslashes.
  446. if (in_array($style, $dirs) && is_dir($dir.$style)) {
  447. return true;
  448. }
  449. return false;
  450. }
  451. /**
  452. * Search options
  453. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  454. * @author Marco Villegas <marvil07@gmail.com>
  455. */
  456. function handle_search() {
  457. global $SettingsStored, $_configuration;
  458. $search_enabled = api_get_setting('search_enabled');
  459. $settings = api_get_settings('Search');
  460. if ($search_enabled !== 'true' || count($settings) < 1) {
  461. Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
  462. return;
  463. }
  464. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  465. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  466. $renderer = & $form->defaultRenderer();
  467. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  468. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  469. // Search_show_unlinked_results.
  470. $form->addElement('header', null, get_lang('SearchShowUnlinkedResultsTitle'));
  471. $form->addElement('label', null, get_lang('SearchShowUnlinkedResultsComment'));
  472. $values = get_settings_options('search_show_unlinked_results');
  473. $group = array ();
  474. foreach ($values as $key => $value) {
  475. $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
  476. $group[] = $element;
  477. }
  478. $form->addGroup($group, 'search_show_unlinked_results', get_lang('SearchShowUnlinkedResultsComment'), '<br />', false);
  479. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  480. // Search_prefilter_prefix.
  481. $form->addElement('header', null, get_lang('SearchPrefilterPrefix'));
  482. $form->addElement('label', null, get_lang('SearchPrefilterPrefixComment'));
  483. $specific_fields = get_specific_field_list();
  484. $sf_values = array();
  485. foreach ($specific_fields as $sf) {
  486. $sf_values[$sf['code']] = $sf['name'];
  487. }
  488. $group = array();
  489. $form->addElement('select', 'search_prefilter_prefix', get_lang('SearchPrefilterPrefix'), $sf_values, '');
  490. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  491. //$form->addRule('search_show_unlinked_results', get_lang('ThisFieldIsRequired'), 'required');
  492. $form->addElement('style_submit_button', 'search-options-save', get_lang('Ok'));
  493. $form->setDefaults($default_values);
  494. if ($form->validate()) {
  495. $formvalues = $form->exportValues();
  496. $r = api_set_settings_category('Search', 'false', $_configuration['access_url']);
  497. // Save the settings.
  498. foreach ($formvalues as $key => $value) {
  499. $result = api_set_setting($key, $value, null, null);
  500. }
  501. Display :: display_confirmation_message($SettingsStored);
  502. } else {
  503. echo '<div id="search-options-form">';
  504. $form->display();
  505. echo '</div>';
  506. }
  507. }
  508. /**
  509. * Wrapper for the templates
  510. *
  511. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  512. * @version August 2008
  513. * @since Dokeos 1.8.6
  514. */
  515. function handle_templates() {
  516. if ($_GET['action'] != 'add') {
  517. echo '<div class="actions" style="margin-left: 1px;">';
  518. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('new_template.png', get_lang('AddTemplate'),'','32').'</a>';
  519. echo '</div>';
  520. }
  521. if ($_GET['action'] == 'add' || ($_GET['action'] == 'edit' && is_numeric($_GET['id']))) {
  522. add_edit_template();
  523. // Add event to the system log.
  524. $time = time();
  525. $user_id = api_get_user_id();
  526. $category = $_GET['category'];
  527. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  528. } else {
  529. if ($_GET['action'] == 'delete' && is_numeric($_GET['id'])) {
  530. delete_template($_GET['id']);
  531. // Add event to the system log
  532. $time = time();
  533. $user_id = api_get_user_id();
  534. $category = $_GET['category'];
  535. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  536. }
  537. display_templates();
  538. }
  539. }
  540. /**
  541. * Display a sortable table with all the templates that the platform administrator has defined.
  542. *
  543. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  544. * @version August 2008
  545. * @since Dokeos 1.8.6
  546. */
  547. function display_templates() {
  548. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data', 1);
  549. $table->set_additional_parameters(array('category' => Security::remove_XSS($_GET['category'])));
  550. $table->set_header(0, get_lang('Image'), true, array('style' => 'width: 101px;'));
  551. $table->set_header(1, get_lang('Title'));
  552. $table->set_header(2, get_lang('Actions'), false, array('style' => 'width:50px;'));
  553. $table->set_column_filter(2, 'actions_filter');
  554. $table->set_column_filter(0, 'image_filter');
  555. $table->display();
  556. }
  557. /**
  558. * Gets the number of templates that are defined by the platform admin.
  559. *
  560. * @return integer
  561. *
  562. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  563. * @version August 2008
  564. * @since Dokeos 1.8.6
  565. */
  566. function get_number_of_templates() {
  567. // Database table definition.
  568. $table_system_template = Database :: get_main_table('system_template');
  569. // The sql statement.
  570. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  571. $result = Database::query($sql);
  572. $row = Database::fetch_array($result);
  573. // Returning the number of templates.
  574. return $row['total'];
  575. }
  576. /**
  577. * Gets all the template data for the sortable table.
  578. *
  579. * @param integer $from the start of the limit statement
  580. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  581. * @param integer $column the column that is
  582. * @param string $direction the sorting direction (ASC or DESC�
  583. * @return array
  584. *
  585. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  586. * @version August 2008
  587. * @since Dokeos 1.8.6
  588. */
  589. function get_template_data($from, $number_of_items, $column, $direction) {
  590. // Database table definition.
  591. $table_system_template = Database :: get_main_table('system_template');
  592. // The sql statement.
  593. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  594. $sql .= " ORDER BY col$column $direction ";
  595. $sql .= " LIMIT $from,$number_of_items";
  596. $result = Database::query($sql);
  597. while ($row = Database::fetch_array($result)) {
  598. $row['1'] = get_lang($row['1']);
  599. $return[] = $row;
  600. }
  601. // Returning all the information for the sortable table.
  602. return $return;
  603. }
  604. /**
  605. * display the edit and delete icons in the sortable table
  606. *
  607. * @param integer $id the id of the template
  608. * @return html code for the link to edit and delete the template
  609. *
  610. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  611. * @version August 2008
  612. * @since Dokeos 1.8.6
  613. */
  614. function actions_filter($id) {
  615. $return = '<a href="settings.php?category=Templates&amp;action=edit&amp;id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.png', get_lang('Edit'),'',22).'</a>';
  616. $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'),'',22).'</a>';
  617. return $return;
  618. }
  619. /**
  620. * Display the image of the template in the sortable table
  621. *
  622. * @param string $image the image
  623. * @return html code for the image
  624. *
  625. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  626. * @version August 2008
  627. * @since Dokeos 1.8.6
  628. */
  629. function image_filter($image) {
  630. if (!empty($image)) {
  631. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  632. } else {
  633. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  634. }
  635. }
  636. /**
  637. * 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
  638. *
  639. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  640. * @version August 2008
  641. * @since Dokeos 1.8.6
  642. */
  643. function add_edit_template() {
  644. // Initialize the object.
  645. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).'&id='.Security::remove_XSS($_GET['id']));
  646. // Settting the form elements: the header.
  647. if ($_GET['action'] == 'add') {
  648. $title = get_lang('AddTemplate');
  649. } else {
  650. $title = get_lang('EditTemplate');
  651. }
  652. $form->addElement('header', '', $title);
  653. // Settting the form elements: the title of the template.
  654. $form->add_textfield('title', get_lang('Title'), false);
  655. // Settting the form elements: the content of the template (wysiwyg editor).
  656. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  657. // Settting the form elements: the form to upload an image to be used with the template.
  658. $form->addElement('file','template_image',get_lang('Image'),'');
  659. // Settting the form elements: a little bit information about the template image.
  660. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  661. // Getting all the information of the template when editing a template.
  662. if ($_GET['action'] == 'edit') {
  663. // Database table definition.
  664. $table_system_template = Database :: get_main_table('system_template');
  665. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
  666. $result = Database::query($sql);
  667. $row = Database::fetch_array($result);
  668. $defaults['template_id'] = intval($_GET['id']);
  669. $defaults['template_text'] = $row['content'];
  670. // Forcing get_lang().
  671. $defaults['title'] = get_lang($row['title']);
  672. // Adding an extra field: a hidden field with the id of the template we are editing.
  673. $form->addElement('hidden', 'template_id');
  674. // Adding an extra field: a preview of the image that is currently used.
  675. if (!empty($row['image'])) {
  676. $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').'"/>');
  677. } else {
  678. $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').'"/>');
  679. }
  680. // Setting the information of the template that we are editing.
  681. $form->setDefaults($defaults);
  682. }
  683. // Settting the form elements: the submit button.
  684. $form->addElement('style_submit_button' , 'submit', get_lang('Ok') ,'class="save"');
  685. // Setting the rules: the required fields.
  686. $form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  687. $form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  688. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  689. if ($form->validate()) {
  690. $check = Security::check_token('post');
  691. if ($check) {
  692. // Exporting the values.
  693. $values = $form->exportValues();
  694. // Upload the file.
  695. if (!empty($_FILES['template_image']['name'])) {
  696. include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  697. $upload_ok = process_uploaded_file($_FILES['template_image']);
  698. if ($upload_ok) {
  699. // Try to add an extension to the file if it hasn't one.
  700. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  701. // The upload directory.
  702. $upload_dir = api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/';
  703. // Create the directory if it does not exist.
  704. if (!is_dir($upload_dir)) {
  705. mkdir($upload_dir, api_get_permissions_for_new_directories());
  706. }
  707. // Resize the preview image to max default and upload.
  708. require_once api_get_path(LIBRARY_PATH).'image.lib.php';
  709. $temp = new image($_FILES['template_image']['tmp_name']);
  710. $picture_infos = @getimagesize($_FILES['template_image']['tmp_name']);
  711. $max_width_for_picture = 100;
  712. if ($picture_infos[0] > $max_width_for_picture) {
  713. $thumbwidth = $max_width_for_picture;
  714. if (empty($thumbwidth) || $thumbwidth == 0) {
  715. $thumbwidth = $max_width_for_picture;
  716. }
  717. $new_height = round(($thumbwidth / $picture_infos[0]) * $picture_infos[1]);
  718. $temp->resize($thumbwidth, $new_height, 0);
  719. }
  720. $type = $picture_infos[2];
  721. //switch (!empty($type)) { // Obviously wrong line.
  722. switch (type) {
  723. case 2: $temp->send_image('JPG', $upload_dir.$new_file_name);
  724. break;
  725. case 3: $temp->send_image('PNG', $upload_dir.$new_file_name);
  726. break;
  727. case 1: $temp->send_image('GIF', $upload_dir.$new_file_name);
  728. break;
  729. }
  730. }
  731. }
  732. // Store the information in the database (as insert or as update).
  733. $table_system_template = Database :: get_main_table('system_template');
  734. if ($_GET['action'] == 'add') {
  735. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  736. $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".$content_template."','".Database::escape_string($new_file_name)."')";
  737. $result = Database::query($sql);
  738. // Display a feedback message.
  739. Display::display_confirmation_message(get_lang('TemplateAdded'));
  740. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('new_template.png', get_lang('AddTemplate'),'','32').'</a>';
  741. } else {
  742. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  743. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."', content = '".$content_template."'";
  744. if (!empty($new_file_name)) {
  745. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  746. }
  747. $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
  748. $result = Database::query($sql);
  749. // Display a feedback message.
  750. Display::display_confirmation_message(get_lang('TemplateEdited'));
  751. }
  752. }
  753. Security::clear_token();
  754. display_templates();
  755. } else {
  756. $token = Security::get_token();
  757. $form->addElement('hidden','sec_token');
  758. $form->setConstants(array('sec_token' => $token));
  759. // Display the form.
  760. $form->display();
  761. }
  762. }
  763. /**
  764. * Delete a template
  765. *
  766. * @param integer $id the id of the template that has to be deleted
  767. *
  768. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  769. * @version August 2008
  770. * @since Dokeos 1.8.6
  771. */
  772. function delete_template($id) {
  773. // First we remove the image.
  774. $table_system_template = Database :: get_main_table('system_template');
  775. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  776. $result = Database::query($sql);
  777. $row = Database::fetch_array($result);
  778. if (!empty($row['image'])) {
  779. @unlink(api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  780. }
  781. // Now we remove it from the database.
  782. $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  783. $result = Database::query($sql);
  784. // Display a feedback message.
  785. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  786. }
  787. /**
  788. * Returns the list of timezone identifiers used to populate the select
  789. *
  790. * @return array List of timezone identifiers
  791. *
  792. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  793. * @since Chamilo 1.8.7
  794. */
  795. function select_timezone_value() {
  796. return api_get_timezones();
  797. }
  798. /**
  799. * Returns an array containing the list of options used to populate the gradebook_number_decimals variable
  800. *
  801. * @return array List of gradebook_number_decimals options
  802. *
  803. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  804. */
  805. function select_gradebook_number_decimals() {
  806. return array('0', '1', '2');
  807. }
  808. /**
  809. * Updates the gradebook score custom values using the scoredisplay class of the
  810. * gradebook module
  811. *
  812. * @param array List of gradebook score custom values
  813. *
  814. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  815. */
  816. function update_gradebook_score_display_custom_values($values) {
  817. require_once api_get_path(SYS_CODE_PATH).'gradebook/lib/scoredisplay.class.php';
  818. $scoredisplay = ScoreDisplay::instance();
  819. $scores = $values['gradebook_score_display_custom_values_endscore'];
  820. $displays = $values['gradebook_score_display_custom_values_displaytext'];
  821. $nr_displays = count($displays);
  822. $final = array();
  823. for ($i = 1; $i < $nr_displays; $i++) {
  824. if (!empty($scores[$i]) && !empty($displays[$i])) {
  825. $final[$i]['score'] = $scores[$i];
  826. $final[$i]['display'] = $displays[$i];
  827. }
  828. }
  829. $scoredisplay->update_custom_score_display_settings($final);
  830. }