PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Zend/Mime/Magic.php

http://php-reader.googlecode.com/
PHP | 196 lines | 97 code | 10 blank | 89 comment | 11 complexity | 1a36de594b5eeeeea90401de55abd2a7 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Mime
 17 * @subpackage Magic
 18 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Magic.php 193 2010-04-08 14:46:41Z svollbehr $
 21 */
 22
 23/**#@+ @ignore */
 24require_once 'Zend/Io/FileReader.php';
 25/**#@-*/
 26
 27/**
 28 * This class is used to classify the given file using some magic bytes
 29 * characteristic to a particular file type. The classification information can
 30 * be a MIME type or just text describing the file.
 31 *
 32 * This method is slower than determining the type by file suffix but on the
 33 * other hand reduces the risk of fail positives during the test.
 34 *
 35 * The magic file consists of ASCII characters defining the magic numbers for
 36 * different file types. Each row has 4 to 5 columns, empty and commented lines
 37 * (those starting with a hash character) are ignored. Columns are described
 38 * below.
 39 *
 40 *  o <b>1</b> -- byte number to begin checking from. '>' indicates a dependency
 41 *    upon the previous non-'>' line
 42 *  o <b>2</b> -- type of data to match. Can be one of following
 43 *    - <i>byte</i> (single character)
 44 *    - <i>short</i> (machine-order 16-bit integer)
 45 *    - <i>long</i> (machine-order 32-bit integer)
 46 *    - <i>string</i> (arbitrary-length string)
 47 *    - <i>date</i> (long integer date (seconds since Unix epoch/1970))
 48 *    - <i>beshort</i> (big-endian 16-bit integer)
 49 *    - <i>belong</i> (big-endian 32-bit integer)
 50 *    - <i>bedate</i> (big-endian 32-bit integer date)
 51 *    - <i>leshort</i> (little-endian 16-bit integer)
 52 *    - <i>lelong</i> (little-endian 32-bit integer)
 53 *    - <i>ledate</i> (little-endian 32-bit integer date)
 54 *  o <b>3</b> -- contents of data to match
 55 *  o <b>4</b> -- file description/MIME type if matched
 56 *  o <b>5</b> -- optional MIME encoding if matched and if above was a MIME type
 57 *
 58 * @category   Zend
 59 * @package    Zend_Mime
 60 * @subpackage Magic
 61 * @author     Sven Vollbehr <sven@vollbehr.eu>
 62 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 63 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 64 * @version    $Id: Magic.php 193 2010-04-08 14:46:41Z svollbehr $
 65 */
 66final class Zend_Mime_Magic
 67{
 68    /** @var string */
 69    private $_magic;
 70
 71    /**
 72     * Reads the magic information from given magic file.
 73     *
 74     * @param string $filename The path to the magic file.
 75     */
 76    public function __construct($filename)
 77    {
 78        $reader = new Zend_Io_FileReader($filename);
 79        $this->_magic = $reader->read($reader->getSize());
 80    }
 81
 82    /**
 83     * Returns the recognized MIME type/description of the given file. The type
 84     * is determined by the content using magic bytes characteristic for the
 85     * particular file type.
 86     *
 87     * If the type could not be found, the function returns the default value,
 88     * or <var>null</var>.
 89     *
 90     * @param string $filename The file path whose type to determine.
 91     * @param string $default  The default value.
 92     * @return string|false
 93     */
 94    public function getMimeType($filename, $default = null)
 95    {
 96        $reader = new Zend_Io_FileReader($filename);
 97
 98        $parentOffset = 0;
 99        foreach (preg_split('/^/m', $this->_magic) as $line) {
100            $chunks = array();
101            if (!preg_match("/^(?P<Dependant>>?)(?P<Byte>\d+)\s+(?P<MatchType" .
102                            ">\S+)\s+(?P<MatchData>\S+)(?:\s+(?P<MIMEType>[a-" .
103                            "z]+\/[a-z-0-9]+)?(?:\s+(?P<Description>.?+))?)?$/",
104                            $line, $chunks)) {
105                continue;
106            }
107
108            if ($chunks['Dependant']) {
109                $reader->setOffset($parentOffset);
110                $reader->skip($chunks['Byte']);
111            } else {
112                $reader->setOffset($parentOffset = $chunks['Byte']);
113            }
114
115            $matchType = strtolower($chunks['MatchType']);
116            $matchData = preg_replace
117                (array("/\\\\ /", "/\\\\\\\\/", "/\\\\([0-7]{1,3})/e",
118                       "/\\\\x([0-9A-Fa-f]{1,2})/e", "/0x([0-9A-Fa-f]+)/e"),
119                 array(" ", "\\\\",
120                       "pack(\"H*\", base_convert(\"$1\", 8, 16));",
121                       "pack(\"H*\", \"$1\");", "hexdec(\"$1\");"),
122                 $chunks["MatchData"]);
123
124            switch ($matchType) {
125                case 'byte':    // single character
126                    $data = $reader->readInt8();
127                    break;
128                case 'short':   // machine-order 16-bit integer
129                    $data = $reader->readInt16();
130                    break;
131                case 'long':    // machine-order 32-bit integer
132                    $data = $reader->readInt32();
133                    break;
134                case 'string':  // arbitrary-length string
135                    $data = $reader->readString8(strlen($matchData));
136                    break;
137                case 'date':    // long integer date (seconds since Unix epoch)
138                    $data = $reader->readInt64BE();
139                    break;
140                case 'beshort': // big-endian 16-bit integer
141                    $data = $reader->readUInt16BE();
142                    break;
143                case 'belong':  // big-endian 32-bit integer
144                    // break intentionally omitted
145                case 'bedate':  // big-endian 32-bit integer date
146                    $data = $reader->readUInt32BE();
147                    break;
148                case 'leshort': // little-endian 16-bit integer
149                    $data = $reader->readUInt16LE();
150                    break;
151                case 'lelong':  // little-endian 32-bit integer
152                    // break intentionally omitted
153                case 'ledate':  // little-endian 32-bit integer date
154                    $data = $reader->readUInt32LE();
155                    break;
156                default:
157                    $data = null;
158                    break;
159            }
160
161            if (strcmp($data, $matchData) == 0) {
162                if (!empty($chunks['MIMEType'])) {
163                    return $chunks['MIMEType'];
164                }
165                if (!empty($chunks['Description'])) {
166                    return rtrim($chunks['Description'], "\n");
167                }
168            }
169        }
170        return $default;
171    }
172
173    /**
174     * Returns the results of the mime type check either as a boolean or an
175     * array of boolean values.
176     *
177     * @param string|Array $filename The file path whose type to test.
178     * @param string|Array $mimeType The mime type to test against.
179     * @return boolean|Array
180     */
181    public function isMimeType($filename, $mimeType)
182    {
183        if (is_array($filename)) {
184            $result = array();
185            foreach ($filename as $key => $value) {
186                $result[] =
187                    ($this->getMimeType($value) ==
188                     (is_array($mimeType) ? $mimeType[$key] : $mimeType)) ?
189                    true : false;
190            }
191            return $result;
192        } else {
193            return $this->getMimeType($filename) == $mimeType ? true : false;
194        }
195    }
196}