settings.lib.php 69 KB

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