summaryrefslogtreecommitdiff
path: root/lib/file_api_core.php
blob: cefd26148fc0ba3feff10f31ac38d0bfd40e603f (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab File API                                  |
 |                                                                          |
 | Copyright (C) 2012-2014, 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_api_core extends file_locale
{
    const API_VERSION = 2;

    const ERROR_CODE    = 500;
    const ERROR_INVALID = 501;

    const OUTPUT_JSON = 'application/json';
    const OUTPUT_HTML = 'text/html';

    public $config = array(
        'date_format' => 'Y-m-d H:i',
        'language'    => 'en_US',
    );

    protected $app_name = 'Kolab File API';
    protected $drivers  = array();
    protected $backend;

    /**
     * Returns API version
     */
    public function client_version()
    {
        return self::API_VERSION;
    }

    /**
     * Initialise authentication/configuration backend class
     *
     * @return file_storage Main storage driver
     */
    public function get_backend()
    {
        if ($this->backend) {
            return $this->backend;
        }

        $rcube  = rcube::get_instance();
        $driver = $rcube->config->get('fileapi_backend', 'kolab');

        $this->backend = $this->load_driver_object($driver);

        // configure api
        $this->backend->configure($this->config);

        return $this->backend;
    }

    /**
     * Return supported/enabled external storage instances
     *
     * @param bool $as_objects Return drivers as objects not config data
     *
     * @return array List of storage drivers
     */
    public function get_drivers($as_objects = false)
    {
        $rcube   = rcube::get_instance();
        $enabled = $rcube->config->get('fileapi_drivers');
        $preconf = $rcube->config->get('fileapi_sources');
        $result  = array();
        $all     = array();

        if (!empty($enabled)) {
            $backend = $this->get_backend();
            $drivers = $backend->driver_list();

            foreach ($drivers as $item) {
                $all[] = $item['title'];

                if ($item['enabled'] && in_array($item['driver'], (array) $enabled)) {
                    $result[] = $as_objects ? $this->get_driver_object($item) : $item;
                }
            }
        }

        if (empty($result) && !empty($preconf)) {
            foreach ((array) $preconf as $title => $item) {
                if (!in_array($title, $all)) {
                    $item['title'] = $title;
                    $item['admin'] = true;

                    $result[] = $as_objects ? $this->get_driver_object($item) : $item;
                }
            }
        }

        return $result;
    }

    /**
     * Return driver for specified file/folder path
     *
     * @param string $path Folder/file path
     *
     * @return array Storage driver object, modified path, driver config
     */
    public function get_driver($path)
    {
        $drivers = $this->get_drivers();

        foreach ($drivers as $item) {
            $prefix = $item['title'] . file_storage::SEPARATOR;

            if ($path == $item['title'] || strpos($path, $prefix) === 0) {
                $selected = $item;
                break;
            }
        }

        if (empty($selected)) {
            return array($this->get_backend(), $path);
        }

        $path = substr($path, strlen($selected['title']) + 1);

        return array($this->get_driver_object($selected), $path, $selected);
    }

    /**
     * Initialize driver instance
     *
     * @param array $config Driver config
     *
     * @return file_storage Storage driver instance
     */
    public function get_driver_object($config)
    {
        $key = $config['title'];

        if (empty($this->drivers[$key])) {
            $this->drivers[$key] = $driver = $this->load_driver_object($config['driver']);

            if ($config['username'] == '%u') {
                $backend            = $this->get_backend();
                $auth_info          = $backend->auth_info();
                $config['username'] = $auth_info['username'];
                $config['password'] = $auth_info['password'];
            }
            else if (!empty($config['password']) && empty($config['admin']) && !empty($key)) {
                $config['password'] = $this->decrypt($config['password']);
            }

            // configure api
            $driver->configure(array_merge($config, $this->config), $key);
        }

        return $this->drivers[$key];
    }

    /**
     * Loads a driver
     */
    public function load_driver_object($name)
    {
        $class = $name . '_file_storage';

        if (!class_exists($class, false)) {
            $include_path = __DIR__ . "/drivers/$name" . PATH_SEPARATOR;
            $include_path .= ini_get('include_path');
            set_include_path($include_path);
        }

        return new $class;
    }

    /**
     * Returns storage(s) capabilities
     *
     * @return array Capabilities
     */
    public function capabilities()
    {
        $rcube   = rcube::get_instance();
        $backend = $this->get_backend();
        $caps    = array();

        // check support for upload progress
        if (($progress_sec = $rcube->config->get('upload_progress'))
            && ini_get('apc.rfc1867') && function_exists('apc_fetch')
        ) {
            $caps[file_storage::CAPS_PROGRESS_NAME] = ini_get('apc.rfc1867_name');
            $caps[file_storage::CAPS_PROGRESS_TIME] = $progress_sec;
        }

        // get capabilities of main storage module
        foreach ($backend->capabilities() as $name => $value) {
            // skip disabled capabilities
            if ($value !== false) {
                $caps[$name] = $value;
            }
        }

        // get capabilities of other drivers
        $drivers = $this->get_drivers(true);

        foreach ($drivers as $driver) {
            if ($driver != $backend) {
                $title = $driver->title();
                foreach ($driver->capabilities() as $name => $value) {
                    // skip disabled capabilities
                    if ($value !== false) {
                        $caps['roots'][$title][$name] = $value;
                    }
                }
            }
        }

        return $caps;
    }

    /**
     * Return mimetypes list supported by built-in viewers
     *
     * @return array List of mimetypes
     */
    protected function supported_mimetypes()
    {
        $mimetypes = array();
        $dir       = __DIR__ . '/viewers';

        if ($handle = opendir($dir)) {
            while (false !== ($file = readdir($handle))) {
                if (preg_match('/^([a-z0-9_]+)\.php$/i', $file, $matches)) {
                    include_once $dir . '/' . $file;
                    $class  = 'file_viewer_' . $matches[1];
                    $viewer = new $class($this);

                    $mimetypes = array_merge($mimetypes, $viewer->supported_mimetypes());
                }
            }
            closedir($handle);
        }

        return $mimetypes;
    }

    /**
     * Encrypts data with current user password
     *
     * @param string $str A string to encrypt
     *
     * @return string Encrypted string (and base64-encoded)
     */
    public function encrypt($str)
    {
        $rcube = rcube::get_instance();
        $key   = $this->get_crypto_key();

        return $rcube->encrypt($str, $key, true);
    }

    /**
     * Decrypts data encrypted with encrypt() method
     *
     * @param string $str Encrypted string (base64-encoded)
     *
     * @return string Decrypted string
     */
    public function decrypt($str)
    {
        $rcube = rcube::get_instance();
        $key   = $this->get_crypto_key();

        return $rcube->decrypt($str, $key, true);
    }

    /**
     * Set encryption password
     */
    protected function get_crypto_key()
    {
        $key      = 'chwala_crypto_key';
        $rcube    = rcube::get_instance();
        $backend  = $this->get_backend();
        $user     = $backend->auth_info();
        $password = $user['password'] . $user['username'];

        // encryption password must be 24 characters, no less, no more
        if (($len = strlen($password)) > 24) {
            $password = substr($password, 0, 24);
        }
        else {
            $password = $password . substr($rcube->config->get('des_key'), 0, 24 - $len);
        }

        $rcube->config->set($key, $password);

        return $key;
    }
}