127) { continue; } if ($ord === 13) { if ($i + 2 >= $total) { continue; } $lf = ord($value[$i + 1]); $sp = ord($value[$i + 2]); if ($lf !== 10 || $sp !== 32) { continue; } $result .= "\r\n "; $i += 2; continue; } $result .= $value[$i]; } return $result; } /** * Determine if the header value contains any invalid characters. * * @see http://www.rfc-base.org/txt/rfc-2822.txt (section 2.2) * * @param string $value * @return bool */ public static function isValid($value) { $total = strlen($value); for ($i = 0; $i < $total; $i += 1) { $ord = ord($value[$i]); // bare LF means we aren't valid if ($ord === 10 || $ord > 127) { return false; } if ($ord === 13) { if ($i + 2 >= $total) { return false; } $lf = ord($value[$i + 1]); $sp = ord($value[$i + 2]); if ($lf !== 10 || ! in_array($sp, [9, 32], true)) { return false; } // skip over the LF following this $i += 2; } } return true; } /** * Assert that the header value is valid. * * Raises an exception if invalid. * * @param string $value * @throws Exception\RuntimeException * @return void */ public static function assertValid($value) { if (! self::isValid($value)) { throw new Exception\RuntimeException('Invalid header value detected'); } } }