/**
  * @NoAdminRequired
  */
 public function addGroup()
 {
     $name = $this->request->post['name'];
     $response = new JSONResponse();
     if (is_null($name) || $name === "") {
         $response->bailOut(App::$l10n->t('No group name given.'));
     }
     $id = $this->tags->add($name);
     if ($id === false) {
         $response->bailOut(App::$l10n->t('Error adding group.'));
     } else {
         $response->setParams(array('id' => $id, 'name' => $name));
     }
     return $response;
 }
 /**
  * @NoAdminRequired
  */
 public function set()
 {
     $request = $this->request;
     //$request = json_decode(file_get_contents('php://input'), true);
     $key = $request->post['key'];
     $value = $request->post['value'];
     $response = new JSONResponse();
     if (is_null($key) || $key === "") {
         $response->bailOut(App::$l10n->t('No key is given.'));
     }
     if (is_null($value) || $value === "") {
         $response->bailOut(App::$l10n->t('No value is given.'));
     }
     if (\OCP\Config::setUserValue(\OCP\User::getUser(), 'contacts', $key, $value)) {
         $response->setParams(array('key' => $key, 'value' => $value));
         return $response;
     } else {
         $response->bailOut(App::$l10n->t('Could not set preference: ' . $key . ':' . $value));
     }
 }
 /**
  * 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();
     $photoResponse = $this->getPhoto($maxSize = 400);
     if (!$photoResponse instanceof ImageResponse) {
         return $photoResponse;
     }
     $data = $photoResponse->render();
     $tmpkey = 'contact-photo-' . $params['contactId'];
     if (!$this->server->getCache()->set($tmpkey, $data, 600)) {
         $response->bailOut(App::$l10n->t('Couldn\'t save temporary image: ') . $tmpkey);
         return $response;
     }
     $response->setParams(array('tmp' => $tmpkey, 'metadata' => array('contactId' => $params['contactId'], 'addressBookId' => $params['addressBookId'], 'backend' => $params['backend'])));
     return $response;
 }
 /**
  * @NoAdminRequired
  */
 public function status()
 {
     $request = $this->request;
     $response = new JSONResponse();
     $progresskey = isset($request->get['progresskey']) ? $request->get['progresskey'] : null;
     if (is_null($progresskey)) {
         $response->bailOut(App::$l10n->t('Progress key missing from request.'));
         return $response;
     }
     $response->setParams(array('progress' => \OC_Cache::get($progresskey)));
     return $response;
 }
 /**
  * @NoAdminRequired
  */
 public function moveChild()
 {
     $params = $this->request->urlParams;
     $targetInfo = $this->request->post['target'];
     $response = new JSONResponse();
     // TODO: Check if the backend supports move (is 'local' or 'shared') and use that operation instead.
     // If so, set status 204 and don't return the serialized contact.
     $fromAddressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
     $contact = $fromAddressBook->getChild($params['contactId']);
     if (!$contact) {
         throw new \Exception(App::$l10n->t('Error retrieving contact'), 500);
     }
     $contactId = $targetAddressBook->addChild($contact);
     // Retrieve the contact again to be sure it's in sync
     $contact = $targetAddressBook->getChild($contactId);
     if (!$contact) {
         throw new \Exception(App::$l10n->t('Error saving contact'), 500);
     }
     if (!$fromAddressBook->deleteChild($params['contactId'])) {
         // Don't bail out because we have to return the contact
         return $response->debug(App::$l10n->t('Error removing contact from other address book.'));
     }
     $serialized = JSONSerializer::serializeContact($contact);
     if (is_null($serialized)) {
         throw new \Exception(App::$l10n->t('Error getting moved contact'));
     }
     return $response->setParams($serialized);
 }
 /**
  * @NoAdminRequired
  */
 public function moveChild()
 {
     $params = $this->request->urlParams;
     $targetInfo = $this->request->post['target'];
     $response = new JSONResponse();
     // TODO: Check if the backend supports move (is 'local' or 'shared') and use that operation instead.
     // If so, set status 204 and don't return the serialized contact.
     $fromAddressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
     $targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
     $contact = $fromAddressBook->getChild($params['contactId']);
     if (!$contact) {
         $response->bailOut(App::$l10n->t('Error retrieving contact.'));
         return $response;
     }
     try {
         $contactId = $targetAddressBook->addChild($contact);
     } catch (Exception $e) {
         return $response->bailOut($e->getMessage());
     }
     $contact = $targetAddressBook->getChild($contactId);
     if (!$contact) {
         return $response->bailOut(App::$l10n->t('Error saving contact.'));
     }
     if (!$fromAddressBook->deleteChild($params['contactId'])) {
         // Don't bail out because we have to return the contact
         return $response->debug(App::$l10n->t('Error removing contact from other address book.'));
     }
     return $response->setParams(JSONSerializer::serializeContact($contact));
 }
 /**
  * @NoAdminRequired
  */
 public function status()
 {
     $request = $this->request;
     $response = new JSONResponse();
     $progresskey = isset($request->get['progresskey']) ? $request->get['progresskey'] : null;
     if (is_null($progresskey)) {
         $response->bailOut(App::$l10n->t('Progress key missing from request.'));
         return $response;
     }
     error_log("progresskey: " . $this->cache->get($progresskey) . " total: " . $this->cache->get($progresskey . '_total'));
     $response->setParams(array('progress' => $this->cache->get($progresskey), 'total' => $this->cache->get($progresskey . '_total')));
     return $response;
 }
 /**
  * @NoAdminRequired
  */
 public function renameGroup()
 {
     $from = $this->request->post['from'];
     $to = $this->request->post['to'];
     $response = new JSONResponse();
     if (empty($from)) {
         $response->bailOut(App::$l10n->t('No group name to rename from given.'));
         return $response;
     }
     if (empty($to)) {
         $response->bailOut(App::$l10n->t('No group name to rename to given.'));
         return $response;
     }
     if (!$this->tags->rename($from, $to)) {
         $response->bailOut(App::$l10n->t('Error renaming group.'));
         return $response;
     }
     $tag = $this->tags->getTag($from);
     if (!$tag) {
         $response->bailOut(App::$l10n->t('Error renaming group.'));
         return $response;
     }
     $response->setParams(array('displayname' => $this->displayName($tag)));
     $ids = $this->tags->getIdsForTag($to);
     if ($ids !== false) {
         $backend = $this->app->getBackend('local');
         foreach ($ids as $id) {
             $contact = $backend->getContact(null, $id, array('noCollection' => true));
             $obj = \Sabre\VObject\Reader::read($contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES);
             if ($obj) {
                 if (!isset($obj->CATEGORIES)) {
                     continue;
                 }
                 $obj->CATEGORIES->renameGroup($from, $to);
                 $backend->updateContact(null, $id, $obj, array('noCollection' => true));
             } else {
                 \OCP\Util::writeLog('contacts', __METHOD__ . ', could not parse card ' . $id, \OCP\Util::DEBUG);
             }
         }
     }
     return $response;
 }
 /**
  * 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->get['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));
     }
     if (!$contact->save()) {
         return $response->bailOut(App::$l10n->t('Error saving contact.'));
     }
     $thumbnail = $contact->cacheThumbnail($image);
     $response->setParams(array('id' => $params['contactId'], 'thumbnail' => $thumbnail));
     $this->server->getCache()->remove($tmpkey);
     return $response;
 }
 /**
  * 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->cache, 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'])));
 }