Jump.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * This action performs HTTP redirect to a specific page.
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm_Controller
  27. * @author Alexey Borzov <avb@php.net>
  28. * @version Release: 1.0.10
  29. */
  30. class HTML_QuickForm_Action_Jump extends HTML_QuickForm_Action
  31. {
  32. /**
  33. * Splits (part of) the URI into path and query components
  34. *
  35. * @param string String of the form 'foo?bar'
  36. * @return array Array of the form array('foo', '?bar)
  37. * @access private
  38. */
  39. function _splitUri($uri)
  40. {
  41. if (false === ($qm = strpos($uri, '?'))) {
  42. return array($uri, '');
  43. } else {
  44. return array(substr($uri, 0, $qm), substr($uri, $qm));
  45. }
  46. }
  47. /**
  48. * Removes the '..' and '.' segments from the path component
  49. *
  50. * @param string Path component of the URL, possibly with '.' and '..' segments
  51. * @return string Path component of the URL with '.' and '..' segments removed
  52. * @access private
  53. */
  54. function _normalizePath($path)
  55. {
  56. $pathAry = explode('/', $path);
  57. $i = 1;
  58. do {
  59. if ('.' == $pathAry[$i]) {
  60. if ($i < count($pathAry) - 1) {
  61. array_splice($pathAry, $i, 1);
  62. } else {
  63. $pathAry[$i] = '';
  64. $i++;
  65. }
  66. } elseif ('..' == $pathAry[$i] && $i > 1 && '..' != $pathAry[$i - 1]) {
  67. if ($i < count($pathAry) -1) {
  68. array_splice($pathAry, $i - 1, 2);
  69. $i--;
  70. } else {
  71. array_splice($pathAry, $i - 1, 2, '');
  72. }
  73. } else {
  74. $i++;
  75. }
  76. } while ($i < count($pathAry));
  77. return implode('/', $pathAry);
  78. }
  79. /**
  80. * Resolves relative URL using current page's URL as base
  81. *
  82. * The method follows procedure described in section 4 of RFC 1808 and
  83. * passes the examples provided in section 5 of said RFC. Values from
  84. * $_SERVER array are used for calculation of "current URL"
  85. *
  86. * @param string Relative URL, probably from form's action attribute
  87. * @return string Absolute URL
  88. * @access private
  89. */
  90. function _resolveRelativeURL($url)
  91. {
  92. $https = !empty($_SERVER['HTTPS']) && ('off' != strtolower($_SERVER['HTTPS']));
  93. $scheme = ($https? 'https:': 'http:');
  94. if ('//' == substr($url, 0, 2)) {
  95. return $scheme . $url;
  96. } else {
  97. $host = $scheme . '//' . $_SERVER['SERVER_NAME'] .
  98. (($https && 443 == $_SERVER['SERVER_PORT'] ||
  99. !$https && 80 == $_SERVER['SERVER_PORT'])? '': ':' . $_SERVER['SERVER_PORT']);
  100. if ('' == $url) {
  101. return $host . $_SERVER['REQUEST_URI'];
  102. } elseif ('/' == $url[0]) {
  103. return $host . $url;
  104. } else {
  105. list($basePath, $baseQuery) = $this->_splitUri($_SERVER['REQUEST_URI']);
  106. list($actPath, $actQuery) = $this->_splitUri($url);
  107. if ('' == $actPath) {
  108. return $host . $basePath . $actQuery;
  109. } else {
  110. $path = substr($basePath, 0, strrpos($basePath, '/') + 1) . $actPath;
  111. return $host . $this->_normalizePath($path) . $actQuery;
  112. }
  113. }
  114. }
  115. }
  116. function perform(&$page, $actionName)
  117. {
  118. // check whether the page is valid before trying to go to it
  119. if ($page->controller->isModal()) {
  120. // we check whether *all* pages up to current are valid
  121. // if there is an invalid page we go to it, instead of the
  122. // requested one
  123. $pageName = $page->getAttribute('id');
  124. if (!$page->controller->isValid($pageName)) {
  125. $pageName = $page->controller->findInvalid();
  126. }
  127. $current =& $page->controller->getPage($pageName);
  128. } else {
  129. $current =& $page;
  130. }
  131. // generate the URL for the page 'display' event and redirect to it
  132. $action = $current->getAttribute('action');
  133. // Bug #13087: RFC 2616 requires an absolute URI in Location header
  134. if (!preg_match('!^https?://!i', $action)) {
  135. $action = $this->_resolveRelativeURL($action);
  136. }
  137. $url = $action . (false === strpos($action, '?')? '?': '&') .
  138. $current->getButtonName('display') . '=true' .
  139. ((!defined('SID') || '' == SID || ini_get('session.use_only_cookies'))? '': '&' . SID);
  140. header('Location: ' . $url);
  141. exit;
  142. }
  143. }
  144. ?>