4
0

Extension.php 10 KB

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