extensionController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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')['body'];
  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. try {
  121. $this->view->extension->handleConfigureAction();
  122. } catch (Minz_Exception $e) { // @phpstan-ignore catch.neverThrown (Thrown by extensions)
  123. Minz_Log::error('Error while configuring extension ' . $ext->getName() . ': ' . $e->getMessage());
  124. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')), ['c' => 'extension', 'a' => 'index']);
  125. }
  126. }
  127. /**
  128. * This action enables a disabled extension for the current user.
  129. *
  130. * System extensions can only be enabled by an administrator.
  131. * This action must be reached by a POST request.
  132. *
  133. * Parameter is:
  134. * - e: the extension name (urlencoded).
  135. */
  136. public function enableAction(): void {
  137. $url_redirect = ['c' => 'extension', 'a' => 'index'];
  138. if (Minz_Request::isPost()) {
  139. $ext_name = urldecode(Minz_Request::paramString('e'));
  140. $ext = Minz_ExtensionManager::findExtension($ext_name);
  141. if (is_null($ext)) {
  142. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  143. return;
  144. }
  145. if ($ext->isEnabled()) {
  146. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name), $url_redirect);
  147. }
  148. $type = $ext->getType();
  149. if ($type !== 'user' && !FreshRSS_Auth::hasAccess('admin')) {
  150. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  151. return;
  152. }
  153. $conf = null;
  154. if ($type === 'system') {
  155. $conf = FreshRSS_Context::systemConf();
  156. } elseif ($type === 'user') {
  157. $conf = FreshRSS_Context::userConf();
  158. }
  159. $res = $ext->install();
  160. if ($conf !== null && $res === true) {
  161. $ext_list = $conf->extensions_enabled;
  162. $ext_list = array_filter($ext_list, static function (string $key) use ($type) {
  163. // Remove from list the extensions that have disappeared or changed type
  164. $extension = Minz_ExtensionManager::findExtension($key);
  165. return $extension !== null && $extension->getType() === $type;
  166. }, ARRAY_FILTER_USE_KEY);
  167. $ext_list[$ext_name] = true;
  168. $conf->extensions_enabled = $ext_list;
  169. $conf->save();
  170. Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name), $url_redirect);
  171. } else {
  172. Minz_Log::warning('Cannot enable extension ' . $ext_name . ': ' . $res);
  173. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  174. }
  175. }
  176. Minz_Request::forward($url_redirect, true);
  177. }
  178. /**
  179. * This action disables an enabled extension for the current user.
  180. *
  181. * System extensions can only be disabled by an administrator.
  182. * This action must be reached by a POST request.
  183. *
  184. * Parameter is:
  185. * - e: the extension name (urlencoded).
  186. */
  187. public function disableAction(): void {
  188. $url_redirect = ['c' => 'extension', 'a' => 'index'];
  189. if (Minz_Request::isPost()) {
  190. $ext_name = urldecode(Minz_Request::paramString('e'));
  191. $ext = Minz_ExtensionManager::findExtension($ext_name);
  192. if (is_null($ext)) {
  193. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  194. return;
  195. }
  196. if (!$ext->isEnabled()) {
  197. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name), $url_redirect);
  198. }
  199. $type = $ext->getType();
  200. if ($type !== 'user' && !FreshRSS_Auth::hasAccess('admin')) {
  201. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  202. return;
  203. }
  204. $conf = null;
  205. if ($type === 'system') {
  206. $conf = FreshRSS_Context::systemConf();
  207. } elseif ($type === 'user') {
  208. $conf = FreshRSS_Context::userConf();
  209. }
  210. $res = $ext->uninstall();
  211. if ($conf !== null && $res === true) {
  212. $ext_list = $conf->extensions_enabled;
  213. $ext_list = array_filter($ext_list, static function (string $key) use ($type) {
  214. // Remove from list the extensions that have disappeared or changed type
  215. $extension = Minz_ExtensionManager::findExtension($key);
  216. return $extension !== null && $extension->getType() === $type;
  217. }, ARRAY_FILTER_USE_KEY);
  218. $ext_list[$ext_name] = false;
  219. $conf->extensions_enabled = $ext_list;
  220. $conf->save();
  221. Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name), $url_redirect);
  222. } else {
  223. Minz_Log::warning('Cannot disable extension ' . $ext_name . ': ' . $res);
  224. Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  225. }
  226. }
  227. Minz_Request::forward($url_redirect, true);
  228. }
  229. /**
  230. * This action handles deletion of an extension.
  231. *
  232. * Only administrator can remove an extension.
  233. * This action must be reached by a POST request.
  234. *
  235. * Parameter is:
  236. * -e: extension name (urlencoded)
  237. */
  238. public function removeAction(): void {
  239. if (!FreshRSS_Auth::hasAccess('admin')) {
  240. Minz_Error::error(403);
  241. }
  242. $url_redirect = ['c' => 'extension', 'a' => 'index'];
  243. if (Minz_Request::isPost()) {
  244. $ext_name = urldecode(Minz_Request::paramString('e'));
  245. $ext = Minz_ExtensionManager::findExtension($ext_name);
  246. if (is_null($ext)) {
  247. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  248. return;
  249. }
  250. $res = recursive_unlink($ext->getPath());
  251. if ($res) {
  252. Minz_Request::good(_t('feedback.extensions.removed', $ext_name), $url_redirect);
  253. } else {
  254. Minz_Request::bad(_t('feedback.extensions.cannot_remove', $ext_name), $url_redirect);
  255. }
  256. }
  257. Minz_Request::forward($url_redirect, true);
  258. }
  259. // Supported types with their associated content type
  260. public const MIME_TYPES = [
  261. 'css' => 'text/css; charset=UTF-8',
  262. 'gif' => 'image/gif',
  263. 'jpeg' => 'image/jpeg',
  264. 'jpg' => 'image/jpeg',
  265. 'js' => 'application/javascript; charset=UTF-8',
  266. 'png' => 'image/png',
  267. 'svg' => 'image/svg+xml',
  268. ];
  269. public function serveAction(): void {
  270. $extensionName = Minz_Request::paramString('x');
  271. $filename = Minz_Request::paramString('f');
  272. $mimeType = pathinfo($filename, PATHINFO_EXTENSION);
  273. if ($extensionName === '' || $filename === '' || $mimeType === '' || empty(self::MIME_TYPES[$mimeType])) {
  274. header('HTTP/1.1 400 Bad Request');
  275. die('Bad Request!');
  276. }
  277. $extension = Minz_ExtensionManager::findExtension($extensionName);
  278. if ($extension === null || !$extension->isEnabled() || ($mtime = $extension->mtimeFile($filename)) === null) {
  279. header('HTTP/1.1 404 Not Found');
  280. die('Not Found!');
  281. }
  282. $this->view->_layout(null);
  283. $content_type = self::MIME_TYPES[$mimeType];
  284. header("Content-Type: {$content_type}");
  285. header("Content-Disposition: inline; filename='{$filename}'");
  286. header('Referrer-Policy: same-origin');
  287. if (file_exists(DATA_PATH . '/no-cache.txt') || !httpConditional($mtime, cacheSeconds: 604800, cachePrivacy: 2)) {
  288. echo $extension->getFile($filename);
  289. }
  290. }
  291. }