]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/CardDAV/Card.php
Move friendica-specific parts into an own subdirectory
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / CardDAV / Card.php
1 <?php
2
3 /**
4  * The Card object represents a single Card from an addressbook
5  *
6  * @package Sabre
7  * @subpackage CardDAV
8  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9  * @author Evert Pot (http://www.rooftopsolutions.nl/)
10  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
11  */
12 class Sabre_CardDAV_Card extends Sabre_DAV_File implements Sabre_CardDAV_ICard, Sabre_DAVACL_IACL {
13
14     /**
15      * CardDAV backend
16      *
17      * @var Sabre_CardDAV_Backend_Abstract
18      */
19     private $carddavBackend;
20
21     /**
22      * Array with information about this Card
23      *
24      * @var array
25      */
26     private $cardData;
27
28     /**
29      * Array with information about the containing addressbook
30      *
31      * @var array
32      */
33     private $addressBookInfo;
34
35     /**
36      * Constructor
37      *
38      * @param Sabre_CardDAV_Backend_Abstract $carddavBackend
39      * @param array $addressBookInfo
40      * @param array $cardData
41      */
42     public function __construct(Sabre_CardDAV_Backend_Abstract $carddavBackend,array $addressBookInfo,array $cardData) {
43
44         $this->carddavBackend = $carddavBackend;
45         $this->addressBookInfo = $addressBookInfo;
46         $this->cardData = $cardData;
47
48     }
49
50     /**
51      * Returns the uri for this object
52      *
53      * @return string
54      */
55     public function getName() {
56
57         return $this->cardData['uri'];
58
59     }
60
61     /**
62      * Returns the VCard-formatted object
63      *
64      * @return string
65      */
66     public function get() {
67
68         // Pre-populating 'carddata' is optional. If we don't yet have it
69         // already, we fetch it from the backend.
70         if (!isset($this->cardData['carddata'])) {
71             $this->cardData = $this->carddavBackend->getCard($this->addressBookInfo['id'], $this->cardData['uri']);
72         }
73         return $this->cardData['carddata'];
74
75     }
76
77     /**
78      * Updates the VCard-formatted object
79      *
80      * @param string $cardData
81      * @return string|null
82      */
83     public function put($cardData) {
84
85         if (is_resource($cardData))
86             $cardData = stream_get_contents($cardData);
87
88         // Converting to UTF-8, if needed
89         $cardData = Sabre_DAV_StringUtil::ensureUTF8($cardData);
90
91         $etag = $this->carddavBackend->updateCard($this->addressBookInfo['id'],$this->cardData['uri'],$cardData);
92         $this->cardData['carddata'] = $cardData;
93         $this->cardData['etag'] = $etag;
94
95         return $etag;
96
97     }
98
99     /**
100      * Deletes the card
101      *
102      * @return void
103      */
104     public function delete() {
105
106         $this->carddavBackend->deleteCard($this->addressBookInfo['id'],$this->cardData['uri']);
107
108     }
109
110     /**
111      * Returns the mime content-type
112      *
113      * @return string
114      */
115     public function getContentType() {
116
117         return 'text/x-vcard; charset=utf-8';
118
119     }
120
121     /**
122      * Returns an ETag for this object
123      *
124      * @return string
125      */
126     public function getETag() {
127
128         if (isset($this->cardData['etag'])) {
129             return $this->cardData['etag'];
130         } else {
131             return '"' . md5($this->get()) . '"';
132         }
133
134     }
135
136     /**
137      * Returns the last modification date as a unix timestamp
138      *
139      * @return int
140      */
141     public function getLastModified() {
142
143         return isset($this->cardData['lastmodified'])?$this->cardData['lastmodified']:null;
144
145     }
146
147     /**
148      * Returns the size of this object in bytes
149      *
150      * @return int
151      */
152     public function getSize() {
153
154         if (array_key_exists('size', $this->cardData)) {
155             return $this->cardData['size'];
156         } else {
157             return strlen($this->get());
158         }
159
160     }
161
162     /**
163      * Returns the owner principal
164      *
165      * This must be a url to a principal, or null if there's no owner
166      *
167      * @return string|null
168      */
169     public function getOwner() {
170
171         return $this->addressBookInfo['principaluri'];
172
173     }
174
175     /**
176      * Returns a group principal
177      *
178      * This must be a url to a principal, or null if there's no owner
179      *
180      * @return string|null
181      */
182     public function getGroup() {
183
184         return null;
185
186     }
187
188     /**
189      * Returns a list of ACE's for this node.
190      *
191      * Each ACE has the following properties:
192      *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
193      *     currently the only supported privileges
194      *   * 'principal', a url to the principal who owns the node
195      *   * 'protected' (optional), indicating that this ACE is not allowed to
196      *      be updated.
197      *
198      * @return array
199      */
200     public function getACL() {
201
202         return array(
203             array(
204                 'privilege' => '{DAV:}read',
205                 'principal' => $this->addressBookInfo['principaluri'],
206                 'protected' => true,
207             ),
208             array(
209                 'privilege' => '{DAV:}write',
210                 'principal' => $this->addressBookInfo['principaluri'],
211                 'protected' => true,
212             ),
213         );
214
215     }
216
217     /**
218      * Updates the ACL
219      *
220      * This method will receive a list of new ACE's.
221      *
222      * @param array $acl
223      * @return void
224      */
225     public function setACL(array $acl) {
226
227         throw new Sabre_DAV_Exception_MethodNotAllowed('Changing ACL is not yet supported');
228
229     }
230
231     /**
232      * Returns the list of supported privileges for this node.
233      *
234      * The returned data structure is a list of nested privileges.
235      * See Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet for a simple
236      * standard structure.
237      *
238      * If null is returned from this method, the default privilege set is used,
239      * which is fine for most common usecases.
240      *
241      * @return array|null
242      */
243     public function getSupportedPrivilegeSet() {
244
245         return null;
246
247     }
248
249 }
250