xref: /plugin/davcal/vendor/sabre/dav/lib/DAV/PropertyStorage/Plugin.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1*a1a3b679SAndreas Boehler<?php
2*a1a3b679SAndreas Boehler
3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\PropertyStorage;
4*a1a3b679SAndreas Boehler
5*a1a3b679SAndreas Boehleruse Sabre\DAV\Server;
6*a1a3b679SAndreas Boehleruse Sabre\DAV\ServerPlugin;
7*a1a3b679SAndreas Boehleruse Sabre\DAV\PropPatch;
8*a1a3b679SAndreas Boehleruse Sabre\DAV\PropFind;
9*a1a3b679SAndreas Boehleruse Sabre\DAV\INode;
10*a1a3b679SAndreas Boehler
11*a1a3b679SAndreas Boehler/**
12*a1a3b679SAndreas Boehler * PropertyStorage Plugin.
13*a1a3b679SAndreas Boehler *
14*a1a3b679SAndreas Boehler * Adding this plugin to your server allows clients to store any arbitrary
15*a1a3b679SAndreas Boehler * WebDAV property.
16*a1a3b679SAndreas Boehler *
17*a1a3b679SAndreas Boehler * See:
18*a1a3b679SAndreas Boehler *   http://sabre.io/dav/property-storage/
19*a1a3b679SAndreas Boehler *
20*a1a3b679SAndreas Boehler * for more information.
21*a1a3b679SAndreas Boehler *
22*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH. (https://fruux.com/)
23*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/)
24*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License
25*a1a3b679SAndreas Boehler */
26*a1a3b679SAndreas Boehlerclass Plugin extends ServerPlugin {
27*a1a3b679SAndreas Boehler
28*a1a3b679SAndreas Boehler    /**
29*a1a3b679SAndreas Boehler     * If you only want this plugin to store properties for a limited set of
30*a1a3b679SAndreas Boehler     * paths, you can use a pathFilter to do this.
31*a1a3b679SAndreas Boehler     *
32*a1a3b679SAndreas Boehler     * The pathFilter should be a callable. The callable retrieves a path as
33*a1a3b679SAndreas Boehler     * its argument, and should return true or false wether it allows
34*a1a3b679SAndreas Boehler     * properties to be stored.
35*a1a3b679SAndreas Boehler     *
36*a1a3b679SAndreas Boehler     * @var callable
37*a1a3b679SAndreas Boehler     */
38*a1a3b679SAndreas Boehler    public $pathFilter;
39*a1a3b679SAndreas Boehler
40*a1a3b679SAndreas Boehler    /**
41*a1a3b679SAndreas Boehler     * Creates the plugin
42*a1a3b679SAndreas Boehler     *
43*a1a3b679SAndreas Boehler     * @param Backend\BackendInterface $backend
44*a1a3b679SAndreas Boehler     */
45*a1a3b679SAndreas Boehler    function __construct(Backend\BackendInterface $backend) {
46*a1a3b679SAndreas Boehler
47*a1a3b679SAndreas Boehler        $this->backend = $backend;
48*a1a3b679SAndreas Boehler
49*a1a3b679SAndreas Boehler    }
50*a1a3b679SAndreas Boehler
51*a1a3b679SAndreas Boehler    /**
52*a1a3b679SAndreas Boehler     * This initializes the plugin.
53*a1a3b679SAndreas Boehler     *
54*a1a3b679SAndreas Boehler     * This function is called by Sabre\DAV\Server, after
55*a1a3b679SAndreas Boehler     * addPlugin is called.
56*a1a3b679SAndreas Boehler     *
57*a1a3b679SAndreas Boehler     * This method should set up the required event subscriptions.
58*a1a3b679SAndreas Boehler     *
59*a1a3b679SAndreas Boehler     * @param Server $server
60*a1a3b679SAndreas Boehler     * @return void
61*a1a3b679SAndreas Boehler     */
62*a1a3b679SAndreas Boehler    function initialize(Server $server) {
63*a1a3b679SAndreas Boehler
64*a1a3b679SAndreas Boehler        $server->on('propFind',    [$this, 'propFind'], 130);
65*a1a3b679SAndreas Boehler        $server->on('propPatch',   [$this, 'propPatch'], 300);
66*a1a3b679SAndreas Boehler        $server->on('afterMove',   [$this, 'afterMove']);
67*a1a3b679SAndreas Boehler        $server->on('afterUnbind', [$this, 'afterUnbind']);
68*a1a3b679SAndreas Boehler
69*a1a3b679SAndreas Boehler    }
70*a1a3b679SAndreas Boehler
71*a1a3b679SAndreas Boehler    /**
72*a1a3b679SAndreas Boehler     * Called during PROPFIND operations.
73*a1a3b679SAndreas Boehler     *
74*a1a3b679SAndreas Boehler     * If there's any requested properties that don't have a value yet, this
75*a1a3b679SAndreas Boehler     * plugin will look in the property storage backend to find them.
76*a1a3b679SAndreas Boehler     *
77*a1a3b679SAndreas Boehler     * @param PropFind $propFind
78*a1a3b679SAndreas Boehler     * @param INode $node
79*a1a3b679SAndreas Boehler     * @return void
80*a1a3b679SAndreas Boehler     */
81*a1a3b679SAndreas Boehler    function propFind(PropFind $propFind, INode $node) {
82*a1a3b679SAndreas Boehler
83*a1a3b679SAndreas Boehler        $path = $propFind->getPath();
84*a1a3b679SAndreas Boehler        $pathFilter = $this->pathFilter;
85*a1a3b679SAndreas Boehler        if ($pathFilter && !$pathFilter($path)) return;
86*a1a3b679SAndreas Boehler        $this->backend->propFind($propFind->getPath(), $propFind);
87*a1a3b679SAndreas Boehler
88*a1a3b679SAndreas Boehler    }
89*a1a3b679SAndreas Boehler
90*a1a3b679SAndreas Boehler    /**
91*a1a3b679SAndreas Boehler     * Called during PROPPATCH operations
92*a1a3b679SAndreas Boehler     *
93*a1a3b679SAndreas Boehler     * If there's any updated properties that haven't been stored, the
94*a1a3b679SAndreas Boehler     * propertystorage backend can handle it.
95*a1a3b679SAndreas Boehler     *
96*a1a3b679SAndreas Boehler     * @param string $path
97*a1a3b679SAndreas Boehler     * @param PropPatch $propPatch
98*a1a3b679SAndreas Boehler     * @return void
99*a1a3b679SAndreas Boehler     */
100*a1a3b679SAndreas Boehler    function propPatch($path, PropPatch $propPatch) {
101*a1a3b679SAndreas Boehler
102*a1a3b679SAndreas Boehler        $pathFilter = $this->pathFilter;
103*a1a3b679SAndreas Boehler        if ($pathFilter && !$pathFilter($path)) return;
104*a1a3b679SAndreas Boehler        $this->backend->propPatch($path, $propPatch);
105*a1a3b679SAndreas Boehler
106*a1a3b679SAndreas Boehler    }
107*a1a3b679SAndreas Boehler
108*a1a3b679SAndreas Boehler    /**
109*a1a3b679SAndreas Boehler     * Called after a node is deleted.
110*a1a3b679SAndreas Boehler     *
111*a1a3b679SAndreas Boehler     * This allows the backend to clean up any properties still in the
112*a1a3b679SAndreas Boehler     * database.
113*a1a3b679SAndreas Boehler     *
114*a1a3b679SAndreas Boehler     * @param string $path
115*a1a3b679SAndreas Boehler     * @return void
116*a1a3b679SAndreas Boehler     */
117*a1a3b679SAndreas Boehler    function afterUnbind($path) {
118*a1a3b679SAndreas Boehler
119*a1a3b679SAndreas Boehler        $pathFilter = $this->pathFilter;
120*a1a3b679SAndreas Boehler        if ($pathFilter && !$pathFilter($path)) return;
121*a1a3b679SAndreas Boehler        $this->backend->delete($path);
122*a1a3b679SAndreas Boehler
123*a1a3b679SAndreas Boehler    }
124*a1a3b679SAndreas Boehler
125*a1a3b679SAndreas Boehler    /**
126*a1a3b679SAndreas Boehler     * Called after a node is moved.
127*a1a3b679SAndreas Boehler     *
128*a1a3b679SAndreas Boehler     * This allows the backend to move all the associated properties.
129*a1a3b679SAndreas Boehler     *
130*a1a3b679SAndreas Boehler     * @param string $source
131*a1a3b679SAndreas Boehler     * @param string $destination
132*a1a3b679SAndreas Boehler     * @return void
133*a1a3b679SAndreas Boehler     */
134*a1a3b679SAndreas Boehler    function afterMove($source, $destination) {
135*a1a3b679SAndreas Boehler
136*a1a3b679SAndreas Boehler        $pathFilter = $this->pathFilter;
137*a1a3b679SAndreas Boehler        if ($pathFilter && !$pathFilter($source)) return;
138*a1a3b679SAndreas Boehler        // If the destination is filtered, afterUnbind will handle cleaning up
139*a1a3b679SAndreas Boehler        // the properties.
140*a1a3b679SAndreas Boehler        if ($pathFilter && !$pathFilter($destination)) return;
141*a1a3b679SAndreas Boehler
142*a1a3b679SAndreas Boehler        $this->backend->move($source, $destination);
143*a1a3b679SAndreas Boehler
144*a1a3b679SAndreas Boehler    }
145*a1a3b679SAndreas Boehler
146*a1a3b679SAndreas Boehler    /**
147*a1a3b679SAndreas Boehler     * Returns a plugin name.
148*a1a3b679SAndreas Boehler     *
149*a1a3b679SAndreas Boehler     * Using this name other plugins will be able to access other plugins
150*a1a3b679SAndreas Boehler     * using \Sabre\DAV\Server::getPlugin
151*a1a3b679SAndreas Boehler     *
152*a1a3b679SAndreas Boehler     * @return string
153*a1a3b679SAndreas Boehler     */
154*a1a3b679SAndreas Boehler    function getPluginName() {
155*a1a3b679SAndreas Boehler
156*a1a3b679SAndreas Boehler        return 'property-storage';
157*a1a3b679SAndreas Boehler
158*a1a3b679SAndreas Boehler    }
159*a1a3b679SAndreas Boehler
160*a1a3b679SAndreas Boehler    /**
161*a1a3b679SAndreas Boehler     * Returns a bunch of meta-data about the plugin.
162*a1a3b679SAndreas Boehler     *
163*a1a3b679SAndreas Boehler     * Providing this information is optional, and is mainly displayed by the
164*a1a3b679SAndreas Boehler     * Browser plugin.
165*a1a3b679SAndreas Boehler     *
166*a1a3b679SAndreas Boehler     * The description key in the returned array may contain html and will not
167*a1a3b679SAndreas Boehler     * be sanitized.
168*a1a3b679SAndreas Boehler     *
169*a1a3b679SAndreas Boehler     * @return array
170*a1a3b679SAndreas Boehler     */
171*a1a3b679SAndreas Boehler    function getPluginInfo() {
172*a1a3b679SAndreas Boehler
173*a1a3b679SAndreas Boehler        return [
174*a1a3b679SAndreas Boehler            'name'        => $this->getPluginName(),
175*a1a3b679SAndreas Boehler            'description' => 'This plugin allows any arbitrary WebDAV property to be set on any resource.',
176*a1a3b679SAndreas Boehler            'link'        => 'http://sabre.io/dav/property-storage/',
177*a1a3b679SAndreas Boehler        ];
178*a1a3b679SAndreas Boehler
179*a1a3b679SAndreas Boehler    }
180*a1a3b679SAndreas Boehler}
181