From 8fc1142e81a45c348ef713e9c4e89778c27480e3 Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 03:26:20 +0530 Subject: [PATCH 01/17] feat: remove toro, serve now uses Http\Request and returns Http\Response --- Idno/Common/Page.php | 15 +++++---- Idno/Core/Hook.php | 40 +++++++++++++++++++++++ Idno/Core/Http/Request.php | 11 +++++++ Idno/Core/Http/Response.php | 11 +++++++ Idno/Core/Idno.php | 27 +++++++++++++++ Idno/Core/PageHandler.php | 65 ++++++++++++++++++++++++++++++++++++- composer.json | 2 +- composer.lock | 16 ++++----- index.php | 7 ++-- 9 files changed, 175 insertions(+), 19 deletions(-) create mode 100644 Idno/Core/Hook.php create mode 100644 Idno/Core/Http/Request.php create mode 100644 Idno/Core/Http/Response.php diff --git a/Idno/Common/Page.php b/Idno/Common/Page.php index 32ccf31a91..30fab515c7 100644 --- a/Idno/Common/Page.php +++ b/Idno/Common/Page.php @@ -18,6 +18,7 @@ namespace Idno\Common { use Idno\Entities\User; + use Idno\Core\Http\Response; abstract class Page extends \Idno\Common\Component { @@ -166,7 +167,8 @@ function setResponse(int $code) { $code = (int)$code; $this->response = $code; - http_response_code($this->response); + \Idno\Core\Idno::site()->response()->setStatusCode($code); + } /** @@ -818,14 +820,15 @@ function goneContent() function noContent() { $this->setResponse(404); - http_response_code($this->response); + + // http_response_code($this->response); + \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Ts'); + \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Token'); - header_remove('X-Known-CSRF-Ts'); - header_remove('X-Known-CSRF-Token'); $t = \Idno\Core\Idno::site()->template(); - $t->__(array('body' => $t->draw('pages/404'), 'title' => \Idno\Core\Idno::site()->language()->_('This page can\'t be found.')))->drawPage(); - exit; + $content = $t->__(array('body' => $t->draw('pages/404'), 'title' => \Idno\Core\Idno::site()->language()->_('This page can\'t be found.')))->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } /** diff --git a/Idno/Core/Hook.php b/Idno/Core/Hook.php new file mode 100644 index 0000000000..302daa6c2b --- /dev/null +++ b/Idno/Core/Hook.php @@ -0,0 +1,40 @@ +hooks[$hook_name][] = $fn; + } + + public static function fire($hook_name, $params = null) + { + $instance = self::get_instance(); + if (isset($instance->hooks[$hook_name])) { + foreach ($instance->hooks[$hook_name] as $fn) { + call_user_func_array($fn, array(&$params)); + } + } + } + + public static function get_instance() + { + if (empty(self::$instance)) { + self::$instance = new Hook(); + } + return self::$instance; + } + } +} \ No newline at end of file diff --git a/Idno/Core/Http/Request.php b/Idno/Core/Http/Request.php new file mode 100644 index 0000000000..3affe5cd3b --- /dev/null +++ b/Idno/Core/Http/Request.php @@ -0,0 +1,11 @@ +request = Request::createFromGlobals(); + $this->response = new \Idno\Core\Http\Response(); $this->routes = new PageHandler(); $this->dispatcher = new EventDispatcher(); $this->config = new Config(); @@ -307,6 +314,26 @@ function &events() : ?EventDispatcher return $this->dispatcher; } + /** + * Return the request object loaded as part of this site + * + * @return \Idno\Core\Http\Request + */ + function &request() : ?Request + { + return $this->request; + } + + /** + * Return the response object loaded as part of this site + * + * @return \Idno\Core\Http\Response + */ + function &response() : ?Response + { + return $this->response; + } + /** * Access to the EventQueue for dispatching events * asynchronously diff --git a/Idno/Core/PageHandler.php b/Idno/Core/PageHandler.php index 93cbb463e0..b6ce8e5faf 100644 --- a/Idno/Core/PageHandler.php +++ b/Idno/Core/PageHandler.php @@ -12,7 +12,7 @@ /** * Routing class. */ - class PageHandler extends \Toro implements \ArrayAccess, \Iterator + class PageHandler implements \ArrayAccess, \Iterator { private $routes = []; @@ -28,6 +28,69 @@ function routeTokens() ); } + public static function serve($routes): \Idno\Core\Http\Response + { + Hook::fire('before_request', compact('routes')); + + $request_method = strtolower(Idno::site()->request()->getMethod()); + $path_info = '/'; + + if (! empty(Idno::site()->request()->getPathInfo())) { + $path_info = Idno::site()->request()->getPathInfo(); + } + + $discovered_handler = null; + $regex_matches = array(); + + if (isset($routes[$path_info])) { + $discovered_handler = $routes[$path_info]; + } elseif ($routes) { + $tokens = array( + ':string' => '([a-zA-Z]+)', + ':number' => '([0-9]+)', + ':alpha' => '([a-zA-Z0-9-_]+)' + ); + foreach ($routes as $pattern => $handler_name) { + $pattern = strtr($pattern, $tokens); + if (preg_match('#^/?' . $pattern . '/?$#', $path_info, $matches)) { + $discovered_handler = $handler_name; + $regex_matches = $matches; + break; + } + } + } + + $result = null; + $handler_instance = null; + + if ($discovered_handler) { + if (is_string($discovered_handler)) { + $handler_instance = new $discovered_handler(); + } elseif (is_callable($discovered_handler)) { + $handler_instance = $discovered_handler(); + } + } + + if ($handler_instance) { + unset($regex_matches[0]); + + + if (method_exists($handler_instance, $request_method)) { + Hook::fire('before_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); + $result = call_user_func_array(array($handler_instance, $request_method), $regex_matches); + Hook::fire('after_handler', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); + } else { + Hook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); + } + } else { + Hook::fire('404', compact('routes', 'discovered_handler', 'request_method', 'regex_matches')); + } + + Hook::fire('after_request', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); + + return Idno::site()->response(); + } + /** * Registers a page handler for a given pattern, using Toro * page handling syntax diff --git a/composer.json b/composer.json index 16efca8ef5..52faa9265d 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "tinymce/tinymce": "~6.2", "symfony/event-dispatcher": "~7.0", "symfony/console": "~6.1", - "symfony/http-foundation": "~7.0", + "symfony/http-foundation": "^7.1", "symfony/cache": "~7.0", "simplepie/simplepie": "1.8.0", "mapkyca/mrclay_autop_known": "^1.0", diff --git a/composer.lock b/composer.lock index e43f52677a..0ccee32b0d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4b11c5586f4bbaa12e5539653c5b99a5", + "content-hash": "065bdf72bb684997eb83e298a2759adb", "packages": [ { "name": "brick/math", @@ -3283,16 +3283,16 @@ }, { "name": "symfony/http-foundation", - "version": "v7.0.7", + "version": "v7.1.5", "source": { "type": "git", "url": "https://github.com/symfony/http-foundation.git", - "reference": "0194e064b8bdc29381462f790bab04e1cac8fdc8" + "reference": "e30ef73b1e44eea7eb37ba69600a354e553f694b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/http-foundation/zipball/0194e064b8bdc29381462f790bab04e1cac8fdc8", - "reference": "0194e064b8bdc29381462f790bab04e1cac8fdc8", + "url": "https://api.github.com/repos/symfony/http-foundation/zipball/e30ef73b1e44eea7eb37ba69600a354e553f694b", + "reference": "e30ef73b1e44eea7eb37ba69600a354e553f694b", "shasum": "" }, "require": { @@ -3340,7 +3340,7 @@ "description": "Defines an object-oriented layer for the HTTP specification", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/http-foundation/tree/v7.0.7" + "source": "https://github.com/symfony/http-foundation/tree/v7.1.5" }, "funding": [ { @@ -3356,7 +3356,7 @@ "type": "tidelift" } ], - "time": "2024-04-18T09:29:19+00:00" + "time": "2024-09-20T08:28:38+00:00" }, { "name": "symfony/polyfill-ctype", @@ -7026,5 +7026,5 @@ "ext-gettext": "*" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/index.php b/index.php index 52da9ef52f..f171cae5dd 100644 --- a/index.php +++ b/index.php @@ -50,13 +50,14 @@ \Idno\Core\PageHandler::hook( '404', function ($params = array()) { - http_response_code(404); $t = \Idno\Core\Idno::site()->template(); // Take over page detection \Idno\Core\Idno::site()->template()->autodetectTemplateType(); - (new \Idno\Pages\Homepage())->noContent(); + (new \Idno\Pages\Homepage())->noContent(); } ); -\Idno\Core\PageHandler::serve($routes); + +$response = \Idno\Core\PageHandler::serve($routes); +$response->send(); From 002ea35fb76c4dc5273ba0dfebbb99397e5d4997 Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 04:40:17 +0530 Subject: [PATCH 02/17] feat: integrate HttpFoundation with \Common\Page --- Idno/Common/Page.php | 150 +++++++++--------- Idno/Core/Http/RedirectResponse.php | 10 ++ Idno/Core/Http/Response.php | 7 + Idno/Core/Idno.php | 228 ++++++++++++++++------------ Idno/Core/PageHandler.php | 4 +- index.php | 3 +- 6 files changed, 227 insertions(+), 175 deletions(-) create mode 100644 Idno/Core/Http/RedirectResponse.php diff --git a/Idno/Common/Page.php b/Idno/Common/Page.php index 30fab515c7..7498da2438 100644 --- a/Idno/Common/Page.php +++ b/Idno/Common/Page.php @@ -62,9 +62,10 @@ abstract class Page extends \Idno\Common\Component function init() { if (!defined('KNOWN_UNIT_TEST')) { // Don't do header stuff in unit tests - header('X-Powered-By: https://withknown.com'); - header('X-Clacks-Overhead: GNU Terry Pratchett'); - header('X-Known-Build-Fingerprint: ' . \Idno\Core\TokenProvider::truncateToken(\Idno\Core\Version::fingerprint())); + \Idno\Core\Idno::site()->response()->headers->set('X-Powered-By', 'https://withknown.com'); + \Idno\Core\Idno::site()->response()->headers->set('X-Clacks-Overhead', 'GNU Terry Pratchett'); + \Idno\Core\Idno::site()->response()->headers->set('X-Known-Version', \Idno\Core\Version::version()); + } if ($template = $this->getInput('_t')) { if (\Idno\Core\Idno::site()->template()->templateTypeExists($template)) { @@ -74,8 +75,8 @@ function init() \Idno\Core\Idno::site()->setCurrentPage($this); // Set referrer, and ensure it's not blank - $this->referrer = $_SERVER['HTTP_REFERER']??''; - $_SERVER['HTTP_REFERER'] = $_SERVER['HTTP_REFERER']??''; // Ensure that the $_SERVER['HTTP_REFERER'] is never blank + $this->referrer =\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')??''; + \Idno\Core\Idno::site()->request()->server->set('HTTP_REFERER',$this->referrer); // Default exception handler set_exception_handler( @@ -137,7 +138,6 @@ function getInput($name, $default = null, callable $filter = null) function exception($e) { $this->setResponse(500); - http_response_code($this->response); \Idno\Core\Idno::site()->logging()->critical($e->getMessage() . " [".$e->getFile().":".$e->getLine()."]"); @@ -153,8 +153,8 @@ function exception($e) } $t = \Idno\Core\Idno::site()->template(); - $t->__(array('body' => $t->__(array('exception' => $e))->draw('pages/500'), 'title' => 'Exception'))->drawPage(); - exit; + $content = $t->__(array('body' => $t->__(array('exception' => $e))->draw('pages/500'), 'title' => 'Exception'))->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } /** @@ -214,7 +214,7 @@ function head() $this->getContent(); //if (http_response_code() != 200) - http_response_code($this->response); + $this->setResponse($this->response); } /** @@ -325,8 +325,8 @@ function get($params = array()) $this->getContent(); - if (http_response_code() != 200) { - http_response_code($this->response); + if (\Idno\Core\Idno::site()->response()->getStatusCode() != 200) { + \Idno\Core\Idno::site()->response()->setStatusCode($this->response); } } @@ -334,24 +334,24 @@ protected function debugLogToken() { $ts = ""; - if (empty($_REQUEST['__bTs'])) { + if (empty(\Idno\Core\Idno::site()->request()->request->get('__bTs'))) { \Idno\Core\Idno::site()->logging()->error("__bTs timestamp is missing"); } else { - $ts = $_REQUEST['__bTs']; + $ts = \Idno\Core\Idno::site()->request()->request->get('__bTs'); } $ta = ""; - if (empty($_REQUEST['__bTa'])) { + if (empty(\Idno\Core\Idno::site()->request()->request->get('__bTa'))) { \Idno\Core\Idno::site()->logging()->warning("__bTa action is missing"); } else { - $ta = $_REQUEST['__bTa']; + $ta = \Idno\Core\Idno::site()->request()->request->get('__bTa'); } $tk = ""; - if (empty($_REQUEST['__bTk'])) { + if (empty(\Idno\Core\Idno::site()->request()->request->get('__bTk'))) { \Idno\Core\Idno::site()->logging()->error("__bTk token is missing"); } else { - $tk = $_REQUEST['__bTk']; + $tk = \Idno\Core\Idno::site()->request()->request->get('__bTk'); } $debug = [ @@ -424,7 +424,7 @@ function post() // API currently doesn't explicitly handle this situation (bad), but we don't want to forward (worse). Some plugins will still // forward to / in some situations, these will need rewriting. if ($return === null) { - if (http_response_code() == 200) { + if (\Idno\Core\Idno::site()->response()->getStatusCode() == 200) { $this->setResponse(400); } @@ -435,23 +435,25 @@ function post() } $t = \Idno\Core\Idno::site()->template(); - echo $t->drawPage(); + $content = $t->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } else { // We have a return value, and response hasn't been explicitly set. Assume false is error, everything else is ok - if (($return === false) && (http_response_code() == 200)) { + if (($return === false) && (\Idno\Core\Idno::site()->response()->getStatusCode() == 200)) { $this->setResponse(400); } $t = \Idno\Core\Idno::site()->template(); - echo $t->__(['result' => $return])->drawPage(); + $content = $t->__(['result' => $return])->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } } else { $this->forward(); // If we haven't forwarded yet, do so (if we can) } //if (http_response_code() != 200) { - http_response_code($this->response); + \Idno\Core\Idno::site()->response()->setStatusCode($this->response); //} } @@ -469,8 +471,7 @@ function postContent() function options() { - header( - 'Access-Control-Allow-Methods: ' . implode( + \Idno\Core\Idno::site()->response()->headers->set('Access-Control-Allow-Methods', implode( ', ', [ 'GET', 'POST', @@ -479,12 +480,13 @@ function options() 'PUT', 'DELETE' ] - ) - ); + )); + + + \Idno\Core\Idno::site()->response()->headers->set('Access-Control-Max-Age', '86400'); - header('Access-Control-Max-Age: 86400'); + \Idno\Core\Idno::site()->response()->setStatusCode(204); - http_response_code(204); } /** @@ -564,11 +566,12 @@ function forward(string $location = '', bool $exit = true) if (!empty($call_trace)) { $location['trace'] = $call_trace; } - echo json_encode($location); + \Idno\Core\Idno::site()->response()->setJsonContent($location); } elseif (!\Idno\Core\Idno::site()->session()->isAPIRequest() || $this->response == 200) { if (!empty($call_trace)) { header('X-Known-Forward-Trace: ' . $call_trace); } - header('Location: ' . $location); + + \Idno\Core\Idno::site()->redirect($location); } if ($exit) { @@ -641,7 +644,7 @@ function put() // Ensure we always get a meaningful response from the api if ($return === null) { - if (http_response_code() == 200) { + if (\Idno\Core\Idno::site()->response()->getStatusCode() == 200) { $this->setResponse(400); } @@ -652,24 +655,26 @@ function put() } $t = \Idno\Core\Idno::site()->template(); - echo $t->drawPage(); + $content = $t->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } else { // We have a return value, and response hasn't been explicitly set. Assume false is error, everything else is ok - if (($return === false) && (http_response_code() == 200)) { + if (($return === false) && (\Idno\Core\Idno::site()->response()->getStatusCode() == 200)) { $this->setResponse(400); } $t = \Idno\Core\Idno::site()->template(); - echo $t->__(['result' => $return])->drawPage(); + $content = $t->__(['result' => $return])->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } } else { $this->forward(); // If we haven't forwarded yet, do so (if we can) } - if (http_response_code() != 200) { - http_response_code($this->response); + if (\Idno\Core\Idno::site()->response()->getStatusCode() != 200) { + \Idno\Core\Idno::site()->response()->setStatusCode($this->response); } } @@ -730,7 +735,7 @@ function delete() // Ensure we always get a meaningful response from the api if ($return === null) { - if (http_response_code() == 200) { + if (\Idno\Core\Idno::site()->response()->getStatusCode() == 200) { $this->setResponse(400); } @@ -741,24 +746,26 @@ function delete() } $t = \Idno\Core\Idno::site()->template(); - echo $t->drawPage(); + $content = $t->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } else { // We have a return value, and response hasn't been explicitly set. Assume false is error, everything else is ok - if (($return === false) && (http_response_code() == 200)) { + if (($return === false) && (\Idno\Core\Idno::site()->response()->getStatusCode() == 200)) { $this->setResponse(400); } $t = \Idno\Core\Idno::site()->template(); - echo $t->__(['result' => $return])->drawPage(); + $content = $t->__(['result' => $return])->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } } else { $this->forward(); // If we haven't forwarded yet, do so (if we can) } - if (http_response_code() != 200) { - http_response_code($this->response); + if (\Idno\Core\Idno::site()->response()->getStatusCode() != 200) { + \Idno\Core\Idno::site()->response()->setStatusCode($this->response); } } @@ -804,14 +811,16 @@ function webmentionContent($source, $target, $source_response, $source_mf2) function goneContent() { $this->setResponse(410); - http_response_code($this->response); + \Idno\Core\Idno::site()->response()->setStatusCode($this->response); + + \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Ts'); + \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Token'); - header_remove('X-Known-CSRF-Ts'); - header_remove('X-Known-CSRF-Token'); $t = \Idno\Core\Idno::site()->template(); - $t->__(array('body' => $t->draw('pages/410'), 'title' => \Idno\Core\Idno::site()->language()->_('This content isn\'t here.')))->drawPage(); - exit; + $content = $t->__(array('body' => $t->draw('pages/410'), 'title' => \Idno\Core\Idno::site()->language()->_('This content isn\'t here.')))->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); + \Idno\Core\Idno::site()->sendResponse(); } /** @@ -829,10 +838,12 @@ function noContent() $t = \Idno\Core\Idno::site()->template(); $content = $t->__(array('body' => $t->draw('pages/404'), 'title' => \Idno\Core\Idno::site()->language()->_('This page can\'t be found.')))->drawPage(false); \Idno\Core\Idno::site()->response()->setContent($content); + \Idno\Core\Idno::site()->sendResponse(); } /** * Flushes content to the browser and continues page working asynchronously. + * Depricate ? */ function flushBrowser() { @@ -863,14 +874,13 @@ function createGatekeeper() function deniedContent($title = '') { $this->setResponse(403); - http_response_code($this->response); - - header_remove('X-Known-CSRF-Ts'); - header_remove('X-Known-CSRF-Token'); + \Idno\Core\Idno::site()->response()->setStatusCode($this->response); + \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Ts'); + \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Token'); $t = \Idno\Core\Idno::site()->template(); - $t->__(array('body' => $t->draw('pages/403'), 'title' => $title))->drawPage(); - exit; + $content = $t->__(array('body' => $t->draw('pages/403'), 'title' => $title))->drawPage(); + \Idno\Core\Idno::site()->response()->setContent($content); } /** @@ -1002,10 +1012,8 @@ function sslGatekeeper() $url = str_replace('http://', 'https://', $this->currentUrl()); - header("HTTP/1.1 307 Temporary Redirect"); - header("Location: $url"); + \Idno\Core\Idno::site()->redirect($url); - exit; } } @@ -1016,18 +1024,13 @@ function sslGatekeeper() */ static function isSSL() { - if (isset($_SERVER['HTTPS'])) { - if ($_SERVER['HTTPS'] == '1') { + if (\Idno\Core\Idno::site()->request()->isSecure()) { return true; - } - if (strtolower($_SERVER['HTTPS']) == 'on') { - return true; - } - } else if (isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] == '443')) { + } else if (\Idno\Core\Idno::site()->request()->server->has('SERVER_PORT') && (\Idno\Core\Idno::site()->request()->server->get('SERVER_PORT') == '443')) { return true; } - if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') { + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_X_FORWARDED_PROTO') && strtolower(\Idno\Core\Idno::site()->request()->server->get('HTTP_X_FORWARDED_PROTO')) == 'https') { return true; } @@ -1043,7 +1046,7 @@ static function isSSL() public function currentUrl($tokenise = false) { $url = parse_url(\Idno\Core\Idno::site()->config()->url); - $url['path'] = $_SERVER['REQUEST_URI']; + $url['path'] = \Idno\Core\Idno::site()->request()->getPathInfo(); if ($tokenise) { return $url; @@ -1133,7 +1136,7 @@ function setInput($name, $value) function getReferrer() { - $referrer = $_SERVER['HTTP_REFERER']; + $referrer = \Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER'); if (empty($referrer)) { // TODO: Try other ways - e.g. for nginx @@ -1202,7 +1205,7 @@ function isAcceptedContentType($contentType, $ignore_priority = false) static function getallheaders() { $headers = array(); - foreach ($_SERVER as $name => $value) { + foreach (\Idno\Core\Idno::site()->request()->headers->all() as $name => $value) { if (substr($name, 0, 14) == 'REDIRECT_HTTP_') { $name = substr($name, 9); } @@ -1325,9 +1328,9 @@ function setOwner($user) */ public function setNoCache() { - header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); - header("Cache-Control: post-check=0, pre-check=0", false); - header("Pragma: no-cache"); + \Idno\Core\Idno::site()->response()->headers->set('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0'); + \Idno\Core\Idno::site()->response()->headers->set('Cache-Control', 'post-check=0, pre-check=0', false); + \Idno\Core\Idno::site()->response()->headers->set('Pragma', 'no-cache'); } /** @@ -1338,7 +1341,8 @@ public function setNoCache() */ public function setLastModifiedHeader(int $timestamp) { - header('Last-Modified: ' . \Idno\Core\Time::timestampToRFC2616($timestamp)); + \Idno\Core\Idno::site()->response()->headers->set('Last-Modified', \Idno\Core\Time::timestampToRFC2616($timestamp)); + \Idno\Core\Idno::site()->sendResponse(); } /** @@ -1352,8 +1356,8 @@ public function lastModifiedGatekeeper($timestamp) $headers = self::getallheaders(); if (isset($headers['If-Modified-Since'])) { if (strtotime($headers['If-Modified-Since']) <= $timestamp) { - header('HTTP/1.1 304 Not Modified'); - exit; + \Idno\Core\Idno::site()->response()->setStatusCode(304); + \Idno\Core\Idno::site()->sendResponse(); } } } diff --git a/Idno/Core/Http/RedirectResponse.php b/Idno/Core/Http/RedirectResponse.php new file mode 100644 index 0000000000..932018d567 --- /dev/null +++ b/Idno/Core/Http/RedirectResponse.php @@ -0,0 +1,10 @@ +headers->set('Content-Type', 'application/json'); + $this->setContent(json_encode($content)); + return $this; + } + } } \ No newline at end of file diff --git a/Idno/Core/Idno.php b/Idno/Core/Idno.php index bc70ccfc0f..32259ee889 100644 --- a/Idno/Core/Idno.php +++ b/Idno/Core/Idno.php @@ -1,15 +1,16 @@ request = Request::createFromGlobals(); - $this->response = new \Idno\Core\Http\Response(); - $this->routes = new PageHandler(); + self::$site = $this; + $this->request = Request::createFromGlobals(); + $this->response = new \Idno\Core\Http\Response(); + $this->routes = new PageHandler(); $this->dispatcher = new EventDispatcher(); - $this->config = new Config(); + $this->config = new Config(); if ($this->config->isDefaultConfig()) { header('Location: ./warmup/'); exit; // Load the installer @@ -119,17 +120,17 @@ function init() $this->logging->setLogLevel($this->config->loglevel); } - $this->session = new Session(); - $this->actions = new Actions(); + $this->session = new Session(); + $this->actions = new Actions(); $this->site_details = $this->site_details(); - $this->template = $this->componentFactory($this->config->template, Template::class, "Idno\\Core\\", HybridTwigTemplate::class); - $this->language = new Language(); - $this->language()->register(new GetTextTranslation()); // Register default gettext translations - $this->syndication = new Syndication(); - $this->reader = new Reader(); + $this->template = $this->componentFactory($this->config->template, Template::class, "Idno\\Core\\", HybridTwigTemplate::class); + $this->language = new Language(); + $this->language()->register(new GetTextTranslation()); // Register default gettext translations + $this->syndication = new Syndication(); + $this->reader = new Reader(); $this->helper_robot = new HelperRobot(); - $this->queue = $this->componentFactory($this->config->event_queue, "Idno\\Core\\EventQueue", "Idno\\Core\\", "Idno\\Core\\SynchronousQueue"); - $this->statistics = $this->componentFactory($this->config->statistics_collector, "Idno\\Stats\\StatisticsCollector", "Idno\\Stats\\", "Idno\\Stats\\DummyStatisticsCollector"); + $this->queue = $this->componentFactory($this->config->event_queue, "Idno\\Core\\EventQueue", "Idno\\Core\\", "Idno\\Core\\SynchronousQueue"); + $this->statistics = $this->componentFactory($this->config->statistics_collector, "Idno\\Stats\\StatisticsCollector", "Idno\\Stats\\", "Idno\\Stats\\DummyStatisticsCollector"); // Log some page statistics \Idno\Stats\Timer::start('script'); @@ -151,7 +152,8 @@ function () { // No URL is a critical error, default base fallback is now a warning (Refs #526) if (!defined('KNOWN_CONSOLE')) { - if (!$this->config->url) { throw new \Idno\Exceptions\ConfigurationException('Known was unable to work out your base URL! You might try setting url="http://yourdomain.com/" in your config.ini'); + if (!$this->config->url) { + throw new \Idno\Exceptions\ConfigurationException('Known was unable to work out your base URL! You might try setting url="http://yourdomain.com/" in your config.ini'); } if ($this->config->url == '/') { $this->logging->warning( @@ -167,11 +169,12 @@ function () { if (empty(site()->session()->hub_connect)) { site()->session()->hub_connect = 0; } - if (!empty($this->config->known_hub) + if ( + !empty($this->config->known_hub) && !substr_count($_SERVER['REQUEST_URI'], '.') && $this->config->known_hub != $this->config->url ) { - site()->session()->hub_connect = time(); + site()->session()->hub_connect = time(); \Idno\Core\Idno::site()->known_hub = new \Idno\Core\Hub($this->config->known_hub); \Idno\Core\Idno::site()->hub()->connect(); } @@ -188,7 +191,7 @@ function registerPages() /** * Homepage - */ + */ $this->routes()->addRoute('/?', '\Idno\Pages\Homepage'); $this->routes()->addRoute('/feed\.xml', '\Idno\Pages\Feed'); $this->routes()->addRoute('/feed/?', '\Idno\Pages\Feed'); @@ -197,7 +200,7 @@ function registerPages() /** * Individual entities / posting / deletion - */ + */ $this->routes()->addRoute('/view/:id/?', '\Idno\Pages\Entity\View'); $this->routes()->addRoute('/s/:id/?', '\Idno\Pages\Entity\Shortlink'); $this->routes()->addRoute($permalink_route . '/?', '\Idno\Pages\Entity\View'); @@ -209,32 +212,32 @@ function registerPages() /** * Annotations - */ + */ $this->routes()->addRoute('/view/:id/annotations/:id?', '\Idno\Pages\Annotation\View'); $this->routes()->addRoute($permalink_route . '/annotations/:id?', '\Idno\Pages\Annotation\View'); $this->routes()->addRoute($permalink_route . '/annotations/:id/delete/?', '\Idno\Pages\Annotation\Delete'); // Delete annotation - $this->routes()->addRoute($permalink_route .'/annotation/delete/?', '\Idno\Pages\Annotation\Delete'); // Delete annotation alternate + $this->routes()->addRoute($permalink_route . '/annotation/delete/?', '\Idno\Pages\Annotation\Delete'); // Delete annotation alternate $this->routes()->addRoute('/annotation/post/?', '\Idno\Pages\Annotation\Post'); /** * Bookmarklets and sharing - */ + */ $this->routes()->addRoute('/share/?', '\Idno\Pages\Entity\Share'); $this->routes()->addRoute('/bookmarklet\.js', '\Idno\Pages\Entity\Bookmarklet', true); /** * Mobile integrations - */ + */ $this->routes()->addRoute('/chrome/manifest\.json', '\Idno\Pages\Chrome\Manifest', true); /** * Service worker - */ + */ $this->routes()->addRoute('/service-worker(\.min)?\.js', '\Idno\Pages\Chrome\ServiceWorker', true); /** * Files - */ + */ $this->routes()->addRoute('/file/mint/?', \Idno\Pages\File\Mint::class); $this->routes()->addRoute('/file/upload/?', '\Idno\Pages\File\Upload', true); $this->routes()->addRoute('/file/picker/?', '\Idno\Pages\File\Picker', true); @@ -243,7 +246,7 @@ function registerPages() /** * Users - */ + */ $this->routes()->addRoute('/profile/([^\/]+)/?', '\Idno\Pages\User\View'); $this->routes()->addRoute('/profile/([^\/]+)/edit/?', '\Idno\Pages\User\Edit'); $this->routes()->addRoute('/profile/([^\/]+)/([A-Za-z\-\/]+)+', '\Idno\Pages\User\View'); @@ -251,7 +254,7 @@ function registerPages() /** * Search - */ + */ $this->routes()->addRoute('/search/?', '\Idno\Pages\Search\Forward'); $this->routes()->addRoute('/search/mentions\.json', '\Idno\Pages\Search\Mentions'); $this->routes()->addRoute('/tag/([^\s]+)\/?', '\Idno\Pages\Search\Tags'); @@ -259,17 +262,17 @@ function registerPages() /** * robots.txt - */ + */ $this->routes()->addRoute('/robots\.txt', '\Idno\Pages\Txt\Robots'); /** * Autosave / preview - */ + */ $this->routes()->addRoute('/autosave/?', '\Idno\Pages\Entity\Autosave'); /** * Installation / first use - */ + */ $this->routes()->addRoute('/begin/?', '\Idno\Pages\Onboarding\Begin', true); $this->routes()->addRoute('/begin/register/?', '\Idno\Pages\Onboarding\Register', true); $this->routes()->addRoute('/begin/profile/?', '\Idno\Pages\Onboarding\Profile'); @@ -279,7 +282,7 @@ function registerPages() /** * Add some services - */ + */ $this->routes()->addRoute('/service/db/optimise/?', '\Idno\Pages\Service\Db\Optimise'); $this->routes()->addRoute('/service/vendor/messages/?', '\Idno\Pages\Service\Vendor\Messages'); $this->routes()->addRoute('/service/security/csrftoken/?', '\Idno\Pages\Service\Security\CSRFToken'); @@ -291,7 +294,7 @@ function registerPages() // These must be loaded last $this->plugins = new Plugins(); - $this->themes = new Themes(); + $this->themes = new Themes(); } /** @@ -299,7 +302,7 @@ function registerPages() * * @return \Idno\Core\DataConcierge */ - function &db() : ?DataConcierge + function &db(): ?DataConcierge { return $this->db; } @@ -309,7 +312,7 @@ function &db() : ?DataConcierge * * @return \Idno\Core\EventDispatcher */ - function &events() : ?EventDispatcher + function &events(): ?EventDispatcher { return $this->dispatcher; } @@ -319,20 +322,41 @@ function &events() : ?EventDispatcher * * @return \Idno\Core\Http\Request */ - function &request() : ?Request + function &request(): ?Request { return $this->request; } /** - * Return the response object loaded as part of this site - * - * @return \Idno\Core\Http\Response - */ - function &response() : ?Response - { - return $this->response; - } + * Return the response object loaded as part of this site + * + * @return \Idno\Core\Http\Response + */ + function &response(): ?Response + { + return $this->response; + } + + /** + * Redirect to a new URL + * + * @param string $url + */ + function &redirect($url) + { + $this->response = new RedirectResponse($url); + \Idno\Core\Idno::site()->sendResponse(); + + } + + + /** + * Send the response + */ + function &sendResponse() + { + $this->response->send(); + } /** * Access to the EventQueue for dispatching events @@ -340,7 +364,7 @@ function &response() : ?Response * * @return \Idno\Core\EventQueue */ - function &queue() : ?EventQueue + function &queue(): ?EventQueue { return $this->queue; } @@ -350,7 +374,7 @@ function &queue() : ?EventQueue * * @return \Idno\Files\FileSystem */ - function &filesystem() : ? \Idno\Files\FileSystem + function &filesystem(): ?\Idno\Files\FileSystem { return $this->filesystem; } @@ -360,7 +384,7 @@ function &filesystem() : ? \Idno\Files\FileSystem * * @return \Idno\Core\Hub */ - function &hub() : ?Hub + function &hub(): ?Hub { return $this->known_hub; } @@ -370,7 +394,7 @@ function &hub() : ?Hub * * @return \Idno\Core\Logging */ - function &logging() : ?Logging + function &logging(): ?Logging { return $this->logging; } @@ -380,7 +404,7 @@ function &logging() : ?Logging * * @return \Idno\Caching\PersistentCache */ - function &cache() : ?\Idno\Caching\PersistentCache + function &cache(): ?\Idno\Caching\PersistentCache { return $this->cache; } @@ -390,7 +414,7 @@ function &cache() : ?\Idno\Caching\PersistentCache * * @return \Idno\Stats\StatisticsCollector */ - function &statistics() : ?\Idno\Stats\StatisticsCollector + function &statistics(): ?\Idno\Stats\StatisticsCollector { return $this->statistics; } @@ -400,7 +424,7 @@ function &statistics() : ?\Idno\Stats\StatisticsCollector * * @return \Idno\Core\PageHandler */ - function &routes() : ?PageHandler + function &routes(): ?PageHandler { return $this->routes; } @@ -427,7 +451,7 @@ function &config($setting = false) * * @return \Idno\Core\Syndication */ - function &syndication() : ?Syndication + function &syndication(): ?Syndication { return $this->syndication; } @@ -437,7 +461,7 @@ function &syndication() : ?Syndication * * @return \Idno\Core\Session */ - function &session() : ?Session + function &session(): ?Session { return $this->session; } @@ -447,7 +471,7 @@ function &session() : ?Session * * @return \Idno\Core\Plugins */ - function &plugins() : ?Plugins + function &plugins(): ?Plugins { return $this->plugins; } @@ -457,7 +481,7 @@ function &plugins() : ?Plugins * * @return \Idno\Core\Themes */ - function &themes() : ?Themes + function &themes(): ?Themes { return $this->themes; } @@ -468,7 +492,7 @@ function &themes() : ?Themes * @return \Idno\Core\Template */ - function &template() : ?Template + function &template(): ?Template { return $this->template; } @@ -478,7 +502,7 @@ function &template() : ?Template * * @return \Idno\Core\Language */ - function &language() : ?Language + function &language(): ?Language { if (empty($this->language)) { $this->language = new Language(); @@ -492,7 +516,7 @@ function &language() : ?Language * * @return \Idno\Core\Actions */ - function &actions() : ?Actions + function &actions(): ?Actions { return $this->actions; } @@ -502,7 +526,7 @@ function &actions() : ?Actions * * @return \Idno\Core\Reader */ - function &reader() : ?Reader + function &reader(): ?Reader { return $this->reader; } @@ -510,7 +534,7 @@ function &reader() : ?Reader /** * Return the site object for the current site, creating a new entry if one doesn't exist. */ - function &site_details() : ? Site + function &site_details(): ?Site { if (empty($this->site_details)) { @@ -518,7 +542,7 @@ function &site_details() : ? Site if (!empty($domain) && !empty($this->session())) { - $this->site_details = Site::getOne([ 'domain' => $domain ]); + $this->site_details = Site::getOne(['domain' => $domain]); if (empty($this->site_details)) { @@ -572,7 +596,8 @@ function currentPage() */ function canEdit($user_id = '') { - if (!\Idno\Core\Idno::site()->session()->isLoggedOn()) { return false; + if (!\Idno\Core\Idno::site()->session()->isLoggedOn()) { + return false; } if (empty($user_id)) { @@ -582,11 +607,13 @@ function canEdit($user_id = '') if ($user = \Idno\Entities\User::getByUUID($user_id)) { return \Idno\Core\Idno::site()->events()->triggerEvent( - 'canEdit/site', [ - 'object' => $this, - 'user_id' => $user_id, - 'user' => $user - ], (function () use ($user) { + 'canEdit/site', + [ + 'object' => $this, + 'user_id' => $user_id, + 'user' => $user + ], + (function () use ($user) { if ($user->isAdmin()) { return true; @@ -611,7 +638,8 @@ function canEdit($user_id = '') */ function canWrite($user_id = '') { - if (!\Idno\Core\Idno::site()->session()->isLoggedOn()) { return false; + if (!\Idno\Core\Idno::site()->session()->isLoggedOn()) { + return false; } if (empty($user_id)) { @@ -622,11 +650,13 @@ function canWrite($user_id = '') // Make site level canWrite extensible return \Idno\Core\Idno::site()->events()->triggerEvent( - 'canWrite/site', [ - 'object' => $this, - 'user_id' => $user_id, - 'user' => $user - ], (function () use ($user) { + 'canWrite/site', + [ + 'object' => $this, + 'user_id' => $user_id, + 'user' => $user + ], + (function () use ($user) { // Remote users can't ever create anything :( - for now if ($user instanceof \Idno\Entities\RemoteUser) { @@ -672,23 +702,23 @@ function canRead($user_id = '') * * @returns array An associative array of various icons => url */ - function getSiteIcons() : array + function getSiteIcons(): array { $icons = []; // Set our defaults (TODO: Set these cleaner, perhaps through the template system) $icons['defaults'] = [ - 'default' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k.png', - 'default_16' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_16.png', - 'default_32' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_32.png', - 'default_36' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_36.png', - 'default_48' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_48.png', - 'default_64' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_64.png', - 'default_96' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_96.png', + 'default' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k.png', + 'default_16' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_16.png', + 'default_32' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_32.png', + 'default_36' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_36.png', + 'default_48' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_48.png', + 'default_64' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_64.png', + 'default_96' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/logo_k_96.png', // Apple logos - 'default_57' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/apple-icon-57x57.png', - 'default_72' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/apple-icon-72x72.png', + 'default_57' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/apple-icon-57x57.png', + 'default_72' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/apple-icon-72x72.png', 'default_114' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/apple-icon-114x114.png', 'default_144' => \Idno\Core\Idno::site()->config()->getStaticURL() . 'gfx/logos/apple-icon-144x144.png', @@ -743,12 +773,14 @@ function upgrade() if ($last_update < $machine_version) { - if ($this->events()->triggerEvent( - 'upgrade', [ - 'last_update' => $last_update, - 'new_version' => $machine_version - ] - ) + if ( + $this->events()->triggerEvent( + 'upgrade', + [ + 'last_update' => $last_update, + 'new_version' => $machine_version + ] + ) ) { // Save updated @@ -777,7 +809,7 @@ function __sleep() * * @return \Idno\Core\Idno $site */ - static function &site() : ?Idno + static function &site(): ?Idno { return self::$site; } @@ -791,7 +823,7 @@ static function &site() : ?Idno * @param string $defaultClassNameBase If a full namespace is not provided in $configValue, use this value as base class namespace * @param string $defaultClass If class could not be constructed, return a new instance of this class name */ - public function componentFactory($className, $expectedBaseClass = "Idno\\Common\\Component" , $defaultClassNameBase = "Idno\\Core\\", $defaultClass = null) + public function componentFactory($className, $expectedBaseClass = "Idno\\Common\\Component", $defaultClassNameBase = "Idno\\Core\\", $defaultClass = null) { $component = null; @@ -830,7 +862,7 @@ public function componentFactory($className, $expectedBaseClass = "Idno\\Common\ // validate if (!is_subclass_of($component, $expectedBaseClass)) { - $component = null; + $component = null; } } } @@ -855,7 +887,7 @@ public function getVersion() * @deprecated Use \Idno\Core\Idno::site() * @return \Idno\Core\Idno $site */ - function &site() : Idno + function &site(): Idno { return \Idno\Core\Idno::site(); } diff --git a/Idno/Core/PageHandler.php b/Idno/Core/PageHandler.php index b6ce8e5faf..cb8ef99c6b 100644 --- a/Idno/Core/PageHandler.php +++ b/Idno/Core/PageHandler.php @@ -28,7 +28,7 @@ function routeTokens() ); } - public static function serve($routes): \Idno\Core\Http\Response + public static function serve($routes) { Hook::fire('before_request', compact('routes')); @@ -88,7 +88,7 @@ public static function serve($routes): \Idno\Core\Http\Response Hook::fire('after_request', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); - return Idno::site()->response(); + \Idno\Core\Idno::site()->sendResponse(); } /** diff --git a/index.php b/index.php index f171cae5dd..bf7a80480f 100644 --- a/index.php +++ b/index.php @@ -59,5 +59,4 @@ } ); -$response = \Idno\Core\PageHandler::serve($routes); -$response->send(); +\Idno\Core\PageHandler::serve($routes); From cf1fef86f1abf684f4fb047193f91a386eea22f0 Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 05:58:43 +0530 Subject: [PATCH 03/17] feat : replace echo with setResponse() on non template pages --- Idno/Common/Page.php | 4 ++-- Idno/Core/Bonita/Templates.php | 19 +++++++++++-------- Idno/Pages/Account/Export/RSS.php | 19 +++++++++++++------ Idno/Pages/Account/Notifications.php | 2 +- Idno/Pages/Account/Password.php | 3 ++- Idno/Pages/Account/Register.php | 5 +++-- Idno/Pages/Account/Settings/Tools.php | 5 +++-- Idno/Pages/Admin/Diagnostics.php | 7 ++++--- Idno/Pages/Admin/Export/RSS.php | 20 ++++++++++++++------ Idno/Pages/Admin/Logs.php | 5 +++-- Idno/Pages/Admin/Plugins.php | 16 ++++++++-------- Idno/Pages/Admin/Statistics.php | 5 +++-- Idno/Pages/Chrome/Manifest.php | 2 +- Idno/Pages/Chrome/ServiceWorker.php | 6 +++--- Idno/Pages/Entity/Bookmarklet.php | 2 +- Idno/Pages/Entity/Withdraw.php | 15 +++++++++------ Idno/Pages/File/Mint.php | 4 ++-- Idno/Pages/File/Picker.php | 5 +++-- Idno/Pages/File/Upload.php | 2 +- Idno/Pages/Hub/Register/Site.php | 4 ++-- Idno/Pages/Hub/Register/User.php | 4 ++-- Idno/Pages/Onboarding/Begin.php | 5 +++-- Idno/Pages/Onboarding/Connect.php | 3 ++- Idno/Pages/Onboarding/Profile.php | 3 ++- Idno/Pages/Pubsubhubbub/Callback.php | 4 ++-- Idno/Pages/Search/Mentions.php | 3 +-- Idno/Pages/Search/User.php | 3 ++- Idno/Pages/Service/Db/Optimise.php | 2 +- Idno/Pages/Service/Geo/Geocoder.php | 8 ++++---- Idno/Pages/Service/Security/CSRFToken.php | 5 ++--- Idno/Pages/Service/Vendor/Messages.php | 2 +- Idno/Pages/Service/Web/RemovePreview.php | 3 +-- Idno/Pages/Service/Web/UrlUnfurl.php | 11 +++++------ Idno/Pages/Txt/Robots.php | 2 +- Idno/Pages/Webfinger/View.php | 3 ++- Idno/Pages/Webmentions/Endpoint.php | 7 ++++--- 36 files changed, 124 insertions(+), 94 deletions(-) diff --git a/Idno/Common/Page.php b/Idno/Common/Page.php index 7498da2438..08a40f45b8 100644 --- a/Idno/Common/Page.php +++ b/Idno/Common/Page.php @@ -6,7 +6,7 @@ * Developers should extend the getContent, postContent and dataContent * methods as follows: * - * getContent: echoes HTML to the page + * getContent: set content for the page using \Idno\Core\Idno::site()->response()->setContent() * * postContent: handles content submitted to the page (assuming that form * elements were correctly signed) @@ -879,7 +879,7 @@ function deniedContent($title = '') \Idno\Core\Idno::site()->response()->headers->remove('X-Known-CSRF-Token'); $t = \Idno\Core\Idno::site()->template(); - $content = $t->__(array('body' => $t->draw('pages/403'), 'title' => $title))->drawPage(); + $content = $t->__(array('body' => $t->draw('pages/403'), 'title' => $title))->drawPage(false); \Idno\Core\Idno::site()->response()->setContent($content); } diff --git a/Idno/Core/Bonita/Templates.php b/Idno/Core/Bonita/Templates.php index b9a4acb7b8..188275b252 100644 --- a/Idno/Core/Bonita/Templates.php +++ b/Idno/Core/Bonita/Templates.php @@ -195,18 +195,21 @@ function process($content, $processor = 'text') function drawPage($echo = true, $shell = 'shell') { if ($echo) { + // For Backward Compatibility reasons + \Idno\Core\Idno::site()->response()->setContent($this->draw($shell)); + \Idno\Core\Idno::site()->sendResponse(); - $content = $this->draw($shell); - header('Content-Length: ' . strlen($content)); + // $content = $this->draw($shell); + // header('Content-Length: ' . strlen($content)); - // Break long output to avoid an Apache performance bug - $split_output = str_split($content, 1024); + // // Break long output to avoid an Apache performance bug + // $split_output = str_split($content, 1024); - foreach ($split_output as $chunk) { - echo $chunk; - } + // foreach ($split_output as $chunk) { + // echo $chunk; + // } - exit; + // exit; } else { return $this->draw($shell); } diff --git a/Idno/Pages/Account/Export/RSS.php b/Idno/Pages/Account/Export/RSS.php index 597a094a44..31111bbd0a 100644 --- a/Idno/Pages/Account/Export/RSS.php +++ b/Idno/Pages/Account/Export/RSS.php @@ -5,6 +5,8 @@ use Idno\Common\Page; use Idno\Core\Idno; use Idno\Core\Migration; + use Symfony\Component\HttpFoundation\HeaderUtils; + class RSS extends Page { @@ -16,8 +18,13 @@ function postContent() set_time_limit(0); - header('Content-type: text/rss'); - header('Content-disposition: attachment; filename=user_export.rss'); + \Idno\Core\Idno::site()->response()->headers->set('Content-type', 'text/rss'); + $disposition = HeaderUtils::makeDisposition( + HeaderUtils::DISPOSITION_ATTACHMENT, + 'user_export.rss' + ); + + \Idno\Core\Idno::site()->response()->headers->set('Content-Disposition', $disposition); $hide_private = true; if ($private = $this->getInput('allposts')) { @@ -28,13 +35,13 @@ function postContent() $stats = fstat($f); - header('Content-Length: ' . $stats['size']); - + \Idno\Core\Idno::site()->response()->headers->set('Content-Length:', $stats['size']); + $file = ''; while ($content = fgets($f)) { - echo $content; + $file.=$content; } - fclose($f); + \Idno\Core\Idno::site()->response()->setContent($file); } exit; diff --git a/Idno/Pages/Account/Notifications.php b/Idno/Pages/Account/Notifications.php index c1214a77ee..588610c88e 100644 --- a/Idno/Pages/Account/Notifications.php +++ b/Idno/Pages/Account/Notifications.php @@ -51,7 +51,7 @@ function getContent($params = array()) $notif->save(); } - echo $page; + \Idno\Core\Idno::site()->response()->setContent($page); } } diff --git a/Idno/Pages/Account/Password.php b/Idno/Pages/Account/Password.php index 9f9c404927..6da4dd5b37 100644 --- a/Idno/Pages/Account/Password.php +++ b/Idno/Pages/Account/Password.php @@ -29,7 +29,8 @@ function getContent() $t->title = \Idno\Core\Idno::site()->language()->_('Recover password'); } - echo $t->draw('shell'); + $content = $t->draw('shell'); + \Idno\Core\Idno::site()->response()->setContent($content); } diff --git a/Idno/Pages/Account/Register.php b/Idno/Pages/Account/Register.php index c317f3f54c..0ec766b85b 100644 --- a/Idno/Pages/Account/Register.php +++ b/Idno/Pages/Account/Register.php @@ -37,7 +37,7 @@ function getContent() if (empty($onboarding)) { $t->body = $t->__(array('email' => $email, 'code' => $code))->draw('account/register'); $t->title = \Idno\Core\Idno::site()->language()->_('Create a new account'); - echo $t->draw('shell'); + $content = $t->draw('shell'); } else { $t->body = $t->__( array( @@ -47,8 +47,9 @@ function getContent() 'messages' => \Idno\Core\Idno::site()->session()->getAndFlushMessages()) )->draw('onboarding/register'); $t->title = \Idno\Core\Idno::site()->language()->_('Create a new account'); - echo $t->draw('shell/simple'); + $content = $t->draw('shell/simple'); } + \Idno\Core\Idno::site()->response()->setContent($content); } function postContent() diff --git a/Idno/Pages/Account/Settings/Tools.php b/Idno/Pages/Account/Settings/Tools.php index 399013e256..182ac34cfd 100644 --- a/Idno/Pages/Account/Settings/Tools.php +++ b/Idno/Pages/Account/Settings/Tools.php @@ -19,12 +19,13 @@ function getContent() if ($this->xhr) { \Idno\Core\Actions::validateToken('/account/settings/tools/'); $user = \Idno\Core\Idno::site()->session()->currentUser(); - echo json_encode($user->getAPIkey()); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($user->getAPIkey())); } else { $t = \Idno\Core\Idno::site()->template(); $t->body = $t->draw('account/settings/tools'); $t->title = \Idno\Core\Idno::site()->language()->_('Tools and Apps'); - $t->drawPage(); + $content = $t->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } } diff --git a/Idno/Pages/Admin/Diagnostics.php b/Idno/Pages/Admin/Diagnostics.php index ef9ce53c1e..be677a9d0a 100644 --- a/Idno/Pages/Admin/Diagnostics.php +++ b/Idno/Pages/Admin/Diagnostics.php @@ -49,13 +49,14 @@ function getContent() // Hook so other plugins and subsystems can add their own data to the report. $report = \Idno\Core\Idno::site()->events()->triggerEvent('diagnostics/report', [], $report); - echo $report; - exit; + \Idno\Core\Idno::site()->response()->setContent($report); + \Idno\Core\Idno::site()->sendResponse(); } else { $t = \Idno\Core\Idno::site()->template(); $t->body = $t->__(['basics' => $basics])->draw('admin/diagnostics'); $t->title = \Idno\Core\Idno::site()->language()->_('Diagnostics'); - $t->drawPage(); + $content = $t->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } } diff --git a/Idno/Pages/Admin/Export/RSS.php b/Idno/Pages/Admin/Export/RSS.php index ad2d141d5a..3c3f1ec1f2 100644 --- a/Idno/Pages/Admin/Export/RSS.php +++ b/Idno/Pages/Admin/Export/RSS.php @@ -4,6 +4,8 @@ use Idno\Common\Page; use Idno\Core\Migration; + use Symfony\Component\HttpFoundation\HeaderUtils; + class RSS extends Page { @@ -15,8 +17,13 @@ function postContent() set_time_limit(0); - header('Content-type: text/rss'); - header('Content-disposition: attachment; filename=export.rss'); + \Idno\Core\site()->response()->headers->set('Content-type', 'text/rss'); + $disposition = HeaderUtils::makeDisposition( + HeaderUtils::DISPOSITION_ATTACHMENT, + 'export.rss' + ); + + \Idno\Core\site()->response()->headers->set('Content-Disposition', $disposition); $hide_private = true; if ($private = $this->getInput('allposts')) { @@ -27,15 +34,16 @@ function postContent() $stats = fstat($f); - header('Content-Length: ' . $stats['size']); - + \Idno\Core\site()->response()->headers->set('Content-Length:', $stats['size']); + $file = ''; while ($content = fgets($f)) { - echo $content; + $file .= $content; } fclose($f); + \Idno\Core\site()->response()->setContent($file); } - exit; + \Idno\Core\site()->sendResponse(); } diff --git a/Idno/Pages/Admin/Logs.php b/Idno/Pages/Admin/Logs.php index 786207238d..843cb91d69 100644 --- a/Idno/Pages/Admin/Logs.php +++ b/Idno/Pages/Admin/Logs.php @@ -11,13 +11,14 @@ function getContent() // Retrieved via API, so just dump logs if ($this->xhr || \Idno\Core\Idno::site()->session()->isAPIRequest()) { - echo file_get_contents(\Idno\Core\Idno::site()->config()->getTempDir() . \Idno\Core\Idno::site()->config()->host . '.log'); + \Idno\Core\Idno::site()->response()->setJsonContent(file_get_contents(\Idno\Core\Idno::site()->config()->getTempDir() . \Idno\Core\Idno::site()->config()->host . '.log')); } else { $t = \Idno\Core\Idno::site()->template(); $t->body = $t->__([])->draw('admin/logs'); $t->title = \Idno\Core\Idno::site()->language()->_('Log capture'); - $t->drawPage(); + $content = $t->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } } diff --git a/Idno/Pages/Admin/Plugins.php b/Idno/Pages/Admin/Plugins.php index b9044f989e..f1997dcc12 100644 --- a/Idno/Pages/Admin/Plugins.php +++ b/Idno/Pages/Admin/Plugins.php @@ -42,14 +42,14 @@ function postContent() if (\Idno\Core\Idno::site()->plugins()->enable($plugin)) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->_('The plugin was enabled.')); - echo json_encode( + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode( [ 'action' => $action, 'status' => true, 'message' => \Idno\Core\Idno::site()->language()->_('The plugin was enabled.') ] - ); - exit; + )); + \Idno\Core\Idno::site()->sendResponse(); } break; @@ -58,14 +58,14 @@ function postContent() if (\Idno\Core\Idno::site()->plugins()->disable($plugin)) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->_('The plugin was disabled.')); - echo json_encode( + \Idno\Core\Idno::site()->response()->setJsonContent( json_encode( [ 'action' => $action, 'status' => true, 'message' => \Idno\Core\Idno::site()->language()->_('The plugin was disabled.') ] - ); - exit; + )); + \Idno\Core\Idno::site()->sendResponse(); } break; @@ -73,12 +73,12 @@ function postContent() } - echo json_encode( + \Idno\Core\Idno::site()->response()->setJsonContent( json_encode( [ 'action' => $action, 'status' => false, ] - ); + )); //$this->forward(\Idno\Core\Idno::site()->config()->getDisplayURL() . 'admin/plugins/'); } diff --git a/Idno/Pages/Admin/Statistics.php b/Idno/Pages/Admin/Statistics.php index 0eb1a46e38..0ef974ef40 100644 --- a/Idno/Pages/Admin/Statistics.php +++ b/Idno/Pages/Admin/Statistics.php @@ -20,13 +20,14 @@ function getContent() header('Content-type: application/json'); - echo json_encode($stats); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($stats)); } else { $t = \Idno\Core\Idno::site()->template(); $t->body = $t->__(['statistics' => $stats, 'tab' => $this->getInput('tab', $report)])->draw('admin/statistics'); $t->title = \Idno\Core\Idno::site()->language()->_('Statistics'); - $t->drawPage(); + $content = $t->drawPage(); + \Idno\Core\Idno::site()->response()->setContent($content); } } diff --git a/Idno/Pages/Chrome/Manifest.php b/Idno/Pages/Chrome/Manifest.php index 9cbae8e724..e2b53b9972 100644 --- a/Idno/Pages/Chrome/Manifest.php +++ b/Idno/Pages/Chrome/Manifest.php @@ -10,7 +10,7 @@ class Manifest extends Page function getContent() { - echo \Idno\Core\Idno::site()->template()->draw('chrome/manifest'); + \Idno\Core\Idno::site()->response()->setContent(\Idno\Core\Idno::site()->template()->draw('chrome/manifest')); } diff --git a/Idno/Pages/Chrome/ServiceWorker.php b/Idno/Pages/Chrome/ServiceWorker.php index d83957bbd7..b31d2fdbfc 100644 --- a/Idno/Pages/Chrome/ServiceWorker.php +++ b/Idno/Pages/Chrome/ServiceWorker.php @@ -17,11 +17,11 @@ class ServiceWorker extends Page function getContent() { - header('Content-Type: application/javascript'); + \Idno\Core\Idno::site()->response()->headers->set('Content-Type', 'application/javascript'); if (!empty($this->arguments[0])) { - echo file_get_contents(\Idno\Core\Idno::site()->config()->path . '/js/service-worker.min.js'); + \Idno\Core\Idno::site()->response()->setContent(file_get_contents(\Idno\Core\Idno::site()->config()->path . '/js/service-worker.min.js')); } else { - echo file_get_contents(\Idno\Core\Idno::site()->config()->path . '/js/service-worker.js'); + \Idno\Core\Idno::site()->response()->setContent(file_get_contents(\Idno\Core\Idno::site()->config()->path . '/js/service-worker.js')); } } diff --git a/Idno/Pages/Entity/Bookmarklet.php b/Idno/Pages/Entity/Bookmarklet.php index 20d7707e56..39b90b9e73 100644 --- a/Idno/Pages/Entity/Bookmarklet.php +++ b/Idno/Pages/Entity/Bookmarklet.php @@ -16,7 +16,7 @@ function getContent() { $t = \Idno\Core\Idno::site()->template(); - echo $t->draw('entity/bookmarklet'); + Idno\Core\Idno::site()->response->setContent(($t->draw('entity/bookmarklet'))); } diff --git a/Idno/Pages/Entity/Withdraw.php b/Idno/Pages/Entity/Withdraw.php index bfb05f7080..168cca9c67 100644 --- a/Idno/Pages/Entity/Withdraw.php +++ b/Idno/Pages/Entity/Withdraw.php @@ -19,8 +19,9 @@ function getContent() } if (empty($object)) { $this->setResponse(404); - echo \Idno\Core\Idno::site()->template()->__(array('body' => \Idno\Core\Idno::site()->template()->draw('404'), 'title' => 'Not found'))->drawPage(); - exit; + $content = \Idno\Core\Idno::site()->template()->__(array('body' => \Idno\Core\Idno::site()->template()->draw('404'), 'title' => 'Not found'))->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); + \Idno\Core\Idno::site()->sendResponse(); } } @@ -33,14 +34,16 @@ function postContent() } if (empty($object)) { $this->setResponse(404); - echo \Idno\Core\Idno::site()->template()->__(array('body' => \Idno\Core\Idno::site()->template()->draw('404'), 'title' => 'Not found'))->drawPage(); - exit; + $content = \Idno\Core\Idno::site()->template()->__(array('body' => \Idno\Core\Idno::site()->template()->draw('404'), 'title' => 'Not found'))->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); + \Idno\Core\Idno::site()->sendResponse(); } if (!$object->canEdit()) { $this->setResponse(403); - echo \Idno\Core\Idno::site()->template()->__(array('body' => \Idno\Core\Idno::site()->template()->draw('403'), 'title' => 'Permission denied'))->drawPage(); - exit; + $content = \Idno\Core\Idno::site()->template()->__(array('body' => \Idno\Core\Idno::site()->template()->draw('403'), 'title' => 'Permission denied'))->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); + \Idno\Core\Idno::site()->sendResponse(); } $object->unsyndicate(); diff --git a/Idno/Pages/File/Mint.php b/Idno/Pages/File/Mint.php index fcc10d5d3e..58364e44cf 100644 --- a/Idno/Pages/File/Mint.php +++ b/Idno/Pages/File/Mint.php @@ -53,10 +53,10 @@ public function postContent() // Mint an ID // Lets tell people about where to stick the data - echo json_encode( + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode( [ 'uploadUrl' => $upload_url ] - ); + )); } } diff --git a/Idno/Pages/File/Picker.php b/Idno/Pages/File/Picker.php index 5f4165be77..4e6d4b5a34 100644 --- a/Idno/Pages/File/Picker.php +++ b/Idno/Pages/File/Picker.php @@ -50,8 +50,9 @@ function post() if ($return) { $t = \Idno\Core\Idno::site()->template(); $t->file = $file; - echo $t->draw('file/picker/donejs'); - exit; + $content = $t->draw('file/picker/donejs'); + \Idno\Core\Idno::site()->response()->setContent($content); + \Idno\Core\Idno::site()->sendResponse(); } } else { Idno::site()->session()->addErrorMessage(\Idno\Core\Idno::site()->language()->_("You can only upload images.")); diff --git a/Idno/Pages/File/Upload.php b/Idno/Pages/File/Upload.php index 0808256292..77da0d7dd0 100644 --- a/Idno/Pages/File/Upload.php +++ b/Idno/Pages/File/Upload.php @@ -22,7 +22,7 @@ function post() exit; } if ($file = \Idno\Entities\File::createFromFile($_FILES['file']['tmp_name'], $_FILES['file']['name'], $_FILES['file']['type'], true, true)) { - echo json_encode(\Idno\Core\Idno::site()->config()->url . 'file/' . $file->file['_id']); + \Idno\Core\Idno::site()->response()->setJsonContent( json_encode(\Idno\Core\Idno::site()->config()->url . 'file/' . $file->file['_id'])); } } } diff --git a/Idno/Pages/Hub/Register/Site.php b/Idno/Pages/Hub/Register/Site.php index 7528efc02b..83b8c95501 100644 --- a/Idno/Pages/Hub/Register/Site.php +++ b/Idno/Pages/Hub/Register/Site.php @@ -38,8 +38,8 @@ function post() } - echo json_encode($result); - exit; + \Idno\Core\Idno::site()->response()->setJsonContent( json_encode($result)); + \Idno\Core\Idno::site()->sendResponse(); } diff --git a/Idno/Pages/Hub/Register/User.php b/Idno/Pages/Hub/Register/User.php index 4b45a625fa..8363b34f38 100644 --- a/Idno/Pages/Hub/Register/User.php +++ b/Idno/Pages/Hub/Register/User.php @@ -49,8 +49,8 @@ function post() $result = array('status' => 'fail', 'message' => 'Signature does not match: ' . $signature . ', ' . $hmac); } - echo json_encode($result); - exit; + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($result)); + \Idno\Core\Idno::site()->sendResponse(); } diff --git a/Idno/Pages/Onboarding/Begin.php b/Idno/Pages/Onboarding/Begin.php index c1dfaaf396..b6edea1424 100644 --- a/Idno/Pages/Onboarding/Begin.php +++ b/Idno/Pages/Onboarding/Begin.php @@ -18,13 +18,14 @@ function getContent() } $t = \Idno\Core\Idno::site()->template(); - echo $t->__( + $content = $t->__( array( 'body' => $t->draw('onboarding/begin'), 'title' => \Idno\Core\Idno::site()->language()->_('Welcome to Known'), 'messages' => \Idno\Core\Idno::site()->session()->getAndFlushMessages() ) - )->draw('shell/simple'); + )->draw('shell/simple') + \Idno\Core\Idno::site()->response()->setContent($content); } diff --git a/Idno/Pages/Onboarding/Connect.php b/Idno/Pages/Onboarding/Connect.php index 4f5e649d3a..7ccf05c24a 100644 --- a/Idno/Pages/Onboarding/Connect.php +++ b/Idno/Pages/Onboarding/Connect.php @@ -20,7 +20,7 @@ function getContent() $_SESSION['onboarding_passthrough'] = true; $t = \Idno\Core\Idno::site()->template(); - echo $t->__( + $content = $t->__( array( 'title' => \Idno\Core\Idno::site()->language()->_("Connect some networks"), @@ -29,6 +29,7 @@ function getContent() ) )->draw('shell/simple'); + \Idno\Core\Idno::site()->response()->setContent($content); //} else { // $this->forward(\Idno\Core\Idno::site()->config()->getURL() . 'begin/publish'); //} diff --git a/Idno/Pages/Onboarding/Profile.php b/Idno/Pages/Onboarding/Profile.php index 50521bf74c..cc502fd339 100644 --- a/Idno/Pages/Onboarding/Profile.php +++ b/Idno/Pages/Onboarding/Profile.php @@ -17,7 +17,7 @@ function getContent() $user = \Idno\Core\Idno::site()->session()->currentUser(); $t = \Idno\Core\Idno::site()->template(); - echo $t->__( + $content = $t->__( array( 'title' => \Idno\Core\Idno::site()->language()->_("Create your profile"), @@ -26,6 +26,7 @@ function getContent() ) )->draw('shell/simple'); + \Idno\Core\Idno::site()->response()->setContent($content); } diff --git a/Idno/Pages/Pubsubhubbub/Callback.php b/Idno/Pages/Pubsubhubbub/Callback.php index 1673ecf612..6a8191151a 100644 --- a/Idno/Pages/Pubsubhubbub/Callback.php +++ b/Idno/Pages/Pubsubhubbub/Callback.php @@ -50,8 +50,8 @@ function getContent() $subscriber->save(); \Idno\Core\Idno::site()->logging()->debug("Pubsub: $hub_challenge"); - echo $hub_challenge; - exit; + \Idno\Core\Idno::site()->response()->setContent($hub_challenge); + \Idno\Core\Idno::site()->sendResponse(); } break; } diff --git a/Idno/Pages/Search/Mentions.php b/Idno/Pages/Search/Mentions.php index a3209a1136..c35f0ffdd0 100644 --- a/Idno/Pages/Search/Mentions.php +++ b/Idno/Pages/Search/Mentions.php @@ -34,8 +34,7 @@ function getContent() } } - header('Content-type: text/json'); - echo json_encode($results); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($results)); } diff --git a/Idno/Pages/Search/User.php b/Idno/Pages/Search/User.php index af72d72cc8..f7320ba2d5 100644 --- a/Idno/Pages/Search/User.php +++ b/Idno/Pages/Search/User.php @@ -59,7 +59,8 @@ public function getContent() $results['rendered'] .= $template_postponed_link_actions; } - echo json_encode($results); exit; + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($results)); + \Idno\Core\Idno::site()->sendResponse(); } } diff --git a/Idno/Pages/Service/Db/Optimise.php b/Idno/Pages/Service/Db/Optimise.php index b25bc39243..6635982a7d 100644 --- a/Idno/Pages/Service/Db/Optimise.php +++ b/Idno/Pages/Service/Db/Optimise.php @@ -16,7 +16,7 @@ function getContent() \Idno\Core\Idno::site()->config()->dboptimized = $time; \Idno\Core\Idno::site()->config()->save(); - echo json_encode('optimised'); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode('optimised')); } } diff --git a/Idno/Pages/Service/Geo/Geocoder.php b/Idno/Pages/Service/Geo/Geocoder.php index b032e5c342..053e0f6bd5 100644 --- a/Idno/Pages/Service/Geo/Geocoder.php +++ b/Idno/Pages/Service/Geo/Geocoder.php @@ -22,14 +22,14 @@ function getContent() $lat = $this->getInput('lat'); $long = $this->getInput('long'); if (!empty($lat) && (!empty($long))) { - echo json_encode($geocoder->queryLatLong($lat, $long), JSON_PRETTY_PRINT); - exit; + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($geocoder->queryLatLong($lat, $long), JSON_PRETTY_PRINT)); + \Idno\Core\Idno::site()->sendResponse(); } $address = $this->getInput('address'); if (!empty($address)) { - echo json_encode($geocoder->queryAddress($address), JSON_PRETTY_PRINT); - exit; + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($geocoder->queryAddress($address), JSON_PRETTY_PRINT)); + \Idno\Core\Idno::site()->sendResponse(); } } diff --git a/Idno/Pages/Service/Security/CSRFToken.php b/Idno/Pages/Service/Security/CSRFToken.php index 5b1313ceab..7413ad3512 100644 --- a/Idno/Pages/Service/Security/CSRFToken.php +++ b/Idno/Pages/Service/Security/CSRFToken.php @@ -22,13 +22,12 @@ function getContent() $time = time(); $token = \Idno\Core\Bonita\Forms::token($action, $time); - header('Content-type: application/json'); - echo json_encode( + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode( [ 'time' => $time, 'token' => $token ] - ); + )); } } diff --git a/Idno/Pages/Service/Vendor/Messages.php b/Idno/Pages/Service/Vendor/Messages.php index 9f3e059eeb..c1cb394666 100644 --- a/Idno/Pages/Service/Vendor/Messages.php +++ b/Idno/Pages/Service/Vendor/Messages.php @@ -11,7 +11,7 @@ function getContent() $this->setNoCache(); if ($messages = \Idno\Core\Vendor::getMessages()) { - echo json_encode($messages); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($messages)); } } diff --git a/Idno/Pages/Service/Web/RemovePreview.php b/Idno/Pages/Service/Web/RemovePreview.php index 65bea718d2..6a4b84c349 100644 --- a/Idno/Pages/Service/Web/RemovePreview.php +++ b/Idno/Pages/Service/Web/RemovePreview.php @@ -11,7 +11,6 @@ function postContent() $this->gatekeeper(); \Idno\Core\Idno::site()->template()->setTemplateType('json'); - header('Content-type: application/json'); if (!empty($this->arguments[0])) { $object = \Idno\Common\Entity::getByID($this->arguments[0]); @@ -29,7 +28,7 @@ function postContent() $object->hide_preview = true; - echo json_encode($object->save()); + \Idno\Core\Idno::site()->response()->setContent(json_encode($object->save())); } diff --git a/Idno/Pages/Service/Web/UrlUnfurl.php b/Idno/Pages/Service/Web/UrlUnfurl.php index 0c04239943..cc5ecdb92a 100644 --- a/Idno/Pages/Service/Web/UrlUnfurl.php +++ b/Idno/Pages/Service/Web/UrlUnfurl.php @@ -9,7 +9,6 @@ function deleteContent() { \Idno\Core\Idno::site()->template()->setTemplateType('json'); - header('Content-type: application/json'); $this->xhrGatekeeper(); $this->tokenGatekeeper(); @@ -22,12 +21,12 @@ function deleteContent() // Try and get UnfurledURL entity if ($object = \Idno\Entities\UnfurledUrl::getBySourceURL($url)) { - echo json_encode( + \Idno\Core\Idno::site()->response()->setJsonContent( json_encode( [ 'url' => $url, 'status' => $object->delete() ] - ); + )); } else { $this->noContent(); } @@ -59,9 +58,9 @@ function getContent() $unfurled['id'] = $object->getID(); $unfurled['rendered'] = $template->__(['object' => $object])->draw('entity/UnfurledUrl'); - echo json_encode($unfurled, JSON_PRETTY_PRINT); + \Idno\Core\Idno::site()->response()->setJsonContent( json_encode($unfurled, JSON_PRETTY_PRINT)); - exit; + \Idno\Core\Idno::site()->sendResponse(); } if (empty($object)) { @@ -83,7 +82,7 @@ function getContent() $unfurled['id'] = $object->getID(); $unfurled['rendered'] = $template->__(['object' => $object])->draw('entity/UnfurledUrl'); - echo json_encode($unfurled, JSON_PRETTY_PRINT); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode($unfurled, JSON_PRETTY_PRINT)); } } diff --git a/Idno/Pages/Txt/Robots.php b/Idno/Pages/Txt/Robots.php index 18a8bb4edf..9d4a5d1048 100644 --- a/Idno/Pages/Txt/Robots.php +++ b/Idno/Pages/Txt/Robots.php @@ -15,7 +15,7 @@ class Robots extends \Idno\Common\Page function getContent() { $t = \Idno\Core\Idno::site()->template(); - echo $t->draw('txt/robots'); + \Idno\Core\Idno::site()->response()->setContent($t->draw('txt/robots')); } } diff --git a/Idno/Pages/Webfinger/View.php b/Idno/Pages/Webfinger/View.php index c10a60e1df..ea82d57bf5 100644 --- a/Idno/Pages/Webfinger/View.php +++ b/Idno/Pages/Webfinger/View.php @@ -30,7 +30,7 @@ function getContent() } $t = \Idno\Core\Idno::site()->template(); $t->setTemplateType('json'); - echo $t->__( + $content = $t->__( array( 'properties' => [ 'http://webfinger.example/ns/name' => $user->getName(), @@ -39,6 +39,7 @@ function getContent() 'links' => $links ) )->draw('shell'); + \Idno\Core\Idno::site()->response()->setContent($content); } function postContent() diff --git a/Idno/Pages/Webmentions/Endpoint.php b/Idno/Pages/Webmentions/Endpoint.php index 2b9da3942f..7003f4f9d2 100644 --- a/Idno/Pages/Webmentions/Endpoint.php +++ b/Idno/Pages/Webmentions/Endpoint.php @@ -15,12 +15,13 @@ class Endpoint extends \Idno\Common\Page function getContent() { $t = \Idno\Core\Idno::site()->template(); - $t->__( + $content = $t->__( [ 'title' => \Idno\Core\Idno::site()->language()->_('Webmention endpoint'), 'body' => $t->draw('pages/webmention') ] - )->drawPage(); + )->drawPage(false); + \Idno\Core\Idno::site()->response()->setContent($content); } function post() @@ -103,7 +104,7 @@ function post() $error = 'unknown_error'; $error_text = 'Not all the required webmention variables were set.'; } - echo json_encode(array('error' => $error, 'error_text' => $error_text)); + \Idno\Core\Idno::site()->response()->setJsonContent(json_encode(array('error' => $error, 'error_text' => $error_text))); } } From c3211c15e32f7a536062bb896e6079607b19c70e Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 06:03:36 +0530 Subject: [PATCH 04/17] feat: Dispatch event to modify response before sending --- Idno/Core/Idno.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Idno/Core/Idno.php b/Idno/Core/Idno.php index 32259ee889..cd82a4f120 100644 --- a/Idno/Core/Idno.php +++ b/Idno/Core/Idno.php @@ -355,7 +355,8 @@ function &redirect($url) */ function &sendResponse() { - $this->response->send(); + $response = \Idno\Core\Idno::site()->events()->triggerEvent('response/before', ['response' => $this->response()],$this->response()); + $response->send(); } /** From 5bbf215f5a6c7a71a81b5913036233f469f26c30 Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 06:57:54 +0530 Subject: [PATCH 05/17] feat: symfony and PSR7 conversion support --- Idno/Core/Http/Request.php | 24 +++++++++++ Idno/Core/Http/Response.php | 18 +++++++- Idno/Core/Idno.php | 21 ++++++++- composer.json | 3 +- composer.lock | 85 ++++++++++++++++++++++++++++++++++++- index.php | 2 +- 6 files changed, 147 insertions(+), 6 deletions(-) diff --git a/Idno/Core/Http/Request.php b/Idno/Core/Http/Request.php index 3affe5cd3b..db04cd6b93 100644 --- a/Idno/Core/Http/Request.php +++ b/Idno/Core/Http/Request.php @@ -3,9 +3,33 @@ namespace Idno\Core\http { use Symfony\Component\HttpFoundation\Request as SymfonyRequest; + use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; class Request extends SymfonyRequest{ + public static function createFromSymfonyRequest(SymfonyRequest $request) + { + $newRequest = new self( + $request->query->all(), + $request->request->all(), + $request->attributes->all(), + $request->cookies->all(), + $request->files->all(), + $request->server->all(), + $request->getContent() + ); + + return $newRequest; + } + + public static function createFromPSR7Request(\Psr\Http\Message\ServerRequestInterface $psrRequest) + { + $httpFoundationFactory = new HttpFoundationFactory(); + $symfonyRequest = $httpFoundationFactory->createRequest($psrRequest); + return self::createFromSymfonyRequest($symfonyRequest); + + } + } } \ No newline at end of file diff --git a/Idno/Core/Http/Response.php b/Idno/Core/Http/Response.php index 9eee0e0eb6..ec37724b42 100644 --- a/Idno/Core/Http/Response.php +++ b/Idno/Core/Http/Response.php @@ -4,9 +4,13 @@ use Symfony\Component\HttpFoundation\Response as SymfonyResponse; - class Response extends SymfonyResponse{ + private bool $sendPsr7Response = false; + private $psrHttpFactory = null; + + private $delaySend = false; + public function setJsonContent($content) { $this->headers->set('Content-Type', 'application/json'); @@ -14,5 +18,17 @@ public function setJsonContent($content) return $this; } + public function delaySend() + { + $this->delaySend = true; + } + + public function isDelayed() + { + return $this->delaySend; + } + + + } } \ No newline at end of file diff --git a/Idno/Core/Idno.php b/Idno/Core/Idno.php index cd82a4f120..80aa882c05 100644 --- a/Idno/Core/Idno.php +++ b/Idno/Core/Idno.php @@ -349,14 +349,31 @@ function &redirect($url) } + function &createRequest($request=null) + { + if($request instanceof \Symfony\Component\HttpFoundation\Request){ + $this->request = Request::createFromSymfonyRequest($request); + } elseif ($request instanceof \Psr\Http\Message\ServerRequestInterface) { + $this->request = Request::createFromPsr7Request($request); + } + else { + $this->request = Request::createFromGlobals(); + } + } + /** * Send the response */ function &sendResponse() { - $response = \Idno\Core\Idno::site()->events()->triggerEvent('response/before', ['response' => $this->response()],$this->response()); - $response->send(); + $this->response = \Idno\Core\Idno::site()->events()->triggerEvent('response/before', ['response' => $this->response()],$this->response()); + // Delay sending response so that it can be modified if required + if(!$this->response->isDelayed()){ + $this->response->send(); + } + \Idno\Core\Idno::site()->events()->triggerEvent('response/delayed', ['response' => $this->response()]); + } /** diff --git a/composer.json b/composer.json index 52faa9265d..5b1f069331 100644 --- a/composer.json +++ b/composer.json @@ -58,7 +58,8 @@ "npm-asset/eonasdan-bootstrap-datetimepicker": "^6.2", "composer/installers": "^2", "vlucas/phpdotenv": "^5.3", - "landrok/activitypub": "^0.7.1" + "landrok/activitypub": "^0.7.1", + "symfony/psr-http-message-bridge": "^7.1" }, "license": "Apache-2.0", "prefer-stable": true, diff --git a/composer.lock b/composer.lock index 0ccee32b0d..cc2227a4eb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "065bdf72bb684997eb83e298a2759adb", + "content-hash": "89ff0b9a11e06e8340fa07a33812984a", "packages": [ { "name": "brick/math", @@ -4070,6 +4070,89 @@ ], "time": "2024-01-29T20:11:03+00:00" }, + { + "name": "symfony/psr-http-message-bridge", + "version": "v7.1.4", + "source": { + "type": "git", + "url": "https://github.com/symfony/psr-http-message-bridge.git", + "reference": "405a7bcd872f1563966f64be19f1362d94ce71ab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/psr-http-message-bridge/zipball/405a7bcd872f1563966f64be19f1362d94ce71ab", + "reference": "405a7bcd872f1563966f64be19f1362d94ce71ab", + "shasum": "" + }, + "require": { + "php": ">=8.2", + "psr/http-message": "^1.0|^2.0", + "symfony/http-foundation": "^6.4|^7.0" + }, + "conflict": { + "php-http/discovery": "<1.15", + "symfony/http-kernel": "<6.4" + }, + "require-dev": { + "nyholm/psr7": "^1.1", + "php-http/discovery": "^1.15", + "psr/log": "^1.1.4|^2|^3", + "symfony/browser-kit": "^6.4|^7.0", + "symfony/config": "^6.4|^7.0", + "symfony/event-dispatcher": "^6.4|^7.0", + "symfony/framework-bundle": "^6.4|^7.0", + "symfony/http-kernel": "^6.4|^7.0" + }, + "type": "symfony-bridge", + "autoload": { + "psr-4": { + "Symfony\\Bridge\\PsrHttpMessage\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "PSR HTTP message bridge", + "homepage": "https://symfony.com", + "keywords": [ + "http", + "http-message", + "psr-17", + "psr-7" + ], + "support": { + "source": "https://github.com/symfony/psr-http-message-bridge/tree/v7.1.4" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-08-15T22:48:53+00:00" + }, { "name": "symfony/service-contracts", "version": "v3.5.0", diff --git a/index.php b/index.php index bf7a80480f..a1a140c5d2 100644 --- a/index.php +++ b/index.php @@ -30,7 +30,7 @@ require_once dirname(__FILE__) . '/Idno/start.php'; // Get page routes - +\Idno\Core\Idno::site()->createRequest(); $routes = \Idno\Core\Idno::site()->routes(); // Get subdirectory From a958c070d5556c362e12bbe914e3f88013796e93 Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 10:14:57 +0530 Subject: [PATCH 06/17] fix : onboarding issues --- Idno/Common/Page.php | 1 - Idno/Core/Bonita/Templates.php | 23 ++++--- Idno/Core/Http/RedirectResponse.php | 64 ++++++++++++++++++- Idno/Core/Idno.php | 12 ++-- Idno/Core/IdnoFoundation.php | 34 ++++++++++ Idno/Core/Input.php | 4 +- Idno/Core/PageHandler.php | 4 +- Idno/Data/MySQL.php | 6 +- Idno/Pages/Onboarding/Begin.php | 2 +- Idno/Pages/Onboarding/Register.php | 1 + Idno/Pages/User/View.php | 5 ++ Idno/start.php | 35 +++++----- index.php | 3 + templates/default/settings-shell/css.tpl.php | 2 +- .../default/settings-shell/javascript.tpl.php | 2 +- templates/default/shell/css.tpl.php | 2 +- templates/default/shell/javascript.tpl.php | 2 +- .../templates/default/pages/settings.tpl.php | 10 +-- 18 files changed, 164 insertions(+), 48 deletions(-) create mode 100644 Idno/Core/IdnoFoundation.php diff --git a/Idno/Common/Page.php b/Idno/Common/Page.php index 08a40f45b8..f35ac8bdc4 100644 --- a/Idno/Common/Page.php +++ b/Idno/Common/Page.php @@ -1342,7 +1342,6 @@ public function setNoCache() public function setLastModifiedHeader(int $timestamp) { \Idno\Core\Idno::site()->response()->headers->set('Last-Modified', \Idno\Core\Time::timestampToRFC2616($timestamp)); - \Idno\Core\Idno::site()->sendResponse(); } /** diff --git a/Idno/Core/Bonita/Templates.php b/Idno/Core/Bonita/Templates.php index 188275b252..d7361488b0 100644 --- a/Idno/Core/Bonita/Templates.php +++ b/Idno/Core/Bonita/Templates.php @@ -196,20 +196,23 @@ function drawPage($echo = true, $shell = 'shell') { if ($echo) { // For Backward Compatibility reasons - \Idno\Core\Idno::site()->response()->setContent($this->draw($shell)); - \Idno\Core\Idno::site()->sendResponse(); + + \Idno\Core\Idno::site()->response()->setContent($this->draw($shell)); + \Idno\Core\Idno::site()->sendResponse(); + + // $content = $this->draw($shell); + // header('Content-Length: ' . strlen($content)); - // $content = $this->draw($shell); - // header('Content-Length: ' . strlen($content)); + // // Break long output to avoid an Apache performance bug + // $split_output = str_split($content, 1024); - // // Break long output to avoid an Apache performance bug - // $split_output = str_split($content, 1024); + // foreach ($split_output as $chunk) { + // echo $chunk; + // } + + // exit; - // foreach ($split_output as $chunk) { - // echo $chunk; - // } - // exit; } else { return $this->draw($shell); } diff --git a/Idno/Core/Http/RedirectResponse.php b/Idno/Core/Http/RedirectResponse.php index 932018d567..e8dd9a007e 100644 --- a/Idno/Core/Http/RedirectResponse.php +++ b/Idno/Core/Http/RedirectResponse.php @@ -2,9 +2,69 @@ namespace Idno\Core\http { - use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirectResponse; - class RedirectResponse extends SymfonyRedirectResponse{ + class RedirectResponse extends Response + { + private string $targetUrl; + + public function __construct(string $url, int $status = 302, array $headers = []) + { + parent::__construct('', $status, $headers); + + $this->setTargetUrl($url); + + if (!$this->isRedirect()) { + throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status)); + } + + if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) { + $this->headers->remove('cache-control'); + } + } + + /** + * Returns the target URL. + */ + public function getTargetUrl(): string + { + return $this->targetUrl; + } + + /** + * Sets the redirect target of this response. + * + * @return $this + * + * @throws \InvalidArgumentException + */ + public function setTargetUrl(string $url): static + { + if ('' === $url) { + throw new \InvalidArgumentException('Cannot redirect to an empty URL.'); + } + + $this->targetUrl = $url; + + $this->setContent( + sprintf(' + + + + + + Redirecting to %1$s + + + Redirecting to %1$s. + + ', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8')) + ); + + $this->headers->set('Location', $url); + $this->headers->set('Content-Type', 'text/html; charset=utf-8'); + + return $this; + } } } \ No newline at end of file diff --git a/Idno/Core/Idno.php b/Idno/Core/Idno.php index 80aa882c05..b822622932 100644 --- a/Idno/Core/Idno.php +++ b/Idno/Core/Idno.php @@ -64,8 +64,8 @@ function init() $this->dispatcher = new EventDispatcher(); $this->config = new Config(); if ($this->config->isDefaultConfig()) { - header('Location: ./warmup/'); - exit; // Load the installer + $this->redirect('./warmup/'); + $this->sendResponse(); // Load the installer } // We need to load initial values from the .ini files @@ -825,10 +825,14 @@ function __sleep() /** * Helper method that returns the current site object * - * @return \Idno\Core\Idno $site + * @return \Idno\Core\Idno | \Idno\Core\IdnoFoundation $site */ - static function &site(): ?Idno + static function &site():Idno|IdnoFoundation { + // Workaround for wormup to work + if(empty(self::$site)){ + self::$site = new IdnoFoundation(); + } return self::$site; } diff --git a/Idno/Core/IdnoFoundation.php b/Idno/Core/IdnoFoundation.php new file mode 100644 index 0000000000..0a3ee2b972 --- /dev/null +++ b/Idno/Core/IdnoFoundation.php @@ -0,0 +1,34 @@ +request = Request::createFromGlobals(); + $this->response = new \Idno\Core\Http\Response(); + } + + public function &request() + { + return $this->request; + } + + public function &response() + { + return $this->response; + } + + function &sendResponse() + { + $this->response->send(); + + } + } +} \ No newline at end of file diff --git a/Idno/Core/Input.php b/Idno/Core/Input.php index f1ed34c5e7..a4ca5ff089 100644 --- a/Idno/Core/Input.php +++ b/Idno/Core/Input.php @@ -26,8 +26,8 @@ public static function getInput($name, $default = null, callable $filter = null) { if (!empty($name)) { $value = null; - if (isset($_REQUEST[$name])) { - $value = $_REQUEST[$name]; + if (\Idno\Core\Idno::site()->request()->request->has($name)) { + $value = \Idno\Core\Idno::site()->request()->request->get($name); } if (($value===null) && ($default!==null)) { $value = $default; diff --git a/Idno/Core/PageHandler.php b/Idno/Core/PageHandler.php index cb8ef99c6b..45fb0f6c0a 100644 --- a/Idno/Core/PageHandler.php +++ b/Idno/Core/PageHandler.php @@ -38,6 +38,8 @@ public static function serve($routes) if (! empty(Idno::site()->request()->getPathInfo())) { $path_info = Idno::site()->request()->getPathInfo(); } + + $discovered_handler = null; $regex_matches = array(); @@ -60,6 +62,7 @@ public static function serve($routes) } } + $result = null; $handler_instance = null; @@ -87,7 +90,6 @@ public static function serve($routes) } Hook::fire('after_request', compact('routes', 'discovered_handler', 'request_method', 'regex_matches', 'result')); - \Idno\Core\Idno::site()->sendResponse(); } diff --git a/Idno/Data/MySQL.php b/Idno/Data/MySQL.php index 1259d21696..fc42f7c77a 100644 --- a/Idno/Data/MySQL.php +++ b/Idno/Data/MySQL.php @@ -13,6 +13,7 @@ class MySQL extends AbstractSQL { + private $database; function init() { @@ -27,11 +28,12 @@ function init() } catch (\Exception $e) { error_log($e->getMessage()); if (!empty(\Idno\Core\Idno::site()->config()->forward_on_empty)) { - header('Location: ' . \Idno\Core\Idno::site()->config()->forward_on_empty); - exit; + \Idno\Core\Idno::site()->redirect(\Idno\Core\Idno::site()->config()->forward_on_empty); + \Idno\Core\Idno::site()->sendResponse(); } else { http_response_code(500); + if (\Idno\Core\Idno::site()->config()->debug) { $message = '

' . $e->getMessage() . '

'; diff --git a/Idno/Pages/Onboarding/Begin.php b/Idno/Pages/Onboarding/Begin.php index b6edea1424..bce0c60eb0 100644 --- a/Idno/Pages/Onboarding/Begin.php +++ b/Idno/Pages/Onboarding/Begin.php @@ -24,7 +24,7 @@ function getContent() 'title' => \Idno\Core\Idno::site()->language()->_('Welcome to Known'), 'messages' => \Idno\Core\Idno::site()->session()->getAndFlushMessages() ) - )->draw('shell/simple') + )->draw('shell/simple'); \Idno\Core\Idno::site()->response()->setContent($content); } diff --git a/Idno/Pages/Onboarding/Register.php b/Idno/Pages/Onboarding/Register.php index 338d025b05..aac0567091 100644 --- a/Idno/Pages/Onboarding/Register.php +++ b/Idno/Pages/Onboarding/Register.php @@ -12,6 +12,7 @@ class Register extends \Idno\Common\Page function getContent() { + $set_name = $this->getInput('set_name'); if (!empty($set_name)) { \Idno\Core\Idno::site()->session()->set('set_name', $set_name); diff --git a/Idno/Pages/User/View.php b/Idno/Pages/User/View.php index 59c49b1fa8..84c37fdb4e 100644 --- a/Idno/Pages/User/View.php +++ b/Idno/Pages/User/View.php @@ -28,6 +28,7 @@ function getContent() $this->noContent(); } + // Users own their own profiles $this->setOwner($user); $this->setPermalink(true, $user); // This is a permalink @@ -52,6 +53,7 @@ function getContent() } else { $types = ['IdnoPlugins\Status\Status', 'IdnoPlugins\Text\Entry']; } + $offset = (int)$this->getInput('offset'); $count = \Idno\Common\Entity::countFromX($types, array('owner' => $user->getUUID(), 'publish_status' => 'published')); $feed = \Idno\Common\Entity::getFromX($types, array('owner' => $user->getUUID(), 'publish_status' => 'published'), array(), \Idno\Core\Idno::site()->config()->items_per_page, $offset); @@ -62,8 +64,10 @@ function getContent() $last_modified = $feed[0]->updated; } } + $this->setLastModifiedHeader($last_modified); + $t = \Idno\Core\Idno::site()->template(); $t->__( array( @@ -76,6 +80,7 @@ function getContent() )->drawPage(); } + // Handle POST requests to the entity function postContent() diff --git a/Idno/start.php b/Idno/start.php index e118f5393a..bc903610a5 100644 --- a/Idno/start.php +++ b/Idno/start.php @@ -71,22 +71,25 @@ function () { ); // This is a good time to see if we're running in a subdirectory - if (!defined('KNOWN_UNIT_TEST')) { - if (!empty($_SERVER['PHP_SELF'])) { - if ($subdir = dirname($_SERVER['PHP_SELF'])) { - if ($subdir != DIRECTORY_SEPARATOR) { - if (substr($subdir, -1) == DIRECTORY_SEPARATOR) { - $subdir = substr($subdir, 0, -1); - } - if (substr($subdir, 0, 1) == DIRECTORY_SEPARATOR) { - $subdir = substr($subdir, 1); - } - $subdir = str_replace(DIRECTORY_SEPARATOR, '/', $subdir); - define('KNOWN_SUBDIRECTORY', $subdir); - } - } - } - } + // if (!defined('KNOWN_UNIT_TEST')) { + // if (!empty($_SERVER['PHP_SELF'])) { + // print_r($_SERVER['PHP_SELF'].'
'); + + // if ($subdir = dirname($_SERVER['PHP_SELF'])) { + // if ($subdir != DIRECTORY_SEPARATOR) { + // if (substr($subdir, -1) == DIRECTORY_SEPARATOR) { + // $subdir = substr($subdir, 0, -1); + // } + // if (substr($subdir, 0, 1) == DIRECTORY_SEPARATOR) { + // $subdir = substr($subdir, 1); + // } + // $subdir = str_replace(DIRECTORY_SEPARATOR, '/', $subdir); + // define('KNOWN_SUBDIRECTORY', $subdir); + + // } + // } + // } + // } // Set time limit if we're using less if (ini_get('max_execution_time') < 120 && ini_get('safe_mode')) { diff --git a/index.php b/index.php index a1a140c5d2..f91e70e35f 100644 --- a/index.php +++ b/index.php @@ -14,6 +14,9 @@ * @subpackage core */ + error_reporting(E_ALL ); + + // Check PHP version first of all if (version_compare(phpversion(), '7.2', '<')) { http_response_code(500); diff --git a/templates/default/settings-shell/css.tpl.php b/templates/default/settings-shell/css.tpl.php index e0d4181c46..ca8f259f66 100644 --- a/templates/default/settings-shell/css.tpl.php +++ b/templates/default/settings-shell/css.tpl.php @@ -1,4 +1,4 @@ - + - + diff --git a/templates/default/shell/css.tpl.php b/templates/default/shell/css.tpl.php index 70daf0523c..f56045c942 100644 --- a/templates/default/shell/css.tpl.php +++ b/templates/default/shell/css.tpl.php @@ -1,4 +1,4 @@ - + - + diff --git a/warmup/templates/default/pages/settings.tpl.php b/warmup/templates/default/pages/settings.tpl.php index cc7ebc2200..41347b6eb1 100644 --- a/warmup/templates/default/pages/settings.tpl.php +++ b/warmup/templates/default/pages/settings.tpl.php @@ -73,7 +73,7 @@

-

@@ -93,7 +93,7 @@ class="profile-input" id="site_title">

@@ -101,7 +101,7 @@ class="profile-input" required>

@@ -116,7 +116,7 @@ class="profile-input" required>

@@ -137,7 +137,7 @@ class="profile-input" required>

+ value="" class="profile-input" required>

From f5b8c7fdb99c6107dba73c51a3850d7b651d6c6d Mon Sep 17 00:00:00 2001 From: ipranjal Date: Sun, 22 Sep 2024 17:51:37 +0530 Subject: [PATCH 07/17] replace $_SESSSION superglobal --- Idno/Common/Page.php | 6 ++--- Idno/Core/Bonita/Main.php | 4 ++-- Idno/Core/Config.php | 18 +++++++-------- Idno/Core/Hub.php | 8 +++---- Idno/Core/Idno.php | 12 +++++----- Idno/Core/Language.php | 6 ++--- Idno/Core/Logging.php | 12 +++++----- Idno/Core/Service.php | 3 ++- Idno/Core/Session.php | 22 +++++++++---------- Idno/Core/Templating/Classes.php | 2 +- Idno/Core/Templating/Urls.php | 2 +- Idno/Pages/Account/Password/Reset.php | 2 +- Idno/Pages/Account/Register.php | 2 +- Idno/Pages/Account/Settings.php | 2 +- .../Settings/Following/Bookmarklet.php | 2 +- Idno/Pages/Account/Settings/Notifications.php | 2 +- Idno/Pages/Account/Settings/Tools.php | 2 +- Idno/Pages/Admin/Diagnostics.php | 2 +- Idno/Pages/Annotation/Post.php | 2 +- Idno/Pages/Entity/Attachment/Delete.php | 2 +- Idno/Pages/Entity/Delete.php | 2 +- Idno/Pages/Entity/View.php | 4 ++-- Idno/Pages/File/Picker.php | 2 +- Idno/Pages/File/View.php | 4 ++-- Idno/Pages/Robot/Remove.php | 2 +- Idno/Pages/Session/Logout.php | 2 +- Idno/Pages/User/View.php | 4 ++-- Tests/_bootstrap.php | 16 +++++++------- .../default/cherwell/admin/menu.tpl.php | 2 +- .../templates/default/account/menu.tpl.php | 8 +++---- .../default/account/settings/menu.tpl.php | 6 ++--- templates/default/account/login.tpl.php | 4 ++-- templates/default/pages/403.tpl.php | 2 +- templates/default/shell/simple.tpl.php | 2 +- templates/email/shell.tpl.php | 2 +- warmup/WebInstaller.php | 6 ++--- 36 files changed, 91 insertions(+), 90 deletions(-) diff --git a/Idno/Common/Page.php b/Idno/Common/Page.php index f35ac8bdc4..b17f4edd51 100644 --- a/Idno/Common/Page.php +++ b/Idno/Common/Page.php @@ -225,15 +225,15 @@ function parseJSONPayload() { // First, let's see if we've been sent anything in form input - if (!empty($_REQUEST['json'])) { - $json = trim($_REQUEST['json']); + if ( \Idno\Core\Idno::site()->request()->request->has('json')) { + $json = trim(\Idno\Core\Idno::site()->request()->request->get('json')); $json = str_replace('[]"', '"', $json); // Fake PHP's array conversion if ($parsed = @json_decode($json, true)) { $this->data = array_merge($parsed, $this->data()); } } - if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != 'GET') { + if ( \Idno\Core\Idno->request()->getMethod() != 'GET') { $body = @file_get_contents('php://input'); $body = trim($body); $body = str_replace('[]"', '"', $body); // Fake PHP's array conversion diff --git a/Idno/Core/Bonita/Main.php b/Idno/Core/Bonita/Main.php index 26df72dbaa..381e7a916d 100644 --- a/Idno/Core/Bonita/Main.php +++ b/Idno/Core/Bonita/Main.php @@ -105,10 +105,10 @@ public static function getSiteSecret() */ static function detectDevice() { - if (empty($_SERVER['HTTP_USER_AGENT'])) { + if (!\Idno\Core\Idno::site()->request()->server->has('HTTP_USER_AGENT')) { return 'default'; } - $ua = $_SERVER['HTTP_USER_AGENT']; + $ua = \Idno\Core\Idno::site()->request()->server->get('HTTP_USER_AGENT'); // Android if (preg_match('/android/i', $ua)) { diff --git a/Idno/Core/Config.php b/Idno/Core/Config.php index 8ff0d42cb7..8700c44e69 100644 --- a/Idno/Core/Config.php +++ b/Idno/Core/Config.php @@ -241,7 +241,7 @@ function loadIniFiles() } // Check environment variables and set as appropriate - foreach ($_SERVER as $name => $val) { + foreach (\Idno\Core\Idno::site()->request()->server->all() as $name => $val) { if (substr($name, 0, 6) == 'KNOWN_') { $name = strtolower(str_replace('KNOWN_', '', $name)); $val = $val; @@ -377,17 +377,17 @@ protected function detectBaseURL() { // Otherwise, use the standard server name header - if (!empty($_SERVER['SERVER_NAME'])) { + if (\Idno\Core\Idno::site()->request()->server->has('SERVER_NAME')) { // Servername specified, so we can construct things in the normal way. - $url = (\Idno\Common\Page::isSSL() ? 'https://' : 'http://') . $_SERVER['SERVER_NAME']; - if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) { - if ($_SERVER['HTTP_X_FORWARDED_PORT'] != 80 && $_SERVER['HTTP_X_FORWARDED_PORT'] != 443) { - $url .= ':' . $_SERVER['HTTP_X_FORWARDED_PORT']; + $url = (\Idno\Common\Page::isSSL() ? 'https://' : 'http://') . \Idno\Core\Idno::site()->request()->server->get('SERVER_NAME'); + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_X_FORWARDED_PORT')) { + if (\Idno\Core\Idno::site()->request()->server->get('HTTP_X_FORWARDED_PORT') != 80 && \Idno\Core\Idno::site()->request()->server->get('HTTP_X_FORWARDED_PORT') != 443) { + $url .= ':' . \Idno\Core\Idno::site()->request()->server->get('HTTP_X_FORWARDED_PORT'); } - } else if (!empty($_SERVER['SERVER_PORT'])) { - if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) { - $url .= ':' . $_SERVER['SERVER_PORT']; + } else if (\Idno\Core\Idno::site()->request()->server->has('SERVER_PORT')) { + if (\Idno\Core\Idno::site()->request()->server->get('SERVER_PORT') != 80 && \Idno\Core\Idno::site()->request()->server->get('SERVER_PORT') != 443) { + $url .= ':' . \Idno\Core\Idno::site()->request()->server->get('SERVER_PORT'); } } if (defined('KNOWN_SUBDIRECTORY')) { diff --git a/Idno/Core/Hub.php b/Idno/Core/Hub.php index db3436d577..e2702ab024 100644 --- a/Idno/Core/Hub.php +++ b/Idno/Core/Hub.php @@ -99,7 +99,7 @@ function registerUser($user = false) * Load the locally stored auth token & secret details, or register with the hub if no details have been * saved * - * @return bool + * @return mixed */ function loadDetails() { @@ -144,9 +144,9 @@ function connect() $details = $this->loadDetails(); if (!empty($details['auth_token'])) { // Apply pre-stored auth details and connect to server - } else if (!substr_count($_SERVER['REQUEST_URI'], 'callback') - && !substr_count($_SERVER['REQUEST_URI'], '.') - && !substr_count($_SERVER['REQUEST_URI'], '/file/') + } else if (!substr_count(\Idno\Core\Idno::site()->request()->getRequestUri(), 'callback') + && !substr_count(\Idno\Core\Idno::site()->request()->getRequestUri(), '.') + && !substr_count(\Idno\Core\Idno::site()->request()->getRequestUri(), '/file/') ) { // Establish auth details, save them, and then connect if ($details = $this->register()) { diff --git a/Idno/Core/Idno.php b/Idno/Core/Idno.php index b822622932..20f2040dc7 100644 --- a/Idno/Core/Idno.php +++ b/Idno/Core/Idno.php @@ -80,12 +80,12 @@ function init() $this->db = new \Idno\Data\MySQL(); break; case 'beanstalk-mysql': // A special instance of MYSQL designed for use with Amazon Elastic Beanstalk - $this->config->dbhost = $_SERVER['RDS_HOSTNAME']; - $this->config->dbuser = $_SERVER['RDS_USERNAME']; - $this->config->dbpass = $_SERVER['RDS_PASSWORD']; - $this->config->dbport = $_SERVER['RDS_PORT']; + $this->config->dbhost = \Idno\Core\Idno::site()->request()->server->get('RDS_HOSTNAME'); ; + $this->config->dbuser = \Idno\Core\Idno::site()->request()->server->get('RDS_USERNAME'); + $this->config->dbpass = \Idno\Core\Idno::site()->request()->server->get('RDS_PASSWORD'); + $this->config->dbport = \Idno\Core\Idno::site()->request()->server->get('RDS_PORT'); if (empty($this->config->dbname)) { - $this->config->dbname = $_SERVER['RDS_DB_NAME']; + $this->config->dbname = \Idno\Core\Idno::site()->request()->server->get('RDS_DB_NAME'); } $this->db = new \Idno\Data\MySQL(); break; @@ -171,7 +171,7 @@ function () { } if ( !empty($this->config->known_hub) - && !substr_count($_SERVER['REQUEST_URI'], '.') + && !substr_count(\Idno\Core\Idno::site()->request()->getRequestUri(), '.') && $this->config->known_hub != $this->config->url ) { site()->session()->hub_connect = time(); diff --git a/Idno/Core/Language.php b/Idno/Core/Language.php index 9ee7b7d084..b29cdbd6ab 100644 --- a/Idno/Core/Language.php +++ b/Idno/Core/Language.php @@ -213,12 +213,12 @@ public static function detectBrowserLanguage($full = true) $lang = ""; - if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_ACCEPT_LANGUAGE')) { if ($full) { - $length = strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ','); + $length = strpos(\Idno\Core\Idno::site()->request()->server->get('HTTP_ACCEPT_LANGUAGE'), ','); } - $lang = preg_replace("/[^a-zA-Z\-_\s]/", "", substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, $length)); + $lang = preg_replace("/[^a-zA-Z\-_\s]/", "", substr(\Idno\Core\Idno::site()->request()->server->get('HTTP_ACCEPT_LANGUAGE'), 0, $length)); $lang = str_replace('-', '_', $lang); } diff --git a/Idno/Core/Logging.php b/Idno/Core/Logging.php index 9046ee8b2e..d8de5fa65e 100644 --- a/Idno/Core/Logging.php +++ b/Idno/Core/Logging.php @@ -354,16 +354,16 @@ public static function oopsAlert($message, $title = "") $vars['user'] = \Idno\Core\Idno::site()->session()->currentUserUUID(); } - if (!empty($_SERVER['HTTP_USER_AGENT'])) { - $vars['agent'] = $_SERVER['HTTP_USER_AGENT']; + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_USER_AGENT')) { + $vars['agent'] = \Idno\Core\Idno::site()->request()->server->get('HTTP_USER_AGENT'); } - if (!empty($_SERVER['QUERY_STRING'])) { - $vars['qs'] = $_SERVER['QUERY_STRING']; + if (\Idno\Core\Idno::site()->request()->server->has('QUERY_STRING')) { + $vars['qs'] = \Idno\Core\Idno::site()->request()->server->get('QUERY_STRING'); } - if (!empty($_SERVER['HTTP_REFERER'])) { - $vars['referrer'] = $_SERVER['HTTP_REFERER']; + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_REFERER')) { + $vars['referrer'] = \Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER'); } $email = new Email(); diff --git a/Idno/Core/Service.php b/Idno/Core/Service.php index 46bc4829db..d2a463f6a6 100644 --- a/Idno/Core/Service.php +++ b/Idno/Core/Service.php @@ -20,7 +20,8 @@ class Service extends \Idno\Common\Component */ public static function gatekeeper() { - $service_signature = $_SERVER['HTTP_X_KNOWN_SERVICE_SIGNATURE']; + + $service_signature = \Idno\Core\Idno::site()->request()->server->get('X-KNOWN-SERVICE-SIGNATURE'); if (empty($service_signature)) { throw new \RuntimeException(\Idno\Core\Idno::site()->language()->_('Missing X-Known-Service-Signature, service call is not possible.')); } diff --git a/Idno/Core/Session.php b/Idno/Core/Session.php index 70ca419f95..8bf00e8e96 100644 --- a/Idno/Core/Session.php +++ b/Idno/Core/Session.php @@ -497,7 +497,7 @@ function tryAuthUser() $return = \Idno\Core\Idno::site()->events()->triggerEvent('user/auth/request', [], false); // auth standard API requests - if (!$return && !empty($_SERVER['HTTP_X_KNOWN_USERNAME']) && !empty($_SERVER['HTTP_X_KNOWN_SIGNATURE'])) { + if (!$return && \Idno\Core\Idno::site()->request()->server->has('HTTP_X_KNOWN_USERNAME') && !empty(\Idno\Core\Idno::site()->request()->server->has('HTTP_X_KNOWN_SIGNATURE'))) { \Idno\Core\Idno::site()->logging()->debug("Attempting to auth via API credentials"); $this->setIsAPIRequest(true); @@ -507,16 +507,16 @@ function tryAuthUser() \Idno\Core\Idno::site()->template()->setTemplateType('json'); } - $user = \Idno\Entities\User::getByHandle($_SERVER['HTTP_X_KNOWN_USERNAME']); - if (empty($user)) { $user = \Idno\Entities\User::getByEmail($_SERVER['HTTP_X_KNOWN_USERNAME']); + $user = \Idno\Entities\User::getByHandle(\Idno\Core\Idno::site()->request()->server->get('HTTP_X_KNOWN_USERNAME')); + if (empty($user)) { $user = \Idno\Entities\User::getByEmail(\Idno\Core\Idno::site()->request()->server->get('HTTP_X_KNOWN_USERNAME')); } if (!empty($user)) { - \Idno\Core\Idno::site()->logging()->debug("API auth found user by username: {$_SERVER['HTTP_X_KNOWN_USERNAME']} - " . $user->getName()); + \Idno\Core\Idno::site()->logging()->debug("API auth found user by username: ".{\Idno\Core\Idno::site()->request()->server->has('HTTP_X_KNOWN_USERNAME')." - " . $user->getName()); $key = $user->getAPIkey(); - $hmac = trim($_SERVER['HTTP_X_KNOWN_SIGNATURE']); + $hmac = trim(\Idno\Core\Idno::site()->request()->server->has('HTTP_X_KNOWN_SIGNATURE')); //$compare_hmac = base64_encode(hash_hmac('sha256', explode('?', $_SERVER['REQUEST_URI'])[0], $key, true)); - $compare_hmac = base64_encode(hash_hmac('sha256', ($_SERVER['REQUEST_URI']), $key, true)); + $compare_hmac = base64_encode(hash_hmac('sha256', \Idno\Core\Idno::site()->request()->getUri(), $key, true)); if ($hmac == $compare_hmac) { \Idno\Core\Idno::site()->logging()->debug("API auth verified signature for user: " . $user->getName()); @@ -524,11 +524,11 @@ function tryAuthUser() $return = $this->refreshSessionUser($user); } else { \Idno\Core\Idno::site()->logging()->debug("API auth failed signature validation for user: " . $user->getName()); - \Idno\Core\Idno::site()->logging()->debug("Expected signature formed over base64_encode(hash_hmac('sha256', '{$_SERVER['REQUEST_URI']}', \$key, true)) = '$compare_hmac', but got '$hmac'. "); + \Idno\Core\Idno::site()->logging()->debug("Expected signature formed over base64_encode(hash_hmac('sha256', '{\Idno\Core\Idno::site()->request()->getRequestUri())}', \$key, true)) = '$compare_hmac', but got '$hmac'. "); \Idno\Core\Idno::site()->logging()->debug("Please read http://docs.withknown.com/en/latest/developers/plugins/api/ for further details."); } } else { - \Idno\Core\Idno::site()->logging()->debug("API User given in X_KNOWN_USERNAME ('{$_SERVER['HTTP_X_KNOWN_USERNAME']}') could not be found."); + \Idno\Core\Idno::site()->logging()->debug("API User given in X_KNOWN_USERNAME ('{\Idno\Core\Idno::site()->request()->server->get('HTTP_X_KNOW_USERNAME'))}') could not be found."); } } @@ -544,9 +544,9 @@ function tryAuthUser() } // If this is an API request but we're not logged in, set page response code to access denied if (!$return) { - $ip = $_SERVER['REMOTE_ADDR']; - if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { - $proxies = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); // We are behind a proxy + $ip = \Idno\Core\Idno::site()->request()->server->get('REMOTE_ADDR'); + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_X_FORWARDED_FOR')) { + $proxies = explode(',', \Idno\Core\Idno::site()->request()->server->get('HTTP_X_FORWARDED_FOR')); // We are behind a proxy $ip = trim($proxies[0]); } diff --git a/Idno/Core/Templating/Classes.php b/Idno/Core/Templating/Classes.php index aab284943d..4d3c114214 100644 --- a/Idno/Core/Templating/Classes.php +++ b/Idno/Core/Templating/Classes.php @@ -15,7 +15,7 @@ function getBodyClasses() { $classes = ''; $classes .= (str_replace('\\', '_', strtolower(get_class(\Idno\Core\Idno::site()->currentPage())))); - if ($path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) { + if ($path = parse_url(\Idno\Core\Idno::site()->request()->getUri(), PHP_URL_PATH)) { if ($path == '/') { $classes .= ' homepage'; } diff --git a/Idno/Core/Templating/Urls.php b/Idno/Core/Templating/Urls.php index 1b82777570..d9d12fa6e2 100644 --- a/Idno/Core/Templating/Urls.php +++ b/Idno/Core/Templating/Urls.php @@ -49,7 +49,7 @@ function getCurrentURL() $path = substr($components['path'], 1); } } - $request_uri = substr($_SERVER['REQUEST_URI'], 1); + $request_uri = substr(\Idno\Core\Idno::site()->request()->getUri(), 1); if (!empty($path)) { if (substr($request_uri, 0, strlen($path)) == $path) { $request_uri = substr($request_uri, strlen($path)); diff --git a/Idno/Pages/Account/Password/Reset.php b/Idno/Pages/Account/Password/Reset.php index 35a3cca2e0..be4fb9c202 100644 --- a/Idno/Pages/Account/Password/Reset.php +++ b/Idno/Pages/Account/Password/Reset.php @@ -61,7 +61,7 @@ function postContent() } } else { \Idno\Core\Idno::site()->session()->addErrorMessage(\Idno\Core\Idno::site()->language()->_('Sorry, your passwords either don\'t match, or are too weak')); - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Account/Register.php b/Idno/Pages/Account/Register.php index 0ec766b85b..a259d5fc6c 100644 --- a/Idno/Pages/Account/Register.php +++ b/Idno/Pages/Account/Register.php @@ -166,7 +166,7 @@ function postContent() $this->forward(); } else { \Idno\Core\Idno::site()->session()->addMessageAtStart(\Idno\Core\Idno::site()->language()->_("We couldn't register you.")); - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Account/Settings.php b/Idno/Pages/Account/Settings.php index 02de629d7e..417052e352 100644 --- a/Idno/Pages/Account/Settings.php +++ b/Idno/Pages/Account/Settings.php @@ -65,7 +65,7 @@ function postContent() if ($user->save()) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->_("Your details were saved.")); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Account/Settings/Following/Bookmarklet.php b/Idno/Pages/Account/Settings/Following/Bookmarklet.php index 0f3cae281e..525ae4152b 100644 --- a/Idno/Pages/Account/Settings/Following/Bookmarklet.php +++ b/Idno/Pages/Account/Settings/Following/Bookmarklet.php @@ -69,7 +69,7 @@ function getContent() } // forward back - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } /** diff --git a/Idno/Pages/Account/Settings/Notifications.php b/Idno/Pages/Account/Settings/Notifications.php index 96d2d3eab5..0ba76d286e 100644 --- a/Idno/Pages/Account/Settings/Notifications.php +++ b/Idno/Pages/Account/Settings/Notifications.php @@ -38,7 +38,7 @@ function postContent() if ($user->save()) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->_("Your notification preferences were saved.")); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Account/Settings/Tools.php b/Idno/Pages/Account/Settings/Tools.php index 182ac34cfd..85edfc33a4 100644 --- a/Idno/Pages/Account/Settings/Tools.php +++ b/Idno/Pages/Account/Settings/Tools.php @@ -45,7 +45,7 @@ function postContent() } } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Admin/Diagnostics.php b/Idno/Pages/Admin/Diagnostics.php index be677a9d0a..42aa04e8c9 100644 --- a/Idno/Pages/Admin/Diagnostics.php +++ b/Idno/Pages/Admin/Diagnostics.php @@ -44,7 +44,7 @@ function getContent() $report .= "\nRunning config:\n---------------\n" . var_export($config, true) . "\n\n"; $report .= "\$_SESSION:\n----------\n" . var_export($_SESSION, true) . "\n\n"; - $report .= "\$_SERVER:\n---------\n" . var_export($_SERVER, true) . "\n\n"; + $report .= "\SERVER:\n---------\n" . var_export(\Idno\Core\Idno::site()->request()->server->all(), true) . "\n\n"; // Hook so other plugins and subsystems can add their own data to the report. $report = \Idno\Core\Idno::site()->events()->triggerEvent('diagnostics/report', [], $report); diff --git a/Idno/Pages/Annotation/Post.php b/Idno/Pages/Annotation/Post.php index 427a9a358b..9c19e5ce8f 100644 --- a/Idno/Pages/Annotation/Post.php +++ b/Idno/Pages/Annotation/Post.php @@ -17,7 +17,7 @@ class Post extends \Idno\Common\Page // Handle GET requests to a comment function getContent() { - $this->forward($_SERVER['HTTP_REFERER']); // Send the user back to whence they came + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); // Send the user back to whence they came } // Handle POST requests: this is where the interesting stuff happens diff --git a/Idno/Pages/Entity/Attachment/Delete.php b/Idno/Pages/Entity/Attachment/Delete.php index 902ae70a59..aa354f8406 100644 --- a/Idno/Pages/Entity/Attachment/Delete.php +++ b/Idno/Pages/Entity/Attachment/Delete.php @@ -34,7 +34,7 @@ function postContent() $this->noContent(); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Entity/Delete.php b/Idno/Pages/Entity/Delete.php index 51ddafb635..394d63f7da 100644 --- a/Idno/Pages/Entity/Delete.php +++ b/Idno/Pages/Entity/Delete.php @@ -55,7 +55,7 @@ function postContent() if ($object->delete()) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->esc_('%s was deleted.', [$object->getTitle()])); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/Entity/View.php b/Idno/Pages/Entity/View.php index 45c4e9932d..568f794599 100644 --- a/Idno/Pages/Entity/View.php +++ b/Idno/Pages/Entity/View.php @@ -116,7 +116,7 @@ function postContent() if ($object->saveDataFromInput()) { $this->forward($object->getDisplayURL()); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } // Handle DELETE requests to the entity @@ -134,7 +134,7 @@ function deleteContent() if ($object->delete()) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->esc_('%s was deleted.', [$object->getTitle()])); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/File/Picker.php b/Idno/Pages/File/Picker.php index 4e6d4b5a34..7df2a08f32 100644 --- a/Idno/Pages/File/Picker.php +++ b/Idno/Pages/File/Picker.php @@ -58,7 +58,7 @@ function post() Idno::site()->session()->addErrorMessage(\Idno\Core\Idno::site()->language()->_("You can only upload images.")); } } - $this->forward($_SERVER['HTTP_REDIRECT']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } } diff --git a/Idno/Pages/File/View.php b/Idno/Pages/File/View.php index 9bb860cd18..a7e5bfe814 100644 --- a/Idno/Pages/File/View.php +++ b/Idno/Pages/File/View.php @@ -53,7 +53,7 @@ function getContent() header('Accept-Ranges: bytes'); // Partial content - if (isset($_SERVER['HTTP_RANGE'])) { + if (\Idno\Core\Idno::site()->request()->server->has('HTTP_RANGE')) { $size = $object->getSize(); $start = 0; @@ -63,7 +63,7 @@ function getContent() $c_end = (empty($end)) ? ($size - 1) : min(abs(intval($end)), ($size - 1)); //$end; // Parse range - list(, $range) = explode('=', $_SERVER['HTTP_RANGE'], 2); + list(, $range) = explode('=', \Idno\Core\Idno::site()->request()->server->get('HTTP_RANGE'), 2); if ($range[0] == '-') { // Range form "-123" diff --git a/Idno/Pages/Robot/Remove.php b/Idno/Pages/Robot/Remove.php index 80b359a1c7..0f948b8ce9 100644 --- a/Idno/Pages/Robot/Remove.php +++ b/Idno/Pages/Robot/Remove.php @@ -17,7 +17,7 @@ function postContent() $user = \Idno\Core\Idno::site()->session()->currentUser(); $user->robot_state = 0; $user->save(); - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } diff --git a/Idno/Pages/Session/Logout.php b/Idno/Pages/Session/Logout.php index 0eb7538707..1de71229e4 100644 --- a/Idno/Pages/Session/Logout.php +++ b/Idno/Pages/Session/Logout.php @@ -22,7 +22,7 @@ function postContent() $result = \Idno\Core\Idno::site()->session()->logUserOff(); \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->_("You've signed out. See you soon!")); - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); return $result; } diff --git a/Idno/Pages/User/View.php b/Idno/Pages/User/View.php index 84c37fdb4e..02dfb8ebc0 100644 --- a/Idno/Pages/User/View.php +++ b/Idno/Pages/User/View.php @@ -96,7 +96,7 @@ function postContent() } $this->forward($user->getURL()); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } // Handle DELETE requests to the entity @@ -111,7 +111,7 @@ function deleteContent() if ($object->delete()) { \Idno\Core\Idno::site()->session()->addMessage(\Idno\Core\Idno::site()->language()->esc_('%s was deleted.', [$object->getTitle()])); } - $this->forward($_SERVER['HTTP_REFERER']); + $this->forward(\Idno\Core\Idno::site()->request()->server->get('HTTP_REFERER')); } /** diff --git a/Tests/_bootstrap.php b/Tests/_bootstrap.php index 70150dece4..3e59f3221b 100644 --- a/Tests/_bootstrap.php +++ b/Tests/_bootstrap.php @@ -16,24 +16,24 @@ // Set some environment: Use export KNOWN_DOMAIN / KNOWN_PORT to override from the command line $domain = 'localhost'; -if (isset($_SERVER['KNOWN_DOMAIN'])) { - $domain = $_SERVER['KNOWN_DOMAIN']; +if (\Idno\Core\Idno::site()->request()->server->has('KNOWN_DOMAIN')) { + $domain = \Idno\Core\Idno::site()->request()->server->get('KNOWN_DOMAIN'); } -if (!$domain && isset($_SERVER['SERVER_NAME'])) { - $domain = $_SERVER['SERVER_NAME']; +if (!$domain && \Idno\Core\Idno::site()->request()->server->has('SERVER_NAME')) { + $domain = \Idno\Core\Idno::site()->request()->server->get('SERVER_NAME'); } -$_SERVER['SERVER_NAME'] = $domain; +\Idno\Core\Idno::site()->request()->server->set('SERVER_NAME', $domain); $port = getenv('KNOWN_PORT'); -if (!$port && isset($_SERVER['SERVER_PORT'])) { - $port = $_SERVER['SERVER_PORT']; +if (!$port && \Idno\Core\Idno::site()->request()->server->has('SERVER_PORT')) { + $port =\Idno\Core\Idno::site()->request()->server->get('SERVER_PORT'); } if (!$port) { $port = 80; } -$_SERVER['SERVER_PORT'] = $port; +\Idno\Core\Idno::site()->request()->server->set('SERVER_PORT', $port); try { // Load Known framework diff --git a/Themes/Cherwell/templates/default/cherwell/admin/menu.tpl.php b/Themes/Cherwell/templates/default/cherwell/admin/menu.tpl.php index 9cb26651ab..121a318d8c 100644 --- a/Themes/Cherwell/templates/default/cherwell/admin/menu.tpl.php +++ b/Themes/Cherwell/templates/default/cherwell/admin/menu.tpl.php @@ -1 +1 @@ -
  • >language()->_('Set Background'); ?>
  • \ No newline at end of file +
  • request()->getPathInfo() == '/admin/cherwell/') echo 'class="active"'; ?>>language()->_('Set Background'); ?>
  • \ No newline at end of file diff --git a/Themes/Fauvists/templates/default/account/menu.tpl.php b/Themes/Fauvists/templates/default/account/menu.tpl.php index 5e04a33fa2..59f0351701 100644 --- a/Themes/Fauvists/templates/default/account/menu.tpl.php +++ b/Themes/Fauvists/templates/default/account/menu.tpl.php @@ -1,15 +1,15 @@ diff --git a/Themes/Fauvists/templates/default/account/settings/menu.tpl.php b/Themes/Fauvists/templates/default/account/settings/menu.tpl.php index ef8bf75ef5..57c55067ae 100644 --- a/Themes/Fauvists/templates/default/account/settings/menu.tpl.php +++ b/Themes/Fauvists/templates/default/account/settings/menu.tpl.php @@ -1,15 +1,15 @@