Common.php 13 KB

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