@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | // Check that all parameters from $event exist in $previousEvent |
| 135 | 135 | foreach ($params1 as $key => $parameter) { |
| 136 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 136 | + if (preg_match('/^'.$mergeParameter.'(\d+)?$/', $key)) { |
|
| 137 | 137 | if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
| 138 | 138 | $combined++; |
| 139 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 139 | + $params[$mergeParameter.$combined] = $parameter; |
|
| 140 | 140 | } |
| 141 | 141 | continue; |
| 142 | 142 | } |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | |
| 152 | 152 | // Check that all parameters from $previousEvent exist in $event |
| 153 | 153 | foreach ($params2 as $key => $parameter) { |
| 154 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 154 | + if (preg_match('/^'.$mergeParameter.'(\d+)?$/', $key)) { |
|
| 155 | 155 | if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
| 156 | 156 | $combined++; |
| 157 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 157 | + $params[$mergeParameter.$combined] = $parameter; |
|
| 158 | 158 | } |
| 159 | 159 | continue; |
| 160 | 160 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) { |
| 180 | 180 | foreach ($parameters as $key => $param) { |
| 181 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 181 | + if (preg_match('/^'.$mergeParameter.'(\d+)?$/', $key)) { |
|
| 182 | 182 | if ($param === $parameter) { |
| 183 | 183 | return true; |
| 184 | 184 | } |
@@ -196,30 +196,30 @@ discard block |
||
| 196 | 196 | protected function getExtendedSubject($subject, $parameter, $counter) { |
| 197 | 197 | switch ($counter) { |
| 198 | 198 | case 1: |
| 199 | - $replacement = '{' . $parameter . '1}'; |
|
| 199 | + $replacement = '{'.$parameter.'1}'; |
|
| 200 | 200 | break; |
| 201 | 201 | case 2: |
| 202 | 202 | $replacement = $this->l10n->t( |
| 203 | 203 | '%1$s and %2$s', |
| 204 | - ['{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 204 | + ['{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 205 | 205 | ); |
| 206 | 206 | break; |
| 207 | 207 | case 3: |
| 208 | 208 | $replacement = $this->l10n->t( |
| 209 | 209 | '%1$s, %2$s and %3$s', |
| 210 | - ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 210 | + ['{'.$parameter.'3}', '{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 211 | 211 | ); |
| 212 | 212 | break; |
| 213 | 213 | case 4: |
| 214 | 214 | $replacement = $this->l10n->t( |
| 215 | 215 | '%1$s, %2$s, %3$s and %4$s', |
| 216 | - ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 216 | + ['{'.$parameter.'4}', '{'.$parameter.'3}', '{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 217 | 217 | ); |
| 218 | 218 | break; |
| 219 | 219 | case 5: |
| 220 | 220 | $replacement = $this->l10n->t( |
| 221 | 221 | '%1$s, %2$s, %3$s, %4$s and %5$s', |
| 222 | - ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 222 | + ['{'.$parameter.'5}', '{'.$parameter.'4}', '{'.$parameter.'3}', '{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 223 | 223 | ); |
| 224 | 224 | break; |
| 225 | 225 | default: |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | return str_replace( |
| 230 | - '{' . $parameter . '}', |
|
| 230 | + '{'.$parameter.'}', |
|
| 231 | 231 | $replacement, |
| 232 | 232 | $subject |
| 233 | 233 | ); |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | protected function generateParsedSubject($subject, $parameters) { |
| 242 | 242 | $placeholders = $replacements = []; |
| 243 | 243 | foreach ($parameters as $placeholder => $parameter) { |
| 244 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 244 | + $placeholders[] = '{'.$placeholder.'}'; |
|
| 245 | 245 | if ($parameter['type'] === 'file') { |
| 246 | 246 | $replacements[] = trim($parameter['path'], '/'); |
| 247 | 247 | } elseif (isset($parameter['name'])) { |
@@ -31,231 +31,231 @@ |
||
| 31 | 31 | |
| 32 | 32 | class EventMerger implements IEventMerger { |
| 33 | 33 | |
| 34 | - /** @var IL10N */ |
|
| 35 | - protected $l10n; |
|
| 34 | + /** @var IL10N */ |
|
| 35 | + protected $l10n; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param IL10N $l10n |
|
| 39 | - */ |
|
| 40 | - public function __construct(IL10N $l10n) { |
|
| 41 | - $this->l10n = $l10n; |
|
| 42 | - } |
|
| 37 | + /** |
|
| 38 | + * @param IL10N $l10n |
|
| 39 | + */ |
|
| 40 | + public function __construct(IL10N $l10n) { |
|
| 41 | + $this->l10n = $l10n; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Combines two events when possible to have grouping: |
|
| 46 | - * |
|
| 47 | - * Example1: Two events with subject '{user} created {file}' and |
|
| 48 | - * $mergeParameter file with different file and same user will be merged |
|
| 49 | - * to '{user} created {file1} and {file2}' and the childEvent on the return |
|
| 50 | - * will be set, if the events have been merged. |
|
| 51 | - * |
|
| 52 | - * Example2: Two events with subject '{user} created {file}' and |
|
| 53 | - * $mergeParameter file with same file and same user will be merged to |
|
| 54 | - * '{user} created {file1}' and the childEvent on the return will be set, if |
|
| 55 | - * the events have been merged. |
|
| 56 | - * |
|
| 57 | - * The following requirements have to be met, in order to be merged: |
|
| 58 | - * - Both events need to have the same `getApp()` |
|
| 59 | - * - Both events must not have a message `getMessage()` |
|
| 60 | - * - Both events need to have the same subject `getSubject()` |
|
| 61 | - * - Both events need to have the same object type `getObjectType()` |
|
| 62 | - * - The time difference between both events must not be bigger then 3 hours |
|
| 63 | - * - Only up to 5 events can be merged. |
|
| 64 | - * - All parameters apart from such starting with $mergeParameter must be |
|
| 65 | - * the same for both events. |
|
| 66 | - * |
|
| 67 | - * @param string $mergeParameter |
|
| 68 | - * @param IEvent $event |
|
| 69 | - * @param IEvent|null $previousEvent |
|
| 70 | - * @return IEvent |
|
| 71 | - */ |
|
| 72 | - public function mergeEvents($mergeParameter, IEvent $event, IEvent $previousEvent = null) { |
|
| 73 | - // No second event => can not combine |
|
| 74 | - if (!$previousEvent instanceof IEvent) { |
|
| 75 | - return $event; |
|
| 76 | - } |
|
| 44 | + /** |
|
| 45 | + * Combines two events when possible to have grouping: |
|
| 46 | + * |
|
| 47 | + * Example1: Two events with subject '{user} created {file}' and |
|
| 48 | + * $mergeParameter file with different file and same user will be merged |
|
| 49 | + * to '{user} created {file1} and {file2}' and the childEvent on the return |
|
| 50 | + * will be set, if the events have been merged. |
|
| 51 | + * |
|
| 52 | + * Example2: Two events with subject '{user} created {file}' and |
|
| 53 | + * $mergeParameter file with same file and same user will be merged to |
|
| 54 | + * '{user} created {file1}' and the childEvent on the return will be set, if |
|
| 55 | + * the events have been merged. |
|
| 56 | + * |
|
| 57 | + * The following requirements have to be met, in order to be merged: |
|
| 58 | + * - Both events need to have the same `getApp()` |
|
| 59 | + * - Both events must not have a message `getMessage()` |
|
| 60 | + * - Both events need to have the same subject `getSubject()` |
|
| 61 | + * - Both events need to have the same object type `getObjectType()` |
|
| 62 | + * - The time difference between both events must not be bigger then 3 hours |
|
| 63 | + * - Only up to 5 events can be merged. |
|
| 64 | + * - All parameters apart from such starting with $mergeParameter must be |
|
| 65 | + * the same for both events. |
|
| 66 | + * |
|
| 67 | + * @param string $mergeParameter |
|
| 68 | + * @param IEvent $event |
|
| 69 | + * @param IEvent|null $previousEvent |
|
| 70 | + * @return IEvent |
|
| 71 | + */ |
|
| 72 | + public function mergeEvents($mergeParameter, IEvent $event, IEvent $previousEvent = null) { |
|
| 73 | + // No second event => can not combine |
|
| 74 | + if (!$previousEvent instanceof IEvent) { |
|
| 75 | + return $event; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - // Different app => can not combine |
|
| 79 | - if ($event->getApp() !== $previousEvent->getApp()) { |
|
| 80 | - return $event; |
|
| 81 | - } |
|
| 78 | + // Different app => can not combine |
|
| 79 | + if ($event->getApp() !== $previousEvent->getApp()) { |
|
| 80 | + return $event; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - // Message is set => can not combine |
|
| 84 | - if ($event->getMessage() !== '' || $previousEvent->getMessage() !== '') { |
|
| 85 | - return $event; |
|
| 86 | - } |
|
| 83 | + // Message is set => can not combine |
|
| 84 | + if ($event->getMessage() !== '' || $previousEvent->getMessage() !== '') { |
|
| 85 | + return $event; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // Different subject => can not combine |
|
| 89 | - if ($event->getSubject() !== $previousEvent->getSubject()) { |
|
| 90 | - return $event; |
|
| 91 | - } |
|
| 88 | + // Different subject => can not combine |
|
| 89 | + if ($event->getSubject() !== $previousEvent->getSubject()) { |
|
| 90 | + return $event; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - // Different object type => can not combine |
|
| 94 | - if ($event->getObjectType() !== $previousEvent->getObjectType()) { |
|
| 95 | - return $event; |
|
| 96 | - } |
|
| 93 | + // Different object type => can not combine |
|
| 94 | + if ($event->getObjectType() !== $previousEvent->getObjectType()) { |
|
| 95 | + return $event; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - // More than 3 hours difference => can not combine |
|
| 99 | - if (abs($event->getTimestamp() - $previousEvent->getTimestamp()) > 3 * 60 * 60) { |
|
| 100 | - return $event; |
|
| 101 | - } |
|
| 98 | + // More than 3 hours difference => can not combine |
|
| 99 | + if (abs($event->getTimestamp() - $previousEvent->getTimestamp()) > 3 * 60 * 60) { |
|
| 100 | + return $event; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - // Other parameters are not the same => can not combine |
|
| 104 | - try { |
|
| 105 | - [$combined, $parameters] = $this->combineParameters($mergeParameter, $event, $previousEvent); |
|
| 106 | - } catch (\UnexpectedValueException $e) { |
|
| 107 | - return $event; |
|
| 108 | - } |
|
| 103 | + // Other parameters are not the same => can not combine |
|
| 104 | + try { |
|
| 105 | + [$combined, $parameters] = $this->combineParameters($mergeParameter, $event, $previousEvent); |
|
| 106 | + } catch (\UnexpectedValueException $e) { |
|
| 107 | + return $event; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - try { |
|
| 111 | - $newSubject = $this->getExtendedSubject($event->getRichSubject(), $mergeParameter, $combined); |
|
| 112 | - $parsedSubject = $this->generateParsedSubject($newSubject, $parameters); |
|
| 110 | + try { |
|
| 111 | + $newSubject = $this->getExtendedSubject($event->getRichSubject(), $mergeParameter, $combined); |
|
| 112 | + $parsedSubject = $this->generateParsedSubject($newSubject, $parameters); |
|
| 113 | 113 | |
| 114 | - $event->setRichSubject($newSubject, $parameters) |
|
| 115 | - ->setParsedSubject($parsedSubject) |
|
| 116 | - ->setChildEvent($previousEvent) |
|
| 117 | - ->setTimestamp(max($event->getTimestamp(), $previousEvent->getTimestamp())); |
|
| 118 | - } catch (\UnexpectedValueException $e) { |
|
| 119 | - return $event; |
|
| 120 | - } |
|
| 114 | + $event->setRichSubject($newSubject, $parameters) |
|
| 115 | + ->setParsedSubject($parsedSubject) |
|
| 116 | + ->setChildEvent($previousEvent) |
|
| 117 | + ->setTimestamp(max($event->getTimestamp(), $previousEvent->getTimestamp())); |
|
| 118 | + } catch (\UnexpectedValueException $e) { |
|
| 119 | + return $event; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - return $event; |
|
| 123 | - } |
|
| 122 | + return $event; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @param string $mergeParameter |
|
| 127 | - * @param IEvent $event |
|
| 128 | - * @param IEvent $previousEvent |
|
| 129 | - * @return array |
|
| 130 | - * @throws \UnexpectedValueException |
|
| 131 | - */ |
|
| 132 | - protected function combineParameters($mergeParameter, IEvent $event, IEvent $previousEvent) { |
|
| 133 | - $params1 = $event->getRichSubjectParameters(); |
|
| 134 | - $params2 = $previousEvent->getRichSubjectParameters(); |
|
| 135 | - $params = []; |
|
| 125 | + /** |
|
| 126 | + * @param string $mergeParameter |
|
| 127 | + * @param IEvent $event |
|
| 128 | + * @param IEvent $previousEvent |
|
| 129 | + * @return array |
|
| 130 | + * @throws \UnexpectedValueException |
|
| 131 | + */ |
|
| 132 | + protected function combineParameters($mergeParameter, IEvent $event, IEvent $previousEvent) { |
|
| 133 | + $params1 = $event->getRichSubjectParameters(); |
|
| 134 | + $params2 = $previousEvent->getRichSubjectParameters(); |
|
| 135 | + $params = []; |
|
| 136 | 136 | |
| 137 | - $combined = 0; |
|
| 137 | + $combined = 0; |
|
| 138 | 138 | |
| 139 | - // Check that all parameters from $event exist in $previousEvent |
|
| 140 | - foreach ($params1 as $key => $parameter) { |
|
| 141 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 142 | - if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 143 | - $combined++; |
|
| 144 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 145 | - } |
|
| 146 | - continue; |
|
| 147 | - } |
|
| 139 | + // Check that all parameters from $event exist in $previousEvent |
|
| 140 | + foreach ($params1 as $key => $parameter) { |
|
| 141 | + if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 142 | + if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 143 | + $combined++; |
|
| 144 | + $params[$mergeParameter . $combined] = $parameter; |
|
| 145 | + } |
|
| 146 | + continue; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - if (!isset($params2[$key]) || $params2[$key] !== $parameter) { |
|
| 150 | - // Parameter missing on $previousEvent or different => can not combine |
|
| 151 | - throw new \UnexpectedValueException(); |
|
| 152 | - } |
|
| 149 | + if (!isset($params2[$key]) || $params2[$key] !== $parameter) { |
|
| 150 | + // Parameter missing on $previousEvent or different => can not combine |
|
| 151 | + throw new \UnexpectedValueException(); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - $params[$key] = $parameter; |
|
| 155 | - } |
|
| 154 | + $params[$key] = $parameter; |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - // Check that all parameters from $previousEvent exist in $event |
|
| 158 | - foreach ($params2 as $key => $parameter) { |
|
| 159 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 160 | - if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 161 | - $combined++; |
|
| 162 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 163 | - } |
|
| 164 | - continue; |
|
| 165 | - } |
|
| 157 | + // Check that all parameters from $previousEvent exist in $event |
|
| 158 | + foreach ($params2 as $key => $parameter) { |
|
| 159 | + if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 160 | + if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 161 | + $combined++; |
|
| 162 | + $params[$mergeParameter . $combined] = $parameter; |
|
| 163 | + } |
|
| 164 | + continue; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - if (!isset($params1[$key]) || $params1[$key] !== $parameter) { |
|
| 168 | - // Parameter missing on $event or different => can not combine |
|
| 169 | - throw new \UnexpectedValueException(); |
|
| 170 | - } |
|
| 167 | + if (!isset($params1[$key]) || $params1[$key] !== $parameter) { |
|
| 168 | + // Parameter missing on $event or different => can not combine |
|
| 169 | + throw new \UnexpectedValueException(); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - $params[$key] = $parameter; |
|
| 173 | - } |
|
| 172 | + $params[$key] = $parameter; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - return [$combined, $params]; |
|
| 176 | - } |
|
| 175 | + return [$combined, $params]; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * @param array[] $parameters |
|
| 180 | - * @param string $mergeParameter |
|
| 181 | - * @param array $parameter |
|
| 182 | - * @return bool |
|
| 183 | - */ |
|
| 184 | - protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) { |
|
| 185 | - foreach ($parameters as $key => $param) { |
|
| 186 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 187 | - if ($param === $parameter) { |
|
| 188 | - return true; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - return false; |
|
| 193 | - } |
|
| 178 | + /** |
|
| 179 | + * @param array[] $parameters |
|
| 180 | + * @param string $mergeParameter |
|
| 181 | + * @param array $parameter |
|
| 182 | + * @return bool |
|
| 183 | + */ |
|
| 184 | + protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) { |
|
| 185 | + foreach ($parameters as $key => $param) { |
|
| 186 | + if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 187 | + if ($param === $parameter) { |
|
| 188 | + return true; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + return false; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * @param string $subject |
|
| 197 | - * @param string $parameter |
|
| 198 | - * @param int $counter |
|
| 199 | - * @return mixed |
|
| 200 | - */ |
|
| 201 | - protected function getExtendedSubject($subject, $parameter, $counter) { |
|
| 202 | - switch ($counter) { |
|
| 203 | - case 1: |
|
| 204 | - $replacement = '{' . $parameter . '1}'; |
|
| 205 | - break; |
|
| 206 | - case 2: |
|
| 207 | - $replacement = $this->l10n->t( |
|
| 208 | - '%1$s and %2$s', |
|
| 209 | - ['{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 210 | - ); |
|
| 211 | - break; |
|
| 212 | - case 3: |
|
| 213 | - $replacement = $this->l10n->t( |
|
| 214 | - '%1$s, %2$s and %3$s', |
|
| 215 | - ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 216 | - ); |
|
| 217 | - break; |
|
| 218 | - case 4: |
|
| 219 | - $replacement = $this->l10n->t( |
|
| 220 | - '%1$s, %2$s, %3$s and %4$s', |
|
| 221 | - ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 222 | - ); |
|
| 223 | - break; |
|
| 224 | - case 5: |
|
| 225 | - $replacement = $this->l10n->t( |
|
| 226 | - '%1$s, %2$s, %3$s, %4$s and %5$s', |
|
| 227 | - ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 228 | - ); |
|
| 229 | - break; |
|
| 230 | - default: |
|
| 231 | - throw new \UnexpectedValueException(); |
|
| 232 | - } |
|
| 195 | + /** |
|
| 196 | + * @param string $subject |
|
| 197 | + * @param string $parameter |
|
| 198 | + * @param int $counter |
|
| 199 | + * @return mixed |
|
| 200 | + */ |
|
| 201 | + protected function getExtendedSubject($subject, $parameter, $counter) { |
|
| 202 | + switch ($counter) { |
|
| 203 | + case 1: |
|
| 204 | + $replacement = '{' . $parameter . '1}'; |
|
| 205 | + break; |
|
| 206 | + case 2: |
|
| 207 | + $replacement = $this->l10n->t( |
|
| 208 | + '%1$s and %2$s', |
|
| 209 | + ['{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 210 | + ); |
|
| 211 | + break; |
|
| 212 | + case 3: |
|
| 213 | + $replacement = $this->l10n->t( |
|
| 214 | + '%1$s, %2$s and %3$s', |
|
| 215 | + ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 216 | + ); |
|
| 217 | + break; |
|
| 218 | + case 4: |
|
| 219 | + $replacement = $this->l10n->t( |
|
| 220 | + '%1$s, %2$s, %3$s and %4$s', |
|
| 221 | + ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 222 | + ); |
|
| 223 | + break; |
|
| 224 | + case 5: |
|
| 225 | + $replacement = $this->l10n->t( |
|
| 226 | + '%1$s, %2$s, %3$s, %4$s and %5$s', |
|
| 227 | + ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 228 | + ); |
|
| 229 | + break; |
|
| 230 | + default: |
|
| 231 | + throw new \UnexpectedValueException(); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - return str_replace( |
|
| 235 | - '{' . $parameter . '}', |
|
| 236 | - $replacement, |
|
| 237 | - $subject |
|
| 238 | - ); |
|
| 239 | - } |
|
| 234 | + return str_replace( |
|
| 235 | + '{' . $parameter . '}', |
|
| 236 | + $replacement, |
|
| 237 | + $subject |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * @param string $subject |
|
| 243 | - * @param array[] $parameters |
|
| 244 | - * @return string |
|
| 245 | - */ |
|
| 246 | - protected function generateParsedSubject($subject, $parameters) { |
|
| 247 | - $placeholders = $replacements = []; |
|
| 248 | - foreach ($parameters as $placeholder => $parameter) { |
|
| 249 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 250 | - if ($parameter['type'] === 'file') { |
|
| 251 | - $replacements[] = trim($parameter['path'], '/'); |
|
| 252 | - } elseif (isset($parameter['name'])) { |
|
| 253 | - $replacements[] = $parameter['name']; |
|
| 254 | - } else { |
|
| 255 | - $replacements[] = $parameter['id']; |
|
| 256 | - } |
|
| 257 | - } |
|
| 241 | + /** |
|
| 242 | + * @param string $subject |
|
| 243 | + * @param array[] $parameters |
|
| 244 | + * @return string |
|
| 245 | + */ |
|
| 246 | + protected function generateParsedSubject($subject, $parameters) { |
|
| 247 | + $placeholders = $replacements = []; |
|
| 248 | + foreach ($parameters as $placeholder => $parameter) { |
|
| 249 | + $placeholders[] = '{' . $placeholder . '}'; |
|
| 250 | + if ($parameter['type'] === 'file') { |
|
| 251 | + $replacements[] = trim($parameter['path'], '/'); |
|
| 252 | + } elseif (isset($parameter['name'])) { |
|
| 253 | + $replacements[] = $parameter['name']; |
|
| 254 | + } else { |
|
| 255 | + $replacements[] = $parameter['id']; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - return str_replace($placeholders, $replacements, $subject); |
|
| 260 | - } |
|
| 259 | + return str_replace($placeholders, $replacements, $subject); |
|
| 260 | + } |
|
| 261 | 261 | } |
@@ -26,37 +26,37 @@ |
||
| 26 | 26 | namespace OC\Template; |
| 27 | 27 | |
| 28 | 28 | class TemplateFileLocator { |
| 29 | - protected $dirs; |
|
| 30 | - private $path; |
|
| 29 | + protected $dirs; |
|
| 30 | + private $path; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @param string[] $dirs |
|
| 34 | - */ |
|
| 35 | - public function __construct($dirs) { |
|
| 36 | - $this->dirs = $dirs; |
|
| 37 | - } |
|
| 32 | + /** |
|
| 33 | + * @param string[] $dirs |
|
| 34 | + */ |
|
| 35 | + public function __construct($dirs) { |
|
| 36 | + $this->dirs = $dirs; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $template |
|
| 41 | - * @return string |
|
| 42 | - * @throws \Exception |
|
| 43 | - */ |
|
| 44 | - public function find($template) { |
|
| 45 | - if ($template === '') { |
|
| 46 | - throw new \InvalidArgumentException('Empty template name'); |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * @param string $template |
|
| 41 | + * @return string |
|
| 42 | + * @throws \Exception |
|
| 43 | + */ |
|
| 44 | + public function find($template) { |
|
| 45 | + if ($template === '') { |
|
| 46 | + throw new \InvalidArgumentException('Empty template name'); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - foreach ($this->dirs as $dir) { |
|
| 50 | - $file = $dir.$template.'.php'; |
|
| 51 | - if (is_file($file)) { |
|
| 52 | - $this->path = $dir; |
|
| 53 | - return $file; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - throw new \Exception('template file not found: template:'.$template); |
|
| 57 | - } |
|
| 49 | + foreach ($this->dirs as $dir) { |
|
| 50 | + $file = $dir.$template.'.php'; |
|
| 51 | + if (is_file($file)) { |
|
| 52 | + $this->path = $dir; |
|
| 53 | + return $file; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + throw new \Exception('template file not found: template:'.$template); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function getPath() { |
|
| 60 | - return $this->path; |
|
| 61 | - } |
|
| 59 | + public function getPath() { |
|
| 60 | + return $this->path; |
|
| 61 | + } |
|
| 62 | 62 | } |
@@ -65,7 +65,7 @@ |
||
| 65 | 65 | |
| 66 | 66 | if ($app_path === false && $app_url === false) { |
| 67 | 67 | $this->logger->error('Could not find resource {resource} to load', [ |
| 68 | - 'resource' => $app . '/' . $style . '.css', |
|
| 68 | + 'resource' => $app.'/'.$style.'.css', |
|
| 69 | 69 | 'app' => 'cssresourceloader', |
| 70 | 70 | ]); |
| 71 | 71 | return; |
@@ -35,81 +35,81 @@ |
||
| 35 | 35 | |
| 36 | 36 | class CSSResourceLocator extends ResourceLocator { |
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @param string $theme |
|
| 40 | - * @param array $core_map |
|
| 41 | - * @param array $party_map |
|
| 42 | - */ |
|
| 43 | - public function __construct(LoggerInterface $logger, $theme, $core_map, $party_map) { |
|
| 44 | - parent::__construct($logger, $theme, $core_map, $party_map); |
|
| 45 | - } |
|
| 38 | + /** |
|
| 39 | + * @param string $theme |
|
| 40 | + * @param array $core_map |
|
| 41 | + * @param array $party_map |
|
| 42 | + */ |
|
| 43 | + public function __construct(LoggerInterface $logger, $theme, $core_map, $party_map) { |
|
| 44 | + parent::__construct($logger, $theme, $core_map, $party_map); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param string $style |
|
| 49 | - */ |
|
| 50 | - public function doFind($style) { |
|
| 51 | - $app = substr($style, 0, strpos($style, '/')); |
|
| 52 | - if (strpos($style, '3rdparty') === 0 |
|
| 53 | - && $this->appendIfExist($this->thirdpartyroot, $style.'.css') |
|
| 54 | - || $this->appendIfExist($this->serverroot, $style.'.css') |
|
| 55 | - || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') |
|
| 56 | - ) { |
|
| 57 | - return; |
|
| 58 | - } |
|
| 59 | - $style = substr($style, strpos($style, '/') + 1); |
|
| 60 | - $app_path = \OC_App::getAppPath($app); |
|
| 61 | - $app_url = \OC_App::getAppWebPath($app); |
|
| 47 | + /** |
|
| 48 | + * @param string $style |
|
| 49 | + */ |
|
| 50 | + public function doFind($style) { |
|
| 51 | + $app = substr($style, 0, strpos($style, '/')); |
|
| 52 | + if (strpos($style, '3rdparty') === 0 |
|
| 53 | + && $this->appendIfExist($this->thirdpartyroot, $style.'.css') |
|
| 54 | + || $this->appendIfExist($this->serverroot, $style.'.css') |
|
| 55 | + || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') |
|
| 56 | + ) { |
|
| 57 | + return; |
|
| 58 | + } |
|
| 59 | + $style = substr($style, strpos($style, '/') + 1); |
|
| 60 | + $app_path = \OC_App::getAppPath($app); |
|
| 61 | + $app_url = \OC_App::getAppWebPath($app); |
|
| 62 | 62 | |
| 63 | - if ($app_path === false && $app_url === false) { |
|
| 64 | - $this->logger->error('Could not find resource {resource} to load', [ |
|
| 65 | - 'resource' => $app . '/' . $style . '.css', |
|
| 66 | - 'app' => 'cssresourceloader', |
|
| 67 | - ]); |
|
| 68 | - return; |
|
| 69 | - } |
|
| 63 | + if ($app_path === false && $app_url === false) { |
|
| 64 | + $this->logger->error('Could not find resource {resource} to load', [ |
|
| 65 | + 'resource' => $app . '/' . $style . '.css', |
|
| 66 | + 'app' => 'cssresourceloader', |
|
| 67 | + ]); |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - // Account for the possibility of having symlinks in app path. Doing |
|
| 72 | - // this here instead of above as an empty argument to realpath gets |
|
| 73 | - // turned into cwd. |
|
| 74 | - $app_path = realpath($app_path); |
|
| 71 | + // Account for the possibility of having symlinks in app path. Doing |
|
| 72 | + // this here instead of above as an empty argument to realpath gets |
|
| 73 | + // turned into cwd. |
|
| 74 | + $app_path = realpath($app_path); |
|
| 75 | 75 | |
| 76 | - $this->append($app_path, $style.'.css', $app_url); |
|
| 77 | - } |
|
| 76 | + $this->append($app_path, $style.'.css', $app_url); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * @param string $style |
|
| 81 | - */ |
|
| 82 | - public function doFindTheme($style) { |
|
| 83 | - $theme_dir = 'themes/'.$this->theme.'/'; |
|
| 84 | - $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') |
|
| 85 | - || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') |
|
| 86 | - || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); |
|
| 87 | - } |
|
| 79 | + /** |
|
| 80 | + * @param string $style |
|
| 81 | + */ |
|
| 82 | + public function doFindTheme($style) { |
|
| 83 | + $theme_dir = 'themes/'.$this->theme.'/'; |
|
| 84 | + $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') |
|
| 85 | + || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') |
|
| 86 | + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { |
|
| 90 | - if (!$scss) { |
|
| 91 | - parent::append($root, $file, $webRoot, $throw); |
|
| 92 | - } else { |
|
| 93 | - if (!$webRoot) { |
|
| 94 | - $webRoot = $this->findWebRoot($root); |
|
| 89 | + public function append($root, $file, $webRoot = null, $throw = true, $scss = false) { |
|
| 90 | + if (!$scss) { |
|
| 91 | + parent::append($root, $file, $webRoot, $throw); |
|
| 92 | + } else { |
|
| 93 | + if (!$webRoot) { |
|
| 94 | + $webRoot = $this->findWebRoot($root); |
|
| 95 | 95 | |
| 96 | - if ($webRoot === null) { |
|
| 97 | - $webRoot = ''; |
|
| 98 | - $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
| 99 | - 'app' => 'lib', |
|
| 100 | - 'root' => $root, |
|
| 101 | - 'file' => $file, |
|
| 102 | - 'webRoot' => $webRoot, |
|
| 103 | - 'throw' => $throw ? 'true' : 'false' |
|
| 104 | - ]); |
|
| 96 | + if ($webRoot === null) { |
|
| 97 | + $webRoot = ''; |
|
| 98 | + $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
| 99 | + 'app' => 'lib', |
|
| 100 | + 'root' => $root, |
|
| 101 | + 'file' => $file, |
|
| 102 | + 'webRoot' => $webRoot, |
|
| 103 | + 'throw' => $throw ? 'true' : 'false' |
|
| 104 | + ]); |
|
| 105 | 105 | |
| 106 | - if ($throw && $root === '/') { |
|
| 107 | - throw new ResourceNotFoundException($file, $webRoot); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - } |
|
| 106 | + if ($throw && $root === '/') { |
|
| 107 | + throw new ResourceNotFoundException($file, $webRoot); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file]; |
|
| 113 | - } |
|
| 114 | - } |
|
| 112 | + $this->resources[] = [$webRoot ?: \OC::$WEBROOT, $webRoot, $file]; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | 115 | } |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | $this->doFind($resource); |
| 76 | 76 | } catch (ResourceNotFoundException $e) { |
| 77 | 77 | $resourceApp = substr($resource, 0, strpos($resource, '/')); |
| 78 | - $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
| 78 | + $this->logger->debug('Could not find resource file "'.$e->getResourcePath().'"', ['app' => $resourceApp]); |
|
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | if (!empty($this->theme)) { |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | $this->doFindTheme($resource); |
| 85 | 85 | } catch (ResourceNotFoundException $e) { |
| 86 | 86 | $resourceApp = substr($resource, 0, strpos($resource, '/')); |
| 87 | - $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
| 87 | + $this->logger->debug('Could not find resource file in theme "'.$e->getResourcePath().'"', ['app' => $resourceApp]); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | } |
@@ -158,7 +158,7 @@ discard block |
||
| 158 | 158 | } |
| 159 | 159 | $this->resources[] = [$root, $webRoot, $file]; |
| 160 | 160 | |
| 161 | - if ($throw && !is_file($root . '/' . $file)) { |
|
| 161 | + if ($throw && !is_file($root.'/'.$file)) { |
|
| 162 | 162 | throw new ResourceNotFoundException($file, $webRoot); |
| 163 | 163 | } |
| 164 | 164 | } |
@@ -32,170 +32,170 @@ |
||
| 32 | 32 | use Psr\Log\LoggerInterface; |
| 33 | 33 | |
| 34 | 34 | abstract class ResourceLocator { |
| 35 | - protected $theme; |
|
| 36 | - |
|
| 37 | - protected $mapping; |
|
| 38 | - protected $serverroot; |
|
| 39 | - protected $thirdpartyroot; |
|
| 40 | - protected $webroot; |
|
| 41 | - |
|
| 42 | - protected $resources = []; |
|
| 43 | - |
|
| 44 | - protected LoggerInterface $logger; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param string $theme |
|
| 48 | - * @param array $core_map |
|
| 49 | - * @param array $party_map |
|
| 50 | - */ |
|
| 51 | - public function __construct(LoggerInterface $logger, $theme, $core_map, $party_map) { |
|
| 52 | - $this->logger = $logger; |
|
| 53 | - $this->theme = $theme; |
|
| 54 | - $this->mapping = $core_map + $party_map; |
|
| 55 | - $this->serverroot = key($core_map); |
|
| 56 | - $this->thirdpartyroot = key($party_map); |
|
| 57 | - $this->webroot = $this->mapping[$this->serverroot]; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param string $resource |
|
| 62 | - */ |
|
| 63 | - abstract public function doFind($resource); |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @param string $resource |
|
| 67 | - */ |
|
| 68 | - abstract public function doFindTheme($resource); |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Finds the resources and adds them to the list |
|
| 72 | - * |
|
| 73 | - * @param array $resources |
|
| 74 | - */ |
|
| 75 | - public function find($resources) { |
|
| 76 | - foreach ($resources as $resource) { |
|
| 77 | - try { |
|
| 78 | - $this->doFind($resource); |
|
| 79 | - } catch (ResourceNotFoundException $e) { |
|
| 80 | - $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
| 81 | - $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - if (!empty($this->theme)) { |
|
| 85 | - foreach ($resources as $resource) { |
|
| 86 | - try { |
|
| 87 | - $this->doFindTheme($resource); |
|
| 88 | - } catch (ResourceNotFoundException $e) { |
|
| 89 | - $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
| 90 | - $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * append the $file resource if exist at $root |
|
| 98 | - * |
|
| 99 | - * @param string $root path to check |
|
| 100 | - * @param string $file the filename |
|
| 101 | - * @param string|null $webRoot base for path, default map $root to $webRoot |
|
| 102 | - * @return bool True if the resource was found, false otherwise |
|
| 103 | - */ |
|
| 104 | - protected function appendIfExist($root, $file, $webRoot = null) { |
|
| 105 | - if ($root !== false && is_file($root.'/'.$file)) { |
|
| 106 | - $this->append($root, $file, $webRoot, false); |
|
| 107 | - return true; |
|
| 108 | - } |
|
| 109 | - return false; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Attempt to find the webRoot |
|
| 114 | - * |
|
| 115 | - * traverse the potential web roots upwards in the path |
|
| 116 | - * |
|
| 117 | - * example: |
|
| 118 | - * - root: /srv/www/apps/myapp |
|
| 119 | - * - available mappings: ['/srv/www'] |
|
| 120 | - * |
|
| 121 | - * First we check if a mapping for /srv/www/apps/myapp is available, |
|
| 122 | - * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a |
|
| 123 | - * valid web root |
|
| 124 | - * |
|
| 125 | - * @param string $root |
|
| 126 | - * @return string|null The web root or null on failure |
|
| 127 | - */ |
|
| 128 | - protected function findWebRoot($root) { |
|
| 129 | - $webRoot = null; |
|
| 130 | - $tmpRoot = $root; |
|
| 131 | - |
|
| 132 | - while ($webRoot === null) { |
|
| 133 | - if (isset($this->mapping[$tmpRoot])) { |
|
| 134 | - $webRoot = $this->mapping[$tmpRoot]; |
|
| 135 | - break; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ($tmpRoot === '/') { |
|
| 139 | - break; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $tmpRoot = dirname($tmpRoot); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - if ($webRoot === null) { |
|
| 146 | - $realpath = realpath($root); |
|
| 147 | - |
|
| 148 | - if ($realpath && ($realpath !== $root)) { |
|
| 149 | - return $this->findWebRoot($realpath); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $webRoot; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * append the $file resource at $root |
|
| 158 | - * |
|
| 159 | - * @param string $root path to check |
|
| 160 | - * @param string $file the filename |
|
| 161 | - * @param string|null $webRoot base for path, default map $root to $webRoot |
|
| 162 | - * @param bool $throw Throw an exception, when the route does not exist |
|
| 163 | - * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing |
|
| 164 | - */ |
|
| 165 | - protected function append($root, $file, $webRoot = null, $throw = true) { |
|
| 166 | - if (!is_string($root)) { |
|
| 167 | - if ($throw) { |
|
| 168 | - throw new ResourceNotFoundException($file, $webRoot); |
|
| 169 | - } |
|
| 170 | - return; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - if (!$webRoot) { |
|
| 174 | - $webRoot = $this->findWebRoot($root); |
|
| 175 | - |
|
| 176 | - if ($webRoot === null) { |
|
| 177 | - $webRoot = ''; |
|
| 178 | - $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
| 179 | - 'app' => 'lib', |
|
| 180 | - 'root' => $root, |
|
| 181 | - 'file' => $file, |
|
| 182 | - 'webRoot' => $webRoot, |
|
| 183 | - 'throw' => $throw ? 'true' : 'false' |
|
| 184 | - ]); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - $this->resources[] = [$root, $webRoot, $file]; |
|
| 188 | - |
|
| 189 | - if ($throw && !is_file($root . '/' . $file)) { |
|
| 190 | - throw new ResourceNotFoundException($file, $webRoot); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Returns the list of all resources that should be loaded |
|
| 196 | - * @return array |
|
| 197 | - */ |
|
| 198 | - public function getResources() { |
|
| 199 | - return $this->resources; |
|
| 200 | - } |
|
| 35 | + protected $theme; |
|
| 36 | + |
|
| 37 | + protected $mapping; |
|
| 38 | + protected $serverroot; |
|
| 39 | + protected $thirdpartyroot; |
|
| 40 | + protected $webroot; |
|
| 41 | + |
|
| 42 | + protected $resources = []; |
|
| 43 | + |
|
| 44 | + protected LoggerInterface $logger; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param string $theme |
|
| 48 | + * @param array $core_map |
|
| 49 | + * @param array $party_map |
|
| 50 | + */ |
|
| 51 | + public function __construct(LoggerInterface $logger, $theme, $core_map, $party_map) { |
|
| 52 | + $this->logger = $logger; |
|
| 53 | + $this->theme = $theme; |
|
| 54 | + $this->mapping = $core_map + $party_map; |
|
| 55 | + $this->serverroot = key($core_map); |
|
| 56 | + $this->thirdpartyroot = key($party_map); |
|
| 57 | + $this->webroot = $this->mapping[$this->serverroot]; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param string $resource |
|
| 62 | + */ |
|
| 63 | + abstract public function doFind($resource); |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @param string $resource |
|
| 67 | + */ |
|
| 68 | + abstract public function doFindTheme($resource); |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Finds the resources and adds them to the list |
|
| 72 | + * |
|
| 73 | + * @param array $resources |
|
| 74 | + */ |
|
| 75 | + public function find($resources) { |
|
| 76 | + foreach ($resources as $resource) { |
|
| 77 | + try { |
|
| 78 | + $this->doFind($resource); |
|
| 79 | + } catch (ResourceNotFoundException $e) { |
|
| 80 | + $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
| 81 | + $this->logger->debug('Could not find resource file "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + if (!empty($this->theme)) { |
|
| 85 | + foreach ($resources as $resource) { |
|
| 86 | + try { |
|
| 87 | + $this->doFindTheme($resource); |
|
| 88 | + } catch (ResourceNotFoundException $e) { |
|
| 89 | + $resourceApp = substr($resource, 0, strpos($resource, '/')); |
|
| 90 | + $this->logger->debug('Could not find resource file in theme "' . $e->getResourcePath() . '"', ['app' => $resourceApp]); |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * append the $file resource if exist at $root |
|
| 98 | + * |
|
| 99 | + * @param string $root path to check |
|
| 100 | + * @param string $file the filename |
|
| 101 | + * @param string|null $webRoot base for path, default map $root to $webRoot |
|
| 102 | + * @return bool True if the resource was found, false otherwise |
|
| 103 | + */ |
|
| 104 | + protected function appendIfExist($root, $file, $webRoot = null) { |
|
| 105 | + if ($root !== false && is_file($root.'/'.$file)) { |
|
| 106 | + $this->append($root, $file, $webRoot, false); |
|
| 107 | + return true; |
|
| 108 | + } |
|
| 109 | + return false; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Attempt to find the webRoot |
|
| 114 | + * |
|
| 115 | + * traverse the potential web roots upwards in the path |
|
| 116 | + * |
|
| 117 | + * example: |
|
| 118 | + * - root: /srv/www/apps/myapp |
|
| 119 | + * - available mappings: ['/srv/www'] |
|
| 120 | + * |
|
| 121 | + * First we check if a mapping for /srv/www/apps/myapp is available, |
|
| 122 | + * then /srv/www/apps, /srv/www/apps, /srv/www, ... until we find a |
|
| 123 | + * valid web root |
|
| 124 | + * |
|
| 125 | + * @param string $root |
|
| 126 | + * @return string|null The web root or null on failure |
|
| 127 | + */ |
|
| 128 | + protected function findWebRoot($root) { |
|
| 129 | + $webRoot = null; |
|
| 130 | + $tmpRoot = $root; |
|
| 131 | + |
|
| 132 | + while ($webRoot === null) { |
|
| 133 | + if (isset($this->mapping[$tmpRoot])) { |
|
| 134 | + $webRoot = $this->mapping[$tmpRoot]; |
|
| 135 | + break; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ($tmpRoot === '/') { |
|
| 139 | + break; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $tmpRoot = dirname($tmpRoot); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + if ($webRoot === null) { |
|
| 146 | + $realpath = realpath($root); |
|
| 147 | + |
|
| 148 | + if ($realpath && ($realpath !== $root)) { |
|
| 149 | + return $this->findWebRoot($realpath); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $webRoot; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * append the $file resource at $root |
|
| 158 | + * |
|
| 159 | + * @param string $root path to check |
|
| 160 | + * @param string $file the filename |
|
| 161 | + * @param string|null $webRoot base for path, default map $root to $webRoot |
|
| 162 | + * @param bool $throw Throw an exception, when the route does not exist |
|
| 163 | + * @throws ResourceNotFoundException Only thrown when $throw is true and the resource is missing |
|
| 164 | + */ |
|
| 165 | + protected function append($root, $file, $webRoot = null, $throw = true) { |
|
| 166 | + if (!is_string($root)) { |
|
| 167 | + if ($throw) { |
|
| 168 | + throw new ResourceNotFoundException($file, $webRoot); |
|
| 169 | + } |
|
| 170 | + return; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + if (!$webRoot) { |
|
| 174 | + $webRoot = $this->findWebRoot($root); |
|
| 175 | + |
|
| 176 | + if ($webRoot === null) { |
|
| 177 | + $webRoot = ''; |
|
| 178 | + $this->logger->error('ResourceLocator can not find a web root (root: {root}, file: {file}, webRoot: {webRoot}, throw: {throw})', [ |
|
| 179 | + 'app' => 'lib', |
|
| 180 | + 'root' => $root, |
|
| 181 | + 'file' => $file, |
|
| 182 | + 'webRoot' => $webRoot, |
|
| 183 | + 'throw' => $throw ? 'true' : 'false' |
|
| 184 | + ]); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + $this->resources[] = [$root, $webRoot, $file]; |
|
| 188 | + |
|
| 189 | + if ($throw && !is_file($root . '/' . $file)) { |
|
| 190 | + throw new ResourceNotFoundException($file, $webRoot); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Returns the list of all resources that should be loaded |
|
| 196 | + * @return array |
|
| 197 | + */ |
|
| 198 | + public function getResources() { |
|
| 199 | + return $this->resources; |
|
| 200 | + } |
|
| 201 | 201 | } |
@@ -117,7 +117,7 @@ |
||
| 117 | 117 | if (array_key_exists($key, $this->vars)) { |
| 118 | 118 | $this->vars[$key][] = $value; |
| 119 | 119 | } else { |
| 120 | - $this->vars[$key] = [ $value ]; |
|
| 120 | + $this->vars[$key] = [$value]; |
|
| 121 | 121 | } |
| 122 | 122 | } |
| 123 | 123 | |
@@ -33,159 +33,159 @@ |
||
| 33 | 33 | use Throwable; |
| 34 | 34 | |
| 35 | 35 | class Base { |
| 36 | - private $template; // The template |
|
| 37 | - private $vars; // Vars |
|
| 36 | + private $template; // The template |
|
| 37 | + private $vars; // Vars |
|
| 38 | 38 | |
| 39 | - /** @var \OCP\IL10N */ |
|
| 40 | - private $l10n; |
|
| 39 | + /** @var \OCP\IL10N */ |
|
| 40 | + private $l10n; |
|
| 41 | 41 | |
| 42 | - /** @var Defaults */ |
|
| 43 | - private $theme; |
|
| 42 | + /** @var Defaults */ |
|
| 43 | + private $theme; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param string $template |
|
| 47 | - * @param string $requestToken |
|
| 48 | - * @param \OCP\IL10N $l10n |
|
| 49 | - * @param Defaults $theme |
|
| 50 | - */ |
|
| 51 | - public function __construct($template, $requestToken, $l10n, $theme) { |
|
| 52 | - $this->vars = []; |
|
| 53 | - $this->vars['requesttoken'] = $requestToken; |
|
| 54 | - $this->l10n = $l10n; |
|
| 55 | - $this->template = $template; |
|
| 56 | - $this->theme = $theme; |
|
| 57 | - } |
|
| 45 | + /** |
|
| 46 | + * @param string $template |
|
| 47 | + * @param string $requestToken |
|
| 48 | + * @param \OCP\IL10N $l10n |
|
| 49 | + * @param Defaults $theme |
|
| 50 | + */ |
|
| 51 | + public function __construct($template, $requestToken, $l10n, $theme) { |
|
| 52 | + $this->vars = []; |
|
| 53 | + $this->vars['requesttoken'] = $requestToken; |
|
| 54 | + $this->l10n = $l10n; |
|
| 55 | + $this->template = $template; |
|
| 56 | + $this->theme = $theme; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @param string $serverRoot |
|
| 61 | - * @param string|false $app_dir |
|
| 62 | - * @param string $theme |
|
| 63 | - * @param string $app |
|
| 64 | - * @return string[] |
|
| 65 | - */ |
|
| 66 | - protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
| 67 | - // Check if the app is in the app folder or in the root |
|
| 68 | - if ($app_dir !== false && file_exists($app_dir.'/templates/')) { |
|
| 69 | - return [ |
|
| 70 | - $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
| 71 | - $app_dir.'/templates/', |
|
| 72 | - ]; |
|
| 73 | - } |
|
| 74 | - return [ |
|
| 75 | - $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
| 76 | - $serverRoot.'/'.$app.'/templates/', |
|
| 77 | - ]; |
|
| 78 | - } |
|
| 59 | + /** |
|
| 60 | + * @param string $serverRoot |
|
| 61 | + * @param string|false $app_dir |
|
| 62 | + * @param string $theme |
|
| 63 | + * @param string $app |
|
| 64 | + * @return string[] |
|
| 65 | + */ |
|
| 66 | + protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
| 67 | + // Check if the app is in the app folder or in the root |
|
| 68 | + if ($app_dir !== false && file_exists($app_dir.'/templates/')) { |
|
| 69 | + return [ |
|
| 70 | + $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
| 71 | + $app_dir.'/templates/', |
|
| 72 | + ]; |
|
| 73 | + } |
|
| 74 | + return [ |
|
| 75 | + $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
| 76 | + $serverRoot.'/'.$app.'/templates/', |
|
| 77 | + ]; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @param string $serverRoot |
|
| 82 | - * @param string $theme |
|
| 83 | - * @return string[] |
|
| 84 | - */ |
|
| 85 | - protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
| 86 | - return [ |
|
| 87 | - $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
| 88 | - $serverRoot.'/core/templates/', |
|
| 89 | - ]; |
|
| 90 | - } |
|
| 80 | + /** |
|
| 81 | + * @param string $serverRoot |
|
| 82 | + * @param string $theme |
|
| 83 | + * @return string[] |
|
| 84 | + */ |
|
| 85 | + protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
| 86 | + return [ |
|
| 87 | + $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
| 88 | + $serverRoot.'/core/templates/', |
|
| 89 | + ]; |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | - /** |
|
| 93 | - * Assign variables |
|
| 94 | - * @param string $key key |
|
| 95 | - * @param float|array|bool|integer|string|Throwable $value value |
|
| 96 | - * @return bool |
|
| 97 | - * |
|
| 98 | - * This function assigns a variable. It can be accessed via $_[$key] in |
|
| 99 | - * the template. |
|
| 100 | - * |
|
| 101 | - * If the key existed before, it will be overwritten |
|
| 102 | - */ |
|
| 103 | - public function assign($key, $value) { |
|
| 104 | - $this->vars[$key] = $value; |
|
| 105 | - return true; |
|
| 106 | - } |
|
| 92 | + /** |
|
| 93 | + * Assign variables |
|
| 94 | + * @param string $key key |
|
| 95 | + * @param float|array|bool|integer|string|Throwable $value value |
|
| 96 | + * @return bool |
|
| 97 | + * |
|
| 98 | + * This function assigns a variable. It can be accessed via $_[$key] in |
|
| 99 | + * the template. |
|
| 100 | + * |
|
| 101 | + * If the key existed before, it will be overwritten |
|
| 102 | + */ |
|
| 103 | + public function assign($key, $value) { |
|
| 104 | + $this->vars[$key] = $value; |
|
| 105 | + return true; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Appends a variable |
|
| 110 | - * @param string $key key |
|
| 111 | - * @param mixed $value value |
|
| 112 | - * |
|
| 113 | - * This function assigns a variable in an array context. If the key already |
|
| 114 | - * exists, the value will be appended. It can be accessed via |
|
| 115 | - * $_[$key][$position] in the template. |
|
| 116 | - */ |
|
| 117 | - public function append($key, $value) { |
|
| 118 | - if (array_key_exists($key, $this->vars)) { |
|
| 119 | - $this->vars[$key][] = $value; |
|
| 120 | - } else { |
|
| 121 | - $this->vars[$key] = [ $value ]; |
|
| 122 | - } |
|
| 123 | - } |
|
| 108 | + /** |
|
| 109 | + * Appends a variable |
|
| 110 | + * @param string $key key |
|
| 111 | + * @param mixed $value value |
|
| 112 | + * |
|
| 113 | + * This function assigns a variable in an array context. If the key already |
|
| 114 | + * exists, the value will be appended. It can be accessed via |
|
| 115 | + * $_[$key][$position] in the template. |
|
| 116 | + */ |
|
| 117 | + public function append($key, $value) { |
|
| 118 | + if (array_key_exists($key, $this->vars)) { |
|
| 119 | + $this->vars[$key][] = $value; |
|
| 120 | + } else { |
|
| 121 | + $this->vars[$key] = [ $value ]; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Prints the proceeded template |
|
| 127 | - * @return bool |
|
| 128 | - * |
|
| 129 | - * This function proceeds the template and prints its output. |
|
| 130 | - */ |
|
| 131 | - public function printPage() { |
|
| 132 | - $data = $this->fetchPage(); |
|
| 133 | - if ($data === false) { |
|
| 134 | - return false; |
|
| 135 | - } else { |
|
| 136 | - print $data; |
|
| 137 | - return true; |
|
| 138 | - } |
|
| 139 | - } |
|
| 125 | + /** |
|
| 126 | + * Prints the proceeded template |
|
| 127 | + * @return bool |
|
| 128 | + * |
|
| 129 | + * This function proceeds the template and prints its output. |
|
| 130 | + */ |
|
| 131 | + public function printPage() { |
|
| 132 | + $data = $this->fetchPage(); |
|
| 133 | + if ($data === false) { |
|
| 134 | + return false; |
|
| 135 | + } else { |
|
| 136 | + print $data; |
|
| 137 | + return true; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Process the template |
|
| 143 | - * |
|
| 144 | - * @param array|null $additionalParams |
|
| 145 | - * @return string This function processes the template. |
|
| 146 | - * |
|
| 147 | - * This function processes the template. |
|
| 148 | - */ |
|
| 149 | - public function fetchPage($additionalParams = null) { |
|
| 150 | - return $this->load($this->template, $additionalParams); |
|
| 151 | - } |
|
| 141 | + /** |
|
| 142 | + * Process the template |
|
| 143 | + * |
|
| 144 | + * @param array|null $additionalParams |
|
| 145 | + * @return string This function processes the template. |
|
| 146 | + * |
|
| 147 | + * This function processes the template. |
|
| 148 | + */ |
|
| 149 | + public function fetchPage($additionalParams = null) { |
|
| 150 | + return $this->load($this->template, $additionalParams); |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * doing the actual work |
|
| 155 | - * |
|
| 156 | - * @param string $file |
|
| 157 | - * @param array|null $additionalParams |
|
| 158 | - * @return string content |
|
| 159 | - * |
|
| 160 | - * Includes the template file, fetches its output |
|
| 161 | - */ |
|
| 162 | - protected function load($file, $additionalParams = null) { |
|
| 163 | - // Register the variables |
|
| 164 | - $_ = $this->vars; |
|
| 165 | - $l = $this->l10n; |
|
| 166 | - $theme = $this->theme; |
|
| 153 | + /** |
|
| 154 | + * doing the actual work |
|
| 155 | + * |
|
| 156 | + * @param string $file |
|
| 157 | + * @param array|null $additionalParams |
|
| 158 | + * @return string content |
|
| 159 | + * |
|
| 160 | + * Includes the template file, fetches its output |
|
| 161 | + */ |
|
| 162 | + protected function load($file, $additionalParams = null) { |
|
| 163 | + // Register the variables |
|
| 164 | + $_ = $this->vars; |
|
| 165 | + $l = $this->l10n; |
|
| 166 | + $theme = $this->theme; |
|
| 167 | 167 | |
| 168 | - if (!is_null($additionalParams)) { |
|
| 169 | - $_ = array_merge($additionalParams, $this->vars); |
|
| 170 | - foreach ($_ as $var => $value) { |
|
| 171 | - if (!isset(${$var})) { |
|
| 172 | - ${$var} = $value; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - } |
|
| 168 | + if (!is_null($additionalParams)) { |
|
| 169 | + $_ = array_merge($additionalParams, $this->vars); |
|
| 170 | + foreach ($_ as $var => $value) { |
|
| 171 | + if (!isset(${$var})) { |
|
| 172 | + ${$var} = $value; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - // Include |
|
| 178 | - ob_start(); |
|
| 179 | - try { |
|
| 180 | - include $file; |
|
| 181 | - $data = ob_get_contents(); |
|
| 182 | - } catch (\Exception $e) { |
|
| 183 | - @ob_end_clean(); |
|
| 184 | - throw $e; |
|
| 185 | - } |
|
| 186 | - @ob_end_clean(); |
|
| 177 | + // Include |
|
| 178 | + ob_start(); |
|
| 179 | + try { |
|
| 180 | + include $file; |
|
| 181 | + $data = ob_get_contents(); |
|
| 182 | + } catch (\Exception $e) { |
|
| 183 | + @ob_end_clean(); |
|
| 184 | + throw $e; |
|
| 185 | + } |
|
| 186 | + @ob_end_clean(); |
|
| 187 | 187 | |
| 188 | - // Return data |
|
| 189 | - return $data; |
|
| 190 | - } |
|
| 188 | + // Return data |
|
| 189 | + return $data; |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -65,8 +65,8 @@ |
||
| 65 | 65 | |
| 66 | 66 | $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
| 67 | 67 | $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
| 68 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
| 69 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
| 68 | + $this->issueDate = new \DateTime('@'.$info['validFrom_time_t'], $gmt); |
|
| 69 | + $this->expireDate = new \DateTime('@'.$info['validTo_time_t'], $gmt); |
|
| 70 | 70 | $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
| 71 | 71 | $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
| 72 | 72 | } |
@@ -31,104 +31,104 @@ |
||
| 31 | 31 | use OCP\ICertificate; |
| 32 | 32 | |
| 33 | 33 | class Certificate implements ICertificate { |
| 34 | - protected $name; |
|
| 35 | - |
|
| 36 | - protected $commonName; |
|
| 37 | - |
|
| 38 | - protected $organization; |
|
| 39 | - |
|
| 40 | - protected $serial; |
|
| 41 | - |
|
| 42 | - protected $issueDate; |
|
| 43 | - |
|
| 44 | - protected $expireDate; |
|
| 45 | - |
|
| 46 | - protected $issuerName; |
|
| 47 | - |
|
| 48 | - protected $issuerOrganization; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @param string $data base64 encoded certificate |
|
| 52 | - * @param string $name |
|
| 53 | - * @throws \Exception If the certificate could not get parsed |
|
| 54 | - */ |
|
| 55 | - public function __construct(string $data, string $name) { |
|
| 56 | - $this->name = $name; |
|
| 57 | - $gmt = new \DateTimeZone('GMT'); |
|
| 58 | - |
|
| 59 | - // If string starts with "file://" ignore the certificate |
|
| 60 | - $query = 'file://'; |
|
| 61 | - if (strtolower(substr($data, 0, strlen($query))) === $query) { |
|
| 62 | - throw new \Exception('Certificate could not get parsed.'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $info = openssl_x509_parse($data); |
|
| 66 | - if (!is_array($info)) { |
|
| 67 | - throw new \Exception('Certificate could not get parsed.'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
| 71 | - $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
| 72 | - $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
| 73 | - $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
| 74 | - $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
| 75 | - $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @return string |
|
| 80 | - */ |
|
| 81 | - public function getName(): string { |
|
| 82 | - return $this->name; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @return string|null |
|
| 87 | - */ |
|
| 88 | - public function getCommonName(): ?string { |
|
| 89 | - return $this->commonName; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return string|null |
|
| 94 | - */ |
|
| 95 | - public function getOrganization(): ?string { |
|
| 96 | - return $this->organization; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return \DateTime |
|
| 101 | - */ |
|
| 102 | - public function getIssueDate(): \DateTime { |
|
| 103 | - return $this->issueDate; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return \DateTime |
|
| 108 | - */ |
|
| 109 | - public function getExpireDate(): \DateTime { |
|
| 110 | - return $this->expireDate; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return bool |
|
| 115 | - */ |
|
| 116 | - public function isExpired(): bool { |
|
| 117 | - $now = new \DateTime(); |
|
| 118 | - return $this->issueDate > $now or $now > $this->expireDate; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @return string|null |
|
| 123 | - */ |
|
| 124 | - public function getIssuerName(): ?string { |
|
| 125 | - return $this->issuerName; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @return string|null |
|
| 130 | - */ |
|
| 131 | - public function getIssuerOrganization(): ?string { |
|
| 132 | - return $this->issuerOrganization; |
|
| 133 | - } |
|
| 34 | + protected $name; |
|
| 35 | + |
|
| 36 | + protected $commonName; |
|
| 37 | + |
|
| 38 | + protected $organization; |
|
| 39 | + |
|
| 40 | + protected $serial; |
|
| 41 | + |
|
| 42 | + protected $issueDate; |
|
| 43 | + |
|
| 44 | + protected $expireDate; |
|
| 45 | + |
|
| 46 | + protected $issuerName; |
|
| 47 | + |
|
| 48 | + protected $issuerOrganization; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @param string $data base64 encoded certificate |
|
| 52 | + * @param string $name |
|
| 53 | + * @throws \Exception If the certificate could not get parsed |
|
| 54 | + */ |
|
| 55 | + public function __construct(string $data, string $name) { |
|
| 56 | + $this->name = $name; |
|
| 57 | + $gmt = new \DateTimeZone('GMT'); |
|
| 58 | + |
|
| 59 | + // If string starts with "file://" ignore the certificate |
|
| 60 | + $query = 'file://'; |
|
| 61 | + if (strtolower(substr($data, 0, strlen($query))) === $query) { |
|
| 62 | + throw new \Exception('Certificate could not get parsed.'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $info = openssl_x509_parse($data); |
|
| 66 | + if (!is_array($info)) { |
|
| 67 | + throw new \Exception('Certificate could not get parsed.'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->commonName = isset($info['subject']['CN']) ? $info['subject']['CN'] : null; |
|
| 71 | + $this->organization = isset($info['subject']['O']) ? $info['subject']['O'] : null; |
|
| 72 | + $this->issueDate = new \DateTime('@' . $info['validFrom_time_t'], $gmt); |
|
| 73 | + $this->expireDate = new \DateTime('@' . $info['validTo_time_t'], $gmt); |
|
| 74 | + $this->issuerName = isset($info['issuer']['CN']) ? $info['issuer']['CN'] : null; |
|
| 75 | + $this->issuerOrganization = isset($info['issuer']['O']) ? $info['issuer']['O'] : null; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @return string |
|
| 80 | + */ |
|
| 81 | + public function getName(): string { |
|
| 82 | + return $this->name; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @return string|null |
|
| 87 | + */ |
|
| 88 | + public function getCommonName(): ?string { |
|
| 89 | + return $this->commonName; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return string|null |
|
| 94 | + */ |
|
| 95 | + public function getOrganization(): ?string { |
|
| 96 | + return $this->organization; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return \DateTime |
|
| 101 | + */ |
|
| 102 | + public function getIssueDate(): \DateTime { |
|
| 103 | + return $this->issueDate; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return \DateTime |
|
| 108 | + */ |
|
| 109 | + public function getExpireDate(): \DateTime { |
|
| 110 | + return $this->expireDate; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return bool |
|
| 115 | + */ |
|
| 116 | + public function isExpired(): bool { |
|
| 117 | + $now = new \DateTime(); |
|
| 118 | + return $this->issueDate > $now or $now > $this->expireDate; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @return string|null |
|
| 123 | + */ |
|
| 124 | + public function getIssuerName(): ?string { |
|
| 125 | + return $this->issuerName; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @return string|null |
|
| 130 | + */ |
|
| 131 | + public function getIssuerOrganization(): ?string { |
|
| 132 | + return $this->issuerOrganization; |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -89,7 +89,7 @@ |
||
| 89 | 89 | $user = $this->userManager->get($userId); |
| 90 | 90 | if ($user !== null) { |
| 91 | 91 | $key = $this->keyManager->getKey($user); |
| 92 | - return (bool)openssl_verify( |
|
| 92 | + return (bool) openssl_verify( |
|
| 93 | 93 | json_encode($data['message']), |
| 94 | 94 | base64_decode($data['signature']), |
| 95 | 95 | $key->getPublic(), |
@@ -32,76 +32,76 @@ |
||
| 32 | 32 | use OCP\IUserManager; |
| 33 | 33 | |
| 34 | 34 | class Signer { |
| 35 | - /** @var Manager */ |
|
| 36 | - private $keyManager; |
|
| 37 | - /** @var ITimeFactory */ |
|
| 38 | - private $timeFactory; |
|
| 39 | - /** @var IUserManager */ |
|
| 40 | - private $userManager; |
|
| 35 | + /** @var Manager */ |
|
| 36 | + private $keyManager; |
|
| 37 | + /** @var ITimeFactory */ |
|
| 38 | + private $timeFactory; |
|
| 39 | + /** @var IUserManager */ |
|
| 40 | + private $userManager; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param Manager $keyManager |
|
| 44 | - * @param ITimeFactory $timeFactory |
|
| 45 | - * @param IUserManager $userManager |
|
| 46 | - */ |
|
| 47 | - public function __construct(Manager $keyManager, |
|
| 48 | - ITimeFactory $timeFactory, |
|
| 49 | - IUserManager $userManager) { |
|
| 50 | - $this->keyManager = $keyManager; |
|
| 51 | - $this->timeFactory = $timeFactory; |
|
| 52 | - $this->userManager = $userManager; |
|
| 53 | - } |
|
| 42 | + /** |
|
| 43 | + * @param Manager $keyManager |
|
| 44 | + * @param ITimeFactory $timeFactory |
|
| 45 | + * @param IUserManager $userManager |
|
| 46 | + */ |
|
| 47 | + public function __construct(Manager $keyManager, |
|
| 48 | + ITimeFactory $timeFactory, |
|
| 49 | + IUserManager $userManager) { |
|
| 50 | + $this->keyManager = $keyManager; |
|
| 51 | + $this->timeFactory = $timeFactory; |
|
| 52 | + $this->userManager = $userManager; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Returns a signed blob for $data |
|
| 57 | - * |
|
| 58 | - * @param string $type |
|
| 59 | - * @param array $data |
|
| 60 | - * @param IUser $user |
|
| 61 | - * @return array ['message', 'signature'] |
|
| 62 | - */ |
|
| 63 | - public function sign(string $type, array $data, IUser $user): array { |
|
| 64 | - $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
| 65 | - $data = [ |
|
| 66 | - 'data' => $data, |
|
| 67 | - 'type' => $type, |
|
| 68 | - 'signer' => $user->getCloudId(), |
|
| 69 | - 'timestamp' => $this->timeFactory->getTime(), |
|
| 70 | - ]; |
|
| 71 | - openssl_sign(json_encode($data), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
| 55 | + /** |
|
| 56 | + * Returns a signed blob for $data |
|
| 57 | + * |
|
| 58 | + * @param string $type |
|
| 59 | + * @param array $data |
|
| 60 | + * @param IUser $user |
|
| 61 | + * @return array ['message', 'signature'] |
|
| 62 | + */ |
|
| 63 | + public function sign(string $type, array $data, IUser $user): array { |
|
| 64 | + $privateKey = $this->keyManager->getKey($user)->getPrivate(); |
|
| 65 | + $data = [ |
|
| 66 | + 'data' => $data, |
|
| 67 | + 'type' => $type, |
|
| 68 | + 'signer' => $user->getCloudId(), |
|
| 69 | + 'timestamp' => $this->timeFactory->getTime(), |
|
| 70 | + ]; |
|
| 71 | + openssl_sign(json_encode($data), $signature, $privateKey, OPENSSL_ALGO_SHA512); |
|
| 72 | 72 | |
| 73 | - return [ |
|
| 74 | - 'message' => $data, |
|
| 75 | - 'signature' => base64_encode($signature), |
|
| 76 | - ]; |
|
| 77 | - } |
|
| 73 | + return [ |
|
| 74 | + 'message' => $data, |
|
| 75 | + 'signature' => base64_encode($signature), |
|
| 76 | + ]; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Whether the data is signed properly |
|
| 81 | - * |
|
| 82 | - * @param array $data |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function verify(array $data): bool { |
|
| 86 | - if (isset($data['message']) |
|
| 87 | - && isset($data['signature']) |
|
| 88 | - && isset($data['message']['signer']) |
|
| 89 | - ) { |
|
| 90 | - $location = strrpos($data['message']['signer'], '@'); |
|
| 91 | - $userId = substr($data['message']['signer'], 0, $location); |
|
| 79 | + /** |
|
| 80 | + * Whether the data is signed properly |
|
| 81 | + * |
|
| 82 | + * @param array $data |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function verify(array $data): bool { |
|
| 86 | + if (isset($data['message']) |
|
| 87 | + && isset($data['signature']) |
|
| 88 | + && isset($data['message']['signer']) |
|
| 89 | + ) { |
|
| 90 | + $location = strrpos($data['message']['signer'], '@'); |
|
| 91 | + $userId = substr($data['message']['signer'], 0, $location); |
|
| 92 | 92 | |
| 93 | - $user = $this->userManager->get($userId); |
|
| 94 | - if ($user !== null) { |
|
| 95 | - $key = $this->keyManager->getKey($user); |
|
| 96 | - return (bool)openssl_verify( |
|
| 97 | - json_encode($data['message']), |
|
| 98 | - base64_decode($data['signature']), |
|
| 99 | - $key->getPublic(), |
|
| 100 | - OPENSSL_ALGO_SHA512 |
|
| 101 | - ); |
|
| 102 | - } |
|
| 103 | - } |
|
| 93 | + $user = $this->userManager->get($userId); |
|
| 94 | + if ($user !== null) { |
|
| 95 | + $key = $this->keyManager->getKey($user); |
|
| 96 | + return (bool)openssl_verify( |
|
| 97 | + json_encode($data['message']), |
|
| 98 | + base64_decode($data['signature']), |
|
| 99 | + $key->getPublic(), |
|
| 100 | + OPENSSL_ALGO_SHA512 |
|
| 101 | + ); |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - return false; |
|
| 106 | - } |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | 107 | } |
@@ -27,7 +27,7 @@ |
||
| 27 | 27 | use OCP\Encryption\Exceptions\GenericEncryptionException; |
| 28 | 28 | |
| 29 | 29 | class EncryptionHeaderToLargeException extends GenericEncryptionException { |
| 30 | - public function __construct() { |
|
| 31 | - parent::__construct('max header size exceeded'); |
|
| 32 | - } |
|
| 30 | + public function __construct() { |
|
| 31 | + parent::__construct('max header size exceeded'); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -73,7 +73,7 @@ |
||
| 73 | 73 | // always add owner to the list of users with access to the file |
| 74 | 74 | $userIds = [$owner]; |
| 75 | 75 | |
| 76 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
| 76 | + if (!$this->util->isFile($owner.'/'.$ownerPath)) { |
|
| 77 | 77 | return ['users' => $userIds, 'public' => false]; |
| 78 | 78 | } |
| 79 | 79 | |
@@ -35,99 +35,99 @@ |
||
| 35 | 35 | use OCP\Share\IManager; |
| 36 | 36 | |
| 37 | 37 | class File implements \OCP\Encryption\IFile { |
| 38 | - protected Util $util; |
|
| 39 | - private IRootFolder $rootFolder; |
|
| 40 | - private IManager $shareManager; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Cache results of already checked folders |
|
| 44 | - * @var CappedMemoryCache<array> |
|
| 45 | - */ |
|
| 46 | - protected CappedMemoryCache $cache; |
|
| 47 | - private ?IAppManager $appManager = null; |
|
| 48 | - |
|
| 49 | - public function __construct(Util $util, |
|
| 50 | - IRootFolder $rootFolder, |
|
| 51 | - IManager $shareManager) { |
|
| 52 | - $this->util = $util; |
|
| 53 | - $this->cache = new CappedMemoryCache(); |
|
| 54 | - $this->rootFolder = $rootFolder; |
|
| 55 | - $this->shareManager = $shareManager; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function getAppManager(): IAppManager { |
|
| 59 | - // Lazy evaluate app manager as it initialize the db too early otherwise |
|
| 60 | - if ($this->appManager) { |
|
| 61 | - return $this->appManager; |
|
| 62 | - } |
|
| 63 | - $this->appManager = \OCP\Server::get(IAppManager::class); |
|
| 64 | - return $this->appManager; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Get list of users with access to the file |
|
| 69 | - * |
|
| 70 | - * @param string $path to the file |
|
| 71 | - * @return array{users: string[], public: bool} |
|
| 72 | - */ |
|
| 73 | - public function getAccessList($path) { |
|
| 74 | - |
|
| 75 | - // Make sure that a share key is generated for the owner too |
|
| 76 | - [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
| 77 | - |
|
| 78 | - // always add owner to the list of users with access to the file |
|
| 79 | - $userIds = [$owner]; |
|
| 80 | - |
|
| 81 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
| 82 | - return ['users' => $userIds, 'public' => false]; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $ownerPath = substr($ownerPath, strlen('/files')); |
|
| 86 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 87 | - try { |
|
| 88 | - $file = $userFolder->get($ownerPath); |
|
| 89 | - } catch (NotFoundException $e) { |
|
| 90 | - $file = null; |
|
| 91 | - } |
|
| 92 | - $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
| 93 | - |
|
| 94 | - // first get the shares for the parent and cache the result so that we don't |
|
| 95 | - // need to check all parents for every file |
|
| 96 | - $parent = dirname($ownerPath); |
|
| 97 | - $parentNode = $userFolder->get($parent); |
|
| 98 | - if (isset($this->cache[$parent])) { |
|
| 99 | - $resultForParents = $this->cache[$parent]; |
|
| 100 | - } else { |
|
| 101 | - $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
| 102 | - $this->cache[$parent] = $resultForParents; |
|
| 103 | - } |
|
| 104 | - $userIds = array_merge($userIds, $resultForParents['users']); |
|
| 105 | - $public = $resultForParents['public'] || $resultForParents['remote']; |
|
| 106 | - |
|
| 107 | - |
|
| 108 | - // Find out who, if anyone, is sharing the file |
|
| 109 | - if ($file !== null) { |
|
| 110 | - $resultForFile = $this->shareManager->getAccessList($file, false); |
|
| 111 | - $userIds = array_merge($userIds, $resultForFile['users']); |
|
| 112 | - $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // check if it is a group mount |
|
| 116 | - if ($this->getAppManager()->isEnabledForUser("files_external")) { |
|
| 117 | - /** @var GlobalStoragesService $storageService */ |
|
| 118 | - $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
| 119 | - $storages = $storageService->getAllStorages(); |
|
| 120 | - foreach ($storages as $storage) { |
|
| 121 | - if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) { |
|
| 122 | - $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups()); |
|
| 123 | - $userIds = array_merge($userIds, $mountedFor); |
|
| 124 | - } |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Remove duplicate UIDs |
|
| 129 | - $uniqueUserIds = array_unique($userIds); |
|
| 130 | - |
|
| 131 | - return ['users' => $uniqueUserIds, 'public' => $public]; |
|
| 132 | - } |
|
| 38 | + protected Util $util; |
|
| 39 | + private IRootFolder $rootFolder; |
|
| 40 | + private IManager $shareManager; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Cache results of already checked folders |
|
| 44 | + * @var CappedMemoryCache<array> |
|
| 45 | + */ |
|
| 46 | + protected CappedMemoryCache $cache; |
|
| 47 | + private ?IAppManager $appManager = null; |
|
| 48 | + |
|
| 49 | + public function __construct(Util $util, |
|
| 50 | + IRootFolder $rootFolder, |
|
| 51 | + IManager $shareManager) { |
|
| 52 | + $this->util = $util; |
|
| 53 | + $this->cache = new CappedMemoryCache(); |
|
| 54 | + $this->rootFolder = $rootFolder; |
|
| 55 | + $this->shareManager = $shareManager; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function getAppManager(): IAppManager { |
|
| 59 | + // Lazy evaluate app manager as it initialize the db too early otherwise |
|
| 60 | + if ($this->appManager) { |
|
| 61 | + return $this->appManager; |
|
| 62 | + } |
|
| 63 | + $this->appManager = \OCP\Server::get(IAppManager::class); |
|
| 64 | + return $this->appManager; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Get list of users with access to the file |
|
| 69 | + * |
|
| 70 | + * @param string $path to the file |
|
| 71 | + * @return array{users: string[], public: bool} |
|
| 72 | + */ |
|
| 73 | + public function getAccessList($path) { |
|
| 74 | + |
|
| 75 | + // Make sure that a share key is generated for the owner too |
|
| 76 | + [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
| 77 | + |
|
| 78 | + // always add owner to the list of users with access to the file |
|
| 79 | + $userIds = [$owner]; |
|
| 80 | + |
|
| 81 | + if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
| 82 | + return ['users' => $userIds, 'public' => false]; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $ownerPath = substr($ownerPath, strlen('/files')); |
|
| 86 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 87 | + try { |
|
| 88 | + $file = $userFolder->get($ownerPath); |
|
| 89 | + } catch (NotFoundException $e) { |
|
| 90 | + $file = null; |
|
| 91 | + } |
|
| 92 | + $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
| 93 | + |
|
| 94 | + // first get the shares for the parent and cache the result so that we don't |
|
| 95 | + // need to check all parents for every file |
|
| 96 | + $parent = dirname($ownerPath); |
|
| 97 | + $parentNode = $userFolder->get($parent); |
|
| 98 | + if (isset($this->cache[$parent])) { |
|
| 99 | + $resultForParents = $this->cache[$parent]; |
|
| 100 | + } else { |
|
| 101 | + $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
| 102 | + $this->cache[$parent] = $resultForParents; |
|
| 103 | + } |
|
| 104 | + $userIds = array_merge($userIds, $resultForParents['users']); |
|
| 105 | + $public = $resultForParents['public'] || $resultForParents['remote']; |
|
| 106 | + |
|
| 107 | + |
|
| 108 | + // Find out who, if anyone, is sharing the file |
|
| 109 | + if ($file !== null) { |
|
| 110 | + $resultForFile = $this->shareManager->getAccessList($file, false); |
|
| 111 | + $userIds = array_merge($userIds, $resultForFile['users']); |
|
| 112 | + $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // check if it is a group mount |
|
| 116 | + if ($this->getAppManager()->isEnabledForUser("files_external")) { |
|
| 117 | + /** @var GlobalStoragesService $storageService */ |
|
| 118 | + $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
| 119 | + $storages = $storageService->getAllStorages(); |
|
| 120 | + foreach ($storages as $storage) { |
|
| 121 | + if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) { |
|
| 122 | + $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups()); |
|
| 123 | + $userIds = array_merge($userIds, $mountedFor); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Remove duplicate UIDs |
|
| 129 | + $uniqueUserIds = array_unique($userIds); |
|
| 130 | + |
|
| 131 | + return ['users' => $uniqueUserIds, 'public' => $public]; |
|
| 132 | + } |
|
| 133 | 133 | } |