From 696c795a4a60f76fc85d9639cc4200d9790d9797 Mon Sep 17 00:00:00 2001 From: maxpozdeev Date: Mon, 28 Aug 2023 14:06:26 +0300 Subject: [PATCH] small refactoring --- src/api.php | 30 ++++++---- src/content/mytinytodo.js | 68 ++++++++++++++-------- src/includes/api/ExtSettingsController.php | 8 ++- src/init.php | 8 ++- 4 files changed, 76 insertions(+), 38 deletions(-) diff --git a/src/api.php b/src/api.php index b3c91ae..2d6c717 100644 --- a/src/api.php +++ b/src/api.php @@ -2,7 +2,7 @@ /* This file is a part of myTinyTodo. - (C) Copyright 2022 Max Pozdeev + (C) Copyright 2022-2023 Max Pozdeev Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ @@ -85,17 +85,20 @@ foreach ($endpoints as $search => $methods) { $classDescr = $methods[$req->method] ?? null; // check if http method is supported for path if ( is_null($classDescr) ) { - $response->htmlContent("Unknown method for resource", 500)->exit(); + $response->htmlContent("Unknown method for resource", 500) + ->exit(); } if ( !is_array($classDescr) || count($classDescr) < 2) { - $response->htmlContent("Incorrect method definition", 500)->exit(); + $response->htmlContent("Incorrect method definition", 500) + ->exit(); } // check if class method exists $class = $classDescr[0]; $classMethod = $classDescr[1]; - $isExt = $classDescr[3] ?? false; - if ($isExt) { - if (false == ($classDescr[2] ?? false)) { + $isExtMethod = $classDescr[3] ?? false; + if ($isExtMethod) { + if (false == ($classDescr[2] ?? false)) { //TODO: describe $classDescr[2] + // By default all extension methods require write access rights checkWriteAccess(); } } @@ -106,7 +109,8 @@ foreach ($endpoints as $search => $methods) { if (method_exists($class, $classMethod)) { // test for static with ReflectionMethod? if ($req->method != 'GET' && $req->contentType == 'application/json') { if ($req->decodeJsonBody() === false) { - $response->htmlContent("Failed to parse JSON body", 500)->exit(); + $response->htmlContent("Failed to parse JSON body", 500) + ->exit(); } } $instance = new $class($req, $response); @@ -116,15 +120,21 @@ foreach ($endpoints as $search => $methods) { } else { if (MTT_DEBUG) { - $response->htmlContent("Class method $class:$classMethod() not found", 405)->exit(); + $response->htmlContent("Class method $class:$classMethod() not found", 405) + ->exit(); } - $response->htmlContent("Class method not found", 405)->exit(); + $response->htmlContent("Class method not found", 405) + ->exit(); } } } if (!$executed) { - $response->htmlContent("Unknown command", 404); + if (MTT_DEBUG) { + $response->htmlContent("Unknown endpoint: {$req->method} {$req->path}", 404) + ->exit(); + } + $response->htmlContent("Unknown endpoint", 404); } $response->exit(); diff --git a/src/content/mytinytodo.js b/src/content/mytinytodo.js index 4886e7c..606fa2b 100644 --- a/src/content/mytinytodo.js +++ b/src/content/mytinytodo.js @@ -2863,38 +2863,56 @@ function saveExtensionSettings(frm) }); } -function extensionSettingsAction(actionString, ext) +function extensionSettingsAction(actionString, ext, formData) { if (actionString === undefined || ext === undefined) return false; - var a = actionString.split(':', 2); + const a = actionString.split(':', 2); if (a.length !== 2) return false; - var method = a[0], - action = a[1]; - $.ajax({ - url: _mtt.apiUrl + 'ext/' + ext + '/' + action, - method: method.toUpperCase(), - contentType : 'application/json', - data: '{}', - dataType: 'json', - success: function(json) { - if (json.total && json.total > 0) { - const callback = function() { - if (json.msg) flashInfo(json.msg, json.details); - if (json.reload) { - setTimeout( function(){ - //window.location.hash = ''; - window.location.reload(); - }, 1000); - } + const method = a[0], + action = a[1]; + const success = function(json) { + if (json.total && json.total > 0) { + if (json.redirect) { + window.location.assign(json.redirect); + return; + } + const callback = function() { + if (json.msg) flashInfo(json.msg, json.details); + if (json.reload) { + setTimeout( function(){ + //window.location.hash = ''; + window.location.reload(); + }, 1000); } - showExtensionSettings(ext, callback); - } - else if (json.msg) { - flashInfo(json.msg, json.details); } + showExtensionSettings(ext, callback); } - }); + else if (json.msg) { + flashInfo(json.msg, json.details); + } + }; + if (formData === undefined) { + $.ajax({ + url: _mtt.apiUrl + 'ext/' + ext + '/' + action, + method: method.toUpperCase(), + contentType : 'application/json', + data: '{}', + dataType: 'json', + success: success + }); + } + else { + $.ajax({ + url: _mtt.apiUrl + 'ext/' + ext + '/' + action, + method: method.toUpperCase(), + contentType : false, + data: formData, + processData: false, + success: success + }); + } } +_mtt.extensionSettingsAction = extensionSettingsAction; /* * Dialogs diff --git a/src/includes/api/ExtSettingsController.php b/src/includes/api/ExtSettingsController.php index 7c527be..f218f23 100644 --- a/src/includes/api/ExtSettingsController.php +++ b/src/includes/api/ExtSettingsController.php @@ -34,8 +34,12 @@ class ExtSettingsController extends ApiController { $escapedExt = htmlspecialchars($ext); $e = function($s) use($lang) { return htmlspecialchars($lang->get($s)); }; + $formStart = ''; + $formEnd = ''; $formButtons = ''; if ($instance->settingsPageType() == 0) { + $formStart = "
"; + $formEnd = "
"; $formButtons = << @@ -48,12 +52,12 @@ EOD; << $name -
+$formStart
$data $formButtons
-
+$formEnd EOD; $this->response->htmlContent($data); diff --git a/src/init.php b/src/init.php index 9e01343..22ae13e 100644 --- a/src/init.php +++ b/src/init.php @@ -1,7 +1,7 @@ + (C) Copyright 2009-2011,2019-2023 Max Pozdeev Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details. */ @@ -211,6 +211,12 @@ function access_token(): string } } +/** + * Check if HTTP request have required MTT-Token header with value + * the same as stored in session (if password set) or mtt-token cookie (if no password). + * Prohibits further execution if no tokens are found. + * @return void + */ function check_token() { $token = access_token();