1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\HTTP\Headers;
use ICanBoogie\Accessor\AccessorTrait;
/**
* Representation of a header parameter.
*
* @property-read string $attribute The attribute of the parameter.
* @property-read string $charset The charset of the parameter's value.
*
* @see http://tools.ietf.org/html/rfc2231
* @see http://tools.ietf.org/html/rfc5987
* @see http://greenbytes.de/tech/tc2231/#attwithfn2231utf8
*/
class HeaderParameter
{
use AccessorTrait;
/**
* Token of the parameter.
*
* @var string
*/
protected $attribute;
protected function get_attribute()
{
return $this->attribute;
}
/**
* Value of the parameter.
*
* @var string
*/
public $value;
protected function get_charset()
{
return mb_detect_encoding($this->value) ?: 'ISO-8859-1';
}
/**
* Language of the value.
*
* @var string
*/
public $language;
/**
* Creates a {@link HeaderParameter} instance from the provided source.
*
* @param mixed $source
*
* @return HeaderParameter
*/
static public function from($source)
{
if ($source instanceof self)
{
return $source;
}
$equal_pos = strpos($source, '=');
$language = null;
if ($source[$equal_pos - 1] === '*')
{
$attribute = substr($source, 0, $equal_pos - 1);
$value = substr($source, $equal_pos + 1);
preg_match('#^([a-zA-Z0-9\-]+)?(\'([a-z\-]+)?\')?(")?([^"]+)(")?$#', $value, $matches);
if ($matches[3])
{
$language = $matches[3];
}
$value = urldecode($matches[5]);
if ($matches[1] === 'iso-8859-1')
{
$value = utf8_encode($value);
}
}
else
{
$attribute = substr($source, 0, $equal_pos);
$value = substr($source, $equal_pos + 1);
if ($value[0] === '"')
{
$value = substr($value, 1, -1);
}
}
$value = mb_convert_encoding($value, 'UTF-8');
return new static($attribute, $value, $language);
}
/**
* Checks if the provided string is a token.
*
* <pre>
* token = 1*<any CHAR except CTLs or separators>
* separators = "(" | ")" | "<" | ">" | "@"
* | "," | ";" | ":" | "\" | <">
* | "/" | "[" | "]" | "?" | "="
* | "{" | "}" | SP | HT
* CHAR = <any US-ASCII character (octets 0 - 127)>
* CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
* SP = <US-ASCII SP, space (32)>
* HT = <US-ASCII HT, horizontal-tab (9)>
*</pre>
*
* @param string $str
*
* @return boolean `true` if the provided string is a token, `false` otherwise.
*/
static public function is_token($str)
{
// \x21 = CHAR except 0 - 31 (\x1f) and SP (\x20)
// \x7e = CHAR except DEL
return !preg_match('#[^\x21-\x7e]#', $str) && !preg_match('#[\(\)\<\>\@\,\;\:\\\\"\/\[\]\?\=\{\}\x9]#', $str);
}
/**
* Converts a string to the ASCI charset.
*
* Accents are converted using {@link \ICanBoogie\remove_accents()}. Characters that are not
* in the ASCII range are discarted.
*
* @param string $str The string to convert.
*
* @return string
*/
static public function to_ascii($str)
{
$str = \ICanBoogie\remove_accents($str);
$str = preg_replace('/[^\x20-\x7F]+/', '', $str);
return $str;
}
/**
* Initializes the {@link $attribute}, {@link $value} and {@link $language} properties.
*
* @param string $attribute
* @param string $value
* @param string|null $language
*/
public function __construct($attribute, $value=null, $language=null)
{
$this->attribute = $attribute;
$this->value = $value;
$this->language = $language;
}
/**
* Renders the attribute and value into a string.
*
* <pre>
* A string of text is parsed as a single word if it is quoted using
* double-quote marks.
*
* quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
* qdtext = <any TEXT except <">>
*
* The backslash character ("\") MAY be used as a single-character
* quoting mechanism only within quoted-string and comment constructs.
*
* quoted-pair = "\" CHAR
* </pre>
*
* @return string
*/
public function render()
{
$value = $this->value;
if (!$value)
{
return '';
}
$attribute = $this->attribute;
#
# token
#
if (self::is_token($value))
{
return "{$attribute}={$value}";
}
#
# quoted string
#
$encoding = mb_detect_encoding($value);
if (($encoding === 'ASCII' || $encoding === 'ISO-8859-1') && strpos($value, '"') === false)
{
return "{$attribute}=\"{$value}\"";
}
#
# escaped, with fallback
#
# @see http://greenbytes.de/tech/tc2231/#encoding-2231-fb
#
if ($encoding !== 'UTF-8')
{
$value = mb_convert_encoding($value, 'UTF-8', $encoding);
$encoding = mb_detect_encoding($value);
}
$normalized_value = self::to_ascii($value);
$normalized_value = str_replace([ '"', ';' ], '', $normalized_value);
return "{$attribute}=\"{$normalized_value}\"; {$attribute}*=" . $encoding . "'{$this->language}'" . rawurlencode($value);
}
/**
* Returns the value of the parameter.
*
* Note: {@link render()} to render the attribute and value of the parameter.
*
* @return string
*/
public function __toString()
{
return (string) $this->value;
}
}