You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CardSearchDao.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\ContactsInteraction\Db;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. use OCP\IUser;
  28. use function is_resource;
  29. use function stream_get_contents;
  30. class CardSearchDao {
  31. /** @var IDBConnection */
  32. private $db;
  33. public function __construct(IDBConnection $db) {
  34. $this->db = $db;
  35. }
  36. public function findExisting(IUser $user,
  37. ?string $uid,
  38. ?string $email,
  39. ?string $cloudId): ?string {
  40. $addressbooksQuery = $this->db->getQueryBuilder();
  41. $cardQuery = $this->db->getQueryBuilder();
  42. $propQuery = $this->db->getQueryBuilder();
  43. $propOr = $propQuery->expr()->orX();
  44. if ($uid !== null) {
  45. $propOr->add($propQuery->expr()->andX(
  46. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')),
  47. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid))
  48. ));
  49. }
  50. if ($email !== null) {
  51. $propOr->add($propQuery->expr()->andX(
  52. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')),
  53. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email))
  54. ));
  55. }
  56. if ($cloudId !== null) {
  57. $propOr->add($propQuery->expr()->andX(
  58. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')),
  59. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId))
  60. ));
  61. }
  62. $addressbooksQuery->selectDistinct('id')
  63. ->from('addressbooks')
  64. ->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter("principals/users/" . $user->getUID())));
  65. $propQuery->selectDistinct('cardid')
  66. ->from('cards_properties')
  67. ->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  68. ->andWhere($propOr)
  69. ->groupBy('cardid');
  70. $cardQuery->select('carddata')
  71. ->from('cards')
  72. ->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  73. ->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  74. ->setMaxResults(1);
  75. $result = $cardQuery->execute();
  76. /** @var string|resource|false $card */
  77. $card = $result->fetchOne();
  78. if ($card === false) {
  79. return null;
  80. }
  81. if (is_resource($card)) {
  82. return stream_get_contents($card);
  83. }
  84. return $card;
  85. }
  86. }