extensionController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. if (Minz_Request::isPost()) {
  47. $this->view->extension->handleConfigureAction();
  48. }
  49. }
  50. /**
  51. * This action enables a disabled extension for the current user.
  52. *
  53. * System extensions can only be enabled by an administrator.
  54. * This action must be reached by a POST request.
  55. *
  56. * Parameter is:
  57. * - e: the extension name (urlencoded).
  58. */
  59. public function enableAction() {
  60. $url_redirect = array('c' => 'extension', 'a' => 'index');
  61. if (Minz_Request::isPost()) {
  62. $ext_name = urldecode(Minz_Request::param('e'));
  63. $ext = Minz_ExtensionManager::find_extension($ext_name);
  64. if (is_null($ext)) {
  65. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  66. $url_redirect);
  67. }
  68. if ($ext->is_enabled()) {
  69. Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name),
  70. $url_redirect);
  71. }
  72. $conf = null;
  73. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  74. $conf = FreshRSS_Context::$system_conf;
  75. } elseif ($ext->getType() === 'user') {
  76. $conf = FreshRSS_Context::$user_conf;
  77. } else {
  78. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name),
  79. $url_redirect);
  80. }
  81. $ext->install();
  82. $ext_list = $conf->extensions_enabled;
  83. array_push_unique($ext_list, $ext_name);
  84. $conf->extensions_enabled = $ext_list;
  85. $conf->save();
  86. Minz_Request::good(_t('feedback.extensions.enabled', $ext_name),
  87. $url_redirect);
  88. }
  89. Minz_Request::forward($url_redirect, true);
  90. }
  91. /**
  92. * This action disables an enabled extension for the current user.
  93. *
  94. * System extensions can only be disabled 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 disableAction() {
  101. $url_redirect = array('c' => 'extension', 'a' => 'index');
  102. if (Minz_Request::isPost()) {
  103. $ext_name = urldecode(Minz_Request::param('e'));
  104. $ext = Minz_ExtensionManager::find_extension($ext_name);
  105. if (is_null($ext)) {
  106. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  107. $url_redirect);
  108. }
  109. if (!$ext->is_enabled()) {
  110. Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name),
  111. $url_redirect);
  112. }
  113. $conf = null;
  114. if ($ext->getType() === 'system' && FreshRSS_Auth::hasAccess('admin')) {
  115. $conf = FreshRSS_Context::$system_conf;
  116. } elseif ($ext->getType() === 'user') {
  117. $conf = FreshRSS_Context::$user_conf;
  118. } else {
  119. Minz_Request::bad(_t('feedback.extensions.no_access', $ext_name),
  120. $url_redirect);
  121. }
  122. $ext->uninstall();
  123. $ext_list = $conf->extensions_enabled;
  124. array_remove($ext_list, $ext_name);
  125. $conf->extensions_enabled = $ext_list;
  126. $conf->save();
  127. Minz_Request::good(_t('feedback.extensions.disabled', $ext_name),
  128. $url_redirect);
  129. }
  130. Minz_Request::forward($url_redirect, true);
  131. }
  132. /**
  133. * This action handles deletion of an extension.
  134. *
  135. * Only administrator can remove an extension.
  136. * This action must be reached by a POST request.
  137. *
  138. * Parameter is:
  139. * -e: extension name (urlencoded)
  140. */
  141. public function removeAction() {
  142. if (!FreshRSS_Auth::hasAccess('admin')) {
  143. Minz_Error::error(403);
  144. }
  145. $url_redirect = array('c' => 'extension', 'a' => 'index');
  146. if (Minz_Request::isPost()) {
  147. $ext_name = urldecode(Minz_Request::param('e'));
  148. $ext = Minz_ExtensionManager::find_extension($ext_name);
  149. if (is_null($ext)) {
  150. Minz_Request::bad(_t('feedback.extensions.not_found', $ext_name),
  151. $url_redirect);
  152. }
  153. $res = recursive_unlink($ext->getPath());
  154. if ($res) {
  155. Minz_Request::good(_t('feedback.extensions.removed', $ext_name),
  156. $url_redirect);
  157. } else {
  158. Minz_Request::bad(_t('feedback.extensions.cannot_delete', $ext_name),
  159. $url_redirect);
  160. }
  161. }
  162. Minz_Request::forward($url_redirect, true);
  163. }
  164. }