pluginenable.php 4.6 KB

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