Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezmimetype.php
Go to the documentation of this file.
1 <?php
33 class eZMimeType
34 {
35 public function __construct()
36 {
37 $this->SuffixList = array();
38 $this->PrefixList = array();
39 $this->MIMEList = array();
40
41 $ini = eZINI::instance( 'mime.ini' );
42 $this->QuickMIMETypes = array();
43 foreach ( $ini->groups() as $extension => $blockValues )
44 {
45 foreach ( $blockValues['Types'] as $type )
46 {
47 $this->QuickMIMETypes[] = array( $extension, $type );
48 }
49 }
50
51 foreach ( $this->QuickMIMETypes as $quickMIMEType )
52 {
53 $mimeEntry =& $this->MIMEList[$quickMIMEType[1]];
54 if ( !isset( $mimeEntry ) )
55 $mimeEntry = array( 'suffixes' => array(),
56 'prefixes' => false );
57 $mimeEntry['suffixes'][] = $quickMIMEType[0];
58 }
59
60 eZMimeType::prepareSuffixList( $this->SuffixList, $this->MIMEList );
61 eZMimeType::preparePrefixList( $this->PrefixList, $this->MIMEList );
62 }
63
68 static function defaultMimeType()
69 {
70 return array( 'name' => 'application/octet-stream',
71 'url' => false,
72 'filename' => false,
73 'dirpath' => false,
74 'basename' => false,
75 'suffix' => false,
76 'prefix' => false,
77 'suffixes' => false,
78 'prefixes' => false,
79 'is_valid' => true );
80 }
81
86 static function defaultValue( $url, $returnDefault )
87 {
88 if ( $returnDefault )
89 {
90 $mime = eZMimeType::defaultMimeType();
91 $mime['url'] = $url;
92 $suffixPos = strpos( $url, '.' );
93
94 if ( $suffixPos !== false )
95 {
96 $mime['suffix'] = substr( $url, $suffixPos + 1 );
97 $mime['dirpath'] = dirname( $url );
98 $mime['basename'] = basename( $url, '.' . $mime['suffix'] );
99 $mime['filename'] = $mime['basename'] . '.' . $mime['suffix'];
100 }
101 else
102 {
103 $mime['basename'] = $url;
104 $mime['suffix'] = false;
105 }
106 $mime['is_valid'] = false;
107 return $mime;
108 }
109 else
110 return false;
111 }
112
117 static function changeMIMEType( &$mimeInfo, $mimetype )
118 {
119 $mimeInfo['name'] = $mimetype;
120 $newMimeInfo = eZMimeType::findByName( $mimetype );
121 $mimeInfo['suffixes'] = $newMimeInfo['suffixes'];
122 $mimeInfo['prefixes'] = $newMimeInfo['prefixes'];
123 $mimeInfo['suffix'] = $newMimeInfo['suffix'];
124 $mimeInfo['prefix'] = $newMimeInfo['prefix'];
125 $filename = $mimeInfo['filename'];
126 $dotPosition = strrpos( $filename, '.' );
127 $basename = $filename;
128 if ( $dotPosition !== false )
129 $basename = substr( $filename, 0, $dotPosition );
130 $mimeInfo['filename'] = $basename . '.' . $mimeInfo['suffix'];
131 if ( $mimeInfo['dirpath'] )
132 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
133 else
134 $mimeInfo['url'] = $mimeInfo['filename'];
135 }
136
141 static function changeBasename( &$mimeInfo, $basename )
142 {
143 $mimeInfo['basename'] = $basename;
144 $mimeInfo['filename'] = $basename . '.' . $mimeInfo['suffix'];
145 if ( $mimeInfo['dirpath'] )
146 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
147 else
148 $mimeInfo['url'] = $mimeInfo['filename'];
149 }
150
155 static function changeDirectoryPath( &$mimeInfo, $dirpath )
156 {
157 $mimeInfo['dirpath'] = $dirpath;
158 if ( $mimeInfo['dirpath'] )
159 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
160 else
161 $mimeInfo['url'] = $mimeInfo['filename'];
162 }
163
168 static function changeFileData( &$mimeInfo, $dirpath = false, $basename = false, $suffix = false, $filename = false )
169 {
170 if ( $basename !== false )
171 $mimeInfo['basename'] = $basename;
172 if ( $suffix !== false )
173 $mimeInfo['suffix'] = $suffix;
174 if ( $filename !== false )
175 {
176 $mimeInfo['filename'] = $filename;
177 }
178 else
179 {
180 $mimeInfo['filename'] = $mimeInfo['basename'];
181 $mimeInfo['filename'] .= '.' . $mimeInfo['suffix'];
182 }
183 if ( $dirpath !== false )
184 $mimeInfo['dirpath'] = $dirpath;
185
186 if ( $mimeInfo['dirpath'] )
187 $mimeInfo['url'] = $mimeInfo['dirpath'] . '/' . $mimeInfo['filename'];
188 else
189 $mimeInfo['url'] = $mimeInfo['filename'];
190 }
191
197 static function findByName( $mimeName, $returnDefault = true )
198 {
199 $instance = eZMimeType::instance();
200 $mimeList =& $instance->MIMEList;
201 if ( isset( $mimeList[$mimeName] ) )
202 {
203 $mime = $mimeList[$mimeName];
204 $mime['name'] = $mimeName;
205 $mime['url'] = false;
206 $mime['filename'] = false;
207 $mime['dirpath'] = false;
208 $mime['basename'] = false;
209 $mime['suffix'] = false;
210 if ( isset( $mime['suffixes'][0] ) )
211 $mime['suffix'] = $mime['suffixes'][0];
212 $mime['prefix'] = false;
213 if ( isset( $mime['prefixes'][0] ) )
214 $mime['prefix'] = $mime['prefixes'][0];
215 $mime['is_valid'] = true;
216 return $mime;
217 }
218 return eZMimeType::defaultValue( false, $returnDefault );
219 }
220
227 static function findByURL( $url, $returnDefault = true )
228 {
229 $instance = eZMimeType::instance();
230
231 $file = $url;
232 $dirPosition = strrpos( $url, '/' );
233 if ( $dirPosition !== false )
234 $file = substr( $url, $dirPosition + 1 );
235 $suffixPosition = strrpos( $file, '.' );
236 $suffix = false;
237 $prefix = false;
238 $mimeName = false;
239 if ( $suffixPosition !== false )
240 {
241 $suffix = strtolower( substr( $file, $suffixPosition + 1 ) );
242 if ( $suffix )
243 {
244 $subURL = substr( $file, 0, $suffixPosition );
245 $suffixList =& $instance->SuffixList;
246 if ( array_key_exists( $suffix, $suffixList ) )
247 {
248 $mimeName = $suffixList[$suffix];
249 }
250 }
251 if ( !$mimeName )
252 {
253 $prefixPosition = strpos( $file, '.' );
254 if ( $prefixPosition !== false )
255 {
256 $prefix = strtolower( substr( $file, 0, $prefixPosition ) );
257 }
258 if ( $prefix )
259 {
260 $subURL = substr( $file, $suffixPosition + 1 );
261 $prefixList =& $instance->PrefixList;
262 if ( array_key_exists( $prefix, $prefixList ) )
263 {
264 $mimeName = $prefixList[$prefix];
265 }
266 }
267 }
268 if ( $mimeName )
269 {
270 $mimeList =& $instance->MIMEList;
271 if ( array_key_exists( $mimeName, $mimeList ) )
272 {
273 $lastDirPosition = strrpos( $url, '/' );
274 $filename = $url;
275 $dirpath = false;
276 if ( $lastDirPosition !== false )
277 {
278 $filename = substr( $url, $lastDirPosition + 1 );
279 $dirpath = substr( $url, 0, $lastDirPosition );
280 }
281 $lastDirPosition = strrpos( $subURL, '/' );
282 $basename = $subURL;
283 if ( $lastDirPosition !== false )
284 $basename = substr( $subURL, $lastDirPosition + 1 );
285 $mime = $mimeList[$mimeName];
286 $mime['name'] = $mimeName;
287 $mime['url'] = $url;
288 $mime['filename'] = $filename;
289 $mime['dirpath'] = $dirpath;
290 $mime['basename'] = $basename;
291 $mime['suffix'] = $suffix;
292 $mime['prefix'] = $prefix;
293 $mime['is_valid'] = true;
294 return $mime;
295 }
296 }
297 }
298 return eZMimeType::defaultValue( $url, $returnDefault );
299 }
300
308 static function findByFileContents( $url, $returnDefault = true )
309 {
310 return eZMimeType::findByURL( $url, $returnDefault );
311 }
312
323 static function findByBuffer( $buffer, $length = false, $offset = false, $url = false, $returnDefault = true )
324 {
325 return eZMimeType::findByURL( $url, $returnDefault );
326 }
327
333 static function instance()
334 {
335 $instance =& $GLOBALS['eZMIMETypeInstance'];
336 if ( !isset( $instance ) )
337 {
338 $instance = new eZMimeType();
339 }
340 return $instance;
341 }
342
347 static function prepareSuffixList( &$suffixList, $mimeList )
348 {
349 foreach ( $mimeList as $mimeName => $mimeData )
350 {
351 if ( is_array( $mimeData['suffixes'] ) )
352 {
353 foreach ( $mimeData['suffixes'] as $suffix )
354 {
355 if ( !isset( $suffixList[$suffix] ) )
356 $suffixList[$suffix] = $mimeName;
357 }
358 }
359 }
360 }
361
366 static function preparePrefixList( &$prefixList, $mimeList )
367 {
368 foreach ( $mimeList as $mimeName => $mimeData )
369 {
370 if ( is_array( $mimeData['prefixes'] ) )
371 {
372 foreach ( $mimeData['prefixes'] as $prefix )
373 {
374 if ( !isset( $prefixList[$prefix] ) )
375 $prefixList[$prefix] = $mimeName;
376 }
377 }
378 }
379 }
380
382
384 public $SuffixList;
386 public $PrefixList;
388 public $MIMEList;
389
390 public $QuickContentMatch = array(
391 array( array( 0, 'string', 'GIF87a', 'image/gif' ),
392 array( 0, 'string', 'GIF89a', 'image/gif' ) )
393 );
394
396 public $QuickMIMETypes = array();
397 }
398 ?>
eZMimeType
Detection and management of MIME types.
Definition: ezmimetype.php:34
eZMimeType\preparePrefixList
static preparePrefixList(&$prefixList, $mimeList)
Definition: ezmimetype.php:366
eZMimeType\findByName
static findByName( $mimeName, $returnDefault=true)
Definition: ezmimetype.php:197
eZMimeType\changeMIMEType
static changeMIMEType(&$mimeInfo, $mimetype)
Definition: ezmimetype.php:117
eZMimeType\defaultValue
static defaultValue( $url, $returnDefault)
Definition: ezmimetype.php:86
eZMimeType\$MIMEList
$MIMEList
An associative array which maps MIME type name to MIME structure.
Definition: ezmimetype.php:388
eZMimeType\changeDirectoryPath
static changeDirectoryPath(&$mimeInfo, $dirpath)
Definition: ezmimetype.php:155
eZMimeType\defaultMimeType
static defaultMimeType()
Definition: ezmimetype.php:68
eZMimeType\$SuffixList
$SuffixList
Definition: ezmimetype.php:384
eZMimeType\__construct
__construct()
Definition: ezmimetype.php:35
eZMimeType\$PrefixList
$PrefixList
An associative array which maps from prefix name to MIME type name.
Definition: ezmimetype.php:386
eZMimeType\changeFileData
static changeFileData(&$mimeInfo, $dirpath=false, $basename=false, $suffix=false, $filename=false)
Definition: ezmimetype.php:168
eZMimeType\$QuickMIMETypes
$QuickMIMETypes
A list of suffixes and their MIME types, this is used to quickly initialize the system.
Definition: ezmimetype.php:396
eZMimeType\changeBasename
static changeBasename(&$mimeInfo, $basename)
Definition: ezmimetype.php:141
eZMimeType\findByFileContents
static findByFileContents( $url, $returnDefault=true)
Definition: ezmimetype.php:308
eZMimeType\$QuickContentMatch
$QuickContentMatch
Definition: ezmimetype.php:390
eZMimeType\findByURL
static findByURL( $url, $returnDefault=true)
Definition: ezmimetype.php:227
eZMimeType\prepareSuffixList
static prepareSuffixList(&$suffixList, $mimeList)
Definition: ezmimetype.php:347
eZMimeType\findByBuffer
static findByBuffer( $buffer, $length=false, $offset=false, $url=false, $returnDefault=true)
Definition: ezmimetype.php:323
eZMimeType\instance
static instance()
Definition: ezmimetype.php:333
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.