plugindisable.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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: boolean; 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 PlugindisableAction extends PluginenableAction
  38. {
  39. /**
  40. * Handle request
  41. *
  42. * Disables the plugin and returns results.
  43. *
  44. * @return void
  45. * @throws ClientException
  46. */
  47. function handle()
  48. {
  49. if (PluginList::isPluginLoaded($this->plugin)) {
  50. $config_file = INSTALLDIR . DIRECTORY_SEPARATOR . 'config.php';
  51. $config_lines = file($config_file, FILE_IGNORE_NEW_LINES);
  52. foreach($config_lines as $key => $line) {
  53. // We are doing it this way to avoid deleting things we shouldn't
  54. $line = str_replace('addPlugin(\''.$this->plugin.'\');', '', $line);
  55. $config_lines[$key] = $line;
  56. if($line === ' // Added by sysadmin\'s Plugin UI.') {
  57. unset($config_lines[$key]);
  58. }
  59. }
  60. $new_config_data = implode(PHP_EOL, $config_lines);
  61. if (!file_put_contents($config_file, $new_config_data)) {
  62. $this->clientError(_m('No permissions for writing to config.php'));
  63. }
  64. }
  65. $key = 'disable-' . $this->plugin;
  66. Config::save('plugins', $key, $this->overrideValue());
  67. // @fixme this is a pretty common pattern and should be refactored down
  68. if ($this->boolean('ajax')) {
  69. $this->startHTML('text/xml;charset=utf-8');
  70. $this->elementStart('head');
  71. $this->element('title', null, $this->successShortTitle());
  72. $this->elementEnd('head');
  73. $this->elementStart('body');
  74. $form = $this->successNextForm();
  75. $form->show();
  76. $this->elementEnd('body');
  77. $this->endHTML();
  78. } else {
  79. $url = common_local_url('pluginsadminpanel');
  80. common_redirect($url, 303);
  81. }
  82. }
  83. /**
  84. * Value to save into $config['plugins']['disable-<name>']
  85. */
  86. protected function overrideValue()
  87. {
  88. return 1;
  89. }
  90. protected function successShortTitle()
  91. {
  92. // TRANS: Page title for AJAX form return when a disabling a plugin.
  93. return _m('plugin', 'Disabled');
  94. }
  95. protected function successNextForm()
  96. {
  97. return new PluginEnableForm($this, $this->plugin);
  98. }
  99. }