]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Use Model\Photo as much as possible
[friendica.git] / src / Model / Item.php
index 1eb96609f2ace041f2f9ade9af459bc340753fad..f4290aab971e43e67ea722cd1cbd2e961e073969 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);
                        }
                }
@@ -1037,7 +1030,7 @@ class Item extends BaseObject
                 * generate a resource-id and therefore aren't intimately linked to the item.
                 */
                if (strlen($item['resource-id'])) {
-                       DBA::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
+                       Photo::delete(['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
                }
 
                // If item is a link to an event, delete the event.
@@ -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;
                }
@@ -2377,7 +2370,7 @@ class Item extends BaseObject
                $update = (!$arr['private'] && ((defaults($arr, 'author-link', '') === defaults($arr, 'owner-link', '')) || ($arr["parent-uri"] === $arr["uri"])));
 
                // Is it a forum? Then we don't care about the rules from above
-               if (!$update && ($arr["network"] == Protocol::DFRN) && ($arr["parent-uri"] === $arr["uri"])) {
+               if (!$update && in_array($arr["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN]) && ($arr["parent-uri"] === $arr["uri"])) {
                        if (DBA::exists('contact', ['id' => $arr['contact-id'], 'forum' => true])) {
                                $update = true;
                        }
@@ -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;
@@ -2744,8 +2737,7 @@ class Item extends BaseObject
                                if ($x) {
                                        $res = substr($i, $x + 1);
                                        $i = substr($i, 0, $x);
-                                       $fields = ['data', 'type', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
-                                       $photo = DBA::selectFirst('photo', $fields, ['resource-id' => $i, 'scale' => $res, 'uid' => $uid]);
+                                       $photo = Photo::getPhotoForUser($uid, $i, $res);
                                        if (DBA::isResult($photo)) {
                                                /*
                                                 * Check to see if we should replace this photo link with an embedded image
@@ -2769,9 +2761,7 @@ class Item extends BaseObject
                                                        }
                                                }
                                                if ($replace) {
-                                                       $data = $photo['data'];
-                                                       $type = $photo['type'];
-
+                                                       $photo_img = Photo::getImageForPhoto($photo);
                                                        // If a custom width and height were specified, apply before embedding
                                                        if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
                                                                Logger::log('scaling photo', Logger::DEBUG);
@@ -2779,14 +2769,12 @@ class Item extends BaseObject
                                                                $width = intval($match[1]);
                                                                $height = intval($match[2]);
 
-                                                               $Image = new Image($data, $type);
-                                                               if ($Image->isValid()) {
-                                                                       $Image->scaleDown(max($width, $height));
-                                                                       $data = $Image->asString();
-                                                                       $type = $Image->getType();
-                                                               }
+                                                               $photo_img->scaleDown(max($width, $height));
                                                        }
 
+                                                       $data = $photo_img->asString();
+                                                       $type = $photo_img->getType();
+
                                                        Logger::log('replacing photo', Logger::DEBUG);
                                                        $image = 'data:' . $type . ';base64,' . base64_encode($data);
                                                        Logger::log('replaced: ' . $image, Logger::DATA);
@@ -3217,7 +3205,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 +3228,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 +3434,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 +3446,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 +3467,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,7 +3478,7 @@ 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));