From ccef8995779d8b97e0bd9965eee5c592101969de Mon Sep 17 00:00:00 2001 From: friendica Date: Sun, 27 Nov 2011 17:41:23 -0800 Subject: [PATCH] start of d* private mail, dba without mysqli --- boot.php | 2 +- database.sql | 9 +++ include/dba.php | 48 +++++++++---- include/diaspora.php | 168 ++++++++++++++++++++++++++++++++++++++++++- update.php | 13 +++- 5 files changed, 224 insertions(+), 16 deletions(-) diff --git a/boot.php b/boot.php index 01280c42f4..19cdb87bee 100644 --- a/boot.php +++ b/boot.php @@ -11,7 +11,7 @@ require_once('include/cache.php'); define ( 'FRIENDICA_PLATFORM', 'Friendica'); define ( 'FRIENDICA_VERSION', '2.3.1178' ); define ( 'DFRN_PROTOCOL_VERSION', '2.22' ); -define ( 'DB_UPDATE_VERSION', 1105 ); +define ( 'DB_UPDATE_VERSION', 1106 ); define ( 'EOL', "
\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); diff --git a/database.sql b/database.sql index ffe9bb9071..0599dd3e48 100644 --- a/database.sql +++ b/database.sql @@ -266,10 +266,12 @@ CREATE TABLE IF NOT EXISTS `item` ( CREATE TABLE IF NOT EXISTS `mail` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `uid` int(10) unsigned NOT NULL, + `guid` char(64) NOT NULL, `from-name` char(255) NOT NULL, `from-photo` char(255) NOT NULL, `from-url` char(255) NOT NULL, `contact-id` char(255) NOT NULL, + `convid` int(10) unsigned NOT NULL, `title` char(255) NOT NULL, `body` mediumtext NOT NULL, `seen` tinyint(1) NOT NULL, @@ -703,3 +705,10 @@ INDEX ( `uid` ), INDEX ( `gcid` ) ) ENGINE = MyISAM DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS `conv` ( + `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , + `guid` CHAR( 64 ) NOT NULL , + `recips` MEDIUMTEXT NOT NULL , + `uid` INT NOT NULL +) ENGINE = MyISAM DEFAULT CHARSET=utf8; diff --git a/include/dba.php b/include/dba.php index e9d47cd1a0..3fa615d903 100644 --- a/include/dba.php +++ b/include/dba.php @@ -16,8 +16,10 @@ class dba { private $debug = 0; private $db; + public $mysqli = true; public $connected = false; + function __construct($server,$user,$pass,$db,$install = false) { $server = trim($server); @@ -43,6 +45,7 @@ class dba { } } else { + $this->mysqli = false; $this->db = mysql_connect($server,$user,$pass); if($this->db && mysql_select_db($db,$this->db)) { $this->connected = true; @@ -64,7 +67,7 @@ class dba { if((! $this->db) || (! $this->connected)) return false; - if(class_exists('mysqli')) + if($this->mysqli) $result = @$this->db->query($sql); else $result = @mysql_query($sql,$this->db); @@ -73,7 +76,7 @@ class dba { $mesg = ''; - if(class_exists('mysqli') && $this->db->errno) + if($this->mysqli && $this->db->errno) logger('dba: ' . $this->db->error); else logger('dba: ' . mysql_error($this->db)); @@ -82,9 +85,13 @@ class dba { $mesg = 'false'; elseif($result === true) $mesg = 'true'; - else - $mesg = $result->num_rows . ' results' . EOL; - + else { + if($this->mysqli) + $mesg = $result->num_rows . ' results' . EOL; + else + $mesg = mysql_num_rows($result) . ' results' . EOL; + } + $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL; logger('dba: ' . $str ); @@ -108,11 +115,21 @@ class dba { return $result; $r = array(); - if($result->num_rows) { - while($x = $result->fetch_array(MYSQL_ASSOC)) - $r[] = $x; - $result->free_result(); + if($this->mysqli) { + if($result->num_rows) { + while($x = $result->fetch_array(MYSQL_ASSOC)) + $r[] = $x; + $result->free_result(); + } } + else { + if(mysql_num_rows($result)) { + while($x = mysql_fetch_array($result, MYSQL_ASSOC)) + $r[] = $x; + mysql_free_result($result); + } + } + if($this->debug) logger('dba: ' . printable(print_r($r, true)), LOGGER_DATA); @@ -124,12 +141,19 @@ class dba { } public function escape($str) { - if($this->db && $this->connected) - return @$this->db->real_escape_string($str); + if($this->db && $this->connected) { + if($this->mysqli) + return @$this->db->real_escape_string($str); + else + return @mysql_real_escape_string($str,$this->db); + } } function __destruct() { - @$this->db->close(); + if($this->mysqli) + @$this->db->close(); + else + @mysql_close($this->db); } }} diff --git a/include/diaspora.php b/include/diaspora.php index 0ffb22b956..07d454e450 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -71,6 +71,9 @@ function diaspora_dispatch($importer,$msg) { elseif($xmlbase->photo) { $ret = diaspora_photo($importer,$xmlbase->photo,$msg); } + elseif($xmlbase->conversation) { + $ret = diaspora_conversation($importer,$xmlbase->conversation,$msg); + } else { logger('diaspora_dispatch: unknown message type: ' . print_r($xmlbase,true)); } @@ -902,8 +905,6 @@ function diaspora_comment($importer,$xml,$msg) { $parent_author_signature = (($xml->parent_author_signature) ? notags(unxmlify($xml->parent_author_signature)) : ''); - $text = $xml->text; - $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']); if(! $contact) { logger('diaspora_comment: cannot find contact: ' . $msg['author']); @@ -1050,6 +1051,169 @@ function diaspora_comment($importer,$xml,$msg) { return; } + + + +function diaspora_conversation($importer,$xml,$msg) { + + $a = get_app(); + + $guid = notags(unxmlify($xml->guid)); + $subject = notags(unxmlify($xml->subject)); + $diaspora_handle = notags(unxmlify($xml->diaspora_handle)); + $participant_handles = notags(unxmlify($xml->participant_handles)); + $created_at = datetime_convert('UTC','UTC',notags(unxmlify($xml->created_at))); + + $parent_uri = $diaspora_handle . ':' . $guid; + + $messages = $xml->message; + + if(! count($messages)) { + logger('diaspora_conversation: empty conversation'); + return; + } + + $contact = diaspora_get_contact_by_handle($importer['uid'],$msg['author']); + if(! $contact) { + logger('diaspora_conversation: cannot find contact: ' . $msg['author']); + return; + } + + if(($contact['rel'] == CONTACT_IS_FOLLOWER) || ($contact['blocked']) || ($contact['readonly'])) { + logger('diaspora_conversation: Ignoring this author.'); + return 202; + } + + foreach($messages as $msg) { + $msg_guid = notags(unxmlify($msg->guid)); + $msg_parent_guid = notags(unxmlify($msg->parent_guid)); + $msg_parent_author_signature = notags(unxmlify($msg->parent_author_signature)); + $msg_author_signature = notags(unxmlify($msg->author_signature)); + $msg_text = unxmlify($msg->text); + $msg_created_at = datetime_convert('UTC','UTC',notags(unxmlify($msg->created_at))); + $msg_diaspora_handle = notags(unxmlify($msg->diaspora_handle)); + $msg_conversation_guid = notags(unxmlify($msg->conversation_guid)); + if($msg_conversation_guid != $guid) { + logger('diaspora_conversation: message conversation guid does not belong to the current conversation. ' . $xml); + continue; + } + + + $body = diaspora2bb($msg_text); + $message_id = $msg_diaspora_handle . ':' . $msg_guid; + + $person = find_diaspora_person_by_handle($msg_diaspora_handle); + if(is_array($person) && x($person,'pubkey')) + $key = $person['pubkey']; + else { + logger('diaspora_conversation: unable to find author details'); + continue; + } + + $r = q("select id from mail where `uri` = '%s' limit 1", + dbesc($message_id) + ); + if(count($r)) { + logger('diaspora_conversation: duplicate message already delivered.', LOGGER_DEBUG); + continue; + } + + + // don't forget guid, convid!!! + + q("insert into mail ( `uid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`replied`,`uri`,`parent-uri`,`created`) values ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')", + intval($importer['uid']), + dbesc($person['name']), + dbesc($person['photo']), + dbesc($person['url']), + intval($contact['id']), + dbesc($subject), + dbesc($body), + 0, + 0, + dbesc($message_id), + dbesc($parent_uri), + dbesc($msg_created_at) + ); + + } + + +/* + $author_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle; + + $author_signature = base64_decode($author_signature); + + if(strcasecmp($diaspora_handle,$msg['author']) == 0) { + $person = $contact; + $key = $msg['key']; + } + else { + $person = find_diaspora_person_by_handle($diaspora_handle); + + if(is_array($person) && x($person,'pubkey')) + $key = $person['pubkey']; + else { + logger('diaspora_comment: unable to find author details'); + return; + } + } + + if(! rsa_verify($author_signed_data,$author_signature,$key,'sha256')) { + logger('diaspora_comment: verification failed.'); + return; + } + + if($parent_author_signature) { + $owner_signed_data = $guid . ';' . $parent_guid . ';' . $text . ';' . $diaspora_handle; + + $parent_author_signature = base64_decode($parent_author_signature); + + $key = $msg['key']; + + if(! rsa_verify($owner_signed_data,$parent_author_signature,$key,'sha256')) { + logger('diaspora_comment: owner verification failed.'); + return; + } + } + + // Phew! Everything checks out. Now create an item. + + $body = diaspora2bb($text); + + $message_id = $diaspora_handle . ':' . $guid; + + $datarray = array(); + + $str_tags = ''; + + $tags = get_tags($body); + + if(count($tags)) { + foreach($tags as $tag) { + if(strpos($tag,'#') === 0) { + if(strpos($tag,'[url=')) + continue; + $basetag = str_replace('_',' ',substr($tag,1)); + $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body); + if(strlen($str_tags)) + $str_tags .= ','; + $str_tags .= '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; + continue; + } + } + } + +*/ + + return; +} + + + + + + function diaspora_photo($importer,$xml,$msg) { $a = get_app(); diff --git a/update.php b/update.php index 113bc590aa..45169f1ef8 100644 --- a/update.php +++ b/update.php @@ -1,6 +1,6 @@