v0.3 - Only refactoring, no new features; please note that this is still alpha-software

This commit is contained in:
Tobias Hößl 2012-08-12 09:31:34 +00:00
parent d233ccbcc7
commit 5aa4e7d967
6 changed files with 84 additions and 16 deletions

View File

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

View File

@ -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:_\$!<HomePage>!\$_\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);
}

View File

@ -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 <https://github.com/CatoTH/>
*/

View File

@ -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);

View File

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

View File

@ -339,6 +339,7 @@ function dav_plugin_admin(&$a, &$o)
$o .= t('Installed');
break;
case 1:
case 2:
$o .= "<strong>" . t('Upgrade needed') . "</strong><br>" . t("Please back up all calendar data (the tables beginning with dav_*) before proceeding. While all calendar events <i>should</i> 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.") . "<br><br><input type='submit' name='upgrade' value='" . t('Upgrade') . "'>";
break;
case -1:
@ -355,8 +356,8 @@ function dav_plugin_admin(&$a, &$o)
$o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
$o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
switch ($dbstatus) {
case 1:
$tables = dav_get_update_statements(1);
case 1: case 2:
$tables = dav_get_update_statements($dbstatus);
foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
break;
default: