Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../includes/Google/Utils
File: URITemplate.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* Copyright 2013 Google Inc.
[2] Fix | Delete
*
[3] Fix | Delete
* Licensed under the Apache License, Version 2.0 (the "License");
[4] Fix | Delete
* you may not use this file except in compliance with the License.
[5] Fix | Delete
* You may obtain a copy of the License at
[6] Fix | Delete
*
[7] Fix | Delete
* http://www.apache.org/licenses/LICENSE-2.0
[8] Fix | Delete
*
[9] Fix | Delete
* Unless required by applicable law or agreed to in writing, software
[10] Fix | Delete
* distributed under the License is distributed on an "AS IS" BASIS,
[11] Fix | Delete
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
[12] Fix | Delete
* See the License for the specific language governing permissions and
[13] Fix | Delete
* limitations under the License.
[14] Fix | Delete
*/
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* Implementation of levels 1-3 of the URI Template spec.
[18] Fix | Delete
* @see http://tools.ietf.org/html/rfc6570
[19] Fix | Delete
*/
[20] Fix | Delete
class Google_Utils_URITemplate
[21] Fix | Delete
{
[22] Fix | Delete
const TYPE_MAP = "1";
[23] Fix | Delete
const TYPE_LIST = "2";
[24] Fix | Delete
const TYPE_SCALAR = "4";
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* @var $operators array
[28] Fix | Delete
* These are valid at the start of a template block to
[29] Fix | Delete
* modify the way in which the variables inside are
[30] Fix | Delete
* processed.
[31] Fix | Delete
*/
[32] Fix | Delete
private $operators = array(
[33] Fix | Delete
"+" => "reserved",
[34] Fix | Delete
"/" => "segments",
[35] Fix | Delete
"." => "dotprefix",
[36] Fix | Delete
"#" => "fragment",
[37] Fix | Delete
";" => "semicolon",
[38] Fix | Delete
"?" => "form",
[39] Fix | Delete
"&" => "continuation"
[40] Fix | Delete
);
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* @var reserved array
[44] Fix | Delete
* These are the characters which should not be URL encoded in reserved
[45] Fix | Delete
* strings.
[46] Fix | Delete
*/
[47] Fix | Delete
private $reserved = array(
[48] Fix | Delete
"=", ",", "!", "@", "|", ":", "/", "?", "#",
[49] Fix | Delete
"[", "]",'$', "&", "'", "(", ")", "*", "+", ";"
[50] Fix | Delete
);
[51] Fix | Delete
private $reservedEncoded = array(
[52] Fix | Delete
"%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F",
[53] Fix | Delete
"%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29",
[54] Fix | Delete
"%2A", "%2B", "%3B"
[55] Fix | Delete
);
[56] Fix | Delete
[57] Fix | Delete
public function parse($string, array $parameters)
[58] Fix | Delete
{
[59] Fix | Delete
return $this->resolveNextSection($string, $parameters);
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* This function finds the first matching {...} block and
[64] Fix | Delete
* executes the replacement. It then calls itself to find
[65] Fix | Delete
* subsequent blocks, if any.
[66] Fix | Delete
*/
[67] Fix | Delete
private function resolveNextSection($string, $parameters)
[68] Fix | Delete
{
[69] Fix | Delete
$start = strpos($string, "{");
[70] Fix | Delete
if ($start === false) {
[71] Fix | Delete
return $string;
[72] Fix | Delete
}
[73] Fix | Delete
$end = strpos($string, "}");
[74] Fix | Delete
if ($end === false) {
[75] Fix | Delete
return $string;
[76] Fix | Delete
}
[77] Fix | Delete
$string = $this->replace($string, $start, $end, $parameters);
[78] Fix | Delete
return $this->resolveNextSection($string, $parameters);
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
private function replace($string, $start, $end, $parameters)
[82] Fix | Delete
{
[83] Fix | Delete
// We know a data block will have {} round it, so we can strip that.
[84] Fix | Delete
$data = substr($string, $start + 1, $end - $start - 1);
[85] Fix | Delete
[86] Fix | Delete
// If the first character is one of the reserved operators, it effects
[87] Fix | Delete
// the processing of the stream.
[88] Fix | Delete
if (isset($this->operators[$data[0]])) {
[89] Fix | Delete
$op = $this->operators[$data[0]];
[90] Fix | Delete
$data = substr($data, 1);
[91] Fix | Delete
$prefix = "";
[92] Fix | Delete
$prefix_on_missing = false;
[93] Fix | Delete
[94] Fix | Delete
switch ($op) {
[95] Fix | Delete
case "reserved":
[96] Fix | Delete
// Reserved means certain characters should not be URL encoded
[97] Fix | Delete
$data = $this->replaceVars($data, $parameters, ",", null, true);
[98] Fix | Delete
break;
[99] Fix | Delete
case "fragment":
[100] Fix | Delete
// Comma separated with fragment prefix. Bare values only.
[101] Fix | Delete
$prefix = "#";
[102] Fix | Delete
$prefix_on_missing = true;
[103] Fix | Delete
$data = $this->replaceVars($data, $parameters, ",", null, true);
[104] Fix | Delete
break;
[105] Fix | Delete
case "segments":
[106] Fix | Delete
// Slash separated data. Bare values only.
[107] Fix | Delete
$prefix = "/";
[108] Fix | Delete
$data =$this->replaceVars($data, $parameters, "/");
[109] Fix | Delete
break;
[110] Fix | Delete
case "dotprefix":
[111] Fix | Delete
// Dot separated data. Bare values only.
[112] Fix | Delete
$prefix = ".";
[113] Fix | Delete
$prefix_on_missing = true;
[114] Fix | Delete
$data = $this->replaceVars($data, $parameters, ".");
[115] Fix | Delete
break;
[116] Fix | Delete
case "semicolon":
[117] Fix | Delete
// Semicolon prefixed and separated. Uses the key name
[118] Fix | Delete
$prefix = ";";
[119] Fix | Delete
$data = $this->replaceVars($data, $parameters, ";", "=", false, true, false);
[120] Fix | Delete
break;
[121] Fix | Delete
case "form":
[122] Fix | Delete
// Standard URL format. Uses the key name
[123] Fix | Delete
$prefix = "?";
[124] Fix | Delete
$data = $this->replaceVars($data, $parameters, "&", "=");
[125] Fix | Delete
break;
[126] Fix | Delete
case "continuation":
[127] Fix | Delete
// Standard URL, but with leading ampersand. Uses key name.
[128] Fix | Delete
$prefix = "&";
[129] Fix | Delete
$data = $this->replaceVars($data, $parameters, "&", "=");
[130] Fix | Delete
break;
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
// Add the initial prefix character if data is valid.
[134] Fix | Delete
if ($data || ($data !== false && $prefix_on_missing)) {
[135] Fix | Delete
$data = $prefix . $data;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
} else {
[139] Fix | Delete
// If no operator we replace with the defaults.
[140] Fix | Delete
$data = $this->replaceVars($data, $parameters);
[141] Fix | Delete
}
[142] Fix | Delete
// This is chops out the {...} and replaces with the new section.
[143] Fix | Delete
return substr($string, 0, $start) . $data . substr($string, $end + 1);
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
private function replaceVars(
[147] Fix | Delete
$section,
[148] Fix | Delete
$parameters,
[149] Fix | Delete
$sep = ",",
[150] Fix | Delete
$combine = null,
[151] Fix | Delete
$reserved = false,
[152] Fix | Delete
$tag_empty = false,
[153] Fix | Delete
$combine_on_empty = true
[154] Fix | Delete
) {
[155] Fix | Delete
if (strpos($section, ",") === false) {
[156] Fix | Delete
// If we only have a single value, we can immediately process.
[157] Fix | Delete
return $this->combine(
[158] Fix | Delete
$section,
[159] Fix | Delete
$parameters,
[160] Fix | Delete
$sep,
[161] Fix | Delete
$combine,
[162] Fix | Delete
$reserved,
[163] Fix | Delete
$tag_empty,
[164] Fix | Delete
$combine_on_empty
[165] Fix | Delete
);
[166] Fix | Delete
} else {
[167] Fix | Delete
// If we have multiple values, we need to split and loop over them.
[168] Fix | Delete
// Each is treated individually, then glued together with the
[169] Fix | Delete
// separator character.
[170] Fix | Delete
$vars = explode(",", $section);
[171] Fix | Delete
return $this->combineList(
[172] Fix | Delete
$vars,
[173] Fix | Delete
$sep,
[174] Fix | Delete
$parameters,
[175] Fix | Delete
$combine,
[176] Fix | Delete
$reserved,
[177] Fix | Delete
false, // Never emit empty strings in multi-param replacements
[178] Fix | Delete
$combine_on_empty
[179] Fix | Delete
);
[180] Fix | Delete
}
[181] Fix | Delete
}
[182] Fix | Delete
[183] Fix | Delete
public function combine(
[184] Fix | Delete
$key,
[185] Fix | Delete
$parameters,
[186] Fix | Delete
$sep,
[187] Fix | Delete
$combine,
[188] Fix | Delete
$reserved,
[189] Fix | Delete
$tag_empty,
[190] Fix | Delete
$combine_on_empty
[191] Fix | Delete
) {
[192] Fix | Delete
$length = false;
[193] Fix | Delete
$explode = false;
[194] Fix | Delete
$skip_final_combine = false;
[195] Fix | Delete
$value = false;
[196] Fix | Delete
[197] Fix | Delete
// Check for length restriction.
[198] Fix | Delete
if (strpos($key, ":") !== false) {
[199] Fix | Delete
list($key, $length) = explode(":", $key);
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
// Check for explode parameter.
[203] Fix | Delete
if ($key[strlen($key) - 1] == "*") {
[204] Fix | Delete
$explode = true;
[205] Fix | Delete
$key = substr($key, 0, -1);
[206] Fix | Delete
$skip_final_combine = true;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// Define the list separator.
[210] Fix | Delete
$list_sep = $explode ? $sep : ",";
[211] Fix | Delete
[212] Fix | Delete
if (isset($parameters[$key])) {
[213] Fix | Delete
$data_type = $this->getDataType($parameters[$key]);
[214] Fix | Delete
switch($data_type) {
[215] Fix | Delete
case self::TYPE_SCALAR:
[216] Fix | Delete
$value = $this->getValue($parameters[$key], $length);
[217] Fix | Delete
break;
[218] Fix | Delete
case self::TYPE_LIST:
[219] Fix | Delete
$values = array();
[220] Fix | Delete
foreach ($parameters[$key] as $pkey => $pvalue) {
[221] Fix | Delete
$pvalue = $this->getValue($pvalue, $length);
[222] Fix | Delete
if ($combine && $explode) {
[223] Fix | Delete
$values[$pkey] = $key . $combine . $pvalue;
[224] Fix | Delete
} else {
[225] Fix | Delete
$values[$pkey] = $pvalue;
[226] Fix | Delete
}
[227] Fix | Delete
}
[228] Fix | Delete
$value = implode($list_sep, $values);
[229] Fix | Delete
if ($value == '') {
[230] Fix | Delete
return '';
[231] Fix | Delete
}
[232] Fix | Delete
break;
[233] Fix | Delete
case self::TYPE_MAP:
[234] Fix | Delete
$values = array();
[235] Fix | Delete
foreach ($parameters[$key] as $pkey => $pvalue) {
[236] Fix | Delete
$pvalue = $this->getValue($pvalue, $length);
[237] Fix | Delete
if ($explode) {
[238] Fix | Delete
$pkey = $this->getValue($pkey, $length);
[239] Fix | Delete
$values[] = $pkey . "=" . $pvalue; // Explode triggers = combine.
[240] Fix | Delete
} else {
[241] Fix | Delete
$values[] = $pkey;
[242] Fix | Delete
$values[] = $pvalue;
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
$value = implode($list_sep, $values);
[246] Fix | Delete
if ($value == '') {
[247] Fix | Delete
return false;
[248] Fix | Delete
}
[249] Fix | Delete
break;
[250] Fix | Delete
}
[251] Fix | Delete
} else if ($tag_empty) {
[252] Fix | Delete
// If we are just indicating empty values with their key name, return that.
[253] Fix | Delete
return $key;
[254] Fix | Delete
} else {
[255] Fix | Delete
// Otherwise we can skip this variable due to not being defined.
[256] Fix | Delete
return false;
[257] Fix | Delete
}
[258] Fix | Delete
[259] Fix | Delete
if ($reserved) {
[260] Fix | Delete
$value = str_replace($this->reservedEncoded, $this->reserved, $value);
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
// If we do not need to include the key name, we just return the raw
[264] Fix | Delete
// value.
[265] Fix | Delete
if (!$combine || $skip_final_combine) {
[266] Fix | Delete
return $value;
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
// Else we combine the key name: foo=bar, if value is not the empty string.
[270] Fix | Delete
return $key . ($value != '' || $combine_on_empty ? $combine . $value : '');
[271] Fix | Delete
}
[272] Fix | Delete
[273] Fix | Delete
/**
[274] Fix | Delete
* Return the type of a passed in value
[275] Fix | Delete
*/
[276] Fix | Delete
private function getDataType($data)
[277] Fix | Delete
{
[278] Fix | Delete
if (is_array($data)) {
[279] Fix | Delete
reset($data);
[280] Fix | Delete
if (key($data) !== 0) {
[281] Fix | Delete
return self::TYPE_MAP;
[282] Fix | Delete
}
[283] Fix | Delete
return self::TYPE_LIST;
[284] Fix | Delete
}
[285] Fix | Delete
return self::TYPE_SCALAR;
[286] Fix | Delete
}
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Utility function that merges multiple combine calls
[290] Fix | Delete
* for multi-key templates.
[291] Fix | Delete
*/
[292] Fix | Delete
private function combineList(
[293] Fix | Delete
$vars,
[294] Fix | Delete
$sep,
[295] Fix | Delete
$parameters,
[296] Fix | Delete
$combine,
[297] Fix | Delete
$reserved,
[298] Fix | Delete
$tag_empty,
[299] Fix | Delete
$combine_on_empty
[300] Fix | Delete
) {
[301] Fix | Delete
$ret = array();
[302] Fix | Delete
foreach ($vars as $var) {
[303] Fix | Delete
$response = $this->combine(
[304] Fix | Delete
$var,
[305] Fix | Delete
$parameters,
[306] Fix | Delete
$sep,
[307] Fix | Delete
$combine,
[308] Fix | Delete
$reserved,
[309] Fix | Delete
$tag_empty,
[310] Fix | Delete
$combine_on_empty
[311] Fix | Delete
);
[312] Fix | Delete
if ($response === false) {
[313] Fix | Delete
continue;
[314] Fix | Delete
}
[315] Fix | Delete
$ret[] = $response;
[316] Fix | Delete
}
[317] Fix | Delete
return implode($sep, $ret);
[318] Fix | Delete
}
[319] Fix | Delete
[320] Fix | Delete
/**
[321] Fix | Delete
* Utility function to encode and trim values
[322] Fix | Delete
*/
[323] Fix | Delete
private function getValue($value, $length)
[324] Fix | Delete
{
[325] Fix | Delete
if ($length) {
[326] Fix | Delete
$value = substr($value, 0, $length);
[327] Fix | Delete
}
[328] Fix | Delete
$value = rawurlencode($value);
[329] Fix | Delete
return $value;
[330] Fix | Delete
}
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function