From 02ac0655b0b511f1217a0008aa50d8e19ae0623c Mon Sep 17 00:00:00 2001 From: maxpozdeev Date: Sun, 13 Nov 2022 00:45:24 +0300 Subject: [PATCH] little changes in http api internals --- src/api.php | 8 +++++--- src/includes/classes.php | 17 ++++++++++++++--- src/init.php | 2 +- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/api.php b/src/api.php index c8177bd..b3c91ae 100644 --- a/src/api.php +++ b/src/api.php @@ -67,7 +67,7 @@ foreach (MTTExtensionLoader::loadedExtensions() as $instance) { foreach ($newRoutes as $endpoint => $methods) { $endpoint = '/ext/'. $instance::bundleId. $endpoint; foreach ($methods as $k => &$v) { - $v[2] = true; // Mark extension method + $v[3] = true; // Mark extension method } $endpoints[$endpoint] = $methods; } @@ -93,9 +93,11 @@ foreach ($endpoints as $search => $methods) { // check if class method exists $class = $classDescr[0]; $classMethod = $classDescr[1]; - $isExt = $classDescr[2] ?? false; + $isExt = $classDescr[3] ?? false; if ($isExt) { - checkWriteAccess(); + if (false == ($classDescr[2] ?? false)) { + checkWriteAccess(); + } } $param = null; if (count($m) >= 2) { diff --git a/src/includes/classes.php b/src/includes/classes.php index 7cc23e7..db91f5e 100644 --- a/src/includes/classes.php +++ b/src/includes/classes.php @@ -46,14 +46,24 @@ class ApiResponse public $contentType = 'application/json'; public $code = null; - function htmlContent(string $content, int $code = 200): ApiResponse + function content(string $contentType, string $content, int $code = 200) { - $this->contentType = 'text/html'; + $this->contentType = $contentType; $this->data = $content; $this->code = $code; return $this; } + function htmlContent(string $content, int $code = 200): ApiResponse + { + return $this->content('text/html', $content, $code); + } + + function cssContent(string $content, int $code = 200): ApiResponse + { + return $this->content('text/css', $content, $code); + } + function exit() { if (is_null($this->data) && is_null($this->code)) { @@ -62,7 +72,8 @@ class ApiResponse if (!is_null($this->code)) { http_response_code($this->code); } - if ($this->contentType == 'text/html') { + if ($this->contentType != 'application/json') { + header('Content-type: '. $this->contentType); print $this->data; exit(); } diff --git a/src/init.php b/src/init.php index de3a069..f344625 100644 --- a/src/init.php +++ b/src/init.php @@ -341,7 +341,7 @@ function get_unsafe_mttinfo($v) /* URL for API, like http://localhost/mytinytodo/api/. No need to set by default. */ $_mttinfo['api_url'] = Config::getUrl('api_url'); // need to have a trailing slash if ($_mttinfo['api_url'] == '') { - $_mttinfo['api_url'] = get_unsafe_mttinfo('mtt_url'). 'api/'; + $_mttinfo['api_url'] = get_unsafe_mttinfo('mtt_url'). 'api.php/'; } return $_mttinfo['api_url']; case 'title':