@@ -35,25 +35,25 @@ discard block |
||
35 | 35 | use Psr\Log\LoggerInterface; |
36 | 36 | |
37 | 37 | $authBackend = new Auth( |
38 | - Server::get(ISession::class), |
|
39 | - Server::get(IUserSession::class), |
|
40 | - Server::get(IRequest::class), |
|
41 | - Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
42 | - Server::get(IThrottler::class), |
|
43 | - 'principals/' |
|
38 | + Server::get(ISession::class), |
|
39 | + Server::get(IUserSession::class), |
|
40 | + Server::get(IRequest::class), |
|
41 | + Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
42 | + Server::get(IThrottler::class), |
|
43 | + 'principals/' |
|
44 | 44 | ); |
45 | 45 | $principalBackend = new Principal( |
46 | - Server::get(IUserManager::class), |
|
47 | - Server::get(IGroupManager::class), |
|
48 | - Server::get(IAccountManager::class), |
|
49 | - Server::get(\OCP\Share\IManager::class), |
|
50 | - Server::get(IUserSession::class), |
|
51 | - Server::get(IAppManager::class), |
|
52 | - Server::get(ProxyMapper::class), |
|
53 | - Server::get(KnownUserService::class), |
|
54 | - Server::get(IConfig::class), |
|
55 | - \OC::$server->getL10NFactory(), |
|
56 | - 'principals/' |
|
46 | + Server::get(IUserManager::class), |
|
47 | + Server::get(IGroupManager::class), |
|
48 | + Server::get(IAccountManager::class), |
|
49 | + Server::get(\OCP\Share\IManager::class), |
|
50 | + Server::get(IUserSession::class), |
|
51 | + Server::get(IAppManager::class), |
|
52 | + Server::get(ProxyMapper::class), |
|
53 | + Server::get(KnownUserService::class), |
|
54 | + Server::get(IConfig::class), |
|
55 | + \OC::$server->getL10NFactory(), |
|
56 | + 'principals/' |
|
57 | 57 | ); |
58 | 58 | $db = Server::get(IDBConnection::class); |
59 | 59 | $userManager = Server::get(IUserManager::class); |
@@ -63,15 +63,15 @@ discard block |
||
63 | 63 | $config = Server::get(IConfig::class); |
64 | 64 | |
65 | 65 | $calDavBackend = new CalDavBackend( |
66 | - $db, |
|
67 | - $principalBackend, |
|
68 | - $userManager, |
|
69 | - $random, |
|
70 | - $logger, |
|
71 | - $dispatcher, |
|
72 | - $config, |
|
73 | - Server::get(\OCA\DAV\CalDAV\Sharing\Backend::class), |
|
74 | - true |
|
66 | + $db, |
|
67 | + $principalBackend, |
|
68 | + $userManager, |
|
69 | + $random, |
|
70 | + $logger, |
|
71 | + $dispatcher, |
|
72 | + $config, |
|
73 | + Server::get(\OCA\DAV\CalDAV\Sharing\Backend::class), |
|
74 | + true |
|
75 | 75 | ); |
76 | 76 | |
77 | 77 | $debugging = Server::get(IConfig::class)->getSystemValue('debug', false); |
@@ -85,8 +85,8 @@ discard block |
||
85 | 85 | $addressBookRoot->disableListing = !$debugging; // Disable listing |
86 | 86 | |
87 | 87 | $nodes = [ |
88 | - $principalCollection, |
|
89 | - $addressBookRoot, |
|
88 | + $principalCollection, |
|
89 | + $addressBookRoot, |
|
90 | 90 | ]; |
91 | 91 | |
92 | 92 | // Fire up server |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | |
103 | 103 | $server->addPlugin(new LegacyDAVACL()); |
104 | 104 | if ($debugging) { |
105 | - $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
105 | + $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | $server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | $server->addPlugin(new \OCA\DAV\CalDAV\Schedule\Plugin(Server::get(IConfig::class), Server::get(LoggerInterface::class), Server::get(DefaultCalendarValidator::class))); |
111 | 111 | |
112 | 112 | if ($sendInvitations) { |
113 | - $server->addPlugin(Server::get(IMipPlugin::class)); |
|
113 | + $server->addPlugin(Server::get(IMipPlugin::class)); |
|
114 | 114 | } |
115 | 115 | $server->addPlugin(new ExceptionLoggerPlugin('caldav', $logger)); |
116 | 116 | $server->addPlugin(Server::get(RateLimitingPlugin::class)); |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | $user = $this->userSession->getUser()->getUID(); |
187 | 187 | $this->currentUser = $user; |
188 | 188 | $this->session->close(); |
189 | - return [true, $this->principalPrefix . $user]; |
|
189 | + return [true, $this->principalPrefix.$user]; |
|
190 | 190 | } |
191 | 191 | } |
192 | 192 | |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | $data[1] = substr_replace($data[1], $user, $startPos); |
198 | 198 | } elseif (in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With') ?? ''))) { |
199 | 199 | // For ajax requests use dummy auth name to prevent browser popup in case of invalid creditials |
200 | - $response->addHeader('WWW-Authenticate', 'DummyBasic realm="' . $this->realm . '"'); |
|
200 | + $response->addHeader('WWW-Authenticate', 'DummyBasic realm="'.$this->realm.'"'); |
|
201 | 201 | $response->setStatus(Http::STATUS_UNAUTHORIZED); |
202 | 202 | throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
203 | 203 | } |
@@ -28,179 +28,179 @@ |
||
28 | 28 | use Sabre\HTTP\ResponseInterface; |
29 | 29 | |
30 | 30 | class Auth extends AbstractBasic { |
31 | - public const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND'; |
|
32 | - private ?string $currentUser = null; |
|
33 | - |
|
34 | - public function __construct( |
|
35 | - private ISession $session, |
|
36 | - private Session $userSession, |
|
37 | - private IRequest $request, |
|
38 | - private Manager $twoFactorManager, |
|
39 | - private IThrottler $throttler, |
|
40 | - string $principalPrefix = 'principals/users/', |
|
41 | - ) { |
|
42 | - $this->principalPrefix = $principalPrefix; |
|
43 | - |
|
44 | - // setup realm |
|
45 | - $defaults = new Defaults(); |
|
46 | - $this->realm = $defaults->getName() ?: 'Nextcloud'; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Whether the user has initially authenticated via DAV |
|
51 | - * |
|
52 | - * This is required for WebDAV clients that resent the cookies even when the |
|
53 | - * account was changed. |
|
54 | - * |
|
55 | - * @see https://github.com/owncloud/core/issues/13245 |
|
56 | - */ |
|
57 | - public function isDavAuthenticated(string $username): bool { |
|
58 | - return !is_null($this->session->get(self::DAV_AUTHENTICATED)) |
|
59 | - && $this->session->get(self::DAV_AUTHENTICATED) === $username; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Validates a username and password |
|
64 | - * |
|
65 | - * This method should return true or false depending on if login |
|
66 | - * succeeded. |
|
67 | - * |
|
68 | - * @param string $username |
|
69 | - * @param string $password |
|
70 | - * @return bool |
|
71 | - * @throws PasswordLoginForbidden |
|
72 | - */ |
|
73 | - protected function validateUserPass($username, $password) { |
|
74 | - if ($this->userSession->isLoggedIn() |
|
75 | - && $this->isDavAuthenticated($this->userSession->getUser()->getUID()) |
|
76 | - ) { |
|
77 | - $this->session->close(); |
|
78 | - return true; |
|
79 | - } else { |
|
80 | - try { |
|
81 | - if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) { |
|
82 | - $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
83 | - $this->session->close(); |
|
84 | - return true; |
|
85 | - } else { |
|
86 | - $this->session->close(); |
|
87 | - return false; |
|
88 | - } |
|
89 | - } catch (PasswordLoginForbiddenException $ex) { |
|
90 | - $this->session->close(); |
|
91 | - throw new PasswordLoginForbidden(); |
|
92 | - } catch (MaxDelayReached $ex) { |
|
93 | - $this->session->close(); |
|
94 | - throw new TooManyRequests(); |
|
95 | - } |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * @return array{bool, string} |
|
101 | - * @throws NotAuthenticated |
|
102 | - * @throws ServiceUnavailable |
|
103 | - */ |
|
104 | - public function check(RequestInterface $request, ResponseInterface $response) { |
|
105 | - try { |
|
106 | - return $this->auth($request, $response); |
|
107 | - } catch (NotAuthenticated $e) { |
|
108 | - throw $e; |
|
109 | - } catch (Exception $e) { |
|
110 | - $class = get_class($e); |
|
111 | - $msg = $e->getMessage(); |
|
112 | - Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
|
113 | - throw new ServiceUnavailable("$class: $msg"); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Checks whether a CSRF check is required on the request |
|
119 | - */ |
|
120 | - private function requiresCSRFCheck(): bool { |
|
121 | - |
|
122 | - $methodsWithoutCsrf = ['GET', 'HEAD', 'OPTIONS']; |
|
123 | - if (in_array($this->request->getMethod(), $methodsWithoutCsrf)) { |
|
124 | - return false; |
|
125 | - } |
|
126 | - |
|
127 | - // Official Nextcloud clients require no checks |
|
128 | - if ($this->request->isUserAgent([ |
|
129 | - IRequest::USER_AGENT_CLIENT_DESKTOP, |
|
130 | - IRequest::USER_AGENT_CLIENT_ANDROID, |
|
131 | - IRequest::USER_AGENT_CLIENT_IOS, |
|
132 | - ])) { |
|
133 | - return false; |
|
134 | - } |
|
135 | - |
|
136 | - // If not logged-in no check is required |
|
137 | - if (!$this->userSession->isLoggedIn()) { |
|
138 | - return false; |
|
139 | - } |
|
140 | - |
|
141 | - // POST always requires a check |
|
142 | - if ($this->request->getMethod() === 'POST') { |
|
143 | - return true; |
|
144 | - } |
|
145 | - |
|
146 | - // If logged-in AND DAV authenticated no check is required |
|
147 | - if ($this->userSession->isLoggedIn() |
|
148 | - && $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
|
149 | - return false; |
|
150 | - } |
|
151 | - |
|
152 | - return true; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * @return array{bool, string} |
|
157 | - * @throws NotAuthenticated |
|
158 | - */ |
|
159 | - private function auth(RequestInterface $request, ResponseInterface $response): array { |
|
160 | - $forcedLogout = false; |
|
161 | - |
|
162 | - if (!$this->request->passesCSRFCheck() |
|
163 | - && $this->requiresCSRFCheck()) { |
|
164 | - // In case of a fail with POST we need to recheck the credentials |
|
165 | - if ($this->request->getMethod() === 'POST') { |
|
166 | - $forcedLogout = true; |
|
167 | - } else { |
|
168 | - $response->setStatus(Http::STATUS_UNAUTHORIZED); |
|
169 | - throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.'); |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - if ($forcedLogout) { |
|
174 | - $this->userSession->logout(); |
|
175 | - } else { |
|
176 | - if ($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
177 | - throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
|
178 | - } |
|
179 | - if ( |
|
180 | - //Fix for broken webdav clients |
|
181 | - ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) |
|
182 | - //Well behaved clients that only send the cookie are allowed |
|
183 | - || ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && empty($request->getHeader('Authorization'))) |
|
184 | - || \OC_User::handleApacheAuth() |
|
185 | - ) { |
|
186 | - $user = $this->userSession->getUser()->getUID(); |
|
187 | - $this->currentUser = $user; |
|
188 | - $this->session->close(); |
|
189 | - return [true, $this->principalPrefix . $user]; |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - $data = parent::check($request, $response); |
|
194 | - if ($data[0] === true) { |
|
195 | - $startPos = strrpos($data[1], '/') + 1; |
|
196 | - $user = $this->userSession->getUser()->getUID(); |
|
197 | - $data[1] = substr_replace($data[1], $user, $startPos); |
|
198 | - } elseif (in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With') ?? ''))) { |
|
199 | - // For ajax requests use dummy auth name to prevent browser popup in case of invalid creditials |
|
200 | - $response->addHeader('WWW-Authenticate', 'DummyBasic realm="' . $this->realm . '"'); |
|
201 | - $response->setStatus(Http::STATUS_UNAUTHORIZED); |
|
202 | - throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
|
203 | - } |
|
204 | - return $data; |
|
205 | - } |
|
31 | + public const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND'; |
|
32 | + private ?string $currentUser = null; |
|
33 | + |
|
34 | + public function __construct( |
|
35 | + private ISession $session, |
|
36 | + private Session $userSession, |
|
37 | + private IRequest $request, |
|
38 | + private Manager $twoFactorManager, |
|
39 | + private IThrottler $throttler, |
|
40 | + string $principalPrefix = 'principals/users/', |
|
41 | + ) { |
|
42 | + $this->principalPrefix = $principalPrefix; |
|
43 | + |
|
44 | + // setup realm |
|
45 | + $defaults = new Defaults(); |
|
46 | + $this->realm = $defaults->getName() ?: 'Nextcloud'; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Whether the user has initially authenticated via DAV |
|
51 | + * |
|
52 | + * This is required for WebDAV clients that resent the cookies even when the |
|
53 | + * account was changed. |
|
54 | + * |
|
55 | + * @see https://github.com/owncloud/core/issues/13245 |
|
56 | + */ |
|
57 | + public function isDavAuthenticated(string $username): bool { |
|
58 | + return !is_null($this->session->get(self::DAV_AUTHENTICATED)) |
|
59 | + && $this->session->get(self::DAV_AUTHENTICATED) === $username; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Validates a username and password |
|
64 | + * |
|
65 | + * This method should return true or false depending on if login |
|
66 | + * succeeded. |
|
67 | + * |
|
68 | + * @param string $username |
|
69 | + * @param string $password |
|
70 | + * @return bool |
|
71 | + * @throws PasswordLoginForbidden |
|
72 | + */ |
|
73 | + protected function validateUserPass($username, $password) { |
|
74 | + if ($this->userSession->isLoggedIn() |
|
75 | + && $this->isDavAuthenticated($this->userSession->getUser()->getUID()) |
|
76 | + ) { |
|
77 | + $this->session->close(); |
|
78 | + return true; |
|
79 | + } else { |
|
80 | + try { |
|
81 | + if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) { |
|
82 | + $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID()); |
|
83 | + $this->session->close(); |
|
84 | + return true; |
|
85 | + } else { |
|
86 | + $this->session->close(); |
|
87 | + return false; |
|
88 | + } |
|
89 | + } catch (PasswordLoginForbiddenException $ex) { |
|
90 | + $this->session->close(); |
|
91 | + throw new PasswordLoginForbidden(); |
|
92 | + } catch (MaxDelayReached $ex) { |
|
93 | + $this->session->close(); |
|
94 | + throw new TooManyRequests(); |
|
95 | + } |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * @return array{bool, string} |
|
101 | + * @throws NotAuthenticated |
|
102 | + * @throws ServiceUnavailable |
|
103 | + */ |
|
104 | + public function check(RequestInterface $request, ResponseInterface $response) { |
|
105 | + try { |
|
106 | + return $this->auth($request, $response); |
|
107 | + } catch (NotAuthenticated $e) { |
|
108 | + throw $e; |
|
109 | + } catch (Exception $e) { |
|
110 | + $class = get_class($e); |
|
111 | + $msg = $e->getMessage(); |
|
112 | + Server::get(LoggerInterface::class)->error($e->getMessage(), ['exception' => $e]); |
|
113 | + throw new ServiceUnavailable("$class: $msg"); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Checks whether a CSRF check is required on the request |
|
119 | + */ |
|
120 | + private function requiresCSRFCheck(): bool { |
|
121 | + |
|
122 | + $methodsWithoutCsrf = ['GET', 'HEAD', 'OPTIONS']; |
|
123 | + if (in_array($this->request->getMethod(), $methodsWithoutCsrf)) { |
|
124 | + return false; |
|
125 | + } |
|
126 | + |
|
127 | + // Official Nextcloud clients require no checks |
|
128 | + if ($this->request->isUserAgent([ |
|
129 | + IRequest::USER_AGENT_CLIENT_DESKTOP, |
|
130 | + IRequest::USER_AGENT_CLIENT_ANDROID, |
|
131 | + IRequest::USER_AGENT_CLIENT_IOS, |
|
132 | + ])) { |
|
133 | + return false; |
|
134 | + } |
|
135 | + |
|
136 | + // If not logged-in no check is required |
|
137 | + if (!$this->userSession->isLoggedIn()) { |
|
138 | + return false; |
|
139 | + } |
|
140 | + |
|
141 | + // POST always requires a check |
|
142 | + if ($this->request->getMethod() === 'POST') { |
|
143 | + return true; |
|
144 | + } |
|
145 | + |
|
146 | + // If logged-in AND DAV authenticated no check is required |
|
147 | + if ($this->userSession->isLoggedIn() |
|
148 | + && $this->isDavAuthenticated($this->userSession->getUser()->getUID())) { |
|
149 | + return false; |
|
150 | + } |
|
151 | + |
|
152 | + return true; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * @return array{bool, string} |
|
157 | + * @throws NotAuthenticated |
|
158 | + */ |
|
159 | + private function auth(RequestInterface $request, ResponseInterface $response): array { |
|
160 | + $forcedLogout = false; |
|
161 | + |
|
162 | + if (!$this->request->passesCSRFCheck() |
|
163 | + && $this->requiresCSRFCheck()) { |
|
164 | + // In case of a fail with POST we need to recheck the credentials |
|
165 | + if ($this->request->getMethod() === 'POST') { |
|
166 | + $forcedLogout = true; |
|
167 | + } else { |
|
168 | + $response->setStatus(Http::STATUS_UNAUTHORIZED); |
|
169 | + throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.'); |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + if ($forcedLogout) { |
|
174 | + $this->userSession->logout(); |
|
175 | + } else { |
|
176 | + if ($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) { |
|
177 | + throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.'); |
|
178 | + } |
|
179 | + if ( |
|
180 | + //Fix for broken webdav clients |
|
181 | + ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) |
|
182 | + //Well behaved clients that only send the cookie are allowed |
|
183 | + || ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && empty($request->getHeader('Authorization'))) |
|
184 | + || \OC_User::handleApacheAuth() |
|
185 | + ) { |
|
186 | + $user = $this->userSession->getUser()->getUID(); |
|
187 | + $this->currentUser = $user; |
|
188 | + $this->session->close(); |
|
189 | + return [true, $this->principalPrefix . $user]; |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + $data = parent::check($request, $response); |
|
194 | + if ($data[0] === true) { |
|
195 | + $startPos = strrpos($data[1], '/') + 1; |
|
196 | + $user = $this->userSession->getUser()->getUID(); |
|
197 | + $data[1] = substr_replace($data[1], $user, $startPos); |
|
198 | + } elseif (in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With') ?? ''))) { |
|
199 | + // For ajax requests use dummy auth name to prevent browser popup in case of invalid creditials |
|
200 | + $response->addHeader('WWW-Authenticate', 'DummyBasic realm="' . $this->realm . '"'); |
|
201 | + $response->setStatus(Http::STATUS_UNAUTHORIZED); |
|
202 | + throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls'); |
|
203 | + } |
|
204 | + return $data; |
|
205 | + } |
|
206 | 206 | } |
@@ -16,175 +16,175 @@ |
||
16 | 16 | use Psr\Log\LoggerInterface; |
17 | 17 | |
18 | 18 | class File implements ICache { |
19 | - /** @var View */ |
|
20 | - protected $storage; |
|
19 | + /** @var View */ |
|
20 | + protected $storage; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Returns the cache storage for the logged in user |
|
24 | - * |
|
25 | - * @return \OC\Files\View cache storage |
|
26 | - * @throws \OC\ForbiddenException |
|
27 | - * @throws \OC\User\NoUserException |
|
28 | - */ |
|
29 | - protected function getStorage() { |
|
30 | - if ($this->storage !== null) { |
|
31 | - return $this->storage; |
|
32 | - } |
|
33 | - $session = Server::get(IUserSession::class); |
|
34 | - if ($session->isLoggedIn()) { |
|
35 | - $rootView = new View(); |
|
36 | - $userId = $session->getUser()->getUID(); |
|
37 | - Filesystem::initMountPoints($userId); |
|
38 | - if (!$rootView->file_exists('/' . $userId . '/cache')) { |
|
39 | - $rootView->mkdir('/' . $userId . '/cache'); |
|
40 | - } |
|
41 | - $this->storage = new View('/' . $userId . '/cache'); |
|
42 | - return $this->storage; |
|
43 | - } else { |
|
44 | - Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']); |
|
45 | - throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in'); |
|
46 | - } |
|
47 | - } |
|
22 | + /** |
|
23 | + * Returns the cache storage for the logged in user |
|
24 | + * |
|
25 | + * @return \OC\Files\View cache storage |
|
26 | + * @throws \OC\ForbiddenException |
|
27 | + * @throws \OC\User\NoUserException |
|
28 | + */ |
|
29 | + protected function getStorage() { |
|
30 | + if ($this->storage !== null) { |
|
31 | + return $this->storage; |
|
32 | + } |
|
33 | + $session = Server::get(IUserSession::class); |
|
34 | + if ($session->isLoggedIn()) { |
|
35 | + $rootView = new View(); |
|
36 | + $userId = $session->getUser()->getUID(); |
|
37 | + Filesystem::initMountPoints($userId); |
|
38 | + if (!$rootView->file_exists('/' . $userId . '/cache')) { |
|
39 | + $rootView->mkdir('/' . $userId . '/cache'); |
|
40 | + } |
|
41 | + $this->storage = new View('/' . $userId . '/cache'); |
|
42 | + return $this->storage; |
|
43 | + } else { |
|
44 | + Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']); |
|
45 | + throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in'); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * @param string $key |
|
51 | - * @return mixed|null |
|
52 | - * @throws \OC\ForbiddenException |
|
53 | - */ |
|
54 | - public function get($key) { |
|
55 | - $result = null; |
|
56 | - if ($this->hasKey($key)) { |
|
57 | - $storage = $this->getStorage(); |
|
58 | - $result = $storage->file_get_contents($key); |
|
59 | - } |
|
60 | - return $result; |
|
61 | - } |
|
49 | + /** |
|
50 | + * @param string $key |
|
51 | + * @return mixed|null |
|
52 | + * @throws \OC\ForbiddenException |
|
53 | + */ |
|
54 | + public function get($key) { |
|
55 | + $result = null; |
|
56 | + if ($this->hasKey($key)) { |
|
57 | + $storage = $this->getStorage(); |
|
58 | + $result = $storage->file_get_contents($key); |
|
59 | + } |
|
60 | + return $result; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Returns the size of the stored/cached data |
|
65 | - * |
|
66 | - * @param string $key |
|
67 | - * @return int |
|
68 | - */ |
|
69 | - public function size($key) { |
|
70 | - $result = 0; |
|
71 | - if ($this->hasKey($key)) { |
|
72 | - $storage = $this->getStorage(); |
|
73 | - $result = $storage->filesize($key); |
|
74 | - } |
|
75 | - return $result; |
|
76 | - } |
|
63 | + /** |
|
64 | + * Returns the size of the stored/cached data |
|
65 | + * |
|
66 | + * @param string $key |
|
67 | + * @return int |
|
68 | + */ |
|
69 | + public function size($key) { |
|
70 | + $result = 0; |
|
71 | + if ($this->hasKey($key)) { |
|
72 | + $storage = $this->getStorage(); |
|
73 | + $result = $storage->filesize($key); |
|
74 | + } |
|
75 | + return $result; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * @param string $key |
|
80 | - * @param mixed $value |
|
81 | - * @param int $ttl |
|
82 | - * @return bool|mixed |
|
83 | - * @throws \OC\ForbiddenException |
|
84 | - */ |
|
85 | - public function set($key, $value, $ttl = 0) { |
|
86 | - $storage = $this->getStorage(); |
|
87 | - $result = false; |
|
88 | - // unique id to avoid chunk collision, just in case |
|
89 | - $uniqueId = Server::get(ISecureRandom::class)->generate( |
|
90 | - 16, |
|
91 | - ISecureRandom::CHAR_ALPHANUMERIC |
|
92 | - ); |
|
78 | + /** |
|
79 | + * @param string $key |
|
80 | + * @param mixed $value |
|
81 | + * @param int $ttl |
|
82 | + * @return bool|mixed |
|
83 | + * @throws \OC\ForbiddenException |
|
84 | + */ |
|
85 | + public function set($key, $value, $ttl = 0) { |
|
86 | + $storage = $this->getStorage(); |
|
87 | + $result = false; |
|
88 | + // unique id to avoid chunk collision, just in case |
|
89 | + $uniqueId = Server::get(ISecureRandom::class)->generate( |
|
90 | + 16, |
|
91 | + ISecureRandom::CHAR_ALPHANUMERIC |
|
92 | + ); |
|
93 | 93 | |
94 | - // use part file to prevent hasKey() to find the key |
|
95 | - // while it is being written |
|
96 | - $keyPart = $key . '.' . $uniqueId . '.part'; |
|
97 | - if ($storage && $storage->file_put_contents($keyPart, $value)) { |
|
98 | - if ($ttl === 0) { |
|
99 | - $ttl = 86400; // 60*60*24 |
|
100 | - } |
|
101 | - $result = $storage->touch($keyPart, time() + $ttl); |
|
102 | - $result &= $storage->rename($keyPart, $key); |
|
103 | - } |
|
104 | - return $result; |
|
105 | - } |
|
94 | + // use part file to prevent hasKey() to find the key |
|
95 | + // while it is being written |
|
96 | + $keyPart = $key . '.' . $uniqueId . '.part'; |
|
97 | + if ($storage && $storage->file_put_contents($keyPart, $value)) { |
|
98 | + if ($ttl === 0) { |
|
99 | + $ttl = 86400; // 60*60*24 |
|
100 | + } |
|
101 | + $result = $storage->touch($keyPart, time() + $ttl); |
|
102 | + $result &= $storage->rename($keyPart, $key); |
|
103 | + } |
|
104 | + return $result; |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * @param string $key |
|
109 | - * @return bool |
|
110 | - * @throws \OC\ForbiddenException |
|
111 | - */ |
|
112 | - public function hasKey($key) { |
|
113 | - $storage = $this->getStorage(); |
|
114 | - if ($storage && $storage->is_file($key) && $storage->isReadable($key)) { |
|
115 | - return true; |
|
116 | - } |
|
117 | - return false; |
|
118 | - } |
|
107 | + /** |
|
108 | + * @param string $key |
|
109 | + * @return bool |
|
110 | + * @throws \OC\ForbiddenException |
|
111 | + */ |
|
112 | + public function hasKey($key) { |
|
113 | + $storage = $this->getStorage(); |
|
114 | + if ($storage && $storage->is_file($key) && $storage->isReadable($key)) { |
|
115 | + return true; |
|
116 | + } |
|
117 | + return false; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * @param string $key |
|
122 | - * @return bool|mixed |
|
123 | - * @throws \OC\ForbiddenException |
|
124 | - */ |
|
125 | - public function remove($key) { |
|
126 | - $storage = $this->getStorage(); |
|
127 | - if (!$storage) { |
|
128 | - return false; |
|
129 | - } |
|
130 | - return $storage->unlink($key); |
|
131 | - } |
|
120 | + /** |
|
121 | + * @param string $key |
|
122 | + * @return bool|mixed |
|
123 | + * @throws \OC\ForbiddenException |
|
124 | + */ |
|
125 | + public function remove($key) { |
|
126 | + $storage = $this->getStorage(); |
|
127 | + if (!$storage) { |
|
128 | + return false; |
|
129 | + } |
|
130 | + return $storage->unlink($key); |
|
131 | + } |
|
132 | 132 | |
133 | - /** |
|
134 | - * @param string $prefix |
|
135 | - * @return bool |
|
136 | - * @throws \OC\ForbiddenException |
|
137 | - */ |
|
138 | - public function clear($prefix = '') { |
|
139 | - $storage = $this->getStorage(); |
|
140 | - if ($storage && $storage->is_dir('/')) { |
|
141 | - $dh = $storage->opendir('/'); |
|
142 | - if (is_resource($dh)) { |
|
143 | - while (($file = readdir($dh)) !== false) { |
|
144 | - if ($file !== '.' && $file !== '..' && ($prefix === '' || str_starts_with($file, $prefix))) { |
|
145 | - $storage->unlink('/' . $file); |
|
146 | - } |
|
147 | - } |
|
148 | - } |
|
149 | - } |
|
150 | - return true; |
|
151 | - } |
|
133 | + /** |
|
134 | + * @param string $prefix |
|
135 | + * @return bool |
|
136 | + * @throws \OC\ForbiddenException |
|
137 | + */ |
|
138 | + public function clear($prefix = '') { |
|
139 | + $storage = $this->getStorage(); |
|
140 | + if ($storage && $storage->is_dir('/')) { |
|
141 | + $dh = $storage->opendir('/'); |
|
142 | + if (is_resource($dh)) { |
|
143 | + while (($file = readdir($dh)) !== false) { |
|
144 | + if ($file !== '.' && $file !== '..' && ($prefix === '' || str_starts_with($file, $prefix))) { |
|
145 | + $storage->unlink('/' . $file); |
|
146 | + } |
|
147 | + } |
|
148 | + } |
|
149 | + } |
|
150 | + return true; |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Runs GC |
|
155 | - * @throws \OC\ForbiddenException |
|
156 | - */ |
|
157 | - public function gc() { |
|
158 | - $storage = $this->getStorage(); |
|
159 | - if ($storage) { |
|
160 | - // extra hour safety, in case of stray part chunks that take longer to write, |
|
161 | - // because touch() is only called after the chunk was finished |
|
162 | - $now = time() - 3600; |
|
163 | - $dh = $storage->opendir('/'); |
|
164 | - if (!is_resource($dh)) { |
|
165 | - return null; |
|
166 | - } |
|
167 | - while (($file = readdir($dh)) !== false) { |
|
168 | - if ($file !== '.' && $file !== '..') { |
|
169 | - try { |
|
170 | - $mtime = $storage->filemtime('/' . $file); |
|
171 | - if ($mtime < $now) { |
|
172 | - $storage->unlink('/' . $file); |
|
173 | - } |
|
174 | - } catch (\OCP\Lock\LockedException $e) { |
|
175 | - // ignore locked chunks |
|
176 | - Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); |
|
177 | - } catch (\OCP\Files\ForbiddenException $e) { |
|
178 | - Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); |
|
179 | - } catch (\OCP\Files\LockNotAcquiredException $e) { |
|
180 | - Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); |
|
181 | - } |
|
182 | - } |
|
183 | - } |
|
184 | - } |
|
185 | - } |
|
153 | + /** |
|
154 | + * Runs GC |
|
155 | + * @throws \OC\ForbiddenException |
|
156 | + */ |
|
157 | + public function gc() { |
|
158 | + $storage = $this->getStorage(); |
|
159 | + if ($storage) { |
|
160 | + // extra hour safety, in case of stray part chunks that take longer to write, |
|
161 | + // because touch() is only called after the chunk was finished |
|
162 | + $now = time() - 3600; |
|
163 | + $dh = $storage->opendir('/'); |
|
164 | + if (!is_resource($dh)) { |
|
165 | + return null; |
|
166 | + } |
|
167 | + while (($file = readdir($dh)) !== false) { |
|
168 | + if ($file !== '.' && $file !== '..') { |
|
169 | + try { |
|
170 | + $mtime = $storage->filemtime('/' . $file); |
|
171 | + if ($mtime < $now) { |
|
172 | + $storage->unlink('/' . $file); |
|
173 | + } |
|
174 | + } catch (\OCP\Lock\LockedException $e) { |
|
175 | + // ignore locked chunks |
|
176 | + Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); |
|
177 | + } catch (\OCP\Files\ForbiddenException $e) { |
|
178 | + Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); |
|
179 | + } catch (\OCP\Files\LockNotAcquiredException $e) { |
|
180 | + Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); |
|
181 | + } |
|
182 | + } |
|
183 | + } |
|
184 | + } |
|
185 | + } |
|
186 | 186 | |
187 | - public static function isAvailable(): bool { |
|
188 | - return true; |
|
189 | - } |
|
187 | + public static function isAvailable(): bool { |
|
188 | + return true; |
|
189 | + } |
|
190 | 190 | } |
@@ -35,10 +35,10 @@ discard block |
||
35 | 35 | $rootView = new View(); |
36 | 36 | $userId = $session->getUser()->getUID(); |
37 | 37 | Filesystem::initMountPoints($userId); |
38 | - if (!$rootView->file_exists('/' . $userId . '/cache')) { |
|
39 | - $rootView->mkdir('/' . $userId . '/cache'); |
|
38 | + if (!$rootView->file_exists('/'.$userId.'/cache')) { |
|
39 | + $rootView->mkdir('/'.$userId.'/cache'); |
|
40 | 40 | } |
41 | - $this->storage = new View('/' . $userId . '/cache'); |
|
41 | + $this->storage = new View('/'.$userId.'/cache'); |
|
42 | 42 | return $this->storage; |
43 | 43 | } else { |
44 | 44 | Server::get(LoggerInterface::class)->error('Can\'t get cache storage, user not logged in', ['app' => 'core']); |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | |
94 | 94 | // use part file to prevent hasKey() to find the key |
95 | 95 | // while it is being written |
96 | - $keyPart = $key . '.' . $uniqueId . '.part'; |
|
96 | + $keyPart = $key.'.'.$uniqueId.'.part'; |
|
97 | 97 | if ($storage && $storage->file_put_contents($keyPart, $value)) { |
98 | 98 | if ($ttl === 0) { |
99 | 99 | $ttl = 86400; // 60*60*24 |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | if (is_resource($dh)) { |
143 | 143 | while (($file = readdir($dh)) !== false) { |
144 | 144 | if ($file !== '.' && $file !== '..' && ($prefix === '' || str_starts_with($file, $prefix))) { |
145 | - $storage->unlink('/' . $file); |
|
145 | + $storage->unlink('/'.$file); |
|
146 | 146 | } |
147 | 147 | } |
148 | 148 | } |
@@ -167,17 +167,17 @@ discard block |
||
167 | 167 | while (($file = readdir($dh)) !== false) { |
168 | 168 | if ($file !== '.' && $file !== '..') { |
169 | 169 | try { |
170 | - $mtime = $storage->filemtime('/' . $file); |
|
170 | + $mtime = $storage->filemtime('/'.$file); |
|
171 | 171 | if ($mtime < $now) { |
172 | - $storage->unlink('/' . $file); |
|
172 | + $storage->unlink('/'.$file); |
|
173 | 173 | } |
174 | 174 | } catch (\OCP\Lock\LockedException $e) { |
175 | 175 | // ignore locked chunks |
176 | - Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); |
|
176 | + Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "'.$file.'"', ['app' => 'core']); |
|
177 | 177 | } catch (\OCP\Files\ForbiddenException $e) { |
178 | - Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "' . $file . '"', ['app' => 'core']); |
|
178 | + Server::get(LoggerInterface::class)->debug('Could not cleanup forbidden chunk "'.$file.'"', ['app' => 'core']); |
|
179 | 179 | } catch (\OCP\Files\LockNotAcquiredException $e) { |
180 | - Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "' . $file . '"', ['app' => 'core']); |
|
180 | + Server::get(LoggerInterface::class)->debug('Could not cleanup locked chunk "'.$file.'"', ['app' => 'core']); |
|
181 | 181 | } |
182 | 182 | } |
183 | 183 | } |
@@ -17,93 +17,93 @@ |
||
17 | 17 | * @deprecated 25.0.0 use OCP\Cache\CappedMemoryCache instead |
18 | 18 | */ |
19 | 19 | class CappedMemoryCache implements ICache, \ArrayAccess { |
20 | - private $capacity; |
|
21 | - /** @var T[] */ |
|
22 | - private $cache = []; |
|
23 | - |
|
24 | - public function __construct($capacity = 512) { |
|
25 | - $this->capacity = $capacity; |
|
26 | - } |
|
27 | - |
|
28 | - public function hasKey($key): bool { |
|
29 | - return isset($this->cache[$key]); |
|
30 | - } |
|
31 | - |
|
32 | - /** |
|
33 | - * @return ?T |
|
34 | - */ |
|
35 | - public function get($key) { |
|
36 | - return $this->cache[$key] ?? null; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string $key |
|
41 | - * @param T $value |
|
42 | - * @param int $ttl |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function set($key, $value, $ttl = 0): bool { |
|
46 | - if (is_null($key)) { |
|
47 | - $this->cache[] = $value; |
|
48 | - } else { |
|
49 | - $this->cache[$key] = $value; |
|
50 | - } |
|
51 | - $this->garbageCollect(); |
|
52 | - return true; |
|
53 | - } |
|
54 | - |
|
55 | - public function remove($key) { |
|
56 | - unset($this->cache[$key]); |
|
57 | - return true; |
|
58 | - } |
|
59 | - |
|
60 | - public function clear($prefix = '') { |
|
61 | - $this->cache = []; |
|
62 | - return true; |
|
63 | - } |
|
64 | - |
|
65 | - public function offsetExists($offset): bool { |
|
66 | - return $this->hasKey($offset); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @return T |
|
71 | - */ |
|
72 | - #[\ReturnTypeWillChange] |
|
73 | - public function &offsetGet($offset) { |
|
74 | - return $this->cache[$offset]; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @param string $offset |
|
79 | - * @param T $value |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function offsetSet($offset, $value): void { |
|
83 | - $this->set($offset, $value); |
|
84 | - } |
|
85 | - |
|
86 | - public function offsetUnset($offset): void { |
|
87 | - $this->remove($offset); |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @return T[] |
|
92 | - */ |
|
93 | - public function getData() { |
|
94 | - return $this->cache; |
|
95 | - } |
|
96 | - |
|
97 | - |
|
98 | - private function garbageCollect() { |
|
99 | - while (count($this->cache) > $this->capacity) { |
|
100 | - reset($this->cache); |
|
101 | - $key = key($this->cache); |
|
102 | - $this->remove($key); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - public static function isAvailable(): bool { |
|
107 | - return true; |
|
108 | - } |
|
20 | + private $capacity; |
|
21 | + /** @var T[] */ |
|
22 | + private $cache = []; |
|
23 | + |
|
24 | + public function __construct($capacity = 512) { |
|
25 | + $this->capacity = $capacity; |
|
26 | + } |
|
27 | + |
|
28 | + public function hasKey($key): bool { |
|
29 | + return isset($this->cache[$key]); |
|
30 | + } |
|
31 | + |
|
32 | + /** |
|
33 | + * @return ?T |
|
34 | + */ |
|
35 | + public function get($key) { |
|
36 | + return $this->cache[$key] ?? null; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string $key |
|
41 | + * @param T $value |
|
42 | + * @param int $ttl |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function set($key, $value, $ttl = 0): bool { |
|
46 | + if (is_null($key)) { |
|
47 | + $this->cache[] = $value; |
|
48 | + } else { |
|
49 | + $this->cache[$key] = $value; |
|
50 | + } |
|
51 | + $this->garbageCollect(); |
|
52 | + return true; |
|
53 | + } |
|
54 | + |
|
55 | + public function remove($key) { |
|
56 | + unset($this->cache[$key]); |
|
57 | + return true; |
|
58 | + } |
|
59 | + |
|
60 | + public function clear($prefix = '') { |
|
61 | + $this->cache = []; |
|
62 | + return true; |
|
63 | + } |
|
64 | + |
|
65 | + public function offsetExists($offset): bool { |
|
66 | + return $this->hasKey($offset); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @return T |
|
71 | + */ |
|
72 | + #[\ReturnTypeWillChange] |
|
73 | + public function &offsetGet($offset) { |
|
74 | + return $this->cache[$offset]; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @param string $offset |
|
79 | + * @param T $value |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function offsetSet($offset, $value): void { |
|
83 | + $this->set($offset, $value); |
|
84 | + } |
|
85 | + |
|
86 | + public function offsetUnset($offset): void { |
|
87 | + $this->remove($offset); |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @return T[] |
|
92 | + */ |
|
93 | + public function getData() { |
|
94 | + return $this->cache; |
|
95 | + } |
|
96 | + |
|
97 | + |
|
98 | + private function garbageCollect() { |
|
99 | + while (count($this->cache) > $this->capacity) { |
|
100 | + reset($this->cache); |
|
101 | + $key = key($this->cache); |
|
102 | + $this->remove($key); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + public static function isAvailable(): bool { |
|
107 | + return true; |
|
108 | + } |
|
109 | 109 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | use Behat\Gherkin\Node\TableNode; |
7 | 7 | use PHPUnit\Framework\Assert; |
8 | 8 | |
9 | -require __DIR__ . '/../../vendor/autoload.php'; |
|
9 | +require __DIR__.'/../../vendor/autoload.php'; |
|
10 | 10 | |
11 | 11 | trait Avatar { |
12 | 12 | /** @var string * */ |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function userGetsAvatarForUserWithSize(string $user, string $userAvatar, string $size) { |
48 | 48 | $this->asAn($user); |
49 | - $this->sendingToDirectUrl('GET', '/index.php/avatar/' . $userAvatar . '/' . $size); |
|
49 | + $this->sendingToDirectUrl('GET', '/index.php/avatar/'.$userAvatar.'/'.$size); |
|
50 | 50 | $this->theHTTPStatusCodeShouldBe('200'); |
51 | 51 | |
52 | 52 | $this->getLastAvatar(); |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | */ |
61 | 61 | public function userGetsAvatarForGuest(string $user, string $guestAvatar) { |
62 | 62 | $this->asAn($user); |
63 | - $this->sendingToDirectUrl('GET', '/index.php/avatar/guest/' . $guestAvatar . '/128'); |
|
63 | + $this->sendingToDirectUrl('GET', '/index.php/avatar/guest/'.$guestAvatar.'/128'); |
|
64 | 64 | $this->theHTTPStatusCodeShouldBe('201'); |
65 | 65 | |
66 | 66 | $this->getLastAvatar(); |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | * @param string $path |
112 | 112 | */ |
113 | 113 | public function loggedInUserPostsTemporaryAvatarFromInternalPath(string $path) { |
114 | - $this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path=' . $path); |
|
114 | + $this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path='.$path); |
|
115 | 115 | $this->theHTTPStatusCodeShouldBe('200'); |
116 | 116 | } |
117 | 117 | |
@@ -133,10 +133,10 @@ discard block |
||
133 | 133 | public function loggedInUserCropsTemporaryAvatarWith(string $statusCode, TableNode $crop) { |
134 | 134 | $parameters = []; |
135 | 135 | foreach ($crop->getRowsHash() as $key => $value) { |
136 | - $parameters[] = 'crop[' . $key . ']=' . $value; |
|
136 | + $parameters[] = 'crop['.$key.']='.$value; |
|
137 | 137 | } |
138 | 138 | |
139 | - $this->sendingAToWithRequesttoken('POST', '/index.php/avatar/cropped?' . implode('&', $parameters)); |
|
139 | + $this->sendingAToWithRequesttoken('POST', '/index.php/avatar/cropped?'.implode('&', $parameters)); |
|
140 | 140 | $this->theHTTPStatusCodeShouldBe($statusCode); |
141 | 141 | } |
142 | 142 | |
@@ -187,7 +187,7 @@ discard block |
||
187 | 187 | $colorFromLastAvatar = $this->getColorFromLastAvatar(); |
188 | 188 | |
189 | 189 | Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar), |
190 | - $this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color); |
|
190 | + $this->rgbColorToHexString($colorFromLastAvatar).' does not match expected '.$color); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | private function hexStringToRgbColor($hexString) { |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | private function rgbColorToHexString($rgbColor) { |
210 | 210 | $rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']); |
211 | 211 | |
212 | - return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT); |
|
212 | + return '#'.str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT); |
|
213 | 213 | } |
214 | 214 | |
215 | 215 | private function getColorFromLastAvatar() { |
@@ -10,253 +10,253 @@ |
||
10 | 10 | require __DIR__ . '/../../vendor/autoload.php'; |
11 | 11 | |
12 | 12 | trait Avatar { |
13 | - /** @var string * */ |
|
14 | - private $lastAvatar; |
|
15 | - |
|
16 | - /** @AfterScenario **/ |
|
17 | - public function cleanupLastAvatar() { |
|
18 | - $this->lastAvatar = null; |
|
19 | - } |
|
20 | - |
|
21 | - private function getLastAvatar() { |
|
22 | - $this->lastAvatar = ''; |
|
23 | - |
|
24 | - $body = $this->response->getBody(); |
|
25 | - while (!$body->eof()) { |
|
26 | - $this->lastAvatar .= $body->read(8192); |
|
27 | - } |
|
28 | - $body->close(); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @When user :user gets avatar for user :userAvatar |
|
33 | - * |
|
34 | - * @param string $user |
|
35 | - * @param string $userAvatar |
|
36 | - */ |
|
37 | - public function userGetsAvatarForUser(string $user, string $userAvatar) { |
|
38 | - $this->userGetsAvatarForUserWithSize($user, $userAvatar, '128'); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @When user :user gets avatar for user :userAvatar with size :size |
|
43 | - * |
|
44 | - * @param string $user |
|
45 | - * @param string $userAvatar |
|
46 | - * @param string $size |
|
47 | - */ |
|
48 | - public function userGetsAvatarForUserWithSize(string $user, string $userAvatar, string $size) { |
|
49 | - $this->asAn($user); |
|
50 | - $this->sendingToDirectUrl('GET', '/index.php/avatar/' . $userAvatar . '/' . $size); |
|
51 | - $this->theHTTPStatusCodeShouldBe('200'); |
|
52 | - |
|
53 | - $this->getLastAvatar(); |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * @When user :user gets avatar for guest :guestAvatar |
|
58 | - * |
|
59 | - * @param string $user |
|
60 | - * @param string $guestAvatar |
|
61 | - */ |
|
62 | - public function userGetsAvatarForGuest(string $user, string $guestAvatar) { |
|
63 | - $this->asAn($user); |
|
64 | - $this->sendingToDirectUrl('GET', '/index.php/avatar/guest/' . $guestAvatar . '/128'); |
|
65 | - $this->theHTTPStatusCodeShouldBe('201'); |
|
66 | - |
|
67 | - $this->getLastAvatar(); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @When logged in user gets temporary avatar |
|
72 | - */ |
|
73 | - public function loggedInUserGetsTemporaryAvatar() { |
|
74 | - $this->loggedInUserGetsTemporaryAvatarWith('200'); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @When logged in user gets temporary avatar with :statusCode |
|
79 | - * |
|
80 | - * @param string $statusCode |
|
81 | - */ |
|
82 | - public function loggedInUserGetsTemporaryAvatarWith(string $statusCode) { |
|
83 | - $this->sendingAToWithRequesttoken('GET', '/index.php/avatar/tmp'); |
|
84 | - $this->theHTTPStatusCodeShouldBe($statusCode); |
|
85 | - |
|
86 | - $this->getLastAvatar(); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @When logged in user posts temporary avatar from file :source |
|
91 | - * |
|
92 | - * @param string $source |
|
93 | - */ |
|
94 | - public function loggedInUserPostsTemporaryAvatarFromFile(string $source) { |
|
95 | - $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r')); |
|
96 | - |
|
97 | - $this->sendingAToWithRequesttoken('POST', '/index.php/avatar', |
|
98 | - [ |
|
99 | - 'multipart' => [ |
|
100 | - [ |
|
101 | - 'name' => 'files[]', |
|
102 | - 'contents' => $file |
|
103 | - ] |
|
104 | - ] |
|
105 | - ]); |
|
106 | - $this->theHTTPStatusCodeShouldBe('200'); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @When logged in user posts temporary avatar from internal path :path |
|
111 | - * |
|
112 | - * @param string $path |
|
113 | - */ |
|
114 | - public function loggedInUserPostsTemporaryAvatarFromInternalPath(string $path) { |
|
115 | - $this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path=' . $path); |
|
116 | - $this->theHTTPStatusCodeShouldBe('200'); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @When logged in user crops temporary avatar |
|
121 | - * |
|
122 | - * @param TableNode $crop |
|
123 | - */ |
|
124 | - public function loggedInUserCropsTemporaryAvatar(TableNode $crop) { |
|
125 | - $this->loggedInUserCropsTemporaryAvatarWith('200', $crop); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * @When logged in user crops temporary avatar with :statusCode |
|
130 | - * |
|
131 | - * @param string $statusCode |
|
132 | - * @param TableNode $crop |
|
133 | - */ |
|
134 | - public function loggedInUserCropsTemporaryAvatarWith(string $statusCode, TableNode $crop) { |
|
135 | - $parameters = []; |
|
136 | - foreach ($crop->getRowsHash() as $key => $value) { |
|
137 | - $parameters[] = 'crop[' . $key . ']=' . $value; |
|
138 | - } |
|
139 | - |
|
140 | - $this->sendingAToWithRequesttoken('POST', '/index.php/avatar/cropped?' . implode('&', $parameters)); |
|
141 | - $this->theHTTPStatusCodeShouldBe($statusCode); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @When logged in user deletes the user avatar |
|
146 | - */ |
|
147 | - public function loggedInUserDeletesTheUserAvatar() { |
|
148 | - $this->sendingAToWithRequesttoken('DELETE', '/index.php/avatar'); |
|
149 | - $this->theHTTPStatusCodeShouldBe('200'); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @Then last avatar is a square of size :size |
|
154 | - * |
|
155 | - * @param string size |
|
156 | - */ |
|
157 | - public function lastAvatarIsASquareOfSize(string $size) { |
|
158 | - [$width, $height] = getimagesizefromstring($this->lastAvatar); |
|
159 | - |
|
160 | - Assert::assertEquals($width, $height, 'Expected avatar to be a square'); |
|
161 | - Assert::assertEquals($size, $width); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @Then last avatar is not a square |
|
166 | - */ |
|
167 | - public function lastAvatarIsNotASquare() { |
|
168 | - [$width, $height] = getimagesizefromstring($this->lastAvatar); |
|
169 | - |
|
170 | - Assert::assertNotEquals($width, $height, 'Expected avatar to not be a square'); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @Then last avatar is not a single color |
|
175 | - */ |
|
176 | - public function lastAvatarIsNotASingleColor() { |
|
177 | - Assert::assertEquals(null, $this->getColorFromLastAvatar()); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @Then last avatar is a single :color color |
|
182 | - * |
|
183 | - * @param string $color |
|
184 | - * @param string $size |
|
185 | - */ |
|
186 | - public function lastAvatarIsASingleColor(string $color) { |
|
187 | - $expectedColor = $this->hexStringToRgbColor($color); |
|
188 | - $colorFromLastAvatar = $this->getColorFromLastAvatar(); |
|
189 | - |
|
190 | - Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar), |
|
191 | - $this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color); |
|
192 | - } |
|
193 | - |
|
194 | - private function hexStringToRgbColor($hexString) { |
|
195 | - // Strip initial "#" |
|
196 | - $hexString = substr($hexString, 1); |
|
197 | - |
|
198 | - $rgbColorInt = hexdec($hexString); |
|
199 | - |
|
200 | - // RGBA hex strings are not supported; the given string is assumed to be |
|
201 | - // an RGB hex string. |
|
202 | - return [ |
|
203 | - 'red' => ($rgbColorInt >> 16) & 0xFF, |
|
204 | - 'green' => ($rgbColorInt >> 8) & 0xFF, |
|
205 | - 'blue' => $rgbColorInt & 0xFF, |
|
206 | - 'alpha' => 0 |
|
207 | - ]; |
|
208 | - } |
|
209 | - |
|
210 | - private function rgbColorToHexString($rgbColor) { |
|
211 | - $rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']); |
|
212 | - |
|
213 | - return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT); |
|
214 | - } |
|
215 | - |
|
216 | - private function getColorFromLastAvatar() { |
|
217 | - $image = imagecreatefromstring($this->lastAvatar); |
|
218 | - |
|
219 | - $firstPixelColorIndex = imagecolorat($image, 0, 0); |
|
220 | - $firstPixelColor = imagecolorsforindex($image, $firstPixelColorIndex); |
|
221 | - |
|
222 | - for ($i = 0; $i < imagesx($image); $i++) { |
|
223 | - for ($j = 0; $j < imagesx($image); $j++) { |
|
224 | - $currentPixelColorIndex = imagecolorat($image, $i, $j); |
|
225 | - $currentPixelColor = imagecolorsforindex($image, $currentPixelColorIndex); |
|
226 | - |
|
227 | - // The colors are compared with a small allowed delta, as even |
|
228 | - // on solid color images the resizing can cause some small |
|
229 | - // artifacts that slightly modify the color of certain pixels. |
|
230 | - if (!$this->isSameColor($firstPixelColor, $currentPixelColor)) { |
|
231 | - imagedestroy($image); |
|
232 | - |
|
233 | - return null; |
|
234 | - } |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - imagedestroy($image); |
|
239 | - |
|
240 | - return $firstPixelColor; |
|
241 | - } |
|
242 | - |
|
243 | - private function isSameColor(array $firstColor, array $secondColor, int $allowedDelta = 1) { |
|
244 | - if ($this->isSameColorComponent($firstColor['red'], $secondColor['red'], $allowedDelta) |
|
245 | - && $this->isSameColorComponent($firstColor['green'], $secondColor['green'], $allowedDelta) |
|
246 | - && $this->isSameColorComponent($firstColor['blue'], $secondColor['blue'], $allowedDelta) |
|
247 | - && $this->isSameColorComponent($firstColor['alpha'], $secondColor['alpha'], $allowedDelta)) { |
|
248 | - return true; |
|
249 | - } |
|
250 | - |
|
251 | - return false; |
|
252 | - } |
|
253 | - |
|
254 | - private function isSameColorComponent(int $firstColorComponent, int $secondColorComponent, int $allowedDelta) { |
|
255 | - if ($firstColorComponent >= ($secondColorComponent - $allowedDelta) |
|
256 | - && $firstColorComponent <= ($secondColorComponent + $allowedDelta)) { |
|
257 | - return true; |
|
258 | - } |
|
259 | - |
|
260 | - return false; |
|
261 | - } |
|
13 | + /** @var string * */ |
|
14 | + private $lastAvatar; |
|
15 | + |
|
16 | + /** @AfterScenario **/ |
|
17 | + public function cleanupLastAvatar() { |
|
18 | + $this->lastAvatar = null; |
|
19 | + } |
|
20 | + |
|
21 | + private function getLastAvatar() { |
|
22 | + $this->lastAvatar = ''; |
|
23 | + |
|
24 | + $body = $this->response->getBody(); |
|
25 | + while (!$body->eof()) { |
|
26 | + $this->lastAvatar .= $body->read(8192); |
|
27 | + } |
|
28 | + $body->close(); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @When user :user gets avatar for user :userAvatar |
|
33 | + * |
|
34 | + * @param string $user |
|
35 | + * @param string $userAvatar |
|
36 | + */ |
|
37 | + public function userGetsAvatarForUser(string $user, string $userAvatar) { |
|
38 | + $this->userGetsAvatarForUserWithSize($user, $userAvatar, '128'); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @When user :user gets avatar for user :userAvatar with size :size |
|
43 | + * |
|
44 | + * @param string $user |
|
45 | + * @param string $userAvatar |
|
46 | + * @param string $size |
|
47 | + */ |
|
48 | + public function userGetsAvatarForUserWithSize(string $user, string $userAvatar, string $size) { |
|
49 | + $this->asAn($user); |
|
50 | + $this->sendingToDirectUrl('GET', '/index.php/avatar/' . $userAvatar . '/' . $size); |
|
51 | + $this->theHTTPStatusCodeShouldBe('200'); |
|
52 | + |
|
53 | + $this->getLastAvatar(); |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * @When user :user gets avatar for guest :guestAvatar |
|
58 | + * |
|
59 | + * @param string $user |
|
60 | + * @param string $guestAvatar |
|
61 | + */ |
|
62 | + public function userGetsAvatarForGuest(string $user, string $guestAvatar) { |
|
63 | + $this->asAn($user); |
|
64 | + $this->sendingToDirectUrl('GET', '/index.php/avatar/guest/' . $guestAvatar . '/128'); |
|
65 | + $this->theHTTPStatusCodeShouldBe('201'); |
|
66 | + |
|
67 | + $this->getLastAvatar(); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @When logged in user gets temporary avatar |
|
72 | + */ |
|
73 | + public function loggedInUserGetsTemporaryAvatar() { |
|
74 | + $this->loggedInUserGetsTemporaryAvatarWith('200'); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @When logged in user gets temporary avatar with :statusCode |
|
79 | + * |
|
80 | + * @param string $statusCode |
|
81 | + */ |
|
82 | + public function loggedInUserGetsTemporaryAvatarWith(string $statusCode) { |
|
83 | + $this->sendingAToWithRequesttoken('GET', '/index.php/avatar/tmp'); |
|
84 | + $this->theHTTPStatusCodeShouldBe($statusCode); |
|
85 | + |
|
86 | + $this->getLastAvatar(); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @When logged in user posts temporary avatar from file :source |
|
91 | + * |
|
92 | + * @param string $source |
|
93 | + */ |
|
94 | + public function loggedInUserPostsTemporaryAvatarFromFile(string $source) { |
|
95 | + $file = \GuzzleHttp\Psr7\Utils::streamFor(fopen($source, 'r')); |
|
96 | + |
|
97 | + $this->sendingAToWithRequesttoken('POST', '/index.php/avatar', |
|
98 | + [ |
|
99 | + 'multipart' => [ |
|
100 | + [ |
|
101 | + 'name' => 'files[]', |
|
102 | + 'contents' => $file |
|
103 | + ] |
|
104 | + ] |
|
105 | + ]); |
|
106 | + $this->theHTTPStatusCodeShouldBe('200'); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @When logged in user posts temporary avatar from internal path :path |
|
111 | + * |
|
112 | + * @param string $path |
|
113 | + */ |
|
114 | + public function loggedInUserPostsTemporaryAvatarFromInternalPath(string $path) { |
|
115 | + $this->sendingAToWithRequesttoken('POST', '/index.php/avatar?path=' . $path); |
|
116 | + $this->theHTTPStatusCodeShouldBe('200'); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @When logged in user crops temporary avatar |
|
121 | + * |
|
122 | + * @param TableNode $crop |
|
123 | + */ |
|
124 | + public function loggedInUserCropsTemporaryAvatar(TableNode $crop) { |
|
125 | + $this->loggedInUserCropsTemporaryAvatarWith('200', $crop); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * @When logged in user crops temporary avatar with :statusCode |
|
130 | + * |
|
131 | + * @param string $statusCode |
|
132 | + * @param TableNode $crop |
|
133 | + */ |
|
134 | + public function loggedInUserCropsTemporaryAvatarWith(string $statusCode, TableNode $crop) { |
|
135 | + $parameters = []; |
|
136 | + foreach ($crop->getRowsHash() as $key => $value) { |
|
137 | + $parameters[] = 'crop[' . $key . ']=' . $value; |
|
138 | + } |
|
139 | + |
|
140 | + $this->sendingAToWithRequesttoken('POST', '/index.php/avatar/cropped?' . implode('&', $parameters)); |
|
141 | + $this->theHTTPStatusCodeShouldBe($statusCode); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @When logged in user deletes the user avatar |
|
146 | + */ |
|
147 | + public function loggedInUserDeletesTheUserAvatar() { |
|
148 | + $this->sendingAToWithRequesttoken('DELETE', '/index.php/avatar'); |
|
149 | + $this->theHTTPStatusCodeShouldBe('200'); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @Then last avatar is a square of size :size |
|
154 | + * |
|
155 | + * @param string size |
|
156 | + */ |
|
157 | + public function lastAvatarIsASquareOfSize(string $size) { |
|
158 | + [$width, $height] = getimagesizefromstring($this->lastAvatar); |
|
159 | + |
|
160 | + Assert::assertEquals($width, $height, 'Expected avatar to be a square'); |
|
161 | + Assert::assertEquals($size, $width); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @Then last avatar is not a square |
|
166 | + */ |
|
167 | + public function lastAvatarIsNotASquare() { |
|
168 | + [$width, $height] = getimagesizefromstring($this->lastAvatar); |
|
169 | + |
|
170 | + Assert::assertNotEquals($width, $height, 'Expected avatar to not be a square'); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @Then last avatar is not a single color |
|
175 | + */ |
|
176 | + public function lastAvatarIsNotASingleColor() { |
|
177 | + Assert::assertEquals(null, $this->getColorFromLastAvatar()); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @Then last avatar is a single :color color |
|
182 | + * |
|
183 | + * @param string $color |
|
184 | + * @param string $size |
|
185 | + */ |
|
186 | + public function lastAvatarIsASingleColor(string $color) { |
|
187 | + $expectedColor = $this->hexStringToRgbColor($color); |
|
188 | + $colorFromLastAvatar = $this->getColorFromLastAvatar(); |
|
189 | + |
|
190 | + Assert::assertTrue($this->isSameColor($expectedColor, $colorFromLastAvatar), |
|
191 | + $this->rgbColorToHexString($colorFromLastAvatar) . ' does not match expected ' . $color); |
|
192 | + } |
|
193 | + |
|
194 | + private function hexStringToRgbColor($hexString) { |
|
195 | + // Strip initial "#" |
|
196 | + $hexString = substr($hexString, 1); |
|
197 | + |
|
198 | + $rgbColorInt = hexdec($hexString); |
|
199 | + |
|
200 | + // RGBA hex strings are not supported; the given string is assumed to be |
|
201 | + // an RGB hex string. |
|
202 | + return [ |
|
203 | + 'red' => ($rgbColorInt >> 16) & 0xFF, |
|
204 | + 'green' => ($rgbColorInt >> 8) & 0xFF, |
|
205 | + 'blue' => $rgbColorInt & 0xFF, |
|
206 | + 'alpha' => 0 |
|
207 | + ]; |
|
208 | + } |
|
209 | + |
|
210 | + private function rgbColorToHexString($rgbColor) { |
|
211 | + $rgbColorInt = ($rgbColor['red'] << 16) + ($rgbColor['green'] << 8) + ($rgbColor['blue']); |
|
212 | + |
|
213 | + return '#' . str_pad(strtoupper(dechex($rgbColorInt)), 6, '0', STR_PAD_LEFT); |
|
214 | + } |
|
215 | + |
|
216 | + private function getColorFromLastAvatar() { |
|
217 | + $image = imagecreatefromstring($this->lastAvatar); |
|
218 | + |
|
219 | + $firstPixelColorIndex = imagecolorat($image, 0, 0); |
|
220 | + $firstPixelColor = imagecolorsforindex($image, $firstPixelColorIndex); |
|
221 | + |
|
222 | + for ($i = 0; $i < imagesx($image); $i++) { |
|
223 | + for ($j = 0; $j < imagesx($image); $j++) { |
|
224 | + $currentPixelColorIndex = imagecolorat($image, $i, $j); |
|
225 | + $currentPixelColor = imagecolorsforindex($image, $currentPixelColorIndex); |
|
226 | + |
|
227 | + // The colors are compared with a small allowed delta, as even |
|
228 | + // on solid color images the resizing can cause some small |
|
229 | + // artifacts that slightly modify the color of certain pixels. |
|
230 | + if (!$this->isSameColor($firstPixelColor, $currentPixelColor)) { |
|
231 | + imagedestroy($image); |
|
232 | + |
|
233 | + return null; |
|
234 | + } |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + imagedestroy($image); |
|
239 | + |
|
240 | + return $firstPixelColor; |
|
241 | + } |
|
242 | + |
|
243 | + private function isSameColor(array $firstColor, array $secondColor, int $allowedDelta = 1) { |
|
244 | + if ($this->isSameColorComponent($firstColor['red'], $secondColor['red'], $allowedDelta) |
|
245 | + && $this->isSameColorComponent($firstColor['green'], $secondColor['green'], $allowedDelta) |
|
246 | + && $this->isSameColorComponent($firstColor['blue'], $secondColor['blue'], $allowedDelta) |
|
247 | + && $this->isSameColorComponent($firstColor['alpha'], $secondColor['alpha'], $allowedDelta)) { |
|
248 | + return true; |
|
249 | + } |
|
250 | + |
|
251 | + return false; |
|
252 | + } |
|
253 | + |
|
254 | + private function isSameColorComponent(int $firstColorComponent, int $secondColorComponent, int $allowedDelta) { |
|
255 | + if ($firstColorComponent >= ($secondColorComponent - $allowedDelta) |
|
256 | + && $firstColorComponent <= ($secondColorComponent + $allowedDelta)) { |
|
257 | + return true; |
|
258 | + } |
|
259 | + |
|
260 | + return false; |
|
261 | + } |
|
262 | 262 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | |
27 | 27 | // no php execution timeout for webdav |
28 | 28 | if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) { |
29 | - @set_time_limit(0); |
|
29 | + @set_time_limit(0); |
|
30 | 30 | } |
31 | 31 | ignore_user_abort(true); |
32 | 32 | |
@@ -36,41 +36,41 @@ discard block |
||
36 | 36 | $dispatcher = Server::get(IEventDispatcher::class); |
37 | 37 | |
38 | 38 | $serverFactory = new ServerFactory( |
39 | - Server::get(IConfig::class), |
|
40 | - Server::get(LoggerInterface::class), |
|
41 | - Server::get(IDBConnection::class), |
|
42 | - Server::get(IUserSession::class), |
|
43 | - Server::get(IMountManager::class), |
|
44 | - Server::get(ITagManager::class), |
|
45 | - Server::get(IRequest::class), |
|
46 | - Server::get(IPreview::class), |
|
47 | - $dispatcher, |
|
48 | - \OC::$server->getL10N('dav') |
|
39 | + Server::get(IConfig::class), |
|
40 | + Server::get(LoggerInterface::class), |
|
41 | + Server::get(IDBConnection::class), |
|
42 | + Server::get(IUserSession::class), |
|
43 | + Server::get(IMountManager::class), |
|
44 | + Server::get(ITagManager::class), |
|
45 | + Server::get(IRequest::class), |
|
46 | + Server::get(IPreview::class), |
|
47 | + $dispatcher, |
|
48 | + \OC::$server->getL10N('dav') |
|
49 | 49 | ); |
50 | 50 | |
51 | 51 | // Backends |
52 | 52 | $authBackend = new Auth( |
53 | - Server::get(ISession::class), |
|
54 | - Server::get(IUserSession::class), |
|
55 | - Server::get(IRequest::class), |
|
56 | - Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
57 | - Server::get(IThrottler::class), |
|
58 | - 'principals/' |
|
53 | + Server::get(ISession::class), |
|
54 | + Server::get(IUserSession::class), |
|
55 | + Server::get(IRequest::class), |
|
56 | + Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
57 | + Server::get(IThrottler::class), |
|
58 | + 'principals/' |
|
59 | 59 | ); |
60 | 60 | $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend); |
61 | 61 | $bearerAuthPlugin = new BearerAuth( |
62 | - Server::get(IUserSession::class), |
|
63 | - Server::get(ISession::class), |
|
64 | - Server::get(IRequest::class), |
|
65 | - Server::get(IConfig::class), |
|
62 | + Server::get(IUserSession::class), |
|
63 | + Server::get(ISession::class), |
|
64 | + Server::get(IRequest::class), |
|
65 | + Server::get(IConfig::class), |
|
66 | 66 | ); |
67 | 67 | $authPlugin->addBackend($bearerAuthPlugin); |
68 | 68 | |
69 | 69 | $requestUri = Server::get(IRequest::class)->getRequestUri(); |
70 | 70 | |
71 | 71 | $server = $serverFactory->createServer(false, $baseuri, $requestUri, $authPlugin, function () { |
72 | - // use the view for the logged in user |
|
73 | - return Filesystem::getView(); |
|
72 | + // use the view for the logged in user |
|
73 | + return Filesystem::getView(); |
|
74 | 74 | }); |
75 | 75 | |
76 | 76 | // allow setup of additional plugins |
@@ -74,12 +74,12 @@ discard block |
||
74 | 74 | public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) { |
75 | 75 | if ($size <= 64) { |
76 | 76 | if ($size !== 64) { |
77 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
77 | + $this->logger->debug('Avatar requested in deprecated size '.$size); |
|
78 | 78 | } |
79 | 79 | $size = 64; |
80 | 80 | } else { |
81 | 81 | if ($size !== 512) { |
82 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
82 | + $this->logger->debug('Avatar requested in deprecated size '.$size); |
|
83 | 83 | } |
84 | 84 | $size = 512; |
85 | 85 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | $response = new FileDisplayResponse( |
91 | 91 | $avatarFile, |
92 | 92 | Http::STATUS_OK, |
93 | - ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()] |
|
93 | + ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int) $avatar->isCustomAvatar()] |
|
94 | 94 | ); |
95 | 95 | } catch (\Exception $e) { |
96 | 96 | if ($guestFallback) { |
@@ -126,12 +126,12 @@ discard block |
||
126 | 126 | public function getAvatar(string $userId, int $size, bool $guestFallback = false) { |
127 | 127 | if ($size <= 64) { |
128 | 128 | if ($size !== 64) { |
129 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
129 | + $this->logger->debug('Avatar requested in deprecated size '.$size); |
|
130 | 130 | } |
131 | 131 | $size = 64; |
132 | 132 | } else { |
133 | 133 | if ($size !== 512) { |
134 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
134 | + $this->logger->debug('Avatar requested in deprecated size '.$size); |
|
135 | 135 | } |
136 | 136 | $size = 512; |
137 | 137 | } |
@@ -142,7 +142,7 @@ discard block |
||
142 | 142 | $response = new FileDisplayResponse( |
143 | 143 | $avatarFile, |
144 | 144 | Http::STATUS_OK, |
145 | - ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()] |
|
145 | + ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int) $avatar->isCustomAvatar()] |
|
146 | 146 | ); |
147 | 147 | } catch (\Exception $e) { |
148 | 148 | if ($guestFallback) { |
@@ -311,7 +311,7 @@ discard block |
||
311 | 311 | Http::STATUS_OK, |
312 | 312 | ['Content-Type' => $image->mimeType()]); |
313 | 313 | |
314 | - $resp->setETag((string)crc32($image->data() ?? '')); |
|
314 | + $resp->setETag((string) crc32($image->data() ?? '')); |
|
315 | 315 | $resp->cacheFor(0); |
316 | 316 | $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
317 | 317 | return $resp; |
@@ -340,7 +340,7 @@ discard block |
||
340 | 340 | |
341 | 341 | $image = new Image(); |
342 | 342 | $image->loadFromData($tmpAvatar); |
343 | - $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
343 | + $image->crop($crop['x'], $crop['y'], (int) round($crop['w']), (int) round($crop['h'])); |
|
344 | 344 | try { |
345 | 345 | $avatar = $this->avatarManager->getAvatar($this->userId); |
346 | 346 | $avatar->set($image); |
@@ -37,322 +37,322 @@ |
||
37 | 37 | * @package OC\Core\Controller |
38 | 38 | */ |
39 | 39 | class AvatarController extends Controller { |
40 | - public function __construct( |
|
41 | - string $appName, |
|
42 | - IRequest $request, |
|
43 | - protected IAvatarManager $avatarManager, |
|
44 | - protected ICache $cache, |
|
45 | - protected IL10N $l10n, |
|
46 | - protected IUserManager $userManager, |
|
47 | - protected IRootFolder $rootFolder, |
|
48 | - protected LoggerInterface $logger, |
|
49 | - protected ?string $userId, |
|
50 | - protected TimeFactory $timeFactory, |
|
51 | - protected GuestAvatarController $guestAvatarController, |
|
52 | - ) { |
|
53 | - parent::__construct($appName, $request); |
|
54 | - } |
|
40 | + public function __construct( |
|
41 | + string $appName, |
|
42 | + IRequest $request, |
|
43 | + protected IAvatarManager $avatarManager, |
|
44 | + protected ICache $cache, |
|
45 | + protected IL10N $l10n, |
|
46 | + protected IUserManager $userManager, |
|
47 | + protected IRootFolder $rootFolder, |
|
48 | + protected LoggerInterface $logger, |
|
49 | + protected ?string $userId, |
|
50 | + protected TimeFactory $timeFactory, |
|
51 | + protected GuestAvatarController $guestAvatarController, |
|
52 | + ) { |
|
53 | + parent::__construct($appName, $request); |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @NoSameSiteCookieRequired |
|
58 | - * |
|
59 | - * Get the dark avatar |
|
60 | - * |
|
61 | - * @param string $userId ID of the user |
|
62 | - * @param 64|512 $size Size of the avatar |
|
63 | - * @param bool $guestFallback Fallback to guest avatar if not found |
|
64 | - * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}> |
|
65 | - * |
|
66 | - * 200: Avatar returned |
|
67 | - * 201: Avatar returned |
|
68 | - * 404: Avatar not found |
|
69 | - */ |
|
70 | - #[NoCSRFRequired] |
|
71 | - #[PublicPage] |
|
72 | - #[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}/dark')] |
|
73 | - #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)] |
|
74 | - public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) { |
|
75 | - if ($size <= 64) { |
|
76 | - if ($size !== 64) { |
|
77 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
78 | - } |
|
79 | - $size = 64; |
|
80 | - } else { |
|
81 | - if ($size !== 512) { |
|
82 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
83 | - } |
|
84 | - $size = 512; |
|
85 | - } |
|
56 | + /** |
|
57 | + * @NoSameSiteCookieRequired |
|
58 | + * |
|
59 | + * Get the dark avatar |
|
60 | + * |
|
61 | + * @param string $userId ID of the user |
|
62 | + * @param 64|512 $size Size of the avatar |
|
63 | + * @param bool $guestFallback Fallback to guest avatar if not found |
|
64 | + * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}> |
|
65 | + * |
|
66 | + * 200: Avatar returned |
|
67 | + * 201: Avatar returned |
|
68 | + * 404: Avatar not found |
|
69 | + */ |
|
70 | + #[NoCSRFRequired] |
|
71 | + #[PublicPage] |
|
72 | + #[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}/dark')] |
|
73 | + #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)] |
|
74 | + public function getAvatarDark(string $userId, int $size, bool $guestFallback = false) { |
|
75 | + if ($size <= 64) { |
|
76 | + if ($size !== 64) { |
|
77 | + $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
78 | + } |
|
79 | + $size = 64; |
|
80 | + } else { |
|
81 | + if ($size !== 512) { |
|
82 | + $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
83 | + } |
|
84 | + $size = 512; |
|
85 | + } |
|
86 | 86 | |
87 | - try { |
|
88 | - $avatar = $this->avatarManager->getAvatar($userId); |
|
89 | - $avatarFile = $avatar->getFile($size, true); |
|
90 | - $response = new FileDisplayResponse( |
|
91 | - $avatarFile, |
|
92 | - Http::STATUS_OK, |
|
93 | - ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()] |
|
94 | - ); |
|
95 | - } catch (\Exception $e) { |
|
96 | - if ($guestFallback) { |
|
97 | - return $this->guestAvatarController->getAvatarDark($userId, $size); |
|
98 | - } |
|
99 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
100 | - } |
|
87 | + try { |
|
88 | + $avatar = $this->avatarManager->getAvatar($userId); |
|
89 | + $avatarFile = $avatar->getFile($size, true); |
|
90 | + $response = new FileDisplayResponse( |
|
91 | + $avatarFile, |
|
92 | + Http::STATUS_OK, |
|
93 | + ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()] |
|
94 | + ); |
|
95 | + } catch (\Exception $e) { |
|
96 | + if ($guestFallback) { |
|
97 | + return $this->guestAvatarController->getAvatarDark($userId, $size); |
|
98 | + } |
|
99 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
100 | + } |
|
101 | 101 | |
102 | - // Cache for 1 day |
|
103 | - $response->cacheFor(60 * 60 * 24, false, true); |
|
104 | - return $response; |
|
105 | - } |
|
102 | + // Cache for 1 day |
|
103 | + $response->cacheFor(60 * 60 * 24, false, true); |
|
104 | + return $response; |
|
105 | + } |
|
106 | 106 | |
107 | 107 | |
108 | - /** |
|
109 | - * @NoSameSiteCookieRequired |
|
110 | - * |
|
111 | - * Get the avatar |
|
112 | - * |
|
113 | - * @param string $userId ID of the user |
|
114 | - * @param 64|512 $size Size of the avatar |
|
115 | - * @param bool $guestFallback Fallback to guest avatar if not found |
|
116 | - * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}> |
|
117 | - * |
|
118 | - * 200: Avatar returned |
|
119 | - * 201: Avatar returned |
|
120 | - * 404: Avatar not found |
|
121 | - */ |
|
122 | - #[NoCSRFRequired] |
|
123 | - #[PublicPage] |
|
124 | - #[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}')] |
|
125 | - #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)] |
|
126 | - public function getAvatar(string $userId, int $size, bool $guestFallback = false) { |
|
127 | - if ($size <= 64) { |
|
128 | - if ($size !== 64) { |
|
129 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
130 | - } |
|
131 | - $size = 64; |
|
132 | - } else { |
|
133 | - if ($size !== 512) { |
|
134 | - $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
135 | - } |
|
136 | - $size = 512; |
|
137 | - } |
|
108 | + /** |
|
109 | + * @NoSameSiteCookieRequired |
|
110 | + * |
|
111 | + * Get the avatar |
|
112 | + * |
|
113 | + * @param string $userId ID of the user |
|
114 | + * @param 64|512 $size Size of the avatar |
|
115 | + * @param bool $guestFallback Fallback to guest avatar if not found |
|
116 | + * @return FileDisplayResponse<Http::STATUS_OK|Http::STATUS_CREATED, array{Content-Type: string, X-NC-IsCustomAvatar: int}>|JSONResponse<Http::STATUS_NOT_FOUND, list<empty>, array{}>|Response<Http::STATUS_INTERNAL_SERVER_ERROR, array{}> |
|
117 | + * |
|
118 | + * 200: Avatar returned |
|
119 | + * 201: Avatar returned |
|
120 | + * 404: Avatar not found |
|
121 | + */ |
|
122 | + #[NoCSRFRequired] |
|
123 | + #[PublicPage] |
|
124 | + #[FrontpageRoute(verb: 'GET', url: '/avatar/{userId}/{size}')] |
|
125 | + #[OpenAPI(scope: OpenAPI::SCOPE_DEFAULT)] |
|
126 | + public function getAvatar(string $userId, int $size, bool $guestFallback = false) { |
|
127 | + if ($size <= 64) { |
|
128 | + if ($size !== 64) { |
|
129 | + $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
130 | + } |
|
131 | + $size = 64; |
|
132 | + } else { |
|
133 | + if ($size !== 512) { |
|
134 | + $this->logger->debug('Avatar requested in deprecated size ' . $size); |
|
135 | + } |
|
136 | + $size = 512; |
|
137 | + } |
|
138 | 138 | |
139 | - try { |
|
140 | - $avatar = $this->avatarManager->getAvatar($userId); |
|
141 | - $avatarFile = $avatar->getFile($size); |
|
142 | - $response = new FileDisplayResponse( |
|
143 | - $avatarFile, |
|
144 | - Http::STATUS_OK, |
|
145 | - ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()] |
|
146 | - ); |
|
147 | - } catch (\Exception $e) { |
|
148 | - if ($guestFallback) { |
|
149 | - return $this->guestAvatarController->getAvatar($userId, $size); |
|
150 | - } |
|
151 | - return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
152 | - } |
|
139 | + try { |
|
140 | + $avatar = $this->avatarManager->getAvatar($userId); |
|
141 | + $avatarFile = $avatar->getFile($size); |
|
142 | + $response = new FileDisplayResponse( |
|
143 | + $avatarFile, |
|
144 | + Http::STATUS_OK, |
|
145 | + ['Content-Type' => $avatarFile->getMimeType(), 'X-NC-IsCustomAvatar' => (int)$avatar->isCustomAvatar()] |
|
146 | + ); |
|
147 | + } catch (\Exception $e) { |
|
148 | + if ($guestFallback) { |
|
149 | + return $this->guestAvatarController->getAvatar($userId, $size); |
|
150 | + } |
|
151 | + return new JSONResponse([], Http::STATUS_NOT_FOUND); |
|
152 | + } |
|
153 | 153 | |
154 | - // Cache for 1 day |
|
155 | - $response->cacheFor(60 * 60 * 24, false, true); |
|
156 | - return $response; |
|
157 | - } |
|
154 | + // Cache for 1 day |
|
155 | + $response->cacheFor(60 * 60 * 24, false, true); |
|
156 | + return $response; |
|
157 | + } |
|
158 | 158 | |
159 | - #[NoAdminRequired] |
|
160 | - #[FrontpageRoute(verb: 'POST', url: '/avatar/')] |
|
161 | - public function postAvatar(?string $path = null): JSONResponse { |
|
162 | - $files = $this->request->getUploadedFile('files'); |
|
159 | + #[NoAdminRequired] |
|
160 | + #[FrontpageRoute(verb: 'POST', url: '/avatar/')] |
|
161 | + public function postAvatar(?string $path = null): JSONResponse { |
|
162 | + $files = $this->request->getUploadedFile('files'); |
|
163 | 163 | |
164 | - if (isset($path)) { |
|
165 | - $path = stripslashes($path); |
|
166 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
167 | - /** @var File $node */ |
|
168 | - $node = $userFolder->get($path); |
|
169 | - if (!($node instanceof File)) { |
|
170 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]); |
|
171 | - } |
|
172 | - if ($node->getSize() > 20 * 1024 * 1024) { |
|
173 | - return new JSONResponse( |
|
174 | - ['data' => ['message' => $this->l10n->t('File is too big')]], |
|
175 | - Http::STATUS_BAD_REQUEST |
|
176 | - ); |
|
177 | - } |
|
164 | + if (isset($path)) { |
|
165 | + $path = stripslashes($path); |
|
166 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
167 | + /** @var File $node */ |
|
168 | + $node = $userFolder->get($path); |
|
169 | + if (!($node instanceof File)) { |
|
170 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('Please select a file.')]]); |
|
171 | + } |
|
172 | + if ($node->getSize() > 20 * 1024 * 1024) { |
|
173 | + return new JSONResponse( |
|
174 | + ['data' => ['message' => $this->l10n->t('File is too big')]], |
|
175 | + Http::STATUS_BAD_REQUEST |
|
176 | + ); |
|
177 | + } |
|
178 | 178 | |
179 | - if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
180 | - return new JSONResponse( |
|
181 | - ['data' => ['message' => $this->l10n->t('The selected file is not an image.')]], |
|
182 | - Http::STATUS_BAD_REQUEST |
|
183 | - ); |
|
184 | - } |
|
179 | + if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
180 | + return new JSONResponse( |
|
181 | + ['data' => ['message' => $this->l10n->t('The selected file is not an image.')]], |
|
182 | + Http::STATUS_BAD_REQUEST |
|
183 | + ); |
|
184 | + } |
|
185 | 185 | |
186 | - try { |
|
187 | - $content = $node->getContent(); |
|
188 | - } catch (NotPermittedException $e) { |
|
189 | - return new JSONResponse( |
|
190 | - ['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]], |
|
191 | - Http::STATUS_BAD_REQUEST |
|
192 | - ); |
|
193 | - } |
|
194 | - } elseif (!is_null($files)) { |
|
195 | - if ( |
|
196 | - $files['error'][0] === 0 |
|
197 | - && is_uploaded_file($files['tmp_name'][0]) |
|
198 | - ) { |
|
199 | - if ($files['size'][0] > 20 * 1024 * 1024) { |
|
200 | - return new JSONResponse( |
|
201 | - ['data' => ['message' => $this->l10n->t('File is too big')]], |
|
202 | - Http::STATUS_BAD_REQUEST |
|
203 | - ); |
|
204 | - } |
|
205 | - $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
|
206 | - $content = $this->cache->get('avatar_upload'); |
|
207 | - unlink($files['tmp_name'][0]); |
|
208 | - } else { |
|
209 | - $phpFileUploadErrors = [ |
|
210 | - UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
|
211 | - UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
|
212 | - UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
|
213 | - UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
|
214 | - UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
|
215 | - UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
|
216 | - UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
|
217 | - UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
|
218 | - ]; |
|
219 | - $message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided'); |
|
220 | - $this->logger->warning($message, ['app' => 'core']); |
|
221 | - return new JSONResponse( |
|
222 | - ['data' => ['message' => $message]], |
|
223 | - Http::STATUS_BAD_REQUEST |
|
224 | - ); |
|
225 | - } |
|
226 | - } else { |
|
227 | - //Add imgfile |
|
228 | - return new JSONResponse( |
|
229 | - ['data' => ['message' => $this->l10n->t('No image or file provided')]], |
|
230 | - Http::STATUS_BAD_REQUEST |
|
231 | - ); |
|
232 | - } |
|
186 | + try { |
|
187 | + $content = $node->getContent(); |
|
188 | + } catch (NotPermittedException $e) { |
|
189 | + return new JSONResponse( |
|
190 | + ['data' => ['message' => $this->l10n->t('The selected file cannot be read.')]], |
|
191 | + Http::STATUS_BAD_REQUEST |
|
192 | + ); |
|
193 | + } |
|
194 | + } elseif (!is_null($files)) { |
|
195 | + if ( |
|
196 | + $files['error'][0] === 0 |
|
197 | + && is_uploaded_file($files['tmp_name'][0]) |
|
198 | + ) { |
|
199 | + if ($files['size'][0] > 20 * 1024 * 1024) { |
|
200 | + return new JSONResponse( |
|
201 | + ['data' => ['message' => $this->l10n->t('File is too big')]], |
|
202 | + Http::STATUS_BAD_REQUEST |
|
203 | + ); |
|
204 | + } |
|
205 | + $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
|
206 | + $content = $this->cache->get('avatar_upload'); |
|
207 | + unlink($files['tmp_name'][0]); |
|
208 | + } else { |
|
209 | + $phpFileUploadErrors = [ |
|
210 | + UPLOAD_ERR_OK => $this->l10n->t('The file was uploaded'), |
|
211 | + UPLOAD_ERR_INI_SIZE => $this->l10n->t('The uploaded file exceeds the upload_max_filesize directive in php.ini'), |
|
212 | + UPLOAD_ERR_FORM_SIZE => $this->l10n->t('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'), |
|
213 | + UPLOAD_ERR_PARTIAL => $this->l10n->t('The file was only partially uploaded'), |
|
214 | + UPLOAD_ERR_NO_FILE => $this->l10n->t('No file was uploaded'), |
|
215 | + UPLOAD_ERR_NO_TMP_DIR => $this->l10n->t('Missing a temporary folder'), |
|
216 | + UPLOAD_ERR_CANT_WRITE => $this->l10n->t('Could not write file to disk'), |
|
217 | + UPLOAD_ERR_EXTENSION => $this->l10n->t('A PHP extension stopped the file upload'), |
|
218 | + ]; |
|
219 | + $message = $phpFileUploadErrors[$files['error'][0]] ?? $this->l10n->t('Invalid file provided'); |
|
220 | + $this->logger->warning($message, ['app' => 'core']); |
|
221 | + return new JSONResponse( |
|
222 | + ['data' => ['message' => $message]], |
|
223 | + Http::STATUS_BAD_REQUEST |
|
224 | + ); |
|
225 | + } |
|
226 | + } else { |
|
227 | + //Add imgfile |
|
228 | + return new JSONResponse( |
|
229 | + ['data' => ['message' => $this->l10n->t('No image or file provided')]], |
|
230 | + Http::STATUS_BAD_REQUEST |
|
231 | + ); |
|
232 | + } |
|
233 | 233 | |
234 | - try { |
|
235 | - $image = new Image(); |
|
236 | - $image->loadFromData($content); |
|
237 | - $image->readExif($content); |
|
238 | - $image->fixOrientation(); |
|
234 | + try { |
|
235 | + $image = new Image(); |
|
236 | + $image->loadFromData($content); |
|
237 | + $image->readExif($content); |
|
238 | + $image->fixOrientation(); |
|
239 | 239 | |
240 | - if ($image->valid()) { |
|
241 | - $mimeType = $image->mimeType(); |
|
242 | - if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
243 | - return new JSONResponse( |
|
244 | - ['data' => ['message' => $this->l10n->t('Unknown filetype')]], |
|
245 | - Http::STATUS_OK |
|
246 | - ); |
|
247 | - } |
|
240 | + if ($image->valid()) { |
|
241 | + $mimeType = $image->mimeType(); |
|
242 | + if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
243 | + return new JSONResponse( |
|
244 | + ['data' => ['message' => $this->l10n->t('Unknown filetype')]], |
|
245 | + Http::STATUS_OK |
|
246 | + ); |
|
247 | + } |
|
248 | 248 | |
249 | - if ($image->width() === $image->height()) { |
|
250 | - try { |
|
251 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
252 | - $avatar->set($image); |
|
253 | - // Clean up |
|
254 | - $this->cache->remove('tmpAvatar'); |
|
255 | - return new JSONResponse(['status' => 'success']); |
|
256 | - } catch (\Throwable $e) { |
|
257 | - $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
258 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
259 | - } |
|
260 | - } |
|
249 | + if ($image->width() === $image->height()) { |
|
250 | + try { |
|
251 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
252 | + $avatar->set($image); |
|
253 | + // Clean up |
|
254 | + $this->cache->remove('tmpAvatar'); |
|
255 | + return new JSONResponse(['status' => 'success']); |
|
256 | + } catch (\Throwable $e) { |
|
257 | + $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
258 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
259 | + } |
|
260 | + } |
|
261 | 261 | |
262 | - $this->cache->set('tmpAvatar', $image->data(), 7200); |
|
263 | - return new JSONResponse( |
|
264 | - ['data' => 'notsquare'], |
|
265 | - Http::STATUS_OK |
|
266 | - ); |
|
267 | - } else { |
|
268 | - return new JSONResponse( |
|
269 | - ['data' => ['message' => $this->l10n->t('Invalid image')]], |
|
270 | - Http::STATUS_OK |
|
271 | - ); |
|
272 | - } |
|
273 | - } catch (\Exception $e) { |
|
274 | - $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
275 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
276 | - } |
|
277 | - } |
|
262 | + $this->cache->set('tmpAvatar', $image->data(), 7200); |
|
263 | + return new JSONResponse( |
|
264 | + ['data' => 'notsquare'], |
|
265 | + Http::STATUS_OK |
|
266 | + ); |
|
267 | + } else { |
|
268 | + return new JSONResponse( |
|
269 | + ['data' => ['message' => $this->l10n->t('Invalid image')]], |
|
270 | + Http::STATUS_OK |
|
271 | + ); |
|
272 | + } |
|
273 | + } catch (\Exception $e) { |
|
274 | + $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
275 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
276 | + } |
|
277 | + } |
|
278 | 278 | |
279 | - #[NoAdminRequired] |
|
280 | - #[FrontpageRoute(verb: 'DELETE', url: '/avatar/')] |
|
281 | - public function deleteAvatar(): JSONResponse { |
|
282 | - try { |
|
283 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
284 | - $avatar->remove(); |
|
285 | - return new JSONResponse(); |
|
286 | - } catch (\Exception $e) { |
|
287 | - $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
288 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
289 | - } |
|
290 | - } |
|
279 | + #[NoAdminRequired] |
|
280 | + #[FrontpageRoute(verb: 'DELETE', url: '/avatar/')] |
|
281 | + public function deleteAvatar(): JSONResponse { |
|
282 | + try { |
|
283 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
284 | + $avatar->remove(); |
|
285 | + return new JSONResponse(); |
|
286 | + } catch (\Exception $e) { |
|
287 | + $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
288 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
289 | + } |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * @return JSONResponse|DataDisplayResponse |
|
294 | - */ |
|
295 | - #[NoAdminRequired] |
|
296 | - #[FrontpageRoute(verb: 'GET', url: '/avatar/tmp')] |
|
297 | - public function getTmpAvatar() { |
|
298 | - $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
299 | - if (is_null($tmpAvatar)) { |
|
300 | - return new JSONResponse(['data' => [ |
|
301 | - 'message' => $this->l10n->t('No temporary profile picture available, try again') |
|
302 | - ]], |
|
303 | - Http::STATUS_NOT_FOUND); |
|
304 | - } |
|
292 | + /** |
|
293 | + * @return JSONResponse|DataDisplayResponse |
|
294 | + */ |
|
295 | + #[NoAdminRequired] |
|
296 | + #[FrontpageRoute(verb: 'GET', url: '/avatar/tmp')] |
|
297 | + public function getTmpAvatar() { |
|
298 | + $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
299 | + if (is_null($tmpAvatar)) { |
|
300 | + return new JSONResponse(['data' => [ |
|
301 | + 'message' => $this->l10n->t('No temporary profile picture available, try again') |
|
302 | + ]], |
|
303 | + Http::STATUS_NOT_FOUND); |
|
304 | + } |
|
305 | 305 | |
306 | - $image = new Image(); |
|
307 | - $image->loadFromData($tmpAvatar); |
|
306 | + $image = new Image(); |
|
307 | + $image->loadFromData($tmpAvatar); |
|
308 | 308 | |
309 | - $resp = new DataDisplayResponse( |
|
310 | - $image->data() ?? '', |
|
311 | - Http::STATUS_OK, |
|
312 | - ['Content-Type' => $image->mimeType()]); |
|
309 | + $resp = new DataDisplayResponse( |
|
310 | + $image->data() ?? '', |
|
311 | + Http::STATUS_OK, |
|
312 | + ['Content-Type' => $image->mimeType()]); |
|
313 | 313 | |
314 | - $resp->setETag((string)crc32($image->data() ?? '')); |
|
315 | - $resp->cacheFor(0); |
|
316 | - $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
317 | - return $resp; |
|
318 | - } |
|
314 | + $resp->setETag((string)crc32($image->data() ?? '')); |
|
315 | + $resp->cacheFor(0); |
|
316 | + $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
317 | + return $resp; |
|
318 | + } |
|
319 | 319 | |
320 | - #[NoAdminRequired] |
|
321 | - #[FrontpageRoute(verb: 'POST', url: '/avatar/cropped')] |
|
322 | - public function postCroppedAvatar(?array $crop = null): JSONResponse { |
|
323 | - if (is_null($crop)) { |
|
324 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('No crop data provided')]], |
|
325 | - Http::STATUS_BAD_REQUEST); |
|
326 | - } |
|
320 | + #[NoAdminRequired] |
|
321 | + #[FrontpageRoute(verb: 'POST', url: '/avatar/cropped')] |
|
322 | + public function postCroppedAvatar(?array $crop = null): JSONResponse { |
|
323 | + if (is_null($crop)) { |
|
324 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('No crop data provided')]], |
|
325 | + Http::STATUS_BAD_REQUEST); |
|
326 | + } |
|
327 | 327 | |
328 | - if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
329 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('No valid crop data provided')]], |
|
330 | - Http::STATUS_BAD_REQUEST); |
|
331 | - } |
|
328 | + if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
329 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('No valid crop data provided')]], |
|
330 | + Http::STATUS_BAD_REQUEST); |
|
331 | + } |
|
332 | 332 | |
333 | - $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
334 | - if (is_null($tmpAvatar)) { |
|
335 | - return new JSONResponse(['data' => [ |
|
336 | - 'message' => $this->l10n->t('No temporary profile picture available, try again') |
|
337 | - ]], |
|
338 | - Http::STATUS_BAD_REQUEST); |
|
339 | - } |
|
333 | + $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
334 | + if (is_null($tmpAvatar)) { |
|
335 | + return new JSONResponse(['data' => [ |
|
336 | + 'message' => $this->l10n->t('No temporary profile picture available, try again') |
|
337 | + ]], |
|
338 | + Http::STATUS_BAD_REQUEST); |
|
339 | + } |
|
340 | 340 | |
341 | - $image = new Image(); |
|
342 | - $image->loadFromData($tmpAvatar); |
|
343 | - $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
344 | - try { |
|
345 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
346 | - $avatar->set($image); |
|
347 | - // Clean up |
|
348 | - $this->cache->remove('tmpAvatar'); |
|
349 | - return new JSONResponse(['status' => 'success']); |
|
350 | - } catch (NotSquareException $e) { |
|
351 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]], |
|
352 | - Http::STATUS_BAD_REQUEST); |
|
353 | - } catch (\Exception $e) { |
|
354 | - $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
355 | - return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
356 | - } |
|
357 | - } |
|
341 | + $image = new Image(); |
|
342 | + $image->loadFromData($tmpAvatar); |
|
343 | + $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
344 | + try { |
|
345 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
346 | + $avatar->set($image); |
|
347 | + // Clean up |
|
348 | + $this->cache->remove('tmpAvatar'); |
|
349 | + return new JSONResponse(['status' => 'success']); |
|
350 | + } catch (NotSquareException $e) { |
|
351 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('Crop is not square')]], |
|
352 | + Http::STATUS_BAD_REQUEST); |
|
353 | + } catch (\Exception $e) { |
|
354 | + $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'core']); |
|
355 | + return new JSONResponse(['data' => ['message' => $this->l10n->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
356 | + } |
|
357 | + } |
|
358 | 358 | } |
@@ -36,33 +36,33 @@ discard block |
||
36 | 36 | use Sabre\CardDAV\Plugin; |
37 | 37 | |
38 | 38 | $authBackend = new Auth( |
39 | - Server::get(ISession::class), |
|
40 | - Server::get(IUserSession::class), |
|
41 | - Server::get(IRequest::class), |
|
42 | - Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
43 | - Server::get(IThrottler::class), |
|
44 | - 'principals/' |
|
39 | + Server::get(ISession::class), |
|
40 | + Server::get(IUserSession::class), |
|
41 | + Server::get(IRequest::class), |
|
42 | + Server::get(\OC\Authentication\TwoFactorAuth\Manager::class), |
|
43 | + Server::get(IThrottler::class), |
|
44 | + 'principals/' |
|
45 | 45 | ); |
46 | 46 | $principalBackend = new Principal( |
47 | - Server::get(IUserManager::class), |
|
48 | - Server::get(IGroupManager::class), |
|
49 | - Server::get(IAccountManager::class), |
|
50 | - Server::get(\OCP\Share\IManager::class), |
|
51 | - Server::get(IUserSession::class), |
|
52 | - Server::get(IAppManager::class), |
|
53 | - Server::get(ProxyMapper::class), |
|
54 | - Server::get(KnownUserService::class), |
|
55 | - Server::get(IConfig::class), |
|
56 | - \OC::$server->getL10NFactory(), |
|
57 | - 'principals/' |
|
47 | + Server::get(IUserManager::class), |
|
48 | + Server::get(IGroupManager::class), |
|
49 | + Server::get(IAccountManager::class), |
|
50 | + Server::get(\OCP\Share\IManager::class), |
|
51 | + Server::get(IUserSession::class), |
|
52 | + Server::get(IAppManager::class), |
|
53 | + Server::get(ProxyMapper::class), |
|
54 | + Server::get(KnownUserService::class), |
|
55 | + Server::get(IConfig::class), |
|
56 | + \OC::$server->getL10NFactory(), |
|
57 | + 'principals/' |
|
58 | 58 | ); |
59 | 59 | $db = Server::get(IDBConnection::class); |
60 | 60 | $cardDavBackend = new CardDavBackend( |
61 | - $db, |
|
62 | - $principalBackend, |
|
63 | - Server::get(IUserManager::class), |
|
64 | - Server::get(IEventDispatcher::class), |
|
65 | - Server::get(\OCA\DAV\CardDAV\Sharing\Backend::class), |
|
61 | + $db, |
|
62 | + $principalBackend, |
|
63 | + Server::get(IUserManager::class), |
|
64 | + Server::get(IEventDispatcher::class), |
|
65 | + Server::get(\OCA\DAV\CardDAV\Sharing\Backend::class), |
|
66 | 66 | ); |
67 | 67 | |
68 | 68 | $debugging = Server::get(IConfig::class)->getSystemValue('debug', false); |
@@ -76,8 +76,8 @@ discard block |
||
76 | 76 | $addressBookRoot->disableListing = !$debugging; // Disable listing |
77 | 77 | |
78 | 78 | $nodes = [ |
79 | - $principalCollection, |
|
80 | - $addressBookRoot, |
|
79 | + $principalCollection, |
|
80 | + $addressBookRoot, |
|
81 | 81 | ]; |
82 | 82 | |
83 | 83 | // Fire up server |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | |
93 | 93 | $server->addPlugin(new LegacyDAVACL()); |
94 | 94 | if ($debugging) { |
95 | - $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
95 | + $server->addPlugin(new Sabre\DAV\Browser\Plugin()); |
|
96 | 96 | } |
97 | 97 | |
98 | 98 | $server->addPlugin(new \Sabre\DAV\Sync\Plugin()); |
@@ -15,50 +15,50 @@ |
||
15 | 15 | * @package Test\Cache |
16 | 16 | */ |
17 | 17 | class CappedMemoryCacheTest extends TestCache { |
18 | - protected function setUp(): void { |
|
19 | - parent::setUp(); |
|
20 | - $this->instance = new CappedMemoryCache(); |
|
21 | - } |
|
18 | + protected function setUp(): void { |
|
19 | + parent::setUp(); |
|
20 | + $this->instance = new CappedMemoryCache(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function testSetOverCap(): void { |
|
24 | - $instance = new CappedMemoryCache(3); |
|
23 | + public function testSetOverCap(): void { |
|
24 | + $instance = new CappedMemoryCache(3); |
|
25 | 25 | |
26 | - $instance->set('1', 'a'); |
|
27 | - $instance->set('2', 'b'); |
|
28 | - $instance->set('3', 'c'); |
|
29 | - $instance->set('4', 'd'); |
|
30 | - $instance->set('5', 'e'); |
|
26 | + $instance->set('1', 'a'); |
|
27 | + $instance->set('2', 'b'); |
|
28 | + $instance->set('3', 'c'); |
|
29 | + $instance->set('4', 'd'); |
|
30 | + $instance->set('5', 'e'); |
|
31 | 31 | |
32 | - $this->assertFalse($instance->hasKey('1')); |
|
33 | - $this->assertFalse($instance->hasKey('2')); |
|
34 | - $this->assertTrue($instance->hasKey('3')); |
|
35 | - $this->assertTrue($instance->hasKey('4')); |
|
36 | - $this->assertTrue($instance->hasKey('5')); |
|
37 | - } |
|
32 | + $this->assertFalse($instance->hasKey('1')); |
|
33 | + $this->assertFalse($instance->hasKey('2')); |
|
34 | + $this->assertTrue($instance->hasKey('3')); |
|
35 | + $this->assertTrue($instance->hasKey('4')); |
|
36 | + $this->assertTrue($instance->hasKey('5')); |
|
37 | + } |
|
38 | 38 | |
39 | - public function testClear(): void { |
|
40 | - $value = 'ipsum lorum'; |
|
41 | - $this->instance->set('1_value1', $value); |
|
42 | - $this->instance->set('1_value2', $value); |
|
43 | - $this->instance->set('2_value1', $value); |
|
44 | - $this->instance->set('3_value1', $value); |
|
39 | + public function testClear(): void { |
|
40 | + $value = 'ipsum lorum'; |
|
41 | + $this->instance->set('1_value1', $value); |
|
42 | + $this->instance->set('1_value2', $value); |
|
43 | + $this->instance->set('2_value1', $value); |
|
44 | + $this->instance->set('3_value1', $value); |
|
45 | 45 | |
46 | - $this->assertTrue($this->instance->clear()); |
|
47 | - $this->assertFalse($this->instance->hasKey('1_value1')); |
|
48 | - $this->assertFalse($this->instance->hasKey('1_value2')); |
|
49 | - $this->assertFalse($this->instance->hasKey('2_value1')); |
|
50 | - $this->assertFalse($this->instance->hasKey('3_value1')); |
|
51 | - } |
|
46 | + $this->assertTrue($this->instance->clear()); |
|
47 | + $this->assertFalse($this->instance->hasKey('1_value1')); |
|
48 | + $this->assertFalse($this->instance->hasKey('1_value2')); |
|
49 | + $this->assertFalse($this->instance->hasKey('2_value1')); |
|
50 | + $this->assertFalse($this->instance->hasKey('3_value1')); |
|
51 | + } |
|
52 | 52 | |
53 | - public function testIndirectSet(): void { |
|
54 | - $this->instance->set('array', []); |
|
53 | + public function testIndirectSet(): void { |
|
54 | + $this->instance->set('array', []); |
|
55 | 55 | |
56 | - $this->instance['array'][] = 'foo'; |
|
56 | + $this->instance['array'][] = 'foo'; |
|
57 | 57 | |
58 | - $this->assertEquals(['foo'], $this->instance->get('array')); |
|
58 | + $this->assertEquals(['foo'], $this->instance->get('array')); |
|
59 | 59 | |
60 | - $this->instance['array']['bar'] = 'qwerty'; |
|
60 | + $this->instance['array']['bar'] = 'qwerty'; |
|
61 | 61 | |
62 | - $this->assertEquals(['foo', 'bar' => 'qwerty'], $this->instance->get('array')); |
|
63 | - } |
|
62 | + $this->assertEquals(['foo', 'bar' => 'qwerty'], $this->instance->get('array')); |
|
63 | + } |
|
64 | 64 | } |