1<?php
2
3namespace Sabre\DAVACL\FS;
4
5use Sabre\DAVACL\AbstractPrincipalCollection;
6use Sabre\DAVACL\ACLTrait;
7use Sabre\DAVACL\IACL;
8use Sabre\DAVACL\PrincipalBackend\BackendInterface;
9use Sabre\Uri;
10
11/**
12 * This collection contains a collection for every principal.
13 * It is similar to /home on many unix systems.
14 *
15 * The per-user collections can only be accessed by the user who owns the
16 * collection.
17 *
18 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
19 * @author Evert Pot (http://evertpot.com/)
20 * @license http://sabre.io/license/ Modified BSD License
21 */
22class HomeCollection extends AbstractPrincipalCollection implements IACL {
23
24    use ACLTrait;
25
26    /**
27     * Name of this collection.
28     *
29     * @var string
30     */
31    public $collectionName = 'home';
32
33    /**
34     * Path to where the users' files are actually stored.
35     *
36     * @var string
37     */
38    protected $storagePath;
39
40    /**
41     * Creates the home collection.
42     *
43     * @param BackendInterface $principalBackend
44     * @param string $storagePath Where the actual files are stored.
45     * @param string $principalPrefix list of principals to iterate.
46     */
47    function __construct(BackendInterface $principalBackend, $storagePath, $principalPrefix = 'principals') {
48
49        parent::__construct($principalBackend, $principalPrefix);
50        $this->storagePath = $storagePath;
51
52    }
53
54    /**
55     * Returns the name of the node.
56     *
57     * This is used to generate the url.
58     *
59     * @return string
60     */
61    function getName() {
62
63        return $this->collectionName;
64
65    }
66
67    /**
68     * Returns a principals' collection of files.
69     *
70     * The passed array contains principal information, and is guaranteed to
71     * at least contain a uri item. Other properties may or may not be
72     * supplied by the authentication backend.
73     *
74     * @param array $principalInfo
75     * @return \Sabre\DAV\INode
76     */
77    function getChildForPrincipal(array $principalInfo) {
78
79        $owner = $principalInfo['uri'];
80        $acl = [
81            [
82                'privilege' => '{DAV:}all',
83                'principal' => '{DAV:}owner',
84                'protected' => true,
85            ],
86        ];
87
88        list(, $principalBaseName) = Uri\split($owner);
89
90        $path = $this->storagePath . '/' . $principalBaseName;
91
92        if (!is_dir($path)) {
93            mkdir($path, 0777, true);
94        }
95        return new Collection(
96            $path,
97            $acl,
98            $owner
99        );
100
101    }
102
103
104    /**
105     * Returns a list of ACE's for this node.
106     *
107     * Each ACE has the following properties:
108     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
109     *     currently the only supported privileges
110     *   * 'principal', a url to the principal who owns the node
111     *   * 'protected' (optional), indicating that this ACE is not allowed to
112     *      be updated.
113     *
114     * @return array
115     */
116    function getACL() {
117
118        return [
119            [
120                'principal' => '{DAV:}authenticated',
121                'privilege' => '{DAV:}read',
122                'protected' => true,
123            ]
124        ];
125
126    }
127
128}
129