PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/simplepie/SimplePie/Content/Type/Sniffer.php

https://bitbucket.org/timgws/full-text-rss
PHP | 325 lines | 225 code | 16 blank | 84 comment | 32 complexity | 05d1c8e6bf01859d9f350962295ccaae 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. * Content-type sniffing
  47. *
  48. * Based on the rules in http://tools.ietf.org/html/draft-abarth-mime-sniff-06
  49. * @package SimplePie
  50. */
  51. class SimplePie_Content_Type_Sniffer
  52. {
  53. /**
  54. * File object
  55. *
  56. * @var SimplePie_File
  57. */
  58. var $file;
  59. /**
  60. * Create an instance of the class with the input file
  61. *
  62. * @param SimplePie_Content_Type_Sniffer $file Input file
  63. */
  64. public function __construct($file)
  65. {
  66. $this->file = $file;
  67. }
  68. /**
  69. * Get the Content-Type of the specified file
  70. *
  71. * @return string Actual Content-Type
  72. */
  73. public function get_type()
  74. {
  75. if (isset($this->file->headers['content-type']))
  76. {
  77. if (!isset($this->file->headers['content-encoding'])
  78. && ($this->file->headers['content-type'] === 'text/plain'
  79. || $this->file->headers['content-type'] === 'text/plain; charset=ISO-8859-1'
  80. || $this->file->headers['content-type'] === 'text/plain; charset=iso-8859-1'
  81. || $this->file->headers['content-type'] === 'text/plain; charset=UTF-8'))
  82. {
  83. return $this->text_or_binary();
  84. }
  85. if (($pos = strpos($this->file->headers['content-type'], ';')) !== false)
  86. {
  87. $official = substr($this->file->headers['content-type'], 0, $pos);
  88. }
  89. else
  90. {
  91. $official = $this->file->headers['content-type'];
  92. }
  93. $official = trim(strtolower($official));
  94. if ($official === 'unknown/unknown'
  95. || $official === 'application/unknown')
  96. {
  97. return $this->unknown();
  98. }
  99. elseif (substr($official, -4) === '+xml'
  100. || $official === 'text/xml'
  101. || $official === 'application/xml')
  102. {
  103. return $official;
  104. }
  105. elseif (substr($official, 0, 6) === 'image/')
  106. {
  107. if ($return = $this->image())
  108. {
  109. return $return;
  110. }
  111. else
  112. {
  113. return $official;
  114. }
  115. }
  116. elseif ($official === 'text/html')
  117. {
  118. return $this->feed_or_html();
  119. }
  120. else
  121. {
  122. return $official;
  123. }
  124. }
  125. else
  126. {
  127. return $this->unknown();
  128. }
  129. }
  130. /**
  131. * Sniff text or binary
  132. *
  133. * @return string Actual Content-Type
  134. */
  135. public function text_or_binary()
  136. {
  137. if (substr($this->file->body, 0, 2) === "\xFE\xFF"
  138. || substr($this->file->body, 0, 2) === "\xFF\xFE"
  139. || substr($this->file->body, 0, 4) === "\x00\x00\xFE\xFF"
  140. || substr($this->file->body, 0, 3) === "\xEF\xBB\xBF")
  141. {
  142. return 'text/plain';
  143. }
  144. elseif (preg_match('/[\x00-\x08\x0E-\x1A\x1C-\x1F]/', $this->file->body))
  145. {
  146. return 'application/octect-stream';
  147. }
  148. else
  149. {
  150. return 'text/plain';
  151. }
  152. }
  153. /**
  154. * Sniff unknown
  155. *
  156. * @return string Actual Content-Type
  157. */
  158. public function unknown()
  159. {
  160. $ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20");
  161. if (strtolower(substr($this->file->body, $ws, 14)) === '<!doctype html'
  162. || strtolower(substr($this->file->body, $ws, 5)) === '<html'
  163. || strtolower(substr($this->file->body, $ws, 7)) === '<script')
  164. {
  165. return 'text/html';
  166. }
  167. elseif (substr($this->file->body, 0, 5) === '%PDF-')
  168. {
  169. return 'application/pdf';
  170. }
  171. elseif (substr($this->file->body, 0, 11) === '%!PS-Adobe-')
  172. {
  173. return 'application/postscript';
  174. }
  175. elseif (substr($this->file->body, 0, 6) === 'GIF87a'
  176. || substr($this->file->body, 0, 6) === 'GIF89a')
  177. {
  178. return 'image/gif';
  179. }
  180. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
  181. {
  182. return 'image/png';
  183. }
  184. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF")
  185. {
  186. return 'image/jpeg';
  187. }
  188. elseif (substr($this->file->body, 0, 2) === "\x42\x4D")
  189. {
  190. return 'image/bmp';
  191. }
  192. elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00")
  193. {
  194. return 'image/vnd.microsoft.icon';
  195. }
  196. else
  197. {
  198. return $this->text_or_binary();
  199. }
  200. }
  201. /**
  202. * Sniff images
  203. *
  204. * @return string Actual Content-Type
  205. */
  206. public function image()
  207. {
  208. if (substr($this->file->body, 0, 6) === 'GIF87a'
  209. || substr($this->file->body, 0, 6) === 'GIF89a')
  210. {
  211. return 'image/gif';
  212. }
  213. elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A")
  214. {
  215. return 'image/png';
  216. }
  217. elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF")
  218. {
  219. return 'image/jpeg';
  220. }
  221. elseif (substr($this->file->body, 0, 2) === "\x42\x4D")
  222. {
  223. return 'image/bmp';
  224. }
  225. elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00")
  226. {
  227. return 'image/vnd.microsoft.icon';
  228. }
  229. else
  230. {
  231. return false;
  232. }
  233. }
  234. /**
  235. * Sniff HTML
  236. *
  237. * @return string Actual Content-Type
  238. */
  239. public function feed_or_html()
  240. {
  241. $len = strlen($this->file->body);
  242. $pos = strspn($this->file->body, "\x09\x0A\x0D\x20");
  243. while ($pos < $len)
  244. {
  245. switch ($this->file->body[$pos])
  246. {
  247. case "\x09":
  248. case "\x0A":
  249. case "\x0D":
  250. case "\x20":
  251. $pos += strspn($this->file->body, "\x09\x0A\x0D\x20", $pos);
  252. continue 2;
  253. case '<':
  254. $pos++;
  255. break;
  256. default:
  257. return 'text/html';
  258. }
  259. if (substr($this->file->body, $pos, 3) === '!--')
  260. {
  261. $pos += 3;
  262. if ($pos < $len && ($pos = strpos($this->file->body, '-->', $pos)) !== false)
  263. {
  264. $pos += 3;
  265. }
  266. else
  267. {
  268. return 'text/html';
  269. }
  270. }
  271. elseif (substr($this->file->body, $pos, 1) === '!')
  272. {
  273. if ($pos < $len && ($pos = strpos($this->file->body, '>', $pos)) !== false)
  274. {
  275. $pos++;
  276. }
  277. else
  278. {
  279. return 'text/html';
  280. }
  281. }
  282. elseif (substr($this->file->body, $pos, 1) === '?')
  283. {
  284. if ($pos < $len && ($pos = strpos($this->file->body, '?>', $pos)) !== false)
  285. {
  286. $pos += 2;
  287. }
  288. else
  289. {
  290. return 'text/html';
  291. }
  292. }
  293. elseif (substr($this->file->body, $pos, 3) === 'rss'
  294. || substr($this->file->body, $pos, 7) === 'rdf:RDF')
  295. {
  296. return 'application/rss+xml';
  297. }
  298. elseif (substr($this->file->body, $pos, 4) === 'feed')
  299. {
  300. return 'application/atom+xml';
  301. }
  302. else
  303. {
  304. return 'text/html';
  305. }
  306. }
  307. return 'text/html';
  308. }
  309. }