redirectingaction.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Superclass for actions that redirect to a given return-to page on completion.
  4. *
  5. * PHP version 5
  6. *
  7. * StatusNet - the distributed open-source microblogging tool
  8. * Copyright (C) 2009-2010, StatusNet, Inc.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Action
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Brion Vibber <brion@status.net>
  27. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. /**
  34. * Superclass for actions that redirect to a given return-to page on completion.
  35. *
  36. * @category Action
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @author Brion Vibber <brion@status.net>
  40. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  41. * @link http://status.net/
  42. */
  43. class RedirectingAction extends Action
  44. {
  45. /**
  46. * Redirect browser to the page our hidden parameters requested,
  47. * or if none given, to the url given by $this->defaultReturnTo().
  48. *
  49. * To be called only after successful processing.
  50. *
  51. * Note: this was named returnToArgs() up through 0.9.2, which
  52. * caused problems because there's an Action::returnToArgs()
  53. * already which does something different.
  54. *
  55. * @return void
  56. */
  57. function returnToPrevious()
  58. {
  59. // Now, gotta figure where we go back to
  60. $action = false;
  61. $args = array();
  62. $params = array();
  63. foreach ($this->args as $k => $v) {
  64. if ($k == 'returnto-action') {
  65. $action = $v;
  66. } else if (substr($k, 0, 15) == 'returnto-param-') {
  67. $params[substr($k, 15)] = $v;
  68. } elseif (substr($k, 0, 9) == 'returnto-') {
  69. $args[substr($k, 9)] = $v;
  70. }
  71. }
  72. if ($action) {
  73. common_redirect(common_local_url($action, $args, $params), 303);
  74. } else {
  75. $url = $this->defaultReturnTo();
  76. }
  77. common_redirect($url, 303);
  78. }
  79. /**
  80. * If we reached this form without returnto arguments, where should
  81. * we go? May be overridden by subclasses to a reasonable destination
  82. * for that action; default implementation throws an exception.
  83. *
  84. * @return string URL
  85. */
  86. function defaultReturnTo()
  87. {
  88. // TRANS: Client error displayed when return-to was defined without a target.
  89. $this->clientError(_('No return-to arguments.'));
  90. }
  91. }