PageRenderTime 42ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/document/html/html.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 687 lines | 466 code | 49 blank | 172 comment | 45 complexity | 4e4fbde8b6614fa66870abda5afab319 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Document
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. jimport('joomla.utilities.utility');
  11. /**
  12. * DocumentHTML class, provides an easy interface to parse and display a HTML document
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Document
  16. * @since 11.1
  17. */
  18. class JDocumentHTML extends JDocument
  19. {
  20. /**
  21. * Array of Header <link> tags
  22. *
  23. * @var array
  24. * @since 11.1
  25. */
  26. public $_links = array();
  27. /**
  28. * Array of custom tags
  29. *
  30. * @var array
  31. * @since 11.1
  32. */
  33. public $_custom = array();
  34. /**
  35. * Name of the template
  36. *
  37. * @var string
  38. * @since 11.1
  39. */
  40. public $template = null;
  41. /**
  42. * Base url
  43. *
  44. * @var string
  45. * @since 11.1
  46. */
  47. public $baseurl = null;
  48. /**
  49. * Array of template parameters
  50. *
  51. * @var array
  52. * @since 11.1
  53. */
  54. public $params = null;
  55. /**
  56. * File name
  57. *
  58. * @var array
  59. * @since 11.1
  60. */
  61. public $_file = null;
  62. /**
  63. * String holding parsed template
  64. *
  65. * @var string
  66. * @since 11.1
  67. */
  68. protected $_template = '';
  69. /**
  70. * Array of parsed template JDoc tags
  71. *
  72. * @var array
  73. * @since 11.1
  74. */
  75. protected $_template_tags = array();
  76. /**
  77. * Integer with caching setting
  78. *
  79. * @var integer
  80. * @since 11.1
  81. */
  82. protected $_caching = null;
  83. /**
  84. * Set to true when the document should be output as HTML%
  85. *
  86. * @var boolean
  87. * @since 12.1
  88. */
  89. private $_html5 = null;
  90. /**
  91. * Class constructor
  92. *
  93. * @param array $options Associative array of options
  94. *
  95. * @since 11.1
  96. */
  97. public function __construct($options = array())
  98. {
  99. parent::__construct($options);
  100. // Set document type
  101. $this->_type = 'html';
  102. // Set default mime type and document metadata (meta data syncs with mime type by default)
  103. $this->setMimeEncoding('text/html');
  104. }
  105. /**
  106. * Get the HTML document head data
  107. *
  108. * @return array The document head data in array form
  109. *
  110. * @since 11.1
  111. */
  112. public function getHeadData()
  113. {
  114. $data = array();
  115. $data['title'] = $this->title;
  116. $data['description'] = $this->description;
  117. $data['link'] = $this->link;
  118. $data['metaTags'] = $this->_metaTags;
  119. $data['links'] = $this->_links;
  120. $data['styleSheets'] = $this->_styleSheets;
  121. $data['style'] = $this->_style;
  122. $data['scripts'] = $this->_scripts;
  123. $data['script'] = $this->_script;
  124. $data['custom'] = $this->_custom;
  125. return $data;
  126. }
  127. /**
  128. * Set the HTML document head data
  129. *
  130. * @param array $data The document head data in array form
  131. *
  132. * @return JDocumentHTML instance of $this to allow chaining
  133. *
  134. * @since 11.1
  135. */
  136. public function setHeadData($data)
  137. {
  138. if (empty($data) || !is_array($data))
  139. {
  140. return;
  141. }
  142. $this->title = (isset($data['title']) && !empty($data['title'])) ? $data['title'] : $this->title;
  143. $this->description = (isset($data['description']) && !empty($data['description'])) ? $data['description'] : $this->description;
  144. $this->link = (isset($data['link']) && !empty($data['link'])) ? $data['link'] : $this->link;
  145. $this->_metaTags = (isset($data['metaTags']) && !empty($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
  146. $this->_links = (isset($data['links']) && !empty($data['links'])) ? $data['links'] : $this->_links;
  147. $this->_styleSheets = (isset($data['styleSheets']) && !empty($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
  148. $this->_style = (isset($data['style']) && !empty($data['style'])) ? $data['style'] : $this->_style;
  149. $this->_scripts = (isset($data['scripts']) && !empty($data['scripts'])) ? $data['scripts'] : $this->_scripts;
  150. $this->_script = (isset($data['script']) && !empty($data['script'])) ? $data['script'] : $this->_script;
  151. $this->_custom = (isset($data['custom']) && !empty($data['custom'])) ? $data['custom'] : $this->_custom;
  152. return $this;
  153. }
  154. /**
  155. * Merge the HTML document head data
  156. *
  157. * @param array $data The document head data in array form
  158. *
  159. * @return JDocumentHTML instance of $this to allow chaining
  160. *
  161. * @since 11.1
  162. */
  163. public function mergeHeadData($data)
  164. {
  165. if (empty($data) || !is_array($data))
  166. {
  167. return;
  168. }
  169. $this->title = (isset($data['title']) && !empty($data['title']) && !stristr($this->title, $data['title']))
  170. ? $this->title . $data['title']
  171. : $this->title;
  172. $this->description = (isset($data['description']) && !empty($data['description']) && !stristr($this->description, $data['description']))
  173. ? $this->description . $data['description']
  174. : $this->description;
  175. $this->link = (isset($data['link'])) ? $data['link'] : $this->link;
  176. if (isset($data['metaTags']))
  177. {
  178. foreach ($data['metaTags'] as $type1 => $data1)
  179. {
  180. $booldog = $type1 == 'http-equiv' ? true : false;
  181. foreach ($data1 as $name2 => $data2)
  182. {
  183. $this->setMetaData($name2, $data2, $booldog);
  184. }
  185. }
  186. }
  187. $this->_links = (isset($data['links']) && !empty($data['links']) && is_array($data['links']))
  188. ? array_unique(array_merge($this->_links, $data['links']))
  189. : $this->_links;
  190. $this->_styleSheets = (isset($data['styleSheets']) && !empty($data['styleSheets']) && is_array($data['styleSheets']))
  191. ? array_merge($this->_styleSheets, $data['styleSheets'])
  192. : $this->_styleSheets;
  193. if (isset($data['style']))
  194. {
  195. foreach ($data['style'] as $type => $stdata)
  196. {
  197. if (!isset($this->_style[strtolower($type)]) || !stristr($this->_style[strtolower($type)], $stdata))
  198. {
  199. $this->addStyleDeclaration($stdata, $type);
  200. }
  201. }
  202. }
  203. $this->_scripts = (isset($data['scripts']) && !empty($data['scripts']) && is_array($data['scripts']))
  204. ? array_merge($this->_scripts, $data['scripts'])
  205. : $this->_scripts;
  206. if (isset($data['script']))
  207. {
  208. foreach ($data['script'] as $type => $sdata)
  209. {
  210. if (!isset($this->_script[strtolower($type)]) || !stristr($this->_script[strtolower($type)], $sdata))
  211. {
  212. $this->addScriptDeclaration($sdata, $type);
  213. }
  214. }
  215. }
  216. $this->_custom = (isset($data['custom']) && !empty($data['custom']) && is_array($data['custom']))
  217. ? array_unique(array_merge($this->_custom, $data['custom']))
  218. : $this->_custom;
  219. return $this;
  220. }
  221. /**
  222. * Adds <link> tags to the head of the document
  223. *
  224. * $relType defaults to 'rel' as it is the most common relation type used.
  225. * ('rev' refers to reverse relation, 'rel' indicates normal, forward relation.)
  226. * Typical tag: <link href="index.php" rel="Start">
  227. *
  228. * @param string $href The link that is being related.
  229. * @param string $relation Relation of link.
  230. * @param string $relType Relation type attribute. Either rel or rev (default: 'rel').
  231. * @param array $attribs Associative array of remaining attributes.
  232. *
  233. * @return JDocumentHTML instance of $this to allow chaining
  234. *
  235. * @since 11.1
  236. */
  237. public function addHeadLink($href, $relation, $relType = 'rel', $attribs = array())
  238. {
  239. $this->_links[$href]['relation'] = $relation;
  240. $this->_links[$href]['relType'] = $relType;
  241. $this->_links[$href]['attribs'] = $attribs;
  242. return $this;
  243. }
  244. /**
  245. * Adds a shortcut icon (favicon)
  246. *
  247. * This adds a link to the icon shown in the favorites list or on
  248. * the left of the url in the address bar. Some browsers display
  249. * it on the tab, as well.
  250. *
  251. * @param string $href The link that is being related.
  252. * @param string $type File type
  253. * @param string $relation Relation of link
  254. *
  255. * @return JDocumentHTML instance of $this to allow chaining
  256. *
  257. * @since 11.1
  258. */
  259. public function addFavicon($href, $type = 'image/vnd.microsoft.icon', $relation = 'shortcut icon')
  260. {
  261. $href = str_replace('\\', '/', $href);
  262. $this->addHeadLink($href, $relation, 'rel', array('type' => $type));
  263. return $this;
  264. }
  265. /**
  266. * Adds a custom HTML string to the head block
  267. *
  268. * @param string $html The HTML to add to the head
  269. *
  270. * @return JDocumentHTML instance of $this to allow chaining
  271. *
  272. * @since 11.1
  273. */
  274. public function addCustomTag($html)
  275. {
  276. $this->_custom[] = trim($html);
  277. return $this;
  278. }
  279. /**
  280. * Returns whether the document is set up to be output as HTML5
  281. *
  282. * @return Boolean true when HTML5 is used
  283. *
  284. * @since 12.1
  285. */
  286. public function isHtml5()
  287. {
  288. return $this->_html5;
  289. }
  290. /**
  291. * Sets whether the document should be output as HTML5
  292. *
  293. * @param bool $state True when HTML5 should be output
  294. *
  295. * @return void
  296. *
  297. * @since 12.1
  298. */
  299. public function setHtml5($state)
  300. {
  301. if (is_bool($state))
  302. {
  303. $this->_html5 = $state;
  304. }
  305. }
  306. /**
  307. * Get the contents of a document include
  308. *
  309. * @param string $type The type of renderer
  310. * @param string $name The name of the element to render
  311. * @param array $attribs Associative array of remaining attributes.
  312. *
  313. * @return The output of the renderer
  314. *
  315. * @since 11.1
  316. */
  317. public function getBuffer($type = null, $name = null, $attribs = array())
  318. {
  319. // If no type is specified, return the whole buffer
  320. if ($type === null)
  321. {
  322. return parent::$_buffer;
  323. }
  324. $title = (isset($attribs['title'])) ? $attribs['title'] : null;
  325. if (isset(parent::$_buffer[$type][$name][$title]))
  326. {
  327. return parent::$_buffer[$type][$name][$title];
  328. }
  329. $renderer = $this->loadRenderer($type);
  330. if ($this->_caching == true && $type == 'modules')
  331. {
  332. $cache = JFactory::getCache('com_modules', '');
  333. $hash = md5(serialize(array($name, $attribs, null, $renderer)));
  334. $cbuffer = $cache->get('cbuffer_' . $type);
  335. if (isset($cbuffer[$hash]))
  336. {
  337. return JCache::getWorkarounds($cbuffer[$hash], array('mergehead' => 1));
  338. }
  339. else
  340. {
  341. $options = array();
  342. $options['nopathway'] = 1;
  343. $options['nomodules'] = 1;
  344. $options['modulemode'] = 1;
  345. $this->setBuffer($renderer->render($name, $attribs, null), $type, $name);
  346. $data = parent::$_buffer[$type][$name][$title];
  347. $tmpdata = JCache::setWorkarounds($data, $options);
  348. $cbuffer[$hash] = $tmpdata;
  349. $cache->store($cbuffer, 'cbuffer_' . $type);
  350. }
  351. }
  352. else
  353. {
  354. $this->setBuffer($renderer->render($name, $attribs, null), $type, $name, $title);
  355. }
  356. return parent::$_buffer[$type][$name][$title];
  357. }
  358. /**
  359. * Set the contents a document includes
  360. *
  361. * @param string $content The content to be set in the buffer.
  362. * @param array $options Array of optional elements.
  363. *
  364. * @return JDocumentHTML instance of $this to allow chaining
  365. *
  366. * @since 11.1
  367. */
  368. public function setBuffer($content, $options = array())
  369. {
  370. // The following code is just for backward compatibility.
  371. if (func_num_args() > 1 && !is_array($options))
  372. {
  373. $args = func_get_args();
  374. $options = array();
  375. $options['type'] = $args[1];
  376. $options['name'] = (isset($args[2])) ? $args[2] : null;
  377. $options['title'] = (isset($args[3])) ? $args[3] : null;
  378. }
  379. parent::$_buffer[$options['type']][$options['name']][$options['title']] = $content;
  380. return $this;
  381. }
  382. /**
  383. * Parses the template and populates the buffer
  384. *
  385. * @param array $params Parameters for fetching the template
  386. *
  387. * @return JDocumentHTML instance of $this to allow chaining
  388. *
  389. * @since 11.1
  390. */
  391. public function parse($params = array())
  392. {
  393. return $this->_fetchTemplate($params)->_parseTemplate();
  394. }
  395. /**
  396. * Outputs the template to the browser.
  397. *
  398. * @param boolean $caching If true, cache the output
  399. * @param array $params Associative array of attributes
  400. *
  401. * @return The rendered data
  402. *
  403. * @since 11.1
  404. */
  405. public function render($caching = false, $params = array())
  406. {
  407. $this->_caching = $caching;
  408. if (!empty($this->_template))
  409. {
  410. $data = $this->_renderTemplate();
  411. }
  412. else
  413. {
  414. $this->parse($params);
  415. $data = $this->_renderTemplate();
  416. }
  417. parent::render();
  418. return $data;
  419. }
  420. /**
  421. * Count the modules based on the given condition
  422. *
  423. * @param string $condition The condition to use
  424. *
  425. * @return integer Number of modules found
  426. *
  427. * @since 11.1
  428. */
  429. public function countModules($condition)
  430. {
  431. $operators = '(\+|\-|\*|\/|==|\!=|\<\>|\<|\>|\<=|\>=|and|or|xor)';
  432. $words = preg_split('# ' . $operators . ' #', $condition, null, PREG_SPLIT_DELIM_CAPTURE);
  433. for ($i = 0, $n = count($words); $i < $n; $i += 2)
  434. {
  435. // Odd parts (modules)
  436. $name = strtolower($words[$i]);
  437. $words[$i] = ((isset(parent::$_buffer['modules'][$name])) && (parent::$_buffer['modules'][$name] === false))
  438. ? 0
  439. : count(JModuleHelper::getModules($name));
  440. }
  441. $str = 'return ' . implode(' ', $words) . ';';
  442. return eval($str);
  443. }
  444. /**
  445. * Count the number of child menu items
  446. *
  447. * @return integer Number of child menu items
  448. *
  449. * @since 11.1
  450. */
  451. public function countMenuChildren()
  452. {
  453. static $children;
  454. if (!isset($children))
  455. {
  456. $db = JFactory::getDbo();
  457. $app = JFactory::getApplication();
  458. $menu = $app->getMenu();
  459. $active = $menu->getActive();
  460. if ($active)
  461. {
  462. $query = $db->getQuery(true)
  463. ->select('COUNT(*)')
  464. ->from('#__menu')
  465. ->where('parent_id = ' . $active->id)
  466. ->where('published = 1');
  467. $children = $db->loadResult();
  468. }
  469. else
  470. {
  471. $children = 0;
  472. }
  473. }
  474. return $children;
  475. }
  476. /**
  477. * Load a template file
  478. *
  479. * @param string $directory The name of the template
  480. * @param string $filename The actual filename
  481. *
  482. * @return string The contents of the template
  483. *
  484. * @since 11.1
  485. */
  486. protected function _loadTemplate($directory, $filename)
  487. {
  488. // @todo remove code: $component = JApplicationHelper::getComponentName();
  489. $contents = '';
  490. // Check to see if we have a valid template file
  491. if (file_exists($directory . '/' . $filename))
  492. {
  493. // Store the file path
  494. $this->_file = $directory . '/' . $filename;
  495. // Get the file content
  496. ob_start();
  497. require $directory . '/' . $filename;
  498. $contents = ob_get_contents();
  499. ob_end_clean();
  500. }
  501. // Try to find a favicon by checking the template and root folder
  502. $path = $directory . '/';
  503. $dirs = array($path, JPATH_BASE . '/');
  504. foreach ($dirs as $dir)
  505. {
  506. $icon = $dir . 'favicon.ico';
  507. if (file_exists($icon))
  508. {
  509. $path = str_replace(JPATH_BASE . '/', '', $dir);
  510. $path = str_replace('\\', '/', $path);
  511. $this->addFavicon(JURI::base(true) . '/' . $path . 'favicon.ico');
  512. break;
  513. }
  514. }
  515. return $contents;
  516. }
  517. /**
  518. * Fetch the template, and initialise the params
  519. *
  520. * @param array $params Parameters to determine the template
  521. *
  522. * @return JDocumentHTML instance of $this to allow chaining
  523. *
  524. * @since 11.1
  525. */
  526. protected function _fetchTemplate($params = array())
  527. {
  528. // Check
  529. $directory = isset($params['directory']) ? $params['directory'] : 'templates';
  530. $filter = JFilterInput::getInstance();
  531. $template = $filter->clean($params['template'], 'cmd');
  532. $file = $filter->clean($params['file'], 'cmd');
  533. if (!file_exists($directory . '/' . $template . '/' . $file))
  534. {
  535. $template = 'system';
  536. }
  537. // Load the language file for the template
  538. $lang = JFactory::getLanguage();
  539. // 1.5 or core then 1.6
  540. $lang->load('tpl_' . $template, JPATH_BASE, null, false, false)
  541. || $lang->load('tpl_' . $template, $directory . '/' . $template, null, false, false)
  542. || $lang->load('tpl_' . $template, JPATH_BASE, $lang->getDefault(), false, false)
  543. || $lang->load('tpl_' . $template, $directory . '/' . $template, $lang->getDefault(), false, false);
  544. // Assign the variables
  545. $this->template = $template;
  546. $this->baseurl = JURI::base(true);
  547. $this->params = isset($params['params']) ? $params['params'] : new JRegistry;
  548. // Load
  549. $this->_template = $this->_loadTemplate($directory . '/' . $template, $file);
  550. return $this;
  551. }
  552. /**
  553. * Parse a document template
  554. *
  555. * @return JDocumentHTML instance of $this to allow chaining
  556. *
  557. * @since 11.1
  558. */
  559. protected function _parseTemplate()
  560. {
  561. $matches = array();
  562. if (preg_match_all('#<jdoc:include\ type="([^"]+)" (.*)\/>#iU', $this->_template, $matches))
  563. {
  564. $template_tags_first = array();
  565. $template_tags_last = array();
  566. // Step through the jdocs in reverse order.
  567. for ($i = count($matches[0]) - 1; $i >= 0; $i--)
  568. {
  569. $type = $matches[1][$i];
  570. $attribs = empty($matches[2][$i]) ? array() : JUtility::parseAttributes($matches[2][$i]);
  571. $name = isset($attribs['name']) ? $attribs['name'] : null;
  572. // Separate buffers to be executed first and last
  573. if ($type == 'module' || $type == 'modules')
  574. {
  575. $template_tags_first[$matches[0][$i]] = array('type' => $type, 'name' => $name, 'attribs' => $attribs);
  576. }
  577. else
  578. {
  579. $template_tags_last[$matches[0][$i]] = array('type' => $type, 'name' => $name, 'attribs' => $attribs);
  580. }
  581. }
  582. // Reverse the last array so the jdocs are in forward order.
  583. $template_tags_last = array_reverse($template_tags_last);
  584. $this->_template_tags = $template_tags_first + $template_tags_last;
  585. }
  586. return $this;
  587. }
  588. /**
  589. * Render pre-parsed template
  590. *
  591. * @return string rendered template
  592. *
  593. * @since 11.1
  594. */
  595. protected function _renderTemplate()
  596. {
  597. $replace = array();
  598. $with = array();
  599. foreach ($this->_template_tags as $jdoc => $args)
  600. {
  601. $replace[] = $jdoc;
  602. $with[] = $this->getBuffer($args['type'], $args['name'], $args['attribs']);
  603. }
  604. return str_replace($replace, $with, $this->_template);
  605. }
  606. }