@@ -27,148 +27,148 @@ |
||
27 | 27 | |
28 | 28 | class Action implements IAction { |
29 | 29 | |
30 | - /** @var string */ |
|
31 | - protected $label; |
|
32 | - |
|
33 | - /** @var string */ |
|
34 | - protected $labelParsed; |
|
35 | - |
|
36 | - /** @var string */ |
|
37 | - protected $link; |
|
38 | - |
|
39 | - /** @var string */ |
|
40 | - protected $requestType; |
|
41 | - |
|
42 | - /** @var string */ |
|
43 | - protected $icon; |
|
44 | - |
|
45 | - /** @var bool */ |
|
46 | - protected $primary; |
|
47 | - |
|
48 | - /** |
|
49 | - * Constructor |
|
50 | - */ |
|
51 | - public function __construct() { |
|
52 | - $this->label = ''; |
|
53 | - $this->labelParsed = ''; |
|
54 | - $this->link = ''; |
|
55 | - $this->requestType = ''; |
|
56 | - $this->primary = false; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param string $label |
|
61 | - * @return $this |
|
62 | - * @throws \InvalidArgumentException if the label is invalid |
|
63 | - * @since 8.2.0 |
|
64 | - */ |
|
65 | - public function setLabel($label) { |
|
66 | - if (!is_string($label) || $label === '' || isset($label[32])) { |
|
67 | - throw new \InvalidArgumentException('The given label is invalid'); |
|
68 | - } |
|
69 | - $this->label = $label; |
|
70 | - return $this; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @return string |
|
75 | - * @since 8.2.0 |
|
76 | - */ |
|
77 | - public function getLabel() { |
|
78 | - return $this->label; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @param string $label |
|
83 | - * @return $this |
|
84 | - * @throws \InvalidArgumentException if the label is invalid |
|
85 | - * @since 8.2.0 |
|
86 | - */ |
|
87 | - public function setParsedLabel($label) { |
|
88 | - if (!is_string($label) || $label === '') { |
|
89 | - throw new \InvalidArgumentException('The given parsed label is invalid'); |
|
90 | - } |
|
91 | - $this->labelParsed = $label; |
|
92 | - return $this; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @return string |
|
97 | - * @since 8.2.0 |
|
98 | - */ |
|
99 | - public function getParsedLabel() { |
|
100 | - return $this->labelParsed; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param $primary bool |
|
105 | - * @return $this |
|
106 | - * @throws \InvalidArgumentException if $primary is invalid |
|
107 | - * @since 9.0.0 |
|
108 | - */ |
|
109 | - public function setPrimary($primary) { |
|
110 | - if (!is_bool($primary)) { |
|
111 | - throw new \InvalidArgumentException('The given primary option is invalid'); |
|
112 | - } |
|
113 | - |
|
114 | - $this->primary = $primary; |
|
115 | - return $this; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return bool |
|
120 | - * @since 9.0.0 |
|
121 | - */ |
|
122 | - public function isPrimary() { |
|
123 | - return $this->primary; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $link |
|
128 | - * @param string $requestType |
|
129 | - * @return $this |
|
130 | - * @throws \InvalidArgumentException if the link is invalid |
|
131 | - * @since 8.2.0 |
|
132 | - */ |
|
133 | - public function setLink($link, $requestType) { |
|
134 | - if (!is_string($link) || $link === '' || isset($link[256])) { |
|
135 | - throw new \InvalidArgumentException('The given link is invalid'); |
|
136 | - } |
|
137 | - if (!in_array($requestType, ['GET', 'POST', 'PUT', 'DELETE'], true)) { |
|
138 | - throw new \InvalidArgumentException('The given request type is invalid'); |
|
139 | - } |
|
140 | - $this->link = $link; |
|
141 | - $this->requestType = $requestType; |
|
142 | - return $this; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return string |
|
147 | - * @since 8.2.0 |
|
148 | - */ |
|
149 | - public function getLink() { |
|
150 | - return $this->link; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @return string |
|
155 | - * @since 8.2.0 |
|
156 | - */ |
|
157 | - public function getRequestType() { |
|
158 | - return $this->requestType; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @return bool |
|
163 | - */ |
|
164 | - public function isValid() { |
|
165 | - return $this->label !== '' && $this->link !== ''; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @return bool |
|
170 | - */ |
|
171 | - public function isValidParsed() { |
|
172 | - return $this->labelParsed !== '' && $this->link !== ''; |
|
173 | - } |
|
30 | + /** @var string */ |
|
31 | + protected $label; |
|
32 | + |
|
33 | + /** @var string */ |
|
34 | + protected $labelParsed; |
|
35 | + |
|
36 | + /** @var string */ |
|
37 | + protected $link; |
|
38 | + |
|
39 | + /** @var string */ |
|
40 | + protected $requestType; |
|
41 | + |
|
42 | + /** @var string */ |
|
43 | + protected $icon; |
|
44 | + |
|
45 | + /** @var bool */ |
|
46 | + protected $primary; |
|
47 | + |
|
48 | + /** |
|
49 | + * Constructor |
|
50 | + */ |
|
51 | + public function __construct() { |
|
52 | + $this->label = ''; |
|
53 | + $this->labelParsed = ''; |
|
54 | + $this->link = ''; |
|
55 | + $this->requestType = ''; |
|
56 | + $this->primary = false; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param string $label |
|
61 | + * @return $this |
|
62 | + * @throws \InvalidArgumentException if the label is invalid |
|
63 | + * @since 8.2.0 |
|
64 | + */ |
|
65 | + public function setLabel($label) { |
|
66 | + if (!is_string($label) || $label === '' || isset($label[32])) { |
|
67 | + throw new \InvalidArgumentException('The given label is invalid'); |
|
68 | + } |
|
69 | + $this->label = $label; |
|
70 | + return $this; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @return string |
|
75 | + * @since 8.2.0 |
|
76 | + */ |
|
77 | + public function getLabel() { |
|
78 | + return $this->label; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @param string $label |
|
83 | + * @return $this |
|
84 | + * @throws \InvalidArgumentException if the label is invalid |
|
85 | + * @since 8.2.0 |
|
86 | + */ |
|
87 | + public function setParsedLabel($label) { |
|
88 | + if (!is_string($label) || $label === '') { |
|
89 | + throw new \InvalidArgumentException('The given parsed label is invalid'); |
|
90 | + } |
|
91 | + $this->labelParsed = $label; |
|
92 | + return $this; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @return string |
|
97 | + * @since 8.2.0 |
|
98 | + */ |
|
99 | + public function getParsedLabel() { |
|
100 | + return $this->labelParsed; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param $primary bool |
|
105 | + * @return $this |
|
106 | + * @throws \InvalidArgumentException if $primary is invalid |
|
107 | + * @since 9.0.0 |
|
108 | + */ |
|
109 | + public function setPrimary($primary) { |
|
110 | + if (!is_bool($primary)) { |
|
111 | + throw new \InvalidArgumentException('The given primary option is invalid'); |
|
112 | + } |
|
113 | + |
|
114 | + $this->primary = $primary; |
|
115 | + return $this; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return bool |
|
120 | + * @since 9.0.0 |
|
121 | + */ |
|
122 | + public function isPrimary() { |
|
123 | + return $this->primary; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $link |
|
128 | + * @param string $requestType |
|
129 | + * @return $this |
|
130 | + * @throws \InvalidArgumentException if the link is invalid |
|
131 | + * @since 8.2.0 |
|
132 | + */ |
|
133 | + public function setLink($link, $requestType) { |
|
134 | + if (!is_string($link) || $link === '' || isset($link[256])) { |
|
135 | + throw new \InvalidArgumentException('The given link is invalid'); |
|
136 | + } |
|
137 | + if (!in_array($requestType, ['GET', 'POST', 'PUT', 'DELETE'], true)) { |
|
138 | + throw new \InvalidArgumentException('The given request type is invalid'); |
|
139 | + } |
|
140 | + $this->link = $link; |
|
141 | + $this->requestType = $requestType; |
|
142 | + return $this; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return string |
|
147 | + * @since 8.2.0 |
|
148 | + */ |
|
149 | + public function getLink() { |
|
150 | + return $this->link; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @return string |
|
155 | + * @since 8.2.0 |
|
156 | + */ |
|
157 | + public function getRequestType() { |
|
158 | + return $this->requestType; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @return bool |
|
163 | + */ |
|
164 | + public function isValid() { |
|
165 | + return $this->label !== '' && $this->link !== ''; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @return bool |
|
170 | + */ |
|
171 | + public function isValidParsed() { |
|
172 | + return $this->labelParsed !== '' && $this->link !== ''; |
|
173 | + } |
|
174 | 174 | } |
@@ -31,217 +31,217 @@ |
||
31 | 31 | use OCP\RichObjectStrings\IValidator; |
32 | 32 | |
33 | 33 | class Manager implements IManager { |
34 | - /** @var IValidator */ |
|
35 | - protected $validator; |
|
36 | - |
|
37 | - /** @var IApp[] */ |
|
38 | - protected $apps; |
|
39 | - |
|
40 | - /** @var INotifier[] */ |
|
41 | - protected $notifiers; |
|
42 | - |
|
43 | - /** @var array[] */ |
|
44 | - protected $notifiersInfo; |
|
45 | - |
|
46 | - /** @var \Closure[] */ |
|
47 | - protected $appsClosures; |
|
48 | - |
|
49 | - /** @var \Closure[] */ |
|
50 | - protected $notifiersClosures; |
|
51 | - |
|
52 | - /** @var \Closure[] */ |
|
53 | - protected $notifiersInfoClosures; |
|
54 | - |
|
55 | - /** |
|
56 | - * Manager constructor. |
|
57 | - * |
|
58 | - * @param IValidator $validator |
|
59 | - */ |
|
60 | - public function __construct(IValidator $validator) { |
|
61 | - $this->validator = $validator; |
|
62 | - $this->apps = []; |
|
63 | - $this->notifiers = []; |
|
64 | - $this->notifiersInfo = []; |
|
65 | - $this->appsClosures = []; |
|
66 | - $this->notifiersClosures = []; |
|
67 | - $this->notifiersInfoClosures = []; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @param \Closure $service The service must implement IApp, otherwise a |
|
72 | - * \InvalidArgumentException is thrown later |
|
73 | - * @since 8.2.0 |
|
74 | - */ |
|
75 | - public function registerApp(\Closure $service) { |
|
76 | - $this->appsClosures[] = $service; |
|
77 | - $this->apps = []; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param \Closure $service The service must implement INotifier, otherwise a |
|
82 | - * \InvalidArgumentException is thrown later |
|
83 | - * @param \Closure $info An array with the keys 'id' and 'name' containing |
|
84 | - * the app id and the app name |
|
85 | - * @since 8.2.0 - Parameter $info was added in 9.0.0 |
|
86 | - */ |
|
87 | - public function registerNotifier(\Closure $service, \Closure $info) { |
|
88 | - $this->notifiersClosures[] = $service; |
|
89 | - $this->notifiersInfoClosures[] = $info; |
|
90 | - $this->notifiers = []; |
|
91 | - $this->notifiersInfo = []; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * @return IApp[] |
|
96 | - */ |
|
97 | - protected function getApps() { |
|
98 | - if (!empty($this->apps)) { |
|
99 | - return $this->apps; |
|
100 | - } |
|
101 | - |
|
102 | - $this->apps = []; |
|
103 | - foreach ($this->appsClosures as $closure) { |
|
104 | - $app = $closure(); |
|
105 | - if (!($app instanceof IApp)) { |
|
106 | - throw new \InvalidArgumentException('The given notification app does not implement the IApp interface'); |
|
107 | - } |
|
108 | - $this->apps[] = $app; |
|
109 | - } |
|
110 | - |
|
111 | - return $this->apps; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * @return INotifier[] |
|
116 | - */ |
|
117 | - protected function getNotifiers() { |
|
118 | - if (!empty($this->notifiers)) { |
|
119 | - return $this->notifiers; |
|
120 | - } |
|
121 | - |
|
122 | - $this->notifiers = []; |
|
123 | - foreach ($this->notifiersClosures as $closure) { |
|
124 | - $notifier = $closure(); |
|
125 | - if (!($notifier instanceof INotifier)) { |
|
126 | - throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface'); |
|
127 | - } |
|
128 | - $this->notifiers[] = $notifier; |
|
129 | - } |
|
130 | - |
|
131 | - return $this->notifiers; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @return array[] |
|
136 | - */ |
|
137 | - public function listNotifiers() { |
|
138 | - if (!empty($this->notifiersInfo)) { |
|
139 | - return $this->notifiersInfo; |
|
140 | - } |
|
141 | - |
|
142 | - $this->notifiersInfo = []; |
|
143 | - foreach ($this->notifiersInfoClosures as $closure) { |
|
144 | - $notifier = $closure(); |
|
145 | - if (!is_array($notifier) || sizeof($notifier) !== 2 || !isset($notifier['id']) || !isset($notifier['name'])) { |
|
146 | - throw new \InvalidArgumentException('The given notifier information is invalid'); |
|
147 | - } |
|
148 | - if (isset($this->notifiersInfo[$notifier['id']])) { |
|
149 | - throw new \InvalidArgumentException('The given notifier ID ' . $notifier['id'] . ' is already in use'); |
|
150 | - } |
|
151 | - $this->notifiersInfo[$notifier['id']] = $notifier['name']; |
|
152 | - } |
|
153 | - |
|
154 | - return $this->notifiersInfo; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @return INotification |
|
159 | - * @since 8.2.0 |
|
160 | - */ |
|
161 | - public function createNotification() { |
|
162 | - return new Notification($this->validator); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @return bool |
|
167 | - * @since 8.2.0 |
|
168 | - */ |
|
169 | - public function hasNotifiers() { |
|
170 | - return !empty($this->notifiersClosures); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param INotification $notification |
|
175 | - * @throws \InvalidArgumentException When the notification is not valid |
|
176 | - * @since 8.2.0 |
|
177 | - */ |
|
178 | - public function notify(INotification $notification) { |
|
179 | - if (!$notification->isValid()) { |
|
180 | - throw new \InvalidArgumentException('The given notification is invalid'); |
|
181 | - } |
|
182 | - |
|
183 | - $apps = $this->getApps(); |
|
184 | - |
|
185 | - foreach ($apps as $app) { |
|
186 | - try { |
|
187 | - $app->notify($notification); |
|
188 | - } catch (\InvalidArgumentException $e) { |
|
189 | - } |
|
190 | - } |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * @param INotification $notification |
|
195 | - * @param string $languageCode The code of the language that should be used to prepare the notification |
|
196 | - * @return INotification |
|
197 | - * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
198 | - * @since 8.2.0 |
|
199 | - */ |
|
200 | - public function prepare(INotification $notification, $languageCode) { |
|
201 | - $notifiers = $this->getNotifiers(); |
|
202 | - |
|
203 | - foreach ($notifiers as $notifier) { |
|
204 | - try { |
|
205 | - $notification = $notifier->prepare($notification, $languageCode); |
|
206 | - } catch (\InvalidArgumentException $e) { |
|
207 | - continue; |
|
208 | - } |
|
209 | - |
|
210 | - if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
211 | - throw new \InvalidArgumentException('The given notification has not been handled'); |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
216 | - throw new \InvalidArgumentException('The given notification has not been handled'); |
|
217 | - } |
|
218 | - |
|
219 | - return $notification; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * @param INotification $notification |
|
224 | - */ |
|
225 | - public function markProcessed(INotification $notification) { |
|
226 | - $apps = $this->getApps(); |
|
227 | - |
|
228 | - foreach ($apps as $app) { |
|
229 | - $app->markProcessed($notification); |
|
230 | - } |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * @param INotification $notification |
|
235 | - * @return int |
|
236 | - */ |
|
237 | - public function getCount(INotification $notification) { |
|
238 | - $apps = $this->getApps(); |
|
239 | - |
|
240 | - $count = 0; |
|
241 | - foreach ($apps as $app) { |
|
242 | - $count += $app->getCount($notification); |
|
243 | - } |
|
244 | - |
|
245 | - return $count; |
|
246 | - } |
|
34 | + /** @var IValidator */ |
|
35 | + protected $validator; |
|
36 | + |
|
37 | + /** @var IApp[] */ |
|
38 | + protected $apps; |
|
39 | + |
|
40 | + /** @var INotifier[] */ |
|
41 | + protected $notifiers; |
|
42 | + |
|
43 | + /** @var array[] */ |
|
44 | + protected $notifiersInfo; |
|
45 | + |
|
46 | + /** @var \Closure[] */ |
|
47 | + protected $appsClosures; |
|
48 | + |
|
49 | + /** @var \Closure[] */ |
|
50 | + protected $notifiersClosures; |
|
51 | + |
|
52 | + /** @var \Closure[] */ |
|
53 | + protected $notifiersInfoClosures; |
|
54 | + |
|
55 | + /** |
|
56 | + * Manager constructor. |
|
57 | + * |
|
58 | + * @param IValidator $validator |
|
59 | + */ |
|
60 | + public function __construct(IValidator $validator) { |
|
61 | + $this->validator = $validator; |
|
62 | + $this->apps = []; |
|
63 | + $this->notifiers = []; |
|
64 | + $this->notifiersInfo = []; |
|
65 | + $this->appsClosures = []; |
|
66 | + $this->notifiersClosures = []; |
|
67 | + $this->notifiersInfoClosures = []; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @param \Closure $service The service must implement IApp, otherwise a |
|
72 | + * \InvalidArgumentException is thrown later |
|
73 | + * @since 8.2.0 |
|
74 | + */ |
|
75 | + public function registerApp(\Closure $service) { |
|
76 | + $this->appsClosures[] = $service; |
|
77 | + $this->apps = []; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param \Closure $service The service must implement INotifier, otherwise a |
|
82 | + * \InvalidArgumentException is thrown later |
|
83 | + * @param \Closure $info An array with the keys 'id' and 'name' containing |
|
84 | + * the app id and the app name |
|
85 | + * @since 8.2.0 - Parameter $info was added in 9.0.0 |
|
86 | + */ |
|
87 | + public function registerNotifier(\Closure $service, \Closure $info) { |
|
88 | + $this->notifiersClosures[] = $service; |
|
89 | + $this->notifiersInfoClosures[] = $info; |
|
90 | + $this->notifiers = []; |
|
91 | + $this->notifiersInfo = []; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * @return IApp[] |
|
96 | + */ |
|
97 | + protected function getApps() { |
|
98 | + if (!empty($this->apps)) { |
|
99 | + return $this->apps; |
|
100 | + } |
|
101 | + |
|
102 | + $this->apps = []; |
|
103 | + foreach ($this->appsClosures as $closure) { |
|
104 | + $app = $closure(); |
|
105 | + if (!($app instanceof IApp)) { |
|
106 | + throw new \InvalidArgumentException('The given notification app does not implement the IApp interface'); |
|
107 | + } |
|
108 | + $this->apps[] = $app; |
|
109 | + } |
|
110 | + |
|
111 | + return $this->apps; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * @return INotifier[] |
|
116 | + */ |
|
117 | + protected function getNotifiers() { |
|
118 | + if (!empty($this->notifiers)) { |
|
119 | + return $this->notifiers; |
|
120 | + } |
|
121 | + |
|
122 | + $this->notifiers = []; |
|
123 | + foreach ($this->notifiersClosures as $closure) { |
|
124 | + $notifier = $closure(); |
|
125 | + if (!($notifier instanceof INotifier)) { |
|
126 | + throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface'); |
|
127 | + } |
|
128 | + $this->notifiers[] = $notifier; |
|
129 | + } |
|
130 | + |
|
131 | + return $this->notifiers; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @return array[] |
|
136 | + */ |
|
137 | + public function listNotifiers() { |
|
138 | + if (!empty($this->notifiersInfo)) { |
|
139 | + return $this->notifiersInfo; |
|
140 | + } |
|
141 | + |
|
142 | + $this->notifiersInfo = []; |
|
143 | + foreach ($this->notifiersInfoClosures as $closure) { |
|
144 | + $notifier = $closure(); |
|
145 | + if (!is_array($notifier) || sizeof($notifier) !== 2 || !isset($notifier['id']) || !isset($notifier['name'])) { |
|
146 | + throw new \InvalidArgumentException('The given notifier information is invalid'); |
|
147 | + } |
|
148 | + if (isset($this->notifiersInfo[$notifier['id']])) { |
|
149 | + throw new \InvalidArgumentException('The given notifier ID ' . $notifier['id'] . ' is already in use'); |
|
150 | + } |
|
151 | + $this->notifiersInfo[$notifier['id']] = $notifier['name']; |
|
152 | + } |
|
153 | + |
|
154 | + return $this->notifiersInfo; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @return INotification |
|
159 | + * @since 8.2.0 |
|
160 | + */ |
|
161 | + public function createNotification() { |
|
162 | + return new Notification($this->validator); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @return bool |
|
167 | + * @since 8.2.0 |
|
168 | + */ |
|
169 | + public function hasNotifiers() { |
|
170 | + return !empty($this->notifiersClosures); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param INotification $notification |
|
175 | + * @throws \InvalidArgumentException When the notification is not valid |
|
176 | + * @since 8.2.0 |
|
177 | + */ |
|
178 | + public function notify(INotification $notification) { |
|
179 | + if (!$notification->isValid()) { |
|
180 | + throw new \InvalidArgumentException('The given notification is invalid'); |
|
181 | + } |
|
182 | + |
|
183 | + $apps = $this->getApps(); |
|
184 | + |
|
185 | + foreach ($apps as $app) { |
|
186 | + try { |
|
187 | + $app->notify($notification); |
|
188 | + } catch (\InvalidArgumentException $e) { |
|
189 | + } |
|
190 | + } |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * @param INotification $notification |
|
195 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
|
196 | + * @return INotification |
|
197 | + * @throws \InvalidArgumentException When the notification was not prepared by a notifier |
|
198 | + * @since 8.2.0 |
|
199 | + */ |
|
200 | + public function prepare(INotification $notification, $languageCode) { |
|
201 | + $notifiers = $this->getNotifiers(); |
|
202 | + |
|
203 | + foreach ($notifiers as $notifier) { |
|
204 | + try { |
|
205 | + $notification = $notifier->prepare($notification, $languageCode); |
|
206 | + } catch (\InvalidArgumentException $e) { |
|
207 | + continue; |
|
208 | + } |
|
209 | + |
|
210 | + if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
211 | + throw new \InvalidArgumentException('The given notification has not been handled'); |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + if (!($notification instanceof INotification) || !$notification->isValidParsed()) { |
|
216 | + throw new \InvalidArgumentException('The given notification has not been handled'); |
|
217 | + } |
|
218 | + |
|
219 | + return $notification; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * @param INotification $notification |
|
224 | + */ |
|
225 | + public function markProcessed(INotification $notification) { |
|
226 | + $apps = $this->getApps(); |
|
227 | + |
|
228 | + foreach ($apps as $app) { |
|
229 | + $app->markProcessed($notification); |
|
230 | + } |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * @param INotification $notification |
|
235 | + * @return int |
|
236 | + */ |
|
237 | + public function getCount(INotification $notification) { |
|
238 | + $apps = $this->getApps(); |
|
239 | + |
|
240 | + $count = 0; |
|
241 | + foreach ($apps as $app) { |
|
242 | + $count += $app->getCount($notification); |
|
243 | + } |
|
244 | + |
|
245 | + return $count; |
|
246 | + } |
|
247 | 247 | } |
@@ -30,32 +30,32 @@ |
||
30 | 30 | * @package OC\Integritycheck\Iterator |
31 | 31 | */ |
32 | 32 | class ExcludeFileByNameFilterIterator extends \RecursiveFilterIterator { |
33 | - /** |
|
34 | - * Array of excluded file names. Those are not scanned by the integrity checker. |
|
35 | - * This is used to exclude files which administrators could upload by mistakes |
|
36 | - * such as .DS_Store files. |
|
37 | - * |
|
38 | - * @var array |
|
39 | - */ |
|
40 | - private $excludedFilenames = [ |
|
41 | - '.DS_Store', // Mac OS X |
|
42 | - 'Thumbs.db', // Microsoft Windows |
|
43 | - '.directory', // Dolphin (KDE) |
|
44 | - '.webapp', // Gentoo/Funtoo & derivatives use a tool known as webapp-config to manager wep-apps. |
|
45 | - ]; |
|
33 | + /** |
|
34 | + * Array of excluded file names. Those are not scanned by the integrity checker. |
|
35 | + * This is used to exclude files which administrators could upload by mistakes |
|
36 | + * such as .DS_Store files. |
|
37 | + * |
|
38 | + * @var array |
|
39 | + */ |
|
40 | + private $excludedFilenames = [ |
|
41 | + '.DS_Store', // Mac OS X |
|
42 | + 'Thumbs.db', // Microsoft Windows |
|
43 | + '.directory', // Dolphin (KDE) |
|
44 | + '.webapp', // Gentoo/Funtoo & derivatives use a tool known as webapp-config to manager wep-apps. |
|
45 | + ]; |
|
46 | 46 | |
47 | - /** |
|
48 | - * @return bool |
|
49 | - */ |
|
50 | - public function accept() { |
|
51 | - if($this->isDir()) { |
|
52 | - return true; |
|
53 | - } |
|
47 | + /** |
|
48 | + * @return bool |
|
49 | + */ |
|
50 | + public function accept() { |
|
51 | + if($this->isDir()) { |
|
52 | + return true; |
|
53 | + } |
|
54 | 54 | |
55 | - return !in_array( |
|
56 | - $this->current()->getFilename(), |
|
57 | - $this->excludedFilenames, |
|
58 | - true |
|
59 | - ); |
|
60 | - } |
|
55 | + return !in_array( |
|
56 | + $this->current()->getFilename(), |
|
57 | + $this->excludedFilenames, |
|
58 | + true |
|
59 | + ); |
|
60 | + } |
|
61 | 61 | } |
@@ -24,45 +24,45 @@ |
||
24 | 24 | namespace OC\IntegrityCheck\Iterator; |
25 | 25 | |
26 | 26 | class ExcludeFoldersByPathFilterIterator extends \RecursiveFilterIterator { |
27 | - private $excludedFolders = []; |
|
27 | + private $excludedFolders = []; |
|
28 | 28 | |
29 | - public function __construct(\RecursiveIterator $iterator, $root = '') { |
|
30 | - parent::__construct($iterator); |
|
29 | + public function __construct(\RecursiveIterator $iterator, $root = '') { |
|
30 | + parent::__construct($iterator); |
|
31 | 31 | |
32 | - $appFolders = \OC::$APPSROOTS; |
|
33 | - foreach($appFolders as $key => $appFolder) { |
|
34 | - $appFolders[$key] = rtrim($appFolder['path'], '/'); |
|
35 | - } |
|
32 | + $appFolders = \OC::$APPSROOTS; |
|
33 | + foreach($appFolders as $key => $appFolder) { |
|
34 | + $appFolders[$key] = rtrim($appFolder['path'], '/'); |
|
35 | + } |
|
36 | 36 | |
37 | - $excludedFolders = [ |
|
38 | - rtrim($root . '/data', '/'), |
|
39 | - rtrim($root . '/themes', '/'), |
|
40 | - rtrim($root . '/config', '/'), |
|
41 | - rtrim($root . '/apps', '/'), |
|
42 | - rtrim($root . '/assets', '/'), |
|
43 | - rtrim($root . '/lost+found', '/'), |
|
44 | - // Ignore folders generated by updater since the updater is replaced |
|
45 | - // after the integrity check is run. |
|
46 | - // See https://github.com/owncloud/updater/issues/318#issuecomment-212497846 |
|
47 | - rtrim($root . '/updater', '/'), |
|
48 | - rtrim($root . '/_oc_upgrade', '/'), |
|
49 | - ]; |
|
50 | - $customDataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', ''); |
|
51 | - if($customDataDir !== '') { |
|
52 | - $excludedFolders[] = rtrim($customDataDir, '/'); |
|
53 | - } |
|
37 | + $excludedFolders = [ |
|
38 | + rtrim($root . '/data', '/'), |
|
39 | + rtrim($root . '/themes', '/'), |
|
40 | + rtrim($root . '/config', '/'), |
|
41 | + rtrim($root . '/apps', '/'), |
|
42 | + rtrim($root . '/assets', '/'), |
|
43 | + rtrim($root . '/lost+found', '/'), |
|
44 | + // Ignore folders generated by updater since the updater is replaced |
|
45 | + // after the integrity check is run. |
|
46 | + // See https://github.com/owncloud/updater/issues/318#issuecomment-212497846 |
|
47 | + rtrim($root . '/updater', '/'), |
|
48 | + rtrim($root . '/_oc_upgrade', '/'), |
|
49 | + ]; |
|
50 | + $customDataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', ''); |
|
51 | + if($customDataDir !== '') { |
|
52 | + $excludedFolders[] = rtrim($customDataDir, '/'); |
|
53 | + } |
|
54 | 54 | |
55 | - $this->excludedFolders = array_merge($excludedFolders, $appFolders); |
|
56 | - } |
|
55 | + $this->excludedFolders = array_merge($excludedFolders, $appFolders); |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * @return bool |
|
60 | - */ |
|
61 | - public function accept() { |
|
62 | - return !in_array( |
|
63 | - $this->current()->getPathName(), |
|
64 | - $this->excludedFolders, |
|
65 | - true |
|
66 | - ); |
|
67 | - } |
|
58 | + /** |
|
59 | + * @return bool |
|
60 | + */ |
|
61 | + public function accept() { |
|
62 | + return !in_array( |
|
63 | + $this->current()->getPathName(), |
|
64 | + $this->excludedFolders, |
|
65 | + true |
|
66 | + ); |
|
67 | + } |
|
68 | 68 | } |
@@ -29,21 +29,21 @@ |
||
29 | 29 | * @package OC\IntegrityCheck\Helpers |
30 | 30 | */ |
31 | 31 | class EnvironmentHelper { |
32 | - /** |
|
33 | - * Provides \OC::$SERVERROOT |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function getServerRoot() { |
|
38 | - return rtrim(\OC::$SERVERROOT, '/'); |
|
39 | - } |
|
32 | + /** |
|
33 | + * Provides \OC::$SERVERROOT |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function getServerRoot() { |
|
38 | + return rtrim(\OC::$SERVERROOT, '/'); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Provides \OC_Util::getChannel() |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function getChannel() { |
|
47 | - return \OC_Util::getChannel(); |
|
48 | - } |
|
41 | + /** |
|
42 | + * Provides \OC_Util::getChannel() |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function getChannel() { |
|
47 | + return \OC_Util::getChannel(); |
|
48 | + } |
|
49 | 49 | } |
@@ -29,57 +29,57 @@ |
||
29 | 29 | * @package OC\IntegrityCheck\Helpers |
30 | 30 | */ |
31 | 31 | class FileAccessHelper { |
32 | - /** |
|
33 | - * Wrapper around file_get_contents($filename, $data) |
|
34 | - * |
|
35 | - * @param string $filename |
|
36 | - * @return string|false |
|
37 | - */ |
|
38 | - public function file_get_contents($filename) { |
|
39 | - return file_get_contents($filename); |
|
40 | - } |
|
32 | + /** |
|
33 | + * Wrapper around file_get_contents($filename, $data) |
|
34 | + * |
|
35 | + * @param string $filename |
|
36 | + * @return string|false |
|
37 | + */ |
|
38 | + public function file_get_contents($filename) { |
|
39 | + return file_get_contents($filename); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Wrapper around file_exists($filename) |
|
44 | - * |
|
45 | - * @param string $filename |
|
46 | - * @return bool |
|
47 | - */ |
|
48 | - public function file_exists($filename) { |
|
49 | - return file_exists($filename); |
|
50 | - } |
|
42 | + /** |
|
43 | + * Wrapper around file_exists($filename) |
|
44 | + * |
|
45 | + * @param string $filename |
|
46 | + * @return bool |
|
47 | + */ |
|
48 | + public function file_exists($filename) { |
|
49 | + return file_exists($filename); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Wrapper around file_put_contents($filename, $data) |
|
54 | - * |
|
55 | - * @param string $filename |
|
56 | - * @param string $data |
|
57 | - * @return int |
|
58 | - * @throws \Exception |
|
59 | - */ |
|
60 | - public function file_put_contents($filename, $data) { |
|
61 | - $bytesWritten = @file_put_contents($filename, $data); |
|
62 | - if ($bytesWritten === false || $bytesWritten !== strlen($data)){ |
|
63 | - throw new \Exception('Failed to write into ' . $filename); |
|
64 | - } |
|
65 | - return $bytesWritten; |
|
66 | - } |
|
52 | + /** |
|
53 | + * Wrapper around file_put_contents($filename, $data) |
|
54 | + * |
|
55 | + * @param string $filename |
|
56 | + * @param string $data |
|
57 | + * @return int |
|
58 | + * @throws \Exception |
|
59 | + */ |
|
60 | + public function file_put_contents($filename, $data) { |
|
61 | + $bytesWritten = @file_put_contents($filename, $data); |
|
62 | + if ($bytesWritten === false || $bytesWritten !== strlen($data)){ |
|
63 | + throw new \Exception('Failed to write into ' . $filename); |
|
64 | + } |
|
65 | + return $bytesWritten; |
|
66 | + } |
|
67 | 67 | |
68 | - /** |
|
69 | - * @param string $path |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - public function is_writable($path) { |
|
73 | - return is_writable($path); |
|
74 | - } |
|
68 | + /** |
|
69 | + * @param string $path |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + public function is_writable($path) { |
|
73 | + return is_writable($path); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param string $path |
|
78 | - * @throws \Exception |
|
79 | - */ |
|
80 | - public function assertDirectoryExists($path) { |
|
81 | - if (!is_dir($path)) { |
|
82 | - throw new \Exception('Directory ' . $path . ' does not exist.'); |
|
83 | - } |
|
84 | - } |
|
76 | + /** |
|
77 | + * @param string $path |
|
78 | + * @throws \Exception |
|
79 | + */ |
|
80 | + public function assertDirectoryExists($path) { |
|
81 | + if (!is_dir($path)) { |
|
82 | + throw new \Exception('Directory ' . $path . ' does not exist.'); |
|
83 | + } |
|
84 | + } |
|
85 | 85 | } |
@@ -30,28 +30,28 @@ |
||
30 | 30 | * @package OC\IntegrityCheck\Helpers |
31 | 31 | */ |
32 | 32 | class AppLocator { |
33 | - /** |
|
34 | - * Provides \OC_App::getAppPath($appId) |
|
35 | - * |
|
36 | - * @param string $appId |
|
37 | - * @return string |
|
38 | - * @throws \Exception If the app cannot be found |
|
39 | - */ |
|
40 | - public function getAppPath($appId) { |
|
41 | - $path = \OC_App::getAppPath($appId); |
|
42 | - if($path === false) { |
|
33 | + /** |
|
34 | + * Provides \OC_App::getAppPath($appId) |
|
35 | + * |
|
36 | + * @param string $appId |
|
37 | + * @return string |
|
38 | + * @throws \Exception If the app cannot be found |
|
39 | + */ |
|
40 | + public function getAppPath($appId) { |
|
41 | + $path = \OC_App::getAppPath($appId); |
|
42 | + if($path === false) { |
|
43 | 43 | |
44 | - throw new \Exception('App not found'); |
|
45 | - } |
|
46 | - return $path; |
|
47 | - } |
|
44 | + throw new \Exception('App not found'); |
|
45 | + } |
|
46 | + return $path; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Providers \OC_App::getAllApps() |
|
51 | - * |
|
52 | - * @return array |
|
53 | - */ |
|
54 | - public function getAllApps() { |
|
55 | - return \OC_App::getAllApps(); |
|
56 | - } |
|
49 | + /** |
|
50 | + * Providers \OC_App::getAllApps() |
|
51 | + * |
|
52 | + * @return array |
|
53 | + */ |
|
54 | + public function getAllApps() { |
|
55 | + return \OC_App::getAllApps(); |
|
56 | + } |
|
57 | 57 | } |
@@ -35,46 +35,46 @@ |
||
35 | 35 | */ |
36 | 36 | class HintException extends \Exception { |
37 | 37 | |
38 | - private $hint; |
|
38 | + private $hint; |
|
39 | 39 | |
40 | - /** |
|
41 | - * HintException constructor. |
|
42 | - * |
|
43 | - * @param string $message The error message. It will be not revealed to the |
|
44 | - * the user (unless the hint is empty) and thus |
|
45 | - * should be not translated. |
|
46 | - * @param string $hint A useful message that is presented to the end |
|
47 | - * user. It should be translated, but must not |
|
48 | - * contain sensitive data. |
|
49 | - * @param int $code |
|
50 | - * @param \Exception|null $previous |
|
51 | - */ |
|
52 | - public function __construct($message, $hint = '', $code = 0, \Exception $previous = null) { |
|
53 | - $this->hint = $hint; |
|
54 | - parent::__construct($message, $code, $previous); |
|
55 | - } |
|
40 | + /** |
|
41 | + * HintException constructor. |
|
42 | + * |
|
43 | + * @param string $message The error message. It will be not revealed to the |
|
44 | + * the user (unless the hint is empty) and thus |
|
45 | + * should be not translated. |
|
46 | + * @param string $hint A useful message that is presented to the end |
|
47 | + * user. It should be translated, but must not |
|
48 | + * contain sensitive data. |
|
49 | + * @param int $code |
|
50 | + * @param \Exception|null $previous |
|
51 | + */ |
|
52 | + public function __construct($message, $hint = '', $code = 0, \Exception $previous = null) { |
|
53 | + $this->hint = $hint; |
|
54 | + parent::__construct($message, $code, $previous); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Returns a string representation of this Exception that includes the error |
|
59 | - * code, the message and the hint. |
|
60 | - * |
|
61 | - * @return string |
|
62 | - */ |
|
63 | - public function __toString() { |
|
64 | - return __CLASS__ . ": [{$this->code}]: {$this->message} ({$this->hint})\n"; |
|
65 | - } |
|
57 | + /** |
|
58 | + * Returns a string representation of this Exception that includes the error |
|
59 | + * code, the message and the hint. |
|
60 | + * |
|
61 | + * @return string |
|
62 | + */ |
|
63 | + public function __toString() { |
|
64 | + return __CLASS__ . ": [{$this->code}]: {$this->message} ({$this->hint})\n"; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * Returns the hint with the intention to be presented to the end user. If |
|
69 | - * an empty hint was specified upon instatiation, the message is returned |
|
70 | - * instead. |
|
71 | - * |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - public function getHint() { |
|
75 | - if (empty($this->hint)) { |
|
76 | - return $this->message; |
|
77 | - } |
|
78 | - return $this->hint; |
|
79 | - } |
|
67 | + /** |
|
68 | + * Returns the hint with the intention to be presented to the end user. If |
|
69 | + * an empty hint was specified upon instatiation, the message is returned |
|
70 | + * instead. |
|
71 | + * |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + public function getHint() { |
|
75 | + if (empty($this->hint)) { |
|
76 | + return $this->message; |
|
77 | + } |
|
78 | + return $this->hint; |
|
79 | + } |
|
80 | 80 | } |
@@ -45,168 +45,168 @@ |
||
45 | 45 | */ |
46 | 46 | |
47 | 47 | class Avatar implements IAvatar { |
48 | - /** @var ISimpleFolder */ |
|
49 | - private $folder; |
|
50 | - /** @var IL10N */ |
|
51 | - private $l; |
|
52 | - /** @var User */ |
|
53 | - private $user; |
|
54 | - /** @var ILogger */ |
|
55 | - private $logger; |
|
56 | - /** @var IConfig */ |
|
57 | - private $config; |
|
58 | - |
|
59 | - /** |
|
60 | - * constructor |
|
61 | - * |
|
62 | - * @param ISimpleFolder $folder The folder where the avatars are |
|
63 | - * @param IL10N $l |
|
64 | - * @param User $user |
|
65 | - * @param ILogger $logger |
|
66 | - * @param IConfig $config |
|
67 | - */ |
|
68 | - public function __construct(ISimpleFolder $folder, |
|
69 | - IL10N $l, |
|
70 | - $user, |
|
71 | - ILogger $logger, |
|
72 | - IConfig $config) { |
|
73 | - $this->folder = $folder; |
|
74 | - $this->l = $l; |
|
75 | - $this->user = $user; |
|
76 | - $this->logger = $logger; |
|
77 | - $this->config = $config; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @inheritdoc |
|
82 | - */ |
|
83 | - public function get ($size = 64) { |
|
84 | - try { |
|
85 | - $file = $this->getFile($size); |
|
86 | - } catch (NotFoundException $e) { |
|
87 | - return false; |
|
88 | - } |
|
89 | - |
|
90 | - $avatar = new OC_Image(); |
|
91 | - $avatar->loadFromData($file->getContent()); |
|
92 | - return $avatar; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Check if an avatar exists for the user |
|
97 | - * |
|
98 | - * @return bool |
|
99 | - */ |
|
100 | - public function exists() { |
|
101 | - |
|
102 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * sets the users avatar |
|
107 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
108 | - * @throws \Exception if the provided file is not a jpg or png image |
|
109 | - * @throws \Exception if the provided image is not valid |
|
110 | - * @throws NotSquareException if the image is not square |
|
111 | - * @return void |
|
112 | - */ |
|
113 | - public function set ($data) { |
|
114 | - |
|
115 | - if($data instanceOf IImage) { |
|
116 | - $img = $data; |
|
117 | - $data = $img->data(); |
|
118 | - } else { |
|
119 | - $img = new OC_Image($data); |
|
120 | - } |
|
121 | - $type = substr($img->mimeType(), -3); |
|
122 | - if ($type === 'peg') { |
|
123 | - $type = 'jpg'; |
|
124 | - } |
|
125 | - if ($type !== 'jpg' && $type !== 'png') { |
|
126 | - throw new \Exception($this->l->t("Unknown filetype")); |
|
127 | - } |
|
128 | - |
|
129 | - if (!$img->valid()) { |
|
130 | - throw new \Exception($this->l->t("Invalid image")); |
|
131 | - } |
|
132 | - |
|
133 | - if (!($img->height() === $img->width())) { |
|
134 | - throw new NotSquareException($this->l->t("Avatar image is not square")); |
|
135 | - } |
|
136 | - |
|
137 | - $this->remove(); |
|
138 | - $this->folder->newFile('avatar.'.$type)->putContent($data); |
|
139 | - $this->user->triggerChange('avatar'); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * remove the users avatar |
|
144 | - * @return void |
|
145 | - */ |
|
146 | - public function remove () { |
|
147 | - $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/'; |
|
148 | - $avatars = $this->folder->getDirectoryListing(); |
|
149 | - |
|
150 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
151 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
152 | - |
|
153 | - foreach ($avatars as $avatar) { |
|
154 | - if (preg_match($regex, $avatar->getName())) { |
|
155 | - $avatar->delete(); |
|
156 | - } |
|
157 | - } |
|
158 | - $this->user->triggerChange('avatar'); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @inheritdoc |
|
163 | - */ |
|
164 | - public function getFile($size) { |
|
165 | - $ext = $this->getExtension(); |
|
166 | - |
|
167 | - if ($size === -1) { |
|
168 | - $path = 'avatar.' . $ext; |
|
169 | - } else { |
|
170 | - $path = 'avatar.' . $size . '.' . $ext; |
|
171 | - } |
|
172 | - |
|
173 | - try { |
|
174 | - $file = $this->folder->getFile($path); |
|
175 | - } catch (NotFoundException $e) { |
|
176 | - if ($size <= 0) { |
|
177 | - throw new NotFoundException; |
|
178 | - } |
|
179 | - |
|
180 | - $avatar = new OC_Image(); |
|
181 | - /** @var ISimpleFile $file */ |
|
182 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
183 | - $avatar->loadFromData($file->getContent()); |
|
184 | - if ($size !== -1) { |
|
185 | - $avatar->resize($size); |
|
186 | - } |
|
187 | - try { |
|
188 | - $file = $this->folder->newFile($path); |
|
189 | - $file->putContent($avatar->data()); |
|
190 | - } catch (NotPermittedException $e) { |
|
191 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
192 | - } |
|
193 | - } |
|
194 | - |
|
195 | - return $file; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
200 | - * |
|
201 | - * @return string |
|
202 | - * @throws NotFoundException |
|
203 | - */ |
|
204 | - private function getExtension() { |
|
205 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
206 | - return 'jpg'; |
|
207 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
208 | - return 'png'; |
|
209 | - } |
|
210 | - throw new NotFoundException; |
|
211 | - } |
|
48 | + /** @var ISimpleFolder */ |
|
49 | + private $folder; |
|
50 | + /** @var IL10N */ |
|
51 | + private $l; |
|
52 | + /** @var User */ |
|
53 | + private $user; |
|
54 | + /** @var ILogger */ |
|
55 | + private $logger; |
|
56 | + /** @var IConfig */ |
|
57 | + private $config; |
|
58 | + |
|
59 | + /** |
|
60 | + * constructor |
|
61 | + * |
|
62 | + * @param ISimpleFolder $folder The folder where the avatars are |
|
63 | + * @param IL10N $l |
|
64 | + * @param User $user |
|
65 | + * @param ILogger $logger |
|
66 | + * @param IConfig $config |
|
67 | + */ |
|
68 | + public function __construct(ISimpleFolder $folder, |
|
69 | + IL10N $l, |
|
70 | + $user, |
|
71 | + ILogger $logger, |
|
72 | + IConfig $config) { |
|
73 | + $this->folder = $folder; |
|
74 | + $this->l = $l; |
|
75 | + $this->user = $user; |
|
76 | + $this->logger = $logger; |
|
77 | + $this->config = $config; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @inheritdoc |
|
82 | + */ |
|
83 | + public function get ($size = 64) { |
|
84 | + try { |
|
85 | + $file = $this->getFile($size); |
|
86 | + } catch (NotFoundException $e) { |
|
87 | + return false; |
|
88 | + } |
|
89 | + |
|
90 | + $avatar = new OC_Image(); |
|
91 | + $avatar->loadFromData($file->getContent()); |
|
92 | + return $avatar; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Check if an avatar exists for the user |
|
97 | + * |
|
98 | + * @return bool |
|
99 | + */ |
|
100 | + public function exists() { |
|
101 | + |
|
102 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * sets the users avatar |
|
107 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
108 | + * @throws \Exception if the provided file is not a jpg or png image |
|
109 | + * @throws \Exception if the provided image is not valid |
|
110 | + * @throws NotSquareException if the image is not square |
|
111 | + * @return void |
|
112 | + */ |
|
113 | + public function set ($data) { |
|
114 | + |
|
115 | + if($data instanceOf IImage) { |
|
116 | + $img = $data; |
|
117 | + $data = $img->data(); |
|
118 | + } else { |
|
119 | + $img = new OC_Image($data); |
|
120 | + } |
|
121 | + $type = substr($img->mimeType(), -3); |
|
122 | + if ($type === 'peg') { |
|
123 | + $type = 'jpg'; |
|
124 | + } |
|
125 | + if ($type !== 'jpg' && $type !== 'png') { |
|
126 | + throw new \Exception($this->l->t("Unknown filetype")); |
|
127 | + } |
|
128 | + |
|
129 | + if (!$img->valid()) { |
|
130 | + throw new \Exception($this->l->t("Invalid image")); |
|
131 | + } |
|
132 | + |
|
133 | + if (!($img->height() === $img->width())) { |
|
134 | + throw new NotSquareException($this->l->t("Avatar image is not square")); |
|
135 | + } |
|
136 | + |
|
137 | + $this->remove(); |
|
138 | + $this->folder->newFile('avatar.'.$type)->putContent($data); |
|
139 | + $this->user->triggerChange('avatar'); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * remove the users avatar |
|
144 | + * @return void |
|
145 | + */ |
|
146 | + public function remove () { |
|
147 | + $regex = '/^avatar\.([0-9]+\.)?(jpg|png)$/'; |
|
148 | + $avatars = $this->folder->getDirectoryListing(); |
|
149 | + |
|
150 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
151 | + (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
152 | + |
|
153 | + foreach ($avatars as $avatar) { |
|
154 | + if (preg_match($regex, $avatar->getName())) { |
|
155 | + $avatar->delete(); |
|
156 | + } |
|
157 | + } |
|
158 | + $this->user->triggerChange('avatar'); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @inheritdoc |
|
163 | + */ |
|
164 | + public function getFile($size) { |
|
165 | + $ext = $this->getExtension(); |
|
166 | + |
|
167 | + if ($size === -1) { |
|
168 | + $path = 'avatar.' . $ext; |
|
169 | + } else { |
|
170 | + $path = 'avatar.' . $size . '.' . $ext; |
|
171 | + } |
|
172 | + |
|
173 | + try { |
|
174 | + $file = $this->folder->getFile($path); |
|
175 | + } catch (NotFoundException $e) { |
|
176 | + if ($size <= 0) { |
|
177 | + throw new NotFoundException; |
|
178 | + } |
|
179 | + |
|
180 | + $avatar = new OC_Image(); |
|
181 | + /** @var ISimpleFile $file */ |
|
182 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
183 | + $avatar->loadFromData($file->getContent()); |
|
184 | + if ($size !== -1) { |
|
185 | + $avatar->resize($size); |
|
186 | + } |
|
187 | + try { |
|
188 | + $file = $this->folder->newFile($path); |
|
189 | + $file->putContent($avatar->data()); |
|
190 | + } catch (NotPermittedException $e) { |
|
191 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
192 | + } |
|
193 | + } |
|
194 | + |
|
195 | + return $file; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
200 | + * |
|
201 | + * @return string |
|
202 | + * @throws NotFoundException |
|
203 | + */ |
|
204 | + private function getExtension() { |
|
205 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
206 | + return 'jpg'; |
|
207 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
208 | + return 'png'; |
|
209 | + } |
|
210 | + throw new NotFoundException; |
|
211 | + } |
|
212 | 212 | } |