Extension.php 9.2 KB

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