extensionController.php 8.3 KB

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