Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 297 lines (279 sloc) 6.812 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
<?php
/* SVN FILE: $Id$ */
/**
* MagicDb parser and file analyzer
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 1.2.0
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
if (!class_exists('File')) {
uses('object', 'file');
}
/**
* A class to parse and use the MagicDb for file type analysis
*
* @package cake.tests
* @subpackage cake.tests.cases.libs
*/
class MagicDb extends Object {
/**
* Holds the parsed MagicDb for this class instance
*
* @var array
**/
var $db = array();

/**
* Reads a MagicDb from various formats
*
* @var $magicDb mixed Can be an array containing the db, a magic db as a string, or a filename pointing to a magic db in .db or magic.db.php format
* @return boolean Returns false if reading / validation failed or true on success.
* @author Felix
**/
function read($magicDb = null) {
if (!is_string($magicDb) && !is_array($magicDb)) {
return false;
}
if (is_array($magicDb) || strpos($magicDb, '# FILE_ID DB') === 0) {
$data = $magicDb;
} else {
$File =& new File($magicDb);
if (!$File->exists()) {
return false;
}
if ($File->ext() == 'php') {
include($File->pwd());
$data = $magicDb;
} else {
// @TODO: Needs test coverage
$data = $File->read();
}
}

$magicDb = $this->toArray($data);
if (!$this->validates($magicDb)) {
return false;
}
return !!($this->db = $magicDb);
}

/**
* Parses a MagicDb $data string into an array or returns the current MagicDb instance as an array
*
* @param string $data A MagicDb string to turn into an array
* @return array A parsed MagicDb array or an empty array if the $data param was invalid. Returns the db property if $data is not set.
* @access public
*/
function toArray($data = null) {
if (is_array($data)) {
return $data;
}
if ($data === null) {
return $this->db;
}

if (strpos($data, '# FILE_ID DB') !== 0) {
return array();
}

$lines = explode("\r\n", $data);
$db = array();

$validHeader = count($lines) > 3
&& preg_match('/^# Date:([0-9]{4}-[0-9]{2}-[0-9]{2})$/', $lines[1], $date)
&& preg_match('/^# Source:(.+)$/', $lines[2], $source)
&& strlen($lines[3]) == 0;
if (!$validHeader) {
return $db;
}

$db = array('header' => array('Date' => $date[1], 'Source' => $source[1]), 'database' => array());
$lines = array_splice($lines, 3);

$format = array();
while (!empty($lines)) {
$line = array_shift($lines);
if (isset($line[0]) && $line[0] == '#' || empty($line)) {
continue;
}

$columns = explode("\t", $line);
if (in_array($columns[0]{0}, array('>', '&'))) {
$format[] = $columns;
} elseif (!empty($format)) {
$db['database'][] = $format;
$format = array($columns);
} else {
$format = array($columns);
}
}

return $db;
}

/**
* Returns true if the MagicDb instance or the passed $magicDb is valid
*
* @param mixed $magicDb A $magicDb string / array to validate (optional)
* @return boolean True if the $magicDb / instance db validates, false if not
* @access public
*/
function validates($magicDb = null) {
if (is_null($magicDb)) {
$magicDb = $this->db;
} elseif (!is_array($magicDb)) {
$magicDb = $this->toArray($magicDb);
}

return isset($magicDb['header'], $magicDb['database']) && is_array($magicDb['header']) && is_array($magicDb['database']);
}

/**
* Analyzes a given $file using the currently loaded MagicDb information based on the desired $options
*
* @param string $file Absolute path to the file to analyze
* @param array $options TBT
* @return mixed
* @access public
*/
function analyze($file, $options = array()) {
if (!is_string($file)) {
return false;
}

$matches = array();
$MagicFileResource =& new MagicFileResource($file);
foreach ($this->db['database'] as $format) {
$magic = $format[0];
$match = $MagicFileResource->test($magic);
if ($match === false) {
continue;
}
$matches[] = $magic;
}

return $matches;
}
}

/**
* undocumented class
*
* @package cake.tests
* @subpackage cake.tests.cases.libs
*/
class MagicFileResource extends Object{
/**
* undocumented variable
*
* @var unknown
* @access public
*/
var $resource = null;
/**
* undocumented variable
*
* @var unknown
* @access public
*/
var $offset = 0;
/**
* undocumented function
*
* @param unknown $file
* @return void
* @access public
*/
function __construct($file) {
if (file_exists($file)) {
$this->resource =& new File($file);
} else {
$this->resource = $file;
}
}
/**
* undocumented function
*
* @param unknown $magic
* @return void
* @access public
*/
function test($magic) {
$offset = null;
$type = null;
$expected = null;
$comment = null;
if (isset($magic[0])) {
$offset = $magic[0];
}
if (isset($magic[1])) {
$type = $magic[1];
}
if (isset($magic[2])) {
$expected = $magic[2];
}
if (isset($magic[3])) {
$comment = $magic[3];
}
$val = $this->extract($offset, $type, $expected);
return $val == $expected;
}
/**
* undocumented function
*
* @param unknown $type
* @param unknown $length
* @return void
* @access public
*/
function read($length = null) {
if (!is_object($this->resource)) {
return substr($this->resource, $this->offset, $length);
}
return $this->resource->read($length);
}
/**
* undocumented function
*
* @param unknown $type
* @param unknown $expected
* @return void
* @access public
*/
function extract($offset, $type, $expected) {
switch ($type) {
case 'string':
$this->offset($offset);
$val = $this->read(strlen($expected));
if ($val === $expected) {
return true;
}
break;
}
}
/**
* undocumented function
*
* @param unknown $offset
* @param unknown $whence
* @return void
* @access public
*/
function offset($offset = null) {
if (is_null($offset)) {
if (!is_object($this->resource)) {
return $this->offset;
}
return $this->offset;
}

if (!ctype_digit($offset)) {
return false;
}
if (is_object($this->resource)) {
$this->resource->offset($offset);
} else {
$this->offset = $offset;
}
}
}

?>
Something went wrong with that request. Please try again.