Extension.php 11 KB

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