summaryrefslogtreecommitdiff
path: root/app/ServiceProvider/ObjectStorageProvider.php
blob: a108345f267adc1ab189b447f3521b54bc7143f2 (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
<?php

namespace Kanboard\ServiceProvider;

use Kanboard\Core\ObjectStorage\FileStorage;
use LogicException;
use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
 * Class ObjectStorageProvider
 *
 * @package Kanboard\ServiceProvider
 * @author  Frederic Guillot
 */
class ObjectStorageProvider implements ServiceProviderInterface
{
    public function register(Container $container)
    {
        $container['objectStorage'] = function () {
            if (file_exists(FILES_DIR)) {
                if (! is_writable(FILES_DIR)) {
                    $stat = stat(FILES_DIR);

                    throw new LogicException(sprintf(
                        'The folder to store uploaded files is not writeable by your webserver user (file=%s; mode=%o; uid=%d; gid=%d)',
                        FILES_DIR,
                        $stat['mode'],
                        $stat['uid'],
                        $stat['gid']
                    ));
                }
            } elseif (! @mkdir(FILES_DIR)) {
                $folder = dirname(FILES_DIR);
                $stat = stat($folder);

                throw new LogicException(sprintf(
                    'Unable to create folder to store uploaded files, check the permissions of the parent directory (file=%s; mode=%o; uid=%d; gid=%d)',
                    $folder,
                    $stat['mode'],
                    $stat['uid'],
                    $stat['gid']
                ));
            }

            return new FileStorage(FILES_DIR);
        };

        return $container;
    }
}