@@ -230,7 +230,7 @@ |
||
| 230 | 230 | $r = $l; |
| 231 | 231 | $g = $l; |
| 232 | 232 | $b = $l; // achromatic |
| 233 | - }else{ |
|
| 233 | + } else{ |
|
| 234 | 234 | $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
| 235 | 235 | $p = 2 * $l - $q; |
| 236 | 236 | $r = $hue2rgb($p, $q, $h + 1/3); |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | * @param int $r |
| 269 | 269 | * @param int $g |
| 270 | 270 | * @param int $b |
| 271 | - * @return double[] Array containing h s l in [0, 1] range |
|
| 271 | + * @return double Array containing h s l in [0, 1] range |
|
| 272 | 272 | */ |
| 273 | 273 | private function rgbToHsl($r, $g, $b) { |
| 274 | 274 | $r /= 255.0; |
@@ -306,7 +306,7 @@ discard block |
||
| 306 | 306 | |
| 307 | 307 | /** |
| 308 | 308 | * @param string $text |
| 309 | - * @return int[] Array containting r g b in the range [0, 255] |
|
| 309 | + * @return double[] Array containting r g b in the range [0, 255] |
|
| 310 | 310 | */ |
| 311 | 311 | private function avatarBackgroundColor($text) { |
| 312 | 312 | $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | * @param double $h Hue in range [0, 1] |
| 356 | 356 | * @param double $s Saturation in range [0, 1] |
| 357 | 357 | * @param double $l Lightness in range [0, 1] |
| 358 | - * @return int[] Array containing r g b in the range [0, 255] |
|
| 358 | + * @return double[] Array containing r g b in the range [0, 255] |
|
| 359 | 359 | */ |
| 360 | 360 | private function hslToRgb($h, $s, $l){ |
| 361 | 361 | $hue2rgb = function ($p, $q, $t){ |
@@ -46,351 +46,351 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | |
| 48 | 48 | class Avatar implements IAvatar { |
| 49 | - /** @var ISimpleFolder */ |
|
| 50 | - private $folder; |
|
| 51 | - /** @var IL10N */ |
|
| 52 | - private $l; |
|
| 53 | - /** @var User */ |
|
| 54 | - private $user; |
|
| 55 | - /** @var ILogger */ |
|
| 56 | - private $logger; |
|
| 57 | - /** @var IConfig */ |
|
| 58 | - private $config; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * constructor |
|
| 62 | - * |
|
| 63 | - * @param ISimpleFolder $folder The folder where the avatars are |
|
| 64 | - * @param IL10N $l |
|
| 65 | - * @param User $user |
|
| 66 | - * @param ILogger $logger |
|
| 67 | - * @param IConfig $config |
|
| 68 | - */ |
|
| 69 | - public function __construct(ISimpleFolder $folder, |
|
| 70 | - IL10N $l, |
|
| 71 | - $user, |
|
| 72 | - ILogger $logger, |
|
| 73 | - IConfig $config) { |
|
| 74 | - $this->folder = $folder; |
|
| 75 | - $this->l = $l; |
|
| 76 | - $this->user = $user; |
|
| 77 | - $this->logger = $logger; |
|
| 78 | - $this->config = $config; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @inheritdoc |
|
| 83 | - */ |
|
| 84 | - public function get ($size = 64) { |
|
| 85 | - try { |
|
| 86 | - $file = $this->getFile($size); |
|
| 87 | - } catch (NotFoundException $e) { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $avatar = new OC_Image(); |
|
| 92 | - $avatar->loadFromData($file->getContent()); |
|
| 93 | - return $avatar; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Check if an avatar exists for the user |
|
| 98 | - * |
|
| 99 | - * @return bool |
|
| 100 | - */ |
|
| 101 | - public function exists() { |
|
| 102 | - |
|
| 103 | - return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * sets the users avatar |
|
| 108 | - * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 109 | - * @throws \Exception if the provided file is not a jpg or png image |
|
| 110 | - * @throws \Exception if the provided image is not valid |
|
| 111 | - * @throws NotSquareException if the image is not square |
|
| 112 | - * @return void |
|
| 113 | - */ |
|
| 114 | - public function set ($data) { |
|
| 115 | - |
|
| 116 | - if($data instanceOf IImage) { |
|
| 117 | - $img = $data; |
|
| 118 | - $data = $img->data(); |
|
| 119 | - } else { |
|
| 120 | - $img = new OC_Image($data); |
|
| 121 | - } |
|
| 122 | - $type = substr($img->mimeType(), -3); |
|
| 123 | - if ($type === 'peg') { |
|
| 124 | - $type = 'jpg'; |
|
| 125 | - } |
|
| 126 | - if ($type !== 'jpg' && $type !== 'png') { |
|
| 127 | - throw new \Exception($this->l->t('Unknown filetype')); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - if (!$img->valid()) { |
|
| 131 | - throw new \Exception($this->l->t('Invalid image')); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if (!($img->height() === $img->width())) { |
|
| 135 | - throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - $this->remove(); |
|
| 139 | - $file = $this->folder->newFile('avatar.'.$type); |
|
| 140 | - $file->putContent($data); |
|
| 141 | - |
|
| 142 | - try { |
|
| 143 | - $generated = $this->folder->getFile('generated'); |
|
| 144 | - $generated->delete(); |
|
| 145 | - } catch (NotFoundException $e) { |
|
| 146 | - // |
|
| 147 | - } |
|
| 148 | - $this->user->triggerChange('avatar', $file); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * remove the users avatar |
|
| 153 | - * @return void |
|
| 154 | - */ |
|
| 155 | - public function remove () { |
|
| 156 | - $avatars = $this->folder->getDirectoryListing(); |
|
| 157 | - |
|
| 158 | - $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 159 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 160 | - |
|
| 161 | - foreach ($avatars as $avatar) { |
|
| 162 | - $avatar->delete(); |
|
| 163 | - } |
|
| 164 | - $this->user->triggerChange('avatar', ''); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @inheritdoc |
|
| 169 | - */ |
|
| 170 | - public function getFile($size) { |
|
| 171 | - try { |
|
| 172 | - $ext = $this->getExtension(); |
|
| 173 | - } catch (NotFoundException $e) { |
|
| 174 | - $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 175 | - $avatar = $this->folder->newFile('avatar.png'); |
|
| 176 | - $avatar->putContent($data); |
|
| 177 | - $ext = 'png'; |
|
| 178 | - |
|
| 179 | - $this->folder->newFile('generated'); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($size === -1) { |
|
| 183 | - $path = 'avatar.' . $ext; |
|
| 184 | - } else { |
|
| 185 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - try { |
|
| 189 | - $file = $this->folder->getFile($path); |
|
| 190 | - } catch (NotFoundException $e) { |
|
| 191 | - if ($size <= 0) { |
|
| 192 | - throw new NotFoundException; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - if ($this->folder->fileExists('generated')) { |
|
| 196 | - $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 197 | - |
|
| 198 | - } else { |
|
| 199 | - $avatar = new OC_Image(); |
|
| 200 | - /** @var ISimpleFile $file */ |
|
| 201 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 202 | - $avatar->loadFromData($file->getContent()); |
|
| 203 | - $avatar->resize($size); |
|
| 204 | - $data = $avatar->data(); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - try { |
|
| 208 | - $file = $this->folder->newFile($path); |
|
| 209 | - $file->putContent($data); |
|
| 210 | - } catch (NotPermittedException $e) { |
|
| 211 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 212 | - throw new NotFoundException(); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - return $file; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 222 | - * |
|
| 223 | - * @return string |
|
| 224 | - * @throws NotFoundException |
|
| 225 | - */ |
|
| 226 | - private function getExtension() { |
|
| 227 | - if ($this->folder->fileExists('avatar.jpg')) { |
|
| 228 | - return 'jpg'; |
|
| 229 | - } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 230 | - return 'png'; |
|
| 231 | - } |
|
| 232 | - throw new NotFoundException; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param string $userDisplayName |
|
| 237 | - * @param int $size |
|
| 238 | - * @return string |
|
| 239 | - */ |
|
| 240 | - private function generateAvatar($userDisplayName, $size) { |
|
| 241 | - $text = strtoupper(substr($userDisplayName, 0, 1)); |
|
| 242 | - $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 243 | - |
|
| 244 | - $im = imagecreatetruecolor($size, $size); |
|
| 245 | - $background = imagecolorallocate($im, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]); |
|
| 246 | - $white = imagecolorallocate($im, 255, 255, 255); |
|
| 247 | - imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 248 | - |
|
| 249 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 250 | - |
|
| 251 | - $fontSize = $size * 0.4; |
|
| 252 | - $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 253 | - |
|
| 254 | - $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 255 | - $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 256 | - $x += 1; |
|
| 257 | - $y -= $box[7]; |
|
| 258 | - imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 259 | - |
|
| 260 | - ob_start(); |
|
| 261 | - imagepng($im); |
|
| 262 | - $data = ob_get_contents(); |
|
| 263 | - ob_end_clean(); |
|
| 264 | - |
|
| 265 | - return $data; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @param int $r |
|
| 270 | - * @param int $g |
|
| 271 | - * @param int $b |
|
| 272 | - * @return double[] Array containing h s l in [0, 1] range |
|
| 273 | - */ |
|
| 274 | - private function rgbToHsl($r, $g, $b) { |
|
| 275 | - $r /= 255.0; |
|
| 276 | - $g /= 255.0; |
|
| 277 | - $b /= 255.0; |
|
| 278 | - |
|
| 279 | - $max = max($r, $g, $b); |
|
| 280 | - $min = min($r, $g, $b); |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - $h = ($max + $min) / 2.0; |
|
| 284 | - $l = ($max + $min) / 2.0; |
|
| 285 | - |
|
| 286 | - if($max === $min) { |
|
| 287 | - $h = $s = 0; // Achromatic |
|
| 288 | - } else { |
|
| 289 | - $d = $max - $min; |
|
| 290 | - $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
|
| 291 | - switch($max) { |
|
| 292 | - case $r: |
|
| 293 | - $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
|
| 294 | - break; |
|
| 295 | - case $g: |
|
| 296 | - $h = ($b - $r) / $d + 2.0; |
|
| 297 | - break; |
|
| 298 | - case $b: |
|
| 299 | - $h = ($r - $g) / $d + 4.0; |
|
| 300 | - break; |
|
| 301 | - } |
|
| 302 | - $h /= 6.0; |
|
| 303 | - } |
|
| 304 | - return [$h, $s, $l]; |
|
| 305 | - |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * @param string $text |
|
| 310 | - * @return int[] Array containting r g b in the range [0, 255] |
|
| 311 | - */ |
|
| 312 | - private function avatarBackgroundColor($text) { |
|
| 313 | - $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 314 | - |
|
| 315 | - $hash = md5($hash); |
|
| 316 | - $hashChars = str_split($hash); |
|
| 317 | - |
|
| 318 | - |
|
| 319 | - // Init vars |
|
| 320 | - $result = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']; |
|
| 321 | - $rgb = [0, 0, 0]; |
|
| 322 | - $sat = 0.70; |
|
| 323 | - $lum = 0.68; |
|
| 324 | - $modulo = 16; |
|
| 325 | - |
|
| 326 | - |
|
| 327 | - // Splitting evenly the string |
|
| 328 | - foreach($hashChars as $i => $char) { |
|
| 329 | - $result[$i % $modulo] .= intval($char, 16); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - // Converting our data into a usable rgb format |
|
| 333 | - // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
|
| 334 | - for($count = 1; $count < $modulo; $count++) { |
|
| 335 | - $rgb[$count%3] += (int)$result[$count]; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - // Reduce values bigger than rgb requirements |
|
| 339 | - $rgb[0] %= 255; |
|
| 340 | - $rgb[1] %= 255; |
|
| 341 | - $rgb[2] %= 255; |
|
| 342 | - |
|
| 343 | - $hsl = $this->rgbToHsl($rgb[0], $rgb[1], $rgb[2]); |
|
| 344 | - |
|
| 345 | - // Classic formula to check the brightness for our eye |
|
| 346 | - // If too bright, lower the sat |
|
| 347 | - $bright = sqrt(0.299 * ($rgb[0] ** 2) + 0.587 * ($rgb[1] ** 2) + 0.114 * ($rgb[2] ** 2)); |
|
| 348 | - if ($bright >= 200) { |
|
| 349 | - $sat = 0.60; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return $this->hslToRgb($hsl[0], $sat, $lum); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * @param double $h Hue in range [0, 1] |
|
| 357 | - * @param double $s Saturation in range [0, 1] |
|
| 358 | - * @param double $l Lightness in range [0, 1] |
|
| 359 | - * @return int[] Array containing r g b in the range [0, 255] |
|
| 360 | - */ |
|
| 361 | - private function hslToRgb($h, $s, $l){ |
|
| 362 | - $hue2rgb = function ($p, $q, $t){ |
|
| 363 | - if($t < 0) { |
|
| 364 | - $t += 1; |
|
| 365 | - } |
|
| 366 | - if($t > 1) { |
|
| 367 | - $t -= 1; |
|
| 368 | - } |
|
| 369 | - if($t < 1/6) { |
|
| 370 | - return $p + ($q - $p) * 6 * $t; |
|
| 371 | - } |
|
| 372 | - if($t < 1/2) { |
|
| 373 | - return $q; |
|
| 374 | - } |
|
| 375 | - if($t < 2/3) { |
|
| 376 | - return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 377 | - } |
|
| 378 | - return $p; |
|
| 379 | - }; |
|
| 380 | - |
|
| 381 | - if($s === 0){ |
|
| 382 | - $r = $l; |
|
| 383 | - $g = $l; |
|
| 384 | - $b = $l; // achromatic |
|
| 385 | - }else{ |
|
| 386 | - $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
|
| 387 | - $p = 2 * $l - $q; |
|
| 388 | - $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 389 | - $g = $hue2rgb($p, $q, $h); |
|
| 390 | - $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - return array(round($r * 255), round($g * 255), round($b * 255)); |
|
| 394 | - } |
|
| 49 | + /** @var ISimpleFolder */ |
|
| 50 | + private $folder; |
|
| 51 | + /** @var IL10N */ |
|
| 52 | + private $l; |
|
| 53 | + /** @var User */ |
|
| 54 | + private $user; |
|
| 55 | + /** @var ILogger */ |
|
| 56 | + private $logger; |
|
| 57 | + /** @var IConfig */ |
|
| 58 | + private $config; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * constructor |
|
| 62 | + * |
|
| 63 | + * @param ISimpleFolder $folder The folder where the avatars are |
|
| 64 | + * @param IL10N $l |
|
| 65 | + * @param User $user |
|
| 66 | + * @param ILogger $logger |
|
| 67 | + * @param IConfig $config |
|
| 68 | + */ |
|
| 69 | + public function __construct(ISimpleFolder $folder, |
|
| 70 | + IL10N $l, |
|
| 71 | + $user, |
|
| 72 | + ILogger $logger, |
|
| 73 | + IConfig $config) { |
|
| 74 | + $this->folder = $folder; |
|
| 75 | + $this->l = $l; |
|
| 76 | + $this->user = $user; |
|
| 77 | + $this->logger = $logger; |
|
| 78 | + $this->config = $config; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @inheritdoc |
|
| 83 | + */ |
|
| 84 | + public function get ($size = 64) { |
|
| 85 | + try { |
|
| 86 | + $file = $this->getFile($size); |
|
| 87 | + } catch (NotFoundException $e) { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $avatar = new OC_Image(); |
|
| 92 | + $avatar->loadFromData($file->getContent()); |
|
| 93 | + return $avatar; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Check if an avatar exists for the user |
|
| 98 | + * |
|
| 99 | + * @return bool |
|
| 100 | + */ |
|
| 101 | + public function exists() { |
|
| 102 | + |
|
| 103 | + return $this->folder->fileExists('avatar.jpg') || $this->folder->fileExists('avatar.png'); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * sets the users avatar |
|
| 108 | + * @param IImage|resource|string $data An image object, imagedata or path to set a new avatar |
|
| 109 | + * @throws \Exception if the provided file is not a jpg or png image |
|
| 110 | + * @throws \Exception if the provided image is not valid |
|
| 111 | + * @throws NotSquareException if the image is not square |
|
| 112 | + * @return void |
|
| 113 | + */ |
|
| 114 | + public function set ($data) { |
|
| 115 | + |
|
| 116 | + if($data instanceOf IImage) { |
|
| 117 | + $img = $data; |
|
| 118 | + $data = $img->data(); |
|
| 119 | + } else { |
|
| 120 | + $img = new OC_Image($data); |
|
| 121 | + } |
|
| 122 | + $type = substr($img->mimeType(), -3); |
|
| 123 | + if ($type === 'peg') { |
|
| 124 | + $type = 'jpg'; |
|
| 125 | + } |
|
| 126 | + if ($type !== 'jpg' && $type !== 'png') { |
|
| 127 | + throw new \Exception($this->l->t('Unknown filetype')); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + if (!$img->valid()) { |
|
| 131 | + throw new \Exception($this->l->t('Invalid image')); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if (!($img->height() === $img->width())) { |
|
| 135 | + throw new NotSquareException($this->l->t('Avatar image is not square')); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + $this->remove(); |
|
| 139 | + $file = $this->folder->newFile('avatar.'.$type); |
|
| 140 | + $file->putContent($data); |
|
| 141 | + |
|
| 142 | + try { |
|
| 143 | + $generated = $this->folder->getFile('generated'); |
|
| 144 | + $generated->delete(); |
|
| 145 | + } catch (NotFoundException $e) { |
|
| 146 | + // |
|
| 147 | + } |
|
| 148 | + $this->user->triggerChange('avatar', $file); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * remove the users avatar |
|
| 153 | + * @return void |
|
| 154 | + */ |
|
| 155 | + public function remove () { |
|
| 156 | + $avatars = $this->folder->getDirectoryListing(); |
|
| 157 | + |
|
| 158 | + $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
|
| 159 | + (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 160 | + |
|
| 161 | + foreach ($avatars as $avatar) { |
|
| 162 | + $avatar->delete(); |
|
| 163 | + } |
|
| 164 | + $this->user->triggerChange('avatar', ''); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @inheritdoc |
|
| 169 | + */ |
|
| 170 | + public function getFile($size) { |
|
| 171 | + try { |
|
| 172 | + $ext = $this->getExtension(); |
|
| 173 | + } catch (NotFoundException $e) { |
|
| 174 | + $data = $this->generateAvatar($this->user->getDisplayName(), 1024); |
|
| 175 | + $avatar = $this->folder->newFile('avatar.png'); |
|
| 176 | + $avatar->putContent($data); |
|
| 177 | + $ext = 'png'; |
|
| 178 | + |
|
| 179 | + $this->folder->newFile('generated'); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($size === -1) { |
|
| 183 | + $path = 'avatar.' . $ext; |
|
| 184 | + } else { |
|
| 185 | + $path = 'avatar.' . $size . '.' . $ext; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + try { |
|
| 189 | + $file = $this->folder->getFile($path); |
|
| 190 | + } catch (NotFoundException $e) { |
|
| 191 | + if ($size <= 0) { |
|
| 192 | + throw new NotFoundException; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + if ($this->folder->fileExists('generated')) { |
|
| 196 | + $data = $this->generateAvatar($this->user->getDisplayName(), $size); |
|
| 197 | + |
|
| 198 | + } else { |
|
| 199 | + $avatar = new OC_Image(); |
|
| 200 | + /** @var ISimpleFile $file */ |
|
| 201 | + $file = $this->folder->getFile('avatar.' . $ext); |
|
| 202 | + $avatar->loadFromData($file->getContent()); |
|
| 203 | + $avatar->resize($size); |
|
| 204 | + $data = $avatar->data(); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + try { |
|
| 208 | + $file = $this->folder->newFile($path); |
|
| 209 | + $file->putContent($data); |
|
| 210 | + } catch (NotPermittedException $e) { |
|
| 211 | + $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 212 | + throw new NotFoundException(); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + return $file; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Get the extension of the avatar. If there is no avatar throw Exception |
|
| 222 | + * |
|
| 223 | + * @return string |
|
| 224 | + * @throws NotFoundException |
|
| 225 | + */ |
|
| 226 | + private function getExtension() { |
|
| 227 | + if ($this->folder->fileExists('avatar.jpg')) { |
|
| 228 | + return 'jpg'; |
|
| 229 | + } elseif ($this->folder->fileExists('avatar.png')) { |
|
| 230 | + return 'png'; |
|
| 231 | + } |
|
| 232 | + throw new NotFoundException; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param string $userDisplayName |
|
| 237 | + * @param int $size |
|
| 238 | + * @return string |
|
| 239 | + */ |
|
| 240 | + private function generateAvatar($userDisplayName, $size) { |
|
| 241 | + $text = strtoupper(substr($userDisplayName, 0, 1)); |
|
| 242 | + $backgroundColor = $this->avatarBackgroundColor($userDisplayName); |
|
| 243 | + |
|
| 244 | + $im = imagecreatetruecolor($size, $size); |
|
| 245 | + $background = imagecolorallocate($im, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]); |
|
| 246 | + $white = imagecolorallocate($im, 255, 255, 255); |
|
| 247 | + imagefilledrectangle($im, 0, 0, $size, $size, $background); |
|
| 248 | + |
|
| 249 | + $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 250 | + |
|
| 251 | + $fontSize = $size * 0.4; |
|
| 252 | + $box = imagettfbbox($fontSize, 0, $font, $text); |
|
| 253 | + |
|
| 254 | + $x = ($size - ($box[2] - $box[0])) / 2; |
|
| 255 | + $y = ($size - ($box[1] - $box[7])) / 2; |
|
| 256 | + $x += 1; |
|
| 257 | + $y -= $box[7]; |
|
| 258 | + imagettftext($im, $fontSize, 0, $x, $y, $white, $font, $text); |
|
| 259 | + |
|
| 260 | + ob_start(); |
|
| 261 | + imagepng($im); |
|
| 262 | + $data = ob_get_contents(); |
|
| 263 | + ob_end_clean(); |
|
| 264 | + |
|
| 265 | + return $data; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @param int $r |
|
| 270 | + * @param int $g |
|
| 271 | + * @param int $b |
|
| 272 | + * @return double[] Array containing h s l in [0, 1] range |
|
| 273 | + */ |
|
| 274 | + private function rgbToHsl($r, $g, $b) { |
|
| 275 | + $r /= 255.0; |
|
| 276 | + $g /= 255.0; |
|
| 277 | + $b /= 255.0; |
|
| 278 | + |
|
| 279 | + $max = max($r, $g, $b); |
|
| 280 | + $min = min($r, $g, $b); |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + $h = ($max + $min) / 2.0; |
|
| 284 | + $l = ($max + $min) / 2.0; |
|
| 285 | + |
|
| 286 | + if($max === $min) { |
|
| 287 | + $h = $s = 0; // Achromatic |
|
| 288 | + } else { |
|
| 289 | + $d = $max - $min; |
|
| 290 | + $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
|
| 291 | + switch($max) { |
|
| 292 | + case $r: |
|
| 293 | + $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
|
| 294 | + break; |
|
| 295 | + case $g: |
|
| 296 | + $h = ($b - $r) / $d + 2.0; |
|
| 297 | + break; |
|
| 298 | + case $b: |
|
| 299 | + $h = ($r - $g) / $d + 4.0; |
|
| 300 | + break; |
|
| 301 | + } |
|
| 302 | + $h /= 6.0; |
|
| 303 | + } |
|
| 304 | + return [$h, $s, $l]; |
|
| 305 | + |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * @param string $text |
|
| 310 | + * @return int[] Array containting r g b in the range [0, 255] |
|
| 311 | + */ |
|
| 312 | + private function avatarBackgroundColor($text) { |
|
| 313 | + $hash = preg_replace('/[^0-9a-f]+/', '', $text); |
|
| 314 | + |
|
| 315 | + $hash = md5($hash); |
|
| 316 | + $hashChars = str_split($hash); |
|
| 317 | + |
|
| 318 | + |
|
| 319 | + // Init vars |
|
| 320 | + $result = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']; |
|
| 321 | + $rgb = [0, 0, 0]; |
|
| 322 | + $sat = 0.70; |
|
| 323 | + $lum = 0.68; |
|
| 324 | + $modulo = 16; |
|
| 325 | + |
|
| 326 | + |
|
| 327 | + // Splitting evenly the string |
|
| 328 | + foreach($hashChars as $i => $char) { |
|
| 329 | + $result[$i % $modulo] .= intval($char, 16); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + // Converting our data into a usable rgb format |
|
| 333 | + // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
|
| 334 | + for($count = 1; $count < $modulo; $count++) { |
|
| 335 | + $rgb[$count%3] += (int)$result[$count]; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + // Reduce values bigger than rgb requirements |
|
| 339 | + $rgb[0] %= 255; |
|
| 340 | + $rgb[1] %= 255; |
|
| 341 | + $rgb[2] %= 255; |
|
| 342 | + |
|
| 343 | + $hsl = $this->rgbToHsl($rgb[0], $rgb[1], $rgb[2]); |
|
| 344 | + |
|
| 345 | + // Classic formula to check the brightness for our eye |
|
| 346 | + // If too bright, lower the sat |
|
| 347 | + $bright = sqrt(0.299 * ($rgb[0] ** 2) + 0.587 * ($rgb[1] ** 2) + 0.114 * ($rgb[2] ** 2)); |
|
| 348 | + if ($bright >= 200) { |
|
| 349 | + $sat = 0.60; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return $this->hslToRgb($hsl[0], $sat, $lum); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * @param double $h Hue in range [0, 1] |
|
| 357 | + * @param double $s Saturation in range [0, 1] |
|
| 358 | + * @param double $l Lightness in range [0, 1] |
|
| 359 | + * @return int[] Array containing r g b in the range [0, 255] |
|
| 360 | + */ |
|
| 361 | + private function hslToRgb($h, $s, $l){ |
|
| 362 | + $hue2rgb = function ($p, $q, $t){ |
|
| 363 | + if($t < 0) { |
|
| 364 | + $t += 1; |
|
| 365 | + } |
|
| 366 | + if($t > 1) { |
|
| 367 | + $t -= 1; |
|
| 368 | + } |
|
| 369 | + if($t < 1/6) { |
|
| 370 | + return $p + ($q - $p) * 6 * $t; |
|
| 371 | + } |
|
| 372 | + if($t < 1/2) { |
|
| 373 | + return $q; |
|
| 374 | + } |
|
| 375 | + if($t < 2/3) { |
|
| 376 | + return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 377 | + } |
|
| 378 | + return $p; |
|
| 379 | + }; |
|
| 380 | + |
|
| 381 | + if($s === 0){ |
|
| 382 | + $r = $l; |
|
| 383 | + $g = $l; |
|
| 384 | + $b = $l; // achromatic |
|
| 385 | + }else{ |
|
| 386 | + $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
|
| 387 | + $p = 2 * $l - $q; |
|
| 388 | + $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 389 | + $g = $hue2rgb($p, $q, $h); |
|
| 390 | + $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + return array(round($r * 255), round($g * 255), round($b * 255)); |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | 396 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | /** |
| 82 | 82 | * @inheritdoc |
| 83 | 83 | */ |
| 84 | - public function get ($size = 64) { |
|
| 84 | + public function get($size = 64) { |
|
| 85 | 85 | try { |
| 86 | 86 | $file = $this->getFile($size); |
| 87 | 87 | } catch (NotFoundException $e) { |
@@ -111,9 +111,9 @@ discard block |
||
| 111 | 111 | * @throws NotSquareException if the image is not square |
| 112 | 112 | * @return void |
| 113 | 113 | */ |
| 114 | - public function set ($data) { |
|
| 114 | + public function set($data) { |
|
| 115 | 115 | |
| 116 | - if($data instanceOf IImage) { |
|
| 116 | + if ($data instanceOf IImage) { |
|
| 117 | 117 | $img = $data; |
| 118 | 118 | $data = $img->data(); |
| 119 | 119 | } else { |
@@ -152,11 +152,11 @@ discard block |
||
| 152 | 152 | * remove the users avatar |
| 153 | 153 | * @return void |
| 154 | 154 | */ |
| 155 | - public function remove () { |
|
| 155 | + public function remove() { |
|
| 156 | 156 | $avatars = $this->folder->getDirectoryListing(); |
| 157 | 157 | |
| 158 | 158 | $this->config->setUserValue($this->user->getUID(), 'avatar', 'version', |
| 159 | - (int)$this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 159 | + (int) $this->config->getUserValue($this->user->getUID(), 'avatar', 'version', 0) + 1); |
|
| 160 | 160 | |
| 161 | 161 | foreach ($avatars as $avatar) { |
| 162 | 162 | $avatar->delete(); |
@@ -180,9 +180,9 @@ discard block |
||
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | if ($size === -1) { |
| 183 | - $path = 'avatar.' . $ext; |
|
| 183 | + $path = 'avatar.'.$ext; |
|
| 184 | 184 | } else { |
| 185 | - $path = 'avatar.' . $size . '.' . $ext; |
|
| 185 | + $path = 'avatar.'.$size.'.'.$ext; |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | try { |
@@ -198,7 +198,7 @@ discard block |
||
| 198 | 198 | } else { |
| 199 | 199 | $avatar = new OC_Image(); |
| 200 | 200 | /** @var ISimpleFile $file */ |
| 201 | - $file = $this->folder->getFile('avatar.' . $ext); |
|
| 201 | + $file = $this->folder->getFile('avatar.'.$ext); |
|
| 202 | 202 | $avatar->loadFromData($file->getContent()); |
| 203 | 203 | $avatar->resize($size); |
| 204 | 204 | $data = $avatar->data(); |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | $file = $this->folder->newFile($path); |
| 209 | 209 | $file->putContent($data); |
| 210 | 210 | } catch (NotPermittedException $e) { |
| 211 | - $this->logger->error('Failed to save avatar for ' . $this->user->getUID()); |
|
| 211 | + $this->logger->error('Failed to save avatar for '.$this->user->getUID()); |
|
| 212 | 212 | throw new NotFoundException(); |
| 213 | 213 | } |
| 214 | 214 | |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | $white = imagecolorallocate($im, 255, 255, 255); |
| 247 | 247 | imagefilledrectangle($im, 0, 0, $size, $size, $background); |
| 248 | 248 | |
| 249 | - $font = __DIR__ . '/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 249 | + $font = __DIR__.'/../../core/fonts/OpenSans-Semibold.woff'; |
|
| 250 | 250 | |
| 251 | 251 | $fontSize = $size * 0.4; |
| 252 | 252 | $box = imagettfbbox($fontSize, 0, $font, $text); |
@@ -283,12 +283,12 @@ discard block |
||
| 283 | 283 | $h = ($max + $min) / 2.0; |
| 284 | 284 | $l = ($max + $min) / 2.0; |
| 285 | 285 | |
| 286 | - if($max === $min) { |
|
| 286 | + if ($max === $min) { |
|
| 287 | 287 | $h = $s = 0; // Achromatic |
| 288 | 288 | } else { |
| 289 | 289 | $d = $max - $min; |
| 290 | 290 | $s = $l > 0.5 ? $d / (2 - $max - $min) : $d / ($max + $min); |
| 291 | - switch($max) { |
|
| 291 | + switch ($max) { |
|
| 292 | 292 | case $r: |
| 293 | 293 | $h = ($g - $b) / $d + ($g < $b ? 6 : 0); |
| 294 | 294 | break; |
@@ -325,14 +325,14 @@ discard block |
||
| 325 | 325 | |
| 326 | 326 | |
| 327 | 327 | // Splitting evenly the string |
| 328 | - foreach($hashChars as $i => $char) { |
|
| 328 | + foreach ($hashChars as $i => $char) { |
|
| 329 | 329 | $result[$i % $modulo] .= intval($char, 16); |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | // Converting our data into a usable rgb format |
| 333 | 333 | // Start at 1 because 16%3=1 but 15%3=0 and makes the repartition even |
| 334 | - for($count = 1; $count < $modulo; $count++) { |
|
| 335 | - $rgb[$count%3] += (int)$result[$count]; |
|
| 334 | + for ($count = 1; $count < $modulo; $count++) { |
|
| 335 | + $rgb[$count % 3] += (int) $result[$count]; |
|
| 336 | 336 | } |
| 337 | 337 | |
| 338 | 338 | // Reduce values bigger than rgb requirements |
@@ -358,36 +358,36 @@ discard block |
||
| 358 | 358 | * @param double $l Lightness in range [0, 1] |
| 359 | 359 | * @return int[] Array containing r g b in the range [0, 255] |
| 360 | 360 | */ |
| 361 | - private function hslToRgb($h, $s, $l){ |
|
| 362 | - $hue2rgb = function ($p, $q, $t){ |
|
| 363 | - if($t < 0) { |
|
| 361 | + private function hslToRgb($h, $s, $l) { |
|
| 362 | + $hue2rgb = function($p, $q, $t) { |
|
| 363 | + if ($t < 0) { |
|
| 364 | 364 | $t += 1; |
| 365 | 365 | } |
| 366 | - if($t > 1) { |
|
| 366 | + if ($t > 1) { |
|
| 367 | 367 | $t -= 1; |
| 368 | 368 | } |
| 369 | - if($t < 1/6) { |
|
| 369 | + if ($t < 1 / 6) { |
|
| 370 | 370 | return $p + ($q - $p) * 6 * $t; |
| 371 | 371 | } |
| 372 | - if($t < 1/2) { |
|
| 372 | + if ($t < 1 / 2) { |
|
| 373 | 373 | return $q; |
| 374 | 374 | } |
| 375 | - if($t < 2/3) { |
|
| 376 | - return $p + ($q - $p) * (2/3 - $t) * 6; |
|
| 375 | + if ($t < 2 / 3) { |
|
| 376 | + return $p + ($q - $p) * (2 / 3 - $t) * 6; |
|
| 377 | 377 | } |
| 378 | 378 | return $p; |
| 379 | 379 | }; |
| 380 | 380 | |
| 381 | - if($s === 0){ |
|
| 381 | + if ($s === 0) { |
|
| 382 | 382 | $r = $l; |
| 383 | 383 | $g = $l; |
| 384 | 384 | $b = $l; // achromatic |
| 385 | - }else{ |
|
| 385 | + } else { |
|
| 386 | 386 | $q = $l < 0.5 ? $l * (1 + $s) : $l + $s - $l * $s; |
| 387 | 387 | $p = 2 * $l - $q; |
| 388 | - $r = $hue2rgb($p, $q, $h + 1/3); |
|
| 388 | + $r = $hue2rgb($p, $q, $h + 1 / 3); |
|
| 389 | 389 | $g = $hue2rgb($p, $q, $h); |
| 390 | - $b = $hue2rgb($p, $q, $h - 1/3); |
|
| 390 | + $b = $hue2rgb($p, $q, $h - 1 / 3); |
|
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | return array(round($r * 255), round($g * 255), round($b * 255)); |
@@ -50,292 +50,292 @@ |
||
| 50 | 50 | */ |
| 51 | 51 | class AvatarController extends Controller { |
| 52 | 52 | |
| 53 | - /** @var IAvatarManager */ |
|
| 54 | - protected $avatarManager; |
|
| 55 | - |
|
| 56 | - /** @var ICache */ |
|
| 57 | - protected $cache; |
|
| 58 | - |
|
| 59 | - /** @var IL10N */ |
|
| 60 | - protected $l; |
|
| 61 | - |
|
| 62 | - /** @var IUserManager */ |
|
| 63 | - protected $userManager; |
|
| 64 | - |
|
| 65 | - /** @var IUserSession */ |
|
| 66 | - protected $userSession; |
|
| 67 | - |
|
| 68 | - /** @var IRootFolder */ |
|
| 69 | - protected $rootFolder; |
|
| 70 | - |
|
| 71 | - /** @var ILogger */ |
|
| 72 | - protected $logger; |
|
| 73 | - |
|
| 74 | - /** @var string */ |
|
| 75 | - protected $userId; |
|
| 76 | - |
|
| 77 | - /** @var TimeFactory */ |
|
| 78 | - protected $timeFactory; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param string $appName |
|
| 82 | - * @param IRequest $request |
|
| 83 | - * @param IAvatarManager $avatarManager |
|
| 84 | - * @param ICache $cache |
|
| 85 | - * @param IL10N $l10n |
|
| 86 | - * @param IUserManager $userManager |
|
| 87 | - * @param IRootFolder $rootFolder |
|
| 88 | - * @param ILogger $logger |
|
| 89 | - * @param string $userId |
|
| 90 | - * @param TimeFactory $timeFactory |
|
| 91 | - */ |
|
| 92 | - public function __construct($appName, |
|
| 93 | - IRequest $request, |
|
| 94 | - IAvatarManager $avatarManager, |
|
| 95 | - ICache $cache, |
|
| 96 | - IL10N $l10n, |
|
| 97 | - IUserManager $userManager, |
|
| 98 | - IRootFolder $rootFolder, |
|
| 99 | - ILogger $logger, |
|
| 100 | - $userId, |
|
| 101 | - TimeFactory $timeFactory) { |
|
| 102 | - parent::__construct($appName, $request); |
|
| 103 | - |
|
| 104 | - $this->avatarManager = $avatarManager; |
|
| 105 | - $this->cache = $cache; |
|
| 106 | - $this->l = $l10n; |
|
| 107 | - $this->userManager = $userManager; |
|
| 108 | - $this->rootFolder = $rootFolder; |
|
| 109 | - $this->logger = $logger; |
|
| 110 | - $this->userId = $userId; |
|
| 111 | - $this->timeFactory = $timeFactory; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - |
|
| 115 | - |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * @NoAdminRequired |
|
| 119 | - * @NoCSRFRequired |
|
| 120 | - * @NoSameSiteCookieRequired |
|
| 121 | - * @PublicPage |
|
| 122 | - * |
|
| 123 | - * @param string $userId |
|
| 124 | - * @param int $size |
|
| 125 | - * @return JSONResponse|FileDisplayResponse |
|
| 126 | - */ |
|
| 127 | - public function getAvatar($userId, $size) { |
|
| 128 | - if ($size > 2048) { |
|
| 129 | - $size = 2048; |
|
| 130 | - } elseif ($size <= 0) { |
|
| 131 | - $size = 64; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - try { |
|
| 135 | - $avatar = $this->avatarManager->getAvatar($userId)->getFile($size); |
|
| 136 | - $resp = new FileDisplayResponse($avatar, |
|
| 137 | - Http::STATUS_OK, |
|
| 138 | - ['Content-Type' => $avatar->getMimeType()]); |
|
| 139 | - } catch (\Exception $e) { |
|
| 140 | - $resp = new Http\Response(); |
|
| 141 | - $resp->setStatus(Http::STATUS_NOT_FOUND); |
|
| 142 | - return $resp; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - // Let cache this! |
|
| 146 | - $resp->addHeader('Pragma', 'public'); |
|
| 147 | - // Cache for 30 minutes |
|
| 148 | - $resp->cacheFor(1800); |
|
| 149 | - |
|
| 150 | - $expires = new \DateTime(); |
|
| 151 | - $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 152 | - $expires->add(new \DateInterval('PT30M')); |
|
| 153 | - $resp->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 154 | - |
|
| 155 | - return $resp; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @NoAdminRequired |
|
| 160 | - * |
|
| 161 | - * @param string $path |
|
| 162 | - * @return JSONResponse |
|
| 163 | - */ |
|
| 164 | - public function postAvatar($path) { |
|
| 165 | - $files = $this->request->getUploadedFile('files'); |
|
| 166 | - |
|
| 167 | - if (isset($path)) { |
|
| 168 | - $path = stripslashes($path); |
|
| 169 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 170 | - /** @var File $node */ |
|
| 171 | - $node = $userFolder->get($path); |
|
| 172 | - if (!($node instanceof File)) { |
|
| 173 | - return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
|
| 174 | - } |
|
| 175 | - if ($node->getSize() > 20*1024*1024) { |
|
| 176 | - return new JSONResponse( |
|
| 177 | - ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 178 | - Http::STATUS_BAD_REQUEST |
|
| 179 | - ); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
| 183 | - return new JSONResponse( |
|
| 184 | - ['data' => ['message' => $this->l->t('The selected file is not an image.')]], |
|
| 185 | - Http::STATUS_BAD_REQUEST |
|
| 186 | - ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - try { |
|
| 190 | - $content = $node->getContent(); |
|
| 191 | - } catch (\OCP\Files\NotPermittedException $e) { |
|
| 192 | - return new JSONResponse( |
|
| 193 | - ['data' => ['message' => $this->l->t('The selected file cannot be read.')]], |
|
| 194 | - Http::STATUS_BAD_REQUEST |
|
| 195 | - ); |
|
| 196 | - } |
|
| 197 | - } elseif (!is_null($files)) { |
|
| 198 | - if ( |
|
| 199 | - $files['error'][0] === 0 && |
|
| 200 | - is_uploaded_file($files['tmp_name'][0]) && |
|
| 201 | - !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
|
| 202 | - ) { |
|
| 203 | - if ($files['size'][0] > 20*1024*1024) { |
|
| 204 | - return new JSONResponse( |
|
| 205 | - ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 206 | - Http::STATUS_BAD_REQUEST |
|
| 207 | - ); |
|
| 208 | - } |
|
| 209 | - $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
|
| 210 | - $content = $this->cache->get('avatar_upload'); |
|
| 211 | - unlink($files['tmp_name'][0]); |
|
| 212 | - } else { |
|
| 213 | - return new JSONResponse( |
|
| 214 | - ['data' => ['message' => $this->l->t('Invalid file provided')]], |
|
| 215 | - Http::STATUS_BAD_REQUEST |
|
| 216 | - ); |
|
| 217 | - } |
|
| 218 | - } else { |
|
| 219 | - //Add imgfile |
|
| 220 | - return new JSONResponse( |
|
| 221 | - ['data' => ['message' => $this->l->t('No image or file provided')]], |
|
| 222 | - Http::STATUS_BAD_REQUEST |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - try { |
|
| 227 | - $image = new \OC_Image(); |
|
| 228 | - $image->loadFromData($content); |
|
| 229 | - $image->readExif($content); |
|
| 230 | - $image->fixOrientation(); |
|
| 231 | - |
|
| 232 | - if ($image->valid()) { |
|
| 233 | - $mimeType = $image->mimeType(); |
|
| 234 | - if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
| 235 | - return new JSONResponse( |
|
| 236 | - ['data' => ['message' => $this->l->t('Unknown filetype')]], |
|
| 237 | - Http::STATUS_OK |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->cache->set('tmpAvatar', $image->data(), 7200); |
|
| 242 | - return new JSONResponse( |
|
| 243 | - ['data' => 'notsquare'], |
|
| 244 | - Http::STATUS_OK |
|
| 245 | - ); |
|
| 246 | - } else { |
|
| 247 | - return new JSONResponse( |
|
| 248 | - ['data' => ['message' => $this->l->t('Invalid image')]], |
|
| 249 | - Http::STATUS_OK |
|
| 250 | - ); |
|
| 251 | - } |
|
| 252 | - } catch (\Exception $e) { |
|
| 253 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 254 | - return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * @NoAdminRequired |
|
| 53 | + /** @var IAvatarManager */ |
|
| 54 | + protected $avatarManager; |
|
| 55 | + |
|
| 56 | + /** @var ICache */ |
|
| 57 | + protected $cache; |
|
| 58 | + |
|
| 59 | + /** @var IL10N */ |
|
| 60 | + protected $l; |
|
| 61 | + |
|
| 62 | + /** @var IUserManager */ |
|
| 63 | + protected $userManager; |
|
| 64 | + |
|
| 65 | + /** @var IUserSession */ |
|
| 66 | + protected $userSession; |
|
| 67 | + |
|
| 68 | + /** @var IRootFolder */ |
|
| 69 | + protected $rootFolder; |
|
| 70 | + |
|
| 71 | + /** @var ILogger */ |
|
| 72 | + protected $logger; |
|
| 73 | + |
|
| 74 | + /** @var string */ |
|
| 75 | + protected $userId; |
|
| 76 | + |
|
| 77 | + /** @var TimeFactory */ |
|
| 78 | + protected $timeFactory; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param string $appName |
|
| 82 | + * @param IRequest $request |
|
| 83 | + * @param IAvatarManager $avatarManager |
|
| 84 | + * @param ICache $cache |
|
| 85 | + * @param IL10N $l10n |
|
| 86 | + * @param IUserManager $userManager |
|
| 87 | + * @param IRootFolder $rootFolder |
|
| 88 | + * @param ILogger $logger |
|
| 89 | + * @param string $userId |
|
| 90 | + * @param TimeFactory $timeFactory |
|
| 91 | + */ |
|
| 92 | + public function __construct($appName, |
|
| 93 | + IRequest $request, |
|
| 94 | + IAvatarManager $avatarManager, |
|
| 95 | + ICache $cache, |
|
| 96 | + IL10N $l10n, |
|
| 97 | + IUserManager $userManager, |
|
| 98 | + IRootFolder $rootFolder, |
|
| 99 | + ILogger $logger, |
|
| 100 | + $userId, |
|
| 101 | + TimeFactory $timeFactory) { |
|
| 102 | + parent::__construct($appName, $request); |
|
| 103 | + |
|
| 104 | + $this->avatarManager = $avatarManager; |
|
| 105 | + $this->cache = $cache; |
|
| 106 | + $this->l = $l10n; |
|
| 107 | + $this->userManager = $userManager; |
|
| 108 | + $this->rootFolder = $rootFolder; |
|
| 109 | + $this->logger = $logger; |
|
| 110 | + $this->userId = $userId; |
|
| 111 | + $this->timeFactory = $timeFactory; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + |
|
| 115 | + |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * @NoAdminRequired |
|
| 119 | + * @NoCSRFRequired |
|
| 120 | + * @NoSameSiteCookieRequired |
|
| 121 | + * @PublicPage |
|
| 260 | 122 | * |
| 261 | - * @return JSONResponse |
|
| 262 | - */ |
|
| 263 | - public function deleteAvatar() { |
|
| 264 | - try { |
|
| 265 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 266 | - $avatar->remove(); |
|
| 267 | - return new JSONResponse(); |
|
| 268 | - } catch (\Exception $e) { |
|
| 269 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 270 | - return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @NoAdminRequired |
|
| 276 | - * |
|
| 277 | - * @return JSONResponse|DataDisplayResponse |
|
| 278 | - */ |
|
| 279 | - public function getTmpAvatar() { |
|
| 280 | - $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 281 | - if (is_null($tmpAvatar)) { |
|
| 282 | - return new JSONResponse(['data' => [ |
|
| 283 | - 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 284 | - ]], |
|
| 285 | - Http::STATUS_NOT_FOUND); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $image = new \OC_Image($tmpAvatar); |
|
| 289 | - |
|
| 290 | - $resp = new DataDisplayResponse($image->data(), |
|
| 291 | - Http::STATUS_OK, |
|
| 292 | - ['Content-Type' => $image->mimeType()]); |
|
| 293 | - |
|
| 294 | - $resp->setETag((string)crc32($image->data())); |
|
| 295 | - $resp->cacheFor(0); |
|
| 296 | - $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
| 297 | - return $resp; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * @NoAdminRequired |
|
| 302 | - * |
|
| 303 | - * @param array $crop |
|
| 304 | - * @return JSONResponse |
|
| 305 | - */ |
|
| 306 | - public function postCroppedAvatar($crop) { |
|
| 307 | - if (is_null($crop)) { |
|
| 308 | - return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], |
|
| 309 | - Http::STATUS_BAD_REQUEST); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
| 313 | - return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], |
|
| 314 | - Http::STATUS_BAD_REQUEST); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 318 | - if (is_null($tmpAvatar)) { |
|
| 319 | - return new JSONResponse(['data' => [ |
|
| 320 | - 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 321 | - ]], |
|
| 322 | - Http::STATUS_BAD_REQUEST); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - $image = new \OC_Image($tmpAvatar); |
|
| 326 | - $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
| 327 | - try { |
|
| 328 | - $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 329 | - $avatar->set($image); |
|
| 330 | - // Clean up |
|
| 331 | - $this->cache->remove('tmpAvatar'); |
|
| 332 | - return new JSONResponse(['status' => 'success']); |
|
| 333 | - } catch (\OC\NotSquareException $e) { |
|
| 334 | - return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], |
|
| 335 | - Http::STATUS_BAD_REQUEST); |
|
| 336 | - } catch (\Exception $e) { |
|
| 337 | - $this->logger->logException($e, ['app' => 'core']); |
|
| 338 | - return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 339 | - } |
|
| 340 | - } |
|
| 123 | + * @param string $userId |
|
| 124 | + * @param int $size |
|
| 125 | + * @return JSONResponse|FileDisplayResponse |
|
| 126 | + */ |
|
| 127 | + public function getAvatar($userId, $size) { |
|
| 128 | + if ($size > 2048) { |
|
| 129 | + $size = 2048; |
|
| 130 | + } elseif ($size <= 0) { |
|
| 131 | + $size = 64; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + try { |
|
| 135 | + $avatar = $this->avatarManager->getAvatar($userId)->getFile($size); |
|
| 136 | + $resp = new FileDisplayResponse($avatar, |
|
| 137 | + Http::STATUS_OK, |
|
| 138 | + ['Content-Type' => $avatar->getMimeType()]); |
|
| 139 | + } catch (\Exception $e) { |
|
| 140 | + $resp = new Http\Response(); |
|
| 141 | + $resp->setStatus(Http::STATUS_NOT_FOUND); |
|
| 142 | + return $resp; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + // Let cache this! |
|
| 146 | + $resp->addHeader('Pragma', 'public'); |
|
| 147 | + // Cache for 30 minutes |
|
| 148 | + $resp->cacheFor(1800); |
|
| 149 | + |
|
| 150 | + $expires = new \DateTime(); |
|
| 151 | + $expires->setTimestamp($this->timeFactory->getTime()); |
|
| 152 | + $expires->add(new \DateInterval('PT30M')); |
|
| 153 | + $resp->addHeader('Expires', $expires->format(\DateTime::RFC1123)); |
|
| 154 | + |
|
| 155 | + return $resp; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @NoAdminRequired |
|
| 160 | + * |
|
| 161 | + * @param string $path |
|
| 162 | + * @return JSONResponse |
|
| 163 | + */ |
|
| 164 | + public function postAvatar($path) { |
|
| 165 | + $files = $this->request->getUploadedFile('files'); |
|
| 166 | + |
|
| 167 | + if (isset($path)) { |
|
| 168 | + $path = stripslashes($path); |
|
| 169 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 170 | + /** @var File $node */ |
|
| 171 | + $node = $userFolder->get($path); |
|
| 172 | + if (!($node instanceof File)) { |
|
| 173 | + return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); |
|
| 174 | + } |
|
| 175 | + if ($node->getSize() > 20*1024*1024) { |
|
| 176 | + return new JSONResponse( |
|
| 177 | + ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 178 | + Http::STATUS_BAD_REQUEST |
|
| 179 | + ); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { |
|
| 183 | + return new JSONResponse( |
|
| 184 | + ['data' => ['message' => $this->l->t('The selected file is not an image.')]], |
|
| 185 | + Http::STATUS_BAD_REQUEST |
|
| 186 | + ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + try { |
|
| 190 | + $content = $node->getContent(); |
|
| 191 | + } catch (\OCP\Files\NotPermittedException $e) { |
|
| 192 | + return new JSONResponse( |
|
| 193 | + ['data' => ['message' => $this->l->t('The selected file cannot be read.')]], |
|
| 194 | + Http::STATUS_BAD_REQUEST |
|
| 195 | + ); |
|
| 196 | + } |
|
| 197 | + } elseif (!is_null($files)) { |
|
| 198 | + if ( |
|
| 199 | + $files['error'][0] === 0 && |
|
| 200 | + is_uploaded_file($files['tmp_name'][0]) && |
|
| 201 | + !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0]) |
|
| 202 | + ) { |
|
| 203 | + if ($files['size'][0] > 20*1024*1024) { |
|
| 204 | + return new JSONResponse( |
|
| 205 | + ['data' => ['message' => $this->l->t('File is too big')]], |
|
| 206 | + Http::STATUS_BAD_REQUEST |
|
| 207 | + ); |
|
| 208 | + } |
|
| 209 | + $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); |
|
| 210 | + $content = $this->cache->get('avatar_upload'); |
|
| 211 | + unlink($files['tmp_name'][0]); |
|
| 212 | + } else { |
|
| 213 | + return new JSONResponse( |
|
| 214 | + ['data' => ['message' => $this->l->t('Invalid file provided')]], |
|
| 215 | + Http::STATUS_BAD_REQUEST |
|
| 216 | + ); |
|
| 217 | + } |
|
| 218 | + } else { |
|
| 219 | + //Add imgfile |
|
| 220 | + return new JSONResponse( |
|
| 221 | + ['data' => ['message' => $this->l->t('No image or file provided')]], |
|
| 222 | + Http::STATUS_BAD_REQUEST |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + try { |
|
| 227 | + $image = new \OC_Image(); |
|
| 228 | + $image->loadFromData($content); |
|
| 229 | + $image->readExif($content); |
|
| 230 | + $image->fixOrientation(); |
|
| 231 | + |
|
| 232 | + if ($image->valid()) { |
|
| 233 | + $mimeType = $image->mimeType(); |
|
| 234 | + if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { |
|
| 235 | + return new JSONResponse( |
|
| 236 | + ['data' => ['message' => $this->l->t('Unknown filetype')]], |
|
| 237 | + Http::STATUS_OK |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->cache->set('tmpAvatar', $image->data(), 7200); |
|
| 242 | + return new JSONResponse( |
|
| 243 | + ['data' => 'notsquare'], |
|
| 244 | + Http::STATUS_OK |
|
| 245 | + ); |
|
| 246 | + } else { |
|
| 247 | + return new JSONResponse( |
|
| 248 | + ['data' => ['message' => $this->l->t('Invalid image')]], |
|
| 249 | + Http::STATUS_OK |
|
| 250 | + ); |
|
| 251 | + } |
|
| 252 | + } catch (\Exception $e) { |
|
| 253 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 254 | + return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * @NoAdminRequired |
|
| 260 | + * |
|
| 261 | + * @return JSONResponse |
|
| 262 | + */ |
|
| 263 | + public function deleteAvatar() { |
|
| 264 | + try { |
|
| 265 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 266 | + $avatar->remove(); |
|
| 267 | + return new JSONResponse(); |
|
| 268 | + } catch (\Exception $e) { |
|
| 269 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 270 | + return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @NoAdminRequired |
|
| 276 | + * |
|
| 277 | + * @return JSONResponse|DataDisplayResponse |
|
| 278 | + */ |
|
| 279 | + public function getTmpAvatar() { |
|
| 280 | + $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 281 | + if (is_null($tmpAvatar)) { |
|
| 282 | + return new JSONResponse(['data' => [ |
|
| 283 | + 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 284 | + ]], |
|
| 285 | + Http::STATUS_NOT_FOUND); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $image = new \OC_Image($tmpAvatar); |
|
| 289 | + |
|
| 290 | + $resp = new DataDisplayResponse($image->data(), |
|
| 291 | + Http::STATUS_OK, |
|
| 292 | + ['Content-Type' => $image->mimeType()]); |
|
| 293 | + |
|
| 294 | + $resp->setETag((string)crc32($image->data())); |
|
| 295 | + $resp->cacheFor(0); |
|
| 296 | + $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); |
|
| 297 | + return $resp; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * @NoAdminRequired |
|
| 302 | + * |
|
| 303 | + * @param array $crop |
|
| 304 | + * @return JSONResponse |
|
| 305 | + */ |
|
| 306 | + public function postCroppedAvatar($crop) { |
|
| 307 | + if (is_null($crop)) { |
|
| 308 | + return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], |
|
| 309 | + Http::STATUS_BAD_REQUEST); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { |
|
| 313 | + return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], |
|
| 314 | + Http::STATUS_BAD_REQUEST); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + $tmpAvatar = $this->cache->get('tmpAvatar'); |
|
| 318 | + if (is_null($tmpAvatar)) { |
|
| 319 | + return new JSONResponse(['data' => [ |
|
| 320 | + 'message' => $this->l->t("No temporary profile picture available, try again") |
|
| 321 | + ]], |
|
| 322 | + Http::STATUS_BAD_REQUEST); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + $image = new \OC_Image($tmpAvatar); |
|
| 326 | + $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h'])); |
|
| 327 | + try { |
|
| 328 | + $avatar = $this->avatarManager->getAvatar($this->userId); |
|
| 329 | + $avatar->set($image); |
|
| 330 | + // Clean up |
|
| 331 | + $this->cache->remove('tmpAvatar'); |
|
| 332 | + return new JSONResponse(['status' => 'success']); |
|
| 333 | + } catch (\OC\NotSquareException $e) { |
|
| 334 | + return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], |
|
| 335 | + Http::STATUS_BAD_REQUEST); |
|
| 336 | + } catch (\Exception $e) { |
|
| 337 | + $this->logger->logException($e, ['app' => 'core']); |
|
| 338 | + return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | 341 | } |