]> git.mxchange.org Git - friendica.git/blobdiff - src/BaseModule.php
Merge pull request #13533 from nupplaphil/feat/phpunit_upgrade
[friendica.git] / src / BaseModule.php
index d97cd60a9477c9fefb701cb70b6880ce879bdb82..1cdf96dff69421ed5e930012215b4068f3a74e24 100644 (file)
@@ -33,6 +33,7 @@ use Friendica\Module\Response;
 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
 use Friendica\Network\HTTPException;
 use Friendica\Util\Profiler;
+use Friendica\Util\XML;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Log\LoggerInterface;
 
@@ -476,4 +477,57 @@ abstract class BaseModule implements ICanHandleRequests
 
                System::exit();
        }
+
+       /**
+        * Send HTTP status header and exit.
+        *
+        * @param integer $httpCode HTTP status result value
+        * @param string  $message  Error message. Optional.
+        * @param mixed  $content   Response body. Optional.
+        * @throws \Exception
+        */
+       public function httpError(int $httpCode, string $message = '', $content = '')
+       {
+               if ($httpCode >= 400) {
+                       $this->logger->debug('Exit with error', ['code' => $httpCode, 'message' => $message, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
+               }
+
+               $this->response->setStatus($httpCode, $message);
+
+               $this->httpExit($content);
+       }
+
+       /**
+        * Display the response using JSON to encode the content
+        *
+        * @param mixed  $content
+        * @param string $content_type
+        * @param int    $options A combination of json_encode() binary flags
+        * @return void
+        * @throws HTTPException\InternalServerErrorException
+        * @see json_encode()
+        */
+       public function jsonExit($content, string $content_type = 'application/json', int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
+       {
+               $this->httpExit(json_encode($content, $options), ICanCreateResponses::TYPE_JSON, $content_type);
+       }
+
+       /**
+        * Display a non-200 HTTP code response using JSON to encode the content and exit
+        *
+        * @param int    $httpCode
+        * @param mixed  $content
+        * @param string $content_type
+        * @return void
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function jsonError(int $httpCode, $content, string $content_type = 'application/json')
+       {
+               if ($httpCode >= 400) {
+                       $this->logger->debug('Exit with error', ['code' => $httpCode, 'content_type' => $content_type, 'callstack' => System::callstack(20), 'method' => $this->args->getMethod(), 'agent' => $this->server['HTTP_USER_AGENT'] ?? '']);
+               }
+
+               $this->response->setStatus($httpCode);
+               $this->jsonExit($content, $content_type);
+       }
 }