diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 7223e1233..dc9ec7353 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -1,6 +1,7 @@ filter: excluded_paths: - 'js/modernizr.custom.js' + - 'js/jquery.onfontresize.js' - 'l10n/*' - 'js/placeholder*' - 'css/placeholder*' diff --git a/appinfo/app.php b/appinfo/app.php index 98a801b03..1b1d288b3 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -8,6 +8,7 @@ */ namespace OCA\Contacts; + use \OC\AppFramework\Core\API; //require_once __DIR__ . '/../lib/controller/pagecontroller.php'; @@ -58,11 +59,11 @@ \OCP\Share::registerBackend('addressbook', 'OCA\Contacts\Share\Addressbook', 'contact'); //\OCP\App::registerPersonal('contacts','personalsettings'); -if(\OCP\User::isLoggedIn()) { +if (\OCP\User::isLoggedIn()) { $app = new App($api->getUserId()); $addressBooks = $app->getAddressBooksForUser(); - foreach($addressBooks as $addressBook) { - if($addressBook->isActive()) { + foreach ($addressBooks as $addressBook) { + if ($addressBook->isActive()) { \OCP\Contacts::registerAddressBook(new AddressbookProvider($addressBook)); } } diff --git a/appinfo/remote.php b/appinfo/remote.php index e99e296b1..31bdcbc9d 100644 --- a/appinfo/remote.php +++ b/appinfo/remote.php @@ -24,14 +24,14 @@ OCP\App::checkAppEnabled('contacts'); -if(substr(OCP\Util::getRequestUri(), 0, strlen(OC_App::getAppWebPath('contacts').'/carddav.php')) +if (substr(OCP\Util::getRequestUri(), 0, strlen(OC_App::getAppWebPath('contacts').'/carddav.php')) === OC_App::getAppWebPath('contacts').'/carddav.php' ) { $baseuri = OC_App::getAppWebPath('contacts').'/carddav.php'; } // only need authentication apps -$RUNTIME_APPTYPES=array('authentication'); +$RUNTIME_APPTYPES = array('authentication'); OC_App::loadApps($RUNTIME_APPTYPES); // Backends @@ -53,7 +53,7 @@ $nodes = array( $principalCollection, $addressBookRoot, - ); +); // Fire up server $server = new Sabre_DAV_Server($nodes); @@ -67,8 +67,9 @@ $server->addPlugin(new Sabre_CardDAV_VCFExportPlugin()); $server->addPlugin(new OC_Connector_Sabre_ExceptionLoggerPlugin('carddav')); -if(defined('DEBUG') && DEBUG) { +if (defined('DEBUG') && DEBUG) { $server->debugExceptions = true; } + // And off we go! $server->exec(); diff --git a/appinfo/update.php b/appinfo/update.php index b10d92168..73ed70345 100644 --- a/appinfo/update.php +++ b/appinfo/update.php @@ -7,13 +7,12 @@ * See the COPYING-README file. */ -$installedVersion=OCP\Config::getAppValue('contacts', 'installed_version'); +$installedVersion = OCP\Config::getAppValue('contacts', 'installed_version'); if (version_compare($installedVersion, '0.2.5', '>=')) { // Set all address books active as (de)activating went awol at rewrite. $stmt = OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_addressbooks` SET `active`= 1' ); $result = $stmt->execute(array()); -} -elseif (version_compare($installedVersion, '0.2.4', '==')) { +} elseif (version_compare($installedVersion, '0.2.4', '==')) { // First set all address books in-active. $stmt = OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_addressbooks` SET `active`=0' ); $result = $stmt->execute(array()); @@ -24,9 +23,9 @@ // Prepare statement for updating the new 'active' field. $stmt = OCP\DB::prepare( 'UPDATE `*PREFIX*contacts_addressbooks` SET `active`=? WHERE `id`=? AND `userid`=?' ); - while( $row = $result->fetchRow()) { + while ($row = $result->fetchRow()) { $ids = explode(';', $row['configvalue']); - foreach($ids as $id) { + foreach ($ids as $id) { $r = $stmt->execute(array(1, $id, $row['userid'])); } } diff --git a/carddav.php b/carddav.php index 264eb3083..b592a9de7 100644 --- a/carddav.php +++ b/carddav.php @@ -1,6 +1,7 @@ "); - textarea.after("
"); - - var container = textarea.parent().css(containerCSS); - var pre = container.find("pre").css(preCSS); - - // Store the original styles in case of destroying. - textarea.data('expanding-styles', textarea.attr('style')); - textarea.css(textareaCSS); - - $.each(cloneCSSProperties, function(i, p) { - var val = textarea.css(p); - - // Only set if different to prevent overriding percentage css values. - if (pre.css(p) !== val) { - pre.css(p, val); - } - }); - - textarea.bind("input.expanding propertychange.expanding", resize); - resize.apply(this); - - if (opts.resize) { - textarea.bind("resize.expanding", opts.resize); - } - }); - - return this; - }; - - $(function () { - if ($.expandingTextarea.autoInitialize) { - $($.expandingTextarea.initialSelector).expandingTextarea(); - } - }); - -})); diff --git a/js/groups.js b/js/groups.js index 0f3a2cf47..029f86d2a 100644 --- a/js/groups.js +++ b/js/groups.js @@ -20,7 +20,7 @@ OC.Contacts = OC.Contacts || {}; this.storage = storage; this.$groupList = groupList; var self = this; - var numtypes = ['category', 'fav', 'all']; + this.$groupList.on('click', 'li.group', function(event) { $('.tipsy').remove(); if(wrongKey(event)) { @@ -391,7 +391,7 @@ OC.Contacts = OC.Contacts || {}; cb({ids:ids}); } } else { - if(typeof cb == 'function') { + if(typeof cb === 'function') { cb({error:true, message:response.message}); } } @@ -411,7 +411,7 @@ OC.Contacts = OC.Contacts || {}; GroupList.prototype.removeFromAll = function(contactid, alsoSpecial, onlyInternal) { var self = this; var selector = alsoSpecial ? 'li' : 'li[data-type="category"]'; - $.each(this.$groupList.find(selector), function(i, group) { + $.each(this.$groupList.find(selector), function() { self.removeFrom(contactid, $(this).data('id'), onlyInternal); }); }; @@ -422,7 +422,7 @@ OC.Contacts = OC.Contacts || {}; * dialog, and will probably not be used in this app. */ GroupList.prototype.categoriesChanged = function(newcategories) { - console.log('GroupList.categoriesChanged, I should do something'); + console.log('GroupList.categoriesChanged, I should do something with them:', newcategories); }; /** diff --git a/lib/abstractpimcollection.php b/lib/abstractpimcollection.php index 520394216..eb5feaec8 100644 --- a/lib/abstractpimcollection.php +++ b/lib/abstractpimcollection.php @@ -23,9 +23,8 @@ namespace OCA\Contacts; /** - * Subclass this for PIM collections + * Subclass this for PIM collections. */ - abstract class AbstractPIMCollection extends AbstractPIMObject implements \Iterator, \Countable, \ArrayAccess { // Iterator properties @@ -34,14 +33,6 @@ abstract class AbstractPIMCollection extends AbstractPIMObject implements \Itera protected $counter = 0; - /** - * This is a collection so return null. - * @return null - */ - public function getParent() { - null; - } - /** * Returns a specific child node, referenced by its id * TODO: Maybe implement it here? diff --git a/lib/abstractpimobject.php b/lib/abstractpimobject.php index e44f81451..7d034c81c 100644 --- a/lib/abstractpimobject.php +++ b/lib/abstractpimobject.php @@ -23,92 +23,17 @@ namespace OCA\Contacts; /** - * Subclass this class or implement IPIMObject interface for PIM objects + * Subclass this class or implement IPIMObject interface for PIM objects. */ abstract class AbstractPIMObject implements IPIMObject { - /** - * This variable holds the ID of this object. - * Depending on the backend, this can be either a string - * or an integer, so we treat them all as strings. - * - * @var string - */ - protected $id; - - /** - * This variable holds the owner of this object. - * - * @var string - */ - protected $owner; - - /** - * This variable holds the parent of this object if any. - * - * @var string|null - */ - protected $parent; - - /** - * This variable holds the permissions of this object. - * - * @var integer - */ - protected $permissions; - - /** - * @return string - */ - public function getId() { - return $this->id; - } - - /** - * @return string|null - */ - function getDisplayName() { - return $this->displayName; - } - - /** - * @return string|null - */ - public function getOwner() { - return $this->owner; - } - /** * If this object is part of a collection return a reference * to the parent object, otherwise return null. * @return IPIMObject|null */ public function getParent() { - return $this->parent; - } - - /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask of - * - * \OCP\PERMISSION_CREATE - * \OCP\PERMISSION_READ - * \OCP\PERMISSION_UPDATE - * \OCP\PERMISSION_DELETE - * \OCP\PERMISSION_SHARE - * or - * \OCP\PERMISSION_ALL - * - * @return integer - */ - public function getPermissions() { - return $this->permissions; - } - - /** - * @return AbstractBackend - */ - public function getBackend() { - return $this->backend; } /** diff --git a/lib/addressbook.php b/lib/addressbook.php index 0ced2f081..50f5dbf5f 100644 --- a/lib/addressbook.php +++ b/lib/addressbook.php @@ -36,6 +36,7 @@ class Addressbook extends AbstractPIMCollection { public static $l10n; protected $_count; + /** * @var Backend\AbstractBackend */ @@ -69,11 +70,17 @@ public function __construct(Backend\AbstractBackend $backend, array $addressBook throw new \Exception('Error creating address book.', 500); } $this->addressBookInfo = $this->backend->getAddressBook($id); - //print(__METHOD__. ' '. __LINE__ . ' addressBookInfo: ' . print_r($this->backend->getAddressBook($id), true)); } //\OCP\Util::writeLog('contacts', __METHOD__.' backend: ' . print_r($this->backend, true), \OCP\Util::DEBUG); } + /** + * @return AbstractBackend + */ + public function getBackend() { + return $this->backend; + } + /** * @return string|null */ @@ -347,18 +354,6 @@ public function update(array $data) { return $this->backend->updateAddressBook($this->getId(), $data); } - /** - * Save the address book data to backend - * NOTE: @see IPIMObject::update for consistency considerations. - * - * @return bool - */ - public function save() { - if(!$this->hasPermission(\OCP\PERMISSION_UPDATE)) { - throw new Exception(self::$l10n->t('You don\'t have permissions to update the address book.'), 403); - } - } - /** * Delete the address book from backend * diff --git a/lib/backend/abstractbackend.php b/lib/backend/abstractbackend.php index 242f1ffe5..b0e37645d 100644 --- a/lib/backend/abstractbackend.php +++ b/lib/backend/abstractbackend.php @@ -90,13 +90,16 @@ public function __construct($userid = null) { */ protected function getContactPermissions() { $permissions = 0; - foreach($this->possibleContactPermissions AS $permission => $methodName) { + + foreach ($this->possibleContactPermissions as $permission => $methodName) { + if(method_exists($this, $methodName)) { $permissions |= $permission; } + } - \OCP\Util::writeLog('contacts', __METHOD__.', permissions' . $permissions, \OCP\Util::DEBUG); + //\OCP\Util::writeLog('contacts', __METHOD__.', permissions' . $permissions, \OCP\Util::DEBUG); return $permissions; } @@ -108,14 +111,18 @@ protected function getContactPermissions() { * compared with \OCP\PERMISSION_CREATE etc. */ protected function getAddressBookPermissions() { + $permissions = 0; - foreach($this->possibleAddressBookPermissions AS $permission => $methodName) { - if(method_exists($this, $methodName)) { + + foreach ($this->possibleAddressBookPermissions as $permission => $methodName) { + + if (method_exists($this, $methodName)) { $permissions |= $permission; } + } - \OCP\Util::writeLog('contacts', __METHOD__.', permissions' . $permissions, \OCP\Util::DEBUG); + //\OCP\Util::writeLog('contacts', __METHOD__.', permissions' . $permissions, \OCP\Util::DEBUG); return $permissions; } @@ -128,7 +135,9 @@ protected function getAddressBookPermissions() { * compared with \OCP\PERMISSION_CREATE etc. */ public function hasContactMethodFor($permission) { + return (bool)($this->getContactPermissions() & $permission); + } /** @@ -140,7 +149,9 @@ public function hasContactMethodFor($permission) { * compared with \OCP\PERMISSION_CREATE etc. */ public function hasAddressBookMethodFor($permission) { + return (bool)($this->getAddressBookPermissions() & $permission); + } /** @@ -152,7 +163,9 @@ public function hasAddressBookMethodFor($permission) { * @return bool */ public function hasAddressBook($addressbookid) { + return count($this->getAddressBook($addressbookid)) > 0; + } /** @@ -165,7 +178,9 @@ public function hasAddressBook($addressbookid) { * @return integer|null */ public function numContacts($addressbookid) { + return count($this->getContacts($addressbookid)); + } /** @@ -365,15 +380,20 @@ public function lastModifiedContact($addressbookid, $id) { */ protected function combinedKey($addressBookId = null, $contactId = null) { $key = $this->name; - if(!is_null($addressBookId)) { + if (!is_null($addressBookId)) { + $key .= '_' . substr(md5($addressBookId), 0, 8); - if(!is_null($contactId)) { + + if (!is_null($contactId)) { $key .= '_' . substr(md5($contactId), 0, 8); } - } else if(!is_null($contactId)) { + + } else if (!is_null($contactId)) { + throw new \BadMethodCallException( __METHOD__ . ' cannot be called with a contact ID but no address book ID' ); + } return $key; } @@ -384,6 +404,7 @@ protected function combinedKey($addressBookId = null, $contactId = null) { * @return boolean */ public function isActive($addressBookId = null) { + $key = $this->combinedKey($addressBookId); $key = 'active_' . $key; @@ -397,6 +418,7 @@ public function isActive($addressBookId = null) { * @return boolean */ public function setActive($active, $addressBookId = null) { + $key = $this->combinedKey($addressBookId); $key = 'active_' . $key; @@ -410,6 +432,7 @@ public function setActive($active, $addressBookId = null) { * @return array Format array('param1' => 'value', 'param2' => 'value') */ public function getPreferences($addressBookId) { + $key = $this->combinedKey($addressBookId); $key = 'prefs_' . $key; @@ -424,6 +447,7 @@ public function getPreferences($addressBookId) { * @return boolean */ public function setPreferences($addressbookid, array $params) { + $key = $this->combinedKey($addressBookId); $key = 'prefs_' . $key; diff --git a/lib/backend/database.php b/lib/backend/database.php index 29afaed29..cbcfeaa4a 100644 --- a/lib/backend/database.php +++ b/lib/backend/database.php @@ -66,51 +66,60 @@ public function __construct( public function getAddressBooksForUser(array $options = array()) { try { - if(!isset(self::$preparedQueries['addressbooksforuser'])) { + if (!isset(self::$preparedQueries['addressbooksforuser'])) { $sql = 'SELECT `id`, `displayname`, `description`, `ctag` AS `lastmodified`, `userid` AS `owner`, `uri` FROM `' . $this->addressBooksTableName . '` WHERE `userid` = ? ORDER BY `displayname`'; self::$preparedQueries['addressbooksforuser'] = \OCP\DB::prepare($sql); } + $result = self::$preparedQueries['addressbooksforuser']->execute(array($this->userid)); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return $this->addressbooks; } + } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); return $this->addressbooks; } - while($row = $result->fetchRow()) { + while ($row = $result->fetchRow()) { $row['permissions'] = \OCP\PERMISSION_ALL; $this->addressbooks[$row['id']] = $row; } + return $this->addressbooks; } public function getAddressBook($addressbookid, array $options = array()) { //\OCP\Util::writeLog('contacts', __METHOD__.' id: ' // . $addressbookid, \OCP\Util::DEBUG); - if($this->addressbooks && isset($this->addressbooks[$addressbookid])) { + if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) { //print(__METHOD__ . ' ' . __LINE__ .' addressBookInfo: ' . print_r($this->addressbooks[$addressbookid], true)); return $this->addressbooks[$addressbookid]; } + // Hmm, not found. Lets query the db. try { $query = 'SELECT `id`, `displayname`, `description`, `userid` AS `owner`, `ctag` AS `lastmodified`, `uri` FROM `' . $this->addressBooksTableName . '` WHERE `id` = ?'; - if(!isset(self::$preparedQueries['getaddressbook'])) { + if (!isset(self::$preparedQueries['getaddressbook'])) { self::$preparedQueries['getaddressbook'] = \OCP\DB::prepare($query); } + $result = self::$preparedQueries['getaddressbook']->execute(array($addressbookid)); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return null; } + $row = $result->fetchRow(); + if (!$row) { throw new \Exception('Address Book not found', 404); } @@ -118,19 +127,23 @@ public function getAddressBook($addressbookid, array $options = array()) { $row['backend'] = $this->name; $this->addressbooks[$addressbookid] = $row; return $row; + } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.' exception: ' . $e->getMessage(), \OCP\Util::ERROR); return null; } + return null; } public function hasAddressBook($addressbookid, array $options = array()) { + // First check if it's already cached - if($this->addressbooks && isset($this->addressbooks[$addressbookid])) { + if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) { return true; } + return count($this->getAddressBook($addressbookid)) > 0; } @@ -142,7 +155,8 @@ public function hasAddressBook($addressbookid, array $options = array()) { * @return bool */ public function updateAddressBook($addressbookid, array $changes, array $options = array()) { - if(count($changes) === 0) { + + if (count($changes) === 0) { return false; } @@ -150,20 +164,26 @@ public function updateAddressBook($addressbookid, array $changes, array $options $updates = array(); - if(isset($changes['displayname'])) { + if (isset($changes['displayname'])) { + $query .= '`displayname` = ?, '; $updates[] = $changes['displayname']; - if($this->addressbooks && isset($this->addressbooks[$addressbookid])) { + + if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) { $this->addressbooks[$addressbookid]['displayname'] = $changes['displayname']; } + } - if(isset($changes['description'])) { + if (isset($changes['description'])) { + $query .= '`description` = ?, '; $updates[] = $changes['description']; - if($this->addressbooks && isset($this->addressbooks[$addressbookid])) { + + if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) { $this->addressbooks[$addressbookid]['description'] = $changes['description']; } + } $query .= '`ctag` = ? + 1 WHERE `id` = ?'; @@ -171,14 +191,17 @@ public function updateAddressBook($addressbookid, array $changes, array $options $updates[] = $addressbookid; try { + $stmt = \OCP\DB::prepare($query); $result = $stmt->execute($updates); + 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: ' @@ -203,7 +226,7 @@ public function updateAddressBook($addressbookid, array $changes, array $options */ public function createAddressBook(array $properties, array $options = array()) { - if(count($properties) === 0 || !isset($properties['displayname'])) { + if (count($properties) === 0 || !isset($properties['displayname'])) { return false; } @@ -219,27 +242,32 @@ public function createAddressBook(array $properties, array $options = array()) { $updates[] = $ctag; try { - if(!isset(self::$preparedQueries['createaddressbook'])) { + if (!isset(self::$preparedQueries['createaddressbook'])) { self::$preparedQueries['createaddressbook'] = \OCP\DB::prepare($query); } + $result = self::$preparedQueries['createaddressbook']->execute($updates); + 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; } $newid = \OCP\DB::insertid($this->addressBooksTableName); - if($this->addressbooks) { + + if ($this->addressbooks) { $updates['id'] = $newid; $updates['ctag'] = $ctag; $updates['lastmodified'] = $ctag; $updates['permissions'] = \OCP\PERMISSION_ALL; $this->addressbooks[$newid] = $updates; } + return $newid; } @@ -260,23 +288,29 @@ public function deleteAddressBook($addressbookid, array $options = array()) { $result = null; $stmt = \OCP\DB::prepare('SELECT `id` FROM `' . $this->cardsTableName . '`' . ' WHERE `addressbookid` = ?'); + try { + $result = $stmt->execute(array($addressbookid)); + 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; } - if(!is_null($result)) { - while($id = $result->fetchOne()) { + if (!is_null($result)) { + + while ($id = $result->fetchOne()) { $ids[] = $id; } + } \OCP\Util::emitHook('OCA\Contacts', 'pre_deleteAddressBook', @@ -284,11 +318,12 @@ public function deleteAddressBook($addressbookid, array $options = array()) { ); // Delete contacts in address book. - if(!isset(self::$preparedQueries['deleteaddressbookcontacts'])) { + if (!isset(self::$preparedQueries['deleteaddressbookcontacts'])) { self::$preparedQueries['deleteaddressbookcontacts'] = \OCP\DB::prepare('DELETE FROM `' . $this->cardsTableName . '` WHERE `addressbookid` = ?'); } + try { self::$preparedQueries['deleteaddressbookcontacts'] ->execute(array($addressbookid)); @@ -299,11 +334,12 @@ public function deleteAddressBook($addressbookid, array $options = array()) { } // Delete the address book. - if(!isset(self::$preparedQueries['deleteaddressbook'])) { + if (!isset(self::$preparedQueries['deleteaddressbook'])) { self::$preparedQueries['deleteaddressbook'] = \OCP\DB::prepare('DELETE FROM `' . $this->addressBooksTableName . '` WHERE `id` = ?'); } + try { self::$preparedQueries['deleteaddressbook'] ->execute(array($addressbookid)); @@ -313,7 +349,7 @@ public function deleteAddressBook($addressbookid, array $options = array()) { return false; } - if($this->addressbooks && isset($this->addressbooks[$addressbookid])) { + if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) { unset($this->addressbooks[$addressbookid]); } @@ -328,9 +364,10 @@ public function deleteAddressBook($addressbookid, array $options = array()) { public function setModifiedAddressBook($id) { $query = 'UPDATE `' . $this->addressBooksTableName . '` SET `ctag` = ? + 1 WHERE `id` = ?'; - if(!isset(self::$preparedQueries['touchaddressbook'])) { + if (!isset(self::$preparedQueries['touchaddressbook'])) { self::$preparedQueries['touchaddressbook'] = \OCP\DB::prepare($query); } + $ctag = time(); self::$preparedQueries['touchaddressbook']->execute(array($ctag, $id)); @@ -338,9 +375,11 @@ public function setModifiedAddressBook($id) { } public function lastModifiedAddressBook($addressbookid) { - if($this->addressbooks && isset($this->addressbooks[$addressbookid])) { + + if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) { return $this->addressbooks[$addressbookid]['lastmodified']; } + $addressBook = $this->getAddressBook($addressbookid); return $addressBook ? $addressBook['lastmodified'] : null; } @@ -353,17 +392,21 @@ public function lastModifiedAddressBook($addressbookid) { * @return array */ public function numContacts($addressbookid) { + $query = 'SELECT COUNT(*) AS `count` FROM `' . $this->cardsTableName . '` WHERE ' . '`addressbookid` = ?'; - if(!isset(self::$preparedQueries['count'])) { + if (!isset(self::$preparedQueries['count'])) { self::$preparedQueries['count'] = \OCP\DB::prepare($query); } + $result = self::$preparedQueries['count']->execute(array($addressbookid)); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return null; } + return (int)$result->fetchOne(); } @@ -388,21 +431,26 @@ public function getContacts($addressbookid, array $options = array() ) { isset($options['limit']) ? $options['limit'] : null, isset($options['offset']) ? $options['offset'] : null ); + $result = $stmt->execute(array($addressbookid)); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return $cards; } + } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); return $cards; } - if(!is_null($result)) { - while($row = $result->fetchRow()) { + if (!is_null($result)) { + + while ($row = $result->fetchRow()) { $row['permissions'] = \OCP\PERMISSION_ALL; $cards[] = $row; } + } return $cards; @@ -431,11 +479,11 @@ public function getContact($addressbookid, $id, array $options = array()) { $noCollection = isset($options['noCollection']) ? $options['noCollection'] : false; $where_query = '`id` = ?'; - if(is_array($id)) { + if (is_array($id)) { $where_query = ''; - if(isset($id['id'])) { + if (isset($id['id'])) { $id = $id['id']; - } elseif(isset($id['uri'])) { + } elseif (isset($id['uri'])) { $where_query = '`uri` = ?'; $id = $id['uri']; } else { @@ -447,7 +495,7 @@ public function getContact($addressbookid, $id, array $options = array()) { } $ids = array($id); - if(!$noCollection) { + if (!$noCollection) { $where_query .= ' AND `addressbookid` = ?'; $ids[] = $addressbookid; } @@ -457,10 +505,12 @@ public function getContact($addressbookid, $id, array $options = array()) { . $this->cardsTableName . '` WHERE ' . $where_query; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute($ids); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return null; } + } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__.', exception: '.$e->getMessage(), \OCP\Util::ERROR); \OCP\Util::writeLog('contacts', __METHOD__.', id: '. $id, \OCP\Util::DEBUG); @@ -468,10 +518,12 @@ public function getContact($addressbookid, $id, array $options = array()) { } $row = $result->fetchRow(); + if (!$row) { \OCP\Util::writeLog('contacts', __METHOD__.', Not found, id: '. $id, \OCP\Util::DEBUG); return null; } + $row['permissions'] = \OCP\PERMISSION_ALL; return $row; } @@ -500,13 +552,15 @@ public function createContact($addressbookid, $contact, array $options = array() $qname = 'createcontact'; $uri = isset($options['uri']) ? $options['uri'] : null; - if(!$contact instanceof VCard) { + 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 { @@ -526,11 +580,13 @@ public function createContact($addressbookid, $contact, array $options = array() $prodid = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appversion.'//EN'; $contact->PRODID = $prodid; - if(!isset(self::$preparedQueries[$qname])) { + if (!isset(self::$preparedQueries[$qname])) { + self::$preparedQueries[$qname] = \OCP\DB::prepare('INSERT INTO `' . $this->cardsTableName . '` (`addressbookid`,`fullname`,`carddata`,`uri`,`lastmodified`) VALUES(?,?,?,?,?)' ); } + try { $result = self::$preparedQueries[$qname] ->execute( @@ -542,10 +598,12 @@ public function createContact($addressbookid, $contact, array $options = array() time() ) ); + 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; @@ -576,7 +634,8 @@ public function updateContact($addressbookid, $id, $contact, array $options = ar $updateRevision = true; $isCardDAV = false; - if(!$contact instanceof VCard) { + + if (!$contact instanceof VCard) { try { $contact = Reader::read($contact); } catch(\Exception $e) { @@ -585,18 +644,23 @@ public function updateContact($addressbookid, $id, $contact, array $options = ar } } - if(is_array($id)) { + if (is_array($id)) { + $where_query = ''; - if(isset($id['id'])) { + + if (isset($id['id'])) { $id = $id['id']; - } elseif(isset($id['uri'])) { + } elseif (isset($id['uri'])) { + $updateRevision = false; $isCardDAV = true; $id = $this->getIdFromUri($id['uri']); - if(is_null($id)) { + + 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.' @@ -604,14 +668,14 @@ public function updateContact($addressbookid, $id, $contact, array $options = ar } } - if($updateRevision || !isset($contact->REV)) { + if ($updateRevision || !isset($contact->REV)) { $now = new \DateTime; $contact->REV = $now->format(\DateTime::W3C); } $data = $contact->serialize(); - if($noCollection) { + if ($noCollection) { $me = $this->getContact(null, $id, $options); $addressbookid = $me['parent']; } @@ -620,15 +684,20 @@ public function updateContact($addressbookid, $id, $contact, array $options = ar $query = 'UPDATE `' . $this->cardsTableName . '` SET `fullname` = ?,`carddata` = ?, `lastmodified` = ? WHERE `id` = ? AND `addressbookid` = ?'; - if(!isset(self::$preparedQueries[$qname])) { + + if (!isset(self::$preparedQueries[$qname])) { self::$preparedQueries[$qname] = \OCP\DB::prepare($query); } + try { + $result = self::$preparedQueries[$qname]->execute($updates); + 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); @@ -637,7 +706,9 @@ public function updateContact($addressbookid, $id, $contact, array $options = ar } $this->setModifiedAddressBook($addressbookid); - if(!$isBatch) { + + if (!$isBatch) { + \OCP\Util::emitHook('OCA\Contacts', 'post_updateContact', array( 'backend' => $this->name, @@ -647,7 +718,9 @@ public function updateContact($addressbookid, $id, $contact, array $options = ar 'carddav' => $isCardDAV ) ); + } + return true; } @@ -667,15 +740,19 @@ public function deleteContact($addressbookid, $id, array $options = array()) { $noCollection = isset($options['noCollection']) ? $options['noCollection'] : false; $isBatch = isset($options['isBatch']) ? $options['isBatch'] : false; - if(is_array($id)) { - if(isset($id['id'])) { + if (is_array($id)) { + + if (isset($id['id'])) { $id = $id['id']; - } elseif(isset($id['uri'])) { + } elseif (isset($id['uri'])) { + $id = $this->getIdFromUri($id['uri']); - if(is_null($id)) { + + 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.' @@ -683,30 +760,34 @@ public function deleteContact($addressbookid, $id, array $options = array()) { } } - if(!$isBatch) { + if (!$isBatch) { \OCP\Util::emitHook('OCA\Contacts', 'pre_deleteContact', array('id' => $id) ); } - if($noCollection) { + if ($noCollection) { $me = $this->getContact(null, $id, $options); $addressbookid = $me['parent']; } - if(!isset(self::$preparedQueries[$qname])) { + if (!isset(self::$preparedQueries[$qname])) { self::$preparedQueries[$qname] = \OCP\DB::prepare('DELETE FROM `' . $this->cardsTableName . '` WHERE `id` = ? AND `addressbookid` = ?'); } + \OCP\Util::writeLog('contacts', __METHOD__ . ' updates: ' . $id . '/' . $addressbookid, \OCP\Util::DEBUG); try { + $result = self::$preparedQueries[$qname]->execute(array($id, $addressbookid)); + 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); @@ -714,6 +795,7 @@ public function deleteContact($addressbookid, $id, array $options = array()) { . $id, \OCP\Util::DEBUG); return false; } + $this->setModifiedAddressBook($addressbookid); return true; } @@ -729,8 +811,10 @@ public function deleteContact($addressbookid, $id, array $options = array()) { * @returns int | null */ public function lastModifiedContact($addressbookid, $id) { + $contact = $this->getContact($addressbookid, $id); return ($contact ? $contact['lastmodified'] : null); + } /** @@ -740,14 +824,18 @@ public function lastModifiedContact($addressbookid, $id) { * @returns int | null */ public function getIdFromUri($uri) { + $query = 'SELECT `id` FROM `'. $this->cardsTableName . '` WHERE `uri` = ?'; $stmt = \OCP\DB::prepare($query); $result = $stmt->execute(array($uri)); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return null; } + $one = $result->fetchOne(); + if (!$one) { \OCP\Util::writeLog('contacts', __METHOD__.', Not found, uri: '. $uri, \OCP\Util::DEBUG); return null; @@ -757,27 +845,32 @@ public function getIdFromUri($uri) { } private function createAddressBookURI($displayname, $userid = null) { + $userid = $userid ? $userid : \OCP\User::getUser(); $name = str_replace(' ', '_', strtolower($displayname)); + try { $stmt = \OCP\DB::prepare('SELECT `uri` FROM `' . $this->addressBooksTableName . '` WHERE `userid` = ? '); $result = $stmt->execute(array($userid)); + if (\OCP\DB::isError($result)) { \OCP\Util::writeLog('contacts', __METHOD__. 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR); return $name; } + } catch(\Exception $e) { \OCP\Util::writeLog('contacts', __METHOD__ . ' exception: ' . $e->getMessage(), \OCP\Util::ERROR); return $name; } $uris = array(); - while($row = $result->fetchRow()) { + while ($row = $result->fetchRow()) { $uris[] = $row['uri']; } $newname = $name; - $i = 1; - while(in_array($newname, $uris)) { + $i = 1 + ; + while (in_array($newname, $uris)) { $newname = $name.$i; $i = $i + 1; } @@ -796,15 +889,18 @@ protected function uniqueURI($addressBookId, $uri) { $result = $stmt->execute(array($addressBookId, $uri)); $result = $result->fetchRow(); - if(is_array($result) && count($result) > 0 && $result['count'] > 0) { - while(true) { + if (is_array($result) && count($result) > 0 && $result['count'] > 0) { + + while (true) { $uri = Properties::generateUID() . '.vcf'; $result = $stmt->execute(array($addressBookId, $uri)); - if(is_array($result) && count($result) > 0 && $result['count'] > 0) { + + if (is_array($result) && count($result) > 0 && $result['count'] > 0) { continue; } else { return $uri; } + } } diff --git a/lib/backend/shared.php b/lib/backend/shared.php index 313024ae7..6f2087958 100644 --- a/lib/backend/shared.php +++ b/lib/backend/shared.php @@ -46,15 +46,19 @@ public function getAddressBooksForUser(array $options = array()) { 'addressbook', Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS ); - foreach($maybeSharedAddressBook as $sharedAddressbook) { - if(isset($sharedAddressbook['id'])) { + + foreach ($maybeSharedAddressBook as $sharedAddressbook) { + + if (isset($sharedAddressbook['id'])) { $this->addressbooks[] = $this->getAddressBook($sharedAddressbook['id']); } + } - foreach($this->addressbooks as &$addressBook) { + foreach ($this->addressbooks as &$addressBook) { $addressBook['backend'] = $this->name; } + return $this->addressbooks; } @@ -68,15 +72,19 @@ public function getAddressBooksForUser(array $options = array()) { public function getAddressBook($addressbookid, array $options = array()) { foreach ($this->addressbooks as $addressBook) { + if ($addressBook['id'] === $addressbookid) { return $addressBook; } + } + $addressBook = \OCP\Share::getItemSharedWithBySource( 'addressbook', $addressbookid, Contacts\Share\Addressbook::FORMAT_ADDRESSBOOKS ); + // Not sure if I'm doing it wrongly, or if its supposed to return // the info in an array? $addressBook = (isset($addressBook['permissions']) ? $addressBook : $addressBook[0]); @@ -94,14 +102,16 @@ public function getAddressBook($addressbookid, array $options = array()) { public function getContacts($addressbookid, array $options = array()) { $addressBook = $this->getAddressBook($addressbookid); - if(!$addressBook) { + + if (!$addressBook) { throw new \Exception('Shared Address Book not found: ' . $addressbookid, 404); } + $permissions = $addressBook['permissions']; $cards = parent::getContacts($addressbookid, $options); - foreach($cards as &$card) { + foreach ($cards as &$card) { $card['permissions'] = $permissions; } @@ -121,16 +131,21 @@ public function getContacts($addressbookid, array $options = array()) { * @return array|false */ public function getContact($addressbookid, $id, array $options = array()) { + $addressBook = $this->getAddressBook($addressbookid); - if(!$addressBook) { + + if (!$addressBook) { throw new \Exception('Shared Address Book not found: ' . $addressbookid, 404); } + $permissions = $addressBook['permissions']; $card = parent::getContact($addressbookid, $id, $options); - if(!$card) { + + if (!$card) { throw new \Exception('Shared Contact not found: ' . implode(',', $id), 404); } + $card['permissions'] = $permissions; return $card; } diff --git a/lib/controller/addressbookcontroller.php b/lib/controller/addressbookcontroller.php index af7ec662a..58a706371 100644 --- a/lib/controller/addressbookcontroller.php +++ b/lib/controller/addressbookcontroller.php @@ -28,12 +28,16 @@ public function userAddressBooks() { $addressBooks = $this->app->getAddressBooksForUser(); $result = array(); $lastModified = 0; + foreach($addressBooks as $addressBook) { + $data = $addressBook->getMetaData(); $result[] = $data; - if(!is_null($data['lastmodified'])) { + + if (!is_null($data['lastmodified'])) { $lastModified = max($lastModified, $data['lastmodified']); } + } // To avoid invalid cache deletion time is saved @@ -68,7 +72,7 @@ public function getAddressBook() { $etag = null; $response = new JSONResponse(); - if(!is_null($lastModified)) { + if (!is_null($lastModified)) { //$response->addHeader('Cache-Control', 'private, must-revalidate'); $response->setLastModified(\DateTime::createFromFormat('U', $lastModified) ?: null); $etag = md5($lastModified); @@ -76,20 +80,20 @@ public function getAddressBook() { } //$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) + if ($addressBook->hasPermission(\OCP\PERMISSION_DELETE) && $addressBook->getBackend()->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE)) { $options[] = 'DELETE'; } - if($addressBook->hasPermission(\OCP\PERMISSION_UPDATE) + if ($addressBook->hasPermission(\OCP\PERMISSION_UPDATE) && $addressBook->getBackend()->hasAddressBookMethodFor(\OCP\PERMISSION_UPDATE)) { $options[] = 'POST'; @@ -102,12 +106,14 @@ public function getAddressBook() { break; case 'GET': $contacts = array(); - foreach($addressBook->getChildren() as $i => $contact) { + + foreach ($addressBook->getChildren() as $i => $contact) { $result = JSONSerializer::serializeContact($contact); - if($result !== null) { + if ($result !== null) { $contacts[] = $result; } } + return $response->setData(array('contacts' => $contacts)); break; } @@ -123,15 +129,18 @@ public function addAddressBook() { $response = new JSONResponse(); $backend = $this->app->getBackend($params['backend']); - if(!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_CREATE)) { + + if (!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_CREATE)) { throw new \Exception('This backend does not support adding address books', 501); } + try { $id = $backend->createAddressBook($this->request->post); } catch(Exception $e) { return $response->bailOut($e->getMessage()); } - if($id === false) { + + if ($id === false) { return $response->bailOut(App::$l10n->t('Error creating address book')); } @@ -147,13 +156,8 @@ public function updateAddressBook() { $response = new JSONResponse(); $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); - try { - if(!$addressBook->update($this->request['properties'])) { - return $response->bailOut(App::$l10n->t('Error updating address book')); - } - } catch(Exception $e) { - return $response->bailOut($e->getMessage()); - } + $addressBook->update($this->request['properties']); + return $response->setParams($addressBook->getMetaData()); } @@ -167,24 +171,27 @@ public function deleteAddressBook() { $backend = $this->app->getBackend($params['backend']); - if(!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE)) { - throw new \Exception( + if (!$backend->hasAddressBookMethodFor(\OCP\PERMISSION_DELETE)) { + throw new \Exception(App::$l10n->t( 'The "%s" backend does not support deleting address books', array($backend->name) - ); + ), 501); } $addressBookInfo = $backend->getAddressBook($params['addressBookId']); - if(!$addressBookInfo['permissions'] & \OCP\PERMISSION_DELETE) { - return $response->bailOut(App::$l10n->t( - 'You do not have permissions to delete the "%s" address book'), - array($addressBookInfo['displayname'] - )); + if (!$addressBookInfo['permissions'] & \OCP\PERMISSION_DELETE) { + throw new \Exception(App::$l10n->t( + 'You do not have permissions to delete the "%s" address book', + array($addressBookInfo['displayname']) + ), 403); } - if(!$backend->deleteAddressBook($params['addressBookId'])) { - return $response->bailOut(App::$l10n->t('Error deleting address book')); + if (!$backend->deleteAddressBook($params['addressBookId'])) { + throw new \Exception(App::$l10n->t( + 'Error deleting address book' + ), 500); } + \OCP\Config::setUserValue($this->api->getUserId(), 'contacts', 'last_address_book_deleted', time()); return $response; } @@ -220,7 +227,7 @@ public function addChild() { return $response->bailOut($e->getMessage()); } - if($id === false) { + if ($id === false) { return $response->bailOut(App::$l10n->t('Error creating contact.')); } @@ -256,9 +263,10 @@ public function deleteChild() { return $response->bailOut($e->getMessage()); } - if($result === false) { + if ($result === false) { return $response->bailOut(App::$l10n->t('Error deleting contact.')); } + return $response->setStatus('204'); } @@ -296,23 +304,29 @@ public function moveChild() { $fromAddressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $targetAddressBook = $this->app->getAddressBook($targetInfo['backend'], $targetInfo['id']); $contact = $fromAddressBook->getChild($params['contactId']); - if(!$contact) { + + if (!$contact) { $response->bailOut(App::$l10n->t('Error retrieving contact.')); return $response; } + try { $contactId = $targetAddressBook->addChild($contact); } catch(Exception $e) { return $response->bailOut($e->getMessage()); } + $contact = $targetAddressBook->getChild($contactId); - if(!$contact) { + + if (!$contact) { return $response->bailOut(App::$l10n->t('Error saving contact.')); } - if(!$fromAddressBook->deleteChild($params['contactId'])) { + + if (!$fromAddressBook->deleteChild($params['contactId'])) { // Don't bail out because we have to return the contact return $response->debug(App::$l10n->t('Error removing contact from other address book.')); } + return $response->setParams(JSONSerializer::serializeContact($contact)); } diff --git a/lib/controller/contactcontroller.php b/lib/controller/contactcontroller.php index a19cb8bb4..527b94baa 100644 --- a/lib/controller/contactcontroller.php +++ b/lib/controller/contactcontroller.php @@ -33,7 +33,7 @@ public function getContact() { $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); - if(!$contact) { + if (!$contact) { return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); } @@ -56,18 +56,19 @@ public function saveContact() { $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); - if(!$data) { + if (!$data) { return $response->bailOut(App::$l10n->t('No contact data in request.')); } - if(!$contact) { + if (!$contact) { return $response->bailOut(App::$l10n->t('Couldn\'t find contact.')); } - if(!$contact->mergeFromArray($data)) { + if (!$contact->mergeFromArray($data)) { return $response->bailOut(App::$l10n->t('Error merging into contact.')); } - if(!$contact->save()) { + + if (!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact to backend.')); } @@ -91,28 +92,33 @@ public function patch() { $addressBook = $this->app->getAddressBook($params['backend'], $params['addressBookId']); $contact = $addressBook->getChild($params['contactId']); - if(!$contact) { + if (!$contact) { return $response ->setStatus(Http::STATUS_NOT_FOUND) ->bailOut(App::$l10n->t('Couldn\'t find contact.')); } - if(!$name) { + + if (!$name) { return $response ->setStatus(Http::STATUS_PRECONDITION_FAILED) ->bailOut(App::$l10n->t('Property name is not set.')); } - if(!$checksum && in_array($name, Properties::$multi_properties)) { + + if (!$checksum && in_array($name, Properties::$multi_properties)) { return $response ->setStatus(Http::STATUS_PRECONDITION_FAILED) ->bailOut(App::$l10n->t('Property checksum is not set.')); } - if(is_array($value)) { + + if (is_array($value)) { // NOTE: Important, otherwise the compound value will be // set in the order the fields appear in the form! ksort($value); } + $result = array('contactId' => $params['contactId']); - if($checksum && in_array($name, Properties::$multi_properties)) { + + if ($checksum && in_array($name, Properties::$multi_properties)) { try { if(is_null($value)) { $contact->unsetPropertyByChecksum($checksum); @@ -125,20 +131,25 @@ public function patch() { ->setStatus(Http::STATUS_PRECONDITION_FAILED) ->bailOut(App::$l10n->t('Information about vCard is incorrect. Please reload the page.')); } - } elseif(!in_array($name, Properties::$multi_properties)) { - if(is_null($value)) { + } elseif (!in_array($name, Properties::$multi_properties)) { + + if (is_null($value)) { unset($contact->{$name}); } else { - if(!$contact->setPropertyByName($name, $value, $parameters)) { + + if (!$contact->setPropertyByName($name, $value, $parameters)) { return $response ->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR) ->bailOut(App::$l10n->t('Error updating contact')); } + } } - if(!$contact->save()) { + + if (!$contact->save()) { return $response->bailOut(App::$l10n->t('Error saving contact to backend')); } + $result['lastmodified'] = $contact->lastModified(); return $response->setData($result); diff --git a/lib/controller/groupcontroller.php b/lib/controller/groupcontroller.php index 84d79550f..3bb6eb940 100644 --- a/lib/controller/groupcontroller.php +++ b/lib/controller/groupcontroller.php @@ -25,7 +25,8 @@ class GroupController extends Controller { public function getGroups() { $tagMgr = $this->server->getTagManager()->load('contact'); $tags = $tagMgr->getTags(); - foreach($tags as &$tag) { + + foreach ($tags as &$tag) { try { $ids = $tagMgr->getIdsForTag($tag['name']); $tag['contacts'] = $ids; @@ -54,18 +55,20 @@ public function addGroup() { $name = $this->request->post['name']; $response = new JSONResponse(); - if(is_null($name) || $name === "") { + + if (is_null($name) || $name === "") { $response->bailOut(App::$l10n->t('No group name given.')); } $tagMgr = $this->server->getTagManager()->load('contact'); $id = $tagMgr->add($name); - if($id === false) { + if ($id === false) { $response->bailOut(App::$l10n->t('Error adding group.')); } else { $response->setParams(array('id'=>$id, 'name' => $name)); } + return $response; } @@ -76,12 +79,13 @@ public function deleteGroup() { $name = $this->request->post['name']; $response = new JSONResponse(); - if(is_null($name) || $name === '') { + if (is_null($name) || $name === '') { $response->bailOut(App::$l10n->t('No group name given.')); return $response; } $tagMgr = $this->server->getTagManager()->load('contact'); + try { $ids = $tagMgr->getIdsForTag($name); } catch(\Exception $e) { @@ -89,32 +93,42 @@ public function deleteGroup() { \OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR); return $response; } - if($ids !== false) { + + if ($ids !== false) { + $backend = $this->app->getBackend('local'); - foreach($ids as $id) { + + foreach ($ids as $id) { $contact = $backend->getContact(null, $id, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read( $contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES ); - if($obj) { - if(!$obj->inGroup($name)) { + + if ($obj) { + + if (!$obj->inGroup($name)) { continue; } - if($obj->removeFromGroup($name)) { + + if ($obj->removeFromGroup($name)) { $backend->updateContact(null, $id, $obj, array('noCollection' => true, 'isBatch' => true)); } + } else { \OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG); } } + } + try { $tagMgr->delete($name); } catch(\Exception $e) { $response->setErrorMessage($e->getMessage()); \OCP\Util::writeLog('contacts', __METHOD__.', ' . $e->getMessage(), \OCP\Util::ERROR); } + return $response; } @@ -126,40 +140,54 @@ public function renameGroup() { $to = $this->request->post['to']; $response = new JSONResponse(); - if(is_null($from) || $from === '') { + + if (is_null($from) || $from === '') { $response->bailOut(App::$l10n->t('No group name to rename from given.')); return $response; } - if(is_null($to) || $to === '') { + + if (is_null($to) || $to === '') { $response->bailOut(App::$l10n->t('No group name to rename to given.')); return $response; } $tagMgr = $this->server->getTagManager()->load('contact'); - if(!$tagMgr->rename($from, $to)) { + + if (!$tagMgr->rename($from, $to)) { $response->bailOut(App::$l10n->t('Error renaming group.')); return $response; } + $ids = $tagMgr->getIdsForTag($to); - if($ids !== false) { + + if ($ids !== false) { + $backend = $this->app->getBackend('local'); - foreach($ids as $id) { + + foreach ($ids as $id) { $contact = $backend->getContact(null, $id, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read( $contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES ); - if($obj) { - if(!isset($obj->CATEGORIES)) { + + if ($obj) { + + if (!isset($obj->CATEGORIES)) { continue; } + $obj->CATEGORIES->renameGroup($from, $to); $backend->updateContact(null, $id, $obj, array('noCollection' => true)); + } else { \OCP\Util::writeLog('contacts', __METHOD__.', could not parse card ' . $id, \OCP\Util::DEBUG); } + } + } + return $response; } @@ -174,34 +202,40 @@ public function addToGroup() { $ids = $this->request->post['contactIds']; $response->debug('request: '.print_r($this->request->post, true)); - if(is_null($categoryId) || $categoryId === '') { + if (is_null($categoryId) || $categoryId === '') { $response->bailOut(App::$l10n->t('Group ID missing from request.')); return $response; } - if(is_null($categoryId) || $categoryId === '') { + if (is_null($categoryId) || $categoryId === '') { $response->bailOut(App::$l10n->t('Group name missing from request.')); return $response; } - if(is_null($ids)) { + if (is_null($ids)) { $response->bailOut(App::$l10n->t('Contact ID missing from request.')); return $response; } $backend = $this->app->getBackend('local'); $tagMgr = $this->server->getTagManager()->load('contact'); - foreach($ids as $contactId) { + + foreach ($ids as $contactId) { + $contact = $backend->getContact(null, $contactId, array('noCollection' => true)); $obj = \Sabre\VObject\Reader::read( $contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES ); - if($obj) { - if($obj->addToGroup($categoryname)) { + + if ($obj) { + + if ($obj->addToGroup($categoryname)) { $backend->updateContact(null, $contactId, $obj, array('noCollection' => true)); } + } + $response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId); $tagMgr->tagAs($contactId, $categoryId); } @@ -220,38 +254,47 @@ public function removeFromGroup() { $ids = $this->request->post['contactIds']; //$response->debug('request: '.print_r($this->request->post, true)); - if(is_null($categoryId) || $categoryId === '') { + if (is_null($categoryId) || $categoryId === '') { $response->bailOut(App::$l10n->t('Group ID missing from request.')); return $response; } - if(is_null($ids)) { + if (is_null($ids)) { $response->bailOut(App::$l10n->t('Contact ID missing from request.')); return $response; } $backend = $this->app->getBackend('local'); $tagMgr = $this->server->getTagManager()->load('contact'); - foreach($ids as $contactId) { + + foreach ($ids as $contactId) { + $contact = $backend->getContact(null, $contactId, array('noCollection' => true)); - if(!$contact) { + + if (!$contact) { $response->debug('Couldn\'t get contact: ' . $contactId); continue; } + $obj = \Sabre\VObject\Reader::read( $contact['carddata'], \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES ); - if($obj) { - if(!isset($obj->CATEGORIES)) { + + if ($obj) { + + if (!isset($obj->CATEGORIES)) { return $response; } - if($obj->removeFromGroup($categoryname)) { + + if ($obj->removeFromGroup($categoryname)) { $backend->updateContact(null, $contactId, $obj, array('noCollection' => true)); } + } else { $response->debug('Error parsing contact: ' . $contactId); } + $response->debug('contactId: ' . $contactId . ', categoryId: ' . $categoryId); $tagMgr->unTag($contactId, $categoryId); }