extensionController.php 8.1 KB

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