settings.lib.php 57 KB

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