@@ -83,6 +83,9 @@ |
||
| 83 | 83 | } while (count($rows) > 0); |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | + /** |
|
| 87 | + * @param string $path |
|
| 88 | + */ |
|
| 86 | 89 | private function getId($storage, $path) { |
| 87 | 90 | if (!$this->getIdQuery) { |
| 88 | 91 | $builder = $this->connection->getQueryBuilder(); |
@@ -29,151 +29,151 @@ |
||
| 29 | 29 | use OCP\Migration\IRepairStep; |
| 30 | 30 | |
| 31 | 31 | class RepairInvalidPaths implements IRepairStep { |
| 32 | - const MAX_ROWS = 1000; |
|
| 33 | - |
|
| 34 | - /** @var IDBConnection */ |
|
| 35 | - private $connection; |
|
| 36 | - /** @var IConfig */ |
|
| 37 | - private $config; |
|
| 38 | - |
|
| 39 | - private $getIdQuery; |
|
| 40 | - private $updateQuery; |
|
| 41 | - private $reparentQuery; |
|
| 42 | - private $deleteQuery; |
|
| 43 | - |
|
| 44 | - public function __construct(IDBConnection $connection, IConfig $config) { |
|
| 45 | - $this->connection = $connection; |
|
| 46 | - $this->config = $config; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - |
|
| 50 | - public function getName() { |
|
| 51 | - return 'Repair invalid paths in file cache'; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return \Generator |
|
| 56 | - * @suppress SqlInjectionChecker |
|
| 57 | - */ |
|
| 58 | - private function getInvalidEntries() { |
|
| 59 | - $builder = $this->connection->getQueryBuilder(); |
|
| 60 | - |
|
| 61 | - $computedPath = $builder->func()->concat( |
|
| 62 | - 'p.path', |
|
| 63 | - $builder->func()->concat($builder->createNamedParameter('/'), 'f.name') |
|
| 64 | - ); |
|
| 65 | - |
|
| 66 | - //select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name; |
|
| 67 | - $query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage') |
|
| 68 | - ->from('filecache', 'f') |
|
| 69 | - ->innerJoin('f', 'filecache', 'p', $builder->expr()->andX( |
|
| 70 | - $builder->expr()->eq('f.parent', 'p.fileid'), |
|
| 71 | - $builder->expr()->neq('p.name', $builder->createNamedParameter('')) |
|
| 72 | - )) |
|
| 73 | - ->where($builder->expr()->neq('f.path', $computedPath)) |
|
| 74 | - ->setMaxResults(self::MAX_ROWS); |
|
| 75 | - |
|
| 76 | - do { |
|
| 77 | - $result = $query->execute(); |
|
| 78 | - $rows = $result->fetchAll(); |
|
| 79 | - foreach ($rows as $row) { |
|
| 80 | - yield $row; |
|
| 81 | - } |
|
| 82 | - $result->closeCursor(); |
|
| 83 | - } while (count($rows) > 0); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - private function getId($storage, $path) { |
|
| 87 | - if (!$this->getIdQuery) { |
|
| 88 | - $builder = $this->connection->getQueryBuilder(); |
|
| 89 | - |
|
| 90 | - $this->getIdQuery = $builder->select('fileid') |
|
| 91 | - ->from('filecache') |
|
| 92 | - ->where($builder->expr()->eq('storage', $builder->createParameter('storage'))) |
|
| 93 | - ->andWhere($builder->expr()->eq('path', $builder->createParameter('path'))); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - $this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT); |
|
| 97 | - $this->getIdQuery->setParameter('path', $path); |
|
| 98 | - |
|
| 99 | - return $this->getIdQuery->execute()->fetchColumn(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $fileid |
|
| 104 | - * @param string $newPath |
|
| 105 | - * @suppress SqlInjectionChecker |
|
| 106 | - */ |
|
| 107 | - private function update($fileid, $newPath) { |
|
| 108 | - if (!$this->updateQuery) { |
|
| 109 | - $builder = $this->connection->getQueryBuilder(); |
|
| 110 | - |
|
| 111 | - $this->updateQuery = $builder->update('filecache') |
|
| 112 | - ->set('path', $builder->createParameter('newpath')) |
|
| 113 | - ->set('path_hash', $builder->func()->md5($builder->createParameter('newpath'))) |
|
| 114 | - ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid'))); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->updateQuery->setParameter('newpath', $newPath); |
|
| 118 | - $this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT); |
|
| 119 | - |
|
| 120 | - $this->updateQuery->execute(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - private function reparent($from, $to) { |
|
| 124 | - if (!$this->reparentQuery) { |
|
| 125 | - $builder = $this->connection->getQueryBuilder(); |
|
| 126 | - |
|
| 127 | - $this->reparentQuery = $builder->update('filecache') |
|
| 128 | - ->set('parent', $builder->createParameter('to')) |
|
| 129 | - ->where($builder->expr()->eq('fileid', $builder->createParameter('from'))); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $this->reparentQuery->setParameter('from', $from); |
|
| 133 | - $this->reparentQuery->setParameter('to', $to); |
|
| 134 | - |
|
| 135 | - $this->reparentQuery->execute(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - private function delete($fileid) { |
|
| 139 | - if (!$this->deleteQuery) { |
|
| 140 | - $builder = $this->connection->getQueryBuilder(); |
|
| 141 | - |
|
| 142 | - $this->deleteQuery = $builder->delete('filecache') |
|
| 143 | - ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid'))); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - $this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT); |
|
| 147 | - |
|
| 148 | - $this->deleteQuery->execute(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - private function repair() { |
|
| 152 | - $this->connection->beginTransaction(); |
|
| 153 | - $entries = $this->getInvalidEntries(); |
|
| 154 | - $count = 0; |
|
| 155 | - foreach ($entries as $entry) { |
|
| 156 | - $count++; |
|
| 157 | - $calculatedPath = $entry['parent_path'] . '/' . $entry['name']; |
|
| 158 | - if ($newId = $this->getId($entry['storage'], $calculatedPath)) { |
|
| 159 | - // a new entry with the correct path has already been created, reuse that one and delete the incorrect entry |
|
| 160 | - $this->reparent($entry['fileid'], $newId); |
|
| 161 | - $this->delete($entry['fileid']); |
|
| 162 | - } else { |
|
| 163 | - $this->update($entry['fileid'], $calculatedPath); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - $this->connection->commit(); |
|
| 167 | - return $count; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - public function run(IOutput $output) { |
|
| 171 | - $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
| 172 | - // was added to 12.0.0.30 and 13.0.0.1 |
|
| 173 | - if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) { |
|
| 174 | - $count = $this->repair(); |
|
| 175 | - |
|
| 176 | - $output->info('Repaired ' . $count . ' paths'); |
|
| 177 | - } |
|
| 178 | - } |
|
| 32 | + const MAX_ROWS = 1000; |
|
| 33 | + |
|
| 34 | + /** @var IDBConnection */ |
|
| 35 | + private $connection; |
|
| 36 | + /** @var IConfig */ |
|
| 37 | + private $config; |
|
| 38 | + |
|
| 39 | + private $getIdQuery; |
|
| 40 | + private $updateQuery; |
|
| 41 | + private $reparentQuery; |
|
| 42 | + private $deleteQuery; |
|
| 43 | + |
|
| 44 | + public function __construct(IDBConnection $connection, IConfig $config) { |
|
| 45 | + $this->connection = $connection; |
|
| 46 | + $this->config = $config; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + |
|
| 50 | + public function getName() { |
|
| 51 | + return 'Repair invalid paths in file cache'; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return \Generator |
|
| 56 | + * @suppress SqlInjectionChecker |
|
| 57 | + */ |
|
| 58 | + private function getInvalidEntries() { |
|
| 59 | + $builder = $this->connection->getQueryBuilder(); |
|
| 60 | + |
|
| 61 | + $computedPath = $builder->func()->concat( |
|
| 62 | + 'p.path', |
|
| 63 | + $builder->func()->concat($builder->createNamedParameter('/'), 'f.name') |
|
| 64 | + ); |
|
| 65 | + |
|
| 66 | + //select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name; |
|
| 67 | + $query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage') |
|
| 68 | + ->from('filecache', 'f') |
|
| 69 | + ->innerJoin('f', 'filecache', 'p', $builder->expr()->andX( |
|
| 70 | + $builder->expr()->eq('f.parent', 'p.fileid'), |
|
| 71 | + $builder->expr()->neq('p.name', $builder->createNamedParameter('')) |
|
| 72 | + )) |
|
| 73 | + ->where($builder->expr()->neq('f.path', $computedPath)) |
|
| 74 | + ->setMaxResults(self::MAX_ROWS); |
|
| 75 | + |
|
| 76 | + do { |
|
| 77 | + $result = $query->execute(); |
|
| 78 | + $rows = $result->fetchAll(); |
|
| 79 | + foreach ($rows as $row) { |
|
| 80 | + yield $row; |
|
| 81 | + } |
|
| 82 | + $result->closeCursor(); |
|
| 83 | + } while (count($rows) > 0); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + private function getId($storage, $path) { |
|
| 87 | + if (!$this->getIdQuery) { |
|
| 88 | + $builder = $this->connection->getQueryBuilder(); |
|
| 89 | + |
|
| 90 | + $this->getIdQuery = $builder->select('fileid') |
|
| 91 | + ->from('filecache') |
|
| 92 | + ->where($builder->expr()->eq('storage', $builder->createParameter('storage'))) |
|
| 93 | + ->andWhere($builder->expr()->eq('path', $builder->createParameter('path'))); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + $this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT); |
|
| 97 | + $this->getIdQuery->setParameter('path', $path); |
|
| 98 | + |
|
| 99 | + return $this->getIdQuery->execute()->fetchColumn(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $fileid |
|
| 104 | + * @param string $newPath |
|
| 105 | + * @suppress SqlInjectionChecker |
|
| 106 | + */ |
|
| 107 | + private function update($fileid, $newPath) { |
|
| 108 | + if (!$this->updateQuery) { |
|
| 109 | + $builder = $this->connection->getQueryBuilder(); |
|
| 110 | + |
|
| 111 | + $this->updateQuery = $builder->update('filecache') |
|
| 112 | + ->set('path', $builder->createParameter('newpath')) |
|
| 113 | + ->set('path_hash', $builder->func()->md5($builder->createParameter('newpath'))) |
|
| 114 | + ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid'))); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->updateQuery->setParameter('newpath', $newPath); |
|
| 118 | + $this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT); |
|
| 119 | + |
|
| 120 | + $this->updateQuery->execute(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + private function reparent($from, $to) { |
|
| 124 | + if (!$this->reparentQuery) { |
|
| 125 | + $builder = $this->connection->getQueryBuilder(); |
|
| 126 | + |
|
| 127 | + $this->reparentQuery = $builder->update('filecache') |
|
| 128 | + ->set('parent', $builder->createParameter('to')) |
|
| 129 | + ->where($builder->expr()->eq('fileid', $builder->createParameter('from'))); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $this->reparentQuery->setParameter('from', $from); |
|
| 133 | + $this->reparentQuery->setParameter('to', $to); |
|
| 134 | + |
|
| 135 | + $this->reparentQuery->execute(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + private function delete($fileid) { |
|
| 139 | + if (!$this->deleteQuery) { |
|
| 140 | + $builder = $this->connection->getQueryBuilder(); |
|
| 141 | + |
|
| 142 | + $this->deleteQuery = $builder->delete('filecache') |
|
| 143 | + ->where($builder->expr()->eq('fileid', $builder->createParameter('fileid'))); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + $this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT); |
|
| 147 | + |
|
| 148 | + $this->deleteQuery->execute(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + private function repair() { |
|
| 152 | + $this->connection->beginTransaction(); |
|
| 153 | + $entries = $this->getInvalidEntries(); |
|
| 154 | + $count = 0; |
|
| 155 | + foreach ($entries as $entry) { |
|
| 156 | + $count++; |
|
| 157 | + $calculatedPath = $entry['parent_path'] . '/' . $entry['name']; |
|
| 158 | + if ($newId = $this->getId($entry['storage'], $calculatedPath)) { |
|
| 159 | + // a new entry with the correct path has already been created, reuse that one and delete the incorrect entry |
|
| 160 | + $this->reparent($entry['fileid'], $newId); |
|
| 161 | + $this->delete($entry['fileid']); |
|
| 162 | + } else { |
|
| 163 | + $this->update($entry['fileid'], $calculatedPath); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + $this->connection->commit(); |
|
| 167 | + return $count; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + public function run(IOutput $output) { |
|
| 171 | + $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
| 172 | + // was added to 12.0.0.30 and 13.0.0.1 |
|
| 173 | + if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) { |
|
| 174 | + $count = $this->repair(); |
|
| 175 | + |
|
| 176 | + $output->info('Repaired ' . $count . ' paths'); |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | 179 | } |
@@ -45,373 +45,373 @@ |
||
| 45 | 45 | use Symfony\Component\Console\Question\Question; |
| 46 | 46 | |
| 47 | 47 | class ConvertType extends Command implements CompletionAwareInterface { |
| 48 | - /** |
|
| 49 | - * @var \OCP\IConfig |
|
| 50 | - */ |
|
| 51 | - protected $config; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var \OC\DB\ConnectionFactory |
|
| 55 | - */ |
|
| 56 | - protected $connectionFactory; |
|
| 57 | - |
|
| 58 | - /** @var array */ |
|
| 59 | - protected $columnTypes; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param \OCP\IConfig $config |
|
| 63 | - * @param \OC\DB\ConnectionFactory $connectionFactory |
|
| 64 | - */ |
|
| 65 | - public function __construct(IConfig $config, ConnectionFactory $connectionFactory) { |
|
| 66 | - $this->config = $config; |
|
| 67 | - $this->connectionFactory = $connectionFactory; |
|
| 68 | - parent::__construct(); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - protected function configure() { |
|
| 72 | - $this |
|
| 73 | - ->setName('db:convert-type') |
|
| 74 | - ->setDescription('Convert the Nextcloud database to the newly configured one') |
|
| 75 | - ->addArgument( |
|
| 76 | - 'type', |
|
| 77 | - InputArgument::REQUIRED, |
|
| 78 | - 'the type of the database to convert to' |
|
| 79 | - ) |
|
| 80 | - ->addArgument( |
|
| 81 | - 'username', |
|
| 82 | - InputArgument::REQUIRED, |
|
| 83 | - 'the username of the database to convert to' |
|
| 84 | - ) |
|
| 85 | - ->addArgument( |
|
| 86 | - 'hostname', |
|
| 87 | - InputArgument::REQUIRED, |
|
| 88 | - 'the hostname of the database to convert to' |
|
| 89 | - ) |
|
| 90 | - ->addArgument( |
|
| 91 | - 'database', |
|
| 92 | - InputArgument::REQUIRED, |
|
| 93 | - 'the name of the database to convert to' |
|
| 94 | - ) |
|
| 95 | - ->addOption( |
|
| 96 | - 'port', |
|
| 97 | - null, |
|
| 98 | - InputOption::VALUE_REQUIRED, |
|
| 99 | - 'the port of the database to convert to' |
|
| 100 | - ) |
|
| 101 | - ->addOption( |
|
| 102 | - 'password', |
|
| 103 | - null, |
|
| 104 | - InputOption::VALUE_REQUIRED, |
|
| 105 | - 'the password of the database to convert to. Will be asked when not specified. Can also be passed via stdin.' |
|
| 106 | - ) |
|
| 107 | - ->addOption( |
|
| 108 | - 'clear-schema', |
|
| 109 | - null, |
|
| 110 | - InputOption::VALUE_NONE, |
|
| 111 | - 'remove all tables from the destination database' |
|
| 112 | - ) |
|
| 113 | - ->addOption( |
|
| 114 | - 'all-apps', |
|
| 115 | - null, |
|
| 116 | - InputOption::VALUE_NONE, |
|
| 117 | - 'whether to create schema for all apps instead of only installed apps' |
|
| 118 | - ) |
|
| 119 | - ->addOption( |
|
| 120 | - 'chunk-size', |
|
| 121 | - null, |
|
| 122 | - InputOption::VALUE_REQUIRED, |
|
| 123 | - 'the maximum number of database rows to handle in a single query, bigger tables will be handled in chunks of this size. Lower this if the process runs out of memory during conversion.', |
|
| 124 | - 1000 |
|
| 125 | - ) |
|
| 126 | - ; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - protected function validateInput(InputInterface $input, OutputInterface $output) { |
|
| 130 | - $type = $this->connectionFactory->normalizeType($input->getArgument('type')); |
|
| 131 | - if ($type === 'sqlite3') { |
|
| 132 | - throw new \InvalidArgumentException( |
|
| 133 | - 'Converting to SQLite (sqlite3) is currently not supported.' |
|
| 134 | - ); |
|
| 135 | - } |
|
| 136 | - if ($type === $this->config->getSystemValue('dbtype', '')) { |
|
| 137 | - throw new \InvalidArgumentException(sprintf( |
|
| 138 | - 'Can not convert from %1$s to %1$s.', |
|
| 139 | - $type |
|
| 140 | - )); |
|
| 141 | - } |
|
| 142 | - if ($type === 'oci' && $input->getOption('clear-schema')) { |
|
| 143 | - // Doctrine unconditionally tries (at least in version 2.3) |
|
| 144 | - // to drop sequence triggers when dropping a table, even though |
|
| 145 | - // such triggers may not exist. This results in errors like |
|
| 146 | - // "ORA-04080: trigger 'OC_STORAGES_AI_PK' does not exist". |
|
| 147 | - throw new \InvalidArgumentException( |
|
| 148 | - 'The --clear-schema option is not supported when converting to Oracle (oci).' |
|
| 149 | - ); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - protected function readPassword(InputInterface $input, OutputInterface $output) { |
|
| 154 | - // Explicitly specified password |
|
| 155 | - if ($input->getOption('password')) { |
|
| 156 | - return; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - // Read from stdin. stream_set_blocking is used to prevent blocking |
|
| 160 | - // when nothing is passed via stdin. |
|
| 161 | - stream_set_blocking(STDIN, 0); |
|
| 162 | - $password = file_get_contents('php://stdin'); |
|
| 163 | - stream_set_blocking(STDIN, 1); |
|
| 164 | - if (trim($password) !== '') { |
|
| 165 | - $input->setOption('password', $password); |
|
| 166 | - return; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - // Read password by interacting |
|
| 170 | - if ($input->isInteractive()) { |
|
| 171 | - /** @var QuestionHelper $helper */ |
|
| 172 | - $helper = $this->getHelper('question'); |
|
| 173 | - $question = new Question('What is the database password?'); |
|
| 174 | - $question->setHidden(true); |
|
| 175 | - $question->setHiddenFallback(false); |
|
| 176 | - $password = $helper->ask($input, $output, $question); |
|
| 177 | - $input->setOption('password', $password); |
|
| 178 | - return; |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 183 | - $this->validateInput($input, $output); |
|
| 184 | - $this->readPassword($input, $output); |
|
| 185 | - |
|
| 186 | - $fromDB = \OC::$server->getDatabaseConnection(); |
|
| 187 | - $toDB = $this->getToDBConnection($input, $output); |
|
| 188 | - |
|
| 189 | - if ($input->getOption('clear-schema')) { |
|
| 190 | - $this->clearSchema($toDB, $input, $output); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - $this->createSchema($toDB, $input, $output); |
|
| 194 | - |
|
| 195 | - $toTables = $this->getTables($toDB); |
|
| 196 | - $fromTables = $this->getTables($fromDB); |
|
| 197 | - |
|
| 198 | - // warn/fail if there are more tables in 'from' database |
|
| 199 | - $extraFromTables = array_diff($fromTables, $toTables); |
|
| 200 | - if (!empty($extraFromTables)) { |
|
| 201 | - $output->writeln('<comment>The following tables will not be converted:</comment>'); |
|
| 202 | - $output->writeln($extraFromTables); |
|
| 203 | - if (!$input->getOption('all-apps')) { |
|
| 204 | - $output->writeln('<comment>Please note that tables belonging to available but currently not installed apps</comment>'); |
|
| 205 | - $output->writeln('<comment>can be included by specifying the --all-apps option.</comment>'); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** @var QuestionHelper $helper */ |
|
| 209 | - $helper = $this->getHelper('question'); |
|
| 210 | - $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false); |
|
| 211 | - |
|
| 212 | - if (!$helper->ask($input, $output, $question)) { |
|
| 213 | - return; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - $intersectingTables = array_intersect($toTables, $fromTables); |
|
| 217 | - $this->convertDB($fromDB, $toDB, $intersectingTables, $input, $output); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - protected function createSchema(Connection $toDB, InputInterface $input, OutputInterface $output) { |
|
| 221 | - $output->writeln('<info>Creating schema in new database</info>'); |
|
| 222 | - $schemaManager = new \OC\DB\MDB2SchemaManager($toDB); |
|
| 223 | - $schemaManager->createDbFromStructure(\OC::$SERVERROOT.'/db_structure.xml'); |
|
| 224 | - $apps = $input->getOption('all-apps') ? \OC_App::getAllApps() : \OC_App::getEnabledApps(); |
|
| 225 | - foreach($apps as $app) { |
|
| 226 | - if (file_exists(\OC_App::getAppPath($app).'/appinfo/database.xml')) { |
|
| 227 | - $schemaManager->createDbFromStructure(\OC_App::getAppPath($app).'/appinfo/database.xml'); |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - protected function getToDBConnection(InputInterface $input, OutputInterface $output) { |
|
| 233 | - $type = $input->getArgument('type'); |
|
| 234 | - $connectionParams = array( |
|
| 235 | - 'host' => $input->getArgument('hostname'), |
|
| 236 | - 'user' => $input->getArgument('username'), |
|
| 237 | - 'password' => $input->getOption('password'), |
|
| 238 | - 'dbname' => $input->getArgument('database'), |
|
| 239 | - 'tablePrefix' => $this->config->getSystemValue('dbtableprefix', 'oc_'), |
|
| 240 | - ); |
|
| 241 | - if ($input->getOption('port')) { |
|
| 242 | - $connectionParams['port'] = $input->getOption('port'); |
|
| 243 | - } |
|
| 244 | - return $this->connectionFactory->getConnection($type, $connectionParams); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - protected function clearSchema(Connection $db, InputInterface $input, OutputInterface $output) { |
|
| 248 | - $toTables = $this->getTables($db); |
|
| 249 | - if (!empty($toTables)) { |
|
| 250 | - $output->writeln('<info>Clearing schema in new database</info>'); |
|
| 251 | - } |
|
| 252 | - foreach($toTables as $table) { |
|
| 253 | - $db->getSchemaManager()->dropTable($table); |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - protected function getTables(Connection $db) { |
|
| 258 | - $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 259 | - $db->getConfiguration()-> |
|
| 260 | - setFilterSchemaAssetsExpression($filterExpression); |
|
| 261 | - return $db->getSchemaManager()->listTableNames(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param Connection $fromDB |
|
| 266 | - * @param Connection $toDB |
|
| 267 | - * @param $table |
|
| 268 | - * @param InputInterface $input |
|
| 269 | - * @param OutputInterface $output |
|
| 270 | - * @suppress SqlInjectionChecker |
|
| 271 | - */ |
|
| 272 | - protected function copyTable(Connection $fromDB, Connection $toDB, $table, InputInterface $input, OutputInterface $output) { |
|
| 273 | - $chunkSize = $input->getOption('chunk-size'); |
|
| 274 | - |
|
| 275 | - $query = $fromDB->getQueryBuilder(); |
|
| 276 | - $query->automaticTablePrefix(false); |
|
| 277 | - $query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries') |
|
| 278 | - ->from($table); |
|
| 279 | - $result = $query->execute(); |
|
| 280 | - $count = $result->fetchColumn(); |
|
| 281 | - $result->closeCursor(); |
|
| 282 | - |
|
| 283 | - $numChunks = ceil($count/$chunkSize); |
|
| 284 | - if ($numChunks > 1) { |
|
| 285 | - $output->writeln('chunked query, ' . $numChunks . ' chunks'); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $progress = new ProgressBar($output, $count); |
|
| 289 | - $progress->start(); |
|
| 290 | - $redraw = $count > $chunkSize ? 100 : ($count > 100 ? 5 : 1); |
|
| 291 | - $progress->setRedrawFrequency($redraw); |
|
| 292 | - |
|
| 293 | - $query = $fromDB->getQueryBuilder(); |
|
| 294 | - $query->automaticTablePrefix(false); |
|
| 295 | - $query->select('*') |
|
| 296 | - ->from($table) |
|
| 297 | - ->setMaxResults($chunkSize); |
|
| 298 | - |
|
| 299 | - $insertQuery = $toDB->getQueryBuilder(); |
|
| 300 | - $insertQuery->automaticTablePrefix(false); |
|
| 301 | - $insertQuery->insert($table); |
|
| 302 | - $parametersCreated = false; |
|
| 303 | - |
|
| 304 | - for ($chunk = 0; $chunk < $numChunks; $chunk++) { |
|
| 305 | - $query->setFirstResult($chunk * $chunkSize); |
|
| 306 | - |
|
| 307 | - $result = $query->execute(); |
|
| 308 | - |
|
| 309 | - while ($row = $result->fetch()) { |
|
| 310 | - $progress->advance(); |
|
| 311 | - if (!$parametersCreated) { |
|
| 312 | - foreach ($row as $key => $value) { |
|
| 313 | - $insertQuery->setValue($key, $insertQuery->createParameter($key)); |
|
| 314 | - } |
|
| 315 | - $parametersCreated = true; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - foreach ($row as $key => $value) { |
|
| 319 | - $type = $this->getColumnType($table, $key); |
|
| 320 | - if ($type !== false) { |
|
| 321 | - $insertQuery->setParameter($key, $value, $type); |
|
| 322 | - } else { |
|
| 323 | - $insertQuery->setParameter($key, $value); |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - $insertQuery->execute(); |
|
| 327 | - } |
|
| 328 | - $result->closeCursor(); |
|
| 329 | - } |
|
| 330 | - $progress->finish(); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - protected function getColumnType($table, $column) { |
|
| 334 | - if (isset($this->columnTypes[$table][$column])) { |
|
| 335 | - return $this->columnTypes[$table][$column]; |
|
| 336 | - } |
|
| 337 | - $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); |
|
| 338 | - |
|
| 339 | - $this->columnTypes[$table][$column] = false; |
|
| 340 | - |
|
| 341 | - if ($table === $prefix . 'cards' && $column === 'carddata') { |
|
| 342 | - $this->columnTypes[$table][$column] = IQueryBuilder::PARAM_LOB; |
|
| 343 | - } else if ($column === 'calendardata') { |
|
| 344 | - if ($table === $prefix . 'calendarobjects' || |
|
| 345 | - $table === $prefix . 'schedulingobjects') { |
|
| 346 | - $this->columnTypes[$table][$column] = IQueryBuilder::PARAM_LOB; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - return $this->columnTypes[$table][$column]; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - protected function convertDB(Connection $fromDB, Connection $toDB, array $tables, InputInterface $input, OutputInterface $output) { |
|
| 354 | - $this->config->setSystemValue('maintenance', true); |
|
| 355 | - try { |
|
| 356 | - // copy table rows |
|
| 357 | - foreach($tables as $table) { |
|
| 358 | - $output->writeln($table); |
|
| 359 | - $this->copyTable($fromDB, $toDB, $table, $input, $output); |
|
| 360 | - } |
|
| 361 | - if ($input->getArgument('type') === 'pgsql') { |
|
| 362 | - $tools = new \OC\DB\PgSqlTools($this->config); |
|
| 363 | - $tools->resynchronizeDatabaseSequences($toDB); |
|
| 364 | - } |
|
| 365 | - // save new database config |
|
| 366 | - $this->saveDBInfo($input); |
|
| 367 | - } catch(\Exception $e) { |
|
| 368 | - $this->config->setSystemValue('maintenance', false); |
|
| 369 | - throw $e; |
|
| 370 | - } |
|
| 371 | - $this->config->setSystemValue('maintenance', false); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - protected function saveDBInfo(InputInterface $input) { |
|
| 375 | - $type = $input->getArgument('type'); |
|
| 376 | - $username = $input->getArgument('username'); |
|
| 377 | - $dbHost = $input->getArgument('hostname'); |
|
| 378 | - $dbName = $input->getArgument('database'); |
|
| 379 | - $password = $input->getOption('password'); |
|
| 380 | - if ($input->getOption('port')) { |
|
| 381 | - $dbHost .= ':'.$input->getOption('port'); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - $this->config->setSystemValues([ |
|
| 385 | - 'dbtype' => $type, |
|
| 386 | - 'dbname' => $dbName, |
|
| 387 | - 'dbhost' => $dbHost, |
|
| 388 | - 'dbuser' => $username, |
|
| 389 | - 'dbpassword' => $password, |
|
| 390 | - ]); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Return possible values for the named option |
|
| 395 | - * |
|
| 396 | - * @param string $optionName |
|
| 397 | - * @param CompletionContext $context |
|
| 398 | - * @return string[] |
|
| 399 | - */ |
|
| 400 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 401 | - return []; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Return possible values for the named argument |
|
| 406 | - * |
|
| 407 | - * @param string $argumentName |
|
| 408 | - * @param CompletionContext $context |
|
| 409 | - * @return string[] |
|
| 410 | - */ |
|
| 411 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 412 | - if ($argumentName === 'type') { |
|
| 413 | - return ['mysql', 'oci', 'pgsql']; |
|
| 414 | - } |
|
| 415 | - return []; |
|
| 416 | - } |
|
| 48 | + /** |
|
| 49 | + * @var \OCP\IConfig |
|
| 50 | + */ |
|
| 51 | + protected $config; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var \OC\DB\ConnectionFactory |
|
| 55 | + */ |
|
| 56 | + protected $connectionFactory; |
|
| 57 | + |
|
| 58 | + /** @var array */ |
|
| 59 | + protected $columnTypes; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param \OCP\IConfig $config |
|
| 63 | + * @param \OC\DB\ConnectionFactory $connectionFactory |
|
| 64 | + */ |
|
| 65 | + public function __construct(IConfig $config, ConnectionFactory $connectionFactory) { |
|
| 66 | + $this->config = $config; |
|
| 67 | + $this->connectionFactory = $connectionFactory; |
|
| 68 | + parent::__construct(); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + protected function configure() { |
|
| 72 | + $this |
|
| 73 | + ->setName('db:convert-type') |
|
| 74 | + ->setDescription('Convert the Nextcloud database to the newly configured one') |
|
| 75 | + ->addArgument( |
|
| 76 | + 'type', |
|
| 77 | + InputArgument::REQUIRED, |
|
| 78 | + 'the type of the database to convert to' |
|
| 79 | + ) |
|
| 80 | + ->addArgument( |
|
| 81 | + 'username', |
|
| 82 | + InputArgument::REQUIRED, |
|
| 83 | + 'the username of the database to convert to' |
|
| 84 | + ) |
|
| 85 | + ->addArgument( |
|
| 86 | + 'hostname', |
|
| 87 | + InputArgument::REQUIRED, |
|
| 88 | + 'the hostname of the database to convert to' |
|
| 89 | + ) |
|
| 90 | + ->addArgument( |
|
| 91 | + 'database', |
|
| 92 | + InputArgument::REQUIRED, |
|
| 93 | + 'the name of the database to convert to' |
|
| 94 | + ) |
|
| 95 | + ->addOption( |
|
| 96 | + 'port', |
|
| 97 | + null, |
|
| 98 | + InputOption::VALUE_REQUIRED, |
|
| 99 | + 'the port of the database to convert to' |
|
| 100 | + ) |
|
| 101 | + ->addOption( |
|
| 102 | + 'password', |
|
| 103 | + null, |
|
| 104 | + InputOption::VALUE_REQUIRED, |
|
| 105 | + 'the password of the database to convert to. Will be asked when not specified. Can also be passed via stdin.' |
|
| 106 | + ) |
|
| 107 | + ->addOption( |
|
| 108 | + 'clear-schema', |
|
| 109 | + null, |
|
| 110 | + InputOption::VALUE_NONE, |
|
| 111 | + 'remove all tables from the destination database' |
|
| 112 | + ) |
|
| 113 | + ->addOption( |
|
| 114 | + 'all-apps', |
|
| 115 | + null, |
|
| 116 | + InputOption::VALUE_NONE, |
|
| 117 | + 'whether to create schema for all apps instead of only installed apps' |
|
| 118 | + ) |
|
| 119 | + ->addOption( |
|
| 120 | + 'chunk-size', |
|
| 121 | + null, |
|
| 122 | + InputOption::VALUE_REQUIRED, |
|
| 123 | + 'the maximum number of database rows to handle in a single query, bigger tables will be handled in chunks of this size. Lower this if the process runs out of memory during conversion.', |
|
| 124 | + 1000 |
|
| 125 | + ) |
|
| 126 | + ; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + protected function validateInput(InputInterface $input, OutputInterface $output) { |
|
| 130 | + $type = $this->connectionFactory->normalizeType($input->getArgument('type')); |
|
| 131 | + if ($type === 'sqlite3') { |
|
| 132 | + throw new \InvalidArgumentException( |
|
| 133 | + 'Converting to SQLite (sqlite3) is currently not supported.' |
|
| 134 | + ); |
|
| 135 | + } |
|
| 136 | + if ($type === $this->config->getSystemValue('dbtype', '')) { |
|
| 137 | + throw new \InvalidArgumentException(sprintf( |
|
| 138 | + 'Can not convert from %1$s to %1$s.', |
|
| 139 | + $type |
|
| 140 | + )); |
|
| 141 | + } |
|
| 142 | + if ($type === 'oci' && $input->getOption('clear-schema')) { |
|
| 143 | + // Doctrine unconditionally tries (at least in version 2.3) |
|
| 144 | + // to drop sequence triggers when dropping a table, even though |
|
| 145 | + // such triggers may not exist. This results in errors like |
|
| 146 | + // "ORA-04080: trigger 'OC_STORAGES_AI_PK' does not exist". |
|
| 147 | + throw new \InvalidArgumentException( |
|
| 148 | + 'The --clear-schema option is not supported when converting to Oracle (oci).' |
|
| 149 | + ); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + protected function readPassword(InputInterface $input, OutputInterface $output) { |
|
| 154 | + // Explicitly specified password |
|
| 155 | + if ($input->getOption('password')) { |
|
| 156 | + return; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + // Read from stdin. stream_set_blocking is used to prevent blocking |
|
| 160 | + // when nothing is passed via stdin. |
|
| 161 | + stream_set_blocking(STDIN, 0); |
|
| 162 | + $password = file_get_contents('php://stdin'); |
|
| 163 | + stream_set_blocking(STDIN, 1); |
|
| 164 | + if (trim($password) !== '') { |
|
| 165 | + $input->setOption('password', $password); |
|
| 166 | + return; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + // Read password by interacting |
|
| 170 | + if ($input->isInteractive()) { |
|
| 171 | + /** @var QuestionHelper $helper */ |
|
| 172 | + $helper = $this->getHelper('question'); |
|
| 173 | + $question = new Question('What is the database password?'); |
|
| 174 | + $question->setHidden(true); |
|
| 175 | + $question->setHiddenFallback(false); |
|
| 176 | + $password = $helper->ask($input, $output, $question); |
|
| 177 | + $input->setOption('password', $password); |
|
| 178 | + return; |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 183 | + $this->validateInput($input, $output); |
|
| 184 | + $this->readPassword($input, $output); |
|
| 185 | + |
|
| 186 | + $fromDB = \OC::$server->getDatabaseConnection(); |
|
| 187 | + $toDB = $this->getToDBConnection($input, $output); |
|
| 188 | + |
|
| 189 | + if ($input->getOption('clear-schema')) { |
|
| 190 | + $this->clearSchema($toDB, $input, $output); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + $this->createSchema($toDB, $input, $output); |
|
| 194 | + |
|
| 195 | + $toTables = $this->getTables($toDB); |
|
| 196 | + $fromTables = $this->getTables($fromDB); |
|
| 197 | + |
|
| 198 | + // warn/fail if there are more tables in 'from' database |
|
| 199 | + $extraFromTables = array_diff($fromTables, $toTables); |
|
| 200 | + if (!empty($extraFromTables)) { |
|
| 201 | + $output->writeln('<comment>The following tables will not be converted:</comment>'); |
|
| 202 | + $output->writeln($extraFromTables); |
|
| 203 | + if (!$input->getOption('all-apps')) { |
|
| 204 | + $output->writeln('<comment>Please note that tables belonging to available but currently not installed apps</comment>'); |
|
| 205 | + $output->writeln('<comment>can be included by specifying the --all-apps option.</comment>'); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** @var QuestionHelper $helper */ |
|
| 209 | + $helper = $this->getHelper('question'); |
|
| 210 | + $question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false); |
|
| 211 | + |
|
| 212 | + if (!$helper->ask($input, $output, $question)) { |
|
| 213 | + return; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + $intersectingTables = array_intersect($toTables, $fromTables); |
|
| 217 | + $this->convertDB($fromDB, $toDB, $intersectingTables, $input, $output); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + protected function createSchema(Connection $toDB, InputInterface $input, OutputInterface $output) { |
|
| 221 | + $output->writeln('<info>Creating schema in new database</info>'); |
|
| 222 | + $schemaManager = new \OC\DB\MDB2SchemaManager($toDB); |
|
| 223 | + $schemaManager->createDbFromStructure(\OC::$SERVERROOT.'/db_structure.xml'); |
|
| 224 | + $apps = $input->getOption('all-apps') ? \OC_App::getAllApps() : \OC_App::getEnabledApps(); |
|
| 225 | + foreach($apps as $app) { |
|
| 226 | + if (file_exists(\OC_App::getAppPath($app).'/appinfo/database.xml')) { |
|
| 227 | + $schemaManager->createDbFromStructure(\OC_App::getAppPath($app).'/appinfo/database.xml'); |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + protected function getToDBConnection(InputInterface $input, OutputInterface $output) { |
|
| 233 | + $type = $input->getArgument('type'); |
|
| 234 | + $connectionParams = array( |
|
| 235 | + 'host' => $input->getArgument('hostname'), |
|
| 236 | + 'user' => $input->getArgument('username'), |
|
| 237 | + 'password' => $input->getOption('password'), |
|
| 238 | + 'dbname' => $input->getArgument('database'), |
|
| 239 | + 'tablePrefix' => $this->config->getSystemValue('dbtableprefix', 'oc_'), |
|
| 240 | + ); |
|
| 241 | + if ($input->getOption('port')) { |
|
| 242 | + $connectionParams['port'] = $input->getOption('port'); |
|
| 243 | + } |
|
| 244 | + return $this->connectionFactory->getConnection($type, $connectionParams); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + protected function clearSchema(Connection $db, InputInterface $input, OutputInterface $output) { |
|
| 248 | + $toTables = $this->getTables($db); |
|
| 249 | + if (!empty($toTables)) { |
|
| 250 | + $output->writeln('<info>Clearing schema in new database</info>'); |
|
| 251 | + } |
|
| 252 | + foreach($toTables as $table) { |
|
| 253 | + $db->getSchemaManager()->dropTable($table); |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + protected function getTables(Connection $db) { |
|
| 258 | + $filterExpression = '/^' . preg_quote($this->config->getSystemValue('dbtableprefix', 'oc_')) . '/'; |
|
| 259 | + $db->getConfiguration()-> |
|
| 260 | + setFilterSchemaAssetsExpression($filterExpression); |
|
| 261 | + return $db->getSchemaManager()->listTableNames(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param Connection $fromDB |
|
| 266 | + * @param Connection $toDB |
|
| 267 | + * @param $table |
|
| 268 | + * @param InputInterface $input |
|
| 269 | + * @param OutputInterface $output |
|
| 270 | + * @suppress SqlInjectionChecker |
|
| 271 | + */ |
|
| 272 | + protected function copyTable(Connection $fromDB, Connection $toDB, $table, InputInterface $input, OutputInterface $output) { |
|
| 273 | + $chunkSize = $input->getOption('chunk-size'); |
|
| 274 | + |
|
| 275 | + $query = $fromDB->getQueryBuilder(); |
|
| 276 | + $query->automaticTablePrefix(false); |
|
| 277 | + $query->selectAlias($query->createFunction('COUNT(*)'), 'num_entries') |
|
| 278 | + ->from($table); |
|
| 279 | + $result = $query->execute(); |
|
| 280 | + $count = $result->fetchColumn(); |
|
| 281 | + $result->closeCursor(); |
|
| 282 | + |
|
| 283 | + $numChunks = ceil($count/$chunkSize); |
|
| 284 | + if ($numChunks > 1) { |
|
| 285 | + $output->writeln('chunked query, ' . $numChunks . ' chunks'); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $progress = new ProgressBar($output, $count); |
|
| 289 | + $progress->start(); |
|
| 290 | + $redraw = $count > $chunkSize ? 100 : ($count > 100 ? 5 : 1); |
|
| 291 | + $progress->setRedrawFrequency($redraw); |
|
| 292 | + |
|
| 293 | + $query = $fromDB->getQueryBuilder(); |
|
| 294 | + $query->automaticTablePrefix(false); |
|
| 295 | + $query->select('*') |
|
| 296 | + ->from($table) |
|
| 297 | + ->setMaxResults($chunkSize); |
|
| 298 | + |
|
| 299 | + $insertQuery = $toDB->getQueryBuilder(); |
|
| 300 | + $insertQuery->automaticTablePrefix(false); |
|
| 301 | + $insertQuery->insert($table); |
|
| 302 | + $parametersCreated = false; |
|
| 303 | + |
|
| 304 | + for ($chunk = 0; $chunk < $numChunks; $chunk++) { |
|
| 305 | + $query->setFirstResult($chunk * $chunkSize); |
|
| 306 | + |
|
| 307 | + $result = $query->execute(); |
|
| 308 | + |
|
| 309 | + while ($row = $result->fetch()) { |
|
| 310 | + $progress->advance(); |
|
| 311 | + if (!$parametersCreated) { |
|
| 312 | + foreach ($row as $key => $value) { |
|
| 313 | + $insertQuery->setValue($key, $insertQuery->createParameter($key)); |
|
| 314 | + } |
|
| 315 | + $parametersCreated = true; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + foreach ($row as $key => $value) { |
|
| 319 | + $type = $this->getColumnType($table, $key); |
|
| 320 | + if ($type !== false) { |
|
| 321 | + $insertQuery->setParameter($key, $value, $type); |
|
| 322 | + } else { |
|
| 323 | + $insertQuery->setParameter($key, $value); |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + $insertQuery->execute(); |
|
| 327 | + } |
|
| 328 | + $result->closeCursor(); |
|
| 329 | + } |
|
| 330 | + $progress->finish(); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + protected function getColumnType($table, $column) { |
|
| 334 | + if (isset($this->columnTypes[$table][$column])) { |
|
| 335 | + return $this->columnTypes[$table][$column]; |
|
| 336 | + } |
|
| 337 | + $prefix = $this->config->getSystemValue('dbtableprefix', 'oc_'); |
|
| 338 | + |
|
| 339 | + $this->columnTypes[$table][$column] = false; |
|
| 340 | + |
|
| 341 | + if ($table === $prefix . 'cards' && $column === 'carddata') { |
|
| 342 | + $this->columnTypes[$table][$column] = IQueryBuilder::PARAM_LOB; |
|
| 343 | + } else if ($column === 'calendardata') { |
|
| 344 | + if ($table === $prefix . 'calendarobjects' || |
|
| 345 | + $table === $prefix . 'schedulingobjects') { |
|
| 346 | + $this->columnTypes[$table][$column] = IQueryBuilder::PARAM_LOB; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + return $this->columnTypes[$table][$column]; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + protected function convertDB(Connection $fromDB, Connection $toDB, array $tables, InputInterface $input, OutputInterface $output) { |
|
| 354 | + $this->config->setSystemValue('maintenance', true); |
|
| 355 | + try { |
|
| 356 | + // copy table rows |
|
| 357 | + foreach($tables as $table) { |
|
| 358 | + $output->writeln($table); |
|
| 359 | + $this->copyTable($fromDB, $toDB, $table, $input, $output); |
|
| 360 | + } |
|
| 361 | + if ($input->getArgument('type') === 'pgsql') { |
|
| 362 | + $tools = new \OC\DB\PgSqlTools($this->config); |
|
| 363 | + $tools->resynchronizeDatabaseSequences($toDB); |
|
| 364 | + } |
|
| 365 | + // save new database config |
|
| 366 | + $this->saveDBInfo($input); |
|
| 367 | + } catch(\Exception $e) { |
|
| 368 | + $this->config->setSystemValue('maintenance', false); |
|
| 369 | + throw $e; |
|
| 370 | + } |
|
| 371 | + $this->config->setSystemValue('maintenance', false); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + protected function saveDBInfo(InputInterface $input) { |
|
| 375 | + $type = $input->getArgument('type'); |
|
| 376 | + $username = $input->getArgument('username'); |
|
| 377 | + $dbHost = $input->getArgument('hostname'); |
|
| 378 | + $dbName = $input->getArgument('database'); |
|
| 379 | + $password = $input->getOption('password'); |
|
| 380 | + if ($input->getOption('port')) { |
|
| 381 | + $dbHost .= ':'.$input->getOption('port'); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + $this->config->setSystemValues([ |
|
| 385 | + 'dbtype' => $type, |
|
| 386 | + 'dbname' => $dbName, |
|
| 387 | + 'dbhost' => $dbHost, |
|
| 388 | + 'dbuser' => $username, |
|
| 389 | + 'dbpassword' => $password, |
|
| 390 | + ]); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Return possible values for the named option |
|
| 395 | + * |
|
| 396 | + * @param string $optionName |
|
| 397 | + * @param CompletionContext $context |
|
| 398 | + * @return string[] |
|
| 399 | + */ |
|
| 400 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 401 | + return []; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Return possible values for the named argument |
|
| 406 | + * |
|
| 407 | + * @param string $argumentName |
|
| 408 | + * @param CompletionContext $context |
|
| 409 | + * @return string[] |
|
| 410 | + */ |
|
| 411 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 412 | + if ($argumentName === 'type') { |
|
| 413 | + return ['mysql', 'oci', 'pgsql']; |
|
| 414 | + } |
|
| 415 | + return []; |
|
| 416 | + } |
|
| 417 | 417 | } |
@@ -32,460 +32,460 @@ |
||
| 32 | 32 | * Stores the mount config in the database |
| 33 | 33 | */ |
| 34 | 34 | class DBConfigService { |
| 35 | - const MOUNT_TYPE_ADMIN = 1; |
|
| 36 | - const MOUNT_TYPE_PERSONAl = 2; |
|
| 37 | - |
|
| 38 | - const APPLICABLE_TYPE_GLOBAL = 1; |
|
| 39 | - const APPLICABLE_TYPE_GROUP = 2; |
|
| 40 | - const APPLICABLE_TYPE_USER = 3; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var IDBConnection |
|
| 44 | - */ |
|
| 45 | - private $connection; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var ICrypto |
|
| 49 | - */ |
|
| 50 | - private $crypto; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * DBConfigService constructor. |
|
| 54 | - * |
|
| 55 | - * @param IDBConnection $connection |
|
| 56 | - * @param ICrypto $crypto |
|
| 57 | - */ |
|
| 58 | - public function __construct(IDBConnection $connection, ICrypto $crypto) { |
|
| 59 | - $this->connection = $connection; |
|
| 60 | - $this->crypto = $crypto; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param int $mountId |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public function getMountById($mountId) { |
|
| 68 | - $builder = $this->connection->getQueryBuilder(); |
|
| 69 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 70 | - ->from('external_mounts', 'm') |
|
| 71 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 72 | - $mounts = $this->getMountsFromQuery($query); |
|
| 73 | - if (count($mounts) > 0) { |
|
| 74 | - return $mounts[0]; |
|
| 75 | - } else { |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get all configured mounts |
|
| 82 | - * |
|
| 83 | - * @return array |
|
| 84 | - */ |
|
| 85 | - public function getAllMounts() { |
|
| 86 | - $builder = $this->connection->getQueryBuilder(); |
|
| 87 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 88 | - ->from('external_mounts'); |
|
| 89 | - return $this->getMountsFromQuery($query); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function getMountsForUser($userId, $groupIds) { |
|
| 93 | - $builder = $this->connection->getQueryBuilder(); |
|
| 94 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 95 | - ->from('external_mounts', 'm') |
|
| 96 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 97 | - ->where($builder->expr()->orX( |
|
| 98 | - $builder->expr()->andX( // global mounts |
|
| 99 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)), |
|
| 100 | - $builder->expr()->isNull('a.value') |
|
| 101 | - ), |
|
| 102 | - $builder->expr()->andX( // mounts for user |
|
| 103 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), |
|
| 104 | - $builder->expr()->eq('a.value', $builder->createNamedParameter($userId)) |
|
| 105 | - ), |
|
| 106 | - $builder->expr()->andX( // mounts for group |
|
| 107 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), |
|
| 108 | - $builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 109 | - ) |
|
| 110 | - )); |
|
| 111 | - |
|
| 112 | - return $this->getMountsFromQuery($query); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Get admin defined mounts |
|
| 117 | - * |
|
| 118 | - * @return array |
|
| 119 | - * @suppress SqlInjectionChecker |
|
| 120 | - */ |
|
| 121 | - public function getAdminMounts() { |
|
| 122 | - $builder = $this->connection->getQueryBuilder(); |
|
| 123 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 124 | - ->from('external_mounts') |
|
| 125 | - ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 126 | - return $this->getMountsFromQuery($query); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - protected function getForQuery(IQueryBuilder $builder, $type, $value) { |
|
| 130 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 131 | - ->from('external_mounts', 'm') |
|
| 132 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 133 | - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 134 | - |
|
| 135 | - if (is_null($value)) { |
|
| 136 | - $query = $query->andWhere($builder->expr()->isNull('a.value')); |
|
| 137 | - } else { |
|
| 138 | - $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return $query; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Get mounts by applicable |
|
| 146 | - * |
|
| 147 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 148 | - * @param string|null $value user_id, group_id or null for global mounts |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 151 | - public function getMountsFor($type, $value) { |
|
| 152 | - $builder = $this->connection->getQueryBuilder(); |
|
| 153 | - $query = $this->getForQuery($builder, $type, $value); |
|
| 154 | - |
|
| 155 | - return $this->getMountsFromQuery($query); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get admin defined mounts by applicable |
|
| 160 | - * |
|
| 161 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 162 | - * @param string|null $value user_id, group_id or null for global mounts |
|
| 163 | - * @return array |
|
| 164 | - * @suppress SqlInjectionChecker |
|
| 165 | - */ |
|
| 166 | - public function getAdminMountsFor($type, $value) { |
|
| 167 | - $builder = $this->connection->getQueryBuilder(); |
|
| 168 | - $query = $this->getForQuery($builder, $type, $value); |
|
| 169 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 170 | - |
|
| 171 | - return $this->getMountsFromQuery($query); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Get admin defined mounts for multiple applicable |
|
| 176 | - * |
|
| 177 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 178 | - * @param string[] $values user_ids or group_ids |
|
| 179 | - * @return array |
|
| 180 | - * @suppress SqlInjectionChecker |
|
| 181 | - */ |
|
| 182 | - public function getAdminMountsForMultiple($type, array $values) { |
|
| 183 | - $builder = $this->connection->getQueryBuilder(); |
|
| 184 | - $params = array_map(function ($value) use ($builder) { |
|
| 185 | - return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
|
| 186 | - }, $values); |
|
| 187 | - |
|
| 188 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 189 | - ->from('external_mounts', 'm') |
|
| 190 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 191 | - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
| 192 | - ->andWhere($builder->expr()->in('a.value', $params)); |
|
| 193 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 194 | - |
|
| 195 | - return $this->getMountsFromQuery($query); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get user defined mounts by applicable |
|
| 200 | - * |
|
| 201 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 202 | - * @param string|null $value user_id, group_id or null for global mounts |
|
| 203 | - * @return array |
|
| 204 | - * @suppress SqlInjectionChecker |
|
| 205 | - */ |
|
| 206 | - public function getUserMountsFor($type, $value) { |
|
| 207 | - $builder = $this->connection->getQueryBuilder(); |
|
| 208 | - $query = $this->getForQuery($builder, $type, $value); |
|
| 209 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); |
|
| 210 | - |
|
| 211 | - return $this->getMountsFromQuery($query); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Add a mount to the database |
|
| 216 | - * |
|
| 217 | - * @param string $mountPoint |
|
| 218 | - * @param string $storageBackend |
|
| 219 | - * @param string $authBackend |
|
| 220 | - * @param int $priority |
|
| 221 | - * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL |
|
| 222 | - * @return int the id of the new mount |
|
| 223 | - */ |
|
| 224 | - public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { |
|
| 225 | - if (!$priority) { |
|
| 226 | - $priority = 100; |
|
| 227 | - } |
|
| 228 | - $builder = $this->connection->getQueryBuilder(); |
|
| 229 | - $query = $builder->insert('external_mounts') |
|
| 230 | - ->values([ |
|
| 231 | - 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), |
|
| 232 | - 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), |
|
| 233 | - 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), |
|
| 234 | - 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
|
| 235 | - 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
|
| 236 | - ]); |
|
| 237 | - $query->execute(); |
|
| 238 | - return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Remove a mount from the database |
|
| 243 | - * |
|
| 244 | - * @param int $mountId |
|
| 245 | - */ |
|
| 246 | - public function removeMount($mountId) { |
|
| 247 | - $builder = $this->connection->getQueryBuilder(); |
|
| 248 | - $query = $builder->delete('external_mounts') |
|
| 249 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 250 | - $query->execute(); |
|
| 251 | - |
|
| 252 | - $query = $builder->delete('external_applicable') |
|
| 253 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 254 | - $query->execute(); |
|
| 255 | - |
|
| 256 | - $query = $builder->delete('external_config') |
|
| 257 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 258 | - $query->execute(); |
|
| 259 | - |
|
| 260 | - $query = $builder->delete('external_options') |
|
| 261 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 262 | - $query->execute(); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @param int $mountId |
|
| 267 | - * @param string $newMountPoint |
|
| 268 | - */ |
|
| 269 | - public function setMountPoint($mountId, $newMountPoint) { |
|
| 270 | - $builder = $this->connection->getQueryBuilder(); |
|
| 271 | - |
|
| 272 | - $query = $builder->update('external_mounts') |
|
| 273 | - ->set('mount_point', $builder->createNamedParameter($newMountPoint)) |
|
| 274 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 275 | - |
|
| 276 | - $query->execute(); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @param int $mountId |
|
| 281 | - * @param string $newAuthBackend |
|
| 282 | - */ |
|
| 283 | - public function setAuthBackend($mountId, $newAuthBackend) { |
|
| 284 | - $builder = $this->connection->getQueryBuilder(); |
|
| 285 | - |
|
| 286 | - $query = $builder->update('external_mounts') |
|
| 287 | - ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) |
|
| 288 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 289 | - |
|
| 290 | - $query->execute(); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param int $mountId |
|
| 295 | - * @param string $key |
|
| 296 | - * @param string $value |
|
| 297 | - */ |
|
| 298 | - public function setConfig($mountId, $key, $value) { |
|
| 299 | - if ($key === 'password') { |
|
| 300 | - $value = $this->encryptValue($value); |
|
| 301 | - } |
|
| 302 | - $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ |
|
| 303 | - 'mount_id' => $mountId, |
|
| 304 | - 'key' => $key, |
|
| 305 | - 'value' => $value |
|
| 306 | - ], ['mount_id', 'key']); |
|
| 307 | - if ($count === 0) { |
|
| 308 | - $builder = $this->connection->getQueryBuilder(); |
|
| 309 | - $query = $builder->update('external_config') |
|
| 310 | - ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) |
|
| 311 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 312 | - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 313 | - $query->execute(); |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * @param int $mountId |
|
| 319 | - * @param string $key |
|
| 320 | - * @param string $value |
|
| 321 | - */ |
|
| 322 | - public function setOption($mountId, $key, $value) { |
|
| 323 | - |
|
| 324 | - $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ |
|
| 325 | - 'mount_id' => $mountId, |
|
| 326 | - 'key' => $key, |
|
| 327 | - 'value' => json_encode($value) |
|
| 328 | - ], ['mount_id', 'key']); |
|
| 329 | - if ($count === 0) { |
|
| 330 | - $builder = $this->connection->getQueryBuilder(); |
|
| 331 | - $query = $builder->update('external_options') |
|
| 332 | - ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) |
|
| 333 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 334 | - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 335 | - $query->execute(); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - public function addApplicable($mountId, $type, $value) { |
|
| 340 | - $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ |
|
| 341 | - 'mount_id' => $mountId, |
|
| 342 | - 'type' => $type, |
|
| 343 | - 'value' => $value |
|
| 344 | - ], ['mount_id', 'type', 'value']); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - public function removeApplicable($mountId, $type, $value) { |
|
| 348 | - $builder = $this->connection->getQueryBuilder(); |
|
| 349 | - $query = $builder->delete('external_applicable') |
|
| 350 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 351 | - ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 352 | - |
|
| 353 | - if (is_null($value)) { |
|
| 354 | - $query = $query->andWhere($builder->expr()->isNull('value')); |
|
| 355 | - } else { |
|
| 356 | - $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $query->execute(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - private function getMountsFromQuery(IQueryBuilder $query) { |
|
| 363 | - $result = $query->execute(); |
|
| 364 | - $mounts = $result->fetchAll(); |
|
| 365 | - $uniqueMounts = []; |
|
| 366 | - foreach ($mounts as $mount) { |
|
| 367 | - $id = $mount['mount_id']; |
|
| 368 | - if (!isset($uniqueMounts[$id])) { |
|
| 369 | - $uniqueMounts[$id] = $mount; |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - $uniqueMounts = array_values($uniqueMounts); |
|
| 373 | - |
|
| 374 | - $mountIds = array_map(function ($mount) { |
|
| 375 | - return $mount['mount_id']; |
|
| 376 | - }, $uniqueMounts); |
|
| 377 | - $mountIds = array_values(array_unique($mountIds)); |
|
| 378 | - |
|
| 379 | - $applicable = $this->getApplicableForMounts($mountIds); |
|
| 380 | - $config = $this->getConfigForMounts($mountIds); |
|
| 381 | - $options = $this->getOptionsForMounts($mountIds); |
|
| 382 | - |
|
| 383 | - return array_map(function ($mount, $applicable, $config, $options) { |
|
| 384 | - $mount['type'] = (int)$mount['type']; |
|
| 385 | - $mount['priority'] = (int)$mount['priority']; |
|
| 386 | - $mount['applicable'] = $applicable; |
|
| 387 | - $mount['config'] = $config; |
|
| 388 | - $mount['options'] = $options; |
|
| 389 | - return $mount; |
|
| 390 | - }, $uniqueMounts, $applicable, $config, $options); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Get mount options from a table grouped by mount id |
|
| 395 | - * |
|
| 396 | - * @param string $table |
|
| 397 | - * @param string[] $fields |
|
| 398 | - * @param int[] $mountIds |
|
| 399 | - * @return array [$mountId => [['field1' => $value1, ...], ...], ...] |
|
| 400 | - */ |
|
| 401 | - private function selectForMounts($table, array $fields, array $mountIds) { |
|
| 402 | - if (count($mountIds) === 0) { |
|
| 403 | - return []; |
|
| 404 | - } |
|
| 405 | - $builder = $this->connection->getQueryBuilder(); |
|
| 406 | - $fields[] = 'mount_id'; |
|
| 407 | - $placeHolders = array_map(function ($id) use ($builder) { |
|
| 408 | - return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
|
| 409 | - }, $mountIds); |
|
| 410 | - $query = $builder->select($fields) |
|
| 411 | - ->from($table) |
|
| 412 | - ->where($builder->expr()->in('mount_id', $placeHolders)); |
|
| 413 | - $rows = $query->execute()->fetchAll(); |
|
| 414 | - |
|
| 415 | - $result = []; |
|
| 416 | - foreach ($mountIds as $mountId) { |
|
| 417 | - $result[$mountId] = []; |
|
| 418 | - } |
|
| 419 | - foreach ($rows as $row) { |
|
| 420 | - if (isset($row['type'])) { |
|
| 421 | - $row['type'] = (int)$row['type']; |
|
| 422 | - } |
|
| 423 | - $result[$row['mount_id']][] = $row; |
|
| 424 | - } |
|
| 425 | - return $result; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * @param int[] $mountIds |
|
| 430 | - * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] |
|
| 431 | - */ |
|
| 432 | - public function getApplicableForMounts($mountIds) { |
|
| 433 | - return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * @param int[] $mountIds |
|
| 438 | - * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 439 | - */ |
|
| 440 | - public function getConfigForMounts($mountIds) { |
|
| 441 | - $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); |
|
| 442 | - return array_map([$this, 'createKeyValueMap'], $mountConfigs); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * @param int[] $mountIds |
|
| 447 | - * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 448 | - */ |
|
| 449 | - public function getOptionsForMounts($mountIds) { |
|
| 450 | - $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
|
| 451 | - $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
|
| 452 | - return array_map(function (array $options) { |
|
| 453 | - return array_map(function ($option) { |
|
| 454 | - return json_decode($option); |
|
| 455 | - }, $options); |
|
| 456 | - }, $optionsMap); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] |
|
| 461 | - * @return array ['key1' => $value1, ...] |
|
| 462 | - */ |
|
| 463 | - private function createKeyValueMap(array $keyValuePairs) { |
|
| 464 | - $decryptedPairts = array_map(function ($pair) { |
|
| 465 | - if ($pair['key'] === 'password') { |
|
| 466 | - $pair['value'] = $this->decryptValue($pair['value']); |
|
| 467 | - } |
|
| 468 | - return $pair; |
|
| 469 | - }, $keyValuePairs); |
|
| 470 | - $keys = array_map(function ($pair) { |
|
| 471 | - return $pair['key']; |
|
| 472 | - }, $decryptedPairts); |
|
| 473 | - $values = array_map(function ($pair) { |
|
| 474 | - return $pair['value']; |
|
| 475 | - }, $decryptedPairts); |
|
| 476 | - |
|
| 477 | - return array_combine($keys, $values); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - private function encryptValue($value) { |
|
| 481 | - return $this->crypto->encrypt($value); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - private function decryptValue($value) { |
|
| 485 | - try { |
|
| 486 | - return $this->crypto->decrypt($value); |
|
| 487 | - } catch (\Exception $e) { |
|
| 488 | - return $value; |
|
| 489 | - } |
|
| 490 | - } |
|
| 35 | + const MOUNT_TYPE_ADMIN = 1; |
|
| 36 | + const MOUNT_TYPE_PERSONAl = 2; |
|
| 37 | + |
|
| 38 | + const APPLICABLE_TYPE_GLOBAL = 1; |
|
| 39 | + const APPLICABLE_TYPE_GROUP = 2; |
|
| 40 | + const APPLICABLE_TYPE_USER = 3; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var IDBConnection |
|
| 44 | + */ |
|
| 45 | + private $connection; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var ICrypto |
|
| 49 | + */ |
|
| 50 | + private $crypto; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * DBConfigService constructor. |
|
| 54 | + * |
|
| 55 | + * @param IDBConnection $connection |
|
| 56 | + * @param ICrypto $crypto |
|
| 57 | + */ |
|
| 58 | + public function __construct(IDBConnection $connection, ICrypto $crypto) { |
|
| 59 | + $this->connection = $connection; |
|
| 60 | + $this->crypto = $crypto; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param int $mountId |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public function getMountById($mountId) { |
|
| 68 | + $builder = $this->connection->getQueryBuilder(); |
|
| 69 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 70 | + ->from('external_mounts', 'm') |
|
| 71 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 72 | + $mounts = $this->getMountsFromQuery($query); |
|
| 73 | + if (count($mounts) > 0) { |
|
| 74 | + return $mounts[0]; |
|
| 75 | + } else { |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get all configured mounts |
|
| 82 | + * |
|
| 83 | + * @return array |
|
| 84 | + */ |
|
| 85 | + public function getAllMounts() { |
|
| 86 | + $builder = $this->connection->getQueryBuilder(); |
|
| 87 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 88 | + ->from('external_mounts'); |
|
| 89 | + return $this->getMountsFromQuery($query); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function getMountsForUser($userId, $groupIds) { |
|
| 93 | + $builder = $this->connection->getQueryBuilder(); |
|
| 94 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 95 | + ->from('external_mounts', 'm') |
|
| 96 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 97 | + ->where($builder->expr()->orX( |
|
| 98 | + $builder->expr()->andX( // global mounts |
|
| 99 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)), |
|
| 100 | + $builder->expr()->isNull('a.value') |
|
| 101 | + ), |
|
| 102 | + $builder->expr()->andX( // mounts for user |
|
| 103 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), |
|
| 104 | + $builder->expr()->eq('a.value', $builder->createNamedParameter($userId)) |
|
| 105 | + ), |
|
| 106 | + $builder->expr()->andX( // mounts for group |
|
| 107 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), |
|
| 108 | + $builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 109 | + ) |
|
| 110 | + )); |
|
| 111 | + |
|
| 112 | + return $this->getMountsFromQuery($query); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Get admin defined mounts |
|
| 117 | + * |
|
| 118 | + * @return array |
|
| 119 | + * @suppress SqlInjectionChecker |
|
| 120 | + */ |
|
| 121 | + public function getAdminMounts() { |
|
| 122 | + $builder = $this->connection->getQueryBuilder(); |
|
| 123 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 124 | + ->from('external_mounts') |
|
| 125 | + ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 126 | + return $this->getMountsFromQuery($query); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + protected function getForQuery(IQueryBuilder $builder, $type, $value) { |
|
| 130 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 131 | + ->from('external_mounts', 'm') |
|
| 132 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 133 | + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 134 | + |
|
| 135 | + if (is_null($value)) { |
|
| 136 | + $query = $query->andWhere($builder->expr()->isNull('a.value')); |
|
| 137 | + } else { |
|
| 138 | + $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return $query; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Get mounts by applicable |
|
| 146 | + * |
|
| 147 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 148 | + * @param string|null $value user_id, group_id or null for global mounts |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | + public function getMountsFor($type, $value) { |
|
| 152 | + $builder = $this->connection->getQueryBuilder(); |
|
| 153 | + $query = $this->getForQuery($builder, $type, $value); |
|
| 154 | + |
|
| 155 | + return $this->getMountsFromQuery($query); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get admin defined mounts by applicable |
|
| 160 | + * |
|
| 161 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 162 | + * @param string|null $value user_id, group_id or null for global mounts |
|
| 163 | + * @return array |
|
| 164 | + * @suppress SqlInjectionChecker |
|
| 165 | + */ |
|
| 166 | + public function getAdminMountsFor($type, $value) { |
|
| 167 | + $builder = $this->connection->getQueryBuilder(); |
|
| 168 | + $query = $this->getForQuery($builder, $type, $value); |
|
| 169 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 170 | + |
|
| 171 | + return $this->getMountsFromQuery($query); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Get admin defined mounts for multiple applicable |
|
| 176 | + * |
|
| 177 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 178 | + * @param string[] $values user_ids or group_ids |
|
| 179 | + * @return array |
|
| 180 | + * @suppress SqlInjectionChecker |
|
| 181 | + */ |
|
| 182 | + public function getAdminMountsForMultiple($type, array $values) { |
|
| 183 | + $builder = $this->connection->getQueryBuilder(); |
|
| 184 | + $params = array_map(function ($value) use ($builder) { |
|
| 185 | + return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
|
| 186 | + }, $values); |
|
| 187 | + |
|
| 188 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 189 | + ->from('external_mounts', 'm') |
|
| 190 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 191 | + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
| 192 | + ->andWhere($builder->expr()->in('a.value', $params)); |
|
| 193 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 194 | + |
|
| 195 | + return $this->getMountsFromQuery($query); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get user defined mounts by applicable |
|
| 200 | + * |
|
| 201 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 202 | + * @param string|null $value user_id, group_id or null for global mounts |
|
| 203 | + * @return array |
|
| 204 | + * @suppress SqlInjectionChecker |
|
| 205 | + */ |
|
| 206 | + public function getUserMountsFor($type, $value) { |
|
| 207 | + $builder = $this->connection->getQueryBuilder(); |
|
| 208 | + $query = $this->getForQuery($builder, $type, $value); |
|
| 209 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); |
|
| 210 | + |
|
| 211 | + return $this->getMountsFromQuery($query); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Add a mount to the database |
|
| 216 | + * |
|
| 217 | + * @param string $mountPoint |
|
| 218 | + * @param string $storageBackend |
|
| 219 | + * @param string $authBackend |
|
| 220 | + * @param int $priority |
|
| 221 | + * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL |
|
| 222 | + * @return int the id of the new mount |
|
| 223 | + */ |
|
| 224 | + public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { |
|
| 225 | + if (!$priority) { |
|
| 226 | + $priority = 100; |
|
| 227 | + } |
|
| 228 | + $builder = $this->connection->getQueryBuilder(); |
|
| 229 | + $query = $builder->insert('external_mounts') |
|
| 230 | + ->values([ |
|
| 231 | + 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), |
|
| 232 | + 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), |
|
| 233 | + 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), |
|
| 234 | + 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
|
| 235 | + 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
|
| 236 | + ]); |
|
| 237 | + $query->execute(); |
|
| 238 | + return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Remove a mount from the database |
|
| 243 | + * |
|
| 244 | + * @param int $mountId |
|
| 245 | + */ |
|
| 246 | + public function removeMount($mountId) { |
|
| 247 | + $builder = $this->connection->getQueryBuilder(); |
|
| 248 | + $query = $builder->delete('external_mounts') |
|
| 249 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 250 | + $query->execute(); |
|
| 251 | + |
|
| 252 | + $query = $builder->delete('external_applicable') |
|
| 253 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 254 | + $query->execute(); |
|
| 255 | + |
|
| 256 | + $query = $builder->delete('external_config') |
|
| 257 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 258 | + $query->execute(); |
|
| 259 | + |
|
| 260 | + $query = $builder->delete('external_options') |
|
| 261 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 262 | + $query->execute(); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @param int $mountId |
|
| 267 | + * @param string $newMountPoint |
|
| 268 | + */ |
|
| 269 | + public function setMountPoint($mountId, $newMountPoint) { |
|
| 270 | + $builder = $this->connection->getQueryBuilder(); |
|
| 271 | + |
|
| 272 | + $query = $builder->update('external_mounts') |
|
| 273 | + ->set('mount_point', $builder->createNamedParameter($newMountPoint)) |
|
| 274 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 275 | + |
|
| 276 | + $query->execute(); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @param int $mountId |
|
| 281 | + * @param string $newAuthBackend |
|
| 282 | + */ |
|
| 283 | + public function setAuthBackend($mountId, $newAuthBackend) { |
|
| 284 | + $builder = $this->connection->getQueryBuilder(); |
|
| 285 | + |
|
| 286 | + $query = $builder->update('external_mounts') |
|
| 287 | + ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) |
|
| 288 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 289 | + |
|
| 290 | + $query->execute(); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param int $mountId |
|
| 295 | + * @param string $key |
|
| 296 | + * @param string $value |
|
| 297 | + */ |
|
| 298 | + public function setConfig($mountId, $key, $value) { |
|
| 299 | + if ($key === 'password') { |
|
| 300 | + $value = $this->encryptValue($value); |
|
| 301 | + } |
|
| 302 | + $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ |
|
| 303 | + 'mount_id' => $mountId, |
|
| 304 | + 'key' => $key, |
|
| 305 | + 'value' => $value |
|
| 306 | + ], ['mount_id', 'key']); |
|
| 307 | + if ($count === 0) { |
|
| 308 | + $builder = $this->connection->getQueryBuilder(); |
|
| 309 | + $query = $builder->update('external_config') |
|
| 310 | + ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) |
|
| 311 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 312 | + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 313 | + $query->execute(); |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * @param int $mountId |
|
| 319 | + * @param string $key |
|
| 320 | + * @param string $value |
|
| 321 | + */ |
|
| 322 | + public function setOption($mountId, $key, $value) { |
|
| 323 | + |
|
| 324 | + $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ |
|
| 325 | + 'mount_id' => $mountId, |
|
| 326 | + 'key' => $key, |
|
| 327 | + 'value' => json_encode($value) |
|
| 328 | + ], ['mount_id', 'key']); |
|
| 329 | + if ($count === 0) { |
|
| 330 | + $builder = $this->connection->getQueryBuilder(); |
|
| 331 | + $query = $builder->update('external_options') |
|
| 332 | + ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) |
|
| 333 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 334 | + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 335 | + $query->execute(); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + public function addApplicable($mountId, $type, $value) { |
|
| 340 | + $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ |
|
| 341 | + 'mount_id' => $mountId, |
|
| 342 | + 'type' => $type, |
|
| 343 | + 'value' => $value |
|
| 344 | + ], ['mount_id', 'type', 'value']); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + public function removeApplicable($mountId, $type, $value) { |
|
| 348 | + $builder = $this->connection->getQueryBuilder(); |
|
| 349 | + $query = $builder->delete('external_applicable') |
|
| 350 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 351 | + ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 352 | + |
|
| 353 | + if (is_null($value)) { |
|
| 354 | + $query = $query->andWhere($builder->expr()->isNull('value')); |
|
| 355 | + } else { |
|
| 356 | + $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $query->execute(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + private function getMountsFromQuery(IQueryBuilder $query) { |
|
| 363 | + $result = $query->execute(); |
|
| 364 | + $mounts = $result->fetchAll(); |
|
| 365 | + $uniqueMounts = []; |
|
| 366 | + foreach ($mounts as $mount) { |
|
| 367 | + $id = $mount['mount_id']; |
|
| 368 | + if (!isset($uniqueMounts[$id])) { |
|
| 369 | + $uniqueMounts[$id] = $mount; |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + $uniqueMounts = array_values($uniqueMounts); |
|
| 373 | + |
|
| 374 | + $mountIds = array_map(function ($mount) { |
|
| 375 | + return $mount['mount_id']; |
|
| 376 | + }, $uniqueMounts); |
|
| 377 | + $mountIds = array_values(array_unique($mountIds)); |
|
| 378 | + |
|
| 379 | + $applicable = $this->getApplicableForMounts($mountIds); |
|
| 380 | + $config = $this->getConfigForMounts($mountIds); |
|
| 381 | + $options = $this->getOptionsForMounts($mountIds); |
|
| 382 | + |
|
| 383 | + return array_map(function ($mount, $applicable, $config, $options) { |
|
| 384 | + $mount['type'] = (int)$mount['type']; |
|
| 385 | + $mount['priority'] = (int)$mount['priority']; |
|
| 386 | + $mount['applicable'] = $applicable; |
|
| 387 | + $mount['config'] = $config; |
|
| 388 | + $mount['options'] = $options; |
|
| 389 | + return $mount; |
|
| 390 | + }, $uniqueMounts, $applicable, $config, $options); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Get mount options from a table grouped by mount id |
|
| 395 | + * |
|
| 396 | + * @param string $table |
|
| 397 | + * @param string[] $fields |
|
| 398 | + * @param int[] $mountIds |
|
| 399 | + * @return array [$mountId => [['field1' => $value1, ...], ...], ...] |
|
| 400 | + */ |
|
| 401 | + private function selectForMounts($table, array $fields, array $mountIds) { |
|
| 402 | + if (count($mountIds) === 0) { |
|
| 403 | + return []; |
|
| 404 | + } |
|
| 405 | + $builder = $this->connection->getQueryBuilder(); |
|
| 406 | + $fields[] = 'mount_id'; |
|
| 407 | + $placeHolders = array_map(function ($id) use ($builder) { |
|
| 408 | + return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
|
| 409 | + }, $mountIds); |
|
| 410 | + $query = $builder->select($fields) |
|
| 411 | + ->from($table) |
|
| 412 | + ->where($builder->expr()->in('mount_id', $placeHolders)); |
|
| 413 | + $rows = $query->execute()->fetchAll(); |
|
| 414 | + |
|
| 415 | + $result = []; |
|
| 416 | + foreach ($mountIds as $mountId) { |
|
| 417 | + $result[$mountId] = []; |
|
| 418 | + } |
|
| 419 | + foreach ($rows as $row) { |
|
| 420 | + if (isset($row['type'])) { |
|
| 421 | + $row['type'] = (int)$row['type']; |
|
| 422 | + } |
|
| 423 | + $result[$row['mount_id']][] = $row; |
|
| 424 | + } |
|
| 425 | + return $result; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * @param int[] $mountIds |
|
| 430 | + * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] |
|
| 431 | + */ |
|
| 432 | + public function getApplicableForMounts($mountIds) { |
|
| 433 | + return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * @param int[] $mountIds |
|
| 438 | + * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 439 | + */ |
|
| 440 | + public function getConfigForMounts($mountIds) { |
|
| 441 | + $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); |
|
| 442 | + return array_map([$this, 'createKeyValueMap'], $mountConfigs); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * @param int[] $mountIds |
|
| 447 | + * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 448 | + */ |
|
| 449 | + public function getOptionsForMounts($mountIds) { |
|
| 450 | + $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
|
| 451 | + $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
|
| 452 | + return array_map(function (array $options) { |
|
| 453 | + return array_map(function ($option) { |
|
| 454 | + return json_decode($option); |
|
| 455 | + }, $options); |
|
| 456 | + }, $optionsMap); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] |
|
| 461 | + * @return array ['key1' => $value1, ...] |
|
| 462 | + */ |
|
| 463 | + private function createKeyValueMap(array $keyValuePairs) { |
|
| 464 | + $decryptedPairts = array_map(function ($pair) { |
|
| 465 | + if ($pair['key'] === 'password') { |
|
| 466 | + $pair['value'] = $this->decryptValue($pair['value']); |
|
| 467 | + } |
|
| 468 | + return $pair; |
|
| 469 | + }, $keyValuePairs); |
|
| 470 | + $keys = array_map(function ($pair) { |
|
| 471 | + return $pair['key']; |
|
| 472 | + }, $decryptedPairts); |
|
| 473 | + $values = array_map(function ($pair) { |
|
| 474 | + return $pair['value']; |
|
| 475 | + }, $decryptedPairts); |
|
| 476 | + |
|
| 477 | + return array_combine($keys, $values); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + private function encryptValue($value) { |
|
| 481 | + return $this->crypto->encrypt($value); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + private function decryptValue($value) { |
|
| 485 | + try { |
|
| 486 | + return $this->crypto->decrypt($value); |
|
| 487 | + } catch (\Exception $e) { |
|
| 488 | + return $value; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | 491 | } |
@@ -62,2069 +62,2069 @@ |
||
| 62 | 62 | */ |
| 63 | 63 | class CalDavBackend extends AbstractBackend implements SyncSupport, SubscriptionSupport, SchedulingSupport { |
| 64 | 64 | |
| 65 | - const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 66 | - const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * We need to specify a max date, because we need to stop *somewhere* |
|
| 70 | - * |
|
| 71 | - * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 72 | - * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 73 | - * in 2038-01-19 to avoid problems when the date is converted |
|
| 74 | - * to a unix timestamp. |
|
| 75 | - */ |
|
| 76 | - const MAX_DATE = '2038-01-01'; |
|
| 77 | - |
|
| 78 | - const ACCESS_PUBLIC = 4; |
|
| 79 | - const CLASSIFICATION_PUBLIC = 0; |
|
| 80 | - const CLASSIFICATION_PRIVATE = 1; |
|
| 81 | - const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * List of CalDAV properties, and how they map to database field names |
|
| 85 | - * Add your own properties by simply adding on to this array. |
|
| 86 | - * |
|
| 87 | - * Note that only string-based properties are supported here. |
|
| 88 | - * |
|
| 89 | - * @var array |
|
| 90 | - */ |
|
| 91 | - public $propertyMap = [ |
|
| 92 | - '{DAV:}displayname' => 'displayname', |
|
| 93 | - '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
| 94 | - '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
| 95 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 96 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 97 | - ]; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * List of subscription properties, and how they map to database field names. |
|
| 101 | - * |
|
| 102 | - * @var array |
|
| 103 | - */ |
|
| 104 | - public $subscriptionPropertyMap = [ |
|
| 105 | - '{DAV:}displayname' => 'displayname', |
|
| 106 | - '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
| 107 | - '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 108 | - '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 109 | - '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
| 110 | - '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
| 111 | - '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
| 112 | - ]; |
|
| 113 | - |
|
| 114 | - /** @var array properties to index */ |
|
| 115 | - public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
|
| 116 | - 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
|
| 117 | - 'ORGANIZER']; |
|
| 118 | - |
|
| 119 | - /** @var array parameters to index */ |
|
| 120 | - public static $indexParameters = [ |
|
| 121 | - 'ATTENDEE' => ['CN'], |
|
| 122 | - 'ORGANIZER' => ['CN'], |
|
| 123 | - ]; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @var string[] Map of uid => display name |
|
| 127 | - */ |
|
| 128 | - protected $userDisplayNames; |
|
| 129 | - |
|
| 130 | - /** @var IDBConnection */ |
|
| 131 | - private $db; |
|
| 132 | - |
|
| 133 | - /** @var Backend */ |
|
| 134 | - private $sharingBackend; |
|
| 135 | - |
|
| 136 | - /** @var Principal */ |
|
| 137 | - private $principalBackend; |
|
| 138 | - |
|
| 139 | - /** @var IUserManager */ |
|
| 140 | - private $userManager; |
|
| 141 | - |
|
| 142 | - /** @var ISecureRandom */ |
|
| 143 | - private $random; |
|
| 144 | - |
|
| 145 | - /** @var EventDispatcherInterface */ |
|
| 146 | - private $dispatcher; |
|
| 147 | - |
|
| 148 | - /** @var bool */ |
|
| 149 | - private $legacyEndpoint; |
|
| 150 | - |
|
| 151 | - /** @var string */ |
|
| 152 | - private $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * CalDavBackend constructor. |
|
| 156 | - * |
|
| 157 | - * @param IDBConnection $db |
|
| 158 | - * @param Principal $principalBackend |
|
| 159 | - * @param IUserManager $userManager |
|
| 160 | - * @param ISecureRandom $random |
|
| 161 | - * @param EventDispatcherInterface $dispatcher |
|
| 162 | - * @param bool $legacyEndpoint |
|
| 163 | - */ |
|
| 164 | - public function __construct(IDBConnection $db, |
|
| 165 | - Principal $principalBackend, |
|
| 166 | - IUserManager $userManager, |
|
| 167 | - ISecureRandom $random, |
|
| 168 | - EventDispatcherInterface $dispatcher, |
|
| 169 | - $legacyEndpoint = false) { |
|
| 170 | - $this->db = $db; |
|
| 171 | - $this->principalBackend = $principalBackend; |
|
| 172 | - $this->userManager = $userManager; |
|
| 173 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
| 174 | - $this->random = $random; |
|
| 175 | - $this->dispatcher = $dispatcher; |
|
| 176 | - $this->legacyEndpoint = $legacyEndpoint; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Return the number of calendars for a principal |
|
| 181 | - * |
|
| 182 | - * By default this excludes the automatically generated birthday calendar |
|
| 183 | - * |
|
| 184 | - * @param $principalUri |
|
| 185 | - * @param bool $excludeBirthday |
|
| 186 | - * @return int |
|
| 187 | - */ |
|
| 188 | - public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 189 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 190 | - $query = $this->db->getQueryBuilder(); |
|
| 191 | - $query->select($query->createFunction('COUNT(*)')) |
|
| 192 | - ->from('calendars') |
|
| 193 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 194 | - |
|
| 195 | - if ($excludeBirthday) { |
|
| 196 | - $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return (int)$query->execute()->fetchColumn(); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Returns a list of calendars for a principal. |
|
| 204 | - * |
|
| 205 | - * Every project is an array with the following keys: |
|
| 206 | - * * id, a unique id that will be used by other functions to modify the |
|
| 207 | - * calendar. This can be the same as the uri or a database key. |
|
| 208 | - * * uri, which the basename of the uri with which the calendar is |
|
| 209 | - * accessed. |
|
| 210 | - * * principaluri. The owner of the calendar. Almost always the same as |
|
| 211 | - * principalUri passed to this method. |
|
| 212 | - * |
|
| 213 | - * Furthermore it can contain webdav properties in clark notation. A very |
|
| 214 | - * common one is '{DAV:}displayname'. |
|
| 215 | - * |
|
| 216 | - * Many clients also require: |
|
| 217 | - * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 218 | - * For this property, you can just return an instance of |
|
| 219 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 220 | - * |
|
| 221 | - * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 222 | - * ACL will automatically be put in read-only mode. |
|
| 223 | - * |
|
| 224 | - * @param string $principalUri |
|
| 225 | - * @return array |
|
| 226 | - */ |
|
| 227 | - function getCalendarsForUser($principalUri) { |
|
| 228 | - $principalUriOriginal = $principalUri; |
|
| 229 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 230 | - $fields = array_values($this->propertyMap); |
|
| 231 | - $fields[] = 'id'; |
|
| 232 | - $fields[] = 'uri'; |
|
| 233 | - $fields[] = 'synctoken'; |
|
| 234 | - $fields[] = 'components'; |
|
| 235 | - $fields[] = 'principaluri'; |
|
| 236 | - $fields[] = 'transparent'; |
|
| 237 | - |
|
| 238 | - // Making fields a comma-delimited list |
|
| 239 | - $query = $this->db->getQueryBuilder(); |
|
| 240 | - $query->select($fields)->from('calendars') |
|
| 241 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 242 | - ->orderBy('calendarorder', 'ASC'); |
|
| 243 | - $stmt = $query->execute(); |
|
| 244 | - |
|
| 245 | - $calendars = []; |
|
| 246 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 247 | - |
|
| 248 | - $components = []; |
|
| 249 | - if ($row['components']) { |
|
| 250 | - $components = explode(',',$row['components']); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $calendar = [ |
|
| 254 | - 'id' => $row['id'], |
|
| 255 | - 'uri' => $row['uri'], |
|
| 256 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 257 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 258 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 259 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 260 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 261 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 262 | - ]; |
|
| 263 | - |
|
| 264 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 265 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $this->addOwnerPrincipal($calendar); |
|
| 269 | - |
|
| 270 | - if (!isset($calendars[$calendar['id']])) { |
|
| 271 | - $calendars[$calendar['id']] = $calendar; |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - $stmt->closeCursor(); |
|
| 276 | - |
|
| 277 | - // query for shared calendars |
|
| 278 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 279 | - $principals = array_map(function($principal) { |
|
| 280 | - return urldecode($principal); |
|
| 281 | - }, $principals); |
|
| 282 | - $principals[]= $principalUri; |
|
| 283 | - |
|
| 284 | - $fields = array_values($this->propertyMap); |
|
| 285 | - $fields[] = 'a.id'; |
|
| 286 | - $fields[] = 'a.uri'; |
|
| 287 | - $fields[] = 'a.synctoken'; |
|
| 288 | - $fields[] = 'a.components'; |
|
| 289 | - $fields[] = 'a.principaluri'; |
|
| 290 | - $fields[] = 'a.transparent'; |
|
| 291 | - $fields[] = 's.access'; |
|
| 292 | - $query = $this->db->getQueryBuilder(); |
|
| 293 | - $result = $query->select($fields) |
|
| 294 | - ->from('dav_shares', 's') |
|
| 295 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 296 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 297 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 298 | - ->setParameter('type', 'calendar') |
|
| 299 | - ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
| 300 | - ->execute(); |
|
| 301 | - |
|
| 302 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 303 | - while($row = $result->fetch()) { |
|
| 304 | - if ($row['principaluri'] === $principalUri) { |
|
| 305 | - continue; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 309 | - if (isset($calendars[$row['id']])) { |
|
| 310 | - if ($readOnly) { |
|
| 311 | - // New share can not have more permissions then the old one. |
|
| 312 | - continue; |
|
| 313 | - } |
|
| 314 | - if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 315 | - $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 316 | - // Old share is already read-write, no more permissions can be gained |
|
| 317 | - continue; |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 322 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 323 | - $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 324 | - $components = []; |
|
| 325 | - if ($row['components']) { |
|
| 326 | - $components = explode(',',$row['components']); |
|
| 327 | - } |
|
| 328 | - $calendar = [ |
|
| 329 | - 'id' => $row['id'], |
|
| 330 | - 'uri' => $uri, |
|
| 331 | - 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 332 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 333 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 334 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 335 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 336 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 337 | - $readOnlyPropertyName => $readOnly, |
|
| 338 | - ]; |
|
| 339 | - |
|
| 340 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 341 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - $this->addOwnerPrincipal($calendar); |
|
| 345 | - |
|
| 346 | - $calendars[$calendar['id']] = $calendar; |
|
| 347 | - } |
|
| 348 | - $result->closeCursor(); |
|
| 349 | - |
|
| 350 | - return array_values($calendars); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - public function getUsersOwnCalendars($principalUri) { |
|
| 354 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 355 | - $fields = array_values($this->propertyMap); |
|
| 356 | - $fields[] = 'id'; |
|
| 357 | - $fields[] = 'uri'; |
|
| 358 | - $fields[] = 'synctoken'; |
|
| 359 | - $fields[] = 'components'; |
|
| 360 | - $fields[] = 'principaluri'; |
|
| 361 | - $fields[] = 'transparent'; |
|
| 362 | - // Making fields a comma-delimited list |
|
| 363 | - $query = $this->db->getQueryBuilder(); |
|
| 364 | - $query->select($fields)->from('calendars') |
|
| 365 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 366 | - ->orderBy('calendarorder', 'ASC'); |
|
| 367 | - $stmt = $query->execute(); |
|
| 368 | - $calendars = []; |
|
| 369 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 370 | - $components = []; |
|
| 371 | - if ($row['components']) { |
|
| 372 | - $components = explode(',',$row['components']); |
|
| 373 | - } |
|
| 374 | - $calendar = [ |
|
| 375 | - 'id' => $row['id'], |
|
| 376 | - 'uri' => $row['uri'], |
|
| 377 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 378 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 379 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 380 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 381 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 382 | - ]; |
|
| 383 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 384 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - $this->addOwnerPrincipal($calendar); |
|
| 388 | - |
|
| 389 | - if (!isset($calendars[$calendar['id']])) { |
|
| 390 | - $calendars[$calendar['id']] = $calendar; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - $stmt->closeCursor(); |
|
| 394 | - return array_values($calendars); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - |
|
| 398 | - private function getUserDisplayName($uid) { |
|
| 399 | - if (!isset($this->userDisplayNames[$uid])) { |
|
| 400 | - $user = $this->userManager->get($uid); |
|
| 401 | - |
|
| 402 | - if ($user instanceof IUser) { |
|
| 403 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 404 | - } else { |
|
| 405 | - $this->userDisplayNames[$uid] = $uid; |
|
| 406 | - } |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - return $this->userDisplayNames[$uid]; |
|
| 410 | - } |
|
| 65 | + const PERSONAL_CALENDAR_URI = 'personal'; |
|
| 66 | + const PERSONAL_CALENDAR_NAME = 'Personal'; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * We need to specify a max date, because we need to stop *somewhere* |
|
| 70 | + * |
|
| 71 | + * On 32 bit system the maximum for a signed integer is 2147483647, so |
|
| 72 | + * MAX_DATE cannot be higher than date('Y-m-d', 2147483647) which results |
|
| 73 | + * in 2038-01-19 to avoid problems when the date is converted |
|
| 74 | + * to a unix timestamp. |
|
| 75 | + */ |
|
| 76 | + const MAX_DATE = '2038-01-01'; |
|
| 77 | + |
|
| 78 | + const ACCESS_PUBLIC = 4; |
|
| 79 | + const CLASSIFICATION_PUBLIC = 0; |
|
| 80 | + const CLASSIFICATION_PRIVATE = 1; |
|
| 81 | + const CLASSIFICATION_CONFIDENTIAL = 2; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * List of CalDAV properties, and how they map to database field names |
|
| 85 | + * Add your own properties by simply adding on to this array. |
|
| 86 | + * |
|
| 87 | + * Note that only string-based properties are supported here. |
|
| 88 | + * |
|
| 89 | + * @var array |
|
| 90 | + */ |
|
| 91 | + public $propertyMap = [ |
|
| 92 | + '{DAV:}displayname' => 'displayname', |
|
| 93 | + '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'description', |
|
| 94 | + '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'timezone', |
|
| 95 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 96 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 97 | + ]; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * List of subscription properties, and how they map to database field names. |
|
| 101 | + * |
|
| 102 | + * @var array |
|
| 103 | + */ |
|
| 104 | + public $subscriptionPropertyMap = [ |
|
| 105 | + '{DAV:}displayname' => 'displayname', |
|
| 106 | + '{http://apple.com/ns/ical/}refreshrate' => 'refreshrate', |
|
| 107 | + '{http://apple.com/ns/ical/}calendar-order' => 'calendarorder', |
|
| 108 | + '{http://apple.com/ns/ical/}calendar-color' => 'calendarcolor', |
|
| 109 | + '{http://calendarserver.org/ns/}subscribed-strip-todos' => 'striptodos', |
|
| 110 | + '{http://calendarserver.org/ns/}subscribed-strip-alarms' => 'stripalarms', |
|
| 111 | + '{http://calendarserver.org/ns/}subscribed-strip-attachments' => 'stripattachments', |
|
| 112 | + ]; |
|
| 113 | + |
|
| 114 | + /** @var array properties to index */ |
|
| 115 | + public static $indexProperties = ['CATEGORIES', 'COMMENT', 'DESCRIPTION', |
|
| 116 | + 'LOCATION', 'RESOURCES', 'STATUS', 'SUMMARY', 'ATTENDEE', 'CONTACT', |
|
| 117 | + 'ORGANIZER']; |
|
| 118 | + |
|
| 119 | + /** @var array parameters to index */ |
|
| 120 | + public static $indexParameters = [ |
|
| 121 | + 'ATTENDEE' => ['CN'], |
|
| 122 | + 'ORGANIZER' => ['CN'], |
|
| 123 | + ]; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @var string[] Map of uid => display name |
|
| 127 | + */ |
|
| 128 | + protected $userDisplayNames; |
|
| 129 | + |
|
| 130 | + /** @var IDBConnection */ |
|
| 131 | + private $db; |
|
| 132 | + |
|
| 133 | + /** @var Backend */ |
|
| 134 | + private $sharingBackend; |
|
| 135 | + |
|
| 136 | + /** @var Principal */ |
|
| 137 | + private $principalBackend; |
|
| 138 | + |
|
| 139 | + /** @var IUserManager */ |
|
| 140 | + private $userManager; |
|
| 141 | + |
|
| 142 | + /** @var ISecureRandom */ |
|
| 143 | + private $random; |
|
| 144 | + |
|
| 145 | + /** @var EventDispatcherInterface */ |
|
| 146 | + private $dispatcher; |
|
| 147 | + |
|
| 148 | + /** @var bool */ |
|
| 149 | + private $legacyEndpoint; |
|
| 150 | + |
|
| 151 | + /** @var string */ |
|
| 152 | + private $dbObjectPropertiesTable = 'calendarobjects_props'; |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * CalDavBackend constructor. |
|
| 156 | + * |
|
| 157 | + * @param IDBConnection $db |
|
| 158 | + * @param Principal $principalBackend |
|
| 159 | + * @param IUserManager $userManager |
|
| 160 | + * @param ISecureRandom $random |
|
| 161 | + * @param EventDispatcherInterface $dispatcher |
|
| 162 | + * @param bool $legacyEndpoint |
|
| 163 | + */ |
|
| 164 | + public function __construct(IDBConnection $db, |
|
| 165 | + Principal $principalBackend, |
|
| 166 | + IUserManager $userManager, |
|
| 167 | + ISecureRandom $random, |
|
| 168 | + EventDispatcherInterface $dispatcher, |
|
| 169 | + $legacyEndpoint = false) { |
|
| 170 | + $this->db = $db; |
|
| 171 | + $this->principalBackend = $principalBackend; |
|
| 172 | + $this->userManager = $userManager; |
|
| 173 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'calendar'); |
|
| 174 | + $this->random = $random; |
|
| 175 | + $this->dispatcher = $dispatcher; |
|
| 176 | + $this->legacyEndpoint = $legacyEndpoint; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Return the number of calendars for a principal |
|
| 181 | + * |
|
| 182 | + * By default this excludes the automatically generated birthday calendar |
|
| 183 | + * |
|
| 184 | + * @param $principalUri |
|
| 185 | + * @param bool $excludeBirthday |
|
| 186 | + * @return int |
|
| 187 | + */ |
|
| 188 | + public function getCalendarsForUserCount($principalUri, $excludeBirthday = true) { |
|
| 189 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 190 | + $query = $this->db->getQueryBuilder(); |
|
| 191 | + $query->select($query->createFunction('COUNT(*)')) |
|
| 192 | + ->from('calendars') |
|
| 193 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 194 | + |
|
| 195 | + if ($excludeBirthday) { |
|
| 196 | + $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return (int)$query->execute()->fetchColumn(); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Returns a list of calendars for a principal. |
|
| 204 | + * |
|
| 205 | + * Every project is an array with the following keys: |
|
| 206 | + * * id, a unique id that will be used by other functions to modify the |
|
| 207 | + * calendar. This can be the same as the uri or a database key. |
|
| 208 | + * * uri, which the basename of the uri with which the calendar is |
|
| 209 | + * accessed. |
|
| 210 | + * * principaluri. The owner of the calendar. Almost always the same as |
|
| 211 | + * principalUri passed to this method. |
|
| 212 | + * |
|
| 213 | + * Furthermore it can contain webdav properties in clark notation. A very |
|
| 214 | + * common one is '{DAV:}displayname'. |
|
| 215 | + * |
|
| 216 | + * Many clients also require: |
|
| 217 | + * {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 218 | + * For this property, you can just return an instance of |
|
| 219 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet. |
|
| 220 | + * |
|
| 221 | + * If you return {http://sabredav.org/ns}read-only and set the value to 1, |
|
| 222 | + * ACL will automatically be put in read-only mode. |
|
| 223 | + * |
|
| 224 | + * @param string $principalUri |
|
| 225 | + * @return array |
|
| 226 | + */ |
|
| 227 | + function getCalendarsForUser($principalUri) { |
|
| 228 | + $principalUriOriginal = $principalUri; |
|
| 229 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 230 | + $fields = array_values($this->propertyMap); |
|
| 231 | + $fields[] = 'id'; |
|
| 232 | + $fields[] = 'uri'; |
|
| 233 | + $fields[] = 'synctoken'; |
|
| 234 | + $fields[] = 'components'; |
|
| 235 | + $fields[] = 'principaluri'; |
|
| 236 | + $fields[] = 'transparent'; |
|
| 237 | + |
|
| 238 | + // Making fields a comma-delimited list |
|
| 239 | + $query = $this->db->getQueryBuilder(); |
|
| 240 | + $query->select($fields)->from('calendars') |
|
| 241 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 242 | + ->orderBy('calendarorder', 'ASC'); |
|
| 243 | + $stmt = $query->execute(); |
|
| 244 | + |
|
| 245 | + $calendars = []; |
|
| 246 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 247 | + |
|
| 248 | + $components = []; |
|
| 249 | + if ($row['components']) { |
|
| 250 | + $components = explode(',',$row['components']); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $calendar = [ |
|
| 254 | + 'id' => $row['id'], |
|
| 255 | + 'uri' => $row['uri'], |
|
| 256 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 257 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 258 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 259 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 260 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 261 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 262 | + ]; |
|
| 263 | + |
|
| 264 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 265 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $this->addOwnerPrincipal($calendar); |
|
| 269 | + |
|
| 270 | + if (!isset($calendars[$calendar['id']])) { |
|
| 271 | + $calendars[$calendar['id']] = $calendar; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + $stmt->closeCursor(); |
|
| 276 | + |
|
| 277 | + // query for shared calendars |
|
| 278 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 279 | + $principals = array_map(function($principal) { |
|
| 280 | + return urldecode($principal); |
|
| 281 | + }, $principals); |
|
| 282 | + $principals[]= $principalUri; |
|
| 283 | + |
|
| 284 | + $fields = array_values($this->propertyMap); |
|
| 285 | + $fields[] = 'a.id'; |
|
| 286 | + $fields[] = 'a.uri'; |
|
| 287 | + $fields[] = 'a.synctoken'; |
|
| 288 | + $fields[] = 'a.components'; |
|
| 289 | + $fields[] = 'a.principaluri'; |
|
| 290 | + $fields[] = 'a.transparent'; |
|
| 291 | + $fields[] = 's.access'; |
|
| 292 | + $query = $this->db->getQueryBuilder(); |
|
| 293 | + $result = $query->select($fields) |
|
| 294 | + ->from('dav_shares', 's') |
|
| 295 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 296 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 297 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 298 | + ->setParameter('type', 'calendar') |
|
| 299 | + ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
|
| 300 | + ->execute(); |
|
| 301 | + |
|
| 302 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 303 | + while($row = $result->fetch()) { |
|
| 304 | + if ($row['principaluri'] === $principalUri) { |
|
| 305 | + continue; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 309 | + if (isset($calendars[$row['id']])) { |
|
| 310 | + if ($readOnly) { |
|
| 311 | + // New share can not have more permissions then the old one. |
|
| 312 | + continue; |
|
| 313 | + } |
|
| 314 | + if (isset($calendars[$row['id']][$readOnlyPropertyName]) && |
|
| 315 | + $calendars[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 316 | + // Old share is already read-write, no more permissions can be gained |
|
| 317 | + continue; |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 322 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 323 | + $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 324 | + $components = []; |
|
| 325 | + if ($row['components']) { |
|
| 326 | + $components = explode(',',$row['components']); |
|
| 327 | + } |
|
| 328 | + $calendar = [ |
|
| 329 | + 'id' => $row['id'], |
|
| 330 | + 'uri' => $uri, |
|
| 331 | + 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 332 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 333 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 334 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 335 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 336 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 337 | + $readOnlyPropertyName => $readOnly, |
|
| 338 | + ]; |
|
| 339 | + |
|
| 340 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 341 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + $this->addOwnerPrincipal($calendar); |
|
| 345 | + |
|
| 346 | + $calendars[$calendar['id']] = $calendar; |
|
| 347 | + } |
|
| 348 | + $result->closeCursor(); |
|
| 349 | + |
|
| 350 | + return array_values($calendars); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + public function getUsersOwnCalendars($principalUri) { |
|
| 354 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 355 | + $fields = array_values($this->propertyMap); |
|
| 356 | + $fields[] = 'id'; |
|
| 357 | + $fields[] = 'uri'; |
|
| 358 | + $fields[] = 'synctoken'; |
|
| 359 | + $fields[] = 'components'; |
|
| 360 | + $fields[] = 'principaluri'; |
|
| 361 | + $fields[] = 'transparent'; |
|
| 362 | + // Making fields a comma-delimited list |
|
| 363 | + $query = $this->db->getQueryBuilder(); |
|
| 364 | + $query->select($fields)->from('calendars') |
|
| 365 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 366 | + ->orderBy('calendarorder', 'ASC'); |
|
| 367 | + $stmt = $query->execute(); |
|
| 368 | + $calendars = []; |
|
| 369 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 370 | + $components = []; |
|
| 371 | + if ($row['components']) { |
|
| 372 | + $components = explode(',',$row['components']); |
|
| 373 | + } |
|
| 374 | + $calendar = [ |
|
| 375 | + 'id' => $row['id'], |
|
| 376 | + 'uri' => $row['uri'], |
|
| 377 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 378 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 379 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 380 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 381 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 382 | + ]; |
|
| 383 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 384 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + $this->addOwnerPrincipal($calendar); |
|
| 388 | + |
|
| 389 | + if (!isset($calendars[$calendar['id']])) { |
|
| 390 | + $calendars[$calendar['id']] = $calendar; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + $stmt->closeCursor(); |
|
| 394 | + return array_values($calendars); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + |
|
| 398 | + private function getUserDisplayName($uid) { |
|
| 399 | + if (!isset($this->userDisplayNames[$uid])) { |
|
| 400 | + $user = $this->userManager->get($uid); |
|
| 401 | + |
|
| 402 | + if ($user instanceof IUser) { |
|
| 403 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 404 | + } else { |
|
| 405 | + $this->userDisplayNames[$uid] = $uid; |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + return $this->userDisplayNames[$uid]; |
|
| 410 | + } |
|
| 411 | 411 | |
| 412 | - /** |
|
| 413 | - * @return array |
|
| 414 | - */ |
|
| 415 | - public function getPublicCalendars() { |
|
| 416 | - $fields = array_values($this->propertyMap); |
|
| 417 | - $fields[] = 'a.id'; |
|
| 418 | - $fields[] = 'a.uri'; |
|
| 419 | - $fields[] = 'a.synctoken'; |
|
| 420 | - $fields[] = 'a.components'; |
|
| 421 | - $fields[] = 'a.principaluri'; |
|
| 422 | - $fields[] = 'a.transparent'; |
|
| 423 | - $fields[] = 's.access'; |
|
| 424 | - $fields[] = 's.publicuri'; |
|
| 425 | - $calendars = []; |
|
| 426 | - $query = $this->db->getQueryBuilder(); |
|
| 427 | - $result = $query->select($fields) |
|
| 428 | - ->from('dav_shares', 's') |
|
| 429 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 430 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 431 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 432 | - ->execute(); |
|
| 433 | - |
|
| 434 | - while($row = $result->fetch()) { |
|
| 435 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 436 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 437 | - $components = []; |
|
| 438 | - if ($row['components']) { |
|
| 439 | - $components = explode(',',$row['components']); |
|
| 440 | - } |
|
| 441 | - $calendar = [ |
|
| 442 | - 'id' => $row['id'], |
|
| 443 | - 'uri' => $row['publicuri'], |
|
| 444 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 445 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 446 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 447 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 448 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 449 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 450 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 451 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 452 | - ]; |
|
| 453 | - |
|
| 454 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 455 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - $this->addOwnerPrincipal($calendar); |
|
| 459 | - |
|
| 460 | - if (!isset($calendars[$calendar['id']])) { |
|
| 461 | - $calendars[$calendar['id']] = $calendar; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - $result->closeCursor(); |
|
| 465 | - |
|
| 466 | - return array_values($calendars); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * @param string $uri |
|
| 471 | - * @return array |
|
| 472 | - * @throws NotFound |
|
| 473 | - */ |
|
| 474 | - public function getPublicCalendar($uri) { |
|
| 475 | - $fields = array_values($this->propertyMap); |
|
| 476 | - $fields[] = 'a.id'; |
|
| 477 | - $fields[] = 'a.uri'; |
|
| 478 | - $fields[] = 'a.synctoken'; |
|
| 479 | - $fields[] = 'a.components'; |
|
| 480 | - $fields[] = 'a.principaluri'; |
|
| 481 | - $fields[] = 'a.transparent'; |
|
| 482 | - $fields[] = 's.access'; |
|
| 483 | - $fields[] = 's.publicuri'; |
|
| 484 | - $query = $this->db->getQueryBuilder(); |
|
| 485 | - $result = $query->select($fields) |
|
| 486 | - ->from('dav_shares', 's') |
|
| 487 | - ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 488 | - ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 489 | - ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 490 | - ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 491 | - ->execute(); |
|
| 492 | - |
|
| 493 | - $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
| 494 | - |
|
| 495 | - $result->closeCursor(); |
|
| 496 | - |
|
| 497 | - if ($row === false) { |
|
| 498 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 502 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 503 | - $components = []; |
|
| 504 | - if ($row['components']) { |
|
| 505 | - $components = explode(',',$row['components']); |
|
| 506 | - } |
|
| 507 | - $calendar = [ |
|
| 508 | - 'id' => $row['id'], |
|
| 509 | - 'uri' => $row['publicuri'], |
|
| 510 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 511 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 512 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 513 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 514 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 515 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 516 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 517 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 518 | - ]; |
|
| 519 | - |
|
| 520 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 521 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $this->addOwnerPrincipal($calendar); |
|
| 525 | - |
|
| 526 | - return $calendar; |
|
| 527 | - |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * @param string $principal |
|
| 532 | - * @param string $uri |
|
| 533 | - * @return array|null |
|
| 534 | - */ |
|
| 535 | - public function getCalendarByUri($principal, $uri) { |
|
| 536 | - $fields = array_values($this->propertyMap); |
|
| 537 | - $fields[] = 'id'; |
|
| 538 | - $fields[] = 'uri'; |
|
| 539 | - $fields[] = 'synctoken'; |
|
| 540 | - $fields[] = 'components'; |
|
| 541 | - $fields[] = 'principaluri'; |
|
| 542 | - $fields[] = 'transparent'; |
|
| 543 | - |
|
| 544 | - // Making fields a comma-delimited list |
|
| 545 | - $query = $this->db->getQueryBuilder(); |
|
| 546 | - $query->select($fields)->from('calendars') |
|
| 547 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 548 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 549 | - ->setMaxResults(1); |
|
| 550 | - $stmt = $query->execute(); |
|
| 551 | - |
|
| 552 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 553 | - $stmt->closeCursor(); |
|
| 554 | - if ($row === false) { |
|
| 555 | - return null; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - $components = []; |
|
| 559 | - if ($row['components']) { |
|
| 560 | - $components = explode(',',$row['components']); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - $calendar = [ |
|
| 564 | - 'id' => $row['id'], |
|
| 565 | - 'uri' => $row['uri'], |
|
| 566 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 567 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 568 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 569 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 570 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 571 | - ]; |
|
| 572 | - |
|
| 573 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 574 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - $this->addOwnerPrincipal($calendar); |
|
| 578 | - |
|
| 579 | - return $calendar; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - public function getCalendarById($calendarId) { |
|
| 583 | - $fields = array_values($this->propertyMap); |
|
| 584 | - $fields[] = 'id'; |
|
| 585 | - $fields[] = 'uri'; |
|
| 586 | - $fields[] = 'synctoken'; |
|
| 587 | - $fields[] = 'components'; |
|
| 588 | - $fields[] = 'principaluri'; |
|
| 589 | - $fields[] = 'transparent'; |
|
| 590 | - |
|
| 591 | - // Making fields a comma-delimited list |
|
| 592 | - $query = $this->db->getQueryBuilder(); |
|
| 593 | - $query->select($fields)->from('calendars') |
|
| 594 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 595 | - ->setMaxResults(1); |
|
| 596 | - $stmt = $query->execute(); |
|
| 597 | - |
|
| 598 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 599 | - $stmt->closeCursor(); |
|
| 600 | - if ($row === false) { |
|
| 601 | - return null; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - $components = []; |
|
| 605 | - if ($row['components']) { |
|
| 606 | - $components = explode(',',$row['components']); |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $calendar = [ |
|
| 610 | - 'id' => $row['id'], |
|
| 611 | - 'uri' => $row['uri'], |
|
| 612 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 613 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 614 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 615 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 616 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 617 | - ]; |
|
| 618 | - |
|
| 619 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 620 | - $calendar[$xmlName] = $row[$dbName]; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - $this->addOwnerPrincipal($calendar); |
|
| 624 | - |
|
| 625 | - return $calendar; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * Creates a new calendar for a principal. |
|
| 630 | - * |
|
| 631 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 632 | - * this calendar in other methods, such as updateCalendar. |
|
| 633 | - * |
|
| 634 | - * @param string $principalUri |
|
| 635 | - * @param string $calendarUri |
|
| 636 | - * @param array $properties |
|
| 637 | - * @return int |
|
| 638 | - * @suppress SqlInjectionChecker |
|
| 639 | - */ |
|
| 640 | - function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 641 | - $values = [ |
|
| 642 | - 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 643 | - 'uri' => $calendarUri, |
|
| 644 | - 'synctoken' => 1, |
|
| 645 | - 'transparent' => 0, |
|
| 646 | - 'components' => 'VEVENT,VTODO', |
|
| 647 | - 'displayname' => $calendarUri |
|
| 648 | - ]; |
|
| 649 | - |
|
| 650 | - // Default value |
|
| 651 | - $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 652 | - if (isset($properties[$sccs])) { |
|
| 653 | - if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 654 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 655 | - } |
|
| 656 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 657 | - } |
|
| 658 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 659 | - if (isset($properties[$transp])) { |
|
| 660 | - $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 664 | - if (isset($properties[$xmlName])) { |
|
| 665 | - $values[$dbName] = $properties[$xmlName]; |
|
| 666 | - } |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - $query = $this->db->getQueryBuilder(); |
|
| 670 | - $query->insert('calendars'); |
|
| 671 | - foreach($values as $column => $value) { |
|
| 672 | - $query->setValue($column, $query->createNamedParameter($value)); |
|
| 673 | - } |
|
| 674 | - $query->execute(); |
|
| 675 | - $calendarId = $query->getLastInsertId(); |
|
| 676 | - |
|
| 677 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( |
|
| 678 | - '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', |
|
| 679 | - [ |
|
| 680 | - 'calendarId' => $calendarId, |
|
| 681 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 682 | - ])); |
|
| 683 | - |
|
| 684 | - return $calendarId; |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - /** |
|
| 688 | - * Updates properties for a calendar. |
|
| 689 | - * |
|
| 690 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 691 | - * To do the actual updates, you must tell this object which properties |
|
| 692 | - * you're going to process with the handle() method. |
|
| 693 | - * |
|
| 694 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 695 | - * promise I can handle updating this property". |
|
| 696 | - * |
|
| 697 | - * Read the PropPatch documentation for more info and examples. |
|
| 698 | - * |
|
| 699 | - * @param mixed $calendarId |
|
| 700 | - * @param PropPatch $propPatch |
|
| 701 | - * @return void |
|
| 702 | - */ |
|
| 703 | - function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 704 | - $supportedProperties = array_keys($this->propertyMap); |
|
| 705 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 706 | - |
|
| 707 | - /** |
|
| 708 | - * @suppress SqlInjectionChecker |
|
| 709 | - */ |
|
| 710 | - $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 711 | - $newValues = []; |
|
| 712 | - foreach ($mutations as $propertyName => $propertyValue) { |
|
| 713 | - |
|
| 714 | - switch ($propertyName) { |
|
| 715 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 716 | - $fieldName = 'transparent'; |
|
| 717 | - $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 718 | - break; |
|
| 719 | - default : |
|
| 720 | - $fieldName = $this->propertyMap[$propertyName]; |
|
| 721 | - $newValues[$fieldName] = $propertyValue; |
|
| 722 | - break; |
|
| 723 | - } |
|
| 724 | - |
|
| 725 | - } |
|
| 726 | - $query = $this->db->getQueryBuilder(); |
|
| 727 | - $query->update('calendars'); |
|
| 728 | - foreach ($newValues as $fieldName => $value) { |
|
| 729 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 730 | - } |
|
| 731 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 732 | - $query->execute(); |
|
| 733 | - |
|
| 734 | - $this->addChange($calendarId, "", 2); |
|
| 735 | - |
|
| 736 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( |
|
| 737 | - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', |
|
| 738 | - [ |
|
| 739 | - 'calendarId' => $calendarId, |
|
| 740 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 741 | - 'shares' => $this->getShares($calendarId), |
|
| 742 | - 'propertyMutations' => $mutations, |
|
| 743 | - ])); |
|
| 744 | - |
|
| 745 | - return true; |
|
| 746 | - }); |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * Delete a calendar and all it's objects |
|
| 751 | - * |
|
| 752 | - * @param mixed $calendarId |
|
| 753 | - * @return void |
|
| 754 | - */ |
|
| 755 | - function deleteCalendar($calendarId) { |
|
| 756 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent( |
|
| 757 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', |
|
| 758 | - [ |
|
| 759 | - 'calendarId' => $calendarId, |
|
| 760 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 761 | - 'shares' => $this->getShares($calendarId), |
|
| 762 | - ])); |
|
| 763 | - |
|
| 764 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
| 765 | - $stmt->execute([$calendarId]); |
|
| 766 | - |
|
| 767 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 768 | - $stmt->execute([$calendarId]); |
|
| 769 | - |
|
| 770 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
| 771 | - $stmt->execute([$calendarId]); |
|
| 772 | - |
|
| 773 | - $this->sharingBackend->deleteAllShares($calendarId); |
|
| 774 | - |
|
| 775 | - $query = $this->db->getQueryBuilder(); |
|
| 776 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 777 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 778 | - ->execute(); |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - /** |
|
| 782 | - * Delete all of an user's shares |
|
| 783 | - * |
|
| 784 | - * @param string $principaluri |
|
| 785 | - * @return void |
|
| 786 | - */ |
|
| 787 | - function deleteAllSharesByUser($principaluri) { |
|
| 788 | - $this->sharingBackend->deleteAllSharesByUser($principaluri); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - /** |
|
| 792 | - * Returns all calendar objects within a calendar. |
|
| 793 | - * |
|
| 794 | - * Every item contains an array with the following keys: |
|
| 795 | - * * calendardata - The iCalendar-compatible calendar data |
|
| 796 | - * * uri - a unique key which will be used to construct the uri. This can |
|
| 797 | - * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 798 | - * good idea. This is only the basename, or filename, not the full |
|
| 799 | - * path. |
|
| 800 | - * * lastmodified - a timestamp of the last modification time |
|
| 801 | - * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 802 | - * '"abcdef"') |
|
| 803 | - * * size - The size of the calendar objects, in bytes. |
|
| 804 | - * * component - optional, a string containing the type of object, such |
|
| 805 | - * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 806 | - * the Content-Type header. |
|
| 807 | - * |
|
| 808 | - * Note that the etag is optional, but it's highly encouraged to return for |
|
| 809 | - * speed reasons. |
|
| 810 | - * |
|
| 811 | - * The calendardata is also optional. If it's not returned |
|
| 812 | - * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 813 | - * calendardata. |
|
| 814 | - * |
|
| 815 | - * If neither etag or size are specified, the calendardata will be |
|
| 816 | - * used/fetched to determine these numbers. If both are specified the |
|
| 817 | - * amount of times this is needed is reduced by a great degree. |
|
| 818 | - * |
|
| 819 | - * @param mixed $calendarId |
|
| 820 | - * @return array |
|
| 821 | - */ |
|
| 822 | - function getCalendarObjects($calendarId) { |
|
| 823 | - $query = $this->db->getQueryBuilder(); |
|
| 824 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 825 | - ->from('calendarobjects') |
|
| 826 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 827 | - $stmt = $query->execute(); |
|
| 828 | - |
|
| 829 | - $result = []; |
|
| 830 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 831 | - $result[] = [ |
|
| 832 | - 'id' => $row['id'], |
|
| 833 | - 'uri' => $row['uri'], |
|
| 834 | - 'lastmodified' => $row['lastmodified'], |
|
| 835 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 836 | - 'calendarid' => $row['calendarid'], |
|
| 837 | - 'size' => (int)$row['size'], |
|
| 838 | - 'component' => strtolower($row['componenttype']), |
|
| 839 | - 'classification'=> (int)$row['classification'] |
|
| 840 | - ]; |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - return $result; |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Returns information from a single calendar object, based on it's object |
|
| 848 | - * uri. |
|
| 849 | - * |
|
| 850 | - * The object uri is only the basename, or filename and not a full path. |
|
| 851 | - * |
|
| 852 | - * The returned array must have the same keys as getCalendarObjects. The |
|
| 853 | - * 'calendardata' object is required here though, while it's not required |
|
| 854 | - * for getCalendarObjects. |
|
| 855 | - * |
|
| 856 | - * This method must return null if the object did not exist. |
|
| 857 | - * |
|
| 858 | - * @param mixed $calendarId |
|
| 859 | - * @param string $objectUri |
|
| 860 | - * @return array|null |
|
| 861 | - */ |
|
| 862 | - function getCalendarObject($calendarId, $objectUri) { |
|
| 863 | - |
|
| 864 | - $query = $this->db->getQueryBuilder(); |
|
| 865 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 866 | - ->from('calendarobjects') |
|
| 867 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 868 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
| 869 | - $stmt = $query->execute(); |
|
| 870 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 871 | - |
|
| 872 | - if(!$row) return null; |
|
| 873 | - |
|
| 874 | - return [ |
|
| 875 | - 'id' => $row['id'], |
|
| 876 | - 'uri' => $row['uri'], |
|
| 877 | - 'lastmodified' => $row['lastmodified'], |
|
| 878 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 879 | - 'calendarid' => $row['calendarid'], |
|
| 880 | - 'size' => (int)$row['size'], |
|
| 881 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 882 | - 'component' => strtolower($row['componenttype']), |
|
| 883 | - 'classification'=> (int)$row['classification'] |
|
| 884 | - ]; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - /** |
|
| 888 | - * Returns a list of calendar objects. |
|
| 889 | - * |
|
| 890 | - * This method should work identical to getCalendarObject, but instead |
|
| 891 | - * return all the calendar objects in the list as an array. |
|
| 892 | - * |
|
| 893 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 894 | - * |
|
| 895 | - * @param mixed $calendarId |
|
| 896 | - * @param string[] $uris |
|
| 897 | - * @return array |
|
| 898 | - */ |
|
| 899 | - function getMultipleCalendarObjects($calendarId, array $uris) { |
|
| 900 | - if (empty($uris)) { |
|
| 901 | - return []; |
|
| 902 | - } |
|
| 903 | - |
|
| 904 | - $chunks = array_chunk($uris, 100); |
|
| 905 | - $objects = []; |
|
| 906 | - |
|
| 907 | - $query = $this->db->getQueryBuilder(); |
|
| 908 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 909 | - ->from('calendarobjects') |
|
| 910 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 911 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 912 | - |
|
| 913 | - foreach ($chunks as $uris) { |
|
| 914 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 915 | - $result = $query->execute(); |
|
| 916 | - |
|
| 917 | - while ($row = $result->fetch()) { |
|
| 918 | - $objects[] = [ |
|
| 919 | - 'id' => $row['id'], |
|
| 920 | - 'uri' => $row['uri'], |
|
| 921 | - 'lastmodified' => $row['lastmodified'], |
|
| 922 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 923 | - 'calendarid' => $row['calendarid'], |
|
| 924 | - 'size' => (int)$row['size'], |
|
| 925 | - 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 926 | - 'component' => strtolower($row['componenttype']), |
|
| 927 | - 'classification' => (int)$row['classification'] |
|
| 928 | - ]; |
|
| 929 | - } |
|
| 930 | - $result->closeCursor(); |
|
| 931 | - } |
|
| 932 | - return $objects; |
|
| 933 | - } |
|
| 934 | - |
|
| 935 | - /** |
|
| 936 | - * Creates a new calendar object. |
|
| 937 | - * |
|
| 938 | - * The object uri is only the basename, or filename and not a full path. |
|
| 939 | - * |
|
| 940 | - * It is possible return an etag from this function, which will be used in |
|
| 941 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 942 | - * by double-quotes. |
|
| 943 | - * |
|
| 944 | - * However, you should only really return this ETag if you don't mangle the |
|
| 945 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 946 | - * the exact same as this request body, you should omit the ETag. |
|
| 947 | - * |
|
| 948 | - * @param mixed $calendarId |
|
| 949 | - * @param string $objectUri |
|
| 950 | - * @param string $calendarData |
|
| 951 | - * @return string |
|
| 952 | - */ |
|
| 953 | - function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 954 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 955 | - |
|
| 956 | - $query = $this->db->getQueryBuilder(); |
|
| 957 | - $query->insert('calendarobjects') |
|
| 958 | - ->values([ |
|
| 959 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 960 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 961 | - 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 962 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 963 | - 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 964 | - 'size' => $query->createNamedParameter($extraData['size']), |
|
| 965 | - 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 966 | - 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 967 | - 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 968 | - 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 969 | - 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 970 | - ]) |
|
| 971 | - ->execute(); |
|
| 972 | - |
|
| 973 | - $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
| 974 | - |
|
| 975 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent( |
|
| 976 | - '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', |
|
| 977 | - [ |
|
| 978 | - 'calendarId' => $calendarId, |
|
| 979 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 980 | - 'shares' => $this->getShares($calendarId), |
|
| 981 | - 'objectData' => $this->getCalendarObject($calendarId, $objectUri), |
|
| 982 | - ] |
|
| 983 | - )); |
|
| 984 | - $this->addChange($calendarId, $objectUri, 1); |
|
| 985 | - |
|
| 986 | - return '"' . $extraData['etag'] . '"'; |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * Updates an existing calendarobject, based on it's uri. |
|
| 991 | - * |
|
| 992 | - * The object uri is only the basename, or filename and not a full path. |
|
| 993 | - * |
|
| 994 | - * It is possible return an etag from this function, which will be used in |
|
| 995 | - * the response to this PUT request. Note that the ETag must be surrounded |
|
| 996 | - * by double-quotes. |
|
| 997 | - * |
|
| 998 | - * However, you should only really return this ETag if you don't mangle the |
|
| 999 | - * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1000 | - * the exact same as this request body, you should omit the ETag. |
|
| 1001 | - * |
|
| 1002 | - * @param mixed $calendarId |
|
| 1003 | - * @param string $objectUri |
|
| 1004 | - * @param string $calendarData |
|
| 1005 | - * @return string |
|
| 1006 | - */ |
|
| 1007 | - function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 1008 | - $extraData = $this->getDenormalizedData($calendarData); |
|
| 1009 | - |
|
| 1010 | - $query = $this->db->getQueryBuilder(); |
|
| 1011 | - $query->update('calendarobjects') |
|
| 1012 | - ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1013 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1014 | - ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1015 | - ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1016 | - ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1017 | - ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1018 | - ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1019 | - ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1020 | - ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1021 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1022 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1023 | - ->execute(); |
|
| 1024 | - |
|
| 1025 | - $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
| 1026 | - |
|
| 1027 | - $data = $this->getCalendarObject($calendarId, $objectUri); |
|
| 1028 | - if (is_array($data)) { |
|
| 1029 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent( |
|
| 1030 | - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', |
|
| 1031 | - [ |
|
| 1032 | - 'calendarId' => $calendarId, |
|
| 1033 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 1034 | - 'shares' => $this->getShares($calendarId), |
|
| 1035 | - 'objectData' => $data, |
|
| 1036 | - ] |
|
| 1037 | - )); |
|
| 1038 | - } |
|
| 1039 | - $this->addChange($calendarId, $objectUri, 2); |
|
| 1040 | - |
|
| 1041 | - return '"' . $extraData['etag'] . '"'; |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - /** |
|
| 1045 | - * @param int $calendarObjectId |
|
| 1046 | - * @param int $classification |
|
| 1047 | - */ |
|
| 1048 | - public function setClassification($calendarObjectId, $classification) { |
|
| 1049 | - if (!in_array($classification, [ |
|
| 1050 | - self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1051 | - ])) { |
|
| 1052 | - throw new \InvalidArgumentException(); |
|
| 1053 | - } |
|
| 1054 | - $query = $this->db->getQueryBuilder(); |
|
| 1055 | - $query->update('calendarobjects') |
|
| 1056 | - ->set('classification', $query->createNamedParameter($classification)) |
|
| 1057 | - ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1058 | - ->execute(); |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - /** |
|
| 1062 | - * Deletes an existing calendar object. |
|
| 1063 | - * |
|
| 1064 | - * The object uri is only the basename, or filename and not a full path. |
|
| 1065 | - * |
|
| 1066 | - * @param mixed $calendarId |
|
| 1067 | - * @param string $objectUri |
|
| 1068 | - * @return void |
|
| 1069 | - */ |
|
| 1070 | - function deleteCalendarObject($calendarId, $objectUri) { |
|
| 1071 | - $data = $this->getCalendarObject($calendarId, $objectUri); |
|
| 1072 | - if (is_array($data)) { |
|
| 1073 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( |
|
| 1074 | - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', |
|
| 1075 | - [ |
|
| 1076 | - 'calendarId' => $calendarId, |
|
| 1077 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 1078 | - 'shares' => $this->getShares($calendarId), |
|
| 1079 | - 'objectData' => $data, |
|
| 1080 | - ] |
|
| 1081 | - )); |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
| 1085 | - $stmt->execute([$calendarId, $objectUri]); |
|
| 1086 | - |
|
| 1087 | - $this->purgeProperties($calendarId, $data['id']); |
|
| 1088 | - |
|
| 1089 | - $this->addChange($calendarId, $objectUri, 3); |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - /** |
|
| 1093 | - * Performs a calendar-query on the contents of this calendar. |
|
| 1094 | - * |
|
| 1095 | - * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1096 | - * calendar-query it is possible for a client to request a specific set of |
|
| 1097 | - * object, based on contents of iCalendar properties, date-ranges and |
|
| 1098 | - * iCalendar component types (VTODO, VEVENT). |
|
| 1099 | - * |
|
| 1100 | - * This method should just return a list of (relative) urls that match this |
|
| 1101 | - * query. |
|
| 1102 | - * |
|
| 1103 | - * The list of filters are specified as an array. The exact array is |
|
| 1104 | - * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1105 | - * |
|
| 1106 | - * Note that it is extremely likely that getCalendarObject for every path |
|
| 1107 | - * returned from this method will be called almost immediately after. You |
|
| 1108 | - * may want to anticipate this to speed up these requests. |
|
| 1109 | - * |
|
| 1110 | - * This method provides a default implementation, which parses *all* the |
|
| 1111 | - * iCalendar objects in the specified calendar. |
|
| 1112 | - * |
|
| 1113 | - * This default may well be good enough for personal use, and calendars |
|
| 1114 | - * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1115 | - * or high loads, you are strongly advised to optimize certain paths. |
|
| 1116 | - * |
|
| 1117 | - * The best way to do so is override this method and to optimize |
|
| 1118 | - * specifically for 'common filters'. |
|
| 1119 | - * |
|
| 1120 | - * Requests that are extremely common are: |
|
| 1121 | - * * requests for just VEVENTS |
|
| 1122 | - * * requests for just VTODO |
|
| 1123 | - * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1124 | - * |
|
| 1125 | - * ..and combinations of these requests. It may not be worth it to try to |
|
| 1126 | - * handle every possible situation and just rely on the (relatively |
|
| 1127 | - * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1128 | - * |
|
| 1129 | - * Note that especially time-range-filters may be difficult to parse. A |
|
| 1130 | - * time-range filter specified on a VEVENT must for instance also handle |
|
| 1131 | - * recurrence rules correctly. |
|
| 1132 | - * A good example of how to interprete all these filters can also simply |
|
| 1133 | - * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1134 | - * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1135 | - * to think of. |
|
| 1136 | - * |
|
| 1137 | - * @param mixed $calendarId |
|
| 1138 | - * @param array $filters |
|
| 1139 | - * @return array |
|
| 1140 | - */ |
|
| 1141 | - function calendarQuery($calendarId, array $filters) { |
|
| 1142 | - $componentType = null; |
|
| 1143 | - $requirePostFilter = true; |
|
| 1144 | - $timeRange = null; |
|
| 1145 | - |
|
| 1146 | - // if no filters were specified, we don't need to filter after a query |
|
| 1147 | - if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1148 | - $requirePostFilter = false; |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - // Figuring out if there's a component filter |
|
| 1152 | - if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1153 | - $componentType = $filters['comp-filters'][0]['name']; |
|
| 1154 | - |
|
| 1155 | - // Checking if we need post-filters |
|
| 1156 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1157 | - $requirePostFilter = false; |
|
| 1158 | - } |
|
| 1159 | - // There was a time-range filter |
|
| 1160 | - if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
| 1161 | - $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1162 | - |
|
| 1163 | - // If start time OR the end time is not specified, we can do a |
|
| 1164 | - // 100% accurate mysql query. |
|
| 1165 | - if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1166 | - $requirePostFilter = false; |
|
| 1167 | - } |
|
| 1168 | - } |
|
| 1169 | - |
|
| 1170 | - } |
|
| 1171 | - $columns = ['uri']; |
|
| 1172 | - if ($requirePostFilter) { |
|
| 1173 | - $columns = ['uri', 'calendardata']; |
|
| 1174 | - } |
|
| 1175 | - $query = $this->db->getQueryBuilder(); |
|
| 1176 | - $query->select($columns) |
|
| 1177 | - ->from('calendarobjects') |
|
| 1178 | - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 1179 | - |
|
| 1180 | - if ($componentType) { |
|
| 1181 | - $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - if ($timeRange && $timeRange['start']) { |
|
| 1185 | - $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1186 | - } |
|
| 1187 | - if ($timeRange && $timeRange['end']) { |
|
| 1188 | - $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1189 | - } |
|
| 1190 | - |
|
| 1191 | - $stmt = $query->execute(); |
|
| 1192 | - |
|
| 1193 | - $result = []; |
|
| 1194 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1195 | - if ($requirePostFilter) { |
|
| 1196 | - if (!$this->validateFilterForObject($row, $filters)) { |
|
| 1197 | - continue; |
|
| 1198 | - } |
|
| 1199 | - } |
|
| 1200 | - $result[] = $row['uri']; |
|
| 1201 | - } |
|
| 1202 | - |
|
| 1203 | - return $result; |
|
| 1204 | - } |
|
| 1205 | - |
|
| 1206 | - /** |
|
| 1207 | - * custom Nextcloud search extension for CalDAV |
|
| 1208 | - * |
|
| 1209 | - * @param string $principalUri |
|
| 1210 | - * @param array $filters |
|
| 1211 | - * @param integer|null $limit |
|
| 1212 | - * @param integer|null $offset |
|
| 1213 | - * @return array |
|
| 1214 | - */ |
|
| 1215 | - public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
| 1216 | - $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1217 | - $ownCalendars = []; |
|
| 1218 | - $sharedCalendars = []; |
|
| 1219 | - |
|
| 1220 | - $uriMapper = []; |
|
| 1221 | - |
|
| 1222 | - foreach($calendars as $calendar) { |
|
| 1223 | - if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1224 | - $ownCalendars[] = $calendar['id']; |
|
| 1225 | - } else { |
|
| 1226 | - $sharedCalendars[] = $calendar['id']; |
|
| 1227 | - } |
|
| 1228 | - $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1229 | - } |
|
| 1230 | - if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1231 | - return []; |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - $query = $this->db->getQueryBuilder(); |
|
| 1235 | - // Calendar id expressions |
|
| 1236 | - $calendarExpressions = []; |
|
| 1237 | - foreach($ownCalendars as $id) { |
|
| 1238 | - $calendarExpressions[] = $query->expr() |
|
| 1239 | - ->eq('c.calendarid', $query->createNamedParameter($id)); |
|
| 1240 | - } |
|
| 1241 | - foreach($sharedCalendars as $id) { |
|
| 1242 | - $calendarExpressions[] = $query->expr()->andX( |
|
| 1243 | - $query->expr()->eq('c.calendarid', |
|
| 1244 | - $query->createNamedParameter($id)), |
|
| 1245 | - $query->expr()->eq('c.classification', |
|
| 1246 | - $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)) |
|
| 1247 | - ); |
|
| 1248 | - } |
|
| 1249 | - |
|
| 1250 | - if (count($calendarExpressions) === 1) { |
|
| 1251 | - $calExpr = $calendarExpressions[0]; |
|
| 1252 | - } else { |
|
| 1253 | - $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1254 | - } |
|
| 1255 | - |
|
| 1256 | - // Component expressions |
|
| 1257 | - $compExpressions = []; |
|
| 1258 | - foreach($filters['comps'] as $comp) { |
|
| 1259 | - $compExpressions[] = $query->expr() |
|
| 1260 | - ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1261 | - } |
|
| 1262 | - |
|
| 1263 | - if (count($compExpressions) === 1) { |
|
| 1264 | - $compExpr = $compExpressions[0]; |
|
| 1265 | - } else { |
|
| 1266 | - $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1267 | - } |
|
| 1268 | - |
|
| 1269 | - if (!isset($filters['props'])) { |
|
| 1270 | - $filters['props'] = []; |
|
| 1271 | - } |
|
| 1272 | - if (!isset($filters['params'])) { |
|
| 1273 | - $filters['params'] = []; |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - $propParamExpressions = []; |
|
| 1277 | - foreach($filters['props'] as $prop) { |
|
| 1278 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1279 | - $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1280 | - $query->expr()->isNull('i.parameter') |
|
| 1281 | - ); |
|
| 1282 | - } |
|
| 1283 | - foreach($filters['params'] as $param) { |
|
| 1284 | - $propParamExpressions[] = $query->expr()->andX( |
|
| 1285 | - $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1286 | - $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1287 | - ); |
|
| 1288 | - } |
|
| 1289 | - |
|
| 1290 | - if (count($propParamExpressions) === 1) { |
|
| 1291 | - $propParamExpr = $propParamExpressions[0]; |
|
| 1292 | - } else { |
|
| 1293 | - $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1294 | - } |
|
| 1295 | - |
|
| 1296 | - $query->select(['c.calendarid', 'c.uri']) |
|
| 1297 | - ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1298 | - ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1299 | - ->where($calExpr) |
|
| 1300 | - ->andWhere($compExpr) |
|
| 1301 | - ->andWhere($propParamExpr) |
|
| 1302 | - ->andWhere($query->expr()->iLike('i.value', |
|
| 1303 | - $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))); |
|
| 1304 | - |
|
| 1305 | - if ($offset) { |
|
| 1306 | - $query->setFirstResult($offset); |
|
| 1307 | - } |
|
| 1308 | - if ($limit) { |
|
| 1309 | - $query->setMaxResults($limit); |
|
| 1310 | - } |
|
| 1311 | - |
|
| 1312 | - $stmt = $query->execute(); |
|
| 1313 | - |
|
| 1314 | - $result = []; |
|
| 1315 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1316 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1317 | - if (!in_array($path, $result)) { |
|
| 1318 | - $result[] = $path; |
|
| 1319 | - } |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - return $result; |
|
| 1323 | - } |
|
| 1324 | - |
|
| 1325 | - /** |
|
| 1326 | - * Searches through all of a users calendars and calendar objects to find |
|
| 1327 | - * an object with a specific UID. |
|
| 1328 | - * |
|
| 1329 | - * This method should return the path to this object, relative to the |
|
| 1330 | - * calendar home, so this path usually only contains two parts: |
|
| 1331 | - * |
|
| 1332 | - * calendarpath/objectpath.ics |
|
| 1333 | - * |
|
| 1334 | - * If the uid is not found, return null. |
|
| 1335 | - * |
|
| 1336 | - * This method should only consider * objects that the principal owns, so |
|
| 1337 | - * any calendars owned by other principals that also appear in this |
|
| 1338 | - * collection should be ignored. |
|
| 1339 | - * |
|
| 1340 | - * @param string $principalUri |
|
| 1341 | - * @param string $uid |
|
| 1342 | - * @return string|null |
|
| 1343 | - */ |
|
| 1344 | - function getCalendarObjectByUID($principalUri, $uid) { |
|
| 1345 | - |
|
| 1346 | - $query = $this->db->getQueryBuilder(); |
|
| 1347 | - $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 1348 | - ->from('calendarobjects', 'co') |
|
| 1349 | - ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 1350 | - ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 1351 | - ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
| 1352 | - |
|
| 1353 | - $stmt = $query->execute(); |
|
| 1354 | - |
|
| 1355 | - if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1356 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 1357 | - } |
|
| 1358 | - |
|
| 1359 | - return null; |
|
| 1360 | - } |
|
| 1361 | - |
|
| 1362 | - /** |
|
| 1363 | - * The getChanges method returns all the changes that have happened, since |
|
| 1364 | - * the specified syncToken in the specified calendar. |
|
| 1365 | - * |
|
| 1366 | - * This function should return an array, such as the following: |
|
| 1367 | - * |
|
| 1368 | - * [ |
|
| 1369 | - * 'syncToken' => 'The current synctoken', |
|
| 1370 | - * 'added' => [ |
|
| 1371 | - * 'new.txt', |
|
| 1372 | - * ], |
|
| 1373 | - * 'modified' => [ |
|
| 1374 | - * 'modified.txt', |
|
| 1375 | - * ], |
|
| 1376 | - * 'deleted' => [ |
|
| 1377 | - * 'foo.php.bak', |
|
| 1378 | - * 'old.txt' |
|
| 1379 | - * ] |
|
| 1380 | - * ); |
|
| 1381 | - * |
|
| 1382 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 1383 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 1384 | - * property This is * needed here too, to ensure the operation is atomic. |
|
| 1385 | - * |
|
| 1386 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 1387 | - * sync, and all members should be reported. |
|
| 1388 | - * |
|
| 1389 | - * The modified property is an array of nodenames that have changed since |
|
| 1390 | - * the last token. |
|
| 1391 | - * |
|
| 1392 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 1393 | - * from collection. |
|
| 1394 | - * |
|
| 1395 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 1396 | - * 1, you only have to report changes that happened only directly in |
|
| 1397 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 1398 | - * the nodes below the child collections. (grandchildren) |
|
| 1399 | - * |
|
| 1400 | - * The $limit argument allows a client to specify how many results should |
|
| 1401 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 1402 | - * as infinite. |
|
| 1403 | - * |
|
| 1404 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 1405 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 1406 | - * |
|
| 1407 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 1408 | - * return null. |
|
| 1409 | - * |
|
| 1410 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 1411 | - * |
|
| 1412 | - * @param string $calendarId |
|
| 1413 | - * @param string $syncToken |
|
| 1414 | - * @param int $syncLevel |
|
| 1415 | - * @param int $limit |
|
| 1416 | - * @return array |
|
| 1417 | - */ |
|
| 1418 | - function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 1419 | - // Current synctoken |
|
| 1420 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1421 | - $stmt->execute([ $calendarId ]); |
|
| 1422 | - $currentToken = $stmt->fetchColumn(0); |
|
| 1423 | - |
|
| 1424 | - if (is_null($currentToken)) { |
|
| 1425 | - return null; |
|
| 1426 | - } |
|
| 1427 | - |
|
| 1428 | - $result = [ |
|
| 1429 | - 'syncToken' => $currentToken, |
|
| 1430 | - 'added' => [], |
|
| 1431 | - 'modified' => [], |
|
| 1432 | - 'deleted' => [], |
|
| 1433 | - ]; |
|
| 1434 | - |
|
| 1435 | - if ($syncToken) { |
|
| 1436 | - |
|
| 1437 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
| 1438 | - if ($limit>0) { |
|
| 1439 | - $query.= " `LIMIT` " . (int)$limit; |
|
| 1440 | - } |
|
| 1441 | - |
|
| 1442 | - // Fetching all changes |
|
| 1443 | - $stmt = $this->db->prepare($query); |
|
| 1444 | - $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 1445 | - |
|
| 1446 | - $changes = []; |
|
| 1447 | - |
|
| 1448 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 1449 | - // last change on a node is relevant. |
|
| 1450 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1451 | - |
|
| 1452 | - $changes[$row['uri']] = $row['operation']; |
|
| 1453 | - |
|
| 1454 | - } |
|
| 1455 | - |
|
| 1456 | - foreach($changes as $uri => $operation) { |
|
| 1457 | - |
|
| 1458 | - switch($operation) { |
|
| 1459 | - case 1 : |
|
| 1460 | - $result['added'][] = $uri; |
|
| 1461 | - break; |
|
| 1462 | - case 2 : |
|
| 1463 | - $result['modified'][] = $uri; |
|
| 1464 | - break; |
|
| 1465 | - case 3 : |
|
| 1466 | - $result['deleted'][] = $uri; |
|
| 1467 | - break; |
|
| 1468 | - } |
|
| 1469 | - |
|
| 1470 | - } |
|
| 1471 | - } else { |
|
| 1472 | - // No synctoken supplied, this is the initial sync. |
|
| 1473 | - $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
| 1474 | - $stmt = $this->db->prepare($query); |
|
| 1475 | - $stmt->execute([$calendarId]); |
|
| 1476 | - |
|
| 1477 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 1478 | - } |
|
| 1479 | - return $result; |
|
| 1480 | - |
|
| 1481 | - } |
|
| 1482 | - |
|
| 1483 | - /** |
|
| 1484 | - * Returns a list of subscriptions for a principal. |
|
| 1485 | - * |
|
| 1486 | - * Every subscription is an array with the following keys: |
|
| 1487 | - * * id, a unique id that will be used by other functions to modify the |
|
| 1488 | - * subscription. This can be the same as the uri or a database key. |
|
| 1489 | - * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 1490 | - * * principaluri. The owner of the subscription. Almost always the same as |
|
| 1491 | - * principalUri passed to this method. |
|
| 1492 | - * |
|
| 1493 | - * Furthermore, all the subscription info must be returned too: |
|
| 1494 | - * |
|
| 1495 | - * 1. {DAV:}displayname |
|
| 1496 | - * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 1497 | - * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 1498 | - * should not be stripped). |
|
| 1499 | - * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 1500 | - * should not be stripped). |
|
| 1501 | - * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 1502 | - * attachments should not be stripped). |
|
| 1503 | - * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 1504 | - * Sabre\DAV\Property\Href). |
|
| 1505 | - * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 1506 | - * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 1507 | - * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 1508 | - * (should just be an instance of |
|
| 1509 | - * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 1510 | - * default components). |
|
| 1511 | - * |
|
| 1512 | - * @param string $principalUri |
|
| 1513 | - * @return array |
|
| 1514 | - */ |
|
| 1515 | - function getSubscriptionsForUser($principalUri) { |
|
| 1516 | - $fields = array_values($this->subscriptionPropertyMap); |
|
| 1517 | - $fields[] = 'id'; |
|
| 1518 | - $fields[] = 'uri'; |
|
| 1519 | - $fields[] = 'source'; |
|
| 1520 | - $fields[] = 'principaluri'; |
|
| 1521 | - $fields[] = 'lastmodified'; |
|
| 1522 | - |
|
| 1523 | - $query = $this->db->getQueryBuilder(); |
|
| 1524 | - $query->select($fields) |
|
| 1525 | - ->from('calendarsubscriptions') |
|
| 1526 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1527 | - ->orderBy('calendarorder', 'asc'); |
|
| 1528 | - $stmt =$query->execute(); |
|
| 1529 | - |
|
| 1530 | - $subscriptions = []; |
|
| 1531 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1532 | - |
|
| 1533 | - $subscription = [ |
|
| 1534 | - 'id' => $row['id'], |
|
| 1535 | - 'uri' => $row['uri'], |
|
| 1536 | - 'principaluri' => $row['principaluri'], |
|
| 1537 | - 'source' => $row['source'], |
|
| 1538 | - 'lastmodified' => $row['lastmodified'], |
|
| 1539 | - |
|
| 1540 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1541 | - ]; |
|
| 1542 | - |
|
| 1543 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1544 | - if (!is_null($row[$dbName])) { |
|
| 1545 | - $subscription[$xmlName] = $row[$dbName]; |
|
| 1546 | - } |
|
| 1547 | - } |
|
| 1548 | - |
|
| 1549 | - $subscriptions[] = $subscription; |
|
| 1550 | - |
|
| 1551 | - } |
|
| 1552 | - |
|
| 1553 | - return $subscriptions; |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - /** |
|
| 1557 | - * Creates a new subscription for a principal. |
|
| 1558 | - * |
|
| 1559 | - * If the creation was a success, an id must be returned that can be used to reference |
|
| 1560 | - * this subscription in other methods, such as updateSubscription. |
|
| 1561 | - * |
|
| 1562 | - * @param string $principalUri |
|
| 1563 | - * @param string $uri |
|
| 1564 | - * @param array $properties |
|
| 1565 | - * @return mixed |
|
| 1566 | - */ |
|
| 1567 | - function createSubscription($principalUri, $uri, array $properties) { |
|
| 1568 | - |
|
| 1569 | - if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 1570 | - throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 1571 | - } |
|
| 1572 | - |
|
| 1573 | - $values = [ |
|
| 1574 | - 'principaluri' => $principalUri, |
|
| 1575 | - 'uri' => $uri, |
|
| 1576 | - 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 1577 | - 'lastmodified' => time(), |
|
| 1578 | - ]; |
|
| 1579 | - |
|
| 1580 | - $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 1581 | - |
|
| 1582 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1583 | - if (array_key_exists($xmlName, $properties)) { |
|
| 1584 | - $values[$dbName] = $properties[$xmlName]; |
|
| 1585 | - if (in_array($dbName, $propertiesBoolean)) { |
|
| 1586 | - $values[$dbName] = true; |
|
| 1587 | - } |
|
| 1588 | - } |
|
| 1589 | - } |
|
| 1590 | - |
|
| 1591 | - $valuesToInsert = array(); |
|
| 1592 | - |
|
| 1593 | - $query = $this->db->getQueryBuilder(); |
|
| 1594 | - |
|
| 1595 | - foreach (array_keys($values) as $name) { |
|
| 1596 | - $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 1597 | - } |
|
| 1598 | - |
|
| 1599 | - $query->insert('calendarsubscriptions') |
|
| 1600 | - ->values($valuesToInsert) |
|
| 1601 | - ->execute(); |
|
| 1602 | - |
|
| 1603 | - return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
| 1604 | - } |
|
| 1605 | - |
|
| 1606 | - /** |
|
| 1607 | - * Updates a subscription |
|
| 1608 | - * |
|
| 1609 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 1610 | - * To do the actual updates, you must tell this object which properties |
|
| 1611 | - * you're going to process with the handle() method. |
|
| 1612 | - * |
|
| 1613 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 1614 | - * promise I can handle updating this property". |
|
| 1615 | - * |
|
| 1616 | - * Read the PropPatch documentation for more info and examples. |
|
| 1617 | - * |
|
| 1618 | - * @param mixed $subscriptionId |
|
| 1619 | - * @param PropPatch $propPatch |
|
| 1620 | - * @return void |
|
| 1621 | - */ |
|
| 1622 | - function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 1623 | - $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 1624 | - $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 1625 | - |
|
| 1626 | - /** |
|
| 1627 | - * @suppress SqlInjectionChecker |
|
| 1628 | - */ |
|
| 1629 | - $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 1630 | - |
|
| 1631 | - $newValues = []; |
|
| 1632 | - |
|
| 1633 | - foreach($mutations as $propertyName=>$propertyValue) { |
|
| 1634 | - if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 1635 | - $newValues['source'] = $propertyValue->getHref(); |
|
| 1636 | - } else { |
|
| 1637 | - $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
| 1638 | - $newValues[$fieldName] = $propertyValue; |
|
| 1639 | - } |
|
| 1640 | - } |
|
| 1641 | - |
|
| 1642 | - $query = $this->db->getQueryBuilder(); |
|
| 1643 | - $query->update('calendarsubscriptions') |
|
| 1644 | - ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 1645 | - foreach($newValues as $fieldName=>$value) { |
|
| 1646 | - $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 1647 | - } |
|
| 1648 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1649 | - ->execute(); |
|
| 1650 | - |
|
| 1651 | - return true; |
|
| 1652 | - |
|
| 1653 | - }); |
|
| 1654 | - } |
|
| 1655 | - |
|
| 1656 | - /** |
|
| 1657 | - * Deletes a subscription. |
|
| 1658 | - * |
|
| 1659 | - * @param mixed $subscriptionId |
|
| 1660 | - * @return void |
|
| 1661 | - */ |
|
| 1662 | - function deleteSubscription($subscriptionId) { |
|
| 1663 | - $query = $this->db->getQueryBuilder(); |
|
| 1664 | - $query->delete('calendarsubscriptions') |
|
| 1665 | - ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1666 | - ->execute(); |
|
| 1667 | - } |
|
| 1668 | - |
|
| 1669 | - /** |
|
| 1670 | - * Returns a single scheduling object for the inbox collection. |
|
| 1671 | - * |
|
| 1672 | - * The returned array should contain the following elements: |
|
| 1673 | - * * uri - A unique basename for the object. This will be used to |
|
| 1674 | - * construct a full uri. |
|
| 1675 | - * * calendardata - The iCalendar object |
|
| 1676 | - * * lastmodified - The last modification date. Can be an int for a unix |
|
| 1677 | - * timestamp, or a PHP DateTime object. |
|
| 1678 | - * * etag - A unique token that must change if the object changed. |
|
| 1679 | - * * size - The size of the object, in bytes. |
|
| 1680 | - * |
|
| 1681 | - * @param string $principalUri |
|
| 1682 | - * @param string $objectUri |
|
| 1683 | - * @return array |
|
| 1684 | - */ |
|
| 1685 | - function getSchedulingObject($principalUri, $objectUri) { |
|
| 1686 | - $query = $this->db->getQueryBuilder(); |
|
| 1687 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1688 | - ->from('schedulingobjects') |
|
| 1689 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1690 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1691 | - ->execute(); |
|
| 1692 | - |
|
| 1693 | - $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 1694 | - |
|
| 1695 | - if(!$row) { |
|
| 1696 | - return null; |
|
| 1697 | - } |
|
| 1698 | - |
|
| 1699 | - return [ |
|
| 1700 | - 'uri' => $row['uri'], |
|
| 1701 | - 'calendardata' => $row['calendardata'], |
|
| 1702 | - 'lastmodified' => $row['lastmodified'], |
|
| 1703 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1704 | - 'size' => (int)$row['size'], |
|
| 1705 | - ]; |
|
| 1706 | - } |
|
| 1707 | - |
|
| 1708 | - /** |
|
| 1709 | - * Returns all scheduling objects for the inbox collection. |
|
| 1710 | - * |
|
| 1711 | - * These objects should be returned as an array. Every item in the array |
|
| 1712 | - * should follow the same structure as returned from getSchedulingObject. |
|
| 1713 | - * |
|
| 1714 | - * The main difference is that 'calendardata' is optional. |
|
| 1715 | - * |
|
| 1716 | - * @param string $principalUri |
|
| 1717 | - * @return array |
|
| 1718 | - */ |
|
| 1719 | - function getSchedulingObjects($principalUri) { |
|
| 1720 | - $query = $this->db->getQueryBuilder(); |
|
| 1721 | - $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1722 | - ->from('schedulingobjects') |
|
| 1723 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1724 | - ->execute(); |
|
| 1725 | - |
|
| 1726 | - $result = []; |
|
| 1727 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1728 | - $result[] = [ |
|
| 1729 | - 'calendardata' => $row['calendardata'], |
|
| 1730 | - 'uri' => $row['uri'], |
|
| 1731 | - 'lastmodified' => $row['lastmodified'], |
|
| 1732 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1733 | - 'size' => (int)$row['size'], |
|
| 1734 | - ]; |
|
| 1735 | - } |
|
| 1736 | - |
|
| 1737 | - return $result; |
|
| 1738 | - } |
|
| 1739 | - |
|
| 1740 | - /** |
|
| 1741 | - * Deletes a scheduling object from the inbox collection. |
|
| 1742 | - * |
|
| 1743 | - * @param string $principalUri |
|
| 1744 | - * @param string $objectUri |
|
| 1745 | - * @return void |
|
| 1746 | - */ |
|
| 1747 | - function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 1748 | - $query = $this->db->getQueryBuilder(); |
|
| 1749 | - $query->delete('schedulingobjects') |
|
| 1750 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1751 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1752 | - ->execute(); |
|
| 1753 | - } |
|
| 1754 | - |
|
| 1755 | - /** |
|
| 1756 | - * Creates a new scheduling object. This should land in a users' inbox. |
|
| 1757 | - * |
|
| 1758 | - * @param string $principalUri |
|
| 1759 | - * @param string $objectUri |
|
| 1760 | - * @param string $objectData |
|
| 1761 | - * @return void |
|
| 1762 | - */ |
|
| 1763 | - function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 1764 | - $query = $this->db->getQueryBuilder(); |
|
| 1765 | - $query->insert('schedulingobjects') |
|
| 1766 | - ->values([ |
|
| 1767 | - 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 1768 | - 'calendardata' => $query->createNamedParameter($objectData), |
|
| 1769 | - 'uri' => $query->createNamedParameter($objectUri), |
|
| 1770 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1771 | - 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 1772 | - 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 1773 | - ]) |
|
| 1774 | - ->execute(); |
|
| 1775 | - } |
|
| 1776 | - |
|
| 1777 | - /** |
|
| 1778 | - * Adds a change record to the calendarchanges table. |
|
| 1779 | - * |
|
| 1780 | - * @param mixed $calendarId |
|
| 1781 | - * @param string $objectUri |
|
| 1782 | - * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 1783 | - * @return void |
|
| 1784 | - */ |
|
| 1785 | - protected function addChange($calendarId, $objectUri, $operation) { |
|
| 1786 | - |
|
| 1787 | - $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1788 | - $stmt->execute([ |
|
| 1789 | - $objectUri, |
|
| 1790 | - $calendarId, |
|
| 1791 | - $operation, |
|
| 1792 | - $calendarId |
|
| 1793 | - ]); |
|
| 1794 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 1795 | - $stmt->execute([ |
|
| 1796 | - $calendarId |
|
| 1797 | - ]); |
|
| 1798 | - |
|
| 1799 | - } |
|
| 1800 | - |
|
| 1801 | - /** |
|
| 1802 | - * Parses some information from calendar objects, used for optimized |
|
| 1803 | - * calendar-queries. |
|
| 1804 | - * |
|
| 1805 | - * Returns an array with the following keys: |
|
| 1806 | - * * etag - An md5 checksum of the object without the quotes. |
|
| 1807 | - * * size - Size of the object in bytes |
|
| 1808 | - * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 1809 | - * * firstOccurence |
|
| 1810 | - * * lastOccurence |
|
| 1811 | - * * uid - value of the UID property |
|
| 1812 | - * |
|
| 1813 | - * @param string $calendarData |
|
| 1814 | - * @return array |
|
| 1815 | - */ |
|
| 1816 | - public function getDenormalizedData($calendarData) { |
|
| 1817 | - |
|
| 1818 | - $vObject = Reader::read($calendarData); |
|
| 1819 | - $componentType = null; |
|
| 1820 | - $component = null; |
|
| 1821 | - $firstOccurrence = null; |
|
| 1822 | - $lastOccurrence = null; |
|
| 1823 | - $uid = null; |
|
| 1824 | - $classification = self::CLASSIFICATION_PUBLIC; |
|
| 1825 | - foreach($vObject->getComponents() as $component) { |
|
| 1826 | - if ($component->name!=='VTIMEZONE') { |
|
| 1827 | - $componentType = $component->name; |
|
| 1828 | - $uid = (string)$component->UID; |
|
| 1829 | - break; |
|
| 1830 | - } |
|
| 1831 | - } |
|
| 1832 | - if (!$componentType) { |
|
| 1833 | - throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 1834 | - } |
|
| 1835 | - if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
| 1836 | - $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 1837 | - // Finding the last occurrence is a bit harder |
|
| 1838 | - if (!isset($component->RRULE)) { |
|
| 1839 | - if (isset($component->DTEND)) { |
|
| 1840 | - $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 1841 | - } elseif (isset($component->DURATION)) { |
|
| 1842 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1843 | - $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 1844 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1845 | - } elseif (!$component->DTSTART->hasTime()) { |
|
| 1846 | - $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1847 | - $endDate->modify('+1 day'); |
|
| 1848 | - $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1849 | - } else { |
|
| 1850 | - $lastOccurrence = $firstOccurrence; |
|
| 1851 | - } |
|
| 1852 | - } else { |
|
| 1853 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
| 1854 | - $maxDate = new \DateTime(self::MAX_DATE); |
|
| 1855 | - if ($it->isInfinite()) { |
|
| 1856 | - $lastOccurrence = $maxDate->getTimestamp(); |
|
| 1857 | - } else { |
|
| 1858 | - $end = $it->getDtEnd(); |
|
| 1859 | - while($it->valid() && $end < $maxDate) { |
|
| 1860 | - $end = $it->getDtEnd(); |
|
| 1861 | - $it->next(); |
|
| 1862 | - |
|
| 1863 | - } |
|
| 1864 | - $lastOccurrence = $end->getTimestamp(); |
|
| 1865 | - } |
|
| 1866 | - |
|
| 1867 | - } |
|
| 1868 | - } |
|
| 1869 | - |
|
| 1870 | - if ($component->CLASS) { |
|
| 1871 | - $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 1872 | - switch ($component->CLASS->getValue()) { |
|
| 1873 | - case 'PUBLIC': |
|
| 1874 | - $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 1875 | - break; |
|
| 1876 | - case 'CONFIDENTIAL': |
|
| 1877 | - $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 1878 | - break; |
|
| 1879 | - } |
|
| 1880 | - } |
|
| 1881 | - return [ |
|
| 1882 | - 'etag' => md5($calendarData), |
|
| 1883 | - 'size' => strlen($calendarData), |
|
| 1884 | - 'componentType' => $componentType, |
|
| 1885 | - 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 1886 | - 'lastOccurence' => $lastOccurrence, |
|
| 1887 | - 'uid' => $uid, |
|
| 1888 | - 'classification' => $classification |
|
| 1889 | - ]; |
|
| 1890 | - |
|
| 1891 | - } |
|
| 1892 | - |
|
| 1893 | - private function readBlob($cardData) { |
|
| 1894 | - if (is_resource($cardData)) { |
|
| 1895 | - return stream_get_contents($cardData); |
|
| 1896 | - } |
|
| 1897 | - |
|
| 1898 | - return $cardData; |
|
| 1899 | - } |
|
| 1900 | - |
|
| 1901 | - /** |
|
| 1902 | - * @param IShareable $shareable |
|
| 1903 | - * @param array $add |
|
| 1904 | - * @param array $remove |
|
| 1905 | - */ |
|
| 1906 | - public function updateShares($shareable, $add, $remove) { |
|
| 1907 | - $calendarId = $shareable->getResourceId(); |
|
| 1908 | - $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent( |
|
| 1909 | - '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
| 1910 | - [ |
|
| 1911 | - 'calendarId' => $calendarId, |
|
| 1912 | - 'calendarData' => $this->getCalendarById($calendarId), |
|
| 1913 | - 'shares' => $this->getShares($calendarId), |
|
| 1914 | - 'add' => $add, |
|
| 1915 | - 'remove' => $remove, |
|
| 1916 | - ])); |
|
| 1917 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 1918 | - } |
|
| 1919 | - |
|
| 1920 | - /** |
|
| 1921 | - * @param int $resourceId |
|
| 1922 | - * @return array |
|
| 1923 | - */ |
|
| 1924 | - public function getShares($resourceId) { |
|
| 1925 | - return $this->sharingBackend->getShares($resourceId); |
|
| 1926 | - } |
|
| 1927 | - |
|
| 1928 | - /** |
|
| 1929 | - * @param boolean $value |
|
| 1930 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 1931 | - * @return string|null |
|
| 1932 | - */ |
|
| 1933 | - public function setPublishStatus($value, $calendar) { |
|
| 1934 | - $query = $this->db->getQueryBuilder(); |
|
| 1935 | - if ($value) { |
|
| 1936 | - $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 1937 | - $query->insert('dav_shares') |
|
| 1938 | - ->values([ |
|
| 1939 | - 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 1940 | - 'type' => $query->createNamedParameter('calendar'), |
|
| 1941 | - 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 1942 | - 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 1943 | - 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 1944 | - ]); |
|
| 1945 | - $query->execute(); |
|
| 1946 | - return $publicUri; |
|
| 1947 | - } |
|
| 1948 | - $query->delete('dav_shares') |
|
| 1949 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 1950 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 1951 | - $query->execute(); |
|
| 1952 | - return null; |
|
| 1953 | - } |
|
| 1954 | - |
|
| 1955 | - /** |
|
| 1956 | - * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 1957 | - * @return mixed |
|
| 1958 | - */ |
|
| 1959 | - public function getPublishStatus($calendar) { |
|
| 1960 | - $query = $this->db->getQueryBuilder(); |
|
| 1961 | - $result = $query->select('publicuri') |
|
| 1962 | - ->from('dav_shares') |
|
| 1963 | - ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 1964 | - ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 1965 | - ->execute(); |
|
| 1966 | - |
|
| 1967 | - $row = $result->fetch(); |
|
| 1968 | - $result->closeCursor(); |
|
| 1969 | - return $row ? reset($row) : false; |
|
| 1970 | - } |
|
| 1971 | - |
|
| 1972 | - /** |
|
| 1973 | - * @param int $resourceId |
|
| 1974 | - * @param array $acl |
|
| 1975 | - * @return array |
|
| 1976 | - */ |
|
| 1977 | - public function applyShareAcl($resourceId, $acl) { |
|
| 1978 | - return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
| 1979 | - } |
|
| 1980 | - |
|
| 1981 | - |
|
| 1982 | - |
|
| 1983 | - /** |
|
| 1984 | - * update properties table |
|
| 1985 | - * |
|
| 1986 | - * @param int $calendarId |
|
| 1987 | - * @param string $objectUri |
|
| 1988 | - * @param string $calendarData |
|
| 1989 | - */ |
|
| 1990 | - public function updateProperties($calendarId, $objectUri, $calendarData) { |
|
| 1991 | - $objectId = $this->getCalendarObjectId($calendarId, $objectUri); |
|
| 1992 | - |
|
| 1993 | - try { |
|
| 1994 | - $vCalendar = $this->readCalendarData($calendarData); |
|
| 1995 | - } catch (\Exception $ex) { |
|
| 1996 | - return; |
|
| 1997 | - } |
|
| 1998 | - |
|
| 1999 | - $this->purgeProperties($calendarId, $objectId); |
|
| 2000 | - |
|
| 2001 | - $query = $this->db->getQueryBuilder(); |
|
| 2002 | - $query->insert($this->dbObjectPropertiesTable) |
|
| 2003 | - ->values( |
|
| 2004 | - [ |
|
| 2005 | - 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2006 | - 'objectid' => $query->createNamedParameter($objectId), |
|
| 2007 | - 'name' => $query->createParameter('name'), |
|
| 2008 | - 'parameter' => $query->createParameter('parameter'), |
|
| 2009 | - 'value' => $query->createParameter('value'), |
|
| 2010 | - ] |
|
| 2011 | - ); |
|
| 2012 | - |
|
| 2013 | - $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 2014 | - foreach ($vCalendar->getComponents() as $component) { |
|
| 2015 | - if (!in_array($component->name, $indexComponents)) { |
|
| 2016 | - continue; |
|
| 2017 | - } |
|
| 2018 | - |
|
| 2019 | - foreach ($component->children() as $property) { |
|
| 2020 | - if (in_array($property->name, self::$indexProperties)) { |
|
| 2021 | - $value = $property->getValue(); |
|
| 2022 | - // is this a shitty db? |
|
| 2023 | - if (!$this->db->supports4ByteText()) { |
|
| 2024 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2025 | - } |
|
| 2026 | - $value = substr($value, 0, 254); |
|
| 2027 | - |
|
| 2028 | - $query->setParameter('name', $property->name); |
|
| 2029 | - $query->setParameter('parameter', null); |
|
| 2030 | - $query->setParameter('value', $value); |
|
| 2031 | - $query->execute(); |
|
| 2032 | - } |
|
| 2033 | - |
|
| 2034 | - if (in_array($property->name, array_keys(self::$indexParameters))) { |
|
| 2035 | - $parameters = $property->parameters(); |
|
| 2036 | - $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 2037 | - |
|
| 2038 | - foreach ($parameters as $key => $value) { |
|
| 2039 | - if (in_array($key, $indexedParametersForProperty)) { |
|
| 2040 | - // is this a shitty db? |
|
| 2041 | - if ($this->db->supports4ByteText()) { |
|
| 2042 | - $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2043 | - } |
|
| 2044 | - $value = substr($value, 0, 254); |
|
| 2045 | - |
|
| 2046 | - $query->setParameter('name', $property->name); |
|
| 2047 | - $query->setParameter('parameter', substr($key, 0, 254)); |
|
| 2048 | - $query->setParameter('value', substr($value, 0, 254)); |
|
| 2049 | - $query->execute(); |
|
| 2050 | - } |
|
| 2051 | - } |
|
| 2052 | - } |
|
| 2053 | - } |
|
| 2054 | - } |
|
| 2055 | - } |
|
| 2056 | - |
|
| 2057 | - /** |
|
| 2058 | - * read VCalendar data into a VCalendar object |
|
| 2059 | - * |
|
| 2060 | - * @param string $objectData |
|
| 2061 | - * @return VCalendar |
|
| 2062 | - */ |
|
| 2063 | - protected function readCalendarData($objectData) { |
|
| 2064 | - return Reader::read($objectData); |
|
| 2065 | - } |
|
| 2066 | - |
|
| 2067 | - /** |
|
| 2068 | - * delete all properties from a given calendar object |
|
| 2069 | - * |
|
| 2070 | - * @param int $calendarId |
|
| 2071 | - * @param int $objectId |
|
| 2072 | - */ |
|
| 2073 | - protected function purgeProperties($calendarId, $objectId) { |
|
| 2074 | - $query = $this->db->getQueryBuilder(); |
|
| 2075 | - $query->delete($this->dbObjectPropertiesTable) |
|
| 2076 | - ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 2077 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 2078 | - $query->execute(); |
|
| 2079 | - } |
|
| 2080 | - |
|
| 2081 | - /** |
|
| 2082 | - * get ID from a given calendar object |
|
| 2083 | - * |
|
| 2084 | - * @param int $calendarId |
|
| 2085 | - * @param string $uri |
|
| 2086 | - * @return int |
|
| 2087 | - */ |
|
| 2088 | - protected function getCalendarObjectId($calendarId, $uri) { |
|
| 2089 | - $query = $this->db->getQueryBuilder(); |
|
| 2090 | - $query->select('id')->from('calendarobjects') |
|
| 2091 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 2092 | - ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 2093 | - |
|
| 2094 | - $result = $query->execute(); |
|
| 2095 | - $objectIds = $result->fetch(); |
|
| 2096 | - $result->closeCursor(); |
|
| 2097 | - |
|
| 2098 | - if (!isset($objectIds['id'])) { |
|
| 2099 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 2100 | - } |
|
| 2101 | - |
|
| 2102 | - return (int)$objectIds['id']; |
|
| 2103 | - } |
|
| 2104 | - |
|
| 2105 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 2106 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 2107 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
| 2108 | - if ($toV2 === true) { |
|
| 2109 | - return "principals/users/$name"; |
|
| 2110 | - } |
|
| 2111 | - return "principals/$name"; |
|
| 2112 | - } |
|
| 2113 | - return $principalUri; |
|
| 2114 | - } |
|
| 2115 | - |
|
| 2116 | - private function addOwnerPrincipal(&$calendarInfo) { |
|
| 2117 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 2118 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 2119 | - if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 2120 | - $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 2121 | - } else { |
|
| 2122 | - $uri = $calendarInfo['principaluri']; |
|
| 2123 | - } |
|
| 2124 | - |
|
| 2125 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 2126 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 2127 | - $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 2128 | - } |
|
| 2129 | - } |
|
| 412 | + /** |
|
| 413 | + * @return array |
|
| 414 | + */ |
|
| 415 | + public function getPublicCalendars() { |
|
| 416 | + $fields = array_values($this->propertyMap); |
|
| 417 | + $fields[] = 'a.id'; |
|
| 418 | + $fields[] = 'a.uri'; |
|
| 419 | + $fields[] = 'a.synctoken'; |
|
| 420 | + $fields[] = 'a.components'; |
|
| 421 | + $fields[] = 'a.principaluri'; |
|
| 422 | + $fields[] = 'a.transparent'; |
|
| 423 | + $fields[] = 's.access'; |
|
| 424 | + $fields[] = 's.publicuri'; |
|
| 425 | + $calendars = []; |
|
| 426 | + $query = $this->db->getQueryBuilder(); |
|
| 427 | + $result = $query->select($fields) |
|
| 428 | + ->from('dav_shares', 's') |
|
| 429 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 430 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 431 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 432 | + ->execute(); |
|
| 433 | + |
|
| 434 | + while($row = $result->fetch()) { |
|
| 435 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 436 | + $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 437 | + $components = []; |
|
| 438 | + if ($row['components']) { |
|
| 439 | + $components = explode(',',$row['components']); |
|
| 440 | + } |
|
| 441 | + $calendar = [ |
|
| 442 | + 'id' => $row['id'], |
|
| 443 | + 'uri' => $row['publicuri'], |
|
| 444 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 445 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 446 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 447 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 448 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 449 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 450 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 451 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 452 | + ]; |
|
| 453 | + |
|
| 454 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 455 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + $this->addOwnerPrincipal($calendar); |
|
| 459 | + |
|
| 460 | + if (!isset($calendars[$calendar['id']])) { |
|
| 461 | + $calendars[$calendar['id']] = $calendar; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + $result->closeCursor(); |
|
| 465 | + |
|
| 466 | + return array_values($calendars); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * @param string $uri |
|
| 471 | + * @return array |
|
| 472 | + * @throws NotFound |
|
| 473 | + */ |
|
| 474 | + public function getPublicCalendar($uri) { |
|
| 475 | + $fields = array_values($this->propertyMap); |
|
| 476 | + $fields[] = 'a.id'; |
|
| 477 | + $fields[] = 'a.uri'; |
|
| 478 | + $fields[] = 'a.synctoken'; |
|
| 479 | + $fields[] = 'a.components'; |
|
| 480 | + $fields[] = 'a.principaluri'; |
|
| 481 | + $fields[] = 'a.transparent'; |
|
| 482 | + $fields[] = 's.access'; |
|
| 483 | + $fields[] = 's.publicuri'; |
|
| 484 | + $query = $this->db->getQueryBuilder(); |
|
| 485 | + $result = $query->select($fields) |
|
| 486 | + ->from('dav_shares', 's') |
|
| 487 | + ->join('s', 'calendars', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 488 | + ->where($query->expr()->in('s.access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 489 | + ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
|
| 490 | + ->andWhere($query->expr()->eq('s.publicuri', $query->createNamedParameter($uri))) |
|
| 491 | + ->execute(); |
|
| 492 | + |
|
| 493 | + $row = $result->fetch(\PDO::FETCH_ASSOC); |
|
| 494 | + |
|
| 495 | + $result->closeCursor(); |
|
| 496 | + |
|
| 497 | + if ($row === false) { |
|
| 498 | + throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 502 | + $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 503 | + $components = []; |
|
| 504 | + if ($row['components']) { |
|
| 505 | + $components = explode(',',$row['components']); |
|
| 506 | + } |
|
| 507 | + $calendar = [ |
|
| 508 | + 'id' => $row['id'], |
|
| 509 | + 'uri' => $row['publicuri'], |
|
| 510 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 511 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 512 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 513 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 514 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 515 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 516 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 517 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 518 | + ]; |
|
| 519 | + |
|
| 520 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 521 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $this->addOwnerPrincipal($calendar); |
|
| 525 | + |
|
| 526 | + return $calendar; |
|
| 527 | + |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * @param string $principal |
|
| 532 | + * @param string $uri |
|
| 533 | + * @return array|null |
|
| 534 | + */ |
|
| 535 | + public function getCalendarByUri($principal, $uri) { |
|
| 536 | + $fields = array_values($this->propertyMap); |
|
| 537 | + $fields[] = 'id'; |
|
| 538 | + $fields[] = 'uri'; |
|
| 539 | + $fields[] = 'synctoken'; |
|
| 540 | + $fields[] = 'components'; |
|
| 541 | + $fields[] = 'principaluri'; |
|
| 542 | + $fields[] = 'transparent'; |
|
| 543 | + |
|
| 544 | + // Making fields a comma-delimited list |
|
| 545 | + $query = $this->db->getQueryBuilder(); |
|
| 546 | + $query->select($fields)->from('calendars') |
|
| 547 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 548 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 549 | + ->setMaxResults(1); |
|
| 550 | + $stmt = $query->execute(); |
|
| 551 | + |
|
| 552 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 553 | + $stmt->closeCursor(); |
|
| 554 | + if ($row === false) { |
|
| 555 | + return null; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + $components = []; |
|
| 559 | + if ($row['components']) { |
|
| 560 | + $components = explode(',',$row['components']); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + $calendar = [ |
|
| 564 | + 'id' => $row['id'], |
|
| 565 | + 'uri' => $row['uri'], |
|
| 566 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 567 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 568 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 569 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 570 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 571 | + ]; |
|
| 572 | + |
|
| 573 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 574 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + $this->addOwnerPrincipal($calendar); |
|
| 578 | + |
|
| 579 | + return $calendar; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + public function getCalendarById($calendarId) { |
|
| 583 | + $fields = array_values($this->propertyMap); |
|
| 584 | + $fields[] = 'id'; |
|
| 585 | + $fields[] = 'uri'; |
|
| 586 | + $fields[] = 'synctoken'; |
|
| 587 | + $fields[] = 'components'; |
|
| 588 | + $fields[] = 'principaluri'; |
|
| 589 | + $fields[] = 'transparent'; |
|
| 590 | + |
|
| 591 | + // Making fields a comma-delimited list |
|
| 592 | + $query = $this->db->getQueryBuilder(); |
|
| 593 | + $query->select($fields)->from('calendars') |
|
| 594 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))) |
|
| 595 | + ->setMaxResults(1); |
|
| 596 | + $stmt = $query->execute(); |
|
| 597 | + |
|
| 598 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 599 | + $stmt->closeCursor(); |
|
| 600 | + if ($row === false) { |
|
| 601 | + return null; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + $components = []; |
|
| 605 | + if ($row['components']) { |
|
| 606 | + $components = explode(',',$row['components']); |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $calendar = [ |
|
| 610 | + 'id' => $row['id'], |
|
| 611 | + 'uri' => $row['uri'], |
|
| 612 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 613 | + '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 614 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 615 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 616 | + '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 617 | + ]; |
|
| 618 | + |
|
| 619 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 620 | + $calendar[$xmlName] = $row[$dbName]; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + $this->addOwnerPrincipal($calendar); |
|
| 624 | + |
|
| 625 | + return $calendar; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * Creates a new calendar for a principal. |
|
| 630 | + * |
|
| 631 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 632 | + * this calendar in other methods, such as updateCalendar. |
|
| 633 | + * |
|
| 634 | + * @param string $principalUri |
|
| 635 | + * @param string $calendarUri |
|
| 636 | + * @param array $properties |
|
| 637 | + * @return int |
|
| 638 | + * @suppress SqlInjectionChecker |
|
| 639 | + */ |
|
| 640 | + function createCalendar($principalUri, $calendarUri, array $properties) { |
|
| 641 | + $values = [ |
|
| 642 | + 'principaluri' => $this->convertPrincipal($principalUri, true), |
|
| 643 | + 'uri' => $calendarUri, |
|
| 644 | + 'synctoken' => 1, |
|
| 645 | + 'transparent' => 0, |
|
| 646 | + 'components' => 'VEVENT,VTODO', |
|
| 647 | + 'displayname' => $calendarUri |
|
| 648 | + ]; |
|
| 649 | + |
|
| 650 | + // Default value |
|
| 651 | + $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
|
| 652 | + if (isset($properties[$sccs])) { |
|
| 653 | + if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
|
| 654 | + throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 655 | + } |
|
| 656 | + $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 657 | + } |
|
| 658 | + $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 659 | + if (isset($properties[$transp])) { |
|
| 660 | + $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 664 | + if (isset($properties[$xmlName])) { |
|
| 665 | + $values[$dbName] = $properties[$xmlName]; |
|
| 666 | + } |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + $query = $this->db->getQueryBuilder(); |
|
| 670 | + $query->insert('calendars'); |
|
| 671 | + foreach($values as $column => $value) { |
|
| 672 | + $query->setValue($column, $query->createNamedParameter($value)); |
|
| 673 | + } |
|
| 674 | + $query->execute(); |
|
| 675 | + $calendarId = $query->getLastInsertId(); |
|
| 676 | + |
|
| 677 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( |
|
| 678 | + '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', |
|
| 679 | + [ |
|
| 680 | + 'calendarId' => $calendarId, |
|
| 681 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 682 | + ])); |
|
| 683 | + |
|
| 684 | + return $calendarId; |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + /** |
|
| 688 | + * Updates properties for a calendar. |
|
| 689 | + * |
|
| 690 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 691 | + * To do the actual updates, you must tell this object which properties |
|
| 692 | + * you're going to process with the handle() method. |
|
| 693 | + * |
|
| 694 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 695 | + * promise I can handle updating this property". |
|
| 696 | + * |
|
| 697 | + * Read the PropPatch documentation for more info and examples. |
|
| 698 | + * |
|
| 699 | + * @param mixed $calendarId |
|
| 700 | + * @param PropPatch $propPatch |
|
| 701 | + * @return void |
|
| 702 | + */ |
|
| 703 | + function updateCalendar($calendarId, PropPatch $propPatch) { |
|
| 704 | + $supportedProperties = array_keys($this->propertyMap); |
|
| 705 | + $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 706 | + |
|
| 707 | + /** |
|
| 708 | + * @suppress SqlInjectionChecker |
|
| 709 | + */ |
|
| 710 | + $propPatch->handle($supportedProperties, function($mutations) use ($calendarId) { |
|
| 711 | + $newValues = []; |
|
| 712 | + foreach ($mutations as $propertyName => $propertyValue) { |
|
| 713 | + |
|
| 714 | + switch ($propertyName) { |
|
| 715 | + case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 716 | + $fieldName = 'transparent'; |
|
| 717 | + $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
|
| 718 | + break; |
|
| 719 | + default : |
|
| 720 | + $fieldName = $this->propertyMap[$propertyName]; |
|
| 721 | + $newValues[$fieldName] = $propertyValue; |
|
| 722 | + break; |
|
| 723 | + } |
|
| 724 | + |
|
| 725 | + } |
|
| 726 | + $query = $this->db->getQueryBuilder(); |
|
| 727 | + $query->update('calendars'); |
|
| 728 | + foreach ($newValues as $fieldName => $value) { |
|
| 729 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 730 | + } |
|
| 731 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); |
|
| 732 | + $query->execute(); |
|
| 733 | + |
|
| 734 | + $this->addChange($calendarId, "", 2); |
|
| 735 | + |
|
| 736 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( |
|
| 737 | + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', |
|
| 738 | + [ |
|
| 739 | + 'calendarId' => $calendarId, |
|
| 740 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 741 | + 'shares' => $this->getShares($calendarId), |
|
| 742 | + 'propertyMutations' => $mutations, |
|
| 743 | + ])); |
|
| 744 | + |
|
| 745 | + return true; |
|
| 746 | + }); |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * Delete a calendar and all it's objects |
|
| 751 | + * |
|
| 752 | + * @param mixed $calendarId |
|
| 753 | + * @return void |
|
| 754 | + */ |
|
| 755 | + function deleteCalendar($calendarId) { |
|
| 756 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', new GenericEvent( |
|
| 757 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', |
|
| 758 | + [ |
|
| 759 | + 'calendarId' => $calendarId, |
|
| 760 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 761 | + 'shares' => $this->getShares($calendarId), |
|
| 762 | + ])); |
|
| 763 | + |
|
| 764 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?'); |
|
| 765 | + $stmt->execute([$calendarId]); |
|
| 766 | + |
|
| 767 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 768 | + $stmt->execute([$calendarId]); |
|
| 769 | + |
|
| 770 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarchanges` WHERE `calendarid` = ?'); |
|
| 771 | + $stmt->execute([$calendarId]); |
|
| 772 | + |
|
| 773 | + $this->sharingBackend->deleteAllShares($calendarId); |
|
| 774 | + |
|
| 775 | + $query = $this->db->getQueryBuilder(); |
|
| 776 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 777 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 778 | + ->execute(); |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + /** |
|
| 782 | + * Delete all of an user's shares |
|
| 783 | + * |
|
| 784 | + * @param string $principaluri |
|
| 785 | + * @return void |
|
| 786 | + */ |
|
| 787 | + function deleteAllSharesByUser($principaluri) { |
|
| 788 | + $this->sharingBackend->deleteAllSharesByUser($principaluri); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + /** |
|
| 792 | + * Returns all calendar objects within a calendar. |
|
| 793 | + * |
|
| 794 | + * Every item contains an array with the following keys: |
|
| 795 | + * * calendardata - The iCalendar-compatible calendar data |
|
| 796 | + * * uri - a unique key which will be used to construct the uri. This can |
|
| 797 | + * be any arbitrary string, but making sure it ends with '.ics' is a |
|
| 798 | + * good idea. This is only the basename, or filename, not the full |
|
| 799 | + * path. |
|
| 800 | + * * lastmodified - a timestamp of the last modification time |
|
| 801 | + * * etag - An arbitrary string, surrounded by double-quotes. (e.g.: |
|
| 802 | + * '"abcdef"') |
|
| 803 | + * * size - The size of the calendar objects, in bytes. |
|
| 804 | + * * component - optional, a string containing the type of object, such |
|
| 805 | + * as 'vevent' or 'vtodo'. If specified, this will be used to populate |
|
| 806 | + * the Content-Type header. |
|
| 807 | + * |
|
| 808 | + * Note that the etag is optional, but it's highly encouraged to return for |
|
| 809 | + * speed reasons. |
|
| 810 | + * |
|
| 811 | + * The calendardata is also optional. If it's not returned |
|
| 812 | + * 'getCalendarObject' will be called later, which *is* expected to return |
|
| 813 | + * calendardata. |
|
| 814 | + * |
|
| 815 | + * If neither etag or size are specified, the calendardata will be |
|
| 816 | + * used/fetched to determine these numbers. If both are specified the |
|
| 817 | + * amount of times this is needed is reduced by a great degree. |
|
| 818 | + * |
|
| 819 | + * @param mixed $calendarId |
|
| 820 | + * @return array |
|
| 821 | + */ |
|
| 822 | + function getCalendarObjects($calendarId) { |
|
| 823 | + $query = $this->db->getQueryBuilder(); |
|
| 824 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) |
|
| 825 | + ->from('calendarobjects') |
|
| 826 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 827 | + $stmt = $query->execute(); |
|
| 828 | + |
|
| 829 | + $result = []; |
|
| 830 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 831 | + $result[] = [ |
|
| 832 | + 'id' => $row['id'], |
|
| 833 | + 'uri' => $row['uri'], |
|
| 834 | + 'lastmodified' => $row['lastmodified'], |
|
| 835 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 836 | + 'calendarid' => $row['calendarid'], |
|
| 837 | + 'size' => (int)$row['size'], |
|
| 838 | + 'component' => strtolower($row['componenttype']), |
|
| 839 | + 'classification'=> (int)$row['classification'] |
|
| 840 | + ]; |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + return $result; |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Returns information from a single calendar object, based on it's object |
|
| 848 | + * uri. |
|
| 849 | + * |
|
| 850 | + * The object uri is only the basename, or filename and not a full path. |
|
| 851 | + * |
|
| 852 | + * The returned array must have the same keys as getCalendarObjects. The |
|
| 853 | + * 'calendardata' object is required here though, while it's not required |
|
| 854 | + * for getCalendarObjects. |
|
| 855 | + * |
|
| 856 | + * This method must return null if the object did not exist. |
|
| 857 | + * |
|
| 858 | + * @param mixed $calendarId |
|
| 859 | + * @param string $objectUri |
|
| 860 | + * @return array|null |
|
| 861 | + */ |
|
| 862 | + function getCalendarObject($calendarId, $objectUri) { |
|
| 863 | + |
|
| 864 | + $query = $this->db->getQueryBuilder(); |
|
| 865 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 866 | + ->from('calendarobjects') |
|
| 867 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 868 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); |
|
| 869 | + $stmt = $query->execute(); |
|
| 870 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 871 | + |
|
| 872 | + if(!$row) return null; |
|
| 873 | + |
|
| 874 | + return [ |
|
| 875 | + 'id' => $row['id'], |
|
| 876 | + 'uri' => $row['uri'], |
|
| 877 | + 'lastmodified' => $row['lastmodified'], |
|
| 878 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 879 | + 'calendarid' => $row['calendarid'], |
|
| 880 | + 'size' => (int)$row['size'], |
|
| 881 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 882 | + 'component' => strtolower($row['componenttype']), |
|
| 883 | + 'classification'=> (int)$row['classification'] |
|
| 884 | + ]; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + /** |
|
| 888 | + * Returns a list of calendar objects. |
|
| 889 | + * |
|
| 890 | + * This method should work identical to getCalendarObject, but instead |
|
| 891 | + * return all the calendar objects in the list as an array. |
|
| 892 | + * |
|
| 893 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 894 | + * |
|
| 895 | + * @param mixed $calendarId |
|
| 896 | + * @param string[] $uris |
|
| 897 | + * @return array |
|
| 898 | + */ |
|
| 899 | + function getMultipleCalendarObjects($calendarId, array $uris) { |
|
| 900 | + if (empty($uris)) { |
|
| 901 | + return []; |
|
| 902 | + } |
|
| 903 | + |
|
| 904 | + $chunks = array_chunk($uris, 100); |
|
| 905 | + $objects = []; |
|
| 906 | + |
|
| 907 | + $query = $this->db->getQueryBuilder(); |
|
| 908 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) |
|
| 909 | + ->from('calendarobjects') |
|
| 910 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 911 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 912 | + |
|
| 913 | + foreach ($chunks as $uris) { |
|
| 914 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 915 | + $result = $query->execute(); |
|
| 916 | + |
|
| 917 | + while ($row = $result->fetch()) { |
|
| 918 | + $objects[] = [ |
|
| 919 | + 'id' => $row['id'], |
|
| 920 | + 'uri' => $row['uri'], |
|
| 921 | + 'lastmodified' => $row['lastmodified'], |
|
| 922 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 923 | + 'calendarid' => $row['calendarid'], |
|
| 924 | + 'size' => (int)$row['size'], |
|
| 925 | + 'calendardata' => $this->readBlob($row['calendardata']), |
|
| 926 | + 'component' => strtolower($row['componenttype']), |
|
| 927 | + 'classification' => (int)$row['classification'] |
|
| 928 | + ]; |
|
| 929 | + } |
|
| 930 | + $result->closeCursor(); |
|
| 931 | + } |
|
| 932 | + return $objects; |
|
| 933 | + } |
|
| 934 | + |
|
| 935 | + /** |
|
| 936 | + * Creates a new calendar object. |
|
| 937 | + * |
|
| 938 | + * The object uri is only the basename, or filename and not a full path. |
|
| 939 | + * |
|
| 940 | + * It is possible return an etag from this function, which will be used in |
|
| 941 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 942 | + * by double-quotes. |
|
| 943 | + * |
|
| 944 | + * However, you should only really return this ETag if you don't mangle the |
|
| 945 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 946 | + * the exact same as this request body, you should omit the ETag. |
|
| 947 | + * |
|
| 948 | + * @param mixed $calendarId |
|
| 949 | + * @param string $objectUri |
|
| 950 | + * @param string $calendarData |
|
| 951 | + * @return string |
|
| 952 | + */ |
|
| 953 | + function createCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 954 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 955 | + |
|
| 956 | + $query = $this->db->getQueryBuilder(); |
|
| 957 | + $query->insert('calendarobjects') |
|
| 958 | + ->values([ |
|
| 959 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 960 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 961 | + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), |
|
| 962 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 963 | + 'etag' => $query->createNamedParameter($extraData['etag']), |
|
| 964 | + 'size' => $query->createNamedParameter($extraData['size']), |
|
| 965 | + 'componenttype' => $query->createNamedParameter($extraData['componentType']), |
|
| 966 | + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), |
|
| 967 | + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), |
|
| 968 | + 'classification' => $query->createNamedParameter($extraData['classification']), |
|
| 969 | + 'uid' => $query->createNamedParameter($extraData['uid']), |
|
| 970 | + ]) |
|
| 971 | + ->execute(); |
|
| 972 | + |
|
| 973 | + $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
| 974 | + |
|
| 975 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', new GenericEvent( |
|
| 976 | + '\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', |
|
| 977 | + [ |
|
| 978 | + 'calendarId' => $calendarId, |
|
| 979 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 980 | + 'shares' => $this->getShares($calendarId), |
|
| 981 | + 'objectData' => $this->getCalendarObject($calendarId, $objectUri), |
|
| 982 | + ] |
|
| 983 | + )); |
|
| 984 | + $this->addChange($calendarId, $objectUri, 1); |
|
| 985 | + |
|
| 986 | + return '"' . $extraData['etag'] . '"'; |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * Updates an existing calendarobject, based on it's uri. |
|
| 991 | + * |
|
| 992 | + * The object uri is only the basename, or filename and not a full path. |
|
| 993 | + * |
|
| 994 | + * It is possible return an etag from this function, which will be used in |
|
| 995 | + * the response to this PUT request. Note that the ETag must be surrounded |
|
| 996 | + * by double-quotes. |
|
| 997 | + * |
|
| 998 | + * However, you should only really return this ETag if you don't mangle the |
|
| 999 | + * calendar-data. If the result of a subsequent GET to this object is not |
|
| 1000 | + * the exact same as this request body, you should omit the ETag. |
|
| 1001 | + * |
|
| 1002 | + * @param mixed $calendarId |
|
| 1003 | + * @param string $objectUri |
|
| 1004 | + * @param string $calendarData |
|
| 1005 | + * @return string |
|
| 1006 | + */ |
|
| 1007 | + function updateCalendarObject($calendarId, $objectUri, $calendarData) { |
|
| 1008 | + $extraData = $this->getDenormalizedData($calendarData); |
|
| 1009 | + |
|
| 1010 | + $query = $this->db->getQueryBuilder(); |
|
| 1011 | + $query->update('calendarobjects') |
|
| 1012 | + ->set('calendardata', $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB)) |
|
| 1013 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 1014 | + ->set('etag', $query->createNamedParameter($extraData['etag'])) |
|
| 1015 | + ->set('size', $query->createNamedParameter($extraData['size'])) |
|
| 1016 | + ->set('componenttype', $query->createNamedParameter($extraData['componentType'])) |
|
| 1017 | + ->set('firstoccurence', $query->createNamedParameter($extraData['firstOccurence'])) |
|
| 1018 | + ->set('lastoccurence', $query->createNamedParameter($extraData['lastOccurence'])) |
|
| 1019 | + ->set('classification', $query->createNamedParameter($extraData['classification'])) |
|
| 1020 | + ->set('uid', $query->createNamedParameter($extraData['uid'])) |
|
| 1021 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) |
|
| 1022 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1023 | + ->execute(); |
|
| 1024 | + |
|
| 1025 | + $this->updateProperties($calendarId, $objectUri, $calendarData); |
|
| 1026 | + |
|
| 1027 | + $data = $this->getCalendarObject($calendarId, $objectUri); |
|
| 1028 | + if (is_array($data)) { |
|
| 1029 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', new GenericEvent( |
|
| 1030 | + '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', |
|
| 1031 | + [ |
|
| 1032 | + 'calendarId' => $calendarId, |
|
| 1033 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 1034 | + 'shares' => $this->getShares($calendarId), |
|
| 1035 | + 'objectData' => $data, |
|
| 1036 | + ] |
|
| 1037 | + )); |
|
| 1038 | + } |
|
| 1039 | + $this->addChange($calendarId, $objectUri, 2); |
|
| 1040 | + |
|
| 1041 | + return '"' . $extraData['etag'] . '"'; |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + /** |
|
| 1045 | + * @param int $calendarObjectId |
|
| 1046 | + * @param int $classification |
|
| 1047 | + */ |
|
| 1048 | + public function setClassification($calendarObjectId, $classification) { |
|
| 1049 | + if (!in_array($classification, [ |
|
| 1050 | + self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL |
|
| 1051 | + ])) { |
|
| 1052 | + throw new \InvalidArgumentException(); |
|
| 1053 | + } |
|
| 1054 | + $query = $this->db->getQueryBuilder(); |
|
| 1055 | + $query->update('calendarobjects') |
|
| 1056 | + ->set('classification', $query->createNamedParameter($classification)) |
|
| 1057 | + ->where($query->expr()->eq('id', $query->createNamedParameter($calendarObjectId))) |
|
| 1058 | + ->execute(); |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + /** |
|
| 1062 | + * Deletes an existing calendar object. |
|
| 1063 | + * |
|
| 1064 | + * The object uri is only the basename, or filename and not a full path. |
|
| 1065 | + * |
|
| 1066 | + * @param mixed $calendarId |
|
| 1067 | + * @param string $objectUri |
|
| 1068 | + * @return void |
|
| 1069 | + */ |
|
| 1070 | + function deleteCalendarObject($calendarId, $objectUri) { |
|
| 1071 | + $data = $this->getCalendarObject($calendarId, $objectUri); |
|
| 1072 | + if (is_array($data)) { |
|
| 1073 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( |
|
| 1074 | + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', |
|
| 1075 | + [ |
|
| 1076 | + 'calendarId' => $calendarId, |
|
| 1077 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 1078 | + 'shares' => $this->getShares($calendarId), |
|
| 1079 | + 'objectData' => $data, |
|
| 1080 | + ] |
|
| 1081 | + )); |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + $stmt = $this->db->prepare('DELETE FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ? AND `uri` = ?'); |
|
| 1085 | + $stmt->execute([$calendarId, $objectUri]); |
|
| 1086 | + |
|
| 1087 | + $this->purgeProperties($calendarId, $data['id']); |
|
| 1088 | + |
|
| 1089 | + $this->addChange($calendarId, $objectUri, 3); |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + /** |
|
| 1093 | + * Performs a calendar-query on the contents of this calendar. |
|
| 1094 | + * |
|
| 1095 | + * The calendar-query is defined in RFC4791 : CalDAV. Using the |
|
| 1096 | + * calendar-query it is possible for a client to request a specific set of |
|
| 1097 | + * object, based on contents of iCalendar properties, date-ranges and |
|
| 1098 | + * iCalendar component types (VTODO, VEVENT). |
|
| 1099 | + * |
|
| 1100 | + * This method should just return a list of (relative) urls that match this |
|
| 1101 | + * query. |
|
| 1102 | + * |
|
| 1103 | + * The list of filters are specified as an array. The exact array is |
|
| 1104 | + * documented by Sabre\CalDAV\CalendarQueryParser. |
|
| 1105 | + * |
|
| 1106 | + * Note that it is extremely likely that getCalendarObject for every path |
|
| 1107 | + * returned from this method will be called almost immediately after. You |
|
| 1108 | + * may want to anticipate this to speed up these requests. |
|
| 1109 | + * |
|
| 1110 | + * This method provides a default implementation, which parses *all* the |
|
| 1111 | + * iCalendar objects in the specified calendar. |
|
| 1112 | + * |
|
| 1113 | + * This default may well be good enough for personal use, and calendars |
|
| 1114 | + * that aren't very large. But if you anticipate high usage, big calendars |
|
| 1115 | + * or high loads, you are strongly advised to optimize certain paths. |
|
| 1116 | + * |
|
| 1117 | + * The best way to do so is override this method and to optimize |
|
| 1118 | + * specifically for 'common filters'. |
|
| 1119 | + * |
|
| 1120 | + * Requests that are extremely common are: |
|
| 1121 | + * * requests for just VEVENTS |
|
| 1122 | + * * requests for just VTODO |
|
| 1123 | + * * requests with a time-range-filter on either VEVENT or VTODO. |
|
| 1124 | + * |
|
| 1125 | + * ..and combinations of these requests. It may not be worth it to try to |
|
| 1126 | + * handle every possible situation and just rely on the (relatively |
|
| 1127 | + * easy to use) CalendarQueryValidator to handle the rest. |
|
| 1128 | + * |
|
| 1129 | + * Note that especially time-range-filters may be difficult to parse. A |
|
| 1130 | + * time-range filter specified on a VEVENT must for instance also handle |
|
| 1131 | + * recurrence rules correctly. |
|
| 1132 | + * A good example of how to interprete all these filters can also simply |
|
| 1133 | + * be found in Sabre\CalDAV\CalendarQueryFilter. This class is as correct |
|
| 1134 | + * as possible, so it gives you a good idea on what type of stuff you need |
|
| 1135 | + * to think of. |
|
| 1136 | + * |
|
| 1137 | + * @param mixed $calendarId |
|
| 1138 | + * @param array $filters |
|
| 1139 | + * @return array |
|
| 1140 | + */ |
|
| 1141 | + function calendarQuery($calendarId, array $filters) { |
|
| 1142 | + $componentType = null; |
|
| 1143 | + $requirePostFilter = true; |
|
| 1144 | + $timeRange = null; |
|
| 1145 | + |
|
| 1146 | + // if no filters were specified, we don't need to filter after a query |
|
| 1147 | + if (!$filters['prop-filters'] && !$filters['comp-filters']) { |
|
| 1148 | + $requirePostFilter = false; |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + // Figuring out if there's a component filter |
|
| 1152 | + if (count($filters['comp-filters']) > 0 && !$filters['comp-filters'][0]['is-not-defined']) { |
|
| 1153 | + $componentType = $filters['comp-filters'][0]['name']; |
|
| 1154 | + |
|
| 1155 | + // Checking if we need post-filters |
|
| 1156 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['time-range'] && !$filters['comp-filters'][0]['prop-filters']) { |
|
| 1157 | + $requirePostFilter = false; |
|
| 1158 | + } |
|
| 1159 | + // There was a time-range filter |
|
| 1160 | + if ($componentType == 'VEVENT' && isset($filters['comp-filters'][0]['time-range'])) { |
|
| 1161 | + $timeRange = $filters['comp-filters'][0]['time-range']; |
|
| 1162 | + |
|
| 1163 | + // If start time OR the end time is not specified, we can do a |
|
| 1164 | + // 100% accurate mysql query. |
|
| 1165 | + if (!$filters['prop-filters'] && !$filters['comp-filters'][0]['comp-filters'] && !$filters['comp-filters'][0]['prop-filters'] && (!$timeRange['start'] || !$timeRange['end'])) { |
|
| 1166 | + $requirePostFilter = false; |
|
| 1167 | + } |
|
| 1168 | + } |
|
| 1169 | + |
|
| 1170 | + } |
|
| 1171 | + $columns = ['uri']; |
|
| 1172 | + if ($requirePostFilter) { |
|
| 1173 | + $columns = ['uri', 'calendardata']; |
|
| 1174 | + } |
|
| 1175 | + $query = $this->db->getQueryBuilder(); |
|
| 1176 | + $query->select($columns) |
|
| 1177 | + ->from('calendarobjects') |
|
| 1178 | + ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 1179 | + |
|
| 1180 | + if ($componentType) { |
|
| 1181 | + $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + if ($timeRange && $timeRange['start']) { |
|
| 1185 | + $query->andWhere($query->expr()->gt('lastoccurence', $query->createNamedParameter($timeRange['start']->getTimeStamp()))); |
|
| 1186 | + } |
|
| 1187 | + if ($timeRange && $timeRange['end']) { |
|
| 1188 | + $query->andWhere($query->expr()->lt('firstoccurence', $query->createNamedParameter($timeRange['end']->getTimeStamp()))); |
|
| 1189 | + } |
|
| 1190 | + |
|
| 1191 | + $stmt = $query->execute(); |
|
| 1192 | + |
|
| 1193 | + $result = []; |
|
| 1194 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1195 | + if ($requirePostFilter) { |
|
| 1196 | + if (!$this->validateFilterForObject($row, $filters)) { |
|
| 1197 | + continue; |
|
| 1198 | + } |
|
| 1199 | + } |
|
| 1200 | + $result[] = $row['uri']; |
|
| 1201 | + } |
|
| 1202 | + |
|
| 1203 | + return $result; |
|
| 1204 | + } |
|
| 1205 | + |
|
| 1206 | + /** |
|
| 1207 | + * custom Nextcloud search extension for CalDAV |
|
| 1208 | + * |
|
| 1209 | + * @param string $principalUri |
|
| 1210 | + * @param array $filters |
|
| 1211 | + * @param integer|null $limit |
|
| 1212 | + * @param integer|null $offset |
|
| 1213 | + * @return array |
|
| 1214 | + */ |
|
| 1215 | + public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
| 1216 | + $calendars = $this->getCalendarsForUser($principalUri); |
|
| 1217 | + $ownCalendars = []; |
|
| 1218 | + $sharedCalendars = []; |
|
| 1219 | + |
|
| 1220 | + $uriMapper = []; |
|
| 1221 | + |
|
| 1222 | + foreach($calendars as $calendar) { |
|
| 1223 | + if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
|
| 1224 | + $ownCalendars[] = $calendar['id']; |
|
| 1225 | + } else { |
|
| 1226 | + $sharedCalendars[] = $calendar['id']; |
|
| 1227 | + } |
|
| 1228 | + $uriMapper[$calendar['id']] = $calendar['uri']; |
|
| 1229 | + } |
|
| 1230 | + if (count($ownCalendars) === 0 && count($sharedCalendars) === 0) { |
|
| 1231 | + return []; |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + $query = $this->db->getQueryBuilder(); |
|
| 1235 | + // Calendar id expressions |
|
| 1236 | + $calendarExpressions = []; |
|
| 1237 | + foreach($ownCalendars as $id) { |
|
| 1238 | + $calendarExpressions[] = $query->expr() |
|
| 1239 | + ->eq('c.calendarid', $query->createNamedParameter($id)); |
|
| 1240 | + } |
|
| 1241 | + foreach($sharedCalendars as $id) { |
|
| 1242 | + $calendarExpressions[] = $query->expr()->andX( |
|
| 1243 | + $query->expr()->eq('c.calendarid', |
|
| 1244 | + $query->createNamedParameter($id)), |
|
| 1245 | + $query->expr()->eq('c.classification', |
|
| 1246 | + $query->createNamedParameter(self::CLASSIFICATION_PUBLIC)) |
|
| 1247 | + ); |
|
| 1248 | + } |
|
| 1249 | + |
|
| 1250 | + if (count($calendarExpressions) === 1) { |
|
| 1251 | + $calExpr = $calendarExpressions[0]; |
|
| 1252 | + } else { |
|
| 1253 | + $calExpr = call_user_func_array([$query->expr(), 'orX'], $calendarExpressions); |
|
| 1254 | + } |
|
| 1255 | + |
|
| 1256 | + // Component expressions |
|
| 1257 | + $compExpressions = []; |
|
| 1258 | + foreach($filters['comps'] as $comp) { |
|
| 1259 | + $compExpressions[] = $query->expr() |
|
| 1260 | + ->eq('c.componenttype', $query->createNamedParameter($comp)); |
|
| 1261 | + } |
|
| 1262 | + |
|
| 1263 | + if (count($compExpressions) === 1) { |
|
| 1264 | + $compExpr = $compExpressions[0]; |
|
| 1265 | + } else { |
|
| 1266 | + $compExpr = call_user_func_array([$query->expr(), 'orX'], $compExpressions); |
|
| 1267 | + } |
|
| 1268 | + |
|
| 1269 | + if (!isset($filters['props'])) { |
|
| 1270 | + $filters['props'] = []; |
|
| 1271 | + } |
|
| 1272 | + if (!isset($filters['params'])) { |
|
| 1273 | + $filters['params'] = []; |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + $propParamExpressions = []; |
|
| 1277 | + foreach($filters['props'] as $prop) { |
|
| 1278 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1279 | + $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
|
| 1280 | + $query->expr()->isNull('i.parameter') |
|
| 1281 | + ); |
|
| 1282 | + } |
|
| 1283 | + foreach($filters['params'] as $param) { |
|
| 1284 | + $propParamExpressions[] = $query->expr()->andX( |
|
| 1285 | + $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
|
| 1286 | + $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
|
| 1287 | + ); |
|
| 1288 | + } |
|
| 1289 | + |
|
| 1290 | + if (count($propParamExpressions) === 1) { |
|
| 1291 | + $propParamExpr = $propParamExpressions[0]; |
|
| 1292 | + } else { |
|
| 1293 | + $propParamExpr = call_user_func_array([$query->expr(), 'orX'], $propParamExpressions); |
|
| 1294 | + } |
|
| 1295 | + |
|
| 1296 | + $query->select(['c.calendarid', 'c.uri']) |
|
| 1297 | + ->from($this->dbObjectPropertiesTable, 'i') |
|
| 1298 | + ->join('i', 'calendarobjects', 'c', $query->expr()->eq('i.objectid', 'c.id')) |
|
| 1299 | + ->where($calExpr) |
|
| 1300 | + ->andWhere($compExpr) |
|
| 1301 | + ->andWhere($propParamExpr) |
|
| 1302 | + ->andWhere($query->expr()->iLike('i.value', |
|
| 1303 | + $query->createNamedParameter('%'.$this->db->escapeLikeParameter($filters['search-term']).'%'))); |
|
| 1304 | + |
|
| 1305 | + if ($offset) { |
|
| 1306 | + $query->setFirstResult($offset); |
|
| 1307 | + } |
|
| 1308 | + if ($limit) { |
|
| 1309 | + $query->setMaxResults($limit); |
|
| 1310 | + } |
|
| 1311 | + |
|
| 1312 | + $stmt = $query->execute(); |
|
| 1313 | + |
|
| 1314 | + $result = []; |
|
| 1315 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1316 | + $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1317 | + if (!in_array($path, $result)) { |
|
| 1318 | + $result[] = $path; |
|
| 1319 | + } |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + return $result; |
|
| 1323 | + } |
|
| 1324 | + |
|
| 1325 | + /** |
|
| 1326 | + * Searches through all of a users calendars and calendar objects to find |
|
| 1327 | + * an object with a specific UID. |
|
| 1328 | + * |
|
| 1329 | + * This method should return the path to this object, relative to the |
|
| 1330 | + * calendar home, so this path usually only contains two parts: |
|
| 1331 | + * |
|
| 1332 | + * calendarpath/objectpath.ics |
|
| 1333 | + * |
|
| 1334 | + * If the uid is not found, return null. |
|
| 1335 | + * |
|
| 1336 | + * This method should only consider * objects that the principal owns, so |
|
| 1337 | + * any calendars owned by other principals that also appear in this |
|
| 1338 | + * collection should be ignored. |
|
| 1339 | + * |
|
| 1340 | + * @param string $principalUri |
|
| 1341 | + * @param string $uid |
|
| 1342 | + * @return string|null |
|
| 1343 | + */ |
|
| 1344 | + function getCalendarObjectByUID($principalUri, $uid) { |
|
| 1345 | + |
|
| 1346 | + $query = $this->db->getQueryBuilder(); |
|
| 1347 | + $query->selectAlias('c.uri', 'calendaruri')->selectAlias('co.uri', 'objecturi') |
|
| 1348 | + ->from('calendarobjects', 'co') |
|
| 1349 | + ->leftJoin('co', 'calendars', 'c', $query->expr()->eq('co.calendarid', 'c.id')) |
|
| 1350 | + ->where($query->expr()->eq('c.principaluri', $query->createNamedParameter($principalUri))) |
|
| 1351 | + ->andWhere($query->expr()->eq('co.uid', $query->createNamedParameter($uid))); |
|
| 1352 | + |
|
| 1353 | + $stmt = $query->execute(); |
|
| 1354 | + |
|
| 1355 | + if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1356 | + return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 1357 | + } |
|
| 1358 | + |
|
| 1359 | + return null; |
|
| 1360 | + } |
|
| 1361 | + |
|
| 1362 | + /** |
|
| 1363 | + * The getChanges method returns all the changes that have happened, since |
|
| 1364 | + * the specified syncToken in the specified calendar. |
|
| 1365 | + * |
|
| 1366 | + * This function should return an array, such as the following: |
|
| 1367 | + * |
|
| 1368 | + * [ |
|
| 1369 | + * 'syncToken' => 'The current synctoken', |
|
| 1370 | + * 'added' => [ |
|
| 1371 | + * 'new.txt', |
|
| 1372 | + * ], |
|
| 1373 | + * 'modified' => [ |
|
| 1374 | + * 'modified.txt', |
|
| 1375 | + * ], |
|
| 1376 | + * 'deleted' => [ |
|
| 1377 | + * 'foo.php.bak', |
|
| 1378 | + * 'old.txt' |
|
| 1379 | + * ] |
|
| 1380 | + * ); |
|
| 1381 | + * |
|
| 1382 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 1383 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 1384 | + * property This is * needed here too, to ensure the operation is atomic. |
|
| 1385 | + * |
|
| 1386 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 1387 | + * sync, and all members should be reported. |
|
| 1388 | + * |
|
| 1389 | + * The modified property is an array of nodenames that have changed since |
|
| 1390 | + * the last token. |
|
| 1391 | + * |
|
| 1392 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 1393 | + * from collection. |
|
| 1394 | + * |
|
| 1395 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 1396 | + * 1, you only have to report changes that happened only directly in |
|
| 1397 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 1398 | + * the nodes below the child collections. (grandchildren) |
|
| 1399 | + * |
|
| 1400 | + * The $limit argument allows a client to specify how many results should |
|
| 1401 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 1402 | + * as infinite. |
|
| 1403 | + * |
|
| 1404 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 1405 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 1406 | + * |
|
| 1407 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 1408 | + * return null. |
|
| 1409 | + * |
|
| 1410 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 1411 | + * |
|
| 1412 | + * @param string $calendarId |
|
| 1413 | + * @param string $syncToken |
|
| 1414 | + * @param int $syncLevel |
|
| 1415 | + * @param int $limit |
|
| 1416 | + * @return array |
|
| 1417 | + */ |
|
| 1418 | + function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
|
| 1419 | + // Current synctoken |
|
| 1420 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1421 | + $stmt->execute([ $calendarId ]); |
|
| 1422 | + $currentToken = $stmt->fetchColumn(0); |
|
| 1423 | + |
|
| 1424 | + if (is_null($currentToken)) { |
|
| 1425 | + return null; |
|
| 1426 | + } |
|
| 1427 | + |
|
| 1428 | + $result = [ |
|
| 1429 | + 'syncToken' => $currentToken, |
|
| 1430 | + 'added' => [], |
|
| 1431 | + 'modified' => [], |
|
| 1432 | + 'deleted' => [], |
|
| 1433 | + ]; |
|
| 1434 | + |
|
| 1435 | + if ($syncToken) { |
|
| 1436 | + |
|
| 1437 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
|
| 1438 | + if ($limit>0) { |
|
| 1439 | + $query.= " `LIMIT` " . (int)$limit; |
|
| 1440 | + } |
|
| 1441 | + |
|
| 1442 | + // Fetching all changes |
|
| 1443 | + $stmt = $this->db->prepare($query); |
|
| 1444 | + $stmt->execute([$syncToken, $currentToken, $calendarId]); |
|
| 1445 | + |
|
| 1446 | + $changes = []; |
|
| 1447 | + |
|
| 1448 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 1449 | + // last change on a node is relevant. |
|
| 1450 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1451 | + |
|
| 1452 | + $changes[$row['uri']] = $row['operation']; |
|
| 1453 | + |
|
| 1454 | + } |
|
| 1455 | + |
|
| 1456 | + foreach($changes as $uri => $operation) { |
|
| 1457 | + |
|
| 1458 | + switch($operation) { |
|
| 1459 | + case 1 : |
|
| 1460 | + $result['added'][] = $uri; |
|
| 1461 | + break; |
|
| 1462 | + case 2 : |
|
| 1463 | + $result['modified'][] = $uri; |
|
| 1464 | + break; |
|
| 1465 | + case 3 : |
|
| 1466 | + $result['deleted'][] = $uri; |
|
| 1467 | + break; |
|
| 1468 | + } |
|
| 1469 | + |
|
| 1470 | + } |
|
| 1471 | + } else { |
|
| 1472 | + // No synctoken supplied, this is the initial sync. |
|
| 1473 | + $query = "SELECT `uri` FROM `*PREFIX*calendarobjects` WHERE `calendarid` = ?"; |
|
| 1474 | + $stmt = $this->db->prepare($query); |
|
| 1475 | + $stmt->execute([$calendarId]); |
|
| 1476 | + |
|
| 1477 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 1478 | + } |
|
| 1479 | + return $result; |
|
| 1480 | + |
|
| 1481 | + } |
|
| 1482 | + |
|
| 1483 | + /** |
|
| 1484 | + * Returns a list of subscriptions for a principal. |
|
| 1485 | + * |
|
| 1486 | + * Every subscription is an array with the following keys: |
|
| 1487 | + * * id, a unique id that will be used by other functions to modify the |
|
| 1488 | + * subscription. This can be the same as the uri or a database key. |
|
| 1489 | + * * uri. This is just the 'base uri' or 'filename' of the subscription. |
|
| 1490 | + * * principaluri. The owner of the subscription. Almost always the same as |
|
| 1491 | + * principalUri passed to this method. |
|
| 1492 | + * |
|
| 1493 | + * Furthermore, all the subscription info must be returned too: |
|
| 1494 | + * |
|
| 1495 | + * 1. {DAV:}displayname |
|
| 1496 | + * 2. {http://apple.com/ns/ical/}refreshrate |
|
| 1497 | + * 3. {http://calendarserver.org/ns/}subscribed-strip-todos (omit if todos |
|
| 1498 | + * should not be stripped). |
|
| 1499 | + * 4. {http://calendarserver.org/ns/}subscribed-strip-alarms (omit if alarms |
|
| 1500 | + * should not be stripped). |
|
| 1501 | + * 5. {http://calendarserver.org/ns/}subscribed-strip-attachments (omit if |
|
| 1502 | + * attachments should not be stripped). |
|
| 1503 | + * 6. {http://calendarserver.org/ns/}source (Must be a |
|
| 1504 | + * Sabre\DAV\Property\Href). |
|
| 1505 | + * 7. {http://apple.com/ns/ical/}calendar-color |
|
| 1506 | + * 8. {http://apple.com/ns/ical/}calendar-order |
|
| 1507 | + * 9. {urn:ietf:params:xml:ns:caldav}supported-calendar-component-set |
|
| 1508 | + * (should just be an instance of |
|
| 1509 | + * Sabre\CalDAV\Property\SupportedCalendarComponentSet, with a bunch of |
|
| 1510 | + * default components). |
|
| 1511 | + * |
|
| 1512 | + * @param string $principalUri |
|
| 1513 | + * @return array |
|
| 1514 | + */ |
|
| 1515 | + function getSubscriptionsForUser($principalUri) { |
|
| 1516 | + $fields = array_values($this->subscriptionPropertyMap); |
|
| 1517 | + $fields[] = 'id'; |
|
| 1518 | + $fields[] = 'uri'; |
|
| 1519 | + $fields[] = 'source'; |
|
| 1520 | + $fields[] = 'principaluri'; |
|
| 1521 | + $fields[] = 'lastmodified'; |
|
| 1522 | + |
|
| 1523 | + $query = $this->db->getQueryBuilder(); |
|
| 1524 | + $query->select($fields) |
|
| 1525 | + ->from('calendarsubscriptions') |
|
| 1526 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1527 | + ->orderBy('calendarorder', 'asc'); |
|
| 1528 | + $stmt =$query->execute(); |
|
| 1529 | + |
|
| 1530 | + $subscriptions = []; |
|
| 1531 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1532 | + |
|
| 1533 | + $subscription = [ |
|
| 1534 | + 'id' => $row['id'], |
|
| 1535 | + 'uri' => $row['uri'], |
|
| 1536 | + 'principaluri' => $row['principaluri'], |
|
| 1537 | + 'source' => $row['source'], |
|
| 1538 | + 'lastmodified' => $row['lastmodified'], |
|
| 1539 | + |
|
| 1540 | + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1541 | + ]; |
|
| 1542 | + |
|
| 1543 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1544 | + if (!is_null($row[$dbName])) { |
|
| 1545 | + $subscription[$xmlName] = $row[$dbName]; |
|
| 1546 | + } |
|
| 1547 | + } |
|
| 1548 | + |
|
| 1549 | + $subscriptions[] = $subscription; |
|
| 1550 | + |
|
| 1551 | + } |
|
| 1552 | + |
|
| 1553 | + return $subscriptions; |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + /** |
|
| 1557 | + * Creates a new subscription for a principal. |
|
| 1558 | + * |
|
| 1559 | + * If the creation was a success, an id must be returned that can be used to reference |
|
| 1560 | + * this subscription in other methods, such as updateSubscription. |
|
| 1561 | + * |
|
| 1562 | + * @param string $principalUri |
|
| 1563 | + * @param string $uri |
|
| 1564 | + * @param array $properties |
|
| 1565 | + * @return mixed |
|
| 1566 | + */ |
|
| 1567 | + function createSubscription($principalUri, $uri, array $properties) { |
|
| 1568 | + |
|
| 1569 | + if (!isset($properties['{http://calendarserver.org/ns/}source'])) { |
|
| 1570 | + throw new Forbidden('The {http://calendarserver.org/ns/}source property is required when creating subscriptions'); |
|
| 1571 | + } |
|
| 1572 | + |
|
| 1573 | + $values = [ |
|
| 1574 | + 'principaluri' => $principalUri, |
|
| 1575 | + 'uri' => $uri, |
|
| 1576 | + 'source' => $properties['{http://calendarserver.org/ns/}source']->getHref(), |
|
| 1577 | + 'lastmodified' => time(), |
|
| 1578 | + ]; |
|
| 1579 | + |
|
| 1580 | + $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
|
| 1581 | + |
|
| 1582 | + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1583 | + if (array_key_exists($xmlName, $properties)) { |
|
| 1584 | + $values[$dbName] = $properties[$xmlName]; |
|
| 1585 | + if (in_array($dbName, $propertiesBoolean)) { |
|
| 1586 | + $values[$dbName] = true; |
|
| 1587 | + } |
|
| 1588 | + } |
|
| 1589 | + } |
|
| 1590 | + |
|
| 1591 | + $valuesToInsert = array(); |
|
| 1592 | + |
|
| 1593 | + $query = $this->db->getQueryBuilder(); |
|
| 1594 | + |
|
| 1595 | + foreach (array_keys($values) as $name) { |
|
| 1596 | + $valuesToInsert[$name] = $query->createNamedParameter($values[$name]); |
|
| 1597 | + } |
|
| 1598 | + |
|
| 1599 | + $query->insert('calendarsubscriptions') |
|
| 1600 | + ->values($valuesToInsert) |
|
| 1601 | + ->execute(); |
|
| 1602 | + |
|
| 1603 | + return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); |
|
| 1604 | + } |
|
| 1605 | + |
|
| 1606 | + /** |
|
| 1607 | + * Updates a subscription |
|
| 1608 | + * |
|
| 1609 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 1610 | + * To do the actual updates, you must tell this object which properties |
|
| 1611 | + * you're going to process with the handle() method. |
|
| 1612 | + * |
|
| 1613 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 1614 | + * promise I can handle updating this property". |
|
| 1615 | + * |
|
| 1616 | + * Read the PropPatch documentation for more info and examples. |
|
| 1617 | + * |
|
| 1618 | + * @param mixed $subscriptionId |
|
| 1619 | + * @param PropPatch $propPatch |
|
| 1620 | + * @return void |
|
| 1621 | + */ |
|
| 1622 | + function updateSubscription($subscriptionId, PropPatch $propPatch) { |
|
| 1623 | + $supportedProperties = array_keys($this->subscriptionPropertyMap); |
|
| 1624 | + $supportedProperties[] = '{http://calendarserver.org/ns/}source'; |
|
| 1625 | + |
|
| 1626 | + /** |
|
| 1627 | + * @suppress SqlInjectionChecker |
|
| 1628 | + */ |
|
| 1629 | + $propPatch->handle($supportedProperties, function($mutations) use ($subscriptionId) { |
|
| 1630 | + |
|
| 1631 | + $newValues = []; |
|
| 1632 | + |
|
| 1633 | + foreach($mutations as $propertyName=>$propertyValue) { |
|
| 1634 | + if ($propertyName === '{http://calendarserver.org/ns/}source') { |
|
| 1635 | + $newValues['source'] = $propertyValue->getHref(); |
|
| 1636 | + } else { |
|
| 1637 | + $fieldName = $this->subscriptionPropertyMap[$propertyName]; |
|
| 1638 | + $newValues[$fieldName] = $propertyValue; |
|
| 1639 | + } |
|
| 1640 | + } |
|
| 1641 | + |
|
| 1642 | + $query = $this->db->getQueryBuilder(); |
|
| 1643 | + $query->update('calendarsubscriptions') |
|
| 1644 | + ->set('lastmodified', $query->createNamedParameter(time())); |
|
| 1645 | + foreach($newValues as $fieldName=>$value) { |
|
| 1646 | + $query->set($fieldName, $query->createNamedParameter($value)); |
|
| 1647 | + } |
|
| 1648 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1649 | + ->execute(); |
|
| 1650 | + |
|
| 1651 | + return true; |
|
| 1652 | + |
|
| 1653 | + }); |
|
| 1654 | + } |
|
| 1655 | + |
|
| 1656 | + /** |
|
| 1657 | + * Deletes a subscription. |
|
| 1658 | + * |
|
| 1659 | + * @param mixed $subscriptionId |
|
| 1660 | + * @return void |
|
| 1661 | + */ |
|
| 1662 | + function deleteSubscription($subscriptionId) { |
|
| 1663 | + $query = $this->db->getQueryBuilder(); |
|
| 1664 | + $query->delete('calendarsubscriptions') |
|
| 1665 | + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
|
| 1666 | + ->execute(); |
|
| 1667 | + } |
|
| 1668 | + |
|
| 1669 | + /** |
|
| 1670 | + * Returns a single scheduling object for the inbox collection. |
|
| 1671 | + * |
|
| 1672 | + * The returned array should contain the following elements: |
|
| 1673 | + * * uri - A unique basename for the object. This will be used to |
|
| 1674 | + * construct a full uri. |
|
| 1675 | + * * calendardata - The iCalendar object |
|
| 1676 | + * * lastmodified - The last modification date. Can be an int for a unix |
|
| 1677 | + * timestamp, or a PHP DateTime object. |
|
| 1678 | + * * etag - A unique token that must change if the object changed. |
|
| 1679 | + * * size - The size of the object, in bytes. |
|
| 1680 | + * |
|
| 1681 | + * @param string $principalUri |
|
| 1682 | + * @param string $objectUri |
|
| 1683 | + * @return array |
|
| 1684 | + */ |
|
| 1685 | + function getSchedulingObject($principalUri, $objectUri) { |
|
| 1686 | + $query = $this->db->getQueryBuilder(); |
|
| 1687 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1688 | + ->from('schedulingobjects') |
|
| 1689 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1690 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1691 | + ->execute(); |
|
| 1692 | + |
|
| 1693 | + $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
|
| 1694 | + |
|
| 1695 | + if(!$row) { |
|
| 1696 | + return null; |
|
| 1697 | + } |
|
| 1698 | + |
|
| 1699 | + return [ |
|
| 1700 | + 'uri' => $row['uri'], |
|
| 1701 | + 'calendardata' => $row['calendardata'], |
|
| 1702 | + 'lastmodified' => $row['lastmodified'], |
|
| 1703 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1704 | + 'size' => (int)$row['size'], |
|
| 1705 | + ]; |
|
| 1706 | + } |
|
| 1707 | + |
|
| 1708 | + /** |
|
| 1709 | + * Returns all scheduling objects for the inbox collection. |
|
| 1710 | + * |
|
| 1711 | + * These objects should be returned as an array. Every item in the array |
|
| 1712 | + * should follow the same structure as returned from getSchedulingObject. |
|
| 1713 | + * |
|
| 1714 | + * The main difference is that 'calendardata' is optional. |
|
| 1715 | + * |
|
| 1716 | + * @param string $principalUri |
|
| 1717 | + * @return array |
|
| 1718 | + */ |
|
| 1719 | + function getSchedulingObjects($principalUri) { |
|
| 1720 | + $query = $this->db->getQueryBuilder(); |
|
| 1721 | + $stmt = $query->select(['uri', 'calendardata', 'lastmodified', 'etag', 'size']) |
|
| 1722 | + ->from('schedulingobjects') |
|
| 1723 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1724 | + ->execute(); |
|
| 1725 | + |
|
| 1726 | + $result = []; |
|
| 1727 | + foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1728 | + $result[] = [ |
|
| 1729 | + 'calendardata' => $row['calendardata'], |
|
| 1730 | + 'uri' => $row['uri'], |
|
| 1731 | + 'lastmodified' => $row['lastmodified'], |
|
| 1732 | + 'etag' => '"' . $row['etag'] . '"', |
|
| 1733 | + 'size' => (int)$row['size'], |
|
| 1734 | + ]; |
|
| 1735 | + } |
|
| 1736 | + |
|
| 1737 | + return $result; |
|
| 1738 | + } |
|
| 1739 | + |
|
| 1740 | + /** |
|
| 1741 | + * Deletes a scheduling object from the inbox collection. |
|
| 1742 | + * |
|
| 1743 | + * @param string $principalUri |
|
| 1744 | + * @param string $objectUri |
|
| 1745 | + * @return void |
|
| 1746 | + */ |
|
| 1747 | + function deleteSchedulingObject($principalUri, $objectUri) { |
|
| 1748 | + $query = $this->db->getQueryBuilder(); |
|
| 1749 | + $query->delete('schedulingobjects') |
|
| 1750 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
|
| 1751 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))) |
|
| 1752 | + ->execute(); |
|
| 1753 | + } |
|
| 1754 | + |
|
| 1755 | + /** |
|
| 1756 | + * Creates a new scheduling object. This should land in a users' inbox. |
|
| 1757 | + * |
|
| 1758 | + * @param string $principalUri |
|
| 1759 | + * @param string $objectUri |
|
| 1760 | + * @param string $objectData |
|
| 1761 | + * @return void |
|
| 1762 | + */ |
|
| 1763 | + function createSchedulingObject($principalUri, $objectUri, $objectData) { |
|
| 1764 | + $query = $this->db->getQueryBuilder(); |
|
| 1765 | + $query->insert('schedulingobjects') |
|
| 1766 | + ->values([ |
|
| 1767 | + 'principaluri' => $query->createNamedParameter($principalUri), |
|
| 1768 | + 'calendardata' => $query->createNamedParameter($objectData), |
|
| 1769 | + 'uri' => $query->createNamedParameter($objectUri), |
|
| 1770 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 1771 | + 'etag' => $query->createNamedParameter(md5($objectData)), |
|
| 1772 | + 'size' => $query->createNamedParameter(strlen($objectData)) |
|
| 1773 | + ]) |
|
| 1774 | + ->execute(); |
|
| 1775 | + } |
|
| 1776 | + |
|
| 1777 | + /** |
|
| 1778 | + * Adds a change record to the calendarchanges table. |
|
| 1779 | + * |
|
| 1780 | + * @param mixed $calendarId |
|
| 1781 | + * @param string $objectUri |
|
| 1782 | + * @param int $operation 1 = add, 2 = modify, 3 = delete. |
|
| 1783 | + * @return void |
|
| 1784 | + */ |
|
| 1785 | + protected function addChange($calendarId, $objectUri, $operation) { |
|
| 1786 | + |
|
| 1787 | + $stmt = $this->db->prepare('INSERT INTO `*PREFIX*calendarchanges` (`uri`, `synctoken`, `calendarid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*calendars` WHERE `id` = ?'); |
|
| 1788 | + $stmt->execute([ |
|
| 1789 | + $objectUri, |
|
| 1790 | + $calendarId, |
|
| 1791 | + $operation, |
|
| 1792 | + $calendarId |
|
| 1793 | + ]); |
|
| 1794 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*calendars` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 1795 | + $stmt->execute([ |
|
| 1796 | + $calendarId |
|
| 1797 | + ]); |
|
| 1798 | + |
|
| 1799 | + } |
|
| 1800 | + |
|
| 1801 | + /** |
|
| 1802 | + * Parses some information from calendar objects, used for optimized |
|
| 1803 | + * calendar-queries. |
|
| 1804 | + * |
|
| 1805 | + * Returns an array with the following keys: |
|
| 1806 | + * * etag - An md5 checksum of the object without the quotes. |
|
| 1807 | + * * size - Size of the object in bytes |
|
| 1808 | + * * componentType - VEVENT, VTODO or VJOURNAL |
|
| 1809 | + * * firstOccurence |
|
| 1810 | + * * lastOccurence |
|
| 1811 | + * * uid - value of the UID property |
|
| 1812 | + * |
|
| 1813 | + * @param string $calendarData |
|
| 1814 | + * @return array |
|
| 1815 | + */ |
|
| 1816 | + public function getDenormalizedData($calendarData) { |
|
| 1817 | + |
|
| 1818 | + $vObject = Reader::read($calendarData); |
|
| 1819 | + $componentType = null; |
|
| 1820 | + $component = null; |
|
| 1821 | + $firstOccurrence = null; |
|
| 1822 | + $lastOccurrence = null; |
|
| 1823 | + $uid = null; |
|
| 1824 | + $classification = self::CLASSIFICATION_PUBLIC; |
|
| 1825 | + foreach($vObject->getComponents() as $component) { |
|
| 1826 | + if ($component->name!=='VTIMEZONE') { |
|
| 1827 | + $componentType = $component->name; |
|
| 1828 | + $uid = (string)$component->UID; |
|
| 1829 | + break; |
|
| 1830 | + } |
|
| 1831 | + } |
|
| 1832 | + if (!$componentType) { |
|
| 1833 | + throw new \Sabre\DAV\Exception\BadRequest('Calendar objects must have a VJOURNAL, VEVENT or VTODO component'); |
|
| 1834 | + } |
|
| 1835 | + if ($componentType === 'VEVENT' && $component->DTSTART) { |
|
| 1836 | + $firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); |
|
| 1837 | + // Finding the last occurrence is a bit harder |
|
| 1838 | + if (!isset($component->RRULE)) { |
|
| 1839 | + if (isset($component->DTEND)) { |
|
| 1840 | + $lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); |
|
| 1841 | + } elseif (isset($component->DURATION)) { |
|
| 1842 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1843 | + $endDate->add(DateTimeParser::parse($component->DURATION->getValue())); |
|
| 1844 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1845 | + } elseif (!$component->DTSTART->hasTime()) { |
|
| 1846 | + $endDate = clone $component->DTSTART->getDateTime(); |
|
| 1847 | + $endDate->modify('+1 day'); |
|
| 1848 | + $lastOccurrence = $endDate->getTimeStamp(); |
|
| 1849 | + } else { |
|
| 1850 | + $lastOccurrence = $firstOccurrence; |
|
| 1851 | + } |
|
| 1852 | + } else { |
|
| 1853 | + $it = new EventIterator($vObject, (string)$component->UID); |
|
| 1854 | + $maxDate = new \DateTime(self::MAX_DATE); |
|
| 1855 | + if ($it->isInfinite()) { |
|
| 1856 | + $lastOccurrence = $maxDate->getTimestamp(); |
|
| 1857 | + } else { |
|
| 1858 | + $end = $it->getDtEnd(); |
|
| 1859 | + while($it->valid() && $end < $maxDate) { |
|
| 1860 | + $end = $it->getDtEnd(); |
|
| 1861 | + $it->next(); |
|
| 1862 | + |
|
| 1863 | + } |
|
| 1864 | + $lastOccurrence = $end->getTimestamp(); |
|
| 1865 | + } |
|
| 1866 | + |
|
| 1867 | + } |
|
| 1868 | + } |
|
| 1869 | + |
|
| 1870 | + if ($component->CLASS) { |
|
| 1871 | + $classification = CalDavBackend::CLASSIFICATION_PRIVATE; |
|
| 1872 | + switch ($component->CLASS->getValue()) { |
|
| 1873 | + case 'PUBLIC': |
|
| 1874 | + $classification = CalDavBackend::CLASSIFICATION_PUBLIC; |
|
| 1875 | + break; |
|
| 1876 | + case 'CONFIDENTIAL': |
|
| 1877 | + $classification = CalDavBackend::CLASSIFICATION_CONFIDENTIAL; |
|
| 1878 | + break; |
|
| 1879 | + } |
|
| 1880 | + } |
|
| 1881 | + return [ |
|
| 1882 | + 'etag' => md5($calendarData), |
|
| 1883 | + 'size' => strlen($calendarData), |
|
| 1884 | + 'componentType' => $componentType, |
|
| 1885 | + 'firstOccurence' => is_null($firstOccurrence) ? null : max(0, $firstOccurrence), |
|
| 1886 | + 'lastOccurence' => $lastOccurrence, |
|
| 1887 | + 'uid' => $uid, |
|
| 1888 | + 'classification' => $classification |
|
| 1889 | + ]; |
|
| 1890 | + |
|
| 1891 | + } |
|
| 1892 | + |
|
| 1893 | + private function readBlob($cardData) { |
|
| 1894 | + if (is_resource($cardData)) { |
|
| 1895 | + return stream_get_contents($cardData); |
|
| 1896 | + } |
|
| 1897 | + |
|
| 1898 | + return $cardData; |
|
| 1899 | + } |
|
| 1900 | + |
|
| 1901 | + /** |
|
| 1902 | + * @param IShareable $shareable |
|
| 1903 | + * @param array $add |
|
| 1904 | + * @param array $remove |
|
| 1905 | + */ |
|
| 1906 | + public function updateShares($shareable, $add, $remove) { |
|
| 1907 | + $calendarId = $shareable->getResourceId(); |
|
| 1908 | + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateShares', new GenericEvent( |
|
| 1909 | + '\OCA\DAV\CalDAV\CalDavBackend::updateShares', |
|
| 1910 | + [ |
|
| 1911 | + 'calendarId' => $calendarId, |
|
| 1912 | + 'calendarData' => $this->getCalendarById($calendarId), |
|
| 1913 | + 'shares' => $this->getShares($calendarId), |
|
| 1914 | + 'add' => $add, |
|
| 1915 | + 'remove' => $remove, |
|
| 1916 | + ])); |
|
| 1917 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 1918 | + } |
|
| 1919 | + |
|
| 1920 | + /** |
|
| 1921 | + * @param int $resourceId |
|
| 1922 | + * @return array |
|
| 1923 | + */ |
|
| 1924 | + public function getShares($resourceId) { |
|
| 1925 | + return $this->sharingBackend->getShares($resourceId); |
|
| 1926 | + } |
|
| 1927 | + |
|
| 1928 | + /** |
|
| 1929 | + * @param boolean $value |
|
| 1930 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 1931 | + * @return string|null |
|
| 1932 | + */ |
|
| 1933 | + public function setPublishStatus($value, $calendar) { |
|
| 1934 | + $query = $this->db->getQueryBuilder(); |
|
| 1935 | + if ($value) { |
|
| 1936 | + $publicUri = $this->random->generate(16, ISecureRandom::CHAR_HUMAN_READABLE); |
|
| 1937 | + $query->insert('dav_shares') |
|
| 1938 | + ->values([ |
|
| 1939 | + 'principaluri' => $query->createNamedParameter($calendar->getPrincipalURI()), |
|
| 1940 | + 'type' => $query->createNamedParameter('calendar'), |
|
| 1941 | + 'access' => $query->createNamedParameter(self::ACCESS_PUBLIC), |
|
| 1942 | + 'resourceid' => $query->createNamedParameter($calendar->getResourceId()), |
|
| 1943 | + 'publicuri' => $query->createNamedParameter($publicUri) |
|
| 1944 | + ]); |
|
| 1945 | + $query->execute(); |
|
| 1946 | + return $publicUri; |
|
| 1947 | + } |
|
| 1948 | + $query->delete('dav_shares') |
|
| 1949 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 1950 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))); |
|
| 1951 | + $query->execute(); |
|
| 1952 | + return null; |
|
| 1953 | + } |
|
| 1954 | + |
|
| 1955 | + /** |
|
| 1956 | + * @param \OCA\DAV\CalDAV\Calendar $calendar |
|
| 1957 | + * @return mixed |
|
| 1958 | + */ |
|
| 1959 | + public function getPublishStatus($calendar) { |
|
| 1960 | + $query = $this->db->getQueryBuilder(); |
|
| 1961 | + $result = $query->select('publicuri') |
|
| 1962 | + ->from('dav_shares') |
|
| 1963 | + ->where($query->expr()->eq('resourceid', $query->createNamedParameter($calendar->getResourceId()))) |
|
| 1964 | + ->andWhere($query->expr()->eq('access', $query->createNamedParameter(self::ACCESS_PUBLIC))) |
|
| 1965 | + ->execute(); |
|
| 1966 | + |
|
| 1967 | + $row = $result->fetch(); |
|
| 1968 | + $result->closeCursor(); |
|
| 1969 | + return $row ? reset($row) : false; |
|
| 1970 | + } |
|
| 1971 | + |
|
| 1972 | + /** |
|
| 1973 | + * @param int $resourceId |
|
| 1974 | + * @param array $acl |
|
| 1975 | + * @return array |
|
| 1976 | + */ |
|
| 1977 | + public function applyShareAcl($resourceId, $acl) { |
|
| 1978 | + return $this->sharingBackend->applyShareAcl($resourceId, $acl); |
|
| 1979 | + } |
|
| 1980 | + |
|
| 1981 | + |
|
| 1982 | + |
|
| 1983 | + /** |
|
| 1984 | + * update properties table |
|
| 1985 | + * |
|
| 1986 | + * @param int $calendarId |
|
| 1987 | + * @param string $objectUri |
|
| 1988 | + * @param string $calendarData |
|
| 1989 | + */ |
|
| 1990 | + public function updateProperties($calendarId, $objectUri, $calendarData) { |
|
| 1991 | + $objectId = $this->getCalendarObjectId($calendarId, $objectUri); |
|
| 1992 | + |
|
| 1993 | + try { |
|
| 1994 | + $vCalendar = $this->readCalendarData($calendarData); |
|
| 1995 | + } catch (\Exception $ex) { |
|
| 1996 | + return; |
|
| 1997 | + } |
|
| 1998 | + |
|
| 1999 | + $this->purgeProperties($calendarId, $objectId); |
|
| 2000 | + |
|
| 2001 | + $query = $this->db->getQueryBuilder(); |
|
| 2002 | + $query->insert($this->dbObjectPropertiesTable) |
|
| 2003 | + ->values( |
|
| 2004 | + [ |
|
| 2005 | + 'calendarid' => $query->createNamedParameter($calendarId), |
|
| 2006 | + 'objectid' => $query->createNamedParameter($objectId), |
|
| 2007 | + 'name' => $query->createParameter('name'), |
|
| 2008 | + 'parameter' => $query->createParameter('parameter'), |
|
| 2009 | + 'value' => $query->createParameter('value'), |
|
| 2010 | + ] |
|
| 2011 | + ); |
|
| 2012 | + |
|
| 2013 | + $indexComponents = ['VEVENT', 'VJOURNAL', 'VTODO']; |
|
| 2014 | + foreach ($vCalendar->getComponents() as $component) { |
|
| 2015 | + if (!in_array($component->name, $indexComponents)) { |
|
| 2016 | + continue; |
|
| 2017 | + } |
|
| 2018 | + |
|
| 2019 | + foreach ($component->children() as $property) { |
|
| 2020 | + if (in_array($property->name, self::$indexProperties)) { |
|
| 2021 | + $value = $property->getValue(); |
|
| 2022 | + // is this a shitty db? |
|
| 2023 | + if (!$this->db->supports4ByteText()) { |
|
| 2024 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2025 | + } |
|
| 2026 | + $value = substr($value, 0, 254); |
|
| 2027 | + |
|
| 2028 | + $query->setParameter('name', $property->name); |
|
| 2029 | + $query->setParameter('parameter', null); |
|
| 2030 | + $query->setParameter('value', $value); |
|
| 2031 | + $query->execute(); |
|
| 2032 | + } |
|
| 2033 | + |
|
| 2034 | + if (in_array($property->name, array_keys(self::$indexParameters))) { |
|
| 2035 | + $parameters = $property->parameters(); |
|
| 2036 | + $indexedParametersForProperty = self::$indexParameters[$property->name]; |
|
| 2037 | + |
|
| 2038 | + foreach ($parameters as $key => $value) { |
|
| 2039 | + if (in_array($key, $indexedParametersForProperty)) { |
|
| 2040 | + // is this a shitty db? |
|
| 2041 | + if ($this->db->supports4ByteText()) { |
|
| 2042 | + $value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', "\xEF\xBF\xBD", $value); |
|
| 2043 | + } |
|
| 2044 | + $value = substr($value, 0, 254); |
|
| 2045 | + |
|
| 2046 | + $query->setParameter('name', $property->name); |
|
| 2047 | + $query->setParameter('parameter', substr($key, 0, 254)); |
|
| 2048 | + $query->setParameter('value', substr($value, 0, 254)); |
|
| 2049 | + $query->execute(); |
|
| 2050 | + } |
|
| 2051 | + } |
|
| 2052 | + } |
|
| 2053 | + } |
|
| 2054 | + } |
|
| 2055 | + } |
|
| 2056 | + |
|
| 2057 | + /** |
|
| 2058 | + * read VCalendar data into a VCalendar object |
|
| 2059 | + * |
|
| 2060 | + * @param string $objectData |
|
| 2061 | + * @return VCalendar |
|
| 2062 | + */ |
|
| 2063 | + protected function readCalendarData($objectData) { |
|
| 2064 | + return Reader::read($objectData); |
|
| 2065 | + } |
|
| 2066 | + |
|
| 2067 | + /** |
|
| 2068 | + * delete all properties from a given calendar object |
|
| 2069 | + * |
|
| 2070 | + * @param int $calendarId |
|
| 2071 | + * @param int $objectId |
|
| 2072 | + */ |
|
| 2073 | + protected function purgeProperties($calendarId, $objectId) { |
|
| 2074 | + $query = $this->db->getQueryBuilder(); |
|
| 2075 | + $query->delete($this->dbObjectPropertiesTable) |
|
| 2076 | + ->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId))) |
|
| 2077 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 2078 | + $query->execute(); |
|
| 2079 | + } |
|
| 2080 | + |
|
| 2081 | + /** |
|
| 2082 | + * get ID from a given calendar object |
|
| 2083 | + * |
|
| 2084 | + * @param int $calendarId |
|
| 2085 | + * @param string $uri |
|
| 2086 | + * @return int |
|
| 2087 | + */ |
|
| 2088 | + protected function getCalendarObjectId($calendarId, $uri) { |
|
| 2089 | + $query = $this->db->getQueryBuilder(); |
|
| 2090 | + $query->select('id')->from('calendarobjects') |
|
| 2091 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 2092 | + ->andWhere($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); |
|
| 2093 | + |
|
| 2094 | + $result = $query->execute(); |
|
| 2095 | + $objectIds = $result->fetch(); |
|
| 2096 | + $result->closeCursor(); |
|
| 2097 | + |
|
| 2098 | + if (!isset($objectIds['id'])) { |
|
| 2099 | + throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 2100 | + } |
|
| 2101 | + |
|
| 2102 | + return (int)$objectIds['id']; |
|
| 2103 | + } |
|
| 2104 | + |
|
| 2105 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 2106 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 2107 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
| 2108 | + if ($toV2 === true) { |
|
| 2109 | + return "principals/users/$name"; |
|
| 2110 | + } |
|
| 2111 | + return "principals/$name"; |
|
| 2112 | + } |
|
| 2113 | + return $principalUri; |
|
| 2114 | + } |
|
| 2115 | + |
|
| 2116 | + private function addOwnerPrincipal(&$calendarInfo) { |
|
| 2117 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 2118 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 2119 | + if (isset($calendarInfo[$ownerPrincipalKey])) { |
|
| 2120 | + $uri = $calendarInfo[$ownerPrincipalKey]; |
|
| 2121 | + } else { |
|
| 2122 | + $uri = $calendarInfo['principaluri']; |
|
| 2123 | + } |
|
| 2124 | + |
|
| 2125 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 2126 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 2127 | + $calendarInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 2128 | + } |
|
| 2129 | + } |
|
| 2130 | 2130 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | $query->andWhere($query->expr()->neq('uri', $query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI))); |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - return (int)$query->execute()->fetchColumn(); |
|
| 199 | + return (int) $query->execute()->fetchColumn(); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | /** |
@@ -243,25 +243,25 @@ discard block |
||
| 243 | 243 | $stmt = $query->execute(); |
| 244 | 244 | |
| 245 | 245 | $calendars = []; |
| 246 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 246 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 247 | 247 | |
| 248 | 248 | $components = []; |
| 249 | 249 | if ($row['components']) { |
| 250 | - $components = explode(',',$row['components']); |
|
| 250 | + $components = explode(',', $row['components']); |
|
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | $calendar = [ |
| 254 | 254 | 'id' => $row['id'], |
| 255 | 255 | 'uri' => $row['uri'], |
| 256 | 256 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 257 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 258 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 259 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 260 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 261 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 257 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 258 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 259 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 260 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 261 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
|
| 262 | 262 | ]; |
| 263 | 263 | |
| 264 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 264 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 265 | 265 | $calendar[$xmlName] = $row[$dbName]; |
| 266 | 266 | } |
| 267 | 267 | |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | $principals = array_map(function($principal) { |
| 280 | 280 | return urldecode($principal); |
| 281 | 281 | }, $principals); |
| 282 | - $principals[]= $principalUri; |
|
| 282 | + $principals[] = $principalUri; |
|
| 283 | 283 | |
| 284 | 284 | $fields = array_values($this->propertyMap); |
| 285 | 285 | $fields[] = 'a.id'; |
@@ -299,8 +299,8 @@ discard block |
||
| 299 | 299 | ->setParameter('principaluri', $principals, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY) |
| 300 | 300 | ->execute(); |
| 301 | 301 | |
| 302 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 303 | - while($row = $result->fetch()) { |
|
| 302 | + $readOnlyPropertyName = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only'; |
|
| 303 | + while ($row = $result->fetch()) { |
|
| 304 | 304 | if ($row['principaluri'] === $principalUri) { |
| 305 | 305 | continue; |
| 306 | 306 | } |
@@ -319,25 +319,25 @@ discard block |
||
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | list(, $name) = URLUtil::splitPath($row['principaluri']); |
| 322 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 323 | - $row['displayname'] = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 322 | + $uri = $row['uri'].'_shared_by_'.$name; |
|
| 323 | + $row['displayname'] = $row['displayname'].' ('.$this->getUserDisplayName($name).')'; |
|
| 324 | 324 | $components = []; |
| 325 | 325 | if ($row['components']) { |
| 326 | - $components = explode(',',$row['components']); |
|
| 326 | + $components = explode(',', $row['components']); |
|
| 327 | 327 | } |
| 328 | 328 | $calendar = [ |
| 329 | 329 | 'id' => $row['id'], |
| 330 | 330 | 'uri' => $uri, |
| 331 | 331 | 'principaluri' => $this->convertPrincipal($principalUri, !$this->legacyEndpoint), |
| 332 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 333 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 334 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 335 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 336 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 332 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 333 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 334 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 335 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 336 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 337 | 337 | $readOnlyPropertyName => $readOnly, |
| 338 | 338 | ]; |
| 339 | 339 | |
| 340 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 340 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 341 | 341 | $calendar[$xmlName] = $row[$dbName]; |
| 342 | 342 | } |
| 343 | 343 | |
@@ -366,21 +366,21 @@ discard block |
||
| 366 | 366 | ->orderBy('calendarorder', 'ASC'); |
| 367 | 367 | $stmt = $query->execute(); |
| 368 | 368 | $calendars = []; |
| 369 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 369 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 370 | 370 | $components = []; |
| 371 | 371 | if ($row['components']) { |
| 372 | - $components = explode(',',$row['components']); |
|
| 372 | + $components = explode(',', $row['components']); |
|
| 373 | 373 | } |
| 374 | 374 | $calendar = [ |
| 375 | 375 | 'id' => $row['id'], |
| 376 | 376 | 'uri' => $row['uri'], |
| 377 | 377 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 378 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 379 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 380 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 381 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 378 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 379 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 380 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 381 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 382 | 382 | ]; |
| 383 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 383 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 384 | 384 | $calendar[$xmlName] = $row[$dbName]; |
| 385 | 385 | } |
| 386 | 386 | |
@@ -431,27 +431,27 @@ discard block |
||
| 431 | 431 | ->andWhere($query->expr()->eq('s.type', $query->createNamedParameter('calendar'))) |
| 432 | 432 | ->execute(); |
| 433 | 433 | |
| 434 | - while($row = $result->fetch()) { |
|
| 434 | + while ($row = $result->fetch()) { |
|
| 435 | 435 | list(, $name) = URLUtil::splitPath($row['principaluri']); |
| 436 | - $row['displayname'] = $row['displayname'] . "($name)"; |
|
| 436 | + $row['displayname'] = $row['displayname']."($name)"; |
|
| 437 | 437 | $components = []; |
| 438 | 438 | if ($row['components']) { |
| 439 | - $components = explode(',',$row['components']); |
|
| 439 | + $components = explode(',', $row['components']); |
|
| 440 | 440 | } |
| 441 | 441 | $calendar = [ |
| 442 | 442 | 'id' => $row['id'], |
| 443 | 443 | 'uri' => $row['publicuri'], |
| 444 | 444 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 445 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 446 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 447 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 448 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 449 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 450 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 451 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 445 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 446 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 447 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 448 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 449 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($row['principaluri'], $this->legacyEndpoint), |
|
| 450 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only' => (int) $row['access'] === Backend::ACCESS_READ, |
|
| 451 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}public' => (int) $row['access'] === self::ACCESS_PUBLIC, |
|
| 452 | 452 | ]; |
| 453 | 453 | |
| 454 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 454 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 455 | 455 | $calendar[$xmlName] = $row[$dbName]; |
| 456 | 456 | } |
| 457 | 457 | |
@@ -495,29 +495,29 @@ discard block |
||
| 495 | 495 | $result->closeCursor(); |
| 496 | 496 | |
| 497 | 497 | if ($row === false) { |
| 498 | - throw new NotFound('Node with name \'' . $uri . '\' could not be found'); |
|
| 498 | + throw new NotFound('Node with name \''.$uri.'\' could not be found'); |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | list(, $name) = URLUtil::splitPath($row['principaluri']); |
| 502 | - $row['displayname'] = $row['displayname'] . ' ' . "($name)"; |
|
| 502 | + $row['displayname'] = $row['displayname'].' '."($name)"; |
|
| 503 | 503 | $components = []; |
| 504 | 504 | if ($row['components']) { |
| 505 | - $components = explode(',',$row['components']); |
|
| 505 | + $components = explode(',', $row['components']); |
|
| 506 | 506 | } |
| 507 | 507 | $calendar = [ |
| 508 | 508 | 'id' => $row['id'], |
| 509 | 509 | 'uri' => $row['publicuri'], |
| 510 | 510 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 511 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 512 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 513 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 514 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 515 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 516 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only' => (int)$row['access'] === Backend::ACCESS_READ, |
|
| 517 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}public' => (int)$row['access'] === self::ACCESS_PUBLIC, |
|
| 511 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 512 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 513 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 514 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 515 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
|
| 516 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only' => (int) $row['access'] === Backend::ACCESS_READ, |
|
| 517 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}public' => (int) $row['access'] === self::ACCESS_PUBLIC, |
|
| 518 | 518 | ]; |
| 519 | 519 | |
| 520 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 520 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 521 | 521 | $calendar[$xmlName] = $row[$dbName]; |
| 522 | 522 | } |
| 523 | 523 | |
@@ -557,20 +557,20 @@ discard block |
||
| 557 | 557 | |
| 558 | 558 | $components = []; |
| 559 | 559 | if ($row['components']) { |
| 560 | - $components = explode(',',$row['components']); |
|
| 560 | + $components = explode(',', $row['components']); |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | $calendar = [ |
| 564 | 564 | 'id' => $row['id'], |
| 565 | 565 | 'uri' => $row['uri'], |
| 566 | 566 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 567 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 568 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 569 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 570 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 567 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 568 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 569 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 570 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 571 | 571 | ]; |
| 572 | 572 | |
| 573 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 573 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 574 | 574 | $calendar[$xmlName] = $row[$dbName]; |
| 575 | 575 | } |
| 576 | 576 | |
@@ -603,20 +603,20 @@ discard block |
||
| 603 | 603 | |
| 604 | 604 | $components = []; |
| 605 | 605 | if ($row['components']) { |
| 606 | - $components = explode(',',$row['components']); |
|
| 606 | + $components = explode(',', $row['components']); |
|
| 607 | 607 | } |
| 608 | 608 | |
| 609 | 609 | $calendar = [ |
| 610 | 610 | 'id' => $row['id'], |
| 611 | 611 | 'uri' => $row['uri'], |
| 612 | 612 | 'principaluri' => $this->convertPrincipal($row['principaluri'], !$this->legacyEndpoint), |
| 613 | - '{' . Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($row['synctoken']?$row['synctoken']:'0'), |
|
| 614 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 615 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 616 | - '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent']?'transparent':'opaque'), |
|
| 613 | + '{'.Plugin::NS_CALENDARSERVER.'}getctag' => 'http://sabre.io/ns/sync/'.($row['synctoken'] ? $row['synctoken'] : '0'), |
|
| 614 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 615 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet($components), |
|
| 616 | + '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' => new ScheduleCalendarTransp($row['transparent'] ? 'transparent' : 'opaque'), |
|
| 617 | 617 | ]; |
| 618 | 618 | |
| 619 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 619 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 620 | 620 | $calendar[$xmlName] = $row[$dbName]; |
| 621 | 621 | } |
| 622 | 622 | |
@@ -651,16 +651,16 @@ discard block |
||
| 651 | 651 | $sccs = '{urn:ietf:params:xml:ns:caldav}supported-calendar-component-set'; |
| 652 | 652 | if (isset($properties[$sccs])) { |
| 653 | 653 | if (!($properties[$sccs] instanceof SupportedCalendarComponentSet)) { |
| 654 | - throw new DAV\Exception('The ' . $sccs . ' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 654 | + throw new DAV\Exception('The '.$sccs.' property must be of type: \Sabre\CalDAV\Property\SupportedCalendarComponentSet'); |
|
| 655 | 655 | } |
| 656 | - $values['components'] = implode(',',$properties[$sccs]->getValue()); |
|
| 656 | + $values['components'] = implode(',', $properties[$sccs]->getValue()); |
|
| 657 | 657 | } |
| 658 | - $transp = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 658 | + $transp = '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp'; |
|
| 659 | 659 | if (isset($properties[$transp])) { |
| 660 | 660 | $values['transparent'] = (int) ($properties[$transp]->getValue() === 'transparent'); |
| 661 | 661 | } |
| 662 | 662 | |
| 663 | - foreach($this->propertyMap as $xmlName=>$dbName) { |
|
| 663 | + foreach ($this->propertyMap as $xmlName=>$dbName) { |
|
| 664 | 664 | if (isset($properties[$xmlName])) { |
| 665 | 665 | $values[$dbName] = $properties[$xmlName]; |
| 666 | 666 | } |
@@ -668,7 +668,7 @@ discard block |
||
| 668 | 668 | |
| 669 | 669 | $query = $this->db->getQueryBuilder(); |
| 670 | 670 | $query->insert('calendars'); |
| 671 | - foreach($values as $column => $value) { |
|
| 671 | + foreach ($values as $column => $value) { |
|
| 672 | 672 | $query->setValue($column, $query->createNamedParameter($value)); |
| 673 | 673 | } |
| 674 | 674 | $query->execute(); |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | */ |
| 703 | 703 | function updateCalendar($calendarId, PropPatch $propPatch) { |
| 704 | 704 | $supportedProperties = array_keys($this->propertyMap); |
| 705 | - $supportedProperties[] = '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp'; |
|
| 705 | + $supportedProperties[] = '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp'; |
|
| 706 | 706 | |
| 707 | 707 | /** |
| 708 | 708 | * @suppress SqlInjectionChecker |
@@ -712,7 +712,7 @@ discard block |
||
| 712 | 712 | foreach ($mutations as $propertyName => $propertyValue) { |
| 713 | 713 | |
| 714 | 714 | switch ($propertyName) { |
| 715 | - case '{' . Plugin::NS_CALDAV . '}schedule-calendar-transp' : |
|
| 715 | + case '{'.Plugin::NS_CALDAV.'}schedule-calendar-transp' : |
|
| 716 | 716 | $fieldName = 'transparent'; |
| 717 | 717 | $newValues[$fieldName] = (int) ($propertyValue->getValue() === 'transparent'); |
| 718 | 718 | break; |
@@ -827,16 +827,16 @@ discard block |
||
| 827 | 827 | $stmt = $query->execute(); |
| 828 | 828 | |
| 829 | 829 | $result = []; |
| 830 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 830 | + foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 831 | 831 | $result[] = [ |
| 832 | 832 | 'id' => $row['id'], |
| 833 | 833 | 'uri' => $row['uri'], |
| 834 | 834 | 'lastmodified' => $row['lastmodified'], |
| 835 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 835 | + 'etag' => '"'.$row['etag'].'"', |
|
| 836 | 836 | 'calendarid' => $row['calendarid'], |
| 837 | - 'size' => (int)$row['size'], |
|
| 837 | + 'size' => (int) $row['size'], |
|
| 838 | 838 | 'component' => strtolower($row['componenttype']), |
| 839 | - 'classification'=> (int)$row['classification'] |
|
| 839 | + 'classification'=> (int) $row['classification'] |
|
| 840 | 840 | ]; |
| 841 | 841 | } |
| 842 | 842 | |
@@ -869,18 +869,18 @@ discard block |
||
| 869 | 869 | $stmt = $query->execute(); |
| 870 | 870 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 871 | 871 | |
| 872 | - if(!$row) return null; |
|
| 872 | + if (!$row) return null; |
|
| 873 | 873 | |
| 874 | 874 | return [ |
| 875 | 875 | 'id' => $row['id'], |
| 876 | 876 | 'uri' => $row['uri'], |
| 877 | 877 | 'lastmodified' => $row['lastmodified'], |
| 878 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 878 | + 'etag' => '"'.$row['etag'].'"', |
|
| 879 | 879 | 'calendarid' => $row['calendarid'], |
| 880 | - 'size' => (int)$row['size'], |
|
| 880 | + 'size' => (int) $row['size'], |
|
| 881 | 881 | 'calendardata' => $this->readBlob($row['calendardata']), |
| 882 | 882 | 'component' => strtolower($row['componenttype']), |
| 883 | - 'classification'=> (int)$row['classification'] |
|
| 883 | + 'classification'=> (int) $row['classification'] |
|
| 884 | 884 | ]; |
| 885 | 885 | } |
| 886 | 886 | |
@@ -919,12 +919,12 @@ discard block |
||
| 919 | 919 | 'id' => $row['id'], |
| 920 | 920 | 'uri' => $row['uri'], |
| 921 | 921 | 'lastmodified' => $row['lastmodified'], |
| 922 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 922 | + 'etag' => '"'.$row['etag'].'"', |
|
| 923 | 923 | 'calendarid' => $row['calendarid'], |
| 924 | - 'size' => (int)$row['size'], |
|
| 924 | + 'size' => (int) $row['size'], |
|
| 925 | 925 | 'calendardata' => $this->readBlob($row['calendardata']), |
| 926 | 926 | 'component' => strtolower($row['componenttype']), |
| 927 | - 'classification' => (int)$row['classification'] |
|
| 927 | + 'classification' => (int) $row['classification'] |
|
| 928 | 928 | ]; |
| 929 | 929 | } |
| 930 | 930 | $result->closeCursor(); |
@@ -983,7 +983,7 @@ discard block |
||
| 983 | 983 | )); |
| 984 | 984 | $this->addChange($calendarId, $objectUri, 1); |
| 985 | 985 | |
| 986 | - return '"' . $extraData['etag'] . '"'; |
|
| 986 | + return '"'.$extraData['etag'].'"'; |
|
| 987 | 987 | } |
| 988 | 988 | |
| 989 | 989 | /** |
@@ -1038,7 +1038,7 @@ discard block |
||
| 1038 | 1038 | } |
| 1039 | 1039 | $this->addChange($calendarId, $objectUri, 2); |
| 1040 | 1040 | |
| 1041 | - return '"' . $extraData['etag'] . '"'; |
|
| 1041 | + return '"'.$extraData['etag'].'"'; |
|
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | /** |
@@ -1191,7 +1191,7 @@ discard block |
||
| 1191 | 1191 | $stmt = $query->execute(); |
| 1192 | 1192 | |
| 1193 | 1193 | $result = []; |
| 1194 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1194 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1195 | 1195 | if ($requirePostFilter) { |
| 1196 | 1196 | if (!$this->validateFilterForObject($row, $filters)) { |
| 1197 | 1197 | continue; |
@@ -1212,14 +1212,14 @@ discard block |
||
| 1212 | 1212 | * @param integer|null $offset |
| 1213 | 1213 | * @return array |
| 1214 | 1214 | */ |
| 1215 | - public function calendarSearch($principalUri, array $filters, $limit=null, $offset=null) { |
|
| 1215 | + public function calendarSearch($principalUri, array $filters, $limit = null, $offset = null) { |
|
| 1216 | 1216 | $calendars = $this->getCalendarsForUser($principalUri); |
| 1217 | 1217 | $ownCalendars = []; |
| 1218 | 1218 | $sharedCalendars = []; |
| 1219 | 1219 | |
| 1220 | 1220 | $uriMapper = []; |
| 1221 | 1221 | |
| 1222 | - foreach($calendars as $calendar) { |
|
| 1222 | + foreach ($calendars as $calendar) { |
|
| 1223 | 1223 | if ($calendar['{http://owncloud.org/ns}owner-principal'] === $principalUri) { |
| 1224 | 1224 | $ownCalendars[] = $calendar['id']; |
| 1225 | 1225 | } else { |
@@ -1234,11 +1234,11 @@ discard block |
||
| 1234 | 1234 | $query = $this->db->getQueryBuilder(); |
| 1235 | 1235 | // Calendar id expressions |
| 1236 | 1236 | $calendarExpressions = []; |
| 1237 | - foreach($ownCalendars as $id) { |
|
| 1237 | + foreach ($ownCalendars as $id) { |
|
| 1238 | 1238 | $calendarExpressions[] = $query->expr() |
| 1239 | 1239 | ->eq('c.calendarid', $query->createNamedParameter($id)); |
| 1240 | 1240 | } |
| 1241 | - foreach($sharedCalendars as $id) { |
|
| 1241 | + foreach ($sharedCalendars as $id) { |
|
| 1242 | 1242 | $calendarExpressions[] = $query->expr()->andX( |
| 1243 | 1243 | $query->expr()->eq('c.calendarid', |
| 1244 | 1244 | $query->createNamedParameter($id)), |
@@ -1255,7 +1255,7 @@ discard block |
||
| 1255 | 1255 | |
| 1256 | 1256 | // Component expressions |
| 1257 | 1257 | $compExpressions = []; |
| 1258 | - foreach($filters['comps'] as $comp) { |
|
| 1258 | + foreach ($filters['comps'] as $comp) { |
|
| 1259 | 1259 | $compExpressions[] = $query->expr() |
| 1260 | 1260 | ->eq('c.componenttype', $query->createNamedParameter($comp)); |
| 1261 | 1261 | } |
@@ -1274,13 +1274,13 @@ discard block |
||
| 1274 | 1274 | } |
| 1275 | 1275 | |
| 1276 | 1276 | $propParamExpressions = []; |
| 1277 | - foreach($filters['props'] as $prop) { |
|
| 1277 | + foreach ($filters['props'] as $prop) { |
|
| 1278 | 1278 | $propParamExpressions[] = $query->expr()->andX( |
| 1279 | 1279 | $query->expr()->eq('i.name', $query->createNamedParameter($prop)), |
| 1280 | 1280 | $query->expr()->isNull('i.parameter') |
| 1281 | 1281 | ); |
| 1282 | 1282 | } |
| 1283 | - foreach($filters['params'] as $param) { |
|
| 1283 | + foreach ($filters['params'] as $param) { |
|
| 1284 | 1284 | $propParamExpressions[] = $query->expr()->andX( |
| 1285 | 1285 | $query->expr()->eq('i.name', $query->createNamedParameter($param['property'])), |
| 1286 | 1286 | $query->expr()->eq('i.parameter', $query->createNamedParameter($param['parameter'])) |
@@ -1312,8 +1312,8 @@ discard block |
||
| 1312 | 1312 | $stmt = $query->execute(); |
| 1313 | 1313 | |
| 1314 | 1314 | $result = []; |
| 1315 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1316 | - $path = $uriMapper[$row['calendarid']] . '/' . $row['uri']; |
|
| 1315 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1316 | + $path = $uriMapper[$row['calendarid']].'/'.$row['uri']; |
|
| 1317 | 1317 | if (!in_array($path, $result)) { |
| 1318 | 1318 | $result[] = $path; |
| 1319 | 1319 | } |
@@ -1353,7 +1353,7 @@ discard block |
||
| 1353 | 1353 | $stmt = $query->execute(); |
| 1354 | 1354 | |
| 1355 | 1355 | if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
| 1356 | - return $row['calendaruri'] . '/' . $row['objecturi']; |
|
| 1356 | + return $row['calendaruri'].'/'.$row['objecturi']; |
|
| 1357 | 1357 | } |
| 1358 | 1358 | |
| 1359 | 1359 | return null; |
@@ -1418,7 +1418,7 @@ discard block |
||
| 1418 | 1418 | function getChangesForCalendar($calendarId, $syncToken, $syncLevel, $limit = null) { |
| 1419 | 1419 | // Current synctoken |
| 1420 | 1420 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*calendars` WHERE `id` = ?'); |
| 1421 | - $stmt->execute([ $calendarId ]); |
|
| 1421 | + $stmt->execute([$calendarId]); |
|
| 1422 | 1422 | $currentToken = $stmt->fetchColumn(0); |
| 1423 | 1423 | |
| 1424 | 1424 | if (is_null($currentToken)) { |
@@ -1435,8 +1435,8 @@ discard block |
||
| 1435 | 1435 | if ($syncToken) { |
| 1436 | 1436 | |
| 1437 | 1437 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*calendarchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `calendarid` = ? ORDER BY `synctoken`"; |
| 1438 | - if ($limit>0) { |
|
| 1439 | - $query.= " `LIMIT` " . (int)$limit; |
|
| 1438 | + if ($limit > 0) { |
|
| 1439 | + $query .= " `LIMIT` ".(int) $limit; |
|
| 1440 | 1440 | } |
| 1441 | 1441 | |
| 1442 | 1442 | // Fetching all changes |
@@ -1447,15 +1447,15 @@ discard block |
||
| 1447 | 1447 | |
| 1448 | 1448 | // This loop ensures that any duplicates are overwritten, only the |
| 1449 | 1449 | // last change on a node is relevant. |
| 1450 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1450 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1451 | 1451 | |
| 1452 | 1452 | $changes[$row['uri']] = $row['operation']; |
| 1453 | 1453 | |
| 1454 | 1454 | } |
| 1455 | 1455 | |
| 1456 | - foreach($changes as $uri => $operation) { |
|
| 1456 | + foreach ($changes as $uri => $operation) { |
|
| 1457 | 1457 | |
| 1458 | - switch($operation) { |
|
| 1458 | + switch ($operation) { |
|
| 1459 | 1459 | case 1 : |
| 1460 | 1460 | $result['added'][] = $uri; |
| 1461 | 1461 | break; |
@@ -1525,10 +1525,10 @@ discard block |
||
| 1525 | 1525 | ->from('calendarsubscriptions') |
| 1526 | 1526 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))) |
| 1527 | 1527 | ->orderBy('calendarorder', 'asc'); |
| 1528 | - $stmt =$query->execute(); |
|
| 1528 | + $stmt = $query->execute(); |
|
| 1529 | 1529 | |
| 1530 | 1530 | $subscriptions = []; |
| 1531 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1531 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 1532 | 1532 | |
| 1533 | 1533 | $subscription = [ |
| 1534 | 1534 | 'id' => $row['id'], |
@@ -1537,10 +1537,10 @@ discard block |
||
| 1537 | 1537 | 'source' => $row['source'], |
| 1538 | 1538 | 'lastmodified' => $row['lastmodified'], |
| 1539 | 1539 | |
| 1540 | - '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1540 | + '{'.Plugin::NS_CALDAV.'}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), |
|
| 1541 | 1541 | ]; |
| 1542 | 1542 | |
| 1543 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1543 | + foreach ($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1544 | 1544 | if (!is_null($row[$dbName])) { |
| 1545 | 1545 | $subscription[$xmlName] = $row[$dbName]; |
| 1546 | 1546 | } |
@@ -1579,7 +1579,7 @@ discard block |
||
| 1579 | 1579 | |
| 1580 | 1580 | $propertiesBoolean = ['striptodos', 'stripalarms', 'stripattachments']; |
| 1581 | 1581 | |
| 1582 | - foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1582 | + foreach ($this->subscriptionPropertyMap as $xmlName=>$dbName) { |
|
| 1583 | 1583 | if (array_key_exists($xmlName, $properties)) { |
| 1584 | 1584 | $values[$dbName] = $properties[$xmlName]; |
| 1585 | 1585 | if (in_array($dbName, $propertiesBoolean)) { |
@@ -1630,7 +1630,7 @@ discard block |
||
| 1630 | 1630 | |
| 1631 | 1631 | $newValues = []; |
| 1632 | 1632 | |
| 1633 | - foreach($mutations as $propertyName=>$propertyValue) { |
|
| 1633 | + foreach ($mutations as $propertyName=>$propertyValue) { |
|
| 1634 | 1634 | if ($propertyName === '{http://calendarserver.org/ns/}source') { |
| 1635 | 1635 | $newValues['source'] = $propertyValue->getHref(); |
| 1636 | 1636 | } else { |
@@ -1642,7 +1642,7 @@ discard block |
||
| 1642 | 1642 | $query = $this->db->getQueryBuilder(); |
| 1643 | 1643 | $query->update('calendarsubscriptions') |
| 1644 | 1644 | ->set('lastmodified', $query->createNamedParameter(time())); |
| 1645 | - foreach($newValues as $fieldName=>$value) { |
|
| 1645 | + foreach ($newValues as $fieldName=>$value) { |
|
| 1646 | 1646 | $query->set($fieldName, $query->createNamedParameter($value)); |
| 1647 | 1647 | } |
| 1648 | 1648 | $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) |
@@ -1692,7 +1692,7 @@ discard block |
||
| 1692 | 1692 | |
| 1693 | 1693 | $row = $stmt->fetch(\PDO::FETCH_ASSOC); |
| 1694 | 1694 | |
| 1695 | - if(!$row) { |
|
| 1695 | + if (!$row) { |
|
| 1696 | 1696 | return null; |
| 1697 | 1697 | } |
| 1698 | 1698 | |
@@ -1700,8 +1700,8 @@ discard block |
||
| 1700 | 1700 | 'uri' => $row['uri'], |
| 1701 | 1701 | 'calendardata' => $row['calendardata'], |
| 1702 | 1702 | 'lastmodified' => $row['lastmodified'], |
| 1703 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1704 | - 'size' => (int)$row['size'], |
|
| 1703 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1704 | + 'size' => (int) $row['size'], |
|
| 1705 | 1705 | ]; |
| 1706 | 1706 | } |
| 1707 | 1707 | |
@@ -1724,13 +1724,13 @@ discard block |
||
| 1724 | 1724 | ->execute(); |
| 1725 | 1725 | |
| 1726 | 1726 | $result = []; |
| 1727 | - foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1727 | + foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { |
|
| 1728 | 1728 | $result[] = [ |
| 1729 | 1729 | 'calendardata' => $row['calendardata'], |
| 1730 | 1730 | 'uri' => $row['uri'], |
| 1731 | 1731 | 'lastmodified' => $row['lastmodified'], |
| 1732 | - 'etag' => '"' . $row['etag'] . '"', |
|
| 1733 | - 'size' => (int)$row['size'], |
|
| 1732 | + 'etag' => '"'.$row['etag'].'"', |
|
| 1733 | + 'size' => (int) $row['size'], |
|
| 1734 | 1734 | ]; |
| 1735 | 1735 | } |
| 1736 | 1736 | |
@@ -1822,10 +1822,10 @@ discard block |
||
| 1822 | 1822 | $lastOccurrence = null; |
| 1823 | 1823 | $uid = null; |
| 1824 | 1824 | $classification = self::CLASSIFICATION_PUBLIC; |
| 1825 | - foreach($vObject->getComponents() as $component) { |
|
| 1826 | - if ($component->name!=='VTIMEZONE') { |
|
| 1825 | + foreach ($vObject->getComponents() as $component) { |
|
| 1826 | + if ($component->name !== 'VTIMEZONE') { |
|
| 1827 | 1827 | $componentType = $component->name; |
| 1828 | - $uid = (string)$component->UID; |
|
| 1828 | + $uid = (string) $component->UID; |
|
| 1829 | 1829 | break; |
| 1830 | 1830 | } |
| 1831 | 1831 | } |
@@ -1850,13 +1850,13 @@ discard block |
||
| 1850 | 1850 | $lastOccurrence = $firstOccurrence; |
| 1851 | 1851 | } |
| 1852 | 1852 | } else { |
| 1853 | - $it = new EventIterator($vObject, (string)$component->UID); |
|
| 1853 | + $it = new EventIterator($vObject, (string) $component->UID); |
|
| 1854 | 1854 | $maxDate = new \DateTime(self::MAX_DATE); |
| 1855 | 1855 | if ($it->isInfinite()) { |
| 1856 | 1856 | $lastOccurrence = $maxDate->getTimestamp(); |
| 1857 | 1857 | } else { |
| 1858 | 1858 | $end = $it->getDtEnd(); |
| 1859 | - while($it->valid() && $end < $maxDate) { |
|
| 1859 | + while ($it->valid() && $end < $maxDate) { |
|
| 1860 | 1860 | $end = $it->getDtEnd(); |
| 1861 | 1861 | $it->next(); |
| 1862 | 1862 | |
@@ -2096,10 +2096,10 @@ discard block |
||
| 2096 | 2096 | $result->closeCursor(); |
| 2097 | 2097 | |
| 2098 | 2098 | if (!isset($objectIds['id'])) { |
| 2099 | - throw new \InvalidArgumentException('Calendarobject does not exists: ' . $uri); |
|
| 2099 | + throw new \InvalidArgumentException('Calendarobject does not exists: '.$uri); |
|
| 2100 | 2100 | } |
| 2101 | 2101 | |
| 2102 | - return (int)$objectIds['id']; |
|
| 2102 | + return (int) $objectIds['id']; |
|
| 2103 | 2103 | } |
| 2104 | 2104 | |
| 2105 | 2105 | private function convertPrincipal($principalUri, $toV2) { |
@@ -2114,8 +2114,8 @@ discard block |
||
| 2114 | 2114 | } |
| 2115 | 2115 | |
| 2116 | 2116 | private function addOwnerPrincipal(&$calendarInfo) { |
| 2117 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 2118 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 2117 | + $ownerPrincipalKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal'; |
|
| 2118 | + $displaynameKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}owner-displayname'; |
|
| 2119 | 2119 | if (isset($calendarInfo[$ownerPrincipalKey])) { |
| 2120 | 2120 | $uri = $calendarInfo[$ownerPrincipalKey]; |
| 2121 | 2121 | } else { |
@@ -48,1070 +48,1070 @@ |
||
| 48 | 48 | |
| 49 | 49 | class CardDavBackend implements BackendInterface, SyncSupport { |
| 50 | 50 | |
| 51 | - const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 52 | - const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 53 | - |
|
| 54 | - /** @var Principal */ |
|
| 55 | - private $principalBackend; |
|
| 56 | - |
|
| 57 | - /** @var string */ |
|
| 58 | - private $dbCardsTable = 'cards'; |
|
| 59 | - |
|
| 60 | - /** @var string */ |
|
| 61 | - private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 62 | - |
|
| 63 | - /** @var IDBConnection */ |
|
| 64 | - private $db; |
|
| 65 | - |
|
| 66 | - /** @var Backend */ |
|
| 67 | - private $sharingBackend; |
|
| 68 | - |
|
| 69 | - /** @var array properties to index */ |
|
| 70 | - public static $indexProperties = array( |
|
| 71 | - 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 72 | - 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @var string[] Map of uid => display name |
|
| 76 | - */ |
|
| 77 | - protected $userDisplayNames; |
|
| 78 | - |
|
| 79 | - /** @var IUserManager */ |
|
| 80 | - private $userManager; |
|
| 81 | - |
|
| 82 | - /** @var EventDispatcherInterface */ |
|
| 83 | - private $dispatcher; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * CardDavBackend constructor. |
|
| 87 | - * |
|
| 88 | - * @param IDBConnection $db |
|
| 89 | - * @param Principal $principalBackend |
|
| 90 | - * @param IUserManager $userManager |
|
| 91 | - * @param EventDispatcherInterface $dispatcher |
|
| 92 | - */ |
|
| 93 | - public function __construct(IDBConnection $db, |
|
| 94 | - Principal $principalBackend, |
|
| 95 | - IUserManager $userManager, |
|
| 96 | - EventDispatcherInterface $dispatcher) { |
|
| 97 | - $this->db = $db; |
|
| 98 | - $this->principalBackend = $principalBackend; |
|
| 99 | - $this->userManager = $userManager; |
|
| 100 | - $this->dispatcher = $dispatcher; |
|
| 101 | - $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Return the number of address books for a principal |
|
| 106 | - * |
|
| 107 | - * @param $principalUri |
|
| 108 | - * @return int |
|
| 109 | - */ |
|
| 110 | - public function getAddressBooksForUserCount($principalUri) { |
|
| 111 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 112 | - $query = $this->db->getQueryBuilder(); |
|
| 113 | - $query->select($query->createFunction('COUNT(*)')) |
|
| 114 | - ->from('addressbooks') |
|
| 115 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 116 | - |
|
| 117 | - return (int)$query->execute()->fetchColumn(); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Returns the list of address books for a specific user. |
|
| 122 | - * |
|
| 123 | - * Every addressbook should have the following properties: |
|
| 124 | - * id - an arbitrary unique id |
|
| 125 | - * uri - the 'basename' part of the url |
|
| 126 | - * principaluri - Same as the passed parameter |
|
| 127 | - * |
|
| 128 | - * Any additional clark-notation property may be passed besides this. Some |
|
| 129 | - * common ones are : |
|
| 130 | - * {DAV:}displayname |
|
| 131 | - * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 132 | - * {http://calendarserver.org/ns/}getctag |
|
| 133 | - * |
|
| 134 | - * @param string $principalUri |
|
| 135 | - * @return array |
|
| 136 | - */ |
|
| 137 | - function getAddressBooksForUser($principalUri) { |
|
| 138 | - $principalUriOriginal = $principalUri; |
|
| 139 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 140 | - $query = $this->db->getQueryBuilder(); |
|
| 141 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 142 | - ->from('addressbooks') |
|
| 143 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 144 | - |
|
| 145 | - $addressBooks = []; |
|
| 146 | - |
|
| 147 | - $result = $query->execute(); |
|
| 148 | - while($row = $result->fetch()) { |
|
| 149 | - $addressBooks[$row['id']] = [ |
|
| 150 | - 'id' => $row['id'], |
|
| 151 | - 'uri' => $row['uri'], |
|
| 152 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 153 | - '{DAV:}displayname' => $row['displayname'], |
|
| 154 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 155 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 156 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 157 | - ]; |
|
| 158 | - |
|
| 159 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 160 | - } |
|
| 161 | - $result->closeCursor(); |
|
| 162 | - |
|
| 163 | - // query for shared calendars |
|
| 164 | - $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 165 | - $principals = array_map(function($principal) { |
|
| 166 | - return urldecode($principal); |
|
| 167 | - }, $principals); |
|
| 168 | - $principals[]= $principalUri; |
|
| 169 | - |
|
| 170 | - $query = $this->db->getQueryBuilder(); |
|
| 171 | - $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 172 | - ->from('dav_shares', 's') |
|
| 173 | - ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 174 | - ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 175 | - ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 176 | - ->setParameter('type', 'addressbook') |
|
| 177 | - ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 178 | - ->execute(); |
|
| 179 | - |
|
| 180 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 181 | - while($row = $result->fetch()) { |
|
| 182 | - if ($row['principaluri'] === $principalUri) { |
|
| 183 | - continue; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 187 | - if (isset($addressBooks[$row['id']])) { |
|
| 188 | - if ($readOnly) { |
|
| 189 | - // New share can not have more permissions then the old one. |
|
| 190 | - continue; |
|
| 191 | - } |
|
| 192 | - if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 193 | - $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 194 | - // Old share is already read-write, no more permissions can be gained |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 200 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 201 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 202 | - |
|
| 203 | - $addressBooks[$row['id']] = [ |
|
| 204 | - 'id' => $row['id'], |
|
| 205 | - 'uri' => $uri, |
|
| 206 | - 'principaluri' => $principalUriOriginal, |
|
| 207 | - '{DAV:}displayname' => $displayName, |
|
| 208 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 209 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 210 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 211 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 212 | - $readOnlyPropertyName => $readOnly, |
|
| 213 | - ]; |
|
| 214 | - |
|
| 215 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 216 | - } |
|
| 217 | - $result->closeCursor(); |
|
| 218 | - |
|
| 219 | - return array_values($addressBooks); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - public function getUsersOwnAddressBooks($principalUri) { |
|
| 223 | - $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 224 | - $query = $this->db->getQueryBuilder(); |
|
| 225 | - $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 226 | - ->from('addressbooks') |
|
| 227 | - ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 228 | - |
|
| 229 | - $addressBooks = []; |
|
| 230 | - |
|
| 231 | - $result = $query->execute(); |
|
| 232 | - while($row = $result->fetch()) { |
|
| 233 | - $addressBooks[$row['id']] = [ |
|
| 234 | - 'id' => $row['id'], |
|
| 235 | - 'uri' => $row['uri'], |
|
| 236 | - 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 237 | - '{DAV:}displayname' => $row['displayname'], |
|
| 238 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 239 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 240 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 241 | - ]; |
|
| 242 | - |
|
| 243 | - $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 244 | - } |
|
| 245 | - $result->closeCursor(); |
|
| 246 | - |
|
| 247 | - return array_values($addressBooks); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - private function getUserDisplayName($uid) { |
|
| 251 | - if (!isset($this->userDisplayNames[$uid])) { |
|
| 252 | - $user = $this->userManager->get($uid); |
|
| 253 | - |
|
| 254 | - if ($user instanceof IUser) { |
|
| 255 | - $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 256 | - } else { |
|
| 257 | - $this->userDisplayNames[$uid] = $uid; |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - return $this->userDisplayNames[$uid]; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param int $addressBookId |
|
| 266 | - */ |
|
| 267 | - public function getAddressBookById($addressBookId) { |
|
| 268 | - $query = $this->db->getQueryBuilder(); |
|
| 269 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 270 | - ->from('addressbooks') |
|
| 271 | - ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 272 | - ->execute(); |
|
| 273 | - |
|
| 274 | - $row = $result->fetch(); |
|
| 275 | - $result->closeCursor(); |
|
| 276 | - if ($row === false) { |
|
| 277 | - return null; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - $addressBook = [ |
|
| 281 | - 'id' => $row['id'], |
|
| 282 | - 'uri' => $row['uri'], |
|
| 283 | - 'principaluri' => $row['principaluri'], |
|
| 284 | - '{DAV:}displayname' => $row['displayname'], |
|
| 285 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 286 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 287 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 288 | - ]; |
|
| 289 | - |
|
| 290 | - $this->addOwnerPrincipal($addressBook); |
|
| 291 | - |
|
| 292 | - return $addressBook; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * @param $addressBookUri |
|
| 297 | - * @return array|null |
|
| 298 | - */ |
|
| 299 | - public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 300 | - $query = $this->db->getQueryBuilder(); |
|
| 301 | - $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 302 | - ->from('addressbooks') |
|
| 303 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 304 | - ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 305 | - ->setMaxResults(1) |
|
| 306 | - ->execute(); |
|
| 307 | - |
|
| 308 | - $row = $result->fetch(); |
|
| 309 | - $result->closeCursor(); |
|
| 310 | - if ($row === false) { |
|
| 311 | - return null; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $addressBook = [ |
|
| 315 | - 'id' => $row['id'], |
|
| 316 | - 'uri' => $row['uri'], |
|
| 317 | - 'principaluri' => $row['principaluri'], |
|
| 318 | - '{DAV:}displayname' => $row['displayname'], |
|
| 319 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 320 | - '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 321 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 322 | - ]; |
|
| 323 | - |
|
| 324 | - $this->addOwnerPrincipal($addressBook); |
|
| 325 | - |
|
| 326 | - return $addressBook; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Updates properties for an address book. |
|
| 331 | - * |
|
| 332 | - * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 333 | - * To do the actual updates, you must tell this object which properties |
|
| 334 | - * you're going to process with the handle() method. |
|
| 335 | - * |
|
| 336 | - * Calling the handle method is like telling the PropPatch object "I |
|
| 337 | - * promise I can handle updating this property". |
|
| 338 | - * |
|
| 339 | - * Read the PropPatch documentation for more info and examples. |
|
| 340 | - * |
|
| 341 | - * @param string $addressBookId |
|
| 342 | - * @param \Sabre\DAV\PropPatch $propPatch |
|
| 343 | - * @return void |
|
| 344 | - */ |
|
| 345 | - function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 346 | - $supportedProperties = [ |
|
| 347 | - '{DAV:}displayname', |
|
| 348 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 349 | - ]; |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @suppress SqlInjectionChecker |
|
| 353 | - */ |
|
| 354 | - $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 355 | - |
|
| 356 | - $updates = []; |
|
| 357 | - foreach($mutations as $property=>$newValue) { |
|
| 358 | - |
|
| 359 | - switch($property) { |
|
| 360 | - case '{DAV:}displayname' : |
|
| 361 | - $updates['displayname'] = $newValue; |
|
| 362 | - break; |
|
| 363 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 364 | - $updates['description'] = $newValue; |
|
| 365 | - break; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - $query = $this->db->getQueryBuilder(); |
|
| 369 | - $query->update('addressbooks'); |
|
| 370 | - |
|
| 371 | - foreach($updates as $key=>$value) { |
|
| 372 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 373 | - } |
|
| 374 | - $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 375 | - ->execute(); |
|
| 376 | - |
|
| 377 | - $this->addChange($addressBookId, "", 2); |
|
| 378 | - |
|
| 379 | - return true; |
|
| 380 | - |
|
| 381 | - }); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** |
|
| 385 | - * Creates a new address book |
|
| 386 | - * |
|
| 387 | - * @param string $principalUri |
|
| 388 | - * @param string $url Just the 'basename' of the url. |
|
| 389 | - * @param array $properties |
|
| 390 | - * @return int |
|
| 391 | - * @throws BadRequest |
|
| 392 | - */ |
|
| 393 | - function createAddressBook($principalUri, $url, array $properties) { |
|
| 394 | - $values = [ |
|
| 395 | - 'displayname' => null, |
|
| 396 | - 'description' => null, |
|
| 397 | - 'principaluri' => $principalUri, |
|
| 398 | - 'uri' => $url, |
|
| 399 | - 'synctoken' => 1 |
|
| 400 | - ]; |
|
| 401 | - |
|
| 402 | - foreach($properties as $property=>$newValue) { |
|
| 403 | - |
|
| 404 | - switch($property) { |
|
| 405 | - case '{DAV:}displayname' : |
|
| 406 | - $values['displayname'] = $newValue; |
|
| 407 | - break; |
|
| 408 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 409 | - $values['description'] = $newValue; |
|
| 410 | - break; |
|
| 411 | - default : |
|
| 412 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 418 | - // to work with addressbooks not having a displayname. |
|
| 419 | - if(is_null($values['displayname'])) { |
|
| 420 | - $values['displayname'] = $url; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - $query = $this->db->getQueryBuilder(); |
|
| 424 | - $query->insert('addressbooks') |
|
| 425 | - ->values([ |
|
| 426 | - 'uri' => $query->createParameter('uri'), |
|
| 427 | - 'displayname' => $query->createParameter('displayname'), |
|
| 428 | - 'description' => $query->createParameter('description'), |
|
| 429 | - 'principaluri' => $query->createParameter('principaluri'), |
|
| 430 | - 'synctoken' => $query->createParameter('synctoken'), |
|
| 431 | - ]) |
|
| 432 | - ->setParameters($values) |
|
| 433 | - ->execute(); |
|
| 434 | - |
|
| 435 | - return $query->getLastInsertId(); |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * Deletes an entire addressbook and all its contents |
|
| 440 | - * |
|
| 441 | - * @param mixed $addressBookId |
|
| 442 | - * @return void |
|
| 443 | - */ |
|
| 444 | - function deleteAddressBook($addressBookId) { |
|
| 445 | - $query = $this->db->getQueryBuilder(); |
|
| 446 | - $query->delete('cards') |
|
| 447 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 448 | - ->setParameter('addressbookid', $addressBookId) |
|
| 449 | - ->execute(); |
|
| 450 | - |
|
| 451 | - $query->delete('addressbookchanges') |
|
| 452 | - ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 453 | - ->setParameter('addressbookid', $addressBookId) |
|
| 454 | - ->execute(); |
|
| 455 | - |
|
| 456 | - $query->delete('addressbooks') |
|
| 457 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 458 | - ->setParameter('id', $addressBookId) |
|
| 459 | - ->execute(); |
|
| 460 | - |
|
| 461 | - $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 462 | - |
|
| 463 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 464 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 465 | - ->execute(); |
|
| 466 | - |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * Returns all cards for a specific addressbook id. |
|
| 471 | - * |
|
| 472 | - * This method should return the following properties for each card: |
|
| 473 | - * * carddata - raw vcard data |
|
| 474 | - * * uri - Some unique url |
|
| 475 | - * * lastmodified - A unix timestamp |
|
| 476 | - * |
|
| 477 | - * It's recommended to also return the following properties: |
|
| 478 | - * * etag - A unique etag. This must change every time the card changes. |
|
| 479 | - * * size - The size of the card in bytes. |
|
| 480 | - * |
|
| 481 | - * If these last two properties are provided, less time will be spent |
|
| 482 | - * calculating them. If they are specified, you can also ommit carddata. |
|
| 483 | - * This may speed up certain requests, especially with large cards. |
|
| 484 | - * |
|
| 485 | - * @param mixed $addressBookId |
|
| 486 | - * @return array |
|
| 487 | - */ |
|
| 488 | - function getCards($addressBookId) { |
|
| 489 | - $query = $this->db->getQueryBuilder(); |
|
| 490 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 491 | - ->from('cards') |
|
| 492 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 493 | - |
|
| 494 | - $cards = []; |
|
| 495 | - |
|
| 496 | - $result = $query->execute(); |
|
| 497 | - while($row = $result->fetch()) { |
|
| 498 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 499 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 500 | - $cards[] = $row; |
|
| 501 | - } |
|
| 502 | - $result->closeCursor(); |
|
| 503 | - |
|
| 504 | - return $cards; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Returns a specific card. |
|
| 509 | - * |
|
| 510 | - * The same set of properties must be returned as with getCards. The only |
|
| 511 | - * exception is that 'carddata' is absolutely required. |
|
| 512 | - * |
|
| 513 | - * If the card does not exist, you must return false. |
|
| 514 | - * |
|
| 515 | - * @param mixed $addressBookId |
|
| 516 | - * @param string $cardUri |
|
| 517 | - * @return array |
|
| 518 | - */ |
|
| 519 | - function getCard($addressBookId, $cardUri) { |
|
| 520 | - $query = $this->db->getQueryBuilder(); |
|
| 521 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 522 | - ->from('cards') |
|
| 523 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 524 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 525 | - ->setMaxResults(1); |
|
| 526 | - |
|
| 527 | - $result = $query->execute(); |
|
| 528 | - $row = $result->fetch(); |
|
| 529 | - if (!$row) { |
|
| 530 | - return false; |
|
| 531 | - } |
|
| 532 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 533 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 534 | - |
|
| 535 | - return $row; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Returns a list of cards. |
|
| 540 | - * |
|
| 541 | - * This method should work identical to getCard, but instead return all the |
|
| 542 | - * cards in the list as an array. |
|
| 543 | - * |
|
| 544 | - * If the backend supports this, it may allow for some speed-ups. |
|
| 545 | - * |
|
| 546 | - * @param mixed $addressBookId |
|
| 547 | - * @param string[] $uris |
|
| 548 | - * @return array |
|
| 549 | - */ |
|
| 550 | - function getMultipleCards($addressBookId, array $uris) { |
|
| 551 | - if (empty($uris)) { |
|
| 552 | - return []; |
|
| 553 | - } |
|
| 554 | - |
|
| 555 | - $chunks = array_chunk($uris, 100); |
|
| 556 | - $cards = []; |
|
| 557 | - |
|
| 558 | - $query = $this->db->getQueryBuilder(); |
|
| 559 | - $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 560 | - ->from('cards') |
|
| 561 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 562 | - ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 563 | - |
|
| 564 | - foreach ($chunks as $uris) { |
|
| 565 | - $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 566 | - $result = $query->execute(); |
|
| 567 | - |
|
| 568 | - while ($row = $result->fetch()) { |
|
| 569 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 570 | - $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 571 | - $cards[] = $row; |
|
| 572 | - } |
|
| 573 | - $result->closeCursor(); |
|
| 574 | - } |
|
| 575 | - return $cards; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Creates a new card. |
|
| 580 | - * |
|
| 581 | - * The addressbook id will be passed as the first argument. This is the |
|
| 582 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 583 | - * |
|
| 584 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 585 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 586 | - * |
|
| 587 | - * It is possible to return an ETag from this method. This ETag is for the |
|
| 588 | - * newly created resource, and must be enclosed with double quotes (that |
|
| 589 | - * is, the string itself must contain the double quotes). |
|
| 590 | - * |
|
| 591 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 592 | - * subsequent GET request on the same card does not have the same body, |
|
| 593 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 594 | - * confused. |
|
| 595 | - * |
|
| 596 | - * If you don't return an ETag, you can just return null. |
|
| 597 | - * |
|
| 598 | - * @param mixed $addressBookId |
|
| 599 | - * @param string $cardUri |
|
| 600 | - * @param string $cardData |
|
| 601 | - * @return string |
|
| 602 | - */ |
|
| 603 | - function createCard($addressBookId, $cardUri, $cardData) { |
|
| 604 | - $etag = md5($cardData); |
|
| 605 | - |
|
| 606 | - $query = $this->db->getQueryBuilder(); |
|
| 607 | - $query->insert('cards') |
|
| 608 | - ->values([ |
|
| 609 | - 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 610 | - 'uri' => $query->createNamedParameter($cardUri), |
|
| 611 | - 'lastmodified' => $query->createNamedParameter(time()), |
|
| 612 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 613 | - 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 614 | - 'etag' => $query->createNamedParameter($etag), |
|
| 615 | - ]) |
|
| 616 | - ->execute(); |
|
| 617 | - |
|
| 618 | - $this->addChange($addressBookId, $cardUri, 1); |
|
| 619 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 620 | - |
|
| 621 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 622 | - new GenericEvent(null, [ |
|
| 623 | - 'addressBookId' => $addressBookId, |
|
| 624 | - 'cardUri' => $cardUri, |
|
| 625 | - 'cardData' => $cardData])); |
|
| 626 | - |
|
| 627 | - return '"' . $etag . '"'; |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Updates a card. |
|
| 632 | - * |
|
| 633 | - * The addressbook id will be passed as the first argument. This is the |
|
| 634 | - * same id as it is returned from the getAddressBooksForUser method. |
|
| 635 | - * |
|
| 636 | - * The cardUri is a base uri, and doesn't include the full path. The |
|
| 637 | - * cardData argument is the vcard body, and is passed as a string. |
|
| 638 | - * |
|
| 639 | - * It is possible to return an ETag from this method. This ETag should |
|
| 640 | - * match that of the updated resource, and must be enclosed with double |
|
| 641 | - * quotes (that is: the string itself must contain the actual quotes). |
|
| 642 | - * |
|
| 643 | - * You should only return the ETag if you store the carddata as-is. If a |
|
| 644 | - * subsequent GET request on the same card does not have the same body, |
|
| 645 | - * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 646 | - * confused. |
|
| 647 | - * |
|
| 648 | - * If you don't return an ETag, you can just return null. |
|
| 649 | - * |
|
| 650 | - * @param mixed $addressBookId |
|
| 651 | - * @param string $cardUri |
|
| 652 | - * @param string $cardData |
|
| 653 | - * @return string |
|
| 654 | - */ |
|
| 655 | - function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 656 | - |
|
| 657 | - $etag = md5($cardData); |
|
| 658 | - $query = $this->db->getQueryBuilder(); |
|
| 659 | - $query->update('cards') |
|
| 660 | - ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 661 | - ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 662 | - ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 663 | - ->set('etag', $query->createNamedParameter($etag)) |
|
| 664 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 665 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 666 | - ->execute(); |
|
| 667 | - |
|
| 668 | - $this->addChange($addressBookId, $cardUri, 2); |
|
| 669 | - $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 670 | - |
|
| 671 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 672 | - new GenericEvent(null, [ |
|
| 673 | - 'addressBookId' => $addressBookId, |
|
| 674 | - 'cardUri' => $cardUri, |
|
| 675 | - 'cardData' => $cardData])); |
|
| 676 | - |
|
| 677 | - return '"' . $etag . '"'; |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Deletes a card |
|
| 682 | - * |
|
| 683 | - * @param mixed $addressBookId |
|
| 684 | - * @param string $cardUri |
|
| 685 | - * @return bool |
|
| 686 | - */ |
|
| 687 | - function deleteCard($addressBookId, $cardUri) { |
|
| 688 | - try { |
|
| 689 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 690 | - } catch (\InvalidArgumentException $e) { |
|
| 691 | - $cardId = null; |
|
| 692 | - } |
|
| 693 | - $query = $this->db->getQueryBuilder(); |
|
| 694 | - $ret = $query->delete('cards') |
|
| 695 | - ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 696 | - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 697 | - ->execute(); |
|
| 698 | - |
|
| 699 | - $this->addChange($addressBookId, $cardUri, 3); |
|
| 700 | - |
|
| 701 | - $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 702 | - new GenericEvent(null, [ |
|
| 703 | - 'addressBookId' => $addressBookId, |
|
| 704 | - 'cardUri' => $cardUri])); |
|
| 705 | - |
|
| 706 | - if ($ret === 1) { |
|
| 707 | - if ($cardId !== null) { |
|
| 708 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 709 | - } |
|
| 710 | - return true; |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - return false; |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * The getChanges method returns all the changes that have happened, since |
|
| 718 | - * the specified syncToken in the specified address book. |
|
| 719 | - * |
|
| 720 | - * This function should return an array, such as the following: |
|
| 721 | - * |
|
| 722 | - * [ |
|
| 723 | - * 'syncToken' => 'The current synctoken', |
|
| 724 | - * 'added' => [ |
|
| 725 | - * 'new.txt', |
|
| 726 | - * ], |
|
| 727 | - * 'modified' => [ |
|
| 728 | - * 'modified.txt', |
|
| 729 | - * ], |
|
| 730 | - * 'deleted' => [ |
|
| 731 | - * 'foo.php.bak', |
|
| 732 | - * 'old.txt' |
|
| 733 | - * ] |
|
| 734 | - * ]; |
|
| 735 | - * |
|
| 736 | - * The returned syncToken property should reflect the *current* syncToken |
|
| 737 | - * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 738 | - * property. This is needed here too, to ensure the operation is atomic. |
|
| 739 | - * |
|
| 740 | - * If the $syncToken argument is specified as null, this is an initial |
|
| 741 | - * sync, and all members should be reported. |
|
| 742 | - * |
|
| 743 | - * The modified property is an array of nodenames that have changed since |
|
| 744 | - * the last token. |
|
| 745 | - * |
|
| 746 | - * The deleted property is an array with nodenames, that have been deleted |
|
| 747 | - * from collection. |
|
| 748 | - * |
|
| 749 | - * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 750 | - * 1, you only have to report changes that happened only directly in |
|
| 751 | - * immediate descendants. If it's 2, it should also include changes from |
|
| 752 | - * the nodes below the child collections. (grandchildren) |
|
| 753 | - * |
|
| 754 | - * The $limit argument allows a client to specify how many results should |
|
| 755 | - * be returned at most. If the limit is not specified, it should be treated |
|
| 756 | - * as infinite. |
|
| 757 | - * |
|
| 758 | - * If the limit (infinite or not) is higher than you're willing to return, |
|
| 759 | - * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 760 | - * |
|
| 761 | - * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 762 | - * return null. |
|
| 763 | - * |
|
| 764 | - * The limit is 'suggestive'. You are free to ignore it. |
|
| 765 | - * |
|
| 766 | - * @param string $addressBookId |
|
| 767 | - * @param string $syncToken |
|
| 768 | - * @param int $syncLevel |
|
| 769 | - * @param int $limit |
|
| 770 | - * @return array |
|
| 771 | - */ |
|
| 772 | - function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 773 | - // Current synctoken |
|
| 774 | - $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 775 | - $stmt->execute([ $addressBookId ]); |
|
| 776 | - $currentToken = $stmt->fetchColumn(0); |
|
| 777 | - |
|
| 778 | - if (is_null($currentToken)) return null; |
|
| 779 | - |
|
| 780 | - $result = [ |
|
| 781 | - 'syncToken' => $currentToken, |
|
| 782 | - 'added' => [], |
|
| 783 | - 'modified' => [], |
|
| 784 | - 'deleted' => [], |
|
| 785 | - ]; |
|
| 786 | - |
|
| 787 | - if ($syncToken) { |
|
| 788 | - |
|
| 789 | - $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 790 | - if ($limit>0) { |
|
| 791 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - // Fetching all changes |
|
| 795 | - $stmt = $this->db->prepare($query); |
|
| 796 | - $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 797 | - |
|
| 798 | - $changes = []; |
|
| 799 | - |
|
| 800 | - // This loop ensures that any duplicates are overwritten, only the |
|
| 801 | - // last change on a node is relevant. |
|
| 802 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 803 | - |
|
| 804 | - $changes[$row['uri']] = $row['operation']; |
|
| 805 | - |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - foreach($changes as $uri => $operation) { |
|
| 809 | - |
|
| 810 | - switch($operation) { |
|
| 811 | - case 1: |
|
| 812 | - $result['added'][] = $uri; |
|
| 813 | - break; |
|
| 814 | - case 2: |
|
| 815 | - $result['modified'][] = $uri; |
|
| 816 | - break; |
|
| 817 | - case 3: |
|
| 818 | - $result['deleted'][] = $uri; |
|
| 819 | - break; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - } |
|
| 823 | - } else { |
|
| 824 | - // No synctoken supplied, this is the initial sync. |
|
| 825 | - $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 826 | - $stmt = $this->db->prepare($query); |
|
| 827 | - $stmt->execute([$addressBookId]); |
|
| 828 | - |
|
| 829 | - $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 830 | - } |
|
| 831 | - return $result; |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - /** |
|
| 835 | - * Adds a change record to the addressbookchanges table. |
|
| 836 | - * |
|
| 837 | - * @param mixed $addressBookId |
|
| 838 | - * @param string $objectUri |
|
| 839 | - * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 840 | - * @return void |
|
| 841 | - */ |
|
| 842 | - protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 843 | - $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 844 | - $stmt = $this->db->prepare($sql); |
|
| 845 | - $stmt->execute([ |
|
| 846 | - $objectUri, |
|
| 847 | - $addressBookId, |
|
| 848 | - $operation, |
|
| 849 | - $addressBookId |
|
| 850 | - ]); |
|
| 851 | - $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 852 | - $stmt->execute([ |
|
| 853 | - $addressBookId |
|
| 854 | - ]); |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - private function readBlob($cardData) { |
|
| 858 | - if (is_resource($cardData)) { |
|
| 859 | - return stream_get_contents($cardData); |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - return $cardData; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - /** |
|
| 866 | - * @param IShareable $shareable |
|
| 867 | - * @param string[] $add |
|
| 868 | - * @param string[] $remove |
|
| 869 | - */ |
|
| 870 | - public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 871 | - $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * search contact |
|
| 876 | - * |
|
| 877 | - * @param int $addressBookId |
|
| 878 | - * @param string $pattern which should match within the $searchProperties |
|
| 879 | - * @param array $searchProperties defines the properties within the query pattern should match |
|
| 880 | - * @return array an array of contacts which are arrays of key-value-pairs |
|
| 881 | - */ |
|
| 882 | - public function search($addressBookId, $pattern, $searchProperties) { |
|
| 883 | - $query = $this->db->getQueryBuilder(); |
|
| 884 | - $query2 = $this->db->getQueryBuilder(); |
|
| 885 | - |
|
| 886 | - $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 887 | - $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 888 | - $or = $query2->expr()->orX(); |
|
| 889 | - foreach ($searchProperties as $property) { |
|
| 890 | - $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
| 891 | - } |
|
| 892 | - $query2->andWhere($or); |
|
| 893 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 894 | - |
|
| 895 | - $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 896 | - ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 897 | - |
|
| 898 | - $result = $query->execute(); |
|
| 899 | - $cards = $result->fetchAll(); |
|
| 900 | - |
|
| 901 | - $result->closeCursor(); |
|
| 902 | - |
|
| 903 | - return array_map(function($array) { |
|
| 904 | - $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 905 | - return $array; |
|
| 906 | - }, $cards); |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - /** |
|
| 910 | - * @param int $bookId |
|
| 911 | - * @param string $name |
|
| 912 | - * @return array |
|
| 913 | - */ |
|
| 914 | - public function collectCardProperties($bookId, $name) { |
|
| 915 | - $query = $this->db->getQueryBuilder(); |
|
| 916 | - $result = $query->selectDistinct('value') |
|
| 917 | - ->from($this->dbCardsPropertiesTable) |
|
| 918 | - ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 919 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 920 | - ->execute(); |
|
| 921 | - |
|
| 922 | - $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 923 | - $result->closeCursor(); |
|
| 924 | - |
|
| 925 | - return $all; |
|
| 926 | - } |
|
| 927 | - |
|
| 928 | - /** |
|
| 929 | - * get URI from a given contact |
|
| 930 | - * |
|
| 931 | - * @param int $id |
|
| 932 | - * @return string |
|
| 933 | - */ |
|
| 934 | - public function getCardUri($id) { |
|
| 935 | - $query = $this->db->getQueryBuilder(); |
|
| 936 | - $query->select('uri')->from($this->dbCardsTable) |
|
| 937 | - ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 938 | - ->setParameter('id', $id); |
|
| 939 | - |
|
| 940 | - $result = $query->execute(); |
|
| 941 | - $uri = $result->fetch(); |
|
| 942 | - $result->closeCursor(); |
|
| 943 | - |
|
| 944 | - if (!isset($uri['uri'])) { |
|
| 945 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 946 | - } |
|
| 947 | - |
|
| 948 | - return $uri['uri']; |
|
| 949 | - } |
|
| 950 | - |
|
| 951 | - /** |
|
| 952 | - * return contact with the given URI |
|
| 953 | - * |
|
| 954 | - * @param int $addressBookId |
|
| 955 | - * @param string $uri |
|
| 956 | - * @returns array |
|
| 957 | - */ |
|
| 958 | - public function getContact($addressBookId, $uri) { |
|
| 959 | - $result = []; |
|
| 960 | - $query = $this->db->getQueryBuilder(); |
|
| 961 | - $query->select('*')->from($this->dbCardsTable) |
|
| 962 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 963 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 964 | - $queryResult = $query->execute(); |
|
| 965 | - $contact = $queryResult->fetch(); |
|
| 966 | - $queryResult->closeCursor(); |
|
| 967 | - |
|
| 968 | - if (is_array($contact)) { |
|
| 969 | - $result = $contact; |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - return $result; |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - /** |
|
| 976 | - * Returns the list of people whom this address book is shared with. |
|
| 977 | - * |
|
| 978 | - * Every element in this array should have the following properties: |
|
| 979 | - * * href - Often a mailto: address |
|
| 980 | - * * commonName - Optional, for example a first + last name |
|
| 981 | - * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 982 | - * * readOnly - boolean |
|
| 983 | - * * summary - Optional, a description for the share |
|
| 984 | - * |
|
| 985 | - * @return array |
|
| 986 | - */ |
|
| 987 | - public function getShares($addressBookId) { |
|
| 988 | - return $this->sharingBackend->getShares($addressBookId); |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - /** |
|
| 992 | - * update properties table |
|
| 993 | - * |
|
| 994 | - * @param int $addressBookId |
|
| 995 | - * @param string $cardUri |
|
| 996 | - * @param string $vCardSerialized |
|
| 997 | - */ |
|
| 998 | - protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 999 | - $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1000 | - $vCard = $this->readCard($vCardSerialized); |
|
| 1001 | - |
|
| 1002 | - $this->purgeProperties($addressBookId, $cardId); |
|
| 1003 | - |
|
| 1004 | - $query = $this->db->getQueryBuilder(); |
|
| 1005 | - $query->insert($this->dbCardsPropertiesTable) |
|
| 1006 | - ->values( |
|
| 1007 | - [ |
|
| 1008 | - 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1009 | - 'cardid' => $query->createNamedParameter($cardId), |
|
| 1010 | - 'name' => $query->createParameter('name'), |
|
| 1011 | - 'value' => $query->createParameter('value'), |
|
| 1012 | - 'preferred' => $query->createParameter('preferred') |
|
| 1013 | - ] |
|
| 1014 | - ); |
|
| 1015 | - |
|
| 1016 | - foreach ($vCard->children() as $property) { |
|
| 1017 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 1018 | - continue; |
|
| 1019 | - } |
|
| 1020 | - $preferred = 0; |
|
| 1021 | - foreach($property->parameters as $parameter) { |
|
| 1022 | - if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
| 1023 | - $preferred = 1; |
|
| 1024 | - break; |
|
| 1025 | - } |
|
| 1026 | - } |
|
| 1027 | - $query->setParameter('name', $property->name); |
|
| 1028 | - $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 1029 | - $query->setParameter('preferred', $preferred); |
|
| 1030 | - $query->execute(); |
|
| 1031 | - } |
|
| 1032 | - } |
|
| 1033 | - |
|
| 1034 | - /** |
|
| 1035 | - * read vCard data into a vCard object |
|
| 1036 | - * |
|
| 1037 | - * @param string $cardData |
|
| 1038 | - * @return VCard |
|
| 1039 | - */ |
|
| 1040 | - protected function readCard($cardData) { |
|
| 1041 | - return Reader::read($cardData); |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - /** |
|
| 1045 | - * delete all properties from a given card |
|
| 1046 | - * |
|
| 1047 | - * @param int $addressBookId |
|
| 1048 | - * @param int $cardId |
|
| 1049 | - */ |
|
| 1050 | - protected function purgeProperties($addressBookId, $cardId) { |
|
| 1051 | - $query = $this->db->getQueryBuilder(); |
|
| 1052 | - $query->delete($this->dbCardsPropertiesTable) |
|
| 1053 | - ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1054 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1055 | - $query->execute(); |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - /** |
|
| 1059 | - * get ID from a given contact |
|
| 1060 | - * |
|
| 1061 | - * @param int $addressBookId |
|
| 1062 | - * @param string $uri |
|
| 1063 | - * @return int |
|
| 1064 | - */ |
|
| 1065 | - protected function getCardId($addressBookId, $uri) { |
|
| 1066 | - $query = $this->db->getQueryBuilder(); |
|
| 1067 | - $query->select('id')->from($this->dbCardsTable) |
|
| 1068 | - ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1069 | - ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1070 | - |
|
| 1071 | - $result = $query->execute(); |
|
| 1072 | - $cardIds = $result->fetch(); |
|
| 1073 | - $result->closeCursor(); |
|
| 1074 | - |
|
| 1075 | - if (!isset($cardIds['id'])) { |
|
| 1076 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - return (int)$cardIds['id']; |
|
| 1080 | - } |
|
| 1081 | - |
|
| 1082 | - /** |
|
| 1083 | - * For shared address books the sharee is set in the ACL of the address book |
|
| 1084 | - * @param $addressBookId |
|
| 1085 | - * @param $acl |
|
| 1086 | - * @return array |
|
| 1087 | - */ |
|
| 1088 | - public function applyShareAcl($addressBookId, $acl) { |
|
| 1089 | - return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - private function convertPrincipal($principalUri, $toV2) { |
|
| 1093 | - if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1094 | - list(, $name) = URLUtil::splitPath($principalUri); |
|
| 1095 | - if ($toV2 === true) { |
|
| 1096 | - return "principals/users/$name"; |
|
| 1097 | - } |
|
| 1098 | - return "principals/$name"; |
|
| 1099 | - } |
|
| 1100 | - return $principalUri; |
|
| 1101 | - } |
|
| 1102 | - |
|
| 1103 | - private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1104 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1105 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1106 | - if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1107 | - $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1108 | - } else { |
|
| 1109 | - $uri = $addressbookInfo['principaluri']; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1113 | - if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1114 | - $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1115 | - } |
|
| 1116 | - } |
|
| 51 | + const PERSONAL_ADDRESSBOOK_URI = 'contacts'; |
|
| 52 | + const PERSONAL_ADDRESSBOOK_NAME = 'Contacts'; |
|
| 53 | + |
|
| 54 | + /** @var Principal */ |
|
| 55 | + private $principalBackend; |
|
| 56 | + |
|
| 57 | + /** @var string */ |
|
| 58 | + private $dbCardsTable = 'cards'; |
|
| 59 | + |
|
| 60 | + /** @var string */ |
|
| 61 | + private $dbCardsPropertiesTable = 'cards_properties'; |
|
| 62 | + |
|
| 63 | + /** @var IDBConnection */ |
|
| 64 | + private $db; |
|
| 65 | + |
|
| 66 | + /** @var Backend */ |
|
| 67 | + private $sharingBackend; |
|
| 68 | + |
|
| 69 | + /** @var array properties to index */ |
|
| 70 | + public static $indexProperties = array( |
|
| 71 | + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', |
|
| 72 | + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO', 'CLOUD'); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @var string[] Map of uid => display name |
|
| 76 | + */ |
|
| 77 | + protected $userDisplayNames; |
|
| 78 | + |
|
| 79 | + /** @var IUserManager */ |
|
| 80 | + private $userManager; |
|
| 81 | + |
|
| 82 | + /** @var EventDispatcherInterface */ |
|
| 83 | + private $dispatcher; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * CardDavBackend constructor. |
|
| 87 | + * |
|
| 88 | + * @param IDBConnection $db |
|
| 89 | + * @param Principal $principalBackend |
|
| 90 | + * @param IUserManager $userManager |
|
| 91 | + * @param EventDispatcherInterface $dispatcher |
|
| 92 | + */ |
|
| 93 | + public function __construct(IDBConnection $db, |
|
| 94 | + Principal $principalBackend, |
|
| 95 | + IUserManager $userManager, |
|
| 96 | + EventDispatcherInterface $dispatcher) { |
|
| 97 | + $this->db = $db; |
|
| 98 | + $this->principalBackend = $principalBackend; |
|
| 99 | + $this->userManager = $userManager; |
|
| 100 | + $this->dispatcher = $dispatcher; |
|
| 101 | + $this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook'); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Return the number of address books for a principal |
|
| 106 | + * |
|
| 107 | + * @param $principalUri |
|
| 108 | + * @return int |
|
| 109 | + */ |
|
| 110 | + public function getAddressBooksForUserCount($principalUri) { |
|
| 111 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 112 | + $query = $this->db->getQueryBuilder(); |
|
| 113 | + $query->select($query->createFunction('COUNT(*)')) |
|
| 114 | + ->from('addressbooks') |
|
| 115 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 116 | + |
|
| 117 | + return (int)$query->execute()->fetchColumn(); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Returns the list of address books for a specific user. |
|
| 122 | + * |
|
| 123 | + * Every addressbook should have the following properties: |
|
| 124 | + * id - an arbitrary unique id |
|
| 125 | + * uri - the 'basename' part of the url |
|
| 126 | + * principaluri - Same as the passed parameter |
|
| 127 | + * |
|
| 128 | + * Any additional clark-notation property may be passed besides this. Some |
|
| 129 | + * common ones are : |
|
| 130 | + * {DAV:}displayname |
|
| 131 | + * {urn:ietf:params:xml:ns:carddav}addressbook-description |
|
| 132 | + * {http://calendarserver.org/ns/}getctag |
|
| 133 | + * |
|
| 134 | + * @param string $principalUri |
|
| 135 | + * @return array |
|
| 136 | + */ |
|
| 137 | + function getAddressBooksForUser($principalUri) { |
|
| 138 | + $principalUriOriginal = $principalUri; |
|
| 139 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 140 | + $query = $this->db->getQueryBuilder(); |
|
| 141 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 142 | + ->from('addressbooks') |
|
| 143 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 144 | + |
|
| 145 | + $addressBooks = []; |
|
| 146 | + |
|
| 147 | + $result = $query->execute(); |
|
| 148 | + while($row = $result->fetch()) { |
|
| 149 | + $addressBooks[$row['id']] = [ |
|
| 150 | + 'id' => $row['id'], |
|
| 151 | + 'uri' => $row['uri'], |
|
| 152 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 153 | + '{DAV:}displayname' => $row['displayname'], |
|
| 154 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 155 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 156 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 157 | + ]; |
|
| 158 | + |
|
| 159 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 160 | + } |
|
| 161 | + $result->closeCursor(); |
|
| 162 | + |
|
| 163 | + // query for shared calendars |
|
| 164 | + $principals = $this->principalBackend->getGroupMembership($principalUriOriginal, true); |
|
| 165 | + $principals = array_map(function($principal) { |
|
| 166 | + return urldecode($principal); |
|
| 167 | + }, $principals); |
|
| 168 | + $principals[]= $principalUri; |
|
| 169 | + |
|
| 170 | + $query = $this->db->getQueryBuilder(); |
|
| 171 | + $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
|
| 172 | + ->from('dav_shares', 's') |
|
| 173 | + ->join('s', 'addressbooks', 'a', $query->expr()->eq('s.resourceid', 'a.id')) |
|
| 174 | + ->where($query->expr()->in('s.principaluri', $query->createParameter('principaluri'))) |
|
| 175 | + ->andWhere($query->expr()->eq('s.type', $query->createParameter('type'))) |
|
| 176 | + ->setParameter('type', 'addressbook') |
|
| 177 | + ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
|
| 178 | + ->execute(); |
|
| 179 | + |
|
| 180 | + $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 181 | + while($row = $result->fetch()) { |
|
| 182 | + if ($row['principaluri'] === $principalUri) { |
|
| 183 | + continue; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + $readOnly = (int) $row['access'] === Backend::ACCESS_READ; |
|
| 187 | + if (isset($addressBooks[$row['id']])) { |
|
| 188 | + if ($readOnly) { |
|
| 189 | + // New share can not have more permissions then the old one. |
|
| 190 | + continue; |
|
| 191 | + } |
|
| 192 | + if (isset($addressBooks[$row['id']][$readOnlyPropertyName]) && |
|
| 193 | + $addressBooks[$row['id']][$readOnlyPropertyName] === 0) { |
|
| 194 | + // Old share is already read-write, no more permissions can be gained |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + list(, $name) = URLUtil::splitPath($row['principaluri']); |
|
| 200 | + $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 201 | + $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 202 | + |
|
| 203 | + $addressBooks[$row['id']] = [ |
|
| 204 | + 'id' => $row['id'], |
|
| 205 | + 'uri' => $uri, |
|
| 206 | + 'principaluri' => $principalUriOriginal, |
|
| 207 | + '{DAV:}displayname' => $displayName, |
|
| 208 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 209 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 210 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 211 | + '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 212 | + $readOnlyPropertyName => $readOnly, |
|
| 213 | + ]; |
|
| 214 | + |
|
| 215 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 216 | + } |
|
| 217 | + $result->closeCursor(); |
|
| 218 | + |
|
| 219 | + return array_values($addressBooks); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + public function getUsersOwnAddressBooks($principalUri) { |
|
| 223 | + $principalUri = $this->convertPrincipal($principalUri, true); |
|
| 224 | + $query = $this->db->getQueryBuilder(); |
|
| 225 | + $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 226 | + ->from('addressbooks') |
|
| 227 | + ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
|
| 228 | + |
|
| 229 | + $addressBooks = []; |
|
| 230 | + |
|
| 231 | + $result = $query->execute(); |
|
| 232 | + while($row = $result->fetch()) { |
|
| 233 | + $addressBooks[$row['id']] = [ |
|
| 234 | + 'id' => $row['id'], |
|
| 235 | + 'uri' => $row['uri'], |
|
| 236 | + 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
|
| 237 | + '{DAV:}displayname' => $row['displayname'], |
|
| 238 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 239 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 240 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 241 | + ]; |
|
| 242 | + |
|
| 243 | + $this->addOwnerPrincipal($addressBooks[$row['id']]); |
|
| 244 | + } |
|
| 245 | + $result->closeCursor(); |
|
| 246 | + |
|
| 247 | + return array_values($addressBooks); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + private function getUserDisplayName($uid) { |
|
| 251 | + if (!isset($this->userDisplayNames[$uid])) { |
|
| 252 | + $user = $this->userManager->get($uid); |
|
| 253 | + |
|
| 254 | + if ($user instanceof IUser) { |
|
| 255 | + $this->userDisplayNames[$uid] = $user->getDisplayName(); |
|
| 256 | + } else { |
|
| 257 | + $this->userDisplayNames[$uid] = $uid; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + return $this->userDisplayNames[$uid]; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param int $addressBookId |
|
| 266 | + */ |
|
| 267 | + public function getAddressBookById($addressBookId) { |
|
| 268 | + $query = $this->db->getQueryBuilder(); |
|
| 269 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 270 | + ->from('addressbooks') |
|
| 271 | + ->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 272 | + ->execute(); |
|
| 273 | + |
|
| 274 | + $row = $result->fetch(); |
|
| 275 | + $result->closeCursor(); |
|
| 276 | + if ($row === false) { |
|
| 277 | + return null; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + $addressBook = [ |
|
| 281 | + 'id' => $row['id'], |
|
| 282 | + 'uri' => $row['uri'], |
|
| 283 | + 'principaluri' => $row['principaluri'], |
|
| 284 | + '{DAV:}displayname' => $row['displayname'], |
|
| 285 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 286 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 287 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 288 | + ]; |
|
| 289 | + |
|
| 290 | + $this->addOwnerPrincipal($addressBook); |
|
| 291 | + |
|
| 292 | + return $addressBook; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * @param $addressBookUri |
|
| 297 | + * @return array|null |
|
| 298 | + */ |
|
| 299 | + public function getAddressBooksByUri($principal, $addressBookUri) { |
|
| 300 | + $query = $this->db->getQueryBuilder(); |
|
| 301 | + $result = $query->select(['id', 'uri', 'displayname', 'principaluri', 'description', 'synctoken']) |
|
| 302 | + ->from('addressbooks') |
|
| 303 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($addressBookUri))) |
|
| 304 | + ->andWhere($query->expr()->eq('principaluri', $query->createNamedParameter($principal))) |
|
| 305 | + ->setMaxResults(1) |
|
| 306 | + ->execute(); |
|
| 307 | + |
|
| 308 | + $row = $result->fetch(); |
|
| 309 | + $result->closeCursor(); |
|
| 310 | + if ($row === false) { |
|
| 311 | + return null; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $addressBook = [ |
|
| 315 | + 'id' => $row['id'], |
|
| 316 | + 'uri' => $row['uri'], |
|
| 317 | + 'principaluri' => $row['principaluri'], |
|
| 318 | + '{DAV:}displayname' => $row['displayname'], |
|
| 319 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 320 | + '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
|
| 321 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 322 | + ]; |
|
| 323 | + |
|
| 324 | + $this->addOwnerPrincipal($addressBook); |
|
| 325 | + |
|
| 326 | + return $addressBook; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Updates properties for an address book. |
|
| 331 | + * |
|
| 332 | + * The list of mutations is stored in a Sabre\DAV\PropPatch object. |
|
| 333 | + * To do the actual updates, you must tell this object which properties |
|
| 334 | + * you're going to process with the handle() method. |
|
| 335 | + * |
|
| 336 | + * Calling the handle method is like telling the PropPatch object "I |
|
| 337 | + * promise I can handle updating this property". |
|
| 338 | + * |
|
| 339 | + * Read the PropPatch documentation for more info and examples. |
|
| 340 | + * |
|
| 341 | + * @param string $addressBookId |
|
| 342 | + * @param \Sabre\DAV\PropPatch $propPatch |
|
| 343 | + * @return void |
|
| 344 | + */ |
|
| 345 | + function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
|
| 346 | + $supportedProperties = [ |
|
| 347 | + '{DAV:}displayname', |
|
| 348 | + '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 349 | + ]; |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @suppress SqlInjectionChecker |
|
| 353 | + */ |
|
| 354 | + $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
|
| 355 | + |
|
| 356 | + $updates = []; |
|
| 357 | + foreach($mutations as $property=>$newValue) { |
|
| 358 | + |
|
| 359 | + switch($property) { |
|
| 360 | + case '{DAV:}displayname' : |
|
| 361 | + $updates['displayname'] = $newValue; |
|
| 362 | + break; |
|
| 363 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 364 | + $updates['description'] = $newValue; |
|
| 365 | + break; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + $query = $this->db->getQueryBuilder(); |
|
| 369 | + $query->update('addressbooks'); |
|
| 370 | + |
|
| 371 | + foreach($updates as $key=>$value) { |
|
| 372 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 373 | + } |
|
| 374 | + $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
|
| 375 | + ->execute(); |
|
| 376 | + |
|
| 377 | + $this->addChange($addressBookId, "", 2); |
|
| 378 | + |
|
| 379 | + return true; |
|
| 380 | + |
|
| 381 | + }); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** |
|
| 385 | + * Creates a new address book |
|
| 386 | + * |
|
| 387 | + * @param string $principalUri |
|
| 388 | + * @param string $url Just the 'basename' of the url. |
|
| 389 | + * @param array $properties |
|
| 390 | + * @return int |
|
| 391 | + * @throws BadRequest |
|
| 392 | + */ |
|
| 393 | + function createAddressBook($principalUri, $url, array $properties) { |
|
| 394 | + $values = [ |
|
| 395 | + 'displayname' => null, |
|
| 396 | + 'description' => null, |
|
| 397 | + 'principaluri' => $principalUri, |
|
| 398 | + 'uri' => $url, |
|
| 399 | + 'synctoken' => 1 |
|
| 400 | + ]; |
|
| 401 | + |
|
| 402 | + foreach($properties as $property=>$newValue) { |
|
| 403 | + |
|
| 404 | + switch($property) { |
|
| 405 | + case '{DAV:}displayname' : |
|
| 406 | + $values['displayname'] = $newValue; |
|
| 407 | + break; |
|
| 408 | + case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 409 | + $values['description'] = $newValue; |
|
| 410 | + break; |
|
| 411 | + default : |
|
| 412 | + throw new BadRequest('Unknown property: ' . $property); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + // Fallback to make sure the displayname is set. Some clients may refuse |
|
| 418 | + // to work with addressbooks not having a displayname. |
|
| 419 | + if(is_null($values['displayname'])) { |
|
| 420 | + $values['displayname'] = $url; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + $query = $this->db->getQueryBuilder(); |
|
| 424 | + $query->insert('addressbooks') |
|
| 425 | + ->values([ |
|
| 426 | + 'uri' => $query->createParameter('uri'), |
|
| 427 | + 'displayname' => $query->createParameter('displayname'), |
|
| 428 | + 'description' => $query->createParameter('description'), |
|
| 429 | + 'principaluri' => $query->createParameter('principaluri'), |
|
| 430 | + 'synctoken' => $query->createParameter('synctoken'), |
|
| 431 | + ]) |
|
| 432 | + ->setParameters($values) |
|
| 433 | + ->execute(); |
|
| 434 | + |
|
| 435 | + return $query->getLastInsertId(); |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * Deletes an entire addressbook and all its contents |
|
| 440 | + * |
|
| 441 | + * @param mixed $addressBookId |
|
| 442 | + * @return void |
|
| 443 | + */ |
|
| 444 | + function deleteAddressBook($addressBookId) { |
|
| 445 | + $query = $this->db->getQueryBuilder(); |
|
| 446 | + $query->delete('cards') |
|
| 447 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 448 | + ->setParameter('addressbookid', $addressBookId) |
|
| 449 | + ->execute(); |
|
| 450 | + |
|
| 451 | + $query->delete('addressbookchanges') |
|
| 452 | + ->where($query->expr()->eq('addressbookid', $query->createParameter('addressbookid'))) |
|
| 453 | + ->setParameter('addressbookid', $addressBookId) |
|
| 454 | + ->execute(); |
|
| 455 | + |
|
| 456 | + $query->delete('addressbooks') |
|
| 457 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 458 | + ->setParameter('id', $addressBookId) |
|
| 459 | + ->execute(); |
|
| 460 | + |
|
| 461 | + $this->sharingBackend->deleteAllShares($addressBookId); |
|
| 462 | + |
|
| 463 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 464 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 465 | + ->execute(); |
|
| 466 | + |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * Returns all cards for a specific addressbook id. |
|
| 471 | + * |
|
| 472 | + * This method should return the following properties for each card: |
|
| 473 | + * * carddata - raw vcard data |
|
| 474 | + * * uri - Some unique url |
|
| 475 | + * * lastmodified - A unix timestamp |
|
| 476 | + * |
|
| 477 | + * It's recommended to also return the following properties: |
|
| 478 | + * * etag - A unique etag. This must change every time the card changes. |
|
| 479 | + * * size - The size of the card in bytes. |
|
| 480 | + * |
|
| 481 | + * If these last two properties are provided, less time will be spent |
|
| 482 | + * calculating them. If they are specified, you can also ommit carddata. |
|
| 483 | + * This may speed up certain requests, especially with large cards. |
|
| 484 | + * |
|
| 485 | + * @param mixed $addressBookId |
|
| 486 | + * @return array |
|
| 487 | + */ |
|
| 488 | + function getCards($addressBookId) { |
|
| 489 | + $query = $this->db->getQueryBuilder(); |
|
| 490 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 491 | + ->from('cards') |
|
| 492 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 493 | + |
|
| 494 | + $cards = []; |
|
| 495 | + |
|
| 496 | + $result = $query->execute(); |
|
| 497 | + while($row = $result->fetch()) { |
|
| 498 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 499 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 500 | + $cards[] = $row; |
|
| 501 | + } |
|
| 502 | + $result->closeCursor(); |
|
| 503 | + |
|
| 504 | + return $cards; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Returns a specific card. |
|
| 509 | + * |
|
| 510 | + * The same set of properties must be returned as with getCards. The only |
|
| 511 | + * exception is that 'carddata' is absolutely required. |
|
| 512 | + * |
|
| 513 | + * If the card does not exist, you must return false. |
|
| 514 | + * |
|
| 515 | + * @param mixed $addressBookId |
|
| 516 | + * @param string $cardUri |
|
| 517 | + * @return array |
|
| 518 | + */ |
|
| 519 | + function getCard($addressBookId, $cardUri) { |
|
| 520 | + $query = $this->db->getQueryBuilder(); |
|
| 521 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 522 | + ->from('cards') |
|
| 523 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 524 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 525 | + ->setMaxResults(1); |
|
| 526 | + |
|
| 527 | + $result = $query->execute(); |
|
| 528 | + $row = $result->fetch(); |
|
| 529 | + if (!$row) { |
|
| 530 | + return false; |
|
| 531 | + } |
|
| 532 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 533 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 534 | + |
|
| 535 | + return $row; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Returns a list of cards. |
|
| 540 | + * |
|
| 541 | + * This method should work identical to getCard, but instead return all the |
|
| 542 | + * cards in the list as an array. |
|
| 543 | + * |
|
| 544 | + * If the backend supports this, it may allow for some speed-ups. |
|
| 545 | + * |
|
| 546 | + * @param mixed $addressBookId |
|
| 547 | + * @param string[] $uris |
|
| 548 | + * @return array |
|
| 549 | + */ |
|
| 550 | + function getMultipleCards($addressBookId, array $uris) { |
|
| 551 | + if (empty($uris)) { |
|
| 552 | + return []; |
|
| 553 | + } |
|
| 554 | + |
|
| 555 | + $chunks = array_chunk($uris, 100); |
|
| 556 | + $cards = []; |
|
| 557 | + |
|
| 558 | + $query = $this->db->getQueryBuilder(); |
|
| 559 | + $query->select(['id', 'uri', 'lastmodified', 'etag', 'size', 'carddata']) |
|
| 560 | + ->from('cards') |
|
| 561 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 562 | + ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); |
|
| 563 | + |
|
| 564 | + foreach ($chunks as $uris) { |
|
| 565 | + $query->setParameter('uri', $uris, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 566 | + $result = $query->execute(); |
|
| 567 | + |
|
| 568 | + while ($row = $result->fetch()) { |
|
| 569 | + $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 570 | + $row['carddata'] = $this->readBlob($row['carddata']); |
|
| 571 | + $cards[] = $row; |
|
| 572 | + } |
|
| 573 | + $result->closeCursor(); |
|
| 574 | + } |
|
| 575 | + return $cards; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Creates a new card. |
|
| 580 | + * |
|
| 581 | + * The addressbook id will be passed as the first argument. This is the |
|
| 582 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 583 | + * |
|
| 584 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 585 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 586 | + * |
|
| 587 | + * It is possible to return an ETag from this method. This ETag is for the |
|
| 588 | + * newly created resource, and must be enclosed with double quotes (that |
|
| 589 | + * is, the string itself must contain the double quotes). |
|
| 590 | + * |
|
| 591 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 592 | + * subsequent GET request on the same card does not have the same body, |
|
| 593 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 594 | + * confused. |
|
| 595 | + * |
|
| 596 | + * If you don't return an ETag, you can just return null. |
|
| 597 | + * |
|
| 598 | + * @param mixed $addressBookId |
|
| 599 | + * @param string $cardUri |
|
| 600 | + * @param string $cardData |
|
| 601 | + * @return string |
|
| 602 | + */ |
|
| 603 | + function createCard($addressBookId, $cardUri, $cardData) { |
|
| 604 | + $etag = md5($cardData); |
|
| 605 | + |
|
| 606 | + $query = $this->db->getQueryBuilder(); |
|
| 607 | + $query->insert('cards') |
|
| 608 | + ->values([ |
|
| 609 | + 'carddata' => $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB), |
|
| 610 | + 'uri' => $query->createNamedParameter($cardUri), |
|
| 611 | + 'lastmodified' => $query->createNamedParameter(time()), |
|
| 612 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 613 | + 'size' => $query->createNamedParameter(strlen($cardData)), |
|
| 614 | + 'etag' => $query->createNamedParameter($etag), |
|
| 615 | + ]) |
|
| 616 | + ->execute(); |
|
| 617 | + |
|
| 618 | + $this->addChange($addressBookId, $cardUri, 1); |
|
| 619 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 620 | + |
|
| 621 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::createCard', |
|
| 622 | + new GenericEvent(null, [ |
|
| 623 | + 'addressBookId' => $addressBookId, |
|
| 624 | + 'cardUri' => $cardUri, |
|
| 625 | + 'cardData' => $cardData])); |
|
| 626 | + |
|
| 627 | + return '"' . $etag . '"'; |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Updates a card. |
|
| 632 | + * |
|
| 633 | + * The addressbook id will be passed as the first argument. This is the |
|
| 634 | + * same id as it is returned from the getAddressBooksForUser method. |
|
| 635 | + * |
|
| 636 | + * The cardUri is a base uri, and doesn't include the full path. The |
|
| 637 | + * cardData argument is the vcard body, and is passed as a string. |
|
| 638 | + * |
|
| 639 | + * It is possible to return an ETag from this method. This ETag should |
|
| 640 | + * match that of the updated resource, and must be enclosed with double |
|
| 641 | + * quotes (that is: the string itself must contain the actual quotes). |
|
| 642 | + * |
|
| 643 | + * You should only return the ETag if you store the carddata as-is. If a |
|
| 644 | + * subsequent GET request on the same card does not have the same body, |
|
| 645 | + * byte-by-byte and you did return an ETag here, clients tend to get |
|
| 646 | + * confused. |
|
| 647 | + * |
|
| 648 | + * If you don't return an ETag, you can just return null. |
|
| 649 | + * |
|
| 650 | + * @param mixed $addressBookId |
|
| 651 | + * @param string $cardUri |
|
| 652 | + * @param string $cardData |
|
| 653 | + * @return string |
|
| 654 | + */ |
|
| 655 | + function updateCard($addressBookId, $cardUri, $cardData) { |
|
| 656 | + |
|
| 657 | + $etag = md5($cardData); |
|
| 658 | + $query = $this->db->getQueryBuilder(); |
|
| 659 | + $query->update('cards') |
|
| 660 | + ->set('carddata', $query->createNamedParameter($cardData, IQueryBuilder::PARAM_LOB)) |
|
| 661 | + ->set('lastmodified', $query->createNamedParameter(time())) |
|
| 662 | + ->set('size', $query->createNamedParameter(strlen($cardData))) |
|
| 663 | + ->set('etag', $query->createNamedParameter($etag)) |
|
| 664 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 665 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 666 | + ->execute(); |
|
| 667 | + |
|
| 668 | + $this->addChange($addressBookId, $cardUri, 2); |
|
| 669 | + $this->updateProperties($addressBookId, $cardUri, $cardData); |
|
| 670 | + |
|
| 671 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::updateCard', |
|
| 672 | + new GenericEvent(null, [ |
|
| 673 | + 'addressBookId' => $addressBookId, |
|
| 674 | + 'cardUri' => $cardUri, |
|
| 675 | + 'cardData' => $cardData])); |
|
| 676 | + |
|
| 677 | + return '"' . $etag . '"'; |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Deletes a card |
|
| 682 | + * |
|
| 683 | + * @param mixed $addressBookId |
|
| 684 | + * @param string $cardUri |
|
| 685 | + * @return bool |
|
| 686 | + */ |
|
| 687 | + function deleteCard($addressBookId, $cardUri) { |
|
| 688 | + try { |
|
| 689 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 690 | + } catch (\InvalidArgumentException $e) { |
|
| 691 | + $cardId = null; |
|
| 692 | + } |
|
| 693 | + $query = $this->db->getQueryBuilder(); |
|
| 694 | + $ret = $query->delete('cards') |
|
| 695 | + ->where($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))) |
|
| 696 | + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($cardUri))) |
|
| 697 | + ->execute(); |
|
| 698 | + |
|
| 699 | + $this->addChange($addressBookId, $cardUri, 3); |
|
| 700 | + |
|
| 701 | + $this->dispatcher->dispatch('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', |
|
| 702 | + new GenericEvent(null, [ |
|
| 703 | + 'addressBookId' => $addressBookId, |
|
| 704 | + 'cardUri' => $cardUri])); |
|
| 705 | + |
|
| 706 | + if ($ret === 1) { |
|
| 707 | + if ($cardId !== null) { |
|
| 708 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 709 | + } |
|
| 710 | + return true; |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + return false; |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * The getChanges method returns all the changes that have happened, since |
|
| 718 | + * the specified syncToken in the specified address book. |
|
| 719 | + * |
|
| 720 | + * This function should return an array, such as the following: |
|
| 721 | + * |
|
| 722 | + * [ |
|
| 723 | + * 'syncToken' => 'The current synctoken', |
|
| 724 | + * 'added' => [ |
|
| 725 | + * 'new.txt', |
|
| 726 | + * ], |
|
| 727 | + * 'modified' => [ |
|
| 728 | + * 'modified.txt', |
|
| 729 | + * ], |
|
| 730 | + * 'deleted' => [ |
|
| 731 | + * 'foo.php.bak', |
|
| 732 | + * 'old.txt' |
|
| 733 | + * ] |
|
| 734 | + * ]; |
|
| 735 | + * |
|
| 736 | + * The returned syncToken property should reflect the *current* syncToken |
|
| 737 | + * of the calendar, as reported in the {http://sabredav.org/ns}sync-token |
|
| 738 | + * property. This is needed here too, to ensure the operation is atomic. |
|
| 739 | + * |
|
| 740 | + * If the $syncToken argument is specified as null, this is an initial |
|
| 741 | + * sync, and all members should be reported. |
|
| 742 | + * |
|
| 743 | + * The modified property is an array of nodenames that have changed since |
|
| 744 | + * the last token. |
|
| 745 | + * |
|
| 746 | + * The deleted property is an array with nodenames, that have been deleted |
|
| 747 | + * from collection. |
|
| 748 | + * |
|
| 749 | + * The $syncLevel argument is basically the 'depth' of the report. If it's |
|
| 750 | + * 1, you only have to report changes that happened only directly in |
|
| 751 | + * immediate descendants. If it's 2, it should also include changes from |
|
| 752 | + * the nodes below the child collections. (grandchildren) |
|
| 753 | + * |
|
| 754 | + * The $limit argument allows a client to specify how many results should |
|
| 755 | + * be returned at most. If the limit is not specified, it should be treated |
|
| 756 | + * as infinite. |
|
| 757 | + * |
|
| 758 | + * If the limit (infinite or not) is higher than you're willing to return, |
|
| 759 | + * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception. |
|
| 760 | + * |
|
| 761 | + * If the syncToken is expired (due to data cleanup) or unknown, you must |
|
| 762 | + * return null. |
|
| 763 | + * |
|
| 764 | + * The limit is 'suggestive'. You are free to ignore it. |
|
| 765 | + * |
|
| 766 | + * @param string $addressBookId |
|
| 767 | + * @param string $syncToken |
|
| 768 | + * @param int $syncLevel |
|
| 769 | + * @param int $limit |
|
| 770 | + * @return array |
|
| 771 | + */ |
|
| 772 | + function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
|
| 773 | + // Current synctoken |
|
| 774 | + $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
|
| 775 | + $stmt->execute([ $addressBookId ]); |
|
| 776 | + $currentToken = $stmt->fetchColumn(0); |
|
| 777 | + |
|
| 778 | + if (is_null($currentToken)) return null; |
|
| 779 | + |
|
| 780 | + $result = [ |
|
| 781 | + 'syncToken' => $currentToken, |
|
| 782 | + 'added' => [], |
|
| 783 | + 'modified' => [], |
|
| 784 | + 'deleted' => [], |
|
| 785 | + ]; |
|
| 786 | + |
|
| 787 | + if ($syncToken) { |
|
| 788 | + |
|
| 789 | + $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
|
| 790 | + if ($limit>0) { |
|
| 791 | + $query .= " `LIMIT` " . (int)$limit; |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + // Fetching all changes |
|
| 795 | + $stmt = $this->db->prepare($query); |
|
| 796 | + $stmt->execute([$syncToken, $currentToken, $addressBookId]); |
|
| 797 | + |
|
| 798 | + $changes = []; |
|
| 799 | + |
|
| 800 | + // This loop ensures that any duplicates are overwritten, only the |
|
| 801 | + // last change on a node is relevant. |
|
| 802 | + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 803 | + |
|
| 804 | + $changes[$row['uri']] = $row['operation']; |
|
| 805 | + |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + foreach($changes as $uri => $operation) { |
|
| 809 | + |
|
| 810 | + switch($operation) { |
|
| 811 | + case 1: |
|
| 812 | + $result['added'][] = $uri; |
|
| 813 | + break; |
|
| 814 | + case 2: |
|
| 815 | + $result['modified'][] = $uri; |
|
| 816 | + break; |
|
| 817 | + case 3: |
|
| 818 | + $result['deleted'][] = $uri; |
|
| 819 | + break; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + } |
|
| 823 | + } else { |
|
| 824 | + // No synctoken supplied, this is the initial sync. |
|
| 825 | + $query = "SELECT `uri` FROM `*PREFIX*cards` WHERE `addressbookid` = ?"; |
|
| 826 | + $stmt = $this->db->prepare($query); |
|
| 827 | + $stmt->execute([$addressBookId]); |
|
| 828 | + |
|
| 829 | + $result['added'] = $stmt->fetchAll(\PDO::FETCH_COLUMN); |
|
| 830 | + } |
|
| 831 | + return $result; |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + /** |
|
| 835 | + * Adds a change record to the addressbookchanges table. |
|
| 836 | + * |
|
| 837 | + * @param mixed $addressBookId |
|
| 838 | + * @param string $objectUri |
|
| 839 | + * @param int $operation 1 = add, 2 = modify, 3 = delete |
|
| 840 | + * @return void |
|
| 841 | + */ |
|
| 842 | + protected function addChange($addressBookId, $objectUri, $operation) { |
|
| 843 | + $sql = 'INSERT INTO `*PREFIX*addressbookchanges`(`uri`, `synctoken`, `addressbookid`, `operation`) SELECT ?, `synctoken`, ?, ? FROM `*PREFIX*addressbooks` WHERE `id` = ?'; |
|
| 844 | + $stmt = $this->db->prepare($sql); |
|
| 845 | + $stmt->execute([ |
|
| 846 | + $objectUri, |
|
| 847 | + $addressBookId, |
|
| 848 | + $operation, |
|
| 849 | + $addressBookId |
|
| 850 | + ]); |
|
| 851 | + $stmt = $this->db->prepare('UPDATE `*PREFIX*addressbooks` SET `synctoken` = `synctoken` + 1 WHERE `id` = ?'); |
|
| 852 | + $stmt->execute([ |
|
| 853 | + $addressBookId |
|
| 854 | + ]); |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + private function readBlob($cardData) { |
|
| 858 | + if (is_resource($cardData)) { |
|
| 859 | + return stream_get_contents($cardData); |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + return $cardData; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + /** |
|
| 866 | + * @param IShareable $shareable |
|
| 867 | + * @param string[] $add |
|
| 868 | + * @param string[] $remove |
|
| 869 | + */ |
|
| 870 | + public function updateShares(IShareable $shareable, $add, $remove) { |
|
| 871 | + $this->sharingBackend->updateShares($shareable, $add, $remove); |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * search contact |
|
| 876 | + * |
|
| 877 | + * @param int $addressBookId |
|
| 878 | + * @param string $pattern which should match within the $searchProperties |
|
| 879 | + * @param array $searchProperties defines the properties within the query pattern should match |
|
| 880 | + * @return array an array of contacts which are arrays of key-value-pairs |
|
| 881 | + */ |
|
| 882 | + public function search($addressBookId, $pattern, $searchProperties) { |
|
| 883 | + $query = $this->db->getQueryBuilder(); |
|
| 884 | + $query2 = $this->db->getQueryBuilder(); |
|
| 885 | + |
|
| 886 | + $query2->selectDistinct('cp.cardid')->from($this->dbCardsPropertiesTable, 'cp'); |
|
| 887 | + $query2->andWhere($query2->expr()->eq('cp.addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 888 | + $or = $query2->expr()->orX(); |
|
| 889 | + foreach ($searchProperties as $property) { |
|
| 890 | + $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
|
| 891 | + } |
|
| 892 | + $query2->andWhere($or); |
|
| 893 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 894 | + |
|
| 895 | + $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
|
| 896 | + ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
|
| 897 | + |
|
| 898 | + $result = $query->execute(); |
|
| 899 | + $cards = $result->fetchAll(); |
|
| 900 | + |
|
| 901 | + $result->closeCursor(); |
|
| 902 | + |
|
| 903 | + return array_map(function($array) { |
|
| 904 | + $array['carddata'] = $this->readBlob($array['carddata']); |
|
| 905 | + return $array; |
|
| 906 | + }, $cards); |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + /** |
|
| 910 | + * @param int $bookId |
|
| 911 | + * @param string $name |
|
| 912 | + * @return array |
|
| 913 | + */ |
|
| 914 | + public function collectCardProperties($bookId, $name) { |
|
| 915 | + $query = $this->db->getQueryBuilder(); |
|
| 916 | + $result = $query->selectDistinct('value') |
|
| 917 | + ->from($this->dbCardsPropertiesTable) |
|
| 918 | + ->where($query->expr()->eq('name', $query->createNamedParameter($name))) |
|
| 919 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($bookId))) |
|
| 920 | + ->execute(); |
|
| 921 | + |
|
| 922 | + $all = $result->fetchAll(PDO::FETCH_COLUMN); |
|
| 923 | + $result->closeCursor(); |
|
| 924 | + |
|
| 925 | + return $all; |
|
| 926 | + } |
|
| 927 | + |
|
| 928 | + /** |
|
| 929 | + * get URI from a given contact |
|
| 930 | + * |
|
| 931 | + * @param int $id |
|
| 932 | + * @return string |
|
| 933 | + */ |
|
| 934 | + public function getCardUri($id) { |
|
| 935 | + $query = $this->db->getQueryBuilder(); |
|
| 936 | + $query->select('uri')->from($this->dbCardsTable) |
|
| 937 | + ->where($query->expr()->eq('id', $query->createParameter('id'))) |
|
| 938 | + ->setParameter('id', $id); |
|
| 939 | + |
|
| 940 | + $result = $query->execute(); |
|
| 941 | + $uri = $result->fetch(); |
|
| 942 | + $result->closeCursor(); |
|
| 943 | + |
|
| 944 | + if (!isset($uri['uri'])) { |
|
| 945 | + throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 946 | + } |
|
| 947 | + |
|
| 948 | + return $uri['uri']; |
|
| 949 | + } |
|
| 950 | + |
|
| 951 | + /** |
|
| 952 | + * return contact with the given URI |
|
| 953 | + * |
|
| 954 | + * @param int $addressBookId |
|
| 955 | + * @param string $uri |
|
| 956 | + * @returns array |
|
| 957 | + */ |
|
| 958 | + public function getContact($addressBookId, $uri) { |
|
| 959 | + $result = []; |
|
| 960 | + $query = $this->db->getQueryBuilder(); |
|
| 961 | + $query->select('*')->from($this->dbCardsTable) |
|
| 962 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 963 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 964 | + $queryResult = $query->execute(); |
|
| 965 | + $contact = $queryResult->fetch(); |
|
| 966 | + $queryResult->closeCursor(); |
|
| 967 | + |
|
| 968 | + if (is_array($contact)) { |
|
| 969 | + $result = $contact; |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + return $result; |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + /** |
|
| 976 | + * Returns the list of people whom this address book is shared with. |
|
| 977 | + * |
|
| 978 | + * Every element in this array should have the following properties: |
|
| 979 | + * * href - Often a mailto: address |
|
| 980 | + * * commonName - Optional, for example a first + last name |
|
| 981 | + * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants. |
|
| 982 | + * * readOnly - boolean |
|
| 983 | + * * summary - Optional, a description for the share |
|
| 984 | + * |
|
| 985 | + * @return array |
|
| 986 | + */ |
|
| 987 | + public function getShares($addressBookId) { |
|
| 988 | + return $this->sharingBackend->getShares($addressBookId); |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + /** |
|
| 992 | + * update properties table |
|
| 993 | + * |
|
| 994 | + * @param int $addressBookId |
|
| 995 | + * @param string $cardUri |
|
| 996 | + * @param string $vCardSerialized |
|
| 997 | + */ |
|
| 998 | + protected function updateProperties($addressBookId, $cardUri, $vCardSerialized) { |
|
| 999 | + $cardId = $this->getCardId($addressBookId, $cardUri); |
|
| 1000 | + $vCard = $this->readCard($vCardSerialized); |
|
| 1001 | + |
|
| 1002 | + $this->purgeProperties($addressBookId, $cardId); |
|
| 1003 | + |
|
| 1004 | + $query = $this->db->getQueryBuilder(); |
|
| 1005 | + $query->insert($this->dbCardsPropertiesTable) |
|
| 1006 | + ->values( |
|
| 1007 | + [ |
|
| 1008 | + 'addressbookid' => $query->createNamedParameter($addressBookId), |
|
| 1009 | + 'cardid' => $query->createNamedParameter($cardId), |
|
| 1010 | + 'name' => $query->createParameter('name'), |
|
| 1011 | + 'value' => $query->createParameter('value'), |
|
| 1012 | + 'preferred' => $query->createParameter('preferred') |
|
| 1013 | + ] |
|
| 1014 | + ); |
|
| 1015 | + |
|
| 1016 | + foreach ($vCard->children() as $property) { |
|
| 1017 | + if(!in_array($property->name, self::$indexProperties)) { |
|
| 1018 | + continue; |
|
| 1019 | + } |
|
| 1020 | + $preferred = 0; |
|
| 1021 | + foreach($property->parameters as $parameter) { |
|
| 1022 | + if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
|
| 1023 | + $preferred = 1; |
|
| 1024 | + break; |
|
| 1025 | + } |
|
| 1026 | + } |
|
| 1027 | + $query->setParameter('name', $property->name); |
|
| 1028 | + $query->setParameter('value', substr($property->getValue(), 0, 254)); |
|
| 1029 | + $query->setParameter('preferred', $preferred); |
|
| 1030 | + $query->execute(); |
|
| 1031 | + } |
|
| 1032 | + } |
|
| 1033 | + |
|
| 1034 | + /** |
|
| 1035 | + * read vCard data into a vCard object |
|
| 1036 | + * |
|
| 1037 | + * @param string $cardData |
|
| 1038 | + * @return VCard |
|
| 1039 | + */ |
|
| 1040 | + protected function readCard($cardData) { |
|
| 1041 | + return Reader::read($cardData); |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + /** |
|
| 1045 | + * delete all properties from a given card |
|
| 1046 | + * |
|
| 1047 | + * @param int $addressBookId |
|
| 1048 | + * @param int $cardId |
|
| 1049 | + */ |
|
| 1050 | + protected function purgeProperties($addressBookId, $cardId) { |
|
| 1051 | + $query = $this->db->getQueryBuilder(); |
|
| 1052 | + $query->delete($this->dbCardsPropertiesTable) |
|
| 1053 | + ->where($query->expr()->eq('cardid', $query->createNamedParameter($cardId))) |
|
| 1054 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1055 | + $query->execute(); |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + /** |
|
| 1059 | + * get ID from a given contact |
|
| 1060 | + * |
|
| 1061 | + * @param int $addressBookId |
|
| 1062 | + * @param string $uri |
|
| 1063 | + * @return int |
|
| 1064 | + */ |
|
| 1065 | + protected function getCardId($addressBookId, $uri) { |
|
| 1066 | + $query = $this->db->getQueryBuilder(); |
|
| 1067 | + $query->select('id')->from($this->dbCardsTable) |
|
| 1068 | + ->where($query->expr()->eq('uri', $query->createNamedParameter($uri))) |
|
| 1069 | + ->andWhere($query->expr()->eq('addressbookid', $query->createNamedParameter($addressBookId))); |
|
| 1070 | + |
|
| 1071 | + $result = $query->execute(); |
|
| 1072 | + $cardIds = $result->fetch(); |
|
| 1073 | + $result->closeCursor(); |
|
| 1074 | + |
|
| 1075 | + if (!isset($cardIds['id'])) { |
|
| 1076 | + throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + return (int)$cardIds['id']; |
|
| 1080 | + } |
|
| 1081 | + |
|
| 1082 | + /** |
|
| 1083 | + * For shared address books the sharee is set in the ACL of the address book |
|
| 1084 | + * @param $addressBookId |
|
| 1085 | + * @param $acl |
|
| 1086 | + * @return array |
|
| 1087 | + */ |
|
| 1088 | + public function applyShareAcl($addressBookId, $acl) { |
|
| 1089 | + return $this->sharingBackend->applyShareAcl($addressBookId, $acl); |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + private function convertPrincipal($principalUri, $toV2) { |
|
| 1093 | + if ($this->principalBackend->getPrincipalPrefix() === 'principals') { |
|
| 1094 | + list(, $name) = URLUtil::splitPath($principalUri); |
|
| 1095 | + if ($toV2 === true) { |
|
| 1096 | + return "principals/users/$name"; |
|
| 1097 | + } |
|
| 1098 | + return "principals/$name"; |
|
| 1099 | + } |
|
| 1100 | + return $principalUri; |
|
| 1101 | + } |
|
| 1102 | + |
|
| 1103 | + private function addOwnerPrincipal(&$addressbookInfo) { |
|
| 1104 | + $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1105 | + $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1106 | + if (isset($addressbookInfo[$ownerPrincipalKey])) { |
|
| 1107 | + $uri = $addressbookInfo[$ownerPrincipalKey]; |
|
| 1108 | + } else { |
|
| 1109 | + $uri = $addressbookInfo['principaluri']; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + $principalInformation = $this->principalBackend->getPrincipalByPath($uri); |
|
| 1113 | + if (isset($principalInformation['{DAV:}displayname'])) { |
|
| 1114 | + $addressbookInfo[$displaynameKey] = $principalInformation['{DAV:}displayname']; |
|
| 1115 | + } |
|
| 1116 | + } |
|
| 1117 | 1117 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | ->from('addressbooks') |
| 115 | 115 | ->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri))); |
| 116 | 116 | |
| 117 | - return (int)$query->execute()->fetchColumn(); |
|
| 117 | + return (int) $query->execute()->fetchColumn(); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -145,15 +145,15 @@ discard block |
||
| 145 | 145 | $addressBooks = []; |
| 146 | 146 | |
| 147 | 147 | $result = $query->execute(); |
| 148 | - while($row = $result->fetch()) { |
|
| 148 | + while ($row = $result->fetch()) { |
|
| 149 | 149 | $addressBooks[$row['id']] = [ |
| 150 | 150 | 'id' => $row['id'], |
| 151 | 151 | 'uri' => $row['uri'], |
| 152 | 152 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
| 153 | 153 | '{DAV:}displayname' => $row['displayname'], |
| 154 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 154 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 155 | 155 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 156 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 156 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 157 | 157 | ]; |
| 158 | 158 | |
| 159 | 159 | $this->addOwnerPrincipal($addressBooks[$row['id']]); |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | $principals = array_map(function($principal) { |
| 166 | 166 | return urldecode($principal); |
| 167 | 167 | }, $principals); |
| 168 | - $principals[]= $principalUri; |
|
| 168 | + $principals[] = $principalUri; |
|
| 169 | 169 | |
| 170 | 170 | $query = $this->db->getQueryBuilder(); |
| 171 | 171 | $result = $query->select(['a.id', 'a.uri', 'a.displayname', 'a.principaluri', 'a.description', 'a.synctoken', 's.access']) |
@@ -177,8 +177,8 @@ discard block |
||
| 177 | 177 | ->setParameter('principaluri', $principals, IQueryBuilder::PARAM_STR_ARRAY) |
| 178 | 178 | ->execute(); |
| 179 | 179 | |
| 180 | - $readOnlyPropertyName = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}read-only'; |
|
| 181 | - while($row = $result->fetch()) { |
|
| 180 | + $readOnlyPropertyName = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}read-only'; |
|
| 181 | + while ($row = $result->fetch()) { |
|
| 182 | 182 | if ($row['principaluri'] === $principalUri) { |
| 183 | 183 | continue; |
| 184 | 184 | } |
@@ -197,18 +197,18 @@ discard block |
||
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | list(, $name) = URLUtil::splitPath($row['principaluri']); |
| 200 | - $uri = $row['uri'] . '_shared_by_' . $name; |
|
| 201 | - $displayName = $row['displayname'] . ' (' . $this->getUserDisplayName($name) . ')'; |
|
| 200 | + $uri = $row['uri'].'_shared_by_'.$name; |
|
| 201 | + $displayName = $row['displayname'].' ('.$this->getUserDisplayName($name).')'; |
|
| 202 | 202 | |
| 203 | 203 | $addressBooks[$row['id']] = [ |
| 204 | 204 | 'id' => $row['id'], |
| 205 | 205 | 'uri' => $uri, |
| 206 | 206 | 'principaluri' => $principalUriOriginal, |
| 207 | 207 | '{DAV:}displayname' => $displayName, |
| 208 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 208 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 209 | 209 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 210 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 211 | - '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal' => $row['principaluri'], |
|
| 210 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 211 | + '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal' => $row['principaluri'], |
|
| 212 | 212 | $readOnlyPropertyName => $readOnly, |
| 213 | 213 | ]; |
| 214 | 214 | |
@@ -229,15 +229,15 @@ discard block |
||
| 229 | 229 | $addressBooks = []; |
| 230 | 230 | |
| 231 | 231 | $result = $query->execute(); |
| 232 | - while($row = $result->fetch()) { |
|
| 232 | + while ($row = $result->fetch()) { |
|
| 233 | 233 | $addressBooks[$row['id']] = [ |
| 234 | 234 | 'id' => $row['id'], |
| 235 | 235 | 'uri' => $row['uri'], |
| 236 | 236 | 'principaluri' => $this->convertPrincipal($row['principaluri'], false), |
| 237 | 237 | '{DAV:}displayname' => $row['displayname'], |
| 238 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 238 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 239 | 239 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 240 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 240 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 241 | 241 | ]; |
| 242 | 242 | |
| 243 | 243 | $this->addOwnerPrincipal($addressBooks[$row['id']]); |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | 'uri' => $row['uri'], |
| 283 | 283 | 'principaluri' => $row['principaluri'], |
| 284 | 284 | '{DAV:}displayname' => $row['displayname'], |
| 285 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 285 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 286 | 286 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 287 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 287 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 288 | 288 | ]; |
| 289 | 289 | |
| 290 | 290 | $this->addOwnerPrincipal($addressBook); |
@@ -316,9 +316,9 @@ discard block |
||
| 316 | 316 | 'uri' => $row['uri'], |
| 317 | 317 | 'principaluri' => $row['principaluri'], |
| 318 | 318 | '{DAV:}displayname' => $row['displayname'], |
| 319 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], |
|
| 319 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description' => $row['description'], |
|
| 320 | 320 | '{http://calendarserver.org/ns/}getctag' => $row['synctoken'], |
| 321 | - '{http://sabredav.org/ns}sync-token' => $row['synctoken']?$row['synctoken']:'0', |
|
| 321 | + '{http://sabredav.org/ns}sync-token' => $row['synctoken'] ? $row['synctoken'] : '0', |
|
| 322 | 322 | ]; |
| 323 | 323 | |
| 324 | 324 | $this->addOwnerPrincipal($addressBook); |
@@ -345,7 +345,7 @@ discard block |
||
| 345 | 345 | function updateAddressBook($addressBookId, \Sabre\DAV\PropPatch $propPatch) { |
| 346 | 346 | $supportedProperties = [ |
| 347 | 347 | '{DAV:}displayname', |
| 348 | - '{' . Plugin::NS_CARDDAV . '}addressbook-description', |
|
| 348 | + '{'.Plugin::NS_CARDDAV.'}addressbook-description', |
|
| 349 | 349 | ]; |
| 350 | 350 | |
| 351 | 351 | /** |
@@ -354,13 +354,13 @@ discard block |
||
| 354 | 354 | $propPatch->handle($supportedProperties, function($mutations) use ($addressBookId) { |
| 355 | 355 | |
| 356 | 356 | $updates = []; |
| 357 | - foreach($mutations as $property=>$newValue) { |
|
| 357 | + foreach ($mutations as $property=>$newValue) { |
|
| 358 | 358 | |
| 359 | - switch($property) { |
|
| 359 | + switch ($property) { |
|
| 360 | 360 | case '{DAV:}displayname' : |
| 361 | 361 | $updates['displayname'] = $newValue; |
| 362 | 362 | break; |
| 363 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 363 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
| 364 | 364 | $updates['description'] = $newValue; |
| 365 | 365 | break; |
| 366 | 366 | } |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | $query = $this->db->getQueryBuilder(); |
| 369 | 369 | $query->update('addressbooks'); |
| 370 | 370 | |
| 371 | - foreach($updates as $key=>$value) { |
|
| 371 | + foreach ($updates as $key=>$value) { |
|
| 372 | 372 | $query->set($key, $query->createNamedParameter($value)); |
| 373 | 373 | } |
| 374 | 374 | $query->where($query->expr()->eq('id', $query->createNamedParameter($addressBookId))) |
@@ -399,24 +399,24 @@ discard block |
||
| 399 | 399 | 'synctoken' => 1 |
| 400 | 400 | ]; |
| 401 | 401 | |
| 402 | - foreach($properties as $property=>$newValue) { |
|
| 402 | + foreach ($properties as $property=>$newValue) { |
|
| 403 | 403 | |
| 404 | - switch($property) { |
|
| 404 | + switch ($property) { |
|
| 405 | 405 | case '{DAV:}displayname' : |
| 406 | 406 | $values['displayname'] = $newValue; |
| 407 | 407 | break; |
| 408 | - case '{' . Plugin::NS_CARDDAV . '}addressbook-description' : |
|
| 408 | + case '{'.Plugin::NS_CARDDAV.'}addressbook-description' : |
|
| 409 | 409 | $values['description'] = $newValue; |
| 410 | 410 | break; |
| 411 | 411 | default : |
| 412 | - throw new BadRequest('Unknown property: ' . $property); |
|
| 412 | + throw new BadRequest('Unknown property: '.$property); |
|
| 413 | 413 | } |
| 414 | 414 | |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | // Fallback to make sure the displayname is set. Some clients may refuse |
| 418 | 418 | // to work with addressbooks not having a displayname. |
| 419 | - if(is_null($values['displayname'])) { |
|
| 419 | + if (is_null($values['displayname'])) { |
|
| 420 | 420 | $values['displayname'] = $url; |
| 421 | 421 | } |
| 422 | 422 | |
@@ -494,8 +494,8 @@ discard block |
||
| 494 | 494 | $cards = []; |
| 495 | 495 | |
| 496 | 496 | $result = $query->execute(); |
| 497 | - while($row = $result->fetch()) { |
|
| 498 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 497 | + while ($row = $result->fetch()) { |
|
| 498 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 499 | 499 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 500 | 500 | $cards[] = $row; |
| 501 | 501 | } |
@@ -529,7 +529,7 @@ discard block |
||
| 529 | 529 | if (!$row) { |
| 530 | 530 | return false; |
| 531 | 531 | } |
| 532 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 532 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 533 | 533 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 534 | 534 | |
| 535 | 535 | return $row; |
@@ -566,7 +566,7 @@ discard block |
||
| 566 | 566 | $result = $query->execute(); |
| 567 | 567 | |
| 568 | 568 | while ($row = $result->fetch()) { |
| 569 | - $row['etag'] = '"' . $row['etag'] . '"'; |
|
| 569 | + $row['etag'] = '"'.$row['etag'].'"'; |
|
| 570 | 570 | $row['carddata'] = $this->readBlob($row['carddata']); |
| 571 | 571 | $cards[] = $row; |
| 572 | 572 | } |
@@ -624,7 +624,7 @@ discard block |
||
| 624 | 624 | 'cardUri' => $cardUri, |
| 625 | 625 | 'cardData' => $cardData])); |
| 626 | 626 | |
| 627 | - return '"' . $etag . '"'; |
|
| 627 | + return '"'.$etag.'"'; |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -674,7 +674,7 @@ discard block |
||
| 674 | 674 | 'cardUri' => $cardUri, |
| 675 | 675 | 'cardData' => $cardData])); |
| 676 | 676 | |
| 677 | - return '"' . $etag . '"'; |
|
| 677 | + return '"'.$etag.'"'; |
|
| 678 | 678 | } |
| 679 | 679 | |
| 680 | 680 | /** |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | function getChangesForAddressBook($addressBookId, $syncToken, $syncLevel, $limit = null) { |
| 773 | 773 | // Current synctoken |
| 774 | 774 | $stmt = $this->db->prepare('SELECT `synctoken` FROM `*PREFIX*addressbooks` WHERE `id` = ?'); |
| 775 | - $stmt->execute([ $addressBookId ]); |
|
| 775 | + $stmt->execute([$addressBookId]); |
|
| 776 | 776 | $currentToken = $stmt->fetchColumn(0); |
| 777 | 777 | |
| 778 | 778 | if (is_null($currentToken)) return null; |
@@ -787,8 +787,8 @@ discard block |
||
| 787 | 787 | if ($syncToken) { |
| 788 | 788 | |
| 789 | 789 | $query = "SELECT `uri`, `operation` FROM `*PREFIX*addressbookchanges` WHERE `synctoken` >= ? AND `synctoken` < ? AND `addressbookid` = ? ORDER BY `synctoken`"; |
| 790 | - if ($limit>0) { |
|
| 791 | - $query .= " `LIMIT` " . (int)$limit; |
|
| 790 | + if ($limit > 0) { |
|
| 791 | + $query .= " `LIMIT` ".(int) $limit; |
|
| 792 | 792 | } |
| 793 | 793 | |
| 794 | 794 | // Fetching all changes |
@@ -799,15 +799,15 @@ discard block |
||
| 799 | 799 | |
| 800 | 800 | // This loop ensures that any duplicates are overwritten, only the |
| 801 | 801 | // last change on a node is relevant. |
| 802 | - while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 802 | + while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { |
|
| 803 | 803 | |
| 804 | 804 | $changes[$row['uri']] = $row['operation']; |
| 805 | 805 | |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | - foreach($changes as $uri => $operation) { |
|
| 808 | + foreach ($changes as $uri => $operation) { |
|
| 809 | 809 | |
| 810 | - switch($operation) { |
|
| 810 | + switch ($operation) { |
|
| 811 | 811 | case 1: |
| 812 | 812 | $result['added'][] = $uri; |
| 813 | 813 | break; |
@@ -890,7 +890,7 @@ discard block |
||
| 890 | 890 | $or->add($query2->expr()->eq('cp.name', $query->createNamedParameter($property))); |
| 891 | 891 | } |
| 892 | 892 | $query2->andWhere($or); |
| 893 | - $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%' . $this->db->escapeLikeParameter($pattern) . '%'))); |
|
| 893 | + $query2->andWhere($query2->expr()->ilike('cp.value', $query->createNamedParameter('%'.$this->db->escapeLikeParameter($pattern).'%'))); |
|
| 894 | 894 | |
| 895 | 895 | $query->select('c.carddata', 'c.uri')->from($this->dbCardsTable, 'c') |
| 896 | 896 | ->where($query->expr()->in('c.id', $query->createFunction($query2->getSQL()))); |
@@ -942,7 +942,7 @@ discard block |
||
| 942 | 942 | $result->closeCursor(); |
| 943 | 943 | |
| 944 | 944 | if (!isset($uri['uri'])) { |
| 945 | - throw new \InvalidArgumentException('Card does not exists: ' . $id); |
|
| 945 | + throw new \InvalidArgumentException('Card does not exists: '.$id); |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | return $uri['uri']; |
@@ -1014,11 +1014,11 @@ discard block |
||
| 1014 | 1014 | ); |
| 1015 | 1015 | |
| 1016 | 1016 | foreach ($vCard->children() as $property) { |
| 1017 | - if(!in_array($property->name, self::$indexProperties)) { |
|
| 1017 | + if (!in_array($property->name, self::$indexProperties)) { |
|
| 1018 | 1018 | continue; |
| 1019 | 1019 | } |
| 1020 | 1020 | $preferred = 0; |
| 1021 | - foreach($property->parameters as $parameter) { |
|
| 1021 | + foreach ($property->parameters as $parameter) { |
|
| 1022 | 1022 | if ($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') { |
| 1023 | 1023 | $preferred = 1; |
| 1024 | 1024 | break; |
@@ -1073,10 +1073,10 @@ discard block |
||
| 1073 | 1073 | $result->closeCursor(); |
| 1074 | 1074 | |
| 1075 | 1075 | if (!isset($cardIds['id'])) { |
| 1076 | - throw new \InvalidArgumentException('Card does not exists: ' . $uri); |
|
| 1076 | + throw new \InvalidArgumentException('Card does not exists: '.$uri); |
|
| 1077 | 1077 | } |
| 1078 | 1078 | |
| 1079 | - return (int)$cardIds['id']; |
|
| 1079 | + return (int) $cardIds['id']; |
|
| 1080 | 1080 | } |
| 1081 | 1081 | |
| 1082 | 1082 | /** |
@@ -1101,8 +1101,8 @@ discard block |
||
| 1101 | 1101 | } |
| 1102 | 1102 | |
| 1103 | 1103 | private function addOwnerPrincipal(&$addressbookInfo) { |
| 1104 | - $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; |
|
| 1105 | - $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; |
|
| 1104 | + $ownerPrincipalKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD.'}owner-principal'; |
|
| 1105 | + $displaynameKey = '{'.\OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD.'}owner-displayname'; |
|
| 1106 | 1106 | if (isset($addressbookInfo[$ownerPrincipalKey])) { |
| 1107 | 1107 | $uri = $addressbookInfo[$ownerPrincipalKey]; |
| 1108 | 1108 | } else { |
@@ -26,190 +26,190 @@ |
||
| 26 | 26 | use OCP\IDBConnection; |
| 27 | 27 | |
| 28 | 28 | class Mapper { |
| 29 | - const TABLE_ADMIN_SETTINGS = 'admin_settings'; |
|
| 30 | - const TABLE_ADMIN_SECTIONS = 'admin_sections'; |
|
| 31 | - const TABLE_PERSONAL_SETTINGS = 'personal_settings'; |
|
| 32 | - const TABLE_PERSONAL_SECTIONS = 'personal_sections'; |
|
| 33 | - |
|
| 34 | - /** @var IDBConnection */ |
|
| 35 | - private $dbc; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param IDBConnection $dbc |
|
| 39 | - */ |
|
| 40 | - public function __construct(IDBConnection $dbc) { |
|
| 41 | - $this->dbc = $dbc; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Get the configured admin settings from the database for the provided section |
|
| 46 | - * |
|
| 47 | - * @param string $section |
|
| 48 | - * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 49 | - */ |
|
| 50 | - public function getAdminSettingsFromDB($section) { |
|
| 51 | - return $this->getSettingsFromDB(self::TABLE_ADMIN_SETTINGS, $section); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Get the configured personal settings from the database for the provided section |
|
| 56 | - * |
|
| 57 | - * @param string $section |
|
| 58 | - * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 59 | - */ |
|
| 60 | - public function getPersonalSettingsFromDB($section) { |
|
| 61 | - return $this->getSettingsFromDB(self::TABLE_PERSONAL_SETTINGS, $section); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Get the configured settings from the database for the provided table and section |
|
| 66 | - * |
|
| 67 | - * @param $table |
|
| 68 | - * @param $section |
|
| 69 | - * @return array |
|
| 70 | - */ |
|
| 71 | - private function getSettingsFromDB($table, $section) { |
|
| 72 | - $query = $this->dbc->getQueryBuilder(); |
|
| 73 | - $query->select(['class', 'priority']) |
|
| 74 | - ->from($table) |
|
| 75 | - ->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section'))) |
|
| 76 | - ->setParameter('section', $section); |
|
| 77 | - |
|
| 78 | - $result = $query->execute(); |
|
| 79 | - return $result->fetchAll(); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Get the configured admin sections from the database |
|
| 84 | - * |
|
| 85 | - * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 86 | - */ |
|
| 87 | - public function getAdminSectionsFromDB() { |
|
| 88 | - return $this->getSectionsFromDB('admin'); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get the configured admin sections from the database |
|
| 93 | - * |
|
| 94 | - * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 95 | - */ |
|
| 96 | - public function getPersonalSectionsFromDB() { |
|
| 97 | - return $this->getSectionsFromDB('personal'); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Get the configured sections from the database by table |
|
| 102 | - * |
|
| 103 | - * @param string $type either 'personal' or 'admin' |
|
| 104 | - * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 105 | - */ |
|
| 106 | - public function getSectionsFromDB($type) { |
|
| 107 | - if($type === 'admin') { |
|
| 108 | - $sectionsTable = self::TABLE_ADMIN_SECTIONS; |
|
| 109 | - $settingsTable = self::TABLE_ADMIN_SETTINGS; |
|
| 110 | - } else if($type === 'personal') { |
|
| 111 | - $sectionsTable = self::TABLE_PERSONAL_SECTIONS; |
|
| 112 | - $settingsTable = self::TABLE_PERSONAL_SETTINGS; |
|
| 113 | - } else { |
|
| 114 | - throw new \InvalidArgumentException('"admin" or "personal" expected'); |
|
| 115 | - } |
|
| 116 | - $query = $this->dbc->getQueryBuilder(); |
|
| 117 | - $query->selectDistinct('s.class') |
|
| 118 | - ->addSelect('s.priority') |
|
| 119 | - ->from($sectionsTable, 's') |
|
| 120 | - ->from($settingsTable, 'f') |
|
| 121 | - ->where($query->expr()->eq('s.id', 'f.section')); |
|
| 122 | - $result = $query->execute(); |
|
| 123 | - return array_map(function ($row) { |
|
| 124 | - $row['priority'] = (int)$row['priority']; |
|
| 125 | - return $row; |
|
| 126 | - }, $result->fetchAll()); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * @param string $table one of the Mapper::TABLE_* constants |
|
| 131 | - * @param array $values |
|
| 132 | - */ |
|
| 133 | - public function add($table, array $values) { |
|
| 134 | - $query = $this->dbc->getQueryBuilder(); |
|
| 135 | - $values = array_map(function ($value) use ($query) { |
|
| 136 | - return $query->createNamedParameter($value); |
|
| 137 | - }, $values); |
|
| 138 | - $query->insert($table)->values($values); |
|
| 139 | - $query->execute(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * returns the registered classes in the given table |
|
| 144 | - * |
|
| 145 | - * @param string $table one of the Mapper::TABLE_* constants |
|
| 146 | - * @return string[] |
|
| 147 | - */ |
|
| 148 | - public function getClasses($table) { |
|
| 149 | - $q = $this->dbc->getQueryBuilder(); |
|
| 150 | - $resultStatement = $q->select('class') |
|
| 151 | - ->from($table) |
|
| 152 | - ->execute(); |
|
| 153 | - $data = $resultStatement->fetchAll(); |
|
| 154 | - $resultStatement->closeCursor(); |
|
| 155 | - |
|
| 156 | - return array_map(function ($row) { |
|
| 157 | - return $row['class']; |
|
| 158 | - }, $data); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Check if a class is configured in the database |
|
| 163 | - * |
|
| 164 | - * @param string $table one of the Mapper::TABLE_* constants |
|
| 165 | - * @param string $className |
|
| 166 | - * @return bool |
|
| 167 | - */ |
|
| 168 | - public function has($table, $className) { |
|
| 169 | - $query = $this->dbc->getQueryBuilder(); |
|
| 170 | - $query->select('class') |
|
| 171 | - ->from($table) |
|
| 172 | - ->where($query->expr()->eq('class', $query->createNamedParameter($className))) |
|
| 173 | - ->setMaxResults(1); |
|
| 174 | - |
|
| 175 | - $result = $query->execute(); |
|
| 176 | - $row = $result->fetch(); |
|
| 177 | - $result->closeCursor(); |
|
| 178 | - |
|
| 179 | - return (bool)$row; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * deletes an settings or admin entry from the given table |
|
| 184 | - * |
|
| 185 | - * @param string $table one of the Mapper::TABLE_* constants |
|
| 186 | - * @param string $className |
|
| 187 | - */ |
|
| 188 | - public function remove($table, $className) { |
|
| 189 | - $query = $this->dbc->getQueryBuilder(); |
|
| 190 | - $query->delete($table) |
|
| 191 | - ->where($query->expr()->eq('class', $query->createNamedParameter($className))); |
|
| 192 | - |
|
| 193 | - $query->execute(); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param string $table one of the Mapper::TABLE_* constants |
|
| 198 | - * @param string $idCol |
|
| 199 | - * @param string $id |
|
| 200 | - * @param array $values |
|
| 201 | - * @suppress SqlInjectionChecker |
|
| 202 | - */ |
|
| 203 | - public function update($table, $idCol, $id, $values) { |
|
| 204 | - $query = $this->dbc->getQueryBuilder(); |
|
| 205 | - $query->update($table); |
|
| 206 | - foreach ($values as $key => $value) { |
|
| 207 | - $query->set($key, $query->createNamedParameter($value)); |
|
| 208 | - } |
|
| 209 | - $query |
|
| 210 | - ->where($query->expr()->eq($idCol, $query->createParameter($idCol))) |
|
| 211 | - ->setParameter($idCol, $id) |
|
| 212 | - ->execute(); |
|
| 213 | - } |
|
| 29 | + const TABLE_ADMIN_SETTINGS = 'admin_settings'; |
|
| 30 | + const TABLE_ADMIN_SECTIONS = 'admin_sections'; |
|
| 31 | + const TABLE_PERSONAL_SETTINGS = 'personal_settings'; |
|
| 32 | + const TABLE_PERSONAL_SECTIONS = 'personal_sections'; |
|
| 33 | + |
|
| 34 | + /** @var IDBConnection */ |
|
| 35 | + private $dbc; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param IDBConnection $dbc |
|
| 39 | + */ |
|
| 40 | + public function __construct(IDBConnection $dbc) { |
|
| 41 | + $this->dbc = $dbc; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Get the configured admin settings from the database for the provided section |
|
| 46 | + * |
|
| 47 | + * @param string $section |
|
| 48 | + * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 49 | + */ |
|
| 50 | + public function getAdminSettingsFromDB($section) { |
|
| 51 | + return $this->getSettingsFromDB(self::TABLE_ADMIN_SETTINGS, $section); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Get the configured personal settings from the database for the provided section |
|
| 56 | + * |
|
| 57 | + * @param string $section |
|
| 58 | + * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 59 | + */ |
|
| 60 | + public function getPersonalSettingsFromDB($section) { |
|
| 61 | + return $this->getSettingsFromDB(self::TABLE_PERSONAL_SETTINGS, $section); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Get the configured settings from the database for the provided table and section |
|
| 66 | + * |
|
| 67 | + * @param $table |
|
| 68 | + * @param $section |
|
| 69 | + * @return array |
|
| 70 | + */ |
|
| 71 | + private function getSettingsFromDB($table, $section) { |
|
| 72 | + $query = $this->dbc->getQueryBuilder(); |
|
| 73 | + $query->select(['class', 'priority']) |
|
| 74 | + ->from($table) |
|
| 75 | + ->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section'))) |
|
| 76 | + ->setParameter('section', $section); |
|
| 77 | + |
|
| 78 | + $result = $query->execute(); |
|
| 79 | + return $result->fetchAll(); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Get the configured admin sections from the database |
|
| 84 | + * |
|
| 85 | + * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 86 | + */ |
|
| 87 | + public function getAdminSectionsFromDB() { |
|
| 88 | + return $this->getSectionsFromDB('admin'); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get the configured admin sections from the database |
|
| 93 | + * |
|
| 94 | + * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 95 | + */ |
|
| 96 | + public function getPersonalSectionsFromDB() { |
|
| 97 | + return $this->getSectionsFromDB('personal'); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Get the configured sections from the database by table |
|
| 102 | + * |
|
| 103 | + * @param string $type either 'personal' or 'admin' |
|
| 104 | + * @return array[] [['class' => string, 'priority' => int], ...] |
|
| 105 | + */ |
|
| 106 | + public function getSectionsFromDB($type) { |
|
| 107 | + if($type === 'admin') { |
|
| 108 | + $sectionsTable = self::TABLE_ADMIN_SECTIONS; |
|
| 109 | + $settingsTable = self::TABLE_ADMIN_SETTINGS; |
|
| 110 | + } else if($type === 'personal') { |
|
| 111 | + $sectionsTable = self::TABLE_PERSONAL_SECTIONS; |
|
| 112 | + $settingsTable = self::TABLE_PERSONAL_SETTINGS; |
|
| 113 | + } else { |
|
| 114 | + throw new \InvalidArgumentException('"admin" or "personal" expected'); |
|
| 115 | + } |
|
| 116 | + $query = $this->dbc->getQueryBuilder(); |
|
| 117 | + $query->selectDistinct('s.class') |
|
| 118 | + ->addSelect('s.priority') |
|
| 119 | + ->from($sectionsTable, 's') |
|
| 120 | + ->from($settingsTable, 'f') |
|
| 121 | + ->where($query->expr()->eq('s.id', 'f.section')); |
|
| 122 | + $result = $query->execute(); |
|
| 123 | + return array_map(function ($row) { |
|
| 124 | + $row['priority'] = (int)$row['priority']; |
|
| 125 | + return $row; |
|
| 126 | + }, $result->fetchAll()); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * @param string $table one of the Mapper::TABLE_* constants |
|
| 131 | + * @param array $values |
|
| 132 | + */ |
|
| 133 | + public function add($table, array $values) { |
|
| 134 | + $query = $this->dbc->getQueryBuilder(); |
|
| 135 | + $values = array_map(function ($value) use ($query) { |
|
| 136 | + return $query->createNamedParameter($value); |
|
| 137 | + }, $values); |
|
| 138 | + $query->insert($table)->values($values); |
|
| 139 | + $query->execute(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * returns the registered classes in the given table |
|
| 144 | + * |
|
| 145 | + * @param string $table one of the Mapper::TABLE_* constants |
|
| 146 | + * @return string[] |
|
| 147 | + */ |
|
| 148 | + public function getClasses($table) { |
|
| 149 | + $q = $this->dbc->getQueryBuilder(); |
|
| 150 | + $resultStatement = $q->select('class') |
|
| 151 | + ->from($table) |
|
| 152 | + ->execute(); |
|
| 153 | + $data = $resultStatement->fetchAll(); |
|
| 154 | + $resultStatement->closeCursor(); |
|
| 155 | + |
|
| 156 | + return array_map(function ($row) { |
|
| 157 | + return $row['class']; |
|
| 158 | + }, $data); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Check if a class is configured in the database |
|
| 163 | + * |
|
| 164 | + * @param string $table one of the Mapper::TABLE_* constants |
|
| 165 | + * @param string $className |
|
| 166 | + * @return bool |
|
| 167 | + */ |
|
| 168 | + public function has($table, $className) { |
|
| 169 | + $query = $this->dbc->getQueryBuilder(); |
|
| 170 | + $query->select('class') |
|
| 171 | + ->from($table) |
|
| 172 | + ->where($query->expr()->eq('class', $query->createNamedParameter($className))) |
|
| 173 | + ->setMaxResults(1); |
|
| 174 | + |
|
| 175 | + $result = $query->execute(); |
|
| 176 | + $row = $result->fetch(); |
|
| 177 | + $result->closeCursor(); |
|
| 178 | + |
|
| 179 | + return (bool)$row; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * deletes an settings or admin entry from the given table |
|
| 184 | + * |
|
| 185 | + * @param string $table one of the Mapper::TABLE_* constants |
|
| 186 | + * @param string $className |
|
| 187 | + */ |
|
| 188 | + public function remove($table, $className) { |
|
| 189 | + $query = $this->dbc->getQueryBuilder(); |
|
| 190 | + $query->delete($table) |
|
| 191 | + ->where($query->expr()->eq('class', $query->createNamedParameter($className))); |
|
| 192 | + |
|
| 193 | + $query->execute(); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param string $table one of the Mapper::TABLE_* constants |
|
| 198 | + * @param string $idCol |
|
| 199 | + * @param string $id |
|
| 200 | + * @param array $values |
|
| 201 | + * @suppress SqlInjectionChecker |
|
| 202 | + */ |
|
| 203 | + public function update($table, $idCol, $id, $values) { |
|
| 204 | + $query = $this->dbc->getQueryBuilder(); |
|
| 205 | + $query->update($table); |
|
| 206 | + foreach ($values as $key => $value) { |
|
| 207 | + $query->set($key, $query->createNamedParameter($value)); |
|
| 208 | + } |
|
| 209 | + $query |
|
| 210 | + ->where($query->expr()->eq($idCol, $query->createParameter($idCol))) |
|
| 211 | + ->setParameter($idCol, $id) |
|
| 212 | + ->execute(); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | 215 | } |
@@ -37,246 +37,246 @@ |
||
| 37 | 37 | * Locking provider that stores the locks in the database |
| 38 | 38 | */ |
| 39 | 39 | class DBLockingProvider extends AbstractLockingProvider { |
| 40 | - /** |
|
| 41 | - * @var \OCP\IDBConnection |
|
| 42 | - */ |
|
| 43 | - private $connection; |
|
| 40 | + /** |
|
| 41 | + * @var \OCP\IDBConnection |
|
| 42 | + */ |
|
| 43 | + private $connection; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @var \OCP\ILogger |
|
| 47 | - */ |
|
| 48 | - private $logger; |
|
| 45 | + /** |
|
| 46 | + * @var \OCP\ILogger |
|
| 47 | + */ |
|
| 48 | + private $logger; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @var \OCP\AppFramework\Utility\ITimeFactory |
|
| 52 | - */ |
|
| 53 | - private $timeFactory; |
|
| 50 | + /** |
|
| 51 | + * @var \OCP\AppFramework\Utility\ITimeFactory |
|
| 52 | + */ |
|
| 53 | + private $timeFactory; |
|
| 54 | 54 | |
| 55 | - private $sharedLocks = []; |
|
| 55 | + private $sharedLocks = []; |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Check if we have an open shared lock for a path |
|
| 59 | - * |
|
| 60 | - * @param string $path |
|
| 61 | - * @return bool |
|
| 62 | - */ |
|
| 63 | - protected function isLocallyLocked($path) { |
|
| 64 | - return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
| 65 | - } |
|
| 57 | + /** |
|
| 58 | + * Check if we have an open shared lock for a path |
|
| 59 | + * |
|
| 60 | + * @param string $path |
|
| 61 | + * @return bool |
|
| 62 | + */ |
|
| 63 | + protected function isLocallyLocked($path) { |
|
| 64 | + return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Mark a locally acquired lock |
|
| 69 | - * |
|
| 70 | - * @param string $path |
|
| 71 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 72 | - */ |
|
| 73 | - protected function markAcquire($path, $type) { |
|
| 74 | - parent::markAcquire($path, $type); |
|
| 75 | - if ($type === self::LOCK_SHARED) { |
|
| 76 | - $this->sharedLocks[$path] = true; |
|
| 77 | - } |
|
| 78 | - } |
|
| 67 | + /** |
|
| 68 | + * Mark a locally acquired lock |
|
| 69 | + * |
|
| 70 | + * @param string $path |
|
| 71 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 72 | + */ |
|
| 73 | + protected function markAcquire($path, $type) { |
|
| 74 | + parent::markAcquire($path, $type); |
|
| 75 | + if ($type === self::LOCK_SHARED) { |
|
| 76 | + $this->sharedLocks[$path] = true; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Change the type of an existing tracked lock |
|
| 82 | - * |
|
| 83 | - * @param string $path |
|
| 84 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 85 | - */ |
|
| 86 | - protected function markChange($path, $targetType) { |
|
| 87 | - parent::markChange($path, $targetType); |
|
| 88 | - if ($targetType === self::LOCK_SHARED) { |
|
| 89 | - $this->sharedLocks[$path] = true; |
|
| 90 | - } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 91 | - $this->sharedLocks[$path] = false; |
|
| 92 | - } |
|
| 93 | - } |
|
| 80 | + /** |
|
| 81 | + * Change the type of an existing tracked lock |
|
| 82 | + * |
|
| 83 | + * @param string $path |
|
| 84 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 85 | + */ |
|
| 86 | + protected function markChange($path, $targetType) { |
|
| 87 | + parent::markChange($path, $targetType); |
|
| 88 | + if ($targetType === self::LOCK_SHARED) { |
|
| 89 | + $this->sharedLocks[$path] = true; |
|
| 90 | + } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
| 91 | + $this->sharedLocks[$path] = false; |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @param \OCP\IDBConnection $connection |
|
| 97 | - * @param \OCP\ILogger $logger |
|
| 98 | - * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
| 99 | - * @param int $ttl |
|
| 100 | - */ |
|
| 101 | - public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, $ttl = 3600) { |
|
| 102 | - $this->connection = $connection; |
|
| 103 | - $this->logger = $logger; |
|
| 104 | - $this->timeFactory = $timeFactory; |
|
| 105 | - $this->ttl = $ttl; |
|
| 106 | - } |
|
| 95 | + /** |
|
| 96 | + * @param \OCP\IDBConnection $connection |
|
| 97 | + * @param \OCP\ILogger $logger |
|
| 98 | + * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
| 99 | + * @param int $ttl |
|
| 100 | + */ |
|
| 101 | + public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory, $ttl = 3600) { |
|
| 102 | + $this->connection = $connection; |
|
| 103 | + $this->logger = $logger; |
|
| 104 | + $this->timeFactory = $timeFactory; |
|
| 105 | + $this->ttl = $ttl; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Insert a file locking row if it does not exists. |
|
| 110 | - * |
|
| 111 | - * @param string $path |
|
| 112 | - * @param int $lock |
|
| 113 | - * @return int number of inserted rows |
|
| 114 | - */ |
|
| 108 | + /** |
|
| 109 | + * Insert a file locking row if it does not exists. |
|
| 110 | + * |
|
| 111 | + * @param string $path |
|
| 112 | + * @param int $lock |
|
| 113 | + * @return int number of inserted rows |
|
| 114 | + */ |
|
| 115 | 115 | |
| 116 | - protected function initLockField($path, $lock = 0) { |
|
| 117 | - $expire = $this->getExpireTime(); |
|
| 118 | - return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); |
|
| 119 | - } |
|
| 116 | + protected function initLockField($path, $lock = 0) { |
|
| 117 | + $expire = $this->getExpireTime(); |
|
| 118 | + return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * @return int |
|
| 123 | - */ |
|
| 124 | - protected function getExpireTime() { |
|
| 125 | - return $this->timeFactory->getTime() + $this->ttl; |
|
| 126 | - } |
|
| 121 | + /** |
|
| 122 | + * @return int |
|
| 123 | + */ |
|
| 124 | + protected function getExpireTime() { |
|
| 125 | + return $this->timeFactory->getTime() + $this->ttl; |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * @param string $path |
|
| 130 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 131 | - * @return bool |
|
| 132 | - */ |
|
| 133 | - public function isLocked($path, $type) { |
|
| 134 | - if ($this->hasAcquiredLock($path, $type)) { |
|
| 135 | - return true; |
|
| 136 | - } |
|
| 137 | - $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
| 138 | - $query->execute([$path]); |
|
| 139 | - $lockValue = (int)$query->fetchColumn(); |
|
| 140 | - if ($type === self::LOCK_SHARED) { |
|
| 141 | - if ($this->isLocallyLocked($path)) { |
|
| 142 | - // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
| 143 | - return $lockValue > 1; |
|
| 144 | - } else { |
|
| 145 | - return $lockValue > 0; |
|
| 146 | - } |
|
| 147 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 148 | - return $lockValue === -1; |
|
| 149 | - } else { |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - } |
|
| 128 | + /** |
|
| 129 | + * @param string $path |
|
| 130 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 131 | + * @return bool |
|
| 132 | + */ |
|
| 133 | + public function isLocked($path, $type) { |
|
| 134 | + if ($this->hasAcquiredLock($path, $type)) { |
|
| 135 | + return true; |
|
| 136 | + } |
|
| 137 | + $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
| 138 | + $query->execute([$path]); |
|
| 139 | + $lockValue = (int)$query->fetchColumn(); |
|
| 140 | + if ($type === self::LOCK_SHARED) { |
|
| 141 | + if ($this->isLocallyLocked($path)) { |
|
| 142 | + // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
| 143 | + return $lockValue > 1; |
|
| 144 | + } else { |
|
| 145 | + return $lockValue > 0; |
|
| 146 | + } |
|
| 147 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
| 148 | + return $lockValue === -1; |
|
| 149 | + } else { |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * @param string $path |
|
| 156 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 157 | - * @throws \OCP\Lock\LockedException |
|
| 158 | - */ |
|
| 159 | - public function acquireLock($path, $type) { |
|
| 160 | - $expire = $this->getExpireTime(); |
|
| 161 | - if ($type === self::LOCK_SHARED) { |
|
| 162 | - if (!$this->isLocallyLocked($path)) { |
|
| 163 | - $result = $this->initLockField($path, 1); |
|
| 164 | - if ($result <= 0) { |
|
| 165 | - $result = $this->connection->executeUpdate( |
|
| 166 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
| 167 | - [$expire, $path] |
|
| 168 | - ); |
|
| 169 | - } |
|
| 170 | - } else { |
|
| 171 | - $result = 1; |
|
| 172 | - } |
|
| 173 | - } else { |
|
| 174 | - $existing = 0; |
|
| 175 | - if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
| 176 | - $existing = 1; |
|
| 177 | - } |
|
| 178 | - $result = $this->initLockField($path, -1); |
|
| 179 | - if ($result <= 0) { |
|
| 180 | - $result = $this->connection->executeUpdate( |
|
| 181 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
| 182 | - [$expire, $path, $existing] |
|
| 183 | - ); |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - if ($result !== 1) { |
|
| 187 | - throw new LockedException($path); |
|
| 188 | - } |
|
| 189 | - $this->markAcquire($path, $type); |
|
| 190 | - } |
|
| 154 | + /** |
|
| 155 | + * @param string $path |
|
| 156 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 157 | + * @throws \OCP\Lock\LockedException |
|
| 158 | + */ |
|
| 159 | + public function acquireLock($path, $type) { |
|
| 160 | + $expire = $this->getExpireTime(); |
|
| 161 | + if ($type === self::LOCK_SHARED) { |
|
| 162 | + if (!$this->isLocallyLocked($path)) { |
|
| 163 | + $result = $this->initLockField($path, 1); |
|
| 164 | + if ($result <= 0) { |
|
| 165 | + $result = $this->connection->executeUpdate( |
|
| 166 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
| 167 | + [$expire, $path] |
|
| 168 | + ); |
|
| 169 | + } |
|
| 170 | + } else { |
|
| 171 | + $result = 1; |
|
| 172 | + } |
|
| 173 | + } else { |
|
| 174 | + $existing = 0; |
|
| 175 | + if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
| 176 | + $existing = 1; |
|
| 177 | + } |
|
| 178 | + $result = $this->initLockField($path, -1); |
|
| 179 | + if ($result <= 0) { |
|
| 180 | + $result = $this->connection->executeUpdate( |
|
| 181 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
| 182 | + [$expire, $path, $existing] |
|
| 183 | + ); |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + if ($result !== 1) { |
|
| 187 | + throw new LockedException($path); |
|
| 188 | + } |
|
| 189 | + $this->markAcquire($path, $type); |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - /** |
|
| 193 | - * @param string $path |
|
| 194 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 195 | - */ |
|
| 196 | - public function releaseLock($path, $type) { |
|
| 197 | - $this->markRelease($path, $type); |
|
| 192 | + /** |
|
| 193 | + * @param string $path |
|
| 194 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 195 | + */ |
|
| 196 | + public function releaseLock($path, $type) { |
|
| 197 | + $this->markRelease($path, $type); |
|
| 198 | 198 | |
| 199 | - // we keep shared locks till the end of the request so we can re-use them |
|
| 200 | - if ($type === self::LOCK_EXCLUSIVE) { |
|
| 201 | - $this->connection->executeUpdate( |
|
| 202 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
| 203 | - [$path] |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - } |
|
| 199 | + // we keep shared locks till the end of the request so we can re-use them |
|
| 200 | + if ($type === self::LOCK_EXCLUSIVE) { |
|
| 201 | + $this->connection->executeUpdate( |
|
| 202 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
| 203 | + [$path] |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * Change the type of an existing lock |
|
| 210 | - * |
|
| 211 | - * @param string $path |
|
| 212 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 213 | - * @throws \OCP\Lock\LockedException |
|
| 214 | - */ |
|
| 215 | - public function changeLock($path, $targetType) { |
|
| 216 | - $expire = $this->getExpireTime(); |
|
| 217 | - if ($targetType === self::LOCK_SHARED) { |
|
| 218 | - $result = $this->connection->executeUpdate( |
|
| 219 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
| 220 | - [$expire, $path] |
|
| 221 | - ); |
|
| 222 | - } else { |
|
| 223 | - // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
| 224 | - if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
| 225 | - throw new LockedException($path); |
|
| 226 | - } |
|
| 227 | - $result = $this->connection->executeUpdate( |
|
| 228 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
| 229 | - [$expire, $path] |
|
| 230 | - ); |
|
| 231 | - } |
|
| 232 | - if ($result !== 1) { |
|
| 233 | - throw new LockedException($path); |
|
| 234 | - } |
|
| 235 | - $this->markChange($path, $targetType); |
|
| 236 | - } |
|
| 208 | + /** |
|
| 209 | + * Change the type of an existing lock |
|
| 210 | + * |
|
| 211 | + * @param string $path |
|
| 212 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
| 213 | + * @throws \OCP\Lock\LockedException |
|
| 214 | + */ |
|
| 215 | + public function changeLock($path, $targetType) { |
|
| 216 | + $expire = $this->getExpireTime(); |
|
| 217 | + if ($targetType === self::LOCK_SHARED) { |
|
| 218 | + $result = $this->connection->executeUpdate( |
|
| 219 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
| 220 | + [$expire, $path] |
|
| 221 | + ); |
|
| 222 | + } else { |
|
| 223 | + // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
| 224 | + if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
| 225 | + throw new LockedException($path); |
|
| 226 | + } |
|
| 227 | + $result = $this->connection->executeUpdate( |
|
| 228 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
| 229 | + [$expire, $path] |
|
| 230 | + ); |
|
| 231 | + } |
|
| 232 | + if ($result !== 1) { |
|
| 233 | + throw new LockedException($path); |
|
| 234 | + } |
|
| 235 | + $this->markChange($path, $targetType); |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - /** |
|
| 239 | - * cleanup empty locks |
|
| 240 | - */ |
|
| 241 | - public function cleanExpiredLocks() { |
|
| 242 | - $expire = $this->timeFactory->getTime(); |
|
| 243 | - try { |
|
| 244 | - $this->connection->executeUpdate( |
|
| 245 | - 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
| 246 | - [$expire] |
|
| 247 | - ); |
|
| 248 | - } catch (\Exception $e) { |
|
| 249 | - // If the table is missing, the clean up was successful |
|
| 250 | - if ($this->connection->tableExists('file_locks')) { |
|
| 251 | - throw $e; |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 238 | + /** |
|
| 239 | + * cleanup empty locks |
|
| 240 | + */ |
|
| 241 | + public function cleanExpiredLocks() { |
|
| 242 | + $expire = $this->timeFactory->getTime(); |
|
| 243 | + try { |
|
| 244 | + $this->connection->executeUpdate( |
|
| 245 | + 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
| 246 | + [$expire] |
|
| 247 | + ); |
|
| 248 | + } catch (\Exception $e) { |
|
| 249 | + // If the table is missing, the clean up was successful |
|
| 250 | + if ($this->connection->tableExists('file_locks')) { |
|
| 251 | + throw $e; |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * release all lock acquired by this instance which were marked using the mark* methods |
|
| 258 | - * @suppress SqlInjectionChecker |
|
| 259 | - */ |
|
| 260 | - public function releaseAll() { |
|
| 261 | - parent::releaseAll(); |
|
| 256 | + /** |
|
| 257 | + * release all lock acquired by this instance which were marked using the mark* methods |
|
| 258 | + * @suppress SqlInjectionChecker |
|
| 259 | + */ |
|
| 260 | + public function releaseAll() { |
|
| 261 | + parent::releaseAll(); |
|
| 262 | 262 | |
| 263 | - // since we keep shared locks we need to manually clean those |
|
| 264 | - $lockedPaths = array_keys($this->sharedLocks); |
|
| 265 | - $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
| 266 | - return $this->sharedLocks[$path]; |
|
| 267 | - }); |
|
| 263 | + // since we keep shared locks we need to manually clean those |
|
| 264 | + $lockedPaths = array_keys($this->sharedLocks); |
|
| 265 | + $lockedPaths = array_filter($lockedPaths, function ($path) { |
|
| 266 | + return $this->sharedLocks[$path]; |
|
| 267 | + }); |
|
| 268 | 268 | |
| 269 | - $chunkedPaths = array_chunk($lockedPaths, 100); |
|
| 269 | + $chunkedPaths = array_chunk($lockedPaths, 100); |
|
| 270 | 270 | |
| 271 | - foreach ($chunkedPaths as $chunk) { |
|
| 272 | - $builder = $this->connection->getQueryBuilder(); |
|
| 271 | + foreach ($chunkedPaths as $chunk) { |
|
| 272 | + $builder = $this->connection->getQueryBuilder(); |
|
| 273 | 273 | |
| 274 | - $query = $builder->update('file_locks') |
|
| 275 | - ->set('lock', $builder->createFunction('`lock` -1')) |
|
| 276 | - ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 277 | - ->andWhere($builder->expr()->gt('lock', new Literal(0))); |
|
| 274 | + $query = $builder->update('file_locks') |
|
| 275 | + ->set('lock', $builder->createFunction('`lock` -1')) |
|
| 276 | + ->where($builder->expr()->in('key', $builder->createNamedParameter($chunk, IQueryBuilder::PARAM_STR_ARRAY))) |
|
| 277 | + ->andWhere($builder->expr()->gt('lock', new Literal(0))); |
|
| 278 | 278 | |
| 279 | - $query->execute(); |
|
| 280 | - } |
|
| 281 | - } |
|
| 279 | + $query->execute(); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | 282 | } |
@@ -32,142 +32,142 @@ |
||
| 32 | 32 | use OCP\IDBConnection; |
| 33 | 33 | |
| 34 | 34 | class PostgreSQL extends AbstractDatabase { |
| 35 | - public $dbprettyname = 'PostgreSQL'; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param string $username |
|
| 39 | - * @throws \OC\DatabaseSetupException |
|
| 40 | - * @suppress SqlInjectionChecker |
|
| 41 | - */ |
|
| 42 | - public function setupDatabase($username) { |
|
| 43 | - try { |
|
| 44 | - $connection = $this->connect([ |
|
| 45 | - 'dbname' => 'postgres' |
|
| 46 | - ]); |
|
| 47 | - //check for roles creation rights in postgresql |
|
| 48 | - $builder = $connection->getQueryBuilder(); |
|
| 49 | - $builder->automaticTablePrefix(false); |
|
| 50 | - $query = $builder |
|
| 51 | - ->select('rolname') |
|
| 52 | - ->from('pg_roles') |
|
| 53 | - ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE'))) |
|
| 54 | - ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 55 | - |
|
| 56 | - try { |
|
| 57 | - $result = $query->execute(); |
|
| 58 | - $canCreateRoles = $result->rowCount() > 0; |
|
| 59 | - } catch (DatabaseException $e) { |
|
| 60 | - $canCreateRoles = false; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - if ($canCreateRoles) { |
|
| 64 | - //use the admin login data for the new database user |
|
| 65 | - |
|
| 66 | - //add prefix to the postgresql user name to prevent collisions |
|
| 67 | - $this->dbUser = 'oc_' . strtolower($username); |
|
| 68 | - //create a new password so we don't need to store the admin config in the config file |
|
| 69 | - $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 70 | - |
|
| 71 | - $this->createDBUser($connection); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - $this->config->setValues([ |
|
| 75 | - 'dbuser' => $this->dbUser, |
|
| 76 | - 'dbpassword' => $this->dbPassword, |
|
| 77 | - ]); |
|
| 78 | - |
|
| 79 | - //create the database |
|
| 80 | - $this->createDatabase($connection); |
|
| 81 | - $query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1"); |
|
| 82 | - $query->execute([$this->tablePrefix . "users"]); |
|
| 83 | - $tablesSetup = $query->fetchColumn() > 0; |
|
| 84 | - |
|
| 85 | - // the connection to dbname=postgres is not needed anymore |
|
| 86 | - $connection->close(); |
|
| 87 | - } catch (\Exception $e) { |
|
| 88 | - $this->logger->logException($e); |
|
| 89 | - $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created'); |
|
| 90 | - $tablesSetup = false; |
|
| 91 | - $this->config->setValues([ |
|
| 92 | - 'dbuser' => $this->dbUser, |
|
| 93 | - 'dbpassword' => $this->dbPassword, |
|
| 94 | - ]); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled |
|
| 98 | - $this->dbUser = $this->config->getValue('dbuser'); |
|
| 99 | - $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 100 | - $connection = $this->connect(); |
|
| 101 | - try { |
|
| 102 | - $connection->connect(); |
|
| 103 | - } catch (\Exception $e) { |
|
| 104 | - $this->logger->logException($e); |
|
| 105 | - throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), |
|
| 106 | - $this->trans->t('You need to enter details of an existing account.')); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - if (!$tablesSetup) { |
|
| 111 | - \OC_DB::createDbFromStructure($this->dbDefinitionFile); |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - private function createDatabase(IDBConnection $connection) { |
|
| 116 | - if (!$this->databaseExists($connection)) { |
|
| 117 | - //The database does not exists... let's create it |
|
| 118 | - $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 119 | - try { |
|
| 120 | - $query->execute(); |
|
| 121 | - } catch (DatabaseException $e) { |
|
| 122 | - $this->logger->error('Error while trying to create database'); |
|
| 123 | - $this->logger->logException($e); |
|
| 124 | - } |
|
| 125 | - } else { |
|
| 126 | - $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 127 | - try { |
|
| 128 | - $query->execute(); |
|
| 129 | - } catch (DatabaseException $e) { |
|
| 130 | - $this->logger->error('Error while trying to restrict database permissions'); |
|
| 131 | - $this->logger->logException($e); |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - private function userExists(IDBConnection $connection) { |
|
| 137 | - $builder = $connection->getQueryBuilder(); |
|
| 138 | - $builder->automaticTablePrefix(false); |
|
| 139 | - $query = $builder->select('*') |
|
| 140 | - ->from('pg_roles') |
|
| 141 | - ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 142 | - $result = $query->execute(); |
|
| 143 | - return $result->rowCount() > 0; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - private function databaseExists(IDBConnection $connection) { |
|
| 147 | - $builder = $connection->getQueryBuilder(); |
|
| 148 | - $builder->automaticTablePrefix(false); |
|
| 149 | - $query = $builder->select('datname') |
|
| 150 | - ->from('pg_database') |
|
| 151 | - ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName))); |
|
| 152 | - $result = $query->execute(); |
|
| 153 | - return $result->rowCount() > 0; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - private function createDBUser(IDBConnection $connection) { |
|
| 157 | - $dbUser = $this->dbUser; |
|
| 158 | - try { |
|
| 159 | - $i = 1; |
|
| 160 | - while ($this->userExists($connection)) { |
|
| 161 | - $i++; |
|
| 162 | - $this->dbUser = $dbUser . $i; |
|
| 163 | - }; |
|
| 164 | - |
|
| 165 | - // create the user |
|
| 166 | - $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 167 | - $query->execute(); |
|
| 168 | - } catch (DatabaseException $e) { |
|
| 169 | - $this->logger->error('Error while trying to create database user'); |
|
| 170 | - $this->logger->logException($e); |
|
| 171 | - } |
|
| 172 | - } |
|
| 35 | + public $dbprettyname = 'PostgreSQL'; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param string $username |
|
| 39 | + * @throws \OC\DatabaseSetupException |
|
| 40 | + * @suppress SqlInjectionChecker |
|
| 41 | + */ |
|
| 42 | + public function setupDatabase($username) { |
|
| 43 | + try { |
|
| 44 | + $connection = $this->connect([ |
|
| 45 | + 'dbname' => 'postgres' |
|
| 46 | + ]); |
|
| 47 | + //check for roles creation rights in postgresql |
|
| 48 | + $builder = $connection->getQueryBuilder(); |
|
| 49 | + $builder->automaticTablePrefix(false); |
|
| 50 | + $query = $builder |
|
| 51 | + ->select('rolname') |
|
| 52 | + ->from('pg_roles') |
|
| 53 | + ->where($builder->expr()->eq('rolcreaterole', new Literal('TRUE'))) |
|
| 54 | + ->andWhere($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 55 | + |
|
| 56 | + try { |
|
| 57 | + $result = $query->execute(); |
|
| 58 | + $canCreateRoles = $result->rowCount() > 0; |
|
| 59 | + } catch (DatabaseException $e) { |
|
| 60 | + $canCreateRoles = false; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + if ($canCreateRoles) { |
|
| 64 | + //use the admin login data for the new database user |
|
| 65 | + |
|
| 66 | + //add prefix to the postgresql user name to prevent collisions |
|
| 67 | + $this->dbUser = 'oc_' . strtolower($username); |
|
| 68 | + //create a new password so we don't need to store the admin config in the config file |
|
| 69 | + $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 70 | + |
|
| 71 | + $this->createDBUser($connection); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + $this->config->setValues([ |
|
| 75 | + 'dbuser' => $this->dbUser, |
|
| 76 | + 'dbpassword' => $this->dbPassword, |
|
| 77 | + ]); |
|
| 78 | + |
|
| 79 | + //create the database |
|
| 80 | + $this->createDatabase($connection); |
|
| 81 | + $query = $connection->prepare("select count(*) FROM pg_class WHERE relname=? limit 1"); |
|
| 82 | + $query->execute([$this->tablePrefix . "users"]); |
|
| 83 | + $tablesSetup = $query->fetchColumn() > 0; |
|
| 84 | + |
|
| 85 | + // the connection to dbname=postgres is not needed anymore |
|
| 86 | + $connection->close(); |
|
| 87 | + } catch (\Exception $e) { |
|
| 88 | + $this->logger->logException($e); |
|
| 89 | + $this->logger->warning('Error trying to connect as "postgres", assuming database is setup and tables need to be created'); |
|
| 90 | + $tablesSetup = false; |
|
| 91 | + $this->config->setValues([ |
|
| 92 | + 'dbuser' => $this->dbUser, |
|
| 93 | + 'dbpassword' => $this->dbPassword, |
|
| 94 | + ]); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // connect to the ownCloud database (dbname=$this->dbname) and check if it needs to be filled |
|
| 98 | + $this->dbUser = $this->config->getValue('dbuser'); |
|
| 99 | + $this->dbPassword = $this->config->getValue('dbpassword'); |
|
| 100 | + $connection = $this->connect(); |
|
| 101 | + try { |
|
| 102 | + $connection->connect(); |
|
| 103 | + } catch (\Exception $e) { |
|
| 104 | + $this->logger->logException($e); |
|
| 105 | + throw new \OC\DatabaseSetupException($this->trans->t('PostgreSQL username and/or password not valid'), |
|
| 106 | + $this->trans->t('You need to enter details of an existing account.')); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + if (!$tablesSetup) { |
|
| 111 | + \OC_DB::createDbFromStructure($this->dbDefinitionFile); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + private function createDatabase(IDBConnection $connection) { |
|
| 116 | + if (!$this->databaseExists($connection)) { |
|
| 117 | + //The database does not exists... let's create it |
|
| 118 | + $query = $connection->prepare("CREATE DATABASE " . addslashes($this->dbName) . " OWNER " . addslashes($this->dbUser)); |
|
| 119 | + try { |
|
| 120 | + $query->execute(); |
|
| 121 | + } catch (DatabaseException $e) { |
|
| 122 | + $this->logger->error('Error while trying to create database'); |
|
| 123 | + $this->logger->logException($e); |
|
| 124 | + } |
|
| 125 | + } else { |
|
| 126 | + $query = $connection->prepare("REVOKE ALL PRIVILEGES ON DATABASE " . addslashes($this->dbName) . " FROM PUBLIC"); |
|
| 127 | + try { |
|
| 128 | + $query->execute(); |
|
| 129 | + } catch (DatabaseException $e) { |
|
| 130 | + $this->logger->error('Error while trying to restrict database permissions'); |
|
| 131 | + $this->logger->logException($e); |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + private function userExists(IDBConnection $connection) { |
|
| 137 | + $builder = $connection->getQueryBuilder(); |
|
| 138 | + $builder->automaticTablePrefix(false); |
|
| 139 | + $query = $builder->select('*') |
|
| 140 | + ->from('pg_roles') |
|
| 141 | + ->where($builder->expr()->eq('rolname', $builder->createNamedParameter($this->dbUser))); |
|
| 142 | + $result = $query->execute(); |
|
| 143 | + return $result->rowCount() > 0; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + private function databaseExists(IDBConnection $connection) { |
|
| 147 | + $builder = $connection->getQueryBuilder(); |
|
| 148 | + $builder->automaticTablePrefix(false); |
|
| 149 | + $query = $builder->select('datname') |
|
| 150 | + ->from('pg_database') |
|
| 151 | + ->where($builder->expr()->eq('datname', $builder->createNamedParameter($this->dbName))); |
|
| 152 | + $result = $query->execute(); |
|
| 153 | + return $result->rowCount() > 0; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + private function createDBUser(IDBConnection $connection) { |
|
| 157 | + $dbUser = $this->dbUser; |
|
| 158 | + try { |
|
| 159 | + $i = 1; |
|
| 160 | + while ($this->userExists($connection)) { |
|
| 161 | + $i++; |
|
| 162 | + $this->dbUser = $dbUser . $i; |
|
| 163 | + }; |
|
| 164 | + |
|
| 165 | + // create the user |
|
| 166 | + $query = $connection->prepare("CREATE USER " . addslashes($this->dbUser) . " CREATEDB PASSWORD '" . addslashes($this->dbPassword) . "'"); |
|
| 167 | + $query->execute(); |
|
| 168 | + } catch (DatabaseException $e) { |
|
| 169 | + $this->logger->error('Error while trying to create database user'); |
|
| 170 | + $this->logger->logException($e); |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | 173 | } |
@@ -38,843 +38,843 @@ |
||
| 38 | 38 | |
| 39 | 39 | class Manager implements ICommentsManager { |
| 40 | 40 | |
| 41 | - /** @var IDBConnection */ |
|
| 42 | - protected $dbConn; |
|
| 43 | - |
|
| 44 | - /** @var ILogger */ |
|
| 45 | - protected $logger; |
|
| 46 | - |
|
| 47 | - /** @var IConfig */ |
|
| 48 | - protected $config; |
|
| 49 | - |
|
| 50 | - /** @var IComment[] */ |
|
| 51 | - protected $commentsCache = []; |
|
| 52 | - |
|
| 53 | - /** @var \Closure[] */ |
|
| 54 | - protected $eventHandlerClosures = []; |
|
| 55 | - |
|
| 56 | - /** @var ICommentsEventHandler[] */ |
|
| 57 | - protected $eventHandlers = []; |
|
| 58 | - |
|
| 59 | - /** @var \Closure[] */ |
|
| 60 | - protected $displayNameResolvers = []; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Manager constructor. |
|
| 64 | - * |
|
| 65 | - * @param IDBConnection $dbConn |
|
| 66 | - * @param ILogger $logger |
|
| 67 | - * @param IConfig $config |
|
| 68 | - */ |
|
| 69 | - public function __construct( |
|
| 70 | - IDBConnection $dbConn, |
|
| 71 | - ILogger $logger, |
|
| 72 | - IConfig $config |
|
| 73 | - ) { |
|
| 74 | - $this->dbConn = $dbConn; |
|
| 75 | - $this->logger = $logger; |
|
| 76 | - $this->config = $config; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * converts data base data into PHP native, proper types as defined by |
|
| 81 | - * IComment interface. |
|
| 82 | - * |
|
| 83 | - * @param array $data |
|
| 84 | - * @return array |
|
| 85 | - */ |
|
| 86 | - protected function normalizeDatabaseData(array $data) { |
|
| 87 | - $data['id'] = strval($data['id']); |
|
| 88 | - $data['parent_id'] = strval($data['parent_id']); |
|
| 89 | - $data['topmost_parent_id'] = strval($data['topmost_parent_id']); |
|
| 90 | - $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
| 91 | - if (!is_null($data['latest_child_timestamp'])) { |
|
| 92 | - $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
| 93 | - } |
|
| 94 | - $data['children_count'] = intval($data['children_count']); |
|
| 95 | - return $data; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * prepares a comment for an insert or update operation after making sure |
|
| 100 | - * all necessary fields have a value assigned. |
|
| 101 | - * |
|
| 102 | - * @param IComment $comment |
|
| 103 | - * @return IComment returns the same updated IComment instance as provided |
|
| 104 | - * by parameter for convenience |
|
| 105 | - * @throws \UnexpectedValueException |
|
| 106 | - */ |
|
| 107 | - protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
| 108 | - if (!$comment->getActorType() |
|
| 109 | - || !$comment->getActorId() |
|
| 110 | - || !$comment->getObjectType() |
|
| 111 | - || !$comment->getObjectId() |
|
| 112 | - || !$comment->getVerb() |
|
| 113 | - ) { |
|
| 114 | - throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if ($comment->getId() === '') { |
|
| 118 | - $comment->setChildrenCount(0); |
|
| 119 | - $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
|
| 120 | - $comment->setLatestChildDateTime(null); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (is_null($comment->getCreationDateTime())) { |
|
| 124 | - $comment->setCreationDateTime(new \DateTime()); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - if ($comment->getParentId() !== '0') { |
|
| 128 | - $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
|
| 129 | - } else { |
|
| 130 | - $comment->setTopmostParentId('0'); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - $this->cache($comment); |
|
| 134 | - |
|
| 135 | - return $comment; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * returns the topmost parent id of a given comment identified by ID |
|
| 140 | - * |
|
| 141 | - * @param string $id |
|
| 142 | - * @return string |
|
| 143 | - * @throws NotFoundException |
|
| 144 | - */ |
|
| 145 | - protected function determineTopmostParentId($id) { |
|
| 146 | - $comment = $this->get($id); |
|
| 147 | - if ($comment->getParentId() === '0') { |
|
| 148 | - return $comment->getId(); |
|
| 149 | - } else { |
|
| 150 | - return $this->determineTopmostParentId($comment->getId()); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * updates child information of a comment |
|
| 156 | - * |
|
| 157 | - * @param string $id |
|
| 158 | - * @param \DateTime $cDateTime the date time of the most recent child |
|
| 159 | - * @throws NotFoundException |
|
| 160 | - */ |
|
| 161 | - protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
|
| 162 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 163 | - $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 164 | - ->from('comments') |
|
| 165 | - ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
|
| 166 | - ->setParameter('id', $id); |
|
| 167 | - |
|
| 168 | - $resultStatement = $query->execute(); |
|
| 169 | - $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 170 | - $resultStatement->closeCursor(); |
|
| 171 | - $children = intval($data[0]); |
|
| 172 | - |
|
| 173 | - $comment = $this->get($id); |
|
| 174 | - $comment->setChildrenCount($children); |
|
| 175 | - $comment->setLatestChildDateTime($cDateTime); |
|
| 176 | - $this->save($comment); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Tests whether actor or object type and id parameters are acceptable. |
|
| 181 | - * Throws exception if not. |
|
| 182 | - * |
|
| 183 | - * @param string $role |
|
| 184 | - * @param string $type |
|
| 185 | - * @param string $id |
|
| 186 | - * @throws \InvalidArgumentException |
|
| 187 | - */ |
|
| 188 | - protected function checkRoleParameters($role, $type, $id) { |
|
| 189 | - if ( |
|
| 190 | - !is_string($type) || empty($type) |
|
| 191 | - || !is_string($id) || empty($id) |
|
| 192 | - ) { |
|
| 193 | - throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * run-time caches a comment |
|
| 199 | - * |
|
| 200 | - * @param IComment $comment |
|
| 201 | - */ |
|
| 202 | - protected function cache(IComment $comment) { |
|
| 203 | - $id = $comment->getId(); |
|
| 204 | - if (empty($id)) { |
|
| 205 | - return; |
|
| 206 | - } |
|
| 207 | - $this->commentsCache[strval($id)] = $comment; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * removes an entry from the comments run time cache |
|
| 212 | - * |
|
| 213 | - * @param mixed $id the comment's id |
|
| 214 | - */ |
|
| 215 | - protected function uncache($id) { |
|
| 216 | - $id = strval($id); |
|
| 217 | - if (isset($this->commentsCache[$id])) { |
|
| 218 | - unset($this->commentsCache[$id]); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * returns a comment instance |
|
| 224 | - * |
|
| 225 | - * @param string $id the ID of the comment |
|
| 226 | - * @return IComment |
|
| 227 | - * @throws NotFoundException |
|
| 228 | - * @throws \InvalidArgumentException |
|
| 229 | - * @since 9.0.0 |
|
| 230 | - */ |
|
| 231 | - public function get($id) { |
|
| 232 | - if (intval($id) === 0) { |
|
| 233 | - throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - if (isset($this->commentsCache[$id])) { |
|
| 237 | - return $this->commentsCache[$id]; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 241 | - $resultStatement = $qb->select('*') |
|
| 242 | - ->from('comments') |
|
| 243 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 244 | - ->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
|
| 245 | - ->execute(); |
|
| 246 | - |
|
| 247 | - $data = $resultStatement->fetch(); |
|
| 248 | - $resultStatement->closeCursor(); |
|
| 249 | - if (!$data) { |
|
| 250 | - throw new NotFoundException(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 254 | - $this->cache($comment); |
|
| 255 | - return $comment; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * returns the comment specified by the id and all it's child comments. |
|
| 260 | - * At this point of time, we do only support one level depth. |
|
| 261 | - * |
|
| 262 | - * @param string $id |
|
| 263 | - * @param int $limit max number of entries to return, 0 returns all |
|
| 264 | - * @param int $offset the start entry |
|
| 265 | - * @return array |
|
| 266 | - * @since 9.0.0 |
|
| 267 | - * |
|
| 268 | - * The return array looks like this |
|
| 269 | - * [ |
|
| 270 | - * 'comment' => IComment, // root comment |
|
| 271 | - * 'replies' => |
|
| 272 | - * [ |
|
| 273 | - * 0 => |
|
| 274 | - * [ |
|
| 275 | - * 'comment' => IComment, |
|
| 276 | - * 'replies' => [] |
|
| 277 | - * ] |
|
| 278 | - * 1 => |
|
| 279 | - * [ |
|
| 280 | - * 'comment' => IComment, |
|
| 281 | - * 'replies'=> [] |
|
| 282 | - * ], |
|
| 283 | - * … |
|
| 284 | - * ] |
|
| 285 | - * ] |
|
| 286 | - */ |
|
| 287 | - public function getTree($id, $limit = 0, $offset = 0) { |
|
| 288 | - $tree = []; |
|
| 289 | - $tree['comment'] = $this->get($id); |
|
| 290 | - $tree['replies'] = []; |
|
| 291 | - |
|
| 292 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 293 | - $query = $qb->select('*') |
|
| 294 | - ->from('comments') |
|
| 295 | - ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
|
| 296 | - ->orderBy('creation_timestamp', 'DESC') |
|
| 297 | - ->setParameter('id', $id); |
|
| 298 | - |
|
| 299 | - if ($limit > 0) { |
|
| 300 | - $query->setMaxResults($limit); |
|
| 301 | - } |
|
| 302 | - if ($offset > 0) { |
|
| 303 | - $query->setFirstResult($offset); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - $resultStatement = $query->execute(); |
|
| 307 | - while ($data = $resultStatement->fetch()) { |
|
| 308 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 309 | - $this->cache($comment); |
|
| 310 | - $tree['replies'][] = [ |
|
| 311 | - 'comment' => $comment, |
|
| 312 | - 'replies' => [] |
|
| 313 | - ]; |
|
| 314 | - } |
|
| 315 | - $resultStatement->closeCursor(); |
|
| 316 | - |
|
| 317 | - return $tree; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * returns comments for a specific object (e.g. a file). |
|
| 322 | - * |
|
| 323 | - * The sort order is always newest to oldest. |
|
| 324 | - * |
|
| 325 | - * @param string $objectType the object type, e.g. 'files' |
|
| 326 | - * @param string $objectId the id of the object |
|
| 327 | - * @param int $limit optional, number of maximum comments to be returned. if |
|
| 328 | - * not specified, all comments are returned. |
|
| 329 | - * @param int $offset optional, starting point |
|
| 330 | - * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 331 | - * that may be returned |
|
| 332 | - * @return IComment[] |
|
| 333 | - * @since 9.0.0 |
|
| 334 | - */ |
|
| 335 | - public function getForObject( |
|
| 336 | - $objectType, |
|
| 337 | - $objectId, |
|
| 338 | - $limit = 0, |
|
| 339 | - $offset = 0, |
|
| 340 | - \DateTime $notOlderThan = null |
|
| 341 | - ) { |
|
| 342 | - $comments = []; |
|
| 343 | - |
|
| 344 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 345 | - $query = $qb->select('*') |
|
| 346 | - ->from('comments') |
|
| 347 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 348 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 349 | - ->orderBy('creation_timestamp', 'DESC') |
|
| 350 | - ->setParameter('type', $objectType) |
|
| 351 | - ->setParameter('id', $objectId); |
|
| 352 | - |
|
| 353 | - if ($limit > 0) { |
|
| 354 | - $query->setMaxResults($limit); |
|
| 355 | - } |
|
| 356 | - if ($offset > 0) { |
|
| 357 | - $query->setFirstResult($offset); |
|
| 358 | - } |
|
| 359 | - if (!is_null($notOlderThan)) { |
|
| 360 | - $query |
|
| 361 | - ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 362 | - ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - $resultStatement = $query->execute(); |
|
| 366 | - while ($data = $resultStatement->fetch()) { |
|
| 367 | - $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 368 | - $this->cache($comment); |
|
| 369 | - $comments[] = $comment; |
|
| 370 | - } |
|
| 371 | - $resultStatement->closeCursor(); |
|
| 372 | - |
|
| 373 | - return $comments; |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * @param $objectType string the object type, e.g. 'files' |
|
| 378 | - * @param $objectId string the id of the object |
|
| 379 | - * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 380 | - * that may be returned |
|
| 381 | - * @return Int |
|
| 382 | - * @since 9.0.0 |
|
| 383 | - */ |
|
| 384 | - public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) { |
|
| 385 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 386 | - $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 387 | - ->from('comments') |
|
| 388 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 389 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 390 | - ->setParameter('type', $objectType) |
|
| 391 | - ->setParameter('id', $objectId); |
|
| 392 | - |
|
| 393 | - if (!is_null($notOlderThan)) { |
|
| 394 | - $query |
|
| 395 | - ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 396 | - ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - $resultStatement = $query->execute(); |
|
| 400 | - $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 401 | - $resultStatement->closeCursor(); |
|
| 402 | - return intval($data[0]); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Get the number of unread comments for all files in a folder |
|
| 407 | - * |
|
| 408 | - * @param int $folderId |
|
| 409 | - * @param IUser $user |
|
| 410 | - * @return array [$fileId => $unreadCount] |
|
| 411 | - */ |
|
| 412 | - public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
|
| 413 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 414 | - $query = $qb->select('fileid', $qb->createFunction( |
|
| 415 | - 'COUNT(' . $qb->getColumnName('c.id') . ')') |
|
| 416 | - )->from('comments', 'c') |
|
| 417 | - ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( |
|
| 418 | - $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
|
| 419 | - $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)) |
|
| 420 | - )) |
|
| 421 | - ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
|
| 422 | - $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
|
| 423 | - $qb->expr()->eq('m.object_id', 'c.object_id'), |
|
| 424 | - $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())) |
|
| 425 | - )) |
|
| 426 | - ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId))) |
|
| 427 | - ->andWhere($qb->expr()->orX( |
|
| 428 | - $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'), |
|
| 429 | - $qb->expr()->isNull('marker_datetime') |
|
| 430 | - )) |
|
| 431 | - ->groupBy('f.fileid'); |
|
| 432 | - |
|
| 433 | - $resultStatement = $query->execute(); |
|
| 434 | - return array_map(function ($count) { |
|
| 435 | - return (int)$count; |
|
| 436 | - }, $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR)); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * creates a new comment and returns it. At this point of time, it is not |
|
| 441 | - * saved in the used data storage. Use save() after setting other fields |
|
| 442 | - * of the comment (e.g. message or verb). |
|
| 443 | - * |
|
| 444 | - * @param string $actorType the actor type (e.g. 'users') |
|
| 445 | - * @param string $actorId a user id |
|
| 446 | - * @param string $objectType the object type the comment is attached to |
|
| 447 | - * @param string $objectId the object id the comment is attached to |
|
| 448 | - * @return IComment |
|
| 449 | - * @since 9.0.0 |
|
| 450 | - */ |
|
| 451 | - public function create($actorType, $actorId, $objectType, $objectId) { |
|
| 452 | - $comment = new Comment(); |
|
| 453 | - $comment |
|
| 454 | - ->setActor($actorType, $actorId) |
|
| 455 | - ->setObject($objectType, $objectId); |
|
| 456 | - return $comment; |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * permanently deletes the comment specified by the ID |
|
| 461 | - * |
|
| 462 | - * When the comment has child comments, their parent ID will be changed to |
|
| 463 | - * the parent ID of the item that is to be deleted. |
|
| 464 | - * |
|
| 465 | - * @param string $id |
|
| 466 | - * @return bool |
|
| 467 | - * @throws \InvalidArgumentException |
|
| 468 | - * @since 9.0.0 |
|
| 469 | - */ |
|
| 470 | - public function delete($id) { |
|
| 471 | - if (!is_string($id)) { |
|
| 472 | - throw new \InvalidArgumentException('Parameter must be string'); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - try { |
|
| 476 | - $comment = $this->get($id); |
|
| 477 | - } catch (\Exception $e) { |
|
| 478 | - // Ignore exceptions, we just don't fire a hook then |
|
| 479 | - $comment = null; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 483 | - $query = $qb->delete('comments') |
|
| 484 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 485 | - ->setParameter('id', $id); |
|
| 486 | - |
|
| 487 | - try { |
|
| 488 | - $affectedRows = $query->execute(); |
|
| 489 | - $this->uncache($id); |
|
| 490 | - } catch (DriverException $e) { |
|
| 491 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 492 | - return false; |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - if ($affectedRows > 0 && $comment instanceof IComment) { |
|
| 496 | - $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - return ($affectedRows > 0); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * saves the comment permanently |
|
| 504 | - * |
|
| 505 | - * if the supplied comment has an empty ID, a new entry comment will be |
|
| 506 | - * saved and the instance updated with the new ID. |
|
| 507 | - * |
|
| 508 | - * Otherwise, an existing comment will be updated. |
|
| 509 | - * |
|
| 510 | - * Throws NotFoundException when a comment that is to be updated does not |
|
| 511 | - * exist anymore at this point of time. |
|
| 512 | - * |
|
| 513 | - * @param IComment $comment |
|
| 514 | - * @return bool |
|
| 515 | - * @throws NotFoundException |
|
| 516 | - * @since 9.0.0 |
|
| 517 | - */ |
|
| 518 | - public function save(IComment $comment) { |
|
| 519 | - if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
|
| 520 | - $result = $this->insert($comment); |
|
| 521 | - } else { |
|
| 522 | - $result = $this->update($comment); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - if ($result && !!$comment->getParentId()) { |
|
| 526 | - $this->updateChildrenInformation( |
|
| 527 | - $comment->getParentId(), |
|
| 528 | - $comment->getCreationDateTime() |
|
| 529 | - ); |
|
| 530 | - $this->cache($comment); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - return $result; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * inserts the provided comment in the database |
|
| 538 | - * |
|
| 539 | - * @param IComment $comment |
|
| 540 | - * @return bool |
|
| 541 | - */ |
|
| 542 | - protected function insert(IComment &$comment) { |
|
| 543 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 544 | - $affectedRows = $qb |
|
| 545 | - ->insert('comments') |
|
| 546 | - ->values([ |
|
| 547 | - 'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
|
| 548 | - 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
|
| 549 | - 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
|
| 550 | - 'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
|
| 551 | - 'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
|
| 552 | - 'message' => $qb->createNamedParameter($comment->getMessage()), |
|
| 553 | - 'verb' => $qb->createNamedParameter($comment->getVerb()), |
|
| 554 | - 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
|
| 555 | - 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
|
| 556 | - 'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
|
| 557 | - 'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
|
| 558 | - ]) |
|
| 559 | - ->execute(); |
|
| 560 | - |
|
| 561 | - if ($affectedRows > 0) { |
|
| 562 | - $comment->setId(strval($qb->getLastInsertId())); |
|
| 563 | - $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - return $affectedRows > 0; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * updates a Comment data row |
|
| 571 | - * |
|
| 572 | - * @param IComment $comment |
|
| 573 | - * @return bool |
|
| 574 | - * @throws NotFoundException |
|
| 575 | - */ |
|
| 576 | - protected function update(IComment $comment) { |
|
| 577 | - // for properly working preUpdate Events we need the old comments as is |
|
| 578 | - // in the DB and overcome caching. Also avoid that outdated information stays. |
|
| 579 | - $this->uncache($comment->getId()); |
|
| 580 | - $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
|
| 581 | - $this->uncache($comment->getId()); |
|
| 582 | - |
|
| 583 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 584 | - $affectedRows = $qb |
|
| 585 | - ->update('comments') |
|
| 586 | - ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
|
| 587 | - ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
|
| 588 | - ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
|
| 589 | - ->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
|
| 590 | - ->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
|
| 591 | - ->set('message', $qb->createNamedParameter($comment->getMessage())) |
|
| 592 | - ->set('verb', $qb->createNamedParameter($comment->getVerb())) |
|
| 593 | - ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
|
| 594 | - ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
|
| 595 | - ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
|
| 596 | - ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) |
|
| 597 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 598 | - ->setParameter('id', $comment->getId()) |
|
| 599 | - ->execute(); |
|
| 600 | - |
|
| 601 | - if ($affectedRows === 0) { |
|
| 602 | - throw new NotFoundException('Comment to update does ceased to exist'); |
|
| 603 | - } |
|
| 604 | - |
|
| 605 | - $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
|
| 606 | - |
|
| 607 | - return $affectedRows > 0; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * removes references to specific actor (e.g. on user delete) of a comment. |
|
| 612 | - * The comment itself must not get lost/deleted. |
|
| 613 | - * |
|
| 614 | - * @param string $actorType the actor type (e.g. 'users') |
|
| 615 | - * @param string $actorId a user id |
|
| 616 | - * @return boolean |
|
| 617 | - * @since 9.0.0 |
|
| 618 | - */ |
|
| 619 | - public function deleteReferencesOfActor($actorType, $actorId) { |
|
| 620 | - $this->checkRoleParameters('Actor', $actorType, $actorId); |
|
| 621 | - |
|
| 622 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 623 | - $affectedRows = $qb |
|
| 624 | - ->update('comments') |
|
| 625 | - ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 626 | - ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 627 | - ->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
|
| 628 | - ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
|
| 629 | - ->setParameter('type', $actorType) |
|
| 630 | - ->setParameter('id', $actorId) |
|
| 631 | - ->execute(); |
|
| 632 | - |
|
| 633 | - $this->commentsCache = []; |
|
| 634 | - |
|
| 635 | - return is_int($affectedRows); |
|
| 636 | - } |
|
| 637 | - |
|
| 638 | - /** |
|
| 639 | - * deletes all comments made of a specific object (e.g. on file delete) |
|
| 640 | - * |
|
| 641 | - * @param string $objectType the object type (e.g. 'files') |
|
| 642 | - * @param string $objectId e.g. the file id |
|
| 643 | - * @return boolean |
|
| 644 | - * @since 9.0.0 |
|
| 645 | - */ |
|
| 646 | - public function deleteCommentsAtObject($objectType, $objectId) { |
|
| 647 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 648 | - |
|
| 649 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 650 | - $affectedRows = $qb |
|
| 651 | - ->delete('comments') |
|
| 652 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 653 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 654 | - ->setParameter('type', $objectType) |
|
| 655 | - ->setParameter('id', $objectId) |
|
| 656 | - ->execute(); |
|
| 657 | - |
|
| 658 | - $this->commentsCache = []; |
|
| 659 | - |
|
| 660 | - return is_int($affectedRows); |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * deletes the read markers for the specified user |
|
| 665 | - * |
|
| 666 | - * @param \OCP\IUser $user |
|
| 667 | - * @return bool |
|
| 668 | - * @since 9.0.0 |
|
| 669 | - */ |
|
| 670 | - public function deleteReadMarksFromUser(IUser $user) { |
|
| 671 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 672 | - $query = $qb->delete('comments_read_markers') |
|
| 673 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 674 | - ->setParameter('user_id', $user->getUID()); |
|
| 675 | - |
|
| 676 | - try { |
|
| 677 | - $affectedRows = $query->execute(); |
|
| 678 | - } catch (DriverException $e) { |
|
| 679 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 680 | - return false; |
|
| 681 | - } |
|
| 682 | - return ($affectedRows > 0); |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - /** |
|
| 686 | - * sets the read marker for a given file to the specified date for the |
|
| 687 | - * provided user |
|
| 688 | - * |
|
| 689 | - * @param string $objectType |
|
| 690 | - * @param string $objectId |
|
| 691 | - * @param \DateTime $dateTime |
|
| 692 | - * @param IUser $user |
|
| 693 | - * @since 9.0.0 |
|
| 694 | - * @suppress SqlInjectionChecker |
|
| 695 | - */ |
|
| 696 | - public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
|
| 697 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 698 | - |
|
| 699 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 700 | - $values = [ |
|
| 701 | - 'user_id' => $qb->createNamedParameter($user->getUID()), |
|
| 702 | - 'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
|
| 703 | - 'object_type' => $qb->createNamedParameter($objectType), |
|
| 704 | - 'object_id' => $qb->createNamedParameter($objectId), |
|
| 705 | - ]; |
|
| 706 | - |
|
| 707 | - // Strategy: try to update, if this does not return affected rows, do an insert. |
|
| 708 | - $affectedRows = $qb |
|
| 709 | - ->update('comments_read_markers') |
|
| 710 | - ->set('user_id', $values['user_id']) |
|
| 711 | - ->set('marker_datetime', $values['marker_datetime']) |
|
| 712 | - ->set('object_type', $values['object_type']) |
|
| 713 | - ->set('object_id', $values['object_id']) |
|
| 714 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 715 | - ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 716 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 717 | - ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 718 | - ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 719 | - ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 720 | - ->execute(); |
|
| 721 | - |
|
| 722 | - if ($affectedRows > 0) { |
|
| 723 | - return; |
|
| 724 | - } |
|
| 725 | - |
|
| 726 | - $qb->insert('comments_read_markers') |
|
| 727 | - ->values($values) |
|
| 728 | - ->execute(); |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - /** |
|
| 732 | - * returns the read marker for a given file to the specified date for the |
|
| 733 | - * provided user. It returns null, when the marker is not present, i.e. |
|
| 734 | - * no comments were marked as read. |
|
| 735 | - * |
|
| 736 | - * @param string $objectType |
|
| 737 | - * @param string $objectId |
|
| 738 | - * @param IUser $user |
|
| 739 | - * @return \DateTime|null |
|
| 740 | - * @since 9.0.0 |
|
| 741 | - */ |
|
| 742 | - public function getReadMark($objectType, $objectId, IUser $user) { |
|
| 743 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 744 | - $resultStatement = $qb->select('marker_datetime') |
|
| 745 | - ->from('comments_read_markers') |
|
| 746 | - ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 747 | - ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 748 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 749 | - ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 750 | - ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 751 | - ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 752 | - ->execute(); |
|
| 753 | - |
|
| 754 | - $data = $resultStatement->fetch(); |
|
| 755 | - $resultStatement->closeCursor(); |
|
| 756 | - if (!$data || is_null($data['marker_datetime'])) { |
|
| 757 | - return null; |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - return new \DateTime($data['marker_datetime']); |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - /** |
|
| 764 | - * deletes the read markers on the specified object |
|
| 765 | - * |
|
| 766 | - * @param string $objectType |
|
| 767 | - * @param string $objectId |
|
| 768 | - * @return bool |
|
| 769 | - * @since 9.0.0 |
|
| 770 | - */ |
|
| 771 | - public function deleteReadMarksOnObject($objectType, $objectId) { |
|
| 772 | - $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 773 | - |
|
| 774 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 775 | - $query = $qb->delete('comments_read_markers') |
|
| 776 | - ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 777 | - ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 778 | - ->setParameter('object_type', $objectType) |
|
| 779 | - ->setParameter('object_id', $objectId); |
|
| 780 | - |
|
| 781 | - try { |
|
| 782 | - $affectedRows = $query->execute(); |
|
| 783 | - } catch (DriverException $e) { |
|
| 784 | - $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 785 | - return false; |
|
| 786 | - } |
|
| 787 | - return ($affectedRows > 0); |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * registers an Entity to the manager, so event notifications can be send |
|
| 792 | - * to consumers of the comments infrastructure |
|
| 793 | - * |
|
| 794 | - * @param \Closure $closure |
|
| 795 | - */ |
|
| 796 | - public function registerEventHandler(\Closure $closure) { |
|
| 797 | - $this->eventHandlerClosures[] = $closure; |
|
| 798 | - $this->eventHandlers = []; |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - /** |
|
| 802 | - * registers a method that resolves an ID to a display name for a given type |
|
| 803 | - * |
|
| 804 | - * @param string $type |
|
| 805 | - * @param \Closure $closure |
|
| 806 | - * @throws \OutOfBoundsException |
|
| 807 | - * @since 11.0.0 |
|
| 808 | - * |
|
| 809 | - * Only one resolver shall be registered per type. Otherwise a |
|
| 810 | - * \OutOfBoundsException has to thrown. |
|
| 811 | - */ |
|
| 812 | - public function registerDisplayNameResolver($type, \Closure $closure) { |
|
| 813 | - if (!is_string($type)) { |
|
| 814 | - throw new \InvalidArgumentException('String expected.'); |
|
| 815 | - } |
|
| 816 | - if (isset($this->displayNameResolvers[$type])) { |
|
| 817 | - throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
|
| 818 | - } |
|
| 819 | - $this->displayNameResolvers[$type] = $closure; |
|
| 820 | - } |
|
| 821 | - |
|
| 822 | - /** |
|
| 823 | - * resolves a given ID of a given Type to a display name. |
|
| 824 | - * |
|
| 825 | - * @param string $type |
|
| 826 | - * @param string $id |
|
| 827 | - * @return string |
|
| 828 | - * @throws \OutOfBoundsException |
|
| 829 | - * @since 11.0.0 |
|
| 830 | - * |
|
| 831 | - * If a provided type was not registered, an \OutOfBoundsException shall |
|
| 832 | - * be thrown. It is upon the resolver discretion what to return of the |
|
| 833 | - * provided ID is unknown. It must be ensured that a string is returned. |
|
| 834 | - */ |
|
| 835 | - public function resolveDisplayName($type, $id) { |
|
| 836 | - if (!is_string($type)) { |
|
| 837 | - throw new \InvalidArgumentException('String expected.'); |
|
| 838 | - } |
|
| 839 | - if (!isset($this->displayNameResolvers[$type])) { |
|
| 840 | - throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
|
| 841 | - } |
|
| 842 | - return (string)$this->displayNameResolvers[$type]($id); |
|
| 843 | - } |
|
| 844 | - |
|
| 845 | - /** |
|
| 846 | - * returns valid, registered entities |
|
| 847 | - * |
|
| 848 | - * @return \OCP\Comments\ICommentsEventHandler[] |
|
| 849 | - */ |
|
| 850 | - private function getEventHandlers() { |
|
| 851 | - if (!empty($this->eventHandlers)) { |
|
| 852 | - return $this->eventHandlers; |
|
| 853 | - } |
|
| 854 | - |
|
| 855 | - $this->eventHandlers = []; |
|
| 856 | - foreach ($this->eventHandlerClosures as $name => $closure) { |
|
| 857 | - $entity = $closure(); |
|
| 858 | - if (!($entity instanceof ICommentsEventHandler)) { |
|
| 859 | - throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
|
| 860 | - } |
|
| 861 | - $this->eventHandlers[$name] = $entity; |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - return $this->eventHandlers; |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - /** |
|
| 868 | - * sends notifications to the registered entities |
|
| 869 | - * |
|
| 870 | - * @param $eventType |
|
| 871 | - * @param IComment $comment |
|
| 872 | - */ |
|
| 873 | - private function sendEvent($eventType, IComment $comment) { |
|
| 874 | - $entities = $this->getEventHandlers(); |
|
| 875 | - $event = new CommentsEvent($eventType, $comment); |
|
| 876 | - foreach ($entities as $entity) { |
|
| 877 | - $entity->handle($event); |
|
| 878 | - } |
|
| 879 | - } |
|
| 41 | + /** @var IDBConnection */ |
|
| 42 | + protected $dbConn; |
|
| 43 | + |
|
| 44 | + /** @var ILogger */ |
|
| 45 | + protected $logger; |
|
| 46 | + |
|
| 47 | + /** @var IConfig */ |
|
| 48 | + protected $config; |
|
| 49 | + |
|
| 50 | + /** @var IComment[] */ |
|
| 51 | + protected $commentsCache = []; |
|
| 52 | + |
|
| 53 | + /** @var \Closure[] */ |
|
| 54 | + protected $eventHandlerClosures = []; |
|
| 55 | + |
|
| 56 | + /** @var ICommentsEventHandler[] */ |
|
| 57 | + protected $eventHandlers = []; |
|
| 58 | + |
|
| 59 | + /** @var \Closure[] */ |
|
| 60 | + protected $displayNameResolvers = []; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Manager constructor. |
|
| 64 | + * |
|
| 65 | + * @param IDBConnection $dbConn |
|
| 66 | + * @param ILogger $logger |
|
| 67 | + * @param IConfig $config |
|
| 68 | + */ |
|
| 69 | + public function __construct( |
|
| 70 | + IDBConnection $dbConn, |
|
| 71 | + ILogger $logger, |
|
| 72 | + IConfig $config |
|
| 73 | + ) { |
|
| 74 | + $this->dbConn = $dbConn; |
|
| 75 | + $this->logger = $logger; |
|
| 76 | + $this->config = $config; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * converts data base data into PHP native, proper types as defined by |
|
| 81 | + * IComment interface. |
|
| 82 | + * |
|
| 83 | + * @param array $data |
|
| 84 | + * @return array |
|
| 85 | + */ |
|
| 86 | + protected function normalizeDatabaseData(array $data) { |
|
| 87 | + $data['id'] = strval($data['id']); |
|
| 88 | + $data['parent_id'] = strval($data['parent_id']); |
|
| 89 | + $data['topmost_parent_id'] = strval($data['topmost_parent_id']); |
|
| 90 | + $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']); |
|
| 91 | + if (!is_null($data['latest_child_timestamp'])) { |
|
| 92 | + $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']); |
|
| 93 | + } |
|
| 94 | + $data['children_count'] = intval($data['children_count']); |
|
| 95 | + return $data; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * prepares a comment for an insert or update operation after making sure |
|
| 100 | + * all necessary fields have a value assigned. |
|
| 101 | + * |
|
| 102 | + * @param IComment $comment |
|
| 103 | + * @return IComment returns the same updated IComment instance as provided |
|
| 104 | + * by parameter for convenience |
|
| 105 | + * @throws \UnexpectedValueException |
|
| 106 | + */ |
|
| 107 | + protected function prepareCommentForDatabaseWrite(IComment $comment) { |
|
| 108 | + if (!$comment->getActorType() |
|
| 109 | + || !$comment->getActorId() |
|
| 110 | + || !$comment->getObjectType() |
|
| 111 | + || !$comment->getObjectId() |
|
| 112 | + || !$comment->getVerb() |
|
| 113 | + ) { |
|
| 114 | + throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving'); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if ($comment->getId() === '') { |
|
| 118 | + $comment->setChildrenCount(0); |
|
| 119 | + $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC'))); |
|
| 120 | + $comment->setLatestChildDateTime(null); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (is_null($comment->getCreationDateTime())) { |
|
| 124 | + $comment->setCreationDateTime(new \DateTime()); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + if ($comment->getParentId() !== '0') { |
|
| 128 | + $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId())); |
|
| 129 | + } else { |
|
| 130 | + $comment->setTopmostParentId('0'); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + $this->cache($comment); |
|
| 134 | + |
|
| 135 | + return $comment; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * returns the topmost parent id of a given comment identified by ID |
|
| 140 | + * |
|
| 141 | + * @param string $id |
|
| 142 | + * @return string |
|
| 143 | + * @throws NotFoundException |
|
| 144 | + */ |
|
| 145 | + protected function determineTopmostParentId($id) { |
|
| 146 | + $comment = $this->get($id); |
|
| 147 | + if ($comment->getParentId() === '0') { |
|
| 148 | + return $comment->getId(); |
|
| 149 | + } else { |
|
| 150 | + return $this->determineTopmostParentId($comment->getId()); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * updates child information of a comment |
|
| 156 | + * |
|
| 157 | + * @param string $id |
|
| 158 | + * @param \DateTime $cDateTime the date time of the most recent child |
|
| 159 | + * @throws NotFoundException |
|
| 160 | + */ |
|
| 161 | + protected function updateChildrenInformation($id, \DateTime $cDateTime) { |
|
| 162 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 163 | + $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 164 | + ->from('comments') |
|
| 165 | + ->where($qb->expr()->eq('parent_id', $qb->createParameter('id'))) |
|
| 166 | + ->setParameter('id', $id); |
|
| 167 | + |
|
| 168 | + $resultStatement = $query->execute(); |
|
| 169 | + $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 170 | + $resultStatement->closeCursor(); |
|
| 171 | + $children = intval($data[0]); |
|
| 172 | + |
|
| 173 | + $comment = $this->get($id); |
|
| 174 | + $comment->setChildrenCount($children); |
|
| 175 | + $comment->setLatestChildDateTime($cDateTime); |
|
| 176 | + $this->save($comment); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Tests whether actor or object type and id parameters are acceptable. |
|
| 181 | + * Throws exception if not. |
|
| 182 | + * |
|
| 183 | + * @param string $role |
|
| 184 | + * @param string $type |
|
| 185 | + * @param string $id |
|
| 186 | + * @throws \InvalidArgumentException |
|
| 187 | + */ |
|
| 188 | + protected function checkRoleParameters($role, $type, $id) { |
|
| 189 | + if ( |
|
| 190 | + !is_string($type) || empty($type) |
|
| 191 | + || !is_string($id) || empty($id) |
|
| 192 | + ) { |
|
| 193 | + throw new \InvalidArgumentException($role . ' parameters must be string and not empty'); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * run-time caches a comment |
|
| 199 | + * |
|
| 200 | + * @param IComment $comment |
|
| 201 | + */ |
|
| 202 | + protected function cache(IComment $comment) { |
|
| 203 | + $id = $comment->getId(); |
|
| 204 | + if (empty($id)) { |
|
| 205 | + return; |
|
| 206 | + } |
|
| 207 | + $this->commentsCache[strval($id)] = $comment; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * removes an entry from the comments run time cache |
|
| 212 | + * |
|
| 213 | + * @param mixed $id the comment's id |
|
| 214 | + */ |
|
| 215 | + protected function uncache($id) { |
|
| 216 | + $id = strval($id); |
|
| 217 | + if (isset($this->commentsCache[$id])) { |
|
| 218 | + unset($this->commentsCache[$id]); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * returns a comment instance |
|
| 224 | + * |
|
| 225 | + * @param string $id the ID of the comment |
|
| 226 | + * @return IComment |
|
| 227 | + * @throws NotFoundException |
|
| 228 | + * @throws \InvalidArgumentException |
|
| 229 | + * @since 9.0.0 |
|
| 230 | + */ |
|
| 231 | + public function get($id) { |
|
| 232 | + if (intval($id) === 0) { |
|
| 233 | + throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.'); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + if (isset($this->commentsCache[$id])) { |
|
| 237 | + return $this->commentsCache[$id]; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 241 | + $resultStatement = $qb->select('*') |
|
| 242 | + ->from('comments') |
|
| 243 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 244 | + ->setParameter('id', $id, IQueryBuilder::PARAM_INT) |
|
| 245 | + ->execute(); |
|
| 246 | + |
|
| 247 | + $data = $resultStatement->fetch(); |
|
| 248 | + $resultStatement->closeCursor(); |
|
| 249 | + if (!$data) { |
|
| 250 | + throw new NotFoundException(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 254 | + $this->cache($comment); |
|
| 255 | + return $comment; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * returns the comment specified by the id and all it's child comments. |
|
| 260 | + * At this point of time, we do only support one level depth. |
|
| 261 | + * |
|
| 262 | + * @param string $id |
|
| 263 | + * @param int $limit max number of entries to return, 0 returns all |
|
| 264 | + * @param int $offset the start entry |
|
| 265 | + * @return array |
|
| 266 | + * @since 9.0.0 |
|
| 267 | + * |
|
| 268 | + * The return array looks like this |
|
| 269 | + * [ |
|
| 270 | + * 'comment' => IComment, // root comment |
|
| 271 | + * 'replies' => |
|
| 272 | + * [ |
|
| 273 | + * 0 => |
|
| 274 | + * [ |
|
| 275 | + * 'comment' => IComment, |
|
| 276 | + * 'replies' => [] |
|
| 277 | + * ] |
|
| 278 | + * 1 => |
|
| 279 | + * [ |
|
| 280 | + * 'comment' => IComment, |
|
| 281 | + * 'replies'=> [] |
|
| 282 | + * ], |
|
| 283 | + * … |
|
| 284 | + * ] |
|
| 285 | + * ] |
|
| 286 | + */ |
|
| 287 | + public function getTree($id, $limit = 0, $offset = 0) { |
|
| 288 | + $tree = []; |
|
| 289 | + $tree['comment'] = $this->get($id); |
|
| 290 | + $tree['replies'] = []; |
|
| 291 | + |
|
| 292 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 293 | + $query = $qb->select('*') |
|
| 294 | + ->from('comments') |
|
| 295 | + ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id'))) |
|
| 296 | + ->orderBy('creation_timestamp', 'DESC') |
|
| 297 | + ->setParameter('id', $id); |
|
| 298 | + |
|
| 299 | + if ($limit > 0) { |
|
| 300 | + $query->setMaxResults($limit); |
|
| 301 | + } |
|
| 302 | + if ($offset > 0) { |
|
| 303 | + $query->setFirstResult($offset); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + $resultStatement = $query->execute(); |
|
| 307 | + while ($data = $resultStatement->fetch()) { |
|
| 308 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 309 | + $this->cache($comment); |
|
| 310 | + $tree['replies'][] = [ |
|
| 311 | + 'comment' => $comment, |
|
| 312 | + 'replies' => [] |
|
| 313 | + ]; |
|
| 314 | + } |
|
| 315 | + $resultStatement->closeCursor(); |
|
| 316 | + |
|
| 317 | + return $tree; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * returns comments for a specific object (e.g. a file). |
|
| 322 | + * |
|
| 323 | + * The sort order is always newest to oldest. |
|
| 324 | + * |
|
| 325 | + * @param string $objectType the object type, e.g. 'files' |
|
| 326 | + * @param string $objectId the id of the object |
|
| 327 | + * @param int $limit optional, number of maximum comments to be returned. if |
|
| 328 | + * not specified, all comments are returned. |
|
| 329 | + * @param int $offset optional, starting point |
|
| 330 | + * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 331 | + * that may be returned |
|
| 332 | + * @return IComment[] |
|
| 333 | + * @since 9.0.0 |
|
| 334 | + */ |
|
| 335 | + public function getForObject( |
|
| 336 | + $objectType, |
|
| 337 | + $objectId, |
|
| 338 | + $limit = 0, |
|
| 339 | + $offset = 0, |
|
| 340 | + \DateTime $notOlderThan = null |
|
| 341 | + ) { |
|
| 342 | + $comments = []; |
|
| 343 | + |
|
| 344 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 345 | + $query = $qb->select('*') |
|
| 346 | + ->from('comments') |
|
| 347 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 348 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 349 | + ->orderBy('creation_timestamp', 'DESC') |
|
| 350 | + ->setParameter('type', $objectType) |
|
| 351 | + ->setParameter('id', $objectId); |
|
| 352 | + |
|
| 353 | + if ($limit > 0) { |
|
| 354 | + $query->setMaxResults($limit); |
|
| 355 | + } |
|
| 356 | + if ($offset > 0) { |
|
| 357 | + $query->setFirstResult($offset); |
|
| 358 | + } |
|
| 359 | + if (!is_null($notOlderThan)) { |
|
| 360 | + $query |
|
| 361 | + ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 362 | + ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + $resultStatement = $query->execute(); |
|
| 366 | + while ($data = $resultStatement->fetch()) { |
|
| 367 | + $comment = new Comment($this->normalizeDatabaseData($data)); |
|
| 368 | + $this->cache($comment); |
|
| 369 | + $comments[] = $comment; |
|
| 370 | + } |
|
| 371 | + $resultStatement->closeCursor(); |
|
| 372 | + |
|
| 373 | + return $comments; |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * @param $objectType string the object type, e.g. 'files' |
|
| 378 | + * @param $objectId string the id of the object |
|
| 379 | + * @param \DateTime $notOlderThan optional, timestamp of the oldest comments |
|
| 380 | + * that may be returned |
|
| 381 | + * @return Int |
|
| 382 | + * @since 9.0.0 |
|
| 383 | + */ |
|
| 384 | + public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) { |
|
| 385 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 386 | + $query = $qb->select($qb->createFunction('COUNT(`id`)')) |
|
| 387 | + ->from('comments') |
|
| 388 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 389 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 390 | + ->setParameter('type', $objectType) |
|
| 391 | + ->setParameter('id', $objectId); |
|
| 392 | + |
|
| 393 | + if (!is_null($notOlderThan)) { |
|
| 394 | + $query |
|
| 395 | + ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan'))) |
|
| 396 | + ->setParameter('notOlderThan', $notOlderThan, 'datetime'); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + $resultStatement = $query->execute(); |
|
| 400 | + $data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
| 401 | + $resultStatement->closeCursor(); |
|
| 402 | + return intval($data[0]); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Get the number of unread comments for all files in a folder |
|
| 407 | + * |
|
| 408 | + * @param int $folderId |
|
| 409 | + * @param IUser $user |
|
| 410 | + * @return array [$fileId => $unreadCount] |
|
| 411 | + */ |
|
| 412 | + public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) { |
|
| 413 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 414 | + $query = $qb->select('fileid', $qb->createFunction( |
|
| 415 | + 'COUNT(' . $qb->getColumnName('c.id') . ')') |
|
| 416 | + )->from('comments', 'c') |
|
| 417 | + ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX( |
|
| 418 | + $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')), |
|
| 419 | + $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)) |
|
| 420 | + )) |
|
| 421 | + ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX( |
|
| 422 | + $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')), |
|
| 423 | + $qb->expr()->eq('m.object_id', 'c.object_id'), |
|
| 424 | + $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())) |
|
| 425 | + )) |
|
| 426 | + ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId))) |
|
| 427 | + ->andWhere($qb->expr()->orX( |
|
| 428 | + $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'), |
|
| 429 | + $qb->expr()->isNull('marker_datetime') |
|
| 430 | + )) |
|
| 431 | + ->groupBy('f.fileid'); |
|
| 432 | + |
|
| 433 | + $resultStatement = $query->execute(); |
|
| 434 | + return array_map(function ($count) { |
|
| 435 | + return (int)$count; |
|
| 436 | + }, $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR)); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * creates a new comment and returns it. At this point of time, it is not |
|
| 441 | + * saved in the used data storage. Use save() after setting other fields |
|
| 442 | + * of the comment (e.g. message or verb). |
|
| 443 | + * |
|
| 444 | + * @param string $actorType the actor type (e.g. 'users') |
|
| 445 | + * @param string $actorId a user id |
|
| 446 | + * @param string $objectType the object type the comment is attached to |
|
| 447 | + * @param string $objectId the object id the comment is attached to |
|
| 448 | + * @return IComment |
|
| 449 | + * @since 9.0.0 |
|
| 450 | + */ |
|
| 451 | + public function create($actorType, $actorId, $objectType, $objectId) { |
|
| 452 | + $comment = new Comment(); |
|
| 453 | + $comment |
|
| 454 | + ->setActor($actorType, $actorId) |
|
| 455 | + ->setObject($objectType, $objectId); |
|
| 456 | + return $comment; |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * permanently deletes the comment specified by the ID |
|
| 461 | + * |
|
| 462 | + * When the comment has child comments, their parent ID will be changed to |
|
| 463 | + * the parent ID of the item that is to be deleted. |
|
| 464 | + * |
|
| 465 | + * @param string $id |
|
| 466 | + * @return bool |
|
| 467 | + * @throws \InvalidArgumentException |
|
| 468 | + * @since 9.0.0 |
|
| 469 | + */ |
|
| 470 | + public function delete($id) { |
|
| 471 | + if (!is_string($id)) { |
|
| 472 | + throw new \InvalidArgumentException('Parameter must be string'); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + try { |
|
| 476 | + $comment = $this->get($id); |
|
| 477 | + } catch (\Exception $e) { |
|
| 478 | + // Ignore exceptions, we just don't fire a hook then |
|
| 479 | + $comment = null; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 483 | + $query = $qb->delete('comments') |
|
| 484 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 485 | + ->setParameter('id', $id); |
|
| 486 | + |
|
| 487 | + try { |
|
| 488 | + $affectedRows = $query->execute(); |
|
| 489 | + $this->uncache($id); |
|
| 490 | + } catch (DriverException $e) { |
|
| 491 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 492 | + return false; |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + if ($affectedRows > 0 && $comment instanceof IComment) { |
|
| 496 | + $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + return ($affectedRows > 0); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * saves the comment permanently |
|
| 504 | + * |
|
| 505 | + * if the supplied comment has an empty ID, a new entry comment will be |
|
| 506 | + * saved and the instance updated with the new ID. |
|
| 507 | + * |
|
| 508 | + * Otherwise, an existing comment will be updated. |
|
| 509 | + * |
|
| 510 | + * Throws NotFoundException when a comment that is to be updated does not |
|
| 511 | + * exist anymore at this point of time. |
|
| 512 | + * |
|
| 513 | + * @param IComment $comment |
|
| 514 | + * @return bool |
|
| 515 | + * @throws NotFoundException |
|
| 516 | + * @since 9.0.0 |
|
| 517 | + */ |
|
| 518 | + public function save(IComment $comment) { |
|
| 519 | + if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') { |
|
| 520 | + $result = $this->insert($comment); |
|
| 521 | + } else { |
|
| 522 | + $result = $this->update($comment); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + if ($result && !!$comment->getParentId()) { |
|
| 526 | + $this->updateChildrenInformation( |
|
| 527 | + $comment->getParentId(), |
|
| 528 | + $comment->getCreationDateTime() |
|
| 529 | + ); |
|
| 530 | + $this->cache($comment); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + return $result; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * inserts the provided comment in the database |
|
| 538 | + * |
|
| 539 | + * @param IComment $comment |
|
| 540 | + * @return bool |
|
| 541 | + */ |
|
| 542 | + protected function insert(IComment &$comment) { |
|
| 543 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 544 | + $affectedRows = $qb |
|
| 545 | + ->insert('comments') |
|
| 546 | + ->values([ |
|
| 547 | + 'parent_id' => $qb->createNamedParameter($comment->getParentId()), |
|
| 548 | + 'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()), |
|
| 549 | + 'children_count' => $qb->createNamedParameter($comment->getChildrenCount()), |
|
| 550 | + 'actor_type' => $qb->createNamedParameter($comment->getActorType()), |
|
| 551 | + 'actor_id' => $qb->createNamedParameter($comment->getActorId()), |
|
| 552 | + 'message' => $qb->createNamedParameter($comment->getMessage()), |
|
| 553 | + 'verb' => $qb->createNamedParameter($comment->getVerb()), |
|
| 554 | + 'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'), |
|
| 555 | + 'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'), |
|
| 556 | + 'object_type' => $qb->createNamedParameter($comment->getObjectType()), |
|
| 557 | + 'object_id' => $qb->createNamedParameter($comment->getObjectId()), |
|
| 558 | + ]) |
|
| 559 | + ->execute(); |
|
| 560 | + |
|
| 561 | + if ($affectedRows > 0) { |
|
| 562 | + $comment->setId(strval($qb->getLastInsertId())); |
|
| 563 | + $this->sendEvent(CommentsEvent::EVENT_ADD, $comment); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + return $affectedRows > 0; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * updates a Comment data row |
|
| 571 | + * |
|
| 572 | + * @param IComment $comment |
|
| 573 | + * @return bool |
|
| 574 | + * @throws NotFoundException |
|
| 575 | + */ |
|
| 576 | + protected function update(IComment $comment) { |
|
| 577 | + // for properly working preUpdate Events we need the old comments as is |
|
| 578 | + // in the DB and overcome caching. Also avoid that outdated information stays. |
|
| 579 | + $this->uncache($comment->getId()); |
|
| 580 | + $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId())); |
|
| 581 | + $this->uncache($comment->getId()); |
|
| 582 | + |
|
| 583 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 584 | + $affectedRows = $qb |
|
| 585 | + ->update('comments') |
|
| 586 | + ->set('parent_id', $qb->createNamedParameter($comment->getParentId())) |
|
| 587 | + ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId())) |
|
| 588 | + ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount())) |
|
| 589 | + ->set('actor_type', $qb->createNamedParameter($comment->getActorType())) |
|
| 590 | + ->set('actor_id', $qb->createNamedParameter($comment->getActorId())) |
|
| 591 | + ->set('message', $qb->createNamedParameter($comment->getMessage())) |
|
| 592 | + ->set('verb', $qb->createNamedParameter($comment->getVerb())) |
|
| 593 | + ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime')) |
|
| 594 | + ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime')) |
|
| 595 | + ->set('object_type', $qb->createNamedParameter($comment->getObjectType())) |
|
| 596 | + ->set('object_id', $qb->createNamedParameter($comment->getObjectId())) |
|
| 597 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 598 | + ->setParameter('id', $comment->getId()) |
|
| 599 | + ->execute(); |
|
| 600 | + |
|
| 601 | + if ($affectedRows === 0) { |
|
| 602 | + throw new NotFoundException('Comment to update does ceased to exist'); |
|
| 603 | + } |
|
| 604 | + |
|
| 605 | + $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment); |
|
| 606 | + |
|
| 607 | + return $affectedRows > 0; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * removes references to specific actor (e.g. on user delete) of a comment. |
|
| 612 | + * The comment itself must not get lost/deleted. |
|
| 613 | + * |
|
| 614 | + * @param string $actorType the actor type (e.g. 'users') |
|
| 615 | + * @param string $actorId a user id |
|
| 616 | + * @return boolean |
|
| 617 | + * @since 9.0.0 |
|
| 618 | + */ |
|
| 619 | + public function deleteReferencesOfActor($actorType, $actorId) { |
|
| 620 | + $this->checkRoleParameters('Actor', $actorType, $actorId); |
|
| 621 | + |
|
| 622 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 623 | + $affectedRows = $qb |
|
| 624 | + ->update('comments') |
|
| 625 | + ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 626 | + ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER)) |
|
| 627 | + ->where($qb->expr()->eq('actor_type', $qb->createParameter('type'))) |
|
| 628 | + ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id'))) |
|
| 629 | + ->setParameter('type', $actorType) |
|
| 630 | + ->setParameter('id', $actorId) |
|
| 631 | + ->execute(); |
|
| 632 | + |
|
| 633 | + $this->commentsCache = []; |
|
| 634 | + |
|
| 635 | + return is_int($affectedRows); |
|
| 636 | + } |
|
| 637 | + |
|
| 638 | + /** |
|
| 639 | + * deletes all comments made of a specific object (e.g. on file delete) |
|
| 640 | + * |
|
| 641 | + * @param string $objectType the object type (e.g. 'files') |
|
| 642 | + * @param string $objectId e.g. the file id |
|
| 643 | + * @return boolean |
|
| 644 | + * @since 9.0.0 |
|
| 645 | + */ |
|
| 646 | + public function deleteCommentsAtObject($objectType, $objectId) { |
|
| 647 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 648 | + |
|
| 649 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 650 | + $affectedRows = $qb |
|
| 651 | + ->delete('comments') |
|
| 652 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('type'))) |
|
| 653 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id'))) |
|
| 654 | + ->setParameter('type', $objectType) |
|
| 655 | + ->setParameter('id', $objectId) |
|
| 656 | + ->execute(); |
|
| 657 | + |
|
| 658 | + $this->commentsCache = []; |
|
| 659 | + |
|
| 660 | + return is_int($affectedRows); |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * deletes the read markers for the specified user |
|
| 665 | + * |
|
| 666 | + * @param \OCP\IUser $user |
|
| 667 | + * @return bool |
|
| 668 | + * @since 9.0.0 |
|
| 669 | + */ |
|
| 670 | + public function deleteReadMarksFromUser(IUser $user) { |
|
| 671 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 672 | + $query = $qb->delete('comments_read_markers') |
|
| 673 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 674 | + ->setParameter('user_id', $user->getUID()); |
|
| 675 | + |
|
| 676 | + try { |
|
| 677 | + $affectedRows = $query->execute(); |
|
| 678 | + } catch (DriverException $e) { |
|
| 679 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 680 | + return false; |
|
| 681 | + } |
|
| 682 | + return ($affectedRows > 0); |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + /** |
|
| 686 | + * sets the read marker for a given file to the specified date for the |
|
| 687 | + * provided user |
|
| 688 | + * |
|
| 689 | + * @param string $objectType |
|
| 690 | + * @param string $objectId |
|
| 691 | + * @param \DateTime $dateTime |
|
| 692 | + * @param IUser $user |
|
| 693 | + * @since 9.0.0 |
|
| 694 | + * @suppress SqlInjectionChecker |
|
| 695 | + */ |
|
| 696 | + public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) { |
|
| 697 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 698 | + |
|
| 699 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 700 | + $values = [ |
|
| 701 | + 'user_id' => $qb->createNamedParameter($user->getUID()), |
|
| 702 | + 'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'), |
|
| 703 | + 'object_type' => $qb->createNamedParameter($objectType), |
|
| 704 | + 'object_id' => $qb->createNamedParameter($objectId), |
|
| 705 | + ]; |
|
| 706 | + |
|
| 707 | + // Strategy: try to update, if this does not return affected rows, do an insert. |
|
| 708 | + $affectedRows = $qb |
|
| 709 | + ->update('comments_read_markers') |
|
| 710 | + ->set('user_id', $values['user_id']) |
|
| 711 | + ->set('marker_datetime', $values['marker_datetime']) |
|
| 712 | + ->set('object_type', $values['object_type']) |
|
| 713 | + ->set('object_id', $values['object_id']) |
|
| 714 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 715 | + ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 716 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 717 | + ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 718 | + ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 719 | + ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 720 | + ->execute(); |
|
| 721 | + |
|
| 722 | + if ($affectedRows > 0) { |
|
| 723 | + return; |
|
| 724 | + } |
|
| 725 | + |
|
| 726 | + $qb->insert('comments_read_markers') |
|
| 727 | + ->values($values) |
|
| 728 | + ->execute(); |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + /** |
|
| 732 | + * returns the read marker for a given file to the specified date for the |
|
| 733 | + * provided user. It returns null, when the marker is not present, i.e. |
|
| 734 | + * no comments were marked as read. |
|
| 735 | + * |
|
| 736 | + * @param string $objectType |
|
| 737 | + * @param string $objectId |
|
| 738 | + * @param IUser $user |
|
| 739 | + * @return \DateTime|null |
|
| 740 | + * @since 9.0.0 |
|
| 741 | + */ |
|
| 742 | + public function getReadMark($objectType, $objectId, IUser $user) { |
|
| 743 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 744 | + $resultStatement = $qb->select('marker_datetime') |
|
| 745 | + ->from('comments_read_markers') |
|
| 746 | + ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id'))) |
|
| 747 | + ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 748 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 749 | + ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR) |
|
| 750 | + ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR) |
|
| 751 | + ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR) |
|
| 752 | + ->execute(); |
|
| 753 | + |
|
| 754 | + $data = $resultStatement->fetch(); |
|
| 755 | + $resultStatement->closeCursor(); |
|
| 756 | + if (!$data || is_null($data['marker_datetime'])) { |
|
| 757 | + return null; |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + return new \DateTime($data['marker_datetime']); |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + /** |
|
| 764 | + * deletes the read markers on the specified object |
|
| 765 | + * |
|
| 766 | + * @param string $objectType |
|
| 767 | + * @param string $objectId |
|
| 768 | + * @return bool |
|
| 769 | + * @since 9.0.0 |
|
| 770 | + */ |
|
| 771 | + public function deleteReadMarksOnObject($objectType, $objectId) { |
|
| 772 | + $this->checkRoleParameters('Object', $objectType, $objectId); |
|
| 773 | + |
|
| 774 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 775 | + $query = $qb->delete('comments_read_markers') |
|
| 776 | + ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type'))) |
|
| 777 | + ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id'))) |
|
| 778 | + ->setParameter('object_type', $objectType) |
|
| 779 | + ->setParameter('object_id', $objectId); |
|
| 780 | + |
|
| 781 | + try { |
|
| 782 | + $affectedRows = $query->execute(); |
|
| 783 | + } catch (DriverException $e) { |
|
| 784 | + $this->logger->logException($e, ['app' => 'core_comments']); |
|
| 785 | + return false; |
|
| 786 | + } |
|
| 787 | + return ($affectedRows > 0); |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * registers an Entity to the manager, so event notifications can be send |
|
| 792 | + * to consumers of the comments infrastructure |
|
| 793 | + * |
|
| 794 | + * @param \Closure $closure |
|
| 795 | + */ |
|
| 796 | + public function registerEventHandler(\Closure $closure) { |
|
| 797 | + $this->eventHandlerClosures[] = $closure; |
|
| 798 | + $this->eventHandlers = []; |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + /** |
|
| 802 | + * registers a method that resolves an ID to a display name for a given type |
|
| 803 | + * |
|
| 804 | + * @param string $type |
|
| 805 | + * @param \Closure $closure |
|
| 806 | + * @throws \OutOfBoundsException |
|
| 807 | + * @since 11.0.0 |
|
| 808 | + * |
|
| 809 | + * Only one resolver shall be registered per type. Otherwise a |
|
| 810 | + * \OutOfBoundsException has to thrown. |
|
| 811 | + */ |
|
| 812 | + public function registerDisplayNameResolver($type, \Closure $closure) { |
|
| 813 | + if (!is_string($type)) { |
|
| 814 | + throw new \InvalidArgumentException('String expected.'); |
|
| 815 | + } |
|
| 816 | + if (isset($this->displayNameResolvers[$type])) { |
|
| 817 | + throw new \OutOfBoundsException('Displayname resolver for this type already registered'); |
|
| 818 | + } |
|
| 819 | + $this->displayNameResolvers[$type] = $closure; |
|
| 820 | + } |
|
| 821 | + |
|
| 822 | + /** |
|
| 823 | + * resolves a given ID of a given Type to a display name. |
|
| 824 | + * |
|
| 825 | + * @param string $type |
|
| 826 | + * @param string $id |
|
| 827 | + * @return string |
|
| 828 | + * @throws \OutOfBoundsException |
|
| 829 | + * @since 11.0.0 |
|
| 830 | + * |
|
| 831 | + * If a provided type was not registered, an \OutOfBoundsException shall |
|
| 832 | + * be thrown. It is upon the resolver discretion what to return of the |
|
| 833 | + * provided ID is unknown. It must be ensured that a string is returned. |
|
| 834 | + */ |
|
| 835 | + public function resolveDisplayName($type, $id) { |
|
| 836 | + if (!is_string($type)) { |
|
| 837 | + throw new \InvalidArgumentException('String expected.'); |
|
| 838 | + } |
|
| 839 | + if (!isset($this->displayNameResolvers[$type])) { |
|
| 840 | + throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
|
| 841 | + } |
|
| 842 | + return (string)$this->displayNameResolvers[$type]($id); |
|
| 843 | + } |
|
| 844 | + |
|
| 845 | + /** |
|
| 846 | + * returns valid, registered entities |
|
| 847 | + * |
|
| 848 | + * @return \OCP\Comments\ICommentsEventHandler[] |
|
| 849 | + */ |
|
| 850 | + private function getEventHandlers() { |
|
| 851 | + if (!empty($this->eventHandlers)) { |
|
| 852 | + return $this->eventHandlers; |
|
| 853 | + } |
|
| 854 | + |
|
| 855 | + $this->eventHandlers = []; |
|
| 856 | + foreach ($this->eventHandlerClosures as $name => $closure) { |
|
| 857 | + $entity = $closure(); |
|
| 858 | + if (!($entity instanceof ICommentsEventHandler)) { |
|
| 859 | + throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface'); |
|
| 860 | + } |
|
| 861 | + $this->eventHandlers[$name] = $entity; |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + return $this->eventHandlers; |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + /** |
|
| 868 | + * sends notifications to the registered entities |
|
| 869 | + * |
|
| 870 | + * @param $eventType |
|
| 871 | + * @param IComment $comment |
|
| 872 | + */ |
|
| 873 | + private function sendEvent($eventType, IComment $comment) { |
|
| 874 | + $entities = $this->getEventHandlers(); |
|
| 875 | + $event = new CommentsEvent($eventType, $comment); |
|
| 876 | + foreach ($entities as $entity) { |
|
| 877 | + $entity->handle($event); |
|
| 878 | + } |
|
| 879 | + } |
|
| 880 | 880 | } |