extensionController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = Minz_ExtensionManager::list_extensions();
  22. }
  23. /**
  24. * This action handles configuration of a given extension.
  25. *
  26. * Only administrator can configure a system extension.
  27. *
  28. * Parameters are:
  29. * - e: the extension name (urlencoded)
  30. * - additional parameters which should be handle by the extension
  31. * handleConfigureAction() method (POST request).
  32. */
  33. public function configureAction() {
  34. if (Minz_Request::param('ajax')) {
  35. $this->view->_useLayout(false);
  36. }
  37. $ext_name = urldecode(Minz_Request::param('e'));
  38. $ext = Minz_ExtensionManager::find_extension($ext_name);
  39. if (is_null($ext)) {
  40. Minz_Error::error(404);
  41. }
  42. if ($ext->getType() === 'system' && !FreshRSS_Auth::hasAccess('admin')) {
  43. Minz_Error::error(403);
  44. }
  45. $this->view->extension = $ext;
  46. $this->view->extension->handleConfigureAction();
  47. }
  48. /**
  49. * This action enables a disabled extension for the current user.
  50. *
  51. * System extensions can only be enabled by an administrator.
  52. * This action must be reached by a POST request.
  53. *
  54. * Parameter is:
  55. * - e: the extension name (urlencoded).
  56. */
  57. public function enableAction() {
  58. $url_redirect = array('c' => 'extension', 'a' => 'index');
  59. if (Minz_Request::isPost()) {
  60. $ext_name = urldecode(Minz_Request::param('e'));
  61. $ext = Minz_ExtensionManager::find_extension($ext_name);
  62. if (is_null($ext)) {
  63. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  64. $url_redirect);
  65. }
  66. if ($ext->is_enabled()) {
  67. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name),
  68. $url_redirect);
  69. }
  70. $conf = null;
  71. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  72. $conf = FreshRSS_Context::$system_conf;
  73. } elseif ($ext->getType() === 'user') {
  74. $conf = FreshRSS_Context::$user_conf;
  75. } else {
  76. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name),
  77. $url_redirect);
  78. }
  79. $res = $ext->install();
  80. if ($res === true) {
  81. $ext_list = $conf->extensions_enabled;
  82. array_push_unique($ext_list, $ext_name);
  83. $conf->extensions_enabled = $ext_list;
  84. $conf->save();
  85. Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name),
  86. $url_redirect);
  87. } else {
  88. Minz_Log::warning('Can not enable extension ' . $ext_name . ': ' . $res);
  89. Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')),
  90. $url_redirect);
  91. }
  92. }
  93. Minz_Request::forward($url_redirect, true);
  94. }
  95. /**
  96. * This action disables an enabled extension for the current user.
  97. *
  98. * System extensions can only be disabled by an administrator.
  99. * This action must be reached by a POST request.
  100. *
  101. * Parameter is:
  102. * - e: the extension name (urlencoded).
  103. */
  104. public function disableAction() {
  105. $url_redirect = array('c' => 'extension', 'a' => 'index');
  106. if (Minz_Request::isPost()) {
  107. $ext_name = urldecode(Minz_Request::param('e'));
  108. $ext = Minz_ExtensionManager::find_extension($ext_name);
  109. if (is_null($ext)) {
  110. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  111. $url_redirect);
  112. }
  113. if (!$ext->is_enabled()) {
  114. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name),
  115. $url_redirect);
  116. }
  117. $conf = null;
  118. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  119. $conf = FreshRSS_Context::$system_conf;
  120. } elseif ($ext->getType() === 'user') {
  121. $conf = FreshRSS_Context::$user_conf;
  122. } else {
  123. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name),
  124. $url_redirect);
  125. }
  126. $res = $ext->uninstall();
  127. if ($res === true) {
  128. $ext_list = $conf->extensions_enabled;
  129. array_remove($ext_list, $ext_name);
  130. $conf->extensions_enabled = $ext_list;
  131. $conf->save();
  132. Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name),
  133. $url_redirect);
  134. } else {
  135. Minz_Log::warning('Can not unable extension ' . $ext_name . ': ' . $res);
  136. Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')),
  137. $url_redirect);
  138. }
  139. }
  140. Minz_Request::forward($url_redirect, true);
  141. }
  142. /**
  143. * This action handles deletion of an extension.
  144. *
  145. * Only administrator can remove an extension.
  146. * This action must be reached by a POST request.
  147. *
  148. * Parameter is:
  149. * -e: extension name (urlencoded)
  150. */
  151. public function removeAction() {
  152. if (!FreshRSS_Auth::hasAccess('admin')) {
  153. Minz_Error::error(403);
  154. }
  155. $url_redirect = array('c' => 'extension', 'a' => 'index');
  156. if (Minz_Request::isPost()) {
  157. $ext_name = urldecode(Minz_Request::param('e'));
  158. $ext = Minz_ExtensionManager::find_extension($ext_name);
  159. if (is_null($ext)) {
  160. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  161. $url_redirect);
  162. }
  163. $res = recursive_unlink($ext->getPath());
  164. if ($res) {
  165. Minz_Request::good(_t('feedback.extensions.removed', $ext_name),
  166. $url_redirect);
  167. } else {
  168. Minz_Request::bad(_t('feedback.extensions.cannot_delete', $ext_name),
  169. $url_redirect);
  170. }
  171. }
  172. Minz_Request::forward($url_redirect, true);
  173. }
  174. }