settings.lib.php 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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. define('CSS_UPLOAD_PATH', api_get_path(SYS_APP_PATH).'Resources/public/css/themes/');
  13. use Symfony\Component\Filesystem\Filesystem;
  14. /**
  15. * This function allows easy activating and inactivating of regions
  16. * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  17. */
  18. function handleRegions()
  19. {
  20. if (isset($_POST['submit_plugins'])) {
  21. storeRegions();
  22. // Add event to the system log.
  23. $user_id = api_get_user_id();
  24. $category = $_GET['category'];
  25. Event::addEvent(
  26. LOG_CONFIGURATION_SETTINGS_CHANGE,
  27. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  28. $category,
  29. api_get_utc_datetime(),
  30. $user_id
  31. );
  32. Display :: display_confirmation_message(get_lang('SettingsStored'));
  33. }
  34. $plugin_obj = new AppPlugin();
  35. $possible_plugins = $plugin_obj->read_plugins_from_path();
  36. $installed_plugins = $plugin_obj->get_installed_plugins();
  37. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  38. echo '<table class="data_table">';
  39. echo '<tr>';
  40. echo '<th width="400px">';
  41. echo get_lang('Plugin');
  42. echo '</th><th>';
  43. echo get_lang('Regions');
  44. echo '</th>';
  45. echo '</th>';
  46. echo '</tr>';
  47. /* We display all the possible plugins and the checkboxes */
  48. $plugin_region_list = array();
  49. $my_plugin_list = $plugin_obj->get_plugin_regions();
  50. foreach ($my_plugin_list as $plugin_item) {
  51. $plugin_region_list[$plugin_item] = $plugin_item;
  52. }
  53. // Removing course tool
  54. unset($plugin_region_list['course_tool_plugin']);
  55. foreach ($installed_plugins as $plugin) {
  56. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
  57. if (file_exists($plugin_info_file)) {
  58. $plugin_info = array();
  59. require $plugin_info_file;
  60. if (isset($_GET['name']) && $_GET['name'] == $plugin) {
  61. echo '<tr class="row_selected">';
  62. } else {
  63. echo '<tr>';
  64. }
  65. echo '<td>';
  66. echo '<h4>'.$plugin_info['title'].' <small>v'.$plugin_info['version'].'</small></h4>';
  67. echo '<p>'.$plugin_info['comment'].'</p>';
  68. echo '</td><td>';
  69. $selected_plugins = $plugin_obj->get_areas_by_plugin($plugin);
  70. if (isset($plugin_info['is_course_plugin']) && $plugin_info['is_course_plugin']) {
  71. $region_list = array('course_tool_plugin' => 'course_tool_plugin');
  72. } else {
  73. $region_list = $plugin_region_list;
  74. }
  75. echo Display::select('plugin_'.$plugin.'[]', $region_list, $selected_plugins, array('multiple' => 'multiple', 'style' => 'width:500px'), true, get_lang('None'));
  76. echo '</td></tr>';
  77. }
  78. }
  79. echo '</table>';
  80. echo '<br />';
  81. echo '<button class="btn btn-success" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  82. }
  83. function handleExtensions()
  84. {
  85. echo Display::page_subheader(get_lang('ConfigureExtensions'));
  86. echo '<a class="btn btn-success" href="configure_extensions.php?display=ppt2lp" role="button">'.get_lang('Ppt2lp').'</a>';
  87. }
  88. /**
  89. * This function allows easy activating and inactivating of plugins
  90. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  91. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  92. * @author Julio Montoya <gugli100@gmail.com> Beeznest 2012
  93. */
  94. function handlePlugins()
  95. {
  96. $plugin_obj = new AppPlugin();
  97. $token = Security::get_token();
  98. if (isset($_POST['submit_plugins'])) {
  99. storePlugins();
  100. // Add event to the system log.
  101. $user_id = api_get_user_id();
  102. $category = $_GET['category'];
  103. Event::addEvent(
  104. LOG_CONFIGURATION_SETTINGS_CHANGE,
  105. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  106. $category,
  107. api_get_utc_datetime(),
  108. $user_id
  109. );
  110. Display :: display_confirmation_message(get_lang('SettingsStored'));
  111. }
  112. $all_plugins = $plugin_obj->read_plugins_from_path();
  113. $installed_plugins = $plugin_obj->get_installed_plugins();
  114. //Plugins NOT installed
  115. echo Display::page_subheader(get_lang('Plugins'));
  116. echo '<form class="form-horizontal" name="plugins" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'&sec_token=' . $token . '">';
  117. echo '<table class="data_table">';
  118. echo '<tr>';
  119. echo '<th width="20px">';
  120. echo get_lang('Action');
  121. echo '</th><th>';
  122. echo get_lang('Description');
  123. echo '</th>';
  124. echo '</tr>';
  125. $plugin_list = array();
  126. $my_plugin_list = $plugin_obj->get_plugin_regions();
  127. foreach($my_plugin_list as $plugin_item) {
  128. $plugin_list[$plugin_item] = $plugin_item;
  129. }
  130. foreach ($all_plugins as $plugin) {
  131. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$plugin.'/plugin.php';
  132. if (file_exists($plugin_info_file)) {
  133. $plugin_info = array();
  134. require $plugin_info_file;
  135. if (in_array($plugin, $installed_plugins)) {
  136. echo '<tr class="row_selected">';
  137. } else {
  138. echo '<tr>';
  139. }
  140. echo '<td>';
  141. //Checkbox
  142. if (in_array($plugin, $installed_plugins)) {
  143. echo '<input type="checkbox" name="plugin_'.$plugin.'[]" checked="checked">';
  144. } else {
  145. echo '<input type="checkbox" name="plugin_'.$plugin.'[]">';
  146. }
  147. echo '</td><td>';
  148. echo '<h4>'.$plugin_info['title'].' <small>v '.$plugin_info['version'].'</small></h4>';
  149. echo '<p>'.$plugin_info['comment'].'</p>';
  150. echo '<p>'.get_lang('Author').': '.$plugin_info['author'].'</p>';
  151. echo '<div class="btn-group">';
  152. if (in_array($plugin, $installed_plugins)) {
  153. echo Display::url('<em class="fa fa-cogs"></em> '.get_lang('Configure'), 'configure_plugin.php?name='.$plugin, array('class' => 'btn btn-default'));
  154. echo Display::url('<em class="fa fa-th-large"></em> '.get_lang('Regions'), 'settings.php?category=Regions&name='.$plugin, array('class' => 'btn btn-default'));
  155. }
  156. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$plugin.'/readme.txt')) {
  157. echo Display::url(
  158. "<em class='fa fa-file-text-o'></em> readme.txt",
  159. api_get_path(WEB_PLUGIN_PATH) . $plugin . "/readme.txt",
  160. [
  161. 'class' => 'btn btn-default ajax',
  162. 'data-title' => $plugin_info['title'],
  163. 'data-size' => 'lg',
  164. '_target' => '_blank'
  165. ]
  166. );
  167. }
  168. $readmeFile = api_get_path(SYS_PLUGIN_PATH).$plugin.'/README.md';
  169. if (file_exists($readmeFile)) {
  170. echo Display::url(
  171. "<em class='fa fa-file-text-o'></em> README.md",
  172. api_get_path(WEB_AJAX_PATH).'plugin.ajax.php?a=md_to_html&plugin='.$plugin,
  173. [
  174. 'class' => 'btn btn-default ajax',
  175. 'data-title' => $plugin_info['title'],
  176. 'data-size' => 'lg',
  177. '_target' => '_blank'
  178. ]
  179. );
  180. }
  181. echo '</div>';
  182. echo '</td></tr>';
  183. }
  184. }
  185. echo '</table>';
  186. echo '<div class="form-actions bottom_actions">';
  187. echo '<button class="btn btn-success" type="submit" name="submit_plugins">'.
  188. get_lang('EnablePlugins').'</button>';
  189. echo '</div>';
  190. echo '</form>';
  191. }
  192. /**
  193. * This function allows the platform admin to choose the default stylesheet
  194. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  195. * @author Julio Montoya <gugli100@gmail.com>, Chamilo
  196. */
  197. function handleStylesheets()
  198. {
  199. global $_configuration;
  200. // Current style.
  201. $currentstyle = api_get_setting('stylesheets');
  202. $is_style_changeable = isStyleChangeable();
  203. $form = new FormValidator(
  204. 'stylesheet_upload',
  205. 'post',
  206. 'settings.php?category=Stylesheets#tabs-3'
  207. );
  208. $form->addElement('text', 'name_stylesheet', get_lang('NameStylesheet'),
  209. array('size' => '40', 'maxlength' => '40'));
  210. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  211. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  212. $allowed_file_types = getAllowedFileTypes();
  213. $form->addRule('new_stylesheet', get_lang('InvalidExtension') . ' (' . implode(',', $allowed_file_types) . ')',
  214. 'filetype', $allowed_file_types);
  215. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  216. $form->addButtonUpload(get_lang('Upload'), 'stylesheet_upload');
  217. $show_upload_form = false;
  218. if (!is_writable(CSS_UPLOAD_PATH)) {
  219. Display::display_error_message(CSS_UPLOAD_PATH.get_lang('IsNotWritable'));
  220. } else {
  221. // Uploading a new stylesheet.
  222. if ($_configuration['access_url'] == 1) {
  223. $show_upload_form = true;
  224. } else {
  225. if ($is_style_changeable) {
  226. $show_upload_form = true;
  227. }
  228. }
  229. }
  230. // Stylesheet upload.
  231. if (isset($_POST['stylesheet_upload'])) {
  232. if ($form->validate()) {
  233. $values = $form->exportValues();
  234. $picture_element = $form->getElement('new_stylesheet');
  235. $picture = $picture_element->getValue();
  236. $result = uploadStylesheet($values, $picture);
  237. // Add event to the system log.
  238. $user_id = api_get_user_id();
  239. $category = $_GET['category'];
  240. Event::addEvent(
  241. LOG_CONFIGURATION_SETTINGS_CHANGE,
  242. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  243. $category,
  244. api_get_utc_datetime(),
  245. $user_id
  246. );
  247. if ($result) {
  248. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  249. }
  250. }
  251. }
  252. $form_change = new FormValidator(
  253. 'stylesheet_upload',
  254. 'post',
  255. api_get_self().'?category=Stylesheets',
  256. null,
  257. array('id' => 'stylesheets_id')
  258. );
  259. $list_of_names = array();
  260. $selected = '';
  261. $dirpath = '';
  262. $safe_style_dir = '';
  263. if ($handle = @opendir(CSS_UPLOAD_PATH)) {
  264. $counter = 1;
  265. while (false !== ($style_dir = readdir($handle))) {
  266. if (substr($style_dir, 0, 1) == '.') {
  267. // Skip directories starting with a '.'
  268. continue;
  269. }
  270. $dirpath = CSS_UPLOAD_PATH.$style_dir;
  271. if (is_dir($dirpath)) {
  272. if ($style_dir != '.' && $style_dir != '..') {
  273. if (isset($_POST['style']) &&
  274. (isset($_POST['preview']) || isset($_POST['download'])) &&
  275. $_POST['style'] == $style_dir
  276. ) {
  277. $safe_style_dir = $style_dir;
  278. } else {
  279. if ($currentstyle == $style_dir || ($style_dir == 'chamilo' && !$currentstyle)) {
  280. if (isset($_POST['style'])) {
  281. $selected = Database::escape_string($_POST['style']);
  282. } else {
  283. $selected = $style_dir;
  284. }
  285. }
  286. }
  287. $show_name = ucwords(str_replace('_', ' ', $style_dir));
  288. if ($is_style_changeable) {
  289. $list_of_names[$style_dir] = $show_name;
  290. }
  291. $counter++;
  292. }
  293. }
  294. }
  295. closedir($handle);
  296. }
  297. // Sort styles in alphabetical order.
  298. asort($list_of_names);
  299. $select_list = array();
  300. foreach ($list_of_names as $style_dir => $item) {
  301. $select_list[$style_dir] = $item;
  302. }
  303. $styles = &$form_change->addElement('select', 'style', get_lang('NameStylesheet'), $select_list);
  304. $styles->setSelected($selected);
  305. if ($form_change->validate()) {
  306. // Submit stylesheets.
  307. if (isset($_POST['save'])) {
  308. storeStylesheets();
  309. Display::display_normal_message(get_lang('Saved'));
  310. }
  311. if (isset($_POST['download'])) {
  312. generateCSSDownloadLink($safe_style_dir);
  313. }
  314. }
  315. $logoForm = new FormValidator(
  316. 'logo_upload',
  317. 'post',
  318. 'settings.php?category=Stylesheets#tabs-2'
  319. );
  320. $logoForm->addHtml(Display::return_message(sprintf(get_lang('TheLogoMustBeSizeXAndFormatY'), '250 x 70', 'PNG'), 'info'));
  321. $dir = api_get_path(SYS_PUBLIC_PATH).'css/themes/' . $selected . '/images/';
  322. $url = api_get_path(WEB_CSS_PATH).'themes/' . $selected . '/images/';
  323. $logoFileName = 'header-logo.png';
  324. $newLogoFileName = 'header-logo-custom.png';
  325. if (is_file($dir.$newLogoFileName)) {
  326. $logoForm->addLabel(get_lang('CurrentLogo'), '<img id="header-logo-custom" src="'. $url . $newLogoFileName .'?'. time() . '">');
  327. } else {
  328. $logoForm->addLabel(get_lang('CurrentLogo'), '<img id="header-logo-custom" src="'. $url . $logoFileName .'?'. time() . '">');
  329. }
  330. $logoForm->addFile('new_logo', get_lang('UpdateLogo'));
  331. $allowedFileTypes = ['png'];
  332. if (isset($_POST['logo_reset'])) {
  333. if (is_file($dir.$newLogoFileName)) {
  334. unlink($dir.$newLogoFileName);
  335. Display::display_normal_message(get_lang('ResetToTheOriginalLogo'));
  336. echo '<script>'
  337. . '$("#header-logo").attr("src","'.$url.$logoFileName.'");'
  338. . '</script>';
  339. }
  340. } elseif (isset($_POST['logo_upload'])) {
  341. $logoForm->addRule('new_logo', get_lang('InvalidExtension').' ('.implode(',', $allowedFileTypes).')', 'filetype', $allowedFileTypes);
  342. $logoForm->addRule('new_logo', get_lang('ThisFieldIsRequired'), 'required');
  343. if ($logoForm->validate()) {
  344. $imageInfo = getimagesize($_FILES['new_logo']['tmp_name']);
  345. $width = $imageInfo[0];
  346. $height = $imageInfo[1];
  347. if ($width <= 250 && $height <= 70 ) {
  348. if (is_file($dir.$newLogoFileName)) {
  349. unlink($dir.$newLogoFileName);
  350. }
  351. $status = move_uploaded_file($_FILES['new_logo']['tmp_name'], $dir.$newLogoFileName);
  352. if ($status) {
  353. Display::display_normal_message(get_lang('NewLogoUpdated'));
  354. echo '<script>'
  355. . '$("#header-logo").attr("src","'.$url.$newLogoFileName.'");'
  356. . '</script>';
  357. } else {
  358. Display::display_error_message('Error - '.get_lang('UplNoFileUploaded'));
  359. }
  360. } else {
  361. Display::display_error_message('Error - '.get_lang('InvalidImageDimensions'));
  362. }
  363. }
  364. }
  365. if ($is_style_changeable) {
  366. $group = [
  367. $form_change->addButtonSave(get_lang('SaveSettings'), 'save', true),
  368. $form_change->addButtonPreview(get_lang('Preview'), 'preview', true),
  369. $form_change->addButtonDownload(get_lang('Download'), 'download', true)
  370. ];
  371. $form_change->addGroup($group);
  372. $logoGroup = [
  373. $logoForm->addButtonUpload(get_lang('Upload'), 'logo_upload', true),
  374. $logoForm->addButtonCancel(get_lang('Reset'), 'logo_reset', true)
  375. ];
  376. $logoForm->addGroup($logoGroup);
  377. if ($show_upload_form) {
  378. echo '<script>
  379. $(function() {
  380. $( "#tabs" ).tabs();
  381. });
  382. </script>';
  383. echo Display::tabs(
  384. array(get_lang('Update'),get_lang('UpdateLogo'), get_lang('UploadNewStylesheet')),
  385. array($form_change->return_form(), $logoForm->return_form(), $form->return_form())
  386. );
  387. } else {
  388. $form_change->display();
  389. }
  390. //Little hack to update the logo image in update form when submiting
  391. if (isset($_POST['logo_reset'])) {
  392. echo '<script>'
  393. . '$("#header-logo-custom").attr("src","'.$url.$logoFileName.'");'
  394. . '</script>';
  395. } elseif (isset($_POST['logo_upload']) && is_file($dir.$newLogoFileName)) {
  396. echo '<script>'
  397. . '$("#header-logo-custom").attr("src","'.$url.$newLogoFileName.'");'
  398. . '</script>';
  399. }
  400. } else {
  401. $form_change->freeze();
  402. }
  403. }
  404. /**
  405. * Creates the folder (if needed) and uploads the stylesheet in it
  406. * @param array $values the values of the form
  407. * @param array $picture the values of the uploaded file
  408. * @return bool
  409. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  410. * @version May 2008
  411. * @since v1.8.5
  412. */
  413. function uploadStylesheet($values, $picture)
  414. {
  415. $result = false;
  416. // Valid name for the stylesheet folder.
  417. $style_name = api_preg_replace('/[^A-Za-z0-9]/', '', $values['name_stylesheet']);
  418. $cssToUpload = CSS_UPLOAD_PATH;
  419. // Create the folder if needed.
  420. if (!is_dir($cssToUpload.$style_name.'/')) {
  421. mkdir($cssToUpload.$style_name.'/', api_get_permissions_for_new_directories());
  422. }
  423. $info = pathinfo($picture['name']);
  424. if ($info['extension'] == 'zip') {
  425. // Try to open the file and extract it in the theme.
  426. $zip = new ZipArchive();
  427. if ($zip->open($picture['tmp_name'])) {
  428. // Make sure all files inside the zip are images or css.
  429. $num_files = $zip->numFiles;
  430. $valid = true;
  431. $single_directory = true;
  432. $invalid_files = array();
  433. $allowedFiles = getAllowedFileTypes();
  434. for ($i = 0; $i < $num_files; $i++) {
  435. $file = $zip->statIndex($i);
  436. if (substr($file['name'], -1) != '/') {
  437. $path_parts = pathinfo($file['name']);
  438. if (!in_array($path_parts['extension'], $allowedFiles)) {
  439. $valid = false;
  440. $invalid_files[] = $file['name'];
  441. }
  442. }
  443. if (strpos($file['name'], '/') === false) {
  444. $single_directory = false;
  445. }
  446. }
  447. if (!$valid) {
  448. $error_string = '<ul>';
  449. foreach ($invalid_files as $invalid_file) {
  450. $error_string .= '<li>'.$invalid_file.'</li>';
  451. }
  452. $error_string .= '</ul>';
  453. Display::display_error_message(
  454. get_lang('ErrorStylesheetFilesExtensionsInsideZip').$error_string,
  455. false
  456. );
  457. } else {
  458. // If the zip does not contain a single directory, extract it.
  459. if (!$single_directory) {
  460. // Extract zip file.
  461. $zip->extractTo($cssToUpload.$style_name.'/');
  462. $result = true;
  463. } else {
  464. $extraction_path = $cssToUpload.$style_name.'/';
  465. for ($i = 0; $i < $num_files; $i++) {
  466. $entry = $zip->getNameIndex($i);
  467. if (substr($entry, -1) == '/') {
  468. continue;
  469. }
  470. $pos_slash = strpos($entry, '/');
  471. $entry_without_first_dir = substr($entry, $pos_slash + 1);
  472. // If there is still a slash, we need to make sure the directories are created.
  473. if (strpos($entry_without_first_dir, '/') !== false) {
  474. if (!is_dir($extraction_path.dirname($entry_without_first_dir))) {
  475. // Create it.
  476. @mkdir($extraction_path.dirname($entry_without_first_dir), $mode = 0777, true);
  477. }
  478. }
  479. $fp = $zip->getStream($entry);
  480. $ofp = fopen($extraction_path.dirname($entry_without_first_dir).'/'.basename($entry), 'w');
  481. while (!feof($fp)) {
  482. fwrite($ofp, fread($fp, 8192));
  483. }
  484. fclose($fp);
  485. fclose($ofp);
  486. }
  487. $result = true;
  488. }
  489. }
  490. $zip->close();
  491. } else {
  492. Display::display_error_message(get_lang('ErrorReadingZip').$info['extension'], false);
  493. }
  494. } else {
  495. // Simply move the file.
  496. move_uploaded_file($picture['tmp_name'], $cssToUpload.$style_name.'/'.$picture['name']);
  497. $result = true;
  498. }
  499. if ($result) {
  500. $fs = new Filesystem();
  501. $fs->mirror($cssToUpload, api_get_path(SYS_PATH).'web/css/themes/');
  502. }
  503. return $result;
  504. }
  505. /**
  506. * Store plugin regions.
  507. */
  508. function storeRegions()
  509. {
  510. $plugin_obj = new AppPlugin();
  511. // Get a list of all current 'Plugins' settings
  512. $installed_plugins = $plugin_obj->get_installed_plugins();
  513. $shortlist_installed = array();
  514. if (!empty($installed_plugins)) {
  515. foreach ($installed_plugins as $plugin) {
  516. if (isset($plugin['subkey'])) {
  517. $shortlist_installed[] = $plugin['subkey'];
  518. }
  519. }
  520. }
  521. $shortlist_installed = array_flip(array_flip($shortlist_installed));
  522. $plugin_list = $plugin_obj->read_plugins_from_path();
  523. foreach ($plugin_list as $plugin) {
  524. if (isset($_POST['plugin_'.$plugin])) {
  525. $areas_to_installed = $_POST['plugin_'.$plugin];
  526. if (!empty($areas_to_installed)) {
  527. $plugin_obj->remove_all_regions($plugin);
  528. foreach ($areas_to_installed as $region) {
  529. if (!empty($region) && $region != '-1' ) {
  530. $plugin_obj->add_to_region($plugin, $region);
  531. }
  532. }
  533. }
  534. }
  535. }
  536. }
  537. /**
  538. * This function allows easy activating and inactivating of plugins
  539. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  540. */
  541. function storePlugins()
  542. {
  543. $appPlugin = new AppPlugin();
  544. // Get a list of all current 'Plugins' settings
  545. $plugin_list = $appPlugin->read_plugins_from_path();
  546. $installed_plugins = array();
  547. foreach ($plugin_list as $plugin) {
  548. if (isset($_POST['plugin_'.$plugin])) {
  549. $appPlugin->install($plugin);
  550. $installed_plugins[] = $plugin;
  551. }
  552. }
  553. if (!empty($installed_plugins)) {
  554. $remove_plugins = array_diff($plugin_list, $installed_plugins);
  555. } else {
  556. $remove_plugins = $plugin_list;
  557. }
  558. foreach ($remove_plugins as $plugin) {
  559. $appPlugin->uninstall($plugin);
  560. }
  561. }
  562. /**
  563. * This function allows the platform admin to choose which should be the default stylesheet
  564. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  565. */
  566. function storeStylesheets()
  567. {
  568. // Insert the stylesheet.
  569. if (isStyle($_POST['style'])) {
  570. api_set_setting(
  571. 'stylesheets',
  572. $_POST['style'],
  573. null,
  574. 'stylesheets',
  575. api_get_current_access_url_id()
  576. );
  577. }
  578. return true;
  579. }
  580. /**
  581. * This function checks if the given style is a recognize style that exists in the css directory as
  582. * a standalone directory.
  583. * @param string Style
  584. * @return bool True if this style is recognized, false otherwise
  585. */
  586. function isStyle($style)
  587. {
  588. $dir = CSS_UPLOAD_PATH;
  589. $dirs = scandir($dir);
  590. $style = str_replace(array('/', '\\'), array('', ''), $style); // Avoid slashes or backslashes.
  591. if (in_array($style, $dirs) && is_dir($dir.$style)) {
  592. return true;
  593. }
  594. return false;
  595. }
  596. /**
  597. * Search options
  598. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  599. * @author Marco Villegas <marvil07@gmail.com>
  600. */
  601. function handleSearch()
  602. {
  603. global $SettingsStored, $_configuration;
  604. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  605. $search_enabled = api_get_setting('search_enabled');
  606. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  607. $values = api_get_settings_options('search_enabled');
  608. $form->addElement('header', null, get_lang('SearchEnabledTitle'));
  609. $group = formGenerateElementsGroup($form, $values, 'search_enabled');
  610. //SearchEnabledComment
  611. $form->addGroup($group, 'search_enabled', array(get_lang('SearchEnabledTitle'), get_lang('SearchEnabledComment')), '<br />', false);
  612. $search_enabled = api_get_setting('search_enabled');
  613. if ($form->validate()) {
  614. $formValues = $form->exportValues();
  615. setConfigurationSettingsInDatabase($formValues, $_configuration['access_url']);
  616. $search_enabled = $formValues['search_enabled'];
  617. Display::display_confirmation_message($SettingsStored);
  618. }
  619. $specific_fields = get_specific_field_list();
  620. if ($search_enabled == 'true') {
  621. $values = api_get_settings_options('search_show_unlinked_results');
  622. $group = formGenerateElementsGroup($form, $values, 'search_show_unlinked_results');
  623. $form->addGroup($group, 'search_show_unlinked_results', array(get_lang('SearchShowUnlinkedResultsTitle'),get_lang('SearchShowUnlinkedResultsComment')), '', false);
  624. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  625. $sf_values = array();
  626. foreach ($specific_fields as $sf) {
  627. $sf_values[$sf['code']] = $sf['name'];
  628. }
  629. $url = Display::div(Display::url(get_lang('AddSpecificSearchField'), 'specific_fields.php'), array('class'=>'sectioncomment'));
  630. if (empty($sf_values)) {
  631. $form->addElement('label', [get_lang('SearchPrefilterPrefix'), $url]);
  632. } else {
  633. $form->addElement('select', 'search_prefilter_prefix', array(get_lang('SearchPrefilterPrefix'), $url), $sf_values, '');
  634. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  635. }
  636. }
  637. $default_values['search_enabled'] = $search_enabled;
  638. $form->addButtonSave(get_lang('Save'));
  639. $form->setDefaults($default_values);
  640. echo '<div id="search-options-form">';
  641. $form->display();
  642. echo '</div>';
  643. if ($search_enabled == 'true') {
  644. $xapianPath = api_get_path(SYS_UPLOAD_PATH).'plugins/xapian/searchdb';
  645. /*
  646. @todo Test the Xapian connection
  647. if (extension_loaded('xapian')) {
  648. require_once 'xapian.php';
  649. try {
  650. $db = new XapianDatabase($xapianPath.'/');
  651. } catch (Exception $e) {
  652. var_dump($e->getMessage());
  653. }
  654. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  655. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  656. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  657. $indexable = new IndexableChunk();
  658. $indexable->addValue("content", 'Test');
  659. $di = new ChamiloIndexer();
  660. $di->connectDb(NULL, NULL, 'english');
  661. $di->addChunk($indexable);
  662. $did = $di->index();
  663. }
  664. */
  665. $xapianLoaded = Display::return_icon('bullet_green.png', get_lang('Ok'));
  666. $dir_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
  667. $dir_is_writable = Display::return_icon('bullet_green.png', get_lang('Ok'));
  668. $specific_fields_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
  669. //Testing specific fields
  670. if (empty($specific_fields)) {
  671. $specific_fields_exists = Display::return_icon('bullet_red.png', get_lang('AddSpecificSearchField'));
  672. }
  673. //Testing xapian extension
  674. if (!extension_loaded('xapian')) {
  675. $xapianLoaded = Display::return_icon('bullet_red.png', get_lang('Error'));
  676. }
  677. //Testing xapian searchdb path
  678. if (!is_dir($xapianPath)) {
  679. $dir_exists = Display::return_icon('bullet_red.png', get_lang('Error'));
  680. }
  681. //Testing xapian searchdb path is writable
  682. if (!is_writable($xapianPath)) {
  683. $dir_is_writable = Display::return_icon('bullet_red.png', get_lang('Error'));
  684. }
  685. $data = array();
  686. $data[] = array(get_lang('XapianModuleInstalled'), $xapianLoaded);
  687. $data[] = array(get_lang('DirectoryExists').' - '.$xapianPath, $dir_exists);
  688. $data[] = array(get_lang('IsWritable').' - '.$xapianPath, $dir_is_writable);
  689. $data[] = array(get_lang('SpecificSearchFieldsAvailable'), $specific_fields_exists);
  690. showSearchSettingsTable($data);
  691. showSearchToolsStatusTable();
  692. }
  693. }
  694. /**
  695. * Wrapper for the templates
  696. *
  697. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  698. * @author Julio Montoya.
  699. * @version August 2008
  700. * @since v1.8.6
  701. */
  702. function handleTemplates()
  703. {
  704. /* Drive-by fix to avoid undefined var warnings, without repeating
  705. * isset() combos all over the place. */
  706. $action = isset($_GET['action']) ? $_GET['action'] : "invalid";
  707. if ($action != 'add') {
  708. echo '<div class="actions" style="margin-left: 1px;">';
  709. echo '<a href="settings.php?category=Templates&action=add">'.
  710. Display::return_icon('new_template.png', get_lang('AddTemplate'),'',ICON_SIZE_MEDIUM).'</a>';
  711. echo '</div>';
  712. }
  713. if ($action == 'add' || ($action == 'edit' && is_numeric($_GET['id']))) {
  714. addEditTemplate();
  715. // Add event to the system log.
  716. $user_id = api_get_user_id();
  717. $category = $_GET['category'];
  718. Event::addEvent(
  719. LOG_CONFIGURATION_SETTINGS_CHANGE,
  720. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  721. $category,
  722. api_get_utc_datetime(),
  723. $user_id
  724. );
  725. } else {
  726. if ($action == 'delete' && is_numeric($_GET['id'])) {
  727. deleteTemplate($_GET['id']);
  728. // Add event to the system log
  729. $user_id = api_get_user_id();
  730. $category = $_GET['category'];
  731. Event::addEvent(
  732. LOG_CONFIGURATION_SETTINGS_CHANGE,
  733. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  734. $category,
  735. api_get_utc_datetime(),
  736. $user_id
  737. );
  738. }
  739. displayTemplates();
  740. }
  741. }
  742. /**
  743. * Display a sortable table with all the templates that the platform administrator has defined.
  744. *
  745. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  746. * @version August 2008
  747. * @since v1.8.6
  748. */
  749. function displayTemplates()
  750. {
  751. $table = new SortableTable('templates', 'getNumberOfTemplates', 'getTemplateData', 1);
  752. $table->set_additional_parameters(array('category' => Security::remove_XSS($_GET['category'])));
  753. $table->set_header(0, get_lang('Image'), true, array('style' => 'width: 101px;'));
  754. $table->set_header(1, get_lang('Title'));
  755. $table->set_header(2, get_lang('Actions'), false, array('style' => 'width:50px;'));
  756. $table->set_column_filter(2, 'actionsFilter');
  757. $table->set_column_filter(0, 'searchImageFilter');
  758. $table->display();
  759. }
  760. /**
  761. * Gets the number of templates that are defined by the platform admin.
  762. *
  763. * @return integer
  764. *
  765. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  766. * @version August 2008
  767. * @since v1.8.6
  768. */
  769. function getNumberOfTemplates()
  770. {
  771. // Database table definition.
  772. $table_system_template = Database :: get_main_table('system_template');
  773. // The sql statement.
  774. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  775. $result = Database::query($sql);
  776. $row = Database::fetch_array($result);
  777. // Returning the number of templates.
  778. return $row['total'];
  779. }
  780. /**
  781. * Gets all the template data for the sortable table.
  782. *
  783. * @param integer $from the start of the limit statement
  784. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  785. * @param integer $column the column that is
  786. * @param string $direction the sorting direction (ASC or DESC�
  787. * @return array
  788. *
  789. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  790. * @version August 2008
  791. * @since v1.8.6
  792. */
  793. function getTemplateData($from, $number_of_items, $column, $direction)
  794. {
  795. // Database table definition.
  796. $table_system_template = Database :: get_main_table('system_template');
  797. // The sql statement.
  798. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  799. $sql .= " ORDER BY col$column $direction ";
  800. $sql .= " LIMIT $from,$number_of_items";
  801. $result = Database::query($sql);
  802. $return = array();
  803. while ($row = Database::fetch_array($result)) {
  804. $row['1'] = get_lang($row['1']);
  805. $return[] = $row;
  806. }
  807. // Returning all the information for the sortable table.
  808. return $return;
  809. }
  810. /**
  811. * display the edit and delete icons in the sortable table
  812. *
  813. * @param integer $id the id of the template
  814. * @return string code for the link to edit and delete the template
  815. *
  816. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  817. * @version August 2008
  818. * @since v1.8.6
  819. */
  820. function actionsFilter($id) {
  821. $return = '<a href="settings.php?category=Templates&action=edit&id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>';
  822. $return .= '<a href="settings.php?category=Templates&action=delete&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>';
  823. return $return;
  824. }
  825. /**
  826. * Display the image of the template in the sortable table
  827. *
  828. * @param string $image the image
  829. * @return string code for the image
  830. *
  831. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  832. * @version August 2008
  833. * @since v1.8.6
  834. */
  835. function searchImageFilter($image)
  836. {
  837. if (!empty($image)) {
  838. return '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  839. } else {
  840. return '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  841. }
  842. }
  843. /**
  844. * Add (or edit) a template. This function displays the form and also takes
  845. * care of uploading the image and storing the information in the database
  846. *
  847. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  848. * @version August 2008
  849. * @since v1.8.6
  850. */
  851. function addEditTemplate()
  852. {
  853. // Initialize the object.
  854. $id = isset($_GET['id']) ? '&id='.Security::remove_XSS($_GET['id']) : '';
  855. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).$id);
  856. // Setting the form elements: the header.
  857. if ($_GET['action'] == 'add') {
  858. $title = get_lang('AddTemplate');
  859. } else {
  860. $title = get_lang('EditTemplate');
  861. }
  862. $form->addElement('header', '', $title);
  863. // Setting the form elements: the title of the template.
  864. $form->addText('title', get_lang('Title'), false);
  865. // Setting the form elements: the content of the template (wysiwyg editor).
  866. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  867. // Setting the form elements: the form to upload an image to be used with the template.
  868. $form->addElement('file','template_image',get_lang('Image'),'');
  869. // Setting the form elements: a little bit information about the template image.
  870. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  871. // Getting all the information of the template when editing a template.
  872. if ($_GET['action'] == 'edit') {
  873. // Database table definition.
  874. $table_system_template = Database :: get_main_table('system_template');
  875. $sql = "SELECT * FROM $table_system_template WHERE id = ".intval($_GET['id'])."";
  876. $result = Database::query($sql);
  877. $row = Database::fetch_array($result);
  878. $defaults['template_id'] = intval($_GET['id']);
  879. $defaults['template_text'] = $row['content'];
  880. // Forcing get_lang().
  881. $defaults['title'] = get_lang($row['title']);
  882. // Adding an extra field: a hidden field with the id of the template we are editing.
  883. $form->addElement('hidden', 'template_id');
  884. // Adding an extra field: a preview of the image that is currently used.
  885. if (!empty($row['image'])) {
  886. $form->addElement('static', 'template_image_preview', '',
  887. '<img src="' . api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/' . $row['image'] . '" alt="' . get_lang('TemplatePreview') . '"/>');
  888. } else {
  889. $form->addElement('static', 'template_image_preview', '',
  890. '<img src="' . api_get_path(WEB_APP_PATH) . 'home/default_platform_document/template_thumb/noimage.gif" alt="' . get_lang('NoTemplatePreview') . '"/>');
  891. }
  892. // Setting the information of the template that we are editing.
  893. $form->setDefaults($defaults);
  894. }
  895. // Setting the form elements: the submit button.
  896. $form->addButtonSave(get_lang('Ok'), 'submit');
  897. // Setting the rules: the required fields.
  898. $form->addRule('template_image', get_lang('ThisFieldIsRequired'), 'required');
  899. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  900. $form->addRule('template_text', get_lang('ThisFieldIsRequired'), 'required');
  901. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  902. if ($form->validate()) {
  903. $check = Security::check_token('post');
  904. if ($check) {
  905. // Exporting the values.
  906. $values = $form->exportValues();
  907. // Upload the file.
  908. if (!empty($_FILES['template_image']['name'])) {
  909. $upload_ok = process_uploaded_file($_FILES['template_image']);
  910. if ($upload_ok) {
  911. // Try to add an extension to the file if it hasn't one.
  912. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  913. // The upload directory.
  914. $upload_dir = api_get_path(SYS_APP_PATH).'home/default_platform_document/template_thumb/';
  915. // Create the directory if it does not exist.
  916. if (!is_dir($upload_dir)) {
  917. mkdir($upload_dir, api_get_permissions_for_new_directories());
  918. }
  919. // Resize the preview image to max default and upload.
  920. $temp = new Image($_FILES['template_image']['tmp_name']);
  921. $picture_info = $temp->get_image_info();
  922. $max_width_for_picture = 100;
  923. if ($picture_info['width'] > $max_width_for_picture) {
  924. $temp->resize($max_width_for_picture);
  925. }
  926. $temp->send_image($upload_dir.$new_file_name);
  927. }
  928. }
  929. // Store the information in the database (as insert or as update).
  930. $table_system_template = Database :: get_main_table('system_template');
  931. if ($_GET['action'] == 'add') {
  932. $content_template = Security::remove_XSS($values['template_text'], COURSEMANAGERLOWSECURITY);
  933. $params = [
  934. 'title' => $values['title'],
  935. 'content' => $content_template,
  936. 'image' => $new_file_name
  937. ];
  938. Database::insert($table_system_template, $params);
  939. // Display a feedback message.
  940. Display::display_confirmation_message(get_lang('TemplateAdded'));
  941. echo '<a href="settings.php?category=Templates&action=add">'.Display::return_icon('new_template.png', get_lang('AddTemplate'),'',ICON_SIZE_MEDIUM).'</a>';
  942. } else {
  943. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  944. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."', content = '".$content_template."'";
  945. if (!empty($new_file_name)) {
  946. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  947. }
  948. $sql .= " WHERE id = ".intval($_GET['id'])."";
  949. Database::query($sql);
  950. // Display a feedback message.
  951. Display::display_confirmation_message(get_lang('TemplateEdited'));
  952. }
  953. }
  954. Security::clear_token();
  955. displayTemplates();
  956. } else {
  957. $token = Security::get_token();
  958. $form->addElement('hidden','sec_token');
  959. $form->setConstants(array('sec_token' => $token));
  960. // Display the form.
  961. $form->display();
  962. }
  963. }
  964. /**
  965. * Delete a template
  966. *
  967. * @param integer $id the id of the template that has to be deleted
  968. *
  969. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  970. * @version August 2008
  971. * @since v1.8.6
  972. */
  973. function deleteTemplate($id)
  974. {
  975. // First we remove the image.
  976. $table_system_template = Database :: get_main_table('system_template');
  977. $sql = "SELECT * FROM $table_system_template WHERE id = ".intval($id)."";
  978. $result = Database::query($sql);
  979. $row = Database::fetch_array($result);
  980. if (!empty($row['image'])) {
  981. @unlink(api_get_path(SYS_APP_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  982. }
  983. // Now we remove it from the database.
  984. $sql = "DELETE FROM $table_system_template WHERE id = ".intval($id)."";
  985. Database::query($sql);
  986. // Display a feedback message.
  987. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  988. }
  989. function generateSettingsForm($settings, $settings_by_access_list)
  990. {
  991. global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list;
  992. $em = Database::getManager();
  993. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  994. $form = new FormValidator('settings', 'post', 'settings.php?category='.Security::remove_XSS($_GET['category']));
  995. $form->addElement('hidden', 'search_field',
  996. (!empty($_GET['search_field']) ? Security::remove_XSS($_GET['search_field']) : null));
  997. $url_id = api_get_current_access_url_id();
  998. if (!empty($_configuration['multiple_access_urls']) && api_is_global_platform_admin() && $url_id == 1) {
  999. $group = array();
  1000. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  1001. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  1002. $form->addGroup($group, 'buttons_in_action_right');
  1003. }
  1004. $default_values = array();
  1005. $url_info = api_get_access_url($url_id);
  1006. $i = 0;
  1007. $addedSettings = [];
  1008. foreach ($settings as $row) {
  1009. if (in_array($row['variable'], array_keys($settings_to_avoid))) {
  1010. continue;
  1011. }
  1012. if (in_array($row['variable'], $addedSettings)) {
  1013. continue;
  1014. }
  1015. $addedSettings[] = $row['variable'];
  1016. if (!empty($_configuration['multiple_access_urls'])) {
  1017. if (api_is_global_platform_admin()) {
  1018. if ($row['access_url_locked'] == 0) {
  1019. if ($url_id == 1) {
  1020. if ($row['access_url_changeable'] == '1') {
  1021. $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "0" data_to_send = "'.$row['variable'].'" href="javascript:void(0);">'.
  1022. Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')).'</a></div>');
  1023. } else {
  1024. $form->addElement('html', '<div style="float: right;"><a class="share_this_setting" data_status = "1" data_to_send = "'.$row['variable'].'" href="javascript:void(0);">'.
  1025. Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')).'</a></div>');
  1026. }
  1027. } else {
  1028. if ($row['access_url_changeable'] == '1') {
  1029. $form->addElement('html', '<div style="float: right;">'.
  1030. Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting')).'</div>');
  1031. } else {
  1032. $form->addElement('html', '<div style="float: right;">'.
  1033. Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting')).'</div>');
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. $hideme = array();
  1040. $hide_element = false;
  1041. if ($_configuration['access_url'] != 1) {
  1042. if ($row['access_url_changeable'] == 0) {
  1043. // We hide the element in other cases (checkbox, radiobutton) we 'freeze' the element.
  1044. $hide_element = true;
  1045. $hideme = array('disabled');
  1046. } elseif ($url_info['active'] == 1) {
  1047. // We show the elements.
  1048. if (empty($row['variable'])) {
  1049. $row['variable'] = 0;
  1050. }
  1051. if (empty($row['subkey'])) {
  1052. $row['subkey'] = 0;
  1053. }
  1054. if (empty($row['category'])) {
  1055. $row['category'] = 0;
  1056. }
  1057. if (is_array($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ])) {
  1058. // We are sure that the other site have a selected value.
  1059. if ($settings_by_access_list[$row['variable']] [$row['subkey']] [$row['category']]['selected_value'] != '') {
  1060. $row['selected_value'] = $settings_by_access_list[$row['variable']] [$row['subkey']] [$row['category']]['selected_value'];
  1061. }
  1062. }
  1063. // There is no else{} statement because we load the default $row['selected_value'] of the main Chamilo site.
  1064. }
  1065. }
  1066. switch ($row['type']) {
  1067. case 'textfield':
  1068. if (in_array($row['variable'], $convert_byte_to_mega_list)) {
  1069. $form->addElement(
  1070. 'text',
  1071. $row['variable'],
  1072. array(
  1073. get_lang($row['title']),
  1074. get_lang($row['comment']),
  1075. get_lang('MB'),
  1076. ),
  1077. array('maxlength' => '8')
  1078. );
  1079. $form->applyFilter($row['variable'], 'html_filter');
  1080. $default_values[$row['variable']] = round($row['selected_value']/1024/1024, 1);
  1081. } elseif ($row['variable'] == 'account_valid_duration') {
  1082. $form->addElement(
  1083. 'text',
  1084. $row['variable'],
  1085. array(
  1086. get_lang($row['title']),
  1087. get_lang($row['comment']),
  1088. ),
  1089. array('maxlength' => '5')
  1090. );
  1091. $form->applyFilter($row['variable'], 'html_filter');
  1092. $default_values[$row['variable']] = $row['selected_value'];
  1093. // For platform character set selection: Conversion of the textfield to a select box with valid values.
  1094. } elseif ($row['variable'] == 'platform_charset') {
  1095. continue;
  1096. } else {
  1097. $hideme['class'] = 'col-md-4';
  1098. $form->addElement(
  1099. 'text',
  1100. $row['variable'],
  1101. array(
  1102. get_lang($row['title']),
  1103. get_lang($row['comment']),
  1104. ),
  1105. $hideme
  1106. );
  1107. $form->applyFilter($row['variable'],'html_filter');
  1108. $default_values[$row['variable']] = $row['selected_value'];
  1109. }
  1110. break;
  1111. case 'textarea':
  1112. if ($row['variable'] == 'header_extra_content') {
  1113. $file = api_get_path(SYS_PATH).api_get_home_path().'header_extra_content.txt';
  1114. $value = '';
  1115. if (file_exists($file)) {
  1116. $value = file_get_contents($file);
  1117. }
  1118. $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10'), $hideme);
  1119. $default_values[$row['variable']] = $value;
  1120. } elseif ($row['variable'] == 'footer_extra_content') {
  1121. $file = api_get_path(SYS_PATH).api_get_home_path().'footer_extra_content.txt';
  1122. $value = '';
  1123. if (file_exists($file)) {
  1124. $value = file_get_contents($file);
  1125. }
  1126. $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10'), $hideme);
  1127. $default_values[$row['variable']] = $value;
  1128. } else {
  1129. $form->addElement('textarea', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])) , array('rows'=>'10'), $hideme);
  1130. $default_values[$row['variable']] = $row['selected_value'];
  1131. }
  1132. break;
  1133. case 'radio':
  1134. $values = api_get_settings_options($row['variable']);
  1135. $group = array ();
  1136. if (is_array($values)) {
  1137. foreach ($values as $key => $value) {
  1138. $element = &$form->createElement(
  1139. 'radio',
  1140. $row['variable'],
  1141. '',
  1142. $value['display_text'],
  1143. $value['value']
  1144. );
  1145. if ($hide_element) {
  1146. $element->freeze();
  1147. }
  1148. $group[] = $element;
  1149. }
  1150. }
  1151. $form->addGroup(
  1152. $group,
  1153. $row['variable'],
  1154. array(get_lang($row['title']), get_lang($row['comment'])),
  1155. '',
  1156. false
  1157. );
  1158. $default_values[$row['variable']] = $row['selected_value'];
  1159. break;
  1160. case 'checkbox';
  1161. // 1. We collect all the options of this variable.
  1162. $sql = "SELECT * FROM $table_settings_current
  1163. WHERE variable='".$row['variable']."' AND access_url = 1";
  1164. $result = Database::query($sql);
  1165. $group = array ();
  1166. while ($rowkeys = Database::fetch_array($result)) {
  1167. // Profile tab option should be hidden when the social tool is enabled.
  1168. if (api_get_setting('allow_social_tool') == 'true') {
  1169. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_profile') {
  1170. continue;
  1171. }
  1172. }
  1173. // Hiding the gradebook option.
  1174. if ($rowkeys['variable'] == 'show_tabs' && $rowkeys['subkey'] == 'my_gradebook') {
  1175. continue;
  1176. }
  1177. $element = &$form->createElement(
  1178. 'checkbox',
  1179. $rowkeys['subkey'],
  1180. '',
  1181. get_lang($rowkeys['subkeytext'])
  1182. );
  1183. if ($row['access_url_changeable'] == 1) {
  1184. // 2. We look into the DB if there is a setting for a specific access_url.
  1185. $access_url = $_configuration['access_url'];
  1186. if (empty($access_url)) {
  1187. $access_url = 1;
  1188. }
  1189. $sql = "SELECT selected_value FROM $table_settings_current
  1190. WHERE
  1191. variable='".$rowkeys['variable']."' AND
  1192. subkey='".$rowkeys['subkey']."' AND
  1193. subkeytext='".$rowkeys['subkeytext']."' AND
  1194. access_url = $access_url";
  1195. $result_access = Database::query($sql);
  1196. $row_access = Database::fetch_array($result_access);
  1197. if ($row_access['selected_value'] == 'true' && !$form->isSubmitted()) {
  1198. $element->setChecked(true);
  1199. }
  1200. } else {
  1201. if ($rowkeys['selected_value'] == 'true' && !$form->isSubmitted()) {
  1202. $element->setChecked(true);
  1203. }
  1204. }
  1205. if ($hide_element) {
  1206. $element->freeze();
  1207. }
  1208. $group[] = $element;
  1209. }
  1210. $form->addGroup(
  1211. $group,
  1212. $row['variable'],
  1213. array(get_lang($row['title']), get_lang($row['comment'])),
  1214. ''
  1215. );
  1216. break;
  1217. case 'link':
  1218. $form->addElement('static', null, array(get_lang($row['title']), get_lang($row['comment'])),
  1219. get_lang('CurrentValue') . ' : ' . $row['selected_value'], $hideme);
  1220. break;
  1221. case 'select':
  1222. /*
  1223. * 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.
  1224. * The functions being called must be added to the file settings.lib.php.
  1225. */
  1226. $form->addElement('select', $row['variable'], array(get_lang($row['title']), get_lang($row['comment'])), call_user_func('select_'.$row['variable']), $hideme);
  1227. $default_values[$row['variable']] = $row['selected_value'];
  1228. break;
  1229. case 'custom':
  1230. break;
  1231. case 'select_course':
  1232. $courseSelectOptions = [];
  1233. if (!empty($row['selected_value'])) {
  1234. $course = $em->find('ChamiloCoreBundle:Course', $row['selected_value']);
  1235. $courseSelectOptions[$course->getId()] = $course->getTitle();
  1236. }
  1237. $form->addElement(
  1238. 'select_ajax',
  1239. $row['variable'],
  1240. [get_lang($row['title']), get_lang($row['comment'])],
  1241. $courseSelectOptions,
  1242. ['url' => api_get_path(WEB_AJAX_PATH) . 'course.ajax.php?a=search_course']
  1243. );
  1244. $default_values[$row['variable']] = $row['selected_value'];
  1245. break;
  1246. }
  1247. switch ($row['variable']) {
  1248. case 'pdf_export_watermark_enable':
  1249. $url = PDF::get_watermark(null);
  1250. if ($url != false) {
  1251. $delete_url = '<a href="?delete_watermark">'.get_lang('DelImage').' '.Display::return_icon('delete.png',get_lang('DelImage')).'</a>';
  1252. $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>');
  1253. }
  1254. $form->addElement('file', 'pdf_export_watermark_path', get_lang('AddWaterMark'));
  1255. $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
  1256. $form->addRule('pdf_export_watermark_path', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
  1257. break;
  1258. case 'timezone_value':
  1259. $timezone = $row['selected_value'];
  1260. if (empty($timezone)) {
  1261. $timezone = _api_get_timezone();
  1262. }
  1263. $form->addElement('html', sprintf(get_lang('LocalTimeUsingPortalTimezoneXIsY'), $timezone, api_get_local_time()));
  1264. break;
  1265. }
  1266. } // end for
  1267. if (!empty($settings)) {
  1268. $form->setDefaults($default_values);
  1269. }
  1270. $form->addHtml('<div class="bottom_actions">');
  1271. $form->addButtonSave(get_lang('SaveSettings'));
  1272. $form->addHtml('</div>');
  1273. return $form;
  1274. }
  1275. /**
  1276. * Searches a platform setting in all categories except from the Plugins category
  1277. * @param string $search
  1278. * @return array
  1279. */
  1280. function searchSetting($search)
  1281. {
  1282. if (empty($search)) {
  1283. return array();
  1284. }
  1285. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1286. $sql = "SELECT * FROM $table_settings_current
  1287. WHERE category <> 'Plugins' ORDER BY id ASC ";
  1288. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1289. $settings = array();
  1290. $search = api_strtolower($search);
  1291. if (!empty($result)) {
  1292. foreach ($result as $setting) {
  1293. $found = false;
  1294. $title = api_strtolower(get_lang($setting['title']));
  1295. // try the title
  1296. if (strpos($title, $search) === false) {
  1297. $comment = api_strtolower(get_lang($setting['comment']));
  1298. //Try the comment
  1299. if (strpos($comment, $search) === false) {
  1300. //Try the variable name
  1301. if (strpos($setting['variable'], $search) === false) {
  1302. continue;
  1303. } else {
  1304. $found = true;
  1305. }
  1306. } else {
  1307. $found = true;
  1308. }
  1309. } else {
  1310. $found = true;
  1311. }
  1312. if ($found) {
  1313. $settings[] = $setting;
  1314. }
  1315. }
  1316. }
  1317. return $settings;
  1318. }
  1319. /**
  1320. * Helper function to generates a form elements group
  1321. * @param object $form The form where the elements group has to be added
  1322. * @param array $values Values to browse through
  1323. * @return array
  1324. */
  1325. function formGenerateElementsGroup($form, $values = array(), $elementName)
  1326. {
  1327. $group = array();
  1328. if (is_array($values)) {
  1329. foreach ($values as $key => $value) {
  1330. $element = &$form->createElement('radio', $elementName, '', $value['display_text'], $value['value']);
  1331. $group[] = $element;
  1332. }
  1333. }
  1334. return $group;
  1335. }
  1336. /**
  1337. * Helper function with allowed file types for CSS
  1338. * @return array Array of file types (no indexes)
  1339. */
  1340. function getAllowedFileTypes()
  1341. {
  1342. $allowedFiles = array(
  1343. 'css',
  1344. 'zip',
  1345. 'jpeg',
  1346. 'jpg',
  1347. 'png',
  1348. 'gif',
  1349. 'ico',
  1350. 'psd',
  1351. 'xcf',
  1352. 'svg',
  1353. 'webp',
  1354. 'woff',
  1355. 'woff2'
  1356. );
  1357. return $allowedFiles;
  1358. }
  1359. /**
  1360. * Helper function to set settings in the database
  1361. * @param array $parameters List of values
  1362. * @param int $accessUrl The current access URL
  1363. * @return void
  1364. */
  1365. function setConfigurationSettingsInDatabase($parameters, $accessUrl)
  1366. {
  1367. $r = api_set_settings_category('Search', 'false', $accessUrl);
  1368. // Save the settings.
  1369. foreach ($parameters as $key => $value) {
  1370. $result = api_set_setting($key, $value, null, null);
  1371. }
  1372. }
  1373. /**
  1374. * Helper function to show the status of the search settings table
  1375. * @param array $data Data to show
  1376. * @return void
  1377. */
  1378. function showSearchSettingsTable($data)
  1379. {
  1380. echo Display::tag('h3', get_lang('Settings'));
  1381. $table = new SortableTableFromArray($data);
  1382. $table->set_header(0, get_lang('Setting'), false);
  1383. $table->set_header(1, get_lang('Status'), false);
  1384. echo $table->display();
  1385. }
  1386. /**
  1387. * Helper function to show status table for each command line tool installed
  1388. * @return void
  1389. */
  1390. function showSearchToolsStatusTable()
  1391. {
  1392. //@todo windows support
  1393. if (api_is_windows_os() == false) {
  1394. $list_of_programs = array('pdftotext', 'ps2pdf', 'catdoc', 'html2text', 'unrtf', 'catppt', 'xls2csv');
  1395. foreach($list_of_programs as $program) {
  1396. $output = [];
  1397. $ret_val = null;
  1398. exec("which $program", $output, $ret_val);
  1399. if (!$output) {
  1400. $output[] = '';
  1401. }
  1402. $icon = Display::return_icon('bullet_red.png', get_lang('NotInstalled'));
  1403. if (!empty($output[0])) {
  1404. $icon = Display::return_icon('bullet_green.png', get_lang('Installed'));
  1405. }
  1406. $data2[]= array($program, $output[0], $icon);
  1407. }
  1408. echo Display::tag('h3', get_lang('ProgramsNeededToConvertFiles'));
  1409. $table = new SortableTableFromArray($data2);
  1410. $table->set_header(0, get_lang('Program'), false);
  1411. $table->set_header(1, get_lang('Path'), false);
  1412. $table->set_header(2, get_lang('Status'), false);
  1413. echo $table->display();
  1414. } else {
  1415. Display::display_warning_message(
  1416. get_lang('YouAreUsingChamiloInAWindowsPlatformSadlyYouCantConvertDocumentsInOrderToSearchTheContentUsingThisTool')
  1417. );
  1418. }
  1419. }
  1420. /**
  1421. * Helper function to generate and show CSS Zip download message
  1422. * @param string $style Style path
  1423. * @return void
  1424. */
  1425. function generateCSSDownloadLink($style)
  1426. {
  1427. $arch = api_get_path(SYS_ARCHIVE_PATH).$style.'.zip';
  1428. $dir = api_get_path(SYS_CSS_PATH).'themes/'.$style;
  1429. if (is_dir($dir)) {
  1430. $zip = new PclZip($arch);
  1431. // Remove path prefix except the style name and put file on disk
  1432. $zip->create($dir, PCLZIP_OPT_REMOVE_PATH, substr($dir,0,-strlen($style)));
  1433. //@TODO: use more generic script to download.
  1434. $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>';
  1435. Display::display_normal_message($str, false);
  1436. } else {
  1437. Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
  1438. }
  1439. }
  1440. /**
  1441. * Helper function to tell if the style is changeable in the current URL
  1442. * @return bool $changeable Whether the style can be changed in this URL or not
  1443. */
  1444. function isStyleChangeable() {
  1445. global $_configuration;
  1446. $changeable = false;
  1447. if ($_configuration['access_url'] != 1) {
  1448. $style_info = api_get_settings('stylesheets', '', 1, 0);
  1449. $url_info = api_get_access_url($_configuration['access_url']);
  1450. if ($style_info[0]['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  1451. $changeable = true;
  1452. }
  1453. } else {
  1454. $changeable = true;
  1455. }
  1456. return $changeable;
  1457. }