Extension.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. * @return the url corresponding to the file.
  137. */
  138. public function getFileUrl($filename, $type) {
  139. $dir = substr(strrchr($this->path, '/'), 1);
  140. $file_name_url = urlencode($dir . '/static/' . $filename);
  141. $absolute_path = $this->path . '/static/' . $filename;
  142. $mtime = @filemtime($absolute_path);
  143. $url = '/ext.php?f=' . $file_name_url .
  144. '&amp;t=' . $type .
  145. '&amp;' . $mtime;
  146. return Minz_Url::display($url, 'php');
  147. }
  148. /**
  149. * Register a controller in the Dispatcher.
  150. *
  151. * @param @base_name the base name of the controller. Final name will be:
  152. * FreshExtension_<base_name>_Controller.
  153. */
  154. public function registerController($base_name) {
  155. Minz_Dispatcher::registerController($base_name, $this->path);
  156. }
  157. /**
  158. * Register the views in order to be accessible by the application.
  159. */
  160. public function registerViews() {
  161. Minz_View::addBasePathname($this->path);
  162. }
  163. /**
  164. * Register i18n files from ext_dir/i18n/
  165. */
  166. public function registerTranslates() {
  167. $i18n_dir = $this->path . '/i18n';
  168. Minz_Translate::registerPath($i18n_dir);
  169. }
  170. /**
  171. * Register a new hook.
  172. *
  173. * @param $hook_name the hook name (must exist).
  174. * @param $hook_function the function name to call (must be callable).
  175. */
  176. public function registerHook($hook_name, $hook_function) {
  177. Minz_ExtensionManager::addHook($hook_name, $hook_function, $this);
  178. }
  179. /**
  180. * @return bool
  181. */
  182. private function isUserConfigurationEnabled() {
  183. if (!class_exists('FreshRSS_Context', false)) {
  184. return false;
  185. }
  186. if (null === FreshRSS_Context::$user_conf) {
  187. return false;
  188. }
  189. return true;
  190. }
  191. /**
  192. * @return bool
  193. */
  194. private function isExtensionConfigured() {
  195. if (!FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
  196. return false;
  197. }
  198. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  199. return array_key_exists($this->getName(), $extensions);
  200. }
  201. /**
  202. * @return array
  203. */
  204. public function getUserConfiguration() {
  205. if (!$this->isUserConfigurationEnabled()) {
  206. return [];
  207. }
  208. if (!$this->isExtensionConfigured()) {
  209. return [];
  210. }
  211. return FreshRSS_Context::$user_conf->{$this->config_key}[$this->getName()];
  212. }
  213. /**
  214. * @param mixed $default
  215. * @return mixed
  216. */
  217. public function getUserConfigurationValue(string $key, $default = null) {
  218. if (!is_array($this->user_configuration)) {
  219. $this->user_configuration = $this->getUserConfiguration();
  220. }
  221. if (array_key_exists($key, $this->user_configuration)) {
  222. return $this->user_configuration[$key];
  223. }
  224. return $default;
  225. }
  226. public function setUserConfiguration(array $configuration) {
  227. if (!$this->isUserConfigurationEnabled()) {
  228. return;
  229. }
  230. if (FreshRSS_Context::$user_conf->hasParam($this->config_key)) {
  231. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  232. } else {
  233. $extensions = [];
  234. }
  235. $extensions[$this->getName()] = $configuration;
  236. FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
  237. FreshRSS_Context::$user_conf->save();
  238. $this->user_configuration = $configuration;
  239. }
  240. public function removeUserConfiguration(){
  241. if (!$this->isUserConfigurationEnabled()) {
  242. return;
  243. }
  244. if (!$this->isExtensionConfigured()) {
  245. return;
  246. }
  247. $extensions = FreshRSS_Context::$user_conf->{$this->config_key};
  248. unset($extensions[$this->getName()]);
  249. if (empty($extensions)) {
  250. $extensions = null;
  251. }
  252. FreshRSS_Context::$user_conf->{$this->config_key} = $extensions;
  253. FreshRSS_Context::$user_conf->save();
  254. $this->user_configuration = null;
  255. }
  256. }