hholzgra                Sat Jan 25 11:25:38 2003 EDT

  Modified files:              
    /php4/ext/mime_magic        mime_magic.c 
  Log:
  - mime file parser messages only with mime_magic.debug=On (default: off)
  - fix docref URL in startup error messages
  - show status in php info, either "enabled" or "..., disabled"
  
  
Index: php4/ext/mime_magic/mime_magic.c
diff -u php4/ext/mime_magic/mime_magic.c:1.24 php4/ext/mime_magic/mime_magic.c:1.25
--- php4/ext/mime_magic/mime_magic.c:1.24       Sat Jan 18 19:45:41 2003
+++ php4/ext/mime_magic/mime_magic.c    Sat Jan 25 11:25:37 2003
@@ -15,7 +15,7 @@
   | Author: Hartmut Holzgraefe  <[EMAIL PROTECTED]>                       |
   +----------------------------------------------------------------------+
 
-  $Id: mime_magic.c,v 1.24 2003/01/19 00:45:41 iliaa Exp $ 
+  $Id: mime_magic.c,v 1.25 2003/01/25 16:25:37 hholzgra Exp $ 
 
   This module contains a lot of stuff taken from Apache mod_mime_magic,
   so the license section is a little bit longer than usual:
@@ -240,6 +240,7 @@
  */
 PHP_INI_BEGIN()
 STD_PHP_INI_ENTRY("mime_magic.magicfile", PHP_MIME_MAGIC_FILE_PATH, PHP_INI_SYSTEM, 
OnUpdateString, magicfile, zend_mime_magic_globals, mime_magic_globals)
+STD_PHP_INI_ENTRY("mime_magic.debug", "0", PHP_INI_SYSTEM, OnUpdateBool, debug, 
+zend_mime_magic_globals, mime_magic_globals)
 PHP_INI_END()
 /* }}} */
 
@@ -261,8 +262,15 @@
        mime_global.magicfile = MIME_MAGIC_G(magicfile);
 
        if(mime_global.magicfile) {
-               apprentice();
+               if(apprentice()) {
+                       MIME_MAGIC_G(status) = "invalid magic file, disabled";
+               } else {
+                       MIME_MAGIC_G(status) = "enabled";
+               }
+       } else {
+               MIME_MAGIC_G(status) = "no magic file given, disabled";
        }
+
        return SUCCESS;
 }
 /* }}} */
@@ -289,7 +297,7 @@
 PHP_MINFO_FUNCTION(mime_magic)
 {
        php_info_print_table_start();
-       php_info_print_table_header(2, "mime_magic support", "enabled");
+       php_info_print_table_header(2, "mime_magic support", MIME_MAGIC_G(status));
        php_info_print_table_end();
 
        DISPLAY_INI_ENTRIES();
@@ -311,12 +319,14 @@
        }
 
        if (conf->magic == (struct magic *)-1) {
-               php_error_docref(NULL TSRMLS_CC, E_ERROR, "mime_magic could not be 
initialized, magic file %s is not avaliable", conf->magicfile);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref("http://www.php.net/mime_magic"; TSRMLS_CC, 
+E_ERROR, "mime_magic could not be initialized, magic file %s is not avaliable", 
+conf->magicfile);
                RETURN_FALSE;
        } 
 
        if(!conf->magic) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "mime_magic not 
initialized");
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref("http://www.php.net/mime_magic"; TSRMLS_CC, 
+E_WARNING, "mime_magic not initialized");
                RETURN_FALSE;
        }
 
@@ -349,6 +359,7 @@
     char line[BUFSIZ + 1];
     int errs = 0;
     int lineno;
+       TSRMLS_FETCH();
 
     char *fname;
     magic_server_config_rec *conf = &mime_global;
@@ -390,6 +401,9 @@
                /* parse it */
                if (parse(line + ws_offset, lineno) != 0)
                        ++errs;
+
+               if(errs && !MIME_MAGIC_G(debug)) 
+                       break;
     }
 
     (void) fclose(f);
@@ -429,7 +443,8 @@
                default: 
                 {
                        TSRMLS_FETCH();
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, ": can't happen: 
m->type=%d", m->type);
+                       if(MIME_MAGIC_G(debug))
+                               php_error_docref("http://www.php.net/mime_magic"; 
+TSRMLS_CC, E_WARNING, ": can't happen: m->type=%d", m->type);
                        return -1;
                 }
                }
@@ -437,6 +452,21 @@
 }
 
 /*
+ *
+ */
+static int is_valid_mimetype(char *p)
+{
+       do {
+               if(!isalnum(*p) && (*p != '-')) return 0;
+       } while(*(++p) != '/');
+       ++p;
+       do {
+               if(!isalnum(*p) && (*p != '-')) return 0;
+       } while(*(++p));
+       return 1;
+}
+
+/*
  * parse one line from magic file, put into magic[index++] if valid
  */
 static int parse(char *l, int lineno)
@@ -477,7 +507,8 @@
     /* get offset, then skip over it */
     m->offset = (int) strtol(l, &t, 0);
     if (l == t) {
-       php_error_docref(NULL TSRMLS_CC, E_WARNING, ": (line %d) offset `%s' invalid", 
lineno, l);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref("http://www.php.net/mime_magic"; TSRMLS_CC, 
+E_WARNING, ": (%s:%d) offset `%s' invalid", MIME_MAGIC_G(magicfile), lineno, l);
     }
     l = t;
 
@@ -499,7 +530,8 @@
                                m->in.type = BYTE;
                                break;
                        default:
-                               php_error_docref(NULL TSRMLS_CC, E_WARNING, ": 
indirect offset type %c invalid", *l);
+                               if(MIME_MAGIC_G(debug))
+                                       
+php_error_docref("http://www.php.net/mime_magic"; TSRMLS_CC, E_WARNING, ": (%s:%d) 
+indirect offset type %c invalid", MIME_MAGIC_G(magicfile), lineno, *l);
                                break;
                        }
                        l++;
@@ -515,7 +547,8 @@
                else
                        t = l;
                if (*t++ != ')') {
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, ": missing ')' in 
indirect offset");
+                       if(MIME_MAGIC_G(debug))
+                               php_error_docref("http://www.php.net/mime_magic"; 
+TSRMLS_CC, E_WARNING, ": (%s:%s) missing ')' in indirect offset", 
+MIME_MAGIC_G(magicfile), lineno);
                }
                l = t;
     }
@@ -588,7 +621,8 @@
                l += NLEDATE;
     }
     else {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, ": type %s invalid", l);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref("http://www.php.net/mime_magic"; TSRMLS_CC, 
+E_WARNING, ": (%s:%d) type %s invalid", MIME_MAGIC_G(magicfile), lineno, l);
                return -1;
     }
     /* New-style anding: "0 byte&0x80 =0x80 dynamically linked" */
@@ -646,8 +680,15 @@
     }
     else
                m->nospflag = 0;
-    strncpy(m->desc, l, sizeof(m->desc) - 1);
-    m->desc[sizeof(m->desc) - 1] = '\0';
+
+       if(!is_valid_mimetype(l)) {
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref("http://www.php.net/mime_magic"; TSRMLS_CC, 
+E_WARNING, ": (%s:%d) '%s' is not a valid mimetype, etry skipped", 
+MIME_MAGIC_G(magicfile), lineno, l);
+               return -1;
+       }
+       
+       strncpy(m->desc, l, sizeof(m->desc) - 1);
+       m->desc[sizeof(m->desc) - 1] = '\0';
 
     return 0;
 }
@@ -688,7 +729,8 @@
                        break;
                if (p >= pmax) {
                        TSRMLS_FETCH();
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "string too long: 
%s", origs);
+                       if(MIME_MAGIC_G(debug))
+                               php_error_docref("http://www.php.net/mime_magic"; 
+TSRMLS_CC, E_WARNING, "string too long: %s", origs);
                        break;
                }
                if (c == '\\') {
@@ -846,7 +888,8 @@
        
    /* make sure we have a list to put it in */
     if (!req_dat) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "request config should not 
be NULL");
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "request config 
+should not be NULL");
                if (!(req_dat = magic_set_config())) {
                        /* failure */
                        return -1;
@@ -982,7 +1025,8 @@
 
     if (stream == NULL) {
                /* We can't open it, but we were able to stat it. */
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't read `%s'", 
filename);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "can't read `%s'", 
+filename);
                /* let some other handler decide what the problem is */
                return MIME_MAGIC_DECLINED;
     }
@@ -991,7 +1035,8 @@
      * try looking at the first HOWMANY bytes
      */
     if ((nbytes = php_stream_read(stream, (char *) buf, sizeof(buf) - 1)) == -1) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "read failed: %s", 
filename);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "read failed: %s", 
+filename);
                return MIME_MAGIC_ERROR;
     }
 
@@ -1086,7 +1131,8 @@
                /* We used stat(), the only possible reason for this is that the
                 * symlink is broken.
                 */
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "broken symlink (%s)", 
filename);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "broken symlink 
+(%s)", filename);
                return MIME_MAGIC_ERROR;
 #endif
 #ifdef    S_IFSOCK
@@ -1101,7 +1147,8 @@
        case 0:
                break;
     default:
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid mode 0%o.", 
(unsigned int)stat_ssb.sb.st_mode);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid mode 
+0%o.", (unsigned int)stat_ssb.sb.st_mode);
                return MIME_MAGIC_ERROR;
     }
 
@@ -1458,9 +1505,9 @@
     TSRMLS_FETCH();
 
     if ((m->value.s[0] == 'x') && (m->value.s[1] == '\0')) {
-    
-       php_error_docref(NULL TSRMLS_CC, E_WARNING, "BOINK");
-       return 1;
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "BOINK");
+               return 1;
     }
 
     switch (m->type) {
@@ -1503,7 +1550,8 @@
                break;
     default:
                /*  bogosity, pretend that it just wasn't a match */
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid type %d in 
mcheck().", m->type);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid type %d 
+in mcheck().", m->type);
                return 0;
     }
 
@@ -1551,7 +1599,8 @@
     default:
                /* bogosity, pretend it didn't match */
                matched = 0;
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "mcheck: can't happen: 
invalid relation %d.", m->reln);
+               if(MIME_MAGIC_G(debug))
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "mcheck: can't 
+happen: invalid relation %d.", m->reln);
                break;
     }
 
@@ -1635,9 +1684,10 @@
     default:
        {
                TSRMLS_FETCH();
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid m->type (%d) in 
mprint().", m->type);
-               return;
-       }
+                       if(MIME_MAGIC_G(debug))
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid 
+m->type (%d) in mprint().", m->type);
+                       return;
+               }
     }
 
     v = signextend(m, v) & m->mask;
@@ -1682,9 +1732,10 @@
     default:
        {
                TSRMLS_FETCH();
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid type %d in 
mconvert().", m->type);
-               return 0;
-       }
+                       if(MIME_MAGIC_G(debug))
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid 
+type %d in mconvert().", m->type);
+                       return 0;
+               }
     }
 }
 
@@ -1758,7 +1809,8 @@
                                else {
                                        /* should not be possible */
                                        /* abandon malfunctioning module */
-                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, ": 
bad state %d (ws)", state);
+                                       if(MIME_MAGIC_G(debug))
+                                               php_error_docref(NULL TSRMLS_CC, 
+E_WARNING, ": bad state %d (ws)", state);
                                        return MIME_MAGIC_DECLINED;
                                }
                                /* NOTREACHED */
@@ -1801,7 +1853,8 @@
                                else {
                                        /* should not be possible */
                                        /* abandon malfunctioning module */
-                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, 
"bad state %d (ns)", state);
+                                       if(MIME_MAGIC_G(debug))
+                                               php_error_docref(NULL TSRMLS_CC, 
+E_WARNING, "bad state %d (ns)", state);
                                        return MIME_MAGIC_DECLINED;
                                }
                                /* NOTREACHED */

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to