extensionController.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 list<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 (!is_array($extension)) {
  70. continue;
  71. }
  72. if (isset($extension['version']) && is_numeric($extension['version'])) {
  73. $extension['version'] = (string)$extension['version'];
  74. }
  75. $keys = ['author', 'description', 'directory', 'entrypoint', 'method', 'name', 'type', 'url', 'version'];
  76. $extension = array_intersect_key($extension, array_flip($keys)); // Keep only valid keys
  77. $extension = array_filter($extension, 'is_string');
  78. foreach ($keys as $key) {
  79. if (empty($extension[$key])) {
  80. continue 2;
  81. }
  82. }
  83. if (!in_array($extension['type'], ['system', 'user'], true) || trim($extension['name']) === '') {
  84. continue;
  85. }
  86. /** @var array{name:string,author:string,description:string,version:string,entrypoint:string,type:'system'|'user',url:string,method:string,directory:string} $extension */
  87. $extensions[] = $extension;
  88. }
  89. return $extensions;
  90. }
  91. /**
  92. * This action handles configuration of a given extension.
  93. *
  94. * Only administrator can configure a system extension.
  95. *
  96. * Parameters are:
  97. * - e: the extension name (urlencoded)
  98. * - additional parameters which should be handle by the extension
  99. * handleConfigureAction() method (POST request).
  100. */
  101. public function configureAction(): void {
  102. if (Minz_Request::paramBoolean('ajax')) {
  103. $this->view->_layout(null);
  104. } elseif (Minz_Request::paramBoolean('slider')) {
  105. $this->indexAction();
  106. $this->view->_path('extension/index.phtml');
  107. }
  108. $ext_name = urldecode(Minz_Request::paramString('e'));
  109. $ext = Minz_ExtensionManager::findExtension($ext_name);
  110. if ($ext === null) {
  111. Minz_Error::error(404);
  112. return;
  113. }
  114. if ($ext->getType() === 'system' && !FreshRSS_Auth::hasAccess('admin')) {
  115. Minz_Error::error(403);
  116. return;
  117. }
  118. FreshRSS_View::prependTitle($ext->getName() . ' · ' . _t('admin.extensions.title') . ' · ');
  119. $this->view->extension = $ext;
  120. $this->view->extension->handleConfigureAction();
  121. }
  122. /**
  123. * This action enables a disabled extension for the current user.
  124. *
  125. * System extensions can only be enabled by an administrator.
  126. * This action must be reached by a POST request.
  127. *
  128. * Parameter is:
  129. * - e: the extension name (urlencoded).
  130. */
  131. public function enableAction(): void {
  132. $url_redirect = ['c' => 'extension', 'a' => 'index'];
  133. if (Minz_Request::isPost()) {
  134. $ext_name = urldecode(Minz_Request::paramString('e'));
  135. $ext = Minz_ExtensionManager::findExtension($ext_name);
  136. if (is_null($ext)) {
  137. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  138. return;
  139. }
  140. if ($ext->isEnabled()) {
  141. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name), $url_redirect);
  142. }
  143. $type = $ext->getType();
  144. if ($type !== 'user' && !FreshRSS_Auth::hasAccess('admin')) {
  145. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  146. return;
  147. }
  148. $conf = null;
  149. if ($type === 'system') {
  150. $conf = FreshRSS_Context::systemConf();
  151. } elseif ($type === 'user') {
  152. $conf = FreshRSS_Context::userConf();
  153. }
  154. $res = $ext->install();
  155. if ($conf !== null && $res === true) {
  156. $ext_list = $conf->extensions_enabled;
  157. $ext_list = array_filter($ext_list, static function (string $key) use ($type) {
  158. // Remove from list the extensions that have disappeared or changed type
  159. $extension = Minz_ExtensionManager::findExtension($key);
  160. return $extension !== null && $extension->getType() === $type;
  161. }, ARRAY_FILTER_USE_KEY);
  162. $ext_list[$ext_name] = true;
  163. $conf->extensions_enabled = $ext_list;
  164. $conf->save();
  165. Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name), $url_redirect);
  166. } else {
  167. Minz_Log::warning('Cannot enable extension ' . $ext_name . ': ' . $res);
  168. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  169. }
  170. }
  171. Minz_Request::forward($url_redirect, true);
  172. }
  173. /**
  174. * This action disables an enabled extension for the current user.
  175. *
  176. * System extensions can only be disabled by an administrator.
  177. * This action must be reached by a POST request.
  178. *
  179. * Parameter is:
  180. * - e: the extension name (urlencoded).
  181. */
  182. public function disableAction(): void {
  183. $url_redirect = ['c' => 'extension', 'a' => 'index'];
  184. if (Minz_Request::isPost()) {
  185. $ext_name = urldecode(Minz_Request::paramString('e'));
  186. $ext = Minz_ExtensionManager::findExtension($ext_name);
  187. if (is_null($ext)) {
  188. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  189. return;
  190. }
  191. if (!$ext->isEnabled()) {
  192. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name), $url_redirect);
  193. }
  194. $type = $ext->getType();
  195. if ($type !== 'user' && !FreshRSS_Auth::hasAccess('admin')) {
  196. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  197. return;
  198. }
  199. $conf = null;
  200. if ($type === 'system') {
  201. $conf = FreshRSS_Context::systemConf();
  202. } elseif ($type === 'user') {
  203. $conf = FreshRSS_Context::userConf();
  204. }
  205. $res = $ext->uninstall();
  206. if ($conf !== null && $res === true) {
  207. $ext_list = $conf->extensions_enabled;
  208. $ext_list = array_filter($ext_list, static function (string $key) use ($type) {
  209. // Remove from list the extensions that have disappeared or changed type
  210. $extension = Minz_ExtensionManager::findExtension($key);
  211. return $extension !== null && $extension->getType() === $type;
  212. }, ARRAY_FILTER_USE_KEY);
  213. $ext_list[$ext_name] = false;
  214. $conf->extensions_enabled = $ext_list;
  215. $conf->save();
  216. Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name), $url_redirect);
  217. } else {
  218. Minz_Log::warning('Cannot disable extension ' . $ext_name . ': ' . $res);
  219. Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  220. }
  221. }
  222. Minz_Request::forward($url_redirect, true);
  223. }
  224. /**
  225. * This action handles deletion of an extension.
  226. *
  227. * Only administrator can remove an extension.
  228. * This action must be reached by a POST request.
  229. *
  230. * Parameter is:
  231. * -e: extension name (urlencoded)
  232. */
  233. public function removeAction(): void {
  234. if (!FreshRSS_Auth::hasAccess('admin')) {
  235. Minz_Error::error(403);
  236. }
  237. $url_redirect = ['c' => 'extension', 'a' => 'index'];
  238. if (Minz_Request::isPost()) {
  239. $ext_name = urldecode(Minz_Request::paramString('e'));
  240. $ext = Minz_ExtensionManager::findExtension($ext_name);
  241. if (is_null($ext)) {
  242. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  243. return;
  244. }
  245. $res = recursive_unlink($ext->getPath());
  246. if ($res) {
  247. Minz_Request::good(_t('feedback.extensions.removed', $ext_name), $url_redirect);
  248. } else {
  249. Minz_Request::bad(_t('feedback.extensions.cannot_remove', $ext_name), $url_redirect);
  250. }
  251. }
  252. Minz_Request::forward($url_redirect, true);
  253. }
  254. }