Jump.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * This action performs HTTP redirect to a specific page.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm_Controller
  16. * @author Alexey Borzov <avb@php.net>
  17. * @copyright 2003-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version SVN: $Id: Jump.php 289084 2009-10-02 06:53:09Z avb $
  20. * @link http://pear.php.net/package/HTML_QuickForm_Controller
  21. */
  22. /**
  23. * Class representing an action to perform on HTTP request.
  24. */
  25. require_once 'HTML/QuickForm/Action.php';
  26. /**
  27. * This action performs HTTP redirect to a specific page.
  28. *
  29. * @category HTML
  30. * @package HTML_QuickForm_Controller
  31. * @author Alexey Borzov <avb@php.net>
  32. * @version Release: 1.0.10
  33. */
  34. class HTML_QuickForm_Action_Jump extends HTML_QuickForm_Action
  35. {
  36. /**
  37. * Splits (part of) the URI into path and query components
  38. *
  39. * @param string String of the form 'foo?bar'
  40. * @return array Array of the form array('foo', '?bar)
  41. * @access private
  42. */
  43. function _splitUri($uri)
  44. {
  45. if (false === ($qm = strpos($uri, '?'))) {
  46. return array($uri, '');
  47. } else {
  48. return array(substr($uri, 0, $qm), substr($uri, $qm));
  49. }
  50. }
  51. /**
  52. * Removes the '..' and '.' segments from the path component
  53. *
  54. * @param string Path component of the URL, possibly with '.' and '..' segments
  55. * @return string Path component of the URL with '.' and '..' segments removed
  56. * @access private
  57. */
  58. function _normalizePath($path)
  59. {
  60. $pathAry = explode('/', $path);
  61. $i = 1;
  62. do {
  63. if ('.' == $pathAry[$i]) {
  64. if ($i < count($pathAry) - 1) {
  65. array_splice($pathAry, $i, 1);
  66. } else {
  67. $pathAry[$i] = '';
  68. $i++;
  69. }
  70. } elseif ('..' == $pathAry[$i] && $i > 1 && '..' != $pathAry[$i - 1]) {
  71. if ($i < count($pathAry) -1) {
  72. array_splice($pathAry, $i - 1, 2);
  73. $i--;
  74. } else {
  75. array_splice($pathAry, $i - 1, 2, '');
  76. }
  77. } else {
  78. $i++;
  79. }
  80. } while ($i < count($pathAry));
  81. return implode('/', $pathAry);
  82. }
  83. /**
  84. * Resolves relative URL using current page's URL as base
  85. *
  86. * The method follows procedure described in section 4 of RFC 1808 and
  87. * passes the examples provided in section 5 of said RFC. Values from
  88. * $_SERVER array are used for calculation of "current URL"
  89. *
  90. * @param string Relative URL, probably from form's action attribute
  91. * @return string Absolute URL
  92. * @access private
  93. */
  94. function _resolveRelativeURL($url)
  95. {
  96. $https = !empty($_SERVER['HTTPS']) && ('off' != strtolower($_SERVER['HTTPS']));
  97. $scheme = ($https? 'https:': 'http:');
  98. if ('//' == substr($url, 0, 2)) {
  99. return $scheme . $url;
  100. } else {
  101. $host = $scheme . '//' . $_SERVER['SERVER_NAME'] .
  102. (($https && 443 == $_SERVER['SERVER_PORT'] ||
  103. !$https && 80 == $_SERVER['SERVER_PORT'])? '': ':' . $_SERVER['SERVER_PORT']);
  104. if ('' == $url) {
  105. return $host . $_SERVER['REQUEST_URI'];
  106. } elseif ('/' == $url[0]) {
  107. return $host . $url;
  108. } else {
  109. list($basePath, $baseQuery) = $this->_splitUri($_SERVER['REQUEST_URI']);
  110. list($actPath, $actQuery) = $this->_splitUri($url);
  111. if ('' == $actPath) {
  112. return $host . $basePath . $actQuery;
  113. } else {
  114. $path = substr($basePath, 0, strrpos($basePath, '/') + 1) . $actPath;
  115. return $host . $this->_normalizePath($path) . $actQuery;
  116. }
  117. }
  118. }
  119. }
  120. function perform(&$page, $actionName)
  121. {
  122. // check whether the page is valid before trying to go to it
  123. if ($page->controller->isModal()) {
  124. // we check whether *all* pages up to current are valid
  125. // if there is an invalid page we go to it, instead of the
  126. // requested one
  127. $pageName = $page->getAttribute('id');
  128. if (!$page->controller->isValid($pageName)) {
  129. $pageName = $page->controller->findInvalid();
  130. }
  131. $current =& $page->controller->getPage($pageName);
  132. } else {
  133. $current =& $page;
  134. }
  135. // generate the URL for the page 'display' event and redirect to it
  136. $action = $current->getAttribute('action');
  137. // Bug #13087: RFC 2616 requires an absolute URI in Location header
  138. if (!preg_match('!^https?://!i', $action)) {
  139. $action = $this->_resolveRelativeURL($action);
  140. }
  141. $url = $action . (false === strpos($action, '?')? '?': '&') .
  142. $current->getButtonName('display') . '=true' .
  143. ((!defined('SID') || '' == SID || ini_get('session.use_only_cookies'))? '': '&' . SID);
  144. header('Location: ' . $url);
  145. exit;
  146. }
  147. }
  148. ?>