extensionController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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() {
  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. 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. $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->_layout(false);
  72. } else {
  73. $this->indexAction();
  74. $this->view->_path('extension/index.phtml');
  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), $url_redirect);
  103. }
  104. if ($ext->isEnabled()) {
  105. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name), $url_redirect);
  106. }
  107. $conf = null;
  108. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  109. $conf = FreshRSS_Context::$system_conf;
  110. } elseif ($ext->getType() === 'user') {
  111. $conf = FreshRSS_Context::$user_conf;
  112. } else {
  113. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  114. }
  115. $res = $ext->install();
  116. if ($res === true) {
  117. $ext_list = $conf->extensions_enabled;
  118. $ext_list[$ext_name] = true;
  119. $conf->extensions_enabled = $ext_list;
  120. $conf->save();
  121. Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name), $url_redirect);
  122. } else {
  123. Minz_Log::warning('Can not enable extension ' . $ext_name . ': ' . $res);
  124. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  125. }
  126. }
  127. Minz_Request::forward($url_redirect, true);
  128. }
  129. /**
  130. * This action disables an enabled extension for the current user.
  131. *
  132. * System extensions can only be disabled by an administrator.
  133. * This action must be reached by a POST request.
  134. *
  135. * Parameter is:
  136. * - e: the extension name (urlencoded).
  137. */
  138. public function disableAction() {
  139. $url_redirect = array('c' => 'extension', 'a' => 'index');
  140. if (Minz_Request::isPost()) {
  141. $ext_name = urldecode(Minz_Request::param('e'));
  142. $ext = Minz_ExtensionManager::findExtension($ext_name);
  143. if (is_null($ext)) {
  144. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name), $url_redirect);
  145. }
  146. if (!$ext->isEnabled()) {
  147. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name), $url_redirect);
  148. }
  149. $conf = null;
  150. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  151. $conf = FreshRSS_Context::$system_conf;
  152. } elseif ($ext->getType() === 'user') {
  153. $conf = FreshRSS_Context::$user_conf;
  154. } else {
  155. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name), $url_redirect);
  156. }
  157. $res = $ext->uninstall();
  158. if ($res === true) {
  159. $ext_list = $conf->extensions_enabled;
  160. $legacyKey = array_search($ext_name, $ext_list, true);
  161. if ($legacyKey !== false) { //Legacy format FreshRSS < 1.11.1
  162. unset($ext_list[$legacyKey]);
  163. }
  164. $ext_list[$ext_name] = false;
  165. $conf->extensions_enabled = $ext_list;
  166. $conf->save();
  167. Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name), $url_redirect);
  168. } else {
  169. Minz_Log::warning('Can not unable extension ' . $ext_name . ': ' . $res);
  170. Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')), $url_redirect);
  171. }
  172. }
  173. Minz_Request::forward($url_redirect, true);
  174. }
  175. /**
  176. * This action handles deletion of an extension.
  177. *
  178. * Only administrator can remove an extension.
  179. * This action must be reached by a POST request.
  180. *
  181. * Parameter is:
  182. * -e: extension name (urlencoded)
  183. */
  184. public function removeAction() {
  185. if (!FreshRSS_Auth::hasAccess('admin')) {
  186. Minz_Error::error(403);
  187. }
  188. $url_redirect = array('c' => 'extension', 'a' => 'index');
  189. if (Minz_Request::isPost()) {
  190. $ext_name = urldecode(Minz_Request::param('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. }
  195. $res = recursive_unlink($ext->getPath());
  196. if ($res) {
  197. Minz_Request::good(_t('feedback.extensions.removed', $ext_name), $url_redirect);
  198. } else {
  199. Minz_Request::bad(_t('feedback.extensions.cannot_remove', $ext_name), $url_redirect);
  200. }
  201. }
  202. Minz_Request::forward($url_redirect, true);
  203. }
  204. }