@@ -21,4 +21,4 @@ |
||
21 | 21 | * |
22 | 22 | */ |
23 | 23 | |
24 | - # used for Personal/Additional settings as fallback for legacy settings |
|
24 | + # used for Personal/Additional settings as fallback for legacy settings |
@@ -143,7 +143,7 @@ |
||
143 | 143 | } elseif ($existing === 'exclusive') { |
144 | 144 | return $existing; |
145 | 145 | } else { |
146 | - return $existing . ' shared locks'; |
|
146 | + return $existing.' shared locks'; |
|
147 | 147 | } |
148 | 148 | } |
149 | 149 | } |
@@ -32,95 +32,95 @@ |
||
32 | 32 | use OCP\Lock\LockedException; |
33 | 33 | |
34 | 34 | class MemcacheLockingProvider extends AbstractLockingProvider { |
35 | - private IMemcache $memcache; |
|
35 | + private IMemcache $memcache; |
|
36 | 36 | |
37 | - public function __construct(IMemcache $memcache, int $ttl = 3600) { |
|
38 | - $this->memcache = $memcache; |
|
39 | - $this->ttl = $ttl; |
|
40 | - } |
|
37 | + public function __construct(IMemcache $memcache, int $ttl = 3600) { |
|
38 | + $this->memcache = $memcache; |
|
39 | + $this->ttl = $ttl; |
|
40 | + } |
|
41 | 41 | |
42 | - private function setTTL(string $path): void { |
|
43 | - if ($this->memcache instanceof IMemcacheTTL) { |
|
44 | - $this->memcache->setTTL($path, $this->ttl); |
|
45 | - } |
|
46 | - } |
|
42 | + private function setTTL(string $path): void { |
|
43 | + if ($this->memcache instanceof IMemcacheTTL) { |
|
44 | + $this->memcache->setTTL($path, $this->ttl); |
|
45 | + } |
|
46 | + } |
|
47 | 47 | |
48 | - public function isLocked(string $path, int $type): bool { |
|
49 | - $lockValue = $this->memcache->get($path); |
|
50 | - if ($type === self::LOCK_SHARED) { |
|
51 | - return is_int($lockValue) && $lockValue > 0; |
|
52 | - } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
53 | - return $lockValue === 'exclusive'; |
|
54 | - } else { |
|
55 | - return false; |
|
56 | - } |
|
57 | - } |
|
48 | + public function isLocked(string $path, int $type): bool { |
|
49 | + $lockValue = $this->memcache->get($path); |
|
50 | + if ($type === self::LOCK_SHARED) { |
|
51 | + return is_int($lockValue) && $lockValue > 0; |
|
52 | + } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
53 | + return $lockValue === 'exclusive'; |
|
54 | + } else { |
|
55 | + return false; |
|
56 | + } |
|
57 | + } |
|
58 | 58 | |
59 | - public function acquireLock(string $path, int $type, ?string $readablePath = null): void { |
|
60 | - if ($type === self::LOCK_SHARED) { |
|
61 | - if (!$this->memcache->inc($path)) { |
|
62 | - throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
63 | - } |
|
64 | - } else { |
|
65 | - $this->memcache->add($path, 0); |
|
66 | - if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
67 | - throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
68 | - } |
|
69 | - } |
|
70 | - $this->setTTL($path); |
|
71 | - $this->markAcquire($path, $type); |
|
72 | - } |
|
59 | + public function acquireLock(string $path, int $type, ?string $readablePath = null): void { |
|
60 | + if ($type === self::LOCK_SHARED) { |
|
61 | + if (!$this->memcache->inc($path)) { |
|
62 | + throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
63 | + } |
|
64 | + } else { |
|
65 | + $this->memcache->add($path, 0); |
|
66 | + if (!$this->memcache->cas($path, 0, 'exclusive')) { |
|
67 | + throw new LockedException($path, null, $this->getExistingLockForException($path), $readablePath); |
|
68 | + } |
|
69 | + } |
|
70 | + $this->setTTL($path); |
|
71 | + $this->markAcquire($path, $type); |
|
72 | + } |
|
73 | 73 | |
74 | - public function releaseLock(string $path, int $type): void { |
|
75 | - if ($type === self::LOCK_SHARED) { |
|
76 | - $ownSharedLockCount = $this->getOwnSharedLockCount($path); |
|
77 | - $newValue = 0; |
|
78 | - if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it |
|
79 | - return; |
|
80 | - } |
|
81 | - if ($ownSharedLockCount === 1) { |
|
82 | - $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go |
|
83 | - if (!$removed) { //someone else also has a shared lock, decrease only |
|
84 | - $newValue = $this->memcache->dec($path); |
|
85 | - } |
|
86 | - } else { |
|
87 | - // if we own more than one lock ourselves just decrease |
|
88 | - $newValue = $this->memcache->dec($path); |
|
89 | - } |
|
74 | + public function releaseLock(string $path, int $type): void { |
|
75 | + if ($type === self::LOCK_SHARED) { |
|
76 | + $ownSharedLockCount = $this->getOwnSharedLockCount($path); |
|
77 | + $newValue = 0; |
|
78 | + if ($ownSharedLockCount === 0) { // if we are not holding the lock, don't try to release it |
|
79 | + return; |
|
80 | + } |
|
81 | + if ($ownSharedLockCount === 1) { |
|
82 | + $removed = $this->memcache->cad($path, 1); // if we're the only one having a shared lock we can remove it in one go |
|
83 | + if (!$removed) { //someone else also has a shared lock, decrease only |
|
84 | + $newValue = $this->memcache->dec($path); |
|
85 | + } |
|
86 | + } else { |
|
87 | + // if we own more than one lock ourselves just decrease |
|
88 | + $newValue = $this->memcache->dec($path); |
|
89 | + } |
|
90 | 90 | |
91 | - // if we somehow release more locks then exists, reset the lock |
|
92 | - if ($newValue < 0) { |
|
93 | - $this->memcache->cad($path, $newValue); |
|
94 | - } |
|
95 | - } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
96 | - $this->memcache->cad($path, 'exclusive'); |
|
97 | - } |
|
98 | - $this->markRelease($path, $type); |
|
99 | - } |
|
91 | + // if we somehow release more locks then exists, reset the lock |
|
92 | + if ($newValue < 0) { |
|
93 | + $this->memcache->cad($path, $newValue); |
|
94 | + } |
|
95 | + } elseif ($type === self::LOCK_EXCLUSIVE) { |
|
96 | + $this->memcache->cad($path, 'exclusive'); |
|
97 | + } |
|
98 | + $this->markRelease($path, $type); |
|
99 | + } |
|
100 | 100 | |
101 | - public function changeLock(string $path, int $targetType): void { |
|
102 | - if ($targetType === self::LOCK_SHARED) { |
|
103 | - if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
104 | - throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
105 | - } |
|
106 | - } elseif ($targetType === self::LOCK_EXCLUSIVE) { |
|
107 | - // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
108 | - if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
109 | - throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
110 | - } |
|
111 | - } |
|
112 | - $this->setTTL($path); |
|
113 | - $this->markChange($path, $targetType); |
|
114 | - } |
|
101 | + public function changeLock(string $path, int $targetType): void { |
|
102 | + if ($targetType === self::LOCK_SHARED) { |
|
103 | + if (!$this->memcache->cas($path, 'exclusive', 1)) { |
|
104 | + throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
105 | + } |
|
106 | + } elseif ($targetType === self::LOCK_EXCLUSIVE) { |
|
107 | + // we can only change a shared lock to an exclusive if there's only a single owner of the shared lock |
|
108 | + if (!$this->memcache->cas($path, 1, 'exclusive')) { |
|
109 | + throw new LockedException($path, null, $this->getExistingLockForException($path)); |
|
110 | + } |
|
111 | + } |
|
112 | + $this->setTTL($path); |
|
113 | + $this->markChange($path, $targetType); |
|
114 | + } |
|
115 | 115 | |
116 | - private function getExistingLockForException(string $path): string { |
|
117 | - $existing = $this->memcache->get($path); |
|
118 | - if (!$existing) { |
|
119 | - return 'none'; |
|
120 | - } elseif ($existing === 'exclusive') { |
|
121 | - return $existing; |
|
122 | - } else { |
|
123 | - return $existing . ' shared locks'; |
|
124 | - } |
|
125 | - } |
|
116 | + private function getExistingLockForException(string $path): string { |
|
117 | + $existing = $this->memcache->get($path); |
|
118 | + if (!$existing) { |
|
119 | + return 'none'; |
|
120 | + } elseif ($existing === 'exclusive') { |
|
121 | + return $existing; |
|
122 | + } else { |
|
123 | + return $existing . ' shared locks'; |
|
124 | + } |
|
125 | + } |
|
126 | 126 | } |
@@ -31,44 +31,44 @@ |
||
31 | 31 | * @since 13.0.0 |
32 | 32 | */ |
33 | 33 | class SearchResultType { |
34 | - /** @var string */ |
|
35 | - protected $label; |
|
34 | + /** @var string */ |
|
35 | + protected $label; |
|
36 | 36 | |
37 | - /** |
|
38 | - * SearchResultType constructor. |
|
39 | - * |
|
40 | - * @param string $label |
|
41 | - * @since 13.0.0 |
|
42 | - */ |
|
43 | - public function __construct($label) { |
|
44 | - $this->label = $this->getValidatedType($label); |
|
45 | - } |
|
37 | + /** |
|
38 | + * SearchResultType constructor. |
|
39 | + * |
|
40 | + * @param string $label |
|
41 | + * @since 13.0.0 |
|
42 | + */ |
|
43 | + public function __construct($label) { |
|
44 | + $this->label = $this->getValidatedType($label); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @return string |
|
49 | - * @since 13.0.0 |
|
50 | - */ |
|
51 | - public function getLabel() { |
|
52 | - return $this->label; |
|
53 | - } |
|
47 | + /** |
|
48 | + * @return string |
|
49 | + * @since 13.0.0 |
|
50 | + */ |
|
51 | + public function getLabel() { |
|
52 | + return $this->label; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * @param $type |
|
57 | - * @return string |
|
58 | - * @throws \InvalidArgumentException |
|
59 | - * @since 13.0.0 |
|
60 | - */ |
|
61 | - protected function getValidatedType($type) { |
|
62 | - $type = trim((string)$type); |
|
55 | + /** |
|
56 | + * @param $type |
|
57 | + * @return string |
|
58 | + * @throws \InvalidArgumentException |
|
59 | + * @since 13.0.0 |
|
60 | + */ |
|
61 | + protected function getValidatedType($type) { |
|
62 | + $type = trim((string)$type); |
|
63 | 63 | |
64 | - if ($type === '') { |
|
65 | - throw new \InvalidArgumentException('Type must not be empty'); |
|
66 | - } |
|
64 | + if ($type === '') { |
|
65 | + throw new \InvalidArgumentException('Type must not be empty'); |
|
66 | + } |
|
67 | 67 | |
68 | - if ($type === 'exact') { |
|
69 | - throw new \InvalidArgumentException('Provided type is a reserved word'); |
|
70 | - } |
|
68 | + if ($type === 'exact') { |
|
69 | + throw new \InvalidArgumentException('Provided type is a reserved word'); |
|
70 | + } |
|
71 | 71 | |
72 | - return $type; |
|
73 | - } |
|
72 | + return $type; |
|
73 | + } |
|
74 | 74 | } |
@@ -59,7 +59,7 @@ |
||
59 | 59 | * @since 13.0.0 |
60 | 60 | */ |
61 | 61 | protected function getValidatedType($type) { |
62 | - $type = trim((string)$type); |
|
62 | + $type = trim((string) $type); |
|
63 | 63 | |
64 | 64 | if ($type === '') { |
65 | 65 | throw new \InvalidArgumentException('Type must not be empty'); |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | $instance = new static(); |
52 | 52 | |
53 | 53 | foreach ($params as $key => $value) { |
54 | - $method = 'set' . ucfirst($key); |
|
54 | + $method = 'set'.ucfirst($key); |
|
55 | 55 | $instance->$method($value); |
56 | 56 | } |
57 | 57 | |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | |
70 | 70 | foreach ($row as $key => $value) { |
71 | 71 | $prop = ucfirst($instance->columnToProperty($key)); |
72 | - $setter = 'set' . $prop; |
|
72 | + $setter = 'set'.$prop; |
|
73 | 73 | $instance->$setter($value); |
74 | 74 | } |
75 | 75 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | } |
115 | 115 | $this->$name = $args[0]; |
116 | 116 | } else { |
117 | - throw new \BadFunctionCallException($name . |
|
117 | + throw new \BadFunctionCallException($name. |
|
118 | 118 | ' is not a valid attribute'); |
119 | 119 | } |
120 | 120 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | if (property_exists($this, $name)) { |
129 | 129 | return $this->$name; |
130 | 130 | } else { |
131 | - throw new \BadFunctionCallException($name . |
|
131 | + throw new \BadFunctionCallException($name. |
|
132 | 132 | ' is not a valid attribute'); |
133 | 133 | } |
134 | 134 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | } elseif ($this->isGetterForBoolProperty($methodName)) { |
150 | 150 | return $this->getter(lcfirst(substr($methodName, 2))); |
151 | 151 | } else { |
152 | - throw new \BadFunctionCallException($methodName . |
|
152 | + throw new \BadFunctionCallException($methodName. |
|
153 | 153 | ' does not exist'); |
154 | 154 | } |
155 | 155 | } |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | if ($column === null) { |
214 | 214 | $column = $part; |
215 | 215 | } else { |
216 | - $column .= '_' . lcfirst($part); |
|
216 | + $column .= '_'.lcfirst($part); |
|
217 | 217 | } |
218 | 218 | } |
219 | 219 | |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | // trim '-' |
260 | 260 | return trim($value, '-'); |
261 | 261 | } else { |
262 | - throw new \BadFunctionCallException($attributeName . |
|
262 | + throw new \BadFunctionCallException($attributeName. |
|
263 | 263 | ' is not a valid attribute'); |
264 | 264 | } |
265 | 265 | } |
@@ -35,254 +35,254 @@ |
||
35 | 35 | * @psalm-consistent-constructor |
36 | 36 | */ |
37 | 37 | abstract class Entity { |
38 | - /** |
|
39 | - * @var int |
|
40 | - */ |
|
41 | - public $id; |
|
42 | - |
|
43 | - private array $_updatedFields = []; |
|
44 | - private array $_fieldTypes = ['id' => 'integer']; |
|
45 | - |
|
46 | - /** |
|
47 | - * Simple alternative constructor for building entities from a request |
|
48 | - * @param array $params the array which was obtained via $this->params('key') |
|
49 | - * in the controller |
|
50 | - * @since 7.0.0 |
|
51 | - */ |
|
52 | - public static function fromParams(array $params): static { |
|
53 | - $instance = new static(); |
|
54 | - |
|
55 | - foreach ($params as $key => $value) { |
|
56 | - $method = 'set' . ucfirst($key); |
|
57 | - $instance->$method($value); |
|
58 | - } |
|
59 | - |
|
60 | - return $instance; |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * Maps the keys of the row array to the attributes |
|
66 | - * @param array $row the row to map onto the entity |
|
67 | - * @since 7.0.0 |
|
68 | - */ |
|
69 | - public static function fromRow(array $row): static { |
|
70 | - $instance = new static(); |
|
71 | - |
|
72 | - foreach ($row as $key => $value) { |
|
73 | - $prop = ucfirst($instance->columnToProperty($key)); |
|
74 | - $setter = 'set' . $prop; |
|
75 | - $instance->$setter($value); |
|
76 | - } |
|
77 | - |
|
78 | - $instance->resetUpdatedFields(); |
|
79 | - |
|
80 | - return $instance; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * @return array with attribute and type |
|
86 | - * @since 7.0.0 |
|
87 | - */ |
|
88 | - public function getFieldTypes() { |
|
89 | - return $this->_fieldTypes; |
|
90 | - } |
|
91 | - |
|
92 | - |
|
93 | - /** |
|
94 | - * Marks the entity as clean needed for setting the id after the insertion |
|
95 | - * @since 7.0.0 |
|
96 | - */ |
|
97 | - public function resetUpdatedFields() { |
|
98 | - $this->_updatedFields = []; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * Generic setter for properties |
|
103 | - * @since 7.0.0 |
|
104 | - */ |
|
105 | - protected function setter(string $name, array $args): void { |
|
106 | - // setters should only work for existing attributes |
|
107 | - if (property_exists($this, $name)) { |
|
108 | - if ($this->$name === $args[0]) { |
|
109 | - return; |
|
110 | - } |
|
111 | - $this->markFieldUpdated($name); |
|
112 | - |
|
113 | - // if type definition exists, cast to correct type |
|
114 | - if ($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { |
|
115 | - $type = $this->_fieldTypes[$name]; |
|
116 | - if ($type === 'blob') { |
|
117 | - // (B)LOB is treated as string when we read from the DB |
|
118 | - if (is_resource($args[0])) { |
|
119 | - $args[0] = stream_get_contents($args[0]); |
|
120 | - } |
|
121 | - $type = 'string'; |
|
122 | - } |
|
123 | - |
|
124 | - if ($type === 'datetime') { |
|
125 | - if (!$args[0] instanceof \DateTime) { |
|
126 | - $args[0] = new \DateTime($args[0]); |
|
127 | - } |
|
128 | - } elseif ($type === 'json') { |
|
129 | - if (!is_array($args[0])) { |
|
130 | - $args[0] = json_decode($args[0], true); |
|
131 | - } |
|
132 | - } else { |
|
133 | - settype($args[0], $type); |
|
134 | - } |
|
135 | - } |
|
136 | - $this->$name = $args[0]; |
|
137 | - } else { |
|
138 | - throw new \BadFunctionCallException($name . |
|
139 | - ' is not a valid attribute'); |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Generic getter for properties |
|
145 | - * @since 7.0.0 |
|
146 | - */ |
|
147 | - protected function getter(string $name): mixed { |
|
148 | - // getters should only work for existing attributes |
|
149 | - if (property_exists($this, $name)) { |
|
150 | - return $this->$name; |
|
151 | - } else { |
|
152 | - throw new \BadFunctionCallException($name . |
|
153 | - ' is not a valid attribute'); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * Each time a setter is called, push the part after set |
|
160 | - * into an array: for instance setId will save Id in the |
|
161 | - * updated fields array so it can be easily used to create the |
|
162 | - * getter method |
|
163 | - * @since 7.0.0 |
|
164 | - */ |
|
165 | - public function __call(string $methodName, array $args) { |
|
166 | - if (strpos($methodName, 'set') === 0) { |
|
167 | - $this->setter(lcfirst(substr($methodName, 3)), $args); |
|
168 | - } elseif (strpos($methodName, 'get') === 0) { |
|
169 | - return $this->getter(lcfirst(substr($methodName, 3))); |
|
170 | - } elseif ($this->isGetterForBoolProperty($methodName)) { |
|
171 | - return $this->getter(lcfirst(substr($methodName, 2))); |
|
172 | - } else { |
|
173 | - throw new \BadFunctionCallException($methodName . |
|
174 | - ' does not exist'); |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * @param string $methodName |
|
180 | - * @return bool |
|
181 | - * @since 18.0.0 |
|
182 | - */ |
|
183 | - protected function isGetterForBoolProperty(string $methodName): bool { |
|
184 | - if (strpos($methodName, 'is') === 0) { |
|
185 | - $fieldName = lcfirst(substr($methodName, 2)); |
|
186 | - return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0; |
|
187 | - } |
|
188 | - return false; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Mark am attribute as updated |
|
193 | - * @param string $attribute the name of the attribute |
|
194 | - * @since 7.0.0 |
|
195 | - */ |
|
196 | - protected function markFieldUpdated(string $attribute): void { |
|
197 | - $this->_updatedFields[$attribute] = true; |
|
198 | - } |
|
199 | - |
|
200 | - |
|
201 | - /** |
|
202 | - * Transform a database columnname to a property |
|
203 | - * @param string $columnName the name of the column |
|
204 | - * @return string the property name |
|
205 | - * @since 7.0.0 |
|
206 | - */ |
|
207 | - public function columnToProperty($columnName) { |
|
208 | - $parts = explode('_', $columnName); |
|
209 | - $property = null; |
|
210 | - |
|
211 | - foreach ($parts as $part) { |
|
212 | - if ($property === null) { |
|
213 | - $property = $part; |
|
214 | - } else { |
|
215 | - $property .= ucfirst($part); |
|
216 | - } |
|
217 | - } |
|
218 | - |
|
219 | - return $property; |
|
220 | - } |
|
221 | - |
|
222 | - |
|
223 | - /** |
|
224 | - * Transform a property to a database column name |
|
225 | - * @param string $property the name of the property |
|
226 | - * @return string the column name |
|
227 | - * @since 7.0.0 |
|
228 | - */ |
|
229 | - public function propertyToColumn($property) { |
|
230 | - $parts = preg_split('/(?=[A-Z])/', $property); |
|
231 | - $column = null; |
|
232 | - |
|
233 | - foreach ($parts as $part) { |
|
234 | - if ($column === null) { |
|
235 | - $column = $part; |
|
236 | - } else { |
|
237 | - $column .= '_' . lcfirst($part); |
|
238 | - } |
|
239 | - } |
|
240 | - |
|
241 | - return $column; |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * @return array array of updated fields for update query |
|
247 | - * @since 7.0.0 |
|
248 | - */ |
|
249 | - public function getUpdatedFields() { |
|
250 | - return $this->_updatedFields; |
|
251 | - } |
|
252 | - |
|
253 | - |
|
254 | - /** |
|
255 | - * Adds type information for a field so that its automatically casted to |
|
256 | - * that value once its being returned from the database |
|
257 | - * @param string $fieldName the name of the attribute |
|
258 | - * @param string $type the type which will be used to call settype() |
|
259 | - * @since 7.0.0 |
|
260 | - */ |
|
261 | - protected function addType($fieldName, $type) { |
|
262 | - $this->_fieldTypes[$fieldName] = $type; |
|
263 | - } |
|
264 | - |
|
265 | - |
|
266 | - /** |
|
267 | - * Slugify the value of a given attribute |
|
268 | - * Warning: This doesn't result in a unique value |
|
269 | - * @param string $attributeName the name of the attribute, which value should be slugified |
|
270 | - * @return string slugified value |
|
271 | - * @since 7.0.0 |
|
272 | - * @deprecated 24.0.0 |
|
273 | - */ |
|
274 | - public function slugify($attributeName) { |
|
275 | - // toSlug should only work for existing attributes |
|
276 | - if (property_exists($this, $attributeName)) { |
|
277 | - $value = $this->$attributeName; |
|
278 | - // replace everything except alphanumeric with a single '-' |
|
279 | - $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value); |
|
280 | - $value = strtolower($value); |
|
281 | - // trim '-' |
|
282 | - return trim($value, '-'); |
|
283 | - } else { |
|
284 | - throw new \BadFunctionCallException($attributeName . |
|
285 | - ' is not a valid attribute'); |
|
286 | - } |
|
287 | - } |
|
38 | + /** |
|
39 | + * @var int |
|
40 | + */ |
|
41 | + public $id; |
|
42 | + |
|
43 | + private array $_updatedFields = []; |
|
44 | + private array $_fieldTypes = ['id' => 'integer']; |
|
45 | + |
|
46 | + /** |
|
47 | + * Simple alternative constructor for building entities from a request |
|
48 | + * @param array $params the array which was obtained via $this->params('key') |
|
49 | + * in the controller |
|
50 | + * @since 7.0.0 |
|
51 | + */ |
|
52 | + public static function fromParams(array $params): static { |
|
53 | + $instance = new static(); |
|
54 | + |
|
55 | + foreach ($params as $key => $value) { |
|
56 | + $method = 'set' . ucfirst($key); |
|
57 | + $instance->$method($value); |
|
58 | + } |
|
59 | + |
|
60 | + return $instance; |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * Maps the keys of the row array to the attributes |
|
66 | + * @param array $row the row to map onto the entity |
|
67 | + * @since 7.0.0 |
|
68 | + */ |
|
69 | + public static function fromRow(array $row): static { |
|
70 | + $instance = new static(); |
|
71 | + |
|
72 | + foreach ($row as $key => $value) { |
|
73 | + $prop = ucfirst($instance->columnToProperty($key)); |
|
74 | + $setter = 'set' . $prop; |
|
75 | + $instance->$setter($value); |
|
76 | + } |
|
77 | + |
|
78 | + $instance->resetUpdatedFields(); |
|
79 | + |
|
80 | + return $instance; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * @return array with attribute and type |
|
86 | + * @since 7.0.0 |
|
87 | + */ |
|
88 | + public function getFieldTypes() { |
|
89 | + return $this->_fieldTypes; |
|
90 | + } |
|
91 | + |
|
92 | + |
|
93 | + /** |
|
94 | + * Marks the entity as clean needed for setting the id after the insertion |
|
95 | + * @since 7.0.0 |
|
96 | + */ |
|
97 | + public function resetUpdatedFields() { |
|
98 | + $this->_updatedFields = []; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * Generic setter for properties |
|
103 | + * @since 7.0.0 |
|
104 | + */ |
|
105 | + protected function setter(string $name, array $args): void { |
|
106 | + // setters should only work for existing attributes |
|
107 | + if (property_exists($this, $name)) { |
|
108 | + if ($this->$name === $args[0]) { |
|
109 | + return; |
|
110 | + } |
|
111 | + $this->markFieldUpdated($name); |
|
112 | + |
|
113 | + // if type definition exists, cast to correct type |
|
114 | + if ($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { |
|
115 | + $type = $this->_fieldTypes[$name]; |
|
116 | + if ($type === 'blob') { |
|
117 | + // (B)LOB is treated as string when we read from the DB |
|
118 | + if (is_resource($args[0])) { |
|
119 | + $args[0] = stream_get_contents($args[0]); |
|
120 | + } |
|
121 | + $type = 'string'; |
|
122 | + } |
|
123 | + |
|
124 | + if ($type === 'datetime') { |
|
125 | + if (!$args[0] instanceof \DateTime) { |
|
126 | + $args[0] = new \DateTime($args[0]); |
|
127 | + } |
|
128 | + } elseif ($type === 'json') { |
|
129 | + if (!is_array($args[0])) { |
|
130 | + $args[0] = json_decode($args[0], true); |
|
131 | + } |
|
132 | + } else { |
|
133 | + settype($args[0], $type); |
|
134 | + } |
|
135 | + } |
|
136 | + $this->$name = $args[0]; |
|
137 | + } else { |
|
138 | + throw new \BadFunctionCallException($name . |
|
139 | + ' is not a valid attribute'); |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Generic getter for properties |
|
145 | + * @since 7.0.0 |
|
146 | + */ |
|
147 | + protected function getter(string $name): mixed { |
|
148 | + // getters should only work for existing attributes |
|
149 | + if (property_exists($this, $name)) { |
|
150 | + return $this->$name; |
|
151 | + } else { |
|
152 | + throw new \BadFunctionCallException($name . |
|
153 | + ' is not a valid attribute'); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * Each time a setter is called, push the part after set |
|
160 | + * into an array: for instance setId will save Id in the |
|
161 | + * updated fields array so it can be easily used to create the |
|
162 | + * getter method |
|
163 | + * @since 7.0.0 |
|
164 | + */ |
|
165 | + public function __call(string $methodName, array $args) { |
|
166 | + if (strpos($methodName, 'set') === 0) { |
|
167 | + $this->setter(lcfirst(substr($methodName, 3)), $args); |
|
168 | + } elseif (strpos($methodName, 'get') === 0) { |
|
169 | + return $this->getter(lcfirst(substr($methodName, 3))); |
|
170 | + } elseif ($this->isGetterForBoolProperty($methodName)) { |
|
171 | + return $this->getter(lcfirst(substr($methodName, 2))); |
|
172 | + } else { |
|
173 | + throw new \BadFunctionCallException($methodName . |
|
174 | + ' does not exist'); |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * @param string $methodName |
|
180 | + * @return bool |
|
181 | + * @since 18.0.0 |
|
182 | + */ |
|
183 | + protected function isGetterForBoolProperty(string $methodName): bool { |
|
184 | + if (strpos($methodName, 'is') === 0) { |
|
185 | + $fieldName = lcfirst(substr($methodName, 2)); |
|
186 | + return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0; |
|
187 | + } |
|
188 | + return false; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Mark am attribute as updated |
|
193 | + * @param string $attribute the name of the attribute |
|
194 | + * @since 7.0.0 |
|
195 | + */ |
|
196 | + protected function markFieldUpdated(string $attribute): void { |
|
197 | + $this->_updatedFields[$attribute] = true; |
|
198 | + } |
|
199 | + |
|
200 | + |
|
201 | + /** |
|
202 | + * Transform a database columnname to a property |
|
203 | + * @param string $columnName the name of the column |
|
204 | + * @return string the property name |
|
205 | + * @since 7.0.0 |
|
206 | + */ |
|
207 | + public function columnToProperty($columnName) { |
|
208 | + $parts = explode('_', $columnName); |
|
209 | + $property = null; |
|
210 | + |
|
211 | + foreach ($parts as $part) { |
|
212 | + if ($property === null) { |
|
213 | + $property = $part; |
|
214 | + } else { |
|
215 | + $property .= ucfirst($part); |
|
216 | + } |
|
217 | + } |
|
218 | + |
|
219 | + return $property; |
|
220 | + } |
|
221 | + |
|
222 | + |
|
223 | + /** |
|
224 | + * Transform a property to a database column name |
|
225 | + * @param string $property the name of the property |
|
226 | + * @return string the column name |
|
227 | + * @since 7.0.0 |
|
228 | + */ |
|
229 | + public function propertyToColumn($property) { |
|
230 | + $parts = preg_split('/(?=[A-Z])/', $property); |
|
231 | + $column = null; |
|
232 | + |
|
233 | + foreach ($parts as $part) { |
|
234 | + if ($column === null) { |
|
235 | + $column = $part; |
|
236 | + } else { |
|
237 | + $column .= '_' . lcfirst($part); |
|
238 | + } |
|
239 | + } |
|
240 | + |
|
241 | + return $column; |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * @return array array of updated fields for update query |
|
247 | + * @since 7.0.0 |
|
248 | + */ |
|
249 | + public function getUpdatedFields() { |
|
250 | + return $this->_updatedFields; |
|
251 | + } |
|
252 | + |
|
253 | + |
|
254 | + /** |
|
255 | + * Adds type information for a field so that its automatically casted to |
|
256 | + * that value once its being returned from the database |
|
257 | + * @param string $fieldName the name of the attribute |
|
258 | + * @param string $type the type which will be used to call settype() |
|
259 | + * @since 7.0.0 |
|
260 | + */ |
|
261 | + protected function addType($fieldName, $type) { |
|
262 | + $this->_fieldTypes[$fieldName] = $type; |
|
263 | + } |
|
264 | + |
|
265 | + |
|
266 | + /** |
|
267 | + * Slugify the value of a given attribute |
|
268 | + * Warning: This doesn't result in a unique value |
|
269 | + * @param string $attributeName the name of the attribute, which value should be slugified |
|
270 | + * @return string slugified value |
|
271 | + * @since 7.0.0 |
|
272 | + * @deprecated 24.0.0 |
|
273 | + */ |
|
274 | + public function slugify($attributeName) { |
|
275 | + // toSlug should only work for existing attributes |
|
276 | + if (property_exists($this, $attributeName)) { |
|
277 | + $value = $this->$attributeName; |
|
278 | + // replace everything except alphanumeric with a single '-' |
|
279 | + $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value); |
|
280 | + $value = strtolower($value); |
|
281 | + // trim '-' |
|
282 | + return trim($value, '-'); |
|
283 | + } else { |
|
284 | + throw new \BadFunctionCallException($attributeName . |
|
285 | + ' is not a valid attribute'); |
|
286 | + } |
|
287 | + } |
|
288 | 288 | } |
@@ -33,27 +33,27 @@ |
||
33 | 33 | * @since 7.0.0 |
34 | 34 | */ |
35 | 35 | class RedirectResponse extends Response { |
36 | - private $redirectURL; |
|
36 | + private $redirectURL; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Creates a response that redirects to a url |
|
40 | - * @param string $redirectURL the url to redirect to |
|
41 | - * @since 7.0.0 |
|
42 | - */ |
|
43 | - public function __construct($redirectURL) { |
|
44 | - parent::__construct(); |
|
38 | + /** |
|
39 | + * Creates a response that redirects to a url |
|
40 | + * @param string $redirectURL the url to redirect to |
|
41 | + * @since 7.0.0 |
|
42 | + */ |
|
43 | + public function __construct($redirectURL) { |
|
44 | + parent::__construct(); |
|
45 | 45 | |
46 | - $this->redirectURL = $redirectURL; |
|
47 | - $this->setStatus(Http::STATUS_SEE_OTHER); |
|
48 | - $this->addHeader('Location', $redirectURL); |
|
49 | - } |
|
46 | + $this->redirectURL = $redirectURL; |
|
47 | + $this->setStatus(Http::STATUS_SEE_OTHER); |
|
48 | + $this->addHeader('Location', $redirectURL); |
|
49 | + } |
|
50 | 50 | |
51 | 51 | |
52 | - /** |
|
53 | - * @return string the url to redirect |
|
54 | - * @since 7.0.0 |
|
55 | - */ |
|
56 | - public function getRedirectURL() { |
|
57 | - return $this->redirectURL; |
|
58 | - } |
|
52 | + /** |
|
53 | + * @return string the url to redirect |
|
54 | + * @since 7.0.0 |
|
55 | + */ |
|
56 | + public function getRedirectURL() { |
|
57 | + return $this->redirectURL; |
|
58 | + } |
|
59 | 59 | } |
@@ -34,57 +34,57 @@ |
||
34 | 34 | * @package OC\IntegrityCheck\Helpers |
35 | 35 | */ |
36 | 36 | class FileAccessHelper { |
37 | - /** |
|
38 | - * Wrapper around file_get_contents($filename, $data) |
|
39 | - * |
|
40 | - * @param string $filename |
|
41 | - * @return string|false |
|
42 | - */ |
|
43 | - public function file_get_contents(string $filename) { |
|
44 | - return file_get_contents($filename); |
|
45 | - } |
|
37 | + /** |
|
38 | + * Wrapper around file_get_contents($filename, $data) |
|
39 | + * |
|
40 | + * @param string $filename |
|
41 | + * @return string|false |
|
42 | + */ |
|
43 | + public function file_get_contents(string $filename) { |
|
44 | + return file_get_contents($filename); |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Wrapper around file_exists($filename) |
|
49 | - * |
|
50 | - * @param string $filename |
|
51 | - * @return bool |
|
52 | - */ |
|
53 | - public function file_exists(string $filename): bool { |
|
54 | - return file_exists($filename); |
|
55 | - } |
|
47 | + /** |
|
48 | + * Wrapper around file_exists($filename) |
|
49 | + * |
|
50 | + * @param string $filename |
|
51 | + * @return bool |
|
52 | + */ |
|
53 | + public function file_exists(string $filename): bool { |
|
54 | + return file_exists($filename); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Wrapper around file_put_contents($filename, $data) |
|
59 | - * |
|
60 | - * @param string $filename |
|
61 | - * @param string $data |
|
62 | - * @return int |
|
63 | - * @throws \Exception |
|
64 | - */ |
|
65 | - public function file_put_contents(string $filename, string $data): int { |
|
66 | - $bytesWritten = @file_put_contents($filename, $data); |
|
67 | - if ($bytesWritten === false || $bytesWritten !== \strlen($data)) { |
|
68 | - throw new \Exception('Failed to write into ' . $filename); |
|
69 | - } |
|
70 | - return $bytesWritten; |
|
71 | - } |
|
57 | + /** |
|
58 | + * Wrapper around file_put_contents($filename, $data) |
|
59 | + * |
|
60 | + * @param string $filename |
|
61 | + * @param string $data |
|
62 | + * @return int |
|
63 | + * @throws \Exception |
|
64 | + */ |
|
65 | + public function file_put_contents(string $filename, string $data): int { |
|
66 | + $bytesWritten = @file_put_contents($filename, $data); |
|
67 | + if ($bytesWritten === false || $bytesWritten !== \strlen($data)) { |
|
68 | + throw new \Exception('Failed to write into ' . $filename); |
|
69 | + } |
|
70 | + return $bytesWritten; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * @param string $path |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function is_writable(string $path): bool { |
|
78 | - return is_writable($path); |
|
79 | - } |
|
73 | + /** |
|
74 | + * @param string $path |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function is_writable(string $path): bool { |
|
78 | + return is_writable($path); |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * @param string $path |
|
83 | - * @throws \Exception |
|
84 | - */ |
|
85 | - public function assertDirectoryExists(string $path) { |
|
86 | - if (!is_dir($path)) { |
|
87 | - throw new \Exception('Directory ' . $path . ' does not exist.'); |
|
88 | - } |
|
89 | - } |
|
81 | + /** |
|
82 | + * @param string $path |
|
83 | + * @throws \Exception |
|
84 | + */ |
|
85 | + public function assertDirectoryExists(string $path) { |
|
86 | + if (!is_dir($path)) { |
|
87 | + throw new \Exception('Directory ' . $path . ' does not exist.'); |
|
88 | + } |
|
89 | + } |
|
90 | 90 | } |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | public function file_put_contents(string $filename, string $data): int { |
66 | 66 | $bytesWritten = @file_put_contents($filename, $data); |
67 | 67 | if ($bytesWritten === false || $bytesWritten !== \strlen($data)) { |
68 | - throw new \Exception('Failed to write into ' . $filename); |
|
68 | + throw new \Exception('Failed to write into '.$filename); |
|
69 | 69 | } |
70 | 70 | return $bytesWritten; |
71 | 71 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function assertDirectoryExists(string $path) { |
86 | 86 | if (!is_dir($path)) { |
87 | - throw new \Exception('Directory ' . $path . ' does not exist.'); |
|
87 | + throw new \Exception('Directory '.$path.' does not exist.'); |
|
88 | 88 | } |
89 | 89 | } |
90 | 90 | } |
@@ -34,27 +34,27 @@ |
||
34 | 34 | * @package OC\IntegrityCheck\Helpers |
35 | 35 | */ |
36 | 36 | class AppLocator { |
37 | - /** |
|
38 | - * Provides \OC_App::getAppPath($appId) |
|
39 | - * |
|
40 | - * @param string $appId |
|
41 | - * @return string |
|
42 | - * @throws \Exception If the app cannot be found |
|
43 | - */ |
|
44 | - public function getAppPath(string $appId): string { |
|
45 | - $path = \OC_App::getAppPath($appId); |
|
46 | - if ($path === false) { |
|
47 | - throw new \Exception('App not found'); |
|
48 | - } |
|
49 | - return $path; |
|
50 | - } |
|
37 | + /** |
|
38 | + * Provides \OC_App::getAppPath($appId) |
|
39 | + * |
|
40 | + * @param string $appId |
|
41 | + * @return string |
|
42 | + * @throws \Exception If the app cannot be found |
|
43 | + */ |
|
44 | + public function getAppPath(string $appId): string { |
|
45 | + $path = \OC_App::getAppPath($appId); |
|
46 | + if ($path === false) { |
|
47 | + throw new \Exception('App not found'); |
|
48 | + } |
|
49 | + return $path; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Providers \OC_App::getAllApps() |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function getAllApps(): array { |
|
58 | - return \OC_App::getAllApps(); |
|
59 | - } |
|
52 | + /** |
|
53 | + * Providers \OC_App::getAllApps() |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function getAllApps(): array { |
|
58 | + return \OC_App::getAllApps(); |
|
59 | + } |
|
60 | 60 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | $this->output = $output; |
88 | 88 | |
89 | 89 | if ($user !== '' && $this->userManager->userExists($user) === false) { |
90 | - $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again'); |
|
90 | + $this->output->writeln('User "'.$user.'" does not exist. Please check the username and try again'); |
|
91 | 91 | return false; |
92 | 92 | } |
93 | 93 | |
@@ -105,9 +105,9 @@ discard block |
||
105 | 105 | $this->output->writeln('Files for following users couldn\'t be decrypted, '); |
106 | 106 | $this->output->writeln('maybe the user is not set up in a way that supports this operation: '); |
107 | 107 | foreach ($this->failed as $uid => $paths) { |
108 | - $this->output->writeln(' ' . $uid); |
|
108 | + $this->output->writeln(' '.$uid); |
|
109 | 109 | foreach ($paths as $path) { |
110 | - $this->output->writeln(' ' . $path); |
|
110 | + $this->output->writeln(' '.$path); |
|
111 | 111 | } |
112 | 112 | } |
113 | 113 | $this->output->writeln(''); |
@@ -129,10 +129,10 @@ discard block |
||
129 | 129 | /** @var IEncryptionModule $module */ |
130 | 130 | $module = call_user_func($moduleDesc['callback']); |
131 | 131 | $this->output->writeln(''); |
132 | - $this->output->writeln('Prepare "' . $module->getDisplayName() . '"'); |
|
132 | + $this->output->writeln('Prepare "'.$module->getDisplayName().'"'); |
|
133 | 133 | $this->output->writeln(''); |
134 | 134 | if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) { |
135 | - $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!'); |
|
135 | + $this->output->writeln('Module "'.$moduleDesc['displayName'].'" does not support the functionality to decrypt all files again or the initialization of the module failed!'); |
|
136 | 136 | return false; |
137 | 137 | } |
138 | 138 | } |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) { |
207 | 207 | $this->setupUserFS($uid); |
208 | 208 | $directories = []; |
209 | - $directories[] = '/' . $uid . '/files'; |
|
209 | + $directories[] = '/'.$uid.'/files'; |
|
210 | 210 | |
211 | 211 | while ($root = array_pop($directories)) { |
212 | 212 | $content = $this->rootView->getDirectoryContent($root); |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) { |
216 | 216 | continue; |
217 | 217 | } |
218 | - $path = $root . '/' . $file['name']; |
|
218 | + $path = $root.'/'.$file['name']; |
|
219 | 219 | if ($this->rootView->is_dir($path)) { |
220 | 220 | $directories[] = $path; |
221 | 221 | continue; |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | } |
260 | 260 | |
261 | 261 | $source = $path; |
262 | - $target = $path . '.decrypted.' . $this->getTimestamp(); |
|
262 | + $target = $path.'.decrypted.'.$this->getTimestamp(); |
|
263 | 263 | |
264 | 264 | try { |
265 | 265 | $this->rootView->copy($source, $target); |
@@ -37,258 +37,258 @@ |
||
37 | 37 | use Symfony\Component\Console\Output\OutputInterface; |
38 | 38 | |
39 | 39 | class DecryptAll { |
40 | - /** @var OutputInterface */ |
|
41 | - protected $output; |
|
42 | - |
|
43 | - /** @var InputInterface */ |
|
44 | - protected $input; |
|
45 | - |
|
46 | - /** @var Manager */ |
|
47 | - protected $encryptionManager; |
|
48 | - |
|
49 | - /** @var IUserManager */ |
|
50 | - protected $userManager; |
|
51 | - |
|
52 | - /** @var View */ |
|
53 | - protected $rootView; |
|
54 | - |
|
55 | - /** @var array files which couldn't be decrypted */ |
|
56 | - protected $failed; |
|
57 | - |
|
58 | - /** |
|
59 | - * @param Manager $encryptionManager |
|
60 | - * @param IUserManager $userManager |
|
61 | - * @param View $rootView |
|
62 | - */ |
|
63 | - public function __construct( |
|
64 | - Manager $encryptionManager, |
|
65 | - IUserManager $userManager, |
|
66 | - View $rootView |
|
67 | - ) { |
|
68 | - $this->encryptionManager = $encryptionManager; |
|
69 | - $this->userManager = $userManager; |
|
70 | - $this->rootView = $rootView; |
|
71 | - $this->failed = []; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * start to decrypt all files |
|
76 | - * |
|
77 | - * @param InputInterface $input |
|
78 | - * @param OutputInterface $output |
|
79 | - * @param string $user which users data folder should be decrypted, default = all users |
|
80 | - * @return bool |
|
81 | - * @throws \Exception |
|
82 | - */ |
|
83 | - public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
|
84 | - $this->input = $input; |
|
85 | - $this->output = $output; |
|
86 | - |
|
87 | - if ($user !== '' && $this->userManager->userExists($user) === false) { |
|
88 | - $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again'); |
|
89 | - return false; |
|
90 | - } |
|
91 | - |
|
92 | - $this->output->writeln('prepare encryption modules...'); |
|
93 | - if ($this->prepareEncryptionModules($user) === false) { |
|
94 | - return false; |
|
95 | - } |
|
96 | - $this->output->writeln(' done.'); |
|
97 | - |
|
98 | - $this->decryptAllUsersFiles($user); |
|
99 | - |
|
100 | - if (empty($this->failed)) { |
|
101 | - $this->output->writeln('all files could be decrypted successfully!'); |
|
102 | - } else { |
|
103 | - $this->output->writeln('Files for following users couldn\'t be decrypted, '); |
|
104 | - $this->output->writeln('maybe the user is not set up in a way that supports this operation: '); |
|
105 | - foreach ($this->failed as $uid => $paths) { |
|
106 | - $this->output->writeln(' ' . $uid); |
|
107 | - foreach ($paths as $path) { |
|
108 | - $this->output->writeln(' ' . $path); |
|
109 | - } |
|
110 | - } |
|
111 | - $this->output->writeln(''); |
|
112 | - } |
|
113 | - |
|
114 | - return true; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * prepare encryption modules to perform the decrypt all function |
|
119 | - * |
|
120 | - * @param $user |
|
121 | - * @return bool |
|
122 | - */ |
|
123 | - protected function prepareEncryptionModules($user) { |
|
124 | - // prepare all encryption modules for decrypt all |
|
125 | - $encryptionModules = $this->encryptionManager->getEncryptionModules(); |
|
126 | - foreach ($encryptionModules as $moduleDesc) { |
|
127 | - /** @var IEncryptionModule $module */ |
|
128 | - $module = call_user_func($moduleDesc['callback']); |
|
129 | - $this->output->writeln(''); |
|
130 | - $this->output->writeln('Prepare "' . $module->getDisplayName() . '"'); |
|
131 | - $this->output->writeln(''); |
|
132 | - if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) { |
|
133 | - $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!'); |
|
134 | - return false; |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - return true; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * iterate over all user and encrypt their files |
|
143 | - * |
|
144 | - * @param string $user which users files should be decrypted, default = all users |
|
145 | - */ |
|
146 | - protected function decryptAllUsersFiles($user = '') { |
|
147 | - $this->output->writeln("\n"); |
|
148 | - |
|
149 | - $userList = []; |
|
150 | - if ($user === '') { |
|
151 | - $fetchUsersProgress = new ProgressBar($this->output); |
|
152 | - $fetchUsersProgress->setFormat(" %message% \n [%bar%]"); |
|
153 | - $fetchUsersProgress->start(); |
|
154 | - $fetchUsersProgress->setMessage("Fetch list of users..."); |
|
155 | - $fetchUsersProgress->advance(); |
|
156 | - |
|
157 | - foreach ($this->userManager->getBackends() as $backend) { |
|
158 | - $limit = 500; |
|
159 | - $offset = 0; |
|
160 | - do { |
|
161 | - $users = $backend->getUsers('', $limit, $offset); |
|
162 | - foreach ($users as $user) { |
|
163 | - $userList[] = $user; |
|
164 | - } |
|
165 | - $offset += $limit; |
|
166 | - $fetchUsersProgress->advance(); |
|
167 | - } while (count($users) >= $limit); |
|
168 | - $fetchUsersProgress->setMessage("Fetch list of users... finished"); |
|
169 | - $fetchUsersProgress->finish(); |
|
170 | - } |
|
171 | - } else { |
|
172 | - $userList[] = $user; |
|
173 | - } |
|
174 | - |
|
175 | - $this->output->writeln("\n\n"); |
|
176 | - |
|
177 | - $progress = new ProgressBar($this->output); |
|
178 | - $progress->setFormat(" %message% \n [%bar%]"); |
|
179 | - $progress->start(); |
|
180 | - $progress->setMessage("starting to decrypt files..."); |
|
181 | - $progress->advance(); |
|
182 | - |
|
183 | - $numberOfUsers = count($userList); |
|
184 | - $userNo = 1; |
|
185 | - foreach ($userList as $uid) { |
|
186 | - $userCount = "$uid ($userNo of $numberOfUsers)"; |
|
187 | - $this->decryptUsersFiles($uid, $progress, $userCount); |
|
188 | - $userNo++; |
|
189 | - } |
|
190 | - |
|
191 | - $progress->setMessage("starting to decrypt files... finished"); |
|
192 | - $progress->finish(); |
|
193 | - |
|
194 | - $this->output->writeln("\n\n"); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * encrypt files from the given user |
|
199 | - * |
|
200 | - * @param string $uid |
|
201 | - * @param ProgressBar $progress |
|
202 | - * @param string $userCount |
|
203 | - */ |
|
204 | - protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) { |
|
205 | - $this->setupUserFS($uid); |
|
206 | - $directories = []; |
|
207 | - $directories[] = '/' . $uid . '/files'; |
|
208 | - |
|
209 | - while ($root = array_pop($directories)) { |
|
210 | - $content = $this->rootView->getDirectoryContent($root); |
|
211 | - foreach ($content as $file) { |
|
212 | - // only decrypt files owned by the user |
|
213 | - if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) { |
|
214 | - continue; |
|
215 | - } |
|
216 | - $path = $root . '/' . $file['name']; |
|
217 | - if ($this->rootView->is_dir($path)) { |
|
218 | - $directories[] = $path; |
|
219 | - continue; |
|
220 | - } else { |
|
221 | - try { |
|
222 | - $progress->setMessage("decrypt files for user $userCount: $path"); |
|
223 | - $progress->advance(); |
|
224 | - if ($file->isEncrypted() === false) { |
|
225 | - $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)"); |
|
226 | - $progress->advance(); |
|
227 | - } else { |
|
228 | - if ($this->decryptFile($path) === false) { |
|
229 | - $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)"); |
|
230 | - $progress->advance(); |
|
231 | - } |
|
232 | - } |
|
233 | - } catch (\Exception $e) { |
|
234 | - if (isset($this->failed[$uid])) { |
|
235 | - $this->failed[$uid][] = $path; |
|
236 | - } else { |
|
237 | - $this->failed[$uid] = [$path]; |
|
238 | - } |
|
239 | - } |
|
240 | - } |
|
241 | - } |
|
242 | - } |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * encrypt file |
|
247 | - * |
|
248 | - * @param string $path |
|
249 | - * @return bool |
|
250 | - */ |
|
251 | - protected function decryptFile($path) { |
|
252 | - // skip already decrypted files |
|
253 | - $fileInfo = $this->rootView->getFileInfo($path); |
|
254 | - if ($fileInfo !== false && !$fileInfo->isEncrypted()) { |
|
255 | - return true; |
|
256 | - } |
|
257 | - |
|
258 | - $source = $path; |
|
259 | - $target = $path . '.decrypted.' . $this->getTimestamp(); |
|
260 | - |
|
261 | - try { |
|
262 | - $this->rootView->copy($source, $target); |
|
263 | - $this->rootView->touch($target, $fileInfo->getMTime()); |
|
264 | - $this->rootView->rename($target, $source); |
|
265 | - } catch (DecryptionFailedException $e) { |
|
266 | - if ($this->rootView->file_exists($target)) { |
|
267 | - $this->rootView->unlink($target); |
|
268 | - } |
|
269 | - return false; |
|
270 | - } |
|
271 | - |
|
272 | - return true; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * get current timestamp |
|
277 | - * |
|
278 | - * @return int |
|
279 | - */ |
|
280 | - protected function getTimestamp() { |
|
281 | - return time(); |
|
282 | - } |
|
283 | - |
|
284 | - |
|
285 | - /** |
|
286 | - * setup user file system |
|
287 | - * |
|
288 | - * @param string $uid |
|
289 | - */ |
|
290 | - protected function setupUserFS($uid) { |
|
291 | - \OC_Util::tearDownFS(); |
|
292 | - \OC_Util::setupFS($uid); |
|
293 | - } |
|
40 | + /** @var OutputInterface */ |
|
41 | + protected $output; |
|
42 | + |
|
43 | + /** @var InputInterface */ |
|
44 | + protected $input; |
|
45 | + |
|
46 | + /** @var Manager */ |
|
47 | + protected $encryptionManager; |
|
48 | + |
|
49 | + /** @var IUserManager */ |
|
50 | + protected $userManager; |
|
51 | + |
|
52 | + /** @var View */ |
|
53 | + protected $rootView; |
|
54 | + |
|
55 | + /** @var array files which couldn't be decrypted */ |
|
56 | + protected $failed; |
|
57 | + |
|
58 | + /** |
|
59 | + * @param Manager $encryptionManager |
|
60 | + * @param IUserManager $userManager |
|
61 | + * @param View $rootView |
|
62 | + */ |
|
63 | + public function __construct( |
|
64 | + Manager $encryptionManager, |
|
65 | + IUserManager $userManager, |
|
66 | + View $rootView |
|
67 | + ) { |
|
68 | + $this->encryptionManager = $encryptionManager; |
|
69 | + $this->userManager = $userManager; |
|
70 | + $this->rootView = $rootView; |
|
71 | + $this->failed = []; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * start to decrypt all files |
|
76 | + * |
|
77 | + * @param InputInterface $input |
|
78 | + * @param OutputInterface $output |
|
79 | + * @param string $user which users data folder should be decrypted, default = all users |
|
80 | + * @return bool |
|
81 | + * @throws \Exception |
|
82 | + */ |
|
83 | + public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') { |
|
84 | + $this->input = $input; |
|
85 | + $this->output = $output; |
|
86 | + |
|
87 | + if ($user !== '' && $this->userManager->userExists($user) === false) { |
|
88 | + $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again'); |
|
89 | + return false; |
|
90 | + } |
|
91 | + |
|
92 | + $this->output->writeln('prepare encryption modules...'); |
|
93 | + if ($this->prepareEncryptionModules($user) === false) { |
|
94 | + return false; |
|
95 | + } |
|
96 | + $this->output->writeln(' done.'); |
|
97 | + |
|
98 | + $this->decryptAllUsersFiles($user); |
|
99 | + |
|
100 | + if (empty($this->failed)) { |
|
101 | + $this->output->writeln('all files could be decrypted successfully!'); |
|
102 | + } else { |
|
103 | + $this->output->writeln('Files for following users couldn\'t be decrypted, '); |
|
104 | + $this->output->writeln('maybe the user is not set up in a way that supports this operation: '); |
|
105 | + foreach ($this->failed as $uid => $paths) { |
|
106 | + $this->output->writeln(' ' . $uid); |
|
107 | + foreach ($paths as $path) { |
|
108 | + $this->output->writeln(' ' . $path); |
|
109 | + } |
|
110 | + } |
|
111 | + $this->output->writeln(''); |
|
112 | + } |
|
113 | + |
|
114 | + return true; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * prepare encryption modules to perform the decrypt all function |
|
119 | + * |
|
120 | + * @param $user |
|
121 | + * @return bool |
|
122 | + */ |
|
123 | + protected function prepareEncryptionModules($user) { |
|
124 | + // prepare all encryption modules for decrypt all |
|
125 | + $encryptionModules = $this->encryptionManager->getEncryptionModules(); |
|
126 | + foreach ($encryptionModules as $moduleDesc) { |
|
127 | + /** @var IEncryptionModule $module */ |
|
128 | + $module = call_user_func($moduleDesc['callback']); |
|
129 | + $this->output->writeln(''); |
|
130 | + $this->output->writeln('Prepare "' . $module->getDisplayName() . '"'); |
|
131 | + $this->output->writeln(''); |
|
132 | + if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) { |
|
133 | + $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!'); |
|
134 | + return false; |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + return true; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * iterate over all user and encrypt their files |
|
143 | + * |
|
144 | + * @param string $user which users files should be decrypted, default = all users |
|
145 | + */ |
|
146 | + protected function decryptAllUsersFiles($user = '') { |
|
147 | + $this->output->writeln("\n"); |
|
148 | + |
|
149 | + $userList = []; |
|
150 | + if ($user === '') { |
|
151 | + $fetchUsersProgress = new ProgressBar($this->output); |
|
152 | + $fetchUsersProgress->setFormat(" %message% \n [%bar%]"); |
|
153 | + $fetchUsersProgress->start(); |
|
154 | + $fetchUsersProgress->setMessage("Fetch list of users..."); |
|
155 | + $fetchUsersProgress->advance(); |
|
156 | + |
|
157 | + foreach ($this->userManager->getBackends() as $backend) { |
|
158 | + $limit = 500; |
|
159 | + $offset = 0; |
|
160 | + do { |
|
161 | + $users = $backend->getUsers('', $limit, $offset); |
|
162 | + foreach ($users as $user) { |
|
163 | + $userList[] = $user; |
|
164 | + } |
|
165 | + $offset += $limit; |
|
166 | + $fetchUsersProgress->advance(); |
|
167 | + } while (count($users) >= $limit); |
|
168 | + $fetchUsersProgress->setMessage("Fetch list of users... finished"); |
|
169 | + $fetchUsersProgress->finish(); |
|
170 | + } |
|
171 | + } else { |
|
172 | + $userList[] = $user; |
|
173 | + } |
|
174 | + |
|
175 | + $this->output->writeln("\n\n"); |
|
176 | + |
|
177 | + $progress = new ProgressBar($this->output); |
|
178 | + $progress->setFormat(" %message% \n [%bar%]"); |
|
179 | + $progress->start(); |
|
180 | + $progress->setMessage("starting to decrypt files..."); |
|
181 | + $progress->advance(); |
|
182 | + |
|
183 | + $numberOfUsers = count($userList); |
|
184 | + $userNo = 1; |
|
185 | + foreach ($userList as $uid) { |
|
186 | + $userCount = "$uid ($userNo of $numberOfUsers)"; |
|
187 | + $this->decryptUsersFiles($uid, $progress, $userCount); |
|
188 | + $userNo++; |
|
189 | + } |
|
190 | + |
|
191 | + $progress->setMessage("starting to decrypt files... finished"); |
|
192 | + $progress->finish(); |
|
193 | + |
|
194 | + $this->output->writeln("\n\n"); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * encrypt files from the given user |
|
199 | + * |
|
200 | + * @param string $uid |
|
201 | + * @param ProgressBar $progress |
|
202 | + * @param string $userCount |
|
203 | + */ |
|
204 | + protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) { |
|
205 | + $this->setupUserFS($uid); |
|
206 | + $directories = []; |
|
207 | + $directories[] = '/' . $uid . '/files'; |
|
208 | + |
|
209 | + while ($root = array_pop($directories)) { |
|
210 | + $content = $this->rootView->getDirectoryContent($root); |
|
211 | + foreach ($content as $file) { |
|
212 | + // only decrypt files owned by the user |
|
213 | + if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) { |
|
214 | + continue; |
|
215 | + } |
|
216 | + $path = $root . '/' . $file['name']; |
|
217 | + if ($this->rootView->is_dir($path)) { |
|
218 | + $directories[] = $path; |
|
219 | + continue; |
|
220 | + } else { |
|
221 | + try { |
|
222 | + $progress->setMessage("decrypt files for user $userCount: $path"); |
|
223 | + $progress->advance(); |
|
224 | + if ($file->isEncrypted() === false) { |
|
225 | + $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)"); |
|
226 | + $progress->advance(); |
|
227 | + } else { |
|
228 | + if ($this->decryptFile($path) === false) { |
|
229 | + $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)"); |
|
230 | + $progress->advance(); |
|
231 | + } |
|
232 | + } |
|
233 | + } catch (\Exception $e) { |
|
234 | + if (isset($this->failed[$uid])) { |
|
235 | + $this->failed[$uid][] = $path; |
|
236 | + } else { |
|
237 | + $this->failed[$uid] = [$path]; |
|
238 | + } |
|
239 | + } |
|
240 | + } |
|
241 | + } |
|
242 | + } |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * encrypt file |
|
247 | + * |
|
248 | + * @param string $path |
|
249 | + * @return bool |
|
250 | + */ |
|
251 | + protected function decryptFile($path) { |
|
252 | + // skip already decrypted files |
|
253 | + $fileInfo = $this->rootView->getFileInfo($path); |
|
254 | + if ($fileInfo !== false && !$fileInfo->isEncrypted()) { |
|
255 | + return true; |
|
256 | + } |
|
257 | + |
|
258 | + $source = $path; |
|
259 | + $target = $path . '.decrypted.' . $this->getTimestamp(); |
|
260 | + |
|
261 | + try { |
|
262 | + $this->rootView->copy($source, $target); |
|
263 | + $this->rootView->touch($target, $fileInfo->getMTime()); |
|
264 | + $this->rootView->rename($target, $source); |
|
265 | + } catch (DecryptionFailedException $e) { |
|
266 | + if ($this->rootView->file_exists($target)) { |
|
267 | + $this->rootView->unlink($target); |
|
268 | + } |
|
269 | + return false; |
|
270 | + } |
|
271 | + |
|
272 | + return true; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * get current timestamp |
|
277 | + * |
|
278 | + * @return int |
|
279 | + */ |
|
280 | + protected function getTimestamp() { |
|
281 | + return time(); |
|
282 | + } |
|
283 | + |
|
284 | + |
|
285 | + /** |
|
286 | + * setup user file system |
|
287 | + * |
|
288 | + * @param string $uid |
|
289 | + */ |
|
290 | + protected function setupUserFS($uid) { |
|
291 | + \OC_Util::tearDownFS(); |
|
292 | + \OC_Util::setupFS($uid); |
|
293 | + } |
|
294 | 294 | } |
@@ -33,35 +33,35 @@ |
||
33 | 33 | * @since 14.0.0 |
34 | 34 | */ |
35 | 35 | class CloudFederationNotification implements ICloudFederationNotification { |
36 | - private $message = []; |
|
36 | + private $message = []; |
|
37 | 37 | |
38 | - /** |
|
39 | - * add a message to the notification |
|
40 | - * |
|
41 | - * @param string $notificationType (e.g. SHARE_ACCEPTED) |
|
42 | - * @param string $resourceType (e.g. file, calendar, contact,...) |
|
43 | - * @param string $providerId id of the share |
|
44 | - * @param array $notification payload of the notification |
|
45 | - * |
|
46 | - * @since 14.0.0 |
|
47 | - */ |
|
48 | - public function setMessage($notificationType, $resourceType, $providerId, array $notification) { |
|
49 | - $this->message = [ |
|
50 | - 'notificationType' => $notificationType, |
|
51 | - 'resourceType' => $resourceType, |
|
52 | - 'providerId' => $providerId, |
|
53 | - 'notification' => $notification, |
|
54 | - ]; |
|
55 | - } |
|
38 | + /** |
|
39 | + * add a message to the notification |
|
40 | + * |
|
41 | + * @param string $notificationType (e.g. SHARE_ACCEPTED) |
|
42 | + * @param string $resourceType (e.g. file, calendar, contact,...) |
|
43 | + * @param string $providerId id of the share |
|
44 | + * @param array $notification payload of the notification |
|
45 | + * |
|
46 | + * @since 14.0.0 |
|
47 | + */ |
|
48 | + public function setMessage($notificationType, $resourceType, $providerId, array $notification) { |
|
49 | + $this->message = [ |
|
50 | + 'notificationType' => $notificationType, |
|
51 | + 'resourceType' => $resourceType, |
|
52 | + 'providerId' => $providerId, |
|
53 | + 'notification' => $notification, |
|
54 | + ]; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * get message, ready to send out |
|
59 | - * |
|
60 | - * @return array |
|
61 | - * |
|
62 | - * @since 14.0.0 |
|
63 | - */ |
|
64 | - public function getMessage() { |
|
65 | - return $this->message; |
|
66 | - } |
|
57 | + /** |
|
58 | + * get message, ready to send out |
|
59 | + * |
|
60 | + * @return array |
|
61 | + * |
|
62 | + * @since 14.0.0 |
|
63 | + */ |
|
64 | + public function getMessage() { |
|
65 | + return $this->message; |
|
66 | + } |
|
67 | 67 | } |