1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2013 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.0 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_0.txt. |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Ilia Alshanetsky <ilia@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 /* $Id$ */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include "php.h"
25
26 #include <magic.h>
27 /*
28 * HOWMANY specifies the maximum offset libmagic will look at
29 * this is currently hardcoded in the libmagic source but not exported
30 */
31 #ifndef HOWMANY
32 #define HOWMANY 65536
33 #endif
34
35 #include "php_ini.h"
36 #include "ext/standard/info.h"
37 #include "ext/standard/file.h" /* needed for context stuff */
38 #include "php_fileinfo.h"
39 #include "fopen_wrappers.h" /* needed for is_url */
40
41 #ifndef _S_IFDIR
42 # define _S_IFDIR S_IFDIR
43 #endif
44
45 /* {{{ macros and type definitions */
46 struct php_fileinfo {
47 long options;
48 struct magic_set *magic;
49 };
50
51 static zend_object_handlers finfo_object_handlers;
52 zend_class_entry *finfo_class_entry;
53
54 struct finfo_object {
55 zend_object zo;
56 struct php_fileinfo *ptr;
57 };
58
59 #define FILEINFO_DECLARE_INIT_OBJECT(object) \
60 zval *object = getThis();
61
62 #define FILEINFO_REGISTER_OBJECT(_object, _ptr) \
63 { \
64 struct finfo_object *obj; \
65 obj = (struct finfo_object*)zend_object_store_get_object(_object TSRMLS_CC); \
66 obj->ptr = _ptr; \
67 }
68
69 #define FILEINFO_FROM_OBJECT(finfo, object) \
70 { \
71 struct finfo_object *obj = zend_object_store_get_object(object TSRMLS_CC); \
72 finfo = obj->ptr; \
73 if (!finfo) { \
74 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The invalid fileinfo object."); \
75 RETURN_FALSE; \
76 } \
77 }
78
79 /* {{{ finfo_objects_free
80 */
finfo_objects_free(void * object TSRMLS_DC)81 static void finfo_objects_free(void *object TSRMLS_DC)
82 {
83 struct finfo_object *intern = (struct finfo_object *) object;
84
85 if (intern->ptr) {
86 magic_close(intern->ptr->magic);
87 efree(intern->ptr);
88 }
89
90 zend_object_std_dtor(&intern->zo TSRMLS_CC);
91 efree(intern);
92 }
93 /* }}} */
94
95 /* {{{ finfo_objects_new
96 */
finfo_objects_new(zend_class_entry * class_type TSRMLS_DC)97 PHP_FILEINFO_API zend_object_value finfo_objects_new(zend_class_entry *class_type TSRMLS_DC)
98 {
99 zend_object_value retval;
100 struct finfo_object *intern;
101 zval *tmp;
102
103 intern = emalloc(sizeof(struct finfo_object));
104 memset(intern, 0, sizeof(struct finfo_object));
105
106 zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
107 zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_property_ctor,(void *) &tmp, sizeof(zval *));
108
109 intern->ptr = NULL;
110
111 retval.handle = zend_objects_store_put(intern, NULL,
112 finfo_objects_free, NULL TSRMLS_CC);
113 retval.handlers = (zend_object_handlers *) &finfo_object_handlers;
114
115 return retval;
116 }
117 /* }}} */
118
119 /* {{{ arginfo */
120 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_open, 0, 0, 0)
121 ZEND_ARG_INFO(0, options)
122 ZEND_ARG_INFO(0, arg)
123 ZEND_END_ARG_INFO()
124
125 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_close, 0, 0, 1)
126 ZEND_ARG_INFO(0, finfo)
127 ZEND_END_ARG_INFO()
128
129 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_set_flags, 0, 0, 2)
130 ZEND_ARG_INFO(0, finfo)
131 ZEND_ARG_INFO(0, options)
132 ZEND_END_ARG_INFO()
133
134 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_method_set_flags, 0, 0, 1)
135 ZEND_ARG_INFO(0, options)
136 ZEND_END_ARG_INFO()
137
138 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_file, 0, 0, 2)
139 ZEND_ARG_INFO(0, finfo)
140 ZEND_ARG_INFO(0, filename)
141 ZEND_ARG_INFO(0, options)
142 ZEND_ARG_INFO(0, context)
143 ZEND_END_ARG_INFO()
144
145 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_method_file, 0, 0, 1)
146 ZEND_ARG_INFO(0, filename)
147 ZEND_ARG_INFO(0, options)
148 ZEND_ARG_INFO(0, context)
149 ZEND_END_ARG_INFO()
150
151 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_buffer, 0, 0, 2)
152 ZEND_ARG_INFO(0, finfo)
153 ZEND_ARG_INFO(0, string)
154 ZEND_ARG_INFO(0, options)
155 ZEND_ARG_INFO(0, context)
156 ZEND_END_ARG_INFO()
157
158 ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_method_buffer, 0, 0, 1)
159 ZEND_ARG_INFO(0, string)
160 ZEND_ARG_INFO(0, options)
161 ZEND_ARG_INFO(0, context)
162 ZEND_END_ARG_INFO()
163
164 ZEND_BEGIN_ARG_INFO_EX(arginfo_mime_content_type, 0, 0, 1)
165 ZEND_ARG_INFO(0, string)
166 ZEND_END_ARG_INFO()
167 /* }}} */
168
169 /* {{{ finfo_class_functions
170 */
171 zend_function_entry finfo_class_functions[] = {
172 ZEND_ME_MAPPING(finfo, finfo_open, arginfo_finfo_open, ZEND_ACC_PUBLIC)
173 ZEND_ME_MAPPING(set_flags, finfo_set_flags,arginfo_finfo_method_set_flags, ZEND_ACC_PUBLIC)
174 ZEND_ME_MAPPING(file, finfo_file, arginfo_finfo_method_file, ZEND_ACC_PUBLIC)
175 ZEND_ME_MAPPING(buffer, finfo_buffer, arginfo_finfo_method_buffer, ZEND_ACC_PUBLIC)
176 PHP_FE_END
177 };
178 /* }}} */
179
180 #define FINFO_SET_OPTION(magic, options) \
181 if (magic_setflags(magic, options) == -1) { \
182 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to set option '%ld' %d:%s", \
183 options, magic_errno(magic), magic_error(magic)); \
184 RETURN_FALSE; \
185 }
186
187 /* True global resources - no need for thread safety here */
188 static int le_fileinfo;
189 /* }}} */
190
finfo_resource_destructor(zend_rsrc_list_entry * rsrc TSRMLS_DC)191 void finfo_resource_destructor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
192 {
193 if (rsrc->ptr) {
194 struct php_fileinfo *finfo = (struct php_fileinfo *) rsrc->ptr;
195 magic_close(finfo->magic);
196 efree(rsrc->ptr);
197 rsrc->ptr = NULL;
198 }
199 }
200 /* }}} */
201
202
203 /* {{{ fileinfo_functions[]
204 */
205 zend_function_entry fileinfo_functions[] = {
206 PHP_FE(finfo_open, arginfo_finfo_open)
207 PHP_FE(finfo_close, arginfo_finfo_close)
208 PHP_FE(finfo_set_flags, arginfo_finfo_set_flags)
209 PHP_FE(finfo_file, arginfo_finfo_file)
210 PHP_FE(finfo_buffer, arginfo_finfo_buffer)
211 PHP_FE(mime_content_type, arginfo_mime_content_type)
212 {NULL, NULL, NULL}
213 };
214 /* }}} */
215
216 /* {{{ PHP_MINIT_FUNCTION
217 */
PHP_MINIT_FUNCTION(finfo)218 PHP_MINIT_FUNCTION(finfo)
219 {
220 zend_class_entry _finfo_class_entry;
221 INIT_CLASS_ENTRY(_finfo_class_entry, "finfo", finfo_class_functions);
222 _finfo_class_entry.create_object = finfo_objects_new;
223 finfo_class_entry = zend_register_internal_class(&_finfo_class_entry TSRMLS_CC);
224
225 /* copy the standard object handlers to you handler table */
226 memcpy(&finfo_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
227
228 le_fileinfo = zend_register_list_destructors_ex(finfo_resource_destructor, NULL, "file_info", module_number);
229
230 REGISTER_LONG_CONSTANT("FILEINFO_NONE", MAGIC_NONE, CONST_CS|CONST_PERSISTENT);
231 REGISTER_LONG_CONSTANT("FILEINFO_SYMLINK", MAGIC_SYMLINK, CONST_CS|CONST_PERSISTENT);
232 REGISTER_LONG_CONSTANT("FILEINFO_MIME", MAGIC_MIME, CONST_CS|CONST_PERSISTENT);
233 REGISTER_LONG_CONSTANT("FILEINFO_MIME_TYPE", MAGIC_MIME_TYPE, CONST_CS|CONST_PERSISTENT);
234 REGISTER_LONG_CONSTANT("FILEINFO_MIME_ENCODING",MAGIC_MIME_ENCODING, CONST_CS|CONST_PERSISTENT);
235 /* REGISTER_LONG_CONSTANT("FILEINFO_COMPRESS", MAGIC_COMPRESS, CONST_CS|CONST_PERSISTENT); disabled, as it does fork now */
236 REGISTER_LONG_CONSTANT("FILEINFO_DEVICES", MAGIC_DEVICES, CONST_CS|CONST_PERSISTENT);
237 REGISTER_LONG_CONSTANT("FILEINFO_CONTINUE", MAGIC_CONTINUE, CONST_CS|CONST_PERSISTENT);
238 #ifdef MAGIC_PRESERVE_ATIME
239 REGISTER_LONG_CONSTANT("FILEINFO_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME, CONST_CS|CONST_PERSISTENT);
240 #endif
241 #ifdef MAGIC_RAW
242 REGISTER_LONG_CONSTANT("FILEINFO_RAW", MAGIC_RAW, CONST_CS|CONST_PERSISTENT);
243 #endif
244
245 return SUCCESS;
246 }
247 /* }}} */
248
249 /* {{{ fileinfo_module_entry
250 */
251 zend_module_entry fileinfo_module_entry = {
252 STANDARD_MODULE_HEADER,
253 "fileinfo",
254 fileinfo_functions,
255 PHP_MINIT(finfo),
256 NULL,
257 NULL,
258 NULL,
259 PHP_MINFO(fileinfo),
260 PHP_FILEINFO_VERSION,
261 STANDARD_MODULE_PROPERTIES
262 };
263 /* }}} */
264
265 #ifdef COMPILE_DL_FILEINFO
266 ZEND_GET_MODULE(fileinfo)
267 #endif
268
269 /* {{{ PHP_MINFO_FUNCTION
270 */
PHP_MINFO_FUNCTION(fileinfo)271 PHP_MINFO_FUNCTION(fileinfo)
272 {
273 php_info_print_table_start();
274 php_info_print_table_row(2, "fileinfo support", "enabled");
275 php_info_print_table_row(2, "version", PHP_FILEINFO_VERSION);
276 php_info_print_table_end();
277 }
278 /* }}} */
279
280 #define FILEINFO_DESTROY_OBJECT(object) \
281 do { \
282 if (object) { \
283 zend_object_store_ctor_failed(object TSRMLS_CC); \
284 zval_dtor(object); \
285 ZVAL_NULL(object); \
286 } \
287 } while (0)
288
289 /* {{{ proto resource finfo_open([int options [, string arg]])
290 Create a new fileinfo resource. */
PHP_FUNCTION(finfo_open)291 PHP_FUNCTION(finfo_open)
292 {
293 long options = MAGIC_NONE;
294 char *file = NULL;
295 int file_len = 0;
296 struct php_fileinfo *finfo;
297 FILEINFO_DECLARE_INIT_OBJECT(object)
298 char resolved_path[MAXPATHLEN];
299
300 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &options, &file, &file_len) == FAILURE) {
301 FILEINFO_DESTROY_OBJECT(object);
302 RETURN_FALSE;
303 }
304
305 if (object) {
306 struct finfo_object *finfo_obj = (struct finfo_object*)zend_object_store_get_object(object TSRMLS_CC);
307
308 if (finfo_obj->ptr) {
309 magic_close(finfo_obj->ptr->magic);
310 efree(finfo_obj->ptr);
311 finfo_obj->ptr = NULL;
312 }
313 }
314
315 if (file_len == 0) {
316 file = NULL;
317 } else if (file && *file) { /* user specified file, perform open_basedir checks */
318 if (strlen(file) != file_len) {
319 FILEINFO_DESTROY_OBJECT(object);
320 RETURN_FALSE;
321 }
322 if (!VCWD_REALPATH(file, resolved_path)) {
323 FILEINFO_DESTROY_OBJECT(object);
324 RETURN_FALSE;
325 }
326 file = resolved_path;
327
328 #if PHP_API_VERSION < 20100412
329 if ((PG(safe_mode) && (!php_checkuid(file, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(file TSRMLS_CC)) {
330 #else
331 if (php_check_open_basedir(file TSRMLS_CC)) {
332 #endif
333 FILEINFO_DESTROY_OBJECT(object);
334 RETURN_FALSE;
335 }
336 }
337
338 finfo = emalloc(sizeof(struct php_fileinfo));
339
340 finfo->options = options;
341 finfo->magic = magic_open(options);
342
343 if (finfo->magic == NULL) {
344 efree(finfo);
345 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid mode '%ld'.", options);
346 FILEINFO_DESTROY_OBJECT(object);
347 RETURN_FALSE;
348 }
349
350 if (magic_load(finfo->magic, file) == -1) {
351 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to load magic database at '%s'.", file);
352 magic_close(finfo->magic);
353 efree(finfo);
354 FILEINFO_DESTROY_OBJECT(object);
355 RETURN_FALSE;
356 }
357
358 if (object) {
359 FILEINFO_REGISTER_OBJECT(object, finfo);
360 } else {
361 ZEND_REGISTER_RESOURCE(return_value, finfo, le_fileinfo);
362 }
363 }
364 /* }}} */
365
366 /* {{{ proto resource finfo_close(resource finfo)
367 Close fileinfo resource. */
368 PHP_FUNCTION(finfo_close)
369 {
370 struct php_fileinfo *finfo;
371 zval *zfinfo;
372
373 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zfinfo) == FAILURE) {
374 RETURN_FALSE;
375 }
376 ZEND_FETCH_RESOURCE(finfo, struct php_fileinfo *, &zfinfo, -1, "file_info", le_fileinfo);
377
378 zend_list_delete(Z_RESVAL_P(zfinfo));
379
380 RETURN_TRUE;
381 }
382 /* }}} */
383
384 /* {{{ proto bool finfo_set_flags(resource finfo, int options)
385 Set libmagic configuration options. */
386 PHP_FUNCTION(finfo_set_flags)
387 {
388 long options;
389 struct php_fileinfo *finfo;
390 zval *zfinfo;
391 FILEINFO_DECLARE_INIT_OBJECT(object)
392
393 if (object) {
394 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &options) == FAILURE) {
395 RETURN_FALSE;
396 }
397 FILEINFO_FROM_OBJECT(finfo, object);
398 } else {
399 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zfinfo, &options) == FAILURE) {
400 RETURN_FALSE;
401 }
402 ZEND_FETCH_RESOURCE(finfo, struct php_fileinfo *, &zfinfo, -1, "file_info", le_fileinfo);
403 }
404
405 FINFO_SET_OPTION(finfo->magic, options)
406 finfo->options = options;
407
408 RETURN_TRUE;
409 }
410 /* }}} */
411
412 #define FILEINFO_MODE_BUFFER 0
413 #define FILEINFO_MODE_STREAM 1
414 #define FILEINFO_MODE_FILE 2
415
416 static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mimetype_emu) /* {{{ */
417 {
418 long options = 0;
419 char *ret_val = NULL, *buffer = NULL;
420 int buffer_len;
421 struct php_fileinfo *finfo = NULL;
422 zval *zfinfo, *zcontext = NULL;
423 zval *what;
424 char mime_directory[] = "directory";
425
426 struct magic_set *magic = NULL;
427 FILEINFO_DECLARE_INIT_OBJECT(object)
428
429 if (mimetype_emu) {
430
431 /* mime_content_type(..) emulation */
432 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &what) == FAILURE) {
433 return;
434 }
435
436 switch (Z_TYPE_P(what)) {
437 case IS_STRING:
438 buffer = Z_STRVAL_P(what);
439 buffer_len = Z_STRLEN_P(what);
440 mode = FILEINFO_MODE_FILE;
441 break;
442
443 case IS_RESOURCE:
444 mode = FILEINFO_MODE_STREAM;
445 break;
446
447 default:
448 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only process string or stream arguments");
449 RETURN_FALSE;
450 }
451
452 magic = magic_open(MAGIC_MIME_TYPE);
453 if (magic_load(magic, NULL) == -1) {
454 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to load magic database.");
455 goto common;
456 }
457 } else if (object) {
458 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|lr", &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
459 RETURN_FALSE;
460 }
461 FILEINFO_FROM_OBJECT(finfo, object);
462 magic = finfo->magic;
463 } else {
464 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|lr", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
465 RETURN_FALSE;
466 }
467 ZEND_FETCH_RESOURCE(finfo, struct php_fileinfo *, &zfinfo, -1, "file_info", le_fileinfo);
468 magic = finfo->magic;
469 }
470
471 /* Set options for the current file/buffer. */
472 if (options) {
473 FINFO_SET_OPTION(magic, options)
474 }
475
476 switch (mode) {
477 case FILEINFO_MODE_BUFFER:
478 {
479 ret_val = (char *) magic_buffer(magic, buffer, buffer_len);
480 break;
481 }
482
483 case FILEINFO_MODE_STREAM:
484 {
485 php_stream *stream;
486 off_t streampos;
487
488 php_stream_from_zval_no_verify(stream, &what);
489 if (!stream) {
490 goto common;
491 }
492
493 streampos = php_stream_tell(stream); /* remember stream position for restoration */
494 php_stream_seek(stream, 0, SEEK_SET);
495
496 ret_val = (char *) magic_stream(magic, stream);
497
498 php_stream_seek(stream, streampos, SEEK_SET);
499 break;
500 }
501
502 case FILEINFO_MODE_FILE:
503 {
504 /* determine if the file is a local file or remote URL */
505 char *tmp2;
506 php_stream_wrapper *wrap;
507 php_stream_statbuf ssb;
508
509 if (buffer == NULL || !*buffer) {
510 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty filename or path");
511 RETVAL_FALSE;
512 goto clean;
513 }
514
515 wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0 TSRMLS_CC);
516
517 if (wrap) {
518 php_stream *stream;
519 php_stream_context *context = php_stream_context_from_zval(zcontext, 0);
520
521 #ifdef PHP_WIN32
522 if (php_stream_stat_path_ex(buffer, 0, &ssb, context) == SUCCESS) {
523 if (ssb.sb.st_mode & S_IFDIR) {
524 ret_val = mime_directory;
525 goto common;
526 }
527 }
528 #endif
529
530 #if PHP_API_VERSION < 20100412
531 stream = php_stream_open_wrapper_ex(buffer, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, context);
532 #else
533 stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context);
534 #endif
535
536 if (!stream) {
537 RETVAL_FALSE;
538 goto clean;
539 }
540
541 if (php_stream_stat(stream, &ssb) == SUCCESS) {
542 if (ssb.sb.st_mode & S_IFDIR) {
543 ret_val = mime_directory;
544 } else {
545 ret_val = (char *)magic_stream(magic, stream);
546 }
547 }
548
549 php_stream_close(stream);
550 }
551 break;
552 }
553
554 default:
555 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only process string or stream arguments");
556 }
557
558 common:
559 if (ret_val) {
560 RETVAL_STRING(ret_val, 1);
561 } else {
562 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic));
563 RETVAL_FALSE;
564 }
565
566 clean:
567 if (mimetype_emu) {
568 magic_close(magic);
569 }
570
571 /* Restore options */
572 if (options) {
573 FINFO_SET_OPTION(magic, finfo->options)
574 }
575 return;
576 }
577 /* }}} */
578
579 /* {{{ proto string finfo_file(resource finfo, char *file_name [, int options [, resource context]])
580 Return information about a file. */
581 PHP_FUNCTION(finfo_file)
582 {
583 _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_FILE, 0);
584 }
585 /* }}} */
586
587 /* {{{ proto string finfo_buffer(resource finfo, char *string [, int options [, resource context]])
588 Return infromation about a string buffer. */
589 PHP_FUNCTION(finfo_buffer)
590 {
591 _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_BUFFER, 0);
592 }
593 /* }}} */
594
595 /* {{{ proto string mime_content_type(string filename|resource stream)
596 Return content-type for file */
597 PHP_FUNCTION(mime_content_type)
598 {
599 _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1, 1);
600 }
601 /* }}} */
602
603
604 /*
605 * Local variables:
606 * tab-width: 4
607 * c-basic-offset: 4
608 * End:
609 * vim600: noet sw=4 ts=4 fdm=marker
610 * vim<600: noet sw=4 ts=4
611 */
612