Common.php 13 KB

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