settings.lib.php 65 KB

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