X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fdfrn.php;h=09688586a3599fe19214c01f88ae0564a0d52321;hb=a941199760420298d5977b285e2016d860b1e073;hp=1e6cc6e71daab1a4de67628272feb38c9ac32e92;hpb=66366b2804fb3f94016663b107a32993ae084dfa;p=friendica.git diff --git a/include/dfrn.php b/include/dfrn.php index 1e6cc6e71d..09688586a3 100644 --- a/include/dfrn.php +++ b/include/dfrn.php @@ -216,13 +216,10 @@ class dfrn { dbesc($sort) ); - if (!dbm::is_result($r)) { - /// @TODO Some logging? - killme(); - } - - // Will check further below if this actually returned results. - // We will provide an empty feed if that is the case. + /* + * Will check further below if this actually returned results. + * We will provide an empty feed if that is the case. + */ $items = $r; @@ -243,10 +240,10 @@ class dfrn { $root = self::add_header($doc, $owner, $author, $alternatelink, true); - // This hook can't work anymore + /// @TODO This hook can't work anymore // call_hooks('atom_feed', $atom); - if (!count($items) OR $onlyheader) { + if (!dbm::is_result($items) OR $onlyheader) { $atom = trim($doc->saveXML()); call_hooks('atom_feed_end', $atom); @@ -557,7 +554,7 @@ class dfrn { xml::add_element($doc, $author, "poco:displayName", $profile["name"]); xml::add_element($doc, $author, "poco:updated", $namdate); - if (trim($profile["dob"]) != "0000-00-00") + if (trim($profile["dob"]) > '0001-01-01') xml::add_element($doc, $author, "poco:birthday", "0000-".date("m-d", strtotime($profile["dob"]))); xml::add_element($doc, $author, "poco:note", $profile["about"]); @@ -918,12 +915,7 @@ class dfrn { intval($owner["uid"]), dbesc(normalise_link($mention))); - if (!dbm::is_result($r)) { - /// @TODO Maybe some logging? - killme(); - } - - if ($r[0]["forum"] OR $r[0]["prv"]) { + if (dbm::is_result($r) AND ($r[0]["forum"] OR $r[0]["prv"])) { xml::add_element($doc, $entry, "link", "", array("rel" => "mentioned", "ostatus:object-type" => ACTIVITY_OBJ_GROUP, "href" => $mention)); @@ -939,6 +931,30 @@ class dfrn { return $entry; } + /** + * @brief encrypts data via AES + * + * @param string $data The data that is to be encrypted + * @param string $key The AES key + * + * @return string encrypted data + */ + private static function aes_encrypt($data, $key) { + return openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA); + } + + /** + * @brief decrypts data via AES + * + * @param string $encrypted The encrypted data + * @param string $key The AES key + * + * @return string decrypted data + */ + public static function aes_decrypt($encrypted, $key) { + return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA); + } + /** * @brief Delivers the atom content to the contacts * @@ -963,20 +979,15 @@ class dfrn { $idtosend = '1:' . $orig_id; } - $rino = get_config('system','rino_encrypt'); + $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){ + switch ($ssl_val) { case SSL_POLICY_FULL: $ssl_policy = 'full'; break; @@ -1095,14 +1106,14 @@ class dfrn { } - if ($rino>0 && $rino_remote_version>0 && (! $dissolve)) { + if ($rino > 0 && $rino_remote_version > 0 && (! $dissolve)) { logger('rino version: '. $rino_remote_version); - switch($rino_remote_version) { + switch ($rino_remote_version) { case 1: // Deprecated rino version! - $key = substr(random_string(),0,16); - $data = aes_encrypt($postvars['data'],$key); + $key = openssl_random_pseudo_bytes(16); + $data = self::aes_encrypt($postvars['data'], $key); break; case 2: // RINO 2 based on php-encryption @@ -1137,10 +1148,9 @@ class dfrn { 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']))) - + 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']); @@ -1298,19 +1308,19 @@ class dfrn { } if (dbm::is_result($r) AND !$onlyfetch) { - logger("Check if contact details for contact ".$r[0]["id"]." (".$r[0]["nick"].") have to be updated.", LOGGER_DEBUG); + logger("Check if contact details for contact " . $r[0]["id"] . " (" . $r[0]["nick"] . ") have to be updated.", LOGGER_DEBUG); $poco = array("url" => $contact["url"]); // When was the last change to name or uri? - $name_element = $xpath->query($element."/atom:name", $context)->item(0); + $name_element = $xpath->query($element . "/atom:name", $context)->item(0); foreach ($name_element->attributes AS $attributes) { if ($attributes->name == "updated") { $poco["name-date"] = $attributes->textContent; } } - $link_element = $xpath->query($element."/atom:link", $context)->item(0); + $link_element = $xpath->query($element . "/atom:link", $context)->item(0); foreach ($link_element->attributes AS $attributes) { if ($attributes->name == "updated") { $poco["uri-date"] = $attributes->textContent; @@ -1318,33 +1328,33 @@ class dfrn { } // Update contact data - $value = $xpath->evaluate($element."/dfrn:handle/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/dfrn:handle/text()", $context)->item(0)->nodeValue; if ($value != "") { $poco["addr"] = $value; } - $value = $xpath->evaluate($element."/poco:displayName/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/poco:displayName/text()", $context)->item(0)->nodeValue; if ($value != "") { $poco["name"] = $value; } - $value = $xpath->evaluate($element."/poco:preferredUsername/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/poco:preferredUsername/text()", $context)->item(0)->nodeValue; if ($value != "") { $poco["nick"] = $value; } - $value = $xpath->evaluate($element."/poco:note/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/poco:note/text()", $context)->item(0)->nodeValue; if ($value != "") { $poco["about"] = $value; } - $value = $xpath->evaluate($element."/poco:address/poco:formatted/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/poco:address/poco:formatted/text()", $context)->item(0)->nodeValue; if ($value != "") { $poco["location"] = $value; } /// @todo Only search for elements with "poco:type" = "xmpp" - $value = $xpath->evaluate($element."/poco:ims/poco:value/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/poco:ims/poco:value/text()", $context)->item(0)->nodeValue; if ($value != "") { $poco["xmpp"] = $value; } @@ -1357,9 +1367,9 @@ class dfrn { /// - poco:country // If the "hide" element is present then the profile isn't searchable. - $hide = intval($xpath->evaluate($element."/dfrn:hide/text()", $context)->item(0)->nodeValue == "true"); + $hide = intval($xpath->evaluate($element . "/dfrn:hide/text()", $context)->item(0)->nodeValue == "true"); - logger("Hidden status for contact ".$contact["url"].": ".$hide, LOGGER_DEBUG); + logger("Hidden status for contact " . $contact["url"] . ": " . $hide, LOGGER_DEBUG); // If the contact isn't searchable then set the contact to "hidden". // Problem: This can be manually overridden by the user. @@ -1369,7 +1379,7 @@ class dfrn { // Save the keywords into the contact table $tags = array(); - $tagelements = $xpath->evaluate($element."/poco:tags/text()", $context); + $tagelements = $xpath->evaluate($element . "/poco:tags/text()", $context); foreach ($tagelements AS $tag) { $tags[$tag->nodeValue] = $tag->nodeValue; } @@ -1381,7 +1391,7 @@ class dfrn { // "dfrn:birthday" contains the birthday converted to UTC $old_bdyear = $contact["bdyear"]; - $birthday = $xpath->evaluate($element."/dfrn:birthday/text()", $context)->item(0)->nodeValue; + $birthday = $xpath->evaluate($element . "/dfrn:birthday/text()", $context)->item(0)->nodeValue; if (strtotime($birthday) > time()) { $bd_timestamp = strtotime($birthday); @@ -1390,9 +1400,9 @@ class dfrn { } // "poco:birthday" is the birthday in the format "yyyy-mm-dd" - $value = $xpath->evaluate($element."/poco:birthday/text()", $context)->item(0)->nodeValue; + $value = $xpath->evaluate($element . "/poco:birthday/text()", $context)->item(0)->nodeValue; - if (!in_array($value, array("", "0000-00-00"))) { + if (!in_array($value, array("", "0000-00-00", "0001-01-01"))) { $bdyear = date("Y"); $value = str_replace("0000", $bdyear, $value); @@ -1427,20 +1437,20 @@ class dfrn { $datefields = array("name-date", "uri-date"); foreach ($datefields AS $field) { if (strtotime($contact[$field]) > strtotime($r[0][$field])) { - logger("Difference for contact ".$contact["id"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG); + logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $r[0][$field] . "'", LOGGER_DEBUG); $update = true; } } foreach ($fields AS $field => $data) { if ($contact[$field] != $r[0][$field]) { - logger("Difference for contact ".$contact["id"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG); + logger("Difference for contact " . $contact["id"] . " in field '" . $field . "'. New value: '" . $contact[$field] . "', old value '" . $r[0][$field] . "'", LOGGER_DEBUG); $update = true; } } if ($update) { - logger("Update contact data for contact ".$contact["id"]." (".$contact["nick"].")", LOGGER_DEBUG); + logger("Update contact data for contact " . $contact["id"] . " (" . $contact["nick"] . ")", LOGGER_DEBUG); q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `about` = '%s', `location` = '%s', `addr` = '%s', `keywords` = '%s', `bdyear` = '%s', `bd` = '%s', `hidden` = %d, @@ -1456,16 +1466,20 @@ class dfrn { update_contact_avatar($author["avatar"], $importer["uid"], $contact["id"], (strtotime($contact["avatar-date"]) > strtotime($r[0]["avatar-date"]))); - // The generation is a sign for the reliability of the provided data. - // It is used in the socgraph.php to prevent that old contact data - // that was relayed over several servers can overwrite contact - // data that we received directly. + /* + * The generation is a sign for the reliability of the provided data. + * It is used in the socgraph.php to prevent that old contact data + * that was relayed over several servers can overwrite contact + * data that we received directly. + */ $poco["generation"] = 2; $poco["photo"] = $author["avatar"]; $poco["hide"] = $hide; $poco["contact-type"] = $contact["contact-type"]; - update_gcontact($poco); + $gcid = update_gcontact($poco); + + link_gcontact($gcid, $importer["uid"], $contact["id"]); } return($author); @@ -1637,7 +1651,13 @@ class dfrn { intval($fid) ); - /// @TODO Really abort on valid result??? Maybe missed ! here? + /* + * The valid result means the friend we're about to send a friend + * suggestion already has them in their contact, which means no further + * action is required. + * + * @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246 + */ if (dbm::is_result($r)) { return false; } @@ -1656,7 +1676,10 @@ class dfrn { dbesc($suggest["request"]) ); - // This way a useless else-block is being spared, resulting in easier code. + /* + * If no record in fcontact is found, below INSERT statement will not + * link an introduction to it. + */ if (!dbm::is_result($r)) { // database record did not get created. Quietly give up. killme(); @@ -1740,11 +1763,7 @@ class dfrn { intval($importer["importer_uid"])); if (!dbm::is_result($r)) { - /* - * @TODO maybe one day: logger("Query failed to execute, no result returned in " . __FUNCTION__); - killme(); - */ return false; } @@ -1813,7 +1832,7 @@ class dfrn { 'author-link' => array($old["url"], $relocate["url"]), //'owner-avatar' => array($old["photo"], $relocate["photo"]), //'author-avatar' => array($old["photo"], $relocate["photo"]), - ); + ); foreach ($fields as $n=>$f) { $r = q("SELECT `id` FROM `item` WHERE `%s` = '%s' AND `uid` = %d LIMIT 1", $n, dbesc($f[0]), @@ -1824,8 +1843,10 @@ class dfrn { $n, dbesc($f[1]), $n, dbesc($f[0]), intval($importer["importer_uid"])); - if ($x === false) + + if ($x === false) { return false; + } } } @@ -1844,6 +1865,7 @@ class dfrn { * @param array $item the new item record * @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? + * @todo set proper type-hints (array?) */ private static function update_content($current, $item, $importer, $entrytype) { $changed = false; @@ -1851,8 +1873,9 @@ class dfrn { if (edited_timestamp_is_newer($current, $item)) { // do not accept (ignore) an earlier edit than one we currently have. - if(datetime_convert("UTC","UTC",$item["edited"]) < $current["edited"]) - return(false); + if (datetime_convert("UTC","UTC",$item["edited"]) < $current["edited"]) { + return false; + } $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s', `tag` = '%s', `edited` = '%s', `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d", dbesc($item["title"]), @@ -1897,12 +1920,13 @@ class dfrn { * @param array $item the new item record * * @return int Is it a toplevel entry, a comment or a relayed comment? + * @todo set proper type-hints (array?) */ private static function get_entry_type($importer, $item) { if ($item["parent-uri"] != $item["uri"]) { $community = false; - if($importer["page-flags"] == PAGE_COMMUNITY || $importer["page-flags"] == PAGE_PRVGROUP) { + if ($importer["page-flags"] == PAGE_COMMUNITY || $importer["page-flags"] == PAGE_PRVGROUP) { $sql_extra = ""; $community = true; logger("possible community action"); @@ -1937,16 +1961,17 @@ class dfrn { } } - // Does this have the characteristics of a community or private group action? - // If it's an action to a wall post on a community/prvgroup page it's a - // valid community action. Also forum_mode makes it valid for sure. - // If neither, it's not. + /* + * Does this have the characteristics of a community or private group action? + * If it's an action to a wall post on a community/prvgroup page it's a + * valid community action. Also forum_mode makes it valid for sure. + * If neither, it's not. + */ - if ($is_a_remote_action && $community) { - if ((!$r[0]["forum_mode"]) && (!$r[0]["wall"])) { - $is_a_remote_action = false; - logger("not a community action"); - } + /// @TODO Maybe merge these if() blocks into one? + if ($is_a_remote_action && $community && (!$r[0]["forum_mode"]) && (!$r[0]["wall"])) { + $is_a_remote_action = false; + logger("not a community action"); } if ($is_a_remote_action) { @@ -1954,7 +1979,6 @@ class dfrn { } else { return DFRN_REPLY; } - } else { return DFRN_TOP_LEVEL; } @@ -1967,6 +1991,7 @@ class dfrn { * @param array $item the new item record * @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 + * @todo set proper type-hints (array?) */ private static function do_poke($item, $importer, $posted_id) { $verb = urldecode(substr($item["verb"],strpos($item["verb"], "#")+1)); @@ -1980,7 +2005,7 @@ class dfrn { // somebody was poked/prodded. Was it me? foreach ($xo->link as $l) { $atts = $l->attributes(); - switch($atts["rel"]) { + switch ($atts["rel"]) { case "alternate": $Blink = $atts["href"]; break; @@ -1989,7 +2014,7 @@ class dfrn { } } - if($Blink && link_compare($Blink,App::get_baseurl()."/profile/".$importer["nickname"])) { + if ($Blink && link_compare($Blink, App::get_baseurl() . "/profile/" . $importer["nickname"])) { // send a notification notification(array( @@ -2023,6 +2048,7 @@ class dfrn { * @param bool $is_like Is the verb a "like"? * * @return bool Should the processing of the entries be continued? + * @todo set proper type-hints (array?) */ private static function process_verbs($entrytype, $importer, &$item, &$is_like) { @@ -2103,7 +2129,8 @@ class dfrn { ); if (!dbm::is_result($r)) { - killme(); + logger("Query failed to execute, no result returned in " . __FUNCTION__); + return false; } // extract tag, if not duplicate, add to parent item @@ -2127,7 +2154,7 @@ class dfrn { * * @param object $links link elements * @param array $item the item record - * @todo Add type-hints + * @todo set proper type-hints */ private static function parse_links($links, &$item) { $rel = ""; @@ -2154,8 +2181,8 @@ class dfrn { $title = $attributes->textContent; } } - if (($rel != "") AND ($href != "")) - switch($rel) { + if (($rel != "") AND ($href != "")) { + switch ($rel) { case "alternate": $item["plink"] = $href; break; @@ -2165,9 +2192,10 @@ class dfrn { $item["attach"] .= ","; } - $item["attach"] .= '[attach]href="'.$href.'" length="'.$length.'" type="'.$type.'" title="'.$title.'"[/attach]'; + $item["attach"] .= '[attach]href="' . $href . '" length="' . $length . '" type="' . $type . '" title="' . $title . '"[/attach]'; break; } + } } } @@ -2486,10 +2514,12 @@ class dfrn { } } else { // $entrytype == DFRN_TOP_LEVEL if (!link_compare($item["owner-link"],$importer["url"])) { - // The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, - // but otherwise there's a possible data mixup on the sender's system. - // the tgroup delivery code called from item_store will correct it if it's a forum, - // but we're going to unconditionally correct it here so that the post will always be owned by our contact. + /* + * The item owner info is not our contact. It's OK and is to be expected if this is a tgroup delivery, + * but otherwise there's a possible data mixup on the sender's system. + * the tgroup delivery code called from item_store will correct it if it's a forum, + * but we're going to unconditionally correct it here so that the post will always be owned by our contact. + */ logger('Correcting item owner.', LOGGER_DEBUG); $item["owner-name"] = $importer["senderName"]; $item["owner-link"] = $importer["url"]; @@ -2520,6 +2550,7 @@ class dfrn { * @param object $xpath XPath object * @param object $deletion deletion elements * @param array $importer Record of the importer user mixed with contact of the content + * @todo set proper type-hints */ private static function process_deletion($xpath, $deletion, $importer) { @@ -2551,7 +2582,7 @@ class dfrn { intval($importer["id"]) ); if (!dbm::is_result($r)) { - logger("Item with uri ".$uri." from contact ".$importer["id"]." for user ".$importer["uid"]." wasn't found.", LOGGER_DEBUG); + logger("Item with uri " . $uri . " from contact " . $importer["id"] . " for user " . $importer["uid"] . " wasn't found.", LOGGER_DEBUG); return; } else { @@ -2602,7 +2633,7 @@ class dfrn { } } q("UPDATE `item` SET `tag` = '%s' WHERE `id` = %d", - dbesc(implode(',',$newtags)), + dbesc(implode(',', $newtags)), intval($i[0]["id"]) ); create_tags_from_item($i[0]["id"]); @@ -2611,7 +2642,7 @@ class dfrn { } } - if($entrytype == DFRN_TOP_LEVEL) { + if ($entrytype == DFRN_TOP_LEVEL) { $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', `changed` = '%s', `body` = '', `title` = '' WHERE `parent-uri` = '%s' AND `uid` = %d", @@ -2657,7 +2688,7 @@ class dfrn { // if this is a relayed delete, propagate it to other recipients if ($entrytype == DFRN_REPLY_RC) { - logger("Notifying followers about deletion of post ".$item["id"], LOGGER_DEBUG); + logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG); proc_run(PRIORITY_HIGH, "include/notifier.php","drop", $item["id"]); } } @@ -2670,6 +2701,7 @@ class dfrn { * @param text $xml The DFRN message * @param array $importer Record of the importer user mixed with contact of the content * @param bool $sort_by_date Is used when feeds are polled + * @todo set proper type-hints */ public static function import($xml,$importer, $sort_by_date = false) { @@ -2785,6 +2817,6 @@ class dfrn { self::process_entry($header, $xpath, $entry, $importer); } } - logger("Import done for user ".$importer["uid"]." from contact ".$importer["id"], LOGGER_DEBUG); + logger("Import done for user " . $importer["uid"] . " from contact " . $importer["id"], LOGGER_DEBUG); } }