/
Contents.php
1413 lines (1243 loc) · 45.4 KB
/
Contents.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 2002-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.horde.org/licenses/gpl.
*
* @category Horde
* @copyright 2002-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
/**
* The IMP_Contents:: class contains all functions related to handling the
* content and output of mail messages in IMP.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2002-2014 Horde LLC
* @license http://www.horde.org/licenses/gpl GPL
* @package IMP
*/
class IMP_Contents
{
/* Mask entries for getSummary(). */
const SUMMARY_BYTES = 1;
const SUMMARY_SIZE = 2;
const SUMMARY_ICON = 4;
const SUMMARY_ICON_RAW = 16384;
const SUMMARY_DESCRIP = 8;
const SUMMARY_DESCRIP_LINK = 16;
const SUMMARY_DOWNLOAD = 32;
const SUMMARY_IMAGE_SAVE = 64;
const SUMMARY_PRINT = 128;
const SUMMARY_PRINT_STUB = 256;
const SUMMARY_STRIP = 512;
/* Rendering mask entries. */
const RENDER_FULL = 1;
const RENDER_INLINE = 2;
const RENDER_INLINE_DISP_NO = 4;
const RENDER_INFO = 8;
const RENDER_INLINE_AUTO = 16;
const RENDER_RAW = 32;
const RENDER_RAW_FALLBACK = 64;
/* Header return type for getHeader(). */
const HEADER_OB = 1;
const HEADER_TEXT = 2;
const HEADER_STREAM = 3;
/**
* Have we scanned for embedded parts?
*
* @var boolean
*/
protected $_build = false;
/**
* The list of MIME IDs that consist of embedded data.
*
* @var array
*/
protected $_embedded = array();
/**
* Message header.
*
* @var mixed
*/
protected $_header;
/**
* The index of the current message.
*
* @var IMP_Indices_Mailbox
*/
protected $_indices;
/**
* The Horde_Mime_Part object for the message.
*
* @var Horde_Mime_Part
*/
protected $_message;
/**
* Cached data for the MIME Viewer objects.
*
* @var object
*/
protected $_viewcache;
/**
* Constructor.
*
* @param mixed $in An IMP_Indices_Mailbox or Horde_Mime_Part object.
*
* @throws IMP_Exception
*/
public function __construct($in)
{
if ($in instanceof Horde_Mime_Part) {
$this->_message = $in;
} else {
$this->_indices = $in;
/* Get the Horde_Mime_Part object for the given UID. */
$query = new Horde_Imap_Client_Fetch_Query();
$query->structure();
if (!($ret = $this->_fetchData($query))) {
$e = new IMP_Exception(_("Error displaying message: message does not exist on server."));
$e->setLogLevel('NOTICE');
throw $e;
}
$this->_message = $ret->getStructure();
}
}
/**
* String representation of object.
*
* @return string The indices string.
*/
public function __toString()
{
return strval($this->getIndicesOb());
}
/**
* Returns the IMAP UID for the current message.
*
* @return integer The message UID.
*/
public function getUid()
{
list(,$uid) = $this->_indices->getSingle();
return $uid;
}
/**
* Returns the IMAP mailbox for the current message.
*
* @return IMP_Mailbox The message mailbox.
*/
public function getMailbox()
{
list($mbox,) = $this->_indices->getSingle();
return $mbox;
}
/**
* Return an IMP_Indices object for the current message.
*
* @return IMP_Indices An indices object.
*/
public function getIndicesOb()
{
return $this->_indices;
}
/**
* Returns the entire body of the message.
*
* @param array $options Additional options:
* - stream: (boolean) If true, return a stream.
* DEFAULT: No
*
* @return mixed The text of the part, or a stream resource if 'stream'
* is true.
*/
public function getBody($options = array())
{
if (!$this->_indices) {
return $this->_message->toString(array(
'headers' => true,
'stream' => !empty($options['stream'])
));
}
$query = new Horde_Imap_Client_Fetch_Query();
$query->bodytext(array(
'peek' => true
));
return ($res = $this->_fetchData($query))
? $res->getBodyText(0, !empty($options['stream']))
: '';
}
/**
* Gets the raw text for one section of the message.
*
* @param integer $id The ID of the MIME part.
* @param array $options Additional options:
* - decode: (boolean) Attempt to decode the bodypart on the remote
* server.
* DEFAULT: No
* - length: (integer) If set, only download this many bytes of the
* bodypart from the server.
* DEFAULT: All data is retrieved.
* - mimeheaders: (boolean) Include the MIME headers also?
* DEFAULT: No
* - stream: (boolean) If true, return a stream.
* DEFAULT: No
*
* @return object Object with the following properties:
* - data: (mixed) The text of the part or a stream resource if 'stream'
* option is true.
* - decode: (string) If 'decode' option is true, and bodypart decoded
* on server, the content-type of the decoded data.
*/
public function getBodyPart($id, $options = array())
{
$ret = new stdClass;
$ret->data = '';
$ret->decode = null;
if (empty($id)) {
return $ret;
}
if (!$this->_indices || $this->isEmbedded($id)) {
if (empty($options['mimeheaders']) ||
in_array($id, $this->_embedded)) {
$ob = $this->getMIMEPart($id, array('nocontents' => true));
if (empty($options['stream'])) {
if (!is_null($ob)) {
$ret->data = $ob->getContents();
}
} else {
$ret->data = is_null($ob)
? fopen('php://temp', 'r+')
: $ob->getContents(array('stream' => true));
}
return $ret;
}
$base_id = new Horde_Mime_Id($id);
while (!in_array($base_id->id, $this->_embedded, true)) {
$base_id->id = $base_id->idArithmetic($base_id::ID_UP);
if (is_null($base_id->id)) {
return $ret;
}
}
$part = $this->getMIMEPart($base_id->id, array('nocontents' => true));
$txt = $part->addMimeHeaders()->toString() .
"\n" .
$part->getContents();
try {
$body = Horde_Mime_Part::getRawPartText($txt, 'header', '1') .
"\n\n" .
Horde_Mime_Part::getRawPartText($txt, 'body', '1');
} catch (Horde_Mime_Exception $e) {
$body = '';
}
if (empty($options['stream'])) {
$ret->data = $body;
return $ret;
}
$ret->data = fopen('php://temp', 'r+');
if (strlen($body)) {
fwrite($ret->data, $body);
fseek($ret->data, 0);
}
return $ret;
}
$query = new Horde_Imap_Client_Fetch_Query();
if (substr($id, -2) === '.0') {
$rfc822 = true;
$id = substr($id, 0, -2);
} else {
$rfc822 = false;
}
if (!isset($options['length']) || !empty($options['length'])) {
$bodypart_params = array(
'decode' => !empty($options['decode']),
'peek' => true
);
if (isset($options['length'])) {
$bodypart_params['start'] = 0;
$bodypart_params['length'] = $options['length'];
}
if ($rfc822) {
$bodypart_params['id'] = $id;
$query->bodyText($bodypart_params);
} else {
$query->bodyPart($id, $bodypart_params);
}
}
if (!empty($options['mimeheaders'])) {
if ($rfc822) {
$query->headerText(array(
'id' => $id,
'peek' => true
));
} else {
$query->mimeHeader($id, array(
'peek' => true
));
}
}
if ($res = $this->_fetchData($query)) {
try {
if (empty($options['mimeheaders'])) {
$ret->decode = $res->getBodyPartDecode($id);
$ret->data = $rfc822
? $res->getBodyText($id, !empty($options['stream']))
: $res->getBodyPart($id, !empty($options['stream']));
return $ret;
} elseif (empty($options['stream'])) {
$ret->data = $rfc822
? ($res->getHeaderText($id) . $res->getBodyText($id))
: ($res->getMimeHeader($id) . $res->getBodyPart($id));
return $ret;
}
if ($rfc822) {
$data = array(
$res->getHeaderText($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM),
$res->getBodyText($id, true)
);
} else {
$data = array(
$res->getMimeHeader($id, Horde_Imap_Client_Data_Fetch::HEADER_STREAM),
$res->getBodyPart($id, true)
);
}
$ret->data = Horde_Stream_Wrapper_Combine::getStream($data);
return $ret;
} catch (Horde_Exception $e) {}
}
if (!empty($options['stream'])) {
$ret->data = fopen('php://temp', 'r+');
}
return $ret;
}
/**
* Returns the full message text.
*
* @param array $options Additional options:
* - stream: (boolean) If true, return a stream for bodytext.
* DEFAULT: No
*
* @return mixed The full message text or a stream resource if 'stream'
* is true.
*/
public function fullMessageText($options = array())
{
if (!$this->_indices) {
return $this->_message->toString();
}
$query = new Horde_Imap_Client_Fetch_Query();
$query->bodyText(array(
'peek' => true
));
if ($res = $this->_fetchData($query)) {
try {
if (empty($options['stream'])) {
return $this->getHeader(self::HEADER_TEXT) . $res->getBodyText(0);
}
return Horde_Stream_Wrapper_Combine::getStream(array(
$this->getHeader(self::HEADER_STREAM),
$res->getBodyText(0, true)
));
} catch (Horde_Exception $e) {}
}
return empty($options['stream'])
? ''
: fopen('php://temp', 'r+');
}
/**
* Returns base header information.
*
* @param integer $type Return type (HEADER_* constant).
*
* @return mixed Either a Horde_Mime_Headers object (HEADER_OB), header
* text (HEADER_TEXT), or a stream resource (HEADER_STREAM).
*/
public function getHeader($type = self::HEADER_OB)
{
return $this->_getHeader($type, false);
}
/**
* Returns base header information and marks the message as seen.
*
* @param integer $type See getHeader().
*
* @return mixed See getHeader().
*/
public function getHeaderAndMarkAsSeen($type = self::HEADER_OB)
{
$mbox = $this->getMailbox();
if ($mbox->readonly) {
$seen = false;
} else {
$seen = true;
if (isset($this->_header)) {
try {
$imp_imap = $mbox->imp_imap;
$imp_imap->store($mbox, array(
'add' => array(
Horde_Imap_Client::FLAG_SEEN
),
'ids' => $imp_imap->getIdsOb($this->getUid())
));
} catch (Exception $e) {}
}
}
return $this->_getHeader($type, $seen);
}
/**
* Returns base header information.
*
* @param integer $type See getHeader().
* @param boolean $seen Mark message as seen?
*
* @return mixed See getHeader().
*/
protected function _getHeader($type, $seen)
{
if (!isset($this->_header)) {
if (!$this->_indices) {
$this->_header = $this->_message->addMimeHeaders();
} else {
$query = new Horde_Imap_Client_Fetch_Query();
$query->headerText(array(
'peek' => !$seen
));
$this->_header = ($res = $this->_fetchData($query))
? $res
: new Horde_Imap_Client_Data_Fetch();
}
}
switch ($type) {
case self::HEADER_OB:
return $this->_indices
? $this->_header->getHeaderText(0, Horde_Imap_Client_Data_Fetch::HEADER_PARSE)
: $this->_header;
case self::HEADER_TEXT:
return $this->_indices
? $this->_header->getHeaderText()
: $this->_header->toString();
case self::HEADER_STREAM:
if ($this->_indices) {
return $this->_header->getHeaderText(0, Horde_Imap_Client_Data_Fetch::HEADER_STREAM);
}
$stream = new Horde_Support_StringStream($this->_header->toString());
$stream->fopen();
return $stream;
}
}
/**
* Returns the Horde_Mime_Part object.
*
* @return Horde_Mime_Part A Horde_Mime_Part object.
*/
public function getMIMEMessage()
{
return $this->_message;
}
/**
* Fetch a part of a MIME message.
*
* @param integer $id The MIME index of the part requested.
* @param array $options Additional options:
* - length: (integer) If set, only download this many bytes of the
* bodypart from the server.
* DEFAULT: All data is retrieved.
* - nocontents: (boolean) If true, don't add the contents to the part
* DEFAULT: Contents are added to the part
*
* @return Horde_Mime_Part The raw MIME part asked for (reference).
*/
public function getMIMEPart($id, $options = array())
{
$this->_buildMessage();
$part = $this->_message->getPart($id);
/* Ticket #9201: Treat 'ISO-8859-1' as 'windows-1252'. 1252 has some
* characters (e.g. euro sign, back quote) not in 8859-1. There
* shouldn't be any issue doing this since the additional code points
* in 1252 don't map to anything in 8859-1. */
if ($part &&
(strcasecmp($part->getCharset(), 'ISO-8859-1') === 0)) {
$part->setCharset('windows-1252');
}
/* Don't download contents of entire body if ID == 0 (indicating the
* body of the main multipart message). I'm pretty sure we never
* want to download the body of that part here. */
if (!empty($id) &&
!is_null($part) &&
(substr($id, -2) != '.0') &&
empty($options['nocontents']) &&
$this->_indices &&
!$part->getContents(array('stream' => true))) {
$body = $this->getBodyPart($id, array(
'decode' => true,
'length' => empty($options['length']) ? null : $options['length'],
'stream' => true
));
$part->setContents($body->data, array(
'encoding' => $body->decode,
'usestream' => true
));
}
return $part;
}
/**
* Render a MIME Part.
*
* @param string $mime_id The MIME ID to render.
* @param integer $mode One of the RENDER_ constants.
* @param array $options Additional options:
* - autodetect: (boolean) Attempt to auto-detect MIME type?
* - mime_part: (Horde_Mime_Part) The MIME part to render.
* - type: (string) Use this MIME type instead of the MIME type
* identified in the MIME part.
*
* @return array See Horde_Mime_Viewer_Base::render(). The following
* fields may also be present in addition to the fields
* defined in Horde_Mime_Viewer_Base:
* - attach: (boolean) Force display of this part as an attachment.
* - js: (array) A list of javascript commands to run after the content
* is displayed on screen.
* - name: (string) Contains the MIME name information.
* - wrap: (string) If present, indicates that this part, and all child
* parts, will be wrapped in a DIV with the given class name.
*/
public function renderMIMEPart($mime_id, $mode, array $options = array())
{
$this->_buildMessage();
$mime_part = empty($options['mime_part'])
? $this->getMIMEPart($mime_id)
: $options['mime_part'];
if (!$mime_part) {
return array($mime_id => null);
}
if (!empty($options['autodetect']) &&
($tempfile = Horde::getTempFile()) &&
($fp = fopen($tempfile, 'w')) &&
!is_null($contents = $mime_part->getContents(array('stream' => true)))) {
rewind($contents);
while (!feof($contents)) {
fwrite($fp, fread($contents, 65536));
}
fclose($fp);
$options['type'] = Horde_Mime_Magic::analyzeFile($tempfile, empty($GLOBALS['conf']['mime']['magic_db']) ? null : $GLOBALS['conf']['mime']['magic_db']);
}
$type = empty($options['type'])
? null
: $options['type'];
$viewer = $GLOBALS['injector']->getInstance('IMP_Factory_MimeViewer')->create($mime_part, array('contents' => $this, 'type' => $type));
switch ($mode) {
case self::RENDER_INLINE:
case self::RENDER_INLINE_AUTO:
case self::RENDER_INLINE_DISP_NO:
$textmode = 'inline';
$limit = $viewer->getConfigParam('limit_inline_size');
if ($limit && ($mime_part->getBytes() > $limit)) {
$data = '';
$status = new IMP_Mime_Status(array(
_("This message part cannot be viewed because it is too large."),
sprintf(_("Click %s to download the data."), $this->linkView($mime_part, 'download_attach', _("HERE")))
));
$status->icon('alerts/warning.png', _("Warning"));
if (method_exists($viewer, 'overLimitText')) {
$data = $viewer->overLimitText();
$status->addText(_("The initial portion of this text part is displayed below."));
}
return array(
$mime_id => array(
'data' => $data,
'name' => '',
'status' => $status,
'type' => 'text/html; charset=' . 'UTF-8'
)
);
}
break;
case self::RENDER_INFO:
$textmode = 'info';
break;
case self::RENDER_RAW:
$textmode = 'raw';
break;
case self::RENDER_RAW_FALLBACK:
$textmode = $viewer->canRender('raw')
? 'raw'
: 'full';
break;
case self::RENDER_FULL:
default:
$textmode = 'full';
break;
}
$ret = $viewer->render($textmode);
if (empty($ret)) {
return ($mode == self::RENDER_INLINE_AUTO)
? $this->renderMIMEPart($mime_id, self::RENDER_INFO, $options)
: array();
}
if (!empty($ret[$mime_id]) && !isset($ret[$mime_id]['name'])) {
$ret[$mime_id]['name'] = $mime_part->getName(true);
}
/* Don't show empty parts. */
if (($textmode == 'inline') &&
!is_null($ret[$mime_id]['data']) &&
!strlen($ret[$mime_id]['data']) &&
!isset($ret[$mime_id]['status'])) {
$ret[$mime_id] = null;
}
return $ret;
}
/**
* Finds the main "body" text part (if any) in a message.
* "Body" data is the first text part in the base MIME part.
*
* @param string $subtype Specifically search for this subtype.
*
* @return string The MIME ID of the main body part.
*/
public function findBody($subtype = null)
{
$this->_buildMessage();
return $this->_message->findBody($subtype);
}
/**
* Generate the preview text.
*
* @return array Array with the following keys:
* - cut: (boolean) Was the preview text cut?
* - text: (string) The preview text.
*/
public function generatePreview()
{
// For preview generation, don't go through overhead of scanning for
// embedded parts. Necessary evil, or else very large parts (e.g
// 5 MB+ text parts) will take ages to scan.
$oldbuild = $this->_build;
$this->_build = true;
$mimeid = $this->findBody();
if (is_null($mimeid)) {
$this->_build = $oldbuild;
return array('cut' => false, 'text' => '');
}
$maxlen = empty($GLOBALS['conf']['msgcache']['preview_size'])
? $GLOBALS['prefs']->getValue('preview_maxlen')
: $GLOBALS['conf']['msgcache']['preview_size'];
// Retrieve 3x the size of $maxlen of bodytext data. This should
// account for any content-encoding & HTML tags.
$pmime = $this->getMIMEPart($mimeid, array('length' => $maxlen * 3));
$ptext = Horde_String::convertCharset($pmime->getContents(), $pmime->getCharset(), 'UTF-8');
if ($pmime->getType() == 'text/html') {
$ptext = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($ptext, 'Html2text');
}
$this->_build = $oldbuild;
if (Horde_String::length($ptext) > $maxlen) {
return array(
'cut' => true,
'text' => Horde_String::truncate($ptext, $maxlen)
);
}
return array(
'cut' => false,
'text' => $ptext
);
}
/**
* Get summary info for a MIME ID.
*
* @param string $id The MIME ID.
* @param integer $mask A bitmask indicating what information to return:
* <pre>
* Always output:
* 'type' = MIME type
*
* IMP_Contents::SUMMARY_BYTES
* Output: parts = 'bytes'
*
* IMP_Contents::SUMMARY_SIZE
* Output: parts = 'size'
*
* IMP_Contents::SUMMARY_ICON
* IMP_Contents::SUMMARY_ICON_RAW
* Output: parts = 'icon'
*
* IMP_Contents::SUMMARY_DESCRIP
* Output: parts = 'description_raw'
*
* IMP_Contents::SUMMARY_DESCRIP_LINK
* Output: parts = 'description'
*
* IMP_Contents::SUMMARY_DOWNLOAD
* Output: parts = 'download', 'download_url'
*
* IMP_Contents::SUMMARY_IMAGE_SAVE
* Output: parts = 'img_save'
*
* IMP_Contents::SUMMARY_PRINT
* IMP_Contents::SUMMARY_PRINT_STUB
* Output: parts = 'print'
*
* IMP_Contents::SUMMARY_STRIP
* Output: parts = 'strip'
* </pre>
*
* @return array An array with the requested information.
*/
public function getSummary($id, $mask = 0)
{
$autodetect_link = false;
$param_array = array();
$this->_buildMessage();
$part = array(
'bytes' => null,
'download' => null,
'download_url' => null,
'id' => $id,
'img_save' => null,
'size' => null,
'strip' => null
);
$mime_part = $this->getMIMEPart($id, array('nocontents' => true));
$mime_type = $mime_part->getType();
/* If this is an attachment that has no specific MIME type info, see
* if we can guess a rendering type. */
if (in_array($mime_type, array('application/octet-stream', 'application/base64'))) {
$mime_type = Horde_Mime_Magic::filenameToMIME($mime_part->getName());
if ($mime_type == $mime_part->getType()) {
$autodetect_link = true;
} else {
$mime_part = clone $mime_part;
$mime_part->setType($mime_type);
$param_array['ctype'] = $mime_type;
}
}
$part['type'] = $mime_type;
/* Is this part an attachment? */
$is_atc = $this->isAttachment($mime_type);
/* Get bytes/size information. */
if (($mask & self::SUMMARY_BYTES) ||
($mask & self::SUMMARY_SIZE)) {
$part['bytes'] = $size = $mime_part->getBytes();
$part['size'] = ($size > 1048576)
? sprintf(_("%s MB"), IMP::numberFormat($size / 1048576, 1))
: sprintf(_("%s KB"), max(round($size / 1024), 1));
}
/* Get part's icon. */
if (($mask & self::SUMMARY_ICON) ||
($mask & self::SUMMARY_ICON_RAW)) {
$part['icon'] = $GLOBALS['injector']->getInstance('IMP_Factory_MimeViewer')->getIcon($mime_type);
if ($mask & self::SUMMARY_ICON) {
$part['icon'] = Horde_Themes_Image::tag($part['icon'], array(
'attr' => array(
'title' => $mime_type
)
));
}
} else {
$part['icon'] = null;
}
/* Get part's description. */
$description = $this->getPartName($mime_part, true);
if ($mask & self::SUMMARY_DESCRIP_LINK) {
if (($can_d = $this->canDisplay($mime_part, self::RENDER_FULL)) ||
$autodetect_link) {
$part['description'] = $this->linkViewJS($mime_part, 'view_attach', htmlspecialchars($description), array('jstext' => sprintf(_("View %s"), $description), 'params' => array_filter(array_merge($param_array, array(
'autodetect' => !$can_d
)))));
} else {
$part['description'] = htmlspecialchars($description);
}
}
if ($mask & self::SUMMARY_DESCRIP) {
$part['description_raw'] = $description;
}
/* Download column. */
if (($mask & self::SUMMARY_DOWNLOAD) &&
$is_atc &&
(is_null($part['bytes']) || $part['bytes'])) {
$part['download'] = $this->linkView($mime_part, 'download_attach', '', array('class' => 'iconImg downloadAtc', 'jstext' => _("Download")));
$part['download_url'] = $this->urlView($mime_part, 'download_attach');
}
/* Display the image save link if the required registry calls are
* present. */
if (($mask & self::SUMMARY_IMAGE_SAVE) &&
$GLOBALS['registry']->hasMethod('images/selectGalleries') &&
($mime_part->getPrimaryType() == 'image')) {
$part['img_save'] = Horde::link('#', _("Save Image in Gallery"), 'iconImg saveImgAtc', null, Horde::popupJs(IMP_Basic_Saveimage::url(), array('params' => array('muid' => strval($this->getIndicesOb()), 'id' => $id), 'height' => 200, 'width' => 450, 'urlencode' => true)) . 'return false;') . '</a>';
}
/* Add print link? */
if ((($mask & self::SUMMARY_PRINT) ||
($mask & self::SUMMARY_PRINT_STUB)) &&
$this->canDisplay($id, self::RENDER_FULL)) {
$part['print'] = ($mask & self::SUMMARY_PRINT)
? $this->linkViewJS($mime_part, 'print_attach', '', array('css' => 'iconImg printAtc', 'jstext' => _("Print"), 'onload' => 'IMP_JS.printWindow', 'params' => $param_array))
: Horde::link('#', _("Print"), 'iconImg printAtc', null, null, null, null, array('mimeid' => $id)) . '</a>';
}
/* Strip Attachment? Allow stripping of base parts other than the
* base multipart and the base text (body) part. */
if (($mask & self::SUMMARY_STRIP) &&
($id != 0) &&
(intval($id) != 1) &&
(strpos($id, '.') === false)) {
$part['strip'] = Horde::link(
Horde::selfUrlParams()->add(array(
'actionID' => 'strip_attachment',
'imapid' => $id,
'muid' => strval($this->getIndicesOb()),
'token' => $GLOBALS['session']->getToken()
)),
_("Strip Attachment"),
'iconImg deleteImg stripAtc',
null,
null,
null,
null,
array('mimeid' => $id)
) . '</a>';
}
return $part;
}
/**
* Return the URL to the download/view page.
*
* @param Horde_Mime_Part $mime_part The MIME part to view.
* @param integer $actionID The actionID to perform.
* @param array $options Additional options:
* - params: (array) A list of any additional parameters that need to be
* passed to the download/view page (key => name).
*
* @return Horde_Url The URL to the download/view page.
*/
public function urlView($mime_part = null, $actionID = 'view_attach',
array $options = array())
{
$params = $this->_urlViewParams($mime_part, $actionID, isset($options['params']) ? $options['params'] : array());
return (strpos($actionID, 'download_') === 0)
? IMP_Contents_View::downloadUrl($mime_part->getName(true), $params)
: Horde::url('view.php', true)->add($params);
}
/**
* Generates the necessary URL parameters for the download/view page.
*
* @param Horde_Mime_Part $mime_part The MIME part to view.
* @param integer $actionID The actionID to perform.
* @param array $params Additional parameters to pass.
*
* @return array The array of parameters.
*/
protected function _urlViewParams($mime_part, $actionID, $params)
{
/* Add the necessary local parameters. */
$params = array_merge($params, array(
'actionID' => $actionID,
'id' => isset($params['id']) ? $params['id'] : $mime_part->getMIMEId()
));
if ($this->_indices) {
$params['muid'] = strval($this->getIndicesOb());
}
return IMP_Contents_View::addToken($params);
}
/**
* Generate a link to the download/view page.
*
* @param Horde_Mime_Part $mime_part The MIME part to view.
* @param integer $actionID The actionID value.
* @param string $text The ESCAPED (!) link text.
* @param array $options Additional parameters:
* - class: (string) The CSS class to use.
* - jstext: (string) The JS text to use.
* - params: (array) A list of any additional parameters that need to be
* passed to the download/view page.
*
* @return string A HTML href link to the download/view page.
*/
public function linkView($mime_part, $actionID, $text, $options = array())
{
$options = array_merge(array(
'class' => null,
'jstext' => $text,
'params' => array()
), $options);
return Horde::link(
$this->urlView($mime_part, $actionID, $options),
$options['jstext'],
$options['class'],
($actionID == 'download_attach') ? null : strval(new Horde_Support_Randomid())
) . $text . '</a>';
}
/**
* Generate a javascript link to the download/view page.
*
* @param Horde_Mime_Part $mime_part The MIME part to view.
* @param string $actionID The actionID to perform.
* @param string $text The ESCAPED (!) link text.
* @param array $options Additional options:
* - css: (string) The CSS class to use.
* - jstext: (string) The javascript link text.
* - onload: (string) A JS function to run when popup window is
* fully loaded.
* - params: (array) A list of any additional parameters that need to be
* passed to download/view page. (key = name)
* - widget: (boolean) If true use Horde::widget() to generate,
* Horde::link() otherwise.
*
* @return string A HTML href link to the download/view page.
*/