PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/joomla/libraries/joomla/document/document.php

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