PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/HTML/Common.php

https://bitbucket.org/chamilo/chamilo/
PHP | 465 lines | 349 code | 15 blank | 101 comment | 6 complexity | 0052e6817de06169eee554c258a29a2a MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Base class for all HTML classes
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_Common
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Common.php 137 2009-11-09 13:24:37Z vanpouckesven $
  20. * @link http://pear.php.net/package/HTML_Common/
  21. */
  22. /**
  23. * Base class for all HTML classes
  24. *
  25. * @category HTML
  26. * @package HTML_Common
  27. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  28. * @version Release: 1.2.5
  29. * @abstract
  30. */
  31. class HTML_Common
  32. {
  33. /**
  34. * Associative array of attributes
  35. * @var array
  36. * @access private
  37. */
  38. var $_attributes = array();
  39. /**
  40. * Tab offset of the tag
  41. * @var int
  42. * @access private
  43. */
  44. var $_tabOffset = 0;
  45. /**
  46. * Tab string
  47. * @var string
  48. * @since 1.7
  49. * @access private
  50. */
  51. var $_tab = "\11";
  52. /**
  53. * Contains the line end string
  54. * @var string
  55. * @since 1.7
  56. * @access private
  57. */
  58. var $_lineEnd = "\12";
  59. /**
  60. * HTML comment on the object
  61. * @var string
  62. * @since 1.5
  63. * @access private
  64. */
  65. var $_comment = '';
  66. /**
  67. * Class constructor
  68. * @param mixed $attributes Associative array of table tag attributes
  69. * or HTML attributes name="value" pairs
  70. * @param int $tabOffset Indent offset in tabs
  71. * @access public
  72. */
  73. function __construct($attributes = null, $tabOffset = 0)
  74. {
  75. $this->setAttributes($attributes);
  76. $this->setTabOffset($tabOffset);
  77. } // end constructor
  78. /**
  79. * Returns the current API version
  80. * @access public
  81. * @returns double
  82. */
  83. function apiVersion()
  84. {
  85. return 1.7;
  86. } // end func apiVersion
  87. /**
  88. * Returns the lineEnd
  89. *
  90. * @since 1.7
  91. * @access private
  92. * @return string
  93. */
  94. function _getLineEnd()
  95. {
  96. return $this->_lineEnd;
  97. } // end func getLineEnd
  98. /**
  99. * Returns a string containing the unit for indenting HTML
  100. *
  101. * @since 1.7
  102. * @access private
  103. * @return string
  104. */
  105. function _getTab()
  106. {
  107. return $this->_tab;
  108. } // end func _getTab
  109. /**
  110. * Returns a string containing the offset for the whole HTML code
  111. *
  112. * @return string
  113. * @access private
  114. */
  115. function _getTabs()
  116. {
  117. return str_repeat($this->_getTab(), $this->_tabOffset);
  118. } // end func _getTabs
  119. /**
  120. * Returns an HTML formatted attribute string
  121. * @param array $attributes
  122. * @return string
  123. * @access private
  124. */
  125. function _getAttrString($attributes)
  126. {
  127. $strAttr = '';
  128. if (is_array($attributes)) {
  129. $charset = HTML_Common::charset();
  130. foreach ($attributes as $key => $value) {
  131. $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
  132. }
  133. }
  134. return $strAttr;
  135. } // end func _getAttrString
  136. /**
  137. * Returns a valid atrributes array from either a string or array
  138. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  139. * @access private
  140. * @return array
  141. */
  142. function _parseAttributes($attributes)
  143. {
  144. if (is_array($attributes)) {
  145. $ret = array();
  146. foreach ($attributes as $key => $value) {
  147. if (is_int($key)) {
  148. $key = $value = strtolower($value);
  149. } else {
  150. $key = strtolower($key);
  151. }
  152. $ret[$key] = $value;
  153. }
  154. return $ret;
  155. } elseif (is_string($attributes)) {
  156. $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" .
  157. "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  158. if (preg_match_all($preg, $attributes, $regs)) {
  159. for ($counter=0; $counter<count($regs[1]); $counter++) {
  160. $name = $regs[1][$counter];
  161. $check = $regs[0][$counter];
  162. $value = $regs[7][$counter];
  163. if (trim($name) == trim($check)) {
  164. $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
  165. } else {
  166. if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'") {
  167. $arrAttr[strtolower(trim($name))] = substr($value, 1, -1);
  168. } else {
  169. $arrAttr[strtolower(trim($name))] = trim($value);
  170. }
  171. }
  172. }
  173. return $arrAttr;
  174. }
  175. }
  176. } // end func _parseAttributes
  177. /**
  178. * Returns the array key for the given non-name-value pair attribute
  179. *
  180. * @param string $attr Attribute
  181. * @param array $attributes Array of attribute
  182. * @since 1.0
  183. * @access private
  184. * @return bool
  185. */
  186. function _getAttrKey($attr, $attributes)
  187. {
  188. if (isset($attributes[strtolower($attr)])) {
  189. return true;
  190. } else {
  191. return null;
  192. }
  193. } //end func _getAttrKey
  194. /**
  195. * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
  196. * @param array $attr1 Original attributes array
  197. * @param array $attr2 New attributes array
  198. * @access private
  199. */
  200. function _updateAttrArray(&$attr1, $attr2)
  201. {
  202. if (!is_array($attr2)) {
  203. return false;
  204. }
  205. foreach ($attr2 as $key => $value) {
  206. $attr1[$key] = $value;
  207. }
  208. } // end func _updateAtrrArray
  209. /**
  210. * Removes the given attribute from the given array
  211. *
  212. * @param string $attr Attribute name
  213. * @param array $attributes Attribute array
  214. * @since 1.4
  215. * @access private
  216. * @return void
  217. */
  218. function _removeAttr($attr, &$attributes)
  219. {
  220. $attr = strtolower($attr);
  221. if (isset($attributes[$attr])) {
  222. unset($attributes[$attr]);
  223. }
  224. } //end func _removeAttr
  225. /**
  226. * Returns the value of the given attribute
  227. *
  228. * @param string $attr Attribute name
  229. * @since 1.5
  230. * @access public
  231. * @return string|null returns null if an attribute does not exist
  232. */
  233. function getAttribute($attr)
  234. {
  235. $attr = strtolower($attr);
  236. if (isset($this->_attributes[$attr])) {
  237. return $this->_attributes[$attr];
  238. }
  239. return null;
  240. } //end func getAttribute
  241. /**
  242. * Sets the value of the attribute
  243. *
  244. * @param string Attribute name
  245. * @param string Attribute value (will be set to $name if omitted)
  246. * @access public
  247. */
  248. function setAttribute($name, $value = null)
  249. {
  250. $name = strtolower($name);
  251. if (is_null($value)) {
  252. $value = $name;
  253. }
  254. $this->_attributes[$name] = $value;
  255. } // end func setAttribute
  256. /**
  257. * Sets the HTML attributes
  258. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  259. * @access public
  260. */
  261. function setAttributes($attributes)
  262. {
  263. $this->_attributes = $this->_parseAttributes($attributes);
  264. } // end func setAttributes
  265. /**
  266. * Returns the assoc array (default) or string of attributes
  267. *
  268. * @param bool Whether to return the attributes as string
  269. * @since 1.6
  270. * @access public
  271. * @return mixed attributes
  272. */
  273. function getAttributes($asString = false)
  274. {
  275. if ($asString) {
  276. return $this->_getAttrString($this->_attributes);
  277. } else {
  278. return $this->_attributes;
  279. }
  280. } //end func getAttributes
  281. /**
  282. * Updates the passed attributes without changing the other existing attributes
  283. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  284. * @access public
  285. */
  286. function updateAttributes($attributes)
  287. {
  288. $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
  289. } // end func updateAttributes
  290. /**
  291. * Removes an attribute
  292. *
  293. * @param string $attr Attribute name
  294. * @since 1.4
  295. * @access public
  296. * @return void
  297. */
  298. function removeAttribute($attr)
  299. {
  300. $this->_removeAttr($attr, $this->_attributes);
  301. } //end func removeAttribute
  302. /**
  303. * Sets the line end style to Windows, Mac, Unix or a custom string.
  304. *
  305. * @param string $style "win", "mac", "unix" or custom string.
  306. * @since 1.7
  307. * @access public
  308. * @return void
  309. */
  310. function setLineEnd($style)
  311. {
  312. switch ($style) {
  313. case 'win':
  314. $this->_lineEnd = "\15\12";
  315. break;
  316. case 'unix':
  317. $this->_lineEnd = "\12";
  318. break;
  319. case 'mac':
  320. $this->_lineEnd = "\15";
  321. break;
  322. default:
  323. $this->_lineEnd = $style;
  324. }
  325. } // end func setLineEnd
  326. /**
  327. * Sets the tab offset
  328. *
  329. * @param int $offset
  330. * @access public
  331. */
  332. function setTabOffset($offset)
  333. {
  334. $this->_tabOffset = $offset;
  335. } // end func setTabOffset
  336. /**
  337. * Returns the tabOffset
  338. *
  339. * @since 1.5
  340. * @access public
  341. * @return int
  342. */
  343. function getTabOffset()
  344. {
  345. return $this->_tabOffset;
  346. } //end func getTabOffset
  347. /**
  348. * Sets the string used to indent HTML
  349. *
  350. * @since 1.7
  351. * @param string $string String used to indent ("\11", "\t", ' ', etc.).
  352. * @access public
  353. * @return void
  354. */
  355. function setTab($string)
  356. {
  357. $this->_tab = $string;
  358. } // end func setTab
  359. /**
  360. * Sets the HTML comment to be displayed at the beginning of the HTML string
  361. *
  362. * @param string
  363. * @since 1.4
  364. * @access public
  365. * @return void
  366. */
  367. function setComment($comment)
  368. {
  369. $this->_comment = $comment;
  370. } // end func setHtmlComment
  371. /**
  372. * Returns the HTML comment
  373. *
  374. * @since 1.5
  375. * @access public
  376. * @return string
  377. */
  378. function getComment()
  379. {
  380. return $this->_comment;
  381. } //end func getComment
  382. /**
  383. * Abstract method. Must be extended to return the objects HTML
  384. *
  385. * @access public
  386. * @return string
  387. * @abstract
  388. */
  389. function toHtml()
  390. {
  391. return '';
  392. } // end func toHtml
  393. /**
  394. * Displays the HTML to the screen
  395. *
  396. * @access public
  397. */
  398. function display()
  399. {
  400. print $this->toHtml();
  401. } // end func display
  402. /**
  403. * Sets the charset to use by htmlspecialchars() function
  404. *
  405. * Since this parameter is expected to be global, the function is designed
  406. * to be called statically:
  407. * <code>
  408. * HTML_Common::charset('utf-8');
  409. * </code>
  410. * or
  411. * <code>
  412. * $charset = HTML_Common::charset();
  413. * </code>
  414. *
  415. * @param string New charset to use. Omit if just getting the
  416. * current value. Consult the htmlspecialchars() docs
  417. * for a list of supported character sets.
  418. * @return string Current charset
  419. * @access public
  420. * @static
  421. */
  422. function charset($newCharset = null)
  423. {
  424. static $charset = 'ISO-8859-1';
  425. if (!is_null($newCharset)) {
  426. $charset = $newCharset;
  427. }
  428. return $charset;
  429. } // end func charset
  430. } // end class HTML_Common
  431. ?>