]> git.mxchange.org Git - friendica.git/blobdiff - mod/fetch.php
New item field "Post-type" and new table "permissionset" (#5408)
[friendica.git] / mod / fetch.php
index 457fc86a6d071e5504a2ed7c7e02c762d37c9937..5f1b24a79209f181c2a8e01a493c1c73bb5601cb 100644 (file)
@@ -2,87 +2,64 @@
 /*
 This file is part of the Diaspora protocol. It is used for fetching single public posts.
 */
-require_once("include/diaspora.php");
 
-function fetch_init($a){
+use Friendica\App;
+use Friendica\Core\L10n;
+use Friendica\Core\System;
+use Friendica\Protocol\Diaspora;
+use Friendica\Model\Item;
+use Friendica\Model\User;
+use Friendica\Util\XML;
+use Friendica\Database\DBM;
 
-       if (($a->argc != 3) OR (!in_array($a->argv[1], array("post", "status_message", "reshare")))) {
-               header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
+function fetch_init(App $a)
+{
+
+       if (($a->argc != 3) || (!in_array($a->argv[1], ["post", "status_message", "reshare"]))) {
+               header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
                killme();
        }
 
        $guid = $a->argv[2];
 
-       $item = q("SELECT `uid`, `title`, `body`, `guid`, `contact-id`, `private`, `created`, `app`, `location`
-                       FROM `item` WHERE `wall` AND NOT `private`  AND `guid` = '%s' AND `network` IN ('%s', '%s') AND `id` = `parent` LIMIT 1",
-               dbesc($guid), NETWORK_DFRN, NETWORK_DIASPORA);
-       if (!$item) {
-               header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
+       // Fetch the item
+       $fields = ['uid', 'title', 'body', 'guid', 'contact-id', 'private', 'created', 'app', 'location', 'coord', 'network',
+               'event-id', 'resource-id', 'author-link', 'owner-link', 'attach'];
+       $condition = ['wall' => true, 'private' => false, 'guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
+       $item = Item::selectFirst($fields, $condition);
+       if (!DBM::is_result($item)) {
+               $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
+               $item = Item::selectFirst(['author-link'], $condition);
+               if (DBM::is_result($item)) {
+                       $parts = parse_url($item["author-link"]);
+                       $host = $parts["scheme"]."://".$parts["host"];
+
+                       if (normalise_link($host) != normalise_link(System::baseUrl())) {
+                               $location = $host."/fetch/".$a->argv[1]."/".urlencode($guid);
+
+                               header("HTTP/1.1 301 Moved Permanently");
+                               header("Location:".$location);
+                               killme();
+                       }
+               }
+
+               header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
                killme();
        }
-       $post = array();
-
-       $reshared = diaspora::is_reshare($item[0]["body"]);
-
-       if ($reshared) {
-               $nodename = "reshare";
-               $post["root_diaspora_id"] = $reshared["root_handle"];
-               $post["root_guid"] = $reshared["root_guid"];
-               $post["guid"] = $item[0]["guid"];
-               $post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
-               $post["public"] = (!$item[0]["private"] ? 'true':'false');
-               $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
-       } else {
-               $body = bb2diaspora($item[0]["body"]);
-
-               if(strlen($item[0]["title"]))
-                       $body = "## ".html_entity_decode($item[0]["title"])."\n\n".$body;
-
-               $nodename = "status_message";
-               $post["raw_message"] = str_replace("&", "&", $body);
-               $post["location"] = $item[0]["location"];
-               $post["guid"] = $item[0]["guid"];
-               $post["diaspora_handle"] = diaspora::handle_from_contact($item[0]["contact-id"]);
-               $post["public"] = (!$item[0]["private"] ? 'true':'false');
-               $post["created_at"] = datetime_convert('UTC','UTC',$item[0]["created"]);
-               $post["provider_display_name"] = $item[0]["app"];
-       }
 
-       $data = array("XML" => array("post" => array($nodename => $post)));
-       $xml = xml::from_array($data, $xmlobj);
-
-       $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d", intval($item[0]["uid"]));
-       if (!$r) {
-               header($_SERVER["SERVER_PROTOCOL"].' 404 '.t('Not Found'));
+       // Fetch some data from the author (We could combine both queries - but I think this is more readable)
+       $user = User::getOwnerDataById($item["uid"]);
+       if (!$user) {
+               header($_SERVER["SERVER_PROTOCOL"].' 404 '.L10n::t('Not Found'));
                killme();
        }
 
-       $user = $r[0];
-
-       $key_id = "";
-
-       $b64url_data = base64url_encode($xml);
-       $data = str_replace(array("\n", "\r", " ", "\t"), array("", "", "", ""), $b64url_data);
-
-       $type = "application/xml";
-       $encoding = "base64url";
-       $alg = "RSA-SHA256";
-       $signable_data = $data.".".base64url_encode($type).".".base64url_encode($encoding).".".base64url_encode($alg);
-       $signature = rsa_sign($signable_data, $user["prvkey"]);
-       $sig = base64url_encode($signature);
-
-       $xmldata = array("me:env" => array("me:data" => $data,
-                                                       "@attributes" => array("type" => $type),
-                                                       "me:encoding" => $encoding,
-                                                       "me:alg" => $alg,
-                                                       "me:sig" => $sig,
-                                                       "@attributes2" => array("key_id" => $key_id)));
-
-       $namespaces = array("me" => "http://salmon-protocol.org/ns/magic-env");
+       $status = Diaspora::buildStatus($item, $user);
+       $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
 
-       $envelope = xml::from_array($xmldata, $xml, false, $namespaces);
-       header("Content-Type: application/xml; charset=utf-8");
-       echo $envelope;
+       // Send the envelope
+       header("Content-Type: application/magic-envelope+xml; charset=utf-8");
+       echo Diaspora::buildMagicEnvelope($xml, $user);
 
        killme();
 }