@@ -31,89 +31,89 @@ |
||
31 | 31 | |
32 | 32 | class OldGroupMembershipShares implements IRepairStep { |
33 | 33 | |
34 | - /** @var \OCP\IDBConnection */ |
|
35 | - protected $connection; |
|
36 | - |
|
37 | - /** @var \OCP\IGroupManager */ |
|
38 | - protected $groupManager; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var array [gid => [uid => (bool)]] |
|
42 | - */ |
|
43 | - protected $memberships; |
|
44 | - |
|
45 | - /** |
|
46 | - * @param IDBConnection $connection |
|
47 | - * @param IGroupManager $groupManager |
|
48 | - */ |
|
49 | - public function __construct(IDBConnection $connection, IGroupManager $groupManager) { |
|
50 | - $this->connection = $connection; |
|
51 | - $this->groupManager = $groupManager; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Returns the step's name |
|
56 | - * |
|
57 | - * @return string |
|
58 | - */ |
|
59 | - public function getName() { |
|
60 | - return 'Remove shares of old group memberships'; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Run repair step. |
|
65 | - * Must throw exception on error. |
|
66 | - * |
|
67 | - * @throws \Exception in case of failure |
|
68 | - * @suppress SqlInjectionChecker |
|
69 | - */ |
|
70 | - public function run(IOutput $output) { |
|
71 | - $deletedEntries = 0; |
|
72 | - |
|
73 | - $query = $this->connection->getQueryBuilder(); |
|
74 | - $query->select('s1.id')->selectAlias('s1.share_with', 'user')->selectAlias('s2.share_with', 'group') |
|
75 | - ->from('share', 's1') |
|
76 | - ->where($query->expr()->isNotNull('s1.parent')) |
|
77 | - // \OC\Share\Constant::$shareTypeGroupUserUnique === 2 |
|
78 | - ->andWhere($query->expr()->eq('s1.share_type', $query->expr()->literal(2))) |
|
79 | - ->andWhere($query->expr()->isNotNull('s2.id')) |
|
80 | - ->andWhere($query->expr()->eq('s2.share_type', $query->expr()->literal(Share::SHARE_TYPE_GROUP))) |
|
81 | - ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id')); |
|
82 | - |
|
83 | - $deleteQuery = $this->connection->getQueryBuilder(); |
|
84 | - $deleteQuery->delete('share') |
|
85 | - ->where($query->expr()->eq('id', $deleteQuery->createParameter('share'))); |
|
86 | - |
|
87 | - $result = $query->execute(); |
|
88 | - while ($row = $result->fetch()) { |
|
89 | - if (!$this->isMember($row['group'], $row['user'])) { |
|
90 | - $deletedEntries += $deleteQuery->setParameter('share', (int) $row['id']) |
|
91 | - ->execute(); |
|
92 | - } |
|
93 | - } |
|
94 | - $result->closeCursor(); |
|
95 | - |
|
96 | - if ($deletedEntries) { |
|
97 | - $output->info('Removed ' . $deletedEntries . ' shares where user is not a member of the group anymore'); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param string $gid |
|
103 | - * @param string $uid |
|
104 | - * @return bool |
|
105 | - */ |
|
106 | - protected function isMember($gid, $uid) { |
|
107 | - if (isset($this->memberships[$gid][$uid])) { |
|
108 | - return $this->memberships[$gid][$uid]; |
|
109 | - } |
|
110 | - |
|
111 | - $isMember = $this->groupManager->isInGroup($uid, $gid); |
|
112 | - if (!isset($this->memberships[$gid])) { |
|
113 | - $this->memberships[$gid] = []; |
|
114 | - } |
|
115 | - $this->memberships[$gid][$uid] = $isMember; |
|
116 | - |
|
117 | - return $isMember; |
|
118 | - } |
|
34 | + /** @var \OCP\IDBConnection */ |
|
35 | + protected $connection; |
|
36 | + |
|
37 | + /** @var \OCP\IGroupManager */ |
|
38 | + protected $groupManager; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var array [gid => [uid => (bool)]] |
|
42 | + */ |
|
43 | + protected $memberships; |
|
44 | + |
|
45 | + /** |
|
46 | + * @param IDBConnection $connection |
|
47 | + * @param IGroupManager $groupManager |
|
48 | + */ |
|
49 | + public function __construct(IDBConnection $connection, IGroupManager $groupManager) { |
|
50 | + $this->connection = $connection; |
|
51 | + $this->groupManager = $groupManager; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Returns the step's name |
|
56 | + * |
|
57 | + * @return string |
|
58 | + */ |
|
59 | + public function getName() { |
|
60 | + return 'Remove shares of old group memberships'; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Run repair step. |
|
65 | + * Must throw exception on error. |
|
66 | + * |
|
67 | + * @throws \Exception in case of failure |
|
68 | + * @suppress SqlInjectionChecker |
|
69 | + */ |
|
70 | + public function run(IOutput $output) { |
|
71 | + $deletedEntries = 0; |
|
72 | + |
|
73 | + $query = $this->connection->getQueryBuilder(); |
|
74 | + $query->select('s1.id')->selectAlias('s1.share_with', 'user')->selectAlias('s2.share_with', 'group') |
|
75 | + ->from('share', 's1') |
|
76 | + ->where($query->expr()->isNotNull('s1.parent')) |
|
77 | + // \OC\Share\Constant::$shareTypeGroupUserUnique === 2 |
|
78 | + ->andWhere($query->expr()->eq('s1.share_type', $query->expr()->literal(2))) |
|
79 | + ->andWhere($query->expr()->isNotNull('s2.id')) |
|
80 | + ->andWhere($query->expr()->eq('s2.share_type', $query->expr()->literal(Share::SHARE_TYPE_GROUP))) |
|
81 | + ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id')); |
|
82 | + |
|
83 | + $deleteQuery = $this->connection->getQueryBuilder(); |
|
84 | + $deleteQuery->delete('share') |
|
85 | + ->where($query->expr()->eq('id', $deleteQuery->createParameter('share'))); |
|
86 | + |
|
87 | + $result = $query->execute(); |
|
88 | + while ($row = $result->fetch()) { |
|
89 | + if (!$this->isMember($row['group'], $row['user'])) { |
|
90 | + $deletedEntries += $deleteQuery->setParameter('share', (int) $row['id']) |
|
91 | + ->execute(); |
|
92 | + } |
|
93 | + } |
|
94 | + $result->closeCursor(); |
|
95 | + |
|
96 | + if ($deletedEntries) { |
|
97 | + $output->info('Removed ' . $deletedEntries . ' shares where user is not a member of the group anymore'); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param string $gid |
|
103 | + * @param string $uid |
|
104 | + * @return bool |
|
105 | + */ |
|
106 | + protected function isMember($gid, $uid) { |
|
107 | + if (isset($this->memberships[$gid][$uid])) { |
|
108 | + return $this->memberships[$gid][$uid]; |
|
109 | + } |
|
110 | + |
|
111 | + $isMember = $this->groupManager->isInGroup($uid, $gid); |
|
112 | + if (!isset($this->memberships[$gid])) { |
|
113 | + $this->memberships[$gid] = []; |
|
114 | + } |
|
115 | + $this->memberships[$gid][$uid] = $isMember; |
|
116 | + |
|
117 | + return $isMember; |
|
118 | + } |
|
119 | 119 | } |
@@ -33,90 +33,90 @@ |
||
33 | 33 | */ |
34 | 34 | class RepairInvalidShares implements IRepairStep { |
35 | 35 | |
36 | - const CHUNK_SIZE = 200; |
|
37 | - |
|
38 | - /** @var \OCP\IConfig */ |
|
39 | - protected $config; |
|
40 | - |
|
41 | - /** @var \OCP\IDBConnection */ |
|
42 | - protected $connection; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param \OCP\IConfig $config |
|
46 | - * @param \OCP\IDBConnection $connection |
|
47 | - */ |
|
48 | - public function __construct($config, $connection) { |
|
49 | - $this->connection = $connection; |
|
50 | - $this->config = $config; |
|
51 | - } |
|
52 | - |
|
53 | - public function getName() { |
|
54 | - return 'Repair invalid shares'; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Adjust file share permissions |
|
59 | - * @suppress SqlInjectionChecker |
|
60 | - */ |
|
61 | - private function adjustFileSharePermissions(IOutput $out) { |
|
62 | - $mask = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE; |
|
63 | - $builder = $this->connection->getQueryBuilder(); |
|
64 | - |
|
65 | - $permsFunc = $builder->expr()->bitwiseAnd('permissions', $mask); |
|
66 | - $builder |
|
67 | - ->update('share') |
|
68 | - ->set('permissions', $permsFunc) |
|
69 | - ->where($builder->expr()->eq('item_type', $builder->expr()->literal('file'))) |
|
70 | - ->andWhere($builder->expr()->neq('permissions', $permsFunc)); |
|
71 | - |
|
72 | - $updatedEntries = $builder->execute(); |
|
73 | - if ($updatedEntries > 0) { |
|
74 | - $out->info('Fixed file share permissions for ' . $updatedEntries . ' shares'); |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Remove shares where the parent share does not exist anymore |
|
80 | - */ |
|
81 | - private function removeSharesNonExistingParent(IOutput $out) { |
|
82 | - $deletedEntries = 0; |
|
83 | - |
|
84 | - $query = $this->connection->getQueryBuilder(); |
|
85 | - $query->select('s1.parent') |
|
86 | - ->from('share', 's1') |
|
87 | - ->where($query->expr()->isNotNull('s1.parent')) |
|
88 | - ->andWhere($query->expr()->isNull('s2.id')) |
|
89 | - ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id')) |
|
90 | - ->groupBy('s1.parent') |
|
91 | - ->setMaxResults(self::CHUNK_SIZE); |
|
92 | - |
|
93 | - $deleteQuery = $this->connection->getQueryBuilder(); |
|
94 | - $deleteQuery->delete('share') |
|
95 | - ->where($deleteQuery->expr()->eq('parent', $deleteQuery->createParameter('parent'))); |
|
96 | - |
|
97 | - $deletedInLastChunk = self::CHUNK_SIZE; |
|
98 | - while ($deletedInLastChunk === self::CHUNK_SIZE) { |
|
99 | - $deletedInLastChunk = 0; |
|
100 | - $result = $query->execute(); |
|
101 | - while ($row = $result->fetch()) { |
|
102 | - $deletedInLastChunk++; |
|
103 | - $deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent']) |
|
104 | - ->execute(); |
|
105 | - } |
|
106 | - $result->closeCursor(); |
|
107 | - } |
|
108 | - |
|
109 | - if ($deletedEntries) { |
|
110 | - $out->info('Removed ' . $deletedEntries . ' shares where the parent did not exist'); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - public function run(IOutput $out) { |
|
115 | - $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
116 | - if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.11', '<')) { |
|
117 | - $this->adjustFileSharePermissions($out); |
|
118 | - } |
|
119 | - |
|
120 | - $this->removeSharesNonExistingParent($out); |
|
121 | - } |
|
36 | + const CHUNK_SIZE = 200; |
|
37 | + |
|
38 | + /** @var \OCP\IConfig */ |
|
39 | + protected $config; |
|
40 | + |
|
41 | + /** @var \OCP\IDBConnection */ |
|
42 | + protected $connection; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param \OCP\IConfig $config |
|
46 | + * @param \OCP\IDBConnection $connection |
|
47 | + */ |
|
48 | + public function __construct($config, $connection) { |
|
49 | + $this->connection = $connection; |
|
50 | + $this->config = $config; |
|
51 | + } |
|
52 | + |
|
53 | + public function getName() { |
|
54 | + return 'Repair invalid shares'; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Adjust file share permissions |
|
59 | + * @suppress SqlInjectionChecker |
|
60 | + */ |
|
61 | + private function adjustFileSharePermissions(IOutput $out) { |
|
62 | + $mask = \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE; |
|
63 | + $builder = $this->connection->getQueryBuilder(); |
|
64 | + |
|
65 | + $permsFunc = $builder->expr()->bitwiseAnd('permissions', $mask); |
|
66 | + $builder |
|
67 | + ->update('share') |
|
68 | + ->set('permissions', $permsFunc) |
|
69 | + ->where($builder->expr()->eq('item_type', $builder->expr()->literal('file'))) |
|
70 | + ->andWhere($builder->expr()->neq('permissions', $permsFunc)); |
|
71 | + |
|
72 | + $updatedEntries = $builder->execute(); |
|
73 | + if ($updatedEntries > 0) { |
|
74 | + $out->info('Fixed file share permissions for ' . $updatedEntries . ' shares'); |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Remove shares where the parent share does not exist anymore |
|
80 | + */ |
|
81 | + private function removeSharesNonExistingParent(IOutput $out) { |
|
82 | + $deletedEntries = 0; |
|
83 | + |
|
84 | + $query = $this->connection->getQueryBuilder(); |
|
85 | + $query->select('s1.parent') |
|
86 | + ->from('share', 's1') |
|
87 | + ->where($query->expr()->isNotNull('s1.parent')) |
|
88 | + ->andWhere($query->expr()->isNull('s2.id')) |
|
89 | + ->leftJoin('s1', 'share', 's2', $query->expr()->eq('s1.parent', 's2.id')) |
|
90 | + ->groupBy('s1.parent') |
|
91 | + ->setMaxResults(self::CHUNK_SIZE); |
|
92 | + |
|
93 | + $deleteQuery = $this->connection->getQueryBuilder(); |
|
94 | + $deleteQuery->delete('share') |
|
95 | + ->where($deleteQuery->expr()->eq('parent', $deleteQuery->createParameter('parent'))); |
|
96 | + |
|
97 | + $deletedInLastChunk = self::CHUNK_SIZE; |
|
98 | + while ($deletedInLastChunk === self::CHUNK_SIZE) { |
|
99 | + $deletedInLastChunk = 0; |
|
100 | + $result = $query->execute(); |
|
101 | + while ($row = $result->fetch()) { |
|
102 | + $deletedInLastChunk++; |
|
103 | + $deletedEntries += $deleteQuery->setParameter('parent', (int) $row['parent']) |
|
104 | + ->execute(); |
|
105 | + } |
|
106 | + $result->closeCursor(); |
|
107 | + } |
|
108 | + |
|
109 | + if ($deletedEntries) { |
|
110 | + $out->info('Removed ' . $deletedEntries . ' shares where the parent did not exist'); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + public function run(IOutput $out) { |
|
115 | + $ocVersionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
116 | + if (version_compare($ocVersionFromBeforeUpdate, '12.0.0.11', '<')) { |
|
117 | + $this->adjustFileSharePermissions($out); |
|
118 | + } |
|
119 | + |
|
120 | + $this->removeSharesNonExistingParent($out); |
|
121 | + } |
|
122 | 122 | } |
@@ -34,156 +34,156 @@ |
||
34 | 34 | |
35 | 35 | class RemoteController extends OCSController { |
36 | 36 | |
37 | - /** @var Manager */ |
|
38 | - private $externalManager; |
|
39 | - |
|
40 | - /** @var ILogger */ |
|
41 | - private $logger; |
|
42 | - |
|
43 | - /** |
|
44 | - * @NoAdminRequired |
|
45 | - * |
|
46 | - * Remote constructor. |
|
47 | - * |
|
48 | - * @param string $appName |
|
49 | - * @param IRequest $request |
|
50 | - * @param Manager $externalManager |
|
51 | - */ |
|
52 | - public function __construct($appName, |
|
53 | - IRequest $request, |
|
54 | - Manager $externalManager, |
|
55 | - ILogger $logger) { |
|
56 | - parent::__construct($appName, $request); |
|
57 | - |
|
58 | - $this->externalManager = $externalManager; |
|
59 | - $this->logger = $logger; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @NoAdminRequired |
|
64 | - * |
|
65 | - * Get list of pending remote shares |
|
66 | - * |
|
67 | - * @return DataResponse |
|
68 | - */ |
|
69 | - public function getOpenShares() { |
|
70 | - return new DataResponse($this->externalManager->getOpenShares()); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @NoAdminRequired |
|
75 | - * |
|
76 | - * Accept a remote share |
|
77 | - * |
|
78 | - * @param int $id |
|
79 | - * @return DataResponse |
|
80 | - * @throws OCSNotFoundException |
|
81 | - */ |
|
82 | - public function acceptShare($id) { |
|
83 | - if ($this->externalManager->acceptShare($id)) { |
|
84 | - return new DataResponse(); |
|
85 | - } |
|
86 | - |
|
87 | - $this->logger->error('Could not accept federated share with id: ' . $id, |
|
88 | - ['app' => 'files_sharing']); |
|
89 | - |
|
90 | - throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @NoAdminRequired |
|
95 | - * |
|
96 | - * Decline a remote share |
|
97 | - * |
|
98 | - * @param int $id |
|
99 | - * @return DataResponse |
|
100 | - * @throws OCSNotFoundException |
|
101 | - */ |
|
102 | - public function declineShare($id) { |
|
103 | - if ($this->externalManager->declineShare($id)) { |
|
104 | - return new DataResponse(); |
|
105 | - } |
|
106 | - |
|
107 | - // Make sure the user has no notification for something that does not exist anymore. |
|
108 | - $this->externalManager->processNotification($id); |
|
109 | - |
|
110 | - throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @param array $share Share with info from the share_external table |
|
115 | - * @return array enriched share info with data from the filecache |
|
116 | - */ |
|
117 | - private static function extendShareInfo($share) { |
|
118 | - $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/'); |
|
119 | - $info = $view->getFileInfo($share['mountpoint']); |
|
120 | - |
|
121 | - $share['mimetype'] = $info->getMimetype(); |
|
122 | - $share['mtime'] = $info->getMTime(); |
|
123 | - $share['permissions'] = $info->getPermissions(); |
|
124 | - $share['type'] = $info->getType(); |
|
125 | - $share['file_id'] = $info->getId(); |
|
126 | - |
|
127 | - return $share; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * @NoAdminRequired |
|
132 | - * |
|
133 | - * List accepted remote shares |
|
134 | - * |
|
135 | - * @return DataResponse |
|
136 | - */ |
|
137 | - public function getShares() { |
|
138 | - $shares = $this->externalManager->getAcceptedShares(); |
|
139 | - $shares = array_map('self::extendShareInfo', $shares); |
|
140 | - |
|
141 | - return new DataResponse($shares); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @NoAdminRequired |
|
146 | - * |
|
147 | - * Get info of a remote share |
|
148 | - * |
|
149 | - * @param int $id |
|
150 | - * @return DataResponse |
|
151 | - * @throws OCSNotFoundException |
|
152 | - */ |
|
153 | - public function getShare($id) { |
|
154 | - $shareInfo = $this->externalManager->getShare($id); |
|
155 | - |
|
156 | - if ($shareInfo === false) { |
|
157 | - throw new OCSNotFoundException('share does not exist'); |
|
158 | - } else { |
|
159 | - $shareInfo = self::extendShareInfo($shareInfo); |
|
160 | - return new DataResponse($shareInfo); |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @NoAdminRequired |
|
166 | - * |
|
167 | - * Unshare a remote share |
|
168 | - * |
|
169 | - * @param int $id |
|
170 | - * @return DataResponse |
|
171 | - * @throws OCSNotFoundException |
|
172 | - * @throws OCSForbiddenException |
|
173 | - */ |
|
174 | - public function unshare($id) { |
|
175 | - $shareInfo = $this->externalManager->getShare($id); |
|
176 | - |
|
177 | - if ($shareInfo === false) { |
|
178 | - throw new OCSNotFoundException('Share does not exist'); |
|
179 | - } |
|
180 | - |
|
181 | - $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint']; |
|
182 | - |
|
183 | - if ($this->externalManager->removeShare($mountPoint) === true) { |
|
184 | - return new DataResponse(); |
|
185 | - } else { |
|
186 | - throw new OCSForbiddenException('Could not unshare'); |
|
187 | - } |
|
188 | - } |
|
37 | + /** @var Manager */ |
|
38 | + private $externalManager; |
|
39 | + |
|
40 | + /** @var ILogger */ |
|
41 | + private $logger; |
|
42 | + |
|
43 | + /** |
|
44 | + * @NoAdminRequired |
|
45 | + * |
|
46 | + * Remote constructor. |
|
47 | + * |
|
48 | + * @param string $appName |
|
49 | + * @param IRequest $request |
|
50 | + * @param Manager $externalManager |
|
51 | + */ |
|
52 | + public function __construct($appName, |
|
53 | + IRequest $request, |
|
54 | + Manager $externalManager, |
|
55 | + ILogger $logger) { |
|
56 | + parent::__construct($appName, $request); |
|
57 | + |
|
58 | + $this->externalManager = $externalManager; |
|
59 | + $this->logger = $logger; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @NoAdminRequired |
|
64 | + * |
|
65 | + * Get list of pending remote shares |
|
66 | + * |
|
67 | + * @return DataResponse |
|
68 | + */ |
|
69 | + public function getOpenShares() { |
|
70 | + return new DataResponse($this->externalManager->getOpenShares()); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @NoAdminRequired |
|
75 | + * |
|
76 | + * Accept a remote share |
|
77 | + * |
|
78 | + * @param int $id |
|
79 | + * @return DataResponse |
|
80 | + * @throws OCSNotFoundException |
|
81 | + */ |
|
82 | + public function acceptShare($id) { |
|
83 | + if ($this->externalManager->acceptShare($id)) { |
|
84 | + return new DataResponse(); |
|
85 | + } |
|
86 | + |
|
87 | + $this->logger->error('Could not accept federated share with id: ' . $id, |
|
88 | + ['app' => 'files_sharing']); |
|
89 | + |
|
90 | + throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @NoAdminRequired |
|
95 | + * |
|
96 | + * Decline a remote share |
|
97 | + * |
|
98 | + * @param int $id |
|
99 | + * @return DataResponse |
|
100 | + * @throws OCSNotFoundException |
|
101 | + */ |
|
102 | + public function declineShare($id) { |
|
103 | + if ($this->externalManager->declineShare($id)) { |
|
104 | + return new DataResponse(); |
|
105 | + } |
|
106 | + |
|
107 | + // Make sure the user has no notification for something that does not exist anymore. |
|
108 | + $this->externalManager->processNotification($id); |
|
109 | + |
|
110 | + throw new OCSNotFoundException('wrong share ID, share doesn\'t exist.'); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @param array $share Share with info from the share_external table |
|
115 | + * @return array enriched share info with data from the filecache |
|
116 | + */ |
|
117 | + private static function extendShareInfo($share) { |
|
118 | + $view = new \OC\Files\View('/' . \OC_User::getUser() . '/files/'); |
|
119 | + $info = $view->getFileInfo($share['mountpoint']); |
|
120 | + |
|
121 | + $share['mimetype'] = $info->getMimetype(); |
|
122 | + $share['mtime'] = $info->getMTime(); |
|
123 | + $share['permissions'] = $info->getPermissions(); |
|
124 | + $share['type'] = $info->getType(); |
|
125 | + $share['file_id'] = $info->getId(); |
|
126 | + |
|
127 | + return $share; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * @NoAdminRequired |
|
132 | + * |
|
133 | + * List accepted remote shares |
|
134 | + * |
|
135 | + * @return DataResponse |
|
136 | + */ |
|
137 | + public function getShares() { |
|
138 | + $shares = $this->externalManager->getAcceptedShares(); |
|
139 | + $shares = array_map('self::extendShareInfo', $shares); |
|
140 | + |
|
141 | + return new DataResponse($shares); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @NoAdminRequired |
|
146 | + * |
|
147 | + * Get info of a remote share |
|
148 | + * |
|
149 | + * @param int $id |
|
150 | + * @return DataResponse |
|
151 | + * @throws OCSNotFoundException |
|
152 | + */ |
|
153 | + public function getShare($id) { |
|
154 | + $shareInfo = $this->externalManager->getShare($id); |
|
155 | + |
|
156 | + if ($shareInfo === false) { |
|
157 | + throw new OCSNotFoundException('share does not exist'); |
|
158 | + } else { |
|
159 | + $shareInfo = self::extendShareInfo($shareInfo); |
|
160 | + return new DataResponse($shareInfo); |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @NoAdminRequired |
|
166 | + * |
|
167 | + * Unshare a remote share |
|
168 | + * |
|
169 | + * @param int $id |
|
170 | + * @return DataResponse |
|
171 | + * @throws OCSNotFoundException |
|
172 | + * @throws OCSForbiddenException |
|
173 | + */ |
|
174 | + public function unshare($id) { |
|
175 | + $shareInfo = $this->externalManager->getShare($id); |
|
176 | + |
|
177 | + if ($shareInfo === false) { |
|
178 | + throw new OCSNotFoundException('Share does not exist'); |
|
179 | + } |
|
180 | + |
|
181 | + $mountPoint = '/' . \OC_User::getUser() . '/files' . $shareInfo['mountpoint']; |
|
182 | + |
|
183 | + if ($this->externalManager->removeShare($mountPoint) === true) { |
|
184 | + return new DataResponse(); |
|
185 | + } else { |
|
186 | + throw new OCSForbiddenException('Could not unshare'); |
|
187 | + } |
|
188 | + } |
|
189 | 189 | } |
@@ -29,39 +29,39 @@ |
||
29 | 29 | |
30 | 30 | class ConfigController extends OCSController { |
31 | 31 | |
32 | - /** @var IConfig */ |
|
33 | - private $config; |
|
32 | + /** @var IConfig */ |
|
33 | + private $config; |
|
34 | 34 | |
35 | - /** |
|
36 | - * @param string $appName |
|
37 | - * @param IRequest $request |
|
38 | - * @param IConfig $config |
|
39 | - */ |
|
40 | - public function __construct($appName, |
|
41 | - IRequest $request, |
|
42 | - IConfig $config) { |
|
43 | - parent::__construct($appName, $request); |
|
44 | - $this->config = $config; |
|
45 | - } |
|
35 | + /** |
|
36 | + * @param string $appName |
|
37 | + * @param IRequest $request |
|
38 | + * @param IConfig $config |
|
39 | + */ |
|
40 | + public function __construct($appName, |
|
41 | + IRequest $request, |
|
42 | + IConfig $config) { |
|
43 | + parent::__construct($appName, $request); |
|
44 | + $this->config = $config; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string $appid |
|
49 | - * @param string $configkey |
|
50 | - * @param string $value |
|
51 | - * @return DataResponse |
|
52 | - */ |
|
53 | - public function setAppValue($appid, $configkey, $value) { |
|
54 | - $this->config->setAppValue($appid, $configkey, $value); |
|
55 | - return new DataResponse(); |
|
56 | - } |
|
47 | + /** |
|
48 | + * @param string $appid |
|
49 | + * @param string $configkey |
|
50 | + * @param string $value |
|
51 | + * @return DataResponse |
|
52 | + */ |
|
53 | + public function setAppValue($appid, $configkey, $value) { |
|
54 | + $this->config->setAppValue($appid, $configkey, $value); |
|
55 | + return new DataResponse(); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param string $appid |
|
60 | - * @param string $configkey |
|
61 | - * @return DataResponse |
|
62 | - */ |
|
63 | - public function deleteAppValue($appid, $configkey) { |
|
64 | - $this->config->deleteAppValue($appid, $configkey); |
|
65 | - return new DataResponse(); |
|
66 | - } |
|
58 | + /** |
|
59 | + * @param string $appid |
|
60 | + * @param string $configkey |
|
61 | + * @return DataResponse |
|
62 | + */ |
|
63 | + public function deleteAppValue($appid, $configkey) { |
|
64 | + $this->config->deleteAppValue($appid, $configkey); |
|
65 | + return new DataResponse(); |
|
66 | + } |
|
67 | 67 | } |
@@ -39,208 +39,208 @@ |
||
39 | 39 | |
40 | 40 | class LockingController extends OCSController { |
41 | 41 | |
42 | - /** @var ILockingProvider */ |
|
43 | - protected $lockingProvider; |
|
44 | - |
|
45 | - /** @var FakeDBLockingProvider */ |
|
46 | - protected $fakeDBLockingProvider; |
|
47 | - |
|
48 | - /** @var IDBConnection */ |
|
49 | - protected $connection; |
|
50 | - |
|
51 | - /** @var IConfig */ |
|
52 | - protected $config; |
|
53 | - |
|
54 | - /** @var IRootFolder */ |
|
55 | - protected $rootFolder; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $appName |
|
59 | - * @param IRequest $request |
|
60 | - * @param ILockingProvider $lockingProvider |
|
61 | - * @param FakeDBLockingProvider $fakeDBLockingProvider |
|
62 | - * @param IDBConnection $connection |
|
63 | - * @param IConfig $config |
|
64 | - * @param IRootFolder $rootFolder |
|
65 | - */ |
|
66 | - public function __construct($appName, |
|
67 | - IRequest $request, |
|
68 | - ILockingProvider $lockingProvider, |
|
69 | - FakeDBLockingProvider $fakeDBLockingProvider, |
|
70 | - IDBConnection $connection, |
|
71 | - IConfig $config, |
|
72 | - IRootFolder $rootFolder) { |
|
73 | - parent::__construct($appName, $request); |
|
74 | - |
|
75 | - $this->lockingProvider = $lockingProvider; |
|
76 | - $this->fakeDBLockingProvider = $fakeDBLockingProvider; |
|
77 | - $this->connection = $connection; |
|
78 | - $this->config = $config; |
|
79 | - $this->rootFolder = $rootFolder; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * @return ILockingProvider |
|
84 | - * @throws \RuntimeException |
|
85 | - */ |
|
86 | - protected function getLockingProvider() { |
|
87 | - if ($this->lockingProvider instanceof DBLockingProvider) { |
|
88 | - return $this->fakeDBLockingProvider; |
|
89 | - } |
|
90 | - throw new \RuntimeException('Lock provisioning is only possible using the DBLockingProvider'); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @param string $user |
|
95 | - * @param string $path |
|
96 | - * @return string |
|
97 | - * @throws NotFoundException |
|
98 | - */ |
|
99 | - protected function getPath($user, $path) { |
|
100 | - $node = $this->rootFolder->getUserFolder($user)->get($path); |
|
101 | - return 'files/' . md5($node->getStorage()->getId() . '::' . trim($node->getInternalPath(), '/')); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * @return DataResponse |
|
106 | - * @throws OCSException |
|
107 | - */ |
|
108 | - public function isLockingEnabled() { |
|
109 | - try { |
|
110 | - $this->getLockingProvider(); |
|
111 | - return new DataResponse(); |
|
112 | - } catch (\RuntimeException $e) { |
|
113 | - throw new OCSException($e->getMessage(), Http::STATUS_NOT_IMPLEMENTED, $e); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @param int $type |
|
119 | - * @param string $user |
|
120 | - * @param string $path |
|
121 | - * @return DataResponse |
|
122 | - * @throws OCSException |
|
123 | - */ |
|
124 | - public function acquireLock($type, $user, $path) { |
|
125 | - try { |
|
126 | - $path = $this->getPath($user, $path); |
|
127 | - } catch (NoUserException $e) { |
|
128 | - throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
129 | - } catch (NotFoundException $e) { |
|
130 | - throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
131 | - } |
|
132 | - |
|
133 | - $lockingProvider = $this->getLockingProvider(); |
|
134 | - |
|
135 | - try { |
|
136 | - $lockingProvider->acquireLock($path, $type); |
|
137 | - $this->config->setAppValue('testing', 'locking_' . $path, $type); |
|
138 | - return new DataResponse(); |
|
139 | - } catch (LockedException $e) { |
|
140 | - throw new OCSException('', Http::STATUS_LOCKED, $e); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @param int $type |
|
146 | - * @param string $user |
|
147 | - * @param string $path |
|
148 | - * @return DataResponse |
|
149 | - * @throws OCSException |
|
150 | - */ |
|
151 | - public function changeLock($type, $user, $path) { |
|
152 | - try { |
|
153 | - $path = $this->getPath($user, $path); |
|
154 | - } catch (NoUserException $e) { |
|
155 | - throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
156 | - } catch (NotFoundException $e) { |
|
157 | - throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
158 | - } |
|
159 | - |
|
160 | - $lockingProvider = $this->getLockingProvider(); |
|
161 | - |
|
162 | - try { |
|
163 | - $lockingProvider->changeLock($path, $type); |
|
164 | - $this->config->setAppValue('testing', 'locking_' . $path, $type); |
|
165 | - return new DataResponse(); |
|
166 | - } catch (LockedException $e) { |
|
167 | - throw new OCSException('', Http::STATUS_LOCKED, $e); |
|
168 | - } |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param int $type |
|
173 | - * @param string $user |
|
174 | - * @param string $path |
|
175 | - * @return DataResponse |
|
176 | - * @throws OCSException |
|
177 | - */ |
|
178 | - public function releaseLock($type, $user, $path) { |
|
179 | - try { |
|
180 | - $path = $this->getPath($user, $path); |
|
181 | - } catch (NoUserException $e) { |
|
182 | - throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
183 | - } catch (NotFoundException $e) { |
|
184 | - throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
185 | - } |
|
186 | - |
|
187 | - $lockingProvider = $this->getLockingProvider(); |
|
188 | - |
|
189 | - try { |
|
190 | - $lockingProvider->releaseLock($path, $type); |
|
191 | - $this->config->deleteAppValue('testing', 'locking_' . $path); |
|
192 | - return new DataResponse(); |
|
193 | - } catch (LockedException $e) { |
|
194 | - throw new OCSException('', Http::STATUS_LOCKED, $e); |
|
195 | - } |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * @param int $type |
|
200 | - * @param string $user |
|
201 | - * @param string $path |
|
202 | - * @return DataResponse |
|
203 | - * @throws OCSException |
|
204 | - */ |
|
205 | - public function isLocked($type, $user, $path) { |
|
206 | - try { |
|
207 | - $path = $this->getPath($user, $path); |
|
208 | - } catch (NoUserException $e) { |
|
209 | - throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
210 | - } catch (NotFoundException $e) { |
|
211 | - throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
212 | - } |
|
213 | - |
|
214 | - $lockingProvider = $this->getLockingProvider(); |
|
215 | - |
|
216 | - if ($lockingProvider->isLocked($path, $type)) { |
|
217 | - return new DataResponse(); |
|
218 | - } |
|
219 | - |
|
220 | - throw new OCSException('', Http::STATUS_LOCKED); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * @param int $type |
|
225 | - * @return DataResponse |
|
226 | - */ |
|
227 | - public function releaseAll($type = null) { |
|
228 | - $lockingProvider = $this->getLockingProvider(); |
|
229 | - |
|
230 | - foreach ($this->config->getAppKeys('testing') as $lock) { |
|
231 | - if (strpos($lock, 'locking_') === 0) { |
|
232 | - $path = substr($lock, strlen('locking_')); |
|
233 | - |
|
234 | - if ($type === ILockingProvider::LOCK_EXCLUSIVE && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_EXCLUSIVE) { |
|
235 | - $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock)); |
|
236 | - } else if ($type === ILockingProvider::LOCK_SHARED && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_SHARED) { |
|
237 | - $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock)); |
|
238 | - } else { |
|
239 | - $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock)); |
|
240 | - } |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - return new DataResponse(); |
|
245 | - } |
|
42 | + /** @var ILockingProvider */ |
|
43 | + protected $lockingProvider; |
|
44 | + |
|
45 | + /** @var FakeDBLockingProvider */ |
|
46 | + protected $fakeDBLockingProvider; |
|
47 | + |
|
48 | + /** @var IDBConnection */ |
|
49 | + protected $connection; |
|
50 | + |
|
51 | + /** @var IConfig */ |
|
52 | + protected $config; |
|
53 | + |
|
54 | + /** @var IRootFolder */ |
|
55 | + protected $rootFolder; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $appName |
|
59 | + * @param IRequest $request |
|
60 | + * @param ILockingProvider $lockingProvider |
|
61 | + * @param FakeDBLockingProvider $fakeDBLockingProvider |
|
62 | + * @param IDBConnection $connection |
|
63 | + * @param IConfig $config |
|
64 | + * @param IRootFolder $rootFolder |
|
65 | + */ |
|
66 | + public function __construct($appName, |
|
67 | + IRequest $request, |
|
68 | + ILockingProvider $lockingProvider, |
|
69 | + FakeDBLockingProvider $fakeDBLockingProvider, |
|
70 | + IDBConnection $connection, |
|
71 | + IConfig $config, |
|
72 | + IRootFolder $rootFolder) { |
|
73 | + parent::__construct($appName, $request); |
|
74 | + |
|
75 | + $this->lockingProvider = $lockingProvider; |
|
76 | + $this->fakeDBLockingProvider = $fakeDBLockingProvider; |
|
77 | + $this->connection = $connection; |
|
78 | + $this->config = $config; |
|
79 | + $this->rootFolder = $rootFolder; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * @return ILockingProvider |
|
84 | + * @throws \RuntimeException |
|
85 | + */ |
|
86 | + protected function getLockingProvider() { |
|
87 | + if ($this->lockingProvider instanceof DBLockingProvider) { |
|
88 | + return $this->fakeDBLockingProvider; |
|
89 | + } |
|
90 | + throw new \RuntimeException('Lock provisioning is only possible using the DBLockingProvider'); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @param string $user |
|
95 | + * @param string $path |
|
96 | + * @return string |
|
97 | + * @throws NotFoundException |
|
98 | + */ |
|
99 | + protected function getPath($user, $path) { |
|
100 | + $node = $this->rootFolder->getUserFolder($user)->get($path); |
|
101 | + return 'files/' . md5($node->getStorage()->getId() . '::' . trim($node->getInternalPath(), '/')); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * @return DataResponse |
|
106 | + * @throws OCSException |
|
107 | + */ |
|
108 | + public function isLockingEnabled() { |
|
109 | + try { |
|
110 | + $this->getLockingProvider(); |
|
111 | + return new DataResponse(); |
|
112 | + } catch (\RuntimeException $e) { |
|
113 | + throw new OCSException($e->getMessage(), Http::STATUS_NOT_IMPLEMENTED, $e); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @param int $type |
|
119 | + * @param string $user |
|
120 | + * @param string $path |
|
121 | + * @return DataResponse |
|
122 | + * @throws OCSException |
|
123 | + */ |
|
124 | + public function acquireLock($type, $user, $path) { |
|
125 | + try { |
|
126 | + $path = $this->getPath($user, $path); |
|
127 | + } catch (NoUserException $e) { |
|
128 | + throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
129 | + } catch (NotFoundException $e) { |
|
130 | + throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
131 | + } |
|
132 | + |
|
133 | + $lockingProvider = $this->getLockingProvider(); |
|
134 | + |
|
135 | + try { |
|
136 | + $lockingProvider->acquireLock($path, $type); |
|
137 | + $this->config->setAppValue('testing', 'locking_' . $path, $type); |
|
138 | + return new DataResponse(); |
|
139 | + } catch (LockedException $e) { |
|
140 | + throw new OCSException('', Http::STATUS_LOCKED, $e); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @param int $type |
|
146 | + * @param string $user |
|
147 | + * @param string $path |
|
148 | + * @return DataResponse |
|
149 | + * @throws OCSException |
|
150 | + */ |
|
151 | + public function changeLock($type, $user, $path) { |
|
152 | + try { |
|
153 | + $path = $this->getPath($user, $path); |
|
154 | + } catch (NoUserException $e) { |
|
155 | + throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
156 | + } catch (NotFoundException $e) { |
|
157 | + throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
158 | + } |
|
159 | + |
|
160 | + $lockingProvider = $this->getLockingProvider(); |
|
161 | + |
|
162 | + try { |
|
163 | + $lockingProvider->changeLock($path, $type); |
|
164 | + $this->config->setAppValue('testing', 'locking_' . $path, $type); |
|
165 | + return new DataResponse(); |
|
166 | + } catch (LockedException $e) { |
|
167 | + throw new OCSException('', Http::STATUS_LOCKED, $e); |
|
168 | + } |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param int $type |
|
173 | + * @param string $user |
|
174 | + * @param string $path |
|
175 | + * @return DataResponse |
|
176 | + * @throws OCSException |
|
177 | + */ |
|
178 | + public function releaseLock($type, $user, $path) { |
|
179 | + try { |
|
180 | + $path = $this->getPath($user, $path); |
|
181 | + } catch (NoUserException $e) { |
|
182 | + throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
183 | + } catch (NotFoundException $e) { |
|
184 | + throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
185 | + } |
|
186 | + |
|
187 | + $lockingProvider = $this->getLockingProvider(); |
|
188 | + |
|
189 | + try { |
|
190 | + $lockingProvider->releaseLock($path, $type); |
|
191 | + $this->config->deleteAppValue('testing', 'locking_' . $path); |
|
192 | + return new DataResponse(); |
|
193 | + } catch (LockedException $e) { |
|
194 | + throw new OCSException('', Http::STATUS_LOCKED, $e); |
|
195 | + } |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * @param int $type |
|
200 | + * @param string $user |
|
201 | + * @param string $path |
|
202 | + * @return DataResponse |
|
203 | + * @throws OCSException |
|
204 | + */ |
|
205 | + public function isLocked($type, $user, $path) { |
|
206 | + try { |
|
207 | + $path = $this->getPath($user, $path); |
|
208 | + } catch (NoUserException $e) { |
|
209 | + throw new OCSException('User not found', Http::STATUS_NOT_FOUND, $e); |
|
210 | + } catch (NotFoundException $e) { |
|
211 | + throw new OCSException('Path not found', Http::STATUS_NOT_FOUND, $e); |
|
212 | + } |
|
213 | + |
|
214 | + $lockingProvider = $this->getLockingProvider(); |
|
215 | + |
|
216 | + if ($lockingProvider->isLocked($path, $type)) { |
|
217 | + return new DataResponse(); |
|
218 | + } |
|
219 | + |
|
220 | + throw new OCSException('', Http::STATUS_LOCKED); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * @param int $type |
|
225 | + * @return DataResponse |
|
226 | + */ |
|
227 | + public function releaseAll($type = null) { |
|
228 | + $lockingProvider = $this->getLockingProvider(); |
|
229 | + |
|
230 | + foreach ($this->config->getAppKeys('testing') as $lock) { |
|
231 | + if (strpos($lock, 'locking_') === 0) { |
|
232 | + $path = substr($lock, strlen('locking_')); |
|
233 | + |
|
234 | + if ($type === ILockingProvider::LOCK_EXCLUSIVE && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_EXCLUSIVE) { |
|
235 | + $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock)); |
|
236 | + } else if ($type === ILockingProvider::LOCK_SHARED && (int)$this->config->getAppValue('testing', $lock) === ILockingProvider::LOCK_SHARED) { |
|
237 | + $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock)); |
|
238 | + } else { |
|
239 | + $lockingProvider->releaseLock($path, $this->config->getAppValue('testing', $lock)); |
|
240 | + } |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + return new DataResponse(); |
|
245 | + } |
|
246 | 246 | } |
@@ -33,12 +33,12 @@ |
||
33 | 33 | */ |
34 | 34 | interface ICallbackResponse { |
35 | 35 | |
36 | - /** |
|
37 | - * Outputs the content that should be printed |
|
38 | - * |
|
39 | - * @param IOutput $output a small wrapper that handles output |
|
40 | - * @since 8.1.0 |
|
41 | - */ |
|
42 | - public function callback(IOutput $output); |
|
36 | + /** |
|
37 | + * Outputs the content that should be printed |
|
38 | + * |
|
39 | + * @param IOutput $output a small wrapper that handles output |
|
40 | + * @since 8.1.0 |
|
41 | + */ |
|
42 | + public function callback(IOutput $output); |
|
43 | 43 | |
44 | 44 | } |
@@ -32,45 +32,45 @@ |
||
32 | 32 | */ |
33 | 33 | class File extends \OCP\Search\Provider { |
34 | 34 | |
35 | - /** |
|
36 | - * Search for files and folders matching the given query |
|
37 | - * @param string $query |
|
38 | - * @return \OCP\Search\Result |
|
39 | - */ |
|
40 | - public function search($query) { |
|
41 | - $files = Filesystem::search($query); |
|
42 | - $results = array(); |
|
43 | - // edit results |
|
44 | - foreach ($files as $fileData) { |
|
45 | - // skip versions |
|
46 | - if (strpos($fileData['path'], '_versions') === 0) { |
|
47 | - continue; |
|
48 | - } |
|
49 | - // skip top-level folder |
|
50 | - if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { |
|
51 | - continue; |
|
52 | - } |
|
53 | - // create audio result |
|
54 | - if($fileData['mimepart'] === 'audio'){ |
|
55 | - $result = new \OC\Search\Result\Audio($fileData); |
|
56 | - } |
|
57 | - // create image result |
|
58 | - elseif($fileData['mimepart'] === 'image'){ |
|
59 | - $result = new \OC\Search\Result\Image($fileData); |
|
60 | - } |
|
61 | - // create folder result |
|
62 | - elseif($fileData['mimetype'] === 'httpd/unix-directory'){ |
|
63 | - $result = new \OC\Search\Result\Folder($fileData); |
|
64 | - } |
|
65 | - // or create file result |
|
66 | - else{ |
|
67 | - $result = new \OC\Search\Result\File($fileData); |
|
68 | - } |
|
69 | - // add to results |
|
70 | - $results[] = $result; |
|
71 | - } |
|
72 | - // return |
|
73 | - return $results; |
|
74 | - } |
|
35 | + /** |
|
36 | + * Search for files and folders matching the given query |
|
37 | + * @param string $query |
|
38 | + * @return \OCP\Search\Result |
|
39 | + */ |
|
40 | + public function search($query) { |
|
41 | + $files = Filesystem::search($query); |
|
42 | + $results = array(); |
|
43 | + // edit results |
|
44 | + foreach ($files as $fileData) { |
|
45 | + // skip versions |
|
46 | + if (strpos($fileData['path'], '_versions') === 0) { |
|
47 | + continue; |
|
48 | + } |
|
49 | + // skip top-level folder |
|
50 | + if ($fileData['name'] === 'files' && $fileData['parent'] === -1) { |
|
51 | + continue; |
|
52 | + } |
|
53 | + // create audio result |
|
54 | + if($fileData['mimepart'] === 'audio'){ |
|
55 | + $result = new \OC\Search\Result\Audio($fileData); |
|
56 | + } |
|
57 | + // create image result |
|
58 | + elseif($fileData['mimepart'] === 'image'){ |
|
59 | + $result = new \OC\Search\Result\Image($fileData); |
|
60 | + } |
|
61 | + // create folder result |
|
62 | + elseif($fileData['mimetype'] === 'httpd/unix-directory'){ |
|
63 | + $result = new \OC\Search\Result\Folder($fileData); |
|
64 | + } |
|
65 | + // or create file result |
|
66 | + else{ |
|
67 | + $result = new \OC\Search\Result\File($fileData); |
|
68 | + } |
|
69 | + // add to results |
|
70 | + $results[] = $result; |
|
71 | + } |
|
72 | + // return |
|
73 | + return $results; |
|
74 | + } |
|
75 | 75 | |
76 | 76 | } |
@@ -34,246 +34,246 @@ |
||
34 | 34 | use OCP\ITempManager; |
35 | 35 | |
36 | 36 | class TempManager implements ITempManager { |
37 | - /** @var string[] Current temporary files and folders, used for cleanup */ |
|
38 | - protected $current = []; |
|
39 | - /** @var string i.e. /tmp on linux systems */ |
|
40 | - protected $tmpBaseDir; |
|
41 | - /** @var ILogger */ |
|
42 | - protected $log; |
|
43 | - /** @var IConfig */ |
|
44 | - protected $config; |
|
37 | + /** @var string[] Current temporary files and folders, used for cleanup */ |
|
38 | + protected $current = []; |
|
39 | + /** @var string i.e. /tmp on linux systems */ |
|
40 | + protected $tmpBaseDir; |
|
41 | + /** @var ILogger */ |
|
42 | + protected $log; |
|
43 | + /** @var IConfig */ |
|
44 | + protected $config; |
|
45 | 45 | |
46 | - /** Prefix */ |
|
47 | - const TMP_PREFIX = 'oc_tmp_'; |
|
46 | + /** Prefix */ |
|
47 | + const TMP_PREFIX = 'oc_tmp_'; |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param \OCP\ILogger $logger |
|
51 | - * @param \OCP\IConfig $config |
|
52 | - */ |
|
53 | - public function __construct(ILogger $logger, IConfig $config) { |
|
54 | - $this->log = $logger; |
|
55 | - $this->config = $config; |
|
56 | - $this->tmpBaseDir = $this->getTempBaseDir(); |
|
57 | - } |
|
49 | + /** |
|
50 | + * @param \OCP\ILogger $logger |
|
51 | + * @param \OCP\IConfig $config |
|
52 | + */ |
|
53 | + public function __construct(ILogger $logger, IConfig $config) { |
|
54 | + $this->log = $logger; |
|
55 | + $this->config = $config; |
|
56 | + $this->tmpBaseDir = $this->getTempBaseDir(); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Builds the filename with suffix and removes potential dangerous characters |
|
61 | - * such as directory separators. |
|
62 | - * |
|
63 | - * @param string $absolutePath Absolute path to the file / folder |
|
64 | - * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
68 | - if($postFix !== '') { |
|
69 | - $postFix = '.' . ltrim($postFix, '.'); |
|
70 | - $postFix = str_replace(['\\', '/'], '', $postFix); |
|
71 | - $absolutePath .= '-'; |
|
72 | - } |
|
59 | + /** |
|
60 | + * Builds the filename with suffix and removes potential dangerous characters |
|
61 | + * such as directory separators. |
|
62 | + * |
|
63 | + * @param string $absolutePath Absolute path to the file / folder |
|
64 | + * @param string $postFix Postfix appended to the temporary file name, may be user controlled |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + private function buildFileNameWithSuffix($absolutePath, $postFix = '') { |
|
68 | + if($postFix !== '') { |
|
69 | + $postFix = '.' . ltrim($postFix, '.'); |
|
70 | + $postFix = str_replace(['\\', '/'], '', $postFix); |
|
71 | + $absolutePath .= '-'; |
|
72 | + } |
|
73 | 73 | |
74 | - return $absolutePath . $postFix; |
|
75 | - } |
|
74 | + return $absolutePath . $postFix; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Create a temporary file and return the path |
|
79 | - * |
|
80 | - * @param string $postFix Postfix appended to the temporary file name |
|
81 | - * @return string |
|
82 | - */ |
|
83 | - public function getTemporaryFile($postFix = '') { |
|
84 | - if (is_writable($this->tmpBaseDir)) { |
|
85 | - // To create an unique file and prevent the risk of race conditions |
|
86 | - // or duplicated temporary files by other means such as collisions |
|
87 | - // we need to create the file using `tempnam` and append a possible |
|
88 | - // postfix to it later |
|
89 | - $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
90 | - $this->current[] = $file; |
|
77 | + /** |
|
78 | + * Create a temporary file and return the path |
|
79 | + * |
|
80 | + * @param string $postFix Postfix appended to the temporary file name |
|
81 | + * @return string |
|
82 | + */ |
|
83 | + public function getTemporaryFile($postFix = '') { |
|
84 | + if (is_writable($this->tmpBaseDir)) { |
|
85 | + // To create an unique file and prevent the risk of race conditions |
|
86 | + // or duplicated temporary files by other means such as collisions |
|
87 | + // we need to create the file using `tempnam` and append a possible |
|
88 | + // postfix to it later |
|
89 | + $file = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
90 | + $this->current[] = $file; |
|
91 | 91 | |
92 | - // If a postfix got specified sanitize it and create a postfixed |
|
93 | - // temporary file |
|
94 | - if($postFix !== '') { |
|
95 | - $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
96 | - touch($fileNameWithPostfix); |
|
97 | - chmod($fileNameWithPostfix, 0600); |
|
98 | - $this->current[] = $fileNameWithPostfix; |
|
99 | - return $fileNameWithPostfix; |
|
100 | - } |
|
92 | + // If a postfix got specified sanitize it and create a postfixed |
|
93 | + // temporary file |
|
94 | + if($postFix !== '') { |
|
95 | + $fileNameWithPostfix = $this->buildFileNameWithSuffix($file, $postFix); |
|
96 | + touch($fileNameWithPostfix); |
|
97 | + chmod($fileNameWithPostfix, 0600); |
|
98 | + $this->current[] = $fileNameWithPostfix; |
|
99 | + return $fileNameWithPostfix; |
|
100 | + } |
|
101 | 101 | |
102 | - return $file; |
|
103 | - } else { |
|
104 | - $this->log->warning( |
|
105 | - 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
106 | - [ |
|
107 | - 'dir' => $this->tmpBaseDir, |
|
108 | - ] |
|
109 | - ); |
|
110 | - return false; |
|
111 | - } |
|
112 | - } |
|
102 | + return $file; |
|
103 | + } else { |
|
104 | + $this->log->warning( |
|
105 | + 'Can not create a temporary file in directory {dir}. Check it exists and has correct permissions', |
|
106 | + [ |
|
107 | + 'dir' => $this->tmpBaseDir, |
|
108 | + ] |
|
109 | + ); |
|
110 | + return false; |
|
111 | + } |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Create a temporary folder and return the path |
|
116 | - * |
|
117 | - * @param string $postFix Postfix appended to the temporary folder name |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function getTemporaryFolder($postFix = '') { |
|
121 | - if (is_writable($this->tmpBaseDir)) { |
|
122 | - // To create an unique directory and prevent the risk of race conditions |
|
123 | - // or duplicated temporary files by other means such as collisions |
|
124 | - // we need to create the file using `tempnam` and append a possible |
|
125 | - // postfix to it later |
|
126 | - $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
127 | - $this->current[] = $uniqueFileName; |
|
114 | + /** |
|
115 | + * Create a temporary folder and return the path |
|
116 | + * |
|
117 | + * @param string $postFix Postfix appended to the temporary folder name |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function getTemporaryFolder($postFix = '') { |
|
121 | + if (is_writable($this->tmpBaseDir)) { |
|
122 | + // To create an unique directory and prevent the risk of race conditions |
|
123 | + // or duplicated temporary files by other means such as collisions |
|
124 | + // we need to create the file using `tempnam` and append a possible |
|
125 | + // postfix to it later |
|
126 | + $uniqueFileName = tempnam($this->tmpBaseDir, self::TMP_PREFIX); |
|
127 | + $this->current[] = $uniqueFileName; |
|
128 | 128 | |
129 | - // Build a name without postfix |
|
130 | - $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
131 | - mkdir($path, 0700); |
|
132 | - $this->current[] = $path; |
|
129 | + // Build a name without postfix |
|
130 | + $path = $this->buildFileNameWithSuffix($uniqueFileName . '-folder', $postFix); |
|
131 | + mkdir($path, 0700); |
|
132 | + $this->current[] = $path; |
|
133 | 133 | |
134 | - return $path . '/'; |
|
135 | - } else { |
|
136 | - $this->log->warning( |
|
137 | - 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
138 | - [ |
|
139 | - 'dir' => $this->tmpBaseDir, |
|
140 | - ] |
|
141 | - ); |
|
142 | - return false; |
|
143 | - } |
|
144 | - } |
|
134 | + return $path . '/'; |
|
135 | + } else { |
|
136 | + $this->log->warning( |
|
137 | + 'Can not create a temporary folder in directory {dir}. Check it exists and has correct permissions', |
|
138 | + [ |
|
139 | + 'dir' => $this->tmpBaseDir, |
|
140 | + ] |
|
141 | + ); |
|
142 | + return false; |
|
143 | + } |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Remove the temporary files and folders generated during this request |
|
148 | - */ |
|
149 | - public function clean() { |
|
150 | - $this->cleanFiles($this->current); |
|
151 | - } |
|
146 | + /** |
|
147 | + * Remove the temporary files and folders generated during this request |
|
148 | + */ |
|
149 | + public function clean() { |
|
150 | + $this->cleanFiles($this->current); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * @param string[] $files |
|
155 | - */ |
|
156 | - protected function cleanFiles($files) { |
|
157 | - foreach ($files as $file) { |
|
158 | - if (file_exists($file)) { |
|
159 | - try { |
|
160 | - \OC_Helper::rmdirr($file); |
|
161 | - } catch (\UnexpectedValueException $ex) { |
|
162 | - $this->log->warning( |
|
163 | - "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
164 | - [ |
|
165 | - 'file' => $file, |
|
166 | - 'error' => $ex->getMessage(), |
|
167 | - ] |
|
168 | - ); |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - } |
|
153 | + /** |
|
154 | + * @param string[] $files |
|
155 | + */ |
|
156 | + protected function cleanFiles($files) { |
|
157 | + foreach ($files as $file) { |
|
158 | + if (file_exists($file)) { |
|
159 | + try { |
|
160 | + \OC_Helper::rmdirr($file); |
|
161 | + } catch (\UnexpectedValueException $ex) { |
|
162 | + $this->log->warning( |
|
163 | + "Error deleting temporary file/folder: {file} - Reason: {error}", |
|
164 | + [ |
|
165 | + 'file' => $file, |
|
166 | + 'error' => $ex->getMessage(), |
|
167 | + ] |
|
168 | + ); |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Remove old temporary files and folders that were failed to be cleaned |
|
176 | - */ |
|
177 | - public function cleanOld() { |
|
178 | - $this->cleanFiles($this->getOldFiles()); |
|
179 | - } |
|
174 | + /** |
|
175 | + * Remove old temporary files and folders that were failed to be cleaned |
|
176 | + */ |
|
177 | + public function cleanOld() { |
|
178 | + $this->cleanFiles($this->getOldFiles()); |
|
179 | + } |
|
180 | 180 | |
181 | - /** |
|
182 | - * Get all temporary files and folders generated by oc older than an hour |
|
183 | - * |
|
184 | - * @return string[] |
|
185 | - */ |
|
186 | - protected function getOldFiles() { |
|
187 | - $cutOfTime = time() - 3600; |
|
188 | - $files = []; |
|
189 | - $dh = opendir($this->tmpBaseDir); |
|
190 | - if ($dh) { |
|
191 | - while (($file = readdir($dh)) !== false) { |
|
192 | - if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
193 | - $path = $this->tmpBaseDir . '/' . $file; |
|
194 | - $mtime = filemtime($path); |
|
195 | - if ($mtime < $cutOfTime) { |
|
196 | - $files[] = $path; |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
201 | - return $files; |
|
202 | - } |
|
181 | + /** |
|
182 | + * Get all temporary files and folders generated by oc older than an hour |
|
183 | + * |
|
184 | + * @return string[] |
|
185 | + */ |
|
186 | + protected function getOldFiles() { |
|
187 | + $cutOfTime = time() - 3600; |
|
188 | + $files = []; |
|
189 | + $dh = opendir($this->tmpBaseDir); |
|
190 | + if ($dh) { |
|
191 | + while (($file = readdir($dh)) !== false) { |
|
192 | + if (substr($file, 0, 7) === self::TMP_PREFIX) { |
|
193 | + $path = $this->tmpBaseDir . '/' . $file; |
|
194 | + $mtime = filemtime($path); |
|
195 | + if ($mtime < $cutOfTime) { |
|
196 | + $files[] = $path; |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | + return $files; |
|
202 | + } |
|
203 | 203 | |
204 | - /** |
|
205 | - * Get the temporary base directory configured on the server |
|
206 | - * |
|
207 | - * @return string Path to the temporary directory or null |
|
208 | - * @throws \UnexpectedValueException |
|
209 | - */ |
|
210 | - public function getTempBaseDir() { |
|
211 | - if ($this->tmpBaseDir) { |
|
212 | - return $this->tmpBaseDir; |
|
213 | - } |
|
204 | + /** |
|
205 | + * Get the temporary base directory configured on the server |
|
206 | + * |
|
207 | + * @return string Path to the temporary directory or null |
|
208 | + * @throws \UnexpectedValueException |
|
209 | + */ |
|
210 | + public function getTempBaseDir() { |
|
211 | + if ($this->tmpBaseDir) { |
|
212 | + return $this->tmpBaseDir; |
|
213 | + } |
|
214 | 214 | |
215 | - $directories = []; |
|
216 | - if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
217 | - $directories[] = $temp; |
|
218 | - } |
|
219 | - if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) { |
|
220 | - $directories[] = $temp; |
|
221 | - } |
|
222 | - if ($temp = getenv('TMP')) { |
|
223 | - $directories[] = $temp; |
|
224 | - } |
|
225 | - if ($temp = getenv('TEMP')) { |
|
226 | - $directories[] = $temp; |
|
227 | - } |
|
228 | - if ($temp = getenv('TMPDIR')) { |
|
229 | - $directories[] = $temp; |
|
230 | - } |
|
231 | - if ($temp = sys_get_temp_dir()) { |
|
232 | - $directories[] = $temp; |
|
233 | - } |
|
215 | + $directories = []; |
|
216 | + if ($temp = $this->config->getSystemValue('tempdirectory', null)) { |
|
217 | + $directories[] = $temp; |
|
218 | + } |
|
219 | + if ($temp = \OC::$server->getIniWrapper()->get('upload_tmp_dir')) { |
|
220 | + $directories[] = $temp; |
|
221 | + } |
|
222 | + if ($temp = getenv('TMP')) { |
|
223 | + $directories[] = $temp; |
|
224 | + } |
|
225 | + if ($temp = getenv('TEMP')) { |
|
226 | + $directories[] = $temp; |
|
227 | + } |
|
228 | + if ($temp = getenv('TMPDIR')) { |
|
229 | + $directories[] = $temp; |
|
230 | + } |
|
231 | + if ($temp = sys_get_temp_dir()) { |
|
232 | + $directories[] = $temp; |
|
233 | + } |
|
234 | 234 | |
235 | - foreach ($directories as $dir) { |
|
236 | - if ($this->checkTemporaryDirectory($dir)) { |
|
237 | - return $dir; |
|
238 | - } |
|
239 | - } |
|
235 | + foreach ($directories as $dir) { |
|
236 | + if ($this->checkTemporaryDirectory($dir)) { |
|
237 | + return $dir; |
|
238 | + } |
|
239 | + } |
|
240 | 240 | |
241 | - $temp = tempnam(dirname(__FILE__), ''); |
|
242 | - if (file_exists($temp)) { |
|
243 | - unlink($temp); |
|
244 | - return dirname($temp); |
|
245 | - } |
|
246 | - throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
247 | - } |
|
241 | + $temp = tempnam(dirname(__FILE__), ''); |
|
242 | + if (file_exists($temp)) { |
|
243 | + unlink($temp); |
|
244 | + return dirname($temp); |
|
245 | + } |
|
246 | + throw new \UnexpectedValueException('Unable to detect system temporary directory'); |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * Check if a temporary directory is ready for use |
|
251 | - * |
|
252 | - * @param mixed $directory |
|
253 | - * @return bool |
|
254 | - */ |
|
255 | - private function checkTemporaryDirectory($directory) { |
|
256 | - // suppress any possible errors caused by is_writable |
|
257 | - // checks missing or invalid path or characters, wrong permissions etc |
|
258 | - try { |
|
259 | - if (is_writable($directory)) { |
|
260 | - return true; |
|
261 | - } |
|
262 | - } catch (\Exception $e) { |
|
263 | - } |
|
264 | - $this->log->warning('Temporary directory {dir} is not present or writable', |
|
265 | - ['dir' => $directory] |
|
266 | - ); |
|
267 | - return false; |
|
268 | - } |
|
249 | + /** |
|
250 | + * Check if a temporary directory is ready for use |
|
251 | + * |
|
252 | + * @param mixed $directory |
|
253 | + * @return bool |
|
254 | + */ |
|
255 | + private function checkTemporaryDirectory($directory) { |
|
256 | + // suppress any possible errors caused by is_writable |
|
257 | + // checks missing or invalid path or characters, wrong permissions etc |
|
258 | + try { |
|
259 | + if (is_writable($directory)) { |
|
260 | + return true; |
|
261 | + } |
|
262 | + } catch (\Exception $e) { |
|
263 | + } |
|
264 | + $this->log->warning('Temporary directory {dir} is not present or writable', |
|
265 | + ['dir' => $directory] |
|
266 | + ); |
|
267 | + return false; |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Override the temporary base directory |
|
272 | - * |
|
273 | - * @param string $directory |
|
274 | - */ |
|
275 | - public function overrideTempBaseDir($directory) { |
|
276 | - $this->tmpBaseDir = $directory; |
|
277 | - } |
|
270 | + /** |
|
271 | + * Override the temporary base directory |
|
272 | + * |
|
273 | + * @param string $directory |
|
274 | + */ |
|
275 | + public function overrideTempBaseDir($directory) { |
|
276 | + $this->tmpBaseDir = $directory; |
|
277 | + } |
|
278 | 278 | |
279 | 279 | } |
@@ -33,193 +33,193 @@ |
||
33 | 33 | use OCP\IMemcache; |
34 | 34 | |
35 | 35 | class Memcached extends Cache implements IMemcache { |
36 | - use CASTrait; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var \Memcached $cache |
|
40 | - */ |
|
41 | - private static $cache = null; |
|
42 | - |
|
43 | - use CADTrait; |
|
44 | - |
|
45 | - public function __construct($prefix = '') { |
|
46 | - parent::__construct($prefix); |
|
47 | - if (is_null(self::$cache)) { |
|
48 | - self::$cache = new \Memcached(); |
|
49 | - |
|
50 | - $defaultOptions = [ |
|
51 | - \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
52 | - \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
53 | - \Memcached::OPT_SEND_TIMEOUT => 50, |
|
54 | - \Memcached::OPT_RECV_TIMEOUT => 50, |
|
55 | - \Memcached::OPT_POLL_TIMEOUT => 50, |
|
56 | - |
|
57 | - // Enable compression |
|
58 | - \Memcached::OPT_COMPRESSION => true, |
|
59 | - |
|
60 | - // Turn on consistent hashing |
|
61 | - \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
62 | - |
|
63 | - // Enable Binary Protocol |
|
64 | - //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
65 | - ]; |
|
66 | - // by default enable igbinary serializer if available |
|
67 | - if (\Memcached::HAVE_IGBINARY) { |
|
68 | - $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
69 | - \Memcached::SERIALIZER_IGBINARY; |
|
70 | - } |
|
71 | - $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
72 | - if (is_array($options)) { |
|
73 | - $options = $options + $defaultOptions; |
|
74 | - self::$cache->setOptions($options); |
|
75 | - } else { |
|
76 | - throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
77 | - } |
|
78 | - |
|
79 | - $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
80 | - if (!$servers) { |
|
81 | - $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
82 | - if ($server) { |
|
83 | - $servers = [$server]; |
|
84 | - } else { |
|
85 | - $servers = [['localhost', 11211]]; |
|
86 | - } |
|
87 | - } |
|
88 | - self::$cache->addServers($servers); |
|
89 | - } |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
94 | - */ |
|
95 | - protected function getNameSpace() { |
|
96 | - return $this->prefix; |
|
97 | - } |
|
98 | - |
|
99 | - public function get($key) { |
|
100 | - $result = self::$cache->get($this->getNameSpace() . $key); |
|
101 | - if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
102 | - return null; |
|
103 | - } else { |
|
104 | - return $result; |
|
105 | - } |
|
106 | - } |
|
107 | - |
|
108 | - public function set($key, $value, $ttl = 0) { |
|
109 | - if ($ttl > 0) { |
|
110 | - $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
111 | - } else { |
|
112 | - $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
113 | - } |
|
114 | - if ($result !== true) { |
|
115 | - $this->verifyReturnCode(); |
|
116 | - } |
|
117 | - return $result; |
|
118 | - } |
|
119 | - |
|
120 | - public function hasKey($key) { |
|
121 | - self::$cache->get($this->getNameSpace() . $key); |
|
122 | - return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
123 | - } |
|
124 | - |
|
125 | - public function remove($key) { |
|
126 | - $result= self::$cache->delete($this->getNameSpace() . $key); |
|
127 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
128 | - $this->verifyReturnCode(); |
|
129 | - } |
|
130 | - return $result; |
|
131 | - } |
|
132 | - |
|
133 | - public function clear($prefix = '') { |
|
134 | - $prefix = $this->getNameSpace() . $prefix; |
|
135 | - $allKeys = self::$cache->getAllKeys(); |
|
136 | - if ($allKeys === false) { |
|
137 | - // newer Memcached doesn't like getAllKeys(), flush everything |
|
138 | - self::$cache->flush(); |
|
139 | - return true; |
|
140 | - } |
|
141 | - $keys = array(); |
|
142 | - $prefixLength = strlen($prefix); |
|
143 | - foreach ($allKeys as $key) { |
|
144 | - if (substr($key, 0, $prefixLength) === $prefix) { |
|
145 | - $keys[] = $key; |
|
146 | - } |
|
147 | - } |
|
148 | - if (method_exists(self::$cache, 'deleteMulti')) { |
|
149 | - self::$cache->deleteMulti($keys); |
|
150 | - } else { |
|
151 | - foreach ($keys as $key) { |
|
152 | - self::$cache->delete($key); |
|
153 | - } |
|
154 | - } |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Set a value in the cache if it's not already stored |
|
160 | - * |
|
161 | - * @param string $key |
|
162 | - * @param mixed $value |
|
163 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
164 | - * @return bool |
|
165 | - * @throws \Exception |
|
166 | - */ |
|
167 | - public function add($key, $value, $ttl = 0) { |
|
168 | - $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
169 | - if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
170 | - $this->verifyReturnCode(); |
|
171 | - } |
|
172 | - return $result; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Increase a stored number |
|
177 | - * |
|
178 | - * @param string $key |
|
179 | - * @param int $step |
|
180 | - * @return int | bool |
|
181 | - */ |
|
182 | - public function inc($key, $step = 1) { |
|
183 | - $this->add($key, 0); |
|
184 | - $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
185 | - |
|
186 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - return $result; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Decrease a stored number |
|
195 | - * |
|
196 | - * @param string $key |
|
197 | - * @param int $step |
|
198 | - * @return int | bool |
|
199 | - */ |
|
200 | - public function dec($key, $step = 1) { |
|
201 | - $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
202 | - |
|
203 | - if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
204 | - return false; |
|
205 | - } |
|
206 | - |
|
207 | - return $result; |
|
208 | - } |
|
209 | - |
|
210 | - static public function isAvailable() { |
|
211 | - return extension_loaded('memcached'); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * @throws \Exception |
|
216 | - */ |
|
217 | - private function verifyReturnCode() { |
|
218 | - $code = self::$cache->getResultCode(); |
|
219 | - if ($code === \Memcached::RES_SUCCESS) { |
|
220 | - return; |
|
221 | - } |
|
222 | - $message = self::$cache->getResultMessage(); |
|
223 | - throw new \Exception("Error $code interacting with memcached : $message"); |
|
224 | - } |
|
36 | + use CASTrait; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var \Memcached $cache |
|
40 | + */ |
|
41 | + private static $cache = null; |
|
42 | + |
|
43 | + use CADTrait; |
|
44 | + |
|
45 | + public function __construct($prefix = '') { |
|
46 | + parent::__construct($prefix); |
|
47 | + if (is_null(self::$cache)) { |
|
48 | + self::$cache = new \Memcached(); |
|
49 | + |
|
50 | + $defaultOptions = [ |
|
51 | + \Memcached::OPT_CONNECT_TIMEOUT => 50, |
|
52 | + \Memcached::OPT_RETRY_TIMEOUT => 50, |
|
53 | + \Memcached::OPT_SEND_TIMEOUT => 50, |
|
54 | + \Memcached::OPT_RECV_TIMEOUT => 50, |
|
55 | + \Memcached::OPT_POLL_TIMEOUT => 50, |
|
56 | + |
|
57 | + // Enable compression |
|
58 | + \Memcached::OPT_COMPRESSION => true, |
|
59 | + |
|
60 | + // Turn on consistent hashing |
|
61 | + \Memcached::OPT_LIBKETAMA_COMPATIBLE => true, |
|
62 | + |
|
63 | + // Enable Binary Protocol |
|
64 | + //\Memcached::OPT_BINARY_PROTOCOL => true, |
|
65 | + ]; |
|
66 | + // by default enable igbinary serializer if available |
|
67 | + if (\Memcached::HAVE_IGBINARY) { |
|
68 | + $defaultOptions[\Memcached::OPT_SERIALIZER] = |
|
69 | + \Memcached::SERIALIZER_IGBINARY; |
|
70 | + } |
|
71 | + $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []); |
|
72 | + if (is_array($options)) { |
|
73 | + $options = $options + $defaultOptions; |
|
74 | + self::$cache->setOptions($options); |
|
75 | + } else { |
|
76 | + throw new HintException("Expected 'memcached_options' config to be an array, got $options"); |
|
77 | + } |
|
78 | + |
|
79 | + $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers'); |
|
80 | + if (!$servers) { |
|
81 | + $server = \OC::$server->getSystemConfig()->getValue('memcached_server'); |
|
82 | + if ($server) { |
|
83 | + $servers = [$server]; |
|
84 | + } else { |
|
85 | + $servers = [['localhost', 11211]]; |
|
86 | + } |
|
87 | + } |
|
88 | + self::$cache->addServers($servers); |
|
89 | + } |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users |
|
94 | + */ |
|
95 | + protected function getNameSpace() { |
|
96 | + return $this->prefix; |
|
97 | + } |
|
98 | + |
|
99 | + public function get($key) { |
|
100 | + $result = self::$cache->get($this->getNameSpace() . $key); |
|
101 | + if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { |
|
102 | + return null; |
|
103 | + } else { |
|
104 | + return $result; |
|
105 | + } |
|
106 | + } |
|
107 | + |
|
108 | + public function set($key, $value, $ttl = 0) { |
|
109 | + if ($ttl > 0) { |
|
110 | + $result = self::$cache->set($this->getNameSpace() . $key, $value, $ttl); |
|
111 | + } else { |
|
112 | + $result = self::$cache->set($this->getNameSpace() . $key, $value); |
|
113 | + } |
|
114 | + if ($result !== true) { |
|
115 | + $this->verifyReturnCode(); |
|
116 | + } |
|
117 | + return $result; |
|
118 | + } |
|
119 | + |
|
120 | + public function hasKey($key) { |
|
121 | + self::$cache->get($this->getNameSpace() . $key); |
|
122 | + return self::$cache->getResultCode() === \Memcached::RES_SUCCESS; |
|
123 | + } |
|
124 | + |
|
125 | + public function remove($key) { |
|
126 | + $result= self::$cache->delete($this->getNameSpace() . $key); |
|
127 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) { |
|
128 | + $this->verifyReturnCode(); |
|
129 | + } |
|
130 | + return $result; |
|
131 | + } |
|
132 | + |
|
133 | + public function clear($prefix = '') { |
|
134 | + $prefix = $this->getNameSpace() . $prefix; |
|
135 | + $allKeys = self::$cache->getAllKeys(); |
|
136 | + if ($allKeys === false) { |
|
137 | + // newer Memcached doesn't like getAllKeys(), flush everything |
|
138 | + self::$cache->flush(); |
|
139 | + return true; |
|
140 | + } |
|
141 | + $keys = array(); |
|
142 | + $prefixLength = strlen($prefix); |
|
143 | + foreach ($allKeys as $key) { |
|
144 | + if (substr($key, 0, $prefixLength) === $prefix) { |
|
145 | + $keys[] = $key; |
|
146 | + } |
|
147 | + } |
|
148 | + if (method_exists(self::$cache, 'deleteMulti')) { |
|
149 | + self::$cache->deleteMulti($keys); |
|
150 | + } else { |
|
151 | + foreach ($keys as $key) { |
|
152 | + self::$cache->delete($key); |
|
153 | + } |
|
154 | + } |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Set a value in the cache if it's not already stored |
|
160 | + * |
|
161 | + * @param string $key |
|
162 | + * @param mixed $value |
|
163 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
164 | + * @return bool |
|
165 | + * @throws \Exception |
|
166 | + */ |
|
167 | + public function add($key, $value, $ttl = 0) { |
|
168 | + $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl); |
|
169 | + if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) { |
|
170 | + $this->verifyReturnCode(); |
|
171 | + } |
|
172 | + return $result; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Increase a stored number |
|
177 | + * |
|
178 | + * @param string $key |
|
179 | + * @param int $step |
|
180 | + * @return int | bool |
|
181 | + */ |
|
182 | + public function inc($key, $step = 1) { |
|
183 | + $this->add($key, 0); |
|
184 | + $result = self::$cache->increment($this->getPrefix() . $key, $step); |
|
185 | + |
|
186 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + return $result; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Decrease a stored number |
|
195 | + * |
|
196 | + * @param string $key |
|
197 | + * @param int $step |
|
198 | + * @return int | bool |
|
199 | + */ |
|
200 | + public function dec($key, $step = 1) { |
|
201 | + $result = self::$cache->decrement($this->getPrefix() . $key, $step); |
|
202 | + |
|
203 | + if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) { |
|
204 | + return false; |
|
205 | + } |
|
206 | + |
|
207 | + return $result; |
|
208 | + } |
|
209 | + |
|
210 | + static public function isAvailable() { |
|
211 | + return extension_loaded('memcached'); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * @throws \Exception |
|
216 | + */ |
|
217 | + private function verifyReturnCode() { |
|
218 | + $code = self::$cache->getResultCode(); |
|
219 | + if ($code === \Memcached::RES_SUCCESS) { |
|
220 | + return; |
|
221 | + } |
|
222 | + $message = self::$cache->getResultMessage(); |
|
223 | + throw new \Exception("Error $code interacting with memcached : $message"); |
|
224 | + } |
|
225 | 225 | } |