X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdfrn.php;h=30057001cd45de059cc248c8edbbc5ad27d02c4c;hb=95bb8d38e87d8b09ef37404c4b23c08b6feb9da4;hp=27be0c32a24c7e74312054b9f8edb4a0514a61b5;hpb=63610c7b73fcaf862adbf880d4968dd2b1678d9b;p=friendica.git diff --git a/include/dfrn.php b/include/dfrn.php index 27be0c32a2..30057001cd 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -105,8 +105,9 @@ class dfrn { dbesc($owner_nick) ); - if(! dbm::is_result($r)) + if (! dbm::is_result($r)) { killme(); + } $owner = $r[0]; $owner_id = $owner['uid']; @@ -139,8 +140,9 @@ class dfrn { intval($owner_id) ); - if(! dbm::is_result($r)) + if (! dbm::is_result($r)) { killme(); + } $contact = $r[0]; require_once('include/security.php'); @@ -192,7 +194,7 @@ class dfrn { `contact`.`name-date`, `contact`.`uri-date`, `contact`.`avatar-date`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, `sign`.`signed_text`, `sign`.`signature`, `sign`.`signer` - FROM `item` USE INDEX (`uid_wall_changed`, `uid_type_changed`) $sql_post_table + FROM `item` USE INDEX (`uid_wall_changed`) $sql_post_table STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND (NOT `contact`.`blocked` OR `contact`.`pending`) LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` @@ -389,7 +391,7 @@ class dfrn { * * @return object XML root object */ - private function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) { + private static function add_header($doc, $owner, $authorelement, $alternatelink = "", $public = false) { if ($alternatelink == "") $alternatelink = $owner['url']; @@ -460,7 +462,7 @@ class dfrn { * * @return object XML author object */ - private function add_author($doc, $owner, $authorelement, $public) { + private static function add_author($doc, $owner, $authorelement, $public) { // Is the profile hidden or shouldn't be published in the net? Then add the "hide" element $r = q("SELECT `id` FROM `profile` INNER JOIN `user` ON `user`.`uid` = `profile`.`uid` @@ -589,7 +591,7 @@ class dfrn { * * @return object XML author object */ - private function add_entry_author($doc, $element, $contact_url, $item) { + private static function add_entry_author($doc, $element, $contact_url, $item) { $contact = get_contact_details_by_url($contact_url, $item["uid"]); @@ -629,7 +631,7 @@ class dfrn { * * @return object XML activity object */ - private function create_activity($doc, $element, $activity) { + private static function create_activity($doc, $element, $activity) { if($activity) { $entry = $doc->createElement($element); @@ -683,7 +685,7 @@ class dfrn { * * @return object XML attachment object */ - private function get_attachment($doc, $root, $item) { + private static function get_attachment($doc, $root, $item) { $arr = explode('[/attach],',$item['attach']); if(count($arr)) { foreach($arr as $r) { @@ -718,7 +720,7 @@ class dfrn { * * @return object XML entry object */ - private function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) { + private static function entry($doc, $type, $item, $owner, $comment = false, $cid = 0) { $mentioned = array(); @@ -911,11 +913,17 @@ class dfrn { logger('dfrn_deliver: ' . $url); - $xml = fetch_url($url); + $ret = z_fetch_url($url); + + if ($ret['errno'] == CURLE_OPERATION_TIMEDOUT) { + return -2; // timed out + } + + $xml = $ret['body']; $curl_stat = $a->get_curl_code(); if(! $curl_stat) - return(-1); // timed out + return -3; // timed out logger('dfrn_deliver: ' . $xml, LOGGER_DATA); @@ -1015,24 +1023,24 @@ class dfrn { $key = Crypto::createNewRandomKey(); } catch (CryptoTestFailed $ex) { logger('Cannot safely create a key'); - return -1; + return -4; } catch (CannotPerformOperation $ex) { logger('Cannot safely create a key'); - return -1; + return -5; } try { $data = Crypto::encrypt($postvars['data'], $key); } catch (CryptoTestFailed $ex) { logger('Cannot safely perform encryption'); - return -1; + return -6; } catch (CannotPerformOperation $ex) { logger('Cannot safely perform encryption'); - return -1; + return -7; } break; default: logger("rino: invalid requested verision '$rino_remote_version'"); - return -1; + return -8; } $postvars['rino'] = $rino_remote_version; @@ -1066,16 +1074,16 @@ class dfrn { logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA); - $xml = post_url($contact['notify'],$postvars); + $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 + return -9; // timed out if(($curl_stat == 503) && (stristr($a->get_curl_headers(),'retry-after'))) - return(-1); + return -10; if(strpos($xml,'evaluate($element."/atom:name/text()", $context)->item(0)->nodeValue; @@ -1159,7 +1177,7 @@ class dfrn { // Until now we aren't serving different sizes - but maybe later $avatarlist = array(); - // @todo check if "avatar" or "photo" would be the best field in the specification + /// @todo check if "avatar" or "photo" would be the best field in the specification $avatars = $xpath->query($element."/atom:link[@rel='avatar']", $context); foreach($avatars AS $avatar) { $href = ""; @@ -1346,7 +1364,7 @@ class dfrn { * * @return string XML string */ - private function transform_activity($xpath, $activity, $element) { + private static function transform_activity($xpath, $activity, $element) { if (!is_object($activity)) return ""; @@ -1379,7 +1397,7 @@ class dfrn { $objxml = $obj_doc->saveXML($obj_element); - // @todo This isn't totally clean. We should find a way to transform the namespaces + /// @todo This isn't totally clean. We should find a way to transform the namespaces $objxml = str_replace("<".$element.' xmlns="http://www.w3.org/2005/Atom">', "<".$element.">", $objxml); return($objxml); } @@ -1391,7 +1409,7 @@ class dfrn { * @param object $mail mail elements * @param array $importer Record of the importer user mixed with contact of the content */ - private function process_mail($xpath, $mail, $importer) { + private static function process_mail($xpath, $mail, $importer) { logger("Processing mails"); @@ -1409,9 +1427,9 @@ class dfrn { $msg["seen"] = 0; $msg["replied"] = 0; - dbesc_array($msg); + dbm::esc_array($msg, true); - $r = dbq("INSERT INTO `mail` (`".implode("`, `", array_keys($msg))."`) VALUES ('".implode("', '", array_values($msg))."')"); + $r = dbq("INSERT INTO `mail` (`".implode("`, `", array_keys($msg))."`) VALUES (".implode(", ", array_values($msg)).")"); // send notifications. @@ -1442,7 +1460,7 @@ class dfrn { * @param object $suggestion suggestion elements * @param array $importer Record of the importer user mixed with contact of the content */ - private function process_suggestion($xpath, $suggestion, $importer) { + private static function process_suggestion($xpath, $suggestion, $importer) { $a = get_app(); logger("Processing suggestions"); @@ -1463,7 +1481,7 @@ class dfrn { dbesc(normalise_link($suggest["url"])), intval($suggest["uid"]) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) return false; // Do we already have an fcontact record for this person? @@ -1474,7 +1492,7 @@ class dfrn { dbesc($suggest["name"]), dbesc($suggest["request"]) ); - if(dbm::is_result($r)) { + if (dbm::is_result($r)) { $fid = $r[0]["id"]; // OK, we do. Do we already have an introduction for this person ? @@ -1482,7 +1500,7 @@ class dfrn { intval($suggest["uid"]), intval($fid) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) return false; } if(!$fid) @@ -1497,7 +1515,7 @@ class dfrn { dbesc($suggest["name"]), dbesc($suggest["request"]) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) $fid = $r[0]["id"]; else // database record did not get created. Quietly give up. @@ -1525,7 +1543,7 @@ class dfrn { "to_email" => $importer["email"], "uid" => $importer["importer_uid"], "item" => $suggest, - "link" => $a->get_baseurl()."/notifications/intros", + "link" => App::get_baseurl()."/notifications/intros", "source_name" => $importer["name"], "source_link" => $importer["url"], "source_photo" => $importer["photo"], @@ -1544,7 +1562,7 @@ class dfrn { * @param object $relocation relocation elements * @param array $importer Record of the importer user mixed with contact of the content */ - private function process_relocation($xpath, $relocation, $importer) { + private static function process_relocation($xpath, $relocation, $importer) { logger("Processing relocations"); @@ -1673,7 +1691,7 @@ class dfrn { * @param array $importer Record of the importer user mixed with contact of the content * @param int $entrytype Is it a toplevel entry, a comment or a relayed comment? */ - private function update_content($current, $item, $importer, $entrytype) { + private static function update_content($current, $item, $importer, $entrytype) { $changed = false; if (edited_timestamp_is_newer($current, $item)) { @@ -1725,7 +1743,7 @@ class dfrn { * * @return int Is it a toplevel entry, a comment or a relayed comment? */ - private function get_entry_type($importer, $item) { + private static function get_entry_type($importer, $item) { if ($item["parent-uri"] != $item["uri"]) { $community = false; @@ -1746,7 +1764,7 @@ class dfrn { LIMIT 1", dbesc($item["parent-uri"]) ); - if(dbm::is_result($r)) { + if (dbm::is_result($r)) { $r = q("SELECT `item`.`forum_mode`, `item`.`wall` FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` WHERE `item`.`uri` = '%s' AND (`item`.`parent-uri` = '%s' OR `item`.`thr-parent` = '%s') @@ -1758,7 +1776,7 @@ class dfrn { dbesc($r[0]["parent-uri"]), intval($importer["importer_uid"]) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) $is_a_remote_action = true; } @@ -1791,9 +1809,7 @@ class dfrn { * @param array $importer Record of the importer user mixed with contact of the content * @param int $posted_id The record number of item record that was just posted */ - private function do_poke($item, $importer, $posted_id) { - $a = get_app(); - + private static function do_poke($item, $importer, $posted_id) { $verb = urldecode(substr($item["verb"],strpos($item["verb"], "#")+1)); if(!$verb) return; @@ -1813,7 +1829,7 @@ class dfrn { } } - if($Blink && link_compare($Blink,$a->get_baseurl()."/profile/".$importer["nickname"])) { + if($Blink && link_compare($Blink,App::get_baseurl()."/profile/".$importer["nickname"])) { // send a notification notification(array( @@ -1824,7 +1840,7 @@ class dfrn { "to_email" => $importer["email"], "uid" => $importer["importer_uid"], "item" => $item, - "link" => $a->get_baseurl()."/display/".urlencode(get_item_guid($posted_id)), + "link" => App::get_baseurl()."/display/".urlencode(get_item_guid($posted_id)), "source_name" => stripslashes($item["author-name"]), "source_link" => $item["author-link"], "source_photo" => ((link_compare($item["author-link"],$importer["url"])) @@ -1848,7 +1864,7 @@ class dfrn { * * @return bool Should the processing of the entries be continued? */ - private function process_verbs($entrytype, $importer, &$item, &$is_like) { + private static function process_verbs($entrytype, $importer, &$item, &$is_like) { logger("Process verb ".$item["verb"]." and object-type ".$item["object-type"]." for entrytype ".$entrytype, LOGGER_DEBUG); @@ -1898,7 +1914,7 @@ class dfrn { dbesc($item["verb"]), dbesc($item["parent-uri"]) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) return false; $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1", @@ -1907,7 +1923,7 @@ class dfrn { dbesc($item["verb"]), dbesc($item["parent-uri"]) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) return false; } else $is_like = false; @@ -1923,7 +1939,7 @@ class dfrn { intval($importer["importer_uid"]) ); - if(!dbm::is_result($r)) + if (!dbm::is_result($r)) return false; // extract tag, if not duplicate, add to parent item @@ -1948,7 +1964,7 @@ class dfrn { * @param object $links link elements * @param array $item the item record */ - private function parse_links($links, &$item) { + private static function parse_links($links, &$item) { $rel = ""; $href = ""; $type = ""; @@ -1991,7 +2007,7 @@ class dfrn { * @param object $entry entry elements * @param array $importer Record of the importer user mixed with contact of the content */ - private function process_entry($header, $xpath, $entry, $importer) { + private static function process_entry($header, $xpath, $entry, $importer) { logger("Processing entries"); @@ -2000,6 +2016,20 @@ class dfrn { // Get the uri $item["uri"] = $xpath->query("atom:id/text()", $entry)->item(0)->nodeValue; + $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue; + + $current = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + dbesc($item["uri"]), + intval($importer["importer_uid"]) + ); + + // Is there an existing item? + if (dbm::is_result($current) AND edited_timestamp_is_newer($current[0], $item) AND + (datetime_convert("UTC","UTC",$item["edited"]) < $current[0]["edited"])) { + logger("Item ".$item["uri"]." already existed.", LOGGER_DEBUG); + return; + } + // Fetch the owner $owner = self::fetchauthor($xpath, $entry, $importer, "dfrn:owner", true); @@ -2017,7 +2047,6 @@ class dfrn { $item["title"] = $xpath->query("atom:title/text()", $entry)->item(0)->nodeValue; $item["created"] = $xpath->query("atom:published/text()", $entry)->item(0)->nodeValue; - $item["edited"] = $xpath->query("atom:updated/text()", $entry)->item(0)->nodeValue; $item["body"] = $xpath->query("dfrn:env/text()", $entry)->item(0)->nodeValue; $item["body"] = str_replace(array(' ',"\t","\r","\n"), array('','','',''),$item["body"]); @@ -2195,7 +2224,7 @@ class dfrn { dbesc($item["uri"]), intval($importer["uid"]) ); - if(dbm::is_result($r)) + if (dbm::is_result($r)) $ev["id"] = $r[0]["id"]; $event_id = event_store($ev); @@ -2205,18 +2234,13 @@ class dfrn { } } - $r = q("SELECT `id`, `uid`, `last-child`, `edited`, `body` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", - dbesc($item["uri"]), - intval($importer["importer_uid"]) - ); - if (!self::process_verbs($entrytype, $importer, $item, $is_like)) { logger("Exiting because 'process_verbs' told us so", LOGGER_DEBUG); return; } // Update content if 'updated' changes - if(dbm::is_result($r)) { + if (dbm::is_result($current)) { if (self::update_content($r[0], $item, $importer, $entrytype)) logger("Item ".$item["uri"]." was updated.", LOGGER_DEBUG); else @@ -2238,7 +2262,7 @@ class dfrn { intval($posted_id), intval($importer["importer_uid"]) ); - if(dbm::is_result($r)) { + if (dbm::is_result($r)) { $parent = $r[0]["parent"]; $parent_uri = $r[0]["parent-uri"]; } @@ -2301,7 +2325,7 @@ class dfrn { * @param object $deletion deletion elements * @param array $importer Record of the importer user mixed with contact of the content */ - private function process_deletion($xpath, $deletion, $importer) { + private static function process_deletion($xpath, $deletion, $importer) { logger("Processing deletions"); @@ -2326,7 +2350,7 @@ class dfrn { intval($importer["uid"]), intval($importer["id"]) ); - if(!dbm::is_result($r)) { + if (!dbm::is_result($r)) { logger("Item with uri ".$uri." from contact ".$importer["id"]." for user ".$importer["uid"]." wasn't found.", LOGGER_DEBUG); return; } else { @@ -2355,7 +2379,7 @@ class dfrn { dbesc($xt->id), intval($importer["importer_uid"]) ); - if(count($i)) { + if (dbm::is_result($i)) { // For tags, the owner cannot remove the tag on the author's copy of the post. @@ -2420,7 +2444,7 @@ class dfrn { dbesc($item["parent-uri"]), intval($importer["uid"]) ); - if(dbm::is_result($r)) { + if (dbm::is_result($r)) { q("UPDATE `item` SET `last-child` = 1 WHERE `id` = %d", intval($r[0]["id"]) );