From fe137a92ef20109708e7af1359b694c8d76576f6 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 11 Jul 2015 14:36:04 +0200
Subject: [PATCH] Automatically update contact data.

---
 include/follow.php  | 32 +++++++++++++++++++++++++++++---
 include/items.php   |  8 ++++----
 include/onepoll.php | 21 ++++++++++++++-------
 mod/receive.php     |  2 +-
 4 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/include/follow.php b/include/follow.php
index 287a38b6bf..b2b6c75027 100644
--- a/include/follow.php
+++ b/include/follow.php
@@ -1,5 +1,30 @@
 <?php
 
+function update_contact($id) {
+	$r = q("SELECT `url`, `network` FROM `contact` WHERE `id` = %d", intval($id));
+	if (!$r)
+		return;
+
+	$ret = probe_url($r[0]["url"]);
+
+	// If probe_url fails the network code will be different
+	if ($ret["network"] != $r[0]["network"])
+		return;
+
+	q("UPDATE `contact` SET `url` = '%s', `nurl` = '%s', `addr` = '%s', `alias` = '%s', `batch` = '%s', `notify` = '%s', `poll` = '%s', `poco` = '%s', `name` = '%s', `nick` = '%s' WHERE `id` = %d",
+		dbesc($ret['url']),
+		dbesc(normalise_link($ret['url'])),
+		dbesc($ret['addr']),
+		dbesc($ret['alias']),
+		dbesc($ret['batch']),
+		dbesc($ret['notify']),
+		dbesc($ret['poll']),
+		dbesc($ret['poco']),
+		dbesc($ret['name']),
+		dbesc($ret['nick']),
+		intval($id)
+	);
+}
 
 //
 // Takes a $uid and a url/handle and adds a new contact
@@ -120,9 +145,10 @@ function new_contact($uid,$url,$interactive = false) {
 	// the poll url is more reliable than the profile url, as we may have
 	// indirect links or webfinger links
 
-	$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` = '%s' AND `network` = '%s' LIMIT 1",
+	$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' LIMIT 1",
 		intval($uid),
 		dbesc($ret['poll']),
+		dbesc(normalise_link($ret['poll'])),
 		dbesc($ret['network'])
 	);
 
@@ -136,8 +162,8 @@ function new_contact($uid,$url,$interactive = false) {
 				intval($uid)
 			);
 		}
-	}
-	else {
+
+	} else {
 
 
 		// check service class limits
diff --git a/include/items.php b/include/items.php
index 45d25e379f..c61ad1b1e2 100644
--- a/include/items.php
+++ b/include/items.php
@@ -2064,11 +2064,11 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
 		|| ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
 		openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
 		openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
-	}
-	else {
+	} elseif($contact['prvkey']) {
 		openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
 		openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
-	}
+	} else
+		logger("No private or public key for contact ".$contact['id']." ".$contact['url']);
 
 	$final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
 
@@ -2076,7 +2076,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
 		$final_dfrn_id = substr($final_dfrn_id,2);
 
 	if($final_dfrn_id != $orig_id) {
-		logger('dfrn_deliver: wrong dfrn_id.');
+		logger('dfrn_deliver: wrong dfrn_id. Original: '.$orig_id.' Target: '.$final_dfrn_id.' Test: '.$test);
 		// did not decode properly - cannot trust this site
 		return 3;
 	}
diff --git a/include/onepoll.php b/include/onepoll.php
index 7b93a9a2f0..57b3521d53 100644
--- a/include/onepoll.php
+++ b/include/onepoll.php
@@ -1,6 +1,8 @@
 <?php
 
 require_once("boot.php");
+require_once("include/follow.php");
+require_once("include/Scrape.php");
 
 function RemoveReply($subject) {
 	while (in_array(strtolower(substr($subject, 0, 3)), array("re:", "aw:")))
@@ -129,6 +131,10 @@ function onepoll_run(&$argv, &$argc){
 		: datetime_convert('UTC','UTC',$contact['last-update'], ATOM_TIME)
 	);
 
+	// Update the contact entry
+	if(($contact['network'] === NETWORK_OSTATUS) || ($contact['network'] === NETWORK_DIASPORA) || ($contact['network'] === NETWORK_DFRN))
+		update_contact($contact["id"]);
+
 	if($contact['network'] === NETWORK_DFRN) {
 
 
@@ -231,11 +237,11 @@ function onepoll_run(&$argv, &$argc){
 		if(($contact['duplex']) && strlen($contact['prvkey'])) {
 			openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
 			openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
-		}
-		else {
+		} elseif($contact['pubkey']) {
 			openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']);
 			openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']);
-		}
+		} else
+			logger("No private or public key for contact ".$contact['id']." ".$contact['url']);
 
 		$final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
 
@@ -243,8 +249,8 @@ function onepoll_run(&$argv, &$argc){
 			$final_dfrn_id = substr($final_dfrn_id,2);
 
 		if($final_dfrn_id != $orig_id) {
-			logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);	
-			// did not decode properly - cannot trust this site 
+			logger('poller: ID did not decode: ' . $contact['id'] . ' orig: ' . $orig_id . ' final: ' . $final_dfrn_id);
+			// did not decode properly - cannot trust this site
 			return;
 		}
 
@@ -255,7 +261,7 @@ function onepoll_run(&$argv, &$argc){
 		$xml = post_url($contact['poll'],$postvars);
 
 	}
-	elseif(($contact['network'] === NETWORK_OSTATUS) 
+	elseif(($contact['network'] === NETWORK_OSTATUS)
 		|| ($contact['network'] === NETWORK_DIASPORA)
 		|| ($contact['network'] === NETWORK_FEED) ) {
 
@@ -265,7 +271,8 @@ function onepoll_run(&$argv, &$argc){
 
 		$stat_writeable = ((($contact['notify']) && ($contact['rel'] == CONTACT_IS_FOLLOWER || $contact['rel'] == CONTACT_IS_FRIEND)) ? 1 : 0);
 
-		if($contact['network'] === NETWORK_OSTATUS && get_pconfig($importer_uid,'system','ostatus_autofriend'))
+		// Contacts from OStatus are always writable
+		if($contact['network'] === NETWORK_OSTATUS)
 			$stat_writeable = 1;
 
 		if($stat_writeable != $contact['writable']) {
diff --git a/mod/receive.php b/mod/receive.php
index ee15ebe8a3..95a5101675 100644
--- a/mod/receive.php
+++ b/mod/receive.php
@@ -9,7 +9,7 @@ require_once('include/salmon.php');
 require_once('include/crypto.php');
 require_once('include/diaspora.php');
 
-	
+
 function receive_post(&$a) {
 
 
-- 
2.39.5