1<?php
2
3namespace Sabre\DAVACL\FS;
4
5use Sabre\DAV\Exception\Forbidden;
6use Sabre\DAVACL\AbstractPrincipalCollection;
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) 2007-2015 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    /**
25     * Name of this collection.
26     *
27     * @var string
28     */
29    public $collectionName = 'home';
30
31    /**
32     * Path to where the users' files are actually stored.
33     *
34     * @var string
35     */
36    protected $storagePath;
37
38    /**
39     * Creates the home collection.
40     *
41     * @param BackendInterface $principalBackend
42     * @param string $storagePath Where the actual files are stored.
43     * @param string $principalPrefix list of principals to iterate.
44     */
45    function __construct(BackendInterface $principalBackend, $storagePath, $principalPrefix = 'principals') {
46
47        parent::__construct($principalBackend, $principalPrefix);
48        $this->storagePath = $storagePath;
49
50    }
51
52    /**
53     * Returns the name of the node.
54     *
55     * This is used to generate the url.
56     *
57     * @return string
58     */
59    function getName() {
60
61        return $this->collectionName;
62
63    }
64
65    /**
66     * Returns a principals' collection of files.
67     *
68     * The passed array contains principal information, and is guaranteed to
69     * at least contain a uri item. Other properties may or may not be
70     * supplied by the authentication backend.
71     *
72     * @param array $principalInfo
73     * @return void
74     */
75    function getChildForPrincipal(array $principalInfo) {
76
77        $owner = $principalInfo['uri'];
78        $acl = [
79            [
80                'privilege' => '{DAV:}read',
81                'principal' => $owner,
82                'protected' => true,
83            ],
84            [
85                'privilege' => '{DAV:}write',
86                'principal' => $owner,
87                'protected' => true,
88            ],
89        ];
90
91        list(, $principalBaseName) = Uri\split($owner);
92
93        $path = $this->storagePath . '/' . $principalBaseName;
94
95        if (!is_dir($path)) {
96            mkdir($path, 0777, true);
97        }
98        return new Collection(
99            $path,
100            $acl,
101            $owner
102        );
103
104    }
105
106    /**
107     * Returns the owner principal
108     *
109     * This must be a url to a principal, or null if there's no owner
110     *
111     * @return string|null
112     */
113    function getOwner() {
114
115        return null;
116
117    }
118
119    /**
120     * Returns a group principal
121     *
122     * This must be a url to a principal, or null if there's no owner
123     *
124     * @return string|null
125     */
126    function getGroup() {
127
128        return null;
129
130    }
131
132    /**
133     * Returns a list of ACE's for this node.
134     *
135     * Each ACE has the following properties:
136     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
137     *     currently the only supported privileges
138     *   * 'principal', a url to the principal who owns the node
139     *   * 'protected' (optional), indicating that this ACE is not allowed to
140     *      be updated.
141     *
142     * @return array
143     */
144    function getACL() {
145
146        return [
147            [
148                'principal' => '{DAV:}authenticated',
149                'privilege' => '{DAV:}read',
150                'protected' => true,
151            ]
152        ];
153
154    }
155
156    /**
157     * Updates the ACL
158     *
159     * This method will receive a list of new ACE's as an array argument.
160     *
161     * @param array $acl
162     * @return void
163     */
164    function setACL(array $acl) {
165
166        throw new Forbidden('Setting ACL is not allowed here');
167
168    }
169
170    /**
171     * Returns the list of supported privileges for this node.
172     *
173     * The returned data structure is a list of nested privileges.
174     * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
175     * standard structure.
176     *
177     * If null is returned from this method, the default privilege set is used,
178     * which is fine for most common usecases.
179     *
180     * @return array|null
181     */
182    function getSupportedPrivilegeSet() {
183
184        return null;
185
186    }
187
188}
189