small refactoring

This commit is contained in:
maxpozdeev 2023-08-28 14:06:26 +03:00
parent 675d523d0d
commit 696c795a4a
4 changed files with 76 additions and 38 deletions

View file

@ -2,7 +2,7 @@
/*
This file is a part of myTinyTodo.
(C) Copyright 2022 Max Pozdeev <maxpozdeev@gmail.com>
(C) Copyright 2022-2023 Max Pozdeev <maxpozdeev@gmail.com>
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();

View file

@ -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

View file

@ -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 = "<form id='ext_settings_form' data-ext='$escapedExt'>";
$formEnd = "</form>";
$formButtons =
<<<EOD
<div class="tr form-bottom-buttons">
@ -48,12 +52,12 @@ EOD;
<<<EOD
<h3 class="page-title"><a class="mtt-back-button"></a> $name </h3>
<div id="settings_msg" style="display:none"></div>
<form id="ext_settings_form" data-ext="$escapedExt">
$formStart
<div class="mtt-settings-table">
$data
$formButtons
</div>
</form>
$formEnd
EOD;
$this->response->htmlContent($data);

View file

@ -1,7 +1,7 @@
<?php
/*
This file is a part of myTinyTodo.
(C) Copyright 2009-2011,2019-2022 Max Pozdeev <maxpozdeev@gmail.com>
(C) Copyright 2009-2011,2019-2023 Max Pozdeev <maxpozdeev@gmail.com>
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();