]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Fix image links with descriptions in API and AP transmission
[friendica.git] / include / api.php
index 91e31ac9459bb5864ca69075f046b518d50f5cad..8325e39b71442f6518054ac1293ea3657ae666ab 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;
@@ -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'] . ']';
@@ -1375,7 +1377,7 @@ function api_get_item(array $condition)
  */
 function api_users_show($type)
 {
-       $a = \Friendica\BaseObject::getApp();
+       $a = BaseObject::getApp();
 
        $user_info = api_get_user($a);
 
@@ -2552,6 +2554,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 +2566,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 +2713,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 +2721,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 +2951,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 +2985,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 +4742,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 +4789,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 +4809,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 +4821,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 +4829,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 +4837,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 +4849,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");
        }
@@ -6040,7 +6043,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 +6088,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");