diff --git a/src/api.php b/src/api.php index 3bfac3a..25ae40b 100644 --- a/src/api.php +++ b/src/api.php @@ -60,6 +60,20 @@ $endpoints = array( ] ); +// look for extensions +foreach (MTTExtensionLoader::registeredExtensions() as $instance) { + if ($instance instanceof MTTHttpApiExtender) { + $newRoutes = $instance->extendHttpApi(); + foreach ($newRoutes as $endpoint => $methods) { + $endpoint = '/ext/'. $instance::codename. $endpoint; + foreach ($methods as $k => &$v) { + $v[2] = true; // Mark extension method + } + $endpoints[$endpoint] = $methods; + } + } +} + $req = new ApiRequest(); $response = new ApiResponse(); $executed = false; @@ -73,12 +87,16 @@ foreach ($endpoints as $search => $methods) { if ( is_null($classDescr) ) { $response->htmlContent("Unknown method for resource", 500)->exit(); } - if ( !is_array($classDescr) || count($classDescr) != 2) { + if ( !is_array($classDescr) || count($classDescr) < 2) { $response->htmlContent("Incorrect method definition", 500)->exit(); } // check if class method exists $class = $classDescr[0]; $classMethod = $classDescr[1]; + $isExt = $classDescr[2] ?? false; + if ($isExt) { + checkWriteAccess(); + } $param = null; if (count($m) >= 2) { $param = $m[1]; diff --git a/src/content/mytinytodo.js b/src/content/mytinytodo.js index 914a989..192f979 100644 --- a/src/content/mytinytodo.js +++ b/src/content/mytinytodo.js @@ -626,6 +626,11 @@ var mytinytodo = window.mytinytodo = _mtt = { } }); + $("#page_ajax").on('click', 'a[data-ext-settings-action]', function() { + extensionSettingsAction(this.dataset.extSettingsAction, this.dataset.ext); + return false; + }); + // tab menu this.addAction('listSelected', tabmenuOnListSelected); @@ -2684,7 +2689,30 @@ function saveExtensionSettings(frm) flashInfo(json.msg); }); else showExtensionSettings(ext); + } + } + }); +} +function extensionSettingsAction(actionString, ext) +{ + if (actionString === undefined || ext === undefined) return false; + var 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) { + showExtensionSettings(ext, json.msg ? function(){flashInfo(json.msg)} : null); + } + else if (json.msg) { + flashInfo(json.msg); } } }); diff --git a/src/includes/api/ExtSettingsController.php b/src/includes/api/ExtSettingsController.php index 64b6d7d..831a1b4 100644 --- a/src/includes/api/ExtSettingsController.php +++ b/src/includes/api/ExtSettingsController.php @@ -9,6 +9,8 @@ class ExtSettingsController extends ApiController { */ function get($ext) { + checkWriteAccess(); + /** @var MTTExtension|MTTExtensionSettingsInterface $instance */ $instance = $this->extInstance($ext); if (!$instance) { @@ -45,6 +47,8 @@ EOD; */ function put($ext) { + checkWriteAccess(); + /** @var MTTExtension|MTTExtensionSettingsInterface $instance */ $instance = $this->extInstance($ext); if (!$instance) { diff --git a/src/includes/classes.php b/src/includes/classes.php index 6b838a3..4c32b0e 100644 --- a/src/includes/classes.php +++ b/src/includes/classes.php @@ -78,6 +78,10 @@ abstract class MTTExtension abstract function init(); } +interface MTTHttpApiExtender +{ + function extendHttpApi(): array; +} interface MTTExtensionSettingsInterface {