]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/Diaspora.php
Merge pull request #5574 from annando/issue-5436
[friendica.git] / src / Protocol / Diaspora.php
index 341d3d2a893db6f5ebea5a36252c0be354c6c85d..5036aa357b7fc999d02ada9fb2ff26b972d3bf00 100644 (file)
@@ -19,8 +19,8 @@ use Friendica\Core\PConfig;
 use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
-use Friendica\Database\DBM;
 use Friendica\Model\Contact;
+use Friendica\Model\Conversation;
 use Friendica\Model\GContact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
@@ -71,7 +71,7 @@ class Diaspora
                if (Config::get("system", "relay_directly", false)) {
                        // We distribute our stuff based on the parent to ensure that the thread will be complete
                        $parent = Item::selectFirst(['parent'], ['id' => $item_id]);
-                       if (!DBM::is_result($parent)) {
+                       if (!DBA::isResult($parent)) {
                                return;
                        }
 
@@ -145,10 +145,10 @@ class Diaspora
 
                // Fetch the relay contact
                $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
-                       'contact-type' => ACCOUNT_TYPE_RELAY];
+                       'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
                $contact = DBA::selectFirst('contact', $fields, $condition);
 
-               if (DBM::is_result($contact)) {
+               if (DBA::isResult($contact)) {
                        if ($contact['archive'] || $contact['blocked']) {
                                return false;
                        }
@@ -157,7 +157,7 @@ class Diaspora
                        self::setRelayContact($server_url);
 
                        $contact = DBA::selectFirst('contact', $fields, $condition);
-                       if (DBM::is_result($contact)) {
+                       if (DBA::isResult($contact)) {
                                return $contact;
                        }
                }
@@ -178,13 +178,13 @@ class Diaspora
                        'name' => 'relay', 'nick' => 'relay',
                        'url' => $server_url, 'network' => NETWORK_DIASPORA,
                        'batch' => $server_url . '/receive/public',
-                       'rel' => CONTACT_IS_FOLLOWER, 'blocked' => false,
+                       'rel' => Contact::FOLLOWER, 'blocked' => false,
                        'pending' => false, 'writable' => true];
 
                $fields = array_merge($fields, $network_fields);
 
                $condition = ['uid' => 0, 'nurl' => normalise_link($server_url),
-                       'contact-type' => ACCOUNT_TYPE_RELAY];
+                       'contact-type' => Contact::ACCOUNT_TYPE_RELAY];
 
                if (DBA::exists('contact', $condition)) {
                        unset($fields['created']);
@@ -605,7 +605,7 @@ class Diaspora
                        return false;
                }
 
-               $importer = ["uid" => 0, "page-flags" => PAGE_FREELOVE];
+               $importer = ["uid" => 0, "page-flags" => Contact::PAGE_FREELOVE];
                $success = self::dispatch($importer, $msg, $fields);
 
                return $success;
@@ -900,7 +900,7 @@ class Diaspora
                $update = false;
 
                $person = DBA::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
-               if (DBM::is_result($person)) {
+               if (DBA::isResult($person)) {
                        logger("In cache " . print_r($person, true), LOGGER_DEBUG);
 
                        // update record occasionally so it doesn't get stale
@@ -914,7 +914,7 @@ class Diaspora
                        }
                }
 
-               if (!DBM::is_result($person) || $update) {
+               if (!DBA::isResult($person) || $update) {
                        logger("create or refresh", LOGGER_DEBUG);
                        $r = Probe::uri($handle, NETWORK_DIASPORA);
 
@@ -925,7 +925,7 @@ class Diaspora
 
                                // Fetch the updated or added contact
                                $person = DBA::selectFirst('fcontact', [], ['network' => NETWORK_DIASPORA, 'addr' => $handle]);
-                               if (!DBM::is_result($person)) {
+                               if (!DBA::isResult($person)) {
                                        $person = $r;
                                }
                        }
@@ -974,7 +974,7 @@ class Diaspora
                                intval($pcontact_id)
                        );
 
-                       if (DBM::is_result($r)) {
+                       if (DBA::isResult($r)) {
                                return strtolower($r[0]["addr"]);
                        }
                }
@@ -984,7 +984,7 @@ class Diaspora
                        intval($contact_id)
                );
 
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        $contact = $r[0];
 
                        logger("contact 'self' = ".$contact['self']." 'url' = ".$contact['url'], LOGGER_DEBUG);
@@ -1017,11 +1017,11 @@ class Diaspora
 
                $r = q(
                        "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'",
-                       dbesc(NETWORK_DIASPORA),
-                       dbesc($fcontact_guid)
+                       DBA::escape(NETWORK_DIASPORA),
+                       DBA::escape($fcontact_guid)
                );
 
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        return $r[0]['url'];
                }
 
@@ -1040,46 +1040,26 @@ class Diaspora
         */
        private static function contactByHandle($uid, $handle)
        {
-               // First do a direct search on the contact table
-               $r = q(
-                       "SELECT * FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
-                       intval($uid),
-                       dbesc($handle)
-               );
+               $cid = Contact::getIdForURL($handle, $uid);
+               if (!$cid) {
+                       $handle_parts = explode("@", $handle);
+                       $nurl_sql = "%%://" . $handle_parts[1] . "%%/profile/" . $handle_parts[0];
+                       $cid = Contact::getIdForURL($nurl_sql, $uid);
+               }
 
-               if (DBM::is_result($r)) {
-                       return $r[0];
-               } else {
-                       /*
-                        * We haven't found it?
-                        * We use another function for it that will possibly create a contact entry.
-                        */
-                       $cid = Contact::getIdForURL($handle, $uid);
-
-                       if ($cid > 0) {
-                               /// @TODO Contact retrieval should be encapsulated into an "entity" class like `Contact`
-                               $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($cid));
-
-                               if (DBM::is_result($r)) {
-                                       return $r[0];
-                               }
-                       }
+               if (!$cid) {
+                       logger("Haven't found a contact for user " . $uid . " and handle " . $handle, LOGGER_DEBUG);
+                       return false;
                }
 
-               $handle_parts = explode("@", $handle);
-               $nurl_sql = "%%://".$handle_parts[1]."%%/profile/".$handle_parts[0];
-               $r = q(
-                       "SELECT * FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` LIKE '%s' LIMIT 1",
-                       dbesc(NETWORK_DFRN),
-                       intval($uid),
-                       dbesc($nurl_sql)
-               );
-               if (DBM::is_result($r)) {
-                       return $r[0];
+               $contact = dba::selectFirst('contact', [], ['id' => $cid]);
+               if (!DBA::isResult($contact)) {
+                       // This here shouldn't happen at all
+                       logger("Haven't found a contact for user " . $uid . " and handle " . $handle, LOGGER_DEBUG);
+                       return false;
                }
 
-               logger("Haven't found contact for user ".$uid." and handle ".$handle, LOGGER_DEBUG);
-               return false;
+               return $contact;
        }
 
        /**
@@ -1100,14 +1080,14 @@ class Diaspora
                 */
                // It is deactivated by now, due to side effects. See issue https://github.com/friendica/friendica/pull/4033
                // It is not removed by now. Possibly the code is needed?
-               //if (!$is_comment && $contact["rel"] == CONTACT_IS_FOLLOWER && in_array($importer["page-flags"], array(PAGE_FREELOVE))) {
+               //if (!$is_comment && $contact["rel"] == Contact::FOLLOWER && in_array($importer["page-flags"], array(Contact::PAGE_FREELOVE))) {
                //      dba::update(
                //              'contact',
-               //              array('rel' => CONTACT_IS_FRIEND, 'writable' => true),
+               //              array('rel' => Contact::FRIEND, 'writable' => true),
                //              array('id' => $contact["id"], 'uid' => $contact["uid"])
                //      );
                //
-               //      $contact["rel"] = CONTACT_IS_FRIEND;
+               //      $contact["rel"] = Contact::FRIEND;
                //      logger("defining user ".$contact["nick"]." as friend");
                //}
 
@@ -1116,11 +1096,11 @@ class Diaspora
                        // Maybe blocked, don't accept.
                        return false;
                        // We are following this person?
-               } elseif (($contact["rel"] == CONTACT_IS_SHARING) || ($contact["rel"] == CONTACT_IS_FRIEND)) {
+               } elseif (($contact["rel"] == Contact::SHARING) || ($contact["rel"] == Contact::FRIEND)) {
                        // Yes, then it is fine.
                        return true;
                        // Is it a post to a community?
-               } elseif (($contact["rel"] == CONTACT_IS_FOLLOWER) && in_array($importer["page-flags"], [PAGE_COMMUNITY, PAGE_PRVGROUP])) {
+               } elseif (($contact["rel"] == Contact::FOLLOWER) && in_array($importer["page-flags"], [Contact::PAGE_COMMUNITY, Contact::PAGE_PRVGROUP])) {
                        // That's good
                        return true;
                        // Is the message a global user or a comment?
@@ -1147,7 +1127,9 @@ class Diaspora
                if (!$contact) {
                        logger("A Contact for handle ".$handle." and user ".$importer["uid"]." was not found");
                        // If a contact isn't found, we accept it anyway if it is a comment
-                       if ($is_comment) {
+                       if ($is_comment && ($importer["uid"] != 0)) {
+                               return self::contactByHandle(0, $handle);
+                       } elseif ($is_comment) {
                                return $importer;
                        } else {
                                return false;
@@ -1172,7 +1154,7 @@ class Diaspora
        private static function messageExists($uid, $guid)
        {
                $item = Item::selectFirst(['id'], ['uid' => $uid, 'guid' => $guid]);
-               if (DBM::is_result($item)) {
+               if (DBA::isResult($item)) {
                        logger("message ".$guid." already exists for user ".$uid);
                        return $item["id"];
                }
@@ -1268,6 +1250,11 @@ class Diaspora
        private static function storeByGuid($guid, $server, $uid = 0)
        {
                $serverparts = parse_url($server);
+
+               if (empty($serverparts["host"]) || empty($serverparts["scheme"])) {
+                       return false;
+               }
+
                $server = $serverparts["scheme"]."://".$serverparts["host"];
 
                logger("Trying to fetch item ".$guid." from ".$server, LOGGER_DEBUG);
@@ -1387,12 +1374,13 @@ class Diaspora
                $condition = ['uid' => $uid, 'guid' => $guid];
                $item = Item::selectFirst($fields, $condition);
 
-               if (!DBM::is_result($item)) {
-                       $result = self::storeByGuid($guid, $contact["url"], $uid);
+               if (!DBA::isResult($item)) {
+                       $person = self::personByHandle($author);
+                       $result = self::storeByGuid($guid, $person["url"], $uid);
 
-                       if (!$result) {
-                               $person = self::personByHandle($author);
-                               $result = self::storeByGuid($guid, $person["url"], $uid);
+                       // We don't have an url for items that arrived at the public dispatcher
+                       if (!$result && !empty($contact["url"])) {
+                               $result = self::storeByGuid($guid, $contact["url"], $uid);
                        }
 
                        if ($result) {
@@ -1402,7 +1390,7 @@ class Diaspora
                        }
                }
 
-               if (!DBM::is_result($item)) {
+               if (!DBA::isResult($item)) {
                        logger("parent item not found: parent: ".$guid." - user: ".$uid);
                        return false;
                } else {
@@ -1426,7 +1414,7 @@ class Diaspora
        {
                $condition = ['nurl' => normalise_link($person["url"]), 'uid' => $uid];
                $contact = DBA::selectFirst('contact', ['id', 'network'], $condition);
-               if (DBM::is_result($contact)) {
+               if (DBA::isResult($contact)) {
                        $cid = $contact["id"];
                        $network = $contact["network"];
                } else {
@@ -1582,7 +1570,7 @@ class Diaspora
        private static function getUriFromGuid($author, $guid, $onlyfound = false)
        {
                $item = Item::selectFirst(['uri'], ['guid' => $guid]);
-               if (DBM::is_result($item)) {
+               if (DBA::isResult($item)) {
                        return $item["uri"];
                } elseif (!$onlyfound) {
                        $contact = Contact::getDetailsByAddr($author, 0);
@@ -1612,7 +1600,7 @@ class Diaspora
        private static function getGuidFromUri($uri, $uid)
        {
                $item = Item::selectFirst(['guid'], ['uri' => $uri, 'uid' => $uid]);
-               if (DBM::is_result($item)) {
+               if (DBA::isResult($item)) {
                        return $item["guid"];
                } else {
                        return false;
@@ -1629,10 +1617,10 @@ class Diaspora
        private static function importerForGuid($guid)
        {
                $item = Item::selectFirst(['uid'], ['origin' => true, 'guid' => $guid]);
-               if (DBM::is_result($item)) {
+               if (DBA::isResult($item)) {
                        logger("Found user ".$item['uid']." as owner of item ".$guid, LOGGER_DEBUG);
                        $contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $item['uid']]);
-                       if (DBM::is_result($contact)) {
+                       if (DBA::isResult($contact)) {
                                return $contact;
                        }
                }
@@ -1719,7 +1707,7 @@ class Diaspora
 
                $datarray["object-type"] = ACTIVITY_OBJ_COMMENT;
 
-               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
 
                $datarray["changed"] = $datarray["created"] = $datarray["edited"] = $created_at;
@@ -1801,10 +1789,10 @@ class Diaspora
 
                $r = q(
                        "SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc($msg_guid),
+                       DBA::escape($msg_guid),
                        intval($importer["uid"])
                );
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        logger("duplicate message already delivered.", LOGGER_DEBUG);
                        return false;
                }
@@ -1813,19 +1801,19 @@ class Diaspora
                        "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"]),
-                       dbesc($msg_guid),
+                       DBA::escape($msg_guid),
                        intval($conversation["id"]),
-                       dbesc($person["name"]),
-                       dbesc($person["photo"]),
-                       dbesc($person["url"]),
+                       DBA::escape($person["name"]),
+                       DBA::escape($person["photo"]),
+                       DBA::escape($person["url"]),
                        intval($contact["id"]),
-                       dbesc($subject),
-                       dbesc($body),
+                       DBA::escape($subject),
+                       DBA::escape($body),
                        0,
                        0,
-                       dbesc($message_uri),
-                       dbesc($author.":".$guid),
-                       dbesc($msg_created_at)
+                       DBA::escape($message_uri),
+                       DBA::escape($author.":".$guid),
+                       DBA::escape($msg_created_at)
                );
 
                DBA::unlock();
@@ -1884,7 +1872,7 @@ class Diaspora
                $c = q(
                        "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
                        intval($importer["uid"]),
-                       dbesc($guid)
+                       DBA::escape($guid)
                );
                if ($c)
                        $conversation = $c[0];
@@ -1893,18 +1881,18 @@ class Diaspora
                                "INSERT INTO `conv` (`uid`, `guid`, `creator`, `created`, `updated`, `subject`, `recips`)
                                VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s')",
                                intval($importer["uid"]),
-                               dbesc($guid),
-                               dbesc($author),
-                               dbesc($created_at),
-                               dbesc(DateTimeFormat::utcNow()),
-                               dbesc($subject),
-                               dbesc($participants)
+                               DBA::escape($guid),
+                               DBA::escape($author),
+                               DBA::escape($created_at),
+                               DBA::escape(DateTimeFormat::utcNow()),
+                               DBA::escape($subject),
+                               DBA::escape($participants)
                        );
                        if ($r) {
                                $c = q(
                                        "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
                                        intval($importer["uid"]),
-                                       dbesc($guid)
+                                       DBA::escape($guid)
                                );
                        }
 
@@ -1981,7 +1969,7 @@ class Diaspora
 
                $datarray = [];
 
-               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
 
                $datarray["uid"] = $importer["uid"];
                $datarray["contact-id"] = $author_contact["cid"];
@@ -2063,7 +2051,7 @@ class Diaspora
                $c = q(
                        "SELECT * FROM `conv` WHERE `uid` = %d AND `guid` = '%s' LIMIT 1",
                        intval($importer["uid"]),
-                       dbesc($conversation_guid)
+                       DBA::escape($conversation_guid)
                );
                if ($c) {
                        $conversation = $c[0];
@@ -2088,10 +2076,10 @@ class Diaspora
 
                $r = q(
                        "SELECT `id` FROM `mail` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc($guid),
+                       DBA::escape($guid),
                        intval($importer["uid"])
                );
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        logger("duplicate message already delivered.", LOGGER_DEBUG);
                        return false;
                }
@@ -2100,19 +2088,19 @@ class Diaspora
                        "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"]),
-                       dbesc($guid),
+                       DBA::escape($guid),
                        intval($conversation["id"]),
-                       dbesc($person["name"]),
-                       dbesc($person["photo"]),
-                       dbesc($person["url"]),
+                       DBA::escape($person["name"]),
+                       DBA::escape($person["photo"]),
+                       DBA::escape($person["url"]),
                        intval($contact["id"]),
-                       dbesc($conversation["subject"]),
-                       dbesc($body),
+                       DBA::escape($conversation["subject"]),
+                       DBA::escape($body),
                        0,
                        1,
-                       dbesc($message_uri),
-                       dbesc($author.":".$conversation["guid"]),
-                       dbesc($created_at)
+                       DBA::escape($message_uri),
+                       DBA::escape($author.":".$conversation["guid"]),
+                       DBA::escape($created_at)
                );
 
                DBA::unlock();
@@ -2147,7 +2135,7 @@ class Diaspora
                }
 
                $item = Item::selectFirst(['id'], ['guid' => $parent_guid, 'origin' => true, 'private' => false]);
-               if (!DBM::is_result($item)) {
+               if (!DBA::isResult($item)) {
                        logger('Item not found, no origin or private: '.$parent_guid);
                        return false;
                }
@@ -2319,10 +2307,10 @@ class Diaspora
        {
                $a = get_app();
 
-               if ($contact["rel"] == CONTACT_IS_SHARING) {
+               if ($contact["rel"] == Contact::SHARING) {
                        DBA::update(
                                'contact',
-                               ['rel' => CONTACT_IS_FRIEND, 'writable' => true],
+                               ['rel' => Contact::FRIEND, 'writable' => true],
                                ['id' => $contact["id"], 'uid' => $importer["uid"]]
                        );
                }
@@ -2373,7 +2361,7 @@ class Diaspora
 
                                // If we are now friends, we are sending a share message.
                                // Normally we needn't to do so, but the first message could have been vanished.
-                               if (in_array($contact["rel"], [CONTACT_IS_FRIEND])) {
+                               if (in_array($contact["rel"], [Contact::FRIEND])) {
                                        $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer["uid"]));
                                        if ($u) {
                                                logger("Sending share message to author ".$author." - Contact: ".$contact["id"]." - User: ".$importer["uid"], LOGGER_DEBUG);
@@ -2388,7 +2376,7 @@ class Diaspora
                        }
                }
 
-               if (!$following && $sharing && in_array($importer["page-flags"], [PAGE_SOAPBOX, PAGE_NORMAL])) {
+               if (!$following && $sharing && in_array($importer["page-flags"], [Contact::PAGE_SOAPBOX, Contact::PAGE_NORMAL])) {
                        logger("Author ".$author." wants to share with us - but doesn't want to listen. Request is ignored.", LOGGER_DEBUG);
                        return false;
                } elseif (!$following && !$sharing) {
@@ -2415,18 +2403,18 @@ class Diaspora
                        "INSERT INTO `contact` (`uid`, `network`,`addr`,`created`,`url`,`nurl`,`batch`,`name`,`nick`,`photo`,`pubkey`,`notify`,`poll`,`blocked`,`priority`)
                        VALUES (%d, '%s', '%s', '%s', '%s','%s','%s','%s','%s','%s','%s','%s','%s',%d,%d)",
                        intval($importer["uid"]),
-                       dbesc($ret["network"]),
-                       dbesc($ret["addr"]),
+                       DBA::escape($ret["network"]),
+                       DBA::escape($ret["addr"]),
                        DateTimeFormat::utcNow(),
-                       dbesc($ret["url"]),
-                       dbesc(normalise_link($ret["url"])),
-                       dbesc($batch),
-                       dbesc($ret["name"]),
-                       dbesc($ret["nick"]),
-                       dbesc($ret["photo"]),
-                       dbesc($ret["pubkey"]),
-                       dbesc($ret["notify"]),
-                       dbesc($ret["poll"]),
+                       DBA::escape($ret["url"]),
+                       DBA::escape(normalise_link($ret["url"])),
+                       DBA::escape($batch),
+                       DBA::escape($ret["name"]),
+                       DBA::escape($ret["nick"]),
+                       DBA::escape($ret["photo"]),
+                       DBA::escape($ret["pubkey"]),
+                       DBA::escape($ret["notify"]),
+                       DBA::escape($ret["poll"]),
                        1,
                        2
                );
@@ -2446,7 +2434,7 @@ class Diaspora
 
                Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true);
 
-               if (in_array($importer["page-flags"], [PAGE_NORMAL, PAGE_PRVGROUP])) {
+               if (in_array($importer["page-flags"], [Contact::PAGE_NORMAL, Contact::PAGE_PRVGROUP])) {
                        logger("Sending intra message for author ".$author.".", LOGGER_DEBUG);
 
                        $hash = random_string().(string)time();   // Generate a confirm_key
@@ -2458,9 +2446,9 @@ class Diaspora
                                intval($contact_record["id"]),
                                0,
                                0,
-                               dbesc(L10n::t("Sharing notification from Diaspora network")),
-                               dbesc($hash),
-                               dbesc(DateTimeFormat::utcNow())
+                               DBA::escape(L10n::t("Sharing notification from Diaspora network")),
+                               DBA::escape($hash),
+                               DBA::escape(DateTimeFormat::utcNow())
                        );
                } else {
                        // automatic friend approval
@@ -2469,16 +2457,16 @@ class Diaspora
 
                        Contact::updateAvatar($contact_record["photo"], $importer["uid"], $contact_record["id"]);
 
-                       // technically they are sharing with us (CONTACT_IS_SHARING),
+                       // technically they are sharing with us (Contact::SHARING),
                        // but if our page-type is PAGE_COMMUNITY or PAGE_SOAPBOX
                        // we are going to change the relationship and make them a follower.
 
-                       if (($importer["page-flags"] == PAGE_FREELOVE) && $sharing && $following) {
-                               $new_relation = CONTACT_IS_FRIEND;
-                       } elseif (($importer["page-flags"] == PAGE_FREELOVE) && $sharing) {
-                               $new_relation = CONTACT_IS_SHARING;
+                       if (($importer["page-flags"] == Contact::PAGE_FREELOVE) && $sharing && $following) {
+                               $new_relation = Contact::FRIEND;
+                       } elseif (($importer["page-flags"] == Contact::PAGE_FREELOVE) && $sharing) {
+                               $new_relation = Contact::SHARING;
                        } else {
-                               $new_relation = CONTACT_IS_FOLLOWER;
+                               $new_relation = Contact::FOLLOWER;
                        }
 
                        $r = q(
@@ -2491,8 +2479,8 @@ class Diaspora
                                WHERE `id` = %d
                                ",
                                intval($new_relation),
-                               dbesc(DateTimeFormat::utcNow()),
-                               dbesc(DateTimeFormat::utcNow()),
+                               DBA::escape(DateTimeFormat::utcNow()),
+                               DBA::escape(DateTimeFormat::utcNow()),
                                intval($contact_record["id"])
                        );
 
@@ -2531,7 +2519,7 @@ class Diaspora
                $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false];
                $item = Item::selectFirst($fields, $condition);
 
-               if (DBM::is_result($item)) {
+               if (DBA::isResult($item)) {
                        logger("reshared message ".$guid." already exists on system.");
 
                        // Maybe it is already a reshared item?
@@ -2553,7 +2541,7 @@ class Diaspora
                        }
                }
 
-               if (!DBM::is_result($item)) {
+               if (!DBA::isResult($item)) {
                        if (empty($orig_author)) {
                                logger('Empty author for guid ' . $guid . '. Quitting.');
                                return false;
@@ -2575,7 +2563,7 @@ class Diaspora
                                $condition = ['guid' => $guid, 'visible' => true, 'deleted' => false, 'private' => false];
                                $item = Item::selectFirst($fields, $condition);
 
-                               if (DBM::is_result($item)) {
+                               if (DBA::isResult($item)) {
                                        // If it is a reshared post from another network then reformat to avoid display problems with two share elements
                                        if (self::isReshare($item["body"], false)) {
                                                $item["body"] = Markdown::toBBCode(BBCode::toMarkdown($item["body"]));
@@ -2643,7 +2631,7 @@ class Diaspora
                $datarray["verb"] = ACTIVITY_POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
-               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
 
                $prefix = share_header(
@@ -2717,7 +2705,7 @@ class Diaspora
                }
 
                $r = Item::select($fields, $condition);
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        logger("Target guid ".$target_guid." was not found on this system for user ".$importer['uid'].".");
                        return false;
                }
@@ -2871,7 +2859,7 @@ class Diaspora
                $datarray["verb"] = ACTIVITY_POST;
                $datarray["gravity"] = GRAVITY_PARENT;
 
-               $datarray["protocol"] = PROTOCOL_DIASPORA;
+               $datarray["protocol"] = Conversation::PARCEL_DIASPORA;
                $datarray["source"] = $xml;
 
                $datarray["body"] = self::replacePeopleGuid($body, $contact["url"]);
@@ -3119,7 +3107,7 @@ class Diaspora
                logger("transmit: ".$logid."-".$guid." to ".$dest_url." returns: ".$return_code);
 
                if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) {
-                       if (!$no_queue && ($contact['contact-type'] != ACCOUNT_TYPE_RELAY)) {
+                       if (!$no_queue && ($contact['contact-type'] != Contact::ACCOUNT_TYPE_RELAY)) {
                                logger("queue message");
                                // queue message for redelivery
                                Queue::add($contact["id"], NETWORK_DIASPORA, $envelope, $public_batch, $guid);
@@ -3283,13 +3271,15 @@ class Diaspora
 
                /*
                switch ($contact["rel"]) {
-                       case CONTACT_IS_FRIEND:
+                       case Contact::FRIEND:
                                $following = true;
                                $sharing = true;
-                       case CONTACT_IS_SHARING:
+
+                       case Contact::SHARING:
                                $following = false;
                                $sharing = true;
-                       case CONTACT_IS_FOLLOWER:
+
+                       case Contact::FOLLOWER:
                                $following = true;
                                $sharing = false;
                }
@@ -3370,7 +3360,7 @@ class Diaspora
                if (($guid != "") && $complete) {
                        $condition = ['guid' => $guid, 'network' => [NETWORK_DFRN, NETWORK_DIASPORA]];
                        $item = Item::selectFirst(['contact-id'], $condition);
-                       if (DBM::is_result($item)) {
+                       if (DBA::isResult($item)) {
                                $ret= [];
                                $ret["root_handle"] = self::handleFromContact($item["contact-id"]);
                                $ret["root_guid"] = $guid;
@@ -3423,7 +3413,7 @@ class Diaspora
        private static function buildEvent($event_id)
        {
                $r = q("SELECT `guid`, `uid`, `start`, `finish`, `nofinish`, `summary`, `desc`, `location`, `adjust` FROM `event` WHERE `id` = %d", intval($event_id));
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        return [];
                }
 
@@ -3432,14 +3422,14 @@ class Diaspora
                $eventdata = [];
 
                $r = q("SELECT `timezone` FROM `user` WHERE `uid` = %d", intval($event['uid']));
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        return [];
                }
 
                $user = $r[0];
 
                $r = q("SELECT `addr`, `nick` FROM `contact` WHERE `uid` = %d AND `self`", intval($event['uid']));
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        return [];
                }
 
@@ -3551,7 +3541,7 @@ class Diaspora
 
                        if ($item["attach"]) {
                                $cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism', $item["attach"], $matches, PREG_SET_ORDER);
-                               if (cnt) {
+                               if ($cnt) {
                                        $body .= "\n".L10n::t("Attachments:")."\n";
                                        foreach ($matches as $mtch) {
                                                $body .= "[".$mtch[3]."](".$mtch[1].")\n";
@@ -3637,7 +3627,7 @@ class Diaspora
        private static function constructLike(array $item, array $owner)
        {
                $parent = Item::selectFirst(['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]);
-               if (!DBM::is_result($parent)) {
+               if (!DBA::isResult($parent)) {
                        return false;
                }
 
@@ -3668,7 +3658,7 @@ class Diaspora
        private static function constructAttend(array $item, array $owner)
        {
                $parent = Item::selectFirst(['guid', 'uri', 'parent-uri'], ['uri' => $item["thr-parent"]]);
-               if (!DBM::is_result($parent)) {
+               if (!DBA::isResult($parent)) {
                        return false;
                }
 
@@ -3712,7 +3702,7 @@ class Diaspora
                }
 
                $parent = Item::selectFirst(['guid'], ['id' => $item["parent"], 'parent' => $item["parent"]]);
-               if (!DBM::is_result($parent)) {
+               if (!DBA::isResult($parent)) {
                        return false;
                }
 
@@ -3938,7 +3928,7 @@ class Diaspora
                        intval($item["uid"])
                );
 
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        logger("conversation not found.");
                        return;
                }
@@ -4097,7 +4087,7 @@ class Diaspora
                                $arr = explode(' ', $profile['pub_keywords']);
                                if (count($arr)) {
                                        for ($x = 0; $x < 5; $x ++) {
-                                               if (trim($arr[$x])) {
+                                               if (!empty($arr[$x])) {
                                                        $tags .= '#'. trim($arr[$x]) .' ';
                                                }
                                        }
@@ -4143,9 +4133,9 @@ class Diaspora
                        $recips = q(
                                "SELECT `id`,`name`,`network`,`pubkey`,`notify` FROM `contact` WHERE `network` = '%s'
                                AND `uid` = %d AND `rel` != %d",
-                               dbesc(NETWORK_DIASPORA),
+                               DBA::escape(NETWORK_DIASPORA),
                                intval($uid),
-                               intval(CONTACT_IS_SHARING)
+                               intval(Contact::SHARING)
                        );
                }
 
@@ -4178,14 +4168,14 @@ class Diaspora
                }
 
                $r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid']));
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        return false;
                }
 
                $contact["uprvkey"] = $r[0]['prvkey'];
 
                $item = Item::selectFirst([], ['id' => $post_id]);
-               if (!DBM::is_result($item)) {
+               if (!DBA::isResult($item)) {
                        return false;
                }