2012-02-06 07:32:57 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Addressbook
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
|
|
|
* @copyright 2012 Thomas Tanghus <thomas@tanghus.net>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// Check if we are a user
|
2012-05-03 12:23:29 +02:00
|
|
|
OCP\JSON::checkLoggedIn();
|
|
|
|
OCP\JSON::checkAppEnabled('contacts');
|
2012-06-13 17:35:42 +02:00
|
|
|
OCP\JSON::callCheck();
|
2012-02-06 07:32:57 +01:00
|
|
|
|
2012-08-02 19:35:38 +02:00
|
|
|
// Firefox and Konqueror tries to download application/json for me. --Arthur
|
2012-08-02 19:53:22 +02:00
|
|
|
OCP\JSON::setContentTypeHeader('text/plain; charset=utf-8');
|
2012-08-02 19:35:38 +02:00
|
|
|
|
2012-07-19 23:48:17 +02:00
|
|
|
require_once 'loghandler.php';
|
2012-02-06 07:32:57 +01:00
|
|
|
|
|
|
|
$image = null;
|
|
|
|
|
2012-04-22 01:50:46 +02:00
|
|
|
$x1 = (isset($_POST['x1']) && $_POST['x1']) ? $_POST['x1'] : 0;
|
2012-02-06 07:32:57 +01:00
|
|
|
//$x2 = isset($_POST['x2']) ? $_POST['x2'] : -1;
|
2012-04-22 01:50:46 +02:00
|
|
|
$y1 = (isset($_POST['y1']) && $_POST['y1']) ? $_POST['y1'] : 0;
|
2012-02-06 07:32:57 +01:00
|
|
|
//$y2 = isset($_POST['y2']) ? $_POST['y2'] : -1;
|
|
|
|
$w = (isset($_POST['w']) && $_POST['w']) ? $_POST['w'] : -1;
|
|
|
|
$h = (isset($_POST['h']) && $_POST['h']) ? $_POST['h'] : -1;
|
2012-06-05 20:30:22 +02:00
|
|
|
$tmpkey = isset($_POST['tmpkey']) ? $_POST['tmpkey'] : '';
|
2012-02-06 07:32:57 +01:00
|
|
|
$id = isset($_POST['id']) ? $_POST['id'] : '';
|
|
|
|
|
2012-06-05 20:30:22 +02:00
|
|
|
if($tmpkey == '') {
|
|
|
|
bailOut('Missing key to temporary file.');
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($id == '') {
|
|
|
|
bailOut('Missing contact id.');
|
|
|
|
}
|
|
|
|
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::writeLog('contacts', 'savecrop.php: key: '.$tmpkey, OCP\Util::DEBUG);
|
2012-02-06 07:32:57 +01:00
|
|
|
|
2012-06-05 20:30:22 +02:00
|
|
|
$data = OC_Cache::get($tmpkey);
|
|
|
|
if($data) {
|
2012-02-06 07:32:57 +01:00
|
|
|
$image = new OC_Image();
|
2012-06-05 20:30:22 +02:00
|
|
|
if($image->loadFromdata($data)) {
|
2012-04-22 01:50:46 +02:00
|
|
|
$w = ($w != -1 ? $w : $image->width());
|
|
|
|
$h = ($h != -1 ? $h : $image->height());
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::writeLog('contacts',
|
2012-08-02 05:02:36 +02:00
|
|
|
'savecrop.php, x: '.$x1.' y: '.$y1.' w: '.$w.' h: '.$h,
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::DEBUG);
|
2012-02-06 07:32:57 +01:00
|
|
|
if($image->crop($x1, $y1, $w, $h)) {
|
2012-08-02 05:02:36 +02:00
|
|
|
if(($image->width() <= 200 && $image->height() <= 200)
|
2012-10-22 23:11:36 +02:00
|
|
|
|| $image->resize(200)) {
|
|
|
|
|
2012-10-25 03:34:12 +02:00
|
|
|
$vcard = OCA\Contacts\App::getContactVCard($id);
|
2012-08-09 16:31:04 +02:00
|
|
|
if(!$vcard) {
|
2012-06-05 20:30:22 +02:00
|
|
|
OC_Cache::remove($tmpkey);
|
2012-10-25 03:34:12 +02:00
|
|
|
bailOut(OCA\Contacts\App::$l10n
|
2012-08-02 05:02:36 +02:00
|
|
|
->t('Error getting contact object.'));
|
2012-06-05 20:30:22 +02:00
|
|
|
}
|
2012-08-09 16:31:04 +02:00
|
|
|
if($vcard->__isset('PHOTO')) {
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::writeLog('contacts',
|
2012-08-02 05:02:36 +02:00
|
|
|
'savecrop.php: PHOTO property exists.',
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::DEBUG);
|
2012-08-09 16:31:04 +02:00
|
|
|
$property = $vcard->__get('PHOTO');
|
2012-06-05 20:30:22 +02:00
|
|
|
if(!$property) {
|
|
|
|
OC_Cache::remove($tmpkey);
|
2012-10-25 03:34:12 +02:00
|
|
|
bailOut(OCA\Contacts\App::$l10n
|
2012-08-02 05:02:36 +02:00
|
|
|
->t('Error getting PHOTO property.'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
2012-06-05 20:30:22 +02:00
|
|
|
$property->setValue($image->__toString());
|
2012-08-02 05:02:36 +02:00
|
|
|
$property->parameters[]
|
2012-10-17 14:32:18 +02:00
|
|
|
= new Sabre\VObject\Parameter('ENCODING', 'b');
|
2012-08-02 05:02:36 +02:00
|
|
|
$property->parameters[]
|
2012-10-17 14:32:18 +02:00
|
|
|
= new Sabre\VObject\Parameter('TYPE', $image->mimeType());
|
2012-08-09 16:31:04 +02:00
|
|
|
$vcard->__set('PHOTO', $property);
|
2012-02-06 07:32:57 +01:00
|
|
|
} else {
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::writeLog('contacts',
|
2012-08-02 05:02:36 +02:00
|
|
|
'savecrop.php: files: Adding PHOTO property.',
|
2012-07-19 23:48:17 +02:00
|
|
|
OCP\Util::DEBUG);
|
2012-08-09 16:31:04 +02:00
|
|
|
$vcard->addProperty('PHOTO',
|
2012-08-02 05:02:36 +02:00
|
|
|
$image->__toString(), array('ENCODING' => 'b',
|
2012-07-19 23:48:17 +02:00
|
|
|
'TYPE' => $image->mimeType()));
|
2012-06-05 20:30:22 +02:00
|
|
|
}
|
|
|
|
$now = new DateTime;
|
2012-08-09 16:31:04 +02:00
|
|
|
$vcard->setString('REV', $now->format(DateTime::W3C));
|
2012-10-25 03:34:12 +02:00
|
|
|
if(!OCA\Contacts\VCard::edit($id, $vcard)) {
|
|
|
|
bailOut(OCA\Contacts\App::$l10n->t('Error saving contact.'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
2012-10-25 03:34:12 +02:00
|
|
|
OCA\Contacts\App::cacheThumbnail($id, $image);
|
2012-08-09 16:31:04 +02:00
|
|
|
OCP\JSON::success(array(
|
|
|
|
'data' => array(
|
|
|
|
'id' => $id,
|
|
|
|
'width' => $image->width(),
|
|
|
|
'height' => $image->height(),
|
2012-10-25 03:34:12 +02:00
|
|
|
'lastmodified' => OCA\Contacts\App::lastModified($vcard)->format('U')
|
2012-08-09 16:31:04 +02:00
|
|
|
)
|
|
|
|
));
|
2012-02-06 07:32:57 +01:00
|
|
|
} else {
|
2012-10-25 03:34:12 +02:00
|
|
|
bailOut(OCA\Contacts\App::$l10n->t('Error resizing image'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-25 03:34:12 +02:00
|
|
|
bailOut(OCA\Contacts\App::$l10n->t('Error cropping image'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-25 03:34:12 +02:00
|
|
|
bailOut(OCA\Contacts\App::$l10n->t('Error creating temporary image'));
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
} else {
|
2012-10-25 03:34:12 +02:00
|
|
|
bailOut(OCA\Contacts\App::$l10n->t('Error finding image: ').$tmpkey);
|
2012-02-06 07:32:57 +01:00
|
|
|
}
|
|
|
|
|
2012-06-05 20:30:22 +02:00
|
|
|
OC_Cache::remove($tmpkey);
|