Extension.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 string html content from ext_dir/configure.phtml, false if it does not exist.
  86. */
  87. public function getConfigureView() {
  88. $filename = $this->path . '/configure.phtml';
  89. if (!file_exists($filename)) {
  90. return false;
  91. }
  92. ob_start();
  93. include($filename);
  94. return ob_get_clean();
  95. }
  96. /**
  97. * Handle the configure action.
  98. */
  99. public function handleConfigureAction() {}
  100. /**
  101. * Getters and setters.
  102. */
  103. public function getName() {
  104. return $this->name;
  105. }
  106. public function getEntrypoint() {
  107. return $this->entrypoint;
  108. }
  109. public function getPath() {
  110. return $this->path;
  111. }
  112. public function getAuthor() {
  113. return $this->author;
  114. }
  115. public function getDescription() {
  116. return $this->description;
  117. }
  118. public function getVersion() {
  119. return $this->version;
  120. }
  121. public function getType() {
  122. return $this->type;
  123. }
  124. private function setType($type) {
  125. if (!in_array($type, self::$authorized_types)) {
  126. throw new Minz_ExtensionException('invalid `type` info', $this->name);
  127. }
  128. $this->type = $type;
  129. }
  130. /**
  131. * Return the url for a given file.
  132. *
  133. * @param $filename name of the file to serve.
  134. * @param $type the type (js or css) of the file to serve.
  135. * @param $isStatic indicates if the file is a static file or a user file. Default is static.
  136. * @return string url corresponding to the file.
  137. */
  138. public function getFileUrl($filename, $type, $isStatic = true) {
  139. if ($isStatic) {
  140. $dir = basename($this->path);
  141. $file_name_url = urlencode("{$dir}/static/{$filename}");
  142. $mtime = @filemtime("{$this->path}/static/{$filename}");
  143. } else {
  144. $username = Minz_Session::param('currentUser');
  145. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
  146. $file_name_url = urlencode("{$username}/{$this->config_key}/{$this->getName()}/{$filename}");
  147. $mtime = @filemtime($path);
  148. }
  149. return Minz_Url::display("/ext.php?f={$file_name_url}&amp;t={$type}&amp;{$mtime}", 'php');
  150. }
  151. /**
  152. * Register a controller in the Dispatcher.
  153. *
  154. * @param @base_name the base name of the controller. Final name will be:
  155. * FreshExtension_<base_name>_Controller.
  156. */
  157. public function registerController($base_name) {
  158. Minz_Dispatcher::registerController($base_name, $this->path);
  159. }
  160. /**
  161. * Register the views in order to be accessible by the application.
  162. */
  163. public function registerViews() {
  164. Minz_View::addBasePathname($this->path);
  165. }
  166. /**
  167. * Register i18n files from ext_dir/i18n/
  168. */
  169. public function registerTranslates() {
  170. $i18n_dir = $this->path . '/i18n';
  171. Minz_Translate::registerPath($i18n_dir);
  172. }
  173. /**
  174. * Register a new hook.
  175. *
  176. * @param $hook_name the hook name (must exist).
  177. * @param $hook_function the function name to call (must be callable).
  178. */
  179. public function registerHook($hook_name, $hook_function) {
  180. Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);
  181. }
  182. /**
  183. * @return bool
  184. */
  185. private function isUserConfigurationEnabled() {
  186. if (!class_exists('FreshRSS_Context', false)) {
  187. return false;
  188. }
  189. if (null === FreshRSS_Context::$user_conf) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. /**
  195. * @return bool
  196. */
  197. private function isExtensionConfigured() {
  198. if (!FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
  199. return false;
  200. }
  201. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  202. return array_key_exists($this->getName(), $extensions);
  203. }
  204. /**
  205. * @return array
  206. */
  207. public function getUserConfiguration() {
  208. if (!$this->isUserConfigurationEnabled()) {
  209. return [];
  210. }
  211. if (!$this->isExtensionConfigured()) {
  212. return [];
  213. }
  214. return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()];
  215. }
  216. /**
  217. * @param mixed $default
  218. * @return mixed
  219. */
  220. public function getUserConfigurationValue(string $key, $default = null) {
  221. if (!is_array($this->user_configuration)) {
  222. $this->user_configuration = $this->getUserConfiguration();
  223. }
  224. if (array_key_exists($key, $this->user_configuration)) {
  225. return $this->user_configuration[$key];
  226. }
  227. return $default;
  228. }
  229. public function setUserConfiguration(array $configuration) {
  230. if (!$this->isUserConfigurationEnabled()) {
  231. return;
  232. }
  233. if (FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
  234. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  235. } else {
  236. $extensions = [];
  237. }
  238. $extensions[$this->getName()] = $configuration;
  239. FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
  240. FreshRSS_Context::$user_conf->save();
  241. $this->user_configuration = $configuration;
  242. }
  243. public function removeUserConfiguration() {
  244. if (!$this->isUserConfigurationEnabled()) {
  245. return;
  246. }
  247. if (!$this->isExtensionConfigured()) {
  248. return;
  249. }
  250. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  251. unset($extensions[$this->getName()]);
  252. if (empty($extensions)) {
  253. $extensions = null;
  254. }
  255. FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
  256. FreshRSS_Context::$user_conf->save();
  257. $this->user_configuration = null;
  258. }
  259. public function saveFile(string $filename, string $content) {
  260. $username = Minz_Session::param('currentUser');
  261. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}";
  262. if (!file_exists($path)) {
  263. mkdir($path, 0777, true);
  264. }
  265. file_put_contents("{$path}/{$filename}", $content);
  266. }
  267. public function removeFile(string $filename) {
  268. $username = Minz_Session::param('currentUser');
  269. $path = USERS_PATH . "/{$username}/{$this->config_key}/{$this->getName()}/{$filename}";
  270. if (file_exists($path)) {
  271. unlink($path);
  272. }
  273. }
  274. }