function ParameterizedHeader::createParameter
Render an RFC 2047 compliant header parameter from the $name and $value.
2 calls to ParameterizedHeader::createParameter()
- ParameterizedHeader::getBodyAsString in vendor/
symfony/ mime/ Header/ ParameterizedHeader.php - Get the value of this header prepared for rendering.
- ParameterizedHeader::toTokens in vendor/
symfony/ mime/ Header/ ParameterizedHeader.php - Generate a list of all tokens in the final header.
File
-
vendor/
symfony/ mime/ Header/ ParameterizedHeader.php, line 107
Class
- ParameterizedHeader
- @author Chris Corbyn
Namespace
Symfony\Component\Mime\HeaderCode
private function createParameter(string $name, string $value) : string {
$origValue = $value;
$encoded = false;
// Allow room for parameter name, indices, "=" and DQUOTEs
$maxValueLength = $this->getMaxLineLength() - \strlen($name . '=*N"";') - 1;
$firstLineOffset = 0;
// If it's not already a valid parameter value...
if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) {
// TODO: text, or something else??
// ... and it's not ascii
if (!preg_match('/^[\\x00-\\x08\\x0B\\x0C\\x0E-\\x7F]*$/D', $value)) {
$encoded = true;
// Allow space for the indices, charset and language
$maxValueLength = $this->getMaxLineLength() - \strlen($name . '*N*="";') - 1;
$firstLineOffset = \strlen($this->getCharset() . "'" . $this->getLanguage() . "'");
}
if (\in_array($name, [
'name',
'filename',
], true) && 'form-data' === $this->getValue() && 'content-disposition' === strtolower($this->getName()) && preg_match('//u', $value)) {
// WHATWG HTML living standard 4.10.21.8 2 specifies:
// For field names and filenames for file fields, the result of the
// encoding in the previous bullet point must be escaped by replacing
// any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D`
// and 0x22 (") with `%22`.
// The user agent must not perform any other escapes.
$value = str_replace([
'"',
"\r",
"\n",
], [
'%22',
'%0D',
'%0A',
], $value);
if (\strlen($value) <= $maxValueLength) {
return $name . '="' . $value . '"';
}
$value = $origValue;
}
}
// Encode if we need to
if ($encoded || \strlen($value) > $maxValueLength) {
if (null !== $this->encoder) {
$value = $this->encoder
->encodeString($origValue, $this->getCharset(), $firstLineOffset, $maxValueLength);
}
else {
// We have to go against RFC 2183/2231 in some areas for interoperability
$value = $this->getTokenAsEncodedWord($origValue);
$encoded = false;
}
}
$valueLines = $this->encoder ? explode("\r\n", $value) : [
$value,
];
// Need to add indices
if (\count($valueLines) > 1) {
$paramLines = [];
foreach ($valueLines as $i => $line) {
$paramLines[] = $name . '*' . $i . $this->getEndOfParameterValue($line, true, 0 === $i);
}
return implode(";\r\n ", $paramLines);
}
return $name . $this->getEndOfParameterValue($valueLines[0], $encoded, true);
}