PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/system/library/PEAR/HTML/QuickForm2/Controller/Action/Jump.php

https://bitbucket.org/spekkionu/passworddb
PHP | 246 lines | 121 code | 20 blank | 105 comment | 45 complexity | ffb017d319036dd5188ca3bb3eb77041 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * This handler performs an HTTP redirect to a specific page
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2012, Alexey Borzov <avb@php.net>,
  10. * Bertrand Mansion <golgote@mamasam.com>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm2
  39. * @author Alexey Borzov <avb@php.net>
  40. * @author Bertrand Mansion <golgote@mamasam.com>
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version SVN: $Id: Jump.php 323441 2012-02-23 12:27:59Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /** Interface for Controller action handlers */
  46. require_once 'HTML/QuickForm2/Controller/Action.php';
  47. /**
  48. * This handler performs an HTTP redirect to a specific page
  49. *
  50. * @category HTML
  51. * @package HTML_QuickForm2
  52. * @author Alexey Borzov <avb@php.net>
  53. * @author Bertrand Mansion <golgote@mamasam.com>
  54. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  55. * @version Release: 2.0.0
  56. * @link http://pear.php.net/package/HTML_QuickForm2
  57. */
  58. class HTML_QuickForm2_Controller_Action_Jump
  59. implements HTML_QuickForm2_Controller_Action
  60. {
  61. /**
  62. * Whether to trust $_SERVER entries coming from proxies
  63. * @var bool
  64. */
  65. protected $trustProxy = false;
  66. /**
  67. * Splits (part of) the URI into path and query components
  68. *
  69. * @param string $uri String of the form 'foo?bar'
  70. *
  71. * @return array Array of the form array('foo', '?bar)
  72. */
  73. protected static function splitUri($uri)
  74. {
  75. if (false === ($qm = strpos($uri, '?'))) {
  76. return array($uri, '');
  77. } else {
  78. return array(substr($uri, 0, $qm), substr($uri, $qm));
  79. }
  80. }
  81. /**
  82. * Removes the '..' and '.' segments from the path component
  83. *
  84. * @param string $path Path component of the URL, possibly with '.' and '..' segments
  85. *
  86. * @return string Path component of the URL with '.' and '..' segments removed
  87. */
  88. protected static function normalizePath($path)
  89. {
  90. $pathAry = explode('/', $path);
  91. $i = 1;
  92. do {
  93. if ('.' == $pathAry[$i]) {
  94. if ($i < count($pathAry) - 1) {
  95. array_splice($pathAry, $i, 1);
  96. } else {
  97. $pathAry[$i] = '';
  98. $i++;
  99. }
  100. } elseif ('..' == $pathAry[$i]) {
  101. if (1 == $i) {
  102. array_splice($pathAry, 1, 1);
  103. } elseif ('..' != $pathAry[$i - 1]) {
  104. if ($i < count($pathAry) - 1) {
  105. array_splice($pathAry, $i - 1, 2);
  106. $i--;
  107. } else {
  108. array_splice($pathAry, $i - 1, 2, '');
  109. }
  110. }
  111. } else {
  112. $i++;
  113. }
  114. } while ($i < count($pathAry));
  115. return implode('/', $pathAry);
  116. }
  117. /**
  118. * Constructor, sets $trustProxy flag
  119. *
  120. * @param bool $trustProxy Whether to trust $_SERVER entries
  121. * (specifically HTTP_X_FORWARDED_HOST) coming from proxies.
  122. * Should be set to true if application is hosted behind a
  123. * reverse proxy that you manage.
  124. */
  125. public function __construct($trustProxy = false)
  126. {
  127. $this->trustProxy = (bool)$trustProxy;
  128. }
  129. /**
  130. * Resolves relative URL using current page's URL as base
  131. *
  132. * The method follows procedure described in section 4 of RFC 1808 and
  133. * passes the examples provided in section 5 of said RFC. Values from
  134. * $_SERVER array are used for calculation of "current URL"
  135. *
  136. * @param string $url Relative URL, probably from form's action attribute
  137. *
  138. * @return string Absolute URL
  139. */
  140. protected function resolveRelativeURL($url)
  141. {
  142. $https = !empty($_SERVER['HTTPS']) && ('off' != strtolower($_SERVER['HTTPS']));
  143. $scheme = ($https? 'https:': 'http:');
  144. if ('//' == substr($url, 0, 2)) {
  145. return $scheme . $url;
  146. } else {
  147. if ($this->trustProxy && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  148. $parts = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
  149. $host = trim(end($parts));
  150. } else {
  151. $host = '';
  152. foreach (array('HTTP_HOST', 'SERVER_NAME', 'SERVER_ADDR') as $key) {
  153. if (!empty($_SERVER[$key])) {
  154. $host = $_SERVER[$key];
  155. break;
  156. }
  157. }
  158. }
  159. $host = $scheme . '//' . preg_replace('/:\d+$/', '', $host)
  160. . (($https && 443 == $_SERVER['SERVER_PORT']
  161. || !$https && 80 == $_SERVER['SERVER_PORT'])
  162. ? '' : ':' . $_SERVER['SERVER_PORT']);
  163. if ('' == $url) {
  164. return $host . $_SERVER['REQUEST_URI'];
  165. } elseif ('/' == $url[0]) {
  166. list($actPath, $actQuery) = self::splitUri($url);
  167. return $host . self::normalizePath($actPath) . $actQuery;
  168. } else {
  169. list($basePath, $baseQuery) = self::splitUri($_SERVER['REQUEST_URI']);
  170. list($actPath, $actQuery) = self::splitUri($url);
  171. if ('' == $actPath) {
  172. return $host . $basePath . $actQuery;
  173. } else {
  174. $path = substr($basePath, 0, strrpos($basePath, '/') + 1) . $actPath;
  175. return $host . self::normalizePath($path) . $actQuery;
  176. }
  177. }
  178. }
  179. }
  180. public function perform(HTML_QuickForm2_Controller_Page $page, $name)
  181. {
  182. // we check whether *all* pages up to current are valid
  183. // if there is an invalid page we go to it, instead of the
  184. // requested one
  185. if ($page->getController()->isWizard()
  186. && !$page->getController()->isValid($page)
  187. ) {
  188. $page = $page->getController()->getFirstInvalidPage();
  189. }
  190. // generate the URL for the page 'display' event and redirect to it
  191. $action = $page->getForm()->getAttribute('action');
  192. // Bug #13087: RFC 2616 requires an absolute URI in Location header
  193. if (!preg_match('@^([a-z][a-z0-9.+-]*):@i', $action)) {
  194. $action = $this->resolveRelativeURL($action);
  195. }
  196. if (!$page->getController()->propagateId()) {
  197. $controllerId = '';
  198. } else {
  199. $controllerId = '&' . HTML_QuickForm2_Controller::KEY_ID . '=' .
  200. $page->getController()->getId();
  201. }
  202. if (!defined('SID') || '' == SID || ini_get('session.use_only_cookies')) {
  203. $sessionId = '';
  204. } else {
  205. $sessionId = '&' . SID;
  206. }
  207. return $this->doRedirect(
  208. $action . (false === strpos($action, '?')? '?': '&') .
  209. $page->getButtonName('display') . '=true' . $controllerId . $sessionId
  210. );
  211. }
  212. /**
  213. * Redirects to a given URL via Location: header and exits the script
  214. *
  215. * A separate method is mostly needed for creating mocks of this class
  216. * during testing.
  217. *
  218. * @param string $url URL to redirect to
  219. */
  220. protected function doRedirect($url)
  221. {
  222. header('Location: ' . $url);
  223. exit;
  224. }
  225. }
  226. ?>