diff --git a/src/App/Page.php b/src/App/Page.php index 364da32eb0..08efac627f 100644 --- a/src/App/Page.php +++ b/src/App/Page.php @@ -496,7 +496,9 @@ class Page implements ArrayAccess } if ($_GET["mode"] == "raw") { - System::httpExit(substr($target->saveHTML(), 6, -8), Response::TYPE_HTML); + $response->withBody(Utils::streamFor($target->saveHTML())); + System::echoResponse($response); + System::exit(); } } diff --git a/src/BaseModule.php b/src/BaseModule.php index 619095dfb6..d97cd60a94 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -27,6 +27,7 @@ use Friendica\Capabilities\ICanCreateResponses; use Friendica\Core\Hook; use Friendica\Core\L10n; use Friendica\Core\Logger; +use Friendica\Core\System; use Friendica\Model\User; use Friendica\Module\Response; use Friendica\Module\Special\HTTPException as ModuleHTTPException; @@ -106,8 +107,7 @@ abstract class BaseModule implements ICanHandleRequests */ protected function rawContent(array $request = []) { - // echo ''; - // exit; + // $this->httpExit(...); } /** @@ -234,7 +234,8 @@ abstract class BaseModule implements ICanHandleRequests $timestamp = microtime(true); // "rawContent" is especially meant for technical endpoints. - // This endpoint doesn't need any theme initialization or other comparable stuff. + // This endpoint doesn't need any theme initialization or + // templating and is expected to exit on its own if it is set. $this->rawContent($request); try { @@ -456,4 +457,23 @@ abstract class BaseModule implements ICanHandleRequests return $tabs; } + + /** + * This function adds the content and a content-type HTTP header to the output. + * After finishing the process is getting killed. + * + * @param string $content + * @param string $type + * @param string|null $content_type + * @return void + * @throws HTTPException\InternalServerErrorException + */ + public function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null) + { + $this->response->setType($type, $content_type); + $this->response->addContent($content); + System::echoResponse($this->response->generate()); + + System::exit(); + } } diff --git a/src/Capabilities/ICanCreateResponses.php b/src/Capabilities/ICanCreateResponses.php index 300b1938aa..837e0cd886 100644 --- a/src/Capabilities/ICanCreateResponses.php +++ b/src/Capabilities/ICanCreateResponses.php @@ -70,7 +70,7 @@ interface ICanCreateResponses * * @throws InternalServerErrorException */ - public function setType(string $type, ?string $content_type = null): void; + public function setType(string $type = ICanCreateResponses::TYPE_HTML, ?string $content_type = null): void; /** * Sets the status and the reason for the response diff --git a/src/Core/System.php b/src/Core/System.php index 5c2cd8dc12..e1e84b775b 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -28,6 +28,7 @@ use Friendica\DI; use Friendica\Model\User; use Friendica\Module\Response; use Friendica\Network\HTTPException\FoundException; +use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\MovedPermanentlyException; use Friendica\Network\HTTPException\TemporaryRedirectException; use Friendica\Util\BasePath; @@ -357,12 +358,15 @@ class System * This function adds the content and a content-type HTTP header to the output. * After finishing the process is getting killed. * - * @param string $content - * @param string $type + * @param string $content + * @param string $type * @param string|null $content_type * @return void + * @throws InternalServerErrorException + * @deprecated since 2023.09 Use BaseModule->httpExit() instead */ - public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null) { + public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null) + { DI::apiResponse()->setType($type, $content_type); DI::apiResponse()->addContent($content); self::echoResponse(DI::apiResponse()->generate()); diff --git a/src/Module/Calendar/Event/API.php b/src/Module/Calendar/Event/API.php index e8fce8ed7e..af0c9fb8a6 100644 --- a/src/Module/Calendar/Event/API.php +++ b/src/Module/Calendar/Event/API.php @@ -183,7 +183,7 @@ class API extends BaseModule if (strcmp($finish, $start) < 0 && !$noFinish) { if ($isPreview) { - System::httpExit($this->t('Event can not end before it has started.')); + $this->httpExit($this->t('Event can not end before it has started.')); } else { $this->sysMessages->addNotice($this->t('Event can not end before it has started.')); $this->baseUrl->redirect($redirectOnError); @@ -192,7 +192,7 @@ class API extends BaseModule if (empty($summary) || ($start === DBA::NULL_DATETIME)) { if ($isPreview) { - System::httpExit($this->t('Event title and start time are required.')); + $this->httpExit($this->t('Event title and start time are required.')); } else { $this->sysMessages->addNotice($this->t('Event title and start time are required.')); $this->baseUrl->redirect($redirectOnError); @@ -251,7 +251,7 @@ class API extends BaseModule ]; if (intval($request['preview'])) { - System::httpExit(Event::getHTML($datarray)); + $this->httpExit(Event::getHTML($datarray)); } $eventId = Event::store($datarray); diff --git a/src/Module/Calendar/Event/Show.php b/src/Module/Calendar/Event/Show.php index b6a0bd56cc..22c00d855f 100644 --- a/src/Module/Calendar/Event/Show.php +++ b/src/Module/Calendar/Event/Show.php @@ -81,6 +81,6 @@ class Show extends BaseModule '$event' => $tplEvent, ]); - System::httpExit($o); + $this->httpExit($o); } } diff --git a/src/Module/Contact/Hovercard.php b/src/Module/Contact/Hovercard.php index 4d58d994c2..9f2fb67bdb 100644 --- a/src/Module/Contact/Hovercard.php +++ b/src/Module/Contact/Hovercard.php @@ -115,6 +115,6 @@ class Hovercard extends BaseModule ], ]); - System::httpExit($o); + $this->httpExit($o); } } diff --git a/src/Module/DFRN/Poll.php b/src/Module/DFRN/Poll.php index dbf91712fc..3a8d370e9e 100644 --- a/src/Module/DFRN/Poll.php +++ b/src/Module/DFRN/Poll.php @@ -48,6 +48,6 @@ class Poll extends BaseModule } $last_update = $request['last_update'] ?? ''; - System::httpExit(OStatus::feed($owner['nickname'], $last_update, 10) ?? '', Response::TYPE_ATOM); + $this->httpExit(OStatus::feed($owner['nickname'], $last_update, 10) ?? '', Response::TYPE_ATOM); } } diff --git a/src/Module/Diaspora/Fetch.php b/src/Module/Diaspora/Fetch.php index 6e1259152f..28b3d87ee3 100644 --- a/src/Module/Diaspora/Fetch.php +++ b/src/Module/Diaspora/Fetch.php @@ -85,6 +85,6 @@ class Fetch extends BaseModule $xml = Diaspora::buildPostXml($status["type"], $status["message"]); // Send the envelope - System::httpExit(Diaspora::buildMagicEnvelope($xml, $user), Response::TYPE_XML, 'application/magic-envelope+xml'); + $this->httpExit(Diaspora::buildMagicEnvelope($xml, $user), Response::TYPE_XML, 'application/magic-envelope+xml'); } } diff --git a/src/Module/Feed.php b/src/Module/Feed.php index 6d291198dc..b9d573979a 100644 --- a/src/Module/Feed.php +++ b/src/Module/Feed.php @@ -71,6 +71,6 @@ class Feed extends BaseModule $feed = ProtocolFeed::atom($owner, $last_update, 10, $type); - System::httpExit($feed, Response::TYPE_ATOM); + $this->httpExit($feed, Response::TYPE_ATOM); } } diff --git a/src/Module/Item/Feed.php b/src/Module/Item/Feed.php index 5cca0dfd63..80a14e4bd4 100644 --- a/src/Module/Item/Feed.php +++ b/src/Module/Item/Feed.php @@ -86,6 +86,6 @@ class Feed extends BaseModule throw new HTTPException\InternalServerErrorException($this->t('The feed for this item is unavailable.', ['uri-id' => $uriId])); } - System::httpExit($xml, Response::TYPE_ATOM); + $this->httpExit($xml, Response::TYPE_ATOM); } } diff --git a/src/Module/Media/Attachment/Browser.php b/src/Module/Media/Attachment/Browser.php index 80a87d5bde..5ff070cf96 100644 --- a/src/Module/Media/Attachment/Browser.php +++ b/src/Module/Media/Attachment/Browser.php @@ -80,7 +80,7 @@ class Browser extends BaseModule ]); if (empty($request['mode'])) { - System::httpExit($output); + $this->httpExit($output); } return $output; diff --git a/src/Module/Media/Photo/Browser.php b/src/Module/Media/Photo/Browser.php index bf545d0db6..be0e931f90 100644 --- a/src/Module/Media/Photo/Browser.php +++ b/src/Module/Media/Photo/Browser.php @@ -91,7 +91,7 @@ class Browser extends BaseModule ]); if (empty($request['mode'])) { - System::httpExit($output); + $this->httpExit($output); } return $output; diff --git a/src/Module/Notifications/Ping.php b/src/Module/Notifications/Ping.php index 603d6408c7..3b44bbe914 100644 --- a/src/Module/Notifications/Ping.php +++ b/src/Module/Notifications/Ping.php @@ -303,7 +303,7 @@ class Ping extends BaseModule if (isset($_GET['callback'])) { // JSONP support - System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript'); + $this->httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript'); } else { System::jsonExit(['result' => $data]); } diff --git a/src/Module/OStatus/PubSub.php b/src/Module/OStatus/PubSub.php index f1f3266cb8..1271d85ebe 100644 --- a/src/Module/OStatus/PubSub.php +++ b/src/Module/OStatus/PubSub.php @@ -155,6 +155,6 @@ class PubSub extends \Friendica\BaseModule $this->logger->notice('Success for contact.', ['mode' => $hub_mode, 'contact' => $contact_id]); } - System::httpExit($hub_challenge); + $this->httpExit($hub_challenge); } } diff --git a/src/Module/OpenSearch.php b/src/Module/OpenSearch.php index 7c44bc510e..1a4303a683 100644 --- a/src/Module/OpenSearch.php +++ b/src/Module/OpenSearch.php @@ -85,6 +85,6 @@ class OpenSearch extends BaseModule 'template' => "$baseUrl/opensearch", ]); - System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/opensearchdescription+xml'); + $this->httpExit($xml->saveXML(), Response::TYPE_XML, 'application/opensearchdescription+xml'); } } diff --git a/src/Module/ParseUrl.php b/src/Module/ParseUrl.php index e2d75ee844..dfccafad42 100644 --- a/src/Module/ParseUrl.php +++ b/src/Module/ParseUrl.php @@ -102,7 +102,7 @@ class ParseUrl extends BaseModule if ($format == 'json') { System::jsonExit($arr['text']); } else { - System::httpExit($arr['text']); + $this->httpExit($arr['text']); } } @@ -135,7 +135,7 @@ class ParseUrl extends BaseModule System::jsonExit($ret); } else { - System::httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? '')); + $this->httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? '')); } } } diff --git a/src/Module/PermissionTooltip.php b/src/Module/PermissionTooltip.php index 4e5191e5a4..9db90538c5 100644 --- a/src/Module/PermissionTooltip.php +++ b/src/Module/PermissionTooltip.php @@ -166,9 +166,9 @@ class PermissionTooltip extends \Friendica\BaseModule } if (!empty($l)) { - System::httpExit($o . implode(', ', $l)); + $this->httpExit($o . implode(', ', $l)); } else { - System::httpExit($o . $receivers);; + $this->httpExit($o . $receivers);; } } diff --git a/src/Module/Post/Share.php b/src/Module/Post/Share.php index db95c38c94..210a3fa6bc 100644 --- a/src/Module/Post/Share.php +++ b/src/Module/Post/Share.php @@ -74,6 +74,6 @@ class Share extends \Friendica\BaseModule $content = '[share]' . $item['uri'] . '[/share]'; } - System::httpExit($content); + $this->httpExit($content); } } diff --git a/src/Module/PublicRSAKey.php b/src/Module/PublicRSAKey.php index 810a9210cd..289e5aaa9a 100644 --- a/src/Module/PublicRSAKey.php +++ b/src/Module/PublicRSAKey.php @@ -45,7 +45,7 @@ class PublicRSAKey extends BaseModule throw new BadRequestException(); } - System::httpExit( + $this->httpExit( Salmon::salmonKey($user['spubkey']), Response::TYPE_BLANK, 'application/magic-public-key' diff --git a/src/Module/ReallySimpleDiscovery.php b/src/Module/ReallySimpleDiscovery.php index 6d608063e7..4dc00240aa 100644 --- a/src/Module/ReallySimpleDiscovery.php +++ b/src/Module/ReallySimpleDiscovery.php @@ -67,6 +67,6 @@ class ReallySimpleDiscovery extends BaseModule ], ], ]); - System::httpExit($content, Response::TYPE_XML); + $this->httpExit($content, Response::TYPE_XML); } } diff --git a/src/Module/Response.php b/src/Module/Response.php index d38b330bb3..78db953bb4 100644 --- a/src/Module/Response.php +++ b/src/Module/Response.php @@ -89,7 +89,7 @@ class Response implements ICanCreateResponses /** * {@inheritDoc} */ - public function setType(string $type, ?string $content_type = null): void + public function setType(string $type = Response::TYPE_HTML, ?string $content_type = null): void { if (!in_array($type, static::ALLOWED_TYPES)) { throw new InternalServerErrorException('wrong type'); diff --git a/src/Module/WellKnown/HostMeta.php b/src/Module/WellKnown/HostMeta.php index eadc9f221a..d787f10986 100644 --- a/src/Module/WellKnown/HostMeta.php +++ b/src/Module/WellKnown/HostMeta.php @@ -90,6 +90,6 @@ class HostMeta extends BaseModule ], ], $xml, false, ['hm' => 'http://host-meta.net/xrd/1.0', 'mk' => 'http://salmon-protocol.org/ns/magic-key']); - System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml'); + $this->httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml'); } } diff --git a/src/Module/Xrd.php b/src/Module/Xrd.php index af40bc3b02..40358250e6 100644 --- a/src/Module/Xrd.php +++ b/src/Module/Xrd.php @@ -330,6 +330,6 @@ class Xrd extends BaseModule ]); header('Access-Control-Allow-Origin: *'); - System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml'); + $this->httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml'); } }