summaryrefslogtreecommitdiff
path: root/lib/file_utils.php
blob: 416d151b716d0e36ed948b81e628e98abd66cc64 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab File API                                  |
 |                                                                          |
 | Copyright (C) 2012-2013, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 +--------------------------------------------------------------------------+
*/

class file_utils
{
    static $class_map = array(
        'document' => array(
            // text
            'text/',
            'application/rtf',
            'application/x-rtf',
            'application/xml',
            // office
            'application/wordperfect',
            'application/excel',
            'application/msword',
            'application/msexcel',
            'application/mspowerpoint',
            'application/vnd.ms-word',
            'application/vnd.ms-excel',
            'application/vnd.ms-powerpoint',
            'application/vnd.openxmlformats-officedocument',
            'application/vnd.oasis.opendocument',
            'application/vnd.sun.xml.calc',
            'application/vnd.sun.xml.writer',
            'application/vnd.stardivision.calc',
            'application/vnd.stardivision.writer',
            // pdf
            'application/pdf',
            'application/x-pdf',
            'application/acrobat',
            'application/vnd.pdf',
        ),
        'audio' => array(
            'audio/',
        ),
        'video' => array(
            'video/',
        ),
        'image' => array(
            'image/',
            'application/dxf',
            'application/acad',
        ),
        'empty' => array(
            'application/x-empty',
        ),
    );

    // list of known file extensions, more in Roundcube config
    static $ext_map = array(
        'doc'  => 'application/msword',
        'gz'   => 'application/gzip',
        'htm'  => 'text/html',
        'html' => 'text/html',
        'mp3'  => 'audio/mpeg',
        'odp'  => 'application/vnd.oasis.opendocument.presentation',
        'ods'  => 'application/vnd.oasis.opendocument.spreadsheet',
        'odt'  => 'application/vnd.oasis.opendocument.text',
        'ogg'  => 'application/ogg',
        'pdf'  => 'application/pdf',
        'ppt'  => 'application/vnd.ms-powerpoint',
        'rar'  => 'application/x-rar-compressed',
        'tgz'  => 'application/gzip',
        'txt'  => 'text/plain',
        'zip'  => 'application/zip',
    );


    /**
     * Return list of mimetype prefixes for specified file class
     *
     * @param string $class Class name
     *
     * @return array List of mimetype prefixes
     */
    static function class2mimetypes($class)
    {
        return isset(self::$class_map[$class]) ? self::$class_map[$class] : self::$class_map['empty'];
    }

    /**
     * Finds class of specified mimetype
     *
     * @param string $mimetype File mimetype
     *
     * @return string Class name
     */
    static function mimetype2class($mimetype)
    {
        $mimetype = strtolower($mimetype);

        foreach (self::$class_map as $class => $prefixes) {
            foreach ($prefixes as $prefix) {
                if (strpos($mimetype, $prefix) === 0) {
                    return $class;
                }
            }
        }
    }

    /**
     * Apply some fixes on file mimetype string
     *
     * @param string $mimetype File type
     *
     * @return string File type
     */
    static function real_mimetype($mimetype)
    {
        if (preg_match('/^text\/(.+)/i', $mimetype, $m)) {
            // fix pdf mimetype
            if (preg_match('/^(pdf|x-pdf)$/i', $m[1])) {
                $mimetype = 'application/pdf';
            }
        }

        return $mimetype;
    }

    /**
     * Find mimetype from file name (extension)
     *
     * @param string $filename File name
     * @param string $fallback Follback mimetype
     *
     * @return string File mimetype
     */
    static function ext_to_type($filename, $fallback = 'application/octet-stream')
    {
        static $mime_ext = array();

        $config = rcube::get_instance()->config;
        $ext    = substr($filename, strrpos($filename, '.') + 1);

        if (empty($mime_ext)) {
            $mime_ext = self::$ext_map;
            foreach ($config->resolve_paths('mimetypes.php') as $fpath) {
                $mime_ext = array_merge($mime_ext, (array) @include($fpath));
            }
        }

        if (is_array($mime_ext) && $ext) {
            $mimetype = $mime_ext[strtolower($ext)];
        }

        return $mimetype ?: $fallback;
    }

    /**
     * Returns script URI
     *
     * @return string Script URI
     */
    static function script_uri()
    {
        if (!empty($_SERVER['SCRIPT_URI'])) {
            return $_SERVER['SCRIPT_URI'];
        }

        $uri = $_SERVER['SERVER_PORT'] == 443 ? 'https://' : 'http://';
        $uri .= $_SERVER['HTTP_HOST'];
        $uri .= preg_replace('/\?.*$/', '', $_SERVER['REQUEST_URI']);

        return $uri;
    }
}