Html.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Markup
  17. * @subpackage Renderer
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Filter_HtmlEntities
  24. */
  25. require_once 'Zend/Filter/HtmlEntities.php';
  26. /**
  27. * @see Zend_Filter_PregReplace
  28. */
  29. require_once 'Zend/Filter/PregReplace.php';
  30. /**
  31. * @see Zend_Filter_Callback
  32. */
  33. require_once 'Zend/Filter/Callback.php';
  34. /**
  35. * @see Zend_Markup_Renderer_RendererAbstract
  36. */
  37. require_once 'Zend/Markup/Renderer/RendererAbstract.php';
  38. /**
  39. * HTML renderer
  40. *
  41. * @category Zend
  42. * @package Zend_Markup
  43. * @subpackage Renderer
  44. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  45. * @license http://framework.zend.com/license/new-bsd New BSD License
  46. */
  47. class Zend_Markup_Renderer_Html extends Zend_Markup_Renderer_RendererAbstract
  48. {
  49. /**
  50. * Element groups
  51. *
  52. * @var array
  53. */
  54. protected $_groups = array(
  55. 'block' => array('block', 'inline', 'block-empty', 'inline-empty', 'list'),
  56. 'inline' => array('inline', 'inline-empty'),
  57. 'list' => array('list-item'),
  58. 'list-item' => array('inline', 'inline-empty', 'list'),
  59. 'block-empty' => array(),
  60. 'inline-empty' => array(),
  61. );
  62. /**
  63. * The current group
  64. *
  65. * @var string
  66. */
  67. protected $_group = 'block';
  68. /**
  69. * Default attributes
  70. *
  71. * @var array
  72. */
  73. protected static $_defaultAttributes = array(
  74. 'id' => '',
  75. 'class' => '',
  76. 'style' => '',
  77. 'lang' => '',
  78. 'title' => ''
  79. );
  80. /**
  81. * Constructor
  82. *
  83. * @param array|Zend_Config $options
  84. *
  85. * @return void
  86. */
  87. public function __construct($options = array())
  88. {
  89. if ($options instanceof Zend_Config) {
  90. $options = $options->toArray();
  91. }
  92. $this->_pluginLoader = new Zend_Loader_PluginLoader(array(
  93. 'Zend_Markup_Renderer_Html' => 'Zend/Markup/Renderer/Html/'
  94. ));
  95. $this->_defineDefaultMarkups();
  96. parent::__construct($options);
  97. }
  98. /**
  99. * Define the default markups
  100. *
  101. * @return void
  102. */
  103. protected function _defineDefaultMarkups()
  104. {
  105. $this->_markups = array(
  106. 'b' => array(
  107. 'type' => 10, // self::TYPE_REPLACE | self::TAG_NORMAL
  108. 'tag' => 'strong',
  109. 'group' => 'inline',
  110. 'filter' => true,
  111. ),
  112. 'u' => array(
  113. 'type' => 10,
  114. 'tag' => 'span',
  115. 'attributes' => array(
  116. 'style' => 'text-decoration: underline;',
  117. ),
  118. 'group' => 'inline',
  119. 'filter' => true,
  120. ),
  121. 'i' => array(
  122. 'type' => 10,
  123. 'tag' => 'em',
  124. 'group' => 'inline',
  125. 'filter' => true,
  126. ),
  127. 'cite' => array(
  128. 'type' => 10,
  129. 'tag' => 'cite',
  130. 'group' => 'inline',
  131. 'filter' => true,
  132. ),
  133. 'del' => array(
  134. 'type' => 10,
  135. 'tag' => 'del',
  136. 'group' => 'inline',
  137. 'filter' => true,
  138. ),
  139. 'ins' => array(
  140. 'type' => 10,
  141. 'tag' => 'ins',
  142. 'group' => 'inline',
  143. 'filter' => true,
  144. ),
  145. 'sub' => array(
  146. 'type' => 10,
  147. 'tag' => 'sub',
  148. 'group' => 'inline',
  149. 'filter' => true,
  150. ),
  151. 'sup' => array(
  152. 'type' => 10,
  153. 'tag' => 'sup',
  154. 'group' => 'inline',
  155. 'filter' => true,
  156. ),
  157. 'span' => array(
  158. 'type' => 10,
  159. 'tag' => 'span',
  160. 'group' => 'inline',
  161. 'filter' => true,
  162. ),
  163. 'acronym' => array(
  164. 'type' => 10,
  165. 'tag' => 'acronym',
  166. 'group' => 'inline',
  167. 'filter' => true,
  168. ),
  169. // headings
  170. 'h1' => array(
  171. 'type' => 10,
  172. 'tag' => 'h1',
  173. 'group' => 'inline',
  174. 'filter' => true,
  175. ),
  176. 'h2' => array(
  177. 'type' => 10,
  178. 'tag' => 'h2',
  179. 'group' => 'inline',
  180. 'filter' => true,
  181. ),
  182. 'h3' => array(
  183. 'type' => 10,
  184. 'tag' => 'h3',
  185. 'group' => 'inline',
  186. 'filter' => true,
  187. ),
  188. 'h4' => array(
  189. 'type' => 10,
  190. 'tag' => 'h4',
  191. 'group' => 'inline',
  192. 'filter' => true,
  193. ),
  194. 'h5' => array(
  195. 'type' => 10,
  196. 'tag' => 'h5',
  197. 'group' => 'inline',
  198. 'filter' => true,
  199. ),
  200. 'h6' => array(
  201. 'type' => 10,
  202. 'tag' => 'h6',
  203. 'group' => 'inline',
  204. 'filter' => true,
  205. ),
  206. // callback tags
  207. 'url' => array(
  208. 'type' => 6, // self::TYPE_CALLBACK | self::TAG_NORMAL
  209. 'callback' => null,
  210. 'group' => 'inline',
  211. 'filter' => true,
  212. ),
  213. 'img' => array(
  214. 'type' => 6,
  215. 'callback' => null,
  216. 'group' => 'inline-empty',
  217. 'filter' => true,
  218. ),
  219. 'code' => array(
  220. 'type' => 6,
  221. 'callback' => null,
  222. 'group' => 'block-empty',
  223. 'filter' => false,
  224. ),
  225. 'p' => array(
  226. 'type' => 10,
  227. 'tag' => 'p',
  228. 'group' => 'block',
  229. 'filter' => true,
  230. ),
  231. 'ignore' => array(
  232. 'type' => 10,
  233. 'start' => '',
  234. 'end' => '',
  235. 'group' => 'block-empty',
  236. 'filter' => true,
  237. ),
  238. 'quote' => array(
  239. 'type' => 10,
  240. 'tag' => 'blockquote',
  241. 'group' => 'block',
  242. 'filter' => true,
  243. ),
  244. 'list' => array(
  245. 'type' => 6,
  246. 'callback' => null,
  247. 'group' => 'list',
  248. 'filter' => new Zend_Filter_PregReplace('/.*/is', ''),
  249. ),
  250. '*' => array(
  251. 'type' => 10,
  252. 'tag' => 'li',
  253. 'group' => 'list-item',
  254. 'filter' => true,
  255. ),
  256. 'hr' => array(
  257. 'type' => 9, // self::TYPE_REPLACE | self::TAG_SINGLE
  258. 'tag' => 'hr',
  259. 'group' => 'block',
  260. 'empty' => true,
  261. ),
  262. // aliases
  263. 'bold' => array(
  264. 'type' => 16,
  265. 'name' => 'b',
  266. ),
  267. 'strong' => array(
  268. 'type' => 16,
  269. 'name' => 'b',
  270. ),
  271. 'italic' => array(
  272. 'type' => 16,
  273. 'name' => 'i',
  274. ),
  275. 'em' => array(
  276. 'type' => 16,
  277. 'name' => 'i',
  278. ),
  279. 'emphasized' => array(
  280. 'type' => 16,
  281. 'name' => 'i',
  282. ),
  283. 'underline' => array(
  284. 'type' => 16,
  285. 'name' => 'u',
  286. ),
  287. 'citation' => array(
  288. 'type' => 16,
  289. 'name' => 'cite',
  290. ),
  291. 'deleted' => array(
  292. 'type' => 16,
  293. 'name' => 'del',
  294. ),
  295. 'insert' => array(
  296. 'type' => 16,
  297. 'name' => 'ins',
  298. ),
  299. 'strike' => array(
  300. 'type' => 16,
  301. 'name' => 's',
  302. ),
  303. 's' => array(
  304. 'type' => 16,
  305. 'name' => 'del',
  306. ),
  307. 'subscript' => array(
  308. 'type' => 16,
  309. 'name' => 'sub',
  310. ),
  311. 'superscript' => array(
  312. 'type' => 16,
  313. 'name' => 'sup',
  314. ),
  315. 'a' => array(
  316. 'type' => 16,
  317. 'name' => 'url',
  318. ),
  319. 'image' => array(
  320. 'type' => 16,
  321. 'name' => 'img',
  322. ),
  323. 'li' => array(
  324. 'type' => 16,
  325. 'name' => '*',
  326. ),
  327. 'color' => array(
  328. 'type' => 16,
  329. 'name' => 'span',
  330. ),
  331. );
  332. }
  333. /**
  334. * Add the default filters
  335. *
  336. * @return void
  337. */
  338. public function addDefaultFilters()
  339. {
  340. $this->_defaultFilter = new Zend_Filter();
  341. $this->_defaultFilter->addFilter(new Zend_Filter_HtmlEntities(array('encoding' => self::getEncoding())));
  342. $this->_defaultFilter->addFilter(new Zend_Filter_Callback('nl2br'));
  343. }
  344. /**
  345. * Execute a replace token
  346. *
  347. * @param Zend_Markup_Token $token
  348. * @param array $markup
  349. * @return string
  350. */
  351. protected function _executeReplace(Zend_Markup_Token $token, $markup)
  352. {
  353. if (isset($markup['tag'])) {
  354. if (!isset($markup['attributes'])) {
  355. $markup['attributes'] = array();
  356. }
  357. $attrs = self::renderAttributes($token, $markup['attributes']);
  358. return "<{$markup['tag']}{$attrs}>{$this->_render($token)}</{$markup['tag']}>";
  359. }
  360. return parent::_executeReplace($token, $markup);
  361. }
  362. /**
  363. * Execute a single replace token
  364. *
  365. * @param Zend_Markup_Token $token
  366. * @param array $markup
  367. * @return string
  368. */
  369. protected function _executeSingleReplace(Zend_Markup_Token $token, $markup)
  370. {
  371. if (isset($markup['tag'])) {
  372. if (!isset($markup['attributes'])) {
  373. $markup['attributes'] = array();
  374. }
  375. $attrs = self::renderAttributes($token, $markup['attributes']);
  376. return "<{$markup['tag']}{$attrs} />";
  377. }
  378. return parent::_executeSingleReplace($token, $markup);
  379. }
  380. /**
  381. * Render some attributes
  382. *
  383. * @param Zend_Markup_Token $token
  384. * @param array $attributes
  385. * @return string
  386. */
  387. public static function renderAttributes(Zend_Markup_Token $token, array $attributes = array())
  388. {
  389. $attributes = array_merge(self::$_defaultAttributes, $attributes);
  390. $return = '';
  391. $tokenAttributes = $token->getAttributes();
  392. // correct style attribute
  393. if (isset($tokenAttributes['style'])) {
  394. $tokenAttributes['style'] = trim($tokenAttributes['style']);
  395. if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') {
  396. $tokenAttributes['style'] .= ';';
  397. }
  398. } else {
  399. $tokenAttributes['style'] = '';
  400. }
  401. // special treathment for 'align' and 'color' attribute
  402. if (isset($tokenAttributes['align'])) {
  403. $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';';
  404. unset($tokenAttributes['align']);
  405. }
  406. if (isset($tokenAttributes['color']) && self::checkColor($tokenAttributes['color'])) {
  407. $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';';
  408. unset($tokenAttributes['color']);
  409. }
  410. /*
  411. * loop through all the available attributes, and check if there is
  412. * a value defined by the token
  413. * if there is no value defined by the token, use the default value or
  414. * don't set the attribute
  415. */
  416. foreach ($attributes as $attribute => $value) {
  417. if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) {
  418. $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute],
  419. ENT_QUOTES,
  420. self::getEncoding()) . '"';
  421. } elseif (!empty($value)) {
  422. $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES, self::getEncoding()) . '"';
  423. }
  424. }
  425. return $return;
  426. }
  427. /**
  428. * Check if a color is a valid HTML color
  429. *
  430. * @param string $color
  431. *
  432. * @return bool
  433. */
  434. public static function checkColor($color)
  435. {
  436. /*
  437. * aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
  438. * purple, red, silver, teal, white, and yellow.
  439. */
  440. $colors = array(
  441. 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
  442. 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal',
  443. 'white', 'yellow'
  444. );
  445. if (in_array($color, $colors)) {
  446. return true;
  447. }
  448. if (preg_match('/\#[0-9a-f]{6}/i', $color)) {
  449. return true;
  450. }
  451. return false;
  452. }
  453. /**
  454. * Check if the URI is valid
  455. *
  456. * @param string $uri
  457. *
  458. * @return bool
  459. */
  460. public static function isValidUri($uri)
  461. {
  462. if (!preg_match('/^([a-z][a-z+\-.]*):/i', $uri, $matches)) {
  463. return false;
  464. }
  465. $scheme = strtolower($matches[1]);
  466. switch ($scheme) {
  467. case 'javascript':
  468. // JavaScript scheme is not allowed for security reason
  469. return false;
  470. case 'http':
  471. case 'https':
  472. case 'ftp':
  473. $components = @parse_url($uri);
  474. if ($components === false) {
  475. return false;
  476. }
  477. if (!isset($components['host'])) {
  478. return false;
  479. }
  480. return true;
  481. default:
  482. return true;
  483. }
  484. }
  485. }