/libraries/joomla/document/document.php

https://gitlab.com/vitaliylukin91/lavka · PHP · 1068 lines · 397 code · 117 blank · 554 comment · 36 complexity · 4e084d7ac12e115688702115c088bafa MD5 · raw file

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