From c148f09386624fc1de498e8c58c437885dc9c80d Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sat, 29 Mar 2014 11:54:08 +0100 Subject: [PATCH 01/29] WIP Implenting getContacts --- appinfo/database.xml | 141 ++++++++++++++++++++++++++++++++++ appinfo/version | 2 +- lib/app.php | 1 + lib/backend/owncloudusers.php | 71 +++++++++++++++++ 4 files changed, 214 insertions(+), 1 deletion(-) create mode 100644 lib/backend/owncloudusers.php diff --git a/appinfo/database.xml b/appinfo/database.xml index 5033108a..071bd5dc 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -345,4 +345,145 @@ + + + + *dbprefix*contacts_ocu_addressbooks + + + + + id + integer + 0 + true + 1 + true + 4 + + + + userid + text + + true + 255 + + + + displayname + text + + false + 255 + + + + uri + text + + false + 200 + + + + description + text + false + 255 + + + + ctag + integer + 1 + true + true + 4 + + + + active + integer + 1 + true + 4 + + + + c_addressbook_userid_index + + userid + ascending + + + + +
+ + + + *dbprefix*contacts_ocu_cards + + + + + userid + text + + true + 255 + + + + ownerid + text + + true + 255 + + + + addressbookid + integer + + true + true + 4 + + + + fullname + text + + false + 255 + + + + carddata + clob + false + + + + uri + text + + false + 200 + + + + lastmodified + integer + + false + true + 4 + + + + +
diff --git a/appinfo/version b/appinfo/version index b8b1645c..34039699 100644 --- a/appinfo/version +++ b/appinfo/version @@ -1 +1 @@ -0.3.0.1 +0.3.0.6 \ No newline at end of file diff --git a/lib/app.php b/lib/app.php index cc186f69..86587c68 100644 --- a/lib/app.php +++ b/lib/app.php @@ -56,6 +56,7 @@ class App { 'ldap' => 'OCA\Contacts\Backend\Ldap', 'local' => 'OCA\Contacts\Backend\Database', 'shared' => 'OCA\Contacts\Backend\Shared', + 'owncloudusers' => 'OCA\Contacts\Backend\OwnCloudUsers', ); public function __construct( diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php new file mode 100644 index 00000000..bb56f2d9 --- /dev/null +++ b/lib/backend/owncloudusers.php @@ -0,0 +1,71 @@ +userid = $userid ? $userid : \OCP\User::getUser(); + } + + public function getAddressBooksForUser(array $options = array()) { + // Only 1 addressbook for every user + $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE userid = ?'; + $args = array($this->userid); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + $row = $result->fetchRow(); + $row['permissions'] = \OCP\PERMISSION_ALL; + + return array($row); + } + + public function getAddressBook($addressBookId, array $options = array()) { + + $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; + $args = array($addressBookId); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + $row = $result->fetchRow(); + $row['permissions'] = \OCP\PERMISSION_ALL; + $row['backend'] = $this->name; + + return array($row); + } + + public function getContacts($addressbookid, array $options = array()){ + $contacts = array(); + + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE id = ?'; + $args = array($addressBookId); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + $row = $result->fetchRow(); + $row['permissions'] = \OCP\PERMISSION_ALL; + $row['backend'] = $this->name; + } + + public function getContact($addressbookid, $id, array $options = array()){ + + } + + public function createAddressBook(array $properties) { + + } + + private function syncDbWithOc(){ + + } + + +} From 0856f4ccc8147ff42677c1f2c39d01e338c2bc37 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Mon, 31 Mar 2014 15:29:16 +0200 Subject: [PATCH 02/29] 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)); } - } + //} } /** From a234865f71ebc98114295e27ff839c11f2da87ca Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 19:36:39 +0200 Subject: [PATCH 03/29] Create addressbook if don't exists --- lib/backend/owncloudusers.php | 36 +++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index eb4074e9..6f009b5b 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -35,14 +35,42 @@ class OwnCloudUsers extends AbstractBackend { public function getAddressBooksForUser(array $options = array()) { // Only 1 addressbook for every user - $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE userid = ?'; + $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; $args = array($this->userid); $query = \OCP\DB::prepare($sql); $result = $query->execute($args); $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_ALL; - - return array($row); + // Check if there are no results TODO? + if(!$row){ + // Create new addressbook + $sql = 'INSERT INTO ' . $this->addressBooksTableName + . ' ( ' + . 'id, ' + . 'displayname, ' + //. 'uri, ' TODO + . 'description, ' + // . 'ctag, ' + . 'active ' + . ') VALUES ( ' + . '?, ' + . '?, ' + . '?, ' + . '? ' + . ')'; + $args = array( + $this->userid, + 'ownCloud Users', + 'ownCloud Users', + 1 + ); + $query = \OCP\DB::prepare($sql); + $query->execute($args); + + return $this->getAddressBooksForUser(); + } else { + $row['permissions'] = \OCP\PERMISSION_ALL; + return array($row); + } } public function getAddressBook($addressBookId, array $options = array()) { From a6393f96951737cd99912c1fe956a4647d9f388d Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 20:48:00 +0200 Subject: [PATCH 04/29] Add Docs --- lib/backend/owncloudusers.php | 106 ++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 32 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index 6f009b5b..e923ca0a 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -7,10 +7,12 @@ use OCA\Contacts\Contact, OCA\Contacts\Utils\Properties, 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. + * userid of the owner. */ class OwnCloudUsers extends AbstractBackend { @@ -33,6 +35,9 @@ class OwnCloudUsers extends AbstractBackend { $this->userid = $userid ? $userid : \OCP\User::getUser(); } + /** + * {@inheritdoc} + */ public function getAddressBooksForUser(array $options = array()) { // Only 1 addressbook for every user $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; @@ -40,17 +45,17 @@ class OwnCloudUsers extends AbstractBackend { $query = \OCP\DB::prepare($sql); $result = $query->execute($args); $row = $result->fetchRow(); - // Check if there are no results TODO? - if(!$row){ - // Create new addressbook + + if(!$row){ // TODO -> better way? + // Create new addressbook $sql = 'INSERT INTO ' . $this->addressBooksTableName . ' ( ' . 'id, ' . 'displayname, ' //. 'uri, ' TODO . 'description, ' - // . 'ctag, ' - . 'active ' + //. 'ctag, ' + . 'active ' . ') VALUES ( ' . '?, ' . '?, ' @@ -73,6 +78,10 @@ class OwnCloudUsers extends AbstractBackend { } } + /** + * {@inheritdoc} + * Only 1 addressbook for every user + */ public function getAddressBook($addressBookId, array $options = array()) { $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; @@ -86,6 +95,10 @@ class OwnCloudUsers extends AbstractBackend { return array($row); } + /** + * {@inheritdoc} + * There are as many contacts in this addressbook as in this ownCloud installation + */ public function getContacts($addressbookid, array $options = array()){ $contacts = array(); @@ -125,6 +138,13 @@ class OwnCloudUsers extends AbstractBackend { } } + /** + * {@inheritdoc} + * If your username is "admin" and you want to retrieve your own contact + * the params would be: $addressbookid = 'admin'; $id = 'admin'; + * If your username is 'foo' and you want to retrieve the contact with + * ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar'; + */ public function getContact($addressbookid, $id, array $options = array()){ $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; $query = \OCP\DB::prepare($sql); @@ -135,10 +155,18 @@ class OwnCloudUsers extends AbstractBackend { return $row; } + // Not needed since there is only one addressbook for every user public function createAddressBook(array $properties) { } + /** + * Help function to add contacts to an addressbook. + * This only happens when an admin creates new users + * @param array $contacts array with userid of ownCloud users + * @param string $addressBookId + * @return bool + */ private function addContacts($contacts, $addressbookid){ foreach($contacts as $user){ $sql = 'INSERT INTO ' . $this->cardsTableName . ' (' @@ -173,19 +201,32 @@ class OwnCloudUsers extends AbstractBackend { ); $carddata = $this->generateCardData($contact); $result = $query->execute(array($user, $this->userid, $addressbookid, \OCP\User::getDisplayName($user), $carddata->serialize(), 'test', time())); + // TODO Check if $result succeeded } } + /** + * Help function to remove contacts from an addressbook. + * This only happens when an admin remove an ownCloud user + * @param array $contacts array with userid of ownCloud users + * @param string $addressBookId + * @return bool + */ 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)); + // TODO Check if $result succeeded } } - + /** + * Help function to generate the carddate which than can be stored in the db + * @param string|VCard $data + * @return Vcard + */ private function generateCardData($data){ if (!$data instanceof VCard) { try { @@ -215,49 +256,50 @@ class OwnCloudUsers extends AbstractBackend { return $data; } + /** + * @inheritdoc + */ 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; - } + 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 (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 (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); + $now = new \DateTime; + $contact->REV = $now->format(\DateTime::W3C); } $data = $contact->serialize(); - $sql = 'UPDATE ' . $this->cardsTableName . ' SET ' . '`addressbookid` = ?, ' From 12b2b1cd619d7022dbc3d61cfd1059eb8c5e6976 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 21:08:29 +0200 Subject: [PATCH 05/29] Fix scrutinizer issues --- lib/app.php | 2 +- lib/backend/owncloudusers.php | 424 +++++++++++++++++----------------- 2 files changed, 213 insertions(+), 213 deletions(-) diff --git a/lib/app.php b/lib/app.php index 86587c68..30339f53 100644 --- a/lib/app.php +++ b/lib/app.php @@ -56,7 +56,7 @@ class App { 'ldap' => 'OCA\Contacts\Backend\Ldap', 'local' => 'OCA\Contacts\Backend\Database', 'shared' => 'OCA\Contacts\Backend\Shared', - 'owncloudusers' => 'OCA\Contacts\Backend\OwnCloudUsers', + 'owncloudusers' => 'OCA\Contacts\Backend\OwnCloudUsers', ); public function __construct( diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index e923ca0a..4918251f 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -6,7 +6,7 @@ use OCA\Contacts\Contact, OCA\Contacts\VObject\VCard, OCA\Contacts\Utils\Properties, Sabre\VObject\Reader, - OCA\Contacts\Addressbook; + OCA\Contacts\Addressbook; /** @@ -17,127 +17,127 @@ use OCA\Contacts\Contact, 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){ - $this->userid = $userid ? $userid : \OCP\User::getUser(); + $this->userid = $userid ? $userid : \OCP\User::getUser(); } - + /** * {@inheritdoc} */ public function getAddressBooksForUser(array $options = array()) { - // Only 1 addressbook for every user - $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; - $args = array($this->userid); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - $row = $result->fetchRow(); + // Only 1 addressbook for every user + $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; + $args = array($this->userid); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + $row = $result->fetchRow(); - if(!$row){ // TODO -> better way? - // Create new addressbook - $sql = 'INSERT INTO ' . $this->addressBooksTableName - . ' ( ' - . 'id, ' - . 'displayname, ' - //. 'uri, ' TODO - . 'description, ' - //. 'ctag, ' - . 'active ' - . ') VALUES ( ' - . '?, ' - . '?, ' - . '?, ' - . '? ' - . ')'; - $args = array( - $this->userid, - 'ownCloud Users', - 'ownCloud Users', - 1 - ); - $query = \OCP\DB::prepare($sql); - $query->execute($args); - - return $this->getAddressBooksForUser(); - } else { - $row['permissions'] = \OCP\PERMISSION_ALL; - return array($row); - } + if(!$row){ // TODO -> better way? + // Create new addressbook + $sql = 'INSERT INTO ' . $this->addressBooksTableName + . ' ( ' + . 'id, ' + . 'displayname, ' + //. 'uri, ' TODO + . 'description, ' + //. 'ctag, ' + . 'active ' + . ') VALUES ( ' + . '?, ' + . '?, ' + . '?, ' + . '? ' + . ')'; + $args = array( + $this->userid, + 'ownCloud Users', + 'ownCloud Users', + 1 + ); + $query = \OCP\DB::prepare($sql); + $query->execute($args); + + return $this->getAddressBooksForUser(); + } else { + $row['permissions'] = \OCP\PERMISSION_ALL; + return array($row); + } } - + /** * {@inheritdoc} * Only 1 addressbook for every user */ public function getAddressBook($addressBookId, array $options = array()) { - - $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; - $args = array($addressBookId); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_ALL; - $row['backend'] = $this->name; - return array($row); + $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; + $args = array($addressBookId); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + $row = $result->fetchRow(); + $row['permissions'] = \OCP\PERMISSION_ALL; + $row['backend'] = $this->name; + + return array($row); } - + /** * {@inheritdoc} * There are as many contacts in this addressbook as in this ownCloud installation */ public function getContacts($addressbookid, array $options = array()){ - $contacts = array(); - - $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; - $query = \OCP\DB::prepare($sql); - $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; - } + $contacts = array(); + + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; + $query = \OCP\DB::prepare($sql); + $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; + } } - + /** * {@inheritdoc} * If your username is "admin" and you want to retrieve your own contact @@ -146,20 +146,20 @@ class OwnCloudUsers extends AbstractBackend { * ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar'; */ 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; + $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; } - + // Not needed since there is only one addressbook for every user public function createAddressBook(array $properties) { - + } - + /** * Help function to add contacts to an addressbook. * This only happens when an admin creates new users @@ -168,43 +168,43 @@ class OwnCloudUsers extends AbstractBackend { * @return bool */ 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); + foreach($contacts as $user){ + $sql = 'INSERT INTO ' . $this->cardsTableName . ' (' + . 'id, ' + . 'owner,' + . 'addressbookid, ' + . 'fullname, ' + . 'carddata, ' + . 'uri, ' + . 'lastmodified' + . ') VALUES (' + . '?,' + . '?,' + . '?,' + . '?,' + . '?,' + . '?,' + . '?' + . ')'; - $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())); - // TODO Check if $result succeeded - } + $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())); + // TODO Check if $result succeeded + } } - + /** * Help function to remove contacts from an addressbook. * This only happens when an admin remove an ownCloud user @@ -213,104 +213,104 @@ class OwnCloudUsers extends AbstractBackend { * @return bool */ 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)); - // TODO Check if $result succeeded - } + 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)); + // TODO Check if $result succeeded + } } - + /** * Help function to generate the carddate which than can be stored in the db * @param string|VCard $data * @return Vcard */ 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; - } - } + 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; - } + 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); + $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; + $appinfo = \OCP\App::getAppInfo('contacts'); + $appversion = \OCP\App::getAppVersion('contacts'); + $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN'; + $data->PRODID = $prodid; + + return $data; } - + /** * @inheritdoc */ public function updateContact($addressBookId, $id, $contact, array $options = array()) { - $updateRevision = true; - $isCardDAV = false; + $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 (!$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_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; - } + 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.' - ); - } - } + } 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); - } + 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; + $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; } } From 39ac52062a32aef5a05943b082ef707c05507fdf Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 21:09:48 +0200 Subject: [PATCH 06/29] Add copyright notice --- lib/backend/owncloudusers.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index 4918251f..9b98df2c 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -1,4 +1,24 @@ . + * + */ namespace OCA\Contacts\Backend; From 8362ef1c990aed716c64d3f0a7b31e8d2ae126f0 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 21:20:19 +0200 Subject: [PATCH 07/29] Check if querys succeeded --- lib/backend/owncloudusers.php | 192 +++++++++++++++++++++------------- 1 file changed, 121 insertions(+), 71 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index 9b98df2c..c495aeb0 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -68,29 +68,41 @@ class OwnCloudUsers extends AbstractBackend { if(!$row){ // TODO -> better way? // Create new addressbook - $sql = 'INSERT INTO ' . $this->addressBooksTableName - . ' ( ' - . 'id, ' - . 'displayname, ' - //. 'uri, ' TODO - . 'description, ' - //. 'ctag, ' - . 'active ' - . ') VALUES ( ' - . '?, ' - . '?, ' - . '?, ' - . '? ' - . ')'; - $args = array( - $this->userid, - 'ownCloud Users', - 'ownCloud Users', - 1 - ); - $query = \OCP\DB::prepare($sql); - $query->execute($args); + try{ + $sql = 'INSERT INTO ' . $this->addressBooksTableName + . ' ( ' + . 'id, ' + . 'displayname, ' + //. 'uri, ' TODO + . 'description, ' + //. 'ctag, ' + . 'active ' + . ') VALUES ( ' + . '?, ' + . '?, ' + . '?, ' + . '? ' + . ')'; + $args = array( + $this->userid, + 'ownCloud Users', + 'ownCloud Users', + 1 + ); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return array(); + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return $this->addressBooks; + } + return $this->getAddressBooksForUser(); } else { $row['permissions'] = \OCP\PERMISSION_ALL; @@ -103,15 +115,28 @@ class OwnCloudUsers extends AbstractBackend { * Only 1 addressbook for every user */ public function getAddressBook($addressBookId, array $options = array()) { - - $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; - $args = array($addressBookId); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_ALL; - $row['backend'] = $this->name; - + try{ + $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; + $args = array($addressBookId); + $query = \OCP\DB::prepare($sql); + $result = $query->execute($args); + + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return array(); + } else { + $row = $result->fetchRow(); + // TODO create address book if it doesn't exists + $row['permissions'] = \OCP\PERMISSION_ALL; + $row['backend'] = $this->name; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return $this->addressBooks; + } return array($row); } @@ -121,41 +146,54 @@ class OwnCloudUsers extends AbstractBackend { */ public function getContacts($addressbookid, array $options = array()){ $contacts = array(); + try{ + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($this->userid)); + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return array(); + } else { + while($row = $result->fetchRow()){ + $row['permissions'] = \OCP\PERMISSION_ALL; + $contacts[] = $row; + } - $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; - $query = \OCP\DB::prepare($sql); - $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; + $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; + } + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return $this->addressBooks; } + } /** @@ -166,13 +204,25 @@ class OwnCloudUsers extends AbstractBackend { * ownCloud username 'bar' the params would be: $addressbookid = 'foo'; $id = 'bar'; */ 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; + try{ + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($this->userid)); + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return array(); + } else { + $row = $result->fetchRow(); + $row['permissions'] = \OCP\PERMISSION_ALL; + return $row; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return $this->addressBooks; + } } // Not needed since there is only one addressbook for every user From ab03ba5ff28e8ba420638272701ab19dd308e62f Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 21:25:41 +0200 Subject: [PATCH 08/29] Check if querys succeeded --- lib/backend/owncloudusers.php | 115 ++++++++++++++++++++++------------ 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index c495aeb0..7ace5349 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -98,9 +98,9 @@ class OwnCloudUsers extends AbstractBackend { return array(); } } catch(\Exception $e) { - \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' - . $e->getMessage(), \OCP\Util::ERROR); - return $this->addressBooks; + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return array(); } return $this->getAddressBooksForUser(); @@ -131,13 +131,13 @@ class OwnCloudUsers extends AbstractBackend { // TODO create address book if it doesn't exists $row['permissions'] = \OCP\PERMISSION_ALL; $row['backend'] = $this->name; + return array($row); } } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); - return $this->addressBooks; + return array(); } - return array($row); } /** @@ -191,7 +191,7 @@ class OwnCloudUsers extends AbstractBackend { } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); - return $this->addressBooks; + return array(); } } @@ -221,7 +221,7 @@ class OwnCloudUsers extends AbstractBackend { } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); - return $this->addressBooks; + return array(); } } @@ -239,7 +239,8 @@ class OwnCloudUsers extends AbstractBackend { */ private function addContacts($contacts, $addressbookid){ foreach($contacts as $user){ - $sql = 'INSERT INTO ' . $this->cardsTableName . ' (' + try{ + $sql = 'INSERT INTO ' . $this->cardsTableName . ' (' . 'id, ' . 'owner,' . 'addressbookid, ' @@ -257,24 +258,35 @@ class OwnCloudUsers extends AbstractBackend { . '?' . ')'; - $query = \OCP\DB::prepare($sql); + $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())); - // TODO Check if $result succeeded + $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())); + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return false; + } else { + return true; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return false; + } } } - /** * Help function to remove contacts from an addressbook. * This only happens when an admin remove an ownCloud user @@ -284,11 +296,22 @@ class OwnCloudUsers extends AbstractBackend { */ 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)); - // TODO Check if $result succeeded + try{ + $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?'; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($this->userid, $user)); + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return false; + } else { + return true; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return false; + } } } @@ -369,18 +392,30 @@ class OwnCloudUsers extends AbstractBackend { } $data = $contact->serialize(); - - $sql = 'UPDATE ' . $this->cardsTableName - . ' SET ' + + try{ + $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; + . '`fullname` = ?, ' + . '`carddata` = ?, ' + . '`lastmodified` = ? ' + . ' WHERE ' + . '`id` = ? ' + . 'AND `owner` = ? '; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($addressBookId, $contact->FN, $data, time(), $id, $this->userid)); + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return false; + } else { + return true; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' + . $e->getMessage(), \OCP\Util::ERROR); + return false; + } } } From 69bca057165db68a4288cc4a081f87a8f79dcbf5 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 21:28:49 +0200 Subject: [PATCH 09/29] Undo uneeded changes --- lib/backend/database.php | 2 +- lib/controller/addressbookcontroller.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/backend/database.php b/lib/backend/database.php index 2c7a25ed..5deb3182 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -430,7 +430,6 @@ class Database extends AbstractBackend { \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); return $cards; } - if (!is_null($result)) { @@ -440,6 +439,7 @@ class Database extends AbstractBackend { } } + return $cards; } diff --git a/lib/controller/addressbookcontroller.php b/lib/controller/addressbookcontroller.php index 72f581b0..ab2735f5 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)); } - //} + } } /** From e95ca5124b89629729072eea758a8e40ce8986c2 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 1 Apr 2014 22:04:55 +0200 Subject: [PATCH 10/29] Remove uneeded table for address books --- appinfo/database.xml | 58 ----------------------- lib/backend/owncloudusers.php | 87 ++++++----------------------------- 2 files changed, 15 insertions(+), 130 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index c1fc34c4..dad79f27 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -346,64 +346,6 @@ - - - *dbprefix*contacts_ocu_addressbooks - - - - - id - text - - true - 255 - - - - displayname - text - - false - 255 - - - - uri - text - - false - 200 - - - - description - text - false - 255 - - - - ctag - integer - 1 - true - true - 4 - - - - active - integer - 1 - true - 4 - - - - -
- *dbprefix*contacts_ocu_cards diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index 7ace5349..f2dc98e2 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -52,6 +52,7 @@ class OwnCloudUsers extends AbstractBackend { private $cardsTableName = '*PREFIX*contacts_ocu_cards'; public function __construct($userid){ + $this->userid = $userid ? $userid : \OCP\User::getUser(); } @@ -59,55 +60,7 @@ class OwnCloudUsers extends AbstractBackend { * {@inheritdoc} */ public function getAddressBooksForUser(array $options = array()) { - // Only 1 addressbook for every user - $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; - $args = array($this->userid); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - $row = $result->fetchRow(); - - if(!$row){ // TODO -> better way? - // Create new addressbook - try{ - $sql = 'INSERT INTO ' . $this->addressBooksTableName - . ' ( ' - . 'id, ' - . 'displayname, ' - //. 'uri, ' TODO - . 'description, ' - //. 'ctag, ' - . 'active ' - . ') VALUES ( ' - . '?, ' - . '?, ' - . '?, ' - . '? ' - . ')'; - $args = array( - $this->userid, - 'ownCloud Users', - 'ownCloud Users', - 1 - ); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - - if (\OCP\DB::isError($result)) { - \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' - . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); - return array(); - } - } catch(\Exception $e) { - \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' - . $e->getMessage(), \OCP\Util::ERROR); - return array(); - } - - return $this->getAddressBooksForUser(); - } else { - $row['permissions'] = \OCP\PERMISSION_ALL; - return array($row); - } + return array($this->getAddressBook('admin')); } /** @@ -115,29 +68,19 @@ class OwnCloudUsers extends AbstractBackend { * Only 1 addressbook for every user */ public function getAddressBook($addressBookId, array $options = array()) { - try{ - $sql = 'SELECT * FROM ' . $this->addressBooksTableName . ' WHERE id = ?'; - $args = array($addressBookId); - $query = \OCP\DB::prepare($sql); - $result = $query->execute($args); - - - if (\OCP\DB::isError($result)) { - \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' - . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); - return array(); - } else { - $row = $result->fetchRow(); - // TODO create address book if it doesn't exists - $row['permissions'] = \OCP\PERMISSION_ALL; - $row['backend'] = $this->name; - return array($row); - } - } catch(\Exception $e) { - \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' - . $e->getMessage(), \OCP\Util::ERROR); - return array(); - } + $addressbook = array( + "id" => $addressBookId, + "displayname" => 'ownCloudUsers', + "description" => 'ownCloud Users', + "ctag" => time(), + "permissions" => \OCP\PERMISSION_ALL, + "backend" => $this->name, + "active" => 1 + ); + //var_dump($addressbook); + //throw new \Exception($addressBookId); + + return $addressbook; } /** From dd68af97e4fa6a3e7849b2b08d9d071acd1cbc6e Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 18:54:09 +0200 Subject: [PATCH 11/29] Fix several issues --- lib/backend/owncloudusers.php | 74 +++++++++-------------------------- 1 file changed, 18 insertions(+), 56 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index f2dc98e2..74616560 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -73,7 +73,7 @@ class OwnCloudUsers extends AbstractBackend { "displayname" => 'ownCloudUsers', "description" => 'ownCloud Users', "ctag" => time(), - "permissions" => \OCP\PERMISSION_ALL, + "permissions" => \OCP\PERMISSION_READ, "backend" => $this->name, "active" => 1 ); @@ -90,7 +90,7 @@ class OwnCloudUsers extends AbstractBackend { public function getContacts($addressbookid, array $options = array()){ $contacts = array(); try{ - $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?'; $query = \OCP\DB::prepare($sql); $result = $query->execute(array($this->userid)); @@ -100,7 +100,7 @@ class OwnCloudUsers extends AbstractBackend { return array(); } else { while($row = $result->fetchRow()){ - $row['permissions'] = \OCP\PERMISSION_ALL; + $row['permissions'] = \OCP\PERMISSION_UPDATE; $contacts[] = $row; } @@ -148,7 +148,7 @@ class OwnCloudUsers extends AbstractBackend { */ public function getContact($addressbookid, $id, array $options = array()){ try{ - $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE owner = ?'; + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?'; $query = \OCP\DB::prepare($sql); $result = $query->execute(array($this->userid)); @@ -158,7 +158,7 @@ class OwnCloudUsers extends AbstractBackend { return array(); } else { $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_ALL; + $row['permissions'] = \OCP\PERMISSION_UPDATE; return $row; } } catch(\Exception $e) { @@ -185,36 +185,32 @@ class OwnCloudUsers extends AbstractBackend { try{ $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())); + + $vcard = \Sabre\VObject\Component::create('VCARD'); + $vcard->FN = \OCP\User::getDisplayName($user); + $now = new \DateTime('now'); + $vcard->REV = $now->format(\DateTime::W3C); + + $appinfo = \OCP\App::getAppInfo('contacts'); + $appversion = \OCP\App::getAppVersion('contacts'); + $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN'; + $vcard->PRODID = $prodid; + + + $result = $query->execute(array($user, $this->userid, \OCP\User::getDisplayName($user), $vcard->serialize(), time())); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' @@ -258,40 +254,6 @@ class OwnCloudUsers extends AbstractBackend { } } - /** - * Help function to generate the carddate which than can be stored in the db - * @param string|VCard $data - * @return Vcard - */ - 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; - } - /** * @inheritdoc */ From fa4839c6ae4a1ee0bfccf4df008e36ead9b06799 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 20:31:57 +0200 Subject: [PATCH 12/29] Remove / --- lib/backend/owncloudusers.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index 74616560..9c4cdd53 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -271,26 +271,6 @@ class OwnCloudUsers extends AbstractBackend { } } - 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); From 7b440cb68cdc8c54c6ee53366f018ba376e63972 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 20:37:16 +0200 Subject: [PATCH 13/29] Remove create function --- lib/backend/owncloudusers.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/backend/owncloudusers.php b/lib/backend/owncloudusers.php index 9c4cdd53..275192ca 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/owncloudusers.php @@ -100,7 +100,7 @@ class OwnCloudUsers extends AbstractBackend { return array(); } else { while($row = $result->fetchRow()){ - $row['permissions'] = \OCP\PERMISSION_UPDATE; + $row['permissions'] = \OCP\PERMISSION_READ; $contacts[] = $row; } @@ -158,7 +158,7 @@ class OwnCloudUsers extends AbstractBackend { return array(); } else { $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_UPDATE; + $row['permissions'] = \OCP\PERMISSION_READ; return $row; } } catch(\Exception $e) { @@ -168,11 +168,6 @@ class OwnCloudUsers extends AbstractBackend { } } - // Not needed since there is only one addressbook for every user - public function createAddressBook(array $properties) { - - } - /** * Help function to add contacts to an addressbook. * This only happens when an admin creates new users From 1fa00044927cea4ebb165c7e8ee369a186323681 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 20:39:04 +0200 Subject: [PATCH 14/29] Change name of the backend --- lib/app.php | 2 +- lib/backend/{owncloudusers.php => localusers.php} | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename lib/backend/{owncloudusers.php => localusers.php} (98%) diff --git a/lib/app.php b/lib/app.php index 30339f53..4620f222 100644 --- a/lib/app.php +++ b/lib/app.php @@ -56,7 +56,7 @@ class App { 'ldap' => 'OCA\Contacts\Backend\Ldap', 'local' => 'OCA\Contacts\Backend\Database', 'shared' => 'OCA\Contacts\Backend\Shared', - 'owncloudusers' => 'OCA\Contacts\Backend\OwnCloudUsers', + 'localusers' => 'OCA\Contacts\Backend\LocalUsers', ); public function __construct( diff --git a/lib/backend/owncloudusers.php b/lib/backend/localusers.php similarity index 98% rename from lib/backend/owncloudusers.php rename to lib/backend/localusers.php index 275192ca..d68ed757 100644 --- a/lib/backend/owncloudusers.php +++ b/lib/backend/localusers.php @@ -34,9 +34,9 @@ use OCA\Contacts\Contact, * Every user has *1* personal addressbook. The id of this addresbook is the * userid of the owner. */ -class OwnCloudUsers extends AbstractBackend { +class LocalUsers extends AbstractBackend { - public $name = 'OwnCloudUsers'; + public $name = 'localusers'; /** * The table that holds the address books. @@ -70,8 +70,8 @@ class OwnCloudUsers extends AbstractBackend { public function getAddressBook($addressBookId, array $options = array()) { $addressbook = array( "id" => $addressBookId, - "displayname" => 'ownCloudUsers', - "description" => 'ownCloud Users', + "displayname" => 'Local Users', + "description" => 'Local Users', "ctag" => time(), "permissions" => \OCP\PERMISSION_READ, "backend" => $this->name, From ef12447f8d7524ede5c014634a8bfb0f254a791b Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 21:16:08 +0200 Subject: [PATCH 15/29] Finnaly fix permissions --- lib/backend/localusers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index d68ed757..564a5a0d 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -100,7 +100,7 @@ class LocalUsers extends AbstractBackend { return array(); } else { while($row = $result->fetchRow()){ - $row['permissions'] = \OCP\PERMISSION_READ; + $row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; $contacts[] = $row; } @@ -158,7 +158,7 @@ class LocalUsers extends AbstractBackend { return array(); } else { $row = $result->fetchRow(); - $row['permissions'] = \OCP\PERMISSION_READ; + $row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; return $row; } } catch(\Exception $e) { From 615077b90056d717440529dd0f8755626202d554 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 21:44:03 +0200 Subject: [PATCH 16/29] Fix issue when updating a contact --- lib/backend/localusers.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index 564a5a0d..f7c263a1 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -28,7 +28,6 @@ use OCA\Contacts\Contact, 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 @@ -124,6 +123,8 @@ class LocalUsers extends AbstractBackend { $this->removeContacts($remove, $addressbookid); $recall = true; } + //var_dump($contacts); + //throw new \Exception('err'); if($recall === true){ return $this->getContacts($addressbookid); @@ -148,9 +149,9 @@ class LocalUsers extends AbstractBackend { */ public function getContact($addressbookid, $id, array $options = array()){ try{ - $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?'; + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND id = ?'; $query = \OCP\DB::prepare($sql); - $result = $query->execute(array($this->userid)); + $result = $query->execute(array($this->userid, $id)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' @@ -272,19 +273,17 @@ class LocalUsers extends AbstractBackend { } $data = $contact->serialize(); - try{ $sql = 'UPDATE ' . $this->cardsTableName . ' SET ' - . '`addressbookid` = ?, ' . '`fullname` = ?, ' . '`carddata` = ?, ' . '`lastmodified` = ? ' . ' WHERE ' . '`id` = ? ' - . 'AND `owner` = ? '; + . 'AND `addressbookid` = ? '; $query = \OCP\DB::prepare($sql); - $result = $query->execute(array($addressBookId, $contact->FN, $data, time(), $id, $this->userid)); + $result = $query->execute(array($contact->FN, $data, time(), $id, $this->userid)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); From 4cf45fa91f60ce36d599920c7ed95e07b5ccb3aa Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 2 Apr 2014 21:49:53 +0200 Subject: [PATCH 17/29] fix issue when a user is removed --- lib/backend/localusers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index f7c263a1..0244ad19 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -59,7 +59,7 @@ class LocalUsers extends AbstractBackend { * {@inheritdoc} */ public function getAddressBooksForUser(array $options = array()) { - return array($this->getAddressBook('admin')); + return array($this->getAddressBook($this->userid)); } /** @@ -232,7 +232,7 @@ class LocalUsers extends AbstractBackend { private function removeContacts($contacts, $addressbookid){ foreach($contacts as $user){ try{ - $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE owner = ? AND id = ?'; + $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND id = ?'; $query = \OCP\DB::prepare($sql); $result = $query->execute(array($this->userid, $user)); if (\OCP\DB::isError($result)) { From e70814997f41b774c362d64e6bb98c02811585a6 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Thu, 3 Apr 2014 18:36:11 +0200 Subject: [PATCH 18/29] Add search() function --- appinfo/app.php | 1 + lib/backend/localusers.php | 7 ++- lib/localusersaddressbookprovider.php | 86 +++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 lib/localusersaddressbookprovider.php diff --git a/appinfo/app.php b/appinfo/app.php index 60112aea..c36e43df 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -68,3 +68,4 @@ if (\OCP\User::isLoggedIn()) { } } } + diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index 0244ad19..69facf06 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -26,7 +26,8 @@ use OCA\Contacts\Contact, OCA\Contacts\VObject\VCard, OCA\Contacts\Utils\Properties, Sabre\VObject\Reader, - OCA\Contacts\Addressbook; + OCA\Contacts\Addressbook, + OCA\Contacts\LocalUsersAddressbookProvider; /** * Contact backend for storing all the ownCloud users in this installation. @@ -297,4 +298,8 @@ class LocalUsers extends AbstractBackend { return false; } } + + public function getSearchProvider(){ + return new LocalUsersAddressbookProvider(); + } } diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php new file mode 100644 index 00000000..433d6996 --- /dev/null +++ b/lib/localusersaddressbookprovider.php @@ -0,0 +1,86 @@ +execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%")); + } elseif(in_array("FN", $searchProperties)){ + echo "fn"; + $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? '; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); + } elseif(in_array("id", $searchProperties)){ + echo "id"; + $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? '; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); + } else { + echo "else"; + $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?'; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array(\OCP\User::getUser())); + } + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), + \OCP\Util::ERROR); + return false; + } + + $contacts = array(); + + while( $row = $result->fetchRow()) { + $contacts[] = $row['id']; + } + + return $contacts; + } + + public function getKey(){ + + } + + /** + * In comparison to getKey() this function returns a human readable (maybe translated) name + * @return mixed + */ + public function getDisplayName(){ + +} + + public function createOrUpdate($properties){ + + } + + /** + * @return mixed + */ + public function getPermissions(){ + + } + + /** + * @param object $id the unique identifier to a contact + * @return bool successful or not + */ + public function delete($id){ + + } + +} From 00cf07ec1a67eb81215ab310a928d7854e20023b Mon Sep 17 00:00:00 2001 From: LEDfan Date: Thu, 3 Apr 2014 18:59:34 +0200 Subject: [PATCH 19/29] Small cleanup --- lib/backend/localusers.php | 7 +------ lib/localusersaddressbookprovider.php | 8 ++------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index 69facf06..c6a698fa 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -77,9 +77,6 @@ class LocalUsers extends AbstractBackend { "backend" => $this->name, "active" => 1 ); - //var_dump($addressbook); - //throw new \Exception($addressBookId); - return $addressbook; } @@ -124,8 +121,6 @@ class LocalUsers extends AbstractBackend { $this->removeContacts($remove, $addressbookid); $recall = true; } - //var_dump($contacts); - //throw new \Exception('err'); if($recall === true){ return $this->getContacts($addressbookid); @@ -232,7 +227,7 @@ class LocalUsers extends AbstractBackend { */ private function removeContacts($contacts, $addressbookid){ foreach($contacts as $user){ - try{ + try{ $sql = 'DELETE FROM ' . $this->cardsTableName . ' WHERE addressbookid = ? AND id = ?'; $query = \OCP\DB::prepare($sql); $result = $query->execute(array($this->userid, $user)); diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 433d6996..63e09288 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -16,22 +16,18 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { */ public function search($pattern, $searchProperties, $options) { if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){ - echo "beide"; $query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) '; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%")); } elseif(in_array("FN", $searchProperties)){ - echo "fn"; $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? '; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); } elseif(in_array("id", $searchProperties)){ - echo "id"; $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? '; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); } else { - echo "else"; $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?'; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute(array(\OCP\User::getUser())); @@ -46,7 +42,7 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { $contacts = array(); while( $row = $result->fetchRow()) { - $contacts[] = $row['id']; + $contacts[] = $row; } return $contacts; @@ -80,7 +76,7 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { * @return bool successful or not */ public function delete($id){ - + } } From 90a090af985feb9f58f14e1ca5a1dceee6369619 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sat, 5 Apr 2014 18:56:21 +0200 Subject: [PATCH 20/29] Add index properties to provide a solid search function --- appinfo/database.xml | 60 ++++++++++++++++++++++++ appinfo/version | 2 +- lib/backend/database.php | 5 ++ lib/backend/localusers.php | 66 ++++++++++++++++++++++++++- lib/localusersaddressbookprovider.php | 26 ++++++----- 5 files changed, 144 insertions(+), 15 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index dad79f27..703e4edf 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -411,4 +411,64 @@
+ + + + *dbprefix*contacts_ocu_cards_properties + + + + + id + integer + 0 + true + 1 + true + 4 + + + + addressbookid + text + + true + 255 + + + + contactid + text + + true + 255 + + + + name + text + + false + 64 + + + + value + text + + false + 255 + + + + preferred + integer + 1 + true + 4 + + + +
+ diff --git a/appinfo/version b/appinfo/version index 8d13a336..7af4042f 100644 --- a/appinfo/version +++ b/appinfo/version @@ -1 +1 @@ -0.3.0.8 \ No newline at end of file +0.3.0.14 \ No newline at end of file diff --git a/lib/backend/database.php b/lib/backend/database.php index 5deb3182..2a0e9a04 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -1018,4 +1018,9 @@ class Database extends AbstractBackend { return self::$preparedQueries[$identifier]; } + + public function getSearchProvider($addressbook){ + return new \OCA\Contacts\AddressbookProvider($addressbook); + } + } diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index c6a698fa..d2c679b0 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -50,7 +50,13 @@ class LocalUsers extends AbstractBackend { * @var string */ private $cardsTableName = '*PREFIX*contacts_ocu_cards'; + + private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; + private $indexProperties = array( + 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', + 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO'); + public function __construct($userid){ $this->userid = $userid ? $userid : \OCP\User::getUser(); @@ -209,6 +215,9 @@ class LocalUsers extends AbstractBackend { . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return false; } else { + // All done + // now update the index table with all the properties + $this->updateIndex($user, $vcard); return true; } } catch(\Exception $e) { @@ -268,7 +277,6 @@ class LocalUsers extends AbstractBackend { $contact->REV = $now->format(\DateTime::W3C); } - $data = $contact->serialize(); try{ $sql = 'UPDATE ' . $this->cardsTableName . ' SET ' @@ -279,12 +287,15 @@ class LocalUsers extends AbstractBackend { . '`id` = ? ' . 'AND `addressbookid` = ? '; $query = \OCP\DB::prepare($sql); - $result = $query->execute(array($contact->FN, $data, time(), $id, $this->userid)); + $result = $query->execute(array($contact->FN, $contact->serialize(), time(), $id, $this->userid)); if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return false; } else { + // All done + // now update the indexes in the DB + $this->updateIndex($id, $contact); return true; } } catch(\Exception $e) { @@ -297,4 +308,55 @@ class LocalUsers extends AbstractBackend { public function getSearchProvider(){ return new LocalUsersAddressbookProvider(); } + + private function updateIndex($contactId, $vcard){ + // Utils\Properties::updateIndex($parameters['id'], $contact); + $this->purgeIndex($contactId); + $updatestmt = \OCP\DB::prepare('INSERT INTO `' . $this->indexTableName . '` ' + . '(`addressbookid`, `contactid`,`name`,`value`,`preferred`) VALUES(?,?,?,?,?)'); + // Insert all properties in the table + foreach($vcard->children as $property) { + if(!in_array($property->name, $this->indexProperties)) { + continue; + } + $preferred = 0; + foreach($property->parameters as $parameter) { + if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') { + $preferred = 1; + break; + } + } + try { + $result = $updatestmt->execute( + array( + \OCP\User::getUser(), + $contactId, + $property->name, + substr($property->value, 0, 254), + $preferred, + ) + ); + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return false; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + return false; + } + } + } + + private function purgeIndex($id){ + // Remove all indexes from the table + try { + $query = \OCP\DB::prepare('DELETE FROM `' . $this->indexTableName . '`' + . ' WHERE `contactid` = ?'); + $query->execute(array($id)); + + } catch(\Exception $e) { + return false; + } + } } diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 63e09288..3b813109 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -15,18 +15,20 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { * @return array|false */ public function search($pattern, $searchProperties, $options) { - if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){ - $query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) '; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%")); - } elseif(in_array("FN", $searchProperties)){ - $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? '; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); - } elseif(in_array("id", $searchProperties)){ - $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? '; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); + if($pattern !== ''){ + if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){ + $query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) '; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%")); + } elseif(in_array("FN", $searchProperties)){ + $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? '; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); + } elseif(in_array("id", $searchProperties)){ + $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? '; + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); + } } else { $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?'; $stmt = \OCP\DB::prepare($query); From 2835402b578d3147ae04cb8e414725195aedf3f4 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sat, 5 Apr 2014 20:35:29 +0200 Subject: [PATCH 21/29] Add searc() func tion --- lib/localusersaddressbookprovider.php | 108 +++++++++++++++++++------- 1 file changed, 81 insertions(+), 27 deletions(-) diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 3b813109..02ffae30 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -2,8 +2,15 @@ namespace OCA\Contacts; +use OCA\Contacts\Utils\Properties; + + class LocalUsersAddressbookProvider implements \OCP\IAddressBook { + private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; + private $contactTableName = '*PREFIX*contacts_ocu_cards'; + + public function __construct(){ } @@ -15,39 +22,45 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { * @return array|false */ public function search($pattern, $searchProperties, $options) { - if($pattern !== ''){ - if(in_array("FN", $searchProperties) && in_array("id", $searchProperties)){ - $query = 'SELECT DISTINCT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND (`id` LIKE ? OR `fullname` LIKE ?) '; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%", '%' . $pattern . "%")); - } elseif(in_array("FN", $searchProperties)){ - $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `fullname` LIKE ? '; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); - } elseif(in_array("id", $searchProperties)){ - $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ? AND `id` LIKE ? '; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser(), '%' . $pattern . "%")); - } - } else { - $query = 'SELECT * FROM `*PREFIX*contacts_ocu_cards` WHERE addressbookid = ?'; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array(\OCP\User::getUser())); + $ids = array(); + $results = array(); + $query = 'SELECT DISTINCT `contactid` FROM `' . $this->indexTableName . '` WHERE ('; + $params = array(); + foreach($searchProperties as $property) { + $params[] = $property; + $params[] = '%' . $pattern . '%'; + $query .= '(`name` = ? AND `value` LIKE ?) OR '; } - + $query = substr($query, 0, strlen($query) - 4); + $query .= ')'; + + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute($params); if (\OCP\DB::isError($result)) { - \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), - \OCP\Util::ERROR); - return false; + \OCP\Util::writeLog('contacts', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), + \OCP\Util::ERROR); + return false; } - - $contacts = array(); - while( $row = $result->fetchRow()) { - $contacts[] = $row; + $ids[] = $row['contactid']; } - return $contacts; + if(count($ids) > 0) { + $query = 'SELECT `' . $this->contactTableName . '`.`addressbookid`, `' . $this->indexTableName . '`.`contactid`, `' + . $this->indexTableName . '`.`name`, `' . $this->indexTableName . '`.`value` FROM `' + . $this->indexTableName . '`,`' . $this->contactTableName . '` WHERE `' + . $this->contactTableName . '`.`addressbookid` = \'' . \OCP\User::getUser() . '\' AND `' + . $this->indexTableName . '`.`contactid` = `' . $this->contactTableName . '`.`id` AND `' + . $this->indexTableName . '`.`contactid` IN (' . join(',', array_fill(0, count($ids), '?')) . ')'; + + $stmt = \OCP\DB::prepare($query); + $result = $stmt->execute($ids); + } + + while( $row = $result->fetchRow()) { + $this->getProperty($results, $row); + } + return $results; } public function getKey(){ @@ -80,5 +93,46 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { public function delete($id){ } + + private function getProperty(&$results, $row) { + if(!$row['name'] || !$row['value']) { + return false; + } + + $value = null; + + switch($row['name']) { + case 'PHOTO': + $value = 'VALUE=uri:' . \OCP\Util::linkToAbsolute('contacts', 'photo.php') . '?id=' . $row['contactid']; + break; + case 'N': + case 'ORG': + case 'ADR': + case 'GEO': + case 'CATEGORIES': + $property = \Sabre\VObject\Property::create($row['name'], $row['value']); + $value = $property->getParts(); + break; + default: + $value = $value = strtr($row['value'], array('\,' => ',', '\;' => ';')); + break; + } + + if(in_array($row['name'], Properties::$multiProperties)) { + if(!isset($results[$row['contactid']])) { + $results[$row['contactid']] = array('id' => $row['contactid'], $row['name'] => array($value)); + } elseif(!isset($results[$row['contactid']][$row['name']])) { + $results[$row['contactid']][$row['name']] = array($value); + } else { + $results[$row['contactid']][$row['name']][] = $value; + } + } else { + if(!isset($results[$row['contactid']])) { + $results[$row['contactid']] = array('id' => $row['contactid'], $row['name'] => $value); + } elseif(!isset($results[$row['contactid']][$row['name']])) { + $results[$row['contactid']][$row['name']] = $value; + } + } + } } From 17d7ed7e9913955f4d5c5d9db661087b68ebaab1 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sun, 6 Apr 2014 08:21:24 +0200 Subject: [PATCH 22/29] Add x-owncloud-handle by default --- lib/backend/localusers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index d2c679b0..9eceda90 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -206,7 +206,7 @@ class LocalUsers extends AbstractBackend { $appversion = \OCP\App::getAppVersion('contacts'); $prodid = '-//ownCloud//NONSGML ' . $appinfo['name'] . ' ' . $appversion.'//EN'; $vcard->PRODID = $prodid; - + $vcard->add('IMPP', 'x-owncloud-handle:' . $user, array("X-SERVICE-TYPE" => array("owncloud-handle"))); $result = $query->execute(array($user, $this->userid, \OCP\User::getDisplayName($user), $vcard->serialize(), time())); From c769e33a4961257cafe8031774a34343951798a8 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sun, 6 Apr 2014 08:39:58 +0200 Subject: [PATCH 23/29] Add docs --- lib/backend/localusers.php | 89 ++++++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 24 deletions(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index 9eceda90..25ed2e98 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -51,14 +51,36 @@ class LocalUsers extends AbstractBackend { */ private $cardsTableName = '*PREFIX*contacts_ocu_cards'; + /** + * The table that holds the properties of the contacts. + * This is used to provice a search function. + * @var string + */ private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; + /** + * All possible properties which can be stored in the $indexTableName. + * @var string + */ private $indexProperties = array( 'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME', 'ORG', 'CATEGORIES', 'EMAIL', 'TEL', 'IMPP', 'ADR', 'URL', 'GEO'); + /** + * language object + * @var OC_L10N + */ + public static $l10n; + + /** + * Defaults object + * @var OC_Defaults + */ + public static $defaults; + public function __construct($userid){ - + self::$l10n = \OCP\Util::getL10N('contacts'); + self::$defaults = new \OCP\Defaults(); $this->userid = $userid ? $userid : \OCP\User::getUser(); } @@ -76,8 +98,8 @@ class LocalUsers extends AbstractBackend { public function getAddressBook($addressBookId, array $options = array()) { $addressbook = array( "id" => $addressBookId, - "displayname" => 'Local Users', - "description" => 'Local Users', + "displayname" => self::$l10n->t('On this') . self::$defaults->getName(), + "description" => self::$l10n->t('On this') . self::$defaults->getName(), "ctag" => time(), "permissions" => \OCP\PERMISSION_READ, "backend" => $this->name, @@ -227,6 +249,7 @@ class LocalUsers extends AbstractBackend { } } } + /** * Help function to remove contacts from an addressbook. * This only happens when an admin remove an ownCloud user @@ -305,10 +328,21 @@ class LocalUsers extends AbstractBackend { } } + /** + * This is a hack so backends can have different search functions. + * @return \OCA\Contacts\LocalUsersAddressbookProvider + */ public function getSearchProvider(){ return new LocalUsersAddressbookProvider(); } + /** + * Updates the index table. All properties of a contact are stored in it. + * Needed for the search function. + * @param type $contactId + * @param type $vcard + * @return boolean + */ private function updateIndex($contactId, $vcard){ // Utils\Properties::updateIndex($parameters['id'], $contact); $this->purgeIndex($contactId); @@ -317,37 +351,44 @@ class LocalUsers extends AbstractBackend { // Insert all properties in the table foreach($vcard->children as $property) { if(!in_array($property->name, $this->indexProperties)) { - continue; + continue; } $preferred = 0; foreach($property->parameters as $parameter) { - if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') { - $preferred = 1; - break; - } + if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') { + $preferred = 1; + break; + } } try { - $result = $updatestmt->execute( - array( - \OCP\User::getUser(), - $contactId, - $property->name, - substr($property->value, 0, 254), - $preferred, - ) - ); - if (\OCP\DB::isError($result)) { - \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' - . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); - return false; - } - } catch(\Exception $e) { - \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + $result = $updatestmt->execute( + array( + \OCP\User::getUser(), + $contactId, + $property->name, + substr($property->value, 0, 254), + $preferred, + ) + ); + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return false; + } + } catch(\Exception $e) { + \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); + return false; } } } + /** + * Remove all indexes from the table. + * This is always called before adding new properties. + * @param type $contactId + * @param type $vcard + * @return boolean + */ private function purgeIndex($id){ // Remove all indexes from the table try { From 393c0cf5340feb64211b427bd6ac207f5d3a6823 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sun, 6 Apr 2014 08:41:08 +0200 Subject: [PATCH 24/29] Add docs --- lib/localusersaddressbookprovider.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 02ffae30..46063aaa 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -7,9 +7,25 @@ use OCA\Contacts\Utils\Properties; class LocalUsersAddressbookProvider implements \OCP\IAddressBook { - private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; - private $contactTableName = '*PREFIX*contacts_ocu_cards'; + /** + * 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'; + + /** + * The table that holds the properties of the contacts. + * This is used to provice a search function. + * @var string + */ + private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; public function __construct(){ @@ -46,10 +62,10 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { } if(count($ids) > 0) { - $query = 'SELECT `' . $this->contactTableName . '`.`addressbookid`, `' . $this->indexTableName . '`.`contactid`, `' + $query = 'SELECT `' . $this->cardsTableName . '`.`addressbookid`, `' . $this->indexTableName . '`.`contactid`, `' . $this->indexTableName . '`.`name`, `' . $this->indexTableName . '`.`value` FROM `' - . $this->indexTableName . '`,`' . $this->contactTableName . '` WHERE `' - . $this->contactTableName . '`.`addressbookid` = \'' . \OCP\User::getUser() . '\' AND `' + . $this->indexTableName . '`,`' . $this->cardsTableNam . '` WHERE `' + . $this->cardsTableNam . '`.`addressbookid` = \'' . \OCP\User::getUser() . '\' AND `' . $this->indexTableName . '`.`contactid` = `' . $this->contactTableName . '`.`id` AND `' . $this->indexTableName . '`.`contactid` IN (' . join(',', array_fill(0, count($ids), '?')) . ')'; From 25d79d534c4c058f14b66710d91be5768b34d3eb Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sun, 6 Apr 2014 09:50:02 +0200 Subject: [PATCH 25/29] fix typos --- lib/localusersaddressbookprovider.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 46063aaa..9884c26a 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -64,9 +64,9 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { if(count($ids) > 0) { $query = 'SELECT `' . $this->cardsTableName . '`.`addressbookid`, `' . $this->indexTableName . '`.`contactid`, `' . $this->indexTableName . '`.`name`, `' . $this->indexTableName . '`.`value` FROM `' - . $this->indexTableName . '`,`' . $this->cardsTableNam . '` WHERE `' - . $this->cardsTableNam . '`.`addressbookid` = \'' . \OCP\User::getUser() . '\' AND `' - . $this->indexTableName . '`.`contactid` = `' . $this->contactTableName . '`.`id` AND `' + . $this->indexTableName . '`,`' . $this->cardsTableName . '` WHERE `' + . $this->cardsTableName . '`.`addressbookid` = \'' . \OCP\User::getUser() . '\' AND `' + . $this->indexTableName . '`.`contactid` = `' . $this->cardsTableName . '`.`id` AND `' . $this->indexTableName . '`.`contactid` IN (' . join(',', array_fill(0, count($ids), '?')) . ')'; $stmt = \OCP\DB::prepare($query); From a65549f089149a448c2547023bb6965e05caa0c0 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Sun, 6 Apr 2014 09:58:49 +0200 Subject: [PATCH 26/29] Update DB on getContacts, getContact and search --- lib/backend/localusers.php | 69 +++++++++++++++------------ lib/localusersaddressbookprovider.php | 13 ++++- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index 25ed2e98..5a31aa92 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -113,49 +113,24 @@ class LocalUsers extends AbstractBackend { * There are as many contacts in this addressbook as in this ownCloud installation */ public function getContacts($addressbookid, array $options = array()){ + $this->updateDatabase(); $contacts = array(); try{ $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?'; $query = \OCP\DB::prepare($sql); $result = $query->execute(array($this->userid)); - + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); - return array(); + return true; } else { while($row = $result->fetchRow()){ - $row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; + $row['permissions'] = \OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE; $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; } } + return $contacts; } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); @@ -333,7 +308,7 @@ class LocalUsers extends AbstractBackend { * @return \OCA\Contacts\LocalUsersAddressbookProvider */ public function getSearchProvider(){ - return new LocalUsersAddressbookProvider(); + return new LocalUsersAddressbookProvider($this); } /** @@ -400,4 +375,36 @@ class LocalUsers extends AbstractBackend { return false; } } + + public function updateDatabase(){ + $sql = 'SELECT * FROM ' . $this->cardsTableName . ' WHERE addressbookid = ?'; + $query = \OCP\DB::prepare($sql); + $result = $query->execute(array($this->userid)); + + if (\OCP\DB::isError($result)) { + \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' + . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); + return true; + } else { + while($row = $result->fetchRow()){ + $contactsId[] = $row['id']; + } + + $users = \OCP\User::getUsers(); + + $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; + } + return true; + } + } + } diff --git a/lib/localusersaddressbookprovider.php b/lib/localusersaddressbookprovider.php index 9884c26a..8176ce57 100644 --- a/lib/localusersaddressbookprovider.php +++ b/lib/localusersaddressbookprovider.php @@ -3,6 +3,7 @@ namespace OCA\Contacts; use OCA\Contacts\Utils\Properties; +use OCA\Contacts\Backend\LocalUsers; class LocalUsersAddressbookProvider implements \OCP\IAddressBook { @@ -27,8 +28,13 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { */ private $indexTableName = '*PREFIX*contacts_ocu_cards_properties'; - public function __construct(){ - + /** + * @var LocalUsers + */ + private $backend; + + public function __construct(LocalUsers $backend){ + $this->backend = $backend; } /** @@ -38,6 +44,9 @@ class LocalUsersAddressbookProvider implements \OCP\IAddressBook { * @return array|false */ public function search($pattern, $searchProperties, $options) { + // First make sure the database is updated + $this->backend->updateDatabase(); + $ids = array(); $results = array(); $query = 'SELECT DISTINCT `contactid` FROM `' . $this->indexTableName . '` WHERE ('; From 093ff0920c37712bb0751dfc776acc3a7d9fd18b Mon Sep 17 00:00:00 2001 From: LEDfan Date: Tue, 8 Apr 2014 16:35:10 +0200 Subject: [PATCH 27/29] solve bug --- lib/backend/localusers.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/backend/localusers.php b/lib/backend/localusers.php index 5a31aa92..5e20b158 100644 --- a/lib/backend/localusers.php +++ b/lib/backend/localusers.php @@ -386,6 +386,7 @@ class LocalUsers extends AbstractBackend { . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return true; } else { + $contactsId = array(); while($row = $result->fetchRow()){ $contactsId[] = $row['id']; } From 7a610c487b4cddcedc7b39ce0d5e3ea80457416f Mon Sep 17 00:00:00 2001 From: Jenkins for ownCloud Date: Tue, 15 Apr 2014 00:48:52 -0400 Subject: [PATCH 28/29] [tx-robot] updated from transifex --- l10n/cs_CZ.php | 2 +- l10n/cs_CZ/contacts.po | 42 ++++++++++++++++++------------------- l10n/templates/contacts.pot | 2 +- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/l10n/cs_CZ.php b/l10n/cs_CZ.php index f86cdee4..d83c24e7 100644 --- a/l10n/cs_CZ.php +++ b/l10n/cs_CZ.php @@ -174,7 +174,7 @@ "Address books" => "Adresáře kontaktů", "Display name" => "Zobrazované jméno", "Add Address Book" => "Přidat adresář kontaktů", -"Automatic format" => "Automatícký formát", +"Automatic format" => "Automatický formát", "Select file..." => "Vybrat soubor...", "(De-)select all" => "Vybrat (odznačit) vše", "Sort order" => "Řazení", diff --git a/l10n/cs_CZ/contacts.po b/l10n/cs_CZ/contacts.po index e7c369d2..67de9689 100644 --- a/l10n/cs_CZ/contacts.po +++ b/l10n/cs_CZ/contacts.po @@ -18,9 +18,9 @@ msgid "" msgstr "" "Project-Id-Version: ownCloud\n" "Report-Msgid-Bugs-To: translations@owncloud.org\n" -"POT-Creation-Date: 2014-04-08 00:50-0400\n" -"PO-Revision-Date: 2014-04-08 04:50+0000\n" -"Last-Translator: I Robot\n" +"POT-Creation-Date: 2014-04-15 00:48-0400\n" +"PO-Revision-Date: 2014-04-14 05:00+0000\n" +"Last-Translator: pstast \n" "Language-Team: Czech (Czech Republic) (http://www.transifex.com/projects/p/owncloud/language/cs_CZ/)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" @@ -116,7 +116,7 @@ msgstr "Sloučení selhalo. Chyba při ukládání kontaktu." msgid "Select photo" msgstr "Vybrat fotku" -#: js/app.js:705 js/app.js:1675 +#: js/app.js:705 js/app.js:1679 msgid "Network or server error. Please inform administrator." msgstr "Chyba sítě či serveru. Kontaktujte prosím správce." @@ -167,11 +167,11 @@ msgstr "OK" msgid "Could not find contact: {id}" msgstr "Nelze nalézt kontakt: {id}" -#: js/app.js:1630 +#: js/app.js:1634 msgid "Edit profile picture" msgstr "Upravit obrázek profilu" -#: js/app.js:1634 +#: js/app.js:1638 msgid "Crop photo" msgstr "Oříznout fotku" @@ -191,17 +191,17 @@ msgstr "Chyba parsování narozenin {bday}" msgid "The backend does not support multi-byte characters." msgstr "Úložiště nepodporuje více-bajtové znaky." -#: js/contacts.js:2222 +#: js/contacts.js:2224 msgid "" "Some contacts are marked for deletion, but not deleted yet. Please wait for " "them to be deleted." msgstr "Některé kontakty jsou označeny ke smazání, ale ještě smazány nejsou. Počkejte, prosím, na dokončení operace." -#: js/contacts.js:2233 +#: js/contacts.js:2235 msgid "Click to undo deletion of {num} contacts" msgstr "Klikněte pro navrácení smazání {num} kontaktů" -#: js/contacts.js:2242 +#: js/contacts.js:2244 msgid "Cancelled deletion of {num} contacts" msgstr "Smazání {num} kontaktů zrušeno" @@ -388,49 +388,49 @@ msgstr "Neznámý komunikátor: " msgid "{name}'s Birthday" msgstr "Narozeniny {name}" -#: lib/controller/addressbookcontroller.php:163 +#: lib/controller/addressbookcontroller.php:175 msgid "Error creating address book" msgstr "Chyba při vytváření adresáře kontaktů" -#: lib/controller/addressbookcontroller.php:195 +#: lib/controller/addressbookcontroller.php:207 #, php-format msgid "The \"%s\" backend does not support deleting address books" msgstr "Toto uložiště \"%s\" nepodporuje mazání adresářů kontaktů" -#: lib/controller/addressbookcontroller.php:203 +#: lib/controller/addressbookcontroller.php:215 #, php-format msgid "You do not have permissions to delete the \"%s\" address book" msgstr "Nemáte oprávnění pro smazání adresáře kontaktů \"%s\"" -#: lib/controller/addressbookcontroller.php:210 +#: lib/controller/addressbookcontroller.php:222 msgid "Error deleting address book" msgstr "Chyba při mazání adresáře kontaktů" -#: lib/controller/addressbookcontroller.php:250 +#: lib/controller/addressbookcontroller.php:262 msgid "Error creating contact." msgstr "Chyba při vytváření kontaktu" -#: lib/controller/addressbookcontroller.php:259 +#: lib/controller/addressbookcontroller.php:271 msgid "Error creating contact" msgstr "Chyba při vytváření kontaktu" -#: lib/controller/addressbookcontroller.php:291 +#: lib/controller/addressbookcontroller.php:303 msgid "Error deleting contact" msgstr "Chyba při odstraňování kontaktu" -#: lib/controller/addressbookcontroller.php:331 +#: lib/controller/addressbookcontroller.php:343 msgid "Error retrieving contact" msgstr "Chyba při otevírání kontaktu" -#: lib/controller/addressbookcontroller.php:342 +#: lib/controller/addressbookcontroller.php:354 msgid "Error saving contact" msgstr "Chyba při ukládání kontaktu" -#: lib/controller/addressbookcontroller.php:348 +#: lib/controller/addressbookcontroller.php:360 msgid "Error removing contact from other address book." msgstr "Chyba při odebírání kontaktu z jiného adresáře kontaktů." -#: lib/controller/addressbookcontroller.php:355 +#: lib/controller/addressbookcontroller.php:367 msgid "Error getting moved contact" msgstr "Chyba při získávání přesunutého kontaktu" @@ -753,7 +753,7 @@ msgstr "Přidat adresář kontaktů" #: templates/contacts.php:35 msgid "Automatic format" -msgstr "Automatícký formát" +msgstr "Automatický formát" #: templates/contacts.php:44 templates/contacts.php:45 msgid "Select file..." diff --git a/l10n/templates/contacts.pot b/l10n/templates/contacts.pot index 0b355782..ea0bc165 100644 --- a/l10n/templates/contacts.pot +++ b/l10n/templates/contacts.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: ownCloud contacts 5.0.0\n" "Report-Msgid-Bugs-To: translations@owncloud.org\n" -"POT-Creation-Date: 2014-04-14 00:48-0400\n" +"POT-Creation-Date: 2014-04-15 00:48-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" From 26bed15085a02b3af1ea0abd1d4b789831e47c13 Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Tue, 15 Apr 2014 14:06:44 +0200 Subject: [PATCH 29/29] UI permission fixes noticed when merging #457 --- js/addressbooks.js | 8 +++++++- js/contacts.js | 19 +++++++++++-------- templates/contacts.php | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/js/addressbooks.js b/js/addressbooks.js index f3840227..71342c7f 100644 --- a/js/addressbooks.js +++ b/js/addressbooks.js @@ -29,6 +29,12 @@ OC.Contacts = OC.Contacts || {}; if(!this.hasPermission(OC.PERMISSION_UPDATE)) { this.$li.find('a.action.edit').hide(); } + if(!this.hasPermission(OC.PERMISSION_SHARE)) { + this.$li.find('a.action.share').hide(); + } + if(['local', 'ldap'].indexOf(this.getBackend() === -1)) { + this.$li.find('a.action.carddav').hide(); + } this.$li.find('input:checkbox').prop('checked', this.book.active).on('change', function() { console.log('activate', self.getId()); var checkbox = $(this).get(0); @@ -53,7 +59,7 @@ OC.Contacts = OC.Contacts || {}; console.log('delete', self.getId()); self.destroy(); }); - this.$li.find('a.action.globe').on('click keypress', function() { + this.$li.find('a.action.carddav').on('click keypress', function() { var uri = (self.book.owner === oc_current_user ) ? self.book.uri : self.book.uri + '_shared_by_' + self.book.owner; var link = OC.linkToRemote('carddav')+'/addressbooks/'+encodeURIComponent(oc_current_user)+'/'+encodeURIComponent(uri); var $dropdown = $('
  • ') diff --git a/js/contacts.js b/js/contacts.js index 8306745b..044c9e7d 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -1488,15 +1488,18 @@ OC.Contacts = OC.Contacts || {}; } } }); - if(this.metadata.owner !== OC.currentUser - && !(this.hasPermission(OC.PERMISSION_UPDATE) - || this.hasPermission(OC.PERMISSION_DELETE))) { - this.setEnabled(false); - this.showActions(['close', 'export']); - } else { - this.setEnabled(true); - this.showActions(['close', 'add', 'export', 'delete']); + var actions = ['close', 'export']; + if(this.hasPermission(OC.PERMISSION_DELETE)) { + actions.push('delete'); } + if(this.hasPermission(OC.PERMISSION_UPDATE)) { + actions.push('add'); + this.setEnabled(true); + } else { + this.setEnabled(false); + } + this.showActions(actions); + return this.$fullelem; }; diff --git a/templates/contacts.php b/templates/contacts.php index 74f63911..c06e6440 100644 --- a/templates/contacts.php +++ b/templates/contacts.php @@ -501,7 +501,7 @@ use OCA\Contacts\ImportManager; - +