summaryrefslogtreecommitdiff
path: root/lib/file_utils.php
blob: 8ad36a902df4b8e51bf7da07500aa1c34739e20d (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
<?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',
        ),
    );


    /**
     * 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;
    }

    /**
     * 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;
    }
}