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

/plugins/rss/vendor/simplepie/SimplePie/Sanitize.php

https://github.com/ushahidi/Sweeper
PHP | 400 lines | 308 code | 30 blank | 62 comment | 36 complexity | ee4065226ee8e8dec787f2b0ad935c58 MD5 | raw file
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2009, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @version 1.3-dev
  37. * @copyright 2004-2010 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. * @todo phpDoc comments
  44. */
  45. /**
  46. * @todo Move to using an actual HTML parser (this will allow tags to be properly stripped, and to switch between HTML and XHTML), this will also make it easier to shorten a string while preserving HTML tags
  47. */
  48. class SimplePie_Sanitize
  49. {
  50. // Private vars
  51. var $base;
  52. // Options
  53. var $remove_div = true;
  54. var $image_handler = '';
  55. var $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
  56. var $encode_instead_of_strip = false;
  57. var $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc');
  58. var $strip_comments = false;
  59. var $output_encoding = 'UTF-8';
  60. var $enable_cache = true;
  61. var $cache_location = './cache';
  62. var $cache_name_function = 'md5';
  63. var $cache_class = 'SimplePie_Cache';
  64. var $file_class = 'SimplePie_File';
  65. var $timeout = 10;
  66. var $useragent = '';
  67. var $force_fsockopen = false;
  68. var $replace_url_attributes = array(
  69. 'a' => 'href',
  70. 'area' => 'href',
  71. 'blockquote' => 'cite',
  72. 'del' => 'cite',
  73. 'form' => 'action',
  74. 'img' => array('longdesc', 'src'),
  75. 'input' => 'src',
  76. 'ins' => 'cite',
  77. 'q' => 'cite'
  78. );
  79. public function remove_div($enable = true)
  80. {
  81. $this->remove_div = (bool) $enable;
  82. }
  83. public function set_image_handler($page = false)
  84. {
  85. if ($page)
  86. {
  87. $this->image_handler = (string) $page;
  88. }
  89. else
  90. {
  91. $this->image_handler = false;
  92. }
  93. }
  94. public function pass_cache_data($enable_cache = true, $cache_location = './cache', $cache_name_function = 'md5', $cache_class = 'SimplePie_Cache')
  95. {
  96. if (isset($enable_cache))
  97. {
  98. $this->enable_cache = (bool) $enable_cache;
  99. }
  100. if ($cache_location)
  101. {
  102. $this->cache_location = (string) $cache_location;
  103. }
  104. if ($cache_name_function)
  105. {
  106. $this->cache_name_function = (string) $cache_name_function;
  107. }
  108. if ($cache_class)
  109. {
  110. $this->cache_class = (string) $cache_class;
  111. }
  112. }
  113. public function pass_file_data($file_class = 'SimplePie_File', $timeout = 10, $useragent = '', $force_fsockopen = false)
  114. {
  115. if ($file_class)
  116. {
  117. $this->file_class = (string) $file_class;
  118. }
  119. if ($timeout)
  120. {
  121. $this->timeout = (string) $timeout;
  122. }
  123. if ($useragent)
  124. {
  125. $this->useragent = (string) $useragent;
  126. }
  127. if ($force_fsockopen)
  128. {
  129. $this->force_fsockopen = (string) $force_fsockopen;
  130. }
  131. }
  132. public function strip_htmltags($tags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style'))
  133. {
  134. if ($tags)
  135. {
  136. if (is_array($tags))
  137. {
  138. $this->strip_htmltags = $tags;
  139. }
  140. else
  141. {
  142. $this->strip_htmltags = explode(',', $tags);
  143. }
  144. }
  145. else
  146. {
  147. $this->strip_htmltags = false;
  148. }
  149. }
  150. public function encode_instead_of_strip($encode = false)
  151. {
  152. $this->encode_instead_of_strip = (bool) $encode;
  153. }
  154. public function strip_attributes($attribs = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'))
  155. {
  156. if ($attribs)
  157. {
  158. if (is_array($attribs))
  159. {
  160. $this->strip_attributes = $attribs;
  161. }
  162. else
  163. {
  164. $this->strip_attributes = explode(',', $attribs);
  165. }
  166. }
  167. else
  168. {
  169. $this->strip_attributes = false;
  170. }
  171. }
  172. public function strip_comments($strip = false)
  173. {
  174. $this->strip_comments = (bool) $strip;
  175. }
  176. public function set_output_encoding($encoding = 'UTF-8')
  177. {
  178. $this->output_encoding = (string) $encoding;
  179. }
  180. /**
  181. * Set element/attribute key/value pairs of HTML attributes
  182. * containing URLs that need to be resolved relative to the feed
  183. *
  184. * @access public
  185. * @since 1.0
  186. * @param array $element_attribute Element/attribute key/value pairs
  187. */
  188. public function set_url_replacements($element_attribute = array('a' => 'href', 'area' => 'href', 'blockquote' => 'cite', 'del' => 'cite', 'form' => 'action', 'img' => array('longdesc', 'src'), 'input' => 'src', 'ins' => 'cite', 'q' => 'cite'))
  189. {
  190. $this->replace_url_attributes = (array) $element_attribute;
  191. }
  192. public function sanitize($data, $type, $base = '')
  193. {
  194. $data = trim($data);
  195. if ($data !== '' || $type & SIMPLEPIE_CONSTRUCT_IRI)
  196. {
  197. if ($type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML)
  198. {
  199. if (preg_match('/(&(#(x[0-9a-fA-F]+|[0-9]+)|[a-zA-Z0-9]+)|<\/[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>)/', $data))
  200. {
  201. $type |= SIMPLEPIE_CONSTRUCT_HTML;
  202. }
  203. else
  204. {
  205. $type |= SIMPLEPIE_CONSTRUCT_TEXT;
  206. }
  207. }
  208. if ($type & SIMPLEPIE_CONSTRUCT_BASE64)
  209. {
  210. $data = base64_decode($data);
  211. }
  212. if ($type & SIMPLEPIE_CONSTRUCT_XHTML)
  213. {
  214. if ($this->remove_div)
  215. {
  216. $data = preg_replace('/^<div' . SIMPLEPIE_PCRE_XML_ATTRIBUTE . '>/', '', $data);
  217. $data = preg_replace('/<\/div>$/', '', $data);
  218. }
  219. else
  220. {
  221. $data = preg_replace('/^<div' . SIMPLEPIE_PCRE_XML_ATTRIBUTE . '>/', '<div>', $data);
  222. }
  223. }
  224. if ($type & (SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML))
  225. {
  226. // Strip comments
  227. if ($this->strip_comments)
  228. {
  229. $data = SimplePie_Misc::strip_comments($data);
  230. }
  231. // Strip out HTML tags and attributes that might cause various security problems.
  232. // Based on recommendations by Mark Pilgrim at:
  233. // http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely
  234. if ($this->strip_htmltags)
  235. {
  236. foreach ($this->strip_htmltags as $tag)
  237. {
  238. $pcre = "/<($tag)" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$tag" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>|(\/)?>)/siU';
  239. while (preg_match($pcre, $data))
  240. {
  241. $data = preg_replace_callback($pcre, array(&$this, 'do_strip_htmltags'), $data);
  242. }
  243. }
  244. }
  245. if ($this->strip_attributes)
  246. {
  247. foreach ($this->strip_attributes as $attrib)
  248. {
  249. $data = preg_replace('/(<[A-Za-z][^\x09\x0A\x0B\x0C\x0D\x20\x2F\x3E]*)' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . trim($attrib) . '(?:\s*=\s*(?:"(?:[^"]*)"|\'(?:[^\']*)\'|(?:[^\x09\x0A\x0B\x0C\x0D\x20\x22\x27\x3E][^\x09\x0A\x0B\x0C\x0D\x20\x3E]*)?))?' . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>/', '\1\2\3>', $data);
  250. }
  251. }
  252. // Replace relative URLs
  253. $this->base = $base;
  254. foreach ($this->replace_url_attributes as $element => $attributes)
  255. {
  256. $data = $this->replace_urls($data, $element, $attributes);
  257. }
  258. // If image handling (caching, etc.) is enabled, cache and rewrite all the image tags.
  259. if (isset($this->image_handler) && ((string) $this->image_handler) !== '' && $this->enable_cache)
  260. {
  261. $images = SimplePie_Misc::get_element('img', $data);
  262. foreach ($images as $img)
  263. {
  264. if (isset($img['attribs']['src']['data']))
  265. {
  266. $image_url = call_user_func($this->cache_name_function, $img['attribs']['src']['data']);
  267. $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, $image_url, 'spi');
  268. if ($cache->load())
  269. {
  270. $img['attribs']['src']['data'] = $this->image_handler . $image_url;
  271. $data = str_replace($img['full'], SimplePie_Misc::element_implode($img), $data);
  272. }
  273. else
  274. {
  275. $file = new $this->file_class($img['attribs']['src']['data'], $this->timeout, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen);
  276. $headers = $file->headers;
  277. if ($file->success && ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
  278. {
  279. if ($cache->save(array('headers' => $file->headers, 'body' => $file->body)))
  280. {
  281. $img['attribs']['src']['data'] = $this->image_handler . $image_url;
  282. $data = str_replace($img['full'], SimplePie_Misc::element_implode($img), $data);
  283. }
  284. else
  285. {
  286. trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. // Having (possibly) taken stuff out, there may now be whitespace at the beginning/end of the data
  294. $data = trim($data);
  295. }
  296. if ($type & SIMPLEPIE_CONSTRUCT_IRI)
  297. {
  298. $data = SimplePie_Misc::absolutize_url($data, $base);
  299. }
  300. if ($type & (SIMPLEPIE_CONSTRUCT_TEXT | SIMPLEPIE_CONSTRUCT_IRI))
  301. {
  302. $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
  303. }
  304. if ($this->output_encoding !== 'UTF-8')
  305. {
  306. $data = SimplePie_Misc::change_encoding($data, 'UTF-8', $this->output_encoding);
  307. }
  308. }
  309. return $data;
  310. }
  311. public function replace_urls($data, $tag, $attributes)
  312. {
  313. if (!is_array($this->strip_htmltags) || !in_array($tag, $this->strip_htmltags))
  314. {
  315. $elements = SimplePie_Misc::get_element($tag, $data);
  316. foreach ($elements as $element)
  317. {
  318. if (is_array($attributes))
  319. {
  320. foreach ($attributes as $attribute)
  321. {
  322. if (isset($element['attribs'][$attribute]['data']))
  323. {
  324. $element['attribs'][$attribute]['data'] = SimplePie_Misc::absolutize_url($element['attribs'][$attribute]['data'], $this->base);
  325. $new_element = SimplePie_Misc::element_implode($element);
  326. $data = str_replace($element['full'], $new_element, $data);
  327. $element['full'] = $new_element;
  328. }
  329. }
  330. }
  331. elseif (isset($element['attribs'][$attributes]['data']))
  332. {
  333. $element['attribs'][$attributes]['data'] = SimplePie_Misc::absolutize_url($element['attribs'][$attributes]['data'], $this->base);
  334. $data = str_replace($element['full'], SimplePie_Misc::element_implode($element), $data);
  335. }
  336. }
  337. }
  338. return $data;
  339. }
  340. public function do_strip_htmltags($match)
  341. {
  342. if ($this->encode_instead_of_strip)
  343. {
  344. if (isset($match[4]) && !in_array(strtolower($match[1]), array('script', 'style')))
  345. {
  346. $match[1] = htmlspecialchars($match[1], ENT_COMPAT, 'UTF-8');
  347. $match[2] = htmlspecialchars($match[2], ENT_COMPAT, 'UTF-8');
  348. return "&lt;$match[1]$match[2]&gt;$match[3]&lt;/$match[1]&gt;";
  349. }
  350. else
  351. {
  352. return htmlspecialchars($match[0], ENT_COMPAT, 'UTF-8');
  353. }
  354. }
  355. elseif (isset($match[4]) && !in_array(strtolower($match[1]), array('script', 'style')))
  356. {
  357. return $match[4];
  358. }
  359. else
  360. {
  361. return '';
  362. }
  363. }
  364. }