PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ankursaxena_2/joomla-platform
PHP | 509 lines | 319 code | 62 blank | 128 comment | 53 complexity | 7df38e6d6d1d7d96b6308197e85b81b5 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Document
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 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.application.module.helper');
  11. /**
  12. * DocumentHTML class, provides an easy interface to parse and display an html document
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Document
  16. * @since 11.1
  17. */
  18. jimport('joomla.document.document');
  19. class JDocumentHTML extends JDocument
  20. {
  21. /**
  22. * Array of Header <link> tags
  23. *
  24. * @var array
  25. */
  26. public $_links = array();
  27. /**
  28. * Array of custom tags
  29. *
  30. * @var array
  31. */
  32. public $_custom = array();
  33. public $template = null;
  34. public $baseurl = null;
  35. public $params = null;
  36. public $_file = null;
  37. /**
  38. * String holding parsed template
  39. */
  40. protected $_template = '';
  41. /**
  42. * Array of parsed template JDoc tags
  43. */
  44. protected $_template_tags = array();
  45. /**
  46. * Integer with caching setting
  47. */
  48. protected $_caching = null;
  49. /**
  50. * Class constructor
  51. *
  52. * @param array $options Associative array of options
  53. */
  54. public function __construct($options = array())
  55. {
  56. parent::__construct($options);
  57. // Set document type
  58. $this->_type = 'html';
  59. // Set default mime type and document metadata (meta data syncs with mime type by default)
  60. $this->setMetaData('Content-Type', 'text/html', true);
  61. $this->setMetaData('robots', 'index, follow');
  62. }
  63. /**
  64. * Get the html document head data
  65. *
  66. * @return array The document head data in array form
  67. */
  68. public function getHeadData()
  69. {
  70. $data = array();
  71. $data['title'] = $this->title;
  72. $data['description']= $this->description;
  73. $data['link'] = $this->link;
  74. $data['metaTags'] = $this->_metaTags;
  75. $data['links'] = $this->_links;
  76. $data['styleSheets']= $this->_styleSheets;
  77. $data['style'] = $this->_style;
  78. $data['scripts'] = $this->_scripts;
  79. $data['script'] = $this->_script;
  80. $data['custom'] = $this->_custom;
  81. return $data;
  82. }
  83. /**
  84. * Set the html document head data
  85. *
  86. * @param array $data The document head data in array form
  87. */
  88. public function setHeadData($data)
  89. {
  90. if (empty($data) || !is_array($data)) {
  91. return;
  92. }
  93. $this->title = (isset($data['title']) && !empty($data['title'])) ? $data['title'] : $this->title;
  94. $this->description = (isset($data['description']) && !empty($data['description'])) ? $data['description'] : $this->description;
  95. $this->link = (isset($data['link']) && !empty($data['link'])) ? $data['link'] : $this->link;
  96. $this->_metaTags = (isset($data['metaTags']) && !empty($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
  97. $this->_links = (isset($data['links']) && !empty($data['links'])) ? $data['links'] : $this->_links;
  98. $this->_styleSheets = (isset($data['styleSheets']) && !empty($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
  99. $this->_style = (isset($data['style']) && !empty($data['style'])) ? $data['style'] : $this->_style;
  100. $this->_scripts = (isset($data['scripts']) && !empty($data['scripts'])) ? $data['scripts'] : $this->_scripts;
  101. $this->_script = (isset($data['script']) && !empty($data['script'])) ? $data['script'] : $this->_script;
  102. $this->_custom = (isset($data['custom']) && !empty($data['custom'])) ? $data['custom'] : $this->_custom;
  103. }
  104. /**
  105. * Merge the html document head data
  106. *
  107. * @param array $data The document head data in array form
  108. */
  109. public function mergeHeadData($data)
  110. {
  111. if (empty($data) || !is_array($data)) {
  112. return;
  113. }
  114. $this->title = (isset($data['title']) && !empty($data['title']) && !stristr($this->title, $data['title'])) ? $this->title.$data['title'] : $this->title;
  115. $this->description = (isset($data['description']) && !empty($data['description']) && !stristr($this->description, $data['description'])) ? $this->description. $data['description'] : $this->description;
  116. $this->link = (isset($data['link'])) ? $data['link'] : $this->link;
  117. if (isset($data['metaTags'])) {
  118. foreach($data['metaTags'] AS $type1=>$data1)
  119. {
  120. $booldog = $type1 == 'http-equiv' ? true : false;
  121. foreach($data1 AS $name2=>$data2)
  122. {
  123. $this->setMetaData($name2, $data2, $booldog);
  124. }
  125. }
  126. }
  127. $this->_links = (isset($data['links']) && !empty($data['links']) && is_array($data['links'])) ? array_unique(array_merge($this->_links, $data['links'])) : $this->_links;
  128. $this->_styleSheets = (isset($data['styleSheets']) && !empty($data['styleSheets']) && is_array($data['styleSheets'])) ? array_merge($this->_styleSheets, $data['styleSheets']) : $this->_styleSheets;
  129. if (isset($data['style'])) {
  130. foreach($data['style'] AS $type=>$stdata)
  131. {
  132. if (!isset($this->_style[strtolower($type)]) || !stristr($this->_style[strtolower($type)],$stdata)) {
  133. $this->addStyleDeclaration($stdata, $type);
  134. }
  135. }
  136. }
  137. $this->_scripts = (isset($data['scripts']) && !empty($data['scripts']) && is_array($data['scripts'])) ? array_merge($this->_scripts, $data['scripts']) : $this->_scripts;
  138. if (isset($data['script'])) {
  139. foreach($data['script'] AS $type=>$sdata)
  140. {
  141. if (!isset($this->_script[strtolower($type)]) || !stristr($this->_script[strtolower($type)],$sdata)) {
  142. $this->addScriptDeclaration($sdata, $type);
  143. }
  144. }
  145. }
  146. $this->_custom = (isset($data['custom']) && !empty($data['custom'])&& is_array($data['custom'])) ? array_unique(array_merge($this->_custom, $data['custom'])) : $this->_custom;
  147. }
  148. /**
  149. * Adds <link> tags to the head of the document
  150. *
  151. * $relType defaults to 'rel' as it is the most common relation type used.
  152. * ('rev' refers to reverse relation, 'rel' indicates normal, forward relation.)
  153. * Typical tag: <link href="index.php" rel="Start">
  154. *
  155. * @param string $href The link that is being related.
  156. * @param string $relation Relation of link.
  157. * @param string $relType Relation type attribute. Either rel or rev (default: 'rel').
  158. * @param array $attributes Associative array of remaining attributes.
  159. *
  160. * @return void
  161. */
  162. public function addHeadLink($href, $relation, $relType = 'rel', $attribs = array())
  163. {
  164. $attribs = JArrayHelper::toString($attribs);
  165. $generatedTag = '<link href="'.$href.'" '.$relType.'="'.$relation.'" '.$attribs;
  166. $this->_links[] = $generatedTag;
  167. }
  168. /**
  169. * Adds a shortcut icon (favicon)
  170. *
  171. * This adds a link to the icon shown in the favorites list or on
  172. * the left of the url in the address bar. Some browsers display
  173. * it on the tab, as well.
  174. *
  175. * @param string $href The link that is being related.
  176. * @param string $type File type
  177. * @param string $relation Relation of link
  178. */
  179. public function addFavicon($href, $type = 'image/vnd.microsoft.icon', $relation = 'shortcut icon')
  180. {
  181. $href = str_replace('\\', '/', $href);
  182. $this->_links[] = '<link href="'.$href.'" rel="'.$relation.'" type="'.$type.'"';
  183. }
  184. /**
  185. * Adds a custom html string to the head block
  186. *
  187. * @param string The html to add to the head
  188. * @return void
  189. */
  190. public function addCustomTag($html)
  191. {
  192. $this->_custom[] = trim($html);
  193. }
  194. /**
  195. * Get the contents of a document include
  196. *
  197. * @param string $type The type of renderer
  198. * @param string $name The name of the element to render
  199. * @param array $attribs Associative array of remaining attributes.
  200. *
  201. * @return The output of the renderer
  202. */
  203. public function getBuffer($type = null, $name = null, $attribs = array())
  204. {
  205. // If no type is specified, return the whole buffer
  206. if ($type === null) {
  207. return parent::$_buffer;
  208. }
  209. $result = null;
  210. if (isset(parent::$_buffer[$type][$name])) {
  211. return parent::$_buffer[$type][$name];
  212. }
  213. // If the buffer has been explicitly turned off don't display or attempt to render
  214. if ($result === false) {
  215. return null;
  216. }
  217. $renderer = $this->loadRenderer($type);
  218. if ($this->_caching == true && $type == 'modules') {
  219. $cache = JFactory::getCache('com_modules','');
  220. $hash = md5(serialize(array($name, $attribs, $result, $renderer)));
  221. $cbuffer = $cache->get('cbuffer_'.$type);
  222. if (isset($cbuffer[$hash])) {
  223. return JCache::getWorkarounds($cbuffer[$hash], array('mergehead' => 1));
  224. } else {
  225. $options = array();
  226. $options['nopathway'] = 1;
  227. $options['nomodules'] = 1;
  228. $options['modulemode'] = 1;
  229. $this->setBuffer($renderer->render($name, $attribs, $result), $type, $name);
  230. $data = parent::$_buffer[$type][$name];
  231. $tmpdata = JCache::setWorkarounds($data, $options);
  232. $cbuffer[$hash] = $tmpdata;
  233. $cache->store($cbuffer, 'cbuffer_'.$type);
  234. }
  235. } else {
  236. $this->setBuffer($renderer->render($name, $attribs, $result), $type, $name);
  237. }
  238. return parent::$_buffer[$type][$name];
  239. }
  240. /**
  241. * Set the contents a document includes
  242. *
  243. * @param string $content The content to be set in the buffer.
  244. * @param array $options Array of optional elements.
  245. */
  246. public function setBuffer($content, $options = array())
  247. {
  248. // The following code is just for backward compatibility.
  249. if (func_num_args() > 1 && !is_array($options)) {
  250. $args = func_get_args(); $options = array();
  251. $options['type'] = $args[1];
  252. $options['name'] = (isset($args[2])) ? $args[2] : null;
  253. }
  254. parent::$_buffer[$options['type']][$options['name']] = $content;
  255. }
  256. /**
  257. * Parses the template and populates the buffer
  258. *
  259. * @param array parameters for fetching the template
  260. */
  261. public function parse($params = array()) {
  262. $this->_fetchTemplate($params);
  263. $this->_parseTemplate();
  264. }
  265. /**
  266. * Outputs the template to the browser.
  267. *
  268. * @param boolean $cache If true, cache the output
  269. * @param array $params Associative array of attributes
  270. * @return The rendered data
  271. */
  272. public function render($caching = false, $params = array())
  273. {
  274. $this->_caching = $caching;
  275. if (!empty($this->_template)) {
  276. $data = $this->_renderTemplate();
  277. } else {
  278. $this->parse($params);
  279. $data = $this->_renderTemplate();
  280. }
  281. parent::render();
  282. return $data;
  283. }
  284. /**
  285. * Count the modules based on the given condition
  286. *
  287. * @param string $condition The condition to use
  288. * @return integer Number of modules found
  289. */
  290. public function countModules($condition)
  291. {
  292. $result = '';
  293. $operators = '(\+|\-|\*|\/|==|\!=|\<\>|\<|\>|\<=|\>=|and|or|xor)';
  294. $words = preg_split('# '.$operators.' #', $condition, null, PREG_SPLIT_DELIM_CAPTURE);
  295. for ($i = 0, $n = count($words); $i < $n; $i+=2)
  296. {
  297. // odd parts (modules)
  298. $name = strtolower($words[$i]);
  299. $words[$i] = ((isset(parent::$_buffer['modules'][$name])) && (parent::$_buffer['modules'][$name] === false)) ? 0 : count(JModuleHelper::getModules($name));
  300. }
  301. $str = 'return '.implode(' ', $words).';';
  302. return eval($str);
  303. }
  304. /**
  305. * Count the number of child menu items
  306. *
  307. * @return integer Number of child menu items
  308. */
  309. public function countMenuChildren()
  310. {
  311. static $children;
  312. if (!isset($children)) {
  313. $dbo = JFactory::getDbo();
  314. $app = JFactory::getApplication();
  315. $menu = $app->getMenu();
  316. $where = Array();
  317. $active = $menu->getActive();
  318. if ($active) {
  319. $where[] = 'parent = ' . $active->id;
  320. $where[] = 'published = 1';
  321. $dbo->setQuery('SELECT COUNT(*) FROM #__menu WHERE '. implode(' AND ', $where));
  322. $children = $dbo->loadResult();
  323. } else {
  324. $children = 0;
  325. }
  326. }
  327. return $children;
  328. }
  329. /**
  330. * Load a template file
  331. *
  332. * @param string $template The name of the template
  333. * @param string $filename The actual filename
  334. * @return string The contents of the template
  335. */
  336. private function _loadTemplate($directory, $filename)
  337. {
  338. // $component = JApplicationHelper::getComponentName();
  339. $contents = '';
  340. // Check to see if we have a valid template file
  341. if (file_exists($directory.DS.$filename))
  342. {
  343. // Store the file path
  344. $this->_file = $directory.DS.$filename;
  345. //get the file content
  346. ob_start();
  347. require $directory.DS.$filename;
  348. $contents = ob_get_contents();
  349. ob_end_clean();
  350. }
  351. // Try to find a favicon by checking the template and root folder
  352. $path = $directory . DS;
  353. $dirs = array($path, JPATH_BASE.DS);
  354. foreach ($dirs as $dir)
  355. {
  356. $icon = $dir.'favicon.ico';
  357. if (file_exists($icon))
  358. {
  359. $path = str_replace(JPATH_BASE . DS, '', $dir);
  360. $path = str_replace('\\', '/', $path);
  361. $this->addFavicon(JURI::base(true).'/'.$path.'favicon.ico');
  362. break;
  363. }
  364. }
  365. return $contents;
  366. }
  367. /**
  368. * Fetch the template, and initialise the params
  369. *
  370. * @param array parameters to determine the template
  371. */
  372. protected function _fetchTemplate($params = array())
  373. {
  374. // Check
  375. $directory = isset($params['directory']) ? $params['directory'] : 'templates';
  376. $filter = JFilterInput::getInstance();
  377. $template = $filter->clean($params['template'], 'cmd');
  378. $file = $filter->clean($params['file'], 'cmd');
  379. if (!file_exists($directory.DS.$template.DS.$file)) {
  380. $template = 'system';
  381. }
  382. // Load the language file for the template
  383. $lang = JFactory::getLanguage();
  384. // 1.5 or core then 1.6
  385. $lang->load('tpl_'.$template, JPATH_BASE, null, false, false)
  386. || $lang->load('tpl_'.$template, $directory.DS.$template, null, false, false)
  387. || $lang->load('tpl_'.$template, JPATH_BASE, $lang->getDefault(), false, false)
  388. || $lang->load('tpl_'.$template, $directory.DS.$template, $lang->getDefault(), false, false);
  389. // Assign the variables
  390. $this->template = $template;
  391. $this->baseurl = JURI::base(true);
  392. $this->params = isset($params['params']) ? $params['params'] : new JRegistry;
  393. // Load
  394. $this->_template = $this->_loadTemplate($directory.DS.$template, $file);
  395. }
  396. /**
  397. * Parse a document template
  398. *
  399. * @return The parsed contents of the template
  400. */
  401. private function _parseTemplate()
  402. {
  403. $replace = array();
  404. $matches = array();
  405. if (preg_match_all('#<jdoc:include\ type="([^"]+)" (.*)\/>#iU', $this->_template, $matches))
  406. {
  407. $matches[0] = array_reverse($matches[0]);
  408. $matches[1] = array_reverse($matches[1]);
  409. $matches[2] = array_reverse($matches[2]);
  410. $count = count($matches[1]);
  411. for ($i = 0; $i < $count; $i++)
  412. {
  413. $attribs = JUtility::parseAttributes($matches[2][$i]);
  414. $type = $matches[1][$i];
  415. $name = isset($attribs['name']) ? $attribs['name'] : null;
  416. $this->_template_tags[$matches[0][$i]] = array('type'=>$type, 'name' => $name, 'attribs' => $attribs);
  417. }
  418. }
  419. }
  420. /**
  421. * Render pre-parsed template
  422. *
  423. * @return string rendered template
  424. */
  425. private function _renderTemplate() {
  426. $replace = array();
  427. $with = array();
  428. foreach($this->_template_tags AS $jdoc => $args) {
  429. $replace[] = $jdoc;
  430. $with[] = $this->getBuffer($args['type'], $args['name'], $args['attribs']);
  431. }
  432. return str_replace($replace, $with, $this->_template);
  433. }
  434. }