X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdiaspora.php;h=846a89ba9ced9a50607f17a567913c9ace789b8d;hb=0dfa57948f152a90a4d8093419a2ea5ced07349c;hp=aad4f5c4405822cb6ae65add3369587d7c76d7c5;hpb=2b80bf468358f0553c2f7d6ca1a794cab80eb0e6;p=friendica.git diff --git a/include/diaspora.php b/include/diaspora.php index aad4f5c440..846a89ba9c 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -12,6 +12,8 @@ use Friendica\App; use Friendica\Core\System; use Friendica\Core\Config; use Friendica\Core\PConfig; +use Friendica\Core\Worker; +use Friendica\Network\Probe; require_once 'include/items.php'; require_once 'include/bb2diaspora.php'; @@ -40,7 +42,7 @@ class Diaspora { */ public static function relay_list() { - $serverdata = get_config("system", "relay_server"); + $serverdata = Config::get("system", "relay_server"); if ($serverdata == "") return array(); @@ -409,7 +411,7 @@ class Diaspora { */ public static function dispatch_public($msg) { - $enabled = intval(get_config("system", "diaspora_enabled")); + $enabled = intval(Config::get("system", "diaspora_enabled")); if (!$enabled) { logger("diaspora is disabled"); return false; @@ -498,6 +500,9 @@ class Diaspora { logger("Received message type ".$type." from ".$sender." for user ".$importer["uid"], LOGGER_DEBUG); switch ($type) { + case "account_migration": + return self::receiveAccountMigration($importer, $fields); + case "account_deletion": return self::receive_account_deletion($importer, $fields); @@ -735,7 +740,7 @@ class Diaspora { if (!$person || $update) { logger("create or refresh", LOGGER_DEBUG); - $r = probe_url($handle, PROBE_DIASPORA); + $r = Probe::uri($handle, NETWORK_DIASPORA); // Note that Friendica contacts will return a "Diaspora person" // if Diaspora connectivity is enabled on their server @@ -1130,7 +1135,8 @@ class Diaspora { return false; // This will work for new Diaspora servers and Friendica servers from 3.5 - $source_url = $server."/fetch/post/".$guid; + $source_url = $server."/fetch/post/".urlencode($guid); + logger("Fetch post from ".$source_url, LOGGER_DEBUG); $envelope = fetch_url($source_url); @@ -1146,7 +1152,7 @@ class Diaspora { // This will work for older Diaspora and Friendica servers if (!$x) { - $source_url = $server."/p/".$guid.".xml"; + $source_url = $server."/p/".urlencode($guid).".xml"; logger("Fetch post from ".$source_url, LOGGER_DEBUG); $x = fetch_url($source_url); @@ -1318,6 +1324,91 @@ class Diaspora { } } + /** + * @brief Receives account migration + * + * @param array $importer Array of the importer user + * @param object $data The message object + * + * @return bool Success + */ + private static function receiveAccountMigration($importer, $data) { + $old_handle = notags(unxmlify($data->author)); + $new_handle = notags(unxmlify($data->profile->author)); + $signature = notags(unxmlify($data->signature)); + + $contact = self::contact_by_handle($importer["uid"], $old_handle); + if (!$contact) { + logger("cannot find contact for sender: ".$old_handle." and user ".$importer["uid"]); + return false; + } + + logger("Got migration for ".$old_handle.", to ".$new_handle." with user ".$importer["uid"]); + + // Check signature + $signed_text = 'AccountMigration:'.$old_handle.':'.$new_handle; + $key = self::key($old_handle); + if (!rsa_verify($signed_text, $signature, $key, "sha256")) { + logger('No valid signature for migration.'); + return false; + } + + // Update the profile + self::receive_profile($importer, $data->profile); + + // change the technical stuff in contact and gcontact + $data = Probe::uri($new_handle); + if ($data['network'] == NETWORK_PHANTOM) { + logger('Account for '.$new_handle." couldn't be probed."); + return false; + } + + $fields = array('url' => $data['url'], 'nurl' => normalise_link($data['url']), + 'name' => $data['name'], 'nick' => $data['nick'], + 'addr' => $data['addr'], 'batch' => $data['batch'], + 'notify' => $data['notify'], 'poll' => $data['poll'], + 'network' => $data['network']); + + dba::update('contact', $fields, array('addr' => $old_handle)); + + $fields = array('url' => $data['url'], 'nurl' => normalise_link($data['url']), + 'name' => $data['name'], 'nick' => $data['nick'], + 'addr' => $data['addr'], 'connect' => $data['addr'], + 'notify' => $data['notify'], 'photo' => $data['photo'], + 'server_url' => $data['baseurl'], 'network' => $data['network']); + + dba::update('gcontact', $fields, array('addr' => $old_handle)); + + logger('Contacts are updated.'); + + // update items + /// @todo This is an extreme performance killer + $fields = array( + 'owner-link' => array($contact["url"], $data["url"]), + 'author-link' => array($contact["url"], $data["url"]), + ); + foreach ($fields as $n=>$f) { + $r = q("SELECT `id` FROM `item` WHERE `%s` = '%s' AND `uid` = %d LIMIT 1", + $n, dbesc($f[0]), + intval($importer["uid"])); + + if (dbm::is_result($r)) { + $x = q("UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d", + $n, dbesc($f[1]), + $n, dbesc($f[0]), + intval($importer["uid"])); + + if ($x === false) { + return false; + } + } + } + + logger('Items are updated.'); + + return true; + } + /** * @brief Processes an account deletion * @@ -1515,7 +1606,7 @@ class Diaspora { dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data))); // notify others - proc_run(PRIORITY_HIGH, "include/notifier.php", "comment-import", $message_id); + Worker::add(PRIORITY_HIGH, "notifier", "comment-import", $message_id); } return true; @@ -1825,7 +1916,7 @@ class Diaspora { dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data))); // notify others - proc_run(PRIORITY_HIGH, "include/notifier.php", "comment-import", $message_id); + Worker::add(PRIORITY_HIGH, "notifier", "comment-import", $message_id); } return true; @@ -2060,7 +2151,7 @@ class Diaspora { intval($importer["uid"]) ); - if ($r && !$r[0]["hide-friends"] && !$contact["hidden"] && intval(get_pconfig($importer["uid"], "system", "post_newfriend"))) { + if ($r && !$r[0]["hide-friends"] && !$contact["hidden"] && intval(PConfig::get($importer["uid"], "system", "post_newfriend"))) { $self = q("SELECT * FROM `contact` WHERE `self` AND `uid` = %d LIMIT 1", intval($importer["uid"]) @@ -2101,7 +2192,7 @@ class Diaspora { $i = item_store($arr); if ($i) - proc_run(PRIORITY_HIGH, "include/notifier.php", "activity", $i); + Worker::add(PRIORITY_HIGH, "notifier", "activity", $i); } } } @@ -2524,7 +2615,7 @@ class Diaspora { // Now check if the retraction needs to be relayed by us if ($parent["origin"]) { // notify others - proc_run(PRIORITY_HIGH, "include/notifier.php", "drop", $item["id"]); + Worker::add(PRIORITY_HIGH, "notifier", "drop", $item["id"]); } } @@ -2851,7 +2942,7 @@ class Diaspora { $a = get_app(); - $enabled = intval(get_config("system", "diaspora_enabled")); + $enabled = intval(Config::get("system", "diaspora_enabled")); if (!$enabled) return 200; @@ -2867,7 +2958,7 @@ class Diaspora { if (!$queue_run && was_recently_delayed($contact["id"])) { $return_code = 0; } else { - if (!intval(get_config("system", "diaspora_test"))) { + if (!intval(Config::get("system", "diaspora_test"))) { $content_type = (($public_batch) ? "application/magic-envelope+xml" : "application/json"); post_url($dest_url."/", $envelope, array("Content-Type: ".$content_type)); @@ -2963,17 +3054,18 @@ class Diaspora { * @brief sends an account migration * * @param array $owner the array of the item owner + * @param array $contact Target of the communication * @param int $uid User ID * * @return int The result of the transmission */ - public static function sendAccountMigration($owner, $uid) { + public static function sendAccountMigration($owner, $contact, $uid) { $old_handle = PConfig::get($uid, 'system', 'previous_addr'); $profile = self::createProfileData($uid); $signed_text = 'AccountMigration:'.$old_handle.':'.$profile['author']; - $signature = rsa_sign($signed_text, $owner["uprvkey"], "sha256"); + $signature = base64_encode(rsa_sign($signed_text, $owner["uprvkey"], "sha256")); $message = array("author" => $old_handle, "profile" => $profile, @@ -3478,11 +3570,11 @@ class Diaspora { // Split the signed text $signed_parts = explode(";", $signature['signed_text']); - if ($item["deleted"]) + if ($item["deleted"]) { $message = array("author" => $signature['signer'], "target_guid" => $signed_parts[0], "target_type" => $signed_parts[1]); - elseif ($item['verb'] === ACTIVITY_LIKE) + } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) { $message = array("author" => $signed_parts[4], "guid" => $signed_parts[1], "parent_guid" => $signed_parts[3], @@ -3490,7 +3582,7 @@ class Diaspora { "positive" => $signed_parts[0], "author_signature" => $signature['signature'], "parent_author_signature" => ""); - else { + } else { // Remove the comment guid $guid = array_shift($signed_parts); @@ -3525,12 +3617,13 @@ class Diaspora { */ public static function send_relay($item, $owner, $contact, $public_batch = false) { - if ($item["deleted"]) + if ($item["deleted"]) { return self::send_retraction($item, $owner, $contact, $public_batch, true); - elseif ($item['verb'] === ACTIVITY_LIKE) + } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) { $type = "like"; - else + } else { $type = "comment"; + } logger("Got relayable data ".$type." for item ".$item["guid"]." (".$item["id"].")", LOGGER_DEBUG); @@ -3597,7 +3690,7 @@ class Diaspora { if ($item['id'] == $item['parent']) { $target_type = "Post"; - } elseif ($item["verb"] == ACTIVITY_LIKE) { + } elseif (in_array($item["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE))) { $target_type = "Like"; } else { $target_type = "Comment";