template.lib.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class Template
  5. *
  6. * @author Julio Montoya <gugli100@gmail.com>
  7. * @todo better organization of the class, methods and variables
  8. *
  9. */
  10. class Template
  11. {
  12. /**
  13. * The Template folder name see main/template
  14. * @var string
  15. */
  16. public $templateFolder = 'default';
  17. /**
  18. * The theme that will be used: chamilo, public_admin, chamilo_red, etc
  19. * This variable is set from the database
  20. * @var string
  21. */
  22. public $theme = '';
  23. private $themeDir;
  24. /**
  25. * @var string
  26. */
  27. public $preview_theme = '';
  28. public $title = null;
  29. public $show_header;
  30. public $show_footer;
  31. public $help;
  32. public $menu_navigation = array(); //Used in the userportal.lib.php function: return_navigation_course_links()
  33. public $show_learnpath = false; // This is a learnpath section or not?
  34. public $plugin = null;
  35. public $course_id = null;
  36. public $user_is_logged_in = false;
  37. public $twig = null;
  38. /* Loads chamilo plugins */
  39. public $load_plugins = false;
  40. public $params = array();
  41. public $force_plugin_load = false;
  42. /**
  43. * @param string $title
  44. * @param bool $show_header
  45. * @param bool $show_footer
  46. * @param bool $show_learnpath
  47. * @param bool $hide_global_chat
  48. * @param bool $load_plugins
  49. * @param bool $sendHeaders send http headers or not
  50. */
  51. public function __construct(
  52. $title = '',
  53. $show_header = true,
  54. $show_footer = true,
  55. $show_learnpath = false,
  56. $hide_global_chat = false,
  57. $load_plugins = true,
  58. $sendHeaders = true
  59. ) {
  60. // Page title
  61. $this->title = $title;
  62. $this->show_learnpath = $show_learnpath;
  63. if (empty($this->show_learnpath)) {
  64. $origin = api_get_origin();
  65. if ($origin === 'learnpath') {
  66. $this->show_learnpath = true;
  67. $show_footer = false;
  68. $show_header = false;
  69. }
  70. }
  71. $this->hide_global_chat = $hide_global_chat;
  72. $this->load_plugins = $load_plugins;
  73. $template_paths = array(
  74. api_get_path(SYS_CODE_PATH).'template/overrides', // user defined templates
  75. api_get_path(SYS_CODE_PATH).'template', //template folder
  76. api_get_path(SYS_PLUGIN_PATH) // plugin folder
  77. );
  78. $urlId = api_get_current_access_url_id();
  79. $cache_folder = api_get_path(SYS_ARCHIVE_PATH).'twig/'.$urlId.'/';
  80. if (!is_dir($cache_folder)) {
  81. mkdir($cache_folder, api_get_permissions_for_new_directories(), true);
  82. }
  83. $loader = new Twig_Loader_Filesystem($template_paths);
  84. $isTestMode = api_get_setting('server_type') === 'test';
  85. //Setting Twig options depending on the server see http://twig.sensiolabs.org/doc/api.html#environment-options
  86. if ($isTestMode) {
  87. $options = array(
  88. //'cache' => api_get_path(SYS_ARCHIVE_PATH), //path to the cache folder
  89. 'autoescape' => false,
  90. 'debug' => true,
  91. 'auto_reload' => true,
  92. 'optimizations' => 0,
  93. // turn on optimizations with -1
  94. 'strict_variables' => false,
  95. //If set to false, Twig will silently ignore invalid variables
  96. );
  97. } else {
  98. $options = array(
  99. 'cache' => $cache_folder,
  100. //path to the cache folder
  101. 'autoescape' => false,
  102. 'debug' => false,
  103. 'auto_reload' => false,
  104. 'optimizations' => -1,
  105. // turn on optimizations with -1
  106. 'strict_variables' => false
  107. //If set to false, Twig will silently ignore invalid variables
  108. );
  109. }
  110. $this->twig = new Twig_Environment($loader, $options);
  111. if ($isTestMode) {
  112. $this->twig->addExtension(new Twig_Extension_Debug());
  113. }
  114. // Twig filters setup
  115. $filters = [
  116. 'get_plugin_lang',
  117. 'get_lang',
  118. 'api_get_path',
  119. 'api_get_local_time',
  120. 'api_convert_and_format_date',
  121. 'api_is_allowed_to_edit',
  122. 'api_get_user_info',
  123. 'api_get_configuration_value',
  124. 'api_get_setting',
  125. [
  126. 'name' => 'return_message',
  127. 'callable' => 'Display::return_message_and_translate'
  128. ],
  129. [
  130. 'name' => 'display_page_header',
  131. 'callable' => 'Display::page_header_and_translate'
  132. ],
  133. [
  134. 'name' => 'display_page_subheader',
  135. 'callable' => 'Display::page_subheader_and_translate'
  136. ],
  137. [
  138. 'name' => 'icon',
  139. 'callable' => 'Template::get_icon_path'
  140. ],
  141. [
  142. 'name' => 'img',
  143. 'callable' => 'Template::get_image'
  144. ],
  145. [
  146. 'name' => 'format_date',
  147. 'callable' => 'Template::format_date'
  148. ]
  149. ];
  150. foreach ($filters as $filter) {
  151. if (is_array($filter)) {
  152. $this->twig->addFilter(new Twig_SimpleFilter($filter['name'], $filter['callable']));
  153. } else {
  154. $this->twig->addFilter(new Twig_SimpleFilter($filter, $filter));
  155. }
  156. }
  157. // Setting system variables
  158. $this->set_system_parameters();
  159. // Setting user variables
  160. $this->set_user_parameters();
  161. // Setting course variables
  162. $this->set_course_parameters();
  163. // Setting administrator variables
  164. $this->setAdministratorParams();
  165. //$this->setCSSEditor();
  166. // Header and footer are showed by default
  167. $this->set_footer($show_footer);
  168. $this->set_header($show_header);
  169. $this->set_header_parameters($sendHeaders);
  170. $this->set_footer_parameters();
  171. $defaultStyle = api_get_configuration_value('default_template');
  172. if (!empty($defaultStyle)) {
  173. $this->templateFolder = $defaultStyle;
  174. }
  175. $this->assign('template', $this->templateFolder);
  176. $this->assign('locale', api_get_language_isocode());
  177. $this->assign('login_class', null);
  178. $allow = api_get_configuration_value('show_language_selector_in_menu');
  179. if ($allow) {
  180. $this->assign('language_form', api_display_language_form());
  181. }
  182. // Chamilo plugins
  183. if ($this->show_header) {
  184. if ($this->load_plugins) {
  185. $this->plugin = new AppPlugin();
  186. //1. Showing installed plugins in regions
  187. $pluginRegions = $this->plugin->get_plugin_regions();
  188. foreach ($pluginRegions as $region) {
  189. $this->set_plugin_region($region);
  190. }
  191. //2. Loading the course plugin info
  192. global $course_plugin;
  193. if (isset($course_plugin) && !empty($course_plugin) && !empty($this->course_id)) {
  194. //Load plugin get_langs
  195. $this->plugin->load_plugin_lang_variables($course_plugin);
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * @param string $image
  202. * @param int $size
  203. *
  204. * @return string
  205. */
  206. public static function get_icon_path($image, $size = ICON_SIZE_SMALL)
  207. {
  208. return Display::return_icon($image, '', array(), $size, false, true);
  209. }
  210. /**
  211. * @param string $image
  212. * @param int $size
  213. * @param string $name
  214. * @return string
  215. */
  216. public static function get_image($image, $size = ICON_SIZE_SMALL, $name = '')
  217. {
  218. return Display::return_icon($image, $name, array(), $size);
  219. }
  220. /**
  221. * @param string $timestamp
  222. * @param string $format
  223. *
  224. * @return string
  225. */
  226. public static function format_date($timestamp, $format = null)
  227. {
  228. return api_format_date($timestamp, $format);
  229. }
  230. /**
  231. * Return the item's url key:
  232. *
  233. * c_id=xx&id=xx
  234. *
  235. * @param object $item
  236. * @return string
  237. */
  238. public static function key($item)
  239. {
  240. $id = isset($item->id) ? $item->id : null;
  241. $c_id = isset($item->c_id) ? $item->c_id : null;
  242. $result = '';
  243. if ($c_id) {
  244. $result = "c_id=$c_id";
  245. }
  246. if ($id) {
  247. if ($result) {
  248. $result .= "&amp;id=$id";
  249. } else {
  250. $result .= "&amp;id=$id";
  251. }
  252. }
  253. return $result;
  254. }
  255. /**
  256. * @param string $helpInput
  257. */
  258. public function setHelp($helpInput = null)
  259. {
  260. if (!empty($helpInput)) {
  261. $help = $helpInput;
  262. } else {
  263. $help = $this->help;
  264. }
  265. $content = '';
  266. if (api_get_setting('enable_help_link') == 'true') {
  267. if (!empty($help)) {
  268. $help = Security::remove_XSS($help);
  269. $content = '<div class="help">';
  270. $content .= Display::url(
  271. Display::return_icon('help.large.png', get_lang('Help')),
  272. api_get_path(WEB_CODE_PATH).'help/help.php?open='.$help,
  273. [
  274. 'class' => 'ajax',
  275. 'data-title' => get_lang('Help')
  276. ]
  277. );
  278. $content .= '</div>';
  279. }
  280. }
  281. $this->assign('help_content', $content);
  282. }
  283. /**
  284. * Use template system to parse the actions menu
  285. * @todo finish it!
  286. **/
  287. public function set_actions($actions)
  288. {
  289. $action_string = '';
  290. if (!empty($actions)) {
  291. foreach ($actions as $action) {
  292. $action_string .= $action;
  293. }
  294. }
  295. $this->assign('actions', $actions);
  296. }
  297. /**
  298. * Shortcut to display a 1 col layout (index.php)
  299. * */
  300. public function display_one_col_template()
  301. {
  302. $tpl = $this->get_template('layout/layout_1_col.tpl');
  303. $this->display($tpl);
  304. }
  305. /**
  306. * Shortcut to display a 2 col layout (userportal.php)
  307. **/
  308. public function display_two_col_template()
  309. {
  310. $tpl = $this->get_template('layout/layout_2_col.tpl');
  311. $this->display($tpl);
  312. }
  313. /**
  314. * Displays an empty template
  315. */
  316. public function display_blank_template()
  317. {
  318. $tpl = $this->get_template('layout/blank.tpl');
  319. $this->display($tpl);
  320. }
  321. /**
  322. * Displays an empty template
  323. */
  324. public function displayBlankTemplateNoHeader()
  325. {
  326. $tpl = $this->get_template('layout/blank_no_header.tpl');
  327. $this->display($tpl);
  328. }
  329. /**
  330. * Displays an empty template
  331. */
  332. public function display_no_layout_template()
  333. {
  334. $tpl = $this->get_template('layout/no_layout.tpl');
  335. $this->display($tpl);
  336. }
  337. /**
  338. * Sets the footer visibility
  339. * @param bool true if we show the footer
  340. */
  341. public function set_footer($status)
  342. {
  343. $this->show_footer = $status;
  344. $this->assign('show_footer', $status);
  345. }
  346. /**
  347. * return true if toolbar has to be displayed for user
  348. * @return bool
  349. */
  350. public static function isToolBarDisplayedForUser()
  351. {
  352. //Toolbar
  353. $show_admin_toolbar = api_get_setting('show_admin_toolbar');
  354. $show_toolbar = false;
  355. switch ($show_admin_toolbar) {
  356. case 'do_not_show':
  357. break;
  358. case 'show_to_admin':
  359. if (api_is_platform_admin()) {
  360. $show_toolbar = true;
  361. }
  362. break;
  363. case 'show_to_admin_and_teachers':
  364. if (api_is_platform_admin() || api_is_allowed_to_edit()) {
  365. $show_toolbar = true;
  366. }
  367. break;
  368. case 'show_to_all':
  369. $show_toolbar = true;
  370. break;
  371. }
  372. return $show_toolbar;
  373. }
  374. /**
  375. * Sets the header visibility
  376. * @param bool true if we show the header
  377. */
  378. public function set_header($status)
  379. {
  380. $this->show_header = $status;
  381. $this->assign('show_header', $status);
  382. $show_toolbar = 0;
  383. if (self::isToolBarDisplayedForUser()) {
  384. $show_toolbar = 1;
  385. }
  386. $this->assign('show_toolbar', $show_toolbar);
  387. //Only if course is available
  388. $show_course_shortcut = null;
  389. $show_course_navigation_menu = null;
  390. if (!empty($this->course_id) && $this->user_is_logged_in) {
  391. if (api_get_setting('show_toolshortcuts') != 'false') {
  392. //Course toolbar
  393. $show_course_shortcut = CourseHome::show_navigation_tool_shortcuts();
  394. }
  395. if (api_get_setting('show_navigation_menu') != 'false') {
  396. //Course toolbar
  397. $show_course_navigation_menu = CourseHome::show_navigation_menu();
  398. }
  399. }
  400. $this->assign('show_course_shortcut', $show_course_shortcut);
  401. $this->assign('show_course_navigation_menu', $show_course_navigation_menu);
  402. }
  403. /**
  404. * Returns the sub-folder and filename for the given tpl file.
  405. * If template not found in overrides/ or custom template folder, the
  406. * default template will be used.
  407. * @param string $name
  408. *
  409. * @return string
  410. */
  411. public function get_template($name)
  412. {
  413. // Check if the tpl file is present in the main/template/overrides/ dir
  414. // Overrides is a special directory meant for temporary template
  415. // customization. It must be taken into account before anything else
  416. $file = api_get_path(SYS_CODE_PATH).'template/overrides/'.$name;
  417. if (is_readable($file)) {
  418. return 'overrides/'.$name;
  419. }
  420. // If a template folder has been manually defined, search for the right
  421. // file, and if not found, go for the same file in the default template
  422. if ($this->templateFolder != 'default') {
  423. // Avoid missing template error, use the default file.
  424. $file = api_get_path(SYS_CODE_PATH).'template/'.$this->templateFolder.'/'.$name;
  425. if (!file_exists($file)) {
  426. return 'default/'.$name;
  427. }
  428. }
  429. return $this->templateFolder.'/'.$name;
  430. }
  431. /**
  432. * Prepare the _c array for template files. The _c array contains
  433. * information about the current course
  434. * @return void
  435. */
  436. private function set_course_parameters()
  437. {
  438. //Setting course id
  439. $course = api_get_course_info();
  440. if (empty($course)) {
  441. $this->assign('course_is_set', false);
  442. return;
  443. }
  444. $this->assign('course_is_set', true);
  445. $this->course_id = $course['id'];
  446. $_c = array(
  447. 'id' => $course['real_id'],
  448. 'code' => $course['code'],
  449. 'title' => $course['name'],
  450. 'visibility' => $course['visibility'],
  451. 'language' => $course['language'],
  452. 'directory' => $course['directory'],
  453. 'session_id' => api_get_session_id(),
  454. 'user_is_teacher' => api_is_course_admin(),
  455. 'student_view' => (!empty($_GET['isStudentView']) && $_GET['isStudentView'] == 'true'),
  456. );
  457. $this->assign('course_code', $course['code']);
  458. $this->assign('_c', $_c);
  459. }
  460. /**
  461. * Prepare the _u array for template files. The _u array contains
  462. * information about the current user, as returned by
  463. * api_get_user_info()
  464. * @return void
  465. */
  466. private function set_user_parameters()
  467. {
  468. $user_info = array();
  469. $user_info['logged'] = 0;
  470. $this->user_is_logged_in = false;
  471. if (api_user_is_login()) {
  472. $user_info = api_get_user_info(api_get_user_id(), true);
  473. $user_info['logged'] = 1;
  474. $user_info['is_admin'] = 0;
  475. if (api_is_platform_admin()) {
  476. $user_info['is_admin'] = 1;
  477. }
  478. $user_info['messages_count'] = MessageManager::getCountNewMessages();
  479. $this->user_is_logged_in = true;
  480. }
  481. // Setting the $_u array that could be use in any template
  482. $this->assign('_u', $user_info);
  483. }
  484. /**
  485. * Get CSS themes sub-directory
  486. * @param string $theme
  487. * @return string with a trailing slash, e.g. 'themes/chamilo_red/'
  488. */
  489. public static function getThemeDir($theme)
  490. {
  491. $themeDir = 'themes/'.$theme.'/';
  492. $virtualTheme = api_get_configuration_value('virtual_css_theme_folder');
  493. if (!empty($virtualTheme)) {
  494. $virtualThemeList = api_get_themes(true);
  495. $isVirtualTheme = in_array($theme, array_keys($virtualThemeList));
  496. if ($isVirtualTheme) {
  497. $themeDir = 'themes/'.$virtualTheme.'/'.$theme.'/';
  498. }
  499. }
  500. return $themeDir;
  501. }
  502. /**
  503. * Get an array of all the web paths available (e.g. 'web' => 'https://my.chamilo.site/')
  504. * @return array
  505. */
  506. private function getWebPaths()
  507. {
  508. return [
  509. 'web' => api_get_path(WEB_PATH),
  510. 'web_url' => api_get_web_url(),
  511. 'web_relative' => api_get_path(REL_PATH),
  512. 'web_course' => api_get_path(WEB_COURSE_PATH),
  513. 'web_main' => api_get_path(WEB_CODE_PATH),
  514. 'web_css' => api_get_path(WEB_CSS_PATH),
  515. 'web_css_theme' => api_get_path(WEB_CSS_PATH).$this->themeDir,
  516. 'web_ajax' => api_get_path(WEB_AJAX_PATH),
  517. 'web_img' => api_get_path(WEB_IMG_PATH),
  518. 'web_plugin' => api_get_path(WEB_PLUGIN_PATH),
  519. 'web_lib' => api_get_path(WEB_LIBRARY_PATH),
  520. 'web_upload' => api_get_path(WEB_UPLOAD_PATH),
  521. 'web_self' => api_get_self(),
  522. 'web_query_vars' => api_htmlentities($_SERVER['QUERY_STRING']),
  523. 'web_self_query_vars' => api_htmlentities($_SERVER['REQUEST_URI']),
  524. 'web_cid_query' => api_get_cidreq(),
  525. ];
  526. }
  527. /**
  528. * Set system parameters from api_get_configuration into _s array for use in TPLs
  529. * Also fills the _p array from getWebPaths()
  530. * @uses self::getWebPaths()
  531. */
  532. public function set_system_parameters()
  533. {
  534. $this->theme = api_get_visual_theme();
  535. if (!empty($this->preview_theme)) {
  536. $this->theme = $this->preview_theme;
  537. }
  538. $this->themeDir = self::getThemeDir($this->theme);
  539. // Setting app paths/URLs
  540. $this->assign('_p', $this->getWebPaths());
  541. // Here we can add system parameters that can be use in any template
  542. $_s = array(
  543. 'software_name' => api_get_configuration_value('software_name'),
  544. 'system_version' => api_get_configuration_value('system_version'),
  545. 'site_name' => api_get_setting('siteName'),
  546. 'institution' => api_get_setting('Institution'),
  547. 'date' => api_format_date('now', DATE_FORMAT_LONG),
  548. 'timezone' => api_get_timezone(),
  549. 'gamification_mode' => api_get_setting('gamification_mode')
  550. );
  551. $this->assign('_s', $_s);
  552. }
  553. /**
  554. * Set theme, include mainstream CSS files
  555. * @return void
  556. * @see setCssCustomFiles() for additional CSS sheets
  557. */
  558. public function setCssFiles()
  559. {
  560. global $disable_js_and_css_files;
  561. $css = array();
  562. // Default CSS Bootstrap
  563. $bowerCSSFiles = [
  564. 'fontawesome/css/font-awesome.min.css',
  565. 'jquery-ui/themes/smoothness/theme.css',
  566. 'jquery-ui/themes/smoothness/jquery-ui.min.css',
  567. 'mediaelement/build/mediaelementplayer.min.css',
  568. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.css',
  569. 'bootstrap/dist/css/bootstrap.min.css',
  570. 'jquery.scrollbar/jquery.scrollbar.css',
  571. 'bootstrap-daterangepicker/daterangepicker.css',
  572. 'bootstrap-select/dist/css/bootstrap-select.min.css',
  573. 'select2/dist/css/select2.min.css',
  574. 'flag-icon-css/css/flag-icon.min.css'
  575. ];
  576. foreach ($bowerCSSFiles as $file) {
  577. $css[] = api_get_path(WEB_PUBLIC_PATH).'assets/'.$file;
  578. }
  579. $css[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chosen/chosen.css';
  580. if (api_is_global_chat_enabled()) {
  581. $css[] = api_get_path(WEB_LIBRARY_PATH).'javascript/chat/css/chat.css';
  582. }
  583. $css_file_to_string = '';
  584. foreach ($css as $file) {
  585. $css_file_to_string .= api_get_css($file);
  586. }
  587. if (!$disable_js_and_css_files) {
  588. $this->assign('css_static_file_to_string', $css_file_to_string);
  589. }
  590. }
  591. /**
  592. *
  593. */
  594. public function setCSSEditor()
  595. {
  596. $cssEditor = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'editor.css');
  597. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'editor.css')) {
  598. $cssEditor = api_get_path(WEB_CSS_PATH).$this->themeDir.'editor.css';
  599. }
  600. $this->assign('cssEditor', $cssEditor);
  601. }
  602. /**
  603. * Prepare custom CSS to be added at the very end of the <head> section
  604. * @return void
  605. * @see setCssFiles() for the mainstream CSS files
  606. */
  607. public function setCssCustomFiles()
  608. {
  609. global $disable_js_and_css_files;
  610. // Base CSS
  611. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'base.css');
  612. if ($this->show_learnpath) {
  613. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).'scorm.css');
  614. if (is_file(api_get_path(SYS_CSS_PATH).$this->themeDir.'learnpath.css')) {
  615. $css[] = api_get_path(WEB_CSS_PATH).$this->themeDir.'learnpath.css';
  616. }
  617. }
  618. $css[] = api_get_cdn_path(api_get_path(WEB_CSS_PATH).$this->themeDir.'default.css');
  619. $css_file_to_string = null;
  620. foreach ($css as $file) {
  621. $css_file_to_string .= api_get_css($file);
  622. }
  623. // @todo move this somewhere else. Special fix when using tablets in order to see the text near icons
  624. if (SHOW_TEXT_NEAR_ICONS == true) {
  625. //hack in order to fix the actions buttons
  626. $css_file_to_string .= '<style>
  627. .td_actions a {
  628. float:left;
  629. width:100%;
  630. }
  631. .forum_message_left a {
  632. float:left;
  633. width:100%;
  634. }
  635. </style>';
  636. }
  637. $navigator_info = api_get_navigator();
  638. if ($navigator_info['name'] == 'Internet Explorer' && $navigator_info['version'] == '6') {
  639. $css_file_to_string .= 'img, div { behavior: url('.api_get_path(WEB_LIBRARY_PATH).'javascript/iepngfix/iepngfix.htc) } '."\n";
  640. }
  641. if (!$disable_js_and_css_files) {
  642. $this->assign('css_custom_file_to_string', $css_file_to_string);
  643. $style_print = '';
  644. if (is_readable(api_get_path(SYS_CSS_PATH).$this->theme.'/print.css')) {
  645. $style_print = api_get_css(
  646. api_get_cdn_path(api_get_path(WEB_CSS_PATH).$this->theme.'/print.css'),
  647. 'print'
  648. );
  649. }
  650. $this->assign('css_style_print', $style_print);
  651. }
  652. // Logo
  653. $logo = return_logo($this->theme);
  654. $this->assign('logo', $logo);
  655. $this->assign('show_media_element', 1);
  656. }
  657. /**
  658. * Declare and define the template variable that will be used to load
  659. * javascript libraries in the header.
  660. */
  661. public function set_js_files()
  662. {
  663. global $disable_js_and_css_files, $htmlHeadXtra;
  664. $isoCode = api_get_language_isocode();
  665. $selectLink = 'bootstrap-select/dist/js/i18n/defaults-'.$isoCode.'_'.strtoupper($isoCode).'.min.js';
  666. if ($isoCode == 'en') {
  667. $selectLink = 'bootstrap-select/dist/js/i18n/defaults-'.$isoCode.'_US.min.js';
  668. }
  669. // JS files
  670. $js_files = array(
  671. 'chosen/chosen.jquery.min.js'
  672. );
  673. $viewBySession = api_get_setting('my_courses_view_by_session') === 'true';
  674. if (api_is_global_chat_enabled() || $viewBySession) {
  675. // Do not include the global chat in LP
  676. if ($this->show_learnpath == false &&
  677. $this->show_footer == true &&
  678. $this->hide_global_chat == false
  679. ) {
  680. $js_files[] = 'chat/js/chat.js';
  681. }
  682. }
  683. if (api_get_setting('accessibility_font_resize') == 'true') {
  684. $js_files[] = 'fontresize.js';
  685. }
  686. $js_file_to_string = '';
  687. $bowerJsFiles = [
  688. 'modernizr/modernizr.js',
  689. 'jquery/dist/jquery.min.js',
  690. 'bootstrap/dist/js/bootstrap.min.js',
  691. 'jquery-ui/jquery-ui.min.js',
  692. 'moment/min/moment-with-locales.js',
  693. 'bootstrap-daterangepicker/daterangepicker.js',
  694. 'jquery-timeago/jquery.timeago.js',
  695. 'mediaelement/build/mediaelement-and-player.min.js',
  696. 'jqueryui-timepicker-addon/dist/jquery-ui-timepicker-addon.min.js',
  697. 'image-map-resizer/js/imageMapResizer.min.js',
  698. 'jquery.scrollbar/jquery.scrollbar.min.js',
  699. 'readmore-js/readmore.min.js',
  700. 'bootstrap-select/dist/js/bootstrap-select.min.js',
  701. $selectLink,
  702. 'select2/dist/js/select2.min.js',
  703. "select2/dist/js/i18n/$isoCode.js"
  704. ];
  705. if (CHAMILO_LOAD_WYSIWYG == true) {
  706. $bowerJsFiles[] = 'ckeditor/ckeditor.js';
  707. }
  708. if (api_get_setting('include_asciimathml_script') == 'true') {
  709. $bowerJsFiles[] = 'MathJax/MathJax.js?config=TeX-MML-AM_HTMLorMML';
  710. }
  711. if ($isoCode != 'en') {
  712. $bowerJsFiles[] = 'jqueryui-timepicker-addon/dist/i18n/jquery-ui-timepicker-'.$isoCode.'.js';
  713. $bowerJsFiles[] = 'jquery-ui/ui/minified/i18n/datepicker-'.$isoCode.'.min.js';
  714. }
  715. foreach ($bowerJsFiles as $file) {
  716. $js_file_to_string .= '<script type="text/javascript" src="'.api_get_path(WEB_PUBLIC_PATH).'assets/'.$file.'"></script>'."\n";
  717. }
  718. foreach ($js_files as $file) {
  719. $js_file_to_string .= api_get_js($file);
  720. }
  721. // Loading email_editor js
  722. if (!api_is_anonymous() && api_get_setting('allow_email_editor') == 'true') {
  723. $template = $this->get_template('mail_editor/email_link.js.tpl');
  724. $js_file_to_string .= $this->fetch($template);
  725. }
  726. if (!$disable_js_and_css_files) {
  727. $this->assign('js_file_to_string', $js_file_to_string);
  728. $extra_headers = '<script>var _p = '.json_encode($this->getWebPaths(), JSON_PRETTY_PRINT).'</script>';
  729. //Adding jquery ui by default
  730. $extra_headers .= api_get_jquery_ui_js();
  731. //$extra_headers = '';
  732. if (isset($htmlHeadXtra) && $htmlHeadXtra) {
  733. foreach ($htmlHeadXtra as & $this_html_head) {
  734. $extra_headers .= $this_html_head."\n";
  735. }
  736. }
  737. $this->assign('extra_headers', $extra_headers);
  738. }
  739. }
  740. /**
  741. * Special function to declare last-minute JS libraries which depend on
  742. * other things to be declared first. In particular, it might be useful
  743. * under IE9 with compatibility mode, which for some reason is getting
  744. * upset when a variable is used in a function (even if not used yet)
  745. * when this variable hasn't been defined yet.
  746. */
  747. public function set_js_files_post()
  748. {
  749. global $disable_js_and_css_files;
  750. $js_files = array();
  751. if (api_is_global_chat_enabled()) {
  752. //Do not include the global chat in LP
  753. if ($this->show_learnpath == false && $this->show_footer == true && $this->hide_global_chat == false) {
  754. $js_files[] = 'chat/js/chat.js';
  755. }
  756. }
  757. $js_file_to_string = null;
  758. foreach ($js_files as $js_file) {
  759. $js_file_to_string .= api_get_js($js_file);
  760. }
  761. if (!$disable_js_and_css_files) {
  762. $this->assign('js_file_to_string_post', $js_file_to_string);
  763. }
  764. }
  765. /**
  766. * Set header parameters
  767. * @param bool $sendHeaders send headers
  768. */
  769. private function set_header_parameters($sendHeaders)
  770. {
  771. global $httpHeadXtra, $interbreadcrumb, $language_file, $_configuration, $this_section;
  772. $_course = api_get_course_info();
  773. $nameTools = $this->title;
  774. $navigation = return_navigation_array();
  775. $this->menu_navigation = $navigation['menu_navigation'];
  776. $this->assign('system_charset', api_get_system_encoding());
  777. if (isset($httpHeadXtra) && $httpHeadXtra) {
  778. foreach ($httpHeadXtra as & $thisHttpHead) {
  779. header($thisHttpHead);
  780. }
  781. }
  782. $this->assign(
  783. 'online_button',
  784. Display::return_icon('statusonline.png', null, [], ICON_SIZE_ATOM)
  785. );
  786. $this->assign(
  787. 'offline_button',
  788. Display::return_icon('statusoffline.png', null, [], ICON_SIZE_ATOM)
  789. );
  790. // Get language iso-code for this page - ignore errors
  791. $this->assign('document_language', api_get_language_isocode());
  792. $course_title = isset($_course['name']) ? $_course['name'] : null;
  793. $title_list = array();
  794. $title_list[] = api_get_setting('Institution');
  795. $title_list[] = api_get_setting('siteName');
  796. if (!empty($course_title)) {
  797. $title_list[] = $course_title;
  798. }
  799. if ($nameTools != '') {
  800. $title_list[] = $nameTools;
  801. }
  802. $title_string = '';
  803. for ($i = 0; $i < count($title_list); $i++) {
  804. $title_string .= $title_list[$i];
  805. if (isset($title_list[$i + 1])) {
  806. $item = trim($title_list[$i + 1]);
  807. if (!empty($item)) {
  808. $title_string .= ' - ';
  809. }
  810. }
  811. }
  812. $this->assign('title_string', $title_string);
  813. // Setting the theme and CSS files
  814. $this->setCssFiles();
  815. $this->set_js_files();
  816. $this->setCssCustomFiles();
  817. $browser = api_browser_support('check_browser');
  818. if ($browser[0] == 'Internet Explorer' && $browser[1] >= '11') {
  819. $browser_head = '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />';
  820. $this->assign('browser_specific_head', $browser_head);
  821. }
  822. // Implementation of prefetch.
  823. // See http://cdn.chamilo.org/main/img/online.png for details
  824. $prefetch = '';
  825. if (!empty($_configuration['cdn_enable'])) {
  826. $prefetch .= '<meta http-equiv="x-dns-prefetch-control" content="on">';
  827. foreach ($_configuration['cdn'] as $host => $exts) {
  828. $prefetch .= '<link rel="dns-prefetch" href="'.$host.'">';
  829. }
  830. }
  831. $this->assign('prefetch', $prefetch);
  832. $this->assign('text_direction', api_get_text_direction());
  833. $this->assign('section_name', 'section-'.$this_section);
  834. // Default root chamilo favicon
  835. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_PATH).'favicon.ico" type="image/x-icon" />';
  836. //Added to verify if in the current Chamilo Theme exist a favicon
  837. $favicoThemeUrl = api_get_path(SYS_CSS_PATH).$this->themeDir.'images/';
  838. //If exist pick the current chamilo theme favicon
  839. if (is_file($favicoThemeUrl.'favicon.ico')) {
  840. $favico = '<link rel="shortcut icon" href="'.api_get_path(WEB_CSS_PATH).$this->themeDir.'images/favicon.ico" type="image/x-icon" />';
  841. }
  842. if (api_is_multiple_url_enabled()) {
  843. $access_url_id = api_get_current_access_url_id();
  844. if ($access_url_id != -1) {
  845. $url_info = api_get_access_url($access_url_id);
  846. $url = api_remove_trailing_slash(
  847. preg_replace('/https?:\/\//i', '', $url_info['url'])
  848. );
  849. $clean_url = api_replace_dangerous_char($url);
  850. $clean_url = str_replace('/', '-', $clean_url);
  851. $clean_url .= '/';
  852. $homep = api_get_path(REL_PATH).'home/'.$clean_url; //homep for Home Path
  853. $icon_real_homep = api_get_path(SYS_APP_PATH).'home/'.$clean_url;
  854. //we create the new dir for the new sites
  855. if (is_file($icon_real_homep.'favicon.ico')) {
  856. $favico = '<link rel="shortcut icon" href="'.$homep.'favicon.ico" type="image/x-icon" />';
  857. }
  858. }
  859. }
  860. $this->assign('favico', $favico);
  861. $this->setHelp();
  862. //@todo move this in the template
  863. $rightFloatMenu = '';
  864. $iconBug = Display::return_icon(
  865. 'bug.png',
  866. get_lang('ReportABug'),
  867. [],
  868. ICON_SIZE_LARGE
  869. );
  870. if (api_get_setting('show_link_bug_notification') == 'true' && $this->user_is_logged_in) {
  871. $rightFloatMenu = '<div class="report">
  872. <a href="https://github.com/chamilo/chamilo-lms/wiki/How-to-report-issues" target="_blank">
  873. '.$iconBug.'
  874. </a>
  875. </div>';
  876. }
  877. if (api_get_setting('show_link_ticket_notification') == 'true' && $this->user_is_logged_in) {
  878. // by default is project_id = 1
  879. $iconTicket = Display::return_icon(
  880. 'bug.png',
  881. get_lang('Ticket'),
  882. [],
  883. ICON_SIZE_LARGE
  884. );
  885. $courseInfo = api_get_course_info();
  886. $courseParams = '';
  887. if (!empty($courseInfo)) {
  888. $courseParams = api_get_cidreq();
  889. }
  890. $url = api_get_path(WEB_CODE_PATH).'ticket/tickets.php?project_id=1&'.$courseParams;
  891. $rightFloatMenu .= '<div class="report">
  892. <a href="'.$url.'" target="_blank">
  893. '.$iconTicket.'
  894. </a>
  895. </div>';
  896. }
  897. $this->assign('bug_notification', $rightFloatMenu);
  898. $resize = '';
  899. if (api_get_setting('accessibility_font_resize') == 'true') {
  900. $resize .= '<div class="resize_font">';
  901. $resize .= '<div class="btn-group">';
  902. $resize .= '<a title="'.get_lang('DecreaseFontSize').'" href="#" class="decrease_font btn btn-default"><em class="fa fa-font"></em></a>';
  903. $resize .= '<a title="'.get_lang('ResetFontSize').'" href="#" class="reset_font btn btn-default"><em class="fa fa-font"></em></a>';
  904. $resize .= '<a title="'.get_lang('IncreaseFontSize').'" href="#" class="increase_font btn btn-default"><em class="fa fa-font"></em></a>';
  905. $resize .= '</div>';
  906. $resize .= '</div>';
  907. }
  908. $this->assign('accessibility', $resize);
  909. // Preparing values for the menu
  910. // Logout link
  911. $hideLogout = api_get_setting('hide_logout_button');
  912. if ($hideLogout === 'true') {
  913. $this->assign('logout_link', null);
  914. } else {
  915. $this->assign('logout_link', api_get_path(WEB_PATH).'index.php?logout=logout&uid='.api_get_user_id());
  916. }
  917. // Profile link
  918. if (api_get_setting('allow_social_tool') == 'true') {
  919. $profile_url = api_get_path(WEB_CODE_PATH).'social/home.php';
  920. } else {
  921. $profile_url = api_get_path(WEB_CODE_PATH).'auth/profile.php';
  922. }
  923. $this->assign('profile_url', $profile_url);
  924. //Message link
  925. $message_link = null;
  926. $message_url = null;
  927. if (api_get_setting('allow_message_tool') == 'true') {
  928. $message_url = api_get_path(WEB_CODE_PATH).'messages/inbox.php';
  929. $message_link = '<a href="'.api_get_path(WEB_CODE_PATH).'messages/inbox.php">'.get_lang('Inbox').'</a>';
  930. }
  931. $this->assign('message_link', $message_link);
  932. $this->assign('message_url', $message_url);
  933. // Certificate Link
  934. $allow = api_get_configuration_value('hide_my_certificate_link');
  935. if ($allow === false) {
  936. $certificateUrl = api_get_path(WEB_CODE_PATH).'gradebook/my_certificates.php';
  937. $certificateLink = Display::url(
  938. get_lang('MyCertificates'),
  939. $certificateUrl
  940. );
  941. $this->assign('certificate_link', $certificateLink);
  942. $this->assign('certificate_url', $certificateUrl);
  943. }
  944. $institution = api_get_setting('Institution');
  945. $portal_name = empty($institution) ? api_get_setting('siteName') : $institution;
  946. $this->assign('portal_name', $portal_name);
  947. //Menu
  948. $menu = menuArray();
  949. $this->assign('menu', $menu);
  950. $breadcrumb = '';
  951. // Hide breadcrumb in LP
  952. if ($this->show_learnpath == false) {
  953. $breadcrumb = return_breadcrumb(
  954. $interbreadcrumb,
  955. $language_file,
  956. $nameTools
  957. );
  958. }
  959. $this->assign('breadcrumb', $breadcrumb);
  960. //Extra content
  961. $extra_header = null;
  962. if (!api_is_platform_admin()) {
  963. $extra_header = trim(api_get_setting('header_extra_content'));
  964. }
  965. $this->assign('header_extra_content', $extra_header);
  966. if ($sendHeaders) {
  967. header('Content-Type: text/html; charset='.api_get_system_encoding());
  968. header(
  969. 'X-Powered-By: '.$_configuration['software_name'].' '.substr($_configuration['system_version'], 0, 1)
  970. );
  971. self::addHTTPSecurityHeaders();
  972. }
  973. $socialMeta = '';
  974. $metaTitle = api_get_setting('meta_title');
  975. if (!empty($metaTitle)) {
  976. $socialMeta .= '<meta name="twitter:card" content="summary" />'."\n";
  977. $metaSite = api_get_setting('meta_twitter_site');
  978. if (!empty($metaSite)) {
  979. $socialMeta .= '<meta name="twitter:site" content="'.$metaSite.'" />'."\n";
  980. $metaCreator = api_get_setting('meta_twitter_creator');
  981. if (!empty($metaCreator)) {
  982. $socialMeta .= '<meta name="twitter:creator" content="'.$metaCreator.'" />'."\n";
  983. }
  984. }
  985. // The user badge page emits its own meta tags, so if this is
  986. // enabled, ignore the global ones
  987. $userId = isset($_GET['user']) ? intval($_GET['user']) : 0;
  988. $skillId = isset($_GET['skill']) ? intval($_GET['skill']) : 0;
  989. if (!$userId && !$skillId) {
  990. // no combination of user and skill ID has been defined,
  991. // so print the normal OpenGraph meta tags
  992. $socialMeta .= '<meta property="og:title" content="'.$metaTitle.'" />'."\n";
  993. $socialMeta .= '<meta property="og:url" content="'.api_get_path(WEB_PATH).'" />'."\n";
  994. $metaDescription = api_get_setting('meta_description');
  995. if (!empty($metaDescription)) {
  996. $socialMeta .= '<meta property="og:description" content="'.$metaDescription.'" />'."\n";
  997. }
  998. $metaImage = api_get_setting('meta_image_path');
  999. if (!empty($metaImage)) {
  1000. if (is_file(api_get_path(SYS_PATH).$metaImage)) {
  1001. $path = api_get_path(WEB_PATH).$metaImage;
  1002. $socialMeta .= '<meta property="og:image" content="'.$path.'" />'."\n";
  1003. }
  1004. }
  1005. }
  1006. }
  1007. $this->assign('social_meta', $socialMeta);
  1008. }
  1009. /**
  1010. * Set footer parameters
  1011. */
  1012. private function set_footer_parameters()
  1013. {
  1014. if (api_get_setting('show_administrator_data') === 'true') {
  1015. $firstName = api_get_setting('administratorName');
  1016. $lastName = api_get_setting('administratorSurname');
  1017. if (!empty($firstName) && !empty($lastName)) {
  1018. $name = api_get_person_name($firstName, $lastName);
  1019. } else {
  1020. $name = $lastName;
  1021. if (empty($lastName)) {
  1022. $name = $firstName;
  1023. }
  1024. }
  1025. $adminName = '';
  1026. // Administrator name
  1027. if (!empty($name)) {
  1028. $adminName = get_lang('Manager').' : '.
  1029. Display::encrypted_mailto_link(
  1030. api_get_setting('emailAdministrator'),
  1031. $name
  1032. );
  1033. }
  1034. $this->assign('administrator_name', $adminName);
  1035. }
  1036. // Loading footer extra content
  1037. if (!api_is_platform_admin()) {
  1038. $extra_footer = trim(api_get_setting('footer_extra_content'));
  1039. if (!empty($extra_footer)) {
  1040. $this->assign('footer_extra_content', $extra_footer);
  1041. }
  1042. }
  1043. // Tutor name
  1044. if (api_get_setting('show_tutor_data') == 'true') {
  1045. // Course manager
  1046. $courseId = api_get_course_int_id();
  1047. $id_session = api_get_session_id();
  1048. if (!empty($courseId)) {
  1049. $tutor_data = '';
  1050. if ($id_session != 0) {
  1051. $coachs_email = CourseManager::get_email_of_tutor_to_session(
  1052. $id_session,
  1053. $courseId
  1054. );
  1055. $email_link = array();
  1056. foreach ($coachs_email as $coach) {
  1057. $email_link[] = Display::encrypted_mailto_link($coach['email'], $coach['complete_name']);
  1058. }
  1059. if (count($coachs_email) > 1) {
  1060. $tutor_data .= get_lang('Coachs').' : ';
  1061. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  1062. } elseif (count($coachs_email) == 1) {
  1063. $tutor_data .= get_lang('Coach').' : ';
  1064. $tutor_data .= array_to_string($email_link, CourseManager::USER_SEPARATOR);
  1065. } elseif (count($coachs_email) == 0) {
  1066. $tutor_data .= '';
  1067. }
  1068. }
  1069. $this->assign('session_teachers', $tutor_data);
  1070. }
  1071. }
  1072. if (api_get_setting('show_teacher_data') == 'true') {
  1073. // course manager
  1074. $courseId = api_get_course_int_id();
  1075. if (!empty($courseId)) {
  1076. $teacher_data = '';
  1077. $mail = CourseManager::get_emails_of_tutors_to_course($courseId);
  1078. if (!empty($mail)) {
  1079. $teachers_parsed = array();
  1080. foreach ($mail as $value) {
  1081. foreach ($value as $email => $name) {
  1082. $teachers_parsed[] = Display::encrypted_mailto_link($email, $name);
  1083. }
  1084. }
  1085. $label = get_lang('Teacher');
  1086. if (count($mail) > 1) {
  1087. $label = get_lang('Teachers');
  1088. }
  1089. $teacher_data .= $label.' : '.array_to_string($teachers_parsed, CourseManager::USER_SEPARATOR);
  1090. }
  1091. $this->assign('teachers', $teacher_data);
  1092. }
  1093. }
  1094. }
  1095. /**
  1096. * Show header template.
  1097. */
  1098. public function show_header_template()
  1099. {
  1100. $tpl = $this->get_template('layout/show_header.tpl');
  1101. $this->display($tpl);
  1102. }
  1103. /**
  1104. * Show footer template.
  1105. */
  1106. public function show_footer_template()
  1107. {
  1108. $tpl = $this->get_template('layout/show_footer.tpl');
  1109. $this->display($tpl);
  1110. }
  1111. /**
  1112. * Show footer js template.
  1113. */
  1114. public function show_footer_js_template()
  1115. {
  1116. $tpl = $this->get_template('layout/footer.js.tpl');
  1117. $this->display($tpl);
  1118. }
  1119. /**
  1120. * Sets the plugin content in a template variable
  1121. * @param string $pluginRegion
  1122. * @return null
  1123. */
  1124. public function set_plugin_region($pluginRegion)
  1125. {
  1126. if (!empty($pluginRegion)) {
  1127. $regionContent = $this->plugin->load_region(
  1128. $pluginRegion,
  1129. $this,
  1130. $this->force_plugin_load
  1131. );
  1132. $pluginList = $this->plugin->get_installed_plugins();
  1133. foreach ($pluginList as $plugin_name) {
  1134. // The plugin_info variable is available inside the plugin index
  1135. $pluginInfo = $this->plugin->getPluginInfo($plugin_name);
  1136. if (isset($pluginInfo['is_course_plugin']) && $pluginInfo['is_course_plugin']) {
  1137. $courseInfo = api_get_course_info();
  1138. if (!empty($courseInfo)) {
  1139. if (isset($pluginInfo['obj']) && $pluginInfo['obj'] instanceof Plugin) {
  1140. /** @var Plugin $plugin */
  1141. $plugin = $pluginInfo['obj'];
  1142. $regionContent .= $plugin->renderRegion($pluginRegion);
  1143. }
  1144. }
  1145. } else {
  1146. continue;
  1147. }
  1148. }
  1149. if (!empty($regionContent)) {
  1150. $this->assign('plugin_'.$pluginRegion, $regionContent);
  1151. } else {
  1152. $this->assign('plugin_'.$pluginRegion, null);
  1153. }
  1154. }
  1155. return null;
  1156. }
  1157. /**
  1158. * @param string $template
  1159. * @return string
  1160. */
  1161. public function fetch($template = null)
  1162. {
  1163. $template = $this->twig->loadTemplate($template);
  1164. return $template->render($this->params);
  1165. }
  1166. /**
  1167. * @param string $variable
  1168. * @param mixed $value
  1169. */
  1170. public function assign($variable, $value = '')
  1171. {
  1172. $this->params[$variable] = $value;
  1173. }
  1174. /**
  1175. * Render the template
  1176. * @param string $template The template path
  1177. * @param boolean $clearFlashMessages Clear the $_SESSION variables for flash messages
  1178. */
  1179. public function display($template, $clearFlashMessages = true)
  1180. {
  1181. $this->assign('flash_messages', Display::getFlashToString());
  1182. if ($clearFlashMessages) {
  1183. Display::cleanFlashMessages();
  1184. }
  1185. echo $this->twig->render($template, $this->params);
  1186. }
  1187. /**
  1188. * Adds a body class for login pages
  1189. */
  1190. public function setLoginBodyClass()
  1191. {
  1192. $this->assign('login_class', 'section-login');
  1193. }
  1194. /**
  1195. * The theme that will be used if the database is not working.
  1196. * @return string
  1197. */
  1198. public static function getThemeFallback()
  1199. {
  1200. $theme = api_get_configuration_value('theme_fallback');
  1201. if (empty($theme)) {
  1202. $theme = 'chamilo';
  1203. }
  1204. return $theme;
  1205. }
  1206. /**
  1207. * @param bool|true $setLoginForm
  1208. */
  1209. public function setLoginForm($setLoginForm = true)
  1210. {
  1211. global $loginFailed;
  1212. $userId = api_get_user_id();
  1213. if (!($userId) || api_is_anonymous($userId)) {
  1214. // Only display if the user isn't logged in.
  1215. $this->assign(
  1216. 'login_language_form',
  1217. api_display_language_form(true, true)
  1218. );
  1219. if ($setLoginForm) {
  1220. $this->assign('login_form', $this->displayLoginForm());
  1221. if ($loginFailed) {
  1222. $this->assign('login_failed', $this::handleLoginFailed());
  1223. }
  1224. }
  1225. }
  1226. }
  1227. /**
  1228. * @return string
  1229. */
  1230. public function handleLoginFailed()
  1231. {
  1232. $message = get_lang('InvalidId');
  1233. if (!isset($_GET['error'])) {
  1234. if (api_is_self_registration_allowed()) {
  1235. $message = get_lang('InvalidForSelfRegistration');
  1236. }
  1237. } else {
  1238. switch ($_GET['error']) {
  1239. case '':
  1240. if (api_is_self_registration_allowed()) {
  1241. $message = get_lang('InvalidForSelfRegistration');
  1242. }
  1243. break;
  1244. case 'account_expired':
  1245. $message = get_lang('AccountExpired');
  1246. break;
  1247. case 'account_inactive':
  1248. $message = get_lang('AccountInactive');
  1249. if (api_get_setting('allow_registration') === 'confirmation') {
  1250. $message = get_lang('AccountNotConfirmed').PHP_EOL;
  1251. $message .= Display::url(
  1252. get_lang('ReSendConfirmationMail'),
  1253. api_get_path(WEB_PATH).'main/auth/resend_confirmation_mail.php'
  1254. );
  1255. }
  1256. break;
  1257. case 'user_password_incorrect':
  1258. $message = get_lang('InvalidId');
  1259. break;
  1260. case 'access_url_inactive':
  1261. $message = get_lang('AccountURLInactive');
  1262. break;
  1263. case 'wrong_captcha':
  1264. $message = get_lang('TheTextYouEnteredDoesNotMatchThePicture');
  1265. break;
  1266. case 'blocked_by_captcha':
  1267. $message = get_lang('AccountBlockedByCaptcha');
  1268. break;
  1269. case 'multiple_connection_not_allowed':
  1270. $message = get_lang('MultipleConnectionsAreNotAllow');
  1271. break;
  1272. case 'unrecognize_sso_origin':
  1273. //$message = get_lang('SSOError');
  1274. break;
  1275. }
  1276. }
  1277. return Display::return_message($message, 'error', false);
  1278. }
  1279. /**
  1280. * @return string
  1281. */
  1282. public function displayLoginForm()
  1283. {
  1284. $form = new FormValidator(
  1285. 'formLogin',
  1286. 'POST',
  1287. null,
  1288. null,
  1289. null,
  1290. FormValidator::LAYOUT_BOX_NO_LABEL
  1291. );
  1292. $params = [
  1293. 'id' => 'login',
  1294. 'autofocus' => 'autofocus',
  1295. 'icon' => 'user fa-fw',
  1296. 'placeholder' => get_lang('UserName'),
  1297. ];
  1298. $browserAutoCapitalize= false;
  1299. // Avoid showing the autocapitalize option if the browser doesn't
  1300. // support it: this attribute is against the HTML5 standard
  1301. if (api_browser_support('autocapitalize')) {
  1302. $browserAutoCapitalize = false;
  1303. $params['autocapitalize'] = 'none';
  1304. }
  1305. $form->addText(
  1306. 'login',
  1307. get_lang('UserName'),
  1308. true,
  1309. $params
  1310. );
  1311. $params = [
  1312. 'id' => 'password',
  1313. 'icon' => 'lock fa-fw',
  1314. 'placeholder' => get_lang('Pass'),
  1315. ];
  1316. if ($browserAutoCapitalize) {
  1317. $params['autocapitalize'] = 'none';
  1318. }
  1319. $form->addElement(
  1320. 'password',
  1321. 'password',
  1322. get_lang('Pass'),
  1323. $params
  1324. );
  1325. // Captcha
  1326. $captcha = api_get_setting('allow_captcha');
  1327. $allowCaptcha = $captcha === 'true';
  1328. if ($allowCaptcha) {
  1329. $useCaptcha = isset($_SESSION['loginFailed']) ? $_SESSION['loginFailed'] : null;
  1330. if ($useCaptcha) {
  1331. $ajax = api_get_path(WEB_AJAX_PATH).'form.ajax.php?a=get_captcha';
  1332. $options = array(
  1333. 'width' => 250,
  1334. 'height' => 90,
  1335. 'callback' => $ajax.'&var='.basename(__FILE__, '.php'),
  1336. 'sessionVar' => basename(__FILE__, '.php'),
  1337. 'imageOptions' => array(
  1338. 'font_size' => 20,
  1339. 'font_path' => api_get_path(SYS_FONTS_PATH).'opensans/',
  1340. 'font_file' => 'OpenSans-Regular.ttf',
  1341. //'output' => 'gif'
  1342. )
  1343. );
  1344. // Minimum options using all defaults (including defaults for Image_Text):
  1345. //$options = array('callback' => 'qfcaptcha_image.php');
  1346. $captcha_question = $form->addElement('CAPTCHA_Image', 'captcha_question', '', $options);
  1347. $form->addHtml(get_lang('ClickOnTheImageForANewOne'));
  1348. $form->addElement(
  1349. 'text',
  1350. 'captcha',
  1351. get_lang('EnterTheLettersYouSee')
  1352. );
  1353. $form->addRule(
  1354. 'captcha',
  1355. get_lang('EnterTheCharactersYouReadInTheImage'),
  1356. 'required',
  1357. null,
  1358. 'client'
  1359. );
  1360. $form->addRule(
  1361. 'captcha',
  1362. get_lang('TheTextYouEnteredDoesNotMatchThePicture'),
  1363. 'CAPTCHA',
  1364. $captcha_question
  1365. );
  1366. }
  1367. }
  1368. $form->addButton(
  1369. 'submitAuth',
  1370. get_lang('LoginEnter'),
  1371. null,
  1372. 'primary',
  1373. null,
  1374. 'btn-block'
  1375. );
  1376. $html = $form->returnForm();
  1377. if (api_get_setting('openid_authentication') == 'true') {
  1378. include_once api_get_path(SYS_CODE_PATH).'auth/openid/login.php';
  1379. $html .= '<div>'.openid_form().'</div>';
  1380. }
  1381. return $html;
  1382. }
  1383. /**
  1384. * Set administrator variables
  1385. */
  1386. private function setAdministratorParams()
  1387. {
  1388. $_admin = [
  1389. 'email' => api_get_setting('emailAdministrator'),
  1390. 'surname' => api_get_setting('administratorSurname'),
  1391. 'name' => api_get_setting('administratorName'),
  1392. 'telephone' => api_get_setting('administratorTelephone')
  1393. ];
  1394. $this->assign('_admin', $_admin);
  1395. }
  1396. /**
  1397. * Manage specific HTTP headers security
  1398. * @return void (prints headers directly)
  1399. */
  1400. private function addHTTPSecurityHeaders()
  1401. {
  1402. // Implementation of HTTP headers security, as suggested and checked
  1403. // by https://securityheaders.io/
  1404. // Enable these settings in configuration.php to use them on your site
  1405. // Strict-Transport-Security
  1406. $setting = api_get_configuration_value('security_strict_transport');
  1407. if (!empty($setting)) {
  1408. header('Strict-Transport-Security: '.$setting);
  1409. }
  1410. // Content-Security-Policy
  1411. $setting = api_get_configuration_value('security_content_policy');
  1412. if (!empty($setting)) {
  1413. header('Content-Security-Policy: '.$setting);
  1414. }
  1415. $setting = api_get_configuration_value('security_content_policy_report_only');
  1416. if (!empty($setting)) {
  1417. header('Content-Security-Policy-Report-Only: '.$setting);
  1418. }
  1419. // Public-Key-Pins
  1420. $setting = api_get_configuration_value('security_public_key_pins');
  1421. if (!empty($setting)) {
  1422. header('Public-Key-Pins: '.$setting);
  1423. }
  1424. $setting = api_get_configuration_value('security_public_key_pins_report_only');
  1425. if (!empty($setting)) {
  1426. header('Public-Key-Pins-Report-Only: '.$setting);
  1427. }
  1428. // X-Frame-Options
  1429. $setting = api_get_configuration_value('security_x_frame_options');
  1430. if (!empty($setting)) {
  1431. header('X-Frame-Options: '.$setting);
  1432. }
  1433. // X-XSS-Protection
  1434. $setting = api_get_configuration_value('security_xss_protection');
  1435. if (!empty($setting)) {
  1436. header('X-XSS-Protection: '.$setting);
  1437. }
  1438. // X-Content-Type-Options
  1439. $setting = api_get_configuration_value('security_x_content_type_options');
  1440. if (!empty($setting)) {
  1441. header('X-Content-Type-Options: '.$setting);
  1442. }
  1443. // Referrer-Policy
  1444. $setting = api_get_configuration_value('security_referrer_policy');
  1445. if (!empty($setting)) {
  1446. header('Referrer-Policy: '.$setting);
  1447. }
  1448. // end of HTTP headers security block
  1449. }
  1450. }