1: <?php
2:
3: 4: 5: 6: 7: 8: 9:
10:
11: 12: 13: 14: 15: 16:
17: class Swift_Mime_Headers_ParameterizedHeader extends Swift_Mime_Headers_UnstructuredHeader implements Swift_Mime_ParameterizedHeader
18: {
19: 20: 21: 22:
23: const TOKEN_REGEX = '(?:[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7E]+)';
24:
25: 26: 27: 28: 29:
30: private $_paramEncoder;
31:
32: 33: 34: 35: 36:
37: private $_params = array();
38:
39: 40: 41: 42: 43: 44: 45:
46: public function __construct($name, Swift_Mime_HeaderEncoder $encoder, Swift_Encoder $paramEncoder = null, Swift_Mime_Grammar $grammar)
47: {
48: parent::__construct($name, $encoder, $grammar);
49: $this->_paramEncoder = $paramEncoder;
50: }
51:
52: 53: 54: 55: 56: 57:
58: public function getFieldType()
59: {
60: return self::TYPE_PARAMETERIZED;
61: }
62:
63: 64: 65: 66:
67: public function setCharset($charset)
68: {
69: parent::setCharset($charset);
70: if (isset($this->_paramEncoder)) {
71: $this->_paramEncoder->charsetChanged($charset);
72: }
73: }
74:
75: 76: 77: 78: 79:
80: public function setParameter($parameter, $value)
81: {
82: $this->setParameters(array_merge($this->getParameters(), array($parameter => $value)));
83: }
84:
85: 86: 87: 88:
89: public function getParameter($parameter)
90: {
91: $params = $this->getParameters();
92:
93: return array_key_exists($parameter, $params)
94: ? $params[$parameter]
95: : null;
96: }
97:
98: 99: 100: 101:
102: public function setParameters(array $parameters)
103: {
104: $this->clearCachedValueIf($this->_params != $parameters);
105: $this->_params = $parameters;
106: }
107:
108: 109: 110: 111:
112: public function getParameters()
113: {
114: return $this->_params;
115: }
116:
117: 118: 119: 120:
121: public function getFieldBody()
122: {
123: $body = parent::getFieldBody();
124: foreach ($this->_params as $name => $value) {
125: if (!is_null($value)) {
126:
127: $body .= '; ' . $this->_createParameter($name, $value);
128: }
129: }
130:
131: return $body;
132: }
133:
134:
135:
136: 137: 138: 139: 140: 141: 142: 143:
144: protected function toTokens($string = null)
145: {
146: $tokens = parent::toTokens(parent::getFieldBody());
147:
148:
149: foreach ($this->_params as $name => $value) {
150: if (!is_null($value)) {
151:
152: $tokens[count($tokens)-1] .= ';';
153: $tokens = array_merge($tokens, $this->generateTokenLines(
154: ' ' . $this->_createParameter($name, $value)
155: ));
156: }
157: }
158:
159: return $tokens;
160: }
161:
162:
163:
164: 165: 166: 167: 168: 169: 170:
171: private function _createParameter($name, $value)
172: {
173: $origValue = $value;
174:
175: $encoded = false;
176:
177: $maxValueLength = $this->getMaxLineLength() - strlen($name . '=*N"";') - 1;
178: $firstLineOffset = 0;
179:
180:
181: if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) {
182:
183:
184: if (!preg_match('/^' . $this->getGrammar()->getDefinition('text') . '*$/D', $value)) {
185: $encoded = true;
186:
187: $maxValueLength = $this->getMaxLineLength() - strlen($name . '*N*="";') - 1;
188: $firstLineOffset = strlen(
189: $this->getCharset() . "'" . $this->getLanguage() . "'"
190: );
191: }
192: }
193:
194:
195: if ($encoded || strlen($value) > $maxValueLength) {
196: if (isset($this->_paramEncoder)) {
197: $value = $this->_paramEncoder->encodeString(
198: $origValue, $firstLineOffset, $maxValueLength, $this->getCharset()
199: );
200: } else {
201: $value = $this->getTokenAsEncodedWord($origValue);
202: $encoded = false;
203: }
204: }
205:
206: $valueLines = isset($this->_paramEncoder) ? explode("\r\n", $value) : array($value);
207:
208:
209: if (count($valueLines) > 1) {
210: $paramLines = array();
211: foreach ($valueLines as $i => $line) {
212: $paramLines[] = $name . '*' . $i .
213: $this->_getEndOfParameterValue($line, true, $i == 0);
214: }
215:
216: return implode(";\r\n ", $paramLines);
217: } else {
218: return $name . $this->_getEndOfParameterValue(
219: $valueLines[0], $encoded, true
220: );
221: }
222: }
223:
224: 225: 226: 227: 228: 229: 230: 231:
232: private function _getEndOfParameterValue($value, $encoded = false, $firstLine = false)
233: {
234: if (!preg_match('/^' . self::TOKEN_REGEX . '$/D', $value)) {
235: $value = '"' . $value . '"';
236: }
237: $prepend = '=';
238: if ($encoded) {
239: $prepend = '*=';
240: if ($firstLine) {
241: $prepend = '*=' . $this->getCharset() . "'" . $this->getLanguage() .
242: "'";
243: }
244: }
245:
246: return $prepend . $value;
247: }
248: }
249: