/** * @dataProvider decodeProvider */ public function testDecode($string, $expected) { $decode = new Horde_Mime_ContentParam_Decode(); $res = $decode->decode($string); ksort($res); $this->assertEquals($expected, $res); }
/** * Decodes a MIME content parameter string pursuant to RFC 2183 & 2231 * (Content-Type and Content-Disposition headers). * * Stores value/parameter data in the current object. * * @param mixed $data Parameter data. Either an array or a string. */ public function decode($data) { $add = $convert = array(); if (is_array($data)) { $params = $data; } else { $parts = explode(';', $data, 2); if (isset($parts[0]) && strpos($parts[0], '=') === false) { $this->setContentParamValue($parts[0]); $param = isset($parts[1]) ? $parts[1] : null; } else { $param = $data; } if (empty($param)) { $params = array(); } else { $decode = new Horde_Mime_ContentParam_Decode(); $params = $decode->decode($param); } } $to_add = array(); foreach ($params as $name => $val) { /* Asterisk at end indicates encoded value. */ if (substr($name, -1) == '*') { $name = substr($name, 0, -1); $encoded = true; } else { $encoded = false; } /* This asterisk indicates continuation parameter. */ if (($pos = strrpos($name, '*')) !== false && is_numeric($order = substr($name, $pos + 1))) { $name = substr($name, 0, $pos); $to_add[Horde_String::lower($name)][$order] = $val; } else { $to_add[$name] = array($val); } if ($encoded) { $convert[$name] = true; } } foreach ($to_add as $key => $val) { ksort($val); $add[$key] = implode('', $val); } foreach (array_keys($convert) as $name) { $val = $add[$name]; $quote = strpos($val, "'"); if ($quote === false) { $add[$name] = urldecode($val); } else { $orig_charset = substr($val, 0, $quote); if (Horde_String::lower($orig_charset) == 'iso-8859-1') { $orig_charset = 'windows-1252'; } /* Ignore language. */ $quote = strpos($val, "'", $quote + 1); substr($val, $quote + 1); $add[$name] = Horde_String::convertCharset(urldecode(substr($val, $quote + 1)), $orig_charset, 'UTF-8'); } } /* MIME parameters are supposed to be encoded via RFC 2231, but many * mailers do RFC 2045 encoding instead. However, if we see at least * one RFC 2231 encoding, then assume the sending mailer knew what * it was doing and didn't send any parameters RFC 2045 encoded. */ if (empty($convert)) { foreach ($add as $key => $val) { $add[$key] = Horde_Mime::decode($val); } } if (count($add)) { foreach ($add as $key => $val) { /* When parsing a content-param string, lowercase all * parameter names to normalize. Only maintain case of * parameters explicitly added by calling code. */ $this[Horde_String::lower($key)] = $val; } } elseif (is_string($data)) { $this->setContentParamValue($parts[0]); } }