settings.lib.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  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="table table-hover table-striped table-bordered">';
  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. if (empty($style_name) || is_array($style_name)) {
  473. // The name of the uploaded stylesheet doesn't have the expected format
  474. return $result;
  475. }
  476. $cssToUpload = CSS_UPLOAD_PATH;
  477. // Check if a virtual instance vchamilo is used
  478. $virtualInstanceTheme = api_get_configuration_value('virtual_css_theme_folder');
  479. if (!empty($virtualInstanceTheme)) {
  480. $cssToUpload = $cssToUpload.$virtualInstanceTheme.'/';
  481. }
  482. // Create the folder if needed.
  483. if (!is_dir($cssToUpload.$style_name.'/')) {
  484. mkdir($cssToUpload.$style_name.'/', api_get_permissions_for_new_directories());
  485. }
  486. $info = pathinfo($picture['name']);
  487. if ($info['extension'] == 'zip') {
  488. // Try to open the file and extract it in the theme.
  489. $zip = new ZipArchive();
  490. if ($zip->open($picture['tmp_name'])) {
  491. // Make sure all files inside the zip are images or css.
  492. $num_files = $zip->numFiles;
  493. $valid = true;
  494. $single_directory = true;
  495. $invalid_files = [];
  496. $allowedFiles = getAllowedFileTypes();
  497. for ($i = 0; $i < $num_files; $i++) {
  498. $file = $zip->statIndex($i);
  499. if (substr($file['name'], -1) != '/') {
  500. $path_parts = pathinfo($file['name']);
  501. if (!in_array($path_parts['extension'], $allowedFiles)) {
  502. $valid = false;
  503. $invalid_files[] = $file['name'];
  504. }
  505. }
  506. if (strpos($file['name'], '/') === false) {
  507. $single_directory = false;
  508. }
  509. }
  510. if (!$valid) {
  511. $error_string = '<ul>';
  512. foreach ($invalid_files as $invalid_file) {
  513. $error_string .= '<li>'.$invalid_file.'</li>';
  514. }
  515. $error_string .= '</ul>';
  516. echo Display::return_message(
  517. get_lang('ErrorStylesheetFilesExtensionsInsideZip').$error_string,
  518. 'error',
  519. false
  520. );
  521. } else {
  522. // If the zip does not contain a single directory, extract it.
  523. if (!$single_directory) {
  524. // Extract zip file.
  525. $zip->extractTo($cssToUpload.$style_name.'/');
  526. $result = true;
  527. } else {
  528. $extraction_path = $cssToUpload.$style_name.'/';
  529. $mode = api_get_permissions_for_new_directories();
  530. for ($i = 0; $i < $num_files; $i++) {
  531. $entry = $zip->getNameIndex($i);
  532. if (substr($entry, -1) == '/') {
  533. continue;
  534. }
  535. $pos_slash = strpos($entry, '/');
  536. $entry_without_first_dir = substr($entry, $pos_slash + 1);
  537. // If there is still a slash, we need to make sure the directories are created.
  538. if (strpos($entry_without_first_dir, '/') !== false) {
  539. if (!is_dir($extraction_path.dirname($entry_without_first_dir))) {
  540. // Create it.
  541. @mkdir($extraction_path.dirname($entry_without_first_dir), $mode, true);
  542. }
  543. }
  544. $fp = $zip->getStream($entry);
  545. $ofp = fopen($extraction_path.dirname($entry_without_first_dir).'/'.basename($entry), 'w');
  546. while (!feof($fp)) {
  547. fwrite($ofp, fread($fp, 8192));
  548. }
  549. fclose($fp);
  550. fclose($ofp);
  551. }
  552. $result = true;
  553. }
  554. }
  555. $zip->close();
  556. } else {
  557. echo Display::return_message(get_lang('ErrorReadingZip').$info['extension'], 'error', false);
  558. }
  559. } else {
  560. // Simply move the file.
  561. move_uploaded_file($picture['tmp_name'], $cssToUpload.$style_name.'/'.$picture['name']);
  562. $result = true;
  563. }
  564. if ($result) {
  565. $fs = new Filesystem();
  566. $fs->mirror(
  567. CSS_UPLOAD_PATH,
  568. api_get_path(SYS_PATH).'web/css/themes/',
  569. null,
  570. ['override' => true]
  571. );
  572. }
  573. return $result;
  574. }
  575. /**
  576. * Store plugin regions.
  577. */
  578. function storeRegions()
  579. {
  580. $plugin_obj = new AppPlugin();
  581. // Get a list of all current 'Plugins' settings
  582. $installed_plugins = $plugin_obj->get_installed_plugins();
  583. $shortlist_installed = [];
  584. if (!empty($installed_plugins)) {
  585. foreach ($installed_plugins as $plugin) {
  586. if (isset($plugin['subkey'])) {
  587. $shortlist_installed[] = $plugin['subkey'];
  588. }
  589. }
  590. }
  591. $plugin_list = $plugin_obj->read_plugins_from_path();
  592. foreach ($plugin_list as $plugin) {
  593. if (isset($_POST['plugin_'.$plugin])) {
  594. $areas_to_installed = $_POST['plugin_'.$plugin];
  595. if (!empty($areas_to_installed)) {
  596. $plugin_obj->remove_all_regions($plugin);
  597. foreach ($areas_to_installed as $region) {
  598. if (!empty($region) && $region != '-1') {
  599. $plugin_obj->add_to_region($plugin, $region);
  600. }
  601. }
  602. }
  603. }
  604. }
  605. }
  606. /**
  607. * This function allows easy activating and inactivating of plugins.
  608. *
  609. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  610. */
  611. function storePlugins()
  612. {
  613. $appPlugin = new AppPlugin();
  614. // Get a list of all current 'Plugins' settings
  615. $plugin_list = $appPlugin->read_plugins_from_path();
  616. $installed_plugins = [];
  617. foreach ($plugin_list as $plugin) {
  618. if (isset($_POST['plugin_'.$plugin])) {
  619. $appPlugin->install($plugin);
  620. $installed_plugins[] = $plugin;
  621. }
  622. }
  623. if (!empty($installed_plugins)) {
  624. $remove_plugins = array_diff($plugin_list, $installed_plugins);
  625. } else {
  626. $remove_plugins = $plugin_list;
  627. }
  628. foreach ($remove_plugins as $plugin) {
  629. $appPlugin->uninstall($plugin);
  630. }
  631. }
  632. /**
  633. * This function allows the platform admin to choose which should be the default stylesheet.
  634. *
  635. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  636. */
  637. function storeStylesheets()
  638. {
  639. // Insert the stylesheet.
  640. if (isStyle($_POST['style'])) {
  641. api_set_setting(
  642. 'stylesheets',
  643. $_POST['style'],
  644. null,
  645. 'stylesheets',
  646. api_get_current_access_url_id()
  647. );
  648. }
  649. return true;
  650. }
  651. /**
  652. * This function checks if the given style is a recognize style that exists in the css directory as
  653. * a standalone directory.
  654. *
  655. * @param string $style
  656. *
  657. * @return bool True if this style is recognized, false otherwise
  658. */
  659. function isStyle($style)
  660. {
  661. $themeList = api_get_themes();
  662. return in_array($style, array_keys($themeList));
  663. }
  664. /**
  665. * Search options
  666. * TODO: support for multiple site. aka $_configuration['access_url'] == 1.
  667. *
  668. * @author Marco Villegas <marvil07@gmail.com>
  669. */
  670. function handleSearch()
  671. {
  672. global $SettingsStored, $_configuration;
  673. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  674. $search_enabled = api_get_setting('search_enabled');
  675. $form = new FormValidator(
  676. 'search-options',
  677. 'post',
  678. api_get_self().'?category=Search'
  679. );
  680. $values = api_get_settings_options('search_enabled');
  681. $form->addElement('header', null, get_lang('SearchEnabledTitle'));
  682. $group = formGenerateElementsGroup($form, $values, 'search_enabled');
  683. // SearchEnabledComment
  684. $form->addGroup(
  685. $group,
  686. 'search_enabled',
  687. [get_lang('SearchEnabledTitle'), get_lang('SearchEnabledComment')],
  688. null,
  689. false
  690. );
  691. $search_enabled = api_get_setting('search_enabled');
  692. if ($form->validate()) {
  693. $formValues = $form->exportValues();
  694. setConfigurationSettingsInDatabase($formValues, $_configuration['access_url']);
  695. $search_enabled = $formValues['search_enabled'];
  696. echo Display::return_message($SettingsStored, 'confirm');
  697. }
  698. $specific_fields = get_specific_field_list();
  699. if ($search_enabled == 'true') {
  700. $values = api_get_settings_options('search_show_unlinked_results');
  701. $group = formGenerateElementsGroup(
  702. $form,
  703. $values,
  704. 'search_show_unlinked_results'
  705. );
  706. $form->addGroup(
  707. $group,
  708. 'search_show_unlinked_results',
  709. [
  710. get_lang('SearchShowUnlinkedResultsTitle'),
  711. get_lang('SearchShowUnlinkedResultsComment'),
  712. ],
  713. null,
  714. false
  715. );
  716. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  717. $sf_values = [];
  718. foreach ($specific_fields as $sf) {
  719. $sf_values[$sf['code']] = $sf['name'];
  720. }
  721. $url = Display::div(
  722. Display::url(
  723. get_lang('AddSpecificSearchField'),
  724. 'specific_fields.php'
  725. ),
  726. ['class' => 'sectioncomment']
  727. );
  728. if (empty($sf_values)) {
  729. $form->addElement('label', [get_lang('SearchPrefilterPrefix'), $url]);
  730. } else {
  731. $form->addElement(
  732. 'select',
  733. 'search_prefilter_prefix',
  734. [get_lang('SearchPrefilterPrefix'), $url],
  735. $sf_values,
  736. ''
  737. );
  738. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  739. }
  740. }
  741. $default_values['search_enabled'] = $search_enabled;
  742. $form->addButtonSave(get_lang('Save'));
  743. $form->setDefaults($default_values);
  744. echo '<div id="search-options-form">';
  745. $form->display();
  746. echo '</div>';
  747. if ($search_enabled == 'true') {
  748. $xapianPath = api_get_path(SYS_UPLOAD_PATH).'plugins/xapian/searchdb';
  749. /*
  750. @todo Test the Xapian connection
  751. if (extension_loaded('xapian')) {
  752. require_once 'xapian.php';
  753. try {
  754. $db = new XapianDatabase($xapianPath.'/');
  755. } catch (Exception $e) {
  756. var_dump($e->getMessage());
  757. }
  758. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  759. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  760. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  761. $indexable = new IndexableChunk();
  762. $indexable->addValue("content", 'Test');
  763. $di = new ChamiloIndexer();
  764. $di->connectDb(NULL, NULL, 'english');
  765. $di->addChunk($indexable);
  766. $did = $di->index();
  767. }
  768. */
  769. $xapianLoaded = Display::return_icon('bullet_green.png', get_lang('Ok'));
  770. $dir_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
  771. $dir_is_writable = Display::return_icon('bullet_green.png', get_lang('Ok'));
  772. $specific_fields_exists = Display::return_icon('bullet_green.png', get_lang('Ok'));
  773. //Testing specific fields
  774. if (empty($specific_fields)) {
  775. $specific_fields_exists = Display::return_icon(
  776. 'bullet_red.png',
  777. get_lang('AddSpecificSearchField')
  778. );
  779. }
  780. //Testing xapian extension
  781. if (!extension_loaded('xapian')) {
  782. $xapianLoaded = Display::return_icon('bullet_red.png', get_lang('Error'));
  783. }
  784. //Testing xapian searchdb path
  785. if (!is_dir($xapianPath)) {
  786. $dir_exists = Display::return_icon('bullet_red.png', get_lang('Error'));
  787. }
  788. //Testing xapian searchdb path is writable
  789. if (!is_writable($xapianPath)) {
  790. $dir_is_writable = Display::return_icon('bullet_red.png', get_lang('Error'));
  791. }
  792. $data = [];
  793. $data[] = [get_lang('XapianModuleInstalled'), $xapianLoaded];
  794. $data[] = [get_lang('DirectoryExists').' - '.$xapianPath, $dir_exists];
  795. $data[] = [get_lang('IsWritable').' - '.$xapianPath, $dir_is_writable];
  796. $data[] = [get_lang('SpecificSearchFieldsAvailable'), $specific_fields_exists];
  797. showSearchSettingsTable($data);
  798. showSearchToolsStatusTable();
  799. }
  800. }
  801. /**
  802. * Wrapper for the templates.
  803. *
  804. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  805. * @author Julio Montoya.
  806. *
  807. * @version August 2008
  808. *
  809. * @since v1.8.6
  810. */
  811. function handleTemplates()
  812. {
  813. /* Drive-by fix to avoid undefined var warnings, without repeating
  814. * isset() combos all over the place. */
  815. $action = isset($_GET['action']) ? $_GET['action'] : "invalid";
  816. if ($action != 'add') {
  817. echo '<div class="actions" style="margin-left: 1px;">';
  818. echo '<a href="settings.php?category=Templates&action=add">'.
  819. Display::return_icon('new_template.png', get_lang('AddTemplate'), '', ICON_SIZE_MEDIUM).'</a>';
  820. echo '</div>';
  821. }
  822. if ($action == 'add' || ($action == 'edit' && is_numeric($_GET['id']))) {
  823. addEditTemplate();
  824. // Add event to the system log.
  825. $user_id = api_get_user_id();
  826. $category = $_GET['category'];
  827. Event::addEvent(
  828. LOG_CONFIGURATION_SETTINGS_CHANGE,
  829. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  830. $category,
  831. api_get_utc_datetime(),
  832. $user_id
  833. );
  834. } else {
  835. if ($action == 'delete' && is_numeric($_GET['id'])) {
  836. deleteTemplate($_GET['id']);
  837. // Add event to the system log
  838. $user_id = api_get_user_id();
  839. $category = $_GET['category'];
  840. Event::addEvent(
  841. LOG_CONFIGURATION_SETTINGS_CHANGE,
  842. LOG_CONFIGURATION_SETTINGS_CATEGORY,
  843. $category,
  844. api_get_utc_datetime(),
  845. $user_id
  846. );
  847. }
  848. displayTemplates();
  849. }
  850. }
  851. /**
  852. * Display a sortable table with all the templates that the platform administrator has defined.
  853. *
  854. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  855. *
  856. * @version August 2008
  857. *
  858. * @since v1.8.6
  859. */
  860. function displayTemplates()
  861. {
  862. $table = new SortableTable(
  863. 'templates',
  864. 'getNumberOfTemplates',
  865. 'getTemplateData',
  866. 1
  867. );
  868. $table->set_additional_parameters(
  869. ['category' => Security::remove_XSS($_GET['category'])]
  870. );
  871. $table->set_header(0, get_lang('Image'), true, ['style' => 'width: 101px;']);
  872. $table->set_header(1, get_lang('Title'));
  873. $table->set_header(2, get_lang('Actions'), false, ['style' => 'width:50px;']);
  874. $table->set_column_filter(2, 'actionsFilter');
  875. $table->set_column_filter(0, 'searchImageFilter');
  876. $table->display();
  877. }
  878. /**
  879. * Gets the number of templates that are defined by the platform admin.
  880. *
  881. * @return int
  882. *
  883. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  884. *
  885. * @version August 2008
  886. *
  887. * @since v1.8.6
  888. */
  889. function getNumberOfTemplates()
  890. {
  891. // Database table definition.
  892. $table = Database::get_main_table('system_template');
  893. // The sql statement.
  894. $sql = "SELECT COUNT(id) AS total FROM $table";
  895. $result = Database::query($sql);
  896. $row = Database::fetch_array($result);
  897. // Returning the number of templates.
  898. return $row['total'];
  899. }
  900. /**
  901. * Gets all the template data for the sortable table.
  902. *
  903. * @param int $from the start of the limit statement
  904. * @param int $number_of_items the number of elements that have to be retrieved from the database
  905. * @param int $column the column that is
  906. * @param string $direction the sorting direction (ASC or DESC)
  907. *
  908. * @return array
  909. *
  910. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  911. *
  912. * @version August 2008
  913. *
  914. * @since v1.8.6
  915. */
  916. function getTemplateData($from, $number_of_items, $column, $direction)
  917. {
  918. // Database table definition.
  919. $table_system_template = Database::get_main_table('system_template');
  920. // The sql statement.
  921. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  922. $sql .= " ORDER BY col$column $direction ";
  923. $sql .= " LIMIT $from,$number_of_items";
  924. $result = Database::query($sql);
  925. $return = [];
  926. while ($row = Database::fetch_array($result)) {
  927. $row['1'] = get_lang($row['1']);
  928. $return[] = $row;
  929. }
  930. // Returning all the information for the sortable table.
  931. return $return;
  932. }
  933. /**
  934. * display the edit and delete icons in the sortable table.
  935. *
  936. * @param int $id the id of the template
  937. *
  938. * @return string code for the link to edit and delete the template
  939. *
  940. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  941. *
  942. * @version August 2008
  943. *
  944. * @since v1.8.6
  945. */
  946. function actionsFilter($id)
  947. {
  948. $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>';
  949. $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>';
  950. return $return;
  951. }
  952. /**
  953. * Display the image of the template in the sortable table.
  954. *
  955. * @param string $image the image
  956. *
  957. * @return string code for the image
  958. *
  959. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  960. *
  961. * @version August 2008
  962. *
  963. * @since v1.8.6
  964. */
  965. function searchImageFilter($image)
  966. {
  967. if (!empty($image)) {
  968. return '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  969. } else {
  970. return '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  971. }
  972. }
  973. /**
  974. * Add (or edit) a template. This function displays the form and also takes
  975. * care of uploading the image and storing the information in the database.
  976. *
  977. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  978. *
  979. * @version August 2008
  980. *
  981. * @since v1.8.6
  982. */
  983. function addEditTemplate()
  984. {
  985. $em = Database::getManager();
  986. // Initialize the object.
  987. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  988. /** @var SystemTemplate $template */
  989. $template = $id ? $em->find('ChamiloCoreBundle:SystemTemplate', $id) : new SystemTemplate();
  990. $form = new FormValidator(
  991. 'template',
  992. 'post',
  993. 'settings.php?category=Templates&action='.Security::remove_XSS($_GET['action']).'&id='.$id
  994. );
  995. // Setting the form elements: the header.
  996. if ($_GET['action'] == 'add') {
  997. $title = get_lang('AddTemplate');
  998. } else {
  999. $title = get_lang('EditTemplate');
  1000. }
  1001. $form->addElement('header', '', $title);
  1002. // Setting the form elements: the title of the template.
  1003. $form->addText('title', get_lang('Title'), false);
  1004. // Setting the form elements: the content of the template (wysiwyg editor).
  1005. $form->addHtmlEditor(
  1006. 'template_text',
  1007. get_lang('Text'),
  1008. true,
  1009. true,
  1010. ['ToolbarSet' => 'Documents', 'Width' => '100%', 'Height' => '400']
  1011. );
  1012. // Setting the form elements: the form to upload an image to be used with the template.
  1013. $form->addElement('file', 'template_image', get_lang('Image'), '');
  1014. // Setting the form elements: a little bit information about the template image.
  1015. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  1016. // Getting all the information of the template when editing a template.
  1017. if ($_GET['action'] == 'edit') {
  1018. $defaults['template_id'] = $id;
  1019. $defaults['template_text'] = $template->getContent();
  1020. // Forcing get_lang().
  1021. $defaults['title'] = get_lang($template->getTitle());
  1022. // Adding an extra field: a hidden field with the id of the template we are editing.
  1023. $form->addElement('hidden', 'template_id');
  1024. // Adding an extra field: a preview of the image that is currently used.
  1025. if (!empty($template->getImage())) {
  1026. $form->addElement(
  1027. 'static',
  1028. 'template_image_preview',
  1029. '',
  1030. '<img src="'.api_get_path(WEB_APP_PATH)
  1031. .'home/default_platform_document/template_thumb/'.$template->getImage()
  1032. .'" alt="'.get_lang('TemplatePreview')
  1033. .'"/>'
  1034. );
  1035. } else {
  1036. $form->addElement(
  1037. 'static',
  1038. 'template_image_preview',
  1039. '',
  1040. '<img src="'.api_get_path(WEB_APP_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>'
  1041. );
  1042. }
  1043. // Setting the information of the template that we are editing.
  1044. $form->setDefaults($defaults);
  1045. }
  1046. // Setting the form elements: the submit button.
  1047. $form->addButtonSave(get_lang('Ok'), 'submit');
  1048. // Setting the rules: the required fields.
  1049. $form->addRule(
  1050. 'template_image',
  1051. get_lang('ThisFieldIsRequired'),
  1052. 'required'
  1053. );
  1054. $form->addRule('title', get_lang('ThisFieldIsRequired'), 'required');
  1055. // if the form validates (complies to all rules) we save the information,
  1056. // else we display the form again (with error message if needed)
  1057. if ($form->validate()) {
  1058. $check = Security::check_token('post');
  1059. if ($check) {
  1060. // Exporting the values.
  1061. $values = $form->exportValues();
  1062. // Upload the file.
  1063. if (!empty($_FILES['template_image']['name'])) {
  1064. $upload_ok = process_uploaded_file($_FILES['template_image']);
  1065. if ($upload_ok) {
  1066. // Try to add an extension to the file if it hasn't one.
  1067. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  1068. // The upload directory.
  1069. $upload_dir = api_get_path(SYS_APP_PATH).'home/default_platform_document/template_thumb/';
  1070. // Create the directory if it does not exist.
  1071. if (!is_dir($upload_dir)) {
  1072. mkdir($upload_dir, api_get_permissions_for_new_directories());
  1073. }
  1074. // Resize the preview image to max default and upload.
  1075. $temp = new Image($_FILES['template_image']['tmp_name']);
  1076. $picture_info = $temp->get_image_info();
  1077. $max_width_for_picture = 100;
  1078. if ($picture_info['width'] > $max_width_for_picture) {
  1079. $temp->resize($max_width_for_picture);
  1080. }
  1081. $temp->send_image($upload_dir.$new_file_name);
  1082. }
  1083. }
  1084. // Store the information in the database (as insert or as update).
  1085. $bootstrap = api_get_css(api_get_path(WEB_PUBLIC_PATH).'assets/bootstrap/dist/css/bootstrap.min.css');
  1086. $viewport = '<meta name="viewport" content="width=device-width, initial-scale=1.0">';
  1087. if ($_GET['action'] == 'add') {
  1088. $templateContent = '<head>'.$viewport.'<title>'.$values['title'].'</title>'.$bootstrap.'</head>'
  1089. .$values['template_text'];
  1090. $template
  1091. ->setTitle($values['title'])
  1092. ->setContent(Security::remove_XSS($templateContent, COURSEMANAGERLOWSECURITY))
  1093. ->setImage($new_file_name);
  1094. $em->persist($template);
  1095. $em->flush();
  1096. // Display a feedback message.
  1097. echo Display::return_message(
  1098. get_lang('TemplateAdded'),
  1099. 'confirm'
  1100. );
  1101. echo '<a href="settings.php?category=Templates&action=add">'.
  1102. Display::return_icon('new_template.png', get_lang('AddTemplate'), '', ICON_SIZE_MEDIUM).
  1103. '</a>';
  1104. } else {
  1105. $templateContent = '<head>'.$viewport.'<title>'.$values['title'].'</title>'.$bootstrap.'</head>'
  1106. .$values['template_text'];
  1107. $template
  1108. ->setTitle($values['title'])
  1109. ->setContent(Security::remove_XSS($templateContent, COURSEMANAGERLOWSECURITY));
  1110. if (!empty($new_file_name)) {
  1111. $template->setImage($new_file_name);
  1112. }
  1113. $em->persist($template);
  1114. $em->flush();
  1115. // Display a feedback message.
  1116. echo Display::return_message(get_lang('TemplateEdited'), 'confirm');
  1117. }
  1118. }
  1119. Security::clear_token();
  1120. displayTemplates();
  1121. } else {
  1122. $token = Security::get_token();
  1123. $form->addElement('hidden', 'sec_token');
  1124. $form->setConstants(['sec_token' => $token]);
  1125. // Display the form.
  1126. $form->display();
  1127. }
  1128. }
  1129. /**
  1130. * Delete a template.
  1131. *
  1132. * @param int $id the id of the template that has to be deleted
  1133. *
  1134. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1135. *
  1136. * @version August 2008
  1137. *
  1138. * @since v1.8.6
  1139. */
  1140. function deleteTemplate($id)
  1141. {
  1142. $id = intval($id);
  1143. // First we remove the image.
  1144. $table = Database::get_main_table('system_template');
  1145. $sql = "SELECT * FROM $table WHERE id = $id";
  1146. $result = Database::query($sql);
  1147. $row = Database::fetch_array($result);
  1148. if (!empty($row['image'])) {
  1149. @unlink(api_get_path(SYS_APP_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  1150. }
  1151. // Now we remove it from the database.
  1152. $sql = "DELETE FROM $table WHERE id = $id";
  1153. Database::query($sql);
  1154. // Display a feedback message.
  1155. echo Display::return_message(get_lang('TemplateDeleted'), 'confirm');
  1156. }
  1157. /**
  1158. * Returns the list of timezone identifiers used to populate the select
  1159. * This function is called through a call_user_func() in the generate_settings_form function.
  1160. *
  1161. * @return array List of timezone identifiers
  1162. *
  1163. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1164. *
  1165. * @since Chamilo 1.8.7
  1166. */
  1167. function select_timezone_value()
  1168. {
  1169. return api_get_timezones();
  1170. }
  1171. /**
  1172. * Returns an array containing the list of options used to populate the gradebook_number_decimals variable
  1173. * This function is called through a call_user_func() in the generate_settings_form function.
  1174. *
  1175. * @return array List of gradebook_number_decimals options
  1176. *
  1177. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  1178. */
  1179. function select_gradebook_number_decimals()
  1180. {
  1181. return ['0', '1', '2'];
  1182. }
  1183. /**
  1184. * Get the options for a select element to select gradebook default grade model.
  1185. *
  1186. * @return array
  1187. */
  1188. function select_gradebook_default_grade_model_id()
  1189. {
  1190. $grade_model = new GradeModel();
  1191. $models = $grade_model->get_all();
  1192. $options = [];
  1193. $options[-1] = get_lang('None');
  1194. if (!empty($models)) {
  1195. foreach ($models as $model) {
  1196. $options[$model['id']] = $model['name'];
  1197. }
  1198. }
  1199. return $options;
  1200. }
  1201. /**
  1202. * @param array $settings
  1203. * @param array $settings_by_access_list
  1204. *
  1205. * @throws \Doctrine\ORM\ORMException
  1206. * @throws \Doctrine\ORM\OptimisticLockException
  1207. * @throws \Doctrine\ORM\TransactionRequiredException
  1208. *
  1209. * @return FormValidator
  1210. */
  1211. function generateSettingsForm($settings, $settings_by_access_list)
  1212. {
  1213. global $_configuration, $settings_to_avoid, $convert_byte_to_mega_list;
  1214. $em = Database::getManager();
  1215. $table_settings_current = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1216. $form = new FormValidator(
  1217. 'settings',
  1218. 'post',
  1219. 'settings.php?category='.Security::remove_XSS($_GET['category'])
  1220. );
  1221. $form->addElement(
  1222. 'hidden',
  1223. 'search_field',
  1224. (!empty($_GET['search_field']) ? Security::remove_XSS($_GET['search_field']) : null)
  1225. );
  1226. $url_id = api_get_current_access_url_id();
  1227. /*
  1228. if (!empty($_configuration['multiple_access_urls']) && api_is_global_platform_admin() && $url_id == 1) {
  1229. $group = array();
  1230. $group[] = $form->createElement('button', 'mark_all', get_lang('MarkAll'));
  1231. $group[] = $form->createElement('button', 'unmark_all', get_lang('UnmarkAll'));
  1232. $form->addGroup($group, 'buttons_in_action_right');
  1233. }*/
  1234. $default_values = [];
  1235. $url_info = api_get_access_url($url_id);
  1236. $i = 0;
  1237. $addedSettings = [];
  1238. foreach ($settings as $row) {
  1239. if (in_array($row['variable'], array_keys($settings_to_avoid))) {
  1240. continue;
  1241. }
  1242. if (in_array($row['variable'], $addedSettings)) {
  1243. continue;
  1244. }
  1245. $addedSettings[] = $row['variable'];
  1246. if (!empty($_configuration['multiple_access_urls'])) {
  1247. if (api_is_global_platform_admin()) {
  1248. if ($row['access_url_locked'] == 0) {
  1249. if ($url_id == 1) {
  1250. if ($row['access_url_changeable'] == '1') {
  1251. $form->addElement(
  1252. 'html',
  1253. '<div class="pull-right"><a class="share_this_setting" data_status = "0" data_to_send = "'.$row['variable'].'" href="javascript:void(0);">'.
  1254. Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting'), null, ICON_SIZE_MEDIUM).'</a></div>'
  1255. );
  1256. } else {
  1257. $form->addElement(
  1258. 'html',
  1259. '<div class="pull-right"><a class="share_this_setting" data_status = "1" data_to_send = "'.$row['variable'].'" href="javascript:void(0);">'.
  1260. Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting'), null, ICON_SIZE_MEDIUM).'</a></div>'
  1261. );
  1262. }
  1263. } else {
  1264. if ($row['access_url_changeable'] == '1') {
  1265. $form->addElement(
  1266. 'html',
  1267. '<div class="pull-right">'.
  1268. Display::return_icon('shared_setting.png', get_lang('ChangeSharedSetting'), null, ICON_SIZE_MEDIUM).'</div>'
  1269. );
  1270. } else {
  1271. $form->addElement(
  1272. 'html',
  1273. '<div class="pull-right">'.
  1274. Display::return_icon('shared_setting_na.png', get_lang('ChangeSharedSetting'), null, ICON_SIZE_MEDIUM).'</div>'
  1275. );
  1276. }
  1277. }
  1278. }
  1279. }
  1280. }
  1281. $hideme = [];
  1282. $hide_element = false;
  1283. if ($_configuration['access_url'] != 1) {
  1284. if ($row['access_url_changeable'] == 0) {
  1285. // We hide the element in other cases (checkbox, radiobutton) we 'freeze' the element.
  1286. $hide_element = true;
  1287. $hideme = ['disabled'];
  1288. } elseif ($url_info['active'] == 1) {
  1289. // We show the elements.
  1290. if (empty($row['variable'])) {
  1291. $row['variable'] = 0;
  1292. }
  1293. if (empty($row['subkey'])) {
  1294. $row['subkey'] = 0;
  1295. }
  1296. if (empty($row['category'])) {
  1297. $row['category'] = 0;
  1298. }
  1299. if (isset($settings_by_access_list[$row['variable']]) &&
  1300. isset($settings_by_access_list[$row['variable']][$row['subkey']]) &&
  1301. is_array($settings_by_access_list[$row['variable']][$row['subkey']][$row['category']])
  1302. ) {
  1303. // We are sure that the other site have a selected value.
  1304. if ($settings_by_access_list[$row['variable']][$row['subkey']][$row['category']]['selected_value'] != '') {
  1305. $row['selected_value'] = $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']]['selected_value'];
  1306. }
  1307. }
  1308. // There is no else{} statement because we load the default $row['selected_value'] of the main Chamilo site.
  1309. }
  1310. }
  1311. switch ($row['type']) {
  1312. case 'textfield':
  1313. if (in_array($row['variable'], $convert_byte_to_mega_list)) {
  1314. $form->addElement(
  1315. 'text',
  1316. $row['variable'],
  1317. [
  1318. get_lang($row['title']),
  1319. get_lang($row['comment']),
  1320. get_lang('MB'),
  1321. ],
  1322. ['maxlength' => '8', 'aria-label' => get_lang($row['title'])]
  1323. );
  1324. $form->applyFilter($row['variable'], 'html_filter');
  1325. $default_values[$row['variable']] = round($row['selected_value'] / 1024 / 1024, 1);
  1326. } elseif ($row['variable'] == 'account_valid_duration') {
  1327. $form->addElement(
  1328. 'text',
  1329. $row['variable'],
  1330. [
  1331. get_lang($row['title']),
  1332. get_lang($row['comment']),
  1333. ],
  1334. ['maxlength' => '5', 'aria-label' => get_lang($row['title'])]
  1335. );
  1336. $form->applyFilter($row['variable'], 'html_filter');
  1337. // For platform character set selection:
  1338. // Conversion of the textfield to a select box with valid values.
  1339. $default_values[$row['variable']] = $row['selected_value'];
  1340. } elseif ($row['variable'] == 'platform_charset') {
  1341. break;
  1342. } else {
  1343. $hideme['class'] = 'col-md-4';
  1344. $hideme['aria-label'] = get_lang($row['title']);
  1345. $form->addElement(
  1346. 'text',
  1347. $row['variable'],
  1348. [
  1349. get_lang($row['title']),
  1350. get_lang($row['comment']),
  1351. ],
  1352. $hideme
  1353. );
  1354. $form->applyFilter($row['variable'], 'html_filter');
  1355. $default_values[$row['variable']] = $row['selected_value'];
  1356. }
  1357. break;
  1358. case 'textarea':
  1359. if ($row['variable'] == 'header_extra_content') {
  1360. $file = api_get_home_path().'header_extra_content.txt';
  1361. $value = '';
  1362. if (file_exists($file)) {
  1363. $value = file_get_contents($file);
  1364. }
  1365. $form->addElement(
  1366. 'textarea',
  1367. $row['variable'],
  1368. [get_lang($row['title']), get_lang($row['comment'])],
  1369. ['rows' => '10', 'id' => $row['variable']],
  1370. $hideme
  1371. );
  1372. $default_values[$row['variable']] = $value;
  1373. } elseif ($row['variable'] == 'footer_extra_content') {
  1374. $file = api_get_home_path().'footer_extra_content.txt';
  1375. $value = '';
  1376. if (file_exists($file)) {
  1377. $value = file_get_contents($file);
  1378. }
  1379. $form->addElement(
  1380. 'textarea',
  1381. $row['variable'],
  1382. [get_lang($row['title']), get_lang($row['comment'])],
  1383. ['rows' => '10', 'id' => $row['variable']],
  1384. $hideme
  1385. );
  1386. $default_values[$row['variable']] = $value;
  1387. } else {
  1388. $form->addElement(
  1389. 'textarea',
  1390. $row['variable'],
  1391. [get_lang($row['title']),
  1392. get_lang($row['comment']), ],
  1393. ['rows' => '10', 'id' => $row['variable']],
  1394. $hideme
  1395. );
  1396. $default_values[$row['variable']] = $row['selected_value'];
  1397. }
  1398. break;
  1399. case 'radio':
  1400. $values = api_get_settings_options($row['variable']);
  1401. $group = [];
  1402. if (is_array($values)) {
  1403. foreach ($values as $key => $value) {
  1404. $element = &$form->createElement(
  1405. 'radio',
  1406. $row['variable'],
  1407. '',
  1408. get_lang($value['display_text']),
  1409. $value['value']
  1410. );
  1411. if ($hide_element) {
  1412. $element->freeze();
  1413. }
  1414. $group[] = $element;
  1415. }
  1416. }
  1417. $form->addGroup(
  1418. $group,
  1419. $row['variable'],
  1420. [get_lang($row['title']), get_lang($row['comment'])],
  1421. null,
  1422. false
  1423. );
  1424. $default_values[$row['variable']] = $row['selected_value'];
  1425. break;
  1426. case 'checkbox':
  1427. // 1. We collect all the options of this variable.
  1428. $sql = "SELECT * FROM $table_settings_current
  1429. WHERE variable='".$row['variable']."' AND access_url = 1";
  1430. $result = Database::query($sql);
  1431. $group = [];
  1432. while ($rowkeys = Database::fetch_array($result)) {
  1433. // Profile tab option should be hidden when the social tool is enabled.
  1434. if (api_get_setting('allow_social_tool') == 'true') {
  1435. if ($rowkeys['variable'] === 'show_tabs' && $rowkeys['subkey'] === 'my_profile') {
  1436. continue;
  1437. }
  1438. }
  1439. // Hiding the gradebook option.
  1440. if ($rowkeys['variable'] === 'show_tabs' && $rowkeys['subkey'] === 'my_gradebook') {
  1441. continue;
  1442. }
  1443. $element = &$form->createElement(
  1444. 'checkbox',
  1445. $rowkeys['subkey'],
  1446. '',
  1447. get_lang($rowkeys['subkeytext'])
  1448. );
  1449. if ($row['access_url_changeable'] == 1) {
  1450. // 2. We look into the DB if there is a setting for a specific access_url.
  1451. $access_url = $_configuration['access_url'];
  1452. if (empty($access_url)) {
  1453. $access_url = 1;
  1454. }
  1455. $sql = "SELECT selected_value FROM $table_settings_current
  1456. WHERE
  1457. variable='".$rowkeys['variable']."' AND
  1458. subkey='".$rowkeys['subkey']."' AND
  1459. subkeytext='".$rowkeys['subkeytext']."' AND
  1460. access_url = $access_url";
  1461. $result_access = Database::query($sql);
  1462. $row_access = Database::fetch_array($result_access);
  1463. if ($row_access['selected_value'] === 'true' && !$form->isSubmitted()) {
  1464. $element->setChecked(true);
  1465. }
  1466. } else {
  1467. if ($rowkeys['selected_value'] === 'true' && !$form->isSubmitted()) {
  1468. $element->setChecked(true);
  1469. }
  1470. }
  1471. if ($hide_element) {
  1472. $element->freeze();
  1473. }
  1474. $group[] = $element;
  1475. }
  1476. $form->addGroup(
  1477. $group,
  1478. $row['variable'],
  1479. [get_lang($row['title']), get_lang($row['comment'])],
  1480. null
  1481. );
  1482. break;
  1483. case 'link':
  1484. $form->addElement(
  1485. 'static',
  1486. null,
  1487. [get_lang($row['title']), get_lang($row['comment'])],
  1488. get_lang('CurrentValue').' : '.$row['selected_value'],
  1489. $hideme
  1490. );
  1491. break;
  1492. case 'select':
  1493. /*
  1494. * 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.
  1495. * The functions being called must be added to the file settings.lib.php.
  1496. */
  1497. $form->addElement(
  1498. 'select',
  1499. $row['variable'],
  1500. [get_lang($row['title']), get_lang($row['comment'])],
  1501. call_user_func('select_'.$row['variable']),
  1502. $hideme
  1503. );
  1504. $default_values[$row['variable']] = $row['selected_value'];
  1505. break;
  1506. case 'custom':
  1507. break;
  1508. case 'select_course':
  1509. $courseSelectOptions = [];
  1510. if (!empty($row['selected_value'])) {
  1511. $course = $em->find('ChamiloCoreBundle:Course', $row['selected_value']);
  1512. $courseSelectOptions[$course->getId()] = $course->getTitle();
  1513. }
  1514. $form->addElement(
  1515. 'select_ajax',
  1516. $row['variable'],
  1517. [get_lang($row['title']), get_lang($row['comment'])],
  1518. $courseSelectOptions,
  1519. ['url' => api_get_path(WEB_AJAX_PATH).'course.ajax.php?a=search_course']
  1520. );
  1521. $default_values[$row['variable']] = $row['selected_value'];
  1522. break;
  1523. }
  1524. switch ($row['variable']) {
  1525. case 'pdf_export_watermark_enable':
  1526. $url = PDF::get_watermark(null);
  1527. if ($url != false) {
  1528. $delete_url = '<a href="?delete_watermark">'.get_lang('DelImage').' '.Display::return_icon('delete.png', get_lang('DelImage')).'</a>';
  1529. $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>');
  1530. }
  1531. $form->addElement('file', 'pdf_export_watermark_path', get_lang('AddWaterMark'));
  1532. $allowed_picture_types = ['jpg', 'jpeg', 'png', 'gif'];
  1533. $form->addRule(
  1534. 'pdf_export_watermark_path',
  1535. get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')',
  1536. 'filetype',
  1537. $allowed_picture_types
  1538. );
  1539. break;
  1540. case 'timezone_value':
  1541. $timezone = $row['selected_value'];
  1542. if (empty($timezone)) {
  1543. $timezone = api_get_timezone();
  1544. }
  1545. $form->addLabel('', sprintf(get_lang('LocalTimeUsingPortalTimezoneXIsY'), $timezone, api_get_local_time()));
  1546. break;
  1547. }
  1548. } // end for
  1549. if (!empty($settings)) {
  1550. $form->setDefaults($default_values);
  1551. }
  1552. $form->addHtml('<div class="bottom_actions">');
  1553. $form->addButtonSave(get_lang('SaveSettings'));
  1554. $form->addHtml('</div>');
  1555. return $form;
  1556. }
  1557. /**
  1558. * Searches a platform setting in all categories except from the Plugins category.
  1559. *
  1560. * @param string $search
  1561. *
  1562. * @return array
  1563. */
  1564. function searchSetting($search)
  1565. {
  1566. if (empty($search)) {
  1567. return [];
  1568. }
  1569. $table_settings_current = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1570. $sql = "SELECT * FROM $table_settings_current
  1571. WHERE category <> 'Plugins' ORDER BY id ASC ";
  1572. $result = Database::store_result(Database::query($sql), 'ASSOC');
  1573. $settings = [];
  1574. $search = api_strtolower($search);
  1575. if (!empty($result)) {
  1576. foreach ($result as $setting) {
  1577. $found = false;
  1578. $title = api_strtolower(get_lang($setting['title']));
  1579. // try the title
  1580. if (strpos($title, $search) === false) {
  1581. $comment = api_strtolower(get_lang($setting['comment']));
  1582. //Try the comment
  1583. if (strpos($comment, $search) === false) {
  1584. //Try the variable name
  1585. if (strpos($setting['variable'], $search) === false) {
  1586. continue;
  1587. } else {
  1588. $found = true;
  1589. }
  1590. } else {
  1591. $found = true;
  1592. }
  1593. } else {
  1594. $found = true;
  1595. }
  1596. if ($found) {
  1597. $settings[] = $setting;
  1598. }
  1599. }
  1600. }
  1601. return $settings;
  1602. }
  1603. /**
  1604. * Helper function to generates a form elements group.
  1605. *
  1606. * @param object $form The form where the elements group has to be added
  1607. * @param array $values Values to browse through
  1608. *
  1609. * @return array
  1610. */
  1611. function formGenerateElementsGroup($form, $values = [], $elementName)
  1612. {
  1613. $group = [];
  1614. if (is_array($values)) {
  1615. foreach ($values as $key => $value) {
  1616. $element = &$form->createElement('radio', $elementName, '', get_lang($value['display_text']), $value['value']);
  1617. $group[] = $element;
  1618. }
  1619. }
  1620. return $group;
  1621. }
  1622. /**
  1623. * Helper function with allowed file types for CSS.
  1624. *
  1625. * @return array Array of file types (no indexes)
  1626. */
  1627. function getAllowedFileTypes()
  1628. {
  1629. $allowedFiles = [
  1630. 'css',
  1631. 'zip',
  1632. 'jpeg',
  1633. 'jpg',
  1634. 'png',
  1635. 'gif',
  1636. 'ico',
  1637. 'psd',
  1638. 'xcf',
  1639. 'svg',
  1640. 'webp',
  1641. 'woff',
  1642. 'woff2',
  1643. ];
  1644. return $allowedFiles;
  1645. }
  1646. /**
  1647. * Helper function to set settings in the database.
  1648. *
  1649. * @param array $parameters List of values
  1650. * @param int $accessUrl The current access URL
  1651. */
  1652. function setConfigurationSettingsInDatabase($parameters, $accessUrl)
  1653. {
  1654. api_set_settings_category('Search', 'false', $accessUrl);
  1655. // Save the settings.
  1656. foreach ($parameters as $key => $value) {
  1657. api_set_setting($key, $value, null, null);
  1658. }
  1659. }
  1660. /**
  1661. * Helper function to show the status of the search settings table.
  1662. *
  1663. * @param array $data Data to show
  1664. */
  1665. function showSearchSettingsTable($data)
  1666. {
  1667. echo Display::tag('h3', get_lang('Settings'));
  1668. $table = new SortableTableFromArray($data);
  1669. $table->set_header(0, get_lang('Setting'), false);
  1670. $table->set_header(1, get_lang('Status'), false);
  1671. echo $table->display();
  1672. }
  1673. /**
  1674. * Helper function to show status table for each command line tool installed.
  1675. */
  1676. function showSearchToolsStatusTable()
  1677. {
  1678. //@todo windows support
  1679. if (api_is_windows_os() == false) {
  1680. $list_of_programs = ['pdftotext', 'ps2pdf', 'catdoc', 'html2text', 'unrtf', 'catppt', 'xls2csv'];
  1681. foreach ($list_of_programs as $program) {
  1682. $output = [];
  1683. $ret_val = null;
  1684. exec("which $program", $output, $ret_val);
  1685. if (!$output) {
  1686. $output[] = '';
  1687. }
  1688. $icon = Display::return_icon('bullet_red.png', get_lang('NotInstalled'));
  1689. if (!empty($output[0])) {
  1690. $icon = Display::return_icon('bullet_green.png', get_lang('Installed'));
  1691. }
  1692. $data2[] = [$program, $output[0], $icon];
  1693. }
  1694. echo Display::tag('h3', get_lang('ProgramsNeededToConvertFiles'));
  1695. $table = new SortableTableFromArray($data2);
  1696. $table->set_header(0, get_lang('Program'), false);
  1697. $table->set_header(1, get_lang('Path'), false);
  1698. $table->set_header(2, get_lang('Status'), false);
  1699. echo $table->display();
  1700. } else {
  1701. echo Display::return_message(
  1702. get_lang('YouAreUsingChamiloInAWindowsPlatformSadlyYouCantConvertDocumentsInOrderToSearchTheContentUsingThisTool'),
  1703. 'warning'
  1704. );
  1705. }
  1706. }
  1707. /**
  1708. * Helper function to generate and show CSS Zip download message.
  1709. *
  1710. * @param string $style Style path
  1711. */
  1712. function generateCSSDownloadLink($style)
  1713. {
  1714. $arch = api_get_path(SYS_ARCHIVE_PATH).$style.'.zip';
  1715. $themeDir = Template::getThemeDir($style);
  1716. $dir = api_get_path(SYS_CSS_PATH).$themeDir;
  1717. $check = Security::check_abs_path(
  1718. $dir,
  1719. api_get_path(SYS_CSS_PATH).'themes'
  1720. );
  1721. if (is_dir($dir) && $check) {
  1722. $zip = new PclZip($arch);
  1723. // Remove path prefix except the style name and put file on disk
  1724. $zip->create($dir, PCLZIP_OPT_REMOVE_PATH, substr($dir, 0, -strlen($style)));
  1725. $url = api_get_path(WEB_CODE_PATH).'course_info/download.php?archive_path=&archive='.str_replace(api_get_path(SYS_ARCHIVE_PATH), '', $arch);
  1726. //@TODO: use more generic script to download.
  1727. $str = '<a class="btn btn-primary btn-large" href="'.$url.'">'.get_lang('ClickHereToDownloadTheFile').'</a>';
  1728. echo Display::return_message($str, 'normal', false);
  1729. } else {
  1730. echo Display::return_message(get_lang('FileNotFound'), 'warning');
  1731. }
  1732. }
  1733. /**
  1734. * Helper function to tell if the style is changeable in the current URL.
  1735. *
  1736. * @return bool $changeable Whether the style can be changed in this URL or not
  1737. */
  1738. function isStyleChangeable()
  1739. {
  1740. $changeable = false;
  1741. $urlId = api_get_current_access_url_id();
  1742. if ($urlId) {
  1743. $style_info = api_get_settings('stylesheets', '', 1, 0);
  1744. $url_info = api_get_access_url($urlId);
  1745. if ($style_info[0]['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  1746. $changeable = true;
  1747. }
  1748. } else {
  1749. $changeable = true;
  1750. }
  1751. return $changeable;
  1752. }
  1753. /**
  1754. * Get all settings of one category prepared for display in admin/settings.php.
  1755. *
  1756. * @param string $category
  1757. *
  1758. * @return array
  1759. */
  1760. function getCategorySettings($category = '')
  1761. {
  1762. $url_id = api_get_current_access_url_id();
  1763. $settings_by_access_list = [];
  1764. if ($url_id == 1) {
  1765. $settings = api_get_settings($category, 'group', $url_id);
  1766. } else {
  1767. $url_info = api_get_access_url($url_id);
  1768. if ($url_info['active'] == 1) {
  1769. $categoryToSearch = $category;
  1770. if ($category == 'search_setting') {
  1771. $categoryToSearch = '';
  1772. }
  1773. // The default settings of Chamilo
  1774. $settings = api_get_settings($categoryToSearch, 'group', 1, 0);
  1775. // The settings that are changeable from a particular site.
  1776. $settings_by_access = api_get_settings($categoryToSearch, 'group', $url_id, 1);
  1777. foreach ($settings_by_access as $row) {
  1778. if (empty($row['variable'])) {
  1779. $row['variable'] = 0;
  1780. }
  1781. if (empty($row['subkey'])) {
  1782. $row['subkey'] = 0;
  1783. }
  1784. if (empty($row['category'])) {
  1785. $row['category'] = 0;
  1786. }
  1787. // One more validation if is changeable.
  1788. if ($row['access_url_changeable'] == 1) {
  1789. $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']] = $row;
  1790. } else {
  1791. $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']] = [];
  1792. }
  1793. }
  1794. }
  1795. }
  1796. if (isset($category) && $category == 'search_setting') {
  1797. if (!empty($_REQUEST['search_field'])) {
  1798. $settings = searchSetting($_REQUEST['search_field']);
  1799. }
  1800. }
  1801. return [
  1802. 'settings' => $settings,
  1803. 'settings_by_access_list' => $settings_by_access_list,
  1804. ];
  1805. }