diff --git a/ajax/currentphoto.php b/ajax/currentphoto.php index b6b10770..605ec0d0 100644 --- a/ajax/currentphoto.php +++ b/ajax/currentphoto.php @@ -20,22 +20,39 @@ * */ +namespace OCA\Contacts; + // Firefox and Konqueror tries to download application/json for me. --Arthur -OCP\JSON::setContentTypeHeader('text/plain'); -OCP\JSON::checkLoggedIn(); -OCP\JSON::checkAppEnabled('contacts'); +\OCP\JSON::setContentTypeHeader('text/plain'); +\OCP\JSON::checkLoggedIn(); +\OCP\JSON::checkAppEnabled('contacts'); require_once 'loghandler.php'; -if (!isset($_GET['id'])) { - bailOut(OCA\Contacts\App::$l10n->t('No contact ID was submitted.')); +$contactid = isset($_GET['contactid']) ? $_GET['contactid'] : ''; +$addressbookid = isset($_GET['addressbookid']) ? $_GET['addressbookid'] : ''; +$backend = isset($_GET['backend']) ? $_GET['backend'] : ''; + +if(!$contactid) { + bailOut('Missing contact id.'); } -$contact = OCA\Contacts\App::getContactVCard($_GET['id']); +if(!$addressbookid) { + bailOut('Missing address book id.'); +} + +$app = new App(); +// FIXME: Get backend and addressbookid +$contact = $app->getContact($backend, $addressbookid, $contactid); +if(!$contact) { + \OC_Cache::remove($tmpkey); + bailOut(App::$l10n + ->t('Error getting contact object.')); +} // invalid vcard -if( is_null($contact)) { - bailOut(OCA\Contacts\App::$l10n->t('Error reading contact photo.')); +if(!$contact) { + bailOut(App::$l10n->t('Error reading contact photo.')); } else { - $image = new OC_Image(); + $image = new \OC_Image(); if(!isset($contact->PHOTO) || !$image->loadFromBase64((string)$contact->PHOTO)) { if(isset($contact->LOGO)) { $image->loadFromBase64((string)$contact->LOGO); @@ -43,13 +60,13 @@ if( is_null($contact)) { } if($image->valid()) { $tmpkey = 'contact-photo-'.$contact->UID; - if(OC_Cache::set($tmpkey, $image->data(), 600)) { - OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey))); + if(\OC_Cache::set($tmpkey, $image->data(), 600)) { + \OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey))); exit(); } else { - bailOut(OCA\Contacts\App::$l10n->t('Error saving temporary file.')); + bailOut(App::$l10n->t('Error saving temporary file.')); } } else { - bailOut(OCA\Contacts\App::$l10n->t('The loading photo is not valid.')); + bailOut(App::$l10n->t('The loading photo is not valid.')); } } diff --git a/ajax/savecrop.php b/ajax/savecrop.php index a38a41b1..68350d4b 100644 --- a/ajax/savecrop.php +++ b/ajax/savecrop.php @@ -47,10 +47,14 @@ if($tmpkey == '') { bailOut('Missing key to temporary file.'); } -if($id == '') { +if($contactid == '') { bailOut('Missing contact id.'); } +if($addressbookid == '') { + bailOut('Missing address book id.'); +} + \OCP\Util::writeLog('contacts', 'savecrop.php: key: '.$tmpkey, \OCP\Util::DEBUG); $app = new App(); @@ -75,6 +79,12 @@ if($data) { if(($image->width() <= 200 && $image->height() <= 200) || $image->resize(200)) { + // 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 + $type = strval($contact->VERSION) === '4.0' + ? $image->mimeType() + : strtoupper(array_pop(explode('/', $image->mimeType()))); if(isset($contact->PHOTO)) { \OCP\Util::writeLog('contacts', 'savecrop.php: PHOTO property exists.', @@ -86,23 +96,20 @@ if($data) { ->t('Error getting PHOTO property.')); } $property->setValue(strval($image)); + $property->parameters = []; + /*$property->ENCODING = 'b'; + $property->TYPE = $type;*/ $property->parameters[] - = new Sabre\VObject\Parameter('ENCODING', 'b'); + = new \Sabre\VObject\Parameter('ENCODING', 'b'); $property->parameters[] - = new Sabre\VObject\Parameter('TYPE', $image->mimeType()); + = new \Sabre\VObject\Parameter('TYPE', $image->mimeType()); $contact->PHOTO = $property; } else { \OCP\Util::writeLog('contacts', 'savecrop.php: files: Adding PHOTO property.', \OCP\Util::DEBUG); - // 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 - $type = strval($contact->VERSION) === '4.0' - ? $image->mimeType() - : strtoupper(array_pop(explode('/', $image->mimeType()))); $contact->add('PHOTO', - $image->__toString(), array('ENCODING' => 'b', + strval($image), array('ENCODING' => 'b', 'TYPE' => $type)); } if(!$contact->save()) { @@ -111,10 +118,8 @@ if($data) { $thumbnail = $contact->cacheThumbnail($image); \OCP\JSON::success(array( 'data' => array( - 'width' => $image->width(), - 'height' => $image->height(), + 'id' => $contactid, 'thumbnail' => $thumbnail, - 'lastmodified' => App::lastModified($contact)->format('U') ) )); } else { diff --git a/ajax/uploadphoto.php b/ajax/uploadphoto.php index a26560ba..ab76fa04 100644 --- a/ajax/uploadphoto.php +++ b/ajax/uploadphoto.php @@ -29,13 +29,22 @@ OCP\JSON::callCheck(); OCP\JSON::setContentTypeHeader('text/plain; charset=utf-8'); require_once 'loghandler.php'; $l10n = OCA\Contacts\App::$l10n; + +$contactid = isset($_POST['contactid']) ? $_POST['contactid'] : ''; +$addressbookid = isset($_POST['addressbookid']) ? $_POST['addressbookid'] : ''; +$backend = isset($_POST['backend']) ? $_POST['backend'] : ''; + +if($contactid == '') { + bailOut('Missing contact id.'); +} + +if($addressbookid == '') { + bailOut('Missing address book id.'); +} + // If it is a Drag'n'Drop transfer it's handled here. $fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false); if ($fn) { - if (!isset($_GET['id'])) { - bailOut($l10n->t('No contact ID was submitted.')); - } - $id = $_GET['id']; $tmpkey = 'contact-photo-'.md5($fn); $data = file_get_contents('php://input'); $image = new OC_Image(); @@ -50,10 +59,14 @@ if ($fn) { if(OC_Cache::set($tmpkey, $image->data(), 600)) { OCP\JSON::success(array( 'data' => array( - 'mime'=>$_SERVER['CONTENT_TYPE'], - 'name'=>$fn, - 'id'=>$id, - 'tmp'=>$tmpkey))); + 'mime'=> $_SERVER['CONTENT_TYPE'], + 'name'=> $fn, + 'contactid'=> $id, + 'addressbookid'=> addressbookid, + 'backend'=> $backend, + 'tmp'=>$tmpkey + )) + ); exit(); } else { bailOut($l10n->t('Couldn\'t save temporary image: ').$tmpkey); @@ -63,10 +76,6 @@ if ($fn) { } } -// Uploads from file dialog are handled here. -if (!isset($_POST['id'])) { - bailOut($l10n->t('No contact ID was submitted.')); -} if (!isset($_FILES['imagefile'])) { bailOut($l10n->t('No file was uploaded. Unknown error')); } @@ -101,9 +110,14 @@ if(file_exists($file['tmp_name'])) { 'mime'=>$file['type'], 'size'=>$file['size'], 'name'=>$file['name'], - 'id'=>$_POST['id'], 'tmp'=>$tmpkey, - ))); + ), + 'metadata' => array( + 'contactid'=> $contactid, + 'addressbookid'=> $addressbookid, + 'backend'=> $backend, + ), + )); exit(); } else { bailOut($l10n->t('Couldn\'t save temporary image: ').$tmpkey); diff --git a/appinfo/classpath.php b/appinfo/classpath.php new file mode 100644 index 00000000..3dee4b79 --- /dev/null +++ b/appinfo/classpath.php @@ -0,0 +1,28 @@ + $('#max_upload').val()){ OC.notify({ @@ -1602,7 +1608,10 @@ OC.Contacts = OC.Contacts || { var response=jQuery.parseJSON(target.contents().text()); if(response != undefined && response.status == 'success') { console.log('response', response); - self.editPhoto(self.currentid, response.data.tmp); + self.editPhoto( + response.metadata, + response.data.tmp + ); //alert('File: ' + file.tmp + ' ' + file.name + ' ' + file.mime); } else { OC.notify({message:response.data.message}); @@ -1619,7 +1628,7 @@ OC.Contacts = OC.Contacts || { if(jsondata.status == 'success') { //alert(jsondata.data.page); self.editPhoto(metadata, jsondata.data.tmp); - $('#edit_photo_dialog_img').html(jsondata.data.page); + //$('#edit_photo_dialog_img').html(jsondata.data.page); } else{ OC.notify({message: jsondata.data.message}); @@ -1666,7 +1675,7 @@ OC.Contacts = OC.Contacts || { var $dlg = this.$cropBoxTmpl.octemplate( { backend: metadata.backend, - addressbookid: metadata.parent, + addressbookid: metadata.addressbookid, contactid: metadata.contactid, tmpkey: tmpkey }); @@ -1709,25 +1718,26 @@ OC.Contacts = OC.Contacts || { }); }).error(function () { OC.notify({message:t('contacts','Error loading profile picture.')}); - }).attr('src', OC.linkTo('contacts', 'tmpphoto.php')+'?tmpkey='+tmpkey); + }).attr('src', OC.linkTo('contacts', 'tmpphoto.php')+'?tmpkey='+tmpkey+'&refresh='+Math.random()); }, savePhoto:function($dlg) { var form = $dlg.find('#cropform'); q = form.serialize(); console.log('savePhoto', q); $.post(OC.filePath('contacts', 'ajax', 'savecrop.php'), q, function(response) { - var jsondata = $.parseJSON(response); - console.log('savePhoto, jsondata', typeof jsondata); - if(jsondata && jsondata.status === 'success') { + //var jsondata = $.parseJSON(response); + console.log('savePhoto, response', typeof response); + if(response && response.status === 'success') { // load cropped photo. $(document).trigger('status.contact.photoupdated', { - id: jsondata.data.id + id: response.data.id, + thumbnail: response.data.thumbnail }); } else { - if(!jsondata) { + if(!response) { OC.notify({message:t('contacts', 'Network or server error. Please inform administrator.')}); } else { - OC.notify({message: jsondata.data.message}); + OC.notify({message: response.data.message}); } } });