Edit File by line
/home/barbar84/public_h.../wp-inclu.../Requests
File: IRI.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* IRI parser/serialiser/normaliser
[2] Fix | Delete
*
[3] Fix | Delete
* @package Requests
[4] Fix | Delete
* @subpackage Utilities
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* IRI parser/serialiser/normaliser
[9] Fix | Delete
*
[10] Fix | Delete
* Copyright (c) 2007-2010, Geoffrey Sneddon and Steve Minutillo.
[11] Fix | Delete
* All rights reserved.
[12] Fix | Delete
*
[13] Fix | Delete
* Redistribution and use in source and binary forms, with or without
[14] Fix | Delete
* modification, are permitted provided that the following conditions are met:
[15] Fix | Delete
*
[16] Fix | Delete
* * Redistributions of source code must retain the above copyright notice,
[17] Fix | Delete
* this list of conditions and the following disclaimer.
[18] Fix | Delete
*
[19] Fix | Delete
* * Redistributions in binary form must reproduce the above copyright notice,
[20] Fix | Delete
* this list of conditions and the following disclaimer in the documentation
[21] Fix | Delete
* and/or other materials provided with the distribution.
[22] Fix | Delete
*
[23] Fix | Delete
* * Neither the name of the SimplePie Team nor the names of its contributors
[24] Fix | Delete
* may be used to endorse or promote products derived from this software
[25] Fix | Delete
* without specific prior written permission.
[26] Fix | Delete
*
[27] Fix | Delete
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
[28] Fix | Delete
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
[29] Fix | Delete
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
[30] Fix | Delete
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
[31] Fix | Delete
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
[32] Fix | Delete
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
[33] Fix | Delete
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
[34] Fix | Delete
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
[35] Fix | Delete
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
[36] Fix | Delete
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
[37] Fix | Delete
* POSSIBILITY OF SUCH DAMAGE.
[38] Fix | Delete
*
[39] Fix | Delete
* @package Requests
[40] Fix | Delete
* @subpackage Utilities
[41] Fix | Delete
* @author Geoffrey Sneddon
[42] Fix | Delete
* @author Steve Minutillo
[43] Fix | Delete
* @copyright 2007-2009 Geoffrey Sneddon and Steve Minutillo
[44] Fix | Delete
* @license http://www.opensource.org/licenses/bsd-license.php
[45] Fix | Delete
* @link http://hg.gsnedders.com/iri/
[46] Fix | Delete
*
[47] Fix | Delete
* @property string $iri IRI we're working with
[48] Fix | Delete
* @property-read string $uri IRI in URI form, {@see to_uri}
[49] Fix | Delete
* @property string $scheme Scheme part of the IRI
[50] Fix | Delete
* @property string $authority Authority part, formatted for a URI (userinfo + host + port)
[51] Fix | Delete
* @property string $iauthority Authority part of the IRI (userinfo + host + port)
[52] Fix | Delete
* @property string $userinfo Userinfo part, formatted for a URI (after '://' and before '@')
[53] Fix | Delete
* @property string $iuserinfo Userinfo part of the IRI (after '://' and before '@')
[54] Fix | Delete
* @property string $host Host part, formatted for a URI
[55] Fix | Delete
* @property string $ihost Host part of the IRI
[56] Fix | Delete
* @property string $port Port part of the IRI (after ':')
[57] Fix | Delete
* @property string $path Path part, formatted for a URI (after first '/')
[58] Fix | Delete
* @property string $ipath Path part of the IRI (after first '/')
[59] Fix | Delete
* @property string $query Query part, formatted for a URI (after '?')
[60] Fix | Delete
* @property string $iquery Query part of the IRI (after '?')
[61] Fix | Delete
* @property string $fragment Fragment, formatted for a URI (after '#')
[62] Fix | Delete
* @property string $ifragment Fragment part of the IRI (after '#')
[63] Fix | Delete
*/
[64] Fix | Delete
class Requests_IRI {
[65] Fix | Delete
/**
[66] Fix | Delete
* Scheme
[67] Fix | Delete
*
[68] Fix | Delete
* @var string
[69] Fix | Delete
*/
[70] Fix | Delete
protected $scheme = null;
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* User Information
[74] Fix | Delete
*
[75] Fix | Delete
* @var string
[76] Fix | Delete
*/
[77] Fix | Delete
protected $iuserinfo = null;
[78] Fix | Delete
[79] Fix | Delete
/**
[80] Fix | Delete
* ihost
[81] Fix | Delete
*
[82] Fix | Delete
* @var string
[83] Fix | Delete
*/
[84] Fix | Delete
protected $ihost = null;
[85] Fix | Delete
[86] Fix | Delete
/**
[87] Fix | Delete
* Port
[88] Fix | Delete
*
[89] Fix | Delete
* @var string
[90] Fix | Delete
*/
[91] Fix | Delete
protected $port = null;
[92] Fix | Delete
[93] Fix | Delete
/**
[94] Fix | Delete
* ipath
[95] Fix | Delete
*
[96] Fix | Delete
* @var string
[97] Fix | Delete
*/
[98] Fix | Delete
protected $ipath = '';
[99] Fix | Delete
[100] Fix | Delete
/**
[101] Fix | Delete
* iquery
[102] Fix | Delete
*
[103] Fix | Delete
* @var string
[104] Fix | Delete
*/
[105] Fix | Delete
protected $iquery = null;
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* ifragment
[109] Fix | Delete
*
[110] Fix | Delete
* @var string
[111] Fix | Delete
*/
[112] Fix | Delete
protected $ifragment = null;
[113] Fix | Delete
[114] Fix | Delete
/**
[115] Fix | Delete
* Normalization database
[116] Fix | Delete
*
[117] Fix | Delete
* Each key is the scheme, each value is an array with each key as the IRI
[118] Fix | Delete
* part and value as the default value for that part.
[119] Fix | Delete
*/
[120] Fix | Delete
protected $normalization = array(
[121] Fix | Delete
'acap' => array(
[122] Fix | Delete
'port' => 674
[123] Fix | Delete
),
[124] Fix | Delete
'dict' => array(
[125] Fix | Delete
'port' => 2628
[126] Fix | Delete
),
[127] Fix | Delete
'file' => array(
[128] Fix | Delete
'ihost' => 'localhost'
[129] Fix | Delete
),
[130] Fix | Delete
'http' => array(
[131] Fix | Delete
'port' => 80,
[132] Fix | Delete
),
[133] Fix | Delete
'https' => array(
[134] Fix | Delete
'port' => 443,
[135] Fix | Delete
),
[136] Fix | Delete
);
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Return the entire IRI when you try and read the object as a string
[140] Fix | Delete
*
[141] Fix | Delete
* @return string
[142] Fix | Delete
*/
[143] Fix | Delete
public function __toString() {
[144] Fix | Delete
return $this->get_iri();
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Overload __set() to provide access via properties
[149] Fix | Delete
*
[150] Fix | Delete
* @param string $name Property name
[151] Fix | Delete
* @param mixed $value Property value
[152] Fix | Delete
*/
[153] Fix | Delete
public function __set($name, $value) {
[154] Fix | Delete
if (method_exists($this, 'set_' . $name)) {
[155] Fix | Delete
call_user_func(array($this, 'set_' . $name), $value);
[156] Fix | Delete
}
[157] Fix | Delete
elseif (
[158] Fix | Delete
$name === 'iauthority'
[159] Fix | Delete
|| $name === 'iuserinfo'
[160] Fix | Delete
|| $name === 'ihost'
[161] Fix | Delete
|| $name === 'ipath'
[162] Fix | Delete
|| $name === 'iquery'
[163] Fix | Delete
|| $name === 'ifragment'
[164] Fix | Delete
) {
[165] Fix | Delete
call_user_func(array($this, 'set_' . substr($name, 1)), $value);
[166] Fix | Delete
}
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
/**
[170] Fix | Delete
* Overload __get() to provide access via properties
[171] Fix | Delete
*
[172] Fix | Delete
* @param string $name Property name
[173] Fix | Delete
* @return mixed
[174] Fix | Delete
*/
[175] Fix | Delete
public function __get($name) {
[176] Fix | Delete
// isset() returns false for null, we don't want to do that
[177] Fix | Delete
// Also why we use array_key_exists below instead of isset()
[178] Fix | Delete
$props = get_object_vars($this);
[179] Fix | Delete
[180] Fix | Delete
if (
[181] Fix | Delete
$name === 'iri' ||
[182] Fix | Delete
$name === 'uri' ||
[183] Fix | Delete
$name === 'iauthority' ||
[184] Fix | Delete
$name === 'authority'
[185] Fix | Delete
) {
[186] Fix | Delete
$method = 'get_' . $name;
[187] Fix | Delete
$return = $this->$method();
[188] Fix | Delete
}
[189] Fix | Delete
elseif (array_key_exists($name, $props)) {
[190] Fix | Delete
$return = $this->$name;
[191] Fix | Delete
}
[192] Fix | Delete
// host -> ihost
[193] Fix | Delete
elseif (($prop = 'i' . $name) && array_key_exists($prop, $props)) {
[194] Fix | Delete
$name = $prop;
[195] Fix | Delete
$return = $this->$prop;
[196] Fix | Delete
}
[197] Fix | Delete
// ischeme -> scheme
[198] Fix | Delete
elseif (($prop = substr($name, 1)) && array_key_exists($prop, $props)) {
[199] Fix | Delete
$name = $prop;
[200] Fix | Delete
$return = $this->$prop;
[201] Fix | Delete
}
[202] Fix | Delete
else {
[203] Fix | Delete
trigger_error('Undefined property: ' . get_class($this) . '::' . $name, E_USER_NOTICE);
[204] Fix | Delete
$return = null;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
if ($return === null && isset($this->normalization[$this->scheme][$name])) {
[208] Fix | Delete
return $this->normalization[$this->scheme][$name];
[209] Fix | Delete
}
[210] Fix | Delete
else {
[211] Fix | Delete
return $return;
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* Overload __isset() to provide access via properties
[217] Fix | Delete
*
[218] Fix | Delete
* @param string $name Property name
[219] Fix | Delete
* @return bool
[220] Fix | Delete
*/
[221] Fix | Delete
public function __isset($name) {
[222] Fix | Delete
return (method_exists($this, 'get_' . $name) || isset($this->$name));
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
/**
[226] Fix | Delete
* Overload __unset() to provide access via properties
[227] Fix | Delete
*
[228] Fix | Delete
* @param string $name Property name
[229] Fix | Delete
*/
[230] Fix | Delete
public function __unset($name) {
[231] Fix | Delete
if (method_exists($this, 'set_' . $name)) {
[232] Fix | Delete
call_user_func(array($this, 'set_' . $name), '');
[233] Fix | Delete
}
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Create a new IRI object, from a specified string
[238] Fix | Delete
*
[239] Fix | Delete
* @param string|null $iri
[240] Fix | Delete
*/
[241] Fix | Delete
public function __construct($iri = null) {
[242] Fix | Delete
$this->set_iri($iri);
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
/**
[246] Fix | Delete
* Create a new IRI object by resolving a relative IRI
[247] Fix | Delete
*
[248] Fix | Delete
* Returns false if $base is not absolute, otherwise an IRI.
[249] Fix | Delete
*
[250] Fix | Delete
* @param IRI|string $base (Absolute) Base IRI
[251] Fix | Delete
* @param IRI|string $relative Relative IRI
[252] Fix | Delete
* @return IRI|false
[253] Fix | Delete
*/
[254] Fix | Delete
public static function absolutize($base, $relative) {
[255] Fix | Delete
if (!($relative instanceof Requests_IRI)) {
[256] Fix | Delete
$relative = new Requests_IRI($relative);
[257] Fix | Delete
}
[258] Fix | Delete
if (!$relative->is_valid()) {
[259] Fix | Delete
return false;
[260] Fix | Delete
}
[261] Fix | Delete
elseif ($relative->scheme !== null) {
[262] Fix | Delete
return clone $relative;
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
if (!($base instanceof Requests_IRI)) {
[266] Fix | Delete
$base = new Requests_IRI($base);
[267] Fix | Delete
}
[268] Fix | Delete
if ($base->scheme === null || !$base->is_valid()) {
[269] Fix | Delete
return false;
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
if ($relative->get_iri() !== '') {
[273] Fix | Delete
if ($relative->iuserinfo !== null || $relative->ihost !== null || $relative->port !== null) {
[274] Fix | Delete
$target = clone $relative;
[275] Fix | Delete
$target->scheme = $base->scheme;
[276] Fix | Delete
}
[277] Fix | Delete
else {
[278] Fix | Delete
$target = new Requests_IRI;
[279] Fix | Delete
$target->scheme = $base->scheme;
[280] Fix | Delete
$target->iuserinfo = $base->iuserinfo;
[281] Fix | Delete
$target->ihost = $base->ihost;
[282] Fix | Delete
$target->port = $base->port;
[283] Fix | Delete
if ($relative->ipath !== '') {
[284] Fix | Delete
if ($relative->ipath[0] === '/') {
[285] Fix | Delete
$target->ipath = $relative->ipath;
[286] Fix | Delete
}
[287] Fix | Delete
elseif (($base->iuserinfo !== null || $base->ihost !== null || $base->port !== null) && $base->ipath === '') {
[288] Fix | Delete
$target->ipath = '/' . $relative->ipath;
[289] Fix | Delete
}
[290] Fix | Delete
elseif (($last_segment = strrpos($base->ipath, '/')) !== false) {
[291] Fix | Delete
$target->ipath = substr($base->ipath, 0, $last_segment + 1) . $relative->ipath;
[292] Fix | Delete
}
[293] Fix | Delete
else {
[294] Fix | Delete
$target->ipath = $relative->ipath;
[295] Fix | Delete
}
[296] Fix | Delete
$target->ipath = $target->remove_dot_segments($target->ipath);
[297] Fix | Delete
$target->iquery = $relative->iquery;
[298] Fix | Delete
}
[299] Fix | Delete
else {
[300] Fix | Delete
$target->ipath = $base->ipath;
[301] Fix | Delete
if ($relative->iquery !== null) {
[302] Fix | Delete
$target->iquery = $relative->iquery;
[303] Fix | Delete
}
[304] Fix | Delete
elseif ($base->iquery !== null) {
[305] Fix | Delete
$target->iquery = $base->iquery;
[306] Fix | Delete
}
[307] Fix | Delete
}
[308] Fix | Delete
$target->ifragment = $relative->ifragment;
[309] Fix | Delete
}
[310] Fix | Delete
}
[311] Fix | Delete
else {
[312] Fix | Delete
$target = clone $base;
[313] Fix | Delete
$target->ifragment = null;
[314] Fix | Delete
}
[315] Fix | Delete
$target->scheme_normalization();
[316] Fix | Delete
return $target;
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
/**
[320] Fix | Delete
* Parse an IRI into scheme/authority/path/query/fragment segments
[321] Fix | Delete
*
[322] Fix | Delete
* @param string $iri
[323] Fix | Delete
* @return array
[324] Fix | Delete
*/
[325] Fix | Delete
protected function parse_iri($iri) {
[326] Fix | Delete
$iri = trim($iri, "\x20\x09\x0A\x0C\x0D");
[327] Fix | Delete
$has_match = preg_match('/^((?P<scheme>[^:\/?#]+):)?(\/\/(?P<authority>[^\/?#]*))?(?P<path>[^?#]*)(\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$/', $iri, $match);
[328] Fix | Delete
if (!$has_match) {
[329] Fix | Delete
throw new Requests_Exception('Cannot parse supplied IRI', 'iri.cannot_parse', $iri);
[330] Fix | Delete
}
[331] Fix | Delete
[332] Fix | Delete
if ($match[1] === '') {
[333] Fix | Delete
$match['scheme'] = null;
[334] Fix | Delete
}
[335] Fix | Delete
if (!isset($match[3]) || $match[3] === '') {
[336] Fix | Delete
$match['authority'] = null;
[337] Fix | Delete
}
[338] Fix | Delete
if (!isset($match[5])) {
[339] Fix | Delete
$match['path'] = '';
[340] Fix | Delete
}
[341] Fix | Delete
if (!isset($match[6]) || $match[6] === '') {
[342] Fix | Delete
$match['query'] = null;
[343] Fix | Delete
}
[344] Fix | Delete
if (!isset($match[8]) || $match[8] === '') {
[345] Fix | Delete
$match['fragment'] = null;
[346] Fix | Delete
}
[347] Fix | Delete
return $match;
[348] Fix | Delete
}
[349] Fix | Delete
[350] Fix | Delete
/**
[351] Fix | Delete
* Remove dot segments from a path
[352] Fix | Delete
*
[353] Fix | Delete
* @param string $input
[354] Fix | Delete
* @return string
[355] Fix | Delete
*/
[356] Fix | Delete
protected function remove_dot_segments($input) {
[357] Fix | Delete
$output = '';
[358] Fix | Delete
while (strpos($input, './') !== false || strpos($input, '/.') !== false || $input === '.' || $input === '..') {
[359] Fix | Delete
// A: If the input buffer begins with a prefix of "../" or "./",
[360] Fix | Delete
// then remove that prefix from the input buffer; otherwise,
[361] Fix | Delete
if (strpos($input, '../') === 0) {
[362] Fix | Delete
$input = substr($input, 3);
[363] Fix | Delete
}
[364] Fix | Delete
elseif (strpos($input, './') === 0) {
[365] Fix | Delete
$input = substr($input, 2);
[366] Fix | Delete
}
[367] Fix | Delete
// B: if the input buffer begins with a prefix of "/./" or "/.",
[368] Fix | Delete
// where "." is a complete path segment, then replace that prefix
[369] Fix | Delete
// with "/" in the input buffer; otherwise,
[370] Fix | Delete
elseif (strpos($input, '/./') === 0) {
[371] Fix | Delete
$input = substr($input, 2);
[372] Fix | Delete
}
[373] Fix | Delete
elseif ($input === '/.') {
[374] Fix | Delete
$input = '/';
[375] Fix | Delete
}
[376] Fix | Delete
// C: if the input buffer begins with a prefix of "/../" or "/..",
[377] Fix | Delete
// where ".." is a complete path segment, then replace that prefix
[378] Fix | Delete
// with "/" in the input buffer and remove the last segment and its
[379] Fix | Delete
// preceding "/" (if any) from the output buffer; otherwise,
[380] Fix | Delete
elseif (strpos($input, '/../') === 0) {
[381] Fix | Delete
$input = substr($input, 3);
[382] Fix | Delete
$output = substr_replace($output, '', strrpos($output, '/'));
[383] Fix | Delete
}
[384] Fix | Delete
elseif ($input === '/..') {
[385] Fix | Delete
$input = '/';
[386] Fix | Delete
$output = substr_replace($output, '', strrpos($output, '/'));
[387] Fix | Delete
}
[388] Fix | Delete
// D: if the input buffer consists only of "." or "..", then remove
[389] Fix | Delete
// that from the input buffer; otherwise,
[390] Fix | Delete
elseif ($input === '.' || $input === '..') {
[391] Fix | Delete
$input = '';
[392] Fix | Delete
}
[393] Fix | Delete
// E: move the first path segment in the input buffer to the end of
[394] Fix | Delete
// the output buffer, including the initial "/" character (if any)
[395] Fix | Delete
// and any subsequent characters up to, but not including, the next
[396] Fix | Delete
// "/" character or the end of the input buffer
[397] Fix | Delete
elseif (($pos = strpos($input, '/', 1)) !== false) {
[398] Fix | Delete
$output .= substr($input, 0, $pos);
[399] Fix | Delete
$input = substr_replace($input, '', 0, $pos);
[400] Fix | Delete
}
[401] Fix | Delete
else {
[402] Fix | Delete
$output .= $input;
[403] Fix | Delete
$input = '';
[404] Fix | Delete
}
[405] Fix | Delete
}
[406] Fix | Delete
return $output . $input;
[407] Fix | Delete
}
[408] Fix | Delete
[409] Fix | Delete
/**
[410] Fix | Delete
* Replace invalid character with percent encoding
[411] Fix | Delete
*
[412] Fix | Delete
* @param string $string Input string
[413] Fix | Delete
* @param string $extra_chars Valid characters not in iunreserved or
[414] Fix | Delete
* iprivate (this is ASCII-only)
[415] Fix | Delete
* @param bool $iprivate Allow iprivate
[416] Fix | Delete
* @return string
[417] Fix | Delete
*/
[418] Fix | Delete
protected function replace_invalid_with_pct_encoding($string, $extra_chars, $iprivate = false) {
[419] Fix | Delete
// Normalize as many pct-encoded sections as possible
[420] Fix | Delete
$string = preg_replace_callback('/(?:%[A-Fa-f0-9]{2})+/', array(&$this, 'remove_iunreserved_percent_encoded'), $string);
[421] Fix | Delete
[422] Fix | Delete
// Replace invalid percent characters
[423] Fix | Delete
$string = preg_replace('/%(?![A-Fa-f0-9]{2})/', '%25', $string);
[424] Fix | Delete
[425] Fix | Delete
// Add unreserved and % to $extra_chars (the latter is safe because all
[426] Fix | Delete
// pct-encoded sections are now valid).
[427] Fix | Delete
$extra_chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~%';
[428] Fix | Delete
[429] Fix | Delete
// Now replace any bytes that aren't allowed with their pct-encoded versions
[430] Fix | Delete
$position = 0;
[431] Fix | Delete
$strlen = strlen($string);
[432] Fix | Delete
while (($position += strspn($string, $extra_chars, $position)) < $strlen) {
[433] Fix | Delete
$value = ord($string[$position]);
[434] Fix | Delete
[435] Fix | Delete
// Start position
[436] Fix | Delete
$start = $position;
[437] Fix | Delete
[438] Fix | Delete
// By default we are valid
[439] Fix | Delete
$valid = true;
[440] Fix | Delete
[441] Fix | Delete
// No one byte sequences are valid due to the while.
[442] Fix | Delete
// Two byte sequence:
[443] Fix | Delete
if (($value & 0xE0) === 0xC0) {
[444] Fix | Delete
$character = ($value & 0x1F) << 6;
[445] Fix | Delete
$length = 2;
[446] Fix | Delete
$remaining = 1;
[447] Fix | Delete
}
[448] Fix | Delete
// Three byte sequence:
[449] Fix | Delete
elseif (($value & 0xF0) === 0xE0) {
[450] Fix | Delete
$character = ($value & 0x0F) << 12;
[451] Fix | Delete
$length = 3;
[452] Fix | Delete
$remaining = 2;
[453] Fix | Delete
}
[454] Fix | Delete
// Four byte sequence:
[455] Fix | Delete
elseif (($value & 0xF8) === 0xF0) {
[456] Fix | Delete
$character = ($value & 0x07) << 18;
[457] Fix | Delete
$length = 4;
[458] Fix | Delete
$remaining = 3;
[459] Fix | Delete
}
[460] Fix | Delete
// Invalid byte:
[461] Fix | Delete
else {
[462] Fix | Delete
$valid = false;
[463] Fix | Delete
$length = 1;
[464] Fix | Delete
$remaining = 0;
[465] Fix | Delete
}
[466] Fix | Delete
[467] Fix | Delete
if ($remaining) {
[468] Fix | Delete
if ($position + $length <= $strlen) {
[469] Fix | Delete
for ($position++; $remaining; $position++) {
[470] Fix | Delete
$value = ord($string[$position]);
[471] Fix | Delete
[472] Fix | Delete
// Check that the byte is valid, then add it to the character:
[473] Fix | Delete
if (($value & 0xC0) === 0x80) {
[474] Fix | Delete
$character |= ($value & 0x3F) << (--$remaining * 6);
[475] Fix | Delete
}
[476] Fix | Delete
// If it is invalid, count the sequence as invalid and reprocess the current byte:
[477] Fix | Delete
else {
[478] Fix | Delete
$valid = false;
[479] Fix | Delete
$position--;
[480] Fix | Delete
break;
[481] Fix | Delete
}
[482] Fix | Delete
}
[483] Fix | Delete
}
[484] Fix | Delete
else {
[485] Fix | Delete
$position = $strlen - 1;
[486] Fix | Delete
$valid = false;
[487] Fix | Delete
}
[488] Fix | Delete
}
[489] Fix | Delete
[490] Fix | Delete
// Percent encode anything invalid or not in ucschar
[491] Fix | Delete
if (
[492] Fix | Delete
// Invalid sequences
[493] Fix | Delete
!$valid
[494] Fix | Delete
// Non-shortest form sequences are invalid
[495] Fix | Delete
|| $length > 1 && $character <= 0x7F
[496] Fix | Delete
|| $length > 2 && $character <= 0x7FF
[497] Fix | Delete
|| $length > 3 && $character <= 0xFFFF
[498] Fix | Delete
// Outside of range of ucschar codepoints
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function