formaction.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Form action extendable class.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2008, 2009, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. /**
  33. * Form action extendable class
  34. *
  35. * @category Action
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @author Mikael Nordfeldth <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  40. * @link http://status.net/
  41. */
  42. class FormAction extends ManagedAction
  43. {
  44. protected $form = null;
  45. protected $formOpts = array();
  46. protected $type = null;
  47. protected $needLogin = true;
  48. protected $canPost = true;
  49. protected function prepare(array $args=array()) {
  50. parent::prepare($args);
  51. $this->form = $this->form ?: $this->action;
  52. $this->args['form'] = $this->form;
  53. $this->type = !is_null($this->type) ? $this->type : $this->trimmed('type');
  54. $this->args['context'] = $this->trimmed('action'); // reply for notice for example
  55. if (!$this->type) {
  56. $this->type = null;
  57. }
  58. return true;
  59. }
  60. public function isReadOnly($args) {
  61. return !$this->isPost();
  62. }
  63. public function showPageNotice()
  64. {
  65. $this->showInstructions();
  66. if ($msg = $this->getError()) {
  67. $this->element('div', 'error', $msg);
  68. }
  69. if ($msg = $this->getInfo()) {
  70. $this->element('div', 'info', $msg);
  71. }
  72. }
  73. /**
  74. * Outputs the instructions for the form
  75. *
  76. * SHOULD overload
  77. */
  78. public function showInstructions()
  79. {
  80. // instructions are nice, so users know what to do
  81. $this->raw(common_markup_to_html($this->getInstructions()));
  82. }
  83. /**
  84. * @return string with instructions to pass into common_markup_to_html()
  85. */
  86. public function getInstructions()
  87. {
  88. return null;
  89. }
  90. public function showForm($msg=null, $success=false)
  91. {
  92. $this->msg = $msg;
  93. $this->success = $success;
  94. $this->showPage();
  95. }
  96. protected function showContent()
  97. {
  98. $form = $this->getForm();
  99. $form->show();
  100. }
  101. protected function getForm()
  102. {
  103. $class = $this->form.'Form';
  104. $form = new $class($this, $this->formOpts);
  105. return $form;
  106. }
  107. /**
  108. * Gets called from handle() if isPost() is true;
  109. * @return void
  110. */
  111. protected function handlePost()
  112. {
  113. parent::handlePost();
  114. // check for this before token since all POST and FILES data
  115. // is losts when size is exceeded
  116. if (empty($_POST) && $_SERVER['CONTENT_LENGTH']>0) {
  117. // TRANS: Client error displayed when the number of bytes in a POST request exceeds a limit.
  118. // TRANS: %s is the number of bytes of the CONTENT_LENGTH.
  119. $msg = _m('The server was unable to handle that much POST data (%s MiB) due to its current configuration.',
  120. 'The server was unable to handle that much POST data (%s MiB) due to its current configuration.',
  121. round($_SERVER['CONTENT_LENGTH']/1024/1024,2));
  122. throw new ClientException($msg);
  123. }
  124. $this->checkSessionToken();
  125. }
  126. }