settings.lib.php 44 KB

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