4
0

extensionController.php 8.7 KB

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