Card.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CardDAV;
  4. use Sabre\DAV;
  5. use Sabre\DAVACL;
  6. /**
  7. * The Card object represents a single Card from an addressbook.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class Card extends DAV\File implements ICard, DAVACL\IACL
  14. {
  15. use DAVACL\ACLTrait;
  16. /**
  17. * CardDAV backend.
  18. *
  19. * @var Backend\BackendInterface
  20. */
  21. protected $carddavBackend;
  22. /**
  23. * Array with information about this Card.
  24. *
  25. * @var array
  26. */
  27. protected $cardData;
  28. /**
  29. * Array with information about the containing addressbook.
  30. *
  31. * @var array
  32. */
  33. protected $addressBookInfo;
  34. /**
  35. * Constructor.
  36. */
  37. public function __construct(Backend\BackendInterface $carddavBackend, array $addressBookInfo, array $cardData)
  38. {
  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. {
  50. return $this->cardData['uri'];
  51. }
  52. /**
  53. * Returns the VCard-formatted object.
  54. *
  55. * @return string
  56. */
  57. public function get()
  58. {
  59. // Pre-populating 'carddata' is optional. If we don't yet have it
  60. // already, we fetch it from the backend.
  61. if (!isset($this->cardData['carddata'])) {
  62. $this->cardData = $this->carddavBackend->getCard($this->addressBookInfo['id'], $this->cardData['uri']);
  63. }
  64. return $this->cardData['carddata'];
  65. }
  66. /**
  67. * Updates the VCard-formatted object.
  68. *
  69. * @param string $cardData
  70. *
  71. * @return string|null
  72. */
  73. public function put($cardData)
  74. {
  75. if (is_resource($cardData)) {
  76. $cardData = stream_get_contents($cardData);
  77. }
  78. // Converting to UTF-8, if needed
  79. $cardData = DAV\StringUtil::ensureUTF8($cardData);
  80. $etag = $this->carddavBackend->updateCard($this->addressBookInfo['id'], $this->cardData['uri'], $cardData);
  81. $this->cardData['carddata'] = $cardData;
  82. $this->cardData['etag'] = $etag;
  83. return $etag;
  84. }
  85. /**
  86. * Deletes the card.
  87. */
  88. public function delete()
  89. {
  90. $this->carddavBackend->deleteCard($this->addressBookInfo['id'], $this->cardData['uri']);
  91. }
  92. /**
  93. * Returns the mime content-type.
  94. *
  95. * @return string
  96. */
  97. public function getContentType()
  98. {
  99. return 'text/vcard; charset=utf-8';
  100. }
  101. /**
  102. * Returns an ETag for this object.
  103. *
  104. * @return string
  105. */
  106. public function getETag()
  107. {
  108. if (isset($this->cardData['etag'])) {
  109. return $this->cardData['etag'];
  110. } else {
  111. $data = $this->get();
  112. if (is_string($data)) {
  113. return '"'.md5($data).'"';
  114. } else {
  115. // We refuse to calculate the md5 if it's a stream.
  116. return null;
  117. }
  118. }
  119. }
  120. /**
  121. * Returns the last modification date as a unix timestamp.
  122. *
  123. * @return int
  124. */
  125. public function getLastModified()
  126. {
  127. return isset($this->cardData['lastmodified']) ? $this->cardData['lastmodified'] : null;
  128. }
  129. /**
  130. * Returns the size of this object in bytes.
  131. *
  132. * @return int
  133. */
  134. public function getSize()
  135. {
  136. if (array_key_exists('size', $this->cardData)) {
  137. return $this->cardData['size'];
  138. } else {
  139. return strlen($this->get());
  140. }
  141. }
  142. /**
  143. * Returns the owner principal.
  144. *
  145. * This must be a url to a principal, or null if there's no owner
  146. *
  147. * @return string|null
  148. */
  149. public function getOwner()
  150. {
  151. return $this->addressBookInfo['principaluri'];
  152. }
  153. /**
  154. * Returns a list of ACE's for this node.
  155. *
  156. * Each ACE has the following properties:
  157. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  158. * currently the only supported privileges
  159. * * 'principal', a url to the principal who owns the node
  160. * * 'protected' (optional), indicating that this ACE is not allowed to
  161. * be updated.
  162. *
  163. * @return array
  164. */
  165. public function getACL()
  166. {
  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 [
  172. [
  173. 'privilege' => '{DAV:}all',
  174. 'principal' => $this->addressBookInfo['principaluri'],
  175. 'protected' => true,
  176. ],
  177. ];
  178. }
  179. }