/** * @NoAdminRequired * @NoCSRFRequired */ public function backendStatus() { $response = new JSONResponse(); $params = $this->request->urlParams; $backend = $params['backend']; $enabled = \OCP\Config::getAppValue('contacts', 'backend_' . $backend, "false"); return $response->setData($enabled); }
/** * @NoAdminRequired */ public function patch() { $params = $this->request->urlParams; $patch = $this->request->patch; $response = new JSONResponse(); $name = $patch['name']; $value = $patch['value']; $checksum = isset($patch['checksum']) ? $patch['checksum'] : null; $parameters = isset($patch['parameters']) ? $patch['parameters'] : null; $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); if (!$contact) { return $response->setStatus(Http::STATUS_NOT_FOUND)->bailOut(App::$l10n->t('Couldn\'t find contact.')); } if (!$name) { return $response->setStatus(Http::STATUS_PRECONDITION_FAILED)->bailOut(App::$l10n->t('Property name is not set.')); } if (!$checksum && in_array($name, Properties::$multi_properties)) { return $response->setStatus(Http::STATUS_PRECONDITION_FAILED)->bailOut(App::$l10n->t('Property checksum is not set.')); } if (is_array($value)) { // NOTE: Important, otherwise the compound value will be // set in the order the fields appear in the form! ksort($value); } $result = array('contactId' => $params['contactId']); if ($checksum && in_array($name, Properties::$multi_properties)) { try { if (is_null($value)) { $contact->unsetPropertyByChecksum($checksum); } else { $checksum = $contact->setPropertyByChecksum($checksum, $name, $value, $parameters); $result['checksum'] = $checksum; } } catch (Exception $e) { return $response->setStatus(Http::STATUS_PRECONDITION_FAILED)->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.')); } } elseif (!in_array($name, Properties::$multi_properties)) { if (is_null($value)) { unset($contact->{$name}); } else { if (!$contact->setPropertyByName($name, $value, $parameters)) { return $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR)->bailOut(App::$l10n->t('Error updating contact')); } } } if (!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact to backend')); } $result['lastmodified'] = $contact->lastModified(); return $response->setData($result); }
/** * Export a single contact. * * @NoAdminRequired * @NoCSRFRequired */ public function exportContact() { $params = $this->request->urlParams; $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); if (!$contact) { $response = new JSONResponse(); $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); return $response; } $name = str_replace(' ', '_', $contact->getDisplayName()) . '.vcf'; return new TextDownloadResponse($contact->serialize(), $name, 'text/vcard'); }
/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ public function afterException($controller, $methodName, \Exception $exception) { // If there's no proper status code associated, set it to 500. $response = new JSONResponse(); if ($exception->getCode() < 100) { $response->setStatus(500); } else { $response->setStatus($exception->getCode()); } $response->setErrorMessage($exception->getMessage()); //$this->api->log(get_class($controller) . '->' . $methodName . ': ' . $exception->getMessage()); return $response; }
/** * If an Exception is being caught, return a JSON error response with * a suitable status code * @param Controller $controller the controller that is being called * @param string $methodName the name of the method that will be called on * the controller * @param \Exception $exception the thrown exception * @return Response a Response object */ public function afterException($controller, $methodName, \Exception $exception) { \OCP\Util::writeLog('contacts', __METHOD__ . ' method: ' . $methodName, \OCP\Util::DEBUG); // If there's no proper status code associated, set it to 500. $response = new JSONResponse(); if ($exception->getCode() < 100) { $response->setStatus(HttpStatus::STATUS_INTERNAL_SERVER_ERROR); } else { $response->setStatus($exception->getCode()); } $response->setErrorMessage($exception->getMessage()); \OCP\Util::logException('contacts', $exception); 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)); } }
/** * @NoAdminRequired */ public function removeFromGroup() { $response = new JSONResponse(); $params = $this->request->urlParams; $categoryId = $params['categoryId']; $categoryName = $this->request->post['name']; $ids = $this->request->post['contactIds']; //$response->debug('request: '.print_r($this->request->post, true)); if (is_null($categoryId) || $categoryId === '') { throw new \Exception(App::$l10n->t('Group ID missing from request.'), Http::STATUS_PRECONDITION_FAILED); } if (is_null($categoryName) || $categoryName === '') { throw new \Exception(App::$l10n->t('Group name missing from request.'), Http::STATUS_PRECONDITION_FAILED); } if (is_null($ids)) { throw new \Exception(App::$l10n->t('Contact ID missing from request.'), Http::STATUS_PRECONDITION_FAILED); } $backend = $this->app->getBackend('local'); foreach ($ids as $contactId) { $contact = $backend->getContact(null, $contactId, array('noCollection' => true)); if (!$contact) { $response->debug('Couldn\'t get contact: ' . $contactId); continue; } $obj = \Sabre\VObject\Reader::read($contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); if ($obj) { if (!isset($obj->CATEGORIES)) { return $response; } if ($obj->removeFromGroup($categoryName)) { $backend->updateContact(null, $contactId, $obj, array('noCollection' => true)); } } else { $response->debug('Error parsing contact: ' . $contactId); } $response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId); $this->tags->unTag($contactId, $categoryId); } 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->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; }
/** * @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 removeFromGroup() { $response = new JSONResponse(); $params = $this->request->urlParams; $categoryId = $params['categoryId']; $categoryname = $this->request->post['name']; $ids = $this->request->post['contactIds']; //$response->debug('request: '.print_r($this->request->post, true)); if (is_null($categoryId) || $categoryId === '') { $response->bailOut(App::$l10n->t('Group ID missing from request.')); return $response; } if (is_null($ids)) { $response->bailOut(App::$l10n->t('Contact ID missing from request.')); return $response; } $backend = $this->app->getBackend('local'); $tagMgr = $this->server->getTagManager()->load('contact'); foreach ($ids as $contactId) { $contact = $backend->getContact(null, $contactId, array('noCollection' => true)); if (!$contact) { $response->debug('Couldn\'t get contact: ' . $contactId); continue; } $obj = \Sabre\VObject\Reader::read($contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES); if ($obj) { if (!isset($obj->CATEGORIES)) { $obj->add('CATEGORIES'); } $obj->CATEGORIES->removeGroup($categoryname); $backend->updateContact(null, $contactId, $obj, array('noCollection' => true)); } else { $response->debug('Error parsing contact: ' . $contactId); } $response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId); $tagMgr->unTag($contactId, $categoryId); } 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 = $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; }