<?php
function redirect($url = '', array $params = array()) {
// スキーム省略に対応
$url = preg_replace_callback(
'@^/{2}(?!/)@',
function ($matches) {
return
empty($_SERVER['HTTPS']) || strcasecmp($_SERVER['HTTPS'], 'off') ?
'http://' :
'https://'
;
},
$url
);
// URLをパースする
if (!$p = parse_url($url)) {
throw new BadMethodCallException('URLのパースに失敗しました');
}
// 相対URLで書かれた場合には絶対URLに補正して再パースさせる
if (!isset($p['host'])) {
$url = rtrim($GLOBALS['SITE_URL'], '/') . '/' . ltrim($url, '/');
if (!$p = parse_url($url) or !isset($p['host']) or !isset($p['scheme'])) {
throw new BadMethodCallException('URLのパースに失敗しました');
}
}
// クエリ―ストリングをパースして連想配列にする
parse_str(isset($p['query']) ? $p['query'] : '', $query);
// URLを組み立てていく
$user_and_pass[] = isset($p['user']) ? $p['user'] : null;
$user_and_pass[] = isset($p['pass']) ? $p['pass'] : null;
$host_and_port[] = $p['host'];
$host_and_port[] = isset($p['port']) ? $p['port'] : null;
$server[] = implode(':', array_filter($user_and_pass, 'is_scalar'));
$server[] = implode(':', array_filter($host_and_port, 'is_scalar'));
$server_and_path[] = implode('@', array_filter($server, 'strlen'));
$server_and_path[] = isset($p['path']) ? ltrim($p['path'], '/') : '';
$sp_and_query[] = implode('/', $server_and_path);
$sp_and_query[] = http_build_query($params + $query, '', '&');
$spq_and_fragment[] = implode('?', array_filter($sp_and_query, 'strlen'));
$spq_and_fragment[] = isset($p['fragment']) ? $p['fragment'] : null;
$scheme_and_spqf[] = $p['scheme'];
$scheme_and_spqf[] = implode('#', array_filter($spq_and_fragment, 'is_scalar'));
$url = implode('://', $scheme_and_spqf);
// 改行コードやNULLバイトを削除する
$url = str_replace(array("\r", "\n", "\0"), '', $url);
return $url;
}
$SITE_URL = 'http://user:' . "\n\0\n" . 'pass@exam' . "\n" . 'ple.com:87/p'."\n".'ublic';
var_dump(redirect('//test.php?a=b'));
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- string(20) "http://test.php/?a=b"
- Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected T_FUNCTION in /in/uDsnE on line 7
Process exited with code 255. - Output for 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/uDsnE on line 3
Process exited with code 255. - Output for 4.4.2 - 4.4.9
- Parse error: syntax error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/uDsnE on line 3
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
- Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE or T_CONST in /in/uDsnE on line 3
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error, expecting `'&'' or `T_VARIABLE' or `T_CONST' in /in/uDsnE on line 3
Process exited with code 255.
preferences:
268.45 ms | 401 KiB | 400 Q