From 5aa4e7d9671e20f6a3daab1b83e8f0ca78a62195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B6=C3=9Fl?= Date: Sun, 12 Aug 2012 09:31:34 +0000 Subject: [PATCH] v0.3 - Only refactoring, no new features; please note that this is still alpha-software --- dav/Changelog.txt | 4 +- dav/common/calendar.fnk.php | 48 ++++++++++++++++++- dav/dav.php | 2 +- dav/friendica/database-init.inc.php | 15 +++--- ..._carddav_backend_virtual_friendica.inc.php | 26 +++++++++- dav/friendica/main.php | 5 +- 6 files changed, 84 insertions(+), 16 deletions(-) diff --git a/dav/Changelog.txt b/dav/Changelog.txt index 2a8304c8..c336baa9 100644 --- a/dav/Changelog.txt +++ b/dav/Changelog.txt @@ -1,4 +1,6 @@ -v0.2.1 +v0.3 +[REFACTOR] The new version of the VObject Library is used +[REFACTOR] The addressbook part has beed heavily refactored [REFACTOR] Remove some Friendica-specific code out of the "common"-folder v0.2.0 diff --git a/dav/common/calendar.fnk.php b/dav/common/calendar.fnk.php index 10177b34..11f31f7d 100644 --- a/dav/common/calendar.fnk.php +++ b/dav/common/calendar.fnk.php @@ -7,7 +7,9 @@ define("DAV_DISPLAYNAME", "{DAV:}displayname"); define("DAV_CALENDARCOLOR", "{http://apple.com/ns/ical/}calendar-color"); -class DAVVersionMismatchException extends Exception {} +class DAVVersionMismatchException extends Exception +{ +} class vcard_source_data_email @@ -99,6 +101,48 @@ class vcard_source_data public $photo; } +/** + * @param vcard_source_data $vcardsource + * @return string + */ +function vcard_source_compile($vcardsource) +{ + $str = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n"; + $str .= "N:" . str_replace(";", ",", $vcardsource->name_last) . ";" . str_replace(";", ",", $vcardsource->name_first) . ";" . str_replace(";", ",", $vcardsource->name_middle) . ";;\r\n"; + $str .= "FN:" . str_replace(";", ",", $vcardsource->name_first) . " " . str_replace(";", ",", $vcardsource->name_middle) . " " . str_replace(";", ",", $vcardsource->name_last) . "\r\n"; + $str .= "REV:" . str_replace(" ", "T", $vcardsource->last_update) . "Z\r\n"; + + $item_count = 0; + for ($i = 0; $i < count($vcardsource->homepages); $i++) { + if ($i == 0) $str .= "URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n"; + else { + $c = ++$item_count; + $str .= "item$c.URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n"; + $str .= "item$c.X-ABLabel:_\$!!\$_\r\n"; + } + } + + if (is_object($vcardsource->photo)) { + $data = base64_encode($vcardsource->photo->binarydata); + $str .= "PHOTO;ENCODING=BASE64;TYPE=" . $vcardsource->photo->type . ":" . $data . "\r\n"; + } + + if (isset($vcardsource->socialnetworks) && is_array($vcardsource->socialnetworks)) foreach ($vcardsource->socialnetworks as $netw) switch ($netw->type) { + case "dfrn": + $str .= "X-SOCIALPROFILE;type=dfrn;x-user=" . $netw->nick . ":" . $netw->url . "\r\n"; + break; + case "facebook": + $str .= "X-SOCIALPROFILE;type=facebook;x-user=" . $netw->nick . ":" . $netw->url . "\r\n"; + break; + case "twitter": + $str .= "X-SOCIALPROFILE;type=twitter;x-user=" . $netw->nick . ":" . $netw->url . "\r\n"; + break; + } + + $str .= "END:VCARD\r\n"; + return $str; +} + /** * @param int $phpDate (UTC) @@ -209,7 +253,7 @@ function dav_create_server($force_authentication = false, $needs_caldav = true, $aclPlugin->defaultUsernamePath = "principals/users"; $server->addPlugin($aclPlugin); } else { - $aclPlugin = new Sabre_DAVACL_Plugin(); + $aclPlugin = new Sabre_DAVACL_Plugin(); $aclPlugin->defaultUsernamePath = "principals/users"; $server->addPlugin($aclPlugin); } diff --git a/dav/dav.php b/dav/dav.php index 40040dfe..f4d08f02 100644 --- a/dav/dav.php +++ b/dav/dav.php @@ -2,7 +2,7 @@ /** * Name: Calendar with CalDAV Support * Description: A web-based calendar system with CalDAV-support. Also brings your Friendica-Contacts to your CardDAV-capable mobile phone. Requires PHP >= 5.3. - * Version: 0.2.0 + * Version: 0.3.0 * Author: Tobias Hößl */ diff --git a/dav/friendica/database-init.inc.php b/dav/friendica/database-init.inc.php index 5978fd01..f91ddb25 100644 --- a/dav/friendica/database-init.inc.php +++ b/dav/friendica/database-init.inc.php @@ -44,10 +44,6 @@ 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, @@ -228,8 +224,12 @@ 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 + + $x2 = q("show tables like '%s%saddressbooks'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX); + if (!$x2 || count($x2) == 0) return 2; // Version 0.2 + if (count($x) == 12) return 0; // Correct + return -2; // Unknown version } @@ -257,11 +257,10 @@ 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, 2))) return array("Unknown error"); $stms = dav_get_update_statements($ver); - $errors = array(); + $errors = array(); global $db; foreach ($stms as $st) { // @TODO Friendica-dependent $db->q($st); diff --git a/dav/friendica/dav_carddav_backend_virtual_friendica.inc.php b/dav/friendica/dav_carddav_backend_virtual_friendica.inc.php index 3d7e0974..3a06d042 100644 --- a/dav/friendica/dav_carddav_backend_virtual_friendica.inc.php +++ b/dav/friendica/dav_carddav_backend_virtual_friendica.inc.php @@ -128,15 +128,15 @@ class Sabre_CardDAV_Backend_Friendica extends Sabre_CardDAV_Backend_Virtual /** * @static * @param int $addressbookId + * @param bool $force * @throws Sabre_DAV_Exception_NotFound */ - static protected function createCache_internal($addressbookId) { + static protected function createCache_internal($addressbookId, $force = false) { //$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 = 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)", @@ -146,6 +146,27 @@ class Sabre_CardDAV_Backend_Friendica extends Sabre_CardDAV_Backend_Virtual } + /** + * @static + * @param int $addressbookId + * @param int $contactId + * @param bool $force + * @throws Sabre_DAV_Exception_NotFound + */ + static protected function createCardCache($addressbookId, $contactId, $force = false) + { + $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 AND `id` = %d ORDER BY `name` ASC", + $addressbook[0]["namespace_id"], IntVal($contactId)); + $contact = $contacts[0]; + + $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"] + ); + } /** * Updates a card. * @@ -202,4 +223,5 @@ class Sabre_CardDAV_Backend_Friendica extends Sabre_CardDAV_Backend_Virtual return true; } + } diff --git a/dav/friendica/main.php b/dav/friendica/main.php index 712b6c7b..0fe939eb 100644 --- a/dav/friendica/main.php +++ b/dav/friendica/main.php @@ -339,6 +339,7 @@ function dav_plugin_admin(&$a, &$o) $o .= t('Installed'); break; case 1: + case 2: $o .= "" . t('Upgrade needed') . "
" . t("Please back up all calendar data (the tables beginning with dav_*) before proceeding. While all calendar events should be converted to the new database structure, it's always safe to have a backup. Below, you can have a look at the database-queries that will be made when pressing the 'update'-button.") . "

"; break; case -1: @@ -355,8 +356,8 @@ function dav_plugin_admin(&$a, &$o) $o .= "

" . t("Manual creation of the database tables:") . "

"; $o .= "" . t("Show SQL-statements") . "