template.lib.php 61 KB

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