mytinytodo/src/includes/classes.php

199 lines
5.4 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/*
This file is a part of myTinyTodo.
(C) Copyright 2022 Max Pozdeev <maxpozdeev@gmail.com>
Licensed under the GNU GPL version 2 or any later. See file COPYRIGHT for details.
*/
class ApiRequest
{
public $path;
public $method;
public $contentType;
public $jsonBody;
function __construct() {
$this->path = $_SERVER['PATH_INFO'] ?? '';
$this->method = isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
$this->contentType = $_SERVER['CONTENT_TYPE'] ?? '';
}
function decodeJsonBody() {
$this->jsonBody = json_decode( file_get_contents('php://input'), true, 10, JSON_INVALID_UTF8_SUBSTITUTE );
return $this->jsonBody;
}
}
class ApiResponse
{
public $data = null;
public $contentType = 'application/json';
public $code = null;
function htmlContent(string $content, int $code = 200): ApiResponse
{
$this->contentType = 'text/html';
$this->data = $content;
$this->code = $code;
return $this;
}
function exit()
{
if (is_null($this->data) && is_null($this->code)) {
http_response_code(404);
}
if (!is_null($this->code)) {
http_response_code($this->code);
}
if ($this->contentType == 'text/html') {
print $this->data;
exit();
}
jsonExit($this->data);
}
}
abstract class ApiController
{
/** @var ApiRequest */
protected $req;
/** @var ApiResponse */
protected $response;
function __construct(ApiRequest $req, ApiResponse $response) {
$this->req = $req;
$this->response = $response;
}
}
abstract class MTTExtension
{
2022-09-15 18:39:50 +00:00
const bundleId = '';
2022-09-16 09:08:55 +00:00
const name = '';
abstract function init();
}
interface MTTHttpApiExtender
{
function extendHttpApi(): array;
}
2022-09-08 08:55:05 +00:00
interface MTTExtensionSettingsInterface
{
function settingsPage(): string;
function saveSettings(array $array, ?string &$outMesssage): bool;
2022-09-08 08:55:05 +00:00
}
class MTTExtensionLoader
{
private static $exts = [];
public static function loadExtension(string $ext)
{
if (isset(self::$exts[$ext])) {
error_log("Extension '$ext' is already registered");
return;
}
$loader = MTT_EXT. $ext. '/loader.php';
if (!file_exists($loader)) {
error_log("Failed to init extension '$ext': no loader.php");
return;
}
require_once(MTT_EXT. $ext. '/loader.php');
$getInstance = 'mtt_ext_'. $ext. '_instance';
if (!function_exists($getInstance)) {
throw new Exception("Failed to init extension '$ext': no '$getInstance' function");
}
$instance = $getInstance();
if ( ! ($instance instanceof MTTExtension) ) {
throw new Exception("Failed to init extension '$ext': incompatible instance");
}
$className = get_class($instance);
2022-09-16 09:08:55 +00:00
if (!defined("$className::bundleId") || !defined("$className::name")) {
throw new Exception("Failed to register extension '$ext': require class constants (bundleId, name)");
}
2022-09-15 18:39:50 +00:00
if ($instance::bundleId != $ext) {
throw new Exception("Extension '$ext' bundleId does not conforms to extension dir");
}
2022-09-16 09:08:55 +00:00
Lang::instance()->loadExtensionLang($ext);
$instance->init();
self::$exts[$ext] = $instance;
}
/**
* @return MTTExtension[]
*/
2022-09-08 14:49:58 +00:00
public static function loadedExtensions(): array
{
$a = [];
foreach (self::$exts as $ext => $instance) {
$a[] = $instance;
}
return $a;
}
/**
* @return string[]
*/
public static function bundles(): array
{
2022-09-16 09:08:55 +00:00
$lang = Lang::instance();
$a = [];
$files = array_diff(scandir(MTT_EXT) ?? [], ['.', '..']);
foreach ($files as $ext) {
2022-09-15 18:39:50 +00:00
if ( !is_dir(MTT_EXT. $ext)
|| !file_exists(MTT_EXT. $ext. '/loader.php')
|| !file_exists(MTT_EXT. $ext. '/extension.json') ) {
continue;
}
2022-09-15 18:39:50 +00:00
$jsonData = file_get_contents(MTT_EXT. $ext. '/extension.json');
if ($jsonData === false) {
continue;
}
$meta = json_decode($jsonData, true);
2022-09-16 09:08:55 +00:00
if (!is_array($meta) || !isset($meta['bundleId']) || !isset($meta['name']) || !isset($meta['description'])) {
2022-09-15 18:39:50 +00:00
continue;
}
2022-09-16 09:08:55 +00:00
if (!is_string($meta['bundleId']) || !is_string($meta['name']) || !is_string($meta['description'])) {
2022-09-15 18:39:50 +00:00
continue;
}
2022-09-16 09:08:55 +00:00
if ( $lang->langCode() != 'en' && is_dir(MTT_EXT. $ext. '/lang') ) {
$lf = MTT_EXT. $ext. '/lang/'. $lang->langCode(). '.json';
if (file_exists($lf)) {
$jsonText = file_get_contents($lf) ?? '';
$json = json_decode($jsonText, true) ?? [];
$lt = $json['ext.'.$ext.'.name'] ?? null;
if ($lt !== null) {
$meta['name'] = $lt;
}
}
}
2022-09-15 18:39:50 +00:00
$a[$ext] = $meta;
}
return $a;
}
public static function extensionInstance(string $ext): ?MTTExtension
{
return self::$exts[$ext] ?? null;
}
2022-09-08 14:49:58 +00:00
public static function isLoaded(string $ext): bool
{
return isset(self::$exts[$ext]);
}
}