Extension.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * The extension base class.
  5. */
  6. abstract class Minz_Extension {
  7. /** @var string */
  8. private $name;
  9. /** @var string */
  10. private $entrypoint;
  11. /** @var string */
  12. private $path;
  13. /** @var string */
  14. private $author;
  15. /** @var string */
  16. private $description;
  17. /** @var string */
  18. private $version;
  19. /** @var 'system'|'user' */
  20. private $type;
  21. /** @var string */
  22. private $config_key = 'extensions';
  23. /** @var array<string,mixed>|null */
  24. private $user_configuration;
  25. /** @var array<string,mixed>|null */
  26. private $system_configuration;
  27. /** @var array{0:'system',1:'user'} */
  28. public static array $authorized_types = [
  29. 'system',
  30. 'user',
  31. ];
  32. /** @var bool */
  33. private $is_enabled;
  34. /**
  35. * The constructor to assign specific information to the extension.
  36. *
  37. * Available fields are:
  38. * - name: the name of the extension (required).
  39. * - entrypoint: the extension class name (required).
  40. * - path: the pathname to the extension files (required).
  41. * - author: the name and / or email address of the extension author.
  42. * - description: a short description to describe the extension role.
  43. * - version: a version for the current extension.
  44. * - type: "system" or "user" (default).
  45. *
  46. * @param array{'name':string,'entrypoint':string,'path':string,'author'?:string,'description'?:string,'version'?:string,'type'?:'system'|'user'} $meta_info
  47. * contains information about the extension.
  48. */
  49. final public function __construct(array $meta_info) {
  50. $this->name = $meta_info['name'];
  51. $this->entrypoint = $meta_info['entrypoint'];
  52. $this->path = $meta_info['path'];
  53. $this->author = isset($meta_info['author']) ? $meta_info['author'] : '';
  54. $this->description = isset($meta_info['description']) ? $meta_info['description'] : '';
  55. $this->version = isset($meta_info['version']) ? (string)$meta_info['version'] : '0.1';
  56. $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user');
  57. $this->is_enabled = false;
  58. }
  59. /**
  60. * Used when installing an extension (e.g. update the database scheme).
  61. *
  62. * @return string|true true if the extension has been installed or a string explaining the problem.
  63. */
  64. public function install() {
  65. return true;
  66. }
  67. /**
  68. * Used when uninstalling an extension (e.g. revert the database scheme to
  69. * cancel changes from install).
  70. *
  71. * @return string|true true if the extension has been uninstalled or a string explaining the problem.
  72. */
  73. public function uninstall() {
  74. return true;
  75. }
  76. /**
  77. * Call at the initialization of the extension (i.e. when the extension is
  78. * enabled by the extension manager).
  79. * @return void
  80. */
  81. abstract public function init();
  82. /**
  83. * Set the current extension to enable.
  84. */
  85. public final function enable(): void {
  86. $this->is_enabled = true;
  87. }
  88. /**
  89. * Return if the extension is currently enabled.
  90. *
  91. * @return bool true if extension is enabled, false otherwise.
  92. */
  93. public final function isEnabled(): bool {
  94. return $this->is_enabled;
  95. }
  96. /**
  97. * Return the content of the configure view for the current extension.
  98. *
  99. * @return string|false html content from ext_dir/configure.phtml, false if it does not exist.
  100. */
  101. public final function getConfigureView() {
  102. $filename = $this->path . '/configure.phtml';
  103. if (!file_exists($filename)) {
  104. return false;
  105. }
  106. ob_start();
  107. include($filename);
  108. return ob_get_clean();
  109. }
  110. /**
  111. * Handle the configure action.
  112. * @return void
  113. */
  114. public function handleConfigureAction() {}
  115. /**
  116. * Getters and setters.
  117. */
  118. public final function getName(): string {
  119. return $this->name;
  120. }
  121. public final function getEntrypoint(): string {
  122. return $this->entrypoint;
  123. }
  124. public final function getPath(): string {
  125. return $this->path;
  126. }
  127. public final function getAuthor(): string {
  128. return $this->author;
  129. }
  130. public final function getDescription(): string {
  131. return $this->description;
  132. }
  133. public final function getVersion(): string {
  134. return $this->version;
  135. }
  136. /** @return 'system'|'user' */
  137. public final function getType() {
  138. return $this->type;
  139. }
  140. /** @param 'user'|'system' $type */
  141. private function setType(string $type): void {
  142. if (!in_array($type, ['user', 'system'], true)) {
  143. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  144. }
  145. $this->type = $type;
  146. }
  147. /**
  148. * Return the url for a given file.
  149. *
  150. * @param string $filename name of the file to serve.
  151. * @param 'css'|'js' $type the type (js or css) of the file to serve.
  152. * @param bool $isStatic indicates if the file is a static file or a user file. Default is static.
  153. * @return string url corresponding to the file.
  154. */
  155. public final function getFileUrl(string $filename, string $type, bool $isStatic = true): string {
  156. if ($isStatic) {
  157. $dir = basename($this->path);
  158. $file_name_url = urlencode("{$dir}/static/{$filename}");
  159. $mtime = @filemtime("{$this->path}/static/{$filename}");
  160. } else {
  161. $username = Minz_User::name();
  162. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
  163. $file_name_url = urlencode("{$username}/{$this->config_key}/{$this->getName()}/{$filename}");
  164. $mtime = @filemtime($path);
  165. }
  166. return Minz_Url::display("/ext.php?f={$file_name_url}&amp;t={$type}&amp;{$mtime}", 'php');
  167. }
  168. /**
  169. * Register a controller in the Dispatcher.
  170. *
  171. * @param string $base_name the base name of the controller. Final name will be FreshExtension_<base_name>_Controller.
  172. */
  173. public final function registerController(string $base_name): void {
  174. Minz_Dispatcher::registerController($base_name, $this->path);
  175. }
  176. /**
  177. * Register the views in order to be accessible by the application.
  178. */
  179. public final function registerViews(): void {
  180. Minz_View::addBasePathname($this->path);
  181. }
  182. /**
  183. * Register i18n files from ext_dir/i18n/
  184. */
  185. public final function registerTranslates(): void {
  186. $i18n_dir = $this->path . '/i18n';
  187. Minz_Translate::registerPath($i18n_dir);
  188. }
  189. /**
  190. * Register a new hook.
  191. *
  192. * @param string $hook_name the hook name (must exist).
  193. * @param callable $hook_function the function name to call (must be callable).
  194. */
  195. public final function registerHook(string $hook_name, $hook_function): void {
  196. Minz_ExtensionManager::addHook($hook_name, $hook_function);
  197. }
  198. /** @param 'system'|'user' $type */
  199. private function isConfigurationEnabled(string $type): bool {
  200. if (!class_exists('FreshRSS_Context', false)) {
  201. return false;
  202. }
  203. switch ($type) {
  204. case 'system': return FreshRSS_Context::$system_conf !== null;
  205. case 'user': return FreshRSS_Context::$user_conf !== null;
  206. }
  207. }
  208. /** @param 'system'|'user' $type */
  209. private function isExtensionConfigured(string $type): bool {
  210. switch ($type) {
  211. case 'user':
  212. $conf = FreshRSS_Context::$user_conf;
  213. break;
  214. case 'system':
  215. $conf = FreshRSS_Context::$system_conf;
  216. break;
  217. }
  218. if ($conf === null || !$conf->hasParam($this->config_key)) {
  219. return false;
  220. }
  221. $extensions = $conf->{$this->config_key};
  222. return array_key_exists($this->getName(), $extensions);
  223. }
  224. /**
  225. * @phpstan-param 'system'|'user' $type
  226. * @return array<string,mixed>
  227. */
  228. private function getConfiguration(string $type): array {
  229. if (!$this->isConfigurationEnabled($type)) {
  230. return [];
  231. }
  232. if (!$this->isExtensionConfigured($type)) {
  233. return [];
  234. }
  235. $conf = "{$type}_conf";
  236. return FreshRSS_Context::$$conf->{$this->config_key}[$this->getName()];
  237. }
  238. /**
  239. * @return array<string,mixed>
  240. */
  241. public final function getSystemConfiguration(): array {
  242. return $this->getConfiguration('system');
  243. }
  244. /**
  245. * @return array<string,mixed>
  246. */
  247. public final function getUserConfiguration(): array {
  248. return $this->getConfiguration('user');
  249. }
  250. /**
  251. * @param mixed $default
  252. * @return mixed
  253. */
  254. public final function getSystemConfigurationValue(string $key, $default = null) {
  255. if (!is_array($this->system_configuration)) {
  256. $this->system_configuration = $this->getSystemConfiguration();
  257. }
  258. if (array_key_exists($key, $this->system_configuration)) {
  259. return $this->system_configuration[$key];
  260. }
  261. return $default;
  262. }
  263. /**
  264. * @param mixed $default
  265. * @return mixed
  266. */
  267. public final function getUserConfigurationValue(string $key, $default = null) {
  268. if (!is_array($this->user_configuration)) {
  269. $this->user_configuration = $this->getUserConfiguration();
  270. }
  271. if (array_key_exists($key, $this->user_configuration)) {
  272. return $this->user_configuration[$key];
  273. }
  274. return $default;
  275. }
  276. /**
  277. * @param 'system'|'user' $type
  278. * @param array<string,mixed> $configuration
  279. */
  280. private function setConfiguration(string $type, array $configuration): void {
  281. $conf = "{$type}_conf";
  282. if (FreshRSS_Context::$$conf->hasParam($this->config_key)) {
  283. $extensions = FreshRSS_Context::$$conf->{$this->config_key};
  284. } else {
  285. $extensions = [];
  286. }
  287. $extensions[$this->getName()] = $configuration;
  288. FreshRSS_Context::$$conf->{$this->config_key} = $extensions;
  289. FreshRSS_Context::$$conf->save();
  290. }
  291. /** @param array<string,mixed> $configuration */
  292. public final function setSystemConfiguration(array $configuration): void {
  293. $this->setConfiguration('system', $configuration);
  294. $this->system_configuration = $configuration;
  295. }
  296. /** @param array<string,mixed> $configuration */
  297. public final function setUserConfiguration(array $configuration): void {
  298. $this->setConfiguration('user', $configuration);
  299. $this->user_configuration = $configuration;
  300. }
  301. /** @phpstan-param 'system'|'user' $type */
  302. private function removeConfiguration(string $type): void {
  303. if (!$this->isConfigurationEnabled($type)) {
  304. return;
  305. }
  306. if (!$this->isExtensionConfigured($type)) {
  307. return;
  308. }
  309. $conf = "{$type}_conf";
  310. $extensions = FreshRSS_Context::$$conf->{$this->config_key};
  311. unset($extensions[$this->getName()]);
  312. if (empty($extensions)) {
  313. $extensions = null;
  314. }
  315. FreshRSS_Context::$$conf->{$this->config_key} = $extensions;
  316. FreshRSS_Context::$$conf->save();
  317. }
  318. public final function removeSystemConfiguration(): void {
  319. $this->removeConfiguration('system');
  320. $this->system_configuration = null;
  321. }
  322. public final function removeUserConfiguration(): void {
  323. $this->removeConfiguration('user');
  324. $this->user_configuration = null;
  325. }
  326. public final function saveFile(string $filename, string $content): void {
  327. $username = Minz_User::name();
  328. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}";
  329. if (!file_exists($path)) {
  330. mkdir($path, 0777, true);
  331. }
  332. file_put_contents("{$path}/{$filename}", $content);
  333. }
  334. public final function removeFile(string $filename): void {
  335. $username = Minz_User::name();
  336. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
  337. if (file_exists($path)) {
  338. unlink($path);
  339. }
  340. }
  341. }