]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #6436 from annando/notice
[friendica.git] / src / Model / Item.php
index 4f88c2ed9d1da95d0780d30509a2bf70e37a1f3e..ca6556d275e6325d3d68463e9bf0d8b87591e3ad 100644 (file)
@@ -33,12 +33,9 @@ use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
 use Friendica\Util\XML;
 use Friendica\Util\Security;
+use Friendica\Util\Strings;
 use Text_LanguageDetect;
 
-require_once 'boot.php';
-require_once 'include/items.php';
-require_once 'include/text.php';
-
 class Item extends BaseObject
 {
        // Posting types, inspired by https://www.w3.org/TR/activitystreams-vocabulary/#object-types
@@ -831,7 +828,7 @@ class Item extends BaseObject
                        $files = $fields['file'];
                        $fields['file'] = null;
                } else {
-                       $files = '';
+                       $files = null;
                }
 
                $delivery_data = ['postopts' => defaults($fields, 'postopts', ''),
@@ -910,7 +907,7 @@ class Item extends BaseObject
                                }
                        }
 
-                       if (!empty($files)) {
+                       if (!is_null($files)) {
                                Term::insertFromFileFieldByItemId($item['id'], $files);
                                if (!empty($item['file'])) {
                                        DBA::update('item', ['file' => ''], ['id' => $item['id']]);
@@ -1010,10 +1007,8 @@ class Item extends BaseObject
                $matches = false;
                $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
 
-               if ($cnt)
-               {
-                       foreach ($matches as $mtch)
-                       {
+               if ($cnt) {
+                       foreach ($matches as $mtch) {
                                FileTag::unsaveFile($item['uid'], $item['id'], $mtch[1],true);
                        }
                }
@@ -1022,10 +1017,8 @@ class Item extends BaseObject
 
                $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
 
-               if ($cnt)
-               {
-                       foreach ($matches as $mtch)
-                       {
+               if ($cnt) {
+                       foreach ($matches as $mtch) {
                                FileTag::unsaveFile($item['uid'], $item['id'], $mtch[1],false);
                        }
                }
@@ -1149,13 +1142,13 @@ class Item extends BaseObject
        private static function guid($item, $notify)
        {
                if (!empty($item['guid'])) {
-                       return notags(trim($item['guid']));
+                       return Strings::escapeTags(trim($item['guid']));
                }
 
                if ($notify) {
                        // We have to avoid duplicates. So we create the GUID in form of a hash of the plink or uri.
                        // We add the hash of our own host because our host is the original creator of the post.
-                       $prefix_host = get_app()->getHostName();
+                       $prefix_host = \get_app()->getHostName();
                } else {
                        $prefix_host = '';
 
@@ -1245,7 +1238,7 @@ class Item extends BaseObject
 
        public static function insert($item, $force_parent = false, $notify = false, $dontcache = false)
        {
-               $a = get_app();
+               $a = \get_app();
 
                // If it is a posting where users should get notifications, then define it as wall posting
                if ($notify) {
@@ -1264,7 +1257,7 @@ class Item extends BaseObject
                }
 
                $item['guid'] = self::guid($item, $notify);
-               $item['uri'] = notags(trim(defaults($item, 'uri', self::newURI($item['uid'], $item['guid']))));
+               $item['uri'] = Strings::escapeTags(trim(defaults($item, 'uri', self::newURI($item['uid'], $item['guid']))));
 
                // Store URI data
                $item['uri-id'] = ItemURI::insert(['uri' => $item['uri'], 'guid' => $item['guid']]);
@@ -1362,15 +1355,15 @@ class Item extends BaseObject
                $item['owner-name']    = trim(defaults($item, 'owner-name', ''));
                $item['owner-link']    = trim(defaults($item, 'owner-link', ''));
                $item['owner-avatar']  = trim(defaults($item, 'owner-avatar', ''));
-               $item['received']      = ((x($item, 'received') !== false) ? DateTimeFormat::utc($item['received']) : DateTimeFormat::utcNow());
-               $item['created']       = ((x($item, 'created') !== false) ? DateTimeFormat::utc($item['created']) : $item['received']);
-               $item['edited']        = ((x($item, 'edited') !== false) ? DateTimeFormat::utc($item['edited']) : $item['created']);
-               $item['changed']       = ((x($item, 'changed') !== false) ? DateTimeFormat::utc($item['changed']) : $item['created']);
-               $item['commented']     = ((x($item, 'commented') !== false) ? DateTimeFormat::utc($item['commented']) : $item['created']);
+               $item['received']      = (isset($item['received'])  ? DateTimeFormat::utc($item['received'])  : DateTimeFormat::utcNow());
+               $item['created']       = (isset($item['created'])   ? DateTimeFormat::utc($item['created'])   : $item['received']);
+               $item['edited']        = (isset($item['edited'])    ? DateTimeFormat::utc($item['edited'])    : $item['created']);
+               $item['changed']       = (isset($item['changed'])   ? DateTimeFormat::utc($item['changed'])   : $item['created']);
+               $item['commented']     = (isset($item['commented']) ? DateTimeFormat::utc($item['commented']) : $item['created']);
                $item['title']         = trim(defaults($item, 'title', ''));
                $item['location']      = trim(defaults($item, 'location', ''));
                $item['coord']         = trim(defaults($item, 'coord', ''));
-               $item['visible']       = ((x($item, 'visible') !== false) ? intval($item['visible'])         : 1);
+               $item['visible']       = (isset($item['visible']) ? intval($item['visible']) : 1);
                $item['deleted']       = 0;
                $item['parent-uri']    = trim(defaults($item, 'parent-uri', $item['uri']));
                $item['post-type']     = defaults($item, 'post-type', self::PT_ARTICLE);
@@ -1534,7 +1527,7 @@ class Item extends BaseObject
                                Logger::log("Checking if parent ".$parent_id." has to be tagged as mention for user ".$item['uid'], Logger::DEBUG);
                                $user = DBA::selectFirst('user', ['nickname'], ['uid' => $item['uid']]);
                                if (DBA::isResult($user)) {
-                                       $self = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
+                                       $self = Strings::normaliseLink(System::baseUrl() . '/profile/' . $user['nickname']);
                                        $self_id = Contact::getIdForURL($self, 0, true);
                                        Logger::log("'myself' is ".$self_id." for parent ".$parent_id." checking against ".$item['author-id']." and ".$item['owner-id'], Logger::DEBUG);
                                        if (($item['author-id'] == $self_id) || ($item['owner-id'] == $self_id)) {
@@ -1629,7 +1622,7 @@ class Item extends BaseObject
                // It is mainly used in the "post_local" hook.
                unset($item['api_source']);
 
-               if (x($item, 'cancel')) {
+               if (!empty($item['cancel'])) {
                        Logger::log('post cancelled by addon.');
                        return 0;
                }
@@ -2402,7 +2395,7 @@ class Item extends BaseObject
        public static function setHashtags(&$item)
        {
 
-               $tags = get_tags($item["body"]);
+               $tags = BBCode::getTags($item["body"]);
 
                // No hashtags?
                if (!count($tags)) {
@@ -2451,15 +2444,15 @@ class Item extends BaseObject
 
                        $basetag = str_replace('_',' ',substr($tag,1));
 
-                       $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
+                       $newtag = '#[url=' . System::baseUrl() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
 
                        $item["body"] = str_replace($tag, $newtag, $item["body"]);
 
                        if (!stristr($item["tag"], "/search?tag=" . $basetag . "]" . $basetag . "[/url]")) {
                                if (strlen($item["tag"])) {
-                                       $item["tag"] = ','.$item["tag"];
+                                       $item["tag"] = ',' . $item["tag"];
                                }
-                               $item["tag"] = $newtag.$item["tag"];
+                               $item["tag"] = $newtag . $item["tag"];
                        }
                }
 
@@ -2544,18 +2537,18 @@ class Item extends BaseObject
                        return;
                }
 
-               $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]);
                                }
@@ -2618,7 +2611,7 @@ class Item extends BaseObject
 
        public static function isRemoteSelf($contact, &$datarray)
        {
-               $a = get_app();
+               $a = \get_app();
 
                if (!$contact['remote_self']) {
                        return false;
@@ -3217,7 +3210,7 @@ class Item extends BaseObject
                }
        }
 
-       public static function getPermissionsSQLByUserId($owner_id, $remote_verified = false, $groups = null)
+       public static function getPermissionsSQLByUserId($owner_id, $remote_verified = false, $groups = null, $remote_cid = null)
        {
                $local_user = local_user();
                $remote_user = remote_user();
@@ -3240,7 +3233,7 @@ class Item extends BaseObject
                         * If pre-verified, the caller is expected to have already
                         * done this and passed the groups into this function.
                         */
-                       $set = PermissionSet::get($owner_id, $remote_user, $groups);
+                       $set = PermissionSet::get($owner_id, $remote_cid, $groups);
 
                        if (!empty($set)) {
                                $sql_set = " OR (`item`.`private` IN (1,2) AND `item`.`wall` AND `item`.`psid` IN (" . implode(',', $set) . "))";
@@ -3446,7 +3439,7 @@ class Item extends BaseObject
                                $filesubtype = 'unkn';
                        }
 
-                       $title = escape_tags(trim(!empty($mtch[4]) ? $mtch[4] : $mtch[1]));
+                       $title = Strings::escapeHtml(trim(defaults($mtch, 4, $mtch[1])));
                        $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes');
 
                        $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
@@ -3458,7 +3451,7 @@ class Item extends BaseObject
                }
 
                // Map.
-               if (strpos($s, '<div class="map">') !== false && x($item, 'coord')) {
+               if (strpos($s, '<div class="map">') !== false && !empty($item['coord'])) {
                        $x = Map::byCoordinates(trim($item['coord']));
                        if ($x) {
                                $s = preg_replace('/\<div class\=\"map\"\>/', '$0' . $x, $s);
@@ -3479,7 +3472,7 @@ class Item extends BaseObject
 
                while ((strpos($s, $spoilersearch) !== false)) {
                        $pos = strpos($s, $spoilersearch);
-                       $rnd = random_string(8);
+                       $rnd = Strings::getRandomHex(8);
                        $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" class="spoiler-wrap fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
                                                '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
                        $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
@@ -3490,14 +3483,14 @@ class Item extends BaseObject
 
                while ((strpos($s, $authorsearch) !== false)) {
                        $pos = strpos($s, $authorsearch);
-                       $rnd = random_string(8);
+                       $rnd = Strings::getRandomHex(8);
                        $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" class="author-wrap fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . L10n::t('Click to open/close') . '</span>'.
                                                '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
                        $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
                }
 
                // Replace friendica image url size with theme preference.
-               if (x($a->theme_info, 'item_image_size')) {
+               if (!empty($a->theme_info['item_image_size'])) {
                        $ps = $a->theme_info['item_image_size'];
                        $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
                }
@@ -3521,23 +3514,23 @@ class Item extends BaseObject
 
                if ($a->user['nickname'] != "") {
                        $ret = [
-                                       'href' => "display/" . $item['guid'],
-                                       'orig' => "display/" . $item['guid'],
-                                       'title' => L10n::t('View on separate page'),
-                                       'orig_title' => L10n::t('view on separate page'),
-                               ];
+                               'href' => "display/" . $item['guid'],
+                               'orig' => "display/" . $item['guid'],
+                               'title' => L10n::t('View on separate page'),
+                               'orig_title' => L10n::t('view on separate page'),
+                       ];
 
-                       if (x($item, 'plink')) {
+                       if (!empty($item['plink'])) {
                                $ret["href"] = $a->removeBaseURL($item['plink']);
                                $ret["title"] = L10n::t('link to source');
                        }
 
-               } elseif (x($item, 'plink') && ($item['private'] != 1)) {
+               } elseif (!empty($item['plink']) && ($item['private'] != 1)) {
                        $ret = [
-                                       'href' => $item['plink'],
-                                       'orig' => $item['plink'],
-                                       'title' => L10n::t('link to source'),
-                               ];
+                               'href' => $item['plink'],
+                               'orig' => $item['plink'],
+                               'title' => L10n::t('link to source'),
+                       ];
                } else {
                        $ret = [];
                }