settings.lib.php 69 KB

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