@@ -52,293 +52,293 @@  | 
                                                    ||
| 52 | 52 | */  | 
                                                        
| 53 | 53 |  class AvatarController extends Controller { | 
                                                        
| 54 | 54 | |
| 55 | - /** @var IAvatarManager */  | 
                                                        |
| 56 | - protected $avatarManager;  | 
                                                        |
| 57 | -  | 
                                                        |
| 58 | - /** @var ICache */  | 
                                                        |
| 59 | - protected $cache;  | 
                                                        |
| 60 | -  | 
                                                        |
| 61 | - /** @var IL10N */  | 
                                                        |
| 62 | - protected $l;  | 
                                                        |
| 63 | -  | 
                                                        |
| 64 | - /** @var IUserManager */  | 
                                                        |
| 65 | - protected $userManager;  | 
                                                        |
| 66 | -  | 
                                                        |
| 67 | - /** @var IUserSession */  | 
                                                        |
| 68 | - protected $userSession;  | 
                                                        |
| 69 | -  | 
                                                        |
| 70 | - /** @var IRootFolder */  | 
                                                        |
| 71 | - protected $rootFolder;  | 
                                                        |
| 72 | -  | 
                                                        |
| 73 | - /** @var ILogger */  | 
                                                        |
| 74 | - protected $logger;  | 
                                                        |
| 75 | -  | 
                                                        |
| 76 | - /** @var string */  | 
                                                        |
| 77 | - protected $userId;  | 
                                                        |
| 78 | -  | 
                                                        |
| 79 | - /** @var TimeFactory */  | 
                                                        |
| 80 | - protected $timeFactory;  | 
                                                        |
| 81 | - /** @var IAccountManager */  | 
                                                        |
| 82 | - private $accountManager;  | 
                                                        |
| 83 | -  | 
                                                        |
| 84 | - public function __construct($appName,  | 
                                                        |
| 85 | - IRequest $request,  | 
                                                        |
| 86 | - IAvatarManager $avatarManager,  | 
                                                        |
| 87 | - ICache $cache,  | 
                                                        |
| 88 | - IL10N $l10n,  | 
                                                        |
| 89 | - IUserManager $userManager,  | 
                                                        |
| 90 | - IRootFolder $rootFolder,  | 
                                                        |
| 91 | - ILogger $logger,  | 
                                                        |
| 92 | - $userId,  | 
                                                        |
| 93 | - TimeFactory $timeFactory,  | 
                                                        |
| 94 | -								IAccountManager $accountManager) { | 
                                                        |
| 95 | - parent::__construct($appName, $request);  | 
                                                        |
| 96 | -  | 
                                                        |
| 97 | - $this->avatarManager = $avatarManager;  | 
                                                        |
| 98 | - $this->cache = $cache;  | 
                                                        |
| 99 | - $this->l = $l10n;  | 
                                                        |
| 100 | - $this->userManager = $userManager;  | 
                                                        |
| 101 | - $this->rootFolder = $rootFolder;  | 
                                                        |
| 102 | - $this->logger = $logger;  | 
                                                        |
| 103 | - $this->userId = $userId;  | 
                                                        |
| 104 | - $this->timeFactory = $timeFactory;  | 
                                                        |
| 105 | - $this->accountManager = $accountManager;  | 
                                                        |
| 106 | - }  | 
                                                        |
| 107 | -  | 
                                                        |
| 108 | -  | 
                                                        |
| 109 | - /**  | 
                                                        |
| 110 | - * @NoAdminRequired  | 
                                                        |
| 111 | - * @NoCSRFRequired  | 
                                                        |
| 112 | - * @NoSameSiteCookieRequired  | 
                                                        |
| 113 | - * @PublicPage  | 
                                                        |
| 114 | - *  | 
                                                        |
| 115 | - * @param string $userId  | 
                                                        |
| 116 | - * @param int $size  | 
                                                        |
| 117 | - * @return JSONResponse|FileDisplayResponse  | 
                                                        |
| 118 | - */  | 
                                                        |
| 119 | -	public function getAvatar($userId, $size) { | 
                                                        |
| 120 | - // min/max size  | 
                                                        |
| 121 | -		if ($size > 2048) { | 
                                                        |
| 122 | - $size = 2048;  | 
                                                        |
| 123 | -		} elseif ($size <= 0) { | 
                                                        |
| 124 | - $size = 64;  | 
                                                        |
| 125 | - }  | 
                                                        |
| 126 | -  | 
                                                        |
| 127 | - $user = $this->userManager->get($userId);  | 
                                                        |
| 128 | -		if ($user === null) { | 
                                                        |
| 129 | - return new JSONResponse([], Http::STATUS_NOT_FOUND);  | 
                                                        |
| 130 | - }  | 
                                                        |
| 131 | -  | 
                                                        |
| 132 | - $account = $this->accountManager->getAccount($user);  | 
                                                        |
| 133 | - $scope = $account->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();  | 
                                                        |
| 134 | -  | 
                                                        |
| 135 | -		if ($scope !== IAccountManager::VISIBILITY_PUBLIC && $this->userId === null) { | 
                                                        |
| 136 | - // Public avatar access is not allowed  | 
                                                        |
| 137 | - $response = new JSONResponse([], Http::STATUS_NOT_FOUND);  | 
                                                        |
| 138 | - $response->cacheFor(1800);  | 
                                                        |
| 139 | - return $response;  | 
                                                        |
| 140 | - }  | 
                                                        |
| 141 | -  | 
                                                        |
| 142 | -		try { | 
                                                        |
| 143 | - $avatar = $this->avatarManager->getAvatar($userId);  | 
                                                        |
| 144 | - $avatarFile = $avatar->getFile($size);  | 
                                                        |
| 145 | - $response = new FileDisplayResponse(  | 
                                                        |
| 146 | - $avatarFile,  | 
                                                        |
| 147 | - $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,  | 
                                                        |
| 148 | - ['Content-Type' => $avatarFile->getMimeType()]  | 
                                                        |
| 149 | - );  | 
                                                        |
| 150 | -		} catch (\Exception $e) { | 
                                                        |
| 151 | - return new JSONResponse([], Http::STATUS_NOT_FOUND);  | 
                                                        |
| 152 | - }  | 
                                                        |
| 153 | -  | 
                                                        |
| 154 | - // Cache for 30 minutes  | 
                                                        |
| 155 | - $response->cacheFor(1800);  | 
                                                        |
| 156 | - return $response;  | 
                                                        |
| 157 | - }  | 
                                                        |
| 158 | -  | 
                                                        |
| 159 | - /**  | 
                                                        |
| 160 | - * @NoAdminRequired  | 
                                                        |
| 161 | - *  | 
                                                        |
| 162 | - * @param string $path  | 
                                                        |
| 163 | - * @return JSONResponse  | 
                                                        |
| 164 | - */  | 
                                                        |
| 165 | -	public function postAvatar($path) { | 
                                                        |
| 166 | -		$files = $this->request->getUploadedFile('files'); | 
                                                        |
| 167 | -  | 
                                                        |
| 168 | -		if (isset($path)) { | 
                                                        |
| 169 | - $path = stripslashes($path);  | 
                                                        |
| 170 | - $userFolder = $this->rootFolder->getUserFolder($this->userId);  | 
                                                        |
| 171 | - /** @var File $node */  | 
                                                        |
| 172 | - $node = $userFolder->get($path);  | 
                                                        |
| 173 | -			if (!($node instanceof File)) { | 
                                                        |
| 174 | -				return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); | 
                                                        |
| 175 | - }  | 
                                                        |
| 176 | -			if ($node->getSize() > 20*1024*1024) { | 
                                                        |
| 177 | - return new JSONResponse(  | 
                                                        |
| 178 | -					['data' => ['message' => $this->l->t('File is too big')]], | 
                                                        |
| 179 | - Http::STATUS_BAD_REQUEST  | 
                                                        |
| 180 | - );  | 
                                                        |
| 181 | - }  | 
                                                        |
| 182 | -  | 
                                                        |
| 183 | -			if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { | 
                                                        |
| 184 | - return new JSONResponse(  | 
                                                        |
| 185 | -					['data' => ['message' => $this->l->t('The selected file is not an image.')]], | 
                                                        |
| 186 | - Http::STATUS_BAD_REQUEST  | 
                                                        |
| 187 | - );  | 
                                                        |
| 188 | - }  | 
                                                        |
| 189 | -  | 
                                                        |
| 190 | -			try { | 
                                                        |
| 191 | - $content = $node->getContent();  | 
                                                        |
| 192 | -			} catch (\OCP\Files\NotPermittedException $e) { | 
                                                        |
| 193 | - return new JSONResponse(  | 
                                                        |
| 194 | -					['data' => ['message' => $this->l->t('The selected file cannot be read.')]], | 
                                                        |
| 195 | - Http::STATUS_BAD_REQUEST  | 
                                                        |
| 196 | - );  | 
                                                        |
| 197 | - }  | 
                                                        |
| 198 | -		} elseif (!is_null($files)) { | 
                                                        |
| 199 | - if (  | 
                                                        |
| 200 | - $files['error'][0] === 0 &&  | 
                                                        |
| 201 | - is_uploaded_file($files['tmp_name'][0]) &&  | 
                                                        |
| 202 | - !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])  | 
                                                        |
| 203 | -			) { | 
                                                        |
| 204 | -				if ($files['size'][0] > 20*1024*1024) { | 
                                                        |
| 205 | - return new JSONResponse(  | 
                                                        |
| 206 | -						['data' => ['message' => $this->l->t('File is too big')]], | 
                                                        |
| 207 | - Http::STATUS_BAD_REQUEST  | 
                                                        |
| 208 | - );  | 
                                                        |
| 209 | - }  | 
                                                        |
| 210 | -				$this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); | 
                                                        |
| 211 | -				$content = $this->cache->get('avatar_upload'); | 
                                                        |
| 212 | - unlink($files['tmp_name'][0]);  | 
                                                        |
| 213 | -			} else { | 
                                                        |
| 214 | - return new JSONResponse(  | 
                                                        |
| 215 | -					['data' => ['message' => $this->l->t('Invalid file provided')]], | 
                                                        |
| 216 | - Http::STATUS_BAD_REQUEST  | 
                                                        |
| 217 | - );  | 
                                                        |
| 218 | - }  | 
                                                        |
| 219 | -		} else { | 
                                                        |
| 220 | - //Add imgfile  | 
                                                        |
| 221 | - return new JSONResponse(  | 
                                                        |
| 222 | -				['data' => ['message' => $this->l->t('No image or file provided')]], | 
                                                        |
| 223 | - Http::STATUS_BAD_REQUEST  | 
                                                        |
| 224 | - );  | 
                                                        |
| 225 | - }  | 
                                                        |
| 226 | -  | 
                                                        |
| 227 | -		try { | 
                                                        |
| 228 | - $image = new \OC_Image();  | 
                                                        |
| 229 | - $image->loadFromData($content);  | 
                                                        |
| 230 | - $image->readExif($content);  | 
                                                        |
| 231 | - $image->fixOrientation();  | 
                                                        |
| 232 | -  | 
                                                        |
| 233 | -			if ($image->valid()) { | 
                                                        |
| 234 | - $mimeType = $image->mimeType();  | 
                                                        |
| 235 | -				if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { | 
                                                        |
| 236 | - return new JSONResponse(  | 
                                                        |
| 237 | -						['data' => ['message' => $this->l->t('Unknown filetype')]], | 
                                                        |
| 238 | - Http::STATUS_OK  | 
                                                        |
| 239 | - );  | 
                                                        |
| 240 | - }  | 
                                                        |
| 241 | -  | 
                                                        |
| 242 | -				$this->cache->set('tmpAvatar', $image->data(), 7200); | 
                                                        |
| 243 | - return new JSONResponse(  | 
                                                        |
| 244 | - ['data' => 'notsquare'],  | 
                                                        |
| 245 | - Http::STATUS_OK  | 
                                                        |
| 246 | - );  | 
                                                        |
| 247 | -			} else { | 
                                                        |
| 248 | - return new JSONResponse(  | 
                                                        |
| 249 | -					['data' => ['message' => $this->l->t('Invalid image')]], | 
                                                        |
| 250 | - Http::STATUS_OK  | 
                                                        |
| 251 | - );  | 
                                                        |
| 252 | - }  | 
                                                        |
| 253 | -		} catch (\Exception $e) { | 
                                                        |
| 254 | - $this->logger->logException($e, ['app' => 'core']);  | 
                                                        |
| 255 | -			return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); | 
                                                        |
| 256 | - }  | 
                                                        |
| 257 | - }  | 
                                                        |
| 258 | -  | 
                                                        |
| 259 | - /**  | 
                                                        |
| 260 | - * @NoAdminRequired  | 
                                                        |
| 55 | + /** @var IAvatarManager */  | 
                                                        |
| 56 | + protected $avatarManager;  | 
                                                        |
| 57 | +  | 
                                                        |
| 58 | + /** @var ICache */  | 
                                                        |
| 59 | + protected $cache;  | 
                                                        |
| 60 | +  | 
                                                        |
| 61 | + /** @var IL10N */  | 
                                                        |
| 62 | + protected $l;  | 
                                                        |
| 63 | +  | 
                                                        |
| 64 | + /** @var IUserManager */  | 
                                                        |
| 65 | + protected $userManager;  | 
                                                        |
| 66 | +  | 
                                                        |
| 67 | + /** @var IUserSession */  | 
                                                        |
| 68 | + protected $userSession;  | 
                                                        |
| 69 | +  | 
                                                        |
| 70 | + /** @var IRootFolder */  | 
                                                        |
| 71 | + protected $rootFolder;  | 
                                                        |
| 72 | +  | 
                                                        |
| 73 | + /** @var ILogger */  | 
                                                        |
| 74 | + protected $logger;  | 
                                                        |
| 75 | +  | 
                                                        |
| 76 | + /** @var string */  | 
                                                        |
| 77 | + protected $userId;  | 
                                                        |
| 78 | +  | 
                                                        |
| 79 | + /** @var TimeFactory */  | 
                                                        |
| 80 | + protected $timeFactory;  | 
                                                        |
| 81 | + /** @var IAccountManager */  | 
                                                        |
| 82 | + private $accountManager;  | 
                                                        |
| 83 | +  | 
                                                        |
| 84 | + public function __construct($appName,  | 
                                                        |
| 85 | + IRequest $request,  | 
                                                        |
| 86 | + IAvatarManager $avatarManager,  | 
                                                        |
| 87 | + ICache $cache,  | 
                                                        |
| 88 | + IL10N $l10n,  | 
                                                        |
| 89 | + IUserManager $userManager,  | 
                                                        |
| 90 | + IRootFolder $rootFolder,  | 
                                                        |
| 91 | + ILogger $logger,  | 
                                                        |
| 92 | + $userId,  | 
                                                        |
| 93 | + TimeFactory $timeFactory,  | 
                                                        |
| 94 | +                                IAccountManager $accountManager) { | 
                                                        |
| 95 | + parent::__construct($appName, $request);  | 
                                                        |
| 96 | +  | 
                                                        |
| 97 | + $this->avatarManager = $avatarManager;  | 
                                                        |
| 98 | + $this->cache = $cache;  | 
                                                        |
| 99 | + $this->l = $l10n;  | 
                                                        |
| 100 | + $this->userManager = $userManager;  | 
                                                        |
| 101 | + $this->rootFolder = $rootFolder;  | 
                                                        |
| 102 | + $this->logger = $logger;  | 
                                                        |
| 103 | + $this->userId = $userId;  | 
                                                        |
| 104 | + $this->timeFactory = $timeFactory;  | 
                                                        |
| 105 | + $this->accountManager = $accountManager;  | 
                                                        |
| 106 | + }  | 
                                                        |
| 107 | +  | 
                                                        |
| 108 | +  | 
                                                        |
| 109 | + /**  | 
                                                        |
| 110 | + * @NoAdminRequired  | 
                                                        |
| 111 | + * @NoCSRFRequired  | 
                                                        |
| 112 | + * @NoSameSiteCookieRequired  | 
                                                        |
| 113 | + * @PublicPage  | 
                                                        |
| 261 | 114 | *  | 
                                                        
| 262 | - * @return JSONResponse  | 
                                                        |
| 263 | - */  | 
                                                        |
| 264 | -	public function deleteAvatar() { | 
                                                        |
| 265 | -		try { | 
                                                        |
| 266 | - $avatar = $this->avatarManager->getAvatar($this->userId);  | 
                                                        |
| 267 | - $avatar->remove();  | 
                                                        |
| 268 | - return new JSONResponse();  | 
                                                        |
| 269 | -		} catch (\Exception $e) { | 
                                                        |
| 270 | - $this->logger->logException($e, ['app' => 'core']);  | 
                                                        |
| 271 | -			return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); | 
                                                        |
| 272 | - }  | 
                                                        |
| 273 | - }  | 
                                                        |
| 274 | -  | 
                                                        |
| 275 | - /**  | 
                                                        |
| 276 | - * @NoAdminRequired  | 
                                                        |
| 277 | - *  | 
                                                        |
| 278 | - * @return JSONResponse|DataDisplayResponse  | 
                                                        |
| 279 | - */  | 
                                                        |
| 280 | -	public function getTmpAvatar() { | 
                                                        |
| 281 | -		$tmpAvatar = $this->cache->get('tmpAvatar'); | 
                                                        |
| 282 | -		if (is_null($tmpAvatar)) { | 
                                                        |
| 283 | - return new JSONResponse(['data' => [  | 
                                                        |
| 284 | -										'message' => $this->l->t("No temporary profile picture available, try again") | 
                                                        |
| 285 | - ]],  | 
                                                        |
| 286 | - Http::STATUS_NOT_FOUND);  | 
                                                        |
| 287 | - }  | 
                                                        |
| 288 | -  | 
                                                        |
| 289 | - $image = new \OC_Image();  | 
                                                        |
| 290 | - $image->loadFromData($tmpAvatar);  | 
                                                        |
| 291 | -  | 
                                                        |
| 292 | - $resp = new DataDisplayResponse($image->data(),  | 
                                                        |
| 293 | - Http::STATUS_OK,  | 
                                                        |
| 294 | - ['Content-Type' => $image->mimeType()]);  | 
                                                        |
| 295 | -  | 
                                                        |
| 296 | - $resp->setETag((string)crc32($image->data()));  | 
                                                        |
| 297 | - $resp->cacheFor(0);  | 
                                                        |
| 298 | -		$resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); | 
                                                        |
| 299 | - return $resp;  | 
                                                        |
| 300 | - }  | 
                                                        |
| 301 | -  | 
                                                        |
| 302 | - /**  | 
                                                        |
| 303 | - * @NoAdminRequired  | 
                                                        |
| 304 | - *  | 
                                                        |
| 305 | - * @param array $crop  | 
                                                        |
| 306 | - * @return JSONResponse  | 
                                                        |
| 307 | - */  | 
                                                        |
| 308 | -	public function postCroppedAvatar($crop) { | 
                                                        |
| 309 | -		if (is_null($crop)) { | 
                                                        |
| 310 | -			return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], | 
                                                        |
| 311 | - Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 312 | - }  | 
                                                        |
| 313 | -  | 
                                                        |
| 314 | -		if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { | 
                                                        |
| 315 | -			return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], | 
                                                        |
| 316 | - Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 317 | - }  | 
                                                        |
| 318 | -  | 
                                                        |
| 319 | -		$tmpAvatar = $this->cache->get('tmpAvatar'); | 
                                                        |
| 320 | -		if (is_null($tmpAvatar)) { | 
                                                        |
| 321 | - return new JSONResponse(['data' => [  | 
                                                        |
| 322 | -										'message' => $this->l->t("No temporary profile picture available, try again") | 
                                                        |
| 323 | - ]],  | 
                                                        |
| 324 | - Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 325 | - }  | 
                                                        |
| 326 | -  | 
                                                        |
| 327 | - $image = new \OC_Image();  | 
                                                        |
| 328 | - $image->loadFromData($tmpAvatar);  | 
                                                        |
| 329 | - $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));  | 
                                                        |
| 330 | -		try { | 
                                                        |
| 331 | - $avatar = $this->avatarManager->getAvatar($this->userId);  | 
                                                        |
| 332 | - $avatar->set($image);  | 
                                                        |
| 333 | - // Clean up  | 
                                                        |
| 334 | -			$this->cache->remove('tmpAvatar'); | 
                                                        |
| 335 | - return new JSONResponse(['status' => 'success']);  | 
                                                        |
| 336 | -		} catch (\OC\NotSquareException $e) { | 
                                                        |
| 337 | -			return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], | 
                                                        |
| 338 | - Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 339 | -		} catch (\Exception $e) { | 
                                                        |
| 340 | - $this->logger->logException($e, ['app' => 'core']);  | 
                                                        |
| 341 | -			return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); | 
                                                        |
| 342 | - }  | 
                                                        |
| 343 | - }  | 
                                                        |
| 115 | + * @param string $userId  | 
                                                        |
| 116 | + * @param int $size  | 
                                                        |
| 117 | + * @return JSONResponse|FileDisplayResponse  | 
                                                        |
| 118 | + */  | 
                                                        |
| 119 | +    public function getAvatar($userId, $size) { | 
                                                        |
| 120 | + // min/max size  | 
                                                        |
| 121 | +        if ($size > 2048) { | 
                                                        |
| 122 | + $size = 2048;  | 
                                                        |
| 123 | +        } elseif ($size <= 0) { | 
                                                        |
| 124 | + $size = 64;  | 
                                                        |
| 125 | + }  | 
                                                        |
| 126 | +  | 
                                                        |
| 127 | + $user = $this->userManager->get($userId);  | 
                                                        |
| 128 | +        if ($user === null) { | 
                                                        |
| 129 | + return new JSONResponse([], Http::STATUS_NOT_FOUND);  | 
                                                        |
| 130 | + }  | 
                                                        |
| 131 | +  | 
                                                        |
| 132 | + $account = $this->accountManager->getAccount($user);  | 
                                                        |
| 133 | + $scope = $account->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();  | 
                                                        |
| 134 | +  | 
                                                        |
| 135 | +        if ($scope !== IAccountManager::VISIBILITY_PUBLIC && $this->userId === null) { | 
                                                        |
| 136 | + // Public avatar access is not allowed  | 
                                                        |
| 137 | + $response = new JSONResponse([], Http::STATUS_NOT_FOUND);  | 
                                                        |
| 138 | + $response->cacheFor(1800);  | 
                                                        |
| 139 | + return $response;  | 
                                                        |
| 140 | + }  | 
                                                        |
| 141 | +  | 
                                                        |
| 142 | +        try { | 
                                                        |
| 143 | + $avatar = $this->avatarManager->getAvatar($userId);  | 
                                                        |
| 144 | + $avatarFile = $avatar->getFile($size);  | 
                                                        |
| 145 | + $response = new FileDisplayResponse(  | 
                                                        |
| 146 | + $avatarFile,  | 
                                                        |
| 147 | + $avatar->isCustomAvatar() ? Http::STATUS_OK : Http::STATUS_CREATED,  | 
                                                        |
| 148 | + ['Content-Type' => $avatarFile->getMimeType()]  | 
                                                        |
| 149 | + );  | 
                                                        |
| 150 | +        } catch (\Exception $e) { | 
                                                        |
| 151 | + return new JSONResponse([], Http::STATUS_NOT_FOUND);  | 
                                                        |
| 152 | + }  | 
                                                        |
| 153 | +  | 
                                                        |
| 154 | + // Cache for 30 minutes  | 
                                                        |
| 155 | + $response->cacheFor(1800);  | 
                                                        |
| 156 | + return $response;  | 
                                                        |
| 157 | + }  | 
                                                        |
| 158 | +  | 
                                                        |
| 159 | + /**  | 
                                                        |
| 160 | + * @NoAdminRequired  | 
                                                        |
| 161 | + *  | 
                                                        |
| 162 | + * @param string $path  | 
                                                        |
| 163 | + * @return JSONResponse  | 
                                                        |
| 164 | + */  | 
                                                        |
| 165 | +    public function postAvatar($path) { | 
                                                        |
| 166 | +        $files = $this->request->getUploadedFile('files'); | 
                                                        |
| 167 | +  | 
                                                        |
| 168 | +        if (isset($path)) { | 
                                                        |
| 169 | + $path = stripslashes($path);  | 
                                                        |
| 170 | + $userFolder = $this->rootFolder->getUserFolder($this->userId);  | 
                                                        |
| 171 | + /** @var File $node */  | 
                                                        |
| 172 | + $node = $userFolder->get($path);  | 
                                                        |
| 173 | +            if (!($node instanceof File)) { | 
                                                        |
| 174 | +                return new JSONResponse(['data' => ['message' => $this->l->t('Please select a file.')]]); | 
                                                        |
| 175 | + }  | 
                                                        |
| 176 | +            if ($node->getSize() > 20*1024*1024) { | 
                                                        |
| 177 | + return new JSONResponse(  | 
                                                        |
| 178 | +                    ['data' => ['message' => $this->l->t('File is too big')]], | 
                                                        |
| 179 | + Http::STATUS_BAD_REQUEST  | 
                                                        |
| 180 | + );  | 
                                                        |
| 181 | + }  | 
                                                        |
| 182 | +  | 
                                                        |
| 183 | +            if ($node->getMimeType() !== 'image/jpeg' && $node->getMimeType() !== 'image/png') { | 
                                                        |
| 184 | + return new JSONResponse(  | 
                                                        |
| 185 | +                    ['data' => ['message' => $this->l->t('The selected file is not an image.')]], | 
                                                        |
| 186 | + Http::STATUS_BAD_REQUEST  | 
                                                        |
| 187 | + );  | 
                                                        |
| 188 | + }  | 
                                                        |
| 189 | +  | 
                                                        |
| 190 | +            try { | 
                                                        |
| 191 | + $content = $node->getContent();  | 
                                                        |
| 192 | +            } catch (\OCP\Files\NotPermittedException $e) { | 
                                                        |
| 193 | + return new JSONResponse(  | 
                                                        |
| 194 | +                    ['data' => ['message' => $this->l->t('The selected file cannot be read.')]], | 
                                                        |
| 195 | + Http::STATUS_BAD_REQUEST  | 
                                                        |
| 196 | + );  | 
                                                        |
| 197 | + }  | 
                                                        |
| 198 | +        } elseif (!is_null($files)) { | 
                                                        |
| 199 | + if (  | 
                                                        |
| 200 | + $files['error'][0] === 0 &&  | 
                                                        |
| 201 | + is_uploaded_file($files['tmp_name'][0]) &&  | 
                                                        |
| 202 | + !\OC\Files\Filesystem::isFileBlacklisted($files['tmp_name'][0])  | 
                                                        |
| 203 | +            ) { | 
                                                        |
| 204 | +                if ($files['size'][0] > 20*1024*1024) { | 
                                                        |
| 205 | + return new JSONResponse(  | 
                                                        |
| 206 | +                        ['data' => ['message' => $this->l->t('File is too big')]], | 
                                                        |
| 207 | + Http::STATUS_BAD_REQUEST  | 
                                                        |
| 208 | + );  | 
                                                        |
| 209 | + }  | 
                                                        |
| 210 | +                $this->cache->set('avatar_upload', file_get_contents($files['tmp_name'][0]), 7200); | 
                                                        |
| 211 | +                $content = $this->cache->get('avatar_upload'); | 
                                                        |
| 212 | + unlink($files['tmp_name'][0]);  | 
                                                        |
| 213 | +            } else { | 
                                                        |
| 214 | + return new JSONResponse(  | 
                                                        |
| 215 | +                    ['data' => ['message' => $this->l->t('Invalid file provided')]], | 
                                                        |
| 216 | + Http::STATUS_BAD_REQUEST  | 
                                                        |
| 217 | + );  | 
                                                        |
| 218 | + }  | 
                                                        |
| 219 | +        } else { | 
                                                        |
| 220 | + //Add imgfile  | 
                                                        |
| 221 | + return new JSONResponse(  | 
                                                        |
| 222 | +                ['data' => ['message' => $this->l->t('No image or file provided')]], | 
                                                        |
| 223 | + Http::STATUS_BAD_REQUEST  | 
                                                        |
| 224 | + );  | 
                                                        |
| 225 | + }  | 
                                                        |
| 226 | +  | 
                                                        |
| 227 | +        try { | 
                                                        |
| 228 | + $image = new \OC_Image();  | 
                                                        |
| 229 | + $image->loadFromData($content);  | 
                                                        |
| 230 | + $image->readExif($content);  | 
                                                        |
| 231 | + $image->fixOrientation();  | 
                                                        |
| 232 | +  | 
                                                        |
| 233 | +            if ($image->valid()) { | 
                                                        |
| 234 | + $mimeType = $image->mimeType();  | 
                                                        |
| 235 | +                if ($mimeType !== 'image/jpeg' && $mimeType !== 'image/png') { | 
                                                        |
| 236 | + return new JSONResponse(  | 
                                                        |
| 237 | +                        ['data' => ['message' => $this->l->t('Unknown filetype')]], | 
                                                        |
| 238 | + Http::STATUS_OK  | 
                                                        |
| 239 | + );  | 
                                                        |
| 240 | + }  | 
                                                        |
| 241 | +  | 
                                                        |
| 242 | +                $this->cache->set('tmpAvatar', $image->data(), 7200); | 
                                                        |
| 243 | + return new JSONResponse(  | 
                                                        |
| 244 | + ['data' => 'notsquare'],  | 
                                                        |
| 245 | + Http::STATUS_OK  | 
                                                        |
| 246 | + );  | 
                                                        |
| 247 | +            } else { | 
                                                        |
| 248 | + return new JSONResponse(  | 
                                                        |
| 249 | +                    ['data' => ['message' => $this->l->t('Invalid image')]], | 
                                                        |
| 250 | + Http::STATUS_OK  | 
                                                        |
| 251 | + );  | 
                                                        |
| 252 | + }  | 
                                                        |
| 253 | +        } catch (\Exception $e) { | 
                                                        |
| 254 | + $this->logger->logException($e, ['app' => 'core']);  | 
                                                        |
| 255 | +            return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_OK); | 
                                                        |
| 256 | + }  | 
                                                        |
| 257 | + }  | 
                                                        |
| 258 | +  | 
                                                        |
| 259 | + /**  | 
                                                        |
| 260 | + * @NoAdminRequired  | 
                                                        |
| 261 | + *  | 
                                                        |
| 262 | + * @return JSONResponse  | 
                                                        |
| 263 | + */  | 
                                                        |
| 264 | +    public function deleteAvatar() { | 
                                                        |
| 265 | +        try { | 
                                                        |
| 266 | + $avatar = $this->avatarManager->getAvatar($this->userId);  | 
                                                        |
| 267 | + $avatar->remove();  | 
                                                        |
| 268 | + return new JSONResponse();  | 
                                                        |
| 269 | +        } catch (\Exception $e) { | 
                                                        |
| 270 | + $this->logger->logException($e, ['app' => 'core']);  | 
                                                        |
| 271 | +            return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); | 
                                                        |
| 272 | + }  | 
                                                        |
| 273 | + }  | 
                                                        |
| 274 | +  | 
                                                        |
| 275 | + /**  | 
                                                        |
| 276 | + * @NoAdminRequired  | 
                                                        |
| 277 | + *  | 
                                                        |
| 278 | + * @return JSONResponse|DataDisplayResponse  | 
                                                        |
| 279 | + */  | 
                                                        |
| 280 | +    public function getTmpAvatar() { | 
                                                        |
| 281 | +        $tmpAvatar = $this->cache->get('tmpAvatar'); | 
                                                        |
| 282 | +        if (is_null($tmpAvatar)) { | 
                                                        |
| 283 | + return new JSONResponse(['data' => [  | 
                                                        |
| 284 | +                                        'message' => $this->l->t("No temporary profile picture available, try again") | 
                                                        |
| 285 | + ]],  | 
                                                        |
| 286 | + Http::STATUS_NOT_FOUND);  | 
                                                        |
| 287 | + }  | 
                                                        |
| 288 | +  | 
                                                        |
| 289 | + $image = new \OC_Image();  | 
                                                        |
| 290 | + $image->loadFromData($tmpAvatar);  | 
                                                        |
| 291 | +  | 
                                                        |
| 292 | + $resp = new DataDisplayResponse($image->data(),  | 
                                                        |
| 293 | + Http::STATUS_OK,  | 
                                                        |
| 294 | + ['Content-Type' => $image->mimeType()]);  | 
                                                        |
| 295 | +  | 
                                                        |
| 296 | + $resp->setETag((string)crc32($image->data()));  | 
                                                        |
| 297 | + $resp->cacheFor(0);  | 
                                                        |
| 298 | +        $resp->setLastModified(new \DateTime('now', new \DateTimeZone('GMT'))); | 
                                                        |
| 299 | + return $resp;  | 
                                                        |
| 300 | + }  | 
                                                        |
| 301 | +  | 
                                                        |
| 302 | + /**  | 
                                                        |
| 303 | + * @NoAdminRequired  | 
                                                        |
| 304 | + *  | 
                                                        |
| 305 | + * @param array $crop  | 
                                                        |
| 306 | + * @return JSONResponse  | 
                                                        |
| 307 | + */  | 
                                                        |
| 308 | +    public function postCroppedAvatar($crop) { | 
                                                        |
| 309 | +        if (is_null($crop)) { | 
                                                        |
| 310 | +            return new JSONResponse(['data' => ['message' => $this->l->t("No crop data provided")]], | 
                                                        |
| 311 | + Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 312 | + }  | 
                                                        |
| 313 | +  | 
                                                        |
| 314 | +        if (!isset($crop['x'], $crop['y'], $crop['w'], $crop['h'])) { | 
                                                        |
| 315 | +            return new JSONResponse(['data' => ['message' => $this->l->t("No valid crop data provided")]], | 
                                                        |
| 316 | + Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 317 | + }  | 
                                                        |
| 318 | +  | 
                                                        |
| 319 | +        $tmpAvatar = $this->cache->get('tmpAvatar'); | 
                                                        |
| 320 | +        if (is_null($tmpAvatar)) { | 
                                                        |
| 321 | + return new JSONResponse(['data' => [  | 
                                                        |
| 322 | +                                        'message' => $this->l->t("No temporary profile picture available, try again") | 
                                                        |
| 323 | + ]],  | 
                                                        |
| 324 | + Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 325 | + }  | 
                                                        |
| 326 | +  | 
                                                        |
| 327 | + $image = new \OC_Image();  | 
                                                        |
| 328 | + $image->loadFromData($tmpAvatar);  | 
                                                        |
| 329 | + $image->crop($crop['x'], $crop['y'], (int)round($crop['w']), (int)round($crop['h']));  | 
                                                        |
| 330 | +        try { | 
                                                        |
| 331 | + $avatar = $this->avatarManager->getAvatar($this->userId);  | 
                                                        |
| 332 | + $avatar->set($image);  | 
                                                        |
| 333 | + // Clean up  | 
                                                        |
| 334 | +            $this->cache->remove('tmpAvatar'); | 
                                                        |
| 335 | + return new JSONResponse(['status' => 'success']);  | 
                                                        |
| 336 | +        } catch (\OC\NotSquareException $e) { | 
                                                        |
| 337 | +            return new JSONResponse(['data' => ['message' => $this->l->t('Crop is not square')]], | 
                                                        |
| 338 | + Http::STATUS_BAD_REQUEST);  | 
                                                        |
| 339 | +        } catch (\Exception $e) { | 
                                                        |
| 340 | + $this->logger->logException($e, ['app' => 'core']);  | 
                                                        |
| 341 | +            return new JSONResponse(['data' => ['message' => $this->l->t('An error occurred. Please contact your admin.')]], Http::STATUS_BAD_REQUEST); | 
                                                        |
| 342 | + }  | 
                                                        |
| 343 | + }  | 
                                                        |
| 344 | 344 | }  |