/
Part.php
2432 lines (2158 loc) · 73.1 KB
/
Part.php
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Copyright 1999-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 1999-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
*/
/**
* Object-oriented representation of a MIME part (RFC 2045-2049).
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 1999-2014 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Mime
*/
class Horde_Mime_Part implements ArrayAccess, Countable, Serializable
{
/* Serialized version. */
const VERSION = 1;
/* The character(s) used internally for EOLs. */
const EOL = "\n";
/* The character string designated by RFC 2045 to designate EOLs in MIME
* messages. */
const RFC_EOL = "\r\n";
/* The default encoding. */
const DEFAULT_ENCODING = 'binary';
/* Constants indicating the valid transfer encoding allowed. */
const ENCODE_7BIT = 1;
const ENCODE_8BIT = 2;
const ENCODE_BINARY = 4;
/* Unknown types. */
const UNKNOWN = 'x-unknown';
/* MIME nesting limit. */
const NESTING_LIMIT = 100;
/**
* The default charset to use when parsing text parts with no charset
* information.
*
* @var string
*/
public static $defaultCharset = 'us-ascii';
/**
* Valid encoding types.
*
* @var array
*/
public static $encodingTypes = array(
'7bit', '8bit', 'base64', 'binary', 'quoted-printable',
// Non-RFC types, but old mailers may still use
'uuencode', 'x-uuencode', 'x-uue'
);
/**
* The memory limit for use with the PHP temp stream.
*
* @var integer
*/
public static $memoryLimit = 2097152;
/**
* Valid MIME types.
*
* @var array
*/
public static $mimeTypes = array(
'text', 'multipart', 'message', 'application', 'audio', 'image',
'video', 'model'
);
/**
* The type (ex.: text) of this part.
* Per RFC 2045, the default is 'application'.
*
* @var string
*/
protected $_type = 'application';
/**
* The subtype (ex.: plain) of this part.
* Per RFC 2045, the default is 'octet-stream'.
*
* @var string
*/
protected $_subtype = 'octet-stream';
/**
* The body of the part. Always stored in binary format.
*
* @var resource
*/
protected $_contents;
/**
* The desired transfer encoding of this part.
*
* @var string
*/
protected $_transferEncoding = self::DEFAULT_ENCODING;
/**
* The language(s) of this part.
*
* @var array
*/
protected $_language = array();
/**
* The description of this part.
*
* @var string
*/
protected $_description = '';
/**
* The disposition of this part (inline or attachment).
*
* @var string
*/
protected $_disposition = '';
/**
* The disposition parameters of this part.
*
* @var array
*/
protected $_dispParams = array();
/**
* The content type parameters of this part.
*
* @var Horde_Support_CaseInsensitiveArray
*/
protected $_contentTypeParams;
/**
* The subparts of this part.
*
* @var array
*/
protected $_parts = array();
/**
* The MIME ID of this part.
*
* @var string
*/
protected $_mimeid = null;
/**
* The sequence to use as EOL for this part.
* The default is currently to output the EOL sequence internally as
* just "\n" instead of the canonical "\r\n" required in RFC 822 & 2045.
* To be RFC complaint, the full <CR><LF> EOL combination should be used
* when sending a message.
* It is not crucial here since the PHP/PEAR mailing functions will handle
* the EOL details.
*
* @var string
*/
protected $_eol = self::EOL;
/**
* Internal temp array.
*
* @var array
*/
protected $_temp = array();
/**
* Metadata.
*
* @var array
*/
protected $_metadata = array();
/**
* Unique Horde_Mime_Part boundary string.
*
* @var string
*/
protected $_boundary = null;
/**
* Default value for this Part's size.
*
* @var integer
*/
protected $_bytes;
/**
* The content-ID for this part.
*
* @var string
*/
protected $_contentid = null;
/**
* The duration of this part's media data (RFC 3803).
*
* @var integer
*/
protected $_duration;
/**
* Do we need to reindex the current part?
*
* @var boolean
*/
protected $_reindex = false;
/**
* Is this the base MIME part?
*
* @var boolean
*/
protected $_basepart = false;
/**
* The charset to output the headers in.
*
* @var string
*/
protected $_hdrCharset = null;
/**
* The list of member variables to serialize.
*
* @var array
*/
protected $_serializedVars = array(
'_type',
'_subtype',
'_transferEncoding',
'_language',
'_description',
'_disposition',
'_dispParams',
'_contentTypeParams',
'_parts',
'_mimeid',
'_eol',
'_metadata',
'_boundary',
'_bytes',
'_contentid',
'_duration',
'_reindex',
'_basepart',
'_hdrCharset'
);
/**
* Constructor.
*/
public function __construct()
{
$this->_init();
}
/**
* Initialization tasks.
*/
protected function _init()
{
$this->_contentTypeParams = new Horde_Support_CaseInsensitiveArray();
}
/**
* Function to run on clone.
*/
public function __clone()
{
foreach ($this->_parts as $k => $v) {
$this->_parts[$k] = clone $v;
}
$this->_contentTypeParams = new Horde_Support_CaseInsensitiveArray(
$this->_contentTypeParams->getArrayCopy()
);
if (!empty($this->_contents)) {
$this->_contents = $this->_writeStream($this->_contents);
}
}
/**
* Set the content-disposition of this part.
*
* @param string $disposition The content-disposition to set ('inline',
* 'attachment', or an empty value).
*/
public function setDisposition($disposition = null)
{
if (empty($disposition)) {
$this->_disposition = '';
} else {
$disposition = Horde_String::lower($disposition);
if (in_array($disposition, array('inline', 'attachment'))) {
$this->_disposition = $disposition;
}
}
}
/**
* Get the content-disposition of this part.
*
* @return string The part's content-disposition. An empty string means
* no desired disposition has been set for this part.
*/
public function getDisposition()
{
return $this->_disposition;
}
/**
* Add a disposition parameter to this part.
*
* @param string $label The disposition parameter label.
* @param string $data The disposition parameter data.
*/
public function setDispositionParameter($label, $data)
{
$this->_dispParams[$label] = $data;
switch ($label) {
case 'size':
// RFC 2183 [2.7] - size parameter
$this->_bytes = intval($data);
break;
}
}
/**
* Get a disposition parameter from this part.
*
* @param string $label The disposition parameter label.
*
* @return string The data requested.
* Returns null if $label is not set.
*/
public function getDispositionParameter($label)
{
return (isset($this->_dispParams[$label]))
? $this->_dispParams[$label]
: null;
}
/**
* Get all parameters from the Content-Disposition header.
*
* @return array An array of all the parameters
* Returns the empty array if no parameters set.
*/
public function getAllDispositionParameters()
{
return $this->_dispParams;
}
/**
* Set the name of this part.
*
* @param string $name The name to set.
*/
public function setName($name)
{
$this->setDispositionParameter('filename', $name);
$this->setContentTypeParameter('name', $name);
}
/**
* Get the name of this part.
*
* @param boolean $default If the name parameter doesn't exist, should we
* use the default name from the description
* parameter?
*
* @return string The name of the part.
*/
public function getName($default = false)
{
if (!($name = $this->getDispositionParameter('filename')) &&
!($name = $this->getContentTypeParameter('name')) &&
$default) {
$name = preg_replace('|\W|', '_', $this->getDescription(false));
}
return $name;
}
/**
* Set the body contents of this part.
*
* @param mixed $contents The part body. Either a string or a stream
* resource, or an array containing both.
* @param array $options Additional options:
* - encoding: (string) The encoding of $contents.
* DEFAULT: Current transfer encoding value.
* - usestream: (boolean) If $contents is a stream, should we directly
* use that stream?
* DEFAULT: $contents copied to a new stream.
*/
public function setContents($contents, $options = array())
{
$this->clearContents();
if (empty($options['encoding'])) {
$options['encoding'] = $this->_transferEncoding;
}
$fp = (empty($options['usestream']) || !is_resource($contents))
? $this->_writeStream($contents)
: $contents;
$this->setTransferEncoding($options['encoding']);
$this->_contents = $this->_transferDecode($fp, $options['encoding']);
}
/**
* Add to the body contents of this part.
*
* @param mixed $contents The part body. Either a string or a stream
* resource, or an array containing both.
* - encoding: (string) The encoding of $contents.
* DEFAULT: Current transfer encoding value.
* - usestream: (boolean) If $contents is a stream, should we directly
* use that stream?
* DEFAULT: $contents copied to a new stream.
*/
public function appendContents($contents, $options = array())
{
if (empty($this->_contents)) {
$this->setContents($contents, $options);
} else {
$fp = (empty($options['usestream']) || !is_resource($contents))
? $this->_writeStream($contents)
: $contents;
$this->_writeStream((empty($options['encoding']) || ($options['encoding'] == $this->_transferEncoding)) ? $fp : $this->_transferDecode($fp, $options['encoding']), array('fp' => $this->_contents));
unset($this->_temp['sendTransferEncoding']);
}
}
/**
* Clears the body contents of this part.
*/
public function clearContents()
{
if (!empty($this->_contents)) {
fclose($this->_contents);
$this->_contents = null;
unset($this->_temp['sendTransferEncoding']);
}
}
/**
* Return the body of the part.
*
* @param array $options Additional options:
* - canonical: (boolean) Returns the contents in strict RFC 822 &
* 2045 output - namely, all newlines end with the
* canonical <CR><LF> sequence.
* DEFAULT: No
* - stream: (boolean) Return the body as a stream resource.
* DEFAULT: No
*
* @return mixed The body text (string) of the part, null if there is no
* contents, and a stream resource if 'stream' is true.
*/
public function getContents($options = array())
{
return empty($options['canonical'])
? (empty($options['stream']) ? $this->_readStream($this->_contents) : $this->_contents)
: $this->replaceEOL($this->_contents, self::RFC_EOL, !empty($options['stream']));
}
/**
* Decodes the contents of the part to binary encoding.
*
* @param resource $fp A stream containing the data to decode.
* @param string $encoding The original file encoding.
*
* @return resource A new file resource with the decoded data.
*/
protected function _transferDecode($fp, $encoding)
{
/* If the contents are empty, return now. */
fseek($fp, 0, SEEK_END);
if (ftell($fp)) {
switch ($encoding) {
case 'base64':
try {
return $this->_writeStream($fp, array(
'error' => true,
'filter' => array(
'convert.base64-decode' => array()
)
));
} catch (ErrorException $e) {}
rewind($fp);
return $this->_writeStream(base64_decode(stream_get_contents($fp)));
case 'quoted-printable':
try {
return $this->_writeStream($fp, array(
'error' => true,
'filter' => array(
'convert.quoted-printable-decode' => array()
)
));
} catch (ErrorException $e) {}
// Workaround for Horde Bug #8747
rewind($fp);
return $this->_writeStream(quoted_printable_decode(stream_get_contents($fp)));
case 'uuencode':
case 'x-uuencode':
case 'x-uue':
/* Support for uuencoded encoding - although not required by
* RFCs, some mailers may still encode this way. */
$res = Horde_Mime::uudecode($this->_readStream($fp));
return $this->_writeStream($res[0]['data']);
}
}
return $fp;
}
/**
* Encodes the contents of the part as necessary for transport.
*
* @param resource $fp A stream containing the data to encode.
* @param string $encoding The encoding to use.
*
* @return resource A new file resource with the encoded data.
*/
protected function _transferEncode($fp, $encoding)
{
$this->_temp['transferEncodeClose'] = true;
switch ($encoding) {
case 'base64':
/* Base64 Encoding: See RFC 2045, section 6.8 */
return $this->_writeStream($fp, array(
'filter' => array(
'convert.base64-encode' => array(
'line-break-chars' => $this->getEOL(),
'line-length' => 76
)
)
));
case 'quoted-printable':
$stream = new Horde_Stream_Existing(array(
'stream' => $fp
));
/* Quoted-Printable Encoding: See RFC 2045, section 6.7 */
return $this->_writeStream($fp, array(
'filter' => array(
'convert.quoted-printable-encode' => array_filter(array(
'line-break-chars' => $stream->getEOL(),
'line-length' => 76
))
)
));
default:
$this->_temp['transferEncodeClose'] = false;
return $fp;
}
}
/**
* Set the MIME type of this part.
*
* @param string $type The MIME type to set (ex.: text/plain).
*/
public function setType($type)
{
/* RFC 2045: Any entity with unrecognized encoding must be treated
* as if it has a Content-Type of "application/octet-stream"
* regardless of what the Content-Type field actually says. */
if (($this->_transferEncoding == self::UNKNOWN) ||
(strpos($type, '/') === false)) {
return;
}
list($this->_type, $this->_subtype) = explode('/', Horde_String::lower($type));
if (in_array($this->_type, self::$mimeTypes)) {
/* Set the boundary string for 'multipart/*' parts. */
if ($this->_type == 'multipart') {
if (!$this->getContentTypeParameter('boundary')) {
$this->setContentTypeParameter('boundary', $this->_generateBoundary());
}
} else {
$this->clearContentTypeParameter('boundary');
}
} else {
$this->_type = self::UNKNOWN;
$this->clearContentTypeParameter('boundary');
}
}
/**
* Get the full MIME Content-Type of this part.
*
* @param boolean $charset Append character set information to the end
* of the content type if this is a text/* part?
*`
* @return string The mimetype of this part (ex.: text/plain;
* charset=us-ascii) or false.
*/
public function getType($charset = false)
{
if (empty($this->_type) || empty($this->_subtype)) {
return false;
}
$ptype = $this->getPrimaryType();
$type = $ptype . '/' . $this->getSubType();
if ($charset &&
($ptype == 'text') &&
($charset = $this->getCharset()) &&
($charset !== 'us-ascii')) {
$type .= '; charset=' . $charset;
}
return $type;
}
/**
* If the subtype of a MIME part is unrecognized by an application, the
* default type should be used instead (See RFC 2046). This method
* returns the default subtype for a particular primary MIME type.
*
* @return string The default MIME type of this part (ex.: text/plain).
*/
public function getDefaultType()
{
switch ($this->getPrimaryType()) {
case 'text':
/* RFC 2046 (4.1.4): text parts default to text/plain. */
return 'text/plain';
case 'multipart':
/* RFC 2046 (5.1.3): multipart parts default to multipart/mixed. */
return 'multipart/mixed';
default:
/* RFC 2046 (4.2, 4.3, 4.4, 4.5.3, 5.2.4): all others default to
application/octet-stream. */
return 'application/octet-stream';
}
}
/**
* Get the primary type of this part.
*
* @return string The primary MIME type of this part.
*/
public function getPrimaryType()
{
return $this->_type;
}
/**
* Get the subtype of this part.
*
* @return string The MIME subtype of this part.
*/
public function getSubType()
{
return $this->_subtype;
}
/**
* Set the character set of this part.
*
* @param string $charset The character set of this part.
*/
public function setCharset($charset)
{
$this->setContentTypeParameter('charset', $charset);
}
/**
* Get the character set to use for this part.
*
* @return string The character set of this part. Returns null if there
* is no character set.
*/
public function getCharset()
{
$charset = $this->getContentTypeParameter('charset');
if (is_null($charset)) {
if ($this->getPrimaryType() != 'text') {
return null;
}
$charset = self::$defaultCharset;
}
return Horde_String::lower($charset);
}
/**
* Set the character set to use when outputting MIME headers.
*
* @param string $charset The character set.
*/
public function setHeaderCharset($charset)
{
$this->_hdrCharset = $charset;
}
/**
* Get the character set to use when outputting MIME headers.
*
* @return string The character set.
*/
public function getHeaderCharset()
{
return is_null($this->_hdrCharset)
? $this->getCharset()
: $this->_hdrCharset;
}
/**
* Set the language(s) of this part.
*
* @param mixed $lang A language string, or an array of language
* strings.
*/
public function setLanguage($lang)
{
$this->_language = is_array($lang)
? $lang
: array($lang);
}
/**
* Get the language(s) of this part.
*
* @param array The list of languages.
*/
public function getLanguage()
{
return $this->_language;
}
/**
* Set the content duration of the data contained in this part (see RFC
* 3803).
*
* @param integer $duration The duration of the data, in seconds. If
* null, clears the duration information.
*/
public function setDuration($duration)
{
if (is_null($duration)) {
unset($this->_duration);
} else {
$this->_duration = intval($duration);
}
}
/**
* Get the content duration of the data contained in this part (see RFC
* 3803).
*
* @return integer The duration of the data, in seconds. Returns null if
* there is no duration information.
*/
public function getDuration()
{
return isset($this->_duration)
? $this->_duration
: null;
}
/**
* Set the description of this part.
*
* @param string $description The description of this part.
*/
public function setDescription($description)
{
$this->_description = $description;
}
/**
* Get the description of this part.
*
* @param boolean $default If the description parameter doesn't exist,
* should we use the name of the part?
*
* @return string The description of this part.
*/
public function getDescription($default = false)
{
$desc = $this->_description;
if ($default && empty($desc)) {
$desc = $this->getName();
}
return $desc;
}
/**
* Set the transfer encoding to use for this part. Only needed in the
* following circumstances:
* 1.) Indicate what the transfer encoding is if the data has not yet been
* set in the object (can only be set if there presently are not
* any contents).
* 2.) Force the encoding to a certain type on a toString() call (if
* 'send' is true).
*
* @param string $encoding The transfer encoding to use.
* @param array $options Additional options:
* - send: (boolean) If true, use $encoding as the sending encoding.
* DEFAULT: $encoding is used to change the base encoding.
*/
public function setTransferEncoding($encoding, $options = array())
{
if (empty($encoding) ||
(empty($options['send']) && !empty($this->_contents))) {
return;
}
$encoding = Horde_String::lower($encoding);
if (in_array($encoding, self::$encodingTypes)) {
if (empty($options['send'])) {
$this->_transferEncoding = $encoding;
} else {
$this->_temp['sendEncoding'] = $encoding;
}
} elseif (empty($options['send'])) {
/* RFC 2045: Any entity with unrecognized encoding must be treated
* as if it has a Content-Type of "application/octet-stream"
* regardless of what the Content-Type field actually says. */
$this->setType('application/octet-stream');
$this->_transferEncoding = self::UNKNOWN;
}
}
/**
* Add a MIME subpart.
*
* @param Horde_Mime_Part $mime_part Add a subpart to the current object.
*/
public function addPart($mime_part)
{
$this->_parts[] = $mime_part;
$this->_reindex = true;
}
/**
* Get a list of all MIME subparts.
*
* @return array An array of the Horde_Mime_Part subparts.
*/
public function getParts()
{
return $this->_parts;
}
/**
* Retrieve a specific MIME part.
*
* @param string $id The MIME ID to get.
*
* @return Horde_Mime_Part The part requested or null if the part doesn't
* exist.
*/
public function getPart($id)
{
return $this->_partAction($id, 'get');
}
/**
* Remove a subpart.
*
* @param string $id The MIME ID to delete.
*
* @param boolean Success status.
*/
public function removePart($id)
{
return $this->_partAction($id, 'remove');
}
/**
* Alter a current MIME subpart.
*
* @param string $id The MIME ID to alter.
* @param Horde_Mime_Part $mime_part The MIME part to store.
*
* @param boolean Success status.
*/
public function alterPart($id, $mime_part)
{
return $this->_partAction($id, 'alter', $mime_part);
}
/**
* Function used to find a specific MIME part by ID and perform an action
* on it.
*
* @param string $id The MIME ID.
* @param string $action The action to perform ('get',
* 'remove', or 'alter').
* @param Horde_Mime_Part $mime_part The object to use for 'alter'.
*
* @return mixed See calling functions.
*/
protected function _partAction($id, $action, $mime_part = null)
{
$this_id = $this->getMimeId();
/* Need strcmp() because, e.g., '2.0' == '2'. */
if (($action === 'get') && (strcmp($id, $this_id) === 0)) {
return $this;
}
if ($this->_reindex) {
$this->buildMimeIds(is_null($this_id) ? '1' : $this_id);
}
foreach ($this->_parts as $key => $val) {
$partid = $val->getMimeId();
if (($match = (strcmp($id, $partid) === 0)) ||
(strpos($id, $partid . '.') === 0) ||
(strrchr($partid, '.') === '.0')) {
switch ($action) {
case 'alter':
if ($match) {
$mime_part->setMimeId($partid);
$this->_parts[$key] = $mime_part;
return true;
}
return $val->alterPart($id, $mime_part);
case 'get':
return $match
? $val
: $val->getPart($id);
case 'remove':
if ($match) {
unset($this->_parts[$key]);
$this->_reindex = true;
return true;
}
return $val->removePart($id);
}
}
}
return ($action === 'get') ? null : false;
}
/**
* Add a content type parameter to this part.
*
* @param string $label The disposition parameter label.
* @param string $data The disposition parameter data.
*/
public function setContentTypeParameter($label, $data)
{
$this->_contentTypeParams[$label] = $data;
}
/**
* Clears a content type parameter from this part.
*
* @param string $label The disposition parameter label.
* @param string $data The disposition parameter data.
*/
public function clearContentTypeParameter($label)
{