magic_db.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 if (!class_exists('File')) {
00028 uses ('object', 'file');
00029 }
00030
00031
00032
00033
00034
00035
00036 class MagicDb extends Object {
00037
00038
00039
00040
00041
00042 var $db = array();
00043
00044
00045
00046
00047
00048
00049
00050
00051 function read($magicDb = null) {
00052 if (!is_string($magicDb) && !is_array($magicDb)) {
00053 return false;
00054 }
00055 if (is_array($magicDb) || strpos($magicDb, '# FILE_ID DB') === 0) {
00056 $data = $magicDb;
00057 } else {
00058 $File =& new File($magicDb);
00059 if (!$File->exists()) {
00060 return false;
00061 }
00062 if ($File->ext() == 'php') {
00063 include($File->pwd());
00064 $data = $magicDb;
00065 } else {
00066
00067 $data = $File->read();
00068 }
00069 }
00070
00071 $magicDb = $this->toArray($data);
00072 if (!$this->validates($magicDb)) {
00073 return false;
00074 }
00075 return !!($this->db = $magicDb);
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085 function toArray($data = null) {
00086 if (is_array($data)) {
00087 return $data;
00088 }
00089 if ($data === null) {
00090 return $this->db;
00091 }
00092
00093 if (strpos($data, '# FILE_ID DB') !== 0) {
00094 return array();
00095 }
00096
00097 $lines = explode("\r\n", $data);
00098 $db = array();
00099
00100 $validHeader = count($lines > 3)
00101 && preg_match('/^# Date:([0-9]{4}-[0-9]{2}-[0-9]{2})$/', $lines[1], $date)
00102 && preg_match('/^# Source:(.+)$/', $lines[2], $source)
00103 && strlen($lines[3]) == 0;
00104 if (!$validHeader) {
00105 return $db;
00106 }
00107
00108 $db = array('header' => array('Date' => $date[1], 'Source' => $source[1]), 'database' => array());
00109 $lines = array_splice($lines, 3);
00110
00111 $format = array();
00112 while (!empty($lines)) {
00113 $line = array_shift($lines);
00114 if (isset($line[0]) && $line[0] == '#' || empty($line)) {
00115 continue;
00116 }
00117
00118 $columns = explode("\t", $line);
00119 if (in_array($columns[0]{0}, array('>', '&'))) {
00120 $format[] = $columns;
00121 } elseif (!empty($format)) {
00122 $db['database'][] = $format;
00123 $format = array($columns);
00124 } else {
00125 $format = array($columns);
00126 }
00127 }
00128
00129 return $db;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139 function validates($magicDb = null) {
00140 if (is_null($magicDb)) {
00141 $magicDb = $this->db;
00142 } elseif (!is_array($magicDb)) {
00143 $magicDb = $this->toArray($magicDb);
00144 }
00145
00146 return isset($magicDb['header'], $magicDb['database']) && is_array($magicDb['header']) && is_array($magicDb['database']);
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 function analyze($file, $options = array()) {
00158 if (!is_string($file)) {
00159 return false;
00160 }
00161
00162 $matches = array();
00163 $MagicFileResource =& new MagicFileResource($file);
00164 foreach ($this->db['database'] as $format) {
00165 $magic = $format[0];
00166 $match = $MagicFileResource->test($magic);
00167 if ($match === false) {
00168 continue;
00169 }
00170 $matches[] = $magic;
00171 }
00172
00173 return $matches;
00174 }
00175 }
00176
00177
00178
00179
00180
00181
00182
00183 class MagicFileResource extends Object{
00184
00185
00186
00187
00188
00189
00190 var $resource = null;
00191
00192
00193
00194
00195
00196
00197 var $offset = 0;
00198
00199
00200
00201
00202
00203
00204
00205 function __construct($file) {
00206 if (file_exists($file)) {
00207 $this->resource =& new File($file);
00208 } else {
00209 $this->resource = $file;
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218
00219 function test($magic) {
00220 $offset = null;
00221 $type = null;
00222 $expected = null;
00223 $comment = null;
00224 if (isset($magic[0])) {
00225 $offset = $magic[0];
00226 }
00227 if (isset($magic[1])) {
00228 $type = $magic[1];
00229 }
00230 if (isset($magic[2])) {
00231 $expected = $magic[2];
00232 }
00233 if (isset($magic[3])) {
00234 $comment = $magic[3];
00235 }
00236 $val = $this->extract($offset, $type, $expected);
00237 return $val == $expected;
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247 function read($length = null) {
00248 if (!is_object($this->resource)){
00249 return substr($this->resource, $this->offset, $length);
00250 } else {
00251 return $this->resource->read($length);
00252 }
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262 function extract($offset, $type, $expected) {
00263 switch ($type) {
00264 case 'string':
00265 $this->offset($offset);
00266 $val = $this->read(strlen($expected));
00267 if ($val === $expected) {
00268 return true;
00269 }
00270 break;
00271 }
00272 }
00273
00274
00275
00276
00277
00278
00279
00280
00281 function offset($offset = null) {
00282 if (is_null($offset)) {
00283 if (!is_object($this->resource)) {
00284 return $this->offset;
00285 }
00286 return $this->offset;
00287 }
00288
00289 if (!ctype_digit($offset)) {
00290 return false;
00291 }
00292 if (is_object($this->resource)) {
00293 $this->resource->offset($offset);
00294 } else {
00295 $this->offset = $offset;
00296 }
00297 }
00298 }
00299
00300 ?>