adminpanelaction.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Superclass for admin panel actions
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category UI
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * superclass for admin panel actions
  34. *
  35. * Common code for all admin panel actions.
  36. *
  37. * @category UI
  38. * @package StatusNet
  39. * @author Evan Prodromou <evan@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  41. * @link http://status.net/
  42. *
  43. * @todo Find some commonalities with SettingsAction and combine
  44. */
  45. class AdminPanelAction extends Action
  46. {
  47. public $widgetOpts;
  48. public $scoped;
  49. var $success = true;
  50. var $msg = null;
  51. /**
  52. * Prepare for the action
  53. *
  54. * We check to see that the user is logged in, has
  55. * authenticated in this session, and has the right
  56. * to configure the site.
  57. *
  58. * @param array $args Array of arguments from Web driver
  59. *
  60. * @return boolean success flag
  61. */
  62. function prepare(array $args = array())
  63. {
  64. parent::prepare($args);
  65. // User must be logged in.
  66. if (!common_logged_in()) {
  67. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  68. $this->clientError(_('Not logged in.'));
  69. }
  70. $user = common_current_user();
  71. // ...because they're logged in
  72. if (empty($user)) {
  73. doom("\$userは空です。", __FILE__, __LINE__);
  74. }
  75. // It must be a "real" login, not saved cookie login
  76. if (!common_is_real_login()) {
  77. // Cookie theft is too easy; we require automatic
  78. // logins to re-authenticate before admining the site
  79. common_set_returnto($this->selfUrl());
  80. if (Event::handle('RedirectToLogin', array($this, $user))) {
  81. common_redirect(common_local_url('login'), 303);
  82. }
  83. }
  84. // User must have the right to change admin settings
  85. if (!$user->hasRight(Right::CONFIGURESITE)) {
  86. // TRANS: Client error message thrown when a user tries to change admin settings but has no access rights.
  87. $this->clientError(_('You cannot make changes to this site.'));
  88. }
  89. // This panel must be enabled
  90. $name = $this->trimmed('action');
  91. $name = mb_substr($name, 0, -10);
  92. if (!self::canAdmin($name)) {
  93. // TRANS: Client error message throw when a certain panel's settings cannot be changed.
  94. $this->clientError(_('Changes to that panel are not allowed.'), 403);
  95. }
  96. return true;
  97. }
  98. /**
  99. * handle the action
  100. *
  101. * Check session token and try to save the settings if this is a
  102. * POST. Otherwise, show the form.
  103. *
  104. * @param array $args unused.
  105. *
  106. * @return void
  107. */
  108. function handle()
  109. {
  110. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  111. $this->checkSessionToken();
  112. try {
  113. $this->saveSettings();
  114. // Reload settings
  115. Config::loadSettings();
  116. $this->success = true;
  117. // TRANS: Message after successful saving of administrative settings.
  118. $this->msg = _('Settings saved.');
  119. } catch (Exception $e) {
  120. $this->success = false;
  121. $this->msg = $e->getMessage();
  122. }
  123. }
  124. $this->showPage();
  125. }
  126. /**
  127. * Show tabset for this page
  128. *
  129. * Uses the AdminPanelNav widget
  130. *
  131. * @return void
  132. * @see AdminPanelNav
  133. */
  134. function showLocalNav()
  135. {
  136. $nav = new AdminPanelNav($this);
  137. $nav->show();
  138. }
  139. /**
  140. * Show the content section of the page
  141. *
  142. * Here, we show the admin panel's form.
  143. *
  144. * @return void.
  145. */
  146. function showContent()
  147. {
  148. $this->showForm();
  149. }
  150. /**
  151. * Show content block. Overrided just to add a special class
  152. * to the content div to allow styling.
  153. *
  154. * @return nothing
  155. */
  156. function showContentBlock()
  157. {
  158. $this->elementStart('div', array('id' => 'content', 'class' => 'admin'));
  159. $this->showPageTitle();
  160. $this->showPageNoticeBlock();
  161. $this->elementStart('div', array('id' => 'content_inner'));
  162. // show the actual content (forms, lists, whatever)
  163. $this->showContent();
  164. $this->elementEnd('div');
  165. $this->elementEnd('div');
  166. }
  167. /**
  168. * show human-readable instructions for the page, or
  169. * a success/failure on save.
  170. *
  171. * @return void
  172. */
  173. function showPageNotice()
  174. {
  175. if ($this->msg) {
  176. $this->element('div', ($this->success) ? 'success' : 'error',
  177. $this->msg);
  178. } else {
  179. $inst = $this->getInstructions();
  180. $output = common_markup_to_html($inst);
  181. $this->elementStart('div', 'instructions');
  182. $this->raw($output);
  183. $this->elementEnd('div');
  184. }
  185. }
  186. /**
  187. * Show the admin panel form
  188. *
  189. * Sub-classes should overload this.
  190. *
  191. * @return void
  192. */
  193. function showForm()
  194. {
  195. // TRANS: Client error message.
  196. $this->clientError(_('showForm() not implemented.'));
  197. }
  198. /**
  199. * Instructions for using this form.
  200. *
  201. * String with instructions for using the form.
  202. *
  203. * Subclasses should overload this.
  204. *
  205. * @return void
  206. */
  207. function getInstructions()
  208. {
  209. return '';
  210. }
  211. /**
  212. * Save settings from the form
  213. *
  214. * Validate and save the settings from the user.
  215. *
  216. * @return void
  217. */
  218. function saveSettings()
  219. {
  220. // TRANS: Client error message
  221. $this->clientError(_('saveSettings() not implemented.'));
  222. }
  223. static function canAdmin($name)
  224. {
  225. $isOK = false;
  226. if (Event::handle('AdminPanelCheck', array($name, &$isOK))) {
  227. $isOK = in_array($name, common_config('admin', 'panels'));
  228. }
  229. return $isOK;
  230. }
  231. function showProfileBlock()
  232. {
  233. }
  234. }