]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge pull request #6446 from annando/approve
[friendica.git] / src / Protocol / Diaspora.php
index 657009e7764fef624f9500b822fca1e94b4fa91b..ed583503411799c6b0735d1fd85d21148fed1029 100644 (file)
@@ -38,9 +38,6 @@ use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use SimpleXMLElement;
 
-require_once 'include/dba.php';
-require_once 'include/items.php';
-
 /**
  * @brief This class contain functions to create and send Diaspora XML files
  *
@@ -147,7 +144,7 @@ class Diaspora
                $fields = ['batch', 'id', 'name', 'network', 'archive', 'blocked'];
 
                // Fetch the relay contact
-               $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
+               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
                        'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
                $contact = DBA::selectFirst('contact', $fields, $condition);
 
@@ -186,7 +183,7 @@ class Diaspora
 
                $fields = array_merge($fields, $network_fields);
 
-               $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
+               $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($server_url),
                        'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
 
                if (DBA::exists('contact', $condition)) {
@@ -1329,15 +1326,8 @@ class Diaspora
                        $x = false;
                }
 
-               // This will work for older Diaspora and Friendica servers
                if (!$x) {
-                       $source_url = $server."/p/".urlencode($guid).".xml";
-                       Logger::log("Fetch post from ".$source_url, Logger::DEBUG);
-
-                       $x = Network::fetchUrl($source_url);
-                       if (!$x) {
-                               return false;
-                       }
+                       return false;
                }
 
                $source_xml = XML::parseString($x);
@@ -1434,7 +1424,7 @@ class Diaspora
         */
        private static function authorContactByUrl($def_contact, $person, $uid)
        {
-               $condition = ['nurl' => normalise_link($person["url"]), 'uid' => $uid];
+               $condition = ['nurl' => Strings::normaliseLink($person["url"]), 'uid' => $uid];
                $contact = DBA::selectFirst('contact', ['id', 'network'], $condition);
                if (DBA::isResult($contact)) {
                        $cid = $contact["id"];
@@ -1506,9 +1496,9 @@ class Diaspora
         */
        private static function receiveAccountMigration(array $importer, $data)
        {
-               $old_handle = Strings::removeTags(XML::unescape($data->author));
-               $new_handle = Strings::removeTags(XML::unescape($data->profile->author));
-               $signature = Strings::removeTags(XML::unescape($data->signature));
+               $old_handle = Strings::escapeTags(XML::unescape($data->author));
+               $new_handle = Strings::escapeTags(XML::unescape($data->profile->author));
+               $signature = Strings::escapeTags(XML::unescape($data->signature));
 
                $contact = self::contactByHandle($importer["uid"], $old_handle);
                if (!$contact) {
@@ -1536,7 +1526,7 @@ class Diaspora
                        return false;
                }
 
-               $fields = ['url' => $data['url'], 'nurl' => normalise_link($data['url']),
+               $fields = ['url' => $data['url'], 'nurl' => Strings::normaliseLink($data['url']),
                                'name' => $data['name'], 'nick' => $data['nick'],
                                'addr' => $data['addr'], 'batch' => $data['batch'],
                                'notify' => $data['notify'], 'poll' => $data['poll'],
@@ -1544,7 +1534,7 @@ class Diaspora
 
                DBA::update('contact', $fields, ['addr' => $old_handle]);
 
-               $fields = ['url' => $data['url'], 'nurl' => normalise_link($data['url']),
+               $fields = ['url' => $data['url'], 'nurl' => Strings::normaliseLink($data['url']),
                                'name' => $data['name'], 'nick' => $data['nick'],
                                'addr' => $data['addr'], 'connect' => $data['addr'],
                                'notify' => $data['notify'], 'photo' => $data['photo'],
@@ -1566,7 +1556,7 @@ class Diaspora
         */
        private static function receiveAccountDeletion($data)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
+               $author = Strings::escapeTags(XML::unescape($data->author));
 
                $contacts = DBA::select('contact', ['id'], ['addr' => $author]);
                while ($contact = DBA::fetch($contacts)) {
@@ -1657,19 +1647,19 @@ class Diaspora
         */
        private static function receiveComment(array $importer, $sender, $data, $xml)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $parent_guid = Strings::removeTags(XML::unescape($data->parent_guid));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $parent_guid = Strings::escapeTags(XML::unescape($data->parent_guid));
                $text = XML::unescape($data->text);
 
                if (isset($data->created_at)) {
-                       $created_at = DateTimeFormat::utc(Strings::removeTags(XML::unescape($data->created_at)));
+                       $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
                } else {
                        $created_at = DateTimeFormat::utcNow();
                }
 
                if (isset($data->thread_parent_guid)) {
-                       $thread_parent_guid = Strings::removeTags(XML::unescape($data->thread_parent_guid));
+                       $thread_parent_guid = Strings::escapeTags(XML::unescape($data->thread_parent_guid));
                        $thr_uri = self::getUriFromGuid("", $thread_parent_guid, true);
                } else {
                        $thr_uri = "";
@@ -1774,24 +1764,24 @@ class Diaspora
         */
        private static function receiveConversationMessage(array $importer, array $contact, $data, $msg, $mesg, $conversation)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $subject = Strings::removeTags(XML::unescape($data->subject));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $subject = Strings::escapeTags(XML::unescape($data->subject));
 
                // "diaspora_handle" is the element name from the old version
                // "author" is the element name from the new version
                if ($mesg->author) {
-                       $msg_author = Strings::removeTags(XML::unescape($mesg->author));
+                       $msg_author = Strings::escapeTags(XML::unescape($mesg->author));
                } elseif ($mesg->diaspora_handle) {
-                       $msg_author = Strings::removeTags(XML::unescape($mesg->diaspora_handle));
+                       $msg_author = Strings::escapeTags(XML::unescape($mesg->diaspora_handle));
                } else {
                        return false;
                }
 
-               $msg_guid = Strings::removeTags(XML::unescape($mesg->guid));
-               $msg_conversation_guid = Strings::removeTags(XML::unescape($mesg->conversation_guid));
+               $msg_guid = Strings::escapeTags(XML::unescape($mesg->guid));
+               $msg_conversation_guid = Strings::escapeTags(XML::unescape($mesg->conversation_guid));
                $msg_text = XML::unescape($mesg->text);
-               $msg_created_at = DateTimeFormat::utc(Strings::removeTags(XML::unescape($mesg->created_at)));
+               $msg_created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($mesg->created_at)));
 
                if ($msg_conversation_guid != $guid) {
                        Logger::log("message conversation guid does not belong to the current conversation.");
@@ -1810,44 +1800,45 @@ class Diaspora
                        return false;
                }
 
-               q(
-                       "INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
-                       VALUES (%d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
-                       intval($importer["uid"]),
-                       DBA::escape($msg_guid),
-                       intval($conversation["id"]),
-                       DBA::escape($person["name"]),
-                       DBA::escape($person["photo"]),
-                       DBA::escape($person["url"]),
-                       intval($contact["id"]),
-                       DBA::escape($subject),
-                       DBA::escape($body),
-                       0,
-                       0,
-                       DBA::escape($message_uri),
-                       DBA::escape($author.":".$guid),
-                       DBA::escape($msg_created_at)
-               );
+               DBA::insert('mail', [
+                       'uid'        => $importer['uid'],
+                       'guid'       => $msg_guid,
+                       'convid'     => $conversation['id'],
+                       'from-name'  => $person['name'],
+                       'from-photo' => $person['photo'],
+                       'from-url'   => $person['url'],
+                       'contact-id' => $contact['id'],
+                       'title'      => $subject,
+                       'body'       => $body,
+                       'seen'       => 0,
+                       'reply'      => 0,
+                       'uri'        => $message_uri,
+                       'parent-uri' => $author . ':' . $guid,
+                       'created'    => $msg_created_at
+               ]);
+
+               $message_id = DBA::lastInsertId();
 
                DBA::unlock();
 
                DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
 
-               notification(
-                       [
+               notification([
                        "type" => NOTIFY_MAIL,
                        "notify_flags" => $importer["notify-flags"],
                        "language" => $importer["language"],
                        "to_name" => $importer["username"],
                        "to_email" => $importer["email"],
-                       "uid" =>$importer["uid"],
-                       "item" => ["id" => $conversation["id"], "title" => $subject, "subject" => $subject, "body" => $body],
+                       "uid" => $importer["uid"],
+                       "item" => ["id" => $message_id, "title" => $subject, "subject" => $subject, "body" => $body],
+                       "parent" => $conversation["id"],
                        "source_name" => $person["name"],
                        "source_link" => $person["url"],
                        "source_photo" => $person["photo"],
                        "verb" => ACTIVITY_POST,
-                       "otype" => "mail"]
-               );
+                       "otype" => "mail"
+               ]);
+
                return true;
        }
 
@@ -1862,11 +1853,11 @@ class Diaspora
         */
        private static function receiveConversation(array $importer, $msg, $data)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $subject = Strings::removeTags(XML::unescape($data->subject));
-               $created_at = DateTimeFormat::utc(Strings::removeTags(XML::unescape($data->created_at)));
-               $participants = Strings::removeTags(XML::unescape($data->participants));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $subject = Strings::escapeTags(XML::unescape($data->subject));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
+               $participants = Strings::escapeTags(XML::unescape($data->participants));
 
                $messages = $data->message;
 
@@ -1920,11 +1911,11 @@ class Diaspora
         */
        private static function receiveLike(array $importer, $sender, $data)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $parent_guid = Strings::removeTags(XML::unescape($data->parent_guid));
-               $parent_type = Strings::removeTags(XML::unescape($data->parent_type));
-               $positive = Strings::removeTags(XML::unescape($data->positive));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $parent_guid = Strings::escapeTags(XML::unescape($data->parent_guid));
+               $parent_type = Strings::escapeTags(XML::unescape($data->parent_type));
+               $positive = Strings::escapeTags(XML::unescape($data->positive));
 
                // likes on comments aren't supported by Diaspora - only on posts
                // But maybe this will be supported in the future, so we will accept it.
@@ -2029,11 +2020,11 @@ class Diaspora
         */
        private static function receiveMessage(array $importer, $data)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $conversation_guid = Strings::removeTags(XML::unescape($data->conversation_guid));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $conversation_guid = Strings::escapeTags(XML::unescape($data->conversation_guid));
                $text = XML::unescape($data->text);
-               $created_at = DateTimeFormat::utc(Strings::removeTags(XML::unescape($data->created_at)));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
 
                $contact = self::allowedContactByHandle($importer, $author, true);
                if (!$contact) {
@@ -2069,28 +2060,45 @@ class Diaspora
                        return false;
                }
 
-               q(
-                       "INSERT INTO `mail` (`uid`, `guid`, `convid`, `from-name`,`from-photo`,`from-url`,`contact-id`,`title`,`body`,`seen`,`reply`,`uri`,`parent-uri`,`created`)
-                               VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s','%s','%s')",
-                       intval($importer["uid"]),
-                       DBA::escape($guid),
-                       intval($conversation["id"]),
-                       DBA::escape($person["name"]),
-                       DBA::escape($person["photo"]),
-                       DBA::escape($person["url"]),
-                       intval($contact["id"]),
-                       DBA::escape($conversation["subject"]),
-                       DBA::escape($body),
-                       0,
-                       1,
-                       DBA::escape($message_uri),
-                       DBA::escape($author.":".$conversation["guid"]),
-                       DBA::escape($created_at)
-               );
+               DBA::insert('mail', [
+                       'uid'        => $importer['uid'],
+                       'guid'       => $guid,
+                       'convid'     => $conversation['id'],
+                       'from-name'  => $person['name'],
+                       'from-photo' => $person['photo'],
+                       'from-url'   => $person['url'],
+                       'contact-id' => $contact['id'],
+                       'title'      => $conversation['subject'],
+                       'body'       => $body,
+                       'seen'       => 0,
+                       'reply'      => 1,
+                       'uri'        => $message_uri,
+                       'parent-uri' => $author.":".$conversation['guid'],
+                       'created'    => $created_at
+               ]);
+
+               $message_id = DBA::lastInsertId();
 
                DBA::unlock();
 
                DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $conversation["id"]]);
+
+               notification([
+                       "type" => NOTIFY_MAIL,
+                       "notify_flags" => $importer["notify-flags"],
+                       "language" => $importer["language"],
+                       "to_name" => $importer["username"],
+                       "to_email" => $importer["email"],
+                       "uid" => $importer["uid"],
+                       "item" => ["id" => $message_id, "title" => $conversation["subject"], "subject" => $conversation["subject"], "body" => $body],
+                       "parent" => $conversation["id"],
+                       "source_name" => $person["name"],
+                       "source_link" => $person["url"],
+                       "source_photo" => $person["photo"],
+                       "verb" => ACTIVITY_POST,
+                       "otype" => "mail"
+               ]);
+
                return true;
        }
 
@@ -2104,8 +2112,8 @@ class Diaspora
         */
        private static function receiveParticipation(array $importer, $data)
        {
-               $author = strtolower(Strings::removeTags(XML::unescape($data->author)));
-               $parent_guid = Strings::removeTags(XML::unescape($data->parent_guid));
+               $author = strtolower(Strings::escapeTags(XML::unescape($data->author)));
+               $parent_guid = Strings::escapeTags(XML::unescape($data->parent_guid));
 
                $contact_id = Contact::getIdForURL($author);
                if (!$contact_id) {
@@ -2197,7 +2205,7 @@ class Diaspora
         */
        private static function receiveProfile(array $importer, $data)
        {
-               $author = strtolower(Strings::removeTags(XML::unescape($data->author)));
+               $author = strtolower(Strings::escapeTags(XML::unescape($data->author)));
 
                $contact = self::contactByHandle($importer["uid"], $author);
                if (!$contact) {
@@ -2290,7 +2298,7 @@ class Diaspora
         */
        private static function receiveRequestMakeFriend(array $importer, array $contact)
        {
-               $a = get_app();
+               $a = \get_app();
 
                if ($contact["rel"] == Contact::SHARING) {
                        DBA::update(
@@ -2392,7 +2400,7 @@ class Diaspora
                        DBA::escape($ret["addr"]),
                        DateTimeFormat::utcNow(),
                        DBA::escape($ret["url"]),
-                       DBA::escape(normalise_link($ret["url"])),
+                       DBA::escape(Strings::normaliseLink($ret["url"])),
                        DBA::escape($batch),
                        DBA::escape($ret["name"]),
                        DBA::escape($ret["nick"]),
@@ -2574,13 +2582,13 @@ class Diaspora
         */
        private static function receiveReshare(array $importer, $data, $xml)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $created_at = DateTimeFormat::utc(Strings::removeTags(XML::unescape($data->created_at)));
-               $root_author = Strings::removeTags(XML::unescape($data->root_author));
-               $root_guid = Strings::removeTags(XML::unescape($data->root_guid));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
+               $root_author = Strings::escapeTags(XML::unescape($data->root_author));
+               $root_guid = Strings::escapeTags(XML::unescape($data->root_guid));
                /// @todo handle unprocessed property "provider_display_name"
-               $public = Strings::removeTags(XML::unescape($data->public));
+               $public = Strings::escapeTags(XML::unescape($data->public));
 
                $contact = self::allowedContactByHandle($importer, $author, false);
                if (!$contact) {
@@ -2666,9 +2674,9 @@ class Diaspora
         */
        private static function itemRetraction(array $importer, array $contact, $data)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $target_guid = Strings::removeTags(XML::unescape($data->target_guid));
-               $target_type = Strings::removeTags(XML::unescape($data->target_type));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $target_guid = Strings::escapeTags(XML::unescape($data->target_guid));
+               $target_type = Strings::escapeTags(XML::unescape($data->target_type));
 
                $person = self::personByHandle($author);
                if (!is_array($person)) {
@@ -2730,7 +2738,7 @@ class Diaspora
         */
        private static function receiveRetraction(array $importer, $sender, $data)
        {
-               $target_type = Strings::removeTags(XML::unescape($data->target_type));
+               $target_type = Strings::escapeTags(XML::unescape($data->target_type));
 
                $contact = self::contactByHandle($importer["uid"], $sender);
                if (!$contact && (in_array($target_type, ["Contact", "Person"]))) {
@@ -2775,12 +2783,12 @@ class Diaspora
         */
        private static function receiveStatusMessage(array $importer, SimpleXMLElement $data, $xml)
        {
-               $author = Strings::removeTags(XML::unescape($data->author));
-               $guid = Strings::removeTags(XML::unescape($data->guid));
-               $created_at = DateTimeFormat::utc(Strings::removeTags(XML::unescape($data->created_at)));
-               $public = Strings::removeTags(XML::unescape($data->public));
+               $author = Strings::escapeTags(XML::unescape($data->author));
+               $guid = Strings::escapeTags(XML::unescape($data->guid));
+               $created_at = DateTimeFormat::utc(Strings::escapeTags(XML::unescape($data->created_at)));
+               $public = Strings::escapeTags(XML::unescape($data->public));
                $text = XML::unescape($data->text);
-               $provider_display_name = Strings::removeTags(XML::unescape($data->provider_display_name));
+               $provider_display_name = Strings::escapeTags(XML::unescape($data->provider_display_name));
 
                $contact = self::allowedContactByHandle($importer, $author, false);
                if (!$contact) {
@@ -2795,7 +2803,7 @@ class Diaspora
                $address = [];
                if ($data->location) {
                        foreach ($data->location->children() as $fieldname => $data) {
-                               $address[$fieldname] = Strings::removeTags(XML::unescape($data));
+                               $address[$fieldname] = Strings::escapeTags(XML::unescape($data));
                        }
                }
 
@@ -3049,14 +3057,14 @@ class Diaspora
         */
        public static function transmit(array $owner, array $contact, $envelope, $public_batch, $queue_run = false, $guid = "", $no_queue = false)
        {
-               $a = get_app();
+               $a = \get_app();
 
                $enabled = intval(Config::get("system", "diaspora_enabled"));
                if (!$enabled) {
                        return 200;
                }
 
-               $logid = String::getRandomHex(4);
+               $logid = Strings::getRandomHex(4);
 
                $dest_url = ($public_batch ? $contact["batch"] : $contact["notify"]);
 
@@ -3727,12 +3735,12 @@ class Diaspora
                } elseif (in_array($item["verb"], [ACTIVITY_LIKE, ACTIVITY_DISLIKE])) {
                        $message = self::constructLike($item, $owner);
                        $type = "like";
-               } else {
+               } elseif (!in_array($item["verb"], [ACTIVITY_FOLLOW])) {
                        $message = self::constructComment($item, $owner);
                        $type = "comment";
                }
 
-               if (!$message) {
+               if (empty($message)) {
                        return false;
                }