PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/HTML/Common.php

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