request->urlParams; $etag = null; $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); $tempPhoto = TemporaryPhoto::create( $this->server, TemporaryPhoto::PHOTO_CURRENT, $contact ); $image = $tempPhoto->getPhoto(); if($image->valid()) { $response = new ImageResponse($image); $lastModified = $contact->lastModified(); // Force refresh if modified within the last minute. if(!is_null($lastModified)) { $response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null); } if(!is_null($etag)) { $response->setETag($etag); } if ($image->width() > $maxSize || $image->height() > $maxSize) { $image->resize($maxSize); } return $response; } else { throw new \Exception(App::$l10n->t('Error getting user photo')); } } /** * Uploads a photo and saves in oC cache * @return JSONResponse with data.tmp set to the key in the cache. * * @NoAdminRequired */ public function uploadPhoto() { $params = $this->request->urlParams; $response = new JSONResponse(); $tempPhoto = TemporaryPhoto::create( $this->server, TemporaryPhoto::PHOTO_UPLOADED, $this->request ); return $response->setParams(array( 'tmp'=>$tempPhoto->getKey(), 'metadata' => array( 'contactId'=> $params['contactId'], 'addressBookId'=> $params['addressBookId'], 'backend'=> $params['backend'], ), )); } /** * Saves the photo from the contact being edited to oC cache * @return JSONResponse with data.tmp set to the key in the cache. * * @NoAdminRequired * @NoCSRFRequired */ public function cacheCurrentPhoto() { $params = $this->request->urlParams; $response = new JSONResponse(); $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); $tempPhoto = TemporaryPhoto::create( $this->server, TemporaryPhoto::PHOTO_CURRENT, $contact ); return $response->setParams(array( 'tmp'=>$tempPhoto->getKey(), 'metadata' => array( 'contactId'=> $params['contactId'], 'addressBookId'=> $params['addressBookId'], 'backend'=> $params['backend'], ), )); } /** * Saves the photo from ownCloud FS to oC cache * @return JSONResponse with data.tmp set to the key in the cache. * * @NoAdminRequired * @NoCSRFRequired */ public function cacheFileSystemPhoto() { $params = $this->request->urlParams; $response = new JSONResponse(); if(!isset($this->request->get['path'])) { $response->bailOut(App::$l10n->t('No photo path was submitted.')); } $tempPhoto = TemporaryPhoto::create( $this->server, TemporaryPhoto::PHOTO_FILESYSTEM, $this->request->get['path'] ); return $response->setParams(array( 'tmp'=>$tempPhoto->getKey(), 'metadata' => array( 'contactId'=> $params['contactId'], 'addressBookId'=> $params['addressBookId'], 'backend'=> $params['backend'], ), )); } /** * Get a photo from the oC cache for cropping. * @NoAdminRequired * @NoCSRFRequired */ public function getTempPhoto() { $params = $this->request->urlParams; $tmpkey = $params['key']; $tmpPhoto = new TemporaryPhoto($this->server, $tmpkey); $image = $tmpPhoto->getPhoto(); if($image->valid()) { $response = new ImageResponse($image); return $response; } else { $response = new JSONResponse(); return $response->bailOut('Error getting temporary photo'); } } /** * Get a photo from the oC and crops it with the suplied geometry. * @NoAdminRequired * @NoCSRFRequired */ public function cropPhoto() { $params = $this->request->urlParams; $x = (isset($this->request->post['x']) && $this->request->post['x']) ? $this->request->post['x'] : 0; $y = (isset($this->request->post['y']) && $this->request->post['y']) ? $this->request->post['y'] : 0; $w = (isset($this->request->post['w']) && $this->request->post['w']) ? $this->request->post['w'] : -1; $h = (isset($this->request->post['h']) && $this->request->post['h']) ? $this->request->post['h'] : -1; $tmpkey = $params['key']; $app = new App($this->api->getUserId()); $addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); $response = new JSONResponse(); $tmpPhoto = new TemporaryPhoto($this->server, $tmpkey); $image = $tmpPhoto->getPhoto(); if(!$image || !$image->valid()) { return $response->bailOut(App::$l10n->t('Error loading image from cache')); } $w = ($w !== -1 ? $w : $image->width()); $h = ($h !== -1 ? $h : $image->height()); $image->crop($x, $y, $w, $h); if (!$contact->setPhoto($image)) { $tmpPhoto->remove($tmpkey); return $response->bailOut(App::$l10n->t('Error getting PHOTO property.')); } if(!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact.')); } $thumbnail = Properties::cacheThumbnail( $params['backend'], $params['addressBookId'], $params['contactId'], $image ); $response->setData(array( 'status' => 'success', 'data' => array( 'id' => $params['contactId'], 'thumbnail' => $thumbnail, ) )); $tmpPhoto->remove($tmpkey); return $response; } }