Beispiel #1
0
 /**
  * @brief Data structure of vCard
  * @param VObject\VCard $contact
  * @return associative array|null
  */
 public static function serializeContact(Contact $contact)
 {
     if (!$contact->retrieve()) {
         \OCP\Util::writeLog('contacts', __METHOD__ . ' error reading: ' . print_r($contact, true), \OCP\Util::DEBUG);
         return null;
     }
     $details = array();
     if (isset($contact->PHOTO) || isset($contact->LOGO)) {
         $details['photo'] = true;
         $details['thumbnail'] = Properties::cacheThumbnail($contact->getBackend()->name, $contact->getParent()->getId(), $contact->getId(), null, $contact);
     }
     foreach ($contact->children as $property) {
         $pname = $property->name;
         $temp = self::serializeProperty($property);
         if (!is_null($temp)) {
             // Get Apple X-ABLabels
             if (isset($contact->{$property->group . '.X-ABLABEL'})) {
                 $temp['label'] = $contact->{$property->group . '.X-ABLABEL'}->value;
                 if ($temp['label'] == '_$!<Other>!$_') {
                     $temp['label'] = Properties::$l10n->t('Other');
                 }
                 if ($temp['label'] == '_$!<HomePage>!$_') {
                     $temp['label'] = Properties::$l10n->t('HomePage');
                 }
             }
             if (array_key_exists($pname, $details)) {
                 $details[$pname][] = $temp;
             } else {
                 $details[$pname] = array($temp);
             }
         }
     }
     return array('data' => $details, 'metadata' => $contact->getMetaData());
 }
Beispiel #2
0
 public function __unset($key)
 {
     if (!$this->isRetrieved()) {
         $this->retrieve();
     }
     parent::__unset($key);
     if ($key === 'PHOTO') {
         Properties::cacheThumbnail($this->getBackend()->name, $this->getParent()->getId(), $this->getId(), null, $this, array('remove' => true));
     }
     $this->setSaved(false);
 }
 /**
  * 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'];
     $maxSize = isset($this->request->post['maxSize']) ? $this->request->post['maxSize'] : 200;
     $app = new App($this->api->getUserId());
     $addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']);
     $contact = $addressBook->getChild($params['contactId']);
     $response = new JSONResponse();
     if (!$contact) {
         return $response->bailOut(App::$l10n->t('Couldn\'t find contact.'));
     }
     $data = $this->server->getCache()->get($tmpkey);
     if (!$data) {
         return $response->bailOut(App::$l10n->t('Image has been removed from cache'));
     }
     $image = new \OCP\Image();
     if (!$image->loadFromData($data)) {
         return $response->bailOut(App::$l10n->t('Error creating temporary image'));
     }
     $w = $w !== -1 ? $w : $image->width();
     $h = $h !== -1 ? $h : $image->height();
     if (!$image->crop($x, $y, $w, $h)) {
         return $response->bailOut(App::$l10n->t('Error cropping image'));
     }
     if ($image->width() < $maxSize || $image->height() < $maxSize) {
         if (!$image->resize(200)) {
             return $response->bailOut(App::$l10n->t('Error resizing image'));
         }
     }
     // 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) {
             $this->server->getCache()->remove($tmpkey);
             return $response->bailOut(App::$l10n->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));
         // TODO: Fix this hack
         $contact->setSaved(false);
     }
     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)));
     $this->server->getCache()->remove($tmpkey);
     return $response;
 }
Beispiel #4
0
 public static function contactUpdated($parameters)
 {
     //\OCP\Util::writeLog('contacts', __METHOD__.' parameters: '.print_r($parameters, true), \OCP\Util::DEBUG);
     $contact = $parameters['contact'];
     Utils\Properties::updateIndex($parameters['contactId'], $contact);
     // If updated via CardDAV we don't know if PHOTO has changed
     if (isset($parameters['carddav']) && $parameters['carddav']) {
         if (isset($contact->PHOTO) || isset($contact->LOGO)) {
             Utils\Properties::cacheThumbnail($parameters['backend'], $parameters['addressBookId'], $parameters['contactId'], null, $contact, array('update' => true));
         }
         $tagMgr = \OC::$server->getTagManager()->load('contact');
         $tagMgr->purgeObjects(array($parameters['contactId']));
         if (isset($contact->CATEGORIES)) {
             $tagMgr->addMultiple($contact->CATEGORIES->getParts(), true, $parameters['contactId']);
         }
     }
 }
 /**
  * Get a photo from the oC and crops it with the suplied geometry.
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function cropPhoto()
 {
     $params = $this->request->urlParams;
     $x = $this->params('x', 0);
     $y = $this->params('y', 0);
     $w = $this->params('w', -1);
     $h = $this->params('h', -1);
     $tmpkey = $params['key'];
     $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $contact = $addressBook->getChild($params['contactId']);
     $response = new JSONResponse();
     $tmpPhoto = new TemporaryPhoto($this->cache, $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, $contact);
     $response->setData(array('status' => 'success', 'data' => array('id' => $params['contactId'], 'thumbnail' => $thumbnail)));
     $tmpPhoto->remove($tmpkey);
     return $response;
 }