File: Magic.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (131 lines) | stat: -rw-r--r-- 3,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/*
 * $Horde: horde/lib/MIME/Magic.php,v 1.3.2.9 2003/01/03 12:48:24 jan Exp $
 *
 * Copyright 1999-2003 Anil Madhavapeddy <anil@recoil.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 */

/**
 * The MIME_Magic:: class provides an interface to determine a
 * MIME type for various content, if it provided with different
 * levels of information.
 *
 * Currently, it can map a file extension to a MIME type, but
 * future ideas include using Apache's mod_mime_magic (if available).
 *
 * @author  Anil Madhavapeddy <anil@recoil.org>
 * @version $Revision: 1.3.2.9 $
 * @since   Horde 1.3
 * @package horde.mime
 */
class MIME_Magic {

    /**
     * Returns a copy of the MIME extension map.
     *
     * @access private
     *
     * @return array  The MIME extension map.
     *
     * @since Horde 2.2
     */
    function _getMimeExtensionMap()
    {
        static $mime_extension_map;

        if (!isset($mime_extension_map)) {
            require HORDE_BASE . '/config/mime_mapping.php';
        }

        return $mime_extension_map;
    }

    /**
     * Attempt to convert a file extension to a MIME type, based
     * on the global Horde and application specific config files.
     *
     * If we cannot map the file extension to a specific type, then
     * we fall back to a custom MIME handler x-extension/type, which
     * can be used as a normal MIME type internally throughout Horde.
     *
     * @access public
     *
     * @param string $ext  The file extension to be mapped to a MIME type.
     *
     * @return string  The MIME type of the file extension.
     */
    function extToMIME($ext)
    {
        if (empty($ext)) {
           return 'text/plain';
        } else {
            $ext = strtolower($ext);
            $map = MIME_Magic::_getMimeExtensionMap();
            if (!array_key_exists($ext, $map)) {
                return "x-extension/$ext";
            } else {
                return $map[$ext];
            }
        }
    }

    /**
     * Attempt to convert a filename to a MIME type, based on the
     * global Horde and application specific config files.
     *
     * Unlike extToMIME(), this function will return
     * 'application/octet-stream' for any unknown or empty extension.
     *
     * @access public
     *
     * @param string $filename  The filename to be mapped to a MIME type.
     *
     * @return string  The MIME type of the filename.
     *
     * @since Horde 2.2
     */
    function filenameToMIME($filename)
    {
        $pos = strrpos($filename, '.');
        if (!empty($pos)) {
            $type = MIME_Magic::extToMIME(substr($filename, $pos + 1));
            if (!stristr($type, 'x-extension')) {
                return $type;
            }
        }

        return 'application/octet-stream';
    }

    /**
     * Attempt to convert a MIME type to a file extension, based
     * on the global Horde and application specific config files.
     *
     * If we cannot map the type to a file extension, we return false.
     *
     * @access public
     *
     * @param string $type The MIME type to be mapped to a file extension
     * @return string      The file extension of the MIME type
     *
     * @since Horde 2.1
     */
    function MIMEToExt($type)
    {
        $key = array_search($type, MIME_Magic::_getMimeExtensionMap());
        if (empty($type) || ($key === false) || ($key === null)) {
            list($major, $minor) = explode('/', $type);
            if ($major == 'x-extension') {
                return $minor;
            }
            return false;
        } else {
            return $key;
        }
    }

}
?>