PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/rainloop/v/0.0.0/app/libraries/SabreForRainLoop/CardDAV/Backend/PDO.php

https://gitlab.com/wuhang2003/rainloop-webmail
PHP | 333 lines | 120 code | 66 blank | 147 comment | 1 complexity | 91fafd3419c11165875a192645ee9f5a MD5 | raw file
  1. <?php
  2. namespace SabreForRainLoop\CardDAV\Backend;
  3. use SabreForRainLoop\CardDAV;
  4. use SabreForRainLoop\DAV;
  5. /**
  6. * PDO CardDAV backend
  7. *
  8. * This CardDAV backend uses PDO to store addressbooks
  9. *
  10. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  11. * @author Evert Pot (http://evertpot.com/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class PDO extends AbstractBackend {
  15. /**
  16. * PDO connection
  17. *
  18. * @var PDO
  19. */
  20. protected $pdo;
  21. /**
  22. * The PDO table name used to store addressbooks
  23. */
  24. protected $addressBooksTableName;
  25. /**
  26. * The PDO table name used to store cards
  27. */
  28. protected $cardsTableName;
  29. /**
  30. * Sets up the object
  31. *
  32. * @param \PDO $pdo
  33. * @param string $addressBooksTableName
  34. * @param string $cardsTableName
  35. */
  36. public function __construct(\PDO $pdo, $addressBooksTableName = 'addressbooks', $cardsTableName = 'cards') {
  37. $this->pdo = $pdo;
  38. $this->addressBooksTableName = $addressBooksTableName;
  39. $this->cardsTableName = $cardsTableName;
  40. }
  41. /**
  42. * Returns the list of addressbooks for a specific user.
  43. *
  44. * @param string $principalUri
  45. * @return array
  46. */
  47. public function getAddressBooksForUser($principalUri) {
  48. $stmt = $this->pdo->prepare('SELECT id, uri, displayname, principaluri, description, ctag FROM '.$this->addressBooksTableName.' WHERE principaluri = ?');
  49. $stmt->execute(array($principalUri));
  50. $addressBooks = array();
  51. foreach($stmt->fetchAll() as $row) {
  52. $addressBooks[] = array(
  53. 'id' => $row['id'],
  54. 'uri' => $row['uri'],
  55. 'principaluri' => $row['principaluri'],
  56. '{DAV:}displayname' => $row['displayname'],
  57. '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'],
  58. '{http://calendarserver.org/ns/}getctag' => $row['ctag'],
  59. '{' . CardDAV\Plugin::NS_CARDDAV . '}supported-address-data' =>
  60. new CardDAV\Property\SupportedAddressData(),
  61. );
  62. }
  63. return $addressBooks;
  64. }
  65. /**
  66. * Updates an addressbook's properties
  67. *
  68. * See SabreForRainLoop\DAV\IProperties for a description of the mutations array, as
  69. * well as the return value.
  70. *
  71. * @param mixed $addressBookId
  72. * @param array $mutations
  73. * @see SabreForRainLoop\DAV\IProperties::updateProperties
  74. * @return bool|array
  75. */
  76. public function updateAddressBook($addressBookId, array $mutations) {
  77. $updates = array();
  78. foreach($mutations as $property=>$newValue) {
  79. switch($property) {
  80. case '{DAV:}displayname' :
  81. $updates['displayname'] = $newValue;
  82. break;
  83. case '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' :
  84. $updates['description'] = $newValue;
  85. break;
  86. default :
  87. // If any unsupported values were being updated, we must
  88. // let the entire request fail.
  89. return false;
  90. }
  91. }
  92. // No values are being updated?
  93. if (!$updates) {
  94. return false;
  95. }
  96. $query = 'UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 ';
  97. foreach($updates as $key=>$value) {
  98. $query.=', `' . $key . '` = :' . $key . ' ';
  99. }
  100. $query.=' WHERE id = :addressbookid';
  101. $stmt = $this->pdo->prepare($query);
  102. $updates['addressbookid'] = $addressBookId;
  103. $stmt->execute($updates);
  104. return true;
  105. }
  106. /**
  107. * Creates a new address book
  108. *
  109. * @param string $principalUri
  110. * @param string $url Just the 'basename' of the url.
  111. * @param array $properties
  112. * @return void
  113. */
  114. public function createAddressBook($principalUri, $url, array $properties) {
  115. $values = array(
  116. 'displayname' => null,
  117. 'description' => null,
  118. 'principaluri' => $principalUri,
  119. 'uri' => $url,
  120. );
  121. foreach($properties as $property=>$newValue) {
  122. switch($property) {
  123. case '{DAV:}displayname' :
  124. $values['displayname'] = $newValue;
  125. break;
  126. case '{' . CardDAV\Plugin::NS_CARDDAV . '}addressbook-description' :
  127. $values['description'] = $newValue;
  128. break;
  129. default :
  130. throw new DAV\Exception\BadRequest('Unknown property: ' . $property);
  131. }
  132. }
  133. $query = 'INSERT INTO ' . $this->addressBooksTableName . ' (uri, displayname, description, principaluri, ctag) VALUES (:uri, :displayname, :description, :principaluri, 1)';
  134. $stmt = $this->pdo->prepare($query);
  135. $stmt->execute($values);
  136. }
  137. /**
  138. * Deletes an entire addressbook and all its contents
  139. *
  140. * @param int $addressBookId
  141. * @return void
  142. */
  143. public function deleteAddressBook($addressBookId) {
  144. $stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?');
  145. $stmt->execute(array($addressBookId));
  146. $stmt = $this->pdo->prepare('DELETE FROM ' . $this->addressBooksTableName . ' WHERE id = ?');
  147. $stmt->execute(array($addressBookId));
  148. }
  149. /**
  150. * Returns all cards for a specific addressbook id.
  151. *
  152. * This method should return the following properties for each card:
  153. * * carddata - raw vcard data
  154. * * uri - Some unique url
  155. * * lastmodified - A unix timestamp
  156. *
  157. * It's recommended to also return the following properties:
  158. * * etag - A unique etag. This must change every time the card changes.
  159. * * size - The size of the card in bytes.
  160. *
  161. * If these last two properties are provided, less time will be spent
  162. * calculating them. If they are specified, you can also ommit carddata.
  163. * This may speed up certain requests, especially with large cards.
  164. *
  165. * @param mixed $addressbookId
  166. * @return array
  167. */
  168. public function getCards($addressbookId) {
  169. $stmt = $this->pdo->prepare('SELECT id, carddata, uri, lastmodified FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?');
  170. $stmt->execute(array($addressbookId));
  171. return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  172. }
  173. /**
  174. * Returns a specfic card.
  175. *
  176. * The same set of properties must be returned as with getCards. The only
  177. * exception is that 'carddata' is absolutely required.
  178. *
  179. * @param mixed $addressBookId
  180. * @param string $cardUri
  181. * @return array
  182. */
  183. public function getCard($addressBookId, $cardUri) {
  184. $stmt = $this->pdo->prepare('SELECT id, carddata, uri, lastmodified FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND uri = ? LIMIT 1');
  185. $stmt->execute(array($addressBookId, $cardUri));
  186. $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  187. return (count($result)>0?$result[0]:false);
  188. }
  189. /**
  190. * Creates a new card.
  191. *
  192. * The addressbook id will be passed as the first argument. This is the
  193. * same id as it is returned from the getAddressbooksForUser method.
  194. *
  195. * The cardUri is a base uri, and doesn't include the full path. The
  196. * cardData argument is the vcard body, and is passed as a string.
  197. *
  198. * It is possible to return an ETag from this method. This ETag is for the
  199. * newly created resource, and must be enclosed with double quotes (that
  200. * is, the string itself must contain the double quotes).
  201. *
  202. * You should only return the ETag if you store the carddata as-is. If a
  203. * subsequent GET request on the same card does not have the same body,
  204. * byte-by-byte and you did return an ETag here, clients tend to get
  205. * confused.
  206. *
  207. * If you don't return an ETag, you can just return null.
  208. *
  209. * @param mixed $addressBookId
  210. * @param string $cardUri
  211. * @param string $cardData
  212. * @return string|null
  213. */
  214. public function createCard($addressBookId, $cardUri, $cardData) {
  215. $stmt = $this->pdo->prepare('INSERT INTO ' . $this->cardsTableName . ' (carddata, uri, lastmodified, addressbookid) VALUES (?, ?, ?, ?)');
  216. $result = $stmt->execute(array($cardData, $cardUri, time(), $addressBookId));
  217. $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
  218. $stmt2->execute(array($addressBookId));
  219. return '"' . md5($cardData) . '"';
  220. }
  221. /**
  222. * Updates a card.
  223. *
  224. * The addressbook id will be passed as the first argument. This is the
  225. * same id as it is returned from the getAddressbooksForUser method.
  226. *
  227. * The cardUri is a base uri, and doesn't include the full path. The
  228. * cardData argument is the vcard body, and is passed as a string.
  229. *
  230. * It is possible to return an ETag from this method. This ETag should
  231. * match that of the updated resource, and must be enclosed with double
  232. * quotes (that is: the string itself must contain the actual quotes).
  233. *
  234. * You should only return the ETag if you store the carddata as-is. If a
  235. * subsequent GET request on the same card does not have the same body,
  236. * byte-by-byte and you did return an ETag here, clients tend to get
  237. * confused.
  238. *
  239. * If you don't return an ETag, you can just return null.
  240. *
  241. * @param mixed $addressBookId
  242. * @param string $cardUri
  243. * @param string $cardData
  244. * @return string|null
  245. */
  246. public function updateCard($addressBookId, $cardUri, $cardData) {
  247. $stmt = $this->pdo->prepare('UPDATE ' . $this->cardsTableName . ' SET carddata = ?, lastmodified = ? WHERE uri = ? AND addressbookid =?');
  248. $stmt->execute(array($cardData, time(), $cardUri, $addressBookId));
  249. $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
  250. $stmt2->execute(array($addressBookId));
  251. return '"' . md5($cardData) . '"';
  252. }
  253. /**
  254. * Deletes a card
  255. *
  256. * @param mixed $addressBookId
  257. * @param string $cardUri
  258. * @return bool
  259. */
  260. public function deleteCard($addressBookId, $cardUri) {
  261. $stmt = $this->pdo->prepare('DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND uri = ?');
  262. $stmt->execute(array($addressBookId, $cardUri));
  263. $stmt2 = $this->pdo->prepare('UPDATE ' . $this->addressBooksTableName . ' SET ctag = ctag + 1 WHERE id = ?');
  264. $stmt2->execute(array($addressBookId));
  265. return $stmt->rowCount()===1;
  266. }
  267. }