1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-28 10:24:11 +01:00

Scrutinizer cleanup

This commit is contained in:
Thomas Tanghus 2014-03-08 16:22:51 +01:00
parent f1aa86204f
commit f6bfea77e7
20 changed files with 398 additions and 426 deletions

View File

@ -1,6 +1,7 @@
filter:
excluded_paths:
- 'js/modernizr.custom.js'
- 'js/jquery.onfontresize.js'
- 'l10n/*'
- 'js/placeholder*'
- 'css/placeholder*'

View File

@ -8,6 +8,7 @@
*/
namespace OCA\Contacts;
use \OC\AppFramework\Core\API;
//require_once __DIR__ . '/../lib/controller/pagecontroller.php';
@ -58,11 +59,11 @@ $api->connectHook('OC_Calendar', 'getSources', 'OCA\Contacts\Hooks', 'getCalende
\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));
}
}

View File

@ -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 @@ $addressBookRoot->disableListing = true; // Disable listing
$nodes = array(
$principalCollection,
$addressBookRoot,
);
);
// Fire up server
$server = new Sabre_DAV_Server($nodes);
@ -67,8 +67,9 @@ $server->addPlugin(new Sabre_DAV_Browser_Plugin(false)); // Show something in th
$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();

View File

@ -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 @@ elseif (version_compare($installedVersion, '0.2.4', '==')) {
// 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']));
}
}

View File

@ -1,6 +1,7 @@
<?php
if(!file_exists('../../lib/base.php')) {
if (!file_exists('../../lib/base.php')) {
die('Please update the path to /lib/base.php in carddav.php or make use of /remote.php/carddav/');
}
require_once '../../lib/base.php';
require_once 'appinfo/remote.php';

View File

@ -9,7 +9,7 @@ OC.Contacts = OC.Contacts || {};
this.storage = storage;
this.book = book;
this.$template = template;
}
};
AddressBook.prototype.render = function() {
var self = this;
@ -198,16 +198,17 @@ OC.Contacts = OC.Contacts || {};
if(response.error) {
$(document).trigger('status.contacts.error', response);
}
cb(response);
if(typeof cb === 'function') {
cb(response);
}
});
};
/**
* Delete address book from data store and remove it from the DOM
* @param cb Optional callback function which
* @return An object with a boolean variable 'error'.
*/
AddressBook.prototype.destroy = function(cb) {
AddressBook.prototype.destroy = function() {
var self = this;
$.when(this.storage.deleteAddressBook(this.getBackend(), self.getId()))
.then(function(response) {
@ -233,7 +234,7 @@ OC.Contacts = OC.Contacts || {};
bookTemplate,
bookItemTemplate,
isFileAction
) {
) {
var self = this;
this.isFileAction = isFileAction || false;
this.storage = storage;
@ -271,7 +272,7 @@ OC.Contacts = OC.Contacts || {};
self.addressBooks.splice(self.addressBooks.indexOf(addressBook), 1);
self.buildImportSelect();
});
$(document).bind('status.addressbook.added', function(e) {
$(document).bind('status.addressbook.added', function() {
self.buildImportSelect();
});
this.$importIntoSelect.on('change', function() {
@ -289,7 +290,7 @@ OC.Contacts = OC.Contacts || {};
});
this.$importFileInput.fileupload({
dataType: 'json',
start: function(e, data) {
start: function(/*e, data*/) {
self.$importProgress.progressbar({value:false});
$('.tipsy').remove();
$('.import-upload').hide();
@ -370,7 +371,7 @@ OC.Contacts = OC.Contacts || {};
if(!response.error) {
self.$importProgress.progressbar('value', Number(response.data.progress));
self.$importStatusText.text(t('contacts', 'Imported {count} of {total} contacts',
{count:response.data.progress, total: self.importCount}));
{count:response.data.progress, total: self.importCount}));
} else {
console.warn('Error', response.message);
self.$importStatusText.text(response.message);
@ -386,7 +387,8 @@ OC.Contacts = OC.Contacts || {};
self.storage.startImport(
data.backend, data.addressBookId,
{filename:data.filename, progresskey:data.progresskey}
))
)
)
.then(function(response) {
console.log('response', response);
if(!response.error) {
@ -421,7 +423,7 @@ OC.Contacts = OC.Contacts || {};
$(document).trigger('status.contacts.error', response);
}
return defer;
}
};
/**
* Rebuild the select to choose which address book to import into.
@ -446,7 +448,7 @@ OC.Contacts = OC.Contacts || {};
self.$importFileInput.prop('disabled', true);
}
}
}
};
/**
* Create an AddressBook object, save it in internal list and append it's rendered result to the list
@ -481,7 +483,7 @@ OC.Contacts = OC.Contacts || {};
}
});
return addressBook;
}
};
/**
* Move a contacts from one address book to another..
@ -492,7 +494,6 @@ OC.Contacts = OC.Contacts || {};
*/
AddressBookList.prototype.moveContact = function(contact, from, target) {
console.log('AddressBookList.moveContact, contact', contact, from, target);
var self = this;
$.when(this.storage.moveContact(from.backend, from.id, contact.getId(), {target:target}))
.then(function(response) {
if(!response.error) {
@ -505,7 +506,7 @@ OC.Contacts = OC.Contacts || {};
$(document).trigger('status.contacts.error', response);
}
});
}
};
/**
* Get an array of address books with at least the required permission.
@ -515,7 +516,6 @@ OC.Contacts = OC.Contacts || {};
*/
AddressBookList.prototype.selectByPermission = function(permission, noClone) {
var books = [];
var self = this;
$.each(this.addressBooks, function(idx, book) {
if(book.getPermissions() & permission) {
// Clone the address book not to mess with with original
@ -545,7 +545,7 @@ OC.Contacts = OC.Contacts || {};
}
var error = '';
$.each(this.addressBooks, function(idx, book) {
if(book.getDisplayName() == name) {
if(book.getDisplayName() === name) {
console.log('Dupe');
error = t('contacts', 'An address book called {name} already exists', {name:name});
if(typeof cb === 'function') {
@ -580,7 +580,7 @@ OC.Contacts = OC.Contacts || {};
.fail(function(jqxhr, textStatus, error) {
$(this).removeClass('loading');
var err = textStatus + ', ' + error;
console.log( "Request Failed: " + err);
console.log('Request Failed', + err);
error = t('contacts', 'Failed adding address book: {error}', {error:err});
if(typeof cb === 'function') {
cb({error:true, message:error});
@ -598,9 +598,8 @@ OC.Contacts = OC.Contacts || {};
var defer = $.Deferred();
$.when(this.storage.getAddressBooksForUser()).then(function(response) {
if(!response.error) {
var num = response.data.addressbooks.length;
$.each(response.data.addressbooks, function(idx, addressBook) {
var book = self.insertAddressBook(addressBook);
self.insertAddressBook(addressBook);
});
self.buildImportSelect();
console.log('After buildImportSelect');
@ -620,7 +619,7 @@ OC.Contacts = OC.Contacts || {};
}
})
.fail(function(response) {
console.warn( "Request Failed:", response);
console.warn('Request Failed:', response);
defer.reject({
error: true,
message: t('contacts', 'Failed loading address books: {error}', {error:response.message})

View File

@ -13,7 +13,9 @@ Modernizr.load({
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length !== 2) continue;
if (p.length !== 2) {
continue;
}
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, ' '));
}
return b;

View File

@ -149,7 +149,7 @@ OC.Contacts = OC.Contacts || {};
}
// Not found, so adding it.
self.data[name].push(newproperty);
}
};
var self = this;
$.each(mergees, function(idx, mergee) {
@ -591,7 +591,7 @@ OC.Contacts = OC.Contacts || {};
if(!self.data.FN || !self.data.FN.length) {
self.data.FN = [{name:'FN', value:'', parameters:[]}];
}
self.data.FN[0]['value'] = value;
self.data.FN[0].value = value;
// Used for sorting list elements
self.displayNames.fn = value;
var nempty = true;

View File

@ -1,28 +0,0 @@
jQuery(function($) {
$('#cropbox').Jcrop({
onChange: showCoords,
onSelect: showCoords,
onRelease: clearCoords,
maxSize: [399, 399],
bgColor: 'black',
bgOpacity: .4,
boxWidth: 400,
boxHeight: 400,
setSelect: [ 100, 130, 50, 50 ]//,
//aspectRatio: 0.8
});
});
// Simple event handler, called from onChange and onSelect
// event handlers, as per the Jcrop invocation above
function showCoords(c) {
$('#x1').val(c.x);
$('#y1').val(c.y);
$('#x2').val(c.x2);
$('#y2').val(c.y2);
$('#w').val(c.w);
$('#h').val(c.h);
};
function clearCoords() {
$('#coords input').val('');
};

View File

@ -1,118 +0,0 @@
// Expanding Textareas
// https://github.com/bgrins/ExpandingTextareas
(function(factory) {
// Add jQuery via AMD registration or browser globals
if (typeof define === 'function' && define.amd) {
define([ 'jquery' ], factory);
}
else {
factory(jQuery);
}
}(function ($) {
$.expandingTextarea = $.extend({
autoInitialize: true,
initialSelector: "textarea.expanding",
opts: {
resize: function() { }
}
}, $.expandingTextarea || {});
var cloneCSSProperties = [
'lineHeight', 'textDecoration', 'letterSpacing',
'fontSize', 'fontFamily', 'fontStyle',
'fontWeight', 'textTransform', 'textAlign',
'direction', 'wordSpacing', 'fontSizeAdjust',
'wordWrap',
'borderLeftWidth', 'borderRightWidth',
'borderTopWidth','borderBottomWidth',
'paddingLeft', 'paddingRight',
'paddingTop','paddingBottom',
'marginLeft', 'marginRight',
'marginTop','marginBottom',
'boxSizing', 'webkitBoxSizing', 'mozBoxSizing', 'msBoxSizing'
];
var textareaCSS = {
position: "absolute",
height: "100%",
resize: "none"
};
var preCSS = {
visibility: "hidden",
border: "0 solid",
whiteSpace: "pre-wrap"
};
var containerCSS = {
position: "relative"
};
function resize() {
$(this).closest('.expandingText').find("div").text(this.value + ' ');
$(this).trigger("resize.expanding");
}
$.fn.expandingTextarea = function(o) {
var opts = $.extend({ }, $.expandingTextarea.opts, o);
if (o === "resize") {
return this.trigger("input.expanding");
}
if (o === "destroy") {
this.filter(".expanding-init").each(function() {
var textarea = $(this).removeClass('expanding-init');
var container = textarea.closest('.expandingText');
container.before(textarea).remove();
textarea
.attr('style', textarea.data('expanding-styles') || '')
.removeData('expanding-styles');
});
return this;
}
this.filter("textarea").not(".expanding-init").addClass("expanding-init").each(function() {
var textarea = $(this);
textarea.wrap("<div class='expandingText'></div>");
textarea.after("<pre class='textareaClone'><div></div></pre>");
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();
}
});
}));

View File

@ -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);
};
/**

View File

@ -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?

View File

@ -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;
}
/**

View File

@ -36,6 +36,7 @@ class Addressbook extends AbstractPIMCollection {
public static $l10n;
protected $_count;
/**
* @var Backend\AbstractBackend
*/
@ -69,11 +70,17 @@ class Addressbook extends AbstractPIMCollection {
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 @@ class Addressbook extends AbstractPIMCollection {
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
*

View File

@ -90,13 +90,16 @@ abstract class AbstractBackend {
*/
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 @@ abstract class AbstractBackend {
* 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 @@ abstract class AbstractBackend {
* compared with \OCP\PERMISSION_CREATE etc.
*/
public function hasContactMethodFor($permission) {
return (bool)($this->getContactPermissions() & $permission);
}
/**
@ -140,7 +149,9 @@ abstract class AbstractBackend {
* compared with \OCP\PERMISSION_CREATE etc.
*/
public function hasAddressBookMethodFor($permission) {
return (bool)($this->getAddressBookPermissions() & $permission);
}
/**
@ -152,7 +163,9 @@ abstract class AbstractBackend {
* @return bool
*/
public function hasAddressBook($addressbookid) {
return count($this->getAddressBook($addressbookid)) > 0;
}
/**
@ -165,7 +178,9 @@ abstract class AbstractBackend {
* @return integer|null
*/
public function numContacts($addressbookid) {
return count($this->getContacts($addressbookid));
}
/**
@ -365,15 +380,20 @@ abstract class AbstractBackend {
*/
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 @@ abstract class AbstractBackend {
* @return boolean
*/
public function isActive($addressBookId = null) {
$key = $this->combinedKey($addressBookId);
$key = 'active_' . $key;
@ -397,6 +418,7 @@ abstract class AbstractBackend {
* @return boolean
*/
public function setActive($active, $addressBookId = null) {
$key = $this->combinedKey($addressBookId);
$key = 'active_' . $key;
@ -410,6 +432,7 @@ abstract class AbstractBackend {
* @return array Format array('param1' => 'value', 'param2' => 'value')
*/
public function getPreferences($addressBookId) {
$key = $this->combinedKey($addressBookId);
$key = 'prefs_' . $key;
@ -424,6 +447,7 @@ abstract class AbstractBackend {
* @return boolean
*/
public function setPreferences($addressbookid, array $params) {
$key = $this->combinedKey($addressBookId);
$key = 'prefs_' . $key;

View File

@ -66,51 +66,60 @@ class Database extends AbstractBackend {
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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
* @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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
*/
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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
);
// 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 @@ class Database extends AbstractBackend {
}
// 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 @@ class Database extends AbstractBackend {
return false;
}
if($this->addressbooks && isset($this->addressbooks[$addressbookid])) {
if ($this->addressbooks && isset($this->addressbooks[$addressbookid])) {
unset($this->addressbooks[$addressbookid]);
}
@ -328,9 +364,10 @@ class Database extends AbstractBackend {
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 @@ class Database extends AbstractBackend {
}
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 @@ class Database extends AbstractBackend {
* @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 @@ class Database extends AbstractBackend {
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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
}
$ids = array($id);
if(!$noCollection) {
if (!$noCollection) {
$where_query .= ' AND `addressbookid` = ?';
$ids[] = $addressbookid;
}
@ -457,10 +505,12 @@ class Database extends AbstractBackend {
. $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 @@ class Database extends AbstractBackend {
}
$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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
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 @@ class Database extends AbstractBackend {
$updateRevision = true;
$isCardDAV = false;
if(!$contact instanceof VCard) {
if (!$contact instanceof VCard) {
try {
$contact = Reader::read($contact);
} catch(\Exception $e) {
@ -585,18 +644,23 @@ class Database extends AbstractBackend {
}
}
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 @@ class Database extends AbstractBackend {
}
}
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 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
}
$this->setModifiedAddressBook($addressbookid);
if(!$isBatch) {
if (!$isBatch) {
\OCP\Util::emitHook('OCA\Contacts', 'post_updateContact',
array(
'backend' => $this->name,
@ -647,7 +718,9 @@ class Database extends AbstractBackend {
'carddav' => $isCardDAV
)
);
}
return true;
}
@ -667,15 +740,19 @@ class Database extends AbstractBackend {
$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 @@ class Database extends AbstractBackend {
}
}
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 @@ class Database extends AbstractBackend {
. $id, \OCP\Util::DEBUG);
return false;
}
$this->setModifiedAddressBook($addressbookid);
return true;
}
@ -729,8 +811,10 @@ class Database extends AbstractBackend {
* @returns int | null
*/
public function lastModifiedContact($addressbookid, $id) {
$contact = $this->getContact($addressbookid, $id);
return ($contact ? $contact['lastmodified'] : null);
}
/**
@ -740,14 +824,18 @@ class Database extends AbstractBackend {
* @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 @@ class Database extends AbstractBackend {
}
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 @@ class Database extends AbstractBackend {
$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;
}
}
}

View File

@ -46,15 +46,19 @@ class Shared extends Database {
'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 @@ class Shared extends Database {
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 @@ class Shared extends Database {
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 @@ class Shared extends Database {
* @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;
}

View File

@ -28,12 +28,16 @@ class AddressBookController extends Controller {
$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 @@ class AddressBookController extends Controller {
$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 @@ 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)
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 @@ class AddressBookController extends Controller {
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 @@ class AddressBookController extends Controller {
$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 @@ class AddressBookController extends Controller {
$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 @@ class AddressBookController extends Controller {
$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 @@ class AddressBookController extends Controller {
return $response->bailOut($e->getMessage());
}
if($id === false) {
if ($id === false) {
return $response->bailOut(App::$l10n->t('Error creating contact.'));
}
@ -256,9 +263,10 @@ class AddressBookController extends Controller {
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 @@ class AddressBookController extends Controller {
$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));
}

View File

@ -33,7 +33,7 @@ class ContactController extends Controller {
$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 @@ class ContactController extends Controller {
$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 @@ class ContactController extends Controller {
$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 @@ class ContactController extends Controller {
->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);

View File

@ -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 @@ class GroupController extends Controller {
$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 @@ class GroupController extends Controller {
$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 @@ class GroupController extends Controller {
\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 @@ class GroupController extends Controller {
$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 @@ class GroupController extends Controller {
$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 @@ class GroupController extends Controller {
$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);
}