]> git.mxchange.org Git - friendica.git/blobdiff - src/Protocol/DFRN.php
Merge pull request #6224 from annando/dba-delete-contact
[friendica.git] / src / Protocol / DFRN.php
index b48dc0a8d8963ec4a55a60b0c893aec0c7aaaa8b..7c807b921e22cad9ff923fed1def135bd8b9918a 100644 (file)
@@ -16,7 +16,6 @@ use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
@@ -33,6 +32,7 @@ use Friendica\Object\Image;
 use Friendica\Util\Crypto;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
+use Friendica\Util\Strings;
 use Friendica\Util\XML;
 use HTMLPurifier;
 use HTMLPurifier_Config;
@@ -240,12 +240,12 @@ class DFRN
                if (isset($category)) {
                        $sql_post_table = sprintf(
                                "INNER JOIN (SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d ORDER BY `tid` DESC) AS `term` ON `item`.`id` = `term`.`oid` ",
-                               DBA::escape(protect_sprintf($category)),
+                               DBA::escape(Strings::protectSprintf($category)),
                                intval(TERM_OBJ_POST),
                                intval(TERM_CATEGORY),
                                intval($owner_id)
                        );
-                       //$sql_extra .= file_tag_file_query('item',$category,'category');
+                       //$sql_extra .= FileTag::fileQuery('item',$category,'category');
                }
 
                if ($public_feed && ! $converse) {
@@ -689,7 +689,7 @@ class DFRN
                        XML::addElement($doc, $author, "poco:displayName", $profile["name"]);
                        XML::addElement($doc, $author, "poco:updated", $namdate);
 
-                       if (trim($profile["dob"]) > '0001-01-01') {
+                       if (trim($profile["dob"]) > DBA::NULL_DATE) {
                                XML::addElement($doc, $author, "poco:birthday", "0000-".date("m-d", strtotime($profile["dob"])));
                        }
 
@@ -1001,7 +1001,7 @@ class DFRN
                XML::addElement($doc, $entry, "updated", DateTimeFormat::utc($item["edited"] . "+00:00", DateTimeFormat::ATOM));
 
                // "dfrn:env" is used to read the content
-               XML::addElement($doc, $entry, "dfrn:env", base64url_encode($body, true));
+               XML::addElement($doc, $entry, "dfrn:env", Strings::base64UrlEncode($body, true));
 
                // The "content" field is not read by the receiver. We could remove it when the type is "text"
                // We keep it at the moment, maybe there is some old version that doesn't read "dfrn:env"
@@ -1096,7 +1096,7 @@ class DFRN
                }
 
                foreach ($mentioned as $mention) {
-                       $condition = ['uid' => $owner["uid"], 'nurl' => normalise_link($mention)];
+                       $condition = ['uid' => $owner["uid"], 'nurl' => Strings::normaliseLink($mention)];
                        $contact = DBA::selectFirst('contact', ['forum', 'prv'], $condition);
 
                        if (DBA::isResult($contact) && ($contact["forum"] || $contact["prv"])) {
@@ -1509,43 +1509,6 @@ class DFRN
                return intval($res->status);
        }
 
-       /**
-        * @brief Add new birthday event for this person
-        *
-        * @param array  $contact  Contact record
-        * @param string $birthday Birthday of the contact
-        * @return void
-        * @todo Add array type-hint for $contact
-        */
-       private static function birthdayEvent($contact, $birthday)
-       {
-               // Check for duplicates
-               $condition = ['uid' => $contact['uid'], 'cid' => $contact['id'],
-                       'start' => DateTimeFormat::utc($birthday), 'type' => 'birthday'];
-               if (DBA::exists('event', $condition)) {
-                       return;
-               }
-
-               Logger::log('updating birthday: ' . $birthday . ' for contact ' . $contact['id']);
-
-               $bdtext = L10n::t('%s\'s birthday', $contact['name']);
-               $bdtext2 = L10n::t('Happy Birthday %s', ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]');
-
-               $r = q(
-                       "INSERT INTO `event` (`uid`,`cid`,`created`,`edited`,`start`,`finish`,`summary`,`desc`,`type`)
-                       VALUES ( %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s') ",
-                       intval($contact['uid']),
-                       intval($contact['id']),
-                       DBA::escape(DateTimeFormat::utcNow()),
-                       DBA::escape(DateTimeFormat::utcNow()),
-                       DBA::escape(DateTimeFormat::utc($birthday)),
-                       DBA::escape(DateTimeFormat::utc($birthday . ' + 1 day ')),
-                       DBA::escape($bdtext),
-                       DBA::escape($bdtext2),
-                       DBA::escape('birthday')
-               );
-       }
-
        /**
         * @brief Fetch the author data from head or entry items
         *
@@ -1568,7 +1531,7 @@ class DFRN
                $fields = ['id', 'uid', 'url', 'network', 'avatar-date', 'avatar', 'name-date', 'uri-date', 'addr',
                        'name', 'nick', 'about', 'location', 'keywords', 'xmpp', 'bdyear', 'bd', 'hidden', 'contact-type'];
                $condition = ["`uid` = ? AND `nurl` = ? AND `network` != ?",
-                       $importer["importer_uid"], normalise_link($author["link"]), Protocol::STATUSNET];
+                       $importer["importer_uid"], Strings::normaliseLink($author["link"]), Protocol::STATUSNET];
                $contact_old = DBA::selectFirst('contact', $fields, $condition);
 
                if (DBA::isResult($contact_old)) {
@@ -1721,9 +1684,9 @@ class DFRN
                        // "poco:birthday" is the birthday in the format "yyyy-mm-dd"
                        $value = XML::getFirstNodeValue($xpath, $element . "/poco:birthday/text()", $context);
 
-                       if (!in_array($value, ["", "0000-00-00", "0001-01-01"])) {
+                       if (!in_array($value, ["", "0000-00-00", DBA::NULL_DATE])) {
                                $bdyear = date("Y");
-                               $value = str_replace("0000", $bdyear, $value);
+                               $value = str_replace(["0000", "0001"], $bdyear, $value);
 
                                if (strtotime($value) < time()) {
                                        $value = str_replace($bdyear, $bdyear + 1, $value);
@@ -1736,7 +1699,7 @@ class DFRN
                        $contact = array_merge($contact_old, $poco);
 
                        if ($contact_old["bdyear"] != $contact["bdyear"]) {
-                               self::birthdayEvent($contact, $birthday);
+                               Event::createBirthday($contact, $birthday);
                        }
 
                        // Get all field names
@@ -1959,7 +1922,7 @@ class DFRN
                 *
                 * @see https://github.com/friendica/friendica/pull/3254#discussion_r107315246
                 */
-               $condition = ['name' => $suggest["name"], 'nurl' => normalise_link($suggest["url"]),
+               $condition = ['name' => $suggest["name"], 'nurl' => Strings::normaliseLink($suggest["url"]),
                        'uid' => $suggest["uid"]];
                if (DBA::exists('contact', $condition)) {
                        return false;
@@ -2009,7 +1972,7 @@ class DFRN
 
                $fid = $r[0]["id"];
 
-               $hash = random_string();
+               $hash = Strings::getRandomHex();
 
                $r = q(
                        "INSERT INTO `intro` (`uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked`)
@@ -2099,18 +2062,18 @@ class DFRN
                $relocate["server_url"] = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $relocate["url"]);
 
                $fields = ['name' => $relocate["name"], 'photo' => $relocate["avatar"],
-                       'url' => $relocate["url"], 'nurl' => normalise_link($relocate["url"]),
+                       'url' => $relocate["url"], 'nurl' => Strings::normaliseLink($relocate["url"]),
                        'addr' => $relocate["addr"], 'connect' => $relocate["addr"],
                        'notify' => $relocate["notify"], 'server_url' => $relocate["server_url"]];
-               DBA::update('gcontact', $fields, ['nurl' => normalise_link($old["url"])]);
+               DBA::update('gcontact', $fields, ['nurl' => Strings::normaliseLink($old["url"])]);
 
                // Update the contact table. We try to find every entry.
                $fields = ['name' => $relocate["name"], 'avatar' => $relocate["avatar"],
-                       'url' => $relocate["url"], 'nurl' => normalise_link($relocate["url"]),
+                       'url' => $relocate["url"], 'nurl' => Strings::normaliseLink($relocate["url"]),
                        'addr' => $relocate["addr"], 'request' => $relocate["request"],
                        'confirm' => $relocate["confirm"], 'notify' => $relocate["notify"],
                        'poll' => $relocate["poll"], 'site-pubkey' => $relocate["sitepubkey"]];
-               $condition = ["(`id` = ?) OR (`nurl` = ?)", $importer["id"], normalise_link($old["url"])];
+               $condition = ["(`id` = ?) OR (`nurl` = ?)", $importer["id"], Strings::normaliseLink($old["url"])];
 
                DBA::update('contact', $fields, $condition);
 
@@ -2255,7 +2218,7 @@ class DFRN
                                }
                        }
 
-                       if ($Blink && link_compare($Blink, System::baseUrl() . "/profile/" . $importer["nickname"])) {
+                       if ($Blink && Strings::compareLink($Blink, System::baseUrl() . "/profile/" . $importer["nickname"])) {
                                $author = DBA::selectFirst('contact', ['name', 'thumb', 'url'], ['id' => $item['author-id']]);
 
                                $item['id'] = $posted_id;
@@ -2493,16 +2456,16 @@ class DFRN
                $item["body"] = XML::getFirstNodeValue($xpath, "dfrn:env/text()", $entry);
                $item["body"] = str_replace([' ',"\t","\r","\n"], ['','','',''], $item["body"]);
                // make sure nobody is trying to sneak some html tags by us
-               $item["body"] = notags(base64url_decode($item["body"]));
+               $item["body"] = Strings::escapeTags(Strings::base64UrlDecode($item["body"]));
 
                $item["body"] = BBCode::limitBodySize($item["body"]);
 
                /// @todo Do we really need this check for HTML elements? (It was copied from the old function)
                if ((strpos($item['body'], '<') !== false) && (strpos($item['body'], '>') !== false)) {
                        $base_url = get_app()->getBaseURL();
-                       $item['body'] = reltoabs($item['body'], $base_url);
+                       $item['body'] = HTML::relToAbs($item['body'], $base_url);
 
-                       $item['body'] = html2bb_video($item['body']);
+                       $item['body'] = HTML::toBBCodeVideo($item['body']);
 
                        $item['body'] = OEmbed::HTML2BBCode($item['body']);
 
@@ -2547,7 +2510,7 @@ class DFRN
                $item["guid"] = XML::getFirstNodeValue($xpath, "dfrn:diaspora_guid/text()", $entry);
 
                // We store the data from "dfrn:diaspora_signature" in a different table, this is done in "Item::insert"
-               $dsprsig = unxmlify(XML::getFirstNodeValue($xpath, "dfrn:diaspora_signature/text()", $entry));
+               $dsprsig = XML::unescape(XML::getFirstNodeValue($xpath, "dfrn:diaspora_signature/text()", $entry));
                if ($dsprsig != "") {
                        $item["dsprsig"] = $dsprsig;
                }
@@ -2674,7 +2637,7 @@ class DFRN
                        if (($item["object-type"] == ACTIVITY_OBJ_EVENT) && !$owner_unknown) {
                                Logger::log("Item ".$item["uri"]." seems to contain an event.", Logger::DEBUG);
                                $ev = Event::fromBBCode($item["body"]);
-                               if ((x($ev, "desc") || x($ev, "summary")) && x($ev, "start")) {
+                               if ((!empty($ev['desc']) || !empty($ev['summary'])) && !empty($ev['start'])) {
                                        Logger::log("Event in item ".$item["uri"]." was found.", Logger::DEBUG);
                                        $ev["cid"]     = $importer["id"];
                                        $ev["uid"]     = $importer["importer_uid"];
@@ -2737,7 +2700,7 @@ class DFRN
                                Logger::log("Contact ".$importer["id"]." isn't known to user ".$importer["importer_uid"].". The post will be ignored.", Logger::DEBUG);
                                return;
                        }
-                       if (!link_compare($item["owner-link"], $importer["url"])) {
+                       if (!Strings::compareLink($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.
@@ -2962,7 +2925,7 @@ class DFRN
        public static function autoRedir(App $a, $contact_nick)
        {
                // prevent looping
-               if (x($_REQUEST, 'redir') && intval($_REQUEST['redir'])) {
+               if (!empty($_REQUEST['redir'])) {
                        return;
                }
 
@@ -2985,7 +2948,7 @@ class DFRN
                                return;
                        }
                        $baseurl = substr($baseurl, $domain_st + 3);
-                       $nurl = normalise_link($baseurl);
+                       $nurl = Strings::normaliseLink($baseurl);
 
                        /// @todo Why is there a query for "url" *and* "nurl"? Especially this normalising is strange.
                        $r = q("SELECT `id` FROM `contact` WHERE `uid` = (SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1)
@@ -3030,7 +2993,7 @@ class DFRN
                                return;
                        }
 
-                       $sec = random_string();
+                       $sec = Strings::getRandomHex();
 
                        DBA::insert('profile_check', ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id, 'sec' => $sec, 'expire' => time() + 45]);
 
@@ -3078,18 +3041,18 @@ class DFRN
                $community_page = ($user['page-flags'] == Contact::PAGE_COMMUNITY);
                $prvgroup = ($user['page-flags'] == Contact::PAGE_PRVGROUP);
 
-               $link = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
+               $link = Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname']);
 
                /*
                 * Diaspora uses their own hardwired link URL in @-tags
                 * instead of the one we supply with webfinger
                 */
-               $dlink = normalise_link(System::baseUrl() . '/u/' . $user['nickname']);
+               $dlink = Strings::normaliseLink(System::baseUrl() . '/u/' . $user['nickname']);
 
                $cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
                if ($cnt) {
                        foreach ($matches as $mtch) {
-                               if (link_compare($link, $mtch[1]) || link_compare($dlink, $mtch[1])) {
+                               if (Strings::compareLink($link, $mtch[1]) || Strings::compareLink($dlink, $mtch[1])) {
                                        $mention = true;
                                        Logger::log('mention found: ' . $mtch[2]);
                                }
@@ -3114,10 +3077,10 @@ class DFRN
         */
        private static function isEditedTimestampNewer($existing, $update)
        {
-               if (!x($existing, 'edited') || !$existing['edited']) {
+               if (empty($existing['edited'])) {
                        return true;
                }
-               if (!x($update, 'edited') || !$update['edited']) {
+               if (empty($update['edited'])) {
                        return false;
                }