extensionController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * The controller to manage extensions.
  4. */
  5. class FreshRSS_extension_Controller extends FreshRSS_ActionController {
  6. /**
  7. * This action is called before every other action in that class. It is
  8. * the common boiler plate for every action. It is triggered by the
  9. * underlying framework.
  10. */
  11. public function firstAction(): void {
  12. if (!FreshRSS_Auth::hasAccess()) {
  13. Minz_Error::error(403);
  14. }
  15. }
  16. /**
  17. * This action lists all the extensions available to the current user.
  18. */
  19. public function indexAction(): void {
  20. FreshRSS_View::prependTitle(_t('admin.extensions.title') . ' · ');
  21. $this->view->extension_list = array(
  22. 'system' => array(),
  23. 'user' => array(),
  24. );
  25. $this->view->extensions_installed = array();
  26. $extensions = Minz_ExtensionManager::listExtensions();
  27. foreach ($extensions as $ext) {
  28. $this->view->extension_list[$ext->getType()][] = $ext;
  29. $this->view->extensions_installed[$ext->getEntrypoint()] = $ext->getVersion();
  30. }
  31. $this->view->available_extensions = $this->getAvailableExtensionList();
  32. }
  33. /**
  34. * fetch extension list from GitHub
  35. * @return array<string,array{'name':string,'author':string,'description':string,'version':string,'entrypoint':string,'type':'system'|'user','url':string,'method':string,'directory':string}>
  36. */
  37. protected function getAvailableExtensionList(): array {
  38. $extensionListUrl = 'https://raw.githubusercontent.com/FreshRSS/Extensions/master/extensions.json';
  39. $json = @file_get_contents($extensionListUrl);
  40. // we ran into problems, simply ignore them
  41. if ($json === false) {
  42. Minz_Log::error('Could not fetch available extension from GitHub');
  43. return array();
  44. }
  45. // fetch the list as an array
  46. /** @var array<string,mixed> */
  47. $list = json_decode($json, true);
  48. if (empty($list)) {
  49. Minz_Log::warning('Failed to convert extension file list');
  50. return array();
  51. }
  52. // By now, all the needed data is kept in the main extension file.
  53. // In the future we could fetch detail information from the extensions metadata.json, but I tend to stick with
  54. // the current implementation for now, unless it becomes too much effort maintain the extension list manually
  55. /** @var array<string,array{'name':string,'author':string,'description':string,'version':string,'entrypoint':string,'type':'system'|'user','url':string,'method':string,'directory':string}> */
  56. $extensions = $list['extensions'];
  57. return $extensions;
  58. }
  59. /**
  60. * This action handles configuration of a given extension.
  61. *
  62. * Only administrator can configure a system extension.
  63. *
  64. * Parameters are:
  65. * - e: the extension name (urlencoded)
  66. * - additional parameters which should be handle by the extension
  67. * handleConfigureAction() method (POST request).
  68. */
  69. public function configureAction(): void {
  70. if (Minz_Request::paramBoolean('ajax')) {
  71. $this->view->_layout(null);
  72. } elseif (Minz_Request::paramBoolean('slider')) {
  73. $this->indexAction();
  74. $this->view->_path('extension/index.phtml');
  75. }
  76. $ext_name = urldecode(Minz_Request::paramString('e'));
  77. $ext = Minz_ExtensionManager::findExtension($ext_name);
  78. if ($ext === null) {
  79. Minz_Error::error(404);
  80. return;
  81. }
  82. if ($ext->getType() === 'system' && !FreshRSS_Auth::hasAccess('admin')) {
  83. Minz_Error::error(403);
  84. return;
  85. }
  86. FreshRSS_View::prependTitle($ext->getName() . ' · ' . _t('admin.extensions.title') . ' · ');
  87. $this->view->extension = $ext;
  88. $this->view->extension->handleConfigureAction();
  89. }
  90. /**
  91. * This action enables a disabled extension for the current user.
  92. *
  93. * System extensions can only be enabled by an administrator.
  94. * This action must be reached by a POST request.
  95. *
  96. * Parameter is:
  97. * - e: the extension name (urlencoded).
  98. */
  99. public function enableAction(): void {
  100. $url_redirect = array('c' => 'extension', 'a' => 'index');
  101. if (Minz_Request::isPost()) {
  102. $ext_name = urldecode(Minz_Request::paramString('e'));
  103. $ext = Minz_ExtensionManager::findExtension($ext_name);
  104. if (is_null($ext)) {
  105. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  106. return;
  107. }
  108. if ($ext->isEnabled()) {
  109. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name), $url_redirect);
  110. }
  111. $type = $ext->getType();
  112. if ($type !== 'user' && !FreshRSS_Auth::hasAccess('admin')) {
  113. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  114. return;
  115. }
  116. $conf = null;
  117. if ($type === 'system') {
  118. $conf = FreshRSS_Context::$system_conf;
  119. } elseif ($type === 'user') {
  120. $conf = FreshRSS_Context::$user_conf;
  121. }
  122. $res = $ext->install();
  123. if ($conf !== null && $res === true) {
  124. $ext_list = $conf->extensions_enabled;
  125. $ext_list = array_filter($ext_list, static function(string $key) use($type) {
  126. // Remove from list the extensions that have disappeared or changed type
  127. $extension = Minz_ExtensionManager::findExtension($key);
  128. return $extension !== null && $extension->getType() === $type;
  129. }, ARRAY_FILTER_USE_KEY);
  130. $ext_list[$ext_name] = true;
  131. $conf->extensions_enabled = $ext_list;
  132. $conf->save();
  133. Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name), $url_redirect);
  134. } else {
  135. Minz_Log::warning('Cannot enable extension ' . $ext_name . ': ' . $res);
  136. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  137. }
  138. }
  139. Minz_Request::forward($url_redirect, true);
  140. }
  141. /**
  142. * This action disables an enabled extension for the current user.
  143. *
  144. * System extensions can only be disabled by an administrator.
  145. * This action must be reached by a POST request.
  146. *
  147. * Parameter is:
  148. * - e: the extension name (urlencoded).
  149. */
  150. public function disableAction(): void {
  151. $url_redirect = array('c' => 'extension', 'a' => 'index');
  152. if (Minz_Request::isPost()) {
  153. $ext_name = urldecode(Minz_Request::paramString('e'));
  154. $ext = Minz_ExtensionManager::findExtension($ext_name);
  155. if (is_null($ext)) {
  156. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  157. return;
  158. }
  159. if (!$ext->isEnabled()) {
  160. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name), $url_redirect);
  161. }
  162. $type = $ext->getType();
  163. if ($type !== 'user' && !FreshRSS_Auth::hasAccess('admin')) {
  164. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  165. return;
  166. }
  167. $conf = null;
  168. if ($type === 'system') {
  169. $conf = FreshRSS_Context::$system_conf;
  170. } elseif ($type === 'user') {
  171. $conf = FreshRSS_Context::$user_conf;
  172. }
  173. $res = $ext->uninstall();
  174. if ($conf !== null && $res === true) {
  175. $ext_list = $conf->extensions_enabled;
  176. $ext_list = array_filter($ext_list, static function(string $key) use($type) {
  177. // Remove from list the extensions that have disappeared or changed type
  178. $extension = Minz_ExtensionManager::findExtension($key);
  179. return $extension !== null && $extension->getType() === $type;
  180. }, ARRAY_FILTER_USE_KEY);
  181. $ext_list[$ext_name] = false;
  182. $conf->extensions_enabled = $ext_list;
  183. $conf->save();
  184. Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name), $url_redirect);
  185. } else {
  186. Minz_Log::warning('Cannot disable extension ' . $ext_name . ': ' . $res);
  187. Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  188. }
  189. }
  190. Minz_Request::forward($url_redirect, true);
  191. }
  192. /**
  193. * This action handles deletion of an extension.
  194. *
  195. * Only administrator can remove an extension.
  196. * This action must be reached by a POST request.
  197. *
  198. * Parameter is:
  199. * -e: extension name (urlencoded)
  200. */
  201. public function removeAction(): void {
  202. if (!FreshRSS_Auth::hasAccess('admin')) {
  203. Minz_Error::error(403);
  204. }
  205. $url_redirect = array('c' => 'extension', 'a' => 'index');
  206. if (Minz_Request::isPost()) {
  207. $ext_name = urldecode(Minz_Request::paramString('e'));
  208. $ext = Minz_ExtensionManager::findExtension($ext_name);
  209. if (is_null($ext)) {
  210. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  211. return;
  212. }
  213. $res = recursive_unlink($ext->getPath());
  214. if ($res) {
  215. Minz_Request::good(_t('feedback.extensions.removed', $ext_name), $url_redirect);
  216. } else {
  217. Minz_Request::bad(_t('feedback.extensions.cannot_remove', $ext_name), $url_redirect);
  218. }
  219. }
  220. Minz_Request::forward($url_redirect, true);
  221. }
  222. }