pluginenable.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Plugin enable action.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * PHP version 5
  22. *
  23. * @category Action
  24. * @package StatusNet
  25. * @author Brion Vibber <brion@status.net>
  26. * @copyright 2010 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Plugin enable action.
  35. *
  36. * (Re)-enables a plugin from the default plugins list.
  37. *
  38. * Takes parameters:
  39. *
  40. * - plugin: plugin name
  41. * - token: session token to prevent CSRF attacks
  42. * - ajax: boolean; whether to return Ajax or full-browser results
  43. *
  44. * Only works if the current user is logged in.
  45. *
  46. * @category Action
  47. * @package StatusNet
  48. * @author Brion Vibber <brion@status.net>
  49. * @copyright 2010 StatusNet, Inc.
  50. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
  51. * @link http://status.net/
  52. */
  53. class PluginEnableAction extends Action
  54. {
  55. var $user;
  56. var $plugin;
  57. /**
  58. * Check pre-requisites and instantiate attributes
  59. *
  60. * @param Array $args array of arguments (URL, GET, POST)
  61. *
  62. * @return boolean success flag
  63. */
  64. function prepare(array $args = array())
  65. {
  66. parent::prepare($args);
  67. // @fixme these are pretty common, should a parent class factor these out?
  68. // Only allow POST requests
  69. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  70. // TRANS: Client error displayed when trying to use another method than POST.
  71. // TRANS: Do not translate POST.
  72. $this->clientError(_('This action only accepts POST requests.'));
  73. }
  74. // CSRF protection
  75. $token = $this->trimmed('token');
  76. if (!$token || $token != common_session_token()) {
  77. // TRANS: Client error displayed when the session token does not match or is not given.
  78. $this->clientError(_('There was a problem with your session token.'.
  79. ' Try again, please.'));
  80. }
  81. // Only for logged-in users
  82. $this->user = common_current_user();
  83. if (empty($this->user)) {
  84. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  85. $this->clientError(_('Not logged in.'));
  86. }
  87. if (!AdminPanelAction::canAdmin('plugins')) {
  88. // TRANS: Client error displayed when trying to enable or disable a plugin without access rights.
  89. $this->clientError(_('You cannot administer plugins.'));
  90. }
  91. $this->plugin = $this->arg('plugin');
  92. $defaultPlugins = common_config('plugins', 'default');
  93. if (!array_key_exists($this->plugin, $defaultPlugins)) {
  94. // TRANS: Client error displayed when trying to enable or disable a non-existing plugin.
  95. $this->clientError(_('No such plugin.'));
  96. }
  97. return true;
  98. }
  99. /**
  100. * Handle request
  101. *
  102. * Does the subscription and returns results.
  103. *
  104. * @param Array $args unused.
  105. *
  106. * @return void
  107. */
  108. function handle()
  109. {
  110. $key = 'disable-' . $this->plugin;
  111. Config::save('plugins', $key, $this->overrideValue());
  112. // @fixme this is a pretty common pattern and should be refactored down
  113. if ($this->boolean('ajax')) {
  114. $this->startHTML('text/xml;charset=utf-8');
  115. $this->elementStart('head');
  116. $this->element('title', null, $this->successShortTitle());
  117. $this->elementEnd('head');
  118. $this->elementStart('body');
  119. $form = $this->successNextForm();
  120. $form->show();
  121. $this->elementEnd('body');
  122. $this->endHTML();
  123. } else {
  124. $url = common_local_url('pluginsadminpanel');
  125. common_redirect($url, 303);
  126. }
  127. }
  128. /**
  129. * Value to save into $config['plugins']['disable-<name>']
  130. */
  131. protected function overrideValue()
  132. {
  133. return 0;
  134. }
  135. protected function successShortTitle()
  136. {
  137. // TRANS: Page title for AJAX form return when enabling a plugin.
  138. return _m('plugin', 'Enabled');
  139. }
  140. protected function successNextForm()
  141. {
  142. return new DisablePluginForm($this, $this->plugin);
  143. }
  144. }