extensionController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * The controller to manage extensions.
  4. */
  5. class FreshRSS_extension_Controller extends Minz_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() {
  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() {
  20. Minz_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. $availableExtensions = $this->getAvailableExtensionList();
  32. $this->view->available_extensions = $availableExtensions;
  33. }
  34. /**
  35. * fetch extension list from GitHub
  36. */
  37. protected function getAvailableExtensionList() {
  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. $list = json_decode($json, true);
  47. if (empty($list)) {
  48. Minz_Log::warning('Failed to convert extension file list');
  49. return array();
  50. }
  51. // we could use that for comparing and caching later
  52. $version = $list['version'];
  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 = $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() {
  70. if (Minz_Request::param('ajax')) {
  71. $this->view->_useLayout(false);
  72. } else {
  73. $this->indexAction();
  74. $this->view->change_view('extension', 'index');
  75. }
  76. $ext_name = urldecode(Minz_Request::param('e'));
  77. $ext = Minz_ExtensionManager::findExtension($ext_name);
  78. if (is_null($ext)) {
  79. Minz_Error::error(404);
  80. }
  81. if ($ext->getType() === 'system' && !FreshRSS_Auth::hasAccess('admin')) {
  82. Minz_Error::error(403);
  83. }
  84. $this->view->extension = $ext;
  85. $this->view->extension->handleConfigureAction();
  86. }
  87. /**
  88. * This action enables a disabled extension for the current user.
  89. *
  90. * System extensions can only be enabled by an administrator.
  91. * This action must be reached by a POST request.
  92. *
  93. * Parameter is:
  94. * - e: the extension name (urlencoded).
  95. */
  96. public function enableAction() {
  97. $url_redirect = array('c' => 'extension', 'a' => 'index');
  98. if (Minz_Request::isPost()) {
  99. $ext_name = urldecode(Minz_Request::param('e'));
  100. $ext = Minz_ExtensionManager::findExtension($ext_name);
  101. if (is_null($ext)) {
  102. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  103. $url_redirect);
  104. }
  105. if ($ext->isEnabled()) {
  106. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name),
  107. $url_redirect);
  108. }
  109. $conf = null;
  110. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  111. $conf = FreshRSS_Context::$system_conf;
  112. } elseif ($ext->getType() === 'user') {
  113. $conf = FreshRSS_Context::$user_conf;
  114. } else {
  115. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name),
  116. $url_redirect);
  117. }
  118. $res = $ext->install();
  119. if ($res === true) {
  120. $ext_list = $conf->extensions_enabled;
  121. $ext_list[$ext_name] = true;
  122. $conf->extensions_enabled = $ext_list;
  123. $conf->save();
  124. Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name),
  125. $url_redirect);
  126. } else {
  127. Minz_Log::warning('Can not enable extension ' . $ext_name . ': ' . $res);
  128. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')),
  129. $url_redirect);
  130. }
  131. }
  132. Minz_Request::forward($url_redirect, true);
  133. }
  134. /**
  135. * This action disables an enabled extension for the current user.
  136. *
  137. * System extensions can only be disabled by an administrator.
  138. * This action must be reached by a POST request.
  139. *
  140. * Parameter is:
  141. * - e: the extension name (urlencoded).
  142. */
  143. public function disableAction() {
  144. $url_redirect = array('c' => 'extension', 'a' => 'index');
  145. if (Minz_Request::isPost()) {
  146. $ext_name = urldecode(Minz_Request::param('e'));
  147. $ext = Minz_ExtensionManager::findExtension($ext_name);
  148. if (is_null($ext)) {
  149. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  150. $url_redirect);
  151. }
  152. if (!$ext->isEnabled()) {
  153. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name),
  154. $url_redirect);
  155. }
  156. $conf = null;
  157. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  158. $conf = FreshRSS_Context::$system_conf;
  159. } elseif ($ext->getType() === 'user') {
  160. $conf = FreshRSS_Context::$user_conf;
  161. } else {
  162. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name),
  163. $url_redirect);
  164. }
  165. $res = $ext->uninstall();
  166. if ($res === true) {
  167. $ext_list = $conf->extensions_enabled;
  168. $legacyKey = array_search($ext_name, $ext_list, true);
  169. if ($legacyKey !== false) { //Legacy format FreshRSS < 1.11.1
  170. unset($ext_list[$legacyKey]);
  171. }
  172. $ext_list[$ext_name] = false;
  173. $conf->extensions_enabled = $ext_list;
  174. $conf->save();
  175. Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name),
  176. $url_redirect);
  177. } else {
  178. Minz_Log::warning('Can not unable extension ' . $ext_name . ': ' . $res);
  179. Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')),
  180. $url_redirect);
  181. }
  182. }
  183. Minz_Request::forward($url_redirect, true);
  184. }
  185. /**
  186. * This action handles deletion of an extension.
  187. *
  188. * Only administrator can remove an extension.
  189. * This action must be reached by a POST request.
  190. *
  191. * Parameter is:
  192. * -e: extension name (urlencoded)
  193. */
  194. public function removeAction() {
  195. if (!FreshRSS_Auth::hasAccess('admin')) {
  196. Minz_Error::error(403);
  197. }
  198. $url_redirect = array('c' => 'extension', 'a' => 'index');
  199. if (Minz_Request::isPost()) {
  200. $ext_name = urldecode(Minz_Request::param('e'));
  201. $ext = Minz_ExtensionManager::findExtension($ext_name);
  202. if (is_null($ext)) {
  203. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  204. $url_redirect);
  205. }
  206. $res = recursive_unlink($ext->getPath());
  207. if ($res) {
  208. Minz_Request::good(_t('feedback.extensions.removed', $ext_name),
  209. $url_redirect);
  210. } else {
  211. Minz_Request::bad(_t('feedback.extensions.cannot_delete', $ext_name),
  212. $url_redirect);
  213. }
  214. }
  215. Minz_Request::forward($url_redirect, true);
  216. }
  217. }