1<?php
2
3/**
4 * SimplePie
5 *
6 * A PHP-Based RSS and Atom Feed Framework.
7 * Takes the hard work out of managing a complete RSS/Atom solution.
8 *
9 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without modification, are
13 * permitted provided that the following conditions are met:
14 *
15 * 	* Redistributions of source code must retain the above copyright notice, this list of
16 * 	  conditions and the following disclaimer.
17 *
18 * 	* Redistributions in binary form must reproduce the above copyright notice, this list
19 * 	  of conditions and the following disclaimer in the documentation and/or other materials
20 * 	  provided with the distribution.
21 *
22 * 	* Neither the name of the SimplePie Team nor the names of its contributors may be used
23 * 	  to endorse or promote products derived from this software without specific prior
24 * 	  written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
27 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
29 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * @package SimplePie
37 * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
38 * @author Ryan Parman
39 * @author Sam Sneddon
40 * @author Ryan McCue
41 * @link http://simplepie.org/ SimplePie
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 */
44
45namespace SimplePie\Content\Type;
46
47/**
48 * Content-type sniffing
49 *
50 * Based on the rules in http://tools.ietf.org/html/draft-abarth-mime-sniff-06
51 *
52 * This is used since we can't always trust Content-Type headers, and is based
53 * upon the HTML5 parsing rules.
54 *
55 *
56 * This class can be overloaded with {@see \SimplePie\SimplePie::set_content_type_sniffer_class()}
57 *
58 * @package SimplePie
59 * @subpackage HTTP
60 */
61class Sniffer
62{
63    /**
64     * File object
65     *
66     * @var \SimplePie\File
67     */
68    public $file;
69
70    /**
71     * Create an instance of the class with the input file
72     *
73     * @param Sniffer $file Input file
74     */
75    public function __construct($file)
76    {
77        $this->file = $file;
78    }
79
80    /**
81     * Get the Content-Type of the specified file
82     *
83     * @return string Actual Content-Type
84     */
85    public function get_type()
86    {
87        if (isset($this->file->headers['content-type'])) {
88            if (!isset($this->file->headers['content-encoding'])
89                && ($this->file->headers['content-type'] === 'text/plain'
90                    || $this->file->headers['content-type'] === 'text/plain; charset=ISO-8859-1'
91                    || $this->file->headers['content-type'] === 'text/plain; charset=iso-8859-1'
92                    || $this->file->headers['content-type'] === 'text/plain; charset=UTF-8')) {
93                return $this->text_or_binary();
94            }
95
96            if (($pos = strpos($this->file->headers['content-type'], ';')) !== false) {
97                $official = substr($this->file->headers['content-type'], 0, $pos);
98            } else {
99                $official = $this->file->headers['content-type'];
100            }
101            $official = trim(strtolower($official));
102
103            if ($official === 'unknown/unknown'
104                || $official === 'application/unknown') {
105                return $this->unknown();
106            } elseif (substr($official, -4) === '+xml'
107                || $official === 'text/xml'
108                || $official === 'application/xml') {
109                return $official;
110            } elseif (substr($official, 0, 6) === 'image/') {
111                if ($return = $this->image()) {
112                    return $return;
113                }
114
115                return $official;
116            } elseif ($official === 'text/html') {
117                return $this->feed_or_html();
118            }
119
120            return $official;
121        }
122
123        return $this->unknown();
124    }
125
126    /**
127     * Sniff text or binary
128     *
129     * @return string Actual Content-Type
130     */
131    public function text_or_binary()
132    {
133        if (substr($this->file->body, 0, 2) === "\xFE\xFF"
134            || substr($this->file->body, 0, 2) === "\xFF\xFE"
135            || substr($this->file->body, 0, 4) === "\x00\x00\xFE\xFF"
136            || substr($this->file->body, 0, 3) === "\xEF\xBB\xBF") {
137            return 'text/plain';
138        } elseif (preg_match('/[\x00-\x08\x0E-\x1A\x1C-\x1F]/', $this->file->body)) {
139            return 'application/octet-stream';
140        }
141
142        return 'text/plain';
143    }
144
145    /**
146     * Sniff unknown
147     *
148     * @return string Actual Content-Type
149     */
150    public function unknown()
151    {
152        $ws = strspn($this->file->body, "\x09\x0A\x0B\x0C\x0D\x20");
153        if (strtolower(substr($this->file->body, $ws, 14)) === '<!doctype html'
154            || strtolower(substr($this->file->body, $ws, 5)) === '<html'
155            || strtolower(substr($this->file->body, $ws, 7)) === '<script') {
156            return 'text/html';
157        } elseif (substr($this->file->body, 0, 5) === '%PDF-') {
158            return 'application/pdf';
159        } elseif (substr($this->file->body, 0, 11) === '%!PS-Adobe-') {
160            return 'application/postscript';
161        } elseif (substr($this->file->body, 0, 6) === 'GIF87a'
162            || substr($this->file->body, 0, 6) === 'GIF89a') {
163            return 'image/gif';
164        } elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
165            return 'image/png';
166        } elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF") {
167            return 'image/jpeg';
168        } elseif (substr($this->file->body, 0, 2) === "\x42\x4D") {
169            return 'image/bmp';
170        } elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00") {
171            return 'image/vnd.microsoft.icon';
172        }
173
174        return $this->text_or_binary();
175    }
176
177    /**
178     * Sniff images
179     *
180     * @return string Actual Content-Type
181     */
182    public function image()
183    {
184        if (substr($this->file->body, 0, 6) === 'GIF87a'
185            || substr($this->file->body, 0, 6) === 'GIF89a') {
186            return 'image/gif';
187        } elseif (substr($this->file->body, 0, 8) === "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A") {
188            return 'image/png';
189        } elseif (substr($this->file->body, 0, 3) === "\xFF\xD8\xFF") {
190            return 'image/jpeg';
191        } elseif (substr($this->file->body, 0, 2) === "\x42\x4D") {
192            return 'image/bmp';
193        } elseif (substr($this->file->body, 0, 4) === "\x00\x00\x01\x00") {
194            return 'image/vnd.microsoft.icon';
195        }
196
197        return false;
198    }
199
200    /**
201     * Sniff HTML
202     *
203     * @return string Actual Content-Type
204     */
205    public function feed_or_html()
206    {
207        $len = strlen($this->file->body);
208        $pos = strspn($this->file->body, "\x09\x0A\x0D\x20\xEF\xBB\xBF");
209
210        while ($pos < $len) {
211            switch ($this->file->body[$pos]) {
212                case "\x09":
213                case "\x0A":
214                case "\x0D":
215                case "\x20":
216                    $pos += strspn($this->file->body, "\x09\x0A\x0D\x20", $pos);
217                    continue 2;
218
219                case '<':
220                    $pos++;
221                    break;
222
223                default:
224                    return 'text/html';
225            }
226
227            if (substr($this->file->body, $pos, 3) === '!--') {
228                $pos += 3;
229                if ($pos < $len && ($pos = strpos($this->file->body, '-->', $pos)) !== false) {
230                    $pos += 3;
231                } else {
232                    return 'text/html';
233                }
234            } elseif (substr($this->file->body, $pos, 1) === '!') {
235                if ($pos < $len && ($pos = strpos($this->file->body, '>', $pos)) !== false) {
236                    $pos++;
237                } else {
238                    return 'text/html';
239                }
240            } elseif (substr($this->file->body, $pos, 1) === '?') {
241                if ($pos < $len && ($pos = strpos($this->file->body, '?>', $pos)) !== false) {
242                    $pos += 2;
243                } else {
244                    return 'text/html';
245                }
246            } elseif (substr($this->file->body, $pos, 3) === 'rss'
247                || substr($this->file->body, $pos, 7) === 'rdf:RDF') {
248                return 'application/rss+xml';
249            } elseif (substr($this->file->body, $pos, 4) === 'feed') {
250                return 'application/atom+xml';
251            } else {
252                return 'text/html';
253            }
254        }
255
256        return 'text/html';
257    }
258}
259
260class_alias('SimplePie\Content\Type\Sniffer', 'SimplePie_Content_Type_Sniffer');
261