Back.php
6 years ago
Direct.php
6 years ago
Display.php
6 years ago
Jump.php
6 years ago
Next.php
6 years ago
Submit.php
6 years ago
Jump.php
210 lines
| 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-2010, 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 294039 2010-01-26 12:29:46Z avb $ |
| 43 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 44 | */ |
| 45 | |
| 46 | /** Interface for Controller action handlers */ |
| 47 | // require_once 'HTML/QuickForm2/Controller/Action.php'; |
| 48 | |
| 49 | /** |
| 50 | * This handler performs an HTTP redirect to a specific page |
| 51 | * |
| 52 | * @category HTML |
| 53 | * @package HTML_QuickForm2 |
| 54 | * @author Alexey Borzov <avb@php.net> |
| 55 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 56 | * @version Release: @package_version@ |
| 57 | */ |
| 58 | class HTML_QuickForm2_Controller_Action_Jump |
| 59 | implements HTML_QuickForm2_Controller_Action |
| 60 | { |
| 61 | /** |
| 62 | * Splits (part of) the URI into path and query components |
| 63 | * |
| 64 | * @param string String of the form 'foo?bar' |
| 65 | * @return array Array of the form array('foo', '?bar) |
| 66 | */ |
| 67 | protected static function splitUri($uri) |
| 68 | { |
| 69 | if (false === ($qm = strpos($uri, '?'))) { |
| 70 | return array($uri, ''); |
| 71 | } else { |
| 72 | return array(substr($uri, 0, $qm), substr($uri, $qm)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Removes the '..' and '.' segments from the path component |
| 78 | * |
| 79 | * @param string Path component of the URL, possibly with '.' and '..' segments |
| 80 | * @return string Path component of the URL with '.' and '..' segments removed |
| 81 | */ |
| 82 | protected static function normalizePath($path) |
| 83 | { |
| 84 | $pathAry = explode('/', $path); |
| 85 | $i = 1; |
| 86 | |
| 87 | do { |
| 88 | if ('.' == $pathAry[$i]) { |
| 89 | if ($i < count($pathAry) - 1) { |
| 90 | array_splice($pathAry, $i, 1); |
| 91 | } else { |
| 92 | $pathAry[$i] = ''; |
| 93 | $i++; |
| 94 | } |
| 95 | |
| 96 | } elseif ('..' == $pathAry[$i]) { |
| 97 | if (1 == $i) { |
| 98 | array_splice($pathAry, 1, 1); |
| 99 | |
| 100 | } elseif ('..' != $pathAry[$i - 1]) { |
| 101 | if ($i < count($pathAry) - 1) { |
| 102 | array_splice($pathAry, $i - 1, 2); |
| 103 | $i--; |
| 104 | } else { |
| 105 | array_splice($pathAry, $i - 1, 2, ''); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | } else { |
| 110 | $i++; |
| 111 | } |
| 112 | } while ($i < count($pathAry)); |
| 113 | |
| 114 | return implode('/', $pathAry); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Resolves relative URL using current page's URL as base |
| 119 | * |
| 120 | * The method follows procedure described in section 4 of RFC 1808 and |
| 121 | * passes the examples provided in section 5 of said RFC. Values from |
| 122 | * $_SERVER array are used for calculation of "current URL" |
| 123 | * |
| 124 | * @param string Relative URL, probably from form's action attribute |
| 125 | * @return string Absolute URL |
| 126 | */ |
| 127 | protected static function resolveRelativeURL($url) |
| 128 | { |
| 129 | $https = !empty($_SERVER['HTTPS']) && ('off' != strtolower($_SERVER['HTTPS'])); |
| 130 | $scheme = ($https? 'https:': 'http:'); |
| 131 | if ('//' == substr($url, 0, 2)) { |
| 132 | return $scheme . $url; |
| 133 | |
| 134 | } else { |
| 135 | $host = $scheme . '//' . $_SERVER['SERVER_NAME'] . |
| 136 | (($https && 443 == $_SERVER['SERVER_PORT'] || |
| 137 | !$https && 80 == $_SERVER['SERVER_PORT'])? '': ':' . $_SERVER['SERVER_PORT']); |
| 138 | if ('' == $url) { |
| 139 | return $host . $_SERVER['REQUEST_URI']; |
| 140 | |
| 141 | } elseif ('/' == $url[0]) { |
| 142 | list($actPath, $actQuery) = self::splitUri($url); |
| 143 | return $host . self::normalizePath($actPath) . $actQuery; |
| 144 | |
| 145 | } else { |
| 146 | list($basePath, $baseQuery) = self::splitUri($_SERVER['REQUEST_URI']); |
| 147 | list($actPath, $actQuery) = self::splitUri($url); |
| 148 | if ('' == $actPath) { |
| 149 | return $host . $basePath . $actQuery; |
| 150 | } else { |
| 151 | $path = substr($basePath, 0, strrpos($basePath, '/') + 1) . $actPath; |
| 152 | return $host . self::normalizePath($path) . $actQuery; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | public function perform(HTML_QuickForm2_Controller_Page $page, $name) |
| 159 | { |
| 160 | // we check whether *all* pages up to current are valid |
| 161 | // if there is an invalid page we go to it, instead of the |
| 162 | // requested one |
| 163 | if ($page->getController()->isWizard() |
| 164 | && !$page->getController()->isValid($page) |
| 165 | ) { |
| 166 | $page = $page->getController()->getFirstInvalidPage(); |
| 167 | } |
| 168 | |
| 169 | // generate the URL for the page 'display' event and redirect to it |
| 170 | $action = $page->getForm()->getAttribute('action'); |
| 171 | // Bug #13087: RFC 2616 requires an absolute URI in Location header |
| 172 | if (!preg_match('@^([a-z][a-z0-9.+-]*):@i', $action)) { |
| 173 | $action = self::resolveRelativeURL($action); |
| 174 | } |
| 175 | |
| 176 | if (!$page->getController()->propagateId()) { |
| 177 | $controllerId = ''; |
| 178 | } else { |
| 179 | $controllerId = '&' . HTML_QuickForm2_Controller::KEY_ID . '=' . |
| 180 | $page->getController()->getId(); |
| 181 | } |
| 182 | if (!defined('SID') || '' == SID || ini_get('session.use_only_cookies')) { |
| 183 | $sessionId = ''; |
| 184 | } else { |
| 185 | $sessionId = '&' . SID; |
| 186 | } |
| 187 | |
| 188 | return $this->doRedirect( |
| 189 | $action . (false === strpos($action, '?')? '?': '&') . |
| 190 | $page->getButtonName('display') . '=true' . $controllerId . $sessionId |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Redirects to a given URL via Location: header and exits the script |
| 197 | * |
| 198 | * A separate method is mostly needed for creating mocks of this class |
| 199 | * during testing. |
| 200 | * |
| 201 | * @param string URL to redirect to |
| 202 | */ |
| 203 | protected function doRedirect($url) |
| 204 | { |
| 205 | header('Location: ' . $url); |
| 206 | exit; |
| 207 | } |
| 208 | } |
| 209 | ?> |
| 210 |