Html.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. if (!isset($options['useDefaultMarkups']) && isset($options['useDefaultTags'])) {
  96. $options['useDefaultMarkups'] = $options['useDefaultTags'];
  97. }
  98. if (isset($options['useDefaultMarkups']) && ($options['useDefaultMarkups'] !== false)) {
  99. $this->_defineDefaultMarkups();
  100. }
  101. parent::__construct($options);
  102. }
  103. /**
  104. * Define the default markups
  105. *
  106. * @return void
  107. */
  108. protected function _defineDefaultMarkups()
  109. {
  110. $this->_markups = array(
  111. 'b' => array(
  112. 'type' => 10, // self::TYPE_REPLACE | self::TAG_NORMAL
  113. 'tag' => 'strong',
  114. 'group' => 'inline',
  115. 'filter' => true,
  116. ),
  117. 'u' => array(
  118. 'type' => 10,
  119. 'tag' => 'span',
  120. 'attributes' => array(
  121. 'style' => 'text-decoration: underline;',
  122. ),
  123. 'group' => 'inline',
  124. 'filter' => true,
  125. ),
  126. 'i' => array(
  127. 'type' => 10,
  128. 'tag' => 'em',
  129. 'group' => 'inline',
  130. 'filter' => true,
  131. ),
  132. 'cite' => array(
  133. 'type' => 10,
  134. 'tag' => 'cite',
  135. 'group' => 'inline',
  136. 'filter' => true,
  137. ),
  138. 'del' => array(
  139. 'type' => 10,
  140. 'tag' => 'del',
  141. 'group' => 'inline',
  142. 'filter' => true,
  143. ),
  144. 'ins' => array(
  145. 'type' => 10,
  146. 'tag' => 'ins',
  147. 'group' => 'inline',
  148. 'filter' => true,
  149. ),
  150. 'sub' => array(
  151. 'type' => 10,
  152. 'tag' => 'sub',
  153. 'group' => 'inline',
  154. 'filter' => true,
  155. ),
  156. 'sup' => array(
  157. 'type' => 10,
  158. 'tag' => 'sup',
  159. 'group' => 'inline',
  160. 'filter' => true,
  161. ),
  162. 'span' => array(
  163. 'type' => 10,
  164. 'tag' => 'span',
  165. 'group' => 'inline',
  166. 'filter' => true,
  167. ),
  168. 'acronym' => array(
  169. 'type' => 10,
  170. 'tag' => 'acronym',
  171. 'group' => 'inline',
  172. 'filter' => true,
  173. ),
  174. // headings
  175. 'h1' => array(
  176. 'type' => 10,
  177. 'tag' => 'h1',
  178. 'group' => 'inline',
  179. 'filter' => true,
  180. ),
  181. 'h2' => array(
  182. 'type' => 10,
  183. 'tag' => 'h2',
  184. 'group' => 'inline',
  185. 'filter' => true,
  186. ),
  187. 'h3' => array(
  188. 'type' => 10,
  189. 'tag' => 'h3',
  190. 'group' => 'inline',
  191. 'filter' => true,
  192. ),
  193. 'h4' => array(
  194. 'type' => 10,
  195. 'tag' => 'h4',
  196. 'group' => 'inline',
  197. 'filter' => true,
  198. ),
  199. 'h5' => array(
  200. 'type' => 10,
  201. 'tag' => 'h5',
  202. 'group' => 'inline',
  203. 'filter' => true,
  204. ),
  205. 'h6' => array(
  206. 'type' => 10,
  207. 'tag' => 'h6',
  208. 'group' => 'inline',
  209. 'filter' => true,
  210. ),
  211. // callback tags
  212. 'url' => array(
  213. 'type' => 6, // self::TYPE_CALLBACK | self::TAG_NORMAL
  214. 'callback' => null,
  215. 'group' => 'inline',
  216. 'filter' => true,
  217. ),
  218. 'img' => array(
  219. 'type' => 6,
  220. 'callback' => null,
  221. 'group' => 'inline-empty',
  222. 'filter' => true,
  223. ),
  224. 'code' => array(
  225. 'type' => 6,
  226. 'callback' => null,
  227. 'group' => 'block-empty',
  228. 'filter' => false,
  229. ),
  230. 'p' => array(
  231. 'type' => 10,
  232. 'tag' => 'p',
  233. 'group' => 'block',
  234. 'filter' => true,
  235. ),
  236. 'ignore' => array(
  237. 'type' => 10,
  238. 'start' => '',
  239. 'end' => '',
  240. 'group' => 'block-empty',
  241. 'filter' => true,
  242. ),
  243. 'quote' => array(
  244. 'type' => 10,
  245. 'tag' => 'blockquote',
  246. 'group' => 'block',
  247. 'filter' => true,
  248. ),
  249. 'list' => array(
  250. 'type' => 6,
  251. 'callback' => null,
  252. 'group' => 'list',
  253. 'filter' => new Zend_Filter_PregReplace('/.*/is', ''),
  254. ),
  255. '*' => array(
  256. 'type' => 10,
  257. 'tag' => 'li',
  258. 'group' => 'list-item',
  259. 'filter' => true,
  260. ),
  261. 'hr' => array(
  262. 'type' => 9, // self::TYPE_REPLACE | self::TAG_SINGLE
  263. 'tag' => 'hr',
  264. 'group' => 'block',
  265. 'empty' => true,
  266. ),
  267. // aliases
  268. 'bold' => array(
  269. 'type' => 16,
  270. 'name' => 'b',
  271. ),
  272. 'strong' => array(
  273. 'type' => 16,
  274. 'name' => 'b',
  275. ),
  276. 'italic' => array(
  277. 'type' => 16,
  278. 'name' => 'i',
  279. ),
  280. 'em' => array(
  281. 'type' => 16,
  282. 'name' => 'i',
  283. ),
  284. 'emphasized' => array(
  285. 'type' => 16,
  286. 'name' => 'i',
  287. ),
  288. 'underline' => array(
  289. 'type' => 16,
  290. 'name' => 'u',
  291. ),
  292. 'citation' => array(
  293. 'type' => 16,
  294. 'name' => 'cite',
  295. ),
  296. 'deleted' => array(
  297. 'type' => 16,
  298. 'name' => 'del',
  299. ),
  300. 'insert' => array(
  301. 'type' => 16,
  302. 'name' => 'ins',
  303. ),
  304. 'strike' => array(
  305. 'type' => 16,
  306. 'name' => 's',
  307. ),
  308. 's' => array(
  309. 'type' => 16,
  310. 'name' => 'del',
  311. ),
  312. 'subscript' => array(
  313. 'type' => 16,
  314. 'name' => 'sub',
  315. ),
  316. 'superscript' => array(
  317. 'type' => 16,
  318. 'name' => 'sup',
  319. ),
  320. 'a' => array(
  321. 'type' => 16,
  322. 'name' => 'url',
  323. ),
  324. 'image' => array(
  325. 'type' => 16,
  326. 'name' => 'img',
  327. ),
  328. 'li' => array(
  329. 'type' => 16,
  330. 'name' => '*',
  331. ),
  332. 'color' => array(
  333. 'type' => 16,
  334. 'name' => 'span',
  335. ),
  336. );
  337. }
  338. /**
  339. * Add the default filters
  340. *
  341. * @return void
  342. */
  343. public function addDefaultFilters()
  344. {
  345. $this->_defaultFilter = new Zend_Filter();
  346. $this->_defaultFilter->addFilter(new Zend_Filter_HtmlEntities(array('encoding' => self::getEncoding())));
  347. $this->_defaultFilter->addFilter(new Zend_Filter_Callback('nl2br'));
  348. }
  349. /**
  350. * Execute a replace token
  351. *
  352. * @param Zend_Markup_Token $token
  353. * @param array $markup
  354. * @return string
  355. */
  356. protected function _executeReplace(Zend_Markup_Token $token, $markup)
  357. {
  358. if (isset($markup['tag'])) {
  359. if (!isset($markup['attributes'])) {
  360. $markup['attributes'] = array();
  361. }
  362. $attrs = self::renderAttributes($token, $markup['attributes']);
  363. return "<{$markup['tag']}{$attrs}>{$this->_render($token)}</{$markup['tag']}>";
  364. }
  365. return parent::_executeReplace($token, $markup);
  366. }
  367. /**
  368. * Execute a single replace token
  369. *
  370. * @param Zend_Markup_Token $token
  371. * @param array $markup
  372. * @return string
  373. */
  374. protected function _executeSingleReplace(Zend_Markup_Token $token, $markup)
  375. {
  376. if (isset($markup['tag'])) {
  377. if (!isset($markup['attributes'])) {
  378. $markup['attributes'] = array();
  379. }
  380. $attrs = self::renderAttributes($token, $markup['attributes']);
  381. return "<{$markup['tag']}{$attrs} />";
  382. }
  383. return parent::_executeSingleReplace($token, $markup);
  384. }
  385. /**
  386. * Render some attributes
  387. *
  388. * @param Zend_Markup_Token $token
  389. * @param array $attributes
  390. * @return string
  391. */
  392. public static function renderAttributes(Zend_Markup_Token $token, array $attributes = array())
  393. {
  394. $attributes = array_merge(self::$_defaultAttributes, $attributes);
  395. $return = '';
  396. $tokenAttributes = $token->getAttributes();
  397. // correct style attribute
  398. if (isset($tokenAttributes['style'])) {
  399. $tokenAttributes['style'] = trim($tokenAttributes['style']);
  400. if ($tokenAttributes['style'][strlen($tokenAttributes['style']) - 1] != ';') {
  401. $tokenAttributes['style'] .= ';';
  402. }
  403. } else {
  404. $tokenAttributes['style'] = '';
  405. }
  406. // special treathment for 'align' and 'color' attribute
  407. if (isset($tokenAttributes['align'])) {
  408. $tokenAttributes['style'] .= 'text-align: ' . $tokenAttributes['align'] . ';';
  409. unset($tokenAttributes['align']);
  410. }
  411. if (isset($tokenAttributes['color']) && self::checkColor($tokenAttributes['color'])) {
  412. $tokenAttributes['style'] .= 'color: ' . $tokenAttributes['color'] . ';';
  413. unset($tokenAttributes['color']);
  414. }
  415. /*
  416. * loop through all the available attributes, and check if there is
  417. * a value defined by the token
  418. * if there is no value defined by the token, use the default value or
  419. * don't set the attribute
  420. */
  421. foreach ($attributes as $attribute => $value) {
  422. if (isset($tokenAttributes[$attribute]) && !empty($tokenAttributes[$attribute])) {
  423. $return .= ' ' . $attribute . '="' . htmlentities($tokenAttributes[$attribute],
  424. ENT_QUOTES,
  425. self::getEncoding()) . '"';
  426. } elseif (!empty($value)) {
  427. $return .= ' ' . $attribute . '="' . htmlentities($value, ENT_QUOTES, self::getEncoding()) . '"';
  428. }
  429. }
  430. return $return;
  431. }
  432. /**
  433. * Check if a color is a valid HTML color
  434. *
  435. * @param string $color
  436. *
  437. * @return bool
  438. */
  439. public static function checkColor($color)
  440. {
  441. /*
  442. * aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive,
  443. * purple, red, silver, teal, white, and yellow.
  444. */
  445. $colors = array(
  446. 'aqua', 'black', 'blue', 'fuchsia', 'gray', 'green', 'lime',
  447. 'maroon', 'navy', 'olive', 'purple', 'red', 'silver', 'teal',
  448. 'white', 'yellow'
  449. );
  450. if (in_array($color, $colors)) {
  451. return true;
  452. }
  453. if (preg_match('/\#[0-9a-f]{6}/i', $color)) {
  454. return true;
  455. }
  456. return false;
  457. }
  458. /**
  459. * Check if the URI is valid
  460. *
  461. * @param string $uri
  462. *
  463. * @return bool
  464. */
  465. public static function isValidUri($uri)
  466. {
  467. if (!preg_match('/^([a-z][a-z+\-.]*):/i', $uri, $matches)) {
  468. return false;
  469. }
  470. $scheme = strtolower($matches[1]);
  471. switch ($scheme) {
  472. case 'javascript':
  473. // JavaScript scheme is not allowed for security reason
  474. return false;
  475. case 'http':
  476. case 'https':
  477. case 'ftp':
  478. $components = @parse_url($uri);
  479. if ($components === false) {
  480. return false;
  481. }
  482. if (!isset($components['host'])) {
  483. return false;
  484. }
  485. return true;
  486. default:
  487. return true;
  488. }
  489. }
  490. }