PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/library/Simplepie/Sanitize.php

https://bitbucket.org/aukhanev/xdn-wordpress31
PHP | 319 lines | 228 code | 28 blank | 63 comment | 45 complexity | f6bf11608ff02b3476f73fcb384cd238 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-2011, 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.2.1
  37. * @copyright 2004-2011 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. * @link http://simplepie.org/support/ Please submit all bug reports and feature requests to the Simplepie forums
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @todo phpDoc comments
  45. */
  46. /**
  47. * @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
  48. */
  49. class Simplepie_Sanitize {
  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. function remove_div($enable = true) {
  80. $this->remove_div = (bool) $enable;
  81. }
  82. function set_image_handler($page = false) {
  83. if ($page) {
  84. $this->image_handler = (string) $page;
  85. } else {
  86. $this->image_handler = false;
  87. }
  88. }
  89. function pass_cache_data($enable_cache = true, $cache_location = './cache', $cache_name_function = 'md5', $cache_class = 'Simplepie_Cache') {
  90. if (isset($enable_cache)) {
  91. $this->enable_cache = (bool) $enable_cache;
  92. }
  93. if ($cache_location) {
  94. $this->cache_location = (string) $cache_location;
  95. }
  96. if ($cache_name_function) {
  97. $this->cache_name_function = (string) $cache_name_function;
  98. }
  99. if ($cache_class) {
  100. $this->cache_class = (string) $cache_class;
  101. }
  102. }
  103. function pass_file_data($file_class = 'Simplepie_File', $timeout = 10, $useragent = '', $force_fsockopen = false) {
  104. if ($file_class) {
  105. $this->file_class = (string) $file_class;
  106. }
  107. if ($timeout) {
  108. $this->timeout = (string) $timeout;
  109. }
  110. if ($useragent) {
  111. $this->useragent = (string) $useragent;
  112. }
  113. if ($force_fsockopen) {
  114. $this->force_fsockopen = (string) $force_fsockopen;
  115. }
  116. }
  117. function strip_htmltags($tags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style')) {
  118. if ($tags) {
  119. if (is_array($tags)) {
  120. $this->strip_htmltags = $tags;
  121. } else {
  122. $this->strip_htmltags = explode(',', $tags);
  123. }
  124. } else {
  125. $this->strip_htmltags = false;
  126. }
  127. }
  128. function encode_instead_of_strip($encode = false) {
  129. $this->encode_instead_of_strip = (bool) $encode;
  130. }
  131. function strip_attributes($attribs = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc')) {
  132. if ($attribs) {
  133. if (is_array($attribs)) {
  134. $this->strip_attributes = $attribs;
  135. } else {
  136. $this->strip_attributes = explode(',', $attribs);
  137. }
  138. } else {
  139. $this->strip_attributes = false;
  140. }
  141. }
  142. function strip_comments($strip = false) {
  143. $this->strip_comments = (bool) $strip;
  144. }
  145. function set_output_encoding($encoding = 'UTF-8') {
  146. $this->output_encoding = (string) $encoding;
  147. }
  148. /**
  149. * Set element/attribute key/value pairs of HTML attributes
  150. * containing URLs that need to be resolved relative to the feed
  151. *
  152. * @access public
  153. * @since 1.0
  154. * @param array $element_attribute Element/attribute key/value pairs
  155. */
  156. 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')) {
  157. $this->replace_url_attributes = (array) $element_attribute;
  158. }
  159. function sanitize($data, $type, $base = '') {
  160. $data = trim($data);
  161. if ($data !== '' || $type & SIMPLEPIE_CONSTRUCT_IRI) {
  162. if ($type & SIMPLEPIE_CONSTRUCT_MAYBE_HTML) {
  163. 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)) {
  164. $type |= SIMPLEPIE_CONSTRUCT_HTML;
  165. } else {
  166. $type |= SIMPLEPIE_CONSTRUCT_TEXT;
  167. }
  168. }
  169. if ($type & SIMPLEPIE_CONSTRUCT_BASE64) {
  170. $data = base64_decode($data);
  171. }
  172. if ($type & SIMPLEPIE_CONSTRUCT_XHTML) {
  173. if ($this->remove_div) {
  174. $data = preg_replace('/^<div' . SIMPLEPIE_PCRE_XML_ATTRIBUTE . '>/', '', $data);
  175. $data = preg_replace('/<\/div>$/', '', $data);
  176. } else {
  177. $data = preg_replace('/^<div' . SIMPLEPIE_PCRE_XML_ATTRIBUTE . '>/', '<div>', $data);
  178. }
  179. }
  180. if ($type & (SIMPLEPIE_CONSTRUCT_HTML | SIMPLEPIE_CONSTRUCT_XHTML)) {
  181. // Strip comments
  182. if ($this->strip_comments) {
  183. $data = Simplepie_Misc::strip_comments($data);
  184. }
  185. // Strip out HTML tags and attributes that might cause various security problems.
  186. // Based on recommendations by Mark Pilgrim at:
  187. // http://diveintomark.org/archives/2003/06/12/how_to_consume_rss_safely
  188. if ($this->strip_htmltags) {
  189. foreach ($this->strip_htmltags as $tag) {
  190. $pcre = "/<($tag)" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . "(>(.*)<\/$tag" . SIMPLEPIE_PCRE_HTML_ATTRIBUTE . '>|(\/)?>)/siU';
  191. while (preg_match($pcre, $data)) {
  192. $data = preg_replace_callback($pcre, array(&$this, 'do_strip_htmltags'), $data);
  193. }
  194. }
  195. }
  196. if ($this->strip_attributes) {
  197. foreach ($this->strip_attributes as $attrib) {
  198. $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);
  199. }
  200. }
  201. // Replace relative URLs
  202. $this->base = $base;
  203. foreach ($this->replace_url_attributes as $element => $attributes) {
  204. $data = $this->replace_urls($data, $element, $attributes);
  205. }
  206. // If image handling (caching, etc.) is enabled, cache and rewrite all the image tags.
  207. if (isset($this->image_handler) && ((string) $this->image_handler) !== '' && $this->enable_cache) {
  208. $images = Simplepie_Misc::get_element('img', $data);
  209. foreach ($images as $img) {
  210. if (isset($img['attribs']['src']['data'])) {
  211. $image_url = call_user_func($this->cache_name_function, $img['attribs']['src']['data']);
  212. $cache = call_user_func(array($this->cache_class, 'create'), $this->cache_location, $image_url, 'spi');
  213. if ($cache->load()) {
  214. $img['attribs']['src']['data'] = $this->image_handler . $image_url;
  215. $data = str_replace($img['full'], Simplepie_Misc::element_implode($img), $data);
  216. } else {
  217. $file = new $this->file_class($img['attribs']['src']['data'], $this->timeout, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen);
  218. $headers = $file->headers;
  219. if ($file->success && ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300))) {
  220. if ($cache->save(array('headers' => $file->headers, 'body' => $file->body))) {
  221. $img['attribs']['src']['data'] = $this->image_handler . $image_url;
  222. $data = str_replace($img['full'], Simplepie_Misc::element_implode($img), $data);
  223. } else {
  224. 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);
  225. }
  226. }
  227. }
  228. }
  229. }
  230. }
  231. // Having (possibly) taken stuff out, there may now be whitespace at the beginning/end of the data
  232. $data = trim($data);
  233. }
  234. if ($type & SIMPLEPIE_CONSTRUCT_IRI) {
  235. $data = Simplepie_Misc::absolutize_url($data, $base);
  236. }
  237. if ($type & (SIMPLEPIE_CONSTRUCT_TEXT | SIMPLEPIE_CONSTRUCT_IRI)) {
  238. $data = htmlspecialchars($data, ENT_COMPAT, 'UTF-8');
  239. }
  240. if ($this->output_encoding !== 'UTF-8') {
  241. $data = Simplepie_Misc::change_encoding($data, 'UTF-8', $this->output_encoding);
  242. }
  243. }
  244. return $data;
  245. }
  246. function replace_urls($data, $tag, $attributes) {
  247. if (!is_array($this->strip_htmltags) || !in_array($tag, $this->strip_htmltags)) {
  248. $elements = Simplepie_Misc::get_element($tag, $data);
  249. foreach ($elements as $element) {
  250. if (is_array($attributes)) {
  251. foreach ($attributes as $attribute) {
  252. if (isset($element['attribs'][$attribute]['data'])) {
  253. $element['attribs'][$attribute]['data'] = Simplepie_Misc::absolutize_url($element['attribs'][$attribute]['data'], $this->base);
  254. $new_element = Simplepie_Misc::element_implode($element);
  255. $data = str_replace($element['full'], $new_element, $data);
  256. $element['full'] = $new_element;
  257. }
  258. }
  259. } elseif (isset($element['attribs'][$attributes]['data'])) {
  260. $element['attribs'][$attributes]['data'] = Simplepie_Misc::absolutize_url($element['attribs'][$attributes]['data'], $this->base);
  261. $data = str_replace($element['full'], Simplepie_Misc::element_implode($element), $data);
  262. }
  263. }
  264. }
  265. return $data;
  266. }
  267. function do_strip_htmltags($match) {
  268. if ($this->encode_instead_of_strip) {
  269. if (isset($match[4]) && !in_array(strtolower($match[1]), array('script', 'style'))) {
  270. $match[1] = htmlspecialchars($match[1], ENT_COMPAT, 'UTF-8');
  271. $match[2] = htmlspecialchars($match[2], ENT_COMPAT, 'UTF-8');
  272. return "&lt;$match[1]$match[2]&gt;$match[3]&lt;/$match[1]&gt;";
  273. } else {
  274. return htmlspecialchars($match[0], ENT_COMPAT, 'UTF-8');
  275. }
  276. } elseif (isset($match[4]) && !in_array(strtolower($match[1]), array('script', 'style'))) {
  277. return $match[4];
  278. } else {
  279. return '';
  280. }
  281. }
  282. }
  283. ?>