Extension.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. public static $authorized_types = array(
  16. 'system',
  17. 'user',
  18. );
  19. private $is_enabled;
  20. /**
  21. * The constructor to assign specific information to the extension.
  22. *
  23. * Available fields are:
  24. * - name: the name of the extension (required).
  25. * - entrypoint: the extension class name (required).
  26. * - path: the pathname to the extension files (required).
  27. * - author: the name and / or email address of the extension author.
  28. * - description: a short description to describe the extension role.
  29. * - version: a version for the current extension.
  30. * - type: "system" or "user" (default).
  31. *
  32. * @param $meta_info contains information about the extension.
  33. */
  34. final public function __construct($meta_info) {
  35. $this->name = $meta_info['name'];
  36. $this->entrypoint = $meta_info['entrypoint'];
  37. $this->path = $meta_info['path'];
  38. $this->author = isset($meta_info['author']) ? $meta_info['author'] : '';
  39. $this->description = isset($meta_info['description']) ? $meta_info['description'] : '';
  40. $this->version = isset($meta_info['version']) ? $meta_info['version'] : '0.1';
  41. $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user');
  42. $this->is_enabled = false;
  43. }
  44. /**
  45. * Used when installing an extension (e.g. update the database scheme).
  46. *
  47. * @return true if the extension has been installed or a string explaining
  48. * 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 true if the extension has been uninstalled or a string explaining
  58. * the problem.
  59. */
  60. public function uninstall() {
  61. return true;
  62. }
  63. /**
  64. * Call at the initialization of the extension (i.e. when the extension is
  65. * enabled by the extension manager).
  66. */
  67. abstract public function init();
  68. /**
  69. * Set the current extension to enable.
  70. */
  71. public function enable() {
  72. $this->is_enabled = true;
  73. }
  74. /**
  75. * Return if the extension is currently enabled.
  76. *
  77. * @return true if extension is enabled, false else.
  78. */
  79. public function isEnabled() {
  80. return $this->is_enabled;
  81. }
  82. /**
  83. * Return the content of the configure view for the current extension.
  84. *
  85. * @return the html content from ext_dir/configure.phtml, false if it does
  86. * 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 the 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 isUserConfigurationEnabled() {
  187. if (!class_exists('FreshRSS_Context', false)) {
  188. return false;
  189. }
  190. if (null === FreshRSS_Context::$user_conf) {
  191. return false;
  192. }
  193. return true;
  194. }
  195. /**
  196. * @return bool
  197. */
  198. private function isExtensionConfigured() {
  199. if (!FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
  200. return false;
  201. }
  202. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  203. return array_key_exists($this->getName(), $extensions);
  204. }
  205. /**
  206. * @return array
  207. */
  208. public function getUserConfiguration() {
  209. if (!$this->isUserConfigurationEnabled()) {
  210. return [];
  211. }
  212. if (!$this->isExtensionConfigured()) {
  213. return [];
  214. }
  215. return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()];
  216. }
  217. /**
  218. * @param mixed $default
  219. * @return mixed
  220. */
  221. public function getUserConfigurationValue(string $key, $default = null) {
  222. if (!is_array($this->user_configuration)) {
  223. $this->user_configuration = $this->getUserConfiguration();
  224. }
  225. if (array_key_exists($key, $this->user_configuration)) {
  226. return $this->user_configuration[$key];
  227. }
  228. return $default;
  229. }
  230. public function setUserConfiguration(array $configuration) {
  231. if (!$this->isUserConfigurationEnabled()) {
  232. return;
  233. }
  234. if (FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
  235. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  236. } else {
  237. $extensions = [];
  238. }
  239. $extensions[$this->getName()] = $configuration;
  240. FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
  241. FreshRSS_Context::$user_conf->save();
  242. $this->user_configuration = $configuration;
  243. }
  244. public function removeUserConfiguration() {
  245. if (!$this->isUserConfigurationEnabled()) {
  246. return;
  247. }
  248. if (!$this->isExtensionConfigured()) {
  249. return;
  250. }
  251. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  252. unset($extensions[$this->getName()]);
  253. if (empty($extensions)) {
  254. $extensions = null;
  255. }
  256. FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
  257. FreshRSS_Context::$user_conf->save();
  258. $this->user_configuration = null;
  259. }
  260. public function saveFile(string $filename, string $content) {
  261. $username = Minz_Session::param('currentUser');
  262. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}";
  263. if (!file_exists($path)) {
  264. mkdir($path, 0777, true);
  265. }
  266. file_put_contents("{$path}/{$filename}", $content);
  267. }
  268. public function removeFile(string $filename) {
  269. $username = Minz_Session::param('currentUser');
  270. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
  271. if (file_exists($path)) {
  272. unlink($path);
  273. }
  274. }
  275. }