1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-30 12:24:11 +01:00
OwncloudContactsOfficial/lib/controller/addressbookcontroller.php

342 lines
8.6 KiB
PHP
Raw Normal View History

2013-04-18 01:12:04 +02:00
<?php
/**
* @author Thomas Tanghus
2014-01-26 00:40:22 +01:00
* @copyright 2013-2014 Thomas Tanghus (thomas@tanghus.net)
2013-04-18 01:12:04 +02:00
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Contacts\Controller;
2013-05-24 20:35:23 +02:00
use OCA\Contacts\App,
OCA\Contacts\JSONResponse,
OCA\Contacts\Utils\JSONSerializer,
OCA\Contacts\Controller,
2013-10-23 12:56:30 +02:00
OCP\AppFramework\Http;
2013-04-18 01:12:04 +02:00
/**
* Controller class For Address Books
2013-04-18 01:12:04 +02:00
*/
2013-09-17 18:46:59 +02:00
class AddressBookController extends Controller {
2013-04-18 01:12:04 +02:00
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
* @NoCSRFRequired
2013-04-18 01:12:04 +02:00
*/
public function userAddressBooks() {
2013-09-17 18:46:59 +02:00
$addressBooks = $this->app->getAddressBooksForUser();
2013-10-17 02:10:34 +02:00
$result = array();
2013-09-10 07:17:05 +02:00
$lastModified = 0;
2014-03-08 16:22:51 +01:00
2013-04-18 01:12:04 +02:00
foreach($addressBooks as $addressBook) {
2014-03-08 16:22:51 +01:00
2013-09-10 07:17:05 +02:00
$data = $addressBook->getMetaData();
2013-10-17 02:10:34 +02:00
$result[] = $data;
2014-03-08 16:22:51 +01:00
if (!is_null($data['lastmodified'])) {
2013-09-10 07:17:05 +02:00
$lastModified = max($lastModified, $data['lastmodified']);
}
2014-03-08 16:22:51 +01:00
2013-04-18 01:12:04 +02:00
}
2013-09-10 07:17:05 +02:00
// To avoid invalid cache deletion time is saved
2014-03-09 15:03:21 +01:00
/*$lastModified = max(
$lastModified,
\OCP\Config::getUserValue($this->api->getUserId(), 'contacts', 'last_address_book_deleted', 0)
2014-03-09 15:03:21 +01:00
);*/
2013-09-10 07:17:05 +02:00
$response = new JSONResponse(array(
'addressbooks' => $result,
));
2013-09-10 07:17:05 +02:00
/** FIXME: Caching is currently disabled
2013-09-10 07:17:05 +02:00
if($lastModified > 0) {
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
$response->setETag(md5($lastModified));
}
*/
2013-09-10 07:17:05 +02:00
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getAddressBook() {
$params = $this->request->urlParams;
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$lastModified = $addressBook->lastModified();
$etag = null;
$response = new JSONResponse();
2014-03-08 16:22:51 +01:00
if (!is_null($lastModified)) {
2013-09-06 06:15:23 +02:00
//$response->addHeader('Cache-Control', 'private, must-revalidate');
$response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null);
$etag = md5($lastModified);
$response->setETag($etag);
}
2013-10-17 02:10:34 +02:00
//$response->debug('comparing: "' . $etag . '" to ' . $this->request->getHeader('If-None-Match'));
2014-03-08 16:22:51 +01:00
if (!is_null($etag)
2013-10-17 02:10:34 +02:00
&& $this->request->getHeader('If-None-Match') === '"'.$etag.'"')
{
return $response->setStatus(Http::STATUS_NOT_MODIFIED);
} else {
2014-03-08 16:22:51 +01:00
switch ($this->request->method) {
case 'OPTIONS':
$options = array('GET', 'HEAD', 'OPTIONS');
2014-03-08 16:22:51 +01:00
if ($addressBook->hasPermission(\OCP\PERMISSION_DELETE)
&& $addressBook->getBackend()->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE))
{
$options[] = 'DELETE';
}
2014-03-08 16:22:51 +01:00
if ($addressBook->hasPermission(\OCP\PERMISSION_UPDATE)
&& $addressBook->getBackend()->hasAddressBookMethodFor(\OCP\PERMISSION_UPDATE))
{
$options[] = 'POST';
}
$response->addHeader('Allow' , implode(',', $options));
return $response;
case 'HEAD':
return $response;
case 'GET':
$contacts = array();
2014-03-08 16:22:51 +01:00
foreach ($addressBook->getChildren() as $i => $contact) {
$result = JSONSerializer::serializeContact($contact);
2014-03-08 16:22:51 +01:00
if ($result !== null) {
$contacts[] = $result;
}
}
2014-03-08 16:22:51 +01:00
return $response->setData(array('contacts' => $contacts));
}
}
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function addAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$backend = $this->app->getBackend($params['backend']);
2014-03-08 16:22:51 +01:00
if (!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_CREATE)) {
2013-12-28 10:02:22 +01:00
throw new \Exception('This backend does not support adding address books', 501);
}
2014-03-08 16:22:51 +01:00
try {
$id = $backend->createAddressBook($this->request->post);
2014-03-09 15:03:21 +01:00
} catch(\Exception $e) {
2013-10-17 02:10:34 +02:00
return $response->bailOut($e->getMessage());
}
2014-03-08 16:22:51 +01:00
if ($id === false) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Error creating address book'));
}
2013-10-17 02:10:34 +02:00
return $response->setStatus('201')->setParams($backend->getAddressBook($id));
}
2013-05-07 19:16:06 +02:00
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
2013-05-07 19:16:06 +02:00
*/
public function updateAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
2014-03-08 16:22:51 +01:00
$addressBook->update($this->request['properties']);
2013-10-17 02:10:34 +02:00
return $response->setParams($addressBook->getMetaData());
2013-05-07 19:16:06 +02:00
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function deleteAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
2013-09-17 18:46:59 +02:00
$backend = $this->app->getBackend($params['backend']);
2014-03-08 16:22:51 +01:00
if (!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE)) {
throw new \Exception(App::$l10n->t(
'The "%s" backend does not support deleting address books', array($backend->name)
2014-03-08 16:22:51 +01:00
), 501);
}
$addressBookInfo = $backend->getAddressBook($params['addressBookId']);
2014-03-08 16:22:51 +01:00
if (!$addressBookInfo['permissions'] & \OCP\PERMISSION_DELETE) {
throw new \Exception(App::$l10n->t(
'You do not have permissions to delete the "%s" address book',
array($addressBookInfo['displayname'])
), 403);
}
2014-03-08 16:22:51 +01:00
if (!$backend->deleteAddressBook($params['addressBookId'])) {
throw new \Exception(App::$l10n->t(
'Error deleting address book'
), 500);
}
2014-03-08 16:22:51 +01:00
\OCP\Config::setUserValue($this->api->getUserId(), 'contacts', 'last_address_book_deleted', time());
return $response;
2013-04-18 01:12:04 +02:00
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function activateAddressBook() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
$addressBook->setActive($this->request->post['state']);
return $response;
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function addChild() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
try {
$id = $addressBook->addChild();
2014-03-09 15:03:21 +01:00
} catch(\Exception $e) {
2013-10-17 02:10:34 +02:00
return $response->bailOut($e->getMessage());
}
2014-03-08 16:22:51 +01:00
if ($id === false) {
2013-10-17 02:10:34 +02:00
return $response->bailOut(App::$l10n->t('Error creating contact.'));
}
$contact = $addressBook->getChild($id);
2014-03-09 15:03:21 +01:00
$serialized = JSONSerializer::serializeContact($contact);
if (is_null($serialized)) {
throw new \Exception(App::$l10n->t(
'Error creating contact'
));
}
$response->setStatus('201')->setETag($contact->getETag());
$response->addHeader('Location',
\OCP\Util::linkToRoute(
'contacts_contact_get',
array(
'backend' => $params['backend'],
'addressBookId' => $params['addressBookId'],
'contactId' => $id
)
)
);
2014-03-09 15:03:21 +01:00
return $response->setParams($serialized);
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
*/
public function deleteChild() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
2014-03-09 15:03:21 +01:00
$result = $addressBook->deleteChild($params['contactId']);
2014-03-08 16:22:51 +01:00
if ($result === false) {
2014-03-09 15:03:21 +01:00
throw new \Exception(App::$l10n->t(
'Error deleting contact'
), 500);
}
2014-03-08 16:22:51 +01:00
2013-10-17 02:10:34 +02:00
return $response->setStatus('204');
2013-09-03 14:08:12 +02:00
}
/**
2013-09-17 18:46:59 +02:00
* @NoAdminRequired
2013-09-03 14:08:12 +02:00
*/
public function deleteChildren() {
$params = $this->request->urlParams;
$response = new JSONResponse();
$addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']);
2013-09-03 14:08:12 +02:00
$contacts = $this->request->post['contacts'];
2014-03-09 15:03:21 +01:00
$result = $addressBook->deleteChildren($contacts);
2013-09-03 14:08:12 +02:00
2013-10-17 02:10:34 +02:00
return $response->setParams(array('result' => $result));
}
/**
2013-09-17 18:46:59 +02:00
* @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']);
2013-09-17 18:46:59 +02:00
$targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']);
$contact = $fromAddressBook->getChild($params['contactId']);
2014-03-08 16:22:51 +01:00
if (!$contact) {
2014-03-09 15:03:21 +01:00
throw new \Exception(App::$l10n->t(
'Error retrieving contact'
), 500);
}
2014-03-08 16:22:51 +01:00
2014-03-09 15:03:21 +01:00
$contactId = $targetAddressBook->addChild($contact);
2014-03-08 16:22:51 +01:00
2014-03-09 15:03:21 +01:00
// Retrieve the contact again to be sure it's in sync
$contact = $targetAddressBook->getChild($contactId);
2014-03-08 16:22:51 +01:00
if (!$contact) {
2014-03-09 15:03:21 +01:00
throw new \Exception(App::$l10n->t(
'Error saving contact'
), 500);
}
2014-03-08 16:22:51 +01:00
if (!$fromAddressBook->deleteChild($params['contactId'])) {
// Don't bail out because we have to return the contact
2013-10-17 02:10:34 +02:00
return $response->debug(App::$l10n->t('Error removing contact from other address book.'));
}
2014-03-08 16:22:51 +01:00
2014-03-09 15:03:21 +01:00
$serialized = JSONSerializer::serializeContact($contact);
if (is_null($serialized)) {
throw new \Exception(App::$l10n->t(
'Error getting moved contact'
));
}
return $response->setParams($serialized);
}
2013-04-18 01:12:04 +02:00
}