PageRenderTime 25ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/OLAAaction
PHP | 394 lines | 223 code | 42 blank | 129 comment | 13 complexity | 047854546b00e6a0dda5cc64a615a256 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: html.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage Document
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. jimport('joomla.application.module.helper');
  17. /**
  18. * DocumentHTML class, provides an easy interface to parse and display an html document
  19. *
  20. * @package Joomla.Framework
  21. * @subpackage Document
  22. * @since 1.5
  23. */
  24. class JDocumentHTML extends JDocument
  25. {
  26. /**
  27. * Array of Header <link> tags
  28. *
  29. * @var array
  30. * @access private
  31. */
  32. var $_links = array();
  33. /**
  34. * Array of custom tags
  35. *
  36. * @var string
  37. * @access private
  38. */
  39. var $_custom = array();
  40. /**
  41. * Class constructor
  42. *
  43. * @access protected
  44. * @param array $options Associative array of options
  45. */
  46. function __construct($options = array())
  47. {
  48. parent::__construct($options);
  49. //set document type
  50. $this->_type = 'html';
  51. //set mime type
  52. $this->_mime = 'text/html';
  53. //set default document metadata
  54. $this->setMetaData('Content-Type', $this->_mime . '; charset=' . $this->_charset , true );
  55. $this->setMetaData('robots', 'index, follow' );
  56. }
  57. /**
  58. * Get the html document head data
  59. *
  60. * @access public
  61. * @return array The document head data in array form
  62. */
  63. function getHeadData()
  64. {
  65. $data = array();
  66. $data['title'] = $this->title;
  67. $data['description']= $this->description;
  68. $data['link'] = $this->link;
  69. $data['metaTags'] = $this->_metaTags;
  70. $data['links'] = $this->_links;
  71. $data['styleSheets']= $this->_styleSheets;
  72. $data['style'] = $this->_style;
  73. $data['scripts'] = $this->_scripts;
  74. $data['script'] = $this->_script;
  75. $data['custom'] = $this->_custom;
  76. return $data;
  77. }
  78. /**
  79. * Set the html document head data
  80. *
  81. * @access public
  82. * @param array $data The document head data in array form
  83. */
  84. function setHeadData($data)
  85. {
  86. $this->title = (isset($data['title'])) ? $data['title'] : $this->title;
  87. $this->description = (isset($data['description'])) ? $data['description'] : $this->description;
  88. $this->link = (isset($data['link'])) ? $data['link'] : $this->link;
  89. $this->_metaTags = (isset($data['metaTags'])) ? $data['metaTags'] : $this->_metaTags;
  90. $this->_links = (isset($data['links'])) ? $data['links'] : $this->_links;
  91. $this->_styleSheets = (isset($data['styleSheets'])) ? $data['styleSheets'] : $this->_styleSheets;
  92. $this->_style = (isset($data['style'])) ? $data['style'] : $this->_style;
  93. $this->_scripts = (isset($data['scripts'])) ? $data['scripts'] : $this->_scripts;
  94. $this->_script = (isset($data['script'])) ? $data['script'] : $this->_script;
  95. $this->_custom = (isset($data['custom'])) ? $data['custom'] : $this->_custom;
  96. }
  97. /**
  98. * Adds <link> tags to the head of the document
  99. *
  100. * <p>$relType defaults to 'rel' as it is the most common relation type used.
  101. * ('rev' refers to reverse relation, 'rel' indicates normal, forward relation.)
  102. * Typical tag: <link href="index.php" rel="Start"></p>
  103. *
  104. * @access public
  105. * @param string $href The link that is being related.
  106. * @param string $relation Relation of link.
  107. * @param string $relType Relation type attribute. Either rel or rev (default: 'rel').
  108. * @param array $attributes Associative array of remaining attributes.
  109. * @return void
  110. */
  111. function addHeadLink($href, $relation, $relType = 'rel', $attribs = array())
  112. {
  113. $attribs = JArrayHelper::toString($attribs);
  114. $generatedTag = '<link href="'.$href.'" '.$relType.'="'.$relation.'" '.$attribs;
  115. $this->_links[] = $generatedTag;
  116. }
  117. /**
  118. * Adds a shortcut icon (favicon)
  119. *
  120. * <p>This adds a link to the icon shown in the favorites list or on
  121. * the left of the url in the address bar. Some browsers display
  122. * it on the tab, as well.</p>
  123. *
  124. * @param string $href The link that is being related.
  125. * @param string $type File type
  126. * @param string $relation Relation of link
  127. * @access public
  128. */
  129. function addFavicon($href, $type = 'image/x-icon', $relation = 'shortcut icon')
  130. {
  131. $href = str_replace( '\\', '/', $href );
  132. $this->_links[] = '<link href="'.$href.'" rel="'.$relation.'" type="'.$type.'"';
  133. }
  134. /**
  135. * Adds a custom html string to the head block
  136. *
  137. * @param string The html to add to the head
  138. * @access public
  139. * @return void
  140. */
  141. function addCustomTag( $html )
  142. {
  143. $this->_custom[] = trim( $html );
  144. }
  145. /**
  146. * Get the contents of a document include
  147. *
  148. * @access public
  149. * @param string $type The type of renderer
  150. * @param string $name The name of the element to render
  151. * @param array $attribs Associative array of remaining attributes.
  152. * @return The output of the renderer
  153. */
  154. function getBuffer($type = null, $name = null, $attribs = array())
  155. {
  156. $result = null;
  157. // If no type is specified, return the whole buffer
  158. if ($type === null) {
  159. return $this->_buffer;
  160. }
  161. if(isset($this->_buffer[$type][$name])) {
  162. $result = $this->_buffer[$type][$name];
  163. }
  164. // If the buffer has been explicitly turned off don't display or attempt to render
  165. if ($result === false) {
  166. return null;
  167. }
  168. if( $renderer =& $this->loadRenderer( $type )) {
  169. $result = $renderer->render($name, $attribs, $result);
  170. }
  171. return $result;
  172. }
  173. /**
  174. * Set the contents a document include
  175. *
  176. * @access public
  177. * @param string $type The type of renderer
  178. * @param string $name oke The name of the element to render
  179. * @param string $content The content to be set in the buffer
  180. */
  181. function setBuffer($contents, $type, $name = null)
  182. {
  183. $this->_buffer[$type][$name] = $contents;
  184. }
  185. /**
  186. * Outputs the template to the browser.
  187. *
  188. * @access public
  189. * @param boolean $cache If true, cache the output
  190. * @param array $params Associative array of attributes
  191. * @return The rendered data
  192. */
  193. function render( $caching = false, $params = array())
  194. {
  195. // check
  196. $directory = isset($params['directory']) ? $params['directory'] : 'templates';
  197. $template = JFilterInput::clean($params['template'], 'cmd');
  198. $file = JFilterInput::clean($params['file'], 'cmd');
  199. if ( !file_exists( $directory.DS.$template.DS.$file) ) {
  200. $template = 'system';
  201. }
  202. // Parse the template INI file if it exists for parameters and insert
  203. // them into the template.
  204. if (is_readable( $directory.DS.$template.DS.'params.ini' ) )
  205. {
  206. $content = file_get_contents($directory.DS.$template.DS.'params.ini');
  207. $params = new JParameter($content);
  208. }
  209. // Load the language file for the template
  210. $lang =& JFactory::getLanguage();
  211. $lang->load( 'tpl_'.$template );
  212. // Assign the variables
  213. $this->template = $template;
  214. $this->baseurl = JURI::base(true);
  215. $this->params = $params;
  216. // load
  217. $data = $this->_loadTemplate($directory.DS.$template, $file);
  218. // parse
  219. $data = $this->_parseTemplate($data);
  220. //output
  221. parent::render();
  222. return $data;
  223. }
  224. /**
  225. * Count the modules based on the given condition
  226. *
  227. * @access public
  228. * @param string $condition The condition to use
  229. * @return integer Number of modules found
  230. */
  231. function countModules($condition)
  232. {
  233. $result = '';
  234. $words = explode(' ', $condition);
  235. for($i = 0; $i < count($words); $i+=2)
  236. {
  237. // odd parts (modules)
  238. $name = strtolower($words[$i]);
  239. $words[$i] = ((isset($this->_buffer['modules'][$name])) && ($this->_buffer['modules'][$name] === false)) ? 0 : count(JModuleHelper::getModules($name));
  240. }
  241. $str = 'return '.implode(' ', $words).';';
  242. return eval($str);
  243. }
  244. /**
  245. * Count the number of child menu items
  246. *
  247. * @access public
  248. * @return integer Number of child menu items
  249. */
  250. function countMenuChildren() {
  251. static $children;
  252. if(!isset($children)) {
  253. $dbo =& JFactory::getDBO();
  254. $menu =& JSite::getMenu();
  255. $where = Array();
  256. $active = $menu->getActive();
  257. if($active) {
  258. $where[] = 'parent = ' . $active->id;
  259. $where[] = 'published = 1';
  260. $dbo->setQuery('SELECT COUNT(*) FROM #__menu WHERE '. implode(' AND ', $where));
  261. $children = $dbo->loadResult();
  262. } else {
  263. $children = 0;
  264. }
  265. }
  266. return $children;
  267. }
  268. /**
  269. * Load a template file
  270. *
  271. * @param string $template The name of the template
  272. * @param string $filename The actual filename
  273. * @return string The contents of the template
  274. */
  275. function _loadTemplate($directory, $filename)
  276. {
  277. global $mainframe, $option;
  278. if ($mainframe->getCfg('legacy'))
  279. {
  280. global $task, $_VERSION, $my, $cur_template, $database, $acl, $Itemid;
  281. //For backwards compatibility extract the config vars as globals
  282. $registry =& JFactory::getConfig();
  283. foreach (get_object_vars($registry->toObject()) as $k => $v) {
  284. $name = 'mosConfig_'.$k;
  285. $$name = $v;
  286. }
  287. }
  288. $contents = '';
  289. //Check to see if we have a valid template file
  290. if ( file_exists( $directory.DS.$filename ) )
  291. {
  292. //store the file path
  293. $this->_file = $directory.DS.$filename;
  294. //get the file content
  295. ob_start();
  296. require_once $directory.DS.$filename;
  297. $contents = ob_get_contents();
  298. ob_end_clean();
  299. }
  300. // Try to find a favicon by checking the template and root folder
  301. $path = $directory . DS;
  302. $dirs = array( $path, JPATH_BASE . DS );
  303. foreach ($dirs as $dir )
  304. {
  305. $icon = $dir . 'favicon.ico';
  306. if (file_exists( $icon ))
  307. {
  308. $path = str_replace( JPATH_BASE . DS, '', $dir );
  309. $path = str_replace( '\\', '/', $path );
  310. $this->addFavicon( JURI::base(true).'/'.$path . 'favicon.ico' );
  311. break;
  312. }
  313. }
  314. return $contents;
  315. }
  316. /**
  317. * Parse a document template
  318. *
  319. * @access public
  320. * @param string $data The data too parse
  321. * @return The parsed contents of the template
  322. */
  323. function _parseTemplate($data)
  324. {
  325. $replace = array();
  326. $matches = array();
  327. if(preg_match_all('#<jdoc:include\ type="([^"]+)" (.*)\/>#iU', $data, $matches))
  328. {
  329. $matches[0] = array_reverse($matches[0]);
  330. $matches[1] = array_reverse($matches[1]);
  331. $matches[2] = array_reverse($matches[2]);
  332. $count = count($matches[1]);
  333. for($i = 0; $i < $count; $i++)
  334. {
  335. $attribs = JUtility::parseAttributes( $matches[2][$i] );
  336. $type = $matches[1][$i];
  337. $name = isset($attribs['name']) ? $attribs['name'] : null;
  338. $replace[$i] = $this->getBuffer($type, $name, $attribs);
  339. }
  340. $data = str_replace($matches[0], $replace, $data);
  341. }
  342. return $data;
  343. }
  344. }