- fix very silly behaviour of libmagic if we want to get the mime type intead
authorMichael Wallner <mike@php.net>
Wed, 21 Sep 2005 12:03:44 +0000 (12:03 +0000)
committerMichael Wallner <mike@php.net>
Wed, 21 Sep 2005 12:03:44 +0000 (12:03 +0000)
  of a human readable string, which is usually the default use case

# background: libmagic appends ".mime" to the magic file name if MAGIC_MIME is set (sic!)

http_api.c
http_response_object.c

index abc2c9beeb155a925b1b4e8dcf52568bc8ba0d86..eec41daa0d7068d964cc26ab7377e15dc6b78fba 100644 (file)
@@ -358,15 +358,19 @@ PHP_HTTP_API char *_http_guess_content_type(const char *magicfile, long magicmod
        char *ct = NULL;
 
 #ifdef HTTP_HAVE_MAGIC
        char *ct = NULL;
 
 #ifdef HTTP_HAVE_MAGIC
-       struct magic_set *magic = magic_open(magicmode);
+       /*      magic_load() fails if MAGIC_MIME is set because it 
+               cowardly adds .mime to the file name */
+       struct magic_set *magic = magic_open(magicmode &~ MAGIC_MIME);
        
        if (!magic) {
                http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode);
        } else if (-1 == magic_load(magic, magicfile)) {
        
        if (!magic) {
                http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode);
        } else if (-1 == magic_load(magic, magicfile)) {
-               http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s'", magicfile);
+               http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s' (%s)", magicfile, magic_error(magic));
        } else {
                const char *ctype = NULL;
                
        } else {
                const char *ctype = NULL;
                
+               magic_setflags(magic, magicmode);
+               
                switch (data_mode)
                {
                        case SEND_RSRC:
                switch (data_mode)
                {
                        case SEND_RSRC:
index dfc2476a8819259df634f5b2dbad0577cf9c3cf2..c0c66af090543aee016a5822866e0a04adee15c4 100644 (file)
@@ -500,7 +500,7 @@ PHP_METHOD(HttpResponse, getContentType)
 }
 /* }}} */
 
 }
 /* }}} */
 
-/* {{{ proto static string HttpResponse::guessContentType(string magic_file[, long magic_mode])
+/* {{{ proto static string HttpResponse::guessContentType(string magic_file[, long magic_mode = MAGIC_MIME])
  *
  * Attempts to guess the content type of supplied payload through libmagic.
  */
  *
  * Attempts to guess the content type of supplied payload through libmagic.
  */
@@ -512,6 +512,9 @@ PHP_METHOD(HttpResponse, guessContentType)
        
        RETVAL_NULL();
        
        
        RETVAL_NULL();
        
+#ifdef HTTP_HAVE_MAGIC
+       magic_mode = MAGIC_MIME;
+       
        SET_EH_THROW_HTTP();
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
                switch (Z_LVAL_P(GET_STATIC_PROP(mode))) {
        SET_EH_THROW_HTTP();
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) {
                switch (Z_LVAL_P(GET_STATIC_PROP(mode))) {
@@ -542,6 +545,7 @@ PHP_METHOD(HttpResponse, guessContentType)
                }
        }
        SET_EH_NORMAL();
                }
        }
        SET_EH_NORMAL();
+#endif
 }
 /* }}} */
 
 }
 /* }}} */