]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Ensure APContact::getByUrl always returns an array
[friendica.git] / include / api.php
index 91e31ac9459bb5864ca69075f046b518d50f5cad..681515cc376cc0a9bfeb11bad6ad05521b972cb4 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 use Friendica\App;
+use Friendica\BaseObject;
 use Friendica\Content\ContactSelector;
 use Friendica\Content\Feature;
 use Friendica\Content\Text\BBCode;
@@ -15,7 +16,6 @@ use Friendica\Core\Config;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
-use Friendica\Core\NotificationsManager;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Session;
@@ -26,6 +26,7 @@ use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
 use Friendica\Model\Mail;
+use Friendica\Model\Notify;
 use Friendica\Model\Photo;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
@@ -44,6 +45,7 @@ use Friendica\Object\Image;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\Diaspora;
 use Friendica\Util\DateTimeFormat;
+use Friendica\Util\Images;
 use Friendica\Util\Network;
 use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
@@ -957,7 +959,7 @@ function api_account_verify_credentials($type)
        // - Adding last status
        if (!$skip_status) {
                $item = api_get_last_status($user_info['pid'], $user_info['uid']);
-               if ($item) {
+               if (!empty($item)) {
                        $user_info['status'] = api_format_item($item, $type);
                }
        }
@@ -1166,7 +1168,7 @@ function api_statuses_update($type)
                                api_user()
                        );
                        if (DBA::isResult($r)) {
-                               $phototypes = Image::supportedTypes();
+                               $phototypes = Images::supportedTypes();
                                $ext = $phototypes[$r[0]['type']];
                                $description = $r[0]['desc'] ?? '';
                                $_REQUEST['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $r[0]['nickname'] . '/image/' . $r[0]['resource-id'] . ']';
@@ -1316,7 +1318,7 @@ function api_status_show($type, $item_id)
        $status_info = [];
 
        $item = api_get_item(['id' => $item_id]);
-       if ($item) {
+       if (!empty($item)) {
                $status_info = api_format_item($item, $type);
        }
 
@@ -1375,12 +1377,12 @@ function api_get_item(array $condition)
  */
 function api_users_show($type)
 {
-       $a = \Friendica\BaseObject::getApp();
+       $a = BaseObject::getApp();
 
        $user_info = api_get_user($a);
 
        $item = api_get_last_status($user_info['pid'], $user_info['uid']);
-       if ($item) {
+       if (!empty($item)) {
                $user_info['status'] = api_format_item($item, $type);
        }
 
@@ -2017,7 +2019,7 @@ function api_statuses_repeat($type)
 
        Logger::log('API: api_statuses_repeat: '.$id);
 
-       $fields = ['body', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
+       $fields = ['body', 'title', 'attach', 'tag', 'author-name', 'author-link', 'author-avatar', 'guid', 'created', 'plink'];
        $item = Item::selectFirst($fields, ['id' => $id, 'private' => false]);
 
        if (DBA::isResult($item) && $item['body'] != "") {
@@ -2027,10 +2029,16 @@ function api_statuses_repeat($type)
                } else {
                        $post = share_header($item['author-name'], $item['author-link'], $item['author-avatar'], $item['guid'], $item['created'], $item['plink']);
 
+                       if (!empty($item['title'])) {
+                               $post .= '[h3]' . $item['title'] . "[/h3]\n";
+                       }
+
                        $post .= $item['body'];
                        $post .= "[/share]";
                }
                $_REQUEST['body'] = $post;
+               $_REQUEST['tag'] = $item['tag'];
+               $_REQUEST['attach'] = $item['attach'];
                $_REQUEST['profile_uid'] = api_user();
                $_REQUEST['api_source'] = true;
 
@@ -2496,7 +2504,7 @@ function api_convert_item($item)
                $statustext = mb_substr($statustext, 0, 1000) . "... \n" . ($item['plink'] ?? '');
        }
 
-       $statushtml = BBCode::convert(api_clean_attachments($body), false);
+       $statushtml = BBCode::convert(BBCode::removeAttachment($body), false);
 
        // Workaround for clients with limited HTML parser functionality
        $search = ["<br>", "<blockquote>", "</blockquote>",
@@ -2552,6 +2560,7 @@ function api_get_attachments(&$body)
 {
        $text = $body;
        $text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $text);
+       $text = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $text);
 
        $URLSearchString = "^\[\]";
        $ret = preg_match_all("/\[img\]([$URLSearchString]*)\[\/img\]/ism", $text, $images);
@@ -2563,7 +2572,7 @@ function api_get_attachments(&$body)
        $attachments = [];
 
        foreach ($images[1] as $image) {
-               $imagedata = Image::getInfoFromURL($image);
+               $imagedata = Images::getInfoFromURLCached($image);
 
                if ($imagedata) {
                        $attachments[] = ["url" => $image, "mimetype" => $imagedata["mime"], "size" => $imagedata["size"]];
@@ -2710,7 +2719,7 @@ function api_get_entitities(&$text, $bbcode)
 
                $start = iconv_strpos($text, $url, $offset, "UTF-8");
                if (!($start === false)) {
-                       $image = Image::getInfoFromURL($url);
+                       $image = Images::getInfoFromURLCached($url);
                        if ($image) {
                                // If image cache is activated, then use the following sizes:
                                // thumb  (150), small (340), medium (600) and large (1024)
@@ -2718,19 +2727,19 @@ function api_get_entitities(&$text, $bbcode)
                                        $media_url = ProxyUtils::proxifyUrl($url);
 
                                        $sizes = [];
-                                       $scale = Image::getScalingDimensions($image[0], $image[1], 150);
+                                       $scale = Images::getScalingDimensions($image[0], $image[1], 150);
                                        $sizes["thumb"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
 
                                        if (($image[0] > 150) || ($image[1] > 150)) {
-                                               $scale = Image::getScalingDimensions($image[0], $image[1], 340);
+                                               $scale = Images::getScalingDimensions($image[0], $image[1], 340);
                                                $sizes["small"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
                                        }
 
-                                       $scale = Image::getScalingDimensions($image[0], $image[1], 600);
+                                       $scale = Images::getScalingDimensions($image[0], $image[1], 600);
                                        $sizes["medium"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
 
                                        if (($image[0] > 600) || ($image[1] > 600)) {
-                                               $scale = Image::getScalingDimensions($image[0], $image[1], 1024);
+                                               $scale = Images::getScalingDimensions($image[0], $image[1], 1024);
                                                $sizes["large"] = ["w" => $scale["width"], "h" => $scale["height"], "resize" => "fit"];
                                        }
                                } else {
@@ -2948,7 +2957,7 @@ function api_format_items_profiles($profile_row)
  */
 function api_format_items($items, $user_info, $filter_user = false, $type = "json")
 {
-       $a = \Friendica\BaseObject::getApp();
+       $a = BaseObject::getApp();
 
        $ret = [];
 
@@ -2982,7 +2991,7 @@ function api_format_items($items, $user_info, $filter_user = false, $type = "jso
  */
 function api_format_item($item, $type = "json", $status_user = null, $author_user = null, $owner_user = null)
 {
-       $a = \Friendica\BaseObject::getApp();
+       $a = BaseObject::getApp();
 
        if (empty($status_user) || empty($author_user) || empty($owner_user)) {
                list($status_user, $author_user, $owner_user) = api_item_get_user($a, $item);
@@ -4739,7 +4748,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        }
 
        if ($filetype == "") {
-               $filetype=Image::guessType($filename);
+               $filetype = Images::guessType($filename);
        }
        $imagedata = @getimagesize($src);
        if ($imagedata) {
@@ -4786,19 +4795,19 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        $height = $Image->getHeight();
 
        // create a new resource-id if not already provided
-       $hash = ($photo_id == null) ? Photo::newResource() : $photo_id;
+       $resource_id = ($photo_id == null) ? Photo::newResource() : $photo_id;
 
        if ($mediatype == "photo") {
                // upload normal image (scales 0, 1, 2)
                Logger::log("photo upload: starting new photo upload", Logger::DEBUG);
 
-               $r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 0, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
+               $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 0, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
                if (!$r) {
                        Logger::log("photo upload: image upload with scale 0 (original size) failed");
                }
                if ($width > 640 || $height > 640) {
                        $Image->scaleDown(640);
-                       $r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 1, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
+                       $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 1, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
                        if (!$r) {
                                Logger::log("photo upload: image upload with scale 1 (640x640) failed");
                        }
@@ -4806,7 +4815,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
 
                if ($width > 320 || $height > 320) {
                        $Image->scaleDown(320);
-                       $r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 2, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
+                       $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 2, 0, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
                        if (!$r) {
                                Logger::log("photo upload: image upload with scale 2 (320x320) failed");
                        }
@@ -4818,7 +4827,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
 
                if ($width > 300 || $height > 300) {
                        $Image->scaleDown(300);
-                       $r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 4, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
+                       $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 4, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
                        if (!$r) {
                                Logger::log("photo upload: profile image upload with scale 4 (300x300) failed");
                        }
@@ -4826,7 +4835,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
 
                if ($width > 80 || $height > 80) {
                        $Image->scaleDown(80);
-                       $r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 5, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
+                       $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 5, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
                        if (!$r) {
                                Logger::log("photo upload: profile image upload with scale 5 (80x80) failed");
                        }
@@ -4834,7 +4843,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
 
                if ($width > 48 || $height > 48) {
                        $Image->scaleDown(48);
-                       $r = Photo::store($Image, local_user(), $visitor, $hash, $filename, $album, 6, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
+                       $r = Photo::store($Image, local_user(), $visitor, $resource_id, $filename, $album, 6, $profile, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc);
                        if (!$r) {
                                Logger::log("photo upload: profile image upload with scale 6 (48x48) failed");
                        }
@@ -4846,10 +4855,10 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        if (isset($r) && $r) {
                // create entry in 'item'-table on new uploads to enable users to comment/like/dislike the photo
                if ($photo_id == null && $mediatype == "photo") {
-                       post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility);
+                       post_photo_item($resource_id, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility);
                }
                // on success return image data in json/xml format (like /api/friendica/photo does when no scale is given)
-               return prepare_photo_data($type, false, $hash);
+               return prepare_photo_data($type, false, $resource_id);
        } else {
                throw new InternalServerErrorException("image upload failed");
        }
@@ -5147,99 +5156,30 @@ function api_share_as_retweet(&$item)
                }
        }
 
-       /// @TODO "$1" should maybe mean '$1' ?
-       $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
-       /*
-        * Skip if there is no shared message in there
-        * we already checked this in diaspora::isReshare()
-        * but better one more than one less...
-        */
-       if (($body == $attributes) || empty($attributes)) {
+       $reshared = Item::getShareArray($item);
+       if (empty($reshared)) {
                return false;
        }
 
-       // build the fake reshared item
        $reshared_item = $item;
 
-       $author = "";
-       preg_match("/author='(.*?)'/ism", $attributes, $matches);
-       if (!empty($matches[1])) {
-               $author = html_entity_decode($matches[1], ENT_QUOTES, 'UTF-8');
-       }
-
-       preg_match('/author="(.*?)"/ism', $attributes, $matches);
-       if (!empty($matches[1])) {
-               $author = $matches[1];
-       }
-
-       $profile = "";
-       preg_match("/profile='(.*?)'/ism", $attributes, $matches);
-       if (!empty($matches[1])) {
-               $profile = $matches[1];
-       }
-
-       preg_match('/profile="(.*?)"/ism', $attributes, $matches);
-       if (!empty($matches[1])) {
-               $profile = $matches[1];
-       }
-
-       $avatar = "";
-       preg_match("/avatar='(.*?)'/ism", $attributes, $matches);
-       if (!empty($matches[1])) {
-               $avatar = $matches[1];
-       }
-
-       preg_match('/avatar="(.*?)"/ism', $attributes, $matches);
-       if (!empty($matches[1])) {
-               $avatar = $matches[1];
-       }
-
-       $link = "";
-       preg_match("/link='(.*?)'/ism", $attributes, $matches);
-       if (!empty($matches[1])) {
-               $link = $matches[1];
-       }
-
-       preg_match('/link="(.*?)"/ism', $attributes, $matches);
-       if (!empty($matches[1])) {
-               $link = $matches[1];
-       }
-
-       $posted = "";
-       preg_match("/posted='(.*?)'/ism", $attributes, $matches);
-       if (!empty($matches[1])) {
-               $posted = $matches[1];
-       }
-
-       preg_match('/posted="(.*?)"/ism', $attributes, $matches);
-       if (!empty($matches[1])) {
-               $posted = $matches[1];
-       }
-
-       if (!preg_match("/(.*?)\[share.*?\]\s?(.*?)\s?\[\/share\]\s?(.*?)/ism", $body, $matches)) {
+       if (empty($reshared['shared']) || empty($reshared['profile']) || empty($reshared['author']) || empty($reshared['avatar']) || empty($reshared['posted'])) {
                return false;
        }
 
-       $pre_body = trim($matches[1]);
-       if ($pre_body != '') {
-               $item['body'] = $pre_body;
-       }
-
-       $shared_body = trim($matches[2]);
-
-       if (($shared_body == "") || ($profile == "") || ($author == "") || ($avatar == "") || ($posted == "")) {
-               return false;
+       if (!empty($reshared['comment'])) {
+               $item['body'] = $reshared['comment'];
        }
 
-       $reshared_item["share-pre-body"] = $pre_body;
-       $reshared_item["body"] = $shared_body;
-       $reshared_item["author-id"] = Contact::getIdForURL($profile, 0, true);
-       $reshared_item["author-name"] = $author;
-       $reshared_item["author-link"] = $profile;
-       $reshared_item["author-avatar"] = $avatar;
-       $reshared_item["plink"] = $link;
-       $reshared_item["created"] = $posted;
-       $reshared_item["edited"] = $posted;
+       $reshared_item["share-pre-body"] = $reshared['comment'];
+       $reshared_item["body"] = $reshared['shared'];
+       $reshared_item["author-id"] = Contact::getIdForURL($reshared['profile'], 0, true);
+       $reshared_item["author-name"] = $reshared['author'];
+       $reshared_item["author-link"] = $reshared['profile'];
+       $reshared_item["author-avatar"] = $reshared['avatar'];
+       $reshared_item["plink"] = $reshared['link'] ?? '';
+       $reshared_item["created"] = $reshared['posted'];
+       $reshared_item["edited"] = $reshared['posted'];
 
        return $reshared_item;
 }
@@ -5406,42 +5346,11 @@ function api_clean_plain_items($text)
        }
 
        // Simplify "attachment" element
-       $text = api_clean_attachments($text);
+       $text = BBCode::removeAttachment($text);
 
        return $text;
 }
 
-/**
- * @brief Removes most sharing information for API text export
- *
- * @param string $body The original body
- *
- * @return string Cleaned body
- * @throws InternalServerErrorException
- */
-function api_clean_attachments($body)
-{
-       $data = BBCode::getAttachmentData($body);
-
-       if (empty($data)) {
-               return $body;
-       }
-       $body = "";
-
-       if (isset($data["text"])) {
-               $body = $data["text"];
-       }
-       if (($body == "") && isset($data["title"])) {
-               $body = $data["title"];
-       }
-       if (isset($data["url"])) {
-               $body .= "\n".$data["url"];
-       }
-       $body .= $data["after"];
-
-       return $body;
-}
-
 /**
  *
  * @param array $contacts
@@ -6040,7 +5949,8 @@ function api_friendica_notification($type)
        if ($a->argc!==3) {
                throw new BadRequestException("Invalid argument count");
        }
-       $nm = new NotificationsManager();
+       /** @var Notify $nm */
+       $nm = BaseObject::getClass(Notify::class);
 
        $notes = $nm->getAll([], ['seen' => 'ASC', 'date' => 'DESC'], 50);
 
@@ -6084,7 +5994,8 @@ function api_friendica_notification_seen($type)
 
        $id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0);
 
-       $nm = new NotificationsManager();
+       /** @var Notify $nm */
+       $nm = BaseObject::getClass(Notify::class);
        $note = $nm->getByID($id);
        if (is_null($note)) {
                throw new BadRequestException("Invalid argument");