@@ -34,234 +34,234 @@ |
||
34 | 34 | * Locking provider that stores the locks in the database |
35 | 35 | */ |
36 | 36 | class DBLockingProvider extends AbstractLockingProvider { |
37 | - /** |
|
38 | - * @var \OCP\IDBConnection |
|
39 | - */ |
|
40 | - private $connection; |
|
37 | + /** |
|
38 | + * @var \OCP\IDBConnection |
|
39 | + */ |
|
40 | + private $connection; |
|
41 | 41 | |
42 | - /** |
|
43 | - * @var \OCP\ILogger |
|
44 | - */ |
|
45 | - private $logger; |
|
42 | + /** |
|
43 | + * @var \OCP\ILogger |
|
44 | + */ |
|
45 | + private $logger; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @var \OCP\AppFramework\Utility\ITimeFactory |
|
49 | - */ |
|
50 | - private $timeFactory; |
|
47 | + /** |
|
48 | + * @var \OCP\AppFramework\Utility\ITimeFactory |
|
49 | + */ |
|
50 | + private $timeFactory; |
|
51 | 51 | |
52 | - private $sharedLocks = []; |
|
52 | + private $sharedLocks = []; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Check if we have an open shared lock for a path |
|
56 | - * |
|
57 | - * @param string $path |
|
58 | - * @return bool |
|
59 | - */ |
|
60 | - protected function isLocallyLocked($path) { |
|
61 | - return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
62 | - } |
|
54 | + /** |
|
55 | + * Check if we have an open shared lock for a path |
|
56 | + * |
|
57 | + * @param string $path |
|
58 | + * @return bool |
|
59 | + */ |
|
60 | + protected function isLocallyLocked($path) { |
|
61 | + return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path]; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Mark a locally acquired lock |
|
66 | - * |
|
67 | - * @param string $path |
|
68 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
69 | - */ |
|
70 | - protected function markAcquire($path, $type) { |
|
71 | - parent::markAcquire($path, $type); |
|
72 | - if ($type === self::LOCK_SHARED) { |
|
73 | - $this->sharedLocks[$path] = true; |
|
74 | - } |
|
75 | - } |
|
64 | + /** |
|
65 | + * Mark a locally acquired lock |
|
66 | + * |
|
67 | + * @param string $path |
|
68 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
69 | + */ |
|
70 | + protected function markAcquire($path, $type) { |
|
71 | + parent::markAcquire($path, $type); |
|
72 | + if ($type === self::LOCK_SHARED) { |
|
73 | + $this->sharedLocks[$path] = true; |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Change the type of an existing tracked lock |
|
79 | - * |
|
80 | - * @param string $path |
|
81 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
82 | - */ |
|
83 | - protected function markChange($path, $targetType) { |
|
84 | - parent::markChange($path, $targetType); |
|
85 | - if ($targetType === self::LOCK_SHARED) { |
|
86 | - $this->sharedLocks[$path] = true; |
|
87 | - } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
88 | - $this->sharedLocks[$path] = false; |
|
89 | - } |
|
90 | - } |
|
77 | + /** |
|
78 | + * Change the type of an existing tracked lock |
|
79 | + * |
|
80 | + * @param string $path |
|
81 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
82 | + */ |
|
83 | + protected function markChange($path, $targetType) { |
|
84 | + parent::markChange($path, $targetType); |
|
85 | + if ($targetType === self::LOCK_SHARED) { |
|
86 | + $this->sharedLocks[$path] = true; |
|
87 | + } else if ($targetType === self::LOCK_EXCLUSIVE) { |
|
88 | + $this->sharedLocks[$path] = false; |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @param \OCP\IDBConnection $connection |
|
94 | - * @param \OCP\ILogger $logger |
|
95 | - * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
96 | - */ |
|
97 | - public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory) { |
|
98 | - $this->connection = $connection; |
|
99 | - $this->logger = $logger; |
|
100 | - $this->timeFactory = $timeFactory; |
|
101 | - } |
|
92 | + /** |
|
93 | + * @param \OCP\IDBConnection $connection |
|
94 | + * @param \OCP\ILogger $logger |
|
95 | + * @param \OCP\AppFramework\Utility\ITimeFactory $timeFactory |
|
96 | + */ |
|
97 | + public function __construct(IDBConnection $connection, ILogger $logger, ITimeFactory $timeFactory) { |
|
98 | + $this->connection = $connection; |
|
99 | + $this->logger = $logger; |
|
100 | + $this->timeFactory = $timeFactory; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Insert a file locking row if it does not exists. |
|
105 | - * |
|
106 | - * @param string $path |
|
107 | - * @param int $lock |
|
108 | - * @return int number of inserted rows |
|
109 | - */ |
|
103 | + /** |
|
104 | + * Insert a file locking row if it does not exists. |
|
105 | + * |
|
106 | + * @param string $path |
|
107 | + * @param int $lock |
|
108 | + * @return int number of inserted rows |
|
109 | + */ |
|
110 | 110 | |
111 | - protected function initLockField($path, $lock = 0) { |
|
112 | - $expire = $this->getExpireTime(); |
|
113 | - return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); |
|
114 | - } |
|
111 | + protected function initLockField($path, $lock = 0) { |
|
112 | + $expire = $this->getExpireTime(); |
|
113 | + return $this->connection->insertIfNotExist('*PREFIX*file_locks', ['key' => $path, 'lock' => $lock, 'ttl' => $expire], ['key']); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * @return int |
|
118 | - */ |
|
119 | - protected function getExpireTime() { |
|
120 | - return $this->timeFactory->getTime() + self::TTL; |
|
121 | - } |
|
116 | + /** |
|
117 | + * @return int |
|
118 | + */ |
|
119 | + protected function getExpireTime() { |
|
120 | + return $this->timeFactory->getTime() + self::TTL; |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @param string $path |
|
125 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
126 | - * @return bool |
|
127 | - */ |
|
128 | - public function isLocked($path, $type) { |
|
129 | - if ($this->hasAcquiredLock($path, $type)) { |
|
130 | - return true; |
|
131 | - } |
|
132 | - $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
133 | - $query->execute([$path]); |
|
134 | - $lockValue = (int)$query->fetchColumn(); |
|
135 | - if ($type === self::LOCK_SHARED) { |
|
136 | - if ($this->isLocallyLocked($path)) { |
|
137 | - // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
138 | - return $lockValue > 1; |
|
139 | - } else { |
|
140 | - return $lockValue > 0; |
|
141 | - } |
|
142 | - } else if ($type === self::LOCK_EXCLUSIVE) { |
|
143 | - return $lockValue === -1; |
|
144 | - } else { |
|
145 | - return false; |
|
146 | - } |
|
147 | - } |
|
123 | + /** |
|
124 | + * @param string $path |
|
125 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
126 | + * @return bool |
|
127 | + */ |
|
128 | + public function isLocked($path, $type) { |
|
129 | + if ($this->hasAcquiredLock($path, $type)) { |
|
130 | + return true; |
|
131 | + } |
|
132 | + $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
|
133 | + $query->execute([$path]); |
|
134 | + $lockValue = (int)$query->fetchColumn(); |
|
135 | + if ($type === self::LOCK_SHARED) { |
|
136 | + if ($this->isLocallyLocked($path)) { |
|
137 | + // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
|
138 | + return $lockValue > 1; |
|
139 | + } else { |
|
140 | + return $lockValue > 0; |
|
141 | + } |
|
142 | + } else if ($type === self::LOCK_EXCLUSIVE) { |
|
143 | + return $lockValue === -1; |
|
144 | + } else { |
|
145 | + return false; |
|
146 | + } |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @param string $path |
|
151 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
152 | - * @throws \OCP\Lock\LockedException |
|
153 | - */ |
|
154 | - public function acquireLock($path, $type) { |
|
155 | - $expire = $this->getExpireTime(); |
|
156 | - if ($type === self::LOCK_SHARED) { |
|
157 | - if (!$this->isLocallyLocked($path)) { |
|
158 | - $result = $this->initLockField($path, 1); |
|
159 | - if ($result <= 0) { |
|
160 | - $result = $this->connection->executeUpdate( |
|
161 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
162 | - [$expire, $path] |
|
163 | - ); |
|
164 | - } |
|
165 | - } else { |
|
166 | - $result = 1; |
|
167 | - } |
|
168 | - } else { |
|
169 | - $existing = 0; |
|
170 | - if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
171 | - $existing = 1; |
|
172 | - } |
|
173 | - $result = $this->initLockField($path, -1); |
|
174 | - if ($result <= 0) { |
|
175 | - $result = $this->connection->executeUpdate( |
|
176 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
177 | - [$expire, $path, $existing] |
|
178 | - ); |
|
179 | - } |
|
180 | - } |
|
181 | - if ($result !== 1) { |
|
182 | - throw new LockedException($path); |
|
183 | - } |
|
184 | - $this->markAcquire($path, $type); |
|
185 | - } |
|
149 | + /** |
|
150 | + * @param string $path |
|
151 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
152 | + * @throws \OCP\Lock\LockedException |
|
153 | + */ |
|
154 | + public function acquireLock($path, $type) { |
|
155 | + $expire = $this->getExpireTime(); |
|
156 | + if ($type === self::LOCK_SHARED) { |
|
157 | + if (!$this->isLocallyLocked($path)) { |
|
158 | + $result = $this->initLockField($path, 1); |
|
159 | + if ($result <= 0) { |
|
160 | + $result = $this->connection->executeUpdate( |
|
161 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` + 1, `ttl` = ? WHERE `key` = ? AND `lock` >= 0', |
|
162 | + [$expire, $path] |
|
163 | + ); |
|
164 | + } |
|
165 | + } else { |
|
166 | + $result = 1; |
|
167 | + } |
|
168 | + } else { |
|
169 | + $existing = 0; |
|
170 | + if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) { |
|
171 | + $existing = 1; |
|
172 | + } |
|
173 | + $result = $this->initLockField($path, -1); |
|
174 | + if ($result <= 0) { |
|
175 | + $result = $this->connection->executeUpdate( |
|
176 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = ?', |
|
177 | + [$expire, $path, $existing] |
|
178 | + ); |
|
179 | + } |
|
180 | + } |
|
181 | + if ($result !== 1) { |
|
182 | + throw new LockedException($path); |
|
183 | + } |
|
184 | + $this->markAcquire($path, $type); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * @param string $path |
|
189 | - * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
190 | - */ |
|
191 | - public function releaseLock($path, $type) { |
|
192 | - $this->markRelease($path, $type); |
|
187 | + /** |
|
188 | + * @param string $path |
|
189 | + * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
190 | + */ |
|
191 | + public function releaseLock($path, $type) { |
|
192 | + $this->markRelease($path, $type); |
|
193 | 193 | |
194 | - // we keep shared locks till the end of the request so we can re-use them |
|
195 | - if ($type === self::LOCK_EXCLUSIVE) { |
|
196 | - $this->connection->executeUpdate( |
|
197 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
198 | - [$path] |
|
199 | - ); |
|
200 | - } |
|
201 | - } |
|
194 | + // we keep shared locks till the end of the request so we can re-use them |
|
195 | + if ($type === self::LOCK_EXCLUSIVE) { |
|
196 | + $this->connection->executeUpdate( |
|
197 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 0 WHERE `key` = ? AND `lock` = -1', |
|
198 | + [$path] |
|
199 | + ); |
|
200 | + } |
|
201 | + } |
|
202 | 202 | |
203 | - /** |
|
204 | - * Change the type of an existing lock |
|
205 | - * |
|
206 | - * @param string $path |
|
207 | - * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
208 | - * @throws \OCP\Lock\LockedException |
|
209 | - */ |
|
210 | - public function changeLock($path, $targetType) { |
|
211 | - $expire = $this->getExpireTime(); |
|
212 | - if ($targetType === self::LOCK_SHARED) { |
|
213 | - $result = $this->connection->executeUpdate( |
|
214 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
215 | - [$expire, $path] |
|
216 | - ); |
|
217 | - } else { |
|
218 | - // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
219 | - if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
220 | - throw new LockedException($path); |
|
221 | - } |
|
222 | - $result = $this->connection->executeUpdate( |
|
223 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
224 | - [$expire, $path] |
|
225 | - ); |
|
226 | - } |
|
227 | - if ($result !== 1) { |
|
228 | - throw new LockedException($path); |
|
229 | - } |
|
230 | - $this->markChange($path, $targetType); |
|
231 | - } |
|
203 | + /** |
|
204 | + * Change the type of an existing lock |
|
205 | + * |
|
206 | + * @param string $path |
|
207 | + * @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE |
|
208 | + * @throws \OCP\Lock\LockedException |
|
209 | + */ |
|
210 | + public function changeLock($path, $targetType) { |
|
211 | + $expire = $this->getExpireTime(); |
|
212 | + if ($targetType === self::LOCK_SHARED) { |
|
213 | + $result = $this->connection->executeUpdate( |
|
214 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = 1, `ttl` = ? WHERE `key` = ? AND `lock` = -1', |
|
215 | + [$expire, $path] |
|
216 | + ); |
|
217 | + } else { |
|
218 | + // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually |
|
219 | + if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) { |
|
220 | + throw new LockedException($path); |
|
221 | + } |
|
222 | + $result = $this->connection->executeUpdate( |
|
223 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = -1, `ttl` = ? WHERE `key` = ? AND `lock` = 1', |
|
224 | + [$expire, $path] |
|
225 | + ); |
|
226 | + } |
|
227 | + if ($result !== 1) { |
|
228 | + throw new LockedException($path); |
|
229 | + } |
|
230 | + $this->markChange($path, $targetType); |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * cleanup empty locks |
|
235 | - */ |
|
236 | - public function cleanExpiredLocks() { |
|
237 | - $expire = $this->timeFactory->getTime(); |
|
238 | - try { |
|
239 | - $this->connection->executeUpdate( |
|
240 | - 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
241 | - [$expire] |
|
242 | - ); |
|
243 | - } catch (\Exception $e) { |
|
244 | - // If the table is missing, the clean up was successful |
|
245 | - if ($this->connection->tableExists('file_locks')) { |
|
246 | - throw $e; |
|
247 | - } |
|
248 | - } |
|
249 | - } |
|
233 | + /** |
|
234 | + * cleanup empty locks |
|
235 | + */ |
|
236 | + public function cleanExpiredLocks() { |
|
237 | + $expire = $this->timeFactory->getTime(); |
|
238 | + try { |
|
239 | + $this->connection->executeUpdate( |
|
240 | + 'DELETE FROM `*PREFIX*file_locks` WHERE `ttl` < ?', |
|
241 | + [$expire] |
|
242 | + ); |
|
243 | + } catch (\Exception $e) { |
|
244 | + // If the table is missing, the clean up was successful |
|
245 | + if ($this->connection->tableExists('file_locks')) { |
|
246 | + throw $e; |
|
247 | + } |
|
248 | + } |
|
249 | + } |
|
250 | 250 | |
251 | - /** |
|
252 | - * release all lock acquired by this instance which were marked using the mark* methods |
|
253 | - */ |
|
254 | - public function releaseAll() { |
|
255 | - parent::releaseAll(); |
|
251 | + /** |
|
252 | + * release all lock acquired by this instance which were marked using the mark* methods |
|
253 | + */ |
|
254 | + public function releaseAll() { |
|
255 | + parent::releaseAll(); |
|
256 | 256 | |
257 | - // since we keep shared locks we need to manually clean those |
|
258 | - foreach ($this->sharedLocks as $path => $lock) { |
|
259 | - if ($lock) { |
|
260 | - $this->connection->executeUpdate( |
|
261 | - 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` - 1 WHERE `key` = ? AND `lock` > 0', |
|
262 | - [$path] |
|
263 | - ); |
|
264 | - } |
|
265 | - } |
|
266 | - } |
|
257 | + // since we keep shared locks we need to manually clean those |
|
258 | + foreach ($this->sharedLocks as $path => $lock) { |
|
259 | + if ($lock) { |
|
260 | + $this->connection->executeUpdate( |
|
261 | + 'UPDATE `*PREFIX*file_locks` SET `lock` = `lock` - 1 WHERE `key` = ? AND `lock` > 0', |
|
262 | + [$path] |
|
263 | + ); |
|
264 | + } |
|
265 | + } |
|
266 | + } |
|
267 | 267 | } |
@@ -131,7 +131,7 @@ |
||
131 | 131 | } |
132 | 132 | $query = $this->connection->prepare('SELECT `lock` from `*PREFIX*file_locks` WHERE `key` = ?'); |
133 | 133 | $query->execute([$path]); |
134 | - $lockValue = (int)$query->fetchColumn(); |
|
134 | + $lockValue = (int) $query->fetchColumn(); |
|
135 | 135 | if ($type === self::LOCK_SHARED) { |
136 | 136 | if ($this->isLocallyLocked($path)) { |
137 | 137 | // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db |
@@ -28,53 +28,53 @@ |
||
28 | 28 | |
29 | 29 | abstract class Session implements \ArrayAccess, ISession { |
30 | 30 | |
31 | - /** |
|
32 | - * @var bool |
|
33 | - */ |
|
34 | - protected $sessionClosed = false; |
|
31 | + /** |
|
32 | + * @var bool |
|
33 | + */ |
|
34 | + protected $sessionClosed = false; |
|
35 | 35 | |
36 | - /** |
|
37 | - * $name serves as a namespace for the session keys |
|
38 | - * |
|
39 | - * @param string $name |
|
40 | - */ |
|
41 | - abstract public function __construct($name); |
|
36 | + /** |
|
37 | + * $name serves as a namespace for the session keys |
|
38 | + * |
|
39 | + * @param string $name |
|
40 | + */ |
|
41 | + abstract public function __construct($name); |
|
42 | 42 | |
43 | - /** |
|
44 | - * @param mixed $offset |
|
45 | - * @return bool |
|
46 | - */ |
|
47 | - public function offsetExists($offset) { |
|
48 | - return $this->exists($offset); |
|
49 | - } |
|
43 | + /** |
|
44 | + * @param mixed $offset |
|
45 | + * @return bool |
|
46 | + */ |
|
47 | + public function offsetExists($offset) { |
|
48 | + return $this->exists($offset); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @param mixed $offset |
|
53 | - * @return mixed |
|
54 | - */ |
|
55 | - public function offsetGet($offset) { |
|
56 | - return $this->get($offset); |
|
57 | - } |
|
51 | + /** |
|
52 | + * @param mixed $offset |
|
53 | + * @return mixed |
|
54 | + */ |
|
55 | + public function offsetGet($offset) { |
|
56 | + return $this->get($offset); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @param mixed $offset |
|
61 | - * @param mixed $value |
|
62 | - */ |
|
63 | - public function offsetSet($offset, $value) { |
|
64 | - $this->set($offset, $value); |
|
65 | - } |
|
59 | + /** |
|
60 | + * @param mixed $offset |
|
61 | + * @param mixed $value |
|
62 | + */ |
|
63 | + public function offsetSet($offset, $value) { |
|
64 | + $this->set($offset, $value); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param mixed $offset |
|
69 | - */ |
|
70 | - public function offsetUnset($offset) { |
|
71 | - $this->remove($offset); |
|
72 | - } |
|
67 | + /** |
|
68 | + * @param mixed $offset |
|
69 | + */ |
|
70 | + public function offsetUnset($offset) { |
|
71 | + $this->remove($offset); |
|
72 | + } |
|
73 | 73 | |
74 | - /** |
|
75 | - * Close the session and release the lock |
|
76 | - */ |
|
77 | - public function close() { |
|
78 | - $this->sessionClosed = true; |
|
79 | - } |
|
74 | + /** |
|
75 | + * Close the session and release the lock |
|
76 | + */ |
|
77 | + public function close() { |
|
78 | + $this->sessionClosed = true; |
|
79 | + } |
|
80 | 80 | } |
@@ -48,56 +48,56 @@ |
||
48 | 48 | * @package OC\Session |
49 | 49 | */ |
50 | 50 | class CryptoWrapper { |
51 | - const COOKIE_NAME = 'oc_sessionPassphrase'; |
|
51 | + const COOKIE_NAME = 'oc_sessionPassphrase'; |
|
52 | 52 | |
53 | - /** @var ISession */ |
|
54 | - protected $session; |
|
53 | + /** @var ISession */ |
|
54 | + protected $session; |
|
55 | 55 | |
56 | - /** @var \OCP\Security\ICrypto */ |
|
57 | - protected $crypto; |
|
56 | + /** @var \OCP\Security\ICrypto */ |
|
57 | + protected $crypto; |
|
58 | 58 | |
59 | - /** @var ISecureRandom */ |
|
60 | - protected $random; |
|
59 | + /** @var ISecureRandom */ |
|
60 | + protected $random; |
|
61 | 61 | |
62 | - /** |
|
63 | - * @param IConfig $config |
|
64 | - * @param ICrypto $crypto |
|
65 | - * @param ISecureRandom $random |
|
66 | - * @param IRequest $request |
|
67 | - */ |
|
68 | - public function __construct(IConfig $config, |
|
69 | - ICrypto $crypto, |
|
70 | - ISecureRandom $random, |
|
71 | - IRequest $request) { |
|
72 | - $this->crypto = $crypto; |
|
73 | - $this->config = $config; |
|
74 | - $this->random = $random; |
|
62 | + /** |
|
63 | + * @param IConfig $config |
|
64 | + * @param ICrypto $crypto |
|
65 | + * @param ISecureRandom $random |
|
66 | + * @param IRequest $request |
|
67 | + */ |
|
68 | + public function __construct(IConfig $config, |
|
69 | + ICrypto $crypto, |
|
70 | + ISecureRandom $random, |
|
71 | + IRequest $request) { |
|
72 | + $this->crypto = $crypto; |
|
73 | + $this->config = $config; |
|
74 | + $this->random = $random; |
|
75 | 75 | |
76 | - if (!is_null($request->getCookie(self::COOKIE_NAME))) { |
|
77 | - $this->passphrase = $request->getCookie(self::COOKIE_NAME); |
|
78 | - } else { |
|
79 | - $this->passphrase = $this->random->generate(128); |
|
80 | - $secureCookie = $request->getServerProtocol() === 'https'; |
|
81 | - // FIXME: Required for CI |
|
82 | - if (!defined('PHPUNIT_RUN')) { |
|
83 | - $webRoot = \OC::$WEBROOT; |
|
84 | - if($webRoot === '') { |
|
85 | - $webRoot = '/'; |
|
86 | - } |
|
87 | - setcookie(self::COOKIE_NAME, $this->passphrase, 0, $webRoot, '', $secureCookie, true); |
|
88 | - } |
|
89 | - } |
|
90 | - } |
|
76 | + if (!is_null($request->getCookie(self::COOKIE_NAME))) { |
|
77 | + $this->passphrase = $request->getCookie(self::COOKIE_NAME); |
|
78 | + } else { |
|
79 | + $this->passphrase = $this->random->generate(128); |
|
80 | + $secureCookie = $request->getServerProtocol() === 'https'; |
|
81 | + // FIXME: Required for CI |
|
82 | + if (!defined('PHPUNIT_RUN')) { |
|
83 | + $webRoot = \OC::$WEBROOT; |
|
84 | + if($webRoot === '') { |
|
85 | + $webRoot = '/'; |
|
86 | + } |
|
87 | + setcookie(self::COOKIE_NAME, $this->passphrase, 0, $webRoot, '', $secureCookie, true); |
|
88 | + } |
|
89 | + } |
|
90 | + } |
|
91 | 91 | |
92 | - /** |
|
93 | - * @param ISession $session |
|
94 | - * @return ISession |
|
95 | - */ |
|
96 | - public function wrapSession(ISession $session) { |
|
97 | - if (!($session instanceof CryptoSessionData)) { |
|
98 | - return new CryptoSessionData($session, $this->crypto, $this->passphrase); |
|
99 | - } |
|
92 | + /** |
|
93 | + * @param ISession $session |
|
94 | + * @return ISession |
|
95 | + */ |
|
96 | + public function wrapSession(ISession $session) { |
|
97 | + if (!($session instanceof CryptoSessionData)) { |
|
98 | + return new CryptoSessionData($session, $this->crypto, $this->passphrase); |
|
99 | + } |
|
100 | 100 | |
101 | - return $session; |
|
102 | - } |
|
101 | + return $session; |
|
102 | + } |
|
103 | 103 | } |
@@ -81,7 +81,7 @@ |
||
81 | 81 | // FIXME: Required for CI |
82 | 82 | if (!defined('PHPUNIT_RUN')) { |
83 | 83 | $webRoot = \OC::$WEBROOT; |
84 | - if($webRoot === '') { |
|
84 | + if ($webRoot === '') { |
|
85 | 85 | $webRoot = '/'; |
86 | 86 | } |
87 | 87 | setcookie(self::COOKIE_NAME, $this->passphrase, 0, $webRoot, '', $secureCookie, true); |
@@ -35,105 +35,105 @@ |
||
35 | 35 | * @package OC\Session |
36 | 36 | */ |
37 | 37 | class Internal extends Session { |
38 | - /** |
|
39 | - * @param string $name |
|
40 | - * @throws \Exception |
|
41 | - */ |
|
42 | - public function __construct($name) { |
|
43 | - session_name($name); |
|
44 | - set_error_handler(array($this, 'trapError')); |
|
45 | - try { |
|
46 | - session_start(); |
|
47 | - } catch (\Exception $e) { |
|
48 | - setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/'); |
|
49 | - } |
|
50 | - restore_error_handler(); |
|
51 | - if (!isset($_SESSION)) { |
|
52 | - throw new \Exception('Failed to start session'); |
|
53 | - } |
|
54 | - } |
|
38 | + /** |
|
39 | + * @param string $name |
|
40 | + * @throws \Exception |
|
41 | + */ |
|
42 | + public function __construct($name) { |
|
43 | + session_name($name); |
|
44 | + set_error_handler(array($this, 'trapError')); |
|
45 | + try { |
|
46 | + session_start(); |
|
47 | + } catch (\Exception $e) { |
|
48 | + setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/'); |
|
49 | + } |
|
50 | + restore_error_handler(); |
|
51 | + if (!isset($_SESSION)) { |
|
52 | + throw new \Exception('Failed to start session'); |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param string $key |
|
58 | - * @param integer $value |
|
59 | - */ |
|
60 | - public function set($key, $value) { |
|
61 | - $this->validateSession(); |
|
62 | - $_SESSION[$key] = $value; |
|
63 | - } |
|
56 | + /** |
|
57 | + * @param string $key |
|
58 | + * @param integer $value |
|
59 | + */ |
|
60 | + public function set($key, $value) { |
|
61 | + $this->validateSession(); |
|
62 | + $_SESSION[$key] = $value; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param string $key |
|
67 | - * @return mixed |
|
68 | - */ |
|
69 | - public function get($key) { |
|
70 | - if (!$this->exists($key)) { |
|
71 | - return null; |
|
72 | - } |
|
73 | - return $_SESSION[$key]; |
|
74 | - } |
|
65 | + /** |
|
66 | + * @param string $key |
|
67 | + * @return mixed |
|
68 | + */ |
|
69 | + public function get($key) { |
|
70 | + if (!$this->exists($key)) { |
|
71 | + return null; |
|
72 | + } |
|
73 | + return $_SESSION[$key]; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param string $key |
|
78 | - * @return bool |
|
79 | - */ |
|
80 | - public function exists($key) { |
|
81 | - return isset($_SESSION[$key]); |
|
82 | - } |
|
76 | + /** |
|
77 | + * @param string $key |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | + public function exists($key) { |
|
81 | + return isset($_SESSION[$key]); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @param string $key |
|
86 | - */ |
|
87 | - public function remove($key) { |
|
88 | - if (isset($_SESSION[$key])) { |
|
89 | - unset($_SESSION[$key]); |
|
90 | - } |
|
91 | - } |
|
84 | + /** |
|
85 | + * @param string $key |
|
86 | + */ |
|
87 | + public function remove($key) { |
|
88 | + if (isset($_SESSION[$key])) { |
|
89 | + unset($_SESSION[$key]); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - public function clear() { |
|
94 | - session_unset(); |
|
95 | - $this->regenerateId(); |
|
96 | - @session_start(); |
|
97 | - $_SESSION = array(); |
|
98 | - } |
|
93 | + public function clear() { |
|
94 | + session_unset(); |
|
95 | + $this->regenerateId(); |
|
96 | + @session_start(); |
|
97 | + $_SESSION = array(); |
|
98 | + } |
|
99 | 99 | |
100 | - public function close() { |
|
101 | - session_write_close(); |
|
102 | - parent::close(); |
|
103 | - } |
|
100 | + public function close() { |
|
101 | + session_write_close(); |
|
102 | + parent::close(); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * Wrapper around session_regenerate_id |
|
107 | - * |
|
108 | - * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function regenerateId($deleteOldSession = true) { |
|
112 | - @session_regenerate_id($deleteOldSession); |
|
113 | - } |
|
105 | + /** |
|
106 | + * Wrapper around session_regenerate_id |
|
107 | + * |
|
108 | + * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function regenerateId($deleteOldSession = true) { |
|
112 | + @session_regenerate_id($deleteOldSession); |
|
113 | + } |
|
114 | 114 | |
115 | - /** |
|
116 | - * @throws \Exception |
|
117 | - */ |
|
118 | - public function reopen() { |
|
119 | - throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.'); |
|
120 | - } |
|
115 | + /** |
|
116 | + * @throws \Exception |
|
117 | + */ |
|
118 | + public function reopen() { |
|
119 | + throw new \Exception('The session cannot be reopened - reopen() is ony to be used in unit testing.'); |
|
120 | + } |
|
121 | 121 | |
122 | - /** |
|
123 | - * @param int $errorNumber |
|
124 | - * @param string $errorString |
|
125 | - * @throws \ErrorException |
|
126 | - */ |
|
127 | - public function trapError($errorNumber, $errorString) { |
|
128 | - throw new \ErrorException($errorString); |
|
129 | - } |
|
122 | + /** |
|
123 | + * @param int $errorNumber |
|
124 | + * @param string $errorString |
|
125 | + * @throws \ErrorException |
|
126 | + */ |
|
127 | + public function trapError($errorNumber, $errorString) { |
|
128 | + throw new \ErrorException($errorString); |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * @throws \Exception |
|
133 | - */ |
|
134 | - private function validateSession() { |
|
135 | - if ($this->sessionClosed) { |
|
136 | - throw new \Exception('Session has been closed - no further changes to the session are allowed'); |
|
137 | - } |
|
138 | - } |
|
131 | + /** |
|
132 | + * @throws \Exception |
|
133 | + */ |
|
134 | + private function validateSession() { |
|
135 | + if ($this->sessionClosed) { |
|
136 | + throw new \Exception('Session has been closed - no further changes to the session are allowed'); |
|
137 | + } |
|
138 | + } |
|
139 | 139 | } |
@@ -48,7 +48,7 @@ |
||
48 | 48 | try { |
49 | 49 | session_start(); |
50 | 50 | } catch (\Exception $e) { |
51 | - setcookie(session_name(), null, -1, \OC::$WEBROOT ? : '/'); |
|
51 | + setcookie(session_name(), null, -1, \OC::$WEBROOT ?: '/'); |
|
52 | 52 | } |
53 | 53 | restore_error_handler(); |
54 | 54 | if (!isset($_SESSION)) { |
@@ -35,75 +35,75 @@ |
||
35 | 35 | * @package OC\Session |
36 | 36 | */ |
37 | 37 | class Memory extends Session { |
38 | - protected $data; |
|
38 | + protected $data; |
|
39 | 39 | |
40 | - public function __construct($name) { |
|
41 | - //no need to use $name since all data is already scoped to this instance |
|
42 | - $this->data = array(); |
|
43 | - } |
|
40 | + public function __construct($name) { |
|
41 | + //no need to use $name since all data is already scoped to this instance |
|
42 | + $this->data = array(); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param string $key |
|
47 | - * @param integer $value |
|
48 | - */ |
|
49 | - public function set($key, $value) { |
|
50 | - $this->validateSession(); |
|
51 | - $this->data[$key] = $value; |
|
52 | - } |
|
45 | + /** |
|
46 | + * @param string $key |
|
47 | + * @param integer $value |
|
48 | + */ |
|
49 | + public function set($key, $value) { |
|
50 | + $this->validateSession(); |
|
51 | + $this->data[$key] = $value; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @param string $key |
|
56 | - * @return mixed |
|
57 | - */ |
|
58 | - public function get($key) { |
|
59 | - if (!$this->exists($key)) { |
|
60 | - return null; |
|
61 | - } |
|
62 | - return $this->data[$key]; |
|
63 | - } |
|
54 | + /** |
|
55 | + * @param string $key |
|
56 | + * @return mixed |
|
57 | + */ |
|
58 | + public function get($key) { |
|
59 | + if (!$this->exists($key)) { |
|
60 | + return null; |
|
61 | + } |
|
62 | + return $this->data[$key]; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * @param string $key |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function exists($key) { |
|
70 | - return isset($this->data[$key]); |
|
71 | - } |
|
65 | + /** |
|
66 | + * @param string $key |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function exists($key) { |
|
70 | + return isset($this->data[$key]); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * @param string $key |
|
75 | - */ |
|
76 | - public function remove($key) { |
|
77 | - $this->validateSession(); |
|
78 | - unset($this->data[$key]); |
|
79 | - } |
|
73 | + /** |
|
74 | + * @param string $key |
|
75 | + */ |
|
76 | + public function remove($key) { |
|
77 | + $this->validateSession(); |
|
78 | + unset($this->data[$key]); |
|
79 | + } |
|
80 | 80 | |
81 | - public function clear() { |
|
82 | - $this->data = array(); |
|
83 | - } |
|
81 | + public function clear() { |
|
82 | + $this->data = array(); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Stub since the session ID does not need to get regenerated for the cache |
|
87 | - * |
|
88 | - * @param bool $deleteOldSession |
|
89 | - */ |
|
90 | - public function regenerateId($deleteOldSession = true) {} |
|
85 | + /** |
|
86 | + * Stub since the session ID does not need to get regenerated for the cache |
|
87 | + * |
|
88 | + * @param bool $deleteOldSession |
|
89 | + */ |
|
90 | + public function regenerateId($deleteOldSession = true) {} |
|
91 | 91 | |
92 | - /** |
|
93 | - * Helper function for PHPUnit execution - don't use in non-test code |
|
94 | - */ |
|
95 | - public function reopen() { |
|
96 | - $this->sessionClosed = false; |
|
97 | - } |
|
92 | + /** |
|
93 | + * Helper function for PHPUnit execution - don't use in non-test code |
|
94 | + */ |
|
95 | + public function reopen() { |
|
96 | + $this->sessionClosed = false; |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * In case the session has already been locked an exception will be thrown |
|
101 | - * |
|
102 | - * @throws \Exception |
|
103 | - */ |
|
104 | - private function validateSession() { |
|
105 | - if ($this->sessionClosed) { |
|
106 | - throw new \Exception('Session has been closed - no further changes to the session are allowed'); |
|
107 | - } |
|
108 | - } |
|
99 | + /** |
|
100 | + * In case the session has already been locked an exception will be thrown |
|
101 | + * |
|
102 | + * @throws \Exception |
|
103 | + */ |
|
104 | + private function validateSession() { |
|
105 | + if ($this->sessionClosed) { |
|
106 | + throw new \Exception('Session has been closed - no further changes to the session are allowed'); |
|
107 | + } |
|
108 | + } |
|
109 | 109 | } |
@@ -46,251 +46,251 @@ |
||
46 | 46 | |
47 | 47 | class TemplateLayout extends \OC_Template { |
48 | 48 | |
49 | - private static $versionHash = ''; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var \OCP\IConfig |
|
53 | - */ |
|
54 | - private $config; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $renderAs |
|
58 | - * @param string $appId application id |
|
59 | - */ |
|
60 | - public function __construct( $renderAs, $appId = '' ) { |
|
61 | - |
|
62 | - // yes - should be injected .... |
|
63 | - $this->config = \OC::$server->getConfig(); |
|
64 | - |
|
65 | - // Decide which page we show |
|
66 | - if($renderAs == 'user') { |
|
67 | - parent::__construct( 'core', 'layout.user' ); |
|
68 | - if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
|
69 | - $this->assign('bodyid', 'body-settings'); |
|
70 | - }else{ |
|
71 | - $this->assign('bodyid', 'body-user'); |
|
72 | - } |
|
73 | - |
|
74 | - // Code integrity notification |
|
75 | - $integrityChecker = \OC::$server->getIntegrityCodeChecker(); |
|
76 | - if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) { |
|
77 | - \OCP\Util::addScript('core', 'integritycheck-failed-notification'); |
|
78 | - } |
|
79 | - |
|
80 | - // Add navigation entry |
|
81 | - $this->assign( 'application', ''); |
|
82 | - $this->assign( 'appid', $appId ); |
|
83 | - $navigation = \OC_App::getNavigation(); |
|
84 | - $this->assign( 'navigation', $navigation); |
|
85 | - $settingsNavigation = \OC_App::getSettingsNavigation(); |
|
86 | - $this->assign( 'settingsnavigation', $settingsNavigation); |
|
87 | - foreach($navigation as $entry) { |
|
88 | - if ($entry['active']) { |
|
89 | - $this->assign( 'application', $entry['name'] ); |
|
90 | - break; |
|
91 | - } |
|
92 | - } |
|
49 | + private static $versionHash = ''; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var \OCP\IConfig |
|
53 | + */ |
|
54 | + private $config; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $renderAs |
|
58 | + * @param string $appId application id |
|
59 | + */ |
|
60 | + public function __construct( $renderAs, $appId = '' ) { |
|
61 | + |
|
62 | + // yes - should be injected .... |
|
63 | + $this->config = \OC::$server->getConfig(); |
|
64 | + |
|
65 | + // Decide which page we show |
|
66 | + if($renderAs == 'user') { |
|
67 | + parent::__construct( 'core', 'layout.user' ); |
|
68 | + if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
|
69 | + $this->assign('bodyid', 'body-settings'); |
|
70 | + }else{ |
|
71 | + $this->assign('bodyid', 'body-user'); |
|
72 | + } |
|
73 | + |
|
74 | + // Code integrity notification |
|
75 | + $integrityChecker = \OC::$server->getIntegrityCodeChecker(); |
|
76 | + if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) { |
|
77 | + \OCP\Util::addScript('core', 'integritycheck-failed-notification'); |
|
78 | + } |
|
79 | + |
|
80 | + // Add navigation entry |
|
81 | + $this->assign( 'application', ''); |
|
82 | + $this->assign( 'appid', $appId ); |
|
83 | + $navigation = \OC_App::getNavigation(); |
|
84 | + $this->assign( 'navigation', $navigation); |
|
85 | + $settingsNavigation = \OC_App::getSettingsNavigation(); |
|
86 | + $this->assign( 'settingsnavigation', $settingsNavigation); |
|
87 | + foreach($navigation as $entry) { |
|
88 | + if ($entry['active']) { |
|
89 | + $this->assign( 'application', $entry['name'] ); |
|
90 | + break; |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - foreach($settingsNavigation as $entry) { |
|
95 | - if ($entry['active']) { |
|
96 | - $this->assign( 'application', $entry['name'] ); |
|
97 | - break; |
|
98 | - } |
|
99 | - } |
|
100 | - $userDisplayName = \OC_User::getDisplayName(); |
|
101 | - $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0; |
|
102 | - if ($appsMgmtActive) { |
|
103 | - $l = \OC::$server->getL10N('lib'); |
|
104 | - $this->assign('application', $l->t('Apps')); |
|
105 | - } |
|
106 | - $this->assign('user_displayname', $userDisplayName); |
|
107 | - $this->assign('user_uid', \OC_User::getUser()); |
|
108 | - $this->assign('appsmanagement_active', $appsMgmtActive); |
|
109 | - $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true); |
|
110 | - |
|
111 | - if (\OC_User::getUser() === false) { |
|
112 | - $this->assign('userAvatarSet', false); |
|
113 | - } else { |
|
114 | - $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists()); |
|
115 | - } |
|
116 | - |
|
117 | - } else if ($renderAs == 'error') { |
|
118 | - parent::__construct('core', 'layout.guest', '', false); |
|
119 | - $this->assign('bodyid', 'body-login'); |
|
120 | - } else if ($renderAs == 'guest') { |
|
121 | - parent::__construct('core', 'layout.guest'); |
|
122 | - $this->assign('bodyid', 'body-login'); |
|
123 | - } else { |
|
124 | - parent::__construct('core', 'layout.base'); |
|
125 | - |
|
126 | - } |
|
127 | - // Send the language to our layouts |
|
128 | - $this->assign('language', \OC_L10N::findLanguage()); |
|
129 | - |
|
130 | - if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
131 | - if (empty(self::$versionHash)) { |
|
132 | - $v = \OC_App::getAppVersions(); |
|
133 | - $v['core'] = implode('.', \OCP\Util::getVersion()); |
|
134 | - self::$versionHash = md5(implode(',', $v)); |
|
135 | - } |
|
136 | - } else { |
|
137 | - self::$versionHash = md5('not installed'); |
|
138 | - } |
|
139 | - |
|
140 | - $useAssetPipeline = self::isAssetPipelineEnabled(); |
|
141 | - if ($useAssetPipeline) { |
|
142 | - $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
143 | - $this->generateAssets(); |
|
144 | - } else { |
|
145 | - // Add the js files |
|
146 | - $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
|
147 | - $this->assign('jsfiles', array()); |
|
148 | - if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
|
149 | - $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
150 | - } |
|
151 | - foreach($jsFiles as $info) { |
|
152 | - $web = $info[1]; |
|
153 | - $file = $info[2]; |
|
154 | - $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
|
155 | - } |
|
156 | - |
|
157 | - // Add the css files |
|
158 | - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
|
159 | - $this->assign('cssfiles', array()); |
|
160 | - foreach($cssFiles as $info) { |
|
161 | - $web = $info[1]; |
|
162 | - $file = $info[2]; |
|
163 | - |
|
164 | - $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
|
165 | - } |
|
166 | - } |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param array $styles |
|
171 | - * @return array |
|
172 | - */ |
|
173 | - static public function findStylesheetFiles($styles) { |
|
174 | - // Read the selected theme from the config file |
|
175 | - $theme = \OC_Util::getTheme(); |
|
176 | - |
|
177 | - $locator = new \OC\Template\CSSResourceLocator( |
|
178 | - \OC::$server->getLogger(), |
|
179 | - $theme, |
|
180 | - array( \OC::$SERVERROOT => \OC::$WEBROOT ), |
|
181 | - array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT )); |
|
182 | - $locator->find($styles); |
|
183 | - return $locator->getResources(); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @param array $scripts |
|
188 | - * @return array |
|
189 | - */ |
|
190 | - static public function findJavascriptFiles($scripts) { |
|
191 | - // Read the selected theme from the config file |
|
192 | - $theme = \OC_Util::getTheme(); |
|
193 | - |
|
194 | - $locator = new \OC\Template\JSResourceLocator( |
|
195 | - \OC::$server->getLogger(), |
|
196 | - $theme, |
|
197 | - array( \OC::$SERVERROOT => \OC::$WEBROOT ), |
|
198 | - array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT )); |
|
199 | - $locator->find($scripts); |
|
200 | - return $locator->getResources(); |
|
201 | - } |
|
202 | - |
|
203 | - public function generateAssets() { |
|
204 | - $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT); |
|
205 | - $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
|
206 | - $jsHash = self::hashFileNames($jsFiles); |
|
207 | - |
|
208 | - if (!file_exists("$assetDir/assets/$jsHash.js")) { |
|
209 | - $jsFiles = array_map(function ($item) { |
|
210 | - $root = $item[0]; |
|
211 | - $file = $item[2]; |
|
212 | - // no need to minifiy minified files |
|
213 | - if (substr($file, -strlen('.min.js')) === '.min.js') { |
|
214 | - return new FileAsset($root . '/' . $file, array( |
|
215 | - new SeparatorFilter(';') |
|
216 | - ), $root, $file); |
|
217 | - } |
|
218 | - return new FileAsset($root . '/' . $file, array( |
|
219 | - new JSqueezeFilter(), |
|
220 | - new SeparatorFilter(';') |
|
221 | - ), $root, $file); |
|
222 | - }, $jsFiles); |
|
223 | - $jsCollection = new AssetCollection($jsFiles); |
|
224 | - $jsCollection->setTargetPath("assets/$jsHash.js"); |
|
225 | - |
|
226 | - $writer = new AssetWriter($assetDir); |
|
227 | - $writer->writeAsset($jsCollection); |
|
228 | - } |
|
229 | - |
|
230 | - $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
|
231 | - $cssHash = self::hashFileNames($cssFiles); |
|
232 | - |
|
233 | - if (!file_exists("$assetDir/assets/$cssHash.css")) { |
|
234 | - $cssFiles = array_map(function ($item) { |
|
235 | - $root = $item[0]; |
|
236 | - $file = $item[2]; |
|
237 | - $assetPath = $root . '/' . $file; |
|
238 | - $sourceRoot = \OC::$SERVERROOT; |
|
239 | - $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT)); |
|
240 | - return new FileAsset( |
|
241 | - $assetPath, |
|
242 | - array( |
|
243 | - new CssRewriteFilter(), |
|
244 | - new CssMinFilter(), |
|
245 | - new CssImportFilter() |
|
246 | - ), |
|
247 | - $sourceRoot, |
|
248 | - $sourcePath |
|
249 | - ); |
|
250 | - }, $cssFiles); |
|
251 | - $cssCollection = new AssetCollection($cssFiles); |
|
252 | - $cssCollection->setTargetPath("assets/$cssHash.css"); |
|
253 | - |
|
254 | - $writer = new AssetWriter($assetDir); |
|
255 | - $writer->writeAsset($cssCollection); |
|
256 | - } |
|
257 | - |
|
258 | - $this->append('jsfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$jsHash.js")); |
|
259 | - $this->append('cssfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$cssHash.css")); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Converts the absolute file path to a relative path from \OC::$SERVERROOT |
|
264 | - * @param string $filePath Absolute path |
|
265 | - * @return string Relative path |
|
266 | - * @throws \Exception If $filePath is not under \OC::$SERVERROOT |
|
267 | - */ |
|
268 | - public static function convertToRelativePath($filePath) { |
|
269 | - $relativePath = explode(\OC::$SERVERROOT, $filePath); |
|
270 | - if(count($relativePath) !== 2) { |
|
271 | - throw new \Exception('$filePath is not under the \OC::$SERVERROOT'); |
|
272 | - } |
|
273 | - |
|
274 | - return $relativePath[1]; |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * @param array $files |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - |
|
282 | - private static function hashFileNames($files) { |
|
283 | - foreach($files as $i => $file) { |
|
284 | - try { |
|
285 | - $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2]; |
|
286 | - } catch (\Exception $e) { |
|
287 | - $files[$i] = $file[0].'/'.$file[2]; |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - sort($files); |
|
292 | - // include the apps' versions hash to invalidate the cached assets |
|
293 | - $files[] = self::$versionHash; |
|
294 | - return hash('md5', implode('', $files)); |
|
295 | - } |
|
94 | + foreach($settingsNavigation as $entry) { |
|
95 | + if ($entry['active']) { |
|
96 | + $this->assign( 'application', $entry['name'] ); |
|
97 | + break; |
|
98 | + } |
|
99 | + } |
|
100 | + $userDisplayName = \OC_User::getDisplayName(); |
|
101 | + $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0; |
|
102 | + if ($appsMgmtActive) { |
|
103 | + $l = \OC::$server->getL10N('lib'); |
|
104 | + $this->assign('application', $l->t('Apps')); |
|
105 | + } |
|
106 | + $this->assign('user_displayname', $userDisplayName); |
|
107 | + $this->assign('user_uid', \OC_User::getUser()); |
|
108 | + $this->assign('appsmanagement_active', $appsMgmtActive); |
|
109 | + $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true); |
|
110 | + |
|
111 | + if (\OC_User::getUser() === false) { |
|
112 | + $this->assign('userAvatarSet', false); |
|
113 | + } else { |
|
114 | + $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists()); |
|
115 | + } |
|
116 | + |
|
117 | + } else if ($renderAs == 'error') { |
|
118 | + parent::__construct('core', 'layout.guest', '', false); |
|
119 | + $this->assign('bodyid', 'body-login'); |
|
120 | + } else if ($renderAs == 'guest') { |
|
121 | + parent::__construct('core', 'layout.guest'); |
|
122 | + $this->assign('bodyid', 'body-login'); |
|
123 | + } else { |
|
124 | + parent::__construct('core', 'layout.base'); |
|
125 | + |
|
126 | + } |
|
127 | + // Send the language to our layouts |
|
128 | + $this->assign('language', \OC_L10N::findLanguage()); |
|
129 | + |
|
130 | + if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
131 | + if (empty(self::$versionHash)) { |
|
132 | + $v = \OC_App::getAppVersions(); |
|
133 | + $v['core'] = implode('.', \OCP\Util::getVersion()); |
|
134 | + self::$versionHash = md5(implode(',', $v)); |
|
135 | + } |
|
136 | + } else { |
|
137 | + self::$versionHash = md5('not installed'); |
|
138 | + } |
|
139 | + |
|
140 | + $useAssetPipeline = self::isAssetPipelineEnabled(); |
|
141 | + if ($useAssetPipeline) { |
|
142 | + $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
143 | + $this->generateAssets(); |
|
144 | + } else { |
|
145 | + // Add the js files |
|
146 | + $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
|
147 | + $this->assign('jsfiles', array()); |
|
148 | + if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
|
149 | + $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
150 | + } |
|
151 | + foreach($jsFiles as $info) { |
|
152 | + $web = $info[1]; |
|
153 | + $file = $info[2]; |
|
154 | + $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
|
155 | + } |
|
156 | + |
|
157 | + // Add the css files |
|
158 | + $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
|
159 | + $this->assign('cssfiles', array()); |
|
160 | + foreach($cssFiles as $info) { |
|
161 | + $web = $info[1]; |
|
162 | + $file = $info[2]; |
|
163 | + |
|
164 | + $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
|
165 | + } |
|
166 | + } |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param array $styles |
|
171 | + * @return array |
|
172 | + */ |
|
173 | + static public function findStylesheetFiles($styles) { |
|
174 | + // Read the selected theme from the config file |
|
175 | + $theme = \OC_Util::getTheme(); |
|
176 | + |
|
177 | + $locator = new \OC\Template\CSSResourceLocator( |
|
178 | + \OC::$server->getLogger(), |
|
179 | + $theme, |
|
180 | + array( \OC::$SERVERROOT => \OC::$WEBROOT ), |
|
181 | + array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT )); |
|
182 | + $locator->find($styles); |
|
183 | + return $locator->getResources(); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @param array $scripts |
|
188 | + * @return array |
|
189 | + */ |
|
190 | + static public function findJavascriptFiles($scripts) { |
|
191 | + // Read the selected theme from the config file |
|
192 | + $theme = \OC_Util::getTheme(); |
|
193 | + |
|
194 | + $locator = new \OC\Template\JSResourceLocator( |
|
195 | + \OC::$server->getLogger(), |
|
196 | + $theme, |
|
197 | + array( \OC::$SERVERROOT => \OC::$WEBROOT ), |
|
198 | + array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT )); |
|
199 | + $locator->find($scripts); |
|
200 | + return $locator->getResources(); |
|
201 | + } |
|
202 | + |
|
203 | + public function generateAssets() { |
|
204 | + $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT); |
|
205 | + $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
|
206 | + $jsHash = self::hashFileNames($jsFiles); |
|
207 | + |
|
208 | + if (!file_exists("$assetDir/assets/$jsHash.js")) { |
|
209 | + $jsFiles = array_map(function ($item) { |
|
210 | + $root = $item[0]; |
|
211 | + $file = $item[2]; |
|
212 | + // no need to minifiy minified files |
|
213 | + if (substr($file, -strlen('.min.js')) === '.min.js') { |
|
214 | + return new FileAsset($root . '/' . $file, array( |
|
215 | + new SeparatorFilter(';') |
|
216 | + ), $root, $file); |
|
217 | + } |
|
218 | + return new FileAsset($root . '/' . $file, array( |
|
219 | + new JSqueezeFilter(), |
|
220 | + new SeparatorFilter(';') |
|
221 | + ), $root, $file); |
|
222 | + }, $jsFiles); |
|
223 | + $jsCollection = new AssetCollection($jsFiles); |
|
224 | + $jsCollection->setTargetPath("assets/$jsHash.js"); |
|
225 | + |
|
226 | + $writer = new AssetWriter($assetDir); |
|
227 | + $writer->writeAsset($jsCollection); |
|
228 | + } |
|
229 | + |
|
230 | + $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
|
231 | + $cssHash = self::hashFileNames($cssFiles); |
|
232 | + |
|
233 | + if (!file_exists("$assetDir/assets/$cssHash.css")) { |
|
234 | + $cssFiles = array_map(function ($item) { |
|
235 | + $root = $item[0]; |
|
236 | + $file = $item[2]; |
|
237 | + $assetPath = $root . '/' . $file; |
|
238 | + $sourceRoot = \OC::$SERVERROOT; |
|
239 | + $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT)); |
|
240 | + return new FileAsset( |
|
241 | + $assetPath, |
|
242 | + array( |
|
243 | + new CssRewriteFilter(), |
|
244 | + new CssMinFilter(), |
|
245 | + new CssImportFilter() |
|
246 | + ), |
|
247 | + $sourceRoot, |
|
248 | + $sourcePath |
|
249 | + ); |
|
250 | + }, $cssFiles); |
|
251 | + $cssCollection = new AssetCollection($cssFiles); |
|
252 | + $cssCollection->setTargetPath("assets/$cssHash.css"); |
|
253 | + |
|
254 | + $writer = new AssetWriter($assetDir); |
|
255 | + $writer->writeAsset($cssCollection); |
|
256 | + } |
|
257 | + |
|
258 | + $this->append('jsfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$jsHash.js")); |
|
259 | + $this->append('cssfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$cssHash.css")); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Converts the absolute file path to a relative path from \OC::$SERVERROOT |
|
264 | + * @param string $filePath Absolute path |
|
265 | + * @return string Relative path |
|
266 | + * @throws \Exception If $filePath is not under \OC::$SERVERROOT |
|
267 | + */ |
|
268 | + public static function convertToRelativePath($filePath) { |
|
269 | + $relativePath = explode(\OC::$SERVERROOT, $filePath); |
|
270 | + if(count($relativePath) !== 2) { |
|
271 | + throw new \Exception('$filePath is not under the \OC::$SERVERROOT'); |
|
272 | + } |
|
273 | + |
|
274 | + return $relativePath[1]; |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * @param array $files |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + |
|
282 | + private static function hashFileNames($files) { |
|
283 | + foreach($files as $i => $file) { |
|
284 | + try { |
|
285 | + $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2]; |
|
286 | + } catch (\Exception $e) { |
|
287 | + $files[$i] = $file[0].'/'.$file[2]; |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + sort($files); |
|
292 | + // include the apps' versions hash to invalidate the cached assets |
|
293 | + $files[] = self::$versionHash; |
|
294 | + return hash('md5', implode('', $files)); |
|
295 | + } |
|
296 | 296 | } |
@@ -57,43 +57,43 @@ discard block |
||
57 | 57 | * @param string $renderAs |
58 | 58 | * @param string $appId application id |
59 | 59 | */ |
60 | - public function __construct( $renderAs, $appId = '' ) { |
|
60 | + public function __construct($renderAs, $appId = '') { |
|
61 | 61 | |
62 | 62 | // yes - should be injected .... |
63 | 63 | $this->config = \OC::$server->getConfig(); |
64 | 64 | |
65 | 65 | // Decide which page we show |
66 | - if($renderAs == 'user') { |
|
67 | - parent::__construct( 'core', 'layout.user' ); |
|
68 | - if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
|
66 | + if ($renderAs == 'user') { |
|
67 | + parent::__construct('core', 'layout.user'); |
|
68 | + if (in_array(\OC_App::getCurrentApp(), ['settings', 'admin', 'help']) !== false) { |
|
69 | 69 | $this->assign('bodyid', 'body-settings'); |
70 | - }else{ |
|
70 | + } else { |
|
71 | 71 | $this->assign('bodyid', 'body-user'); |
72 | 72 | } |
73 | 73 | |
74 | 74 | // Code integrity notification |
75 | 75 | $integrityChecker = \OC::$server->getIntegrityCodeChecker(); |
76 | - if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) { |
|
76 | + if (\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) { |
|
77 | 77 | \OCP\Util::addScript('core', 'integritycheck-failed-notification'); |
78 | 78 | } |
79 | 79 | |
80 | 80 | // Add navigation entry |
81 | - $this->assign( 'application', ''); |
|
82 | - $this->assign( 'appid', $appId ); |
|
81 | + $this->assign('application', ''); |
|
82 | + $this->assign('appid', $appId); |
|
83 | 83 | $navigation = \OC_App::getNavigation(); |
84 | - $this->assign( 'navigation', $navigation); |
|
84 | + $this->assign('navigation', $navigation); |
|
85 | 85 | $settingsNavigation = \OC_App::getSettingsNavigation(); |
86 | - $this->assign( 'settingsnavigation', $settingsNavigation); |
|
87 | - foreach($navigation as $entry) { |
|
86 | + $this->assign('settingsnavigation', $settingsNavigation); |
|
87 | + foreach ($navigation as $entry) { |
|
88 | 88 | if ($entry['active']) { |
89 | - $this->assign( 'application', $entry['name'] ); |
|
89 | + $this->assign('application', $entry['name']); |
|
90 | 90 | break; |
91 | 91 | } |
92 | 92 | } |
93 | 93 | |
94 | - foreach($settingsNavigation as $entry) { |
|
94 | + foreach ($settingsNavigation as $entry) { |
|
95 | 95 | if ($entry['active']) { |
96 | - $this->assign( 'application', $entry['name'] ); |
|
96 | + $this->assign('application', $entry['name']); |
|
97 | 97 | break; |
98 | 98 | } |
99 | 99 | } |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | // Send the language to our layouts |
128 | 128 | $this->assign('language', \OC_L10N::findLanguage()); |
129 | 129 | |
130 | - if(\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
130 | + if (\OC::$server->getSystemConfig()->getValue('installed', false)) { |
|
131 | 131 | if (empty(self::$versionHash)) { |
132 | 132 | $v = \OC_App::getAppVersions(); |
133 | 133 | $v['core'] = implode('.', \OCP\Util::getVersion()); |
@@ -139,29 +139,29 @@ discard block |
||
139 | 139 | |
140 | 140 | $useAssetPipeline = self::isAssetPipelineEnabled(); |
141 | 141 | if ($useAssetPipeline) { |
142 | - $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
142 | + $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
143 | 143 | $this->generateAssets(); |
144 | 144 | } else { |
145 | 145 | // Add the js files |
146 | 146 | $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts); |
147 | 147 | $this->assign('jsfiles', array()); |
148 | 148 | if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') { |
149 | - $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
149 | + $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash])); |
|
150 | 150 | } |
151 | - foreach($jsFiles as $info) { |
|
151 | + foreach ($jsFiles as $info) { |
|
152 | 152 | $web = $info[1]; |
153 | 153 | $file = $info[2]; |
154 | - $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
|
154 | + $this->append('jsfiles', $web.'/'.$file.'?v='.self::$versionHash); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | // Add the css files |
158 | 158 | $cssFiles = self::findStylesheetFiles(\OC_Util::$styles); |
159 | 159 | $this->assign('cssfiles', array()); |
160 | - foreach($cssFiles as $info) { |
|
160 | + foreach ($cssFiles as $info) { |
|
161 | 161 | $web = $info[1]; |
162 | 162 | $file = $info[2]; |
163 | 163 | |
164 | - $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash); |
|
164 | + $this->append('cssfiles', $web.'/'.$file.'?v='.self::$versionHash); |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | } |
@@ -177,8 +177,8 @@ discard block |
||
177 | 177 | $locator = new \OC\Template\CSSResourceLocator( |
178 | 178 | \OC::$server->getLogger(), |
179 | 179 | $theme, |
180 | - array( \OC::$SERVERROOT => \OC::$WEBROOT ), |
|
181 | - array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT )); |
|
180 | + array(\OC::$SERVERROOT => \OC::$WEBROOT), |
|
181 | + array(\OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT)); |
|
182 | 182 | $locator->find($styles); |
183 | 183 | return $locator->getResources(); |
184 | 184 | } |
@@ -194,8 +194,8 @@ discard block |
||
194 | 194 | $locator = new \OC\Template\JSResourceLocator( |
195 | 195 | \OC::$server->getLogger(), |
196 | 196 | $theme, |
197 | - array( \OC::$SERVERROOT => \OC::$WEBROOT ), |
|
198 | - array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT )); |
|
197 | + array(\OC::$SERVERROOT => \OC::$WEBROOT), |
|
198 | + array(\OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT)); |
|
199 | 199 | $locator->find($scripts); |
200 | 200 | return $locator->getResources(); |
201 | 201 | } |
@@ -206,16 +206,16 @@ discard block |
||
206 | 206 | $jsHash = self::hashFileNames($jsFiles); |
207 | 207 | |
208 | 208 | if (!file_exists("$assetDir/assets/$jsHash.js")) { |
209 | - $jsFiles = array_map(function ($item) { |
|
209 | + $jsFiles = array_map(function($item) { |
|
210 | 210 | $root = $item[0]; |
211 | 211 | $file = $item[2]; |
212 | 212 | // no need to minifiy minified files |
213 | 213 | if (substr($file, -strlen('.min.js')) === '.min.js') { |
214 | - return new FileAsset($root . '/' . $file, array( |
|
214 | + return new FileAsset($root.'/'.$file, array( |
|
215 | 215 | new SeparatorFilter(';') |
216 | 216 | ), $root, $file); |
217 | 217 | } |
218 | - return new FileAsset($root . '/' . $file, array( |
|
218 | + return new FileAsset($root.'/'.$file, array( |
|
219 | 219 | new JSqueezeFilter(), |
220 | 220 | new SeparatorFilter(';') |
221 | 221 | ), $root, $file); |
@@ -231,11 +231,11 @@ discard block |
||
231 | 231 | $cssHash = self::hashFileNames($cssFiles); |
232 | 232 | |
233 | 233 | if (!file_exists("$assetDir/assets/$cssHash.css")) { |
234 | - $cssFiles = array_map(function ($item) { |
|
234 | + $cssFiles = array_map(function($item) { |
|
235 | 235 | $root = $item[0]; |
236 | 236 | $file = $item[2]; |
237 | - $assetPath = $root . '/' . $file; |
|
238 | - $sourceRoot = \OC::$SERVERROOT; |
|
237 | + $assetPath = $root.'/'.$file; |
|
238 | + $sourceRoot = \OC::$SERVERROOT; |
|
239 | 239 | $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT)); |
240 | 240 | return new FileAsset( |
241 | 241 | $assetPath, |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | */ |
268 | 268 | public static function convertToRelativePath($filePath) { |
269 | 269 | $relativePath = explode(\OC::$SERVERROOT, $filePath); |
270 | - if(count($relativePath) !== 2) { |
|
270 | + if (count($relativePath) !== 2) { |
|
271 | 271 | throw new \Exception('$filePath is not under the \OC::$SERVERROOT'); |
272 | 272 | } |
273 | 273 | |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | */ |
281 | 281 | |
282 | 282 | private static function hashFileNames($files) { |
283 | - foreach($files as $i => $file) { |
|
283 | + foreach ($files as $i => $file) { |
|
284 | 284 | try { |
285 | 285 | $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2]; |
286 | 286 | } catch (\Exception $e) { |
@@ -61,7 +61,7 @@ |
||
61 | 61 | parent::__construct( 'core', 'layout.user' ); |
62 | 62 | if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) { |
63 | 63 | $this->assign('bodyid', 'body-settings'); |
64 | - }else{ |
|
64 | + } else{ |
|
65 | 65 | $this->assign('bodyid', 'body-user'); |
66 | 66 | } |
67 | 67 |
@@ -28,242 +28,242 @@ |
||
28 | 28 | */ |
29 | 29 | |
30 | 30 | class OC_Response { |
31 | - const STATUS_FOUND = 304; |
|
32 | - const STATUS_NOT_MODIFIED = 304; |
|
33 | - const STATUS_TEMPORARY_REDIRECT = 307; |
|
34 | - const STATUS_BAD_REQUEST = 400; |
|
35 | - const STATUS_NOT_FOUND = 404; |
|
36 | - const STATUS_INTERNAL_SERVER_ERROR = 500; |
|
37 | - const STATUS_SERVICE_UNAVAILABLE = 503; |
|
31 | + const STATUS_FOUND = 304; |
|
32 | + const STATUS_NOT_MODIFIED = 304; |
|
33 | + const STATUS_TEMPORARY_REDIRECT = 307; |
|
34 | + const STATUS_BAD_REQUEST = 400; |
|
35 | + const STATUS_NOT_FOUND = 404; |
|
36 | + const STATUS_INTERNAL_SERVER_ERROR = 500; |
|
37 | + const STATUS_SERVICE_UNAVAILABLE = 503; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Enable response caching by sending correct HTTP headers |
|
41 | - * @param integer $cache_time time to cache the response |
|
42 | - * >0 cache time in seconds |
|
43 | - * 0 and <0 enable default browser caching |
|
44 | - * null cache indefinitly |
|
45 | - */ |
|
46 | - static public function enableCaching($cache_time = null) { |
|
47 | - if (is_numeric($cache_time)) { |
|
48 | - header('Pragma: public');// enable caching in IE |
|
49 | - if ($cache_time > 0) { |
|
50 | - self::setExpiresHeader('PT'.$cache_time.'S'); |
|
51 | - header('Cache-Control: max-age='.$cache_time.', must-revalidate'); |
|
52 | - } |
|
53 | - else { |
|
54 | - self::setExpiresHeader(0); |
|
55 | - header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
56 | - } |
|
57 | - } |
|
58 | - else { |
|
59 | - header('Cache-Control: cache'); |
|
60 | - header('Pragma: cache'); |
|
61 | - } |
|
39 | + /** |
|
40 | + * Enable response caching by sending correct HTTP headers |
|
41 | + * @param integer $cache_time time to cache the response |
|
42 | + * >0 cache time in seconds |
|
43 | + * 0 and <0 enable default browser caching |
|
44 | + * null cache indefinitly |
|
45 | + */ |
|
46 | + static public function enableCaching($cache_time = null) { |
|
47 | + if (is_numeric($cache_time)) { |
|
48 | + header('Pragma: public');// enable caching in IE |
|
49 | + if ($cache_time > 0) { |
|
50 | + self::setExpiresHeader('PT'.$cache_time.'S'); |
|
51 | + header('Cache-Control: max-age='.$cache_time.', must-revalidate'); |
|
52 | + } |
|
53 | + else { |
|
54 | + self::setExpiresHeader(0); |
|
55 | + header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
|
56 | + } |
|
57 | + } |
|
58 | + else { |
|
59 | + header('Cache-Control: cache'); |
|
60 | + header('Pragma: cache'); |
|
61 | + } |
|
62 | 62 | |
63 | - } |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * disable browser caching |
|
67 | - * @see enableCaching with cache_time = 0 |
|
68 | - */ |
|
69 | - static public function disableCaching() { |
|
70 | - self::enableCaching(0); |
|
71 | - } |
|
65 | + /** |
|
66 | + * disable browser caching |
|
67 | + * @see enableCaching with cache_time = 0 |
|
68 | + */ |
|
69 | + static public function disableCaching() { |
|
70 | + self::enableCaching(0); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Set response status |
|
75 | - * @param int $status a HTTP status code, see also the STATUS constants |
|
76 | - */ |
|
77 | - static public function setStatus($status) { |
|
78 | - $protocol = \OC::$server->getRequest()->getHttpProtocol(); |
|
79 | - switch($status) { |
|
80 | - case self::STATUS_NOT_MODIFIED: |
|
81 | - $status = $status . ' Not Modified'; |
|
82 | - break; |
|
83 | - case self::STATUS_TEMPORARY_REDIRECT: |
|
84 | - if ($protocol == 'HTTP/1.1') { |
|
85 | - $status = $status . ' Temporary Redirect'; |
|
86 | - break; |
|
87 | - } else { |
|
88 | - $status = self::STATUS_FOUND; |
|
89 | - // fallthrough |
|
90 | - } |
|
91 | - case self::STATUS_FOUND; |
|
92 | - $status = $status . ' Found'; |
|
93 | - break; |
|
94 | - case self::STATUS_NOT_FOUND; |
|
95 | - $status = $status . ' Not Found'; |
|
96 | - break; |
|
97 | - case self::STATUS_INTERNAL_SERVER_ERROR; |
|
98 | - $status = $status . ' Internal Server Error'; |
|
99 | - break; |
|
100 | - case self::STATUS_SERVICE_UNAVAILABLE; |
|
101 | - $status = $status . ' Service Unavailable'; |
|
102 | - break; |
|
103 | - } |
|
104 | - header($protocol.' '.$status); |
|
105 | - } |
|
73 | + /** |
|
74 | + * Set response status |
|
75 | + * @param int $status a HTTP status code, see also the STATUS constants |
|
76 | + */ |
|
77 | + static public function setStatus($status) { |
|
78 | + $protocol = \OC::$server->getRequest()->getHttpProtocol(); |
|
79 | + switch($status) { |
|
80 | + case self::STATUS_NOT_MODIFIED: |
|
81 | + $status = $status . ' Not Modified'; |
|
82 | + break; |
|
83 | + case self::STATUS_TEMPORARY_REDIRECT: |
|
84 | + if ($protocol == 'HTTP/1.1') { |
|
85 | + $status = $status . ' Temporary Redirect'; |
|
86 | + break; |
|
87 | + } else { |
|
88 | + $status = self::STATUS_FOUND; |
|
89 | + // fallthrough |
|
90 | + } |
|
91 | + case self::STATUS_FOUND; |
|
92 | + $status = $status . ' Found'; |
|
93 | + break; |
|
94 | + case self::STATUS_NOT_FOUND; |
|
95 | + $status = $status . ' Not Found'; |
|
96 | + break; |
|
97 | + case self::STATUS_INTERNAL_SERVER_ERROR; |
|
98 | + $status = $status . ' Internal Server Error'; |
|
99 | + break; |
|
100 | + case self::STATUS_SERVICE_UNAVAILABLE; |
|
101 | + $status = $status . ' Service Unavailable'; |
|
102 | + break; |
|
103 | + } |
|
104 | + header($protocol.' '.$status); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Send redirect response |
|
109 | - * @param string $location to redirect to |
|
110 | - */ |
|
111 | - static public function redirect($location) { |
|
112 | - self::setStatus(self::STATUS_TEMPORARY_REDIRECT); |
|
113 | - header('Location: '.$location); |
|
114 | - } |
|
107 | + /** |
|
108 | + * Send redirect response |
|
109 | + * @param string $location to redirect to |
|
110 | + */ |
|
111 | + static public function redirect($location) { |
|
112 | + self::setStatus(self::STATUS_TEMPORARY_REDIRECT); |
|
113 | + header('Location: '.$location); |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Set reponse expire time |
|
118 | - * @param string|DateTime $expires date-time when the response expires |
|
119 | - * string for DateInterval from now |
|
120 | - * DateTime object when to expire response |
|
121 | - */ |
|
122 | - static public function setExpiresHeader($expires) { |
|
123 | - if (is_string($expires) && $expires[0] == 'P') { |
|
124 | - $interval = $expires; |
|
125 | - $expires = new DateTime('now'); |
|
126 | - $expires->add(new DateInterval($interval)); |
|
127 | - } |
|
128 | - if ($expires instanceof DateTime) { |
|
129 | - $expires->setTimezone(new DateTimeZone('GMT')); |
|
130 | - $expires = $expires->format(DateTime::RFC2822); |
|
131 | - } |
|
132 | - header('Expires: '.$expires); |
|
133 | - } |
|
116 | + /** |
|
117 | + * Set reponse expire time |
|
118 | + * @param string|DateTime $expires date-time when the response expires |
|
119 | + * string for DateInterval from now |
|
120 | + * DateTime object when to expire response |
|
121 | + */ |
|
122 | + static public function setExpiresHeader($expires) { |
|
123 | + if (is_string($expires) && $expires[0] == 'P') { |
|
124 | + $interval = $expires; |
|
125 | + $expires = new DateTime('now'); |
|
126 | + $expires->add(new DateInterval($interval)); |
|
127 | + } |
|
128 | + if ($expires instanceof DateTime) { |
|
129 | + $expires->setTimezone(new DateTimeZone('GMT')); |
|
130 | + $expires = $expires->format(DateTime::RFC2822); |
|
131 | + } |
|
132 | + header('Expires: '.$expires); |
|
133 | + } |
|
134 | 134 | |
135 | - /** |
|
136 | - * Checks and set ETag header, when the request matches sends a |
|
137 | - * 'not modified' response |
|
138 | - * @param string $etag token to use for modification check |
|
139 | - */ |
|
140 | - static public function setETagHeader($etag) { |
|
141 | - if (empty($etag)) { |
|
142 | - return; |
|
143 | - } |
|
144 | - $etag = '"'.$etag.'"'; |
|
145 | - if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && |
|
146 | - trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { |
|
147 | - self::setStatus(self::STATUS_NOT_MODIFIED); |
|
148 | - exit; |
|
149 | - } |
|
150 | - header('ETag: '.$etag); |
|
151 | - } |
|
135 | + /** |
|
136 | + * Checks and set ETag header, when the request matches sends a |
|
137 | + * 'not modified' response |
|
138 | + * @param string $etag token to use for modification check |
|
139 | + */ |
|
140 | + static public function setETagHeader($etag) { |
|
141 | + if (empty($etag)) { |
|
142 | + return; |
|
143 | + } |
|
144 | + $etag = '"'.$etag.'"'; |
|
145 | + if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && |
|
146 | + trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { |
|
147 | + self::setStatus(self::STATUS_NOT_MODIFIED); |
|
148 | + exit; |
|
149 | + } |
|
150 | + header('ETag: '.$etag); |
|
151 | + } |
|
152 | 152 | |
153 | - /** |
|
154 | - * Checks and set Last-Modified header, when the request matches sends a |
|
155 | - * 'not modified' response |
|
156 | - * @param int|DateTime|string $lastModified time when the reponse was last modified |
|
157 | - */ |
|
158 | - static public function setLastModifiedHeader($lastModified) { |
|
159 | - if (empty($lastModified)) { |
|
160 | - return; |
|
161 | - } |
|
162 | - if (is_int($lastModified)) { |
|
163 | - $lastModified = gmdate(DateTime::RFC2822, $lastModified); |
|
164 | - } |
|
165 | - if ($lastModified instanceof DateTime) { |
|
166 | - $lastModified = $lastModified->format(DateTime::RFC2822); |
|
167 | - } |
|
168 | - if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && |
|
169 | - trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { |
|
170 | - self::setStatus(self::STATUS_NOT_MODIFIED); |
|
171 | - exit; |
|
172 | - } |
|
173 | - header('Last-Modified: '.$lastModified); |
|
174 | - } |
|
153 | + /** |
|
154 | + * Checks and set Last-Modified header, when the request matches sends a |
|
155 | + * 'not modified' response |
|
156 | + * @param int|DateTime|string $lastModified time when the reponse was last modified |
|
157 | + */ |
|
158 | + static public function setLastModifiedHeader($lastModified) { |
|
159 | + if (empty($lastModified)) { |
|
160 | + return; |
|
161 | + } |
|
162 | + if (is_int($lastModified)) { |
|
163 | + $lastModified = gmdate(DateTime::RFC2822, $lastModified); |
|
164 | + } |
|
165 | + if ($lastModified instanceof DateTime) { |
|
166 | + $lastModified = $lastModified->format(DateTime::RFC2822); |
|
167 | + } |
|
168 | + if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && |
|
169 | + trim($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $lastModified) { |
|
170 | + self::setStatus(self::STATUS_NOT_MODIFIED); |
|
171 | + exit; |
|
172 | + } |
|
173 | + header('Last-Modified: '.$lastModified); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * Sets the content disposition header (with possible workarounds) |
|
178 | - * @param string $filename file name |
|
179 | - * @param string $type disposition type, either 'attachment' or 'inline' |
|
180 | - */ |
|
181 | - static public function setContentDispositionHeader( $filename, $type = 'attachment' ) { |
|
182 | - if (\OC::$server->getRequest()->isUserAgent( |
|
183 | - [ |
|
184 | - \OC\AppFramework\Http\Request::USER_AGENT_IE, |
|
185 | - \OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
|
186 | - \OC\AppFramework\Http\Request::USER_AGENT_FREEBOX, |
|
187 | - ])) { |
|
188 | - header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' ); |
|
189 | - } else { |
|
190 | - header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename ) |
|
191 | - . '; filename="' . rawurlencode( $filename ) . '"' ); |
|
192 | - } |
|
193 | - } |
|
176 | + /** |
|
177 | + * Sets the content disposition header (with possible workarounds) |
|
178 | + * @param string $filename file name |
|
179 | + * @param string $type disposition type, either 'attachment' or 'inline' |
|
180 | + */ |
|
181 | + static public function setContentDispositionHeader( $filename, $type = 'attachment' ) { |
|
182 | + if (\OC::$server->getRequest()->isUserAgent( |
|
183 | + [ |
|
184 | + \OC\AppFramework\Http\Request::USER_AGENT_IE, |
|
185 | + \OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
|
186 | + \OC\AppFramework\Http\Request::USER_AGENT_FREEBOX, |
|
187 | + ])) { |
|
188 | + header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' ); |
|
189 | + } else { |
|
190 | + header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename ) |
|
191 | + . '; filename="' . rawurlencode( $filename ) . '"' ); |
|
192 | + } |
|
193 | + } |
|
194 | 194 | |
195 | - /** |
|
196 | - * Sets the content length header (with possible workarounds) |
|
197 | - * @param string|int|float $length Length to be sent |
|
198 | - */ |
|
199 | - static public function setContentLengthHeader($length) { |
|
200 | - if (PHP_INT_SIZE === 4) { |
|
201 | - if ($length > PHP_INT_MAX && stripos(PHP_SAPI, 'apache') === 0) { |
|
202 | - // Apache PHP SAPI casts Content-Length headers to PHP integers. |
|
203 | - // This enforces a limit of PHP_INT_MAX (2147483647 on 32-bit |
|
204 | - // platforms). So, if the length is greater than PHP_INT_MAX, |
|
205 | - // we just do not send a Content-Length header to prevent |
|
206 | - // bodies from being received incompletely. |
|
207 | - return; |
|
208 | - } |
|
209 | - // Convert signed integer or float to unsigned base-10 string. |
|
210 | - $lfh = new \OC\LargeFileHelper; |
|
211 | - $length = $lfh->formatUnsignedInteger($length); |
|
212 | - } |
|
213 | - header('Content-Length: '.$length); |
|
214 | - } |
|
195 | + /** |
|
196 | + * Sets the content length header (with possible workarounds) |
|
197 | + * @param string|int|float $length Length to be sent |
|
198 | + */ |
|
199 | + static public function setContentLengthHeader($length) { |
|
200 | + if (PHP_INT_SIZE === 4) { |
|
201 | + if ($length > PHP_INT_MAX && stripos(PHP_SAPI, 'apache') === 0) { |
|
202 | + // Apache PHP SAPI casts Content-Length headers to PHP integers. |
|
203 | + // This enforces a limit of PHP_INT_MAX (2147483647 on 32-bit |
|
204 | + // platforms). So, if the length is greater than PHP_INT_MAX, |
|
205 | + // we just do not send a Content-Length header to prevent |
|
206 | + // bodies from being received incompletely. |
|
207 | + return; |
|
208 | + } |
|
209 | + // Convert signed integer or float to unsigned base-10 string. |
|
210 | + $lfh = new \OC\LargeFileHelper; |
|
211 | + $length = $lfh->formatUnsignedInteger($length); |
|
212 | + } |
|
213 | + header('Content-Length: '.$length); |
|
214 | + } |
|
215 | 215 | |
216 | - /** |
|
217 | - * Send file as response, checking and setting caching headers |
|
218 | - * @param string $filepath of file to send |
|
219 | - * @deprecated 8.1.0 - Use \OCP\AppFramework\Http\StreamResponse or another AppFramework controller instead |
|
220 | - */ |
|
221 | - static public function sendFile($filepath) { |
|
222 | - $fp = fopen($filepath, 'rb'); |
|
223 | - if ($fp) { |
|
224 | - self::setLastModifiedHeader(filemtime($filepath)); |
|
225 | - self::setETagHeader(md5_file($filepath)); |
|
216 | + /** |
|
217 | + * Send file as response, checking and setting caching headers |
|
218 | + * @param string $filepath of file to send |
|
219 | + * @deprecated 8.1.0 - Use \OCP\AppFramework\Http\StreamResponse or another AppFramework controller instead |
|
220 | + */ |
|
221 | + static public function sendFile($filepath) { |
|
222 | + $fp = fopen($filepath, 'rb'); |
|
223 | + if ($fp) { |
|
224 | + self::setLastModifiedHeader(filemtime($filepath)); |
|
225 | + self::setETagHeader(md5_file($filepath)); |
|
226 | 226 | |
227 | - self::setContentLengthHeader(filesize($filepath)); |
|
228 | - fpassthru($fp); |
|
229 | - } |
|
230 | - else { |
|
231 | - self::setStatus(self::STATUS_NOT_FOUND); |
|
232 | - } |
|
233 | - } |
|
227 | + self::setContentLengthHeader(filesize($filepath)); |
|
228 | + fpassthru($fp); |
|
229 | + } |
|
230 | + else { |
|
231 | + self::setStatus(self::STATUS_NOT_FOUND); |
|
232 | + } |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * This function adds some security related headers to all requests served via base.php |
|
237 | - * The implementation of this function has to happen here to ensure that all third-party |
|
238 | - * components (e.g. SabreDAV) also benefit from this headers. |
|
239 | - */ |
|
240 | - public static function addSecurityHeaders() { |
|
241 | - /** |
|
242 | - * FIXME: Content Security Policy for legacy ownCloud components. This |
|
243 | - * can be removed once \OCP\AppFramework\Http\Response from the AppFramework |
|
244 | - * is used everywhere. |
|
245 | - * @see \OCP\AppFramework\Http\Response::getHeaders |
|
246 | - */ |
|
247 | - $policy = 'default-src \'self\'; ' |
|
248 | - . 'script-src \'self\' \'unsafe-eval\'; ' |
|
249 | - . 'style-src \'self\' \'unsafe-inline\'; ' |
|
250 | - . 'frame-src *; ' |
|
251 | - . 'img-src * data: blob:; ' |
|
252 | - . 'font-src \'self\' data:; ' |
|
253 | - . 'media-src *; ' |
|
254 | - . 'connect-src *'; |
|
255 | - header('Content-Security-Policy:' . $policy); |
|
235 | + /** |
|
236 | + * This function adds some security related headers to all requests served via base.php |
|
237 | + * The implementation of this function has to happen here to ensure that all third-party |
|
238 | + * components (e.g. SabreDAV) also benefit from this headers. |
|
239 | + */ |
|
240 | + public static function addSecurityHeaders() { |
|
241 | + /** |
|
242 | + * FIXME: Content Security Policy for legacy ownCloud components. This |
|
243 | + * can be removed once \OCP\AppFramework\Http\Response from the AppFramework |
|
244 | + * is used everywhere. |
|
245 | + * @see \OCP\AppFramework\Http\Response::getHeaders |
|
246 | + */ |
|
247 | + $policy = 'default-src \'self\'; ' |
|
248 | + . 'script-src \'self\' \'unsafe-eval\'; ' |
|
249 | + . 'style-src \'self\' \'unsafe-inline\'; ' |
|
250 | + . 'frame-src *; ' |
|
251 | + . 'img-src * data: blob:; ' |
|
252 | + . 'font-src \'self\' data:; ' |
|
253 | + . 'media-src *; ' |
|
254 | + . 'connect-src *'; |
|
255 | + header('Content-Security-Policy:' . $policy); |
|
256 | 256 | |
257 | - // Send fallback headers for installations that don't have the possibility to send |
|
258 | - // custom headers on the webserver side |
|
259 | - if(getenv('modHeadersAvailable') !== 'true') { |
|
260 | - header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters |
|
261 | - header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE |
|
262 | - header('X-Frame-Options: Sameorigin'); // Disallow iFraming from other domains |
|
263 | - header('X-Robots-Tag: none'); // https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag |
|
264 | - header('X-Download-Options: noopen'); // https://msdn.microsoft.com/en-us/library/jj542450(v=vs.85).aspx |
|
265 | - header('X-Permitted-Cross-Domain-Policies: none'); // https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html |
|
266 | - } |
|
267 | - } |
|
257 | + // Send fallback headers for installations that don't have the possibility to send |
|
258 | + // custom headers on the webserver side |
|
259 | + if(getenv('modHeadersAvailable') !== 'true') { |
|
260 | + header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters |
|
261 | + header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE |
|
262 | + header('X-Frame-Options: Sameorigin'); // Disallow iFraming from other domains |
|
263 | + header('X-Robots-Tag: none'); // https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag |
|
264 | + header('X-Download-Options: noopen'); // https://msdn.microsoft.com/en-us/library/jj542450(v=vs.85).aspx |
|
265 | + header('X-Permitted-Cross-Domain-Policies: none'); // https://www.adobe.com/devnet/adobe-media-server/articles/cross-domain-xml-for-streaming.html |
|
266 | + } |
|
267 | + } |
|
268 | 268 | |
269 | 269 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | static public function enableCaching($cache_time = null) { |
49 | 49 | if (is_numeric($cache_time)) { |
50 | - header('Pragma: public');// enable caching in IE |
|
50 | + header('Pragma: public'); // enable caching in IE |
|
51 | 51 | if ($cache_time > 0) { |
52 | 52 | self::setExpiresHeader('PT'.$cache_time.'S'); |
53 | 53 | header('Cache-Control: max-age='.$cache_time.', must-revalidate'); |
@@ -78,29 +78,29 @@ discard block |
||
78 | 78 | */ |
79 | 79 | static public function setStatus($status) { |
80 | 80 | $protocol = \OC::$server->getRequest()->getHttpProtocol(); |
81 | - switch($status) { |
|
81 | + switch ($status) { |
|
82 | 82 | case self::STATUS_NOT_MODIFIED: |
83 | - $status = $status . ' Not Modified'; |
|
83 | + $status = $status.' Not Modified'; |
|
84 | 84 | break; |
85 | 85 | case self::STATUS_TEMPORARY_REDIRECT: |
86 | 86 | if ($protocol == 'HTTP/1.1') { |
87 | - $status = $status . ' Temporary Redirect'; |
|
87 | + $status = $status.' Temporary Redirect'; |
|
88 | 88 | break; |
89 | 89 | } else { |
90 | 90 | $status = self::STATUS_FOUND; |
91 | 91 | // fallthrough |
92 | 92 | } |
93 | 93 | case self::STATUS_FOUND; |
94 | - $status = $status . ' Found'; |
|
94 | + $status = $status.' Found'; |
|
95 | 95 | break; |
96 | 96 | case self::STATUS_NOT_FOUND; |
97 | - $status = $status . ' Not Found'; |
|
97 | + $status = $status.' Not Found'; |
|
98 | 98 | break; |
99 | 99 | case self::STATUS_INTERNAL_SERVER_ERROR; |
100 | - $status = $status . ' Internal Server Error'; |
|
100 | + $status = $status.' Internal Server Error'; |
|
101 | 101 | break; |
102 | 102 | case self::STATUS_SERVICE_UNAVAILABLE; |
103 | - $status = $status . ' Service Unavailable'; |
|
103 | + $status = $status.' Service Unavailable'; |
|
104 | 104 | break; |
105 | 105 | } |
106 | 106 | header($protocol.' '.$status); |
@@ -180,17 +180,17 @@ discard block |
||
180 | 180 | * @param string $filename file name |
181 | 181 | * @param string $type disposition type, either 'attachment' or 'inline' |
182 | 182 | */ |
183 | - static public function setContentDispositionHeader( $filename, $type = 'attachment' ) { |
|
183 | + static public function setContentDispositionHeader($filename, $type = 'attachment') { |
|
184 | 184 | if (\OC::$server->getRequest()->isUserAgent( |
185 | 185 | [ |
186 | 186 | \OC\AppFramework\Http\Request::USER_AGENT_IE, |
187 | 187 | \OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
188 | 188 | \OC\AppFramework\Http\Request::USER_AGENT_FREEBOX, |
189 | 189 | ])) { |
190 | - header( 'Content-Disposition: ' . rawurlencode($type) . '; filename="' . rawurlencode( $filename ) . '"' ); |
|
190 | + header('Content-Disposition: '.rawurlencode($type).'; filename="'.rawurlencode($filename).'"'); |
|
191 | 191 | } else { |
192 | - header( 'Content-Disposition: ' . rawurlencode($type) . '; filename*=UTF-8\'\'' . rawurlencode( $filename ) |
|
193 | - . '; filename="' . rawurlencode( $filename ) . '"' ); |
|
192 | + header('Content-Disposition: '.rawurlencode($type).'; filename*=UTF-8\'\''.rawurlencode($filename) |
|
193 | + . '; filename="'.rawurlencode($filename).'"'); |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
@@ -254,11 +254,11 @@ discard block |
||
254 | 254 | . 'font-src \'self\' data:; ' |
255 | 255 | . 'media-src *; ' |
256 | 256 | . 'connect-src *'; |
257 | - header('Content-Security-Policy:' . $policy); |
|
257 | + header('Content-Security-Policy:'.$policy); |
|
258 | 258 | |
259 | 259 | // Send fallback headers for installations that don't have the possibility to send |
260 | 260 | // custom headers on the webserver side |
261 | - if(getenv('modHeadersAvailable') !== 'true') { |
|
261 | + if (getenv('modHeadersAvailable') !== 'true') { |
|
262 | 262 | header('X-XSS-Protection: 1; mode=block'); // Enforce browser based XSS filters |
263 | 263 | header('X-Content-Type-Options: nosniff'); // Disable sniffing the content type for IE |
264 | 264 | header('X-Frame-Options: Sameorigin'); // Disallow iFraming from other domains |
@@ -51,13 +51,11 @@ discard block |
||
51 | 51 | if ($cache_time > 0) { |
52 | 52 | self::setExpiresHeader('PT'.$cache_time.'S'); |
53 | 53 | header('Cache-Control: max-age='.$cache_time.', must-revalidate'); |
54 | - } |
|
55 | - else { |
|
54 | + } else { |
|
56 | 55 | self::setExpiresHeader(0); |
57 | 56 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
58 | 57 | } |
59 | - } |
|
60 | - else { |
|
58 | + } else { |
|
61 | 59 | header('Cache-Control: cache'); |
62 | 60 | header('Pragma: cache'); |
63 | 61 | } |
@@ -228,8 +226,7 @@ discard block |
||
228 | 226 | |
229 | 227 | self::setContentLengthHeader(filesize($filepath)); |
230 | 228 | fpassthru($fp); |
231 | - } |
|
232 | - else { |
|
229 | + } else { |
|
233 | 230 | self::setStatus(self::STATUS_NOT_FOUND); |
234 | 231 | } |
235 | 232 | } |
@@ -48,247 +48,247 @@ |
||
48 | 48 | |
49 | 49 | class Log implements ILogger { |
50 | 50 | |
51 | - /** @var string */ |
|
52 | - private $logger; |
|
53 | - |
|
54 | - /** @var SystemConfig */ |
|
55 | - private $config; |
|
56 | - |
|
57 | - /** @var boolean|null cache the result of the log condition check for the request */ |
|
58 | - private $logConditionSatisfied = null; |
|
59 | - |
|
60 | - /** @var Normalizer */ |
|
61 | - private $normalizer; |
|
62 | - |
|
63 | - /** |
|
64 | - * @param string $logger The logger that should be used |
|
65 | - * @param SystemConfig $config the system config object |
|
66 | - * @param null $normalizer |
|
67 | - */ |
|
68 | - public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) { |
|
69 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
70 | - if($config === null) { |
|
71 | - $config = \OC::$server->getSystemConfig(); |
|
72 | - } |
|
73 | - |
|
74 | - $this->config = $config; |
|
75 | - |
|
76 | - // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
77 | - if($logger === null) { |
|
78 | - $this->logger = 'OC_Log_'.ucfirst($this->config->getValue('log_type', 'owncloud')); |
|
79 | - call_user_func(array($this->logger, 'init')); |
|
80 | - } else { |
|
81 | - $this->logger = $logger; |
|
82 | - } |
|
83 | - if ($normalizer === null) { |
|
84 | - $this->normalizer = new Normalizer(); |
|
85 | - } else { |
|
86 | - $this->normalizer = $normalizer; |
|
87 | - } |
|
88 | - |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * System is unusable. |
|
93 | - * |
|
94 | - * @param string $message |
|
95 | - * @param array $context |
|
96 | - * @return void |
|
97 | - */ |
|
98 | - public function emergency($message, array $context = array()) { |
|
99 | - $this->log(Util::FATAL, $message, $context); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Action must be taken immediately. |
|
104 | - * |
|
105 | - * Example: Entire website down, database unavailable, etc. This should |
|
106 | - * trigger the SMS alerts and wake you up. |
|
107 | - * |
|
108 | - * @param string $message |
|
109 | - * @param array $context |
|
110 | - * @return void |
|
111 | - */ |
|
112 | - public function alert($message, array $context = array()) { |
|
113 | - $this->log(Util::ERROR, $message, $context); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Critical conditions. |
|
118 | - * |
|
119 | - * Example: Application component unavailable, unexpected exception. |
|
120 | - * |
|
121 | - * @param string $message |
|
122 | - * @param array $context |
|
123 | - * @return void |
|
124 | - */ |
|
125 | - public function critical($message, array $context = array()) { |
|
126 | - $this->log(Util::ERROR, $message, $context); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Runtime errors that do not require immediate action but should typically |
|
131 | - * be logged and monitored. |
|
132 | - * |
|
133 | - * @param string $message |
|
134 | - * @param array $context |
|
135 | - * @return void |
|
136 | - */ |
|
137 | - public function error($message, array $context = array()) { |
|
138 | - $this->log(Util::ERROR, $message, $context); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Exceptional occurrences that are not errors. |
|
143 | - * |
|
144 | - * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
145 | - * that are not necessarily wrong. |
|
146 | - * |
|
147 | - * @param string $message |
|
148 | - * @param array $context |
|
149 | - * @return void |
|
150 | - */ |
|
151 | - public function warning($message, array $context = array()) { |
|
152 | - $this->log(Util::WARN, $message, $context); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Normal but significant events. |
|
157 | - * |
|
158 | - * @param string $message |
|
159 | - * @param array $context |
|
160 | - * @return void |
|
161 | - */ |
|
162 | - public function notice($message, array $context = array()) { |
|
163 | - $this->log(Util::INFO, $message, $context); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Interesting events. |
|
168 | - * |
|
169 | - * Example: User logs in, SQL logs. |
|
170 | - * |
|
171 | - * @param string $message |
|
172 | - * @param array $context |
|
173 | - * @return void |
|
174 | - */ |
|
175 | - public function info($message, array $context = array()) { |
|
176 | - $this->log(Util::INFO, $message, $context); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Detailed debug information. |
|
181 | - * |
|
182 | - * @param string $message |
|
183 | - * @param array $context |
|
184 | - * @return void |
|
185 | - */ |
|
186 | - public function debug($message, array $context = array()) { |
|
187 | - $this->log(Util::DEBUG, $message, $context); |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * Logs with an arbitrary level. |
|
193 | - * |
|
194 | - * @param mixed $level |
|
195 | - * @param string $message |
|
196 | - * @param array $context |
|
197 | - * @return void |
|
198 | - */ |
|
199 | - public function log($level, $message, array $context = array()) { |
|
200 | - $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::ERROR); |
|
201 | - $logCondition = $this->config->getValue('log.condition', []); |
|
202 | - |
|
203 | - array_walk($context, [$this->normalizer, 'format']); |
|
204 | - |
|
205 | - if (isset($context['app'])) { |
|
206 | - $app = $context['app']; |
|
207 | - |
|
208 | - /** |
|
209 | - * check log condition based on the context of each log message |
|
210 | - * once this is met -> change the required log level to debug |
|
211 | - */ |
|
212 | - if(!empty($logCondition) |
|
213 | - && isset($logCondition['apps']) |
|
214 | - && in_array($app, $logCondition['apps'], true)) { |
|
215 | - $minLevel = Util::DEBUG; |
|
216 | - } |
|
217 | - |
|
218 | - } else { |
|
219 | - $app = 'no app in context'; |
|
220 | - } |
|
221 | - // interpolate $message as defined in PSR-3 |
|
222 | - $replace = array(); |
|
223 | - foreach ($context as $key => $val) { |
|
224 | - $replace['{' . $key . '}'] = $val; |
|
225 | - } |
|
226 | - |
|
227 | - // interpolate replacement values into the message and return |
|
228 | - $message = strtr($message, $replace); |
|
229 | - |
|
230 | - /** |
|
231 | - * check for a special log condition - this enables an increased log on |
|
232 | - * a per request/user base |
|
233 | - */ |
|
234 | - if($this->logConditionSatisfied === null) { |
|
235 | - // default to false to just process this once per request |
|
236 | - $this->logConditionSatisfied = false; |
|
237 | - if(!empty($logCondition)) { |
|
238 | - |
|
239 | - // check for secret token in the request |
|
240 | - if(isset($logCondition['shared_secret'])) { |
|
241 | - $request = \OC::$server->getRequest(); |
|
242 | - |
|
243 | - // if token is found in the request change set the log condition to satisfied |
|
244 | - if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret'))) { |
|
245 | - $this->logConditionSatisfied = true; |
|
246 | - } |
|
247 | - } |
|
248 | - |
|
249 | - // check for user |
|
250 | - if(isset($logCondition['users'])) { |
|
251 | - $user = \OC::$server->getUserSession()->getUser(); |
|
252 | - |
|
253 | - // if the user matches set the log condition to satisfied |
|
254 | - if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
255 | - $this->logConditionSatisfied = true; |
|
256 | - } |
|
257 | - } |
|
258 | - } |
|
259 | - } |
|
260 | - |
|
261 | - // if log condition is satisfied change the required log level to DEBUG |
|
262 | - if($this->logConditionSatisfied) { |
|
263 | - $minLevel = Util::DEBUG; |
|
264 | - } |
|
265 | - |
|
266 | - if ($level >= $minLevel) { |
|
267 | - $logger = $this->logger; |
|
268 | - call_user_func(array($logger, 'write'), $app, $message, $level); |
|
269 | - } |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Logs an exception very detailed |
|
274 | - * |
|
275 | - * @param \Exception $exception |
|
276 | - * @param array $context |
|
277 | - * @return void |
|
278 | - * @since 8.2.0 |
|
279 | - */ |
|
280 | - public function logException(\Exception $exception, array $context = array()) { |
|
281 | - $exception = array( |
|
282 | - 'Exception' => get_class($exception), |
|
283 | - 'Message' => $exception->getMessage(), |
|
284 | - 'Code' => $exception->getCode(), |
|
285 | - 'Trace' => $exception->getTraceAsString(), |
|
286 | - 'File' => $exception->getFile(), |
|
287 | - 'Line' => $exception->getLine(), |
|
288 | - ); |
|
289 | - $exception['Trace'] = preg_replace('!(login|checkPassword|updatePrivateKeyPassword|validateUserPass)\(.*\)!', '$1(*** username and password replaced ***)', $exception['Trace']); |
|
290 | - $msg = isset($context['message']) ? $context['message'] : 'Exception'; |
|
291 | - $msg .= ': ' . json_encode($exception); |
|
292 | - $this->error($msg, $context); |
|
293 | - } |
|
51 | + /** @var string */ |
|
52 | + private $logger; |
|
53 | + |
|
54 | + /** @var SystemConfig */ |
|
55 | + private $config; |
|
56 | + |
|
57 | + /** @var boolean|null cache the result of the log condition check for the request */ |
|
58 | + private $logConditionSatisfied = null; |
|
59 | + |
|
60 | + /** @var Normalizer */ |
|
61 | + private $normalizer; |
|
62 | + |
|
63 | + /** |
|
64 | + * @param string $logger The logger that should be used |
|
65 | + * @param SystemConfig $config the system config object |
|
66 | + * @param null $normalizer |
|
67 | + */ |
|
68 | + public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) { |
|
69 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
70 | + if($config === null) { |
|
71 | + $config = \OC::$server->getSystemConfig(); |
|
72 | + } |
|
73 | + |
|
74 | + $this->config = $config; |
|
75 | + |
|
76 | + // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
|
77 | + if($logger === null) { |
|
78 | + $this->logger = 'OC_Log_'.ucfirst($this->config->getValue('log_type', 'owncloud')); |
|
79 | + call_user_func(array($this->logger, 'init')); |
|
80 | + } else { |
|
81 | + $this->logger = $logger; |
|
82 | + } |
|
83 | + if ($normalizer === null) { |
|
84 | + $this->normalizer = new Normalizer(); |
|
85 | + } else { |
|
86 | + $this->normalizer = $normalizer; |
|
87 | + } |
|
88 | + |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * System is unusable. |
|
93 | + * |
|
94 | + * @param string $message |
|
95 | + * @param array $context |
|
96 | + * @return void |
|
97 | + */ |
|
98 | + public function emergency($message, array $context = array()) { |
|
99 | + $this->log(Util::FATAL, $message, $context); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Action must be taken immediately. |
|
104 | + * |
|
105 | + * Example: Entire website down, database unavailable, etc. This should |
|
106 | + * trigger the SMS alerts and wake you up. |
|
107 | + * |
|
108 | + * @param string $message |
|
109 | + * @param array $context |
|
110 | + * @return void |
|
111 | + */ |
|
112 | + public function alert($message, array $context = array()) { |
|
113 | + $this->log(Util::ERROR, $message, $context); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Critical conditions. |
|
118 | + * |
|
119 | + * Example: Application component unavailable, unexpected exception. |
|
120 | + * |
|
121 | + * @param string $message |
|
122 | + * @param array $context |
|
123 | + * @return void |
|
124 | + */ |
|
125 | + public function critical($message, array $context = array()) { |
|
126 | + $this->log(Util::ERROR, $message, $context); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Runtime errors that do not require immediate action but should typically |
|
131 | + * be logged and monitored. |
|
132 | + * |
|
133 | + * @param string $message |
|
134 | + * @param array $context |
|
135 | + * @return void |
|
136 | + */ |
|
137 | + public function error($message, array $context = array()) { |
|
138 | + $this->log(Util::ERROR, $message, $context); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Exceptional occurrences that are not errors. |
|
143 | + * |
|
144 | + * Example: Use of deprecated APIs, poor use of an API, undesirable things |
|
145 | + * that are not necessarily wrong. |
|
146 | + * |
|
147 | + * @param string $message |
|
148 | + * @param array $context |
|
149 | + * @return void |
|
150 | + */ |
|
151 | + public function warning($message, array $context = array()) { |
|
152 | + $this->log(Util::WARN, $message, $context); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Normal but significant events. |
|
157 | + * |
|
158 | + * @param string $message |
|
159 | + * @param array $context |
|
160 | + * @return void |
|
161 | + */ |
|
162 | + public function notice($message, array $context = array()) { |
|
163 | + $this->log(Util::INFO, $message, $context); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Interesting events. |
|
168 | + * |
|
169 | + * Example: User logs in, SQL logs. |
|
170 | + * |
|
171 | + * @param string $message |
|
172 | + * @param array $context |
|
173 | + * @return void |
|
174 | + */ |
|
175 | + public function info($message, array $context = array()) { |
|
176 | + $this->log(Util::INFO, $message, $context); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Detailed debug information. |
|
181 | + * |
|
182 | + * @param string $message |
|
183 | + * @param array $context |
|
184 | + * @return void |
|
185 | + */ |
|
186 | + public function debug($message, array $context = array()) { |
|
187 | + $this->log(Util::DEBUG, $message, $context); |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * Logs with an arbitrary level. |
|
193 | + * |
|
194 | + * @param mixed $level |
|
195 | + * @param string $message |
|
196 | + * @param array $context |
|
197 | + * @return void |
|
198 | + */ |
|
199 | + public function log($level, $message, array $context = array()) { |
|
200 | + $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::ERROR); |
|
201 | + $logCondition = $this->config->getValue('log.condition', []); |
|
202 | + |
|
203 | + array_walk($context, [$this->normalizer, 'format']); |
|
204 | + |
|
205 | + if (isset($context['app'])) { |
|
206 | + $app = $context['app']; |
|
207 | + |
|
208 | + /** |
|
209 | + * check log condition based on the context of each log message |
|
210 | + * once this is met -> change the required log level to debug |
|
211 | + */ |
|
212 | + if(!empty($logCondition) |
|
213 | + && isset($logCondition['apps']) |
|
214 | + && in_array($app, $logCondition['apps'], true)) { |
|
215 | + $minLevel = Util::DEBUG; |
|
216 | + } |
|
217 | + |
|
218 | + } else { |
|
219 | + $app = 'no app in context'; |
|
220 | + } |
|
221 | + // interpolate $message as defined in PSR-3 |
|
222 | + $replace = array(); |
|
223 | + foreach ($context as $key => $val) { |
|
224 | + $replace['{' . $key . '}'] = $val; |
|
225 | + } |
|
226 | + |
|
227 | + // interpolate replacement values into the message and return |
|
228 | + $message = strtr($message, $replace); |
|
229 | + |
|
230 | + /** |
|
231 | + * check for a special log condition - this enables an increased log on |
|
232 | + * a per request/user base |
|
233 | + */ |
|
234 | + if($this->logConditionSatisfied === null) { |
|
235 | + // default to false to just process this once per request |
|
236 | + $this->logConditionSatisfied = false; |
|
237 | + if(!empty($logCondition)) { |
|
238 | + |
|
239 | + // check for secret token in the request |
|
240 | + if(isset($logCondition['shared_secret'])) { |
|
241 | + $request = \OC::$server->getRequest(); |
|
242 | + |
|
243 | + // if token is found in the request change set the log condition to satisfied |
|
244 | + if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret'))) { |
|
245 | + $this->logConditionSatisfied = true; |
|
246 | + } |
|
247 | + } |
|
248 | + |
|
249 | + // check for user |
|
250 | + if(isset($logCondition['users'])) { |
|
251 | + $user = \OC::$server->getUserSession()->getUser(); |
|
252 | + |
|
253 | + // if the user matches set the log condition to satisfied |
|
254 | + if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
255 | + $this->logConditionSatisfied = true; |
|
256 | + } |
|
257 | + } |
|
258 | + } |
|
259 | + } |
|
260 | + |
|
261 | + // if log condition is satisfied change the required log level to DEBUG |
|
262 | + if($this->logConditionSatisfied) { |
|
263 | + $minLevel = Util::DEBUG; |
|
264 | + } |
|
265 | + |
|
266 | + if ($level >= $minLevel) { |
|
267 | + $logger = $this->logger; |
|
268 | + call_user_func(array($logger, 'write'), $app, $message, $level); |
|
269 | + } |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Logs an exception very detailed |
|
274 | + * |
|
275 | + * @param \Exception $exception |
|
276 | + * @param array $context |
|
277 | + * @return void |
|
278 | + * @since 8.2.0 |
|
279 | + */ |
|
280 | + public function logException(\Exception $exception, array $context = array()) { |
|
281 | + $exception = array( |
|
282 | + 'Exception' => get_class($exception), |
|
283 | + 'Message' => $exception->getMessage(), |
|
284 | + 'Code' => $exception->getCode(), |
|
285 | + 'Trace' => $exception->getTraceAsString(), |
|
286 | + 'File' => $exception->getFile(), |
|
287 | + 'Line' => $exception->getLine(), |
|
288 | + ); |
|
289 | + $exception['Trace'] = preg_replace('!(login|checkPassword|updatePrivateKeyPassword|validateUserPass)\(.*\)!', '$1(*** username and password replaced ***)', $exception['Trace']); |
|
290 | + $msg = isset($context['message']) ? $context['message'] : 'Exception'; |
|
291 | + $msg .= ': ' . json_encode($exception); |
|
292 | + $this->error($msg, $context); |
|
293 | + } |
|
294 | 294 | } |
@@ -65,16 +65,16 @@ discard block |
||
65 | 65 | * @param SystemConfig $config the system config object |
66 | 66 | * @param null $normalizer |
67 | 67 | */ |
68 | - public function __construct($logger=null, SystemConfig $config=null, $normalizer = null) { |
|
68 | + public function __construct($logger = null, SystemConfig $config = null, $normalizer = null) { |
|
69 | 69 | // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
70 | - if($config === null) { |
|
70 | + if ($config === null) { |
|
71 | 71 | $config = \OC::$server->getSystemConfig(); |
72 | 72 | } |
73 | 73 | |
74 | 74 | $this->config = $config; |
75 | 75 | |
76 | 76 | // FIXME: Add this for backwards compatibility, should be fixed at some point probably |
77 | - if($logger === null) { |
|
77 | + if ($logger === null) { |
|
78 | 78 | $this->logger = 'OC_Log_'.ucfirst($this->config->getValue('log_type', 'owncloud')); |
79 | 79 | call_user_func(array($this->logger, 'init')); |
80 | 80 | } else { |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | * check log condition based on the context of each log message |
210 | 210 | * once this is met -> change the required log level to debug |
211 | 211 | */ |
212 | - if(!empty($logCondition) |
|
212 | + if (!empty($logCondition) |
|
213 | 213 | && isset($logCondition['apps']) |
214 | 214 | && in_array($app, $logCondition['apps'], true)) { |
215 | 215 | $minLevel = Util::DEBUG; |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | // interpolate $message as defined in PSR-3 |
222 | 222 | $replace = array(); |
223 | 223 | foreach ($context as $key => $val) { |
224 | - $replace['{' . $key . '}'] = $val; |
|
224 | + $replace['{'.$key.'}'] = $val; |
|
225 | 225 | } |
226 | 226 | |
227 | 227 | // interpolate replacement values into the message and return |
@@ -231,27 +231,27 @@ discard block |
||
231 | 231 | * check for a special log condition - this enables an increased log on |
232 | 232 | * a per request/user base |
233 | 233 | */ |
234 | - if($this->logConditionSatisfied === null) { |
|
234 | + if ($this->logConditionSatisfied === null) { |
|
235 | 235 | // default to false to just process this once per request |
236 | 236 | $this->logConditionSatisfied = false; |
237 | - if(!empty($logCondition)) { |
|
237 | + if (!empty($logCondition)) { |
|
238 | 238 | |
239 | 239 | // check for secret token in the request |
240 | - if(isset($logCondition['shared_secret'])) { |
|
240 | + if (isset($logCondition['shared_secret'])) { |
|
241 | 241 | $request = \OC::$server->getRequest(); |
242 | 242 | |
243 | 243 | // if token is found in the request change set the log condition to satisfied |
244 | - if($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret'))) { |
|
244 | + if ($request && hash_equals($logCondition['shared_secret'], $request->getParam('log_secret'))) { |
|
245 | 245 | $this->logConditionSatisfied = true; |
246 | 246 | } |
247 | 247 | } |
248 | 248 | |
249 | 249 | // check for user |
250 | - if(isset($logCondition['users'])) { |
|
250 | + if (isset($logCondition['users'])) { |
|
251 | 251 | $user = \OC::$server->getUserSession()->getUser(); |
252 | 252 | |
253 | 253 | // if the user matches set the log condition to satisfied |
254 | - if($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
254 | + if ($user !== null && in_array($user->getUID(), $logCondition['users'], true)) { |
|
255 | 255 | $this->logConditionSatisfied = true; |
256 | 256 | } |
257 | 257 | } |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | } |
260 | 260 | |
261 | 261 | // if log condition is satisfied change the required log level to DEBUG |
262 | - if($this->logConditionSatisfied) { |
|
262 | + if ($this->logConditionSatisfied) { |
|
263 | 263 | $minLevel = Util::DEBUG; |
264 | 264 | } |
265 | 265 | |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | ); |
289 | 289 | $exception['Trace'] = preg_replace('!(login|checkPassword|updatePrivateKeyPassword|validateUserPass)\(.*\)!', '$1(*** username and password replaced ***)', $exception['Trace']); |
290 | 290 | $msg = isset($context['message']) ? $context['message'] : 'Exception'; |
291 | - $msg .= ': ' . json_encode($exception); |
|
291 | + $msg .= ': '.json_encode($exception); |
|
292 | 292 | $this->error($msg, $context); |
293 | 293 | } |
294 | 294 | } |
@@ -29,356 +29,356 @@ |
||
29 | 29 | |
30 | 30 | class Comment implements IComment { |
31 | 31 | |
32 | - protected $data = [ |
|
33 | - 'id' => '', |
|
34 | - 'parentId' => '0', |
|
35 | - 'topmostParentId' => '0', |
|
36 | - 'childrenCount' => '0', |
|
37 | - 'message' => '', |
|
38 | - 'verb' => '', |
|
39 | - 'actorType' => '', |
|
40 | - 'actorId' => '', |
|
41 | - 'objectType' => '', |
|
42 | - 'objectId' => '', |
|
43 | - 'creationDT' => null, |
|
44 | - 'latestChildDT' => null, |
|
45 | - ]; |
|
46 | - |
|
47 | - /** |
|
48 | - * Comment constructor. |
|
49 | - * |
|
50 | - * @param [] $data optional, array with keys according to column names from |
|
51 | - * the comments database scheme |
|
52 | - */ |
|
53 | - public function __construct(array $data = null) { |
|
54 | - if(is_array($data)) { |
|
55 | - $this->fromArray($data); |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * returns the ID of the comment |
|
61 | - * |
|
62 | - * It may return an empty string, if the comment was not stored. |
|
63 | - * It is expected that the concrete Comment implementation gives an ID |
|
64 | - * by itself (e.g. after saving). |
|
65 | - * |
|
66 | - * @return string |
|
67 | - * @since 9.0.0 |
|
68 | - */ |
|
69 | - public function getId() { |
|
70 | - return $this->data['id']; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * sets the ID of the comment and returns itself |
|
75 | - * |
|
76 | - * It is only allowed to set the ID only, if the current id is an empty |
|
77 | - * string (which means it is not stored in a database, storage or whatever |
|
78 | - * the concrete implementation does), or vice versa. Changing a given ID is |
|
79 | - * not permitted and must result in an IllegalIDChangeException. |
|
80 | - * |
|
81 | - * @param string $id |
|
82 | - * @return IComment |
|
83 | - * @throws IllegalIDChangeException |
|
84 | - * @since 9.0.0 |
|
85 | - */ |
|
86 | - public function setId($id) { |
|
87 | - if(!is_string($id)) { |
|
88 | - throw new \InvalidArgumentException('String expected.'); |
|
89 | - } |
|
90 | - |
|
91 | - $id = trim($id); |
|
92 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
93 | - $this->data['id'] = $id; |
|
94 | - return $this; |
|
95 | - } |
|
96 | - |
|
97 | - throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * returns the parent ID of the comment |
|
102 | - * |
|
103 | - * @return string |
|
104 | - * @since 9.0.0 |
|
105 | - */ |
|
106 | - public function getParentId() { |
|
107 | - return $this->data['parentId']; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * sets the parent ID and returns itself |
|
112 | - * |
|
113 | - * @param string $parentId |
|
114 | - * @return IComment |
|
115 | - * @since 9.0.0 |
|
116 | - */ |
|
117 | - public function setParentId($parentId) { |
|
118 | - if(!is_string($parentId)) { |
|
119 | - throw new \InvalidArgumentException('String expected.'); |
|
120 | - } |
|
121 | - $this->data['parentId'] = trim($parentId); |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * returns the topmost parent ID of the comment |
|
127 | - * |
|
128 | - * @return string |
|
129 | - * @since 9.0.0 |
|
130 | - */ |
|
131 | - public function getTopmostParentId() { |
|
132 | - return $this->data['topmostParentId']; |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * sets the topmost parent ID and returns itself |
|
138 | - * |
|
139 | - * @param string $id |
|
140 | - * @return IComment |
|
141 | - * @since 9.0.0 |
|
142 | - */ |
|
143 | - public function setTopmostParentId($id) { |
|
144 | - if(!is_string($id)) { |
|
145 | - throw new \InvalidArgumentException('String expected.'); |
|
146 | - } |
|
147 | - $this->data['topmostParentId'] = trim($id); |
|
148 | - return $this; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * returns the number of children |
|
153 | - * |
|
154 | - * @return int |
|
155 | - * @since 9.0.0 |
|
156 | - */ |
|
157 | - public function getChildrenCount() { |
|
158 | - return $this->data['childrenCount']; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * sets the number of children |
|
163 | - * |
|
164 | - * @param int $count |
|
165 | - * @return IComment |
|
166 | - * @since 9.0.0 |
|
167 | - */ |
|
168 | - public function setChildrenCount($count) { |
|
169 | - if(!is_int($count)) { |
|
170 | - throw new \InvalidArgumentException('Integer expected.'); |
|
171 | - } |
|
172 | - $this->data['childrenCount'] = $count; |
|
173 | - return $this; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * returns the message of the comment |
|
178 | - * |
|
179 | - * @return string |
|
180 | - * @since 9.0.0 |
|
181 | - */ |
|
182 | - public function getMessage() { |
|
183 | - return $this->data['message']; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * sets the message of the comment and returns itself |
|
188 | - * |
|
189 | - * @param string $message |
|
190 | - * @return IComment |
|
191 | - * @throws MessageTooLongException |
|
192 | - * @since 9.0.0 |
|
193 | - */ |
|
194 | - public function setMessage($message) { |
|
195 | - if(!is_string($message)) { |
|
196 | - throw new \InvalidArgumentException('String expected.'); |
|
197 | - } |
|
198 | - $message = trim($message); |
|
199 | - if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
200 | - throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); |
|
201 | - } |
|
202 | - $this->data['message'] = $message; |
|
203 | - return $this; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * returns the verb of the comment |
|
208 | - * |
|
209 | - * @return string |
|
210 | - * @since 9.0.0 |
|
211 | - */ |
|
212 | - public function getVerb() { |
|
213 | - return $this->data['verb']; |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * sets the verb of the comment, e.g. 'comment' or 'like' |
|
218 | - * |
|
219 | - * @param string $verb |
|
220 | - * @return IComment |
|
221 | - * @since 9.0.0 |
|
222 | - */ |
|
223 | - public function setVerb($verb) { |
|
224 | - if(!is_string($verb) || !trim($verb)) { |
|
225 | - throw new \InvalidArgumentException('Non-empty String expected.'); |
|
226 | - } |
|
227 | - $this->data['verb'] = trim($verb); |
|
228 | - return $this; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * returns the actor type |
|
233 | - * |
|
234 | - * @return string |
|
235 | - * @since 9.0.0 |
|
236 | - */ |
|
237 | - public function getActorType() { |
|
238 | - return $this->data['actorType']; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * returns the actor ID |
|
243 | - * |
|
244 | - * @return string |
|
245 | - * @since 9.0.0 |
|
246 | - */ |
|
247 | - public function getActorId() { |
|
248 | - return $this->data['actorId']; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * sets (overwrites) the actor type and id |
|
253 | - * |
|
254 | - * @param string $actorType e.g. 'users' |
|
255 | - * @param string $actorId e.g. 'zombie234' |
|
256 | - * @return IComment |
|
257 | - * @since 9.0.0 |
|
258 | - */ |
|
259 | - public function setActor($actorType, $actorId) { |
|
260 | - if( |
|
261 | - !is_string($actorType) || !trim($actorType) |
|
262 | - || !is_string($actorId) || !trim($actorId) |
|
263 | - ) { |
|
264 | - throw new \InvalidArgumentException('String expected.'); |
|
265 | - } |
|
266 | - $this->data['actorType'] = trim($actorType); |
|
267 | - $this->data['actorId'] = trim($actorId); |
|
268 | - return $this; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * returns the creation date of the comment. |
|
273 | - * |
|
274 | - * If not explicitly set, it shall default to the time of initialization. |
|
275 | - * |
|
276 | - * @return \DateTime |
|
277 | - * @since 9.0.0 |
|
278 | - */ |
|
279 | - public function getCreationDateTime() { |
|
280 | - return $this->data['creationDT']; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * sets the creation date of the comment and returns itself |
|
285 | - * |
|
286 | - * @param \DateTime $timestamp |
|
287 | - * @return IComment |
|
288 | - * @since 9.0.0 |
|
289 | - */ |
|
290 | - public function setCreationDateTime(\DateTime $timestamp) { |
|
291 | - $this->data['creationDT'] = $timestamp; |
|
292 | - return $this; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * returns the DateTime of the most recent child, if set, otherwise null |
|
297 | - * |
|
298 | - * @return \DateTime|null |
|
299 | - * @since 9.0.0 |
|
300 | - */ |
|
301 | - public function getLatestChildDateTime() { |
|
302 | - return $this->data['latestChildDT']; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * sets the date of the most recent child |
|
307 | - * |
|
308 | - * @param \DateTime $dateTime |
|
309 | - * @return IComment |
|
310 | - * @since 9.0.0 |
|
311 | - */ |
|
312 | - public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
313 | - $this->data['latestChildDT'] = $dateTime; |
|
314 | - return $this; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * returns the object type the comment is attached to |
|
319 | - * |
|
320 | - * @return string |
|
321 | - * @since 9.0.0 |
|
322 | - */ |
|
323 | - public function getObjectType() { |
|
324 | - return $this->data['objectType']; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * returns the object id the comment is attached to |
|
329 | - * |
|
330 | - * @return string |
|
331 | - * @since 9.0.0 |
|
332 | - */ |
|
333 | - public function getObjectId() { |
|
334 | - return $this->data['objectId']; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * sets (overwrites) the object of the comment |
|
339 | - * |
|
340 | - * @param string $objectType e.g. 'files' |
|
341 | - * @param string $objectId e.g. '16435' |
|
342 | - * @return IComment |
|
343 | - * @since 9.0.0 |
|
344 | - */ |
|
345 | - public function setObject($objectType, $objectId) { |
|
346 | - if( |
|
347 | - !is_string($objectType) || !trim($objectType) |
|
348 | - || !is_string($objectId) || !trim($objectId) |
|
349 | - ) { |
|
350 | - throw new \InvalidArgumentException('String expected.'); |
|
351 | - } |
|
352 | - $this->data['objectType'] = trim($objectType); |
|
353 | - $this->data['objectId'] = trim($objectId); |
|
354 | - return $this; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * sets the comment data based on an array with keys as taken from the |
|
359 | - * database. |
|
360 | - * |
|
361 | - * @param [] $data |
|
362 | - * @return IComment |
|
363 | - */ |
|
364 | - protected function fromArray($data) { |
|
365 | - foreach(array_keys($data) as $key) { |
|
366 | - // translate DB keys to internal setter names |
|
367 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
368 | - $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
369 | - |
|
370 | - if(method_exists($this, $setter)) { |
|
371 | - $this->$setter($data[$key]); |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - foreach(['actor', 'object'] as $role) { |
|
376 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
377 | - $setter = 'set' . ucfirst($role); |
|
378 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
379 | - } |
|
380 | - } |
|
381 | - |
|
382 | - return $this; |
|
383 | - } |
|
32 | + protected $data = [ |
|
33 | + 'id' => '', |
|
34 | + 'parentId' => '0', |
|
35 | + 'topmostParentId' => '0', |
|
36 | + 'childrenCount' => '0', |
|
37 | + 'message' => '', |
|
38 | + 'verb' => '', |
|
39 | + 'actorType' => '', |
|
40 | + 'actorId' => '', |
|
41 | + 'objectType' => '', |
|
42 | + 'objectId' => '', |
|
43 | + 'creationDT' => null, |
|
44 | + 'latestChildDT' => null, |
|
45 | + ]; |
|
46 | + |
|
47 | + /** |
|
48 | + * Comment constructor. |
|
49 | + * |
|
50 | + * @param [] $data optional, array with keys according to column names from |
|
51 | + * the comments database scheme |
|
52 | + */ |
|
53 | + public function __construct(array $data = null) { |
|
54 | + if(is_array($data)) { |
|
55 | + $this->fromArray($data); |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * returns the ID of the comment |
|
61 | + * |
|
62 | + * It may return an empty string, if the comment was not stored. |
|
63 | + * It is expected that the concrete Comment implementation gives an ID |
|
64 | + * by itself (e.g. after saving). |
|
65 | + * |
|
66 | + * @return string |
|
67 | + * @since 9.0.0 |
|
68 | + */ |
|
69 | + public function getId() { |
|
70 | + return $this->data['id']; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * sets the ID of the comment and returns itself |
|
75 | + * |
|
76 | + * It is only allowed to set the ID only, if the current id is an empty |
|
77 | + * string (which means it is not stored in a database, storage or whatever |
|
78 | + * the concrete implementation does), or vice versa. Changing a given ID is |
|
79 | + * not permitted and must result in an IllegalIDChangeException. |
|
80 | + * |
|
81 | + * @param string $id |
|
82 | + * @return IComment |
|
83 | + * @throws IllegalIDChangeException |
|
84 | + * @since 9.0.0 |
|
85 | + */ |
|
86 | + public function setId($id) { |
|
87 | + if(!is_string($id)) { |
|
88 | + throw new \InvalidArgumentException('String expected.'); |
|
89 | + } |
|
90 | + |
|
91 | + $id = trim($id); |
|
92 | + if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
93 | + $this->data['id'] = $id; |
|
94 | + return $this; |
|
95 | + } |
|
96 | + |
|
97 | + throw new IllegalIDChangeException('Not allowed to assign a new ID to an already saved comment.'); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * returns the parent ID of the comment |
|
102 | + * |
|
103 | + * @return string |
|
104 | + * @since 9.0.0 |
|
105 | + */ |
|
106 | + public function getParentId() { |
|
107 | + return $this->data['parentId']; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * sets the parent ID and returns itself |
|
112 | + * |
|
113 | + * @param string $parentId |
|
114 | + * @return IComment |
|
115 | + * @since 9.0.0 |
|
116 | + */ |
|
117 | + public function setParentId($parentId) { |
|
118 | + if(!is_string($parentId)) { |
|
119 | + throw new \InvalidArgumentException('String expected.'); |
|
120 | + } |
|
121 | + $this->data['parentId'] = trim($parentId); |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * returns the topmost parent ID of the comment |
|
127 | + * |
|
128 | + * @return string |
|
129 | + * @since 9.0.0 |
|
130 | + */ |
|
131 | + public function getTopmostParentId() { |
|
132 | + return $this->data['topmostParentId']; |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * sets the topmost parent ID and returns itself |
|
138 | + * |
|
139 | + * @param string $id |
|
140 | + * @return IComment |
|
141 | + * @since 9.0.0 |
|
142 | + */ |
|
143 | + public function setTopmostParentId($id) { |
|
144 | + if(!is_string($id)) { |
|
145 | + throw new \InvalidArgumentException('String expected.'); |
|
146 | + } |
|
147 | + $this->data['topmostParentId'] = trim($id); |
|
148 | + return $this; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * returns the number of children |
|
153 | + * |
|
154 | + * @return int |
|
155 | + * @since 9.0.0 |
|
156 | + */ |
|
157 | + public function getChildrenCount() { |
|
158 | + return $this->data['childrenCount']; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * sets the number of children |
|
163 | + * |
|
164 | + * @param int $count |
|
165 | + * @return IComment |
|
166 | + * @since 9.0.0 |
|
167 | + */ |
|
168 | + public function setChildrenCount($count) { |
|
169 | + if(!is_int($count)) { |
|
170 | + throw new \InvalidArgumentException('Integer expected.'); |
|
171 | + } |
|
172 | + $this->data['childrenCount'] = $count; |
|
173 | + return $this; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * returns the message of the comment |
|
178 | + * |
|
179 | + * @return string |
|
180 | + * @since 9.0.0 |
|
181 | + */ |
|
182 | + public function getMessage() { |
|
183 | + return $this->data['message']; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * sets the message of the comment and returns itself |
|
188 | + * |
|
189 | + * @param string $message |
|
190 | + * @return IComment |
|
191 | + * @throws MessageTooLongException |
|
192 | + * @since 9.0.0 |
|
193 | + */ |
|
194 | + public function setMessage($message) { |
|
195 | + if(!is_string($message)) { |
|
196 | + throw new \InvalidArgumentException('String expected.'); |
|
197 | + } |
|
198 | + $message = trim($message); |
|
199 | + if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
200 | + throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); |
|
201 | + } |
|
202 | + $this->data['message'] = $message; |
|
203 | + return $this; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * returns the verb of the comment |
|
208 | + * |
|
209 | + * @return string |
|
210 | + * @since 9.0.0 |
|
211 | + */ |
|
212 | + public function getVerb() { |
|
213 | + return $this->data['verb']; |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * sets the verb of the comment, e.g. 'comment' or 'like' |
|
218 | + * |
|
219 | + * @param string $verb |
|
220 | + * @return IComment |
|
221 | + * @since 9.0.0 |
|
222 | + */ |
|
223 | + public function setVerb($verb) { |
|
224 | + if(!is_string($verb) || !trim($verb)) { |
|
225 | + throw new \InvalidArgumentException('Non-empty String expected.'); |
|
226 | + } |
|
227 | + $this->data['verb'] = trim($verb); |
|
228 | + return $this; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * returns the actor type |
|
233 | + * |
|
234 | + * @return string |
|
235 | + * @since 9.0.0 |
|
236 | + */ |
|
237 | + public function getActorType() { |
|
238 | + return $this->data['actorType']; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * returns the actor ID |
|
243 | + * |
|
244 | + * @return string |
|
245 | + * @since 9.0.0 |
|
246 | + */ |
|
247 | + public function getActorId() { |
|
248 | + return $this->data['actorId']; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * sets (overwrites) the actor type and id |
|
253 | + * |
|
254 | + * @param string $actorType e.g. 'users' |
|
255 | + * @param string $actorId e.g. 'zombie234' |
|
256 | + * @return IComment |
|
257 | + * @since 9.0.0 |
|
258 | + */ |
|
259 | + public function setActor($actorType, $actorId) { |
|
260 | + if( |
|
261 | + !is_string($actorType) || !trim($actorType) |
|
262 | + || !is_string($actorId) || !trim($actorId) |
|
263 | + ) { |
|
264 | + throw new \InvalidArgumentException('String expected.'); |
|
265 | + } |
|
266 | + $this->data['actorType'] = trim($actorType); |
|
267 | + $this->data['actorId'] = trim($actorId); |
|
268 | + return $this; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * returns the creation date of the comment. |
|
273 | + * |
|
274 | + * If not explicitly set, it shall default to the time of initialization. |
|
275 | + * |
|
276 | + * @return \DateTime |
|
277 | + * @since 9.0.0 |
|
278 | + */ |
|
279 | + public function getCreationDateTime() { |
|
280 | + return $this->data['creationDT']; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * sets the creation date of the comment and returns itself |
|
285 | + * |
|
286 | + * @param \DateTime $timestamp |
|
287 | + * @return IComment |
|
288 | + * @since 9.0.0 |
|
289 | + */ |
|
290 | + public function setCreationDateTime(\DateTime $timestamp) { |
|
291 | + $this->data['creationDT'] = $timestamp; |
|
292 | + return $this; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * returns the DateTime of the most recent child, if set, otherwise null |
|
297 | + * |
|
298 | + * @return \DateTime|null |
|
299 | + * @since 9.0.0 |
|
300 | + */ |
|
301 | + public function getLatestChildDateTime() { |
|
302 | + return $this->data['latestChildDT']; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * sets the date of the most recent child |
|
307 | + * |
|
308 | + * @param \DateTime $dateTime |
|
309 | + * @return IComment |
|
310 | + * @since 9.0.0 |
|
311 | + */ |
|
312 | + public function setLatestChildDateTime(\DateTime $dateTime = null) { |
|
313 | + $this->data['latestChildDT'] = $dateTime; |
|
314 | + return $this; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * returns the object type the comment is attached to |
|
319 | + * |
|
320 | + * @return string |
|
321 | + * @since 9.0.0 |
|
322 | + */ |
|
323 | + public function getObjectType() { |
|
324 | + return $this->data['objectType']; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * returns the object id the comment is attached to |
|
329 | + * |
|
330 | + * @return string |
|
331 | + * @since 9.0.0 |
|
332 | + */ |
|
333 | + public function getObjectId() { |
|
334 | + return $this->data['objectId']; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * sets (overwrites) the object of the comment |
|
339 | + * |
|
340 | + * @param string $objectType e.g. 'files' |
|
341 | + * @param string $objectId e.g. '16435' |
|
342 | + * @return IComment |
|
343 | + * @since 9.0.0 |
|
344 | + */ |
|
345 | + public function setObject($objectType, $objectId) { |
|
346 | + if( |
|
347 | + !is_string($objectType) || !trim($objectType) |
|
348 | + || !is_string($objectId) || !trim($objectId) |
|
349 | + ) { |
|
350 | + throw new \InvalidArgumentException('String expected.'); |
|
351 | + } |
|
352 | + $this->data['objectType'] = trim($objectType); |
|
353 | + $this->data['objectId'] = trim($objectId); |
|
354 | + return $this; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * sets the comment data based on an array with keys as taken from the |
|
359 | + * database. |
|
360 | + * |
|
361 | + * @param [] $data |
|
362 | + * @return IComment |
|
363 | + */ |
|
364 | + protected function fromArray($data) { |
|
365 | + foreach(array_keys($data) as $key) { |
|
366 | + // translate DB keys to internal setter names |
|
367 | + $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
368 | + $setter = str_replace('Timestamp', 'DateTime', $setter); |
|
369 | + |
|
370 | + if(method_exists($this, $setter)) { |
|
371 | + $this->$setter($data[$key]); |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + foreach(['actor', 'object'] as $role) { |
|
376 | + if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
377 | + $setter = 'set' . ucfirst($role); |
|
378 | + $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
379 | + } |
|
380 | + } |
|
381 | + |
|
382 | + return $this; |
|
383 | + } |
|
384 | 384 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * the comments database scheme |
52 | 52 | */ |
53 | 53 | public function __construct(array $data = null) { |
54 | - if(is_array($data)) { |
|
54 | + if (is_array($data)) { |
|
55 | 55 | $this->fromArray($data); |
56 | 56 | } |
57 | 57 | } |
@@ -84,12 +84,12 @@ discard block |
||
84 | 84 | * @since 9.0.0 |
85 | 85 | */ |
86 | 86 | public function setId($id) { |
87 | - if(!is_string($id)) { |
|
87 | + if (!is_string($id)) { |
|
88 | 88 | throw new \InvalidArgumentException('String expected.'); |
89 | 89 | } |
90 | 90 | |
91 | 91 | $id = trim($id); |
92 | - if($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
92 | + if ($this->data['id'] === '' || ($this->data['id'] !== '' && $id === '')) { |
|
93 | 93 | $this->data['id'] = $id; |
94 | 94 | return $this; |
95 | 95 | } |
@@ -115,7 +115,7 @@ discard block |
||
115 | 115 | * @since 9.0.0 |
116 | 116 | */ |
117 | 117 | public function setParentId($parentId) { |
118 | - if(!is_string($parentId)) { |
|
118 | + if (!is_string($parentId)) { |
|
119 | 119 | throw new \InvalidArgumentException('String expected.'); |
120 | 120 | } |
121 | 121 | $this->data['parentId'] = trim($parentId); |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | * @since 9.0.0 |
142 | 142 | */ |
143 | 143 | public function setTopmostParentId($id) { |
144 | - if(!is_string($id)) { |
|
144 | + if (!is_string($id)) { |
|
145 | 145 | throw new \InvalidArgumentException('String expected.'); |
146 | 146 | } |
147 | 147 | $this->data['topmostParentId'] = trim($id); |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | * @since 9.0.0 |
167 | 167 | */ |
168 | 168 | public function setChildrenCount($count) { |
169 | - if(!is_int($count)) { |
|
169 | + if (!is_int($count)) { |
|
170 | 170 | throw new \InvalidArgumentException('Integer expected.'); |
171 | 171 | } |
172 | 172 | $this->data['childrenCount'] = $count; |
@@ -192,12 +192,12 @@ discard block |
||
192 | 192 | * @since 9.0.0 |
193 | 193 | */ |
194 | 194 | public function setMessage($message) { |
195 | - if(!is_string($message)) { |
|
195 | + if (!is_string($message)) { |
|
196 | 196 | throw new \InvalidArgumentException('String expected.'); |
197 | 197 | } |
198 | 198 | $message = trim($message); |
199 | - if(mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
200 | - throw new MessageTooLongException('Comment message must not exceed ' . IComment::MAX_MESSAGE_LENGTH . ' characters'); |
|
199 | + if (mb_strlen($message, 'UTF-8') > IComment::MAX_MESSAGE_LENGTH) { |
|
200 | + throw new MessageTooLongException('Comment message must not exceed '.IComment::MAX_MESSAGE_LENGTH.' characters'); |
|
201 | 201 | } |
202 | 202 | $this->data['message'] = $message; |
203 | 203 | return $this; |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | * @since 9.0.0 |
222 | 222 | */ |
223 | 223 | public function setVerb($verb) { |
224 | - if(!is_string($verb) || !trim($verb)) { |
|
224 | + if (!is_string($verb) || !trim($verb)) { |
|
225 | 225 | throw new \InvalidArgumentException('Non-empty String expected.'); |
226 | 226 | } |
227 | 227 | $this->data['verb'] = trim($verb); |
@@ -257,9 +257,9 @@ discard block |
||
257 | 257 | * @since 9.0.0 |
258 | 258 | */ |
259 | 259 | public function setActor($actorType, $actorId) { |
260 | - if( |
|
260 | + if ( |
|
261 | 261 | !is_string($actorType) || !trim($actorType) |
262 | - || !is_string($actorId) || !trim($actorId) |
|
262 | + || !is_string($actorId) || !trim($actorId) |
|
263 | 263 | ) { |
264 | 264 | throw new \InvalidArgumentException('String expected.'); |
265 | 265 | } |
@@ -343,9 +343,9 @@ discard block |
||
343 | 343 | * @since 9.0.0 |
344 | 344 | */ |
345 | 345 | public function setObject($objectType, $objectId) { |
346 | - if( |
|
346 | + if ( |
|
347 | 347 | !is_string($objectType) || !trim($objectType) |
348 | - || !is_string($objectId) || !trim($objectId) |
|
348 | + || !is_string($objectId) || !trim($objectId) |
|
349 | 349 | ) { |
350 | 350 | throw new \InvalidArgumentException('String expected.'); |
351 | 351 | } |
@@ -362,20 +362,20 @@ discard block |
||
362 | 362 | * @return IComment |
363 | 363 | */ |
364 | 364 | protected function fromArray($data) { |
365 | - foreach(array_keys($data) as $key) { |
|
365 | + foreach (array_keys($data) as $key) { |
|
366 | 366 | // translate DB keys to internal setter names |
367 | - $setter = 'set' . implode('', array_map('ucfirst', explode('_', $key))); |
|
367 | + $setter = 'set'.implode('', array_map('ucfirst', explode('_', $key))); |
|
368 | 368 | $setter = str_replace('Timestamp', 'DateTime', $setter); |
369 | 369 | |
370 | - if(method_exists($this, $setter)) { |
|
370 | + if (method_exists($this, $setter)) { |
|
371 | 371 | $this->$setter($data[$key]); |
372 | 372 | } |
373 | 373 | } |
374 | 374 | |
375 | - foreach(['actor', 'object'] as $role) { |
|
376 | - if(isset($data[$role . '_type']) && isset($data[$role . '_id'])) { |
|
377 | - $setter = 'set' . ucfirst($role); |
|
378 | - $this->$setter($data[$role . '_type'], $data[$role . '_id']); |
|
375 | + foreach (['actor', 'object'] as $role) { |
|
376 | + if (isset($data[$role.'_type']) && isset($data[$role.'_id'])) { |
|
377 | + $setter = 'set'.ucfirst($role); |
|
378 | + $this->$setter($data[$role.'_type'], $data[$role.'_id']); |
|
379 | 379 | } |
380 | 380 | } |
381 | 381 |