PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/document/document.php

https://github.com/joebushi/joomla
PHP | 781 lines | 273 code | 83 blank | 425 comment | 38 complexity | d60fe050588f61a93679d04000e010f5 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Framework
  5. * @subpackage Document
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('JPATH_BASE') or die;
  10. //Register the renderer class with the loader
  11. JLoader::register('JDocumentRenderer', dirname(__FILE__).DS.'renderer.php');
  12. /**
  13. * Document class, provides an easy interface to parse and display a document
  14. *
  15. * @abstract
  16. * @package Joomla.Framework
  17. * @subpackage Document
  18. * @since 1.5
  19. */
  20. class JDocument extends JObject
  21. {
  22. /**
  23. * Document title
  24. *
  25. * @var string
  26. * @access public
  27. */
  28. var $title = '';
  29. /**
  30. * Document description
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. var $description = '';
  36. /**
  37. * Document full URL
  38. *
  39. * @var string
  40. * @access public
  41. */
  42. var $link = '';
  43. /**
  44. * Document base URL
  45. *
  46. * @var string
  47. * @access public
  48. */
  49. var $base = '';
  50. /**
  51. * Contains the document language setting
  52. *
  53. * @var string
  54. * @access public
  55. */
  56. var $language = 'en-gb';
  57. /**
  58. * Contains the document direction setting
  59. *
  60. * @var string
  61. * @access public
  62. */
  63. var $direction = 'ltr';
  64. /**
  65. * Document generator
  66. *
  67. * @var string
  68. * @access public
  69. */
  70. var $_generator = 'Joomla! 1.6 - Open Source Content Management';
  71. /**
  72. * Document modified date
  73. *
  74. * @var string
  75. * @access private
  76. */
  77. var $_mdate = '';
  78. /**
  79. * Tab string
  80. *
  81. * @var string
  82. * @access private
  83. */
  84. var $_tab = "\11";
  85. /**
  86. * Contains the line end string
  87. *
  88. * @var string
  89. * @access private
  90. */
  91. var $_lineEnd = "\12";
  92. /**
  93. * Contains the character encoding string
  94. *
  95. * @var string
  96. * @access private
  97. */
  98. var $_charset = 'utf-8';
  99. /**
  100. * Document mime type
  101. *
  102. * @var string
  103. * @access private
  104. */
  105. var $_mime = '';
  106. /**
  107. * Document namespace
  108. *
  109. * @var string
  110. * @access private
  111. */
  112. var $_namespace = '';
  113. /**
  114. * Document profile
  115. *
  116. * @var string
  117. * @access private
  118. */
  119. var $_profile = '';
  120. /**
  121. * Array of linked scripts
  122. *
  123. * @var array
  124. * @access private
  125. */
  126. var $_scripts = array();
  127. /**
  128. * Array of scripts placed in the header
  129. *
  130. * @var array
  131. * @access private
  132. */
  133. var $_script = array();
  134. /**
  135. * Array of linked style sheets
  136. *
  137. * @var array
  138. * @access private
  139. */
  140. var $_styleSheets = array();
  141. /**
  142. * Array of included style declarations
  143. *
  144. * @var array
  145. * @access private
  146. */
  147. var $_style = array();
  148. /**
  149. * Array of meta tags
  150. *
  151. * @var array
  152. * @access private
  153. */
  154. var $_metaTags = array();
  155. /**
  156. * The rendering engine
  157. *
  158. * @var object
  159. * @access private
  160. */
  161. var $_engine = null;
  162. /**
  163. * The document type
  164. *
  165. * @var string
  166. * @access private
  167. */
  168. var $_type = null;
  169. /**
  170. * Array of buffered output
  171. *
  172. * @var mixed (depends on the renderer)
  173. * @access private
  174. */
  175. var $_buffer = null;
  176. /**
  177. * Class constructor
  178. *
  179. * @access protected
  180. * @param array $options Associative array of options
  181. */
  182. function __construct($options = array())
  183. {
  184. parent::__construct();
  185. if (array_key_exists('lineend', $options)) {
  186. $this->setLineEnd($options['lineend']);
  187. }
  188. if (array_key_exists('charset', $options)) {
  189. $this->setCharset($options['charset']);
  190. }
  191. if (array_key_exists('language', $options)) {
  192. $this->setLanguage($options['language']);
  193. }
  194. if (array_key_exists('direction', $options)) {
  195. $this->setDirection($options['direction']);
  196. }
  197. if (array_key_exists('tab', $options)) {
  198. $this->setTab($options['tab']);
  199. }
  200. if (array_key_exists('link', $options)) {
  201. $this->setLink($options['link']);
  202. }
  203. if (array_key_exists('base', $options)) {
  204. $this->setBase($options['base']);
  205. }
  206. }
  207. /**
  208. * Returns the global JDocument object, only creating it
  209. * if it doesn't already exist.
  210. *
  211. * @param type $type The document type to instantiate
  212. * @return object The document object.
  213. */
  214. public static function getInstance($type = 'html', $attributes = array())
  215. {
  216. static $instances;
  217. if (!isset($instances)) {
  218. $instances = array();
  219. }
  220. $signature = serialize(array($type, $attributes));
  221. if (empty($instances[$signature]))
  222. {
  223. $type = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
  224. $path = dirname(__FILE__).DS.$type.DS.$type.'.php';
  225. $ntype = null;
  226. // Check if the document type exists
  227. if (!file_exists($path))
  228. {
  229. // Default to the raw format
  230. $ntype = $type;
  231. $type = 'raw';
  232. }
  233. // Determine the path and class
  234. $class = 'JDocument'.$type;
  235. if (!class_exists($class))
  236. {
  237. $path = dirname(__FILE__).DS.$type.DS.$type.'.php';
  238. if (file_exists($path)) {
  239. require_once $path;
  240. } else {
  241. JError::raiseError(500,JText::_('Unable to load document class'));
  242. }
  243. }
  244. $instance = new $class($attributes);
  245. $instances[$signature] = &$instance;
  246. if (!is_null($ntype))
  247. {
  248. // Set the type to the Document type originally requested
  249. $instance->setType($ntype);
  250. }
  251. }
  252. return $instances[$signature];
  253. }
  254. /**
  255. * Set the document type
  256. *
  257. * @access public
  258. * @param string $type
  259. */
  260. function setType($type) {
  261. $this->_type = $type;
  262. }
  263. /**
  264. * Returns the document type
  265. *
  266. * @access public
  267. * @return string
  268. */
  269. function getType() {
  270. return $this->_type;
  271. }
  272. /**
  273. * Get the document head data
  274. *
  275. * @access public
  276. * @return array The document head data in array form
  277. */
  278. function getHeadData() {
  279. // Impelemented in child classes
  280. }
  281. /**
  282. * Set the document head data
  283. *
  284. * @access public
  285. * @param array $data The document head data in array form
  286. */
  287. function setHeadData($data) {
  288. // Impelemented in child classes
  289. }
  290. /**
  291. * Get the contents of the document buffer
  292. *
  293. * @access public
  294. * @return The contents of the document buffer
  295. */
  296. function getBuffer() {
  297. return $this->_buffer;
  298. }
  299. /**
  300. * Set the contents of the document buffer
  301. *
  302. * @param string $content The content to be set in the buffer.
  303. * @param array $options Array of optional elements.
  304. */
  305. public function setBuffer($content, $options = array()) {
  306. $this->_buffer = $content;
  307. }
  308. /**
  309. * Gets a meta tag.
  310. *
  311. * @param string $name Value of name or http-equiv tag
  312. * @param bool $http_equiv META type "http-equiv" defaults to null
  313. * @return string
  314. * @access public
  315. */
  316. function getMetaData($name, $http_equiv = false)
  317. {
  318. $result = '';
  319. $name = strtolower($name);
  320. if ($name == 'generator') {
  321. $result = $this->getGenerator();
  322. } elseif ($name == 'description') {
  323. $result = $this->getDescription();
  324. } else {
  325. if ($http_equiv == true) {
  326. $result = @$this->_metaTags['http-equiv'][$name];
  327. } else {
  328. $result = @$this->_metaTags['standard'][$name];
  329. }
  330. }
  331. return $result;
  332. }
  333. /**
  334. * Sets or alters a meta tag.
  335. *
  336. * @param string $name Value of name or http-equiv tag
  337. * @param string $content Value of the content tag
  338. * @param bool $http_equiv META type "http-equiv" defaults to null
  339. * @return void
  340. * @access public
  341. */
  342. function setMetaData($name, $content, $http_equiv = false)
  343. {
  344. $name = strtolower($name);
  345. if ($name == 'generator') {
  346. $this->setGenerator($content);
  347. } elseif ($name == 'description') {
  348. $this->setDescription($content);
  349. } else {
  350. if ($http_equiv == true) {
  351. $this->_metaTags['http-equiv'][$name] = $content;
  352. } else {
  353. $this->_metaTags['standard'][$name] = $content;
  354. }
  355. }
  356. }
  357. /**
  358. * Adds a linked script to the page
  359. *
  360. * @param string $url URL to the linked script
  361. * @param string $type Type of script. Defaults to 'text/javascript'
  362. * @access public
  363. */
  364. function addScript($url, $type="text/javascript") {
  365. $this->_scripts[$url] = $type;
  366. }
  367. /**
  368. * Adds a script to the page
  369. *
  370. * @access public
  371. * @param string $content Script
  372. * @param string $type Scripting mime (defaults to 'text/javascript')
  373. * @return void
  374. */
  375. function addScriptDeclaration($content, $type = 'text/javascript')
  376. {
  377. if (!isset($this->_script[strtolower($type)])) {
  378. $this->_script[strtolower($type)] = $content;
  379. } else {
  380. $this->_script[strtolower($type)] .= chr(13).$content;
  381. }
  382. }
  383. /**
  384. * Adds a linked stylesheet to the page
  385. *
  386. * @param string $url URL to the linked style sheet
  387. * @param string $type Mime encoding type
  388. * @param string $media Media type that this stylesheet applies to
  389. * @access public
  390. */
  391. function addStyleSheet($url, $type = 'text/css', $media = null, $attribs = array())
  392. {
  393. $this->_styleSheets[$url]['mime'] = $type;
  394. $this->_styleSheets[$url]['media'] = $media;
  395. $this->_styleSheets[$url]['attribs'] = $attribs;
  396. }
  397. /**
  398. * Adds a stylesheet declaration to the page
  399. *
  400. * @param string $content Style declarations
  401. * @param string $type Type of stylesheet (defaults to 'text/css')
  402. * @access public
  403. * @return void
  404. */
  405. function addStyleDeclaration($content, $type = 'text/css')
  406. {
  407. if (!isset($this->_style[strtolower($type)])) {
  408. $this->_style[strtolower($type)] = $content;
  409. } else {
  410. $this->_style[strtolower($type)] .= chr(13).$content;
  411. }
  412. }
  413. /**
  414. * Sets the document charset
  415. *
  416. * @param string $type Charset encoding string
  417. * @access public
  418. * @return void
  419. */
  420. function setCharset($type = 'utf-8') {
  421. $this->_charset = $type;
  422. }
  423. /**
  424. * Returns the document charset encoding.
  425. *
  426. * @access public
  427. * @return string
  428. */
  429. function getCharset() {
  430. return $this->_charset;
  431. }
  432. /**
  433. * Sets the global document language declaration. Default is English (en-gb).
  434. *
  435. * @access public
  436. * @param string $lang
  437. */
  438. function setLanguage($lang = "en-gb") {
  439. $this->language = strtolower($lang);
  440. }
  441. /**
  442. * Returns the document language.
  443. *
  444. * @return string
  445. * @access public
  446. */
  447. function getLanguage() {
  448. return $this->language;
  449. }
  450. /**
  451. * Sets the global document direction declaration. Default is left-to-right (ltr).
  452. *
  453. * @access public
  454. * @param string $lang
  455. */
  456. function setDirection($dir = "ltr") {
  457. $this->direction = strtolower($dir);
  458. }
  459. /**
  460. * Returns the document language.
  461. *
  462. * @return string
  463. * @access public
  464. */
  465. function getDirection() {
  466. return $this->direction;
  467. }
  468. /**
  469. * Sets the title of the document
  470. *
  471. * @param string $title
  472. * @access public
  473. */
  474. function setTitle($title) {
  475. $this->title = $title;
  476. }
  477. /**
  478. * Return the title of the document.
  479. *
  480. * @return string
  481. * @access public
  482. */
  483. function getTitle() {
  484. return $this->title;
  485. }
  486. /**
  487. * Sets the base URI of the document
  488. *
  489. * @param string $base
  490. * @access public
  491. */
  492. function setBase($base) {
  493. $this->base = $base;
  494. }
  495. /**
  496. * Return the base URI of the document.
  497. *
  498. * @return string
  499. * @access public
  500. */
  501. function getBase() {
  502. return $this->base;
  503. }
  504. /**
  505. * Sets the description of the document
  506. *
  507. * @param string $title
  508. * @access public
  509. */
  510. function setDescription($description) {
  511. $this->description = $description;
  512. }
  513. /**
  514. * Return the title of the page.
  515. *
  516. * @return string
  517. * @access public
  518. */
  519. function getDescription() {
  520. return $this->description;
  521. }
  522. /**
  523. * Sets the document link
  524. *
  525. * @param string $url A url
  526. * @access public
  527. * @return void
  528. */
  529. function setLink($url) {
  530. $this->link = $url;
  531. }
  532. /**
  533. * Returns the document base url
  534. *
  535. * @access public
  536. * @return string
  537. */
  538. function getLink() {
  539. return $this->link;
  540. }
  541. /**
  542. * Sets the document generator
  543. *
  544. * @param string
  545. * @access public
  546. * @return void
  547. */
  548. function setGenerator($generator) {
  549. $this->_generator = $generator;
  550. }
  551. /**
  552. * Returns the document generator
  553. *
  554. * @access public
  555. * @return string
  556. */
  557. function getGenerator() {
  558. return $this->_generator;
  559. }
  560. /**
  561. * Sets the document modified date
  562. *
  563. * @param string
  564. * @access public
  565. * @return void
  566. */
  567. function setModifiedDate($date) {
  568. $this->_mdate = $date;
  569. }
  570. /**
  571. * Returns the document modified date
  572. *
  573. * @access public
  574. * @return string
  575. */
  576. function getModifiedDate() {
  577. return $this->_mdate;
  578. }
  579. /**
  580. * Sets the document MIME encoding that is sent to the browser.
  581. *
  582. * <p>This usually will be text/html because most browsers cannot yet
  583. * accept the proper mime settings for XHTML: application/xhtml+xml
  584. * and to a lesser extent application/xml and text/xml. See the W3C note
  585. * ({@link http://www.w3.org/TR/xhtml-media-types/
  586. * http://www.w3.org/TR/xhtml-media-types/}) for more details.</p>
  587. *
  588. * @param string $type
  589. * @access public
  590. * @return void
  591. */
  592. function setMimeEncoding($type = 'text/html') {
  593. $this->_mime = strtolower($type);
  594. }
  595. /**
  596. * Sets the line end style to Windows, Mac, Unix or a custom string.
  597. *
  598. * @param string $style "win", "mac", "unix" or custom string.
  599. * @access public
  600. * @return void
  601. */
  602. function setLineEnd($style)
  603. {
  604. switch ($style) {
  605. case 'win':
  606. $this->_lineEnd = "\15\12";
  607. break;
  608. case 'unix':
  609. $this->_lineEnd = "\12";
  610. break;
  611. case 'mac':
  612. $this->_lineEnd = "\15";
  613. break;
  614. default:
  615. $this->_lineEnd = $style;
  616. }
  617. }
  618. /**
  619. * Returns the lineEnd
  620. *
  621. * @access private
  622. * @return string
  623. */
  624. function _getLineEnd() {
  625. return $this->_lineEnd;
  626. }
  627. /**
  628. * Sets the string used to indent HTML
  629. *
  630. * @param string $string String used to indent ("\11", "\t", ' ', etc.).
  631. * @access public
  632. * @return void
  633. */
  634. function setTab($string) {
  635. $this->_tab = $string;
  636. }
  637. /**
  638. * Returns a string containing the unit for indenting HTML
  639. *
  640. * @access private
  641. * @return string
  642. */
  643. function _getTab() {
  644. return $this->_tab;
  645. }
  646. /**
  647. * Load a renderer
  648. *
  649. * @access public
  650. * @param string The renderer type
  651. * @return object
  652. * @since 1.5
  653. */
  654. function loadRenderer($type)
  655. {
  656. $class = 'JDocumentRenderer'.$type;
  657. if (!class_exists($class))
  658. {
  659. $path = dirname(__FILE__).DS.$this->_type.DS.'renderer'.DS.$type.'.php';
  660. if (file_exists($path)) {
  661. require_once $path;
  662. } else {
  663. JError::raiseError(500,JText::_('Unable to load renderer class'));
  664. }
  665. }
  666. if (!class_exists($class)) {
  667. return null;
  668. }
  669. $instance = new $class($this);
  670. return $instance;
  671. }
  672. /**
  673. * Parses the document and prepares the buffers
  674. *
  675. * @access public
  676. * @return null
  677. */
  678. public function parse($params = array()) {
  679. return null;
  680. }
  681. /**
  682. * Outputs the document
  683. *
  684. * @access public
  685. * @param boolean $cache If true, cache the output
  686. * @param boolean $compress If true, compress the output
  687. * @param array $params Associative array of attributes
  688. * @return The rendered data
  689. */
  690. function render($cache = false, $params = array())
  691. {
  692. JResponse::setHeader('Expires', gmdate('D, d M Y H:i:s', time() + 900) . ' GMT');
  693. if ($mdate = $this->getModifiedDate()) {
  694. JResponse::setHeader('Last-Modified', $mdate /* gmdate('D, d M Y H:i:s', time() + 900) . ' GMT' */);
  695. }
  696. JResponse::setHeader('Content-Type', $this->_mime . '; charset=' . $this->_charset);
  697. }
  698. }