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