From f039582421bddce3f9f68e62962ad5818cf71f1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B6=C3=9Fl?= Date: Thu, 2 Aug 2012 20:26:52 +0000 Subject: [PATCH] More refactoring, mainly of the addressbook/VCARD-part --- dav/calendar.friendica.fnk.php | 51 ++++- dav/common/calendar.fnk.php | 7 +- dav/common/dav_caldav_backend_private.inc.php | 2 +- .../dav_caldav_calendar_private.inc.php | 50 +++++ dav/common/dav_carddav_backend_common.inc.php | 116 ++++++++++ ...hp => dav_carddav_backend_private.inc.php} | 143 ++++-------- .../dav_carddav_backend_virtual.inc.php | 151 +++++++++++++ dav/common/dav_user_calendars.inc.php | 1 - dav/database-init.inc.php | 106 +++++---- ...v_caldav_backend_virtual_friendica.inc.php | 2 +- ..._carddav_backend_virtual_friendica.inc.php | 203 ++++-------------- dav/main.php | 20 +- 12 files changed, 531 insertions(+), 321 deletions(-) create mode 100644 dav/common/dav_caldav_calendar_private.inc.php create mode 100644 dav/common/dav_carddav_backend_common.inc.php rename dav/common/{dav_carddav_backend_std.inc.php => dav_carddav_backend_private.inc.php} (57%) create mode 100644 dav/common/dav_carddav_backend_virtual.inc.php diff --git a/dav/calendar.friendica.fnk.php b/dav/calendar.friendica.fnk.php index 7019cb50..60dd9c6b 100644 --- a/dav/calendar.friendica.fnk.php +++ b/dav/calendar.friendica.fnk.php @@ -13,16 +13,18 @@ define("CALDAV_URL_PREFIX", $path . "dav/"); define("DAV_APPNAME", "Friendica"); define("CALDAV_NAMESPACE_PRIVATE", 1); - define("CALDAV_FRIENDICA_MINE", "friendica-mine"); define("CALDAV_FRIENDICA_CONTACTS", "friendica-contacts"); $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"] = array(CALDAV_FRIENDICA_MINE, CALDAV_FRIENDICA_CONTACTS); $GLOBALS["CALDAV_PRIVATE_SYSTEM_BACKENDS"] = array("Sabre_CalDAV_Backend_Friendica"); -define("CARDDAV_NAMESPACE_COMMUNITYCONTACTS", 1); -define("CARDDAV_NAMESPACE_PHONECONTACTS", 2); -$GLOBALS["CARDDAV_PRIVATE_SYSTEM_BACKENDS"] = array("Sabre_CardDAV_Backend_FriendicaCommunity"); +define("CARDDAV_NAMESPACE_PRIVATE", 1); +define("CARDDAV_FRIENDICA_CONTACT", "friendica"); +$GLOBALS["CARDDAV_PRIVATE_SYSTEM_ADDRESSBOOKS"] = array(CARDDAV_FRIENDICA_CONTACT); +$GLOBALS["CARDDAV_PRIVATE_SYSTEM_BACKENDS"] = array("Sabre_CardDAV_Backend_Friendica"); + +$GLOBALS["CALDAV_ACL_PLUGIN_CLASS"] = "Sabre_DAVACL_Plugin_Friendica"; define("CALDAV_MAX_YEAR", date("Y") + 5); @@ -229,3 +231,44 @@ function wdcal_create_std_calendars() $stms = wdcal_create_std_calendars_get_statements($a->user["uid"]); foreach ($stms as $stmt) q($stmt); } + + + + +/** + * @param int $user_id + * @param bool $withcheck + * @return array + */ +function wdcal_create_std_addressbooks_get_statements($user_id, $withcheck = true) +{ + $stms = array(); + $a = get_app(); + $uris = array( + 'private' => t("Private Addresses"), + CARDDAV_FRIENDICA_CONTACT => t("Friendica Contacts"), + ); + foreach ($uris as $uri => $name) { + $cals = q("SELECT * FROM %s%saddressbooks WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, IntVal($user_id), dbesc($uri)); + if (count($cals) == 0 || !$withcheck) $stms[] = + sprintf("INSERT INTO %s%saddressbooks (`namespace`, `namespace_id`, `displayname`, `ctag`, `uri`) + VALUES (%d, %d, '%s', 1, '%s')", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CARDDAV_NAMESPACE_PRIVATE, IntVal($user_id), dbesc($name), dbesc($uri)); + } + return $stms; +} + +/** + */ +function wdcal_create_std_addressbooks() +{ + $a = get_app(); + if (!local_user()) return; + + $privates = q("SELECT COUNT(*) num FROM %s%addressbooks WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CARDDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"])); + if ($privates[0]["num"] > 0) return; + + $stms = wdcal_create_std_addressbooks_get_statements($a->user["uid"]); + foreach ($stms as $stmt) q($stmt); +} diff --git a/dav/common/calendar.fnk.php b/dav/common/calendar.fnk.php index 10d88833..14951119 100644 --- a/dav/common/calendar.fnk.php +++ b/dav/common/calendar.fnk.php @@ -249,6 +249,11 @@ function dav_create_server($force_authentication = false, $needs_caldav = true, $server->addPlugin($carddavPlugin); } + $aclPlugin = new $GLOBALS["CALDAV_ACL_PLUGIN_CLASS"](); + $aclPlugin->defaultUsernamePath = "principals/users"; + $server->addPlugin($aclPlugin); + + if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate return $server; @@ -275,7 +280,7 @@ function dav_get_current_user_calendars(&$server, $with_privilege = "") $calendars = array(); /** @var Sabre_DAVACL_Plugin $aclplugin */ $aclplugin = $server->getPlugin("acl"); - foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar")) { + foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar") || is_subclass_of($child, "Sabre_CalDAV_Calendar")) { if ($with_privilege != "") { $caluri = $calendar_path . $child->getName(); if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child; diff --git a/dav/common/dav_caldav_backend_private.inc.php b/dav/common/dav_caldav_backend_private.inc.php index 5356280a..1eb3b365 100644 --- a/dav/common/dav_caldav_backend_private.inc.php +++ b/dav/common/dav_caldav_backend_private.inc.php @@ -177,7 +177,7 @@ class Sabre_CalDAV_Backend_Private extends Sabre_CalDAV_Backend_Common "principaluri" => $principalUri, '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag'] ? $cal['ctag'] : '0', '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}supported-calendar-component-set' => new Sabre_CalDAV_Property_SupportedCalendarComponentSet($components), - "calendar_class" => "Sabre_CalDAV_Calendar", + "calendar_class" => "Sabre_CalDAV_Calendar_Private", ); foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field]; diff --git a/dav/common/dav_caldav_calendar_private.inc.php b/dav/common/dav_caldav_calendar_private.inc.php new file mode 100644 index 00000000..65f7c2b0 --- /dev/null +++ b/dav/common/dav_caldav_calendar_private.inc.php @@ -0,0 +1,50 @@ + '{DAV:}read', + 'principal' => $this->calendarInfo['principaluri'], + 'protected' => true, + ), + array( + 'privilege' => '{DAV:}write', + 'principal' => $this->calendarInfo['principaluri'], + 'protected' => true, + ), + /* + array( + 'privilege' => '{DAV:}read', + 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write', + 'protected' => true, + ), + array( + 'privilege' => '{DAV:}write', + 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write', + 'protected' => true, + ), + array( + 'privilege' => '{DAV:}read', + 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read', + 'protected' => true, + ), + array( + 'privilege' => '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}read-free-busy', + 'principal' => '{DAV:}authenticated', + 'protected' => true, + ), + */ + + ); + + } + + + +} \ No newline at end of file diff --git a/dav/common/dav_carddav_backend_common.inc.php b/dav/common/dav_carddav_backend_common.inc.php new file mode 100644 index 00000000..4279f8a1 --- /dev/null +++ b/dav/common/dav_carddav_backend_common.inc.php @@ -0,0 +1,116 @@ + $newValue) { + + switch ($property) { + case '{DAV:}displayname' : + $updates['displayname'] = $newValue; + break; + case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' : + $updates['description'] = $newValue; + break; + default : + // If any unsupported values were being updated, we must + // let the entire request fail. + return false; + } + + } + + // No values are being updated? + if (!$updates) { + return false; + } + + $query = 'UPDATE ' . CALDAV_SQL_DB . CALDAV_SQL_PREFIX . 'addressbooks SET ctag = ctag + 1 '; + foreach ($updates as $key=> $value) { + $query .= ', `' . dbesc($key) . '` = ' . dbesc($key) . ' '; + } + $query .= ' WHERE id = ' . IntVal($addressBookId); + q($query); + + return true; + + } + + /** + * @param int $addressbookId + */ + protected function increaseAddressbookCtag($addressbookId) + { + q("UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = '%d'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId)); + self::$addressbookCache = array(); + } +} \ No newline at end of file diff --git a/dav/common/dav_carddav_backend_std.inc.php b/dav/common/dav_carddav_backend_private.inc.php similarity index 57% rename from dav/common/dav_carddav_backend_std.inc.php rename to dav/common/dav_carddav_backend_private.inc.php index e257d156..71923b99 100644 --- a/dav/common/dav_carddav_backend_std.inc.php +++ b/dav/common/dav_carddav_backend_private.inc.php @@ -1,17 +1,6 @@ getNamespace()) return array(); $addressBooks = array(); - $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri)); - if (count($books) == 0) { - q("INSERT INTO %s%saddressbooks_phone (uid, principaluri, displayname, uri, description, ctag) VALUES (%d, '%s', '%s', '%s', '%s', 1)", - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $uid, dbesc($principalUri), 'Other', 'phone', 'Manually added contacts' - ); - $books = q("SELECT id, uri, displayname, principaluri, description, ctag FROM %s%saddressbooks_phone WHERE principaluri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($principalUri)); - } + $books = q("SELECT * FROM %s%saddressbooks WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($n["namespace"]), IntVal($n["namespace_id"])); foreach ($books as $row) { + if (in_array($row["uri"], $GLOBALS["CARDDAV_PRIVATE_SYSTEM_ADDRESSBOOKS"])) continue; + $addressBooks[] = array( - 'id' => CARDDAV_NAMESPACE_PHONECONTACTS . "-" . $row['id'], + 'id' => $row['id'], 'uri' => $row['uri'], - 'principaluri' => $row['principaluri'], + 'principaluri' => $principalUri, '{DAV:}displayname' => $row['displayname'], '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' => $row['description'], '{http://calendarserver.org/ns/}getctag' => $row['ctag'], @@ -76,57 +80,6 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract } - /** - * Updates an addressbook's properties - * - * See Sabre_DAV_IProperties for a description of the mutations array, as - * well as the return value. - * - * @param mixed $addressBookId - * @param array $mutations - * @throws Sabre_DAV_Exception_Forbidden - * @see Sabre_DAV_IProperties::updateProperties - * @return bool|array - */ - public function updateAddressBook($addressBookId, array $mutations) - { - $x = explode("-", $addressBookId); - - $updates = array(); - - foreach ($mutations as $property=> $newValue) { - - switch ($property) { - case '{DAV:}displayname' : - $updates['displayname'] = $newValue; - break; - case '{' . Sabre_CardDAV_Plugin::NS_CARDDAV . '}addressbook-description' : - $updates['description'] = $newValue; - break; - default : - // If any unsupported values were being updated, we must - // let the entire request fail. - return false; - } - - } - - // No values are being updated? - if (!$updates) { - return false; - } - - $query = 'UPDATE ' . CALDAV_SQL_DB . CALDAV_SQL_PREFIX . 'addressbooks_phone SET ctag = ctag + 1 '; - foreach ($updates as $key=> $value) { - $query .= ', `' . dbesc($key) . '` = ' . dbesc($key) . ' '; - } - $query .= ' WHERE id = ' . IntVal($x[1]); - q($query); - - return true; - - } - /** * Creates a new address book * @@ -138,6 +91,8 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function createAddressBook($principalUri, $url, array $properties) { + $uid = dav_compat_principal2uid($principalUri); + $values = array( 'displayname' => null, 'description' => null, @@ -160,8 +115,8 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract } - q("INSERT INTO %s%saddressbooks_phone (uri, displayname, description, principaluri, ctag) VALUES ('%s', '%s', '%s', '%s', 1)", - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), dbesc($values["principaluri"]) + q("INSERT INTO %s%saddressbooks (`uri`, `displayname`, `description`, `namespace`, `namespace_id`, `ctag`) VALUES ('%s', '%s', '%s', %d, %d, 1)", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($values["uri"]), dbesc($values["displayname"]), dbesc($values["description"]), CARDDAV_NAMESPACE_PRIVATE, IntVal($uid) ); } @@ -174,9 +129,8 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function deleteAddressBook($addressBookId) { - $x = explode("-", $addressBookId); - q("DELETE FROM %s%scards WHERE namespace = %d AND namespace_id = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1])); - q("DELETE FROM %s%saddressbooks_phone WHERE id = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1])); + q("DELETE FROM %s%saddressbookobjects WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId)); + q("DELETE FROM %s%saddressbooks WHERE `addressbook_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId)); } /** @@ -200,10 +154,8 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function getCards($addressbookId) { - $x = explode("-", $addressbookId); - - $r = q('SELECT id, carddata, uri, lastmodified, etag, size, contact FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND manually_deleted = 0', - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]) + $r = q('SELECT `id`, `carddata`, `uri`, `lastmodified`, `etag`, `size`, `contact` FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `manually_deleted` = 0', + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId) ); if ($r) return $r; return array(); @@ -222,9 +174,8 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function getCard($addressBookId, $cardUri) { - $x = explode("-", $addressBookId); - $x = q("SELECT id, carddata, uri, lastmodified, etag, size FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND uri = '%s'", - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($cardUri)); + $x = q("SELECT `id`, `carddata`, `uri`, `lastmodified`, `etag`, `size` FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `uri` = '%s'", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri)); if (count($x) == 0) throw new Sabre_DAV_Exception_NotFound(); return $x[0]; } @@ -257,14 +208,12 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function createCard($addressBookId, $cardUri, $cardData) { - $x = explode("-", $addressBookId); - $etag = md5($cardData); - q("INSERT INTO %s%scards (carddata, uri, lastmodified, namespace, namespace_id, etag, size) VALUES ('%s', '%s', %d, %d, '%s', %d)", - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($cardUri), time(), IntVal($x[0]), IntVal($x[1]), $etag, strlen($cardData) + q("INSERT INTO %s%saddressbookobjects (`carddata`, `uri`, `lastmodified`, `addressbook_id`, `etag`, `size`) VALUES ('%s', '%s', NOW(), %d, '%s', %d)", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($cardUri), IntVal($addressBookId), dbesc($etag), strlen($cardData) ); - q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1])); + q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId)); return '"' . $etag . '"'; @@ -298,14 +247,12 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function updateCard($addressBookId, $cardUri, $cardData) { - $x = explode("-", $addressBookId); - $etag = md5($cardData); - q("UPDATE %s%scards SET carddata = '%s', lastmodified = %d, etag = '%s', size = %d, manually_edited = 1 WHERE uri = '%s' AND namespace = %d AND namespace_id =%d", - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), time(), $etag, strlen($cardData), dbesc($cardUri), IntVal($x[10]), IntVal($x[1]) + q("UPDATE %s%saddressbookobjects SET `carddata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d, `manually_edited` = 1 WHERE `uri` = '%s' AND `addressbook_id` = %d", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($cardData), dbesc($etag), strlen($cardData), dbesc($cardUri), IntVal($addressBookId) ); - q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1])); + q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId)); return '"' . $etag . '"'; } @@ -320,10 +267,8 @@ class Sabre_CardDAV_Backend_Std extends Sabre_CardDAV_Backend_Abstract */ public function deleteCard($addressBookId, $cardUri) { - $x = explode("-", $addressBookId); - - q("DELETE FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND uri = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($cardUri)); - q('UPDATE %s%saddressbooks_phone SET ctag = ctag + 1 WHERE id = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[1])); + q("DELETE FROM %s%saddressbookobjects WHERE `addressbook_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId), dbesc($cardUri)); + q('UPDATE %s%saddressbooks SET `ctag` = `ctag` + 1 WHERE `id` = %d', CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressBookId)); return true; } diff --git a/dav/common/dav_carddav_backend_virtual.inc.php b/dav/common/dav_carddav_backend_virtual.inc.php new file mode 100644 index 00000000..98597c69 --- /dev/null +++ b/dav/common/dav_carddav_backend_virtual.inc.php @@ -0,0 +1,151 @@ + IntVal($obj["uri"]), + "carddata" => $obj["carddata"], + "uri" => $obj["uri"], + "lastmodified" => $obj["lastmodified"], + "addressbookid" => $addressbookId, + "etag" => $obj["etag"], + "size" => IntVal($obj["size"]), + ); + return $ret; + } + + + + /** + * @param string $principalUri + * @param string $addressbookUri + * @param array $properties + * @throws Sabre_DAV_Exception_Forbidden + * @return void + */ + public function createAddressBook($principalUri, $addressbookUri, array $properties) + { + throw new Sabre_DAV_Exception_Forbidden(); + } + + /** + * @param string $addressbookId + * @throws Sabre_DAV_Exception_Forbidden + * @return void + */ + public function deleteAddressBook($addressbookId) + { + throw new Sabre_DAV_Exception_Forbidden(); + } + + + /** + * @param string $addressbookId + * @param string $objectUri + * @param string $cardData + * @throws Sabre_DAV_Exception_Forbidden + * @return null|string|void + */ + function createCard($addressbookId, $objectUri, $cardData) + { + throw new Sabre_DAV_Exception_Forbidden(); + } + + /** + * @param string $addressbookId + * @param string $objectUri + * @param string $cardData + * @throws Sabre_DAV_Exception_Forbidden + * @return null|string|void + */ + function updateCard($addressbookId, $objectUri, $cardData) + { + throw new Sabre_DAV_Exception_Forbidden(); + } + + /** + * @param string $addressbookId + * @param string $objectUri + * @throws Sabre_DAV_Exception_Forbidden + * @return void + */ + function deleteCard($addressbookId, $objectUri) + { + throw new Sabre_DAV_Exception_Forbidden(); + } + + +} \ No newline at end of file diff --git a/dav/common/dav_user_calendars.inc.php b/dav/common/dav_user_calendars.inc.php index 62837dc5..b292074d 100644 --- a/dav/common/dav_user_calendars.inc.php +++ b/dav/common/dav_user_calendars.inc.php @@ -169,7 +169,6 @@ class Sabre_CalDAV_AnimexxUserCalendars implements Sabre_DAV_IExtendedCollection $objs[] = new $calendar["calendar_class"]($this->principalBackend, $backend, $calendar); } } - //$objs[] = new Sabre_CalDAV_AnimexxUserZirkelCalendars($this->principalBackend, $this->caldavBackend, $this->username); return $objs; } diff --git a/dav/database-init.inc.php b/dav/database-init.inc.php index 8cdd9b9b..7c0d23a3 100644 --- a/dav/database-init.inc.php +++ b/dav/database-init.inc.php @@ -43,6 +43,39 @@ function dav_get_update_statements($from_version) } + if (in_array($from_version, array(1, 2))) { + $stms[] = "DROP TABLE `dav_addressbooks_phone`"; + $stms[] = "DROP TABLE `dav_addressbooks_community`"; + $stms[] = "DROP TABLE `dav_cards`"; + + $stms[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `namespace` mediumint(9) NOT NULL, + `namespace_id` int(11) unsigned NOT NULL, + `displayname` varchar(200) NOT NULL, + `description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, + `uri` varchar(50) NOT NULL, + `ctag` int(11) unsigned NOT NULL DEFAULT '1', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + + $stms[] = "CREATE TABLE IF NOT EXISTS `dav_addressbookobjects` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `addressbook_id` int(11) unsigned NOT NULL, + `contact` int(11) DEFAULT NULL, + `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci, + `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, + `lastmodified` timestamp NULL DEFAULT NULL, + `manually_edited` tinyint(4) NOT NULL DEFAULT '0', + `manually_deleted` tinyint(4) NOT NULL DEFAULT '0', + `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, + `size` int(10) unsigned NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `namespace` (`addressbook_id`,`contact`), + KEY `contact` (`contact`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + } + return $stms; } @@ -54,24 +87,6 @@ function dav_get_create_statements($except = array()) { $arr = array(); - if (!in_array("dav_addressbooks_community", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks_community` ( - `uid` int(11) NOT NULL, - `ctag` int(11) unsigned NOT NULL DEFAULT '1', - PRIMARY KEY (`uid`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8"; - - if (!in_array("dav_addressbooks_phone", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks_phone` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `uid` int(11) NOT NULL, - `principaluri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, - `displayname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, - `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, - `description` text CHARACTER SET utf8 COLLATE utf8_unicode_ci, - `ctag` int(11) unsigned NOT NULL DEFAULT '1', - PRIMARY KEY (`id`), - UNIQUE KEY `principaluri` (`principaluri`,`uri`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8"; - if (!in_array("dav_caldav_log", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_caldav_log` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uid` mediumint(9) NOT NULL, @@ -82,7 +97,7 @@ function dav_get_create_statements($except = array()) `url` varchar(100) NOT NULL, PRIMARY KEY (`id`), KEY `mitglied` (`uid`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8"; +) ENGINE=InnoDB DEFAULT CHARSET=utf8"; if (!in_array("dav_calendarobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendarobjects` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, @@ -98,7 +113,7 @@ function dav_get_create_statements($except = array()) PRIMARY KEY (`id`), UNIQUE KEY `uri` (`uri`), KEY `calendar_id` (`calendar_id`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8"; +) ENGINE=InnoDB DEFAULT CHARSET=utf8"; if (!in_array("dav_calendars", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_calendars` ( `id` int(11) NOT NULL AUTO_INCREMENT, @@ -116,7 +131,7 @@ function dav_get_create_statements($except = array()) PRIMARY KEY (`id`), UNIQUE KEY (`namespace` , `namespace_id` , `uri`), KEY `uri` (`uri`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8"; +) ENGINE=InnoDB DEFAULT CHARSET=utf8"; if (!in_array("dav_cal_virtual_object_cache", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_cache` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, @@ -135,7 +150,7 @@ function dav_get_create_statements($except = array()) PRIMARY KEY (`id`), KEY `data_uri` (`data_uri`), KEY `ref_type` (`calendar_id`,`data_end`) -) ENGINE=MyISAM DEFAULT CHARSET=utf8"; +) ENGINE=InnoDB DEFAULT CHARSET=utf8"; if (!in_array("dav_cal_virtual_object_sync", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cal_virtual_object_sync` ( `calendar_id` int(10) unsigned NOT NULL, @@ -143,23 +158,6 @@ function dav_get_create_statements($except = array()) PRIMARY KEY (`calendar_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; - if (!in_array("dav_cards", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_cards` ( - `id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `namespace` tinyint(3) unsigned NOT NULL, - `namespace_id` int(11) unsigned NOT NULL, - `contact` int(11) DEFAULT NULL, - `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci, - `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, - `lastmodified` int(11) unsigned DEFAULT NULL, - `manually_edited` tinyint(4) NOT NULL DEFAULT '0', - `manually_deleted` tinyint(4) NOT NULL DEFAULT '0', - `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, - `size` int(10) unsigned NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `namespace` (`namespace`,`namespace_id`,`contact`), - KEY `contact` (`contact`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8"; - if (!in_array("dav_jqcalendar", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_jqcalendar` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `ical_recurr_uri` varchar(100) DEFAULT NULL, @@ -190,6 +188,33 @@ function dav_get_create_statements($except = array()) KEY `calendar_id` (`calendar_id`,`calendarobject_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; + if (!in_array("dav_addressbooks", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbooks` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `namespace` mediumint(9) NOT NULL, + `namespace_id` int(11) unsigned NOT NULL, + `displayname` varchar(200) NOT NULL, + `description` varchar(500) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, + `uri` varchar(50) NOT NULL, + `ctag` int(11) unsigned NOT NULL DEFAULT '1', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + + if (!in_array("dav_addressbookobjects", $except)) $arr[] = "CREATE TABLE IF NOT EXISTS `dav_addressbookobjects` ( + `id` int(11) unsigned NOT NULL AUTO_INCREMENT, + `addressbook_id` int(11) unsigned NOT NULL, + `contact` int(11) DEFAULT NULL, + `carddata` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci, + `uri` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL, + `lastmodified` timestamp NULL DEFAULT NULL, + `manually_edited` tinyint(4) NOT NULL DEFAULT '0', + `manually_deleted` tinyint(4) NOT NULL DEFAULT '0', + `etag` varchar(15) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL, + `size` int(10) unsigned NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `namespace` (`addressbook_id`,`contact`), + KEY `contact` (`contact`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; + return $arr; } @@ -201,6 +226,7 @@ function dav_check_tables() $x = q("DESCRIBE %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX); if (!$x) return -1; if (count($x) == 9) return 1; // Version 0.1 + // @TODO Detect Version 0.2 if (count($x) == 12) return 0; // Correct return -2; // Unknown version } @@ -229,7 +255,7 @@ function dav_create_tables() function dav_upgrade_tables() { $ver = dav_check_tables(); - if (!in_array($ver, array(1) )) return array("Unknown error"); + if (!in_array($ver, array(1))) return array("Unknown error"); $stms = dav_get_update_statements($ver); $errors = array(); diff --git a/dav/dav_caldav_backend_virtual_friendica.inc.php b/dav/dav_caldav_backend_virtual_friendica.inc.php index 9178aaab..d2810b4f 100644 --- a/dav/dav_caldav_backend_virtual_friendica.inc.php +++ b/dav/dav_caldav_backend_virtual_friendica.inc.php @@ -33,7 +33,7 @@ class Sabre_CalDAV_Backend_Friendica extends Sabre_CalDAV_Backend_Virtual * @return string */ public static function getBackendTypeName() { - return t("Friendicy-Native events"); + return t("Friendica-Native events"); } /** diff --git a/dav/dav_carddav_backend_virtual_friendica.inc.php b/dav/dav_carddav_backend_virtual_friendica.inc.php index 8d8a156c..3d7e0974 100644 --- a/dav/dav_carddav_backend_virtual_friendica.inc.php +++ b/dav/dav_carddav_backend_virtual_friendica.inc.php @@ -1,30 +1,39 @@ CARDDAV_NAMESPACE_COMMUNITYCONTACTS . "-" . $uid, + 'id' => $books[0]["id"], 'uri' => "friendica", 'principaluri' => $principalUri, '{DAV:}displayname' => t("Friendica-Contacts"), @@ -61,56 +67,12 @@ class Sabre_CardDAV_Backend_FriendicaCommunity extends Sabre_CardDAV_Backend_Abs } - - /** - * Updates an addressbook's properties - * - * See Sabre_DAV_IProperties for a description of the mutations array, as - * well as the return value. - * - * @param string $addressBookId - * @param array $mutations - * @throws Sabre_DAV_Exception_Forbidden - * @see Sabre_DAV_IProperties::updateProperties - * @return bool|array - */ - public function updateAddressBook($addressBookId, array $mutations) - { - throw new Sabre_DAV_Exception_Forbidden(); - } - - /** - * Creates a new address book - * - * @param string $principalUri - * @param string $url Just the 'basename' of the url. - * @param array $properties - * @throws Sabre_DAV_Exception_Forbidden - * @return void - */ - public function createAddressBook($principalUri, $url, array $properties) - { - throw new Sabre_DAV_Exception_Forbidden(); - } - - /** - * Deletes an entire addressbook and all its contents - * - * @param int $addressBookId - * @throws Sabre_DAV_Exception_Forbidden - * @return void - */ - public function deleteAddressBook($addressBookId) - { - throw new Sabre_DAV_Exception_Forbidden(); - } - - /** + * @static * @param array $contact * @return array */ - private function dav_contactarr2vcardsource($contact) + private static function dav_contactarr2vcardsource($contact) { $name = explode(" ", $contact["name"]); $first_name = $last_name = ""; @@ -164,118 +126,25 @@ class Sabre_CardDAV_Backend_FriendicaCommunity extends Sabre_CardDAV_Backend_Abs } /** - * @param int $uid - * @param array|int[] $exclude_ids - * @return array + * @static + * @param int $addressbookId + * @throws Sabre_DAV_Exception_NotFound */ - private function dav_getCommunityContactsVCards($uid = 0, $exclude_ids = array()) - { - $notin = (count($exclude_ids) > 0 ? " AND id NOT IN (" . implode(", ", $exclude_ids) . ") " : ""); - $uid = IntVal($uid); - $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0 $notin ORDER BY `name` ASC", $uid); + static protected function createCache_internal($addressbookId) { + //$notin = (count($exclude_ids) > 0 ? " AND id NOT IN (" . implode(", ", $exclude_ids) . ") " : ""); + $addressbook = q("SELECT * FROM %s%saddressbooks WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($addressbookId)); + if (count($addressbook) != 1 || $addressbook[0]["namespace"] != CARDDAV_NAMESPACE_PRIVATE) throw new Sabre_DAV_Exception_NotFound(); + $contacts = q("SELECT * FROM `contact` WHERE `uid` = %d AND `blocked` = 0 AND `pending` = 0 AND `hidden` = 0 AND `archive` = 0 ORDER BY `name` ASC", $addressbook[0]["namespace_id"]); $retdata = array(); foreach ($contacts as $contact) { - $x = $this->dav_contactarr2vcardsource($contact); - $x["contact"] = $contact["id"]; - $retdata[] = $x; - } - return $retdata; - } - - - /** - * Returns all cards for a specific addressbook id. - * - * This method should return the following properties for each card: - * * carddata - raw vcard data - * * uri - Some unique url - * * lastmodified - A unix timestamp - * - * It's recommended to also return the following properties: - * * etag - A unique etag. This must change every time the card changes. - * * size - The size of the card in bytes. - * - * If these last two properties are provided, less time will be spent - * calculating them. If they are specified, you can also ommit carddata. - * This may speed up certain requests, especially with large cards. - * - * @param string $addressbookId - * @return array - */ - public function getCards($addressbookId) - { - $add = explode("-", $addressbookId); - - $indb = q('SELECT id, carddata, uri, lastmodified, etag, size, contact, manually_deleted FROM %s%scards WHERE namespace = %d AND namespace_id = %d', - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($add[0]), IntVal($add[1]) - ); - $found_contacts = array(); - $contacts = array(); - foreach ($indb as $x) { - if ($x["manually_deleted"] == 0) $contacts[] = $x; - $found_contacts[] = IntVal($x["contact"]); - } - $new_found = $this->dav_getCommunityContactsVCards($add[1], $found_contacts); - foreach ($new_found as $new) { - q("INSERT INTO %s%scards (namespace, namespace_id, contact, carddata, uri, lastmodified, manually_edited, manually_deleted, etag, size) - VALUES (%d, %d, %d, '%s', '%s', %d, 0, 0, '%s', %d)", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, - IntVal($add[0]), IntVal($add[1]), IntVal($new["contact"]), dbesc($new["carddata"]), dbesc($new["uri"]), time(), md5($new["carddata"]), strlen($new["carddata"]) + $x = static::dav_contactarr2vcardsource($contact); + q("INSERT INTO %s%saddressbookobjects (`addressbook_id`, `contact`, `carddata`, `uri`, `lastmodified`, `etag`, `size`) VALUES (%d, %d, '%s', '%s', NOW(), '%s', %d)", + CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $addressbookId, $contact["id"], dbesc($x["carddata"]), dbesc($x["uri"]), dbesc($x["etag"]), $x["size"] ); } - return array_merge($contacts, $new_found); } - /** - * Returns a specfic card. - * - * The same set of properties must be returned as with getCards. The only - * exception is that 'carddata' is absolutely required. - * - * @param mixed $addressBookId - * @param string $cardUri - * @throws Sabre_DAV_Exception_NotFound - * @return array - */ - public function getCard($addressBookId, $cardUri) - { - $x = explode("-", $addressBookId); - $x = q("SELECT id, carddata, uri, lastmodified, etag, size FROM %s%scards WHERE namespace = %d AND namespace_id = %d AND uri = '%s'", - CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($x[0]), IntVal($x[1]), dbesc($cardUri)); - if (count($x) == 0) throw new Sabre_DAV_Exception_NotFound(); - return $x[0]; - } - - /** - * Creates a new card. - * - * The addressbook id will be passed as the first argument. This is the - * same id as it is returned from the getAddressbooksForUser method. - * - * The cardUri is a base uri, and doesn't include the full path. The - * cardData argument is the vcard body, and is passed as a string. - * - * It is possible to return an ETag from this method. This ETag is for the - * newly created resource, and must be enclosed with double quotes (that - * is, the string itself must contain the double quotes). - * - * You should only return the ETag if you store the carddata as-is. If a - * subsequent GET request on the same card does not have the same body, - * byte-by-byte and you did return an ETag here, clients tend to get - * confused. - * - * If you don't return an ETag, you can just return null. - * - * @param string $addressBookId - * @param string $cardUri - * @param string $cardData - * @throws Sabre_DAV_Exception_Forbidden - * @return string - */ - public function createCard($addressBookId, $cardUri, $cardData) - { - throw new Sabre_DAV_Exception_Forbidden(); - } /** * Updates a card. diff --git a/dav/main.php b/dav/main.php index a56862ae..c36d0084 100644 --- a/dav/main.php +++ b/dav/main.php @@ -30,15 +30,21 @@ function dav_include_files() require_once (__DIR__ . "/common/calendar.fnk.php"); require_once (__DIR__ . "/common/calendar_rendering.fnk.php"); + require_once (__DIR__ . "/common/dav_caldav_backend_common.inc.php"); require_once (__DIR__ . "/common/dav_caldav_backend_private.inc.php"); require_once (__DIR__ . "/common/dav_caldav_backend_virtual.inc.php"); require_once (__DIR__ . "/common/dav_caldav_root.inc.php"); require_once (__DIR__ . "/common/dav_user_calendars.inc.php"); - require_once (__DIR__ . "/common/dav_carddav_root.inc.php"); - require_once (__DIR__ . "/common/dav_carddav_backend_std.inc.php"); - require_once (__DIR__ . "/common/dav_user_addressbooks.inc.php"); require_once (__DIR__ . "/common/dav_caldav_calendar_virtual.inc.php"); + require_once (__DIR__ . "/common/dav_caldav_calendar_private.inc.php"); + + require_once (__DIR__ . "/common/dav_carddav_root.inc.php"); + require_once (__DIR__ . "/common/dav_carddav_backend_common.inc.php"); + require_once (__DIR__ . "/common/dav_carddav_backend_virtual.inc.php"); + require_once (__DIR__ . "/common/dav_carddav_backend_private.inc.php"); + require_once (__DIR__ . "/common/dav_user_addressbooks.inc.php"); + require_once (__DIR__ . "/common/wdcal_configuration.php"); require_once (__DIR__ . "/common/wdcal_backend.inc.php"); @@ -65,6 +71,9 @@ function dav_init(&$a) * ALTER TABLE `photo` ADD INDEX ( `contact-id` ) */ + ini_set("display_errors", 1); + error_reporting(E_ALL); + dav_include_files(); if (false) { @@ -74,6 +83,7 @@ function dav_init(&$a) } wdcal_create_std_calendars(); + wdcal_create_std_addressbooks(); wdcal_addRequiredHeaders(); if ($a->argc >= 2 && $a->argv[1] == "wdcal") { @@ -104,10 +114,6 @@ function dav_init(&$a) $browser = new Sabre_DAV_Browser_Plugin(); $server->addPlugin($browser); - $aclPlugin = new Sabre_DAVACL_Plugin_Friendica(); - $aclPlugin->defaultUsernamePath = "principals/users"; - $server->addPlugin($aclPlugin); - $server->exec(); killme();