fileinfo.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Ilia Alshanetsky <ilia@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "libmagic/magic.h"
  21. /*
  22. * HOWMANY specifies the maximum offset libmagic will look at
  23. * this is currently hardcoded in the libmagic source but not exported
  24. */
  25. #ifndef HOWMANY
  26. #define HOWMANY 65536
  27. #endif
  28. #include "php_ini.h"
  29. #include "ext/standard/info.h"
  30. #include "ext/standard/file.h" /* needed for context stuff */
  31. #include "php_fileinfo.h"
  32. #include "fileinfo_arginfo.h"
  33. #include "fopen_wrappers.h" /* needed for is_url */
  34. #include "Zend/zend_exceptions.h"
  35. /* {{{ macros and type definitions */
  36. typedef struct _php_fileinfo {
  37. zend_long options;
  38. struct magic_set *magic;
  39. } php_fileinfo;
  40. static zend_object_handlers finfo_object_handlers;
  41. zend_class_entry *finfo_class_entry;
  42. typedef struct _finfo_object {
  43. php_fileinfo *ptr;
  44. zend_object zo;
  45. } finfo_object;
  46. static inline finfo_object *php_finfo_fetch_object(zend_object *obj) {
  47. return (finfo_object *)((char*)(obj) - XtOffsetOf(finfo_object, zo));
  48. }
  49. #define Z_FINFO_P(zv) php_finfo_fetch_object(Z_OBJ_P((zv)))
  50. #define FILEINFO_FROM_OBJECT(finfo, object) \
  51. { \
  52. finfo_object *obj = Z_FINFO_P(object); \
  53. finfo = obj->ptr; \
  54. if (!finfo) { \
  55. zend_throw_error(NULL, "Invalid finfo object"); \
  56. RETURN_THROWS(); \
  57. } \
  58. }
  59. /* {{{ finfo_objects_free */
  60. static void finfo_objects_free(zend_object *object)
  61. {
  62. finfo_object *intern = php_finfo_fetch_object(object);
  63. if (intern->ptr) {
  64. magic_close(intern->ptr->magic);
  65. efree(intern->ptr);
  66. }
  67. zend_object_std_dtor(&intern->zo);
  68. }
  69. /* }}} */
  70. /* {{{ finfo_objects_new */
  71. PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type)
  72. {
  73. finfo_object *intern;
  74. intern = zend_object_alloc(sizeof(finfo_object), class_type);
  75. zend_object_std_init(&intern->zo, class_type);
  76. object_properties_init(&intern->zo, class_type);
  77. intern->zo.handlers = &finfo_object_handlers;
  78. return &intern->zo;
  79. }
  80. /* }}} */
  81. #define FINFO_SET_OPTION(magic, options) \
  82. if (magic_setflags(magic, options) == -1) { \
  83. php_error_docref(NULL, E_WARNING, "Failed to set option '" ZEND_LONG_FMT "' %d:%s", \
  84. options, magic_errno(magic), magic_error(magic)); \
  85. RETURN_FALSE; \
  86. }
  87. /* }}} */
  88. /* {{{ PHP_MINIT_FUNCTION */
  89. PHP_MINIT_FUNCTION(finfo)
  90. {
  91. finfo_class_entry = register_class_finfo();
  92. finfo_class_entry->create_object = finfo_objects_new;
  93. /* copy the standard object handlers to you handler table */
  94. memcpy(&finfo_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  95. finfo_object_handlers.offset = XtOffsetOf(finfo_object, zo);
  96. finfo_object_handlers.free_obj = finfo_objects_free;
  97. finfo_object_handlers.clone_obj = NULL;
  98. REGISTER_LONG_CONSTANT("FILEINFO_NONE", MAGIC_NONE, CONST_CS|CONST_PERSISTENT);
  99. REGISTER_LONG_CONSTANT("FILEINFO_SYMLINK", MAGIC_SYMLINK, CONST_CS|CONST_PERSISTENT);
  100. REGISTER_LONG_CONSTANT("FILEINFO_MIME", MAGIC_MIME, CONST_CS|CONST_PERSISTENT);
  101. REGISTER_LONG_CONSTANT("FILEINFO_MIME_TYPE", MAGIC_MIME_TYPE, CONST_CS|CONST_PERSISTENT);
  102. REGISTER_LONG_CONSTANT("FILEINFO_MIME_ENCODING",MAGIC_MIME_ENCODING, CONST_CS|CONST_PERSISTENT);
  103. /* REGISTER_LONG_CONSTANT("FILEINFO_COMPRESS", MAGIC_COMPRESS, CONST_CS|CONST_PERSISTENT); disabled, as it does fork now */
  104. REGISTER_LONG_CONSTANT("FILEINFO_DEVICES", MAGIC_DEVICES, CONST_CS|CONST_PERSISTENT);
  105. REGISTER_LONG_CONSTANT("FILEINFO_CONTINUE", MAGIC_CONTINUE, CONST_CS|CONST_PERSISTENT);
  106. #ifdef MAGIC_PRESERVE_ATIME
  107. REGISTER_LONG_CONSTANT("FILEINFO_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME, CONST_CS|CONST_PERSISTENT);
  108. #endif
  109. #ifdef MAGIC_RAW
  110. REGISTER_LONG_CONSTANT("FILEINFO_RAW", MAGIC_RAW, CONST_CS|CONST_PERSISTENT);
  111. #endif
  112. #if 0
  113. /* seems not usable yet. */
  114. REGISTER_LONG_CONSTANT("FILEINFO_APPLE", MAGIC_APPLE, CONST_CS|CONST_PERSISTENT);
  115. #endif
  116. REGISTER_LONG_CONSTANT("FILEINFO_EXTENSION", MAGIC_EXTENSION, CONST_CS|CONST_PERSISTENT);
  117. return SUCCESS;
  118. }
  119. /* }}} */
  120. /* {{{ fileinfo_module_entry */
  121. zend_module_entry fileinfo_module_entry = {
  122. STANDARD_MODULE_HEADER,
  123. "fileinfo",
  124. ext_functions,
  125. PHP_MINIT(finfo),
  126. NULL,
  127. NULL,
  128. NULL,
  129. PHP_MINFO(fileinfo),
  130. PHP_FILEINFO_VERSION,
  131. STANDARD_MODULE_PROPERTIES
  132. };
  133. /* }}} */
  134. #ifdef COMPILE_DL_FILEINFO
  135. ZEND_GET_MODULE(fileinfo)
  136. #endif
  137. /* {{{ PHP_MINFO_FUNCTION */
  138. PHP_MINFO_FUNCTION(fileinfo)
  139. {
  140. char magic_ver[5];
  141. (void)snprintf(magic_ver, 4, "%d", magic_version());
  142. magic_ver[4] = '\0';
  143. php_info_print_table_start();
  144. php_info_print_table_row(2, "fileinfo support", "enabled");
  145. php_info_print_table_row(2, "libmagic", magic_ver);
  146. php_info_print_table_end();
  147. }
  148. /* }}} */
  149. /* {{{ Construct a new fileinfo object. */
  150. PHP_FUNCTION(finfo_open)
  151. {
  152. zend_long options = MAGIC_NONE;
  153. char *file = NULL;
  154. size_t file_len = 0;
  155. php_fileinfo *finfo;
  156. zval *object = getThis();
  157. char resolved_path[MAXPATHLEN];
  158. zend_error_handling zeh;
  159. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lp!", &options, &file, &file_len) == FAILURE) {
  160. RETURN_THROWS();
  161. }
  162. if (object) {
  163. finfo_object *finfo_obj = Z_FINFO_P(object);
  164. zend_replace_error_handling(EH_THROW, NULL, &zeh);
  165. if (finfo_obj->ptr) {
  166. magic_close(finfo_obj->ptr->magic);
  167. efree(finfo_obj->ptr);
  168. finfo_obj->ptr = NULL;
  169. }
  170. }
  171. if (file_len == 0) {
  172. file = NULL;
  173. } else if (file && *file) { /* user specified file, perform open_basedir checks */
  174. if (php_check_open_basedir(file)) {
  175. if (object) {
  176. zend_restore_error_handling(&zeh);
  177. if (!EG(exception)) {
  178. zend_throw_exception(NULL, "Constructor failed", 0);
  179. }
  180. }
  181. RETURN_FALSE;
  182. }
  183. if (!expand_filepath_with_mode(file, resolved_path, NULL, 0, CWD_EXPAND)) {
  184. if (object) {
  185. zend_restore_error_handling(&zeh);
  186. if (!EG(exception)) {
  187. zend_throw_exception(NULL, "Constructor failed", 0);
  188. }
  189. }
  190. RETURN_FALSE;
  191. }
  192. file = resolved_path;
  193. }
  194. finfo = emalloc(sizeof(php_fileinfo));
  195. finfo->options = options;
  196. finfo->magic = magic_open(options);
  197. if (finfo->magic == NULL) {
  198. efree(finfo);
  199. php_error_docref(NULL, E_WARNING, "Invalid mode '" ZEND_LONG_FMT "'.", options);
  200. if (object) {
  201. zend_restore_error_handling(&zeh);
  202. if (!EG(exception)) {
  203. zend_throw_exception(NULL, "Constructor failed", 0);
  204. }
  205. }
  206. RETURN_FALSE;
  207. }
  208. if (magic_load(finfo->magic, file) == -1) {
  209. php_error_docref(NULL, E_WARNING, "Failed to load magic database at \"%s\"", file);
  210. magic_close(finfo->magic);
  211. efree(finfo);
  212. if (object) {
  213. zend_restore_error_handling(&zeh);
  214. if (!EG(exception)) {
  215. zend_throw_exception(NULL, "Constructor failed", 0);
  216. }
  217. }
  218. RETURN_FALSE;
  219. }
  220. if (object) {
  221. finfo_object *obj;
  222. zend_restore_error_handling(&zeh);
  223. obj = Z_FINFO_P(object);
  224. obj->ptr = finfo;
  225. } else {
  226. zend_object *zobj = finfo_objects_new(finfo_class_entry);
  227. finfo_object *obj = php_finfo_fetch_object(zobj);
  228. obj->ptr = finfo;
  229. RETURN_OBJ(zobj);
  230. }
  231. }
  232. /* }}} */
  233. /* {{{ Close fileinfo object - a NOP. */
  234. PHP_FUNCTION(finfo_close)
  235. {
  236. zval *self;
  237. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &self, finfo_class_entry) == FAILURE) {
  238. RETURN_THROWS();
  239. }
  240. RETURN_TRUE;
  241. }
  242. /* }}} */
  243. /* {{{ Set libmagic configuration options. */
  244. PHP_FUNCTION(finfo_set_flags)
  245. {
  246. zend_long options;
  247. php_fileinfo *finfo;
  248. zval *self;
  249. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &self, finfo_class_entry, &options) == FAILURE) {
  250. RETURN_THROWS();
  251. }
  252. FILEINFO_FROM_OBJECT(finfo, self);
  253. FINFO_SET_OPTION(finfo->magic, options)
  254. finfo->options = options;
  255. RETURN_TRUE;
  256. }
  257. /* }}} */
  258. #define FILEINFO_MODE_BUFFER 0
  259. #define FILEINFO_MODE_STREAM 1
  260. #define FILEINFO_MODE_FILE 2
  261. static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mimetype_emu) /* {{{ */
  262. {
  263. zend_long options = 0;
  264. char *ret_val = NULL, *buffer = NULL;
  265. size_t buffer_len;
  266. php_fileinfo *finfo = NULL;
  267. zval *zcontext = NULL;
  268. zval *what;
  269. char mime_directory[] = "directory";
  270. struct magic_set *magic = NULL;
  271. if (mimetype_emu) {
  272. /* mime_content_type(..) emulation */
  273. if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &what) == FAILURE) {
  274. RETURN_THROWS();
  275. }
  276. switch (Z_TYPE_P(what)) {
  277. case IS_STRING:
  278. buffer = Z_STRVAL_P(what);
  279. buffer_len = Z_STRLEN_P(what);
  280. mode = FILEINFO_MODE_FILE;
  281. break;
  282. case IS_RESOURCE:
  283. mode = FILEINFO_MODE_STREAM;
  284. break;
  285. default:
  286. zend_argument_type_error(1, "must be of type resource|string, %s given", zend_zval_type_name(what));
  287. RETURN_THROWS();
  288. }
  289. magic = magic_open(MAGIC_MIME_TYPE);
  290. if (magic_load(magic, NULL) == -1) {
  291. php_error_docref(NULL, E_WARNING, "Failed to load magic database");
  292. goto common;
  293. }
  294. } else {
  295. zval *self;
  296. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|lr!", &self, finfo_class_entry, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
  297. RETURN_THROWS();
  298. }
  299. FILEINFO_FROM_OBJECT(finfo, self);
  300. magic = finfo->magic;
  301. }
  302. /* Set options for the current file/buffer. */
  303. if (options) {
  304. FINFO_SET_OPTION(magic, options)
  305. }
  306. switch (mode) {
  307. case FILEINFO_MODE_BUFFER:
  308. {
  309. ret_val = (char *) magic_buffer(magic, buffer, buffer_len);
  310. break;
  311. }
  312. case FILEINFO_MODE_STREAM:
  313. {
  314. php_stream *stream;
  315. zend_off_t streampos;
  316. php_stream_from_zval_no_verify(stream, what);
  317. if (!stream) {
  318. goto common;
  319. }
  320. streampos = php_stream_tell(stream); /* remember stream position for restoration */
  321. php_stream_seek(stream, 0, SEEK_SET);
  322. ret_val = (char *) magic_stream(magic, stream);
  323. php_stream_seek(stream, streampos, SEEK_SET);
  324. break;
  325. }
  326. case FILEINFO_MODE_FILE:
  327. {
  328. /* determine if the file is a local file or remote URL */
  329. const char *tmp2;
  330. php_stream_wrapper *wrap;
  331. php_stream_statbuf ssb;
  332. if (buffer == NULL || buffer_len == 0) {
  333. zend_argument_value_error(1, "cannot be empty");
  334. goto clean;
  335. }
  336. if (CHECK_NULL_PATH(buffer, buffer_len)) {
  337. zend_argument_type_error(1, "must not contain any null bytes");
  338. goto clean;
  339. }
  340. wrap = php_stream_locate_url_wrapper(buffer, &tmp2, 0);
  341. if (wrap) {
  342. php_stream *stream;
  343. php_stream_context *context = php_stream_context_from_zval(zcontext, 0);
  344. #ifdef PHP_WIN32
  345. if (php_stream_stat_path_ex(buffer, 0, &ssb, context) == SUCCESS) {
  346. if (ssb.sb.st_mode & S_IFDIR) {
  347. ret_val = mime_directory;
  348. goto common;
  349. }
  350. }
  351. #endif
  352. stream = php_stream_open_wrapper_ex(buffer, "rb", REPORT_ERRORS, NULL, context);
  353. if (!stream) {
  354. RETVAL_FALSE;
  355. goto clean;
  356. }
  357. if (php_stream_stat(stream, &ssb) == SUCCESS) {
  358. if (ssb.sb.st_mode & S_IFDIR) {
  359. ret_val = mime_directory;
  360. } else {
  361. ret_val = (char *)magic_stream(magic, stream);
  362. }
  363. }
  364. php_stream_close(stream);
  365. }
  366. break;
  367. }
  368. EMPTY_SWITCH_DEFAULT_CASE()
  369. }
  370. common:
  371. if (ret_val) {
  372. RETVAL_STRING(ret_val);
  373. } else {
  374. php_error_docref(NULL, E_WARNING, "Failed identify data %d:%s", magic_errno(magic), magic_error(magic));
  375. RETVAL_FALSE;
  376. }
  377. clean:
  378. if (mimetype_emu) {
  379. magic_close(magic);
  380. }
  381. /* Restore options */
  382. if (options) {
  383. FINFO_SET_OPTION(magic, finfo->options)
  384. }
  385. return;
  386. }
  387. /* }}} */
  388. /* {{{ Return information about a file. */
  389. PHP_FUNCTION(finfo_file)
  390. {
  391. _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_FILE, 0);
  392. }
  393. /* }}} */
  394. /* {{{ Return information about a string buffer. */
  395. PHP_FUNCTION(finfo_buffer)
  396. {
  397. _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, FILEINFO_MODE_BUFFER, 0);
  398. }
  399. /* }}} */
  400. /* {{{ Return content-type for file */
  401. PHP_FUNCTION(mime_content_type)
  402. {
  403. _php_finfo_get_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, -1, 1);
  404. }
  405. /* }}} */