Extension.php 11 KB

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