Extension.php 9.1 KB

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