extensionController.php 9.1 KB

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