PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/document/document.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 964 lines | 359 code | 104 blank | 501 comment | 30 complexity | c0148e7beee461b9317010293eb49fc9 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.environment.response');
  11. /**
  12. * Document class, provides an easy interface to parse and display a document
  13. *
  14. * @package Joomla.Platform
  15. * @subpackage Document
  16. * @since 11.1
  17. */
  18. class JDocument
  19. {
  20. /**
  21. * Document title
  22. *
  23. * @var string
  24. * @since 11.1
  25. */
  26. public $title = '';
  27. /**
  28. * Document description
  29. *
  30. * @var string
  31. * @since 11.1
  32. */
  33. public $description = '';
  34. /**
  35. * Document full URL
  36. *
  37. * @var string
  38. * @since 11.1
  39. */
  40. public $link = '';
  41. /**
  42. * Document base URL
  43. *
  44. * @var string
  45. * @since 11.1
  46. */
  47. public $base = '';
  48. /**
  49. * Contains the document language setting
  50. *
  51. * @var string
  52. * @since 11.1
  53. */
  54. public $language = 'en-gb';
  55. /**
  56. * Contains the document direction setting
  57. *
  58. * @var string
  59. * @since 11.1
  60. */
  61. public $direction = 'ltr';
  62. /**
  63. * Document generator
  64. *
  65. * @var string
  66. */
  67. public $_generator = 'Joomla! - Open Source Content Management';
  68. /**
  69. * Document modified date
  70. *
  71. * @var string
  72. * @since 11.1
  73. */
  74. public $_mdate = '';
  75. /**
  76. * Tab string
  77. *
  78. * @var string
  79. * @since 11.1
  80. */
  81. public $_tab = "\11";
  82. /**
  83. * Contains the line end string
  84. *
  85. * @var string
  86. * @since 11.1
  87. */
  88. public $_lineEnd = "\12";
  89. /**
  90. * Contains the character encoding string
  91. *
  92. * @var string
  93. * @since 11.1
  94. */
  95. public $_charset = 'utf-8';
  96. /**
  97. * Document mime type
  98. *
  99. * @var string
  100. * @since 11.1
  101. */
  102. public $_mime = '';
  103. /**
  104. * Document namespace
  105. *
  106. * @var string
  107. * @since 11.1
  108. */
  109. public $_namespace = '';
  110. /**
  111. * Document profile
  112. *
  113. * @var string
  114. * @since 11.1
  115. */
  116. public $_profile = '';
  117. /**
  118. * Array of linked scripts
  119. *
  120. * @var array
  121. * @since 11.1
  122. */
  123. public $_scripts = array();
  124. /**
  125. * Array of scripts placed in the header
  126. *
  127. * @var array
  128. * @since 11.1
  129. */
  130. public $_script = array();
  131. /**
  132. * Array of linked style sheets
  133. *
  134. * @var array
  135. * @since 11.1
  136. */
  137. public $_styleSheets = array();
  138. /**
  139. * Array of included style declarations
  140. *
  141. * @var array
  142. * @since 11.1
  143. */
  144. public $_style = array();
  145. /**
  146. * Array of meta tags
  147. *
  148. * @var array
  149. * @since 11.1
  150. */
  151. public $_metaTags = array();
  152. /**
  153. * The rendering engine
  154. *
  155. * @var object
  156. * @since 11.1
  157. */
  158. public $_engine = null;
  159. /**
  160. * The document type
  161. *
  162. * @var string
  163. * @since 11.1
  164. */
  165. public $_type = null;
  166. /**
  167. * Array of buffered output
  168. *
  169. * @var mixed (depends on the renderer)
  170. * @since 11.1
  171. */
  172. public static $_buffer = null;
  173. /**
  174. * @var array JDocument instances container.
  175. * @since 11.3
  176. */
  177. protected static $instances = array();
  178. /**
  179. * Class constructor.
  180. *
  181. * @param array $options Associative array of options
  182. *
  183. * @since 11.1
  184. */
  185. public function __construct($options = array())
  186. {
  187. if (array_key_exists('lineend', $options))
  188. {
  189. $this->setLineEnd($options['lineend']);
  190. }
  191. if (array_key_exists('charset', $options))
  192. {
  193. $this->setCharset($options['charset']);
  194. }
  195. if (array_key_exists('language', $options))
  196. {
  197. $this->setLanguage($options['language']);
  198. }
  199. if (array_key_exists('direction', $options))
  200. {
  201. $this->setDirection($options['direction']);
  202. }
  203. if (array_key_exists('tab', $options))
  204. {
  205. $this->setTab($options['tab']);
  206. }
  207. if (array_key_exists('link', $options))
  208. {
  209. $this->setLink($options['link']);
  210. }
  211. if (array_key_exists('base', $options))
  212. {
  213. $this->setBase($options['base']);
  214. }
  215. }
  216. /**
  217. * Returns the global JDocument object, only creating it
  218. * if it doesn't already exist.
  219. *
  220. * @param string $type The document type to instantiate
  221. * @param array $attributes Array of attributes
  222. *
  223. * @return object The document object.
  224. *
  225. * @since 11.1
  226. * @throws RuntimeException
  227. */
  228. public static function getInstance($type = 'html', $attributes = array())
  229. {
  230. $signature = serialize(array($type, $attributes));
  231. if (empty(self::$instances[$signature]))
  232. {
  233. $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
  234. $path = __DIR__ . '/' . $type . '/' . $type . '.php';
  235. $ntype = null;
  236. // Check if the document type exists
  237. if (!file_exists($path))
  238. {
  239. // Default to the raw format
  240. $ntype = $type;
  241. $type = 'raw';
  242. }
  243. // Determine the path and class
  244. $class = 'JDocument' . $type;
  245. if (!class_exists($class))
  246. {
  247. $path = __DIR__ . '/' . $type . '/' . $type . '.php';
  248. if (file_exists($path))
  249. {
  250. require_once $path;
  251. }
  252. else
  253. {
  254. throw new RuntimeException('Invalid JDocument Class', 500);
  255. }
  256. }
  257. $instance = new $class($attributes);
  258. self::$instances[$signature] = $instance;
  259. if (!is_null($ntype))
  260. {
  261. // Set the type to the Document type originally requested
  262. $instance->setType($ntype);
  263. }
  264. }
  265. return self::$instances[$signature];
  266. }
  267. /**
  268. * Set the document type
  269. *
  270. * @param string $type Type document is to set to
  271. *
  272. * @return JDocument instance of $this to allow chaining
  273. *
  274. * @since 11.1
  275. */
  276. public function setType($type)
  277. {
  278. $this->_type = $type;
  279. return $this;
  280. }
  281. /**
  282. * Returns the document type
  283. *
  284. * @return string
  285. *
  286. * @since 11.1
  287. */
  288. public function getType()
  289. {
  290. return $this->_type;
  291. }
  292. /**
  293. * Get the contents of the document buffer
  294. *
  295. * @return The contents of the document buffer
  296. *
  297. * @since 11.1
  298. */
  299. public function getBuffer()
  300. {
  301. return self::$_buffer;
  302. }
  303. /**
  304. * Set the contents of the document buffer
  305. *
  306. * @param string $content The content to be set in the buffer.
  307. * @param array $options Array of optional elements.
  308. *
  309. * @return JDocument instance of $this to allow chaining
  310. *
  311. * @since 11.1
  312. */
  313. public function setBuffer($content, $options = array())
  314. {
  315. self::$_buffer = $content;
  316. return $this;
  317. }
  318. /**
  319. * Gets a meta tag.
  320. *
  321. * @param string $name Value of name or http-equiv tag
  322. * @param boolean $httpEquiv META type "http-equiv" defaults to null
  323. *
  324. * @return string
  325. *
  326. * @since 11.1
  327. */
  328. public function getMetaData($name, $httpEquiv = false)
  329. {
  330. $result = '';
  331. $name = strtolower($name);
  332. if ($name == 'generator')
  333. {
  334. $result = $this->getGenerator();
  335. }
  336. elseif ($name == 'description')
  337. {
  338. $result = $this->getDescription();
  339. }
  340. else
  341. {
  342. if ($httpEquiv == true)
  343. {
  344. $result = @$this->_metaTags['http-equiv'][$name];
  345. }
  346. else
  347. {
  348. $result = @$this->_metaTags['standard'][$name];
  349. }
  350. }
  351. return $result;
  352. }
  353. /**
  354. * Sets or alters a meta tag.
  355. *
  356. * @param string $name Value of name or http-equiv tag
  357. * @param string $content Value of the content tag
  358. * @param boolean $http_equiv META type "http-equiv" defaults to null
  359. *
  360. * @return JDocument instance of $this to allow chaining
  361. *
  362. * @since 11.1
  363. */
  364. public function setMetaData($name, $content, $http_equiv = false)
  365. {
  366. $name = strtolower($name);
  367. if ($name == 'generator')
  368. {
  369. $this->setGenerator($content);
  370. }
  371. elseif ($name == 'description')
  372. {
  373. $this->setDescription($content);
  374. }
  375. else
  376. {
  377. if ($http_equiv == true)
  378. {
  379. $this->_metaTags['http-equiv'][$name] = $content;
  380. }
  381. else
  382. {
  383. $this->_metaTags['standard'][$name] = $content;
  384. }
  385. }
  386. return $this;
  387. }
  388. /**
  389. * Adds a linked script to the page
  390. *
  391. * @param string $url URL to the linked script
  392. * @param string $type Type of script. Defaults to 'text/javascript'
  393. * @param boolean $defer Adds the defer attribute.
  394. * @param boolean $async Adds the async attribute.
  395. *
  396. * @return JDocument instance of $this to allow chaining
  397. *
  398. * @since 11.1
  399. */
  400. public function addScript($url, $type = "text/javascript", $defer = false, $async = false)
  401. {
  402. $this->_scripts[$url]['mime'] = $type;
  403. $this->_scripts[$url]['defer'] = $defer;
  404. $this->_scripts[$url]['async'] = $async;
  405. return $this;
  406. }
  407. /**
  408. * Adds a script to the page
  409. *
  410. * @param string $content Script
  411. * @param string $type Scripting mime (defaults to 'text/javascript')
  412. *
  413. * @return JDocument instance of $this to allow chaining
  414. *
  415. * @since 11.1
  416. */
  417. public function addScriptDeclaration($content, $type = 'text/javascript')
  418. {
  419. if (!isset($this->_script[strtolower($type)]))
  420. {
  421. $this->_script[strtolower($type)] = $content;
  422. }
  423. else
  424. {
  425. $this->_script[strtolower($type)] .= chr(13) . $content;
  426. }
  427. return $this;
  428. }
  429. /**
  430. * Adds a linked stylesheet to the page
  431. *
  432. * @param string $url URL to the linked style sheet
  433. * @param string $type Mime encoding type
  434. * @param string $media Media type that this stylesheet applies to
  435. * @param array $attribs Array of attributes
  436. *
  437. * @return JDocument instance of $this to allow chaining
  438. *
  439. * @since 11.1
  440. */
  441. public function addStyleSheet($url, $type = 'text/css', $media = null, $attribs = array())
  442. {
  443. $this->_styleSheets[$url]['mime'] = $type;
  444. $this->_styleSheets[$url]['media'] = $media;
  445. $this->_styleSheets[$url]['attribs'] = $attribs;
  446. return $this;
  447. }
  448. /**
  449. * Adds a stylesheet declaration to the page
  450. *
  451. * @param string $content Style declarations
  452. * @param string $type Type of stylesheet (defaults to 'text/css')
  453. *
  454. * @return JDocument instance of $this to allow chaining
  455. *
  456. * @since 11.1
  457. */
  458. public function addStyleDeclaration($content, $type = 'text/css')
  459. {
  460. if (!isset($this->_style[strtolower($type)]))
  461. {
  462. $this->_style[strtolower($type)] = $content;
  463. }
  464. else
  465. {
  466. $this->_style[strtolower($type)] .= chr(13) . $content;
  467. }
  468. return $this;
  469. }
  470. /**
  471. * Sets the document charset
  472. *
  473. * @param string $type Charset encoding string
  474. *
  475. * @return JDocument instance of $this to allow chaining
  476. *
  477. * @since 11.1
  478. */
  479. public function setCharset($type = 'utf-8')
  480. {
  481. $this->_charset = $type;
  482. return $this;
  483. }
  484. /**
  485. * Returns the document charset encoding.
  486. *
  487. * @return string
  488. *
  489. * @since 11.1
  490. */
  491. public function getCharset()
  492. {
  493. return $this->_charset;
  494. }
  495. /**
  496. * Sets the global document language declaration. Default is English (en-gb).
  497. *
  498. * @param string $lang The language to be set
  499. *
  500. * @return JDocument instance of $this to allow chaining
  501. *
  502. * @since 11.1
  503. */
  504. public function setLanguage($lang = "en-gb")
  505. {
  506. $this->language = strtolower($lang);
  507. return $this;
  508. }
  509. /**
  510. * Returns the document language.
  511. *
  512. * @return string
  513. *
  514. * @since 11.1
  515. */
  516. public function getLanguage()
  517. {
  518. return $this->language;
  519. }
  520. /**
  521. * Sets the global document direction declaration. Default is left-to-right (ltr).
  522. *
  523. * @param string $dir The language direction to be set
  524. *
  525. * @return JDocument instance of $this to allow chaining
  526. *
  527. * @since 11.1
  528. */
  529. public function setDirection($dir = "ltr")
  530. {
  531. $this->direction = strtolower($dir);
  532. return $this;
  533. }
  534. /**
  535. * Returns the document direction declaration.
  536. *
  537. * @return string
  538. *
  539. * @since 11.1
  540. */
  541. public function getDirection()
  542. {
  543. return $this->direction;
  544. }
  545. /**
  546. * Sets the title of the document
  547. *
  548. * @param string $title The title to be set
  549. *
  550. * @return JDocument instance of $this to allow chaining
  551. *
  552. * @since 11.1
  553. */
  554. public function setTitle($title)
  555. {
  556. $this->title = $title;
  557. return $this;
  558. }
  559. /**
  560. * Return the title of the document.
  561. *
  562. * @return string
  563. *
  564. * @since 11.1
  565. */
  566. public function getTitle()
  567. {
  568. return $this->title;
  569. }
  570. /**
  571. * Sets the base URI of the document
  572. *
  573. * @param string $base The base URI to be set
  574. *
  575. * @return JDocument instance of $this to allow chaining
  576. *
  577. * @since 11.1
  578. */
  579. public function setBase($base)
  580. {
  581. $this->base = $base;
  582. return $this;
  583. }
  584. /**
  585. * Return the base URI of the document.
  586. *
  587. * @return string
  588. *
  589. * @since 11.1
  590. */
  591. public function getBase()
  592. {
  593. return $this->base;
  594. }
  595. /**
  596. * Sets the description of the document
  597. *
  598. * @param string $description The description to set
  599. *
  600. * @return JDocument instance of $this to allow chaining
  601. *
  602. * @since 11.1
  603. */
  604. public function setDescription($description)
  605. {
  606. $this->description = $description;
  607. return $this;
  608. }
  609. /**
  610. * Return the title of the page.
  611. *
  612. * @return string
  613. *
  614. * @since 11.1
  615. */
  616. public function getDescription()
  617. {
  618. return $this->description;
  619. }
  620. /**
  621. * Sets the document link
  622. *
  623. * @param string $url A url
  624. *
  625. * @return JDocument instance of $this to allow chaining
  626. *
  627. * @since 11.1
  628. */
  629. public function setLink($url)
  630. {
  631. $this->link = $url;
  632. return $this;
  633. }
  634. /**
  635. * Returns the document base url
  636. *
  637. * @return string
  638. *
  639. * @since 11.1
  640. */
  641. public function getLink()
  642. {
  643. return $this->link;
  644. }
  645. /**
  646. * Sets the document generator
  647. *
  648. * @param string $generator The generator to be set
  649. *
  650. * @return JDocument instance of $this to allow chaining
  651. *
  652. * @since 11.1
  653. */
  654. public function setGenerator($generator)
  655. {
  656. $this->_generator = $generator;
  657. return $this;
  658. }
  659. /**
  660. * Returns the document generator
  661. *
  662. * @return string
  663. *
  664. * @since 11.1
  665. */
  666. public function getGenerator()
  667. {
  668. return $this->_generator;
  669. }
  670. /**
  671. * Sets the document modified date
  672. *
  673. * @param string $date The date to be set
  674. *
  675. * @return JDocument instance of $this to allow chaining
  676. *
  677. * @since 11.1
  678. */
  679. public function setModifiedDate($date)
  680. {
  681. $this->_mdate = $date;
  682. return $this;
  683. }
  684. /**
  685. * Returns the document modified date
  686. *
  687. * @return string
  688. *
  689. * @since 11.1
  690. */
  691. public function getModifiedDate()
  692. {
  693. return $this->_mdate;
  694. }
  695. /**
  696. * Sets the document MIME encoding that is sent to the browser.
  697. *
  698. * This usually will be text/html because most browsers cannot yet
  699. * accept the proper mime settings for XHTML: application/xhtml+xml
  700. * and to a lesser extent application/xml and text/xml. See the W3C note
  701. * ({@link http://www.w3.org/TR/xhtml-media-types/
  702. * http://www.w3.org/TR/xhtml-media-types/}) for more details.
  703. *
  704. * @param string $type The document type to be sent
  705. * @param boolean $sync Should the type be synced with HTML?
  706. *
  707. * @return JDocument instance of $this to allow chaining
  708. *
  709. * @since 11.1
  710. *
  711. * @link http://www.w3.org/TR/xhtml-media-types
  712. */
  713. public function setMimeEncoding($type = 'text/html', $sync = true)
  714. {
  715. $this->_mime = strtolower($type);
  716. // Syncing with meta-data
  717. if ($sync)
  718. {
  719. $this->setMetaData('content-type', $type . '; charset=' . $this->_charset, true);
  720. }
  721. return $this;
  722. }
  723. /**
  724. * Return the document MIME encoding that is sent to the browser.
  725. *
  726. * @return string
  727. *
  728. * @since 11.1
  729. */
  730. public function getMimeEncoding()
  731. {
  732. return $this->_mime;
  733. }
  734. /**
  735. * Sets the line end style to Windows, Mac, Unix or a custom string.
  736. *
  737. * @param string $style "win", "mac", "unix" or custom string.
  738. *
  739. * @return JDocument instance of $this to allow chaining
  740. *
  741. * @since 11.1
  742. */
  743. public function setLineEnd($style)
  744. {
  745. switch ($style)
  746. {
  747. case 'win':
  748. $this->_lineEnd = "\15\12";
  749. break;
  750. case 'unix':
  751. $this->_lineEnd = "\12";
  752. break;
  753. case 'mac':
  754. $this->_lineEnd = "\15";
  755. break;
  756. default:
  757. $this->_lineEnd = $style;
  758. }
  759. return $this;
  760. }
  761. /**
  762. * Returns the lineEnd
  763. *
  764. * @return string
  765. *
  766. * @since 11.1
  767. */
  768. public function _getLineEnd()
  769. {
  770. return $this->_lineEnd;
  771. }
  772. /**
  773. * Sets the string used to indent HTML
  774. *
  775. * @param string $string String used to indent ("\11", "\t", ' ', etc.).
  776. *
  777. * @return JDocument instance of $this to allow chaining
  778. *
  779. * @since 11.1
  780. */
  781. public function setTab($string)
  782. {
  783. $this->_tab = $string;
  784. return $this;
  785. }
  786. /**
  787. * Returns a string containing the unit for indenting HTML
  788. *
  789. * @return string
  790. *
  791. * @since 11.1
  792. */
  793. public function _getTab()
  794. {
  795. return $this->_tab;
  796. }
  797. /**
  798. * Load a renderer
  799. *
  800. * @param string $type The renderer type
  801. *
  802. * @return JDocumentRenderer Object or null if class does not exist
  803. *
  804. * @since 11.1
  805. * @throws RuntimeException
  806. */
  807. public function loadRenderer($type)
  808. {
  809. $class = 'JDocumentRenderer' . $type;
  810. if (!class_exists($class))
  811. {
  812. $path = __DIR__ . '/' . $this->_type . '/renderer/' . $type . '.php';
  813. if (file_exists($path))
  814. {
  815. require_once $path;
  816. }
  817. else
  818. {
  819. throw new RuntimeException('Unable to load renderer class', 500);
  820. }
  821. }
  822. if (!class_exists($class))
  823. {
  824. return null;
  825. }
  826. $instance = new $class($this);
  827. return $instance;
  828. }
  829. /**
  830. * Parses the document and prepares the buffers
  831. *
  832. * @param array $params The array of parameters
  833. *
  834. * @return JDocument instance of $this to allow chaining
  835. *
  836. * @since 11.1
  837. */
  838. public function parse($params = array())
  839. {
  840. return $this;
  841. }
  842. /**
  843. * Outputs the document
  844. *
  845. * @param boolean $cache If true, cache the output
  846. * @param array $params Associative array of attributes
  847. *
  848. * @return The rendered data
  849. *
  850. * @since 11.1
  851. */
  852. public function render($cache = false, $params = array())
  853. {
  854. if ($mdate = $this->getModifiedDate())
  855. {
  856. JResponse::setHeader('Last-Modified', $mdate /* gmdate('D, d M Y H:i:s', time() + 900) . ' GMT' */);
  857. }
  858. JResponse::setHeader('Content-Type', $this->_mime . ($this->_charset ? '; charset=' . $this->_charset : ''));
  859. }
  860. }