From 0856f4ccc8147ff42677c1f2c39d01e338c2bc37 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Mon, 31 Mar 2014 15:29:16 +0200 Subject: [PATCH] WIP almost done --- appinfo/database.xml | 23 +-- appinfo/version | 2 +- lib/backend/database.php | 2 +- lib/backend/owncloudusers.php | 195 +++++++++++++++++++++-- lib/controller/addressbookcontroller.php | 6 +- 5 files changed, 193 insertions(+), 35 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index 071bd5dc..c1fc34c4 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -351,19 +351,9 @@ *dbprefix*contacts_ocu_addressbooks - + id - integer - 0 - true - 1 - true - 4 - - - - userid text true @@ -410,13 +400,6 @@ 4 - - c_addressbook_userid_index - - userid - ascending - - @@ -428,7 +411,7 @@ - userid + id text true @@ -436,7 +419,7 @@ - ownerid + owner text true diff --git a/appinfo/version b/appinfo/version index 34039699..8d13a336 100644 --- a/appinfo/version +++ b/appinfo/version @@ -1 +1 @@ -0.3.0.6 \ No newline at end of file +0.3.0.8 \ No newline at end of file diff --git a/lib/backend/database.php b/lib/backend/database.php index b6734f31..2c7a25ed 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -430,6 +430,7 @@ class Database extends AbstractBackend { \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); return $cards; } + if (!is_null($result)) { @@ -439,7 +440,6 @@ class Database extends AbstractBackend { } } - return $cards; } diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index bb56f2d9..eb4074e9 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -5,13 +5,28 @@ namespace OCA\Contacts\Backend; use OCA\Contacts\Contact, OCA\Contacts\VObject\VCard, OCA\Contacts\Utils\Properties, - Sabre\VObject\Reader; - + Sabre\VObject\Reader, + OCA\Contacts\Addressbook; +/** + * Contact backend for storing all the ownCloud users in this installation. + * Every user has *1* personal addressbook. The id of this addresbook is the + * userid of the owner. + */ class OwnCloudUsers extends AbstractBackend { public $name = 'OwnCloudUsers'; + /** + * The table that holds the address books. + * For every user there is *1* addressbook. + * @var string + */ private $addressBooksTableName = '*PREFIX*contacts_ocu_addressbooks'; + + /** + * The table that holds the contacts. + * @var string + */ private $cardsTableName = '*PREFIX*contacts_ocu_cards'; public function __construct($userid){ @@ -46,26 +61,186 @@ class OwnCloudUsers extends AbstractBackend { public function getContacts($addressbookid, array $options = array()){ $contacts = array(); - $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE id = ?'; - $args = array($addressBookId); + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_ALL; - $row['backend'] = $this->name; + $result = $query->execute(array($this->userid)); + while($row = $result->fetchRow()){ + $row['permissions'] = \OCP\PERMISSION_ALL; + $contacts[] = $row; + } + + $contactsId = array(); + + foreach($contacts as $contact){ + $contactsId[] = $contact['id']; + } + + $users = \OCP\User::getUsers(); + $recall = false; + + $add = array_diff($users, $contactsId); + $remove = array_diff($contactsId, $users); + if(count($add) > 0){ + $this->addContacts($add, $addressbookid); + $recall = true; + } + + if(count($remove) > 0){ + $this->removeContacts($remove, $addressbookid); + $recall = true; + } + + if($recall === true){ + return $this->getContacts($addressbookid); + } else { + return $contacts; + } } public function getContact($addressbookid, $id, array $options = array()){ + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($this->userid)); + $row = $result->fetchRow(); + $row['permissions'] = \OCP\PERMISSION_ALL; + return $row; } public function createAddressBook(array $properties) { } - private function syncDbWithOc(){ - + private function addContacts($contacts, $addressbookid){ + foreach($contacts as $user){ + $sql = 'INSERT INTO ' . $this->cardsTableName . ' (' + . 'id, ' + . 'owner,' + . 'addressbookid, ' + . 'fullname, ' + . 'carddata, ' + . 'uri, ' + . 'lastmodified' + . ') VALUES (' + . '?,' + . '?,' + . '?,' + . '?,' + . '?,' + . '?,' + . '?' + . ')'; + + $query = \OCP\DB::prepare($sql); + + $contact = new Contact( + $addressBook = new AddressBook($this , $this->getAddressBooksForUser()), // since there is only one addressbook with OC users for each OC user we can use this function + $this, + array( + "id" => $user, + "lastmodified" => time(), + "displayname" => \OCP\User::getDisplayName($user), + "fullname" => \OCP\User::getDisplayName($user) + ) + ); + $carddata = $this->generateCardData($contact); + $result = $query->execute(array($user, $this->userid, $addressbookid, \OCP\User::getDisplayName($user), $carddata->serialize(), 'test', time())); + } + } + + private function removeContacts($contacts, $addressbookid){ + foreach($contacts as $user){ + $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?'; + + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($this->userid, $user)); + } } + private function generateCardData($data){ + if (!$data instanceof VCard) { + try { + $data = Reader::read($data); + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + return false; + } + } + + try { + $data->validate(VCard::REPAIR|VCard::UPGRADE); + } catch (\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . + 'Error validating vcard: ' . $e->getMessage(), \OCP\Util::ERROR); + return false; + } + + $now = new \DateTime; + $data->REV = $now->format(\DateTime::W3C); + + $appinfo = \OCP\App::getAppInfo('contacts'); + $appversion = \OCP\App::getAppVersion('contacts'); + $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN'; + $data->PRODID = $prodid; + + return $data; + } + + public function updateContact($addressBookId, $id, $contact, array $options = array()) { + + $updateRevision = true; + $isCardDAV = false; + + if (!$contact instanceof VCard) { + try { + $contact = Reader::read($contact); + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + return false; + } + } + + if (is_array($id)) { + + if (isset($id['id'])) { + $id = $id['id']; + } elseif (isset($id['uri'])) { + $updateRevision = false; + $isCardDAV = true; + $id = $this->getIdFromUri($id['uri']); + + if (is_null($id)) { + \OCP\Util::writeLog('contacts', __METHOD__ . ' Couldn\'t find contact', \OCP\Util::ERROR); + return false; + } + + } else { + throw new \Exception( + __METHOD__ . ' If second argument is an array, either \'id\' or \'uri\' has to be set.' + ); + } + } + + if ($updateRevision || !isset($contact->REV)) { + $now = new \DateTime; + $contact->REV = $now->format(\DateTime::W3C); + } + + $data = $contact->serialize(); + + + $sql = 'UPDATE ' . $this->cardsTableName + . ' SET ' + . '`addressbookid` = ?, ' + . '`fullname` = ?, ' + . '`carddata` = ?, ' + . '`lastmodified` = ? ' + . ' WHERE ' + . '`id` = ? ' + . 'AND `owner` = ? '; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($addressBookId, $contact->FN, $data, time(), $id, $this->userid)); + return true; + } } diff --git a/lib/controller/addressbookcontroller.php b/lib/controller/addressbookcontroller.php index ab2735f5..72f581b0 100644 --- a/lib/controller/addressbookcontroller.php +++ b/lib/controller/addressbookcontroller.php @@ -102,12 +102,12 @@ class AddressBookController extends Controller { } //$response->debug('comparing: "' . $etag . '" to ' . $this->request->getHeader('If-None-Match')); - if (!is_null($etag) + /*if (!is_null($etag) && $this->request->getHeader('If-None-Match') === '"'.$etag.'"') { return $response->setStatus(Http::STATUS_NOT_MODIFIED); } else { - switch ($this->request->method) { + */ switch ($this->request->method) { case 'OPTIONS': $options = array('GET', 'HEAD', 'OPTIONS'); if ($addressBook->hasPermission(\OCP\PERMISSION_DELETE) @@ -136,7 +136,7 @@ class AddressBookController extends Controller { return $response->setData(array('contacts' => $contacts)); } - } + //} } /**