template.lib.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  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. }