PageRenderTime 65ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 518 lines | 396 code | 21 blank | 101 comment | 5 complexity | 0a4080f0018e4445a9819b35db9e5ecd 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. {
  130. $charset = HTML_Common :: charset();
  131. foreach ($attributes as $key => $value)
  132. {
  133. $strAttr .= ' ' . $key . '="' . htmlspecialchars($value, ENT_COMPAT, $charset) . '"';
  134. }
  135. }
  136. return $strAttr;
  137. } // end func _getAttrString
  138. /**
  139. * Returns a valid atrributes array from either a string or array
  140. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  141. * @access private
  142. * @return array
  143. */
  144. function _parseAttributes($attributes)
  145. {
  146. if (is_array($attributes))
  147. {
  148. $ret = array();
  149. foreach ($attributes as $key => $value)
  150. {
  151. if (is_int($key))
  152. {
  153. $key = $value = strtolower($value);
  154. }
  155. else
  156. {
  157. $key = strtolower($key);
  158. }
  159. $ret[$key] = $value;
  160. }
  161. return $ret;
  162. }
  163. elseif (is_string($attributes))
  164. {
  165. $preg = "/(([A-Za-z_:]|[^\\x00-\\x7F])([A-Za-z0-9_:.-]|[^\\x00-\\x7F])*)" . "([ \\n\\t\\r]+)?(=([ \\n\\t\\r]+)?(\"[^\"]*\"|'[^']*'|[^ \\n\\t\\r]*))?/";
  166. if (preg_match_all($preg, $attributes, $regs))
  167. {
  168. for($counter = 0; $counter < count($regs[1]); $counter ++)
  169. {
  170. $name = $regs[1][$counter];
  171. $check = $regs[0][$counter];
  172. $value = $regs[7][$counter];
  173. if (trim($name) == trim($check))
  174. {
  175. $arrAttr[strtolower(trim($name))] = strtolower(trim($name));
  176. }
  177. else
  178. {
  179. if (substr($value, 0, 1) == "\"" || substr($value, 0, 1) == "'")
  180. {
  181. $arrAttr[strtolower(trim($name))] = substr($value, 1, - 1);
  182. }
  183. else
  184. {
  185. $arrAttr[strtolower(trim($name))] = trim($value);
  186. }
  187. }
  188. }
  189. return $arrAttr;
  190. }
  191. }
  192. } // end func _parseAttributes
  193. /**
  194. * Returns the array key for the given non-name-value pair attribute
  195. *
  196. * @param string $attr Attribute
  197. * @param array $attributes Array of attribute
  198. * @since 1.0
  199. * @access private
  200. * @return bool
  201. */
  202. function _getAttrKey($attr, $attributes)
  203. {
  204. if (isset($attributes[strtolower($attr)]))
  205. {
  206. return true;
  207. }
  208. else
  209. {
  210. return null;
  211. }
  212. } //end func _getAttrKey
  213. /**
  214. * Updates the attributes in $attr1 with the values in $attr2 without changing the other existing attributes
  215. * @param array $attr1 Original attributes array
  216. * @param array $attr2 New attributes array
  217. * @access private
  218. */
  219. function _updateAttrArray(&$attr1, $attr2)
  220. {
  221. if (! is_array($attr2))
  222. {
  223. return false;
  224. }
  225. foreach ($attr2 as $key => $value)
  226. {
  227. $attr1[$key] = $value;
  228. }
  229. } // end func _updateAtrrArray
  230. /**
  231. * Removes the given attribute from the given array
  232. *
  233. * @param string $attr Attribute name
  234. * @param array $attributes Attribute array
  235. * @since 1.4
  236. * @access private
  237. * @return void
  238. */
  239. function _removeAttr($attr, &$attributes)
  240. {
  241. $attr = strtolower($attr);
  242. if (isset($attributes[$attr]))
  243. {
  244. unset($attributes[$attr]);
  245. }
  246. } //end func _removeAttr
  247. /**
  248. * Returns the value of the given attribute
  249. *
  250. * @param string $attr Attribute name
  251. * @since 1.5
  252. * @access public
  253. * @return string|null returns null if an attribute does not exist
  254. */
  255. function getAttribute($attr)
  256. {
  257. $attr = strtolower($attr);
  258. if (isset($this->_attributes[$attr]))
  259. {
  260. return $this->_attributes[$attr];
  261. }
  262. return null;
  263. } //end func getAttribute
  264. /**
  265. * Sets the value of the attribute
  266. *
  267. * @param string Attribute name
  268. * @param string Attribute value (will be set to $name if omitted)
  269. * @access public
  270. */
  271. function setAttribute($name, $value = null)
  272. {
  273. $name = strtolower($name);
  274. if (is_null($value))
  275. {
  276. $value = $name;
  277. }
  278. $this->_attributes[$name] = $value;
  279. } // end func setAttribute
  280. /**
  281. * Sets the HTML attributes
  282. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  283. * @access public
  284. */
  285. function setAttributes($attributes)
  286. {
  287. $this->_attributes = $this->_parseAttributes($attributes);
  288. } // end func setAttributes
  289. /**
  290. * Returns the assoc array (default) or string of attributes
  291. *
  292. * @param bool Whether to return the attributes as string
  293. * @since 1.6
  294. * @access public
  295. * @return mixed attributes
  296. */
  297. function getAttributes($asString = false)
  298. {
  299. if ($asString)
  300. {
  301. return $this->_getAttrString($this->_attributes);
  302. }
  303. else
  304. {
  305. return $this->_attributes;
  306. }
  307. } //end func getAttributes
  308. /**
  309. * Updates the passed attributes without changing the other existing attributes
  310. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  311. * @access public
  312. */
  313. function updateAttributes($attributes)
  314. {
  315. $this->_updateAttrArray($this->_attributes, $this->_parseAttributes($attributes));
  316. } // end func updateAttributes
  317. /**
  318. * Removes an attribute
  319. *
  320. * @param string $attr Attribute name
  321. * @since 1.4
  322. * @access public
  323. * @return void
  324. */
  325. function removeAttribute($attr)
  326. {
  327. $this->_removeAttr($attr, $this->_attributes);
  328. } //end func removeAttribute
  329. /**
  330. * Sets the line end style to Windows, Mac, Unix or a custom string.
  331. *
  332. * @param string $style "win", "mac", "unix" or custom string.
  333. * @since 1.7
  334. * @access public
  335. * @return void
  336. */
  337. function setLineEnd($style)
  338. {
  339. switch ($style)
  340. {
  341. case 'win' :
  342. $this->_lineEnd = "\15\12";
  343. break;
  344. case 'unix' :
  345. $this->_lineEnd = "\12";
  346. break;
  347. case 'mac' :
  348. $this->_lineEnd = "\15";
  349. break;
  350. default :
  351. $this->_lineEnd = $style;
  352. }
  353. } // end func setLineEnd
  354. /**
  355. * Sets the tab offset
  356. *
  357. * @param int $offset
  358. * @access public
  359. */
  360. function setTabOffset($offset)
  361. {
  362. $this->_tabOffset = $offset;
  363. } // end func setTabOffset
  364. /**
  365. * Returns the tabOffset
  366. *
  367. * @since 1.5
  368. * @access public
  369. * @return int
  370. */
  371. function getTabOffset()
  372. {
  373. return $this->_tabOffset;
  374. } //end func getTabOffset
  375. /**
  376. * Sets the string used to indent HTML
  377. *
  378. * @since 1.7
  379. * @param string $string String used to indent ("\11", "\t", ' ', etc.).
  380. * @access public
  381. * @return void
  382. */
  383. function setTab($string)
  384. {
  385. $this->_tab = $string;
  386. } // end func setTab
  387. /**
  388. * Sets the HTML comment to be displayed at the beginning of the HTML string
  389. *
  390. * @param string
  391. * @since 1.4
  392. * @access public
  393. * @return void
  394. */
  395. function setComment($comment)
  396. {
  397. $this->_comment = $comment;
  398. } // end func setHtmlComment
  399. /**
  400. * Returns the HTML comment
  401. *
  402. * @since 1.5
  403. * @access public
  404. * @return string
  405. */
  406. function getComment()
  407. {
  408. return $this->_comment;
  409. } //end func getComment
  410. /**
  411. * Abstract method. Must be extended to return the objects HTML
  412. *
  413. * @access public
  414. * @return string
  415. * @abstract
  416. */
  417. function toHtml()
  418. {
  419. return '';
  420. } // end func toHtml
  421. /**
  422. * Displays the HTML to the screen
  423. *
  424. * @access public
  425. */
  426. function display()
  427. {
  428. print $this->toHtml();
  429. } // end func display
  430. /**
  431. * Sets the charset to use by htmlspecialchars() function
  432. *
  433. * Since this parameter is expected to be global, the function is designed
  434. * to be called statically:
  435. * <code>
  436. * HTML_Common::charset('utf-8');
  437. * </code>
  438. * or
  439. * <code>
  440. * $charset = HTML_Common::charset();
  441. * </code>
  442. *
  443. * @param string New charset to use. Omit if just getting the
  444. * current value. Consult the htmlspecialchars() docs
  445. * for a list of supported character sets.
  446. * @return string Current charset
  447. * @access public
  448. * @static
  449. */
  450. function charset($newCharset = null)
  451. {
  452. static $charset = 'ISO-8859-1';
  453. if (! is_null($newCharset))
  454. {
  455. $charset = $newCharset;
  456. }
  457. return $charset;
  458. } // end func charset
  459. } // end class HTML_Common
  460. ?>