@@ -32,119 +32,119 @@ |
||
32 | 32 | * @since 16.0.0 |
33 | 33 | */ |
34 | 34 | class InMemoryFile implements ISimpleFile { |
35 | - /** |
|
36 | - * Holds the file name. |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - private $name; |
|
41 | - |
|
42 | - /** |
|
43 | - * Holds the file contents. |
|
44 | - * |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - private $contents; |
|
48 | - |
|
49 | - /** |
|
50 | - * InMemoryFile constructor. |
|
51 | - * |
|
52 | - * @param string $name The file name |
|
53 | - * @param string $contents The file contents |
|
54 | - * @since 16.0.0 |
|
55 | - */ |
|
56 | - public function __construct(string $name, string $contents) { |
|
57 | - $this->name = $name; |
|
58 | - $this->contents = $contents; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @inheritdoc |
|
63 | - * @since 16.0.0 |
|
64 | - */ |
|
65 | - public function getName() { |
|
66 | - return $this->name; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * @inheritdoc |
|
71 | - * @since 16.0.0 |
|
72 | - */ |
|
73 | - public function getSize() { |
|
74 | - return strlen($this->contents); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @inheritdoc |
|
79 | - * @since 16.0.0 |
|
80 | - */ |
|
81 | - public function getETag() { |
|
82 | - return ''; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @inheritdoc |
|
87 | - * @since 16.0.0 |
|
88 | - */ |
|
89 | - public function getMTime() { |
|
90 | - return time(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * @inheritdoc |
|
95 | - * @since 16.0.0 |
|
96 | - */ |
|
97 | - public function getContent() { |
|
98 | - return $this->contents; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @inheritdoc |
|
103 | - * @since 16.0.0 |
|
104 | - */ |
|
105 | - public function putContent($data) { |
|
106 | - $this->contents = $data; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * In memory files can't be deleted. |
|
111 | - * |
|
112 | - * @since 16.0.0 |
|
113 | - */ |
|
114 | - public function delete() { |
|
115 | - // unimplemented for in memory files |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @inheritdoc |
|
120 | - * @since 16.0.0 |
|
121 | - */ |
|
122 | - public function getMimeType() { |
|
123 | - $fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
|
124 | - return $fileInfo->buffer($this->contents); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Stream reading is unsupported for in memory files. |
|
129 | - * |
|
130 | - * @throws NotPermittedException |
|
131 | - * @since 16.0.0 |
|
132 | - */ |
|
133 | - public function read() { |
|
134 | - throw new NotPermittedException( |
|
135 | - 'Stream reading is unsupported for in memory files' |
|
136 | - ); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Stream writing isn't available for in memory files. |
|
141 | - * |
|
142 | - * @throws NotPermittedException |
|
143 | - * @since 16.0.0 |
|
144 | - */ |
|
145 | - public function write() { |
|
146 | - throw new NotPermittedException( |
|
147 | - 'Stream writing is unsupported for in memory files' |
|
148 | - ); |
|
149 | - } |
|
35 | + /** |
|
36 | + * Holds the file name. |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + private $name; |
|
41 | + |
|
42 | + /** |
|
43 | + * Holds the file contents. |
|
44 | + * |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + private $contents; |
|
48 | + |
|
49 | + /** |
|
50 | + * InMemoryFile constructor. |
|
51 | + * |
|
52 | + * @param string $name The file name |
|
53 | + * @param string $contents The file contents |
|
54 | + * @since 16.0.0 |
|
55 | + */ |
|
56 | + public function __construct(string $name, string $contents) { |
|
57 | + $this->name = $name; |
|
58 | + $this->contents = $contents; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @inheritdoc |
|
63 | + * @since 16.0.0 |
|
64 | + */ |
|
65 | + public function getName() { |
|
66 | + return $this->name; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * @inheritdoc |
|
71 | + * @since 16.0.0 |
|
72 | + */ |
|
73 | + public function getSize() { |
|
74 | + return strlen($this->contents); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @inheritdoc |
|
79 | + * @since 16.0.0 |
|
80 | + */ |
|
81 | + public function getETag() { |
|
82 | + return ''; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @inheritdoc |
|
87 | + * @since 16.0.0 |
|
88 | + */ |
|
89 | + public function getMTime() { |
|
90 | + return time(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * @inheritdoc |
|
95 | + * @since 16.0.0 |
|
96 | + */ |
|
97 | + public function getContent() { |
|
98 | + return $this->contents; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @inheritdoc |
|
103 | + * @since 16.0.0 |
|
104 | + */ |
|
105 | + public function putContent($data) { |
|
106 | + $this->contents = $data; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * In memory files can't be deleted. |
|
111 | + * |
|
112 | + * @since 16.0.0 |
|
113 | + */ |
|
114 | + public function delete() { |
|
115 | + // unimplemented for in memory files |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @inheritdoc |
|
120 | + * @since 16.0.0 |
|
121 | + */ |
|
122 | + public function getMimeType() { |
|
123 | + $fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
|
124 | + return $fileInfo->buffer($this->contents); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Stream reading is unsupported for in memory files. |
|
129 | + * |
|
130 | + * @throws NotPermittedException |
|
131 | + * @since 16.0.0 |
|
132 | + */ |
|
133 | + public function read() { |
|
134 | + throw new NotPermittedException( |
|
135 | + 'Stream reading is unsupported for in memory files' |
|
136 | + ); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Stream writing isn't available for in memory files. |
|
141 | + * |
|
142 | + * @throws NotPermittedException |
|
143 | + * @since 16.0.0 |
|
144 | + */ |
|
145 | + public function write() { |
|
146 | + throw new NotPermittedException( |
|
147 | + 'Stream writing is unsupported for in memory files' |
|
148 | + ); |
|
149 | + } |
|
150 | 150 | } |
@@ -34,24 +34,24 @@ |
||
34 | 34 | |
35 | 35 | interface IAvatarManager { |
36 | 36 | |
37 | - /** |
|
38 | - * return a user specific instance of \OCP\IAvatar |
|
39 | - * @see IAvatar |
|
40 | - * @param string $user the ownCloud user id |
|
41 | - * @return IAvatar |
|
42 | - * @throws \Exception In case the username is potentially dangerous |
|
43 | - * @throws \OCP\Files\NotFoundException In case there is no user folder yet |
|
44 | - * @since 6.0.0 |
|
45 | - */ |
|
46 | - public function getAvatar(string $user) : IAvatar; |
|
37 | + /** |
|
38 | + * return a user specific instance of \OCP\IAvatar |
|
39 | + * @see IAvatar |
|
40 | + * @param string $user the ownCloud user id |
|
41 | + * @return IAvatar |
|
42 | + * @throws \Exception In case the username is potentially dangerous |
|
43 | + * @throws \OCP\Files\NotFoundException In case there is no user folder yet |
|
44 | + * @since 6.0.0 |
|
45 | + */ |
|
46 | + public function getAvatar(string $user) : IAvatar; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Returns a guest user avatar instance. |
|
50 | - * |
|
51 | - * @param string $name The guest name, e.g. "Albert". |
|
52 | - * @return IAvatar |
|
53 | - * @since 16.0.0 |
|
54 | - */ |
|
55 | - public function getGuestAvatar(string $name): IAvatar; |
|
48 | + /** |
|
49 | + * Returns a guest user avatar instance. |
|
50 | + * |
|
51 | + * @param string $name The guest name, e.g. "Albert". |
|
52 | + * @return IAvatar |
|
53 | + * @since 16.0.0 |
|
54 | + */ |
|
55 | + public function getGuestAvatar(string $name): IAvatar; |
|
56 | 56 | |
57 | 57 | } |
@@ -44,275 +44,275 @@ |
||
44 | 44 | */ |
45 | 45 | abstract class Avatar implements IAvatar { |
46 | 46 | |
47 | - /** @var ILogger */ |
|
48 | - protected $logger; |
|
49 | - |
|
50 | - /** |
|
51 | - * https://github.com/sebdesign/cap-height -- for 500px height |
|
52 | - * Automated check: https://codepen.io/skjnldsv/pen/PydLBK/ |
|
53 | - * Nunito cap-height is 0.716 and we want a 200px caps height size |
|
54 | - * (0.4 letter-to-total-height ratio, 500*0.4=200), so: 200/0.716 = 279px. |
|
55 | - * Since we start from the baseline (text-anchor) we need to |
|
56 | - * shift the y axis by 100px (half the caps height): 500/2+100=350 |
|
57 | - * |
|
58 | - * @var string |
|
59 | - */ |
|
60 | - private $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
47 | + /** @var ILogger */ |
|
48 | + protected $logger; |
|
49 | + |
|
50 | + /** |
|
51 | + * https://github.com/sebdesign/cap-height -- for 500px height |
|
52 | + * Automated check: https://codepen.io/skjnldsv/pen/PydLBK/ |
|
53 | + * Nunito cap-height is 0.716 and we want a 200px caps height size |
|
54 | + * (0.4 letter-to-total-height ratio, 500*0.4=200), so: 200/0.716 = 279px. |
|
55 | + * Since we start from the baseline (text-anchor) we need to |
|
56 | + * shift the y axis by 100px (half the caps height): 500/2+100=350 |
|
57 | + * |
|
58 | + * @var string |
|
59 | + */ |
|
60 | + private $svgTemplate = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
|
61 | 61 | <svg width="{size}" height="{size}" version="1.1" viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"> |
62 | 62 | <rect width="100%" height="100%" fill="#{fill}"></rect> |
63 | 63 | <text x="50%" y="350" style="font-weight:normal;font-size:279px;font-family:\'Nunito\';text-anchor:middle;fill:#fff">{letter}</text> |
64 | 64 | </svg>'; |
65 | 65 | |
66 | - /** |
|
67 | - * The base avatar constructor. |
|
68 | - * |
|
69 | - * @param ILogger $logger The logger |
|
70 | - */ |
|
71 | - public function __construct(ILogger $logger) { |
|
72 | - $this->logger = $logger; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Returns the user display name. |
|
77 | - * |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - abstract public function getDisplayName(): string; |
|
81 | - |
|
82 | - /** |
|
83 | - * Returns the first letter of the display name, or "?" if no name given. |
|
84 | - * |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - private function getAvatarLetter(): string { |
|
88 | - $displayName = $this->getDisplayName(); |
|
89 | - if (empty($displayName) === true) { |
|
90 | - return '?'; |
|
91 | - } else { |
|
92 | - return mb_strtoupper(mb_substr($displayName, 0, 1), 'UTF-8'); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @inheritdoc |
|
98 | - */ |
|
99 | - public function get($size = 64) { |
|
100 | - $size = (int) $size; |
|
101 | - |
|
102 | - try { |
|
103 | - $file = $this->getFile($size); |
|
104 | - } catch (NotFoundException $e) { |
|
105 | - return false; |
|
106 | - } |
|
107 | - |
|
108 | - $avatar = new OC_Image(); |
|
109 | - $avatar->loadFromData($file->getContent()); |
|
110 | - return $avatar; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * {size} = 500 |
|
115 | - * {fill} = hex color to fill |
|
116 | - * {letter} = Letter to display |
|
117 | - * |
|
118 | - * Generate SVG avatar |
|
119 | - * |
|
120 | - * @param int $size The requested image size in pixel |
|
121 | - * @return string |
|
122 | - * |
|
123 | - */ |
|
124 | - protected function getAvatarVector(int $size): string { |
|
125 | - $userDisplayName = $this->getDisplayName(); |
|
126 | - $bgRGB = $this->avatarBackgroundColor($userDisplayName); |
|
127 | - $bgHEX = sprintf("%02x%02x%02x", $bgRGB->r, $bgRGB->g, $bgRGB->b); |
|
128 | - $letter = $this->getAvatarLetter(); |
|
129 | - $toReplace = ['{size}', '{fill}', '{letter}']; |
|
130 | - return str_replace($toReplace, [$size, $bgHEX, $letter], $this->svgTemplate); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Generate png avatar from svg with Imagick |
|
135 | - * |
|
136 | - * @param int $size |
|
137 | - * @return string|boolean |
|
138 | - */ |
|
139 | - protected function generateAvatarFromSvg(int $size) { |
|
140 | - if (!extension_loaded('imagick')) { |
|
141 | - return false; |
|
142 | - } |
|
143 | - try { |
|
144 | - $font = __DIR__ . '/../../core/fonts/Nunito-Regular.ttf'; |
|
145 | - $svg = $this->getAvatarVector($size); |
|
146 | - $avatar = new Imagick(); |
|
147 | - $avatar->setFont($font); |
|
148 | - $avatar->readImageBlob($svg); |
|
149 | - $avatar->setImageFormat('png'); |
|
150 | - $image = new OC_Image(); |
|
151 | - $image->loadFromData($avatar); |
|
152 | - return $image->data(); |
|
153 | - } catch (\Exception $e) { |
|
154 | - return false; |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Generate png avatar with GD |
|
160 | - * |
|
161 | - * @param string $userDisplayName |
|
162 | - * @param int $size |
|
163 | - * @return string |
|
164 | - */ |
|
165 | - protected function generateAvatar($userDisplayName, $size) { |
|
166 | - $letter = $this->getAvatarLetter(); |
|
167 | - $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
168 | - |
|
169 | - $im = imagecreatetruecolor($size, $size); |
|
170 | - $background = imagecolorallocate( |
|
171 | - $im, |
|
172 | - $backgroundColor->r, |
|
173 | - $backgroundColor->g, |
|
174 | - $backgroundColor->b |
|
175 | - ); |
|
176 | - $white = imagecolorallocate($im, 255, 255, 255); |
|
177 | - imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
178 | - |
|
179 | - $font = __DIR__ . '/../../../core/fonts/Nunito-Regular.ttf'; |
|
180 | - |
|
181 | - $fontSize = $size * 0.4; |
|
182 | - list($x, $y) = $this->imageTTFCenter( |
|
183 | - $im, $letter, $font, (int)$fontSize |
|
184 | - ); |
|
185 | - |
|
186 | - imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $letter); |
|
187 | - |
|
188 | - ob_start(); |
|
189 | - imagepng($im); |
|
190 | - $data = ob_get_contents(); |
|
191 | - ob_end_clean(); |
|
192 | - |
|
193 | - return $data; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Calculate real image ttf center |
|
198 | - * |
|
199 | - * @param resource $image |
|
200 | - * @param string $text text string |
|
201 | - * @param string $font font path |
|
202 | - * @param int $size font size |
|
203 | - * @param int $angle |
|
204 | - * @return array |
|
205 | - */ |
|
206 | - protected function imageTTFCenter( |
|
207 | - $image, |
|
208 | - string $text, |
|
209 | - string $font, |
|
210 | - int $size, |
|
211 | - $angle = 0 |
|
212 | - ): array { |
|
213 | - // Image width & height |
|
214 | - $xi = imagesx($image); |
|
215 | - $yi = imagesy($image); |
|
216 | - |
|
217 | - // bounding box |
|
218 | - $box = imagettfbbox($size, $angle, $font, $text); |
|
219 | - |
|
220 | - // imagettfbbox can return negative int |
|
221 | - $xr = abs(max($box[2], $box[4])); |
|
222 | - $yr = abs(max($box[5], $box[7])); |
|
223 | - |
|
224 | - // calculate bottom left placement |
|
225 | - $x = intval(($xi - $xr) / 2); |
|
226 | - $y = intval(($yi + $yr) / 2); |
|
227 | - |
|
228 | - return array($x, $y); |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Calculate steps between two Colors |
|
233 | - * @param object Color $steps start color |
|
234 | - * @param object Color $ends end color |
|
235 | - * @return array [r,g,b] steps for each color to go from $steps to $ends |
|
236 | - */ |
|
237 | - private function stepCalc($steps, $ends) { |
|
238 | - $step = array(); |
|
239 | - $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
|
240 | - $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
|
241 | - $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
|
242 | - return $step; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Convert a string to an integer evenly |
|
247 | - * @param string $hash the text to parse |
|
248 | - * @param int $maximum the maximum range |
|
249 | - * @return int[] between 0 and $maximum |
|
250 | - */ |
|
251 | - private function mixPalette($steps, $color1, $color2) { |
|
252 | - $palette = array($color1); |
|
253 | - $step = $this->stepCalc($steps, [$color1, $color2]); |
|
254 | - for ($i = 1; $i < $steps; $i++) { |
|
255 | - $r = intval($color1->r + ($step[0] * $i)); |
|
256 | - $g = intval($color1->g + ($step[1] * $i)); |
|
257 | - $b = intval($color1->b + ($step[2] * $i)); |
|
258 | - $palette[] = new Color($r, $g, $b); |
|
259 | - } |
|
260 | - return $palette; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Convert a string to an integer evenly |
|
265 | - * @param string $hash the text to parse |
|
266 | - * @param int $maximum the maximum range |
|
267 | - * @return int between 0 and $maximum |
|
268 | - */ |
|
269 | - private function hashToInt($hash, $maximum) { |
|
270 | - $final = 0; |
|
271 | - $result = array(); |
|
272 | - |
|
273 | - // Splitting evenly the string |
|
274 | - for ($i = 0; $i < strlen($hash); $i++) { |
|
275 | - // chars in md5 goes up to f, hex:16 |
|
276 | - $result[] = intval(substr($hash, $i, 1), 16) % 16; |
|
277 | - } |
|
278 | - // Adds up all results |
|
279 | - foreach ($result as $value) { |
|
280 | - $final += $value; |
|
281 | - } |
|
282 | - // chars in md5 goes up to f, hex:16 |
|
283 | - return intval($final % $maximum); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @param string $hash |
|
288 | - * @return Color Object containting r g b int in the range [0, 255] |
|
289 | - */ |
|
290 | - public function avatarBackgroundColor(string $hash) { |
|
291 | - // Normalize hash |
|
292 | - $hash = strtolower($hash); |
|
293 | - |
|
294 | - // Already a md5 hash? |
|
295 | - if( preg_match('/^([0-9a-f]{4}-?){8}$/', $hash, $matches) !== 1 ) { |
|
296 | - $hash = md5($hash); |
|
297 | - } |
|
298 | - |
|
299 | - // Remove unwanted char |
|
300 | - $hash = preg_replace('/[^0-9a-f]+/', '', $hash); |
|
301 | - |
|
302 | - $red = new Color(182, 70, 157); |
|
303 | - $yellow = new Color(221, 203, 85); |
|
304 | - $blue = new Color(0, 130, 201); // Nextcloud blue |
|
305 | - |
|
306 | - // Number of steps to go from a color to another |
|
307 | - // 3 colors * 6 will result in 18 generated colors |
|
308 | - $steps = 6; |
|
309 | - |
|
310 | - $palette1 = $this->mixPalette($steps, $red, $yellow); |
|
311 | - $palette2 = $this->mixPalette($steps, $yellow, $blue); |
|
312 | - $palette3 = $this->mixPalette($steps, $blue, $red); |
|
313 | - |
|
314 | - $finalPalette = array_merge($palette1, $palette2, $palette3); |
|
315 | - |
|
316 | - return $finalPalette[$this->hashToInt($hash, $steps * 3)]; |
|
317 | - } |
|
66 | + /** |
|
67 | + * The base avatar constructor. |
|
68 | + * |
|
69 | + * @param ILogger $logger The logger |
|
70 | + */ |
|
71 | + public function __construct(ILogger $logger) { |
|
72 | + $this->logger = $logger; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Returns the user display name. |
|
77 | + * |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + abstract public function getDisplayName(): string; |
|
81 | + |
|
82 | + /** |
|
83 | + * Returns the first letter of the display name, or "?" if no name given. |
|
84 | + * |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + private function getAvatarLetter(): string { |
|
88 | + $displayName = $this->getDisplayName(); |
|
89 | + if (empty($displayName) === true) { |
|
90 | + return '?'; |
|
91 | + } else { |
|
92 | + return mb_strtoupper(mb_substr($displayName, 0, 1), 'UTF-8'); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @inheritdoc |
|
98 | + */ |
|
99 | + public function get($size = 64) { |
|
100 | + $size = (int) $size; |
|
101 | + |
|
102 | + try { |
|
103 | + $file = $this->getFile($size); |
|
104 | + } catch (NotFoundException $e) { |
|
105 | + return false; |
|
106 | + } |
|
107 | + |
|
108 | + $avatar = new OC_Image(); |
|
109 | + $avatar->loadFromData($file->getContent()); |
|
110 | + return $avatar; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * {size} = 500 |
|
115 | + * {fill} = hex color to fill |
|
116 | + * {letter} = Letter to display |
|
117 | + * |
|
118 | + * Generate SVG avatar |
|
119 | + * |
|
120 | + * @param int $size The requested image size in pixel |
|
121 | + * @return string |
|
122 | + * |
|
123 | + */ |
|
124 | + protected function getAvatarVector(int $size): string { |
|
125 | + $userDisplayName = $this->getDisplayName(); |
|
126 | + $bgRGB = $this->avatarBackgroundColor($userDisplayName); |
|
127 | + $bgHEX = sprintf("%02x%02x%02x", $bgRGB->r, $bgRGB->g, $bgRGB->b); |
|
128 | + $letter = $this->getAvatarLetter(); |
|
129 | + $toReplace = ['{size}', '{fill}', '{letter}']; |
|
130 | + return str_replace($toReplace, [$size, $bgHEX, $letter], $this->svgTemplate); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Generate png avatar from svg with Imagick |
|
135 | + * |
|
136 | + * @param int $size |
|
137 | + * @return string|boolean |
|
138 | + */ |
|
139 | + protected function generateAvatarFromSvg(int $size) { |
|
140 | + if (!extension_loaded('imagick')) { |
|
141 | + return false; |
|
142 | + } |
|
143 | + try { |
|
144 | + $font = __DIR__ . '/../../core/fonts/Nunito-Regular.ttf'; |
|
145 | + $svg = $this->getAvatarVector($size); |
|
146 | + $avatar = new Imagick(); |
|
147 | + $avatar->setFont($font); |
|
148 | + $avatar->readImageBlob($svg); |
|
149 | + $avatar->setImageFormat('png'); |
|
150 | + $image = new OC_Image(); |
|
151 | + $image->loadFromData($avatar); |
|
152 | + return $image->data(); |
|
153 | + } catch (\Exception $e) { |
|
154 | + return false; |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Generate png avatar with GD |
|
160 | + * |
|
161 | + * @param string $userDisplayName |
|
162 | + * @param int $size |
|
163 | + * @return string |
|
164 | + */ |
|
165 | + protected function generateAvatar($userDisplayName, $size) { |
|
166 | + $letter = $this->getAvatarLetter(); |
|
167 | + $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
168 | + |
|
169 | + $im = imagecreatetruecolor($size, $size); |
|
170 | + $background = imagecolorallocate( |
|
171 | + $im, |
|
172 | + $backgroundColor->r, |
|
173 | + $backgroundColor->g, |
|
174 | + $backgroundColor->b |
|
175 | + ); |
|
176 | + $white = imagecolorallocate($im, 255, 255, 255); |
|
177 | + imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
178 | + |
|
179 | + $font = __DIR__ . '/../../../core/fonts/Nunito-Regular.ttf'; |
|
180 | + |
|
181 | + $fontSize = $size * 0.4; |
|
182 | + list($x, $y) = $this->imageTTFCenter( |
|
183 | + $im, $letter, $font, (int)$fontSize |
|
184 | + ); |
|
185 | + |
|
186 | + imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $letter); |
|
187 | + |
|
188 | + ob_start(); |
|
189 | + imagepng($im); |
|
190 | + $data = ob_get_contents(); |
|
191 | + ob_end_clean(); |
|
192 | + |
|
193 | + return $data; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Calculate real image ttf center |
|
198 | + * |
|
199 | + * @param resource $image |
|
200 | + * @param string $text text string |
|
201 | + * @param string $font font path |
|
202 | + * @param int $size font size |
|
203 | + * @param int $angle |
|
204 | + * @return array |
|
205 | + */ |
|
206 | + protected function imageTTFCenter( |
|
207 | + $image, |
|
208 | + string $text, |
|
209 | + string $font, |
|
210 | + int $size, |
|
211 | + $angle = 0 |
|
212 | + ): array { |
|
213 | + // Image width & height |
|
214 | + $xi = imagesx($image); |
|
215 | + $yi = imagesy($image); |
|
216 | + |
|
217 | + // bounding box |
|
218 | + $box = imagettfbbox($size, $angle, $font, $text); |
|
219 | + |
|
220 | + // imagettfbbox can return negative int |
|
221 | + $xr = abs(max($box[2], $box[4])); |
|
222 | + $yr = abs(max($box[5], $box[7])); |
|
223 | + |
|
224 | + // calculate bottom left placement |
|
225 | + $x = intval(($xi - $xr) / 2); |
|
226 | + $y = intval(($yi + $yr) / 2); |
|
227 | + |
|
228 | + return array($x, $y); |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Calculate steps between two Colors |
|
233 | + * @param object Color $steps start color |
|
234 | + * @param object Color $ends end color |
|
235 | + * @return array [r,g,b] steps for each color to go from $steps to $ends |
|
236 | + */ |
|
237 | + private function stepCalc($steps, $ends) { |
|
238 | + $step = array(); |
|
239 | + $step[0] = ($ends[1]->r - $ends[0]->r) / $steps; |
|
240 | + $step[1] = ($ends[1]->g - $ends[0]->g) / $steps; |
|
241 | + $step[2] = ($ends[1]->b - $ends[0]->b) / $steps; |
|
242 | + return $step; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Convert a string to an integer evenly |
|
247 | + * @param string $hash the text to parse |
|
248 | + * @param int $maximum the maximum range |
|
249 | + * @return int[] between 0 and $maximum |
|
250 | + */ |
|
251 | + private function mixPalette($steps, $color1, $color2) { |
|
252 | + $palette = array($color1); |
|
253 | + $step = $this->stepCalc($steps, [$color1, $color2]); |
|
254 | + for ($i = 1; $i < $steps; $i++) { |
|
255 | + $r = intval($color1->r + ($step[0] * $i)); |
|
256 | + $g = intval($color1->g + ($step[1] * $i)); |
|
257 | + $b = intval($color1->b + ($step[2] * $i)); |
|
258 | + $palette[] = new Color($r, $g, $b); |
|
259 | + } |
|
260 | + return $palette; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Convert a string to an integer evenly |
|
265 | + * @param string $hash the text to parse |
|
266 | + * @param int $maximum the maximum range |
|
267 | + * @return int between 0 and $maximum |
|
268 | + */ |
|
269 | + private function hashToInt($hash, $maximum) { |
|
270 | + $final = 0; |
|
271 | + $result = array(); |
|
272 | + |
|
273 | + // Splitting evenly the string |
|
274 | + for ($i = 0; $i < strlen($hash); $i++) { |
|
275 | + // chars in md5 goes up to f, hex:16 |
|
276 | + $result[] = intval(substr($hash, $i, 1), 16) % 16; |
|
277 | + } |
|
278 | + // Adds up all results |
|
279 | + foreach ($result as $value) { |
|
280 | + $final += $value; |
|
281 | + } |
|
282 | + // chars in md5 goes up to f, hex:16 |
|
283 | + return intval($final % $maximum); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @param string $hash |
|
288 | + * @return Color Object containting r g b int in the range [0, 255] |
|
289 | + */ |
|
290 | + public function avatarBackgroundColor(string $hash) { |
|
291 | + // Normalize hash |
|
292 | + $hash = strtolower($hash); |
|
293 | + |
|
294 | + // Already a md5 hash? |
|
295 | + if( preg_match('/^([0-9a-f]{4}-?){8}$/', $hash, $matches) !== 1 ) { |
|
296 | + $hash = md5($hash); |
|
297 | + } |
|
298 | + |
|
299 | + // Remove unwanted char |
|
300 | + $hash = preg_replace('/[^0-9a-f]+/', '', $hash); |
|
301 | + |
|
302 | + $red = new Color(182, 70, 157); |
|
303 | + $yellow = new Color(221, 203, 85); |
|
304 | + $blue = new Color(0, 130, 201); // Nextcloud blue |
|
305 | + |
|
306 | + // Number of steps to go from a color to another |
|
307 | + // 3 colors * 6 will result in 18 generated colors |
|
308 | + $steps = 6; |
|
309 | + |
|
310 | + $palette1 = $this->mixPalette($steps, $red, $yellow); |
|
311 | + $palette2 = $this->mixPalette($steps, $yellow, $blue); |
|
312 | + $palette3 = $this->mixPalette($steps, $blue, $red); |
|
313 | + |
|
314 | + $finalPalette = array_merge($palette1, $palette2, $palette3); |
|
315 | + |
|
316 | + return $finalPalette[$this->hashToInt($hash, $steps * 3)]; |
|
317 | + } |
|
318 | 318 | } |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | return false; |
142 | 142 | } |
143 | 143 | try { |
144 | - $font = __DIR__ . '/../../core/fonts/Nunito-Regular.ttf'; |
|
144 | + $font = __DIR__.'/../../core/fonts/Nunito-Regular.ttf'; |
|
145 | 145 | $svg = $this->getAvatarVector($size); |
146 | 146 | $avatar = new Imagick(); |
147 | 147 | $avatar->setFont($font); |
@@ -176,11 +176,11 @@ discard block |
||
176 | 176 | $white = imagecolorallocate($im, 255, 255, 255); |
177 | 177 | imagefilledrectangle($im, 0, 0, $size, $size, $background); |
178 | 178 | |
179 | - $font = __DIR__ . '/../../../core/fonts/Nunito-Regular.ttf'; |
|
179 | + $font = __DIR__.'/../../../core/fonts/Nunito-Regular.ttf'; |
|
180 | 180 | |
181 | 181 | $fontSize = $size * 0.4; |
182 | 182 | list($x, $y) = $this->imageTTFCenter( |
183 | - $im, $letter, $font, (int)$fontSize |
|
183 | + $im, $letter, $font, (int) $fontSize |
|
184 | 184 | ); |
185 | 185 | |
186 | 186 | imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $letter); |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | $hash = strtolower($hash); |
293 | 293 | |
294 | 294 | // Already a md5 hash? |
295 | - if( preg_match('/^([0-9a-f]{4}-?){8}$/', $hash, $matches) !== 1 ) { |
|
295 | + if (preg_match('/^([0-9a-f]{4}-?){8}$/', $hash, $matches) !== 1) { |
|
296 | 296 | $hash = md5($hash); |
297 | 297 | } |
298 | 298 |
@@ -39,298 +39,298 @@ |
||
39 | 39 | * This class represents a registered user's avatar. |
40 | 40 | */ |
41 | 41 | class UserAvatar extends Avatar { |
42 | - /** @var IConfig */ |
|
43 | - private $config; |
|
44 | - |
|
45 | - /** @var ISimpleFolder */ |
|
46 | - private $folder; |
|
47 | - |
|
48 | - /** @var IL10N */ |
|
49 | - private $l; |
|
50 | - |
|
51 | - /** @var User */ |
|
52 | - private $user; |
|
53 | - |
|
54 | - /** |
|
55 | - * UserAvatar constructor. |
|
56 | - * |
|
57 | - * @param IConfig $config The configuration |
|
58 | - * @param ISimpleFolder $folder The avatar files folder |
|
59 | - * @param IL10N $l The localization helper |
|
60 | - * @param User $user The user this class manages the avatar for |
|
61 | - * @param ILogger $logger The logger |
|
62 | - */ |
|
63 | - public function __construct( |
|
64 | - ISimpleFolder $folder, |
|
65 | - IL10N $l, |
|
66 | - $user, |
|
67 | - ILogger $logger, |
|
68 | - IConfig $config) { |
|
69 | - parent::__construct($logger); |
|
70 | - $this->folder = $folder; |
|
71 | - $this->l = $l; |
|
72 | - $this->user = $user; |
|
73 | - $this->config = $config; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Check if an avatar exists for the user |
|
78 | - * |
|
79 | - * @return bool |
|
80 | - */ |
|
81 | - public function exists() { |
|
82 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Sets the users avatar. |
|
87 | - * |
|
88 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
89 | - * @throws \Exception if the provided file is not a jpg or png image |
|
90 | - * @throws \Exception if the provided image is not valid |
|
91 | - * @throws NotSquareException if the image is not square |
|
92 | - * @return void |
|
93 | - */ |
|
94 | - public function set($data) { |
|
95 | - $img = $this->getAvatarImage($data); |
|
96 | - $data = $img->data(); |
|
97 | - |
|
98 | - $this->validateAvatar($img); |
|
99 | - |
|
100 | - $this->remove(); |
|
101 | - $type = $this->getAvatarImageType($img); |
|
102 | - $file = $this->folder->newFile('avatar.' . $type); |
|
103 | - $file->putContent($data); |
|
104 | - |
|
105 | - try { |
|
106 | - $generated = $this->folder->getFile('generated'); |
|
107 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
108 | - $generated->delete(); |
|
109 | - } catch (NotFoundException $e) { |
|
110 | - // |
|
111 | - } |
|
112 | - |
|
113 | - $this->user->triggerChange('avatar', $file); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns an image from several sources. |
|
118 | - * |
|
119 | - * @param IImage|resource|string $data An image object, imagedata or path to the avatar |
|
120 | - * @return IImage |
|
121 | - */ |
|
122 | - private function getAvatarImage($data) { |
|
123 | - if ($data instanceof IImage) { |
|
124 | - return $data; |
|
125 | - } |
|
126 | - |
|
127 | - $img = new OC_Image(); |
|
128 | - if (is_resource($data) && get_resource_type($data) === 'gd') { |
|
129 | - $img->setResource($data); |
|
130 | - } elseif (is_resource($data)) { |
|
131 | - $img->loadFromFileHandle($data); |
|
132 | - } else { |
|
133 | - try { |
|
134 | - // detect if it is a path or maybe the images as string |
|
135 | - $result = @realpath($data); |
|
136 | - if ($result === false || $result === null) { |
|
137 | - $img->loadFromData($data); |
|
138 | - } else { |
|
139 | - $img->loadFromFile($data); |
|
140 | - } |
|
141 | - } catch (\Error $e) { |
|
142 | - $img->loadFromData($data); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - return $img; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Returns the avatar image type. |
|
151 | - * |
|
152 | - * @param IImage $avatar |
|
153 | - * @return string |
|
154 | - */ |
|
155 | - private function getAvatarImageType(IImage $avatar) { |
|
156 | - $type = substr($avatar->mimeType(), -3); |
|
157 | - if ($type === 'peg') { |
|
158 | - $type = 'jpg'; |
|
159 | - } |
|
160 | - return $type; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Validates an avatar image: |
|
165 | - * - must be "png" or "jpg" |
|
166 | - * - must be "valid" |
|
167 | - * - must be in square format |
|
168 | - * |
|
169 | - * @param IImage $avatar The avatar to validate |
|
170 | - * @throws \Exception if the provided file is not a jpg or png image |
|
171 | - * @throws \Exception if the provided image is not valid |
|
172 | - * @throws NotSquareException if the image is not square |
|
173 | - */ |
|
174 | - private function validateAvatar(IImage $avatar) { |
|
175 | - $type = $this->getAvatarImageType($avatar); |
|
176 | - |
|
177 | - if ($type !== 'jpg' && $type !== 'png') { |
|
178 | - throw new \Exception($this->l->t('Unknown filetype')); |
|
179 | - } |
|
180 | - |
|
181 | - if (!$avatar->valid()) { |
|
182 | - throw new \Exception($this->l->t('Invalid image')); |
|
183 | - } |
|
184 | - |
|
185 | - if (!($avatar->height() === $avatar->width())) { |
|
186 | - throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
187 | - } |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Removes the users avatar. |
|
192 | - * @return void |
|
193 | - * @throws \OCP\Files\NotPermittedException |
|
194 | - * @throws \OCP\PreConditionNotMetException |
|
195 | - */ |
|
196 | - public function remove() { |
|
197 | - $avatars = $this->folder->getDirectoryListing(); |
|
198 | - |
|
199 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
200 | - (int) $this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
201 | - |
|
202 | - foreach ($avatars as $avatar) { |
|
203 | - $avatar->delete(); |
|
204 | - } |
|
205 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
206 | - $this->user->triggerChange('avatar', ''); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
211 | - * |
|
212 | - * @return string |
|
213 | - * @throws NotFoundException |
|
214 | - */ |
|
215 | - private function getExtension() { |
|
216 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
217 | - return 'jpg'; |
|
218 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
219 | - return 'png'; |
|
220 | - } |
|
221 | - throw new NotFoundException; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Returns the avatar for an user. |
|
226 | - * |
|
227 | - * If there is no avatar file yet, one is generated. |
|
228 | - * |
|
229 | - * @param int $size |
|
230 | - * @return ISimpleFile |
|
231 | - * @throws NotFoundException |
|
232 | - * @throws \OCP\Files\NotPermittedException |
|
233 | - * @throws \OCP\PreConditionNotMetException |
|
234 | - */ |
|
235 | - public function getFile($size) { |
|
236 | - $size = (int) $size; |
|
237 | - |
|
238 | - try { |
|
239 | - $ext = $this->getExtension(); |
|
240 | - } catch (NotFoundException $e) { |
|
241 | - if (!$data = $this->generateAvatarFromSvg(1024)) { |
|
242 | - $data = $this->generateAvatar($this->getDisplayName(), 1024); |
|
243 | - } |
|
244 | - $avatar = $this->folder->newFile('avatar.png'); |
|
245 | - $avatar->putContent($data); |
|
246 | - $ext = 'png'; |
|
247 | - |
|
248 | - $this->folder->newFile('generated'); |
|
249 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
250 | - } |
|
251 | - |
|
252 | - if ($size === -1) { |
|
253 | - $path = 'avatar.' . $ext; |
|
254 | - } else { |
|
255 | - $path = 'avatar.' . $size . '.' . $ext; |
|
256 | - } |
|
257 | - |
|
258 | - try { |
|
259 | - $file = $this->folder->getFile($path); |
|
260 | - } catch (NotFoundException $e) { |
|
261 | - if ($size <= 0) { |
|
262 | - throw new NotFoundException; |
|
263 | - } |
|
264 | - |
|
265 | - if ($this->folder->fileExists('generated')) { |
|
266 | - if (!$data = $this->generateAvatarFromSvg($size)) { |
|
267 | - $data = $this->generateAvatar($this->getDisplayName(), $size); |
|
268 | - } |
|
269 | - |
|
270 | - } else { |
|
271 | - $avatar = new OC_Image(); |
|
272 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
273 | - $avatar->loadFromData($file->getContent()); |
|
274 | - $avatar->resize($size); |
|
275 | - $data = $avatar->data(); |
|
276 | - } |
|
277 | - |
|
278 | - try { |
|
279 | - $file = $this->folder->newFile($path); |
|
280 | - $file->putContent($data); |
|
281 | - } catch (NotPermittedException $e) { |
|
282 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
283 | - throw new NotFoundException(); |
|
284 | - } |
|
285 | - |
|
286 | - } |
|
287 | - |
|
288 | - if ($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
289 | - $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
290 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
291 | - } |
|
292 | - |
|
293 | - return $file; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Returns the user display name. |
|
298 | - * |
|
299 | - * @return string |
|
300 | - */ |
|
301 | - public function getDisplayName(): string { |
|
302 | - return $this->user->getDisplayName(); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Handles user changes. |
|
307 | - * |
|
308 | - * @param string $feature The changed feature |
|
309 | - * @param mixed $oldValue The previous value |
|
310 | - * @param mixed $newValue The new value |
|
311 | - * @throws NotPermittedException |
|
312 | - * @throws \OCP\PreConditionNotMetException |
|
313 | - */ |
|
314 | - public function userChanged($feature, $oldValue, $newValue) { |
|
315 | - // We only change the avatar on display name changes |
|
316 | - if ($feature !== 'displayName') { |
|
317 | - return; |
|
318 | - } |
|
319 | - |
|
320 | - // If the avatar is not generated (so an uploaded image) we skip this |
|
321 | - if (!$this->folder->fileExists('generated')) { |
|
322 | - return; |
|
323 | - } |
|
324 | - |
|
325 | - $this->remove(); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Check if the avatar of a user is a custom uploaded one |
|
330 | - * |
|
331 | - * @return bool |
|
332 | - */ |
|
333 | - public function isCustomAvatar(): bool { |
|
334 | - return $this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', 'false') !== 'true'; |
|
335 | - } |
|
42 | + /** @var IConfig */ |
|
43 | + private $config; |
|
44 | + |
|
45 | + /** @var ISimpleFolder */ |
|
46 | + private $folder; |
|
47 | + |
|
48 | + /** @var IL10N */ |
|
49 | + private $l; |
|
50 | + |
|
51 | + /** @var User */ |
|
52 | + private $user; |
|
53 | + |
|
54 | + /** |
|
55 | + * UserAvatar constructor. |
|
56 | + * |
|
57 | + * @param IConfig $config The configuration |
|
58 | + * @param ISimpleFolder $folder The avatar files folder |
|
59 | + * @param IL10N $l The localization helper |
|
60 | + * @param User $user The user this class manages the avatar for |
|
61 | + * @param ILogger $logger The logger |
|
62 | + */ |
|
63 | + public function __construct( |
|
64 | + ISimpleFolder $folder, |
|
65 | + IL10N $l, |
|
66 | + $user, |
|
67 | + ILogger $logger, |
|
68 | + IConfig $config) { |
|
69 | + parent::__construct($logger); |
|
70 | + $this->folder = $folder; |
|
71 | + $this->l = $l; |
|
72 | + $this->user = $user; |
|
73 | + $this->config = $config; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Check if an avatar exists for the user |
|
78 | + * |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | + public function exists() { |
|
82 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Sets the users avatar. |
|
87 | + * |
|
88 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
89 | + * @throws \Exception if the provided file is not a jpg or png image |
|
90 | + * @throws \Exception if the provided image is not valid |
|
91 | + * @throws NotSquareException if the image is not square |
|
92 | + * @return void |
|
93 | + */ |
|
94 | + public function set($data) { |
|
95 | + $img = $this->getAvatarImage($data); |
|
96 | + $data = $img->data(); |
|
97 | + |
|
98 | + $this->validateAvatar($img); |
|
99 | + |
|
100 | + $this->remove(); |
|
101 | + $type = $this->getAvatarImageType($img); |
|
102 | + $file = $this->folder->newFile('avatar.' . $type); |
|
103 | + $file->putContent($data); |
|
104 | + |
|
105 | + try { |
|
106 | + $generated = $this->folder->getFile('generated'); |
|
107 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'false'); |
|
108 | + $generated->delete(); |
|
109 | + } catch (NotFoundException $e) { |
|
110 | + // |
|
111 | + } |
|
112 | + |
|
113 | + $this->user->triggerChange('avatar', $file); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns an image from several sources. |
|
118 | + * |
|
119 | + * @param IImage|resource|string $data An image object, imagedata or path to the avatar |
|
120 | + * @return IImage |
|
121 | + */ |
|
122 | + private function getAvatarImage($data) { |
|
123 | + if ($data instanceof IImage) { |
|
124 | + return $data; |
|
125 | + } |
|
126 | + |
|
127 | + $img = new OC_Image(); |
|
128 | + if (is_resource($data) && get_resource_type($data) === 'gd') { |
|
129 | + $img->setResource($data); |
|
130 | + } elseif (is_resource($data)) { |
|
131 | + $img->loadFromFileHandle($data); |
|
132 | + } else { |
|
133 | + try { |
|
134 | + // detect if it is a path or maybe the images as string |
|
135 | + $result = @realpath($data); |
|
136 | + if ($result === false || $result === null) { |
|
137 | + $img->loadFromData($data); |
|
138 | + } else { |
|
139 | + $img->loadFromFile($data); |
|
140 | + } |
|
141 | + } catch (\Error $e) { |
|
142 | + $img->loadFromData($data); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + return $img; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Returns the avatar image type. |
|
151 | + * |
|
152 | + * @param IImage $avatar |
|
153 | + * @return string |
|
154 | + */ |
|
155 | + private function getAvatarImageType(IImage $avatar) { |
|
156 | + $type = substr($avatar->mimeType(), -3); |
|
157 | + if ($type === 'peg') { |
|
158 | + $type = 'jpg'; |
|
159 | + } |
|
160 | + return $type; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Validates an avatar image: |
|
165 | + * - must be "png" or "jpg" |
|
166 | + * - must be "valid" |
|
167 | + * - must be in square format |
|
168 | + * |
|
169 | + * @param IImage $avatar The avatar to validate |
|
170 | + * @throws \Exception if the provided file is not a jpg or png image |
|
171 | + * @throws \Exception if the provided image is not valid |
|
172 | + * @throws NotSquareException if the image is not square |
|
173 | + */ |
|
174 | + private function validateAvatar(IImage $avatar) { |
|
175 | + $type = $this->getAvatarImageType($avatar); |
|
176 | + |
|
177 | + if ($type !== 'jpg' && $type !== 'png') { |
|
178 | + throw new \Exception($this->l->t('Unknown filetype')); |
|
179 | + } |
|
180 | + |
|
181 | + if (!$avatar->valid()) { |
|
182 | + throw new \Exception($this->l->t('Invalid image')); |
|
183 | + } |
|
184 | + |
|
185 | + if (!($avatar->height() === $avatar->width())) { |
|
186 | + throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
187 | + } |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Removes the users avatar. |
|
192 | + * @return void |
|
193 | + * @throws \OCP\Files\NotPermittedException |
|
194 | + * @throws \OCP\PreConditionNotMetException |
|
195 | + */ |
|
196 | + public function remove() { |
|
197 | + $avatars = $this->folder->getDirectoryListing(); |
|
198 | + |
|
199 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
200 | + (int) $this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
201 | + |
|
202 | + foreach ($avatars as $avatar) { |
|
203 | + $avatar->delete(); |
|
204 | + } |
|
205 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
206 | + $this->user->triggerChange('avatar', ''); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
211 | + * |
|
212 | + * @return string |
|
213 | + * @throws NotFoundException |
|
214 | + */ |
|
215 | + private function getExtension() { |
|
216 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
217 | + return 'jpg'; |
|
218 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
219 | + return 'png'; |
|
220 | + } |
|
221 | + throw new NotFoundException; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Returns the avatar for an user. |
|
226 | + * |
|
227 | + * If there is no avatar file yet, one is generated. |
|
228 | + * |
|
229 | + * @param int $size |
|
230 | + * @return ISimpleFile |
|
231 | + * @throws NotFoundException |
|
232 | + * @throws \OCP\Files\NotPermittedException |
|
233 | + * @throws \OCP\PreConditionNotMetException |
|
234 | + */ |
|
235 | + public function getFile($size) { |
|
236 | + $size = (int) $size; |
|
237 | + |
|
238 | + try { |
|
239 | + $ext = $this->getExtension(); |
|
240 | + } catch (NotFoundException $e) { |
|
241 | + if (!$data = $this->generateAvatarFromSvg(1024)) { |
|
242 | + $data = $this->generateAvatar($this->getDisplayName(), 1024); |
|
243 | + } |
|
244 | + $avatar = $this->folder->newFile('avatar.png'); |
|
245 | + $avatar->putContent($data); |
|
246 | + $ext = 'png'; |
|
247 | + |
|
248 | + $this->folder->newFile('generated'); |
|
249 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', 'true'); |
|
250 | + } |
|
251 | + |
|
252 | + if ($size === -1) { |
|
253 | + $path = 'avatar.' . $ext; |
|
254 | + } else { |
|
255 | + $path = 'avatar.' . $size . '.' . $ext; |
|
256 | + } |
|
257 | + |
|
258 | + try { |
|
259 | + $file = $this->folder->getFile($path); |
|
260 | + } catch (NotFoundException $e) { |
|
261 | + if ($size <= 0) { |
|
262 | + throw new NotFoundException; |
|
263 | + } |
|
264 | + |
|
265 | + if ($this->folder->fileExists('generated')) { |
|
266 | + if (!$data = $this->generateAvatarFromSvg($size)) { |
|
267 | + $data = $this->generateAvatar($this->getDisplayName(), $size); |
|
268 | + } |
|
269 | + |
|
270 | + } else { |
|
271 | + $avatar = new OC_Image(); |
|
272 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
273 | + $avatar->loadFromData($file->getContent()); |
|
274 | + $avatar->resize($size); |
|
275 | + $data = $avatar->data(); |
|
276 | + } |
|
277 | + |
|
278 | + try { |
|
279 | + $file = $this->folder->newFile($path); |
|
280 | + $file->putContent($data); |
|
281 | + } catch (NotPermittedException $e) { |
|
282 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
283 | + throw new NotFoundException(); |
|
284 | + } |
|
285 | + |
|
286 | + } |
|
287 | + |
|
288 | + if ($this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', null) === null) { |
|
289 | + $generated = $this->folder->fileExists('generated') ? 'true' : 'false'; |
|
290 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'generated', $generated); |
|
291 | + } |
|
292 | + |
|
293 | + return $file; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Returns the user display name. |
|
298 | + * |
|
299 | + * @return string |
|
300 | + */ |
|
301 | + public function getDisplayName(): string { |
|
302 | + return $this->user->getDisplayName(); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Handles user changes. |
|
307 | + * |
|
308 | + * @param string $feature The changed feature |
|
309 | + * @param mixed $oldValue The previous value |
|
310 | + * @param mixed $newValue The new value |
|
311 | + * @throws NotPermittedException |
|
312 | + * @throws \OCP\PreConditionNotMetException |
|
313 | + */ |
|
314 | + public function userChanged($feature, $oldValue, $newValue) { |
|
315 | + // We only change the avatar on display name changes |
|
316 | + if ($feature !== 'displayName') { |
|
317 | + return; |
|
318 | + } |
|
319 | + |
|
320 | + // If the avatar is not generated (so an uploaded image) we skip this |
|
321 | + if (!$this->folder->fileExists('generated')) { |
|
322 | + return; |
|
323 | + } |
|
324 | + |
|
325 | + $this->remove(); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Check if the avatar of a user is a custom uploaded one |
|
330 | + * |
|
331 | + * @return bool |
|
332 | + */ |
|
333 | + public function isCustomAvatar(): bool { |
|
334 | + return $this->config->getUserValue($this->user->getUID(), 'avatar', 'generated', 'false') !== 'true'; |
|
335 | + } |
|
336 | 336 | } |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | |
100 | 100 | $this->remove(); |
101 | 101 | $type = $this->getAvatarImageType($img); |
102 | - $file = $this->folder->newFile('avatar.' . $type); |
|
102 | + $file = $this->folder->newFile('avatar.'.$type); |
|
103 | 103 | $file->putContent($data); |
104 | 104 | |
105 | 105 | try { |
@@ -250,9 +250,9 @@ discard block |
||
250 | 250 | } |
251 | 251 | |
252 | 252 | if ($size === -1) { |
253 | - $path = 'avatar.' . $ext; |
|
253 | + $path = 'avatar.'.$ext; |
|
254 | 254 | } else { |
255 | - $path = 'avatar.' . $size . '.' . $ext; |
|
255 | + $path = 'avatar.'.$size.'.'.$ext; |
|
256 | 256 | } |
257 | 257 | |
258 | 258 | try { |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | |
270 | 270 | } else { |
271 | 271 | $avatar = new OC_Image(); |
272 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
272 | + $file = $this->folder->getFile('avatar.'.$ext); |
|
273 | 273 | $avatar->loadFromData($file->getContent()); |
274 | 274 | $avatar->resize($size); |
275 | 275 | $data = $avatar->data(); |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | $file = $this->folder->newFile($path); |
280 | 280 | $file->putContent($data); |
281 | 281 | } catch (NotPermittedException $e) { |
282 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
282 | + $this->logger->error('Failed to save avatar for '.$this->user->getUID()); |
|
283 | 283 | throw new NotFoundException(); |
284 | 284 | } |
285 | 285 |
@@ -30,90 +30,90 @@ |
||
30 | 30 | * This class represents a guest user's avatar. |
31 | 31 | */ |
32 | 32 | class GuestAvatar extends Avatar { |
33 | - /** |
|
34 | - * Holds the guest user display name. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $userDisplayName; |
|
33 | + /** |
|
34 | + * Holds the guest user display name. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $userDisplayName; |
|
39 | 39 | |
40 | - /** |
|
41 | - * GuestAvatar constructor. |
|
42 | - * |
|
43 | - * @param string $userDisplayName The guest user display name |
|
44 | - * @param ILogger $logger The logger |
|
45 | - */ |
|
46 | - public function __construct(string $userDisplayName, ILogger $logger) { |
|
47 | - parent::__construct($logger); |
|
48 | - $this->userDisplayName = $userDisplayName; |
|
49 | - } |
|
40 | + /** |
|
41 | + * GuestAvatar constructor. |
|
42 | + * |
|
43 | + * @param string $userDisplayName The guest user display name |
|
44 | + * @param ILogger $logger The logger |
|
45 | + */ |
|
46 | + public function __construct(string $userDisplayName, ILogger $logger) { |
|
47 | + parent::__construct($logger); |
|
48 | + $this->userDisplayName = $userDisplayName; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Tests if the user has an avatar. |
|
53 | - * |
|
54 | - * @return true Guests always have an avatar. |
|
55 | - */ |
|
56 | - public function exists() { |
|
57 | - return true; |
|
58 | - } |
|
51 | + /** |
|
52 | + * Tests if the user has an avatar. |
|
53 | + * |
|
54 | + * @return true Guests always have an avatar. |
|
55 | + */ |
|
56 | + public function exists() { |
|
57 | + return true; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Returns the guest user display name. |
|
62 | - * |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function getDisplayName(): string { |
|
66 | - return $this->userDisplayName; |
|
67 | - } |
|
60 | + /** |
|
61 | + * Returns the guest user display name. |
|
62 | + * |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function getDisplayName(): string { |
|
66 | + return $this->userDisplayName; |
|
67 | + } |
|
68 | 68 | |
69 | - /** |
|
70 | - * Setting avatars isn't implemented for guests. |
|
71 | - * |
|
72 | - * @param \OCP\IImage|resource|string $data |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function set($data) { |
|
76 | - // unimplemented for guest user avatars |
|
77 | - } |
|
69 | + /** |
|
70 | + * Setting avatars isn't implemented for guests. |
|
71 | + * |
|
72 | + * @param \OCP\IImage|resource|string $data |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function set($data) { |
|
76 | + // unimplemented for guest user avatars |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Removing avatars isn't implemented for guests. |
|
81 | - */ |
|
82 | - public function remove() { |
|
83 | - // unimplemented for guest user avatars |
|
84 | - } |
|
79 | + /** |
|
80 | + * Removing avatars isn't implemented for guests. |
|
81 | + */ |
|
82 | + public function remove() { |
|
83 | + // unimplemented for guest user avatars |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Generates an avatar for the guest. |
|
88 | - * |
|
89 | - * @param int $size The desired image size. |
|
90 | - * @return InMemoryFile |
|
91 | - */ |
|
92 | - public function getFile($size) { |
|
93 | - $avatar = $this->generateAvatar($this->userDisplayName, $size); |
|
94 | - return new InMemoryFile('avatar.png', $avatar); |
|
95 | - } |
|
86 | + /** |
|
87 | + * Generates an avatar for the guest. |
|
88 | + * |
|
89 | + * @param int $size The desired image size. |
|
90 | + * @return InMemoryFile |
|
91 | + */ |
|
92 | + public function getFile($size) { |
|
93 | + $avatar = $this->generateAvatar($this->userDisplayName, $size); |
|
94 | + return new InMemoryFile('avatar.png', $avatar); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Updates the display name if changed. |
|
99 | - * |
|
100 | - * @param string $feature The changed feature |
|
101 | - * @param mixed $oldValue The previous value |
|
102 | - * @param mixed $newValue The new value |
|
103 | - * @return void |
|
104 | - */ |
|
105 | - public function userChanged($feature, $oldValue, $newValue) { |
|
106 | - if ($feature === 'displayName') { |
|
107 | - $this->userDisplayName = $newValue; |
|
108 | - } |
|
109 | - } |
|
97 | + /** |
|
98 | + * Updates the display name if changed. |
|
99 | + * |
|
100 | + * @param string $feature The changed feature |
|
101 | + * @param mixed $oldValue The previous value |
|
102 | + * @param mixed $newValue The new value |
|
103 | + * @return void |
|
104 | + */ |
|
105 | + public function userChanged($feature, $oldValue, $newValue) { |
|
106 | + if ($feature === 'displayName') { |
|
107 | + $this->userDisplayName = $newValue; |
|
108 | + } |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Guests don't have custom avatars. |
|
113 | - * |
|
114 | - * @return bool |
|
115 | - */ |
|
116 | - public function isCustomAvatar(): bool { |
|
117 | - return false; |
|
118 | - } |
|
111 | + /** |
|
112 | + * Guests don't have custom avatars. |
|
113 | + * |
|
114 | + * @return bool |
|
115 | + */ |
|
116 | + public function isCustomAvatar(): bool { |
|
117 | + return false; |
|
118 | + } |
|
119 | 119 | } |
@@ -42,92 +42,92 @@ |
||
42 | 42 | */ |
43 | 43 | class AvatarManager implements IAvatarManager { |
44 | 44 | |
45 | - /** @var Manager */ |
|
46 | - private $userManager; |
|
45 | + /** @var Manager */ |
|
46 | + private $userManager; |
|
47 | 47 | |
48 | - /** @var IAppData */ |
|
49 | - private $appData; |
|
48 | + /** @var IAppData */ |
|
49 | + private $appData; |
|
50 | 50 | |
51 | - /** @var IL10N */ |
|
52 | - private $l; |
|
51 | + /** @var IL10N */ |
|
52 | + private $l; |
|
53 | 53 | |
54 | - /** @var ILogger */ |
|
55 | - private $logger; |
|
54 | + /** @var ILogger */ |
|
55 | + private $logger; |
|
56 | 56 | |
57 | - /** @var IConfig */ |
|
58 | - private $config; |
|
57 | + /** @var IConfig */ |
|
58 | + private $config; |
|
59 | 59 | |
60 | - /** |
|
61 | - * AvatarManager constructor. |
|
62 | - * |
|
63 | - * @param Manager $userManager |
|
64 | - * @param IAppData $appData |
|
65 | - * @param IL10N $l |
|
66 | - * @param ILogger $logger |
|
67 | - * @param IConfig $config |
|
68 | - */ |
|
69 | - public function __construct( |
|
70 | - Manager $userManager, |
|
71 | - IAppData $appData, |
|
72 | - IL10N $l, |
|
73 | - ILogger $logger, |
|
74 | - IConfig $config) { |
|
75 | - $this->userManager = $userManager; |
|
76 | - $this->appData = $appData; |
|
77 | - $this->l = $l; |
|
78 | - $this->logger = $logger; |
|
79 | - $this->config = $config; |
|
80 | - } |
|
60 | + /** |
|
61 | + * AvatarManager constructor. |
|
62 | + * |
|
63 | + * @param Manager $userManager |
|
64 | + * @param IAppData $appData |
|
65 | + * @param IL10N $l |
|
66 | + * @param ILogger $logger |
|
67 | + * @param IConfig $config |
|
68 | + */ |
|
69 | + public function __construct( |
|
70 | + Manager $userManager, |
|
71 | + IAppData $appData, |
|
72 | + IL10N $l, |
|
73 | + ILogger $logger, |
|
74 | + IConfig $config) { |
|
75 | + $this->userManager = $userManager; |
|
76 | + $this->appData = $appData; |
|
77 | + $this->l = $l; |
|
78 | + $this->logger = $logger; |
|
79 | + $this->config = $config; |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * return a user specific instance of \OCP\IAvatar |
|
84 | - * @see \OCP\IAvatar |
|
85 | - * @param string $userId the ownCloud user id |
|
86 | - * @return \OCP\IAvatar |
|
87 | - * @throws \Exception In case the username is potentially dangerous |
|
88 | - * @throws NotFoundException In case there is no user folder yet |
|
89 | - */ |
|
90 | - public function getAvatar(string $userId) : IAvatar { |
|
91 | - $user = $this->userManager->get($userId); |
|
92 | - if ($user === null) { |
|
93 | - throw new \Exception('user does not exist'); |
|
94 | - } |
|
82 | + /** |
|
83 | + * return a user specific instance of \OCP\IAvatar |
|
84 | + * @see \OCP\IAvatar |
|
85 | + * @param string $userId the ownCloud user id |
|
86 | + * @return \OCP\IAvatar |
|
87 | + * @throws \Exception In case the username is potentially dangerous |
|
88 | + * @throws NotFoundException In case there is no user folder yet |
|
89 | + */ |
|
90 | + public function getAvatar(string $userId) : IAvatar { |
|
91 | + $user = $this->userManager->get($userId); |
|
92 | + if ($user === null) { |
|
93 | + throw new \Exception('user does not exist'); |
|
94 | + } |
|
95 | 95 | |
96 | - // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below) |
|
97 | - $userId = $user->getUID(); |
|
96 | + // sanitize userID - fixes casing issue (needed for the filesystem stuff that is done below) |
|
97 | + $userId = $user->getUID(); |
|
98 | 98 | |
99 | - try { |
|
100 | - $folder = $this->appData->getFolder($userId); |
|
101 | - } catch (NotFoundException $e) { |
|
102 | - $folder = $this->appData->newFolder($userId); |
|
103 | - } |
|
99 | + try { |
|
100 | + $folder = $this->appData->getFolder($userId); |
|
101 | + } catch (NotFoundException $e) { |
|
102 | + $folder = $this->appData->newFolder($userId); |
|
103 | + } |
|
104 | 104 | |
105 | - return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config); |
|
106 | - } |
|
105 | + return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Clear generated avatars |
|
110 | - */ |
|
111 | - public function clearCachedAvatars() { |
|
112 | - $users = $this->config->getUsersForUserValue('avatar', 'generated', 'true'); |
|
113 | - foreach($users as $userId) { |
|
114 | - try { |
|
115 | - $folder = $this->appData->getFolder($userId); |
|
116 | - $folder->delete(); |
|
117 | - } catch (NotFoundException $e) { |
|
118 | - $this->logger->debug("No cache for the user $userId. Ignoring..."); |
|
119 | - } |
|
120 | - $this->config->setUserValue($userId, 'avatar', 'generated', 'false'); |
|
121 | - } |
|
122 | - } |
|
108 | + /** |
|
109 | + * Clear generated avatars |
|
110 | + */ |
|
111 | + public function clearCachedAvatars() { |
|
112 | + $users = $this->config->getUsersForUserValue('avatar', 'generated', 'true'); |
|
113 | + foreach($users as $userId) { |
|
114 | + try { |
|
115 | + $folder = $this->appData->getFolder($userId); |
|
116 | + $folder->delete(); |
|
117 | + } catch (NotFoundException $e) { |
|
118 | + $this->logger->debug("No cache for the user $userId. Ignoring..."); |
|
119 | + } |
|
120 | + $this->config->setUserValue($userId, 'avatar', 'generated', 'false'); |
|
121 | + } |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Returns a GuestAvatar. |
|
126 | - * |
|
127 | - * @param string $name The guest name, e.g. "Albert". |
|
128 | - * @return IAvatar |
|
129 | - */ |
|
130 | - public function getGuestAvatar(string $name): IAvatar { |
|
131 | - return new GuestAvatar($name, $this->logger); |
|
132 | - } |
|
124 | + /** |
|
125 | + * Returns a GuestAvatar. |
|
126 | + * |
|
127 | + * @param string $name The guest name, e.g. "Albert". |
|
128 | + * @return IAvatar |
|
129 | + */ |
|
130 | + public function getGuestAvatar(string $name): IAvatar { |
|
131 | + return new GuestAvatar($name, $this->logger); |
|
132 | + } |
|
133 | 133 | } |
@@ -36,53 +36,53 @@ discard block |
||
36 | 36 | |
37 | 37 | $application = new Application(); |
38 | 38 | $application->registerRoutes($this, [ |
39 | - 'routes' => [ |
|
40 | - ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], |
|
41 | - ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'], |
|
42 | - ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'], |
|
43 | - ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'], |
|
44 | - ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'], |
|
45 | - ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'], |
|
46 | - ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'], |
|
47 | - ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'], |
|
48 | - ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'], |
|
49 | - ['name' => 'GuestAvatar#getAvatar', 'url' => '/avatar/guest/{guestName}/{size}', 'verb' => 'GET'], |
|
50 | - ['name' => 'CSRFToken#index', 'url' => '/csrftoken', 'verb' => 'GET'], |
|
51 | - ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], |
|
52 | - ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'], |
|
53 | - ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], |
|
54 | - ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], |
|
55 | - ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'], |
|
56 | - ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'], |
|
57 | - ['name' => 'ClientFlowLogin#grantPage', 'url' => '/login/flow/grant', 'verb' => 'GET'], |
|
58 | - ['name' => 'ClientFlowLogin#apptokenRedirect', 'url' => '/login/flow/apptoken', 'verb' => 'POST'], |
|
59 | - ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], |
|
60 | - ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], |
|
61 | - ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |
|
62 | - ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'], |
|
63 | - ['name' => 'Preview#getPreviewByFileId', 'url' => '/core/preview', 'verb' => 'GET'], |
|
64 | - ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], |
|
65 | - ['name' => 'Svg#getSvgFromCore', 'url' => '/svg/core/{folder}/{fileName}', 'verb' => 'GET'], |
|
66 | - ['name' => 'Svg#getSvgFromApp', 'url' => '/svg/{app}/{fileName}', 'verb' => 'GET'], |
|
67 | - ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], |
|
68 | - ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], |
|
69 | - ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], |
|
70 | - ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'], |
|
71 | - ['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'], |
|
72 | - ['name' => 'Search#search', 'url' => '/core/search', 'verb' => 'GET'], |
|
73 | - ], |
|
74 | - 'ocs' => [ |
|
75 | - ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'], |
|
76 | - ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'], |
|
77 | - ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'], |
|
78 | - ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'], |
|
79 | - ['root' => '/core', 'name' => 'Navigation#getAppsNavigation', 'url' => '/navigation/apps', 'verb' => 'GET'], |
|
80 | - ['root' => '/core', 'name' => 'Navigation#getSettingsNavigation', 'url' => '/navigation/settings', 'verb' => 'GET'], |
|
81 | - ['root' => '/core', 'name' => 'AutoComplete#get', 'url' => '/autocomplete/get', 'verb' => 'GET'], |
|
82 | - ['root' => '/core', 'name' => 'WhatsNew#get', 'url' => '/whatsnew', 'verb' => 'GET'], |
|
83 | - ['root' => '/core', 'name' => 'WhatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'], |
|
84 | - ['root' => '/core', 'name' => 'AppPassword#getAppPassword', 'url' => '/getapppassword', 'verb' => 'GET'], |
|
85 | - ], |
|
39 | + 'routes' => [ |
|
40 | + ['name' => 'lost#email', 'url' => '/lostpassword/email', 'verb' => 'POST'], |
|
41 | + ['name' => 'lost#resetform', 'url' => '/lostpassword/reset/form/{token}/{userId}', 'verb' => 'GET'], |
|
42 | + ['name' => 'lost#setPassword', 'url' => '/lostpassword/set/{token}/{userId}', 'verb' => 'POST'], |
|
43 | + ['name' => 'user#getDisplayNames', 'url' => '/displaynames', 'verb' => 'POST'], |
|
44 | + ['name' => 'avatar#getAvatar', 'url' => '/avatar/{userId}/{size}', 'verb' => 'GET'], |
|
45 | + ['name' => 'avatar#deleteAvatar', 'url' => '/avatar/', 'verb' => 'DELETE'], |
|
46 | + ['name' => 'avatar#postCroppedAvatar', 'url' => '/avatar/cropped', 'verb' => 'POST'], |
|
47 | + ['name' => 'avatar#getTmpAvatar', 'url' => '/avatar/tmp', 'verb' => 'GET'], |
|
48 | + ['name' => 'avatar#postAvatar', 'url' => '/avatar/', 'verb' => 'POST'], |
|
49 | + ['name' => 'GuestAvatar#getAvatar', 'url' => '/avatar/guest/{guestName}/{size}', 'verb' => 'GET'], |
|
50 | + ['name' => 'CSRFToken#index', 'url' => '/csrftoken', 'verb' => 'GET'], |
|
51 | + ['name' => 'login#tryLogin', 'url' => '/login', 'verb' => 'POST'], |
|
52 | + ['name' => 'login#confirmPassword', 'url' => '/login/confirm', 'verb' => 'POST'], |
|
53 | + ['name' => 'login#showLoginForm', 'url' => '/login', 'verb' => 'GET'], |
|
54 | + ['name' => 'login#logout', 'url' => '/logout', 'verb' => 'GET'], |
|
55 | + ['name' => 'ClientFlowLogin#showAuthPickerPage', 'url' => '/login/flow', 'verb' => 'GET'], |
|
56 | + ['name' => 'ClientFlowLogin#generateAppPassword', 'url' => '/login/flow', 'verb' => 'POST'], |
|
57 | + ['name' => 'ClientFlowLogin#grantPage', 'url' => '/login/flow/grant', 'verb' => 'GET'], |
|
58 | + ['name' => 'ClientFlowLogin#apptokenRedirect', 'url' => '/login/flow/apptoken', 'verb' => 'POST'], |
|
59 | + ['name' => 'TwoFactorChallenge#selectChallenge', 'url' => '/login/selectchallenge', 'verb' => 'GET'], |
|
60 | + ['name' => 'TwoFactorChallenge#showChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'GET'], |
|
61 | + ['name' => 'TwoFactorChallenge#solveChallenge', 'url' => '/login/challenge/{challengeProviderId}', 'verb' => 'POST'], |
|
62 | + ['name' => 'OCJS#getConfig', 'url' => '/core/js/oc.js', 'verb' => 'GET'], |
|
63 | + ['name' => 'Preview#getPreviewByFileId', 'url' => '/core/preview', 'verb' => 'GET'], |
|
64 | + ['name' => 'Preview#getPreview', 'url' => '/core/preview.png', 'verb' => 'GET'], |
|
65 | + ['name' => 'Svg#getSvgFromCore', 'url' => '/svg/core/{folder}/{fileName}', 'verb' => 'GET'], |
|
66 | + ['name' => 'Svg#getSvgFromApp', 'url' => '/svg/{app}/{fileName}', 'verb' => 'GET'], |
|
67 | + ['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'], |
|
68 | + ['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'], |
|
69 | + ['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'], |
|
70 | + ['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'], |
|
71 | + ['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'], |
|
72 | + ['name' => 'Search#search', 'url' => '/core/search', 'verb' => 'GET'], |
|
73 | + ], |
|
74 | + 'ocs' => [ |
|
75 | + ['root' => '/cloud', 'name' => 'OCS#getCapabilities', 'url' => '/capabilities', 'verb' => 'GET'], |
|
76 | + ['root' => '', 'name' => 'OCS#getConfig', 'url' => '/config', 'verb' => 'GET'], |
|
77 | + ['root' => '/person', 'name' => 'OCS#personCheck', 'url' => '/check', 'verb' => 'POST'], |
|
78 | + ['root' => '/identityproof', 'name' => 'OCS#getIdentityProof', 'url' => '/key/{cloudId}', 'verb' => 'GET'], |
|
79 | + ['root' => '/core', 'name' => 'Navigation#getAppsNavigation', 'url' => '/navigation/apps', 'verb' => 'GET'], |
|
80 | + ['root' => '/core', 'name' => 'Navigation#getSettingsNavigation', 'url' => '/navigation/settings', 'verb' => 'GET'], |
|
81 | + ['root' => '/core', 'name' => 'AutoComplete#get', 'url' => '/autocomplete/get', 'verb' => 'GET'], |
|
82 | + ['root' => '/core', 'name' => 'WhatsNew#get', 'url' => '/whatsnew', 'verb' => 'GET'], |
|
83 | + ['root' => '/core', 'name' => 'WhatsNew#dismiss', 'url' => '/whatsnew', 'verb' => 'POST'], |
|
84 | + ['root' => '/core', 'name' => 'AppPassword#getAppPassword', 'url' => '/getapppassword', 'verb' => 'GET'], |
|
85 | + ], |
|
86 | 86 | ]); |
87 | 87 | |
88 | 88 | // Post installation check |
@@ -91,12 +91,12 @@ discard block |
||
91 | 91 | // Core ajax actions |
92 | 92 | // Routing |
93 | 93 | $this->create('core_ajax_update', '/core/ajax/update.php') |
94 | - ->actionInclude('core/ajax/update.php'); |
|
94 | + ->actionInclude('core/ajax/update.php'); |
|
95 | 95 | |
96 | 96 | // File routes |
97 | 97 | $this->create('files.viewcontroller.showFile', '/f/{fileid}')->action(function($urlParams) { |
98 | - $app = new \OCA\Files\AppInfo\Application($urlParams); |
|
99 | - $app->dispatch('ViewController', 'index'); |
|
98 | + $app = new \OCA\Files\AppInfo\Application($urlParams); |
|
99 | + $app->dispatch('ViewController', 'index'); |
|
100 | 100 | }); |
101 | 101 | |
102 | 102 | // Call routes |
@@ -105,12 +105,12 @@ discard block |
||
105 | 105 | * @suppress PhanUndeclaredClassMethod |
106 | 106 | */ |
107 | 107 | $this->create('spreed.pagecontroller.showCall', '/call/{token}')->action(function($urlParams) { |
108 | - if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) { |
|
109 | - $app = new \OCA\Spreed\AppInfo\Application($urlParams); |
|
110 | - $app->dispatch('PageController', 'index'); |
|
111 | - } else { |
|
112 | - throw new \OC\HintException('App spreed is not enabled'); |
|
113 | - } |
|
108 | + if (class_exists(\OCA\Spreed\AppInfo\Application::class, false)) { |
|
109 | + $app = new \OCA\Spreed\AppInfo\Application($urlParams); |
|
110 | + $app->dispatch('PageController', 'index'); |
|
111 | + } else { |
|
112 | + throw new \OC\HintException('App spreed is not enabled'); |
|
113 | + } |
|
114 | 114 | }); |
115 | 115 | |
116 | 116 | // OCM routes |
@@ -119,12 +119,12 @@ discard block |
||
119 | 119 | * @suppress PhanUndeclaredClassMethod |
120 | 120 | */ |
121 | 121 | $this->create('cloud_federation_api.requesthandlercontroller.addShare', '/ocm/shares')->post()->action(function($urlParams) { |
122 | - if (class_exists(\OCA\CloudFederationAPI\AppInfo\Application::class, false)) { |
|
123 | - $app = new \OCA\CloudFederationAPI\AppInfo\Application($urlParams); |
|
124 | - $app->dispatch('RequestHandlerController', 'addShare'); |
|
125 | - } else { |
|
126 | - throw new \OC\HintException('Cloud Federation API not enabled'); |
|
127 | - } |
|
122 | + if (class_exists(\OCA\CloudFederationAPI\AppInfo\Application::class, false)) { |
|
123 | + $app = new \OCA\CloudFederationAPI\AppInfo\Application($urlParams); |
|
124 | + $app->dispatch('RequestHandlerController', 'addShare'); |
|
125 | + } else { |
|
126 | + throw new \OC\HintException('Cloud Federation API not enabled'); |
|
127 | + } |
|
128 | 128 | }); |
129 | 129 | |
130 | 130 | /** |
@@ -132,53 +132,53 @@ discard block |
||
132 | 132 | * @suppress PhanUndeclaredClassMethod |
133 | 133 | */ |
134 | 134 | $this->create('cloud_federation_api.requesthandlercontroller.receiveNotification', '/ocm/notifications')->post()->action(function($urlParams) { |
135 | - if (class_exists(\OCA\CloudFederationAPI\AppInfo\Application::class, false)) { |
|
136 | - $app = new \OCA\CloudFederationAPI\AppInfo\Application($urlParams); |
|
137 | - $app->dispatch('RequestHandlerController', 'receiveNotification'); |
|
138 | - } else { |
|
139 | - throw new \OC\HintException('Cloud Federation API not enabled'); |
|
140 | - } |
|
135 | + if (class_exists(\OCA\CloudFederationAPI\AppInfo\Application::class, false)) { |
|
136 | + $app = new \OCA\CloudFederationAPI\AppInfo\Application($urlParams); |
|
137 | + $app->dispatch('RequestHandlerController', 'receiveNotification'); |
|
138 | + } else { |
|
139 | + throw new \OC\HintException('Cloud Federation API not enabled'); |
|
140 | + } |
|
141 | 141 | }); |
142 | 142 | |
143 | 143 | |
144 | 144 | // Sharing routes |
145 | 145 | $this->create('files_sharing.sharecontroller.showShare', '/s/{token}')->action(function($urlParams) { |
146 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
147 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
148 | - $app->dispatch('ShareController', 'showShare'); |
|
149 | - } else { |
|
150 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
151 | - } |
|
146 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
147 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
148 | + $app->dispatch('ShareController', 'showShare'); |
|
149 | + } else { |
|
150 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
151 | + } |
|
152 | 152 | }); |
153 | 153 | $this->create('files_sharing.sharecontroller.authenticate', '/s/{token}/authenticate/{redirect}')->post()->action(function($urlParams) { |
154 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
155 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
156 | - $app->dispatch('ShareController', 'authenticate'); |
|
157 | - } else { |
|
158 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
159 | - } |
|
154 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
155 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
156 | + $app->dispatch('ShareController', 'authenticate'); |
|
157 | + } else { |
|
158 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
159 | + } |
|
160 | 160 | }); |
161 | 161 | $this->create('files_sharing.sharecontroller.showAuthenticate', '/s/{token}/authenticate/{redirect}')->get()->action(function($urlParams) { |
162 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
163 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
164 | - $app->dispatch('ShareController', 'showAuthenticate'); |
|
165 | - } else { |
|
166 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
167 | - } |
|
162 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
163 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
164 | + $app->dispatch('ShareController', 'showAuthenticate'); |
|
165 | + } else { |
|
166 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
167 | + } |
|
168 | 168 | }); |
169 | 169 | $this->create('files_sharing.sharecontroller.downloadShare', '/s/{token}/download')->get()->action(function($urlParams) { |
170 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
171 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
172 | - $app->dispatch('ShareController', 'downloadShare'); |
|
173 | - } else { |
|
174 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
175 | - } |
|
170 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
171 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
172 | + $app->dispatch('ShareController', 'downloadShare'); |
|
173 | + } else { |
|
174 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
175 | + } |
|
176 | 176 | }); |
177 | 177 | $this->create('files_sharing.publicpreview.directLink', '/s/{token}/preview')->get()->action(function($urlParams) { |
178 | - if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
179 | - $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
180 | - $app->dispatch('PublicPreviewController', 'directLink'); |
|
181 | - } else { |
|
182 | - throw new \OC\HintException('App file sharing is not enabled'); |
|
183 | - } |
|
178 | + if (class_exists(\OCA\Files_Sharing\AppInfo\Application::class, false)) { |
|
179 | + $app = new \OCA\Files_Sharing\AppInfo\Application($urlParams); |
|
180 | + $app->dispatch('PublicPreviewController', 'directLink'); |
|
181 | + } else { |
|
182 | + throw new \OC\HintException('App file sharing is not enabled'); |
|
183 | + } |
|
184 | 184 | }); |
@@ -33,75 +33,75 @@ |
||
33 | 33 | */ |
34 | 34 | class GuestAvatarController extends Controller { |
35 | 35 | |
36 | - /** |
|
37 | - * @var ILogger |
|
38 | - */ |
|
39 | - private $logger; |
|
36 | + /** |
|
37 | + * @var ILogger |
|
38 | + */ |
|
39 | + private $logger; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @var IAvatarManager |
|
43 | - */ |
|
44 | - private $avatarManager; |
|
41 | + /** |
|
42 | + * @var IAvatarManager |
|
43 | + */ |
|
44 | + private $avatarManager; |
|
45 | 45 | |
46 | - /** |
|
47 | - * GuestAvatarController constructor. |
|
48 | - * |
|
49 | - * @param $appName |
|
50 | - * @param IRequest $request |
|
51 | - * @param IAvatarManager $avatarManager |
|
52 | - * @param ILogger $logger |
|
53 | - */ |
|
54 | - public function __construct( |
|
55 | - $appName, |
|
56 | - IRequest $request, |
|
57 | - IAvatarManager $avatarManager, |
|
58 | - ILogger $logger |
|
59 | - ) { |
|
60 | - parent::__construct($appName, $request); |
|
61 | - $this->avatarManager = $avatarManager; |
|
62 | - $this->logger = $logger; |
|
63 | - } |
|
46 | + /** |
|
47 | + * GuestAvatarController constructor. |
|
48 | + * |
|
49 | + * @param $appName |
|
50 | + * @param IRequest $request |
|
51 | + * @param IAvatarManager $avatarManager |
|
52 | + * @param ILogger $logger |
|
53 | + */ |
|
54 | + public function __construct( |
|
55 | + $appName, |
|
56 | + IRequest $request, |
|
57 | + IAvatarManager $avatarManager, |
|
58 | + ILogger $logger |
|
59 | + ) { |
|
60 | + parent::__construct($appName, $request); |
|
61 | + $this->avatarManager = $avatarManager; |
|
62 | + $this->logger = $logger; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Returns a guest avatar image response. |
|
67 | - * |
|
68 | - * @PublicPage |
|
69 | - * @NoCSRFRequired |
|
70 | - * |
|
71 | - * @param string $guestName The guest name, e.g. "Albert" |
|
72 | - * @param string $size The desired avatar size, e.g. 64 for 64x64px |
|
73 | - * @return FileDisplayResponse|Http\Response |
|
74 | - */ |
|
75 | - public function getAvatar($guestName, $size) { |
|
76 | - $size = (int) $size; |
|
65 | + /** |
|
66 | + * Returns a guest avatar image response. |
|
67 | + * |
|
68 | + * @PublicPage |
|
69 | + * @NoCSRFRequired |
|
70 | + * |
|
71 | + * @param string $guestName The guest name, e.g. "Albert" |
|
72 | + * @param string $size The desired avatar size, e.g. 64 for 64x64px |
|
73 | + * @return FileDisplayResponse|Http\Response |
|
74 | + */ |
|
75 | + public function getAvatar($guestName, $size) { |
|
76 | + $size = (int) $size; |
|
77 | 77 | |
78 | - // min/max size |
|
79 | - if ($size > 2048) { |
|
80 | - $size = 2048; |
|
81 | - } elseif ($size <= 0) { |
|
82 | - $size = 64; |
|
83 | - } |
|
78 | + // min/max size |
|
79 | + if ($size > 2048) { |
|
80 | + $size = 2048; |
|
81 | + } elseif ($size <= 0) { |
|
82 | + $size = 64; |
|
83 | + } |
|
84 | 84 | |
85 | - try { |
|
86 | - $avatar = $this->avatarManager->getGuestAvatar($guestName); |
|
87 | - $avatarFile = $avatar->getFile($size); |
|
85 | + try { |
|
86 | + $avatar = $this->avatarManager->getGuestAvatar($guestName); |
|
87 | + $avatarFile = $avatar->getFile($size); |
|
88 | 88 | |
89 | - $resp = new FileDisplayResponse( |
|
90 | - $avatarFile, |
|
91 | - $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED, |
|
92 | - ['Content-Type' => $avatarFile->getMimeType()] |
|
93 | - ); |
|
94 | - } catch (\Exception $e) { |
|
95 | - $this->logger->error('error while creating guest avatar', [ |
|
96 | - 'err' => $e, |
|
97 | - ]); |
|
98 | - $resp = new Http\Response(); |
|
99 | - $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); |
|
100 | - return $resp; |
|
101 | - } |
|
89 | + $resp = new FileDisplayResponse( |
|
90 | + $avatarFile, |
|
91 | + $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED, |
|
92 | + ['Content-Type' => $avatarFile->getMimeType()] |
|
93 | + ); |
|
94 | + } catch (\Exception $e) { |
|
95 | + $this->logger->error('error while creating guest avatar', [ |
|
96 | + 'err' => $e, |
|
97 | + ]); |
|
98 | + $resp = new Http\Response(); |
|
99 | + $resp->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); |
|
100 | + return $resp; |
|
101 | + } |
|
102 | 102 | |
103 | - // Cache for 30 minutes |
|
104 | - $resp->cacheFor(1800); |
|
105 | - return $resp; |
|
106 | - } |
|
103 | + // Cache for 30 minutes |
|
104 | + $resp->cacheFor(1800); |
|
105 | + return $resp; |
|
106 | + } |
|
107 | 107 | } |