Based on the rules in http://tools.ietf.org/html/draft-abarth-mime-sniff-06
This is used since we can't always trust Content-Type headers, and is based
upon the HTML5 parsing rules.
This class can be overloaded with {@see \SimplePie::set_content_type_sniffer_class()}
/** * Get the Content-Type of the specified file * * @access public * @return string Filtered content type */ function get_type() { $contentType = null; $charset = null; if (isset($this->file->headers['content-type'])) { if (!is_array($this->file->headers['content-type'])) { $this->file->headers['content-type'] = array($this->file->headers['content-type']); } foreach ($this->file->headers['content-type'] as $type) { $parts = array_map('trim', explode(";", $type, 2)); if (isset($parts[1])) { $type = $parts[0]; $charset = $parts[1]; } if (preg_match('!(application|text)/((atom|rss|rdf)\\+)?xml!', $type, $ref)) { $contentType = $ref[0]; } } $outHeader = array(); if (!is_null($contentType)) { $outHeader[] = $contentType; } else { $outHeader[] = 'text/xml'; // Generic } if (!is_null($charset)) { $outHeader[] = $charset; } $this->file->headers['content-type'] = implode("; ", $outHeader); } else { // The default SimplePie behavior seems to be to return // text/plain if it can't find a Content-Type header. // The default SimplePie behavior sucks. Particularly // since SimplePie gets so draconian about Content-Type. // And since the WP SimplePie seems to drop Content-Type // from cached copies for some unfortunate reason. $this->file->headers['content-type'] = 'text/xml'; // Generic } return parent::get_type(); }
public static function checkFavicon($favicon) { if ($favicon === null || $favicon == false) { return false; } $file = new \SimplePie_File($favicon); // size in bytes $filesize = strlen($file->body); if ($file->success && $filesize > 0 && $filesize < 50000) { //bigger files are not considered favicons $sniffer = new \SimplePie_Content_Type_Sniffer($file); if (substr($sniffer->get_type(), 0, 6) === 'image/') { $imgsize = getimagesize($favicon); if ($imgsize['0'] <= 32 && $imgsize['1'] <= 32) { //bigger images are not considered favicons return true; } } } return false; }
/** * Test if the file is an image * @param string $url the url to the file * @return bool true if image */ protected function isImage($url) { // check for empty urls if (!$url) { return false; } $file = $this->getFile($url); $sniffer = new \SimplePie_Content_Type_Sniffer($file); return $sniffer->image() !== false; }
/** * Test if the file is an image * @param string $url the url to the file * @return bool true if image */ protected function isImage($url) { // check for empty urls if (!$url) { return false; } $file = $this->getFile($url); $status_range = (string) substr($file->status_code, 0, 1); //echo $status_range; if ($status_range == 2 || $status_range == 3 || !empty($status_range)) { // 200's are a good response and 300's are redirects $sniffer = new \SimplePie_Content_Type_Sniffer($file); return $sniffer->image() !== false; } }