Extension.php 11 KB

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