PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Sabre/CardDAV/Card.php

https://github.com/KOLANICH/SabreDAV
PHP | 265 lines | 89 code | 55 blank | 121 comment | 9 complexity | f6eb8e25e719b682dc245d302c909818 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace Sabre\CardDAV;
  3. use Sabre\DAVACL;
  4. use Sabre\DAV;
  5. /**
  6. * The Card object represents a single Card from an addressbook
  7. *
  8. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  9. * @author Evert Pot (http://evertpot.com/)
  10. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  11. */
  12. class Card extends DAV\File implements ICard, DAVACL\IACL {
  13. /**
  14. * CardDAV backend
  15. *
  16. * @var Backend\BackendInterface
  17. */
  18. protected $carddavBackend;
  19. /**
  20. * Array with information about this Card
  21. *
  22. * @var array
  23. */
  24. protected $cardData;
  25. /**
  26. * Array with information about the containing addressbook
  27. *
  28. * @var array
  29. */
  30. protected $addressBookInfo;
  31. /**
  32. * Constructor
  33. *
  34. * @param Backend\BackendInterface $carddavBackend
  35. * @param array $addressBookInfo
  36. * @param array $cardData
  37. */
  38. public function __construct(Backend\BackendInterface $carddavBackend,array $addressBookInfo,array $cardData) {
  39. $this->carddavBackend = $carddavBackend;
  40. $this->addressBookInfo = $addressBookInfo;
  41. $this->cardData = $cardData;
  42. }
  43. /**
  44. * Returns the uri for this object
  45. *
  46. * @return string
  47. */
  48. public function getName() {
  49. return $this->cardData['uri'];
  50. }
  51. /**
  52. * Returns the VCard-formatted object
  53. *
  54. * @return string
  55. */
  56. public function get() {
  57. // Pre-populating 'carddata' is optional. If we don't yet have it
  58. // already, we fetch it from the backend.
  59. if (!isset($this->cardData['carddata'])) {
  60. $this->cardData = $this->carddavBackend->getCard($this->addressBookInfo['id'], $this->cardData['uri']);
  61. }
  62. return $this->cardData['carddata'];
  63. }
  64. /**
  65. * Updates the VCard-formatted object
  66. *
  67. * @param string $cardData
  68. * @return string|null
  69. */
  70. public function put($cardData) {
  71. if (is_resource($cardData))
  72. $cardData = stream_get_contents($cardData);
  73. // Converting to UTF-8, if needed
  74. $cardData = DAV\StringUtil::ensureUTF8($cardData);
  75. $etag = $this->carddavBackend->updateCard($this->addressBookInfo['id'],$this->cardData['uri'],$cardData);
  76. $this->cardData['carddata'] = $cardData;
  77. $this->cardData['etag'] = $etag;
  78. return $etag;
  79. }
  80. /**
  81. * Deletes the card
  82. *
  83. * @return void
  84. */
  85. public function delete() {
  86. $this->carddavBackend->deleteCard($this->addressBookInfo['id'],$this->cardData['uri']);
  87. }
  88. /**
  89. * Returns the mime content-type
  90. *
  91. * @return string
  92. */
  93. public function getContentType() {
  94. return 'text/x-vcard; charset=utf-8';
  95. }
  96. /**
  97. * Returns an ETag for this object
  98. *
  99. * @return string
  100. */
  101. public function getETag() {
  102. if (isset($this->cardData['etag'])) {
  103. return $this->cardData['etag'];
  104. } else {
  105. $data = $this->get();
  106. if (is_string($data)) {
  107. return '"' . md5($data) . '"';
  108. } else {
  109. // We refuse to calculate the md5 if it's a stream.
  110. return null;
  111. }
  112. }
  113. }
  114. /**
  115. * Returns the last modification date as a unix timestamp
  116. *
  117. * @return int
  118. */
  119. public function getLastModified() {
  120. return isset($this->cardData['lastmodified'])?$this->cardData['lastmodified']:null;
  121. }
  122. /**
  123. * Returns the size of this object in bytes
  124. *
  125. * @return int
  126. */
  127. public function getSize() {
  128. if (array_key_exists('size', $this->cardData)) {
  129. return $this->cardData['size'];
  130. } else {
  131. return strlen($this->get());
  132. }
  133. }
  134. /**
  135. * Returns the owner principal
  136. *
  137. * This must be a url to a principal, or null if there's no owner
  138. *
  139. * @return string|null
  140. */
  141. public function getOwner() {
  142. return $this->addressBookInfo['principaluri'];
  143. }
  144. /**
  145. * Returns a group principal
  146. *
  147. * This must be a url to a principal, or null if there's no owner
  148. *
  149. * @return string|null
  150. */
  151. public function getGroup() {
  152. return null;
  153. }
  154. /**
  155. * Returns a list of ACE's for this node.
  156. *
  157. * Each ACE has the following properties:
  158. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  159. * currently the only supported privileges
  160. * * 'principal', a url to the principal who owns the node
  161. * * 'protected' (optional), indicating that this ACE is not allowed to
  162. * be updated.
  163. *
  164. * @return array
  165. */
  166. public function getACL() {
  167. // An alternative acl may be specified through the cardData array.
  168. if (isset($this->cardData['acl'])) {
  169. return $this->cardData['acl'];
  170. }
  171. return array(
  172. array(
  173. 'privilege' => '{DAV:}read',
  174. 'principal' => $this->addressBookInfo['principaluri'],
  175. 'protected' => true,
  176. ),
  177. array(
  178. 'privilege' => '{DAV:}write',
  179. 'principal' => $this->addressBookInfo['principaluri'],
  180. 'protected' => true,
  181. ),
  182. );
  183. }
  184. /**
  185. * Updates the ACL
  186. *
  187. * This method will receive a list of new ACE's.
  188. *
  189. * @param array $acl
  190. * @return void
  191. */
  192. public function setACL(array $acl) {
  193. throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
  194. }
  195. /**
  196. * Returns the list of supported privileges for this node.
  197. *
  198. * The returned data structure is a list of nested privileges.
  199. * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
  200. * standard structure.
  201. *
  202. * If null is returned from this method, the default privilege set is used,
  203. * which is fine for most common usecases.
  204. *
  205. * @return array|null
  206. */
  207. public function getSupportedPrivilegeSet() {
  208. return null;
  209. }
  210. }