Common.php 12 KB

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