X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseModule.php;h=08efff3d7858f4dec0a07c2536e4715c4c8f3957;hb=7ec4d42461f6fb8397e55df27a83c889c31c7f6d;hp=be4788045c2d283d73c5c30080c1ebf661ee7710;hpb=f00792d370cfbf1d52b37e3eed7e80e682df9d8a;p=friendica.git diff --git a/src/BaseModule.php b/src/BaseModule.php index be4788045c..08efff3d78 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -1,6 +1,6 @@ parameters = $parameters; $this->l10n = $l10n; + $this->baseUrl = $baseUrl; + $this->args = $args; + $this->logger = $logger; + $this->profiler = $profiler; + $this->server = $server; + $this->response = $response; } /** @@ -56,7 +82,7 @@ abstract class BaseModule implements ICanHandleRequests */ protected function t(string $s, ...$args): string { - return $this->l10n->t($s, $args); + return $this->l10n->t($s, ...$args); } /** @@ -70,55 +96,196 @@ abstract class BaseModule implements ICanHandleRequests } /** - * {@inheritDoc} + * Module GET method to display raw content from technical endpoints + * + * Extend this method if the module is supposed to return communication data, + * e.g. from protocol implementations. + * + * @param string[] $request The $_REQUEST content */ - public function rawContent() + protected function rawContent(array $request = []) { // echo ''; // exit; } /** - * {@inheritDoc} + * Module GET method to display any content + * + * Extend this method if the module is supposed to return any display + * through a GET request. It can be an HTML page through templating or a + * XML feed or a JSON output. + * + * @param string[] $request The $_REQUEST content */ - public function content(): string + protected function content(array $request = []): string { return ''; } /** - * {@inheritDoc} + * Module DELETE method to process submitted data + * + * Extend this method if the module is supposed to process DELETE requests. + * Doesn't display any content + * + * @param string[] $request The $_REQUEST content */ - public function delete() + protected function delete(array $request = []) { } /** - * {@inheritDoc} + * Module PATCH method to process submitted data + * + * Extend this method if the module is supposed to process PATCH requests. + * Doesn't display any content + * + * @param string[] $request The $_REQUEST content */ - public function patch() + protected function patch(array $request = []) { } /** - * {@inheritDoc} + * Module POST method to process submitted data + * + * Extend this method if the module is supposed to process POST requests. + * Doesn't display any content + * + * @param string[] $request The $_REQUEST content + * + */ + protected function post(array $request = []) + { + // $this->baseUrl->redirect('module'); + } + + /** + * Module PUT method to process submitted data + * + * Extend this method if the module is supposed to process PUT requests. + * Doesn't display any content + * + * @param string[] $request The $_REQUEST content */ - public function post() + protected function put(array $request = []) { - // DI::baseurl()->redirect('module'); } /** * {@inheritDoc} */ - public function put() + public function run(array $request = []): ResponseInterface { + // @see https://github.com/tootsuite/mastodon/blob/c3aef491d66aec743a3a53e934a494f653745b61/config/initializers/cors.rb + if (substr($request['pagename'] ?? '', 0, 12) == '.well-known/') { + $this->response->setHeader('*', 'Access-Control-Allow-Origin'); + $this->response->setHeader('*', 'Access-Control-Allow-Headers'); + $this->response->setHeader(Router::GET, 'Access-Control-Allow-Methods'); + $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); + } elseif (substr($request['pagename'] ?? '', 0, 8) == 'profile/') { + $this->response->setHeader('*', 'Access-Control-Allow-Origin'); + $this->response->setHeader('*', 'Access-Control-Allow-Headers'); + $this->response->setHeader(Router::GET, 'Access-Control-Allow-Methods'); + $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); + } elseif (substr($request['pagename'] ?? '', 0, 4) == 'api/') { + $this->response->setHeader('*', 'Access-Control-Allow-Origin'); + $this->response->setHeader('*', 'Access-Control-Allow-Headers'); + $this->response->setHeader(implode(',', Router::ALLOWED_METHODS), 'Access-Control-Allow-Methods'); + $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); + $this->response->setHeader('Link', 'Access-Control-Expose-Headers'); + } elseif (substr($request['pagename'] ?? '', 0, 11) == 'oauth/token') { + $this->response->setHeader('*', 'Access-Control-Allow-Origin'); + $this->response->setHeader('*', 'Access-Control-Allow-Headers'); + $this->response->setHeader(Router::POST, 'Access-Control-Allow-Methods'); + $this->response->setHeader('false', 'Access-Control-Allow-Credentials'); + } + + $placeholder = ''; + + $this->profiler->set(microtime(true), 'ready'); + $timestamp = microtime(true); + + Core\Hook::callAll($this->args->getModuleName() . '_mod_init', $placeholder); + + $this->profiler->set(microtime(true) - $timestamp, 'init'); + + switch ($this->server['REQUEST_METHOD'] ?? Router::GET) { + case Router::DELETE: + $this->delete($request); + break; + case Router::PATCH: + $this->patch($request); + break; + case Router::POST: + Core\Hook::callAll($this->args->getModuleName() . '_mod_post', $request); + $this->post($request); + break; + case Router::PUT: + $this->put($request); + break; + } + + $timestamp = microtime(true); + // "rawContent" is especially meant for technical endpoints. + // This endpoint doesn't need any theme initialization or other comparable stuff. + $this->rawContent($request); + + try { + $arr = ['content' => '']; + Hook::callAll(static::class . '_mod_content', $arr); + $this->response->addContent($arr['content']); + $this->response->addContent($this->content($request)); + } catch (HTTPException $e) { + $this->response->addContent((new ModuleHTTPException())->content($e)); + } finally { + $this->profiler->set(microtime(true) - $timestamp, 'content'); + } + + return $this->response->generate(); } - /** Gets the name of the current class */ - public function getClassName(): string + /** + * Checks request inputs and sets default parameters + * + * @param array $defaults Associative array of expected request keys and their default typed value. A null + * value will remove the request key from the resulting value array. + * @param array $input Custom REQUEST array, superglobal instead + * + * @return array Request data + */ + protected function checkDefaults(array $defaults, array $input): array { - return static::class; + $request = []; + + foreach ($defaults as $parameter => $defaultvalue) { + if (is_string($defaultvalue)) { + $request[$parameter] = $input[$parameter] ?? $defaultvalue; + } elseif (is_int($defaultvalue)) { + $request[$parameter] = (int)($input[$parameter] ?? $defaultvalue); + } elseif (is_float($defaultvalue)) { + $request[$parameter] = (float)($input[$parameter] ?? $defaultvalue); + } elseif (is_array($defaultvalue)) { + $request[$parameter] = $input[$parameter] ?? []; + } elseif (is_bool($defaultvalue)) { + $request[$parameter] = in_array(strtolower($input[$parameter] ?? ''), ['true', '1']); + } else { + $this->logger->notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($defaultvalue)]); + } + } + + foreach ($input ?? [] as $parameter => $value) { + if ($parameter == 'pagename') { + continue; + } + if (!in_array($parameter, array_keys($defaults))) { + $this->logger->notice('Unhandled request field', ['parameter' => $parameter, 'value' => $value, 'command' => $this->args->getCommand()]); + } + } + + $this->logger->debug('Got request parameters', ['request' => $request, 'command' => $this->args->getCommand()]); + return $request; } /* @@ -134,9 +301,9 @@ abstract class BaseModule implements ICanHandleRequests */ public static function getFormSecurityToken($typename = '') { - $user = User::getById(DI::app()->getLoggedInUserId(), ['guid', 'prvkey']); + $user = User::getById(DI::app()->getLoggedInUserId(), ['guid', 'prvkey']); $timestamp = time(); - $sec_hash = hash('whirlpool', ($user['guid'] ?? '') . ($user['prvkey'] ?? '') . session_id() . $timestamp . $typename); + $sec_hash = hash('whirlpool', ($user['guid'] ?? '') . ($user['prvkey'] ?? '') . session_id() . $timestamp . $typename); return $timestamp . '.' . $sec_hash; }