Switched to static methods for DFRN

This commit is contained in:
Michael Vogel 2016-01-25 15:20:58 +01:00
parent 77c6020dc2
commit 62de6be495
7 changed files with 934 additions and 931 deletions

View file

@ -530,6 +530,8 @@ class App {
private $cached_profile_image; private $cached_profile_image;
private $cached_profile_picdate; private $cached_profile_picdate;
private static $a;
/** /**
* @brief App constructor. * @brief App constructor.
*/ */
@ -710,6 +712,8 @@ class App {
} }
} }
self::$a = $this;
} }
function get_basepath() { function get_basepath() {
@ -734,6 +738,10 @@ class App {
function get_baseurl($ssl = false) { function get_baseurl($ssl = false) {
// Is the function called statically?
if (!is_object($this))
return(self::$a->get_baseurl($ssl));
$scheme = $this->scheme; $scheme = $this->scheme;
if((x($this->config,'system')) && (x($this->config['system'],'ssl_policy'))) { if((x($this->config,'system')) && (x($this->config['system'],'ssl_policy'))) {

View file

@ -132,8 +132,8 @@ function terminate_friendship($user,$self,$contact) {
diaspora_unshare($user,$contact); diaspora_unshare($user,$contact);
} }
elseif($contact['network'] === NETWORK_DFRN) { elseif($contact['network'] === NETWORK_DFRN) {
require_once('include/items.php'); require_once('include/dfrn.php');
dfrn_deliver($user,$contact,'placeholder', 1); dfrn::deliver($user,$contact,'placeholder', 1);
} }
} }

View file

@ -279,12 +279,12 @@ function delivery_run(&$argv, &$argc){
if ($mail) { if ($mail) {
$item['body'] = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']); $item['body'] = fix_private_photos($item['body'],$owner['uid'],null,$message[0]['contact-id']);
$atom = dfrn_mail($item, $owner); $atom = dfrn::mail($item, $owner);
} elseif ($fsuggest) { } elseif ($fsuggest) {
$atom = dfrn_fsuggest($item, $owner); $atom = dfrn::fsuggest($item, $owner);
q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id'])); q("DELETE FROM `fsuggest` WHERE `id` = %d LIMIT 1", intval($item['id']));
} elseif ($relocate) } elseif ($relocate)
$atom = dfrn_relocate($owner, $uid); $atom = dfrn::relocate($owner, $uid);
elseif($followup) { elseif($followup) {
$msgitems = array(); $msgitems = array();
foreach($items as $item) { // there is only one item foreach($items as $item) { // there is only one item
@ -295,7 +295,7 @@ function delivery_run(&$argv, &$argc){
$msgitems[] = $item; $msgitems[] = $item;
} }
} }
$atom = dfrn_entries($msgitems,$owner); $atom = dfrn::entries($msgitems,$owner);
} else { } else {
$msgitems = array(); $msgitems = array();
foreach($items as $item) { foreach($items as $item) {
@ -321,7 +321,7 @@ function delivery_run(&$argv, &$argc){
$msgitems[] = $item; $msgitems[] = $item;
} }
} }
$atom = dfrn_entries($msgitems,$owner); $atom = dfrn::entries($msgitems,$owner);
} }
logger('notifier entry: '.$contact["url"].' '.$target_item["guid"].' entry: '.$atom, LOGGER_DEBUG); logger('notifier entry: '.$contact["url"].' '.$target_item["guid"].' entry: '.$atom, LOGGER_DEBUG);
@ -380,7 +380,7 @@ function delivery_run(&$argv, &$argc){
} }
if(! was_recently_delayed($contact['id'])) if(! was_recently_delayed($contact['id']))
$deliver_status = dfrn_deliver($owner,$contact,$atom); $deliver_status = dfrn::deliver($owner,$contact,$atom);
else else
$deliver_status = (-1); $deliver_status = (-1);

View file

@ -3,6 +3,8 @@ require_once('include/items.php');
require_once('include/Contact.php'); require_once('include/Contact.php');
require_once('include/ostatus.php'); require_once('include/ostatus.php');
class dfrn {
/** /**
* @brief Generates the atom entries for delivery.php * @brief Generates the atom entries for delivery.php
* *
@ -13,18 +15,18 @@ require_once('include/ostatus.php');
* *
* @return string DFRN entries * @return string DFRN entries
*/ */
function dfrn_entries($items,$owner) { function entries($items,$owner) {
$doc = new DOMDocument('1.0', 'utf-8'); $doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; $doc->formatOutput = true;
$root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false); $root = self::add_header($doc, $owner, "dfrn:owner", "", false);
if(! count($items)) if(! count($items))
return trim($doc->saveXML()); return trim($doc->saveXML());
foreach($items as $item) { foreach($items as $item) {
$entry = dfrn_entry($doc, "text", $item, $owner, $item["entry:comment-allow"], $item["entry:cid"]); $entry = self::entry($doc, "text", $item, $owner, $item["entry:comment-allow"], $item["entry:cid"]);
$root->appendChild($entry); $root->appendChild($entry);
} }
@ -36,7 +38,6 @@ function dfrn_entries($items,$owner) {
* *
* This function is called when another server is pulling data from the user feed. * This function is called when another server is pulling data from the user feed.
* *
* @param App $a
* @param string $dfrn_id DFRN ID from the requesting party * @param string $dfrn_id DFRN ID from the requesting party
* @param string $owner_nick Owner nick name * @param string $owner_nick Owner nick name
* @param string $last_update Date of the last update * @param string $last_update Date of the last update
@ -44,7 +45,9 @@ function dfrn_entries($items,$owner) {
* *
* @return string DFRN feed entries * @return string DFRN feed entries
*/ */
function dfrn_feed(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) { function feed($dfrn_id, $owner_nick, $last_update, $direction = 0) {
$a = get_app();
$sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic $sitefeed = ((strlen($owner_nick)) ? false : true); // not yet implemented, need to rewrite huge chunks of following logic
$public_feed = (($dfrn_id) ? false : true); $public_feed = (($dfrn_id) ? false : true);
@ -120,8 +123,7 @@ function dfrn_feed(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
for($x = 0; $x < count($groups); $x ++) for($x = 0; $x < count($groups); $x ++)
$groups[$x] = '<' . intval($groups[$x]) . '>' ; $groups[$x] = '<' . intval($groups[$x]) . '>' ;
$gs = implode('|', $groups); $gs = implode('|', $groups);
} } else
else
$gs = '<<>>' ; // Impossible to match $gs = '<<>>' ; // Impossible to match
$sql_extra = sprintf(" $sql_extra = sprintf("
@ -201,7 +203,7 @@ function dfrn_feed(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
else else
$author = "author"; $author = "author";
$root = dfrn_add_header($doc, $owner, $author, $alternatelink, true); $root = self::add_header($doc, $owner, $author, $alternatelink, true);
// This hook can't work anymore // This hook can't work anymore
// call_hooks('atom_feed', $atom); // call_hooks('atom_feed', $atom);
@ -227,12 +229,10 @@ function dfrn_feed(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
// catch any email that's in a public conversation and make sure it doesn't leak // catch any email that's in a public conversation and make sure it doesn't leak
if($item['private']) if($item['private'])
continue; continue;
} } else
else {
$type = 'text'; $type = 'text';
}
$entry = dfrn_entry($doc, $type, $item, $owner, true); $entry = self::entry($doc, $type, $item, $owner, true);
$root->appendChild($entry); $root->appendChild($entry);
} }
@ -252,11 +252,11 @@ function dfrn_feed(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0) {
* *
* @return string DFRN mail * @return string DFRN mail
*/ */
function dfrn_mail($item, $owner) { function mail($item, $owner) {
$doc = new DOMDocument('1.0', 'utf-8'); $doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; $doc->formatOutput = true;
$root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false); $root = self::add_header($doc, $owner, "dfrn:owner", "", false);
$mail = $doc->createElement("dfrn:mail"); $mail = $doc->createElement("dfrn:mail");
$sender = $doc->createElement("dfrn:sender"); $sender = $doc->createElement("dfrn:sender");
@ -286,11 +286,11 @@ function dfrn_mail($item, $owner) {
* *
* @return string DFRN suggestions * @return string DFRN suggestions
*/ */
function dfrn_fsuggest($item, $owner) { function fsuggest($item, $owner) {
$doc = new DOMDocument('1.0', 'utf-8'); $doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; $doc->formatOutput = true;
$root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false); $root = self::add_header($doc, $owner, "dfrn:owner", "", false);
$suggest = $doc->createElement("dfrn:suggest"); $suggest = $doc->createElement("dfrn:suggest");
@ -313,9 +313,7 @@ function dfrn_fsuggest($item, $owner) {
* *
* @return string DFRN relocations * @return string DFRN relocations
*/ */
function dfrn_relocate($owner, $uid) { function relocate($owner, $uid) {
$a = get_app();
/* get site pubkey. this could be a new installation with no site keys*/ /* get site pubkey. this could be a new installation with no site keys*/
$pubkey = get_config('system','site_pubkey'); $pubkey = get_config('system','site_pubkey');
@ -329,15 +327,16 @@ function dfrn_relocate($owner, $uid) {
WHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid); WHERE `profile` = 1 AND `uid` = %d ORDER BY scale;", $uid);
$photos = array(); $photos = array();
$ext = Photo::supportedTypes(); $ext = Photo::supportedTypes();
foreach($rp as $p){
$photos[$p['scale']] = $a->get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']]; foreach($rp as $p)
} $photos[$p['scale']] = app::get_baseurl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']];
unset($rp, $ext); unset($rp, $ext);
$doc = new DOMDocument('1.0', 'utf-8'); $doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true; $doc->formatOutput = true;
$root = dfrn_add_header($doc, $owner, "dfrn:owner", "", false); $root = self::add_header($doc, $owner, "dfrn:owner", "", false);
$relocate = $doc->createElement("dfrn:relocate"); $relocate = $doc->createElement("dfrn:relocate");
@ -368,8 +367,7 @@ function dfrn_relocate($owner, $uid) {
* *
* @return object XML root object * @return object XML root object
*/ */
function dfrn_add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) { private function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) {
$a = get_app();
if ($alternatelink == "") if ($alternatelink == "")
$alternatelink = $owner['url']; $alternatelink = $owner['url'];
@ -387,7 +385,8 @@ function dfrn_add_header($doc, $owner, $authorelement, $alternatelink = "", $pub
$root->setAttribute("xmlns:ostatus", NS_OSTATUS); $root->setAttribute("xmlns:ostatus", NS_OSTATUS);
$root->setAttribute("xmlns:statusnet", NS_STATUSNET); $root->setAttribute("xmlns:statusnet", NS_STATUSNET);
xml_add_element($doc, $root, "id", $a->get_baseurl()."/profile/".$owner["nick"]); //xml_add_element($doc, $root, "id", app::get_baseurl()."/profile/".$owner["nick"]);
xml_add_element($doc, $root, "id", app::get_baseurl()."/profile/".$owner["nick"]);
xml_add_element($doc, $root, "title", $owner["name"]); xml_add_element($doc, $root, "title", $owner["name"]);
$attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION); $attributes = array("uri" => "https://friendi.ca", "version" => FRIENDICA_VERSION."-".DB_UPDATE_VERSION);
@ -402,13 +401,13 @@ function dfrn_add_header($doc, $owner, $authorelement, $alternatelink = "", $pub
ostatus_hublinks($doc, $root); ostatus_hublinks($doc, $root);
if ($public) { if ($public) {
$attributes = array("rel" => "salmon", "href" => $a->get_baseurl()."/salmon/".$owner["nick"]); $attributes = array("rel" => "salmon", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
xml_add_element($doc, $root, "link", "", $attributes); xml_add_element($doc, $root, "link", "", $attributes);
$attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => $a->get_baseurl()."/salmon/".$owner["nick"]); $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-replies", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
xml_add_element($doc, $root, "link", "", $attributes); xml_add_element($doc, $root, "link", "", $attributes);
$attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => $a->get_baseurl()."/salmon/".$owner["nick"]); $attributes = array("rel" => "http://salmon-protocol.org/ns/salmon-mention", "href" => app::get_baseurl()."/salmon/".$owner["nick"]);
xml_add_element($doc, $root, "link", "", $attributes); xml_add_element($doc, $root, "link", "", $attributes);
} }
@ -417,7 +416,7 @@ function dfrn_add_header($doc, $owner, $authorelement, $alternatelink = "", $pub
xml_add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME)); xml_add_element($doc, $root, "updated", datetime_convert("UTC", "UTC", "now", ATOM_TIME));
$author = dfrn_add_author($doc, $owner, $authorelement, $public); $author = self::add_author($doc, $owner, $authorelement, $public);
$root->appendChild($author); $root->appendChild($author);
return $root; return $root;
@ -432,8 +431,7 @@ function dfrn_add_header($doc, $owner, $authorelement, $alternatelink = "", $pub
* *
* @return object XML author object * @return object XML author object
*/ */
function dfrn_add_author($doc, $owner, $authorelement, $public) { private function add_author($doc, $owner, $authorelement, $public) {
$a = get_app();
$author = $doc->createElement($authorelement); $author = $doc->createElement($authorelement);
@ -445,7 +443,7 @@ function dfrn_add_author($doc, $owner, $authorelement, $public) {
xml_add_element($doc, $author, "name", $owner["name"], $attributes); xml_add_element($doc, $author, "name", $owner["name"], $attributes);
$attributes = array("dfrn:updated" => $namdate); $attributes = array("dfrn:updated" => $namdate);
xml_add_element($doc, $author, "uri", $a->get_baseurl().'/profile/'.$owner["nickname"], $attributes); xml_add_element($doc, $author, "uri", app::get_baseurl().'/profile/'.$owner["nickname"], $attributes);
$attributes = array("rel" => "photo", "type" => "image/jpeg", "dfrn:updated" => $picdate, $attributes = array("rel" => "photo", "type" => "image/jpeg", "dfrn:updated" => $picdate,
"media:width" => 175, "media:height" => 175, "href" => $owner['photo']); "media:width" => 175, "media:height" => 175, "href" => $owner['photo']);
@ -544,7 +542,7 @@ function dfrn_add_author($doc, $owner, $authorelement, $public) {
* *
* @return object XML author object * @return object XML author object
*/ */
function dfrn_add_entry_author($doc, $element, $contact_url, $item) { private function add_entry_author($doc, $element, $contact_url, $item) {
$contact = get_contact_details_by_url($contact_url, $item["uid"]); $contact = get_contact_details_by_url($contact_url, $item["uid"]);
@ -583,7 +581,7 @@ function dfrn_add_entry_author($doc, $element, $contact_url, $item) {
* *
* @return object XML activity object * @return object XML activity object
*/ */
function dfrn_create_activity($doc, $element, $activity) { private function create_activity($doc, $element, $activity) {
if($activity) { if($activity) {
$entry = $doc->createElement($element); $entry = $doc->createElement($element);
@ -630,7 +628,7 @@ function dfrn_create_activity($doc, $element, $activity) {
* *
* @return object XML attachment object * @return object XML attachment object
*/ */
function dfrn_get_attachment($doc, $root, $item) { private function get_attachment($doc, $root, $item) {
$arr = explode('[/attach],',$item['attach']); $arr = explode('[/attach],',$item['attach']);
if(count($arr)) { if(count($arr)) {
foreach($arr as $r) { foreach($arr as $r) {
@ -665,8 +663,7 @@ function dfrn_get_attachment($doc, $root, $item) {
* *
* @return object XML entry object * @return object XML entry object
*/ */
function dfrn_entry($doc, $type, $item, $owner, $comment = false, $cid = 0) { private function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
$a = get_app();
$mentioned = array(); $mentioned = array();
@ -694,16 +691,16 @@ function dfrn_entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
$htmlbody = bbcode($htmlbody, false, false, 7); $htmlbody = bbcode($htmlbody, false, false, 7);
} }
$author = dfrn_add_entry_author($doc, "author", $item["author-link"], $item); $author = self::add_entry_author($doc, "author", $item["author-link"], $item);
$entry->appendChild($author); $entry->appendChild($author);
$dfrnowner = dfrn_add_entry_author($doc, "dfrn:owner", $item["owner-link"], $item); $dfrnowner = self::add_entry_author($doc, "dfrn:owner", $item["owner-link"], $item);
$entry->appendChild($dfrnowner); $entry->appendChild($dfrnowner);
if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) { if(($item['parent'] != $item['id']) || ($item['parent-uri'] !== $item['uri']) || (($item['thr-parent'] !== '') && ($item['thr-parent'] !== $item['uri']))) {
$parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"])); $parent = q("SELECT `guid` FROM `item` WHERE `id` = %d", intval($item["parent"]));
$parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']); $parent_item = (($item['thr-parent']) ? $item['thr-parent'] : $item['parent-uri']);
$attributes = array("ref" => $parent_item, "type" => "text/html", "href" => $a->get_baseurl().'/display/'.$parent[0]['guid']); $attributes = array("ref" => $parent_item, "type" => "text/html", "href" => app::get_baseurl().'/display/'.$parent[0]['guid']);
xml_add_element($doc, $entry, "thr:in-reply-to", "", $attributes); xml_add_element($doc, $entry, "thr:in-reply-to", "", $attributes);
} }
@ -717,7 +714,7 @@ function dfrn_entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
xml_add_element($doc, $entry, "content", (($type === 'html') ? $htmlbody : $body), array("type" => $type)); xml_add_element($doc, $entry, "content", (($type === 'html') ? $htmlbody : $body), array("type" => $type));
xml_add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html", xml_add_element($doc, $entry, "link", "", array("rel" => "alternate", "type" => "text/html",
"href" => $a->get_baseurl()."/display/".$item["guid"])); "href" => app::get_baseurl()."/display/".$item["guid"]));
// "comment-allow" is some old fashioned stuff for old Friendica versions. // "comment-allow" is some old fashioned stuff for old Friendica versions.
// It is included in the rewritten code for completeness // It is included in the rewritten code for completeness
@ -753,11 +750,11 @@ function dfrn_entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
xml_add_element($doc, $entry, "activity:verb", construct_verb($item)); xml_add_element($doc, $entry, "activity:verb", construct_verb($item));
$actobj = dfrn_create_activity($doc, "activity:object", $item['object']); $actobj = self::create_activity($doc, "activity:object", $item['object']);
if ($actobj) if ($actobj)
$entry->appendChild($actobj); $entry->appendChild($actobj);
$actarg = dfrn_create_activity($doc, "activity:target", $item['target']); $actarg = self::create_activity($doc, "activity:target", $item['target']);
if ($actarg) if ($actarg)
$entry->appendChild($actarg); $entry->appendChild($actarg);
@ -788,7 +785,240 @@ function dfrn_entry($doc, $type, $item, $owner, $comment = false, $cid = 0) {
"href" => $mention)); "href" => $mention));
} }
dfrn_get_attachment($doc, $entry, $item); self::get_attachment($doc, $entry, $item);
return $entry; return $entry;
} }
/**
* @brief Delivers the atom content to the contacts
*
* @param array $owner Owner record
* @param array $contactr Contact record of the receiver
* @param string $atom Content that will be transmitted
* @param bool $dissolve (to be documented)
*
* @return int Deliver status. -1 means an error.
*/
function deliver($owner,$contact,$atom, $dissolve = false) {
$a = get_app();
$idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
if($contact['duplex'] && $contact['dfrn-id'])
$idtosend = '0:' . $orig_id;
if($contact['duplex'] && $contact['issued-id'])
$idtosend = '1:' . $orig_id;
$rino = get_config('system','rino_encrypt');
$rino = intval($rino);
// use RINO1 if mcrypt isn't installed and RINO2 was selected
if ($rino==2 and !function_exists('mcrypt_create_iv')) $rino=1;
logger("Local rino version: ". $rino, LOGGER_DEBUG);
$ssl_val = intval(get_config('system','ssl_policy'));
$ssl_policy = '';
switch($ssl_val){
case SSL_POLICY_FULL:
$ssl_policy = 'full';
break;
case SSL_POLICY_SELFSIGN:
$ssl_policy = 'self';
break;
case SSL_POLICY_NONE:
default:
$ssl_policy = 'none';
break;
}
$url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino='.$rino : '');
logger('dfrn_deliver: ' . $url);
$xml = fetch_url($url);
$curl_stat = $a->get_curl_code();
if(! $curl_stat)
return(-1); // timed out
logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
if(! $xml)
return 3;
if(strpos($xml,'<?xml') === false) {
logger('dfrn_deliver: no valid XML returned');
logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
return 3;
}
$res = parse_xml_string($xml);
if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
return (($res->status) ? $res->status : 3);
$postvars = array();
$sent_dfrn_id = hex2bin((string) $res->dfrn_id);
$challenge = hex2bin((string) $res->challenge);
$perm = (($res->perm) ? $res->perm : null);
$dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
$rino_remote_version = intval($res->rino);
$page = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
logger("Remote rino version: ".$rino_remote_version." for ".$contact["url"], LOGGER_DEBUG);
if($owner['page-flags'] == PAGE_PRVGROUP)
$page = 2;
$final_dfrn_id = '';
if($perm) {
if((($perm == 'rw') && (! intval($contact['writable'])))
|| (($perm == 'r') && (intval($contact['writable'])))) {
q("update contact set writable = %d where id = %d",
intval(($perm == 'rw') ? 1 : 0),
intval($contact['id'])
);
$contact['writable'] = (string) 1 - intval($contact['writable']);
}
}
if(($contact['duplex'] && strlen($contact['pubkey']))
|| ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
|| ($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 {
openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
}
$final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
if(strpos($final_dfrn_id,':') == 1)
$final_dfrn_id = substr($final_dfrn_id,2);
if($final_dfrn_id != $orig_id) {
logger('dfrn_deliver: wrong dfrn_id.');
// did not decode properly - cannot trust this site
return 3;
}
$postvars['dfrn_id'] = $idtosend;
$postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
if($dissolve)
$postvars['dissolve'] = '1';
if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
$postvars['data'] = $atom;
$postvars['perm'] = 'rw';
} else {
$postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
$postvars['perm'] = 'r';
}
$postvars['ssl_policy'] = $ssl_policy;
if($page)
$postvars['page'] = $page;
if($rino>0 && $rino_remote_version>0 && (! $dissolve)) {
logger('rino version: '. $rino_remote_version);
switch($rino_remote_version) {
case 1:
// Deprecated rino version!
$key = substr(random_string(),0,16);
$data = aes_encrypt($postvars['data'],$key);
break;
case 2:
// RINO 2 based on php-encryption
try {
$key = Crypto::createNewRandomKey();
} catch (CryptoTestFailed $ex) {
logger('Cannot safely create a key');
return -1;
} catch (CannotPerformOperation $ex) {
logger('Cannot safely create a key');
return -1;
}
try {
$data = Crypto::encrypt($postvars['data'], $key);
} catch (CryptoTestFailed $ex) {
logger('Cannot safely perform encryption');
return -1;
} catch (CannotPerformOperation $ex) {
logger('Cannot safely perform encryption');
return -1;
}
break;
default:
logger("rino: invalid requested verision '$rino_remote_version'");
return -1;
}
$postvars['rino'] = $rino_remote_version;
$postvars['data'] = bin2hex($data);
#logger('rino: sent key = ' . $key, LOGGER_DEBUG);
if($dfrn_version >= 2.1) {
if(($contact['duplex'] && strlen($contact['pubkey']))
|| ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
|| ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey'])))
openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
else
openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
} else {
if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY))
openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
else
openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
}
logger('md5 rawkey ' . md5($postvars['key']));
$postvars['key'] = bin2hex($postvars['key']);
}
logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
$xml = post_url($contact['notify'],$postvars);
logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
$curl_stat = $a->get_curl_code();
if((! $curl_stat) || (! strlen($xml)))
return(-1); // timed out
if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
return(-1);
if(strpos($xml,'<?xml') === false) {
logger('dfrn_deliver: phase 2: no valid XML returned');
logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
return 3;
}
if($contact['term-date'] != '0000-00-00 00:00:00') {
logger("dfrn_deliver: $url back from the dead - removing mark for death");
require_once('include/Contact.php');
unmark_for_death($contact);
}
$res = parse_xml_string($xml);
return $res->status;
}
}

View file

@ -1696,245 +1696,9 @@ function tgroup_check($uid,$item) {
if((! $community_page) && (! $prvgroup)) if((! $community_page) && (! $prvgroup))
return false; return false;
return true; return true;
} }
function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
$a = get_app();
$idtosend = $orig_id = (($contact['dfrn-id']) ? $contact['dfrn-id'] : $contact['issued-id']);
if($contact['duplex'] && $contact['dfrn-id'])
$idtosend = '0:' . $orig_id;
if($contact['duplex'] && $contact['issued-id'])
$idtosend = '1:' . $orig_id;
$rino = get_config('system','rino_encrypt');
$rino = intval($rino);
// use RINO1 if mcrypt isn't installed and RINO2 was selected
if ($rino==2 and !function_exists('mcrypt_create_iv')) $rino=1;
logger("Local rino version: ". $rino, LOGGER_DEBUG);
$ssl_val = intval(get_config('system','ssl_policy'));
$ssl_policy = '';
switch($ssl_val){
case SSL_POLICY_FULL:
$ssl_policy = 'full';
break;
case SSL_POLICY_SELFSIGN:
$ssl_policy = 'self';
break;
case SSL_POLICY_NONE:
default:
$ssl_policy = 'none';
break;
}
$url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino='.$rino : '');
logger('dfrn_deliver: ' . $url);
$xml = fetch_url($url);
$curl_stat = $a->get_curl_code();
if(! $curl_stat)
return(-1); // timed out
logger('dfrn_deliver: ' . $xml, LOGGER_DATA);
if(! $xml)
return 3;
if(strpos($xml,'<?xml') === false) {
logger('dfrn_deliver: no valid XML returned');
logger('dfrn_deliver: returned XML: ' . $xml, LOGGER_DATA);
return 3;
}
$res = parse_xml_string($xml);
if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id)))
return (($res->status) ? $res->status : 3);
$postvars = array();
$sent_dfrn_id = hex2bin((string) $res->dfrn_id);
$challenge = hex2bin((string) $res->challenge);
$perm = (($res->perm) ? $res->perm : null);
$dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
$rino_remote_version = intval($res->rino);
$page = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
logger("Remote rino version: ".$rino_remote_version." for ".$contact["url"], LOGGER_DEBUG);
if($owner['page-flags'] == PAGE_PRVGROUP)
$page = 2;
$final_dfrn_id = '';
if($perm) {
if((($perm == 'rw') && (! intval($contact['writable'])))
|| (($perm == 'r') && (intval($contact['writable'])))) {
q("update contact set writable = %d where id = %d",
intval(($perm == 'rw') ? 1 : 0),
intval($contact['id'])
);
$contact['writable'] = (string) 1 - intval($contact['writable']);
}
}
if(($contact['duplex'] && strlen($contact['pubkey']))
|| ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
|| ($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 {
openssl_private_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['prvkey']);
openssl_private_decrypt($challenge,$postvars['challenge'],$contact['prvkey']);
}
$final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.'));
if(strpos($final_dfrn_id,':') == 1)
$final_dfrn_id = substr($final_dfrn_id,2);
if($final_dfrn_id != $orig_id) {
logger('dfrn_deliver: wrong dfrn_id.');
// did not decode properly - cannot trust this site
return 3;
}
$postvars['dfrn_id'] = $idtosend;
$postvars['dfrn_version'] = DFRN_PROTOCOL_VERSION;
if($dissolve)
$postvars['dissolve'] = '1';
if((($contact['rel']) && ($contact['rel'] != CONTACT_IS_SHARING) && (! $contact['blocked'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
$postvars['data'] = $atom;
$postvars['perm'] = 'rw';
}
else {
$postvars['data'] = str_replace('<dfrn:comment-allow>1','<dfrn:comment-allow>0',$atom);
$postvars['perm'] = 'r';
}
$postvars['ssl_policy'] = $ssl_policy;
if($page)
$postvars['page'] = $page;
if($rino>0 && $rino_remote_version>0 && (! $dissolve)) {
logger('rino version: '. $rino_remote_version);
switch($rino_remote_version) {
case 1:
// Deprecated rino version!
$key = substr(random_string(),0,16);
$data = aes_encrypt($postvars['data'],$key);
break;
case 2:
// RINO 2 based on php-encryption
try {
$key = Crypto::createNewRandomKey();
} catch (CryptoTestFailed $ex) {
logger('Cannot safely create a key');
return -1;
} catch (CannotPerformOperation $ex) {
logger('Cannot safely create a key');
return -1;
}
try {
$data = Crypto::encrypt($postvars['data'], $key);
} catch (CryptoTestFailed $ex) {
logger('Cannot safely perform encryption');
return -1;
} catch (CannotPerformOperation $ex) {
logger('Cannot safely perform encryption');
return -1;
}
break;
default:
logger("rino: invalid requested verision '$rino_remote_version'");
return -1;
}
$postvars['rino'] = $rino_remote_version;
$postvars['data'] = bin2hex($data);
#logger('rino: sent key = ' . $key, LOGGER_DEBUG);
if($dfrn_version >= 2.1) {
if(($contact['duplex'] && strlen($contact['pubkey']))
|| ($owner['page-flags'] == PAGE_COMMUNITY && strlen($contact['pubkey']))
|| ($contact['rel'] == CONTACT_IS_SHARING && strlen($contact['pubkey']))) {
openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
}
else {
openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
}
}
else {
if(($contact['duplex'] && strlen($contact['prvkey'])) || ($owner['page-flags'] == PAGE_COMMUNITY)) {
openssl_private_encrypt($key,$postvars['key'],$contact['prvkey']);
}
else {
openssl_public_encrypt($key,$postvars['key'],$contact['pubkey']);
}
}
logger('md5 rawkey ' . md5($postvars['key']));
$postvars['key'] = bin2hex($postvars['key']);
}
logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);
$xml = post_url($contact['notify'],$postvars);
logger('dfrn_deliver: ' . "RECEIVED: " . $xml, LOGGER_DATA);
$curl_stat = $a->get_curl_code();
if((! $curl_stat) || (! strlen($xml)))
return(-1); // timed out
if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after')))
return(-1);
if(strpos($xml,'<?xml') === false) {
logger('dfrn_deliver: phase 2: no valid XML returned');
logger('dfrn_deliver: phase 2: returned XML: ' . $xml, LOGGER_DATA);
return 3;
}
if($contact['term-date'] != '0000-00-00 00:00:00') {
logger("dfrn_deliver: $url back from the dead - removing mark for death");
require_once('include/Contact.php');
unmark_for_death($contact);
}
$res = parse_xml_string($xml);
return $res->status;
}
/* /*
This function returns true if $update has an edited timestamp newer This function returns true if $update has an edited timestamp newer
than $existing, i.e. $update contains new data which should override than $existing, i.e. $update contains new data which should override

View file

@ -1,6 +1,7 @@
<?php <?php
require_once("boot.php"); require_once("boot.php");
require_once('include/queue_fn.php'); require_once('include/queue_fn.php');
require_once('include/dfrn.php');
function queue_run(&$argv, &$argc){ function queue_run(&$argv, &$argc){
global $a, $db; global $a, $db;
@ -179,7 +180,7 @@ function queue_run(&$argv, &$argc){
switch($contact['network']) { switch($contact['network']) {
case NETWORK_DFRN: case NETWORK_DFRN:
logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>'); logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = dfrn_deliver($owner,$contact,$data); $deliver_status = dfrn::deliver($owner,$contact,$data);
if($deliver_status == (-1)) { if($deliver_status == (-1)) {
update_queue_time($q_item['id']); update_queue_time($q_item['id']);

View file

@ -44,7 +44,7 @@ function dfrn_poll_init(&$a) {
logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user); logger('dfrn_poll: public feed request from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $user);
header("Content-type: application/atom+xml"); header("Content-type: application/atom+xml");
echo dfrn_feed($a, '', $user,$last_update); echo dfrn::feed('', $user,$last_update);
killme(); killme();
} }
@ -371,7 +371,7 @@ function dfrn_poll_post(&$a) {
} }
header("Content-type: application/atom+xml"); header("Content-type: application/atom+xml");
$o = dfrn_feed($a,$dfrn_id, $a->argv[1], $last_update, $direction); $o = dfrn::feed($dfrn_id, $a->argv[1], $last_update, $direction);
echo $o; echo $o;
killme(); killme();