1<?php
2/**
3 * A Horde_Injector based Horde_Vfs factory.
4 *
5 * @author   Michael J. Rubinsky <mrubinsk@horde.org>
6 * @author   Jan Schneider <jan@horde.org>
7 * @category Horde
8 * @license  http://www.horde.org/licenses/gpl GPL
9 * @package  Gollem
10 */
11
12/**
13 * A Horde_Injector based Horde_Vfs factory.
14 *
15 * Copyright 2011-2017 Horde LLC (http://www.horde.org/)
16 *
17 * See the enclosed file COPYING for license information (GPL). If you
18 * did not receive this file, see http://www.horde.org/licenses/gpl.
19 *
20 * @author   Michael J. Rubinsky <mrubinsk@horde.org>
21 * @author   Jan Schneider <jan@horde.org>
22 * @category Horde
23 * @license  http://www.horde.org/licenses/gpl GPL
24 * @package  Gollem
25 */
26class Gollem_Factory_Vfs extends Horde_Core_Factory_Base
27{
28    /**
29     * Instances.
30     *
31     * @var array
32     */
33    private $_instances = array();
34
35    /**
36     * Returns the VFS instance.
37     *
38     * @param string $backend  The backend to return.
39     *
40     * @return Horde_Vfs  The VFS object.
41     */
42    public function create($backend)
43    {
44        if (empty($this->_instances[$backend])) {
45            $be_config = Gollem_Auth::getBackend($backend);
46            $params = $be_config['params'];
47
48            switch (Horde_String::lower($be_config['driver'])) {
49            case 'sql':
50            case 'sqlfile':
51            case 'musql':
52                $db_params = $params;
53                unset($db_params['table']);
54                $params['db'] = $this->_injector
55                    ->getInstance('Horde_Core_Factory_Db')
56                    ->create('gollem', $db_params);
57                $params['user'] = $GLOBALS['registry']->getAuth();
58                break;
59            }
60
61            $vfs = Horde_Vfs::factory($be_config['driver'], $params);
62
63            if (!empty($be_config['quota'])) {
64                $vfs->setQuotaRoot($be_config['root'] == '/' ? '' : $be_config['root']);
65                if (isset($be_config['quota_val'])) {
66                    $vfs->setQuota($be_config['quota_val'], $be_config['quota_metric']);
67                } else {
68                    $quota_metric = array(
69                        'B' => Horde_Vfs::QUOTA_METRIC_BYTE,
70                        'KB' => Horde_Vfs::QUOTA_METRIC_KB,
71                        'MB' => Horde_Vfs::QUOTA_METRIC_MB,
72                        'GB' => Horde_Vfs::QUOTA_METRIC_GB
73                    );
74                    $quota_str = explode(' ', $be_config['quota'], 2);
75                    if (is_numeric($quota_str[0])) {
76                        $metric = trim(Horde_String::upper($quota_str[1]));
77                        if (!isset($quota_metric[$metric])) {
78                            $metric = 'B';
79                        }
80                        $vfs->setQuota($quota_str[0], $quota_metric[$metric]);
81                    }
82                }
83            }
84
85            $this->_instances[$backend] = $vfs;
86        }
87
88        return $this->_instances[$backend];
89    }
90}
91