]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Proxy.php
Merge pull request #12895 from HankG/mastodon-status-edit-fix-deleting-all-images
[friendica.git] / src / Module / Proxy.php
index 15ef1c44128b70639fc337ef560a56f8023647f4..4d1e1c304b082e912ef54abe6d70756e5b96ceb6 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -25,6 +25,9 @@ use Friendica\BaseModule;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
+use Friendica\Network\HTTPClient\Client\HttpClientOptions;
+use Friendica\Network\HTTPException\NotModifiedException;
 use Friendica\Object\Image;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\Images;
@@ -43,37 +46,36 @@ class Proxy extends BaseModule
        /**
         * Fetch remote image content
         */
-       public static function rawContent(array $parameters = [])
+       protected function rawContent(array $request = [])
        {
-               $request = self::getRequestInfo($parameters);
+               $request = $this->getRequestInfo();
 
                if (!DI::config()->get('system', 'proxify_content')) {
                        Logger::notice('Proxy access is forbidden', ['request' => $request, 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '', 'accept' => $_SERVER['HTTP_ACCEPT'] ?? '']);
                        throw new \Friendica\Network\HTTPException\NotFoundException();
                }
 
-               if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
-                       header("HTTP/1.1 304 Not Modified");
-                       header("Last-Modified: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
-                       if (!empty($_SERVER["HTTP_IF_NONE_MATCH"])) {
-                               header("Etag: " . $_SERVER["HTTP_IF_NONE_MATCH"]);
+               if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
+                       header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
+                       if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
+                               header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
                        }
-                       header("Expires: " . gmdate("D, d M Y H:i:s", time() + (31536000)) . " GMT");
-                       header("Cache-Control: max-age=31536000");
-                       if (function_exists("header_remove")) {
-                               header_remove("Last-Modified");
-                               header_remove("Expires");
-                               header_remove("Cache-Control");
+                       header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
+                       header('Cache-Control: max-age=31536000');
+                       if (function_exists('header_remove')) {
+                               header_remove('Last-Modified');
+                               header_remove('Expires');
+                               header_remove('Cache-Control');
                        }
-                       exit;
+                       throw new NotModifiedException();
                }
 
                if (empty($request['url'])) {
                        throw new \Friendica\Network\HTTPException\BadRequestException();
                }
 
-               if (!local_user()) {
-                       Logger::info('Redirecting not logged in user to original address', ['url' => $request['url']]);
+               if (!DI::userSession()->getLocalUserId()) {
+                       Logger::debug('Redirecting not logged in user to original address', ['url' => $request['url']]);
                        System::externalRedirect($request['url']);
                }
 
@@ -81,20 +83,27 @@ class Proxy extends BaseModule
                $request['url'] = str_replace(' ', '+', $request['url']);
 
                // Fetch the content with the local user
-               $fetchResult = HTTPSignature::fetchRaw($request['url'], local_user(), ['accept_content' => [], 'timeout' => 10]);
-               $img_str = $fetchResult->getBody();
-
-               if (!$fetchResult->isSuccess() || empty($img_str)) {
-                       Logger::info('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
+               try {
+                       $fetchResult = HTTPSignature::fetchRaw($request['url'], DI::userSession()->getLocalUserId(), [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::IMAGE], 'timeout' => 10]);
+                       $img_str = $fetchResult->getBody();
+
+                       if (!$fetchResult->isSuccess() || empty($img_str)) {
+                               Logger::notice('Error fetching image', ['image' => $request['url'], 'return' => $fetchResult->getReturnCode(), 'empty' => empty($img_str)]);
+                               self::responseError();
+                               // stop.
+                       }
+               } catch (\Exception $exception) {
+                       Logger::notice('Error fetching image', ['image' => $request['url'], 'exception' => $exception]);
                        self::responseError();
-                       // stop.
                }
 
+               Logger::debug('Got picture', ['Content-Type' => $fetchResult->getHeader('Content-Type'), 'uid' => DI::userSession()->getLocalUserId(), 'image' => $request['url']]);
+
                $mime = Images::getMimeTypeByData($img_str);
 
                $image = new Image($img_str, $mime);
                if (!$image->isValid()) {
-                       Logger::info('The image is invalid', ['image' => $request['url'], 'mime' => $mime]);
+                       Logger::notice('The image is invalid', ['image' => $request['url'], 'mime' => $mime]);
                        self::responseError();
                        // stop.
                }
@@ -119,13 +128,13 @@ class Proxy extends BaseModule
         *    ]
         * @throws \Exception
         */
-       private static function getRequestInfo(array $parameters)
+       private function getRequestInfo(): array
        {
                $size = ProxyUtils::PIXEL_LARGE;
                $sizetype = '';
 
-               if (!empty($parameters['url']) && empty($_REQUEST['url'])) {
-                       $url = $parameters['url'];
+               if (!empty($this->parameters['url']) && empty($_REQUEST['url'])) {
+                       $url = $this->parameters['url'];
 
                        // thumb, small, medium and large.
                        if (substr($url, -6) == ':micro') {
@@ -183,12 +192,13 @@ class Proxy extends BaseModule
         * Output the image with cache headers
         *
         * @param Image $img
+        * @return void
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function responseImageHttpCache(Image $img)
        {
                if (is_null($img) || !$img->isValid()) {
-                       Logger::info('The cached image is invalid');
+                       Logger::notice('The cached image is invalid');
                        self::responseError();
                        // stop.
                }
@@ -198,6 +208,6 @@ class Proxy extends BaseModule
                header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
                header('Cache-Control: max-age=31536000');
                echo $img->asString();
-               exit();
+               System::exit();
        }
 }