extensionController.php 8.6 KB

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