2013-09-04 08:03:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Thomas Tanghus
|
2014-01-26 00:40:22 +01:00
|
|
|
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
|
2013-09-04 08:03:33 +02:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Contacts\Controller;
|
|
|
|
|
|
|
|
use OCA\Contacts\App,
|
|
|
|
OCA\Contacts\JSONResponse,
|
|
|
|
OCA\Contacts\ImageResponse,
|
|
|
|
OCA\Contacts\Utils\Properties,
|
2014-03-20 01:55:48 +01:00
|
|
|
OCA\Contacts\Utils\TemporaryPhoto,
|
2013-09-17 18:46:59 +02:00
|
|
|
OCA\Contacts\Controller;
|
2013-09-04 08:03:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller class For Contacts
|
|
|
|
*/
|
2013-09-17 18:46:59 +02:00
|
|
|
class ContactPhotoController extends Controller {
|
2013-09-04 08:03:33 +02:00
|
|
|
|
|
|
|
/**
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2013-09-04 08:03:33 +02:00
|
|
|
*/
|
|
|
|
public function getPhoto($maxSize = 170) {
|
|
|
|
// TODO: Cache resized photo
|
|
|
|
$params = $this->request->urlParams;
|
|
|
|
$etag = null;
|
|
|
|
|
2013-10-03 04:11:54 +02:00
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
|
|
|
|
$contact = $addressBook->getChild($params['contactId']);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
|
|
|
if(!$contact) {
|
|
|
|
$response = new JSONResponse();
|
|
|
|
$response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
$image = new \OCP\Image();
|
|
|
|
if (isset($contact->PHOTO) && $image->loadFromBase64((string)$contact->PHOTO)) {
|
|
|
|
// OK
|
|
|
|
$etag = md5($contact->PHOTO);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
// Logo :-/
|
|
|
|
if(isset($contact->LOGO) && $image->loadFromBase64((string)$contact->LOGO)) {
|
|
|
|
// OK
|
|
|
|
$etag = md5($contact->LOGO);
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
$response = new JSONResponse();
|
|
|
|
$response->bailOut(App::$l10n->t('Error getting user photo'));
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads a photo and saves in oC cache
|
|
|
|
* @return JSONResponse with data.tmp set to the key in the cache.
|
|
|
|
*
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2013-09-04 08:03:33 +02:00
|
|
|
*/
|
|
|
|
public function uploadPhoto() {
|
|
|
|
$params = $this->request->urlParams;
|
|
|
|
|
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
$tempPhoto = TemporaryPhoto::get(
|
|
|
|
$this->server,
|
|
|
|
TemporaryPhoto::PHOTO_UPLOADED,
|
|
|
|
$this->request
|
|
|
|
);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
$response = new JSONResponse();
|
2013-09-04 08:03:33 +02:00
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
return $response->setParams(array(
|
|
|
|
'tmp'=>$tempPhoto->getKey(),
|
|
|
|
'metadata' => array(
|
|
|
|
'contactId'=> $params['contactId'],
|
|
|
|
'addressBookId'=> $params['addressBookId'],
|
|
|
|
'backend'=> $params['backend'],
|
|
|
|
),
|
2013-09-04 08:03:33 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the photo from the contact being edited to oC cache
|
|
|
|
* @return JSONResponse with data.tmp set to the key in the cache.
|
|
|
|
*
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2013-09-04 08:03:33 +02:00
|
|
|
*/
|
|
|
|
public function cacheCurrentPhoto() {
|
|
|
|
$params = $this->request->urlParams;
|
|
|
|
$response = new JSONResponse();
|
2014-02-27 11:03:06 +01:00
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
|
|
|
|
$contact = $addressBook->getChild($params['contactId']);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
$tempPhoto = TemporaryPhoto::get(
|
|
|
|
$this->server,
|
|
|
|
TemporaryPhoto::PHOTO_CURRENT,
|
|
|
|
$contact
|
|
|
|
);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
return $response->setParams(array(
|
|
|
|
'tmp'=>$tempPhoto->getKey(),
|
2013-09-04 08:03:33 +02:00
|
|
|
'metadata' => array(
|
2013-10-03 04:11:54 +02:00
|
|
|
'contactId'=> $params['contactId'],
|
|
|
|
'addressBookId'=> $params['addressBookId'],
|
2013-09-04 08:03:33 +02:00
|
|
|
'backend'=> $params['backend'],
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves the photo from ownCloud FS to oC cache
|
|
|
|
* @return JSONResponse with data.tmp set to the key in the cache.
|
|
|
|
*
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2013-09-04 08:03:33 +02:00
|
|
|
*/
|
|
|
|
public function cacheFileSystemPhoto() {
|
|
|
|
$params = $this->request->urlParams;
|
2014-02-27 11:03:06 +01:00
|
|
|
$maxSize = isset($this->request->get['maxSize']) ? $this->request->get['maxSize'] : 400;
|
2013-09-04 08:03:33 +02:00
|
|
|
$response = new JSONResponse();
|
|
|
|
|
|
|
|
if(!isset($this->request->get['path'])) {
|
|
|
|
$response->bailOut(App::$l10n->t('No photo path was submitted.'));
|
|
|
|
}
|
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
$tempPhoto = TemporaryPhoto::get(
|
|
|
|
$this->server,
|
|
|
|
TemporaryPhoto::PHOTO_FILESYSTEM,
|
|
|
|
$this->request->get['path']
|
|
|
|
);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
2014-03-20 01:55:48 +01:00
|
|
|
return $response->setParams(array(
|
|
|
|
'tmp'=>$tempPhoto->getKey(),
|
2013-09-04 08:03:33 +02:00
|
|
|
'metadata' => array(
|
2013-10-03 04:11:54 +02:00
|
|
|
'contactId'=> $params['contactId'],
|
|
|
|
'addressBookId'=> $params['addressBookId'],
|
2013-09-04 08:03:33 +02:00
|
|
|
'backend'=> $params['backend'],
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a photo from the oC cache for cropping.
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2013-09-04 08:03:33 +02:00
|
|
|
*/
|
|
|
|
public function getTempPhoto() {
|
|
|
|
$params = $this->request->urlParams;
|
|
|
|
$tmpkey = $params['key'];
|
|
|
|
|
|
|
|
$image = new \OCP\Image();
|
2013-09-24 13:39:45 +02:00
|
|
|
$image->loadFromData($this->server->getCache()->get($tmpkey));
|
2013-09-04 08:03:33 +02:00
|
|
|
if($image->valid()) {
|
|
|
|
$response = new ImageResponse($image);
|
|
|
|
return $response;
|
|
|
|
} else {
|
|
|
|
$response = new JSONResponse();
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut('Error getting temporary photo');
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a photo from the oC and crops it with the suplied geometry.
|
2013-09-24 13:39:45 +02:00
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
2013-09-04 08:03:33 +02:00
|
|
|
*/
|
|
|
|
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());
|
2013-10-03 04:11:54 +02:00
|
|
|
$addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']);
|
|
|
|
$contact = $addressBook->getChild($params['contactId']);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
|
|
|
$response = new JSONResponse();
|
|
|
|
|
|
|
|
if(!$contact) {
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
|
|
|
|
2013-09-24 13:39:45 +02:00
|
|
|
$data = $this->server->getCache()->get($tmpkey);
|
2013-09-04 08:03:33 +02:00
|
|
|
if(!$data) {
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut(App::$l10n->t('Image has been removed from cache'));
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$image = new \OCP\Image();
|
|
|
|
|
|
|
|
if(!$image->loadFromData($data)) {
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut(App::$l10n->t('Error creating temporary image'));
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$w = ($w !== -1 ? $w : $image->width());
|
|
|
|
$h = ($h !== -1 ? $h : $image->height());
|
|
|
|
|
|
|
|
if(!$image->crop($x, $y, $w, $h)) {
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut(App::$l10n->t('Error cropping image'));
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// For vCard 3.0 the type must be e.g. JPEG or PNG
|
|
|
|
// For version 4.0 the full mimetype should be used.
|
|
|
|
// https://tools.ietf.org/html/rfc2426#section-3.1.4
|
|
|
|
if(strval($contact->VERSION) === '4.0') {
|
|
|
|
$type = $image->mimeType();
|
|
|
|
} else {
|
|
|
|
$type = explode('/', $image->mimeType());
|
|
|
|
$type = strtoupper(array_pop($type));
|
|
|
|
}
|
|
|
|
if(isset($contact->PHOTO)) {
|
|
|
|
$property = $contact->PHOTO;
|
|
|
|
if(!$property) {
|
2013-09-24 13:39:45 +02:00
|
|
|
$this->server->getCache()->remove($tmpkey);
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut(App::$l10n
|
2013-09-04 08:03:33 +02:00
|
|
|
->t('Error getting PHOTO property.'));
|
|
|
|
}
|
|
|
|
$property->setValue(strval($image));
|
|
|
|
$property->parameters = array();
|
|
|
|
$property->parameters[]
|
|
|
|
= new \Sabre\VObject\Parameter('ENCODING', 'b');
|
|
|
|
$property->parameters[]
|
|
|
|
= new \Sabre\VObject\Parameter('TYPE', $image->mimeType());
|
|
|
|
$contact->PHOTO = $property;
|
|
|
|
} else {
|
|
|
|
$contact->add('PHOTO',
|
|
|
|
strval($image), array('ENCODING' => 'b',
|
|
|
|
'TYPE' => $type));
|
2013-10-22 17:10:23 +02:00
|
|
|
// TODO: Fix this hack
|
|
|
|
$contact->setSaved(false);
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
|
|
|
if(!$contact->save()) {
|
2013-10-17 02:10:34 +02:00
|
|
|
return $response->bailOut(App::$l10n->t('Error saving contact.'));
|
2013-09-04 08:03:33 +02:00
|
|
|
}
|
2013-11-07 14:06:12 +01:00
|
|
|
|
|
|
|
$thumbnail = Properties::cacheThumbnail(
|
|
|
|
$params['backend'],
|
2013-11-16 12:00:15 +01:00
|
|
|
$params['addressBookId'],
|
2013-11-07 14:06:12 +01:00
|
|
|
$params['contactId'],
|
|
|
|
$image
|
|
|
|
);
|
|
|
|
|
2013-10-22 17:10:23 +02:00
|
|
|
$response->setData(array(
|
|
|
|
'status' => 'success',
|
|
|
|
'data' => array(
|
|
|
|
'id' => $params['contactId'],
|
|
|
|
'thumbnail' => $thumbnail,
|
|
|
|
)
|
2013-09-04 08:03:33 +02:00
|
|
|
));
|
|
|
|
|
2013-09-24 13:39:45 +02:00
|
|
|
$this->server->getCache()->remove($tmpkey);
|
2013-09-04 08:03:33 +02:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|