/
UIxMailPartHTMLViewer.m
1047 lines (918 loc) · 31.4 KB
/
UIxMailPartHTMLViewer.m
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
/* UIxMailPartHTMLViewer.m - this file is part of SOGo
*
* Copyright (C) 2007-2019 Inverse inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#import <Foundation/NSDictionary.h>
#import <Foundation/NSData.h>
#import <SaxObjC/SaxLexicalHandler.h>
#import <SaxObjC/SaxXMLReaderFactory.h>
#import <NGExtensions/NSString+misc.h>
#import <NGExtensions/NSString+Encoding.h>
#import <NGMime/NGMimeBodyPart.h>
#import <NGMime/NGMimeType.h>
#include <libxml/encoding.h>
#import <SoObjects/SOGo/NSString+Utilities.h>
#import <SoObjects/Mailer/NSData+Mail.h>
#import <SoObjects/Mailer/SOGoMailAccounts.h>
#import <SoObjects/Mailer/SOGoMailObject.h>
#import <SoObjects/Mailer/SOGoMailBodyPart.h>
#import "UIxMailPartHTMLViewer.h"
#if 0
#define showWhoWeAre() NSLog(@"invoked '%@'", NSStringFromSelector(_cmd))
#else
#define showWhoWeAre()
#endif
/* Tags that are forbidden within the body of the html content */
static NSArray *BannedTags = nil;
/* Tags that can't have any contents (no end tag) */
static NSArray *VoidTags= nil;
static xmlCharEncoding
_xmlCharsetForCharset (NSString *charset)
{
struct { NSString *name; xmlCharEncoding encoding; } xmlEncodings[] = {
{ @"us-ascii", XML_CHAR_ENCODING_ASCII},
{ @"utf-8", XML_CHAR_ENCODING_UTF8},
{ @"utf8", XML_CHAR_ENCODING_UTF8}, // broken mailers
{ @"utf-16le", XML_CHAR_ENCODING_UTF16LE},
{ @"utf-16be", XML_CHAR_ENCODING_UTF16BE},
{ @"ucs-4le", XML_CHAR_ENCODING_UCS4LE},
{ @"ucs-4be", XML_CHAR_ENCODING_UCS4BE},
{ @"ebcdic", XML_CHAR_ENCODING_EBCDIC},
// { @"iso-10646" , XML_CHAR_ENCODING_UCS4_2143},
// { , XML_CHAR_ENCODING_UCS4_3412},
// { @"ucs-2", XML_CHAR_ENCODING_UCS2},
{ @"iso8859_1", XML_CHAR_ENCODING_8859_1},
{ @"iso-8859-1", XML_CHAR_ENCODING_8859_1},
{ @"iso-8859-2", XML_CHAR_ENCODING_8859_2},
{ @"iso-8859-3", XML_CHAR_ENCODING_8859_3},
{ @"iso-8859-4", XML_CHAR_ENCODING_8859_4},
{ @"iso-8859-5", XML_CHAR_ENCODING_8859_5},
{ @"iso-8859-6", XML_CHAR_ENCODING_8859_6},
{ @"iso-8859-7", XML_CHAR_ENCODING_8859_7},
{ @"iso-8859-8", XML_CHAR_ENCODING_8859_8},
{ @"iso-8859-9", XML_CHAR_ENCODING_8859_9},
{ @"iso-8859-13", XML_CHAR_ENCODING_ERROR},
{ @"iso-2022-jp", XML_CHAR_ENCODING_2022_JP},
// { @"iso-2022-jp", XML_CHAR_ENCODING_SHIFT_JIS},
{ @"koi8-r", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger koi8-r -> utf8 conversion
{ @"windows-1250", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger windows-1250 -> utf8 conversion
{ @"windows-1251", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger windows-1251 -> utf8 conversion
{ @"windows-1255", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger windows-1255 -> utf8 conversion
{ @"windows-1256", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger windows-1255 -> utf8 conversion
{ @"windows-1257", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger windows-1257 -> utf8 conversion
{ @"gb2312", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger gb2312 -> utf8 conversion
{ @"gbk", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger gb2312 -> utf8 conversion
{ @"gb18030", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger gb2312 -> utf8 conversion
{ @"big5", XML_CHAR_ENCODING_ERROR}, // unsupported, will trigger gb2312 -> utf8 conversion
{ @"euc-jp", XML_CHAR_ENCODING_EUC_JP}};
unsigned count;
xmlCharEncoding encoding;
encoding = XML_CHAR_ENCODING_NONE;
count = 0;
while (encoding == XML_CHAR_ENCODING_NONE
&& count < (sizeof (xmlEncodings) / sizeof (xmlEncodings[0])))
if ([charset isEqualToString: xmlEncodings[count].name])
encoding = xmlEncodings[count].encoding;
else
count++;
if (encoding == XML_CHAR_ENCODING_NONE)
encoding = XML_CHAR_ENCODING_8859_1;
return encoding;
}
@interface _UIxHTMLMailContentHandler : NSObject <SaxContentHandler, SaxLexicalHandler>
{
NSMutableString *result;
NSMutableString *css;
NSDictionary *attachmentIds;
int ignoredContent, embeddedCSSLevel;
NSString *ignoreTag;
BOOL inBody;
BOOL inStyle;
BOOL inCSSDeclaration;
BOOL hasEmbeddedCSS;
xmlCharEncoding contentEncoding;
}
- (NSString *) result;
@end
@implementation _UIxHTMLMailContentHandler
+ (void) initialize
{
if (!BannedTags)
BannedTags = [[NSArray alloc] initWithObjects: @"script", @"frameset",
@"frame", @"iframe", @"applet", @"link",
@"base", @"meta", @"title", nil];
if (!VoidTags)
{
/* see http://www.w3.org/TR/html4/index/elements.html */
VoidTags = [[NSArray alloc] initWithObjects: @"area", @"base",
@"basefont", @"br", @"col", @"frame", @"hr",
@"img", @"input", @"isindex", @"link",
@"meta", @"param", @"", nil];
}
}
- (id) init
{
if ((self = [super init]))
{
css = nil;
result = nil;
ignoreTag = nil;
attachmentIds = nil;
contentEncoding = XML_CHAR_ENCODING_UTF8;
}
return self;
}
- (void) dealloc
{
[result release];
[css release];
[ignoreTag release];
[super dealloc];
}
- (void) setContentEncoding: (xmlCharEncoding) newContentEncoding
{
contentEncoding = newContentEncoding;
}
- (xmlCharEncoding) contentEncoding
{
return contentEncoding;
}
- (void) setAttachmentIds: (NSDictionary *) newAttachmentIds
{
attachmentIds = newAttachmentIds;
}
- (NSString *) css
{
return css;
}
- (NSString *) result
{
return result;
}
/* SaxContentHandler */
- (void) startDocument
{
showWhoWeAre();
[css release];
[result release];
result = [NSMutableString new];
css = [NSMutableString new];
ignoredContent = 0;
[ignoreTag release];
ignoreTag = nil;
inBody = NO;
inStyle = NO;
inCSSDeclaration = NO;
hasEmbeddedCSS = NO;
embeddedCSSLevel = 0;
}
- (void) endDocument
{
showWhoWeAre();
}
- (void) startPrefixMapping: (NSString *)_prefix
uri: (NSString *)_uri
{
showWhoWeAre();
}
- (void) endPrefixMapping: (NSString *)_prefix
{
showWhoWeAre();
}
/**
* About CSS At-Rules (https://css-tricks.com/the-at-rules-of-css/)
*
* At-Rules follow two possible synthaxes:
*
* @[KEYWORD] (RULE);
* Examples:
* @charset "UTF-8";
* @import 'global.css';
* @namespace svg url(http://www.w3.org/2000/svg);
*
* @[KEYWORD] { (Nested Statements) }
* Examples:
* @font-face {
* font-family: 'MyWebFont';
* src: url('myfont.woff2') format('woff2'),
* url('myfont.woff') format('woff');
* }
* @media only screen
* and (min-device-width: 320px)
* and (max-device-width: 480px)
* and (-webkit-min-device-pixel-ratio: 2) {
* .module { width: 100%; }
* }
*/
- (void) _appendStyle: (unichar *) _chars
length: (NSUInteger) _len
{
NSMutableString *sanitizedStyle, *declaration, *rule;
NSUInteger count, length, max;
unichar *sanitizedChars, *start, *currentChar;
BOOL inComment;
/**
* Sanitize style
* - remove control characters
* - remove HTML comment delimiters
* - remove CSS comments
*/
sanitizedStyle = [NSMutableString string];
inComment = NO;
start = _chars;
for (count = 0; count < _len; count++)
{
currentChar = _chars + count;
if (*currentChar < 32)
{
// Ignore control characters
if (!inComment && currentChar > start)
[sanitizedStyle appendString: [NSString stringWithCharacters: start
length: (currentChar - start)]];
start = currentChar + 1;
}
else
{
if ((currentChar < _chars + _len - 3) &&
*currentChar == '<' &&
*(currentChar+1) == '!' &&
*(currentChar+2) == '-' &&
*(currentChar+3) == '-')
{
// Ignore starting HTML comment
if (!inComment && currentChar > start)
[sanitizedStyle appendString: [NSString stringWithCharacters: start
length: (currentChar - start)]];
start = currentChar + 4;
}
else if ((currentChar < _chars + _len - 2) &&
*currentChar == '-' &&
*(currentChar+1) == '-' &&
*(currentChar+2) == '>')
{
// Ignore ending HTML comment
if (!inComment && currentChar > start)
[sanitizedStyle appendString: [NSString stringWithCharacters: start
length: (currentChar - start)]];
start = currentChar + 3;
}
if (currentChar < _chars + _len - 1)
{
// Ignore CSS comments
if (*currentChar == '/' && *(currentChar+1) == '*')
{
inComment = YES;
if (currentChar > start)
[sanitizedStyle appendString: [NSString stringWithCharacters: start
length: (currentChar - start)]];
}
else if (*currentChar == '*' && *(currentChar+1) == '/')
{
inComment = NO;
start = currentChar + 2;
}
}
}
}
if (!inComment && currentChar > start)
[sanitizedStyle appendString: [NSString stringWithCharacters: start
length: (currentChar - start)]];
/**
* Parse sanitized style
* - remove at-rule definitions
* - add custom class to selectors
* - add !important suffix to all rules
*/
rule = [NSMutableString string];
max = [sanitizedStyle length];
sanitizedChars = NSZoneMalloc (NULL, max * sizeof (unichar));
[sanitizedStyle getCharacters: sanitizedChars];
start = sanitizedChars;
currentChar = start;
for (count = 0; count < max; count++)
{
currentChar = sanitizedChars + count;
if (inCSSDeclaration)
{
if (*currentChar == '}')
{
// Prefix CSS rule including ending curly bracket
inCSSDeclaration = NO;
length = (currentChar - start) + 1;
[declaration appendString: [NSString stringWithCharacters: start length: length]];
[css appendString: declaration];
start = currentChar + 1;
}
else if (*currentChar == ';')
{
// Add !important
if ((currentChar < sanitizedChars + 10) ||
!((*(currentChar-1) == 't' || *(currentChar-1) == 'T') &&
(*(currentChar-2) == 'n' || *(currentChar-2) == 'N') &&
(*(currentChar-3) == 'a' || *(currentChar-3) == 'A') &&
(*(currentChar-4) == 't' || *(currentChar-4) == 'T') &&
(*(currentChar-5) == 'r' || *(currentChar-5) == 'R') &&
(*(currentChar-6) == 'o' || *(currentChar-6) == 'O') &&
(*(currentChar-7) == 'p' || *(currentChar-7) == 'P') &&
(*(currentChar-8) == 'm' || *(currentChar-8) == 'M') &&
(*(currentChar-9) == 'i' || *(currentChar-9) == 'I') &&
*(currentChar-10) == '!'))
{
length = (currentChar - start);
[declaration appendFormat: @"%@ !important;",
[NSString stringWithCharacters: start length: length]];
start = currentChar + 1;
}
}
}
else
{
if (*currentChar == '{')
{
if (hasEmbeddedCSS)
{
embeddedCSSLevel++;
}
else
{
// Start of rule declaration
inCSSDeclaration = YES;
length = (currentChar - start);
[rule appendFormat: @".SOGoHTMLMail-CSS-Delimiter %@ {",
[NSString stringWithCharacters: start length: length]];
[css appendString: rule];
rule = [NSMutableString string];
declaration = [NSMutableString string];
}
start = currentChar + 1;
}
if (*currentChar == '}')
{
if (hasEmbeddedCSS)
{
embeddedCSSLevel--;
if (embeddedCSSLevel <= 0)
hasEmbeddedCSS = NO;
}
else
{
// CSS syntax error: ending declaration character while not in a CSS declaration.
// Ignore eveything from last CSS declaration.
rule = [NSMutableString string];
}
start = currentChar + 1;
}
else if (*currentChar == ',')
{
if (!hasEmbeddedCSS)
{
// Prefix CSS selector
length = (currentChar - start);
[rule appendFormat: @" .SOGoHTMLMail-CSS-Delimiter %@,",
[NSString stringWithCharacters: start length: length]];
}
start = currentChar + 1;
}
else if (*currentChar == '@')
{
// Start of at-rule definition
hasEmbeddedCSS = YES;
embeddedCSSLevel = 0;
}
}
}
if (currentChar > start)
{
[css appendString: [NSString stringWithCharacters: start
length: (currentChar - start)]];
}
}
- (void) startElement: (NSString *) _localName
namespace: (NSString *) _ns
rawName: (NSString *) _rawName
attributes: (id <SaxAttributes>) _attributes
{
unsigned int count, max;
NSString *name, *value, *cid, *lowerName;
NSMutableString *resultPart;
BOOL skipAttribute;
showWhoWeAre();
lowerName = [_localName lowercaseString];
if (inStyle || ignoredContent)
;
else if ([lowerName isEqualToString: @"base"])
;
else if ([lowerName isEqualToString: @"meta"])
;
else if ([lowerName isEqualToString: @"body"])
inBody = YES;
else if ([lowerName isEqualToString: @"style"])
inStyle = YES;
else if (inBody)
{
if ([BannedTags containsObject: lowerName])
{
if (!ignoredContent)
ignoreTag = [lowerName copy];
ignoredContent++;
}
else
{
resultPart = [NSMutableString string];
[resultPart appendFormat: @"<%@", _rawName];
if ([VoidTags containsObject: lowerName])
{
if (!ignoredContent)
ignoreTag = [lowerName copy];
ignoredContent++;
}
max = [_attributes count];
for (count = 0; count < max; count++)
{
skipAttribute = NO;
name = [[_attributes nameAtIndex: count] lowercaseString];
if ([name hasPrefix: @"ON"])
skipAttribute = YES;
else if ([name isEqualToString: @"src"])
{
value = [_attributes valueAtIndex: count];
if ([value hasPrefix: @"cid:"])
{
cid = [NSString stringWithFormat: @"<%@>",
[value substringFromIndex: 4]];
value = [attachmentIds objectForKey: cid];
skipAttribute = (value == nil);
}
else if ([lowerName isEqualToString: @"img"])
{
/* [resultPart appendString:
@"src=\"/SOGo.woa/WebServerResources/empty.gif\""]; */
name = @"unsafe-src";
}
else
skipAttribute = YES;
}
else if ([name isEqualToString: @"background"] ||
(([name isEqualToString: @"data"]
|| [name isEqualToString: @"classid"])
&& [lowerName isEqualToString: @"object"]))
{
value = [_attributes valueAtIndex: count];
name = [NSString stringWithFormat: @"unsafe-%@", name];
}
else if ([name isEqualToString: @"href"]
|| [name isEqualToString: @"action"])
{
value = [_attributes valueAtIndex: count];
skipAttribute = ([value rangeOfString: @"://"].location
== NSNotFound
&& ![value hasPrefix: @"mailto:"]
&& ![value hasPrefix: @"#"]);
if (!skipAttribute)
[resultPart appendString: @" rel=\"noopener\""];
}
// Avoid: <div style="background:url('http://www.sogo.nu/fileadmin/sogo/logos/sogo.bts.png' ); width: 200px; height: 200px;" title="ssss">
else if ([name isEqualToString: @"style"])
{
value = [_attributes valueAtIndex: count];
if ([value rangeOfString: @"url" options: NSCaseInsensitiveSearch].location != NSNotFound)
name = [NSString stringWithFormat: @"unsafe-%@", name];
}
else if (
// Mouse Events
[name isEqualToString: @"onclick"] ||
[name isEqualToString: @"ondblclick"] ||
[name isEqualToString: @"onmousedown"] ||
[name isEqualToString: @"onmousemove"] ||
[name isEqualToString: @"onmouseout"] ||
[name isEqualToString: @"onmouseup"] ||
[name isEqualToString: @"onmouseover"] ||
[name isEqualToString: @"onpointerrawupdate"] ||
// Keyboard Events
[name isEqualToString: @"onkeydown"] ||
[name isEqualToString: @"onkeypress"] ||
[name isEqualToString: @"onkeyup"] ||
// Frame/Object Events
[name isEqualToString: @"onabort"] ||
[name isEqualToString: @"onerror"] ||
[name isEqualToString: @"onload"] ||
[name isEqualToString: @"onresize"] ||
[name isEqualToString: @"onscroll"] ||
[name isEqualToString: @"onunload"] ||
// Form Events
[name isEqualToString: @"onblur"] ||
[name isEqualToString: @"onchange"] ||
[name isEqualToString: @"onfocus"] ||
[name isEqualToString: @"onreset"] ||
[name isEqualToString: @"onselect"] ||
[name isEqualToString: @"onsubmit"] ||
// Media Events
[name isEqualToString: @"oncanplay"] ||
[name isEqualToString: @"oncanplaythrough"] ||
[name isEqualToString: @"oncuechange"] ||
[name isEqualToString: @"ondurationchange"] ||
[name isEqualToString: @"onemptied"] ||
[name isEqualToString: @"onended"] ||
[name isEqualToString: @"onloadeddata"] ||
[name isEqualToString: @"onloadedmetadata"] ||
[name isEqualToString: @"onloadstart"] ||
[name isEqualToString: @"onpause"] ||
[name isEqualToString: @"onplay"] ||
[name isEqualToString: @"onplaying"] ||
[name isEqualToString: @"onprogress"] ||
[name isEqualToString: @"onratechange"] ||
[name isEqualToString: @"onseeked"] ||
[name isEqualToString: @"onseeking"] ||
[name isEqualToString: @"onstalled"] ||
[name isEqualToString: @"onsuspend"] ||
[name isEqualToString: @"ontimeupdate"] ||
[name isEqualToString: @"onvolumechange"] ||
[name isEqualToString: @"onwaiting"])
{
skipAttribute = YES;
}
else
value = [_attributes valueAtIndex: count];
if (!skipAttribute)
[resultPart appendFormat: @" %@=\"%@\"",
name, [value stringByReplacingString: @"\""
withString: @""]];
}
if ([VoidTags containsObject: lowerName])
[resultPart appendString: @"/"];
[resultPart appendString: @">"];
[result appendString: resultPart];
}
}
}
- (void) _finishCSS
{
[css replaceString: @"<!--" withString: @""];
[css replaceString: @"-->" withString: @""];
[css replaceString: @".SOGoHTMLMail-CSS-Delimiter body"
withString: @".SOGoHTMLMail-CSS-Delimiter"];
}
- (void) endElement: (NSString *) _localName
namespace: (NSString *) _ns
rawName: (NSString *) _rawName
{
NSString *lowerName;
showWhoWeAre();
lowerName = [_localName lowercaseString];
if (ignoredContent)
{
if ([lowerName isEqualToString: ignoreTag])
{
ignoredContent--;
if (!ignoredContent)
{
[ignoreTag release];
ignoreTag = nil;
}
}
}
else
{
if (inStyle)
{
if ([lowerName isEqualToString: @"style"])
{
inStyle = NO;
inCSSDeclaration = NO;
}
}
else if (inBody)
{
if ([lowerName isEqualToString: @"body"])
{
// Assume <body> never ends to properly display incorrectly constructed messages.
// See bug #4492
// inBody = NO;
if (css)
[self _finishCSS];
}
else
{
//NSLog (@"%@", _localName);
[result appendFormat: @"</%@>", _localName];
}
}
}
}
- (void) characters: (unichar *) _chars
length: (NSUInteger) _len
{
showWhoWeAre();
if (!ignoredContent)
{
if (inStyle)
[self _appendStyle: _chars length: _len];
else if (inBody)
{
NSString *s;
s = [NSString stringWithCharacters: _chars length: _len];
// HACK: This is to avoid appending the useless junk in the <html> tag
// that Outlook adds. It seems to confuse the XML parser for
// forwarded messages as we get this in the _body_ of the email
// while we really aren't in it!
if (![s hasPrefix: @" xmlns:v=\"urn:schemas-microsoft-com:vml\""])
[result appendString: [s stringByEscapingHTMLString]];
}
}
}
- (void) ignorableWhitespace: (unichar *) _chars
length: (NSUInteger) _len
{
showWhoWeAre();
}
- (void) processingInstruction: (NSString *) _pi
data: (NSString *) _data
{
showWhoWeAre();
}
- (void) setDocumentLocator: (id <NSObject, SaxLocator>) _locator
{
showWhoWeAre();
}
- (void) skippedEntity: (NSString *) _entityName
{
showWhoWeAre();
}
/* SaxLexicalHandler */
- (void) comment: (unichar *) _chars
length: (int) _len
{
showWhoWeAre();
if (inStyle)
[self _appendStyle: _chars length: _len];
}
- (void) startDTD: (NSString *) _name
publicId: (NSString *) _pub
systemId: (NSString *) _sys
{
showWhoWeAre();
}
- (void) endDTD
{
showWhoWeAre();
}
- (void) startEntity: (NSString *) _name
{
showWhoWeAre();
}
- (void) endEntity: (NSString *) _name
{
showWhoWeAre();
}
- (void) startCDATA
{
showWhoWeAre();
}
- (void) endCDATA
{
showWhoWeAre();
}
@end
@interface NSDictionary (SOGoDebug)
- (void) dump;
@end
@implementation NSDictionary (SOGoDebug)
- (void) dump
{
NSEnumerator *keys;
NSString *key;
NSMutableString *dump;
dump = [NSMutableString new];
[dump appendFormat: @"\nNSDictionary dump (%@):\n", self];
keys = [[self allKeys] objectEnumerator];
key = [keys nextObject];
while (key)
{
[dump appendFormat: @"%@: %@\n", key, [self objectForKey: key]];
key = [keys nextObject];
}
[dump appendFormat: @"--- end ---\n"];
NSLog(@"%@", dump);
[dump release];
}
@end
@implementation UIxMailPartHTMLViewer
- (id) init
{
if ((self = [super init]))
{
handler = nil;
}
return self;
}
- (void) dealloc
{
[handler release];
[super dealloc];
}
- (xmlCharEncoding) _xmlCharEncoding
{
NSString *charset;
charset = [[bodyInfo objectForKey:@"parameterList"]
objectForKey: @"charset"];
if (![charset length])
charset = @"us-ascii";
return _xmlCharsetForCharset([charset lowercaseString]);
}
- (void) _parseContent
{
NSObject <SaxXMLReader> *parser;
NSData *preparsedContent;
NSMutableData *htmlContent;
NSString *s;
xmlCharEncoding enc;
if ([[self decodedFlatContent] isKindOfClass: [NGMimeBodyPart class]])
preparsedContent = [[[self decodedFlatContent] body] sanitizedContentUsingVoidTags: VoidTags];
else
preparsedContent = [[self decodedFlatContent] sanitizedContentUsingVoidTags: VoidTags];
parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
createXMLReaderForMimeType: @"text/html"];
handler = [_UIxHTMLMailContentHandler new];
[handler setAttachmentIds: attachmentIds];
// Some broken email messages have some additionnal content outside the main HTML tags which are
// ignored by libxml.
// We surround the whole part with additional HTML tags to render all content.
htmlContent = [NSMutableData dataWithBytes: "<html>" length: 6];
[htmlContent appendData: preparsedContent];
[htmlContent appendBytes: "</html>" length: 7];
preparsedContent = (NSData *)htmlContent;
// We check if we got an unsupported charset. If so
// we convert everything to UTF-16{LE,BE} so it passes
// in libxml2 and also in characters: length: defined
// in this file (that expects unichar:s)
enc = [self _xmlCharEncoding];
if (enc == XML_CHAR_ENCODING_ERROR)
{
s = [NSString stringWithData: preparsedContent
usingEncodingNamed: [[bodyInfo objectForKey:@"parameterList"]
objectForKey: @"charset"]];
// In some rare cases (like #3276), we can get utterly broken email messages where
// HTML parts are wrongly encoded. We try to fall back to UTF-8 if that happens and
// if it still happens, we fall back to ISO-Latin-1.
if (!s)
{
s = [[NSString alloc] initWithData: preparsedContent encoding: NSUTF8StringEncoding];
if (!s)
s = [[NSString alloc] initWithData: preparsedContent encoding: NSISOLatin1StringEncoding];
AUTORELEASE(s);
}
#if BYTE_ORDER == BIG_ENDIAN
preparsedContent = [s dataUsingEncoding: NSUTF16BigEndianStringEncoding];
enc = XML_CHAR_ENCODING_UTF16BE;
#else
preparsedContent = [s dataUsingEncoding: NSUTF16LittleEndianStringEncoding];
enc = XML_CHAR_ENCODING_UTF16LE;
#endif
}
// Let's sanitize the string to make sure libxml doesn't go havoc
if (enc == XML_CHAR_ENCODING_UTF8)
{
s = [[NSString alloc] initWithData: preparsedContent encoding: NSUTF8StringEncoding];
// Again, In some rare cases (like #4513), we can get utterly broken email messages where
// HTML parts are wrongly encoded. We try to fall back to UTF-8 if that happens and
// if it still happens, we fall back to ISO-Latin-1.
if (!s)
s = [[NSString alloc] initWithData: preparsedContent encoding: NSISOLatin1StringEncoding];
preparsedContent = [[s safeString] dataUsingEncoding: NSUTF8StringEncoding];
RELEASE(s);
}
[handler setContentEncoding: enc];
[parser setContentHandler: handler];
[parser parseFromSource: preparsedContent];
}
- (NSString *) cssContent
{
NSString *cssContent, *css;
if (!handler)
[self _parseContent];
css = [handler css];
if ([css length])
cssContent
= [NSString stringWithFormat: @"<style type=\"text/css\">%@</style>",
[handler css]];
else
cssContent = @"";
return cssContent;
}
- (NSString *) flatContentAsString
{
if (!handler)
[self _parseContent];
return [handler result];
}
@end
@implementation UIxMailPartExternalHTMLViewer
- (id) init
{
if ((self = [super init]))
{
handler = nil;
}
return self;
}
- (void) dealloc
{
[handler release];
[super dealloc];
}
- (xmlCharEncoding) _xmlCharEncoding
{
NSString *charset;
charset = [[bodyInfo objectForKey:@"parameterList"]
objectForKey: @"charset"];
if (![charset length])
charset = @"us-ascii";
return _xmlCharsetForCharset([charset lowercaseString]);
}
- (void) _parseContent
{
NSObject <SaxXMLReader> *parser;
NSData *preparsedContent;
SOGoMailBodyPart *part;
NSString *encoding;
xmlCharEncoding enc;
parser = [[SaxXMLReaderFactory standardXMLReaderFactory]
createXMLReaderForMimeType: @"text/html"];
if ([[self decodedFlatContent] isKindOfClass: [NGMimeBodyPart class]])
{
preparsedContent = [[[self decodedFlatContent] body] sanitizedContentUsingVoidTags: VoidTags];
encoding = [[[self decodedFlatContent] contentType] valueOfParameter: @"charset"];
}
else
{
part = [self clientObject];
preparsedContent = [[part fetchBLOB] sanitizedContentUsingVoidTags: VoidTags];
encoding = [[part partInfo] valueForKey: @"encoding"];
}
if (![encoding length])
encoding = @"us-ascii";
handler = [_UIxHTMLMailContentHandler new];
[handler setAttachmentIds: attachmentIds];
// We check if we got an unsupported charset. If so
// we convert everything to UTF-16{LE,BE} so it passes
// in libxml2 and also in characters: length: defined
// in this file (that expects unichar:s)
enc = _xmlCharsetForCharset(encoding);
if (enc == XML_CHAR_ENCODING_ERROR)
{
NSString *s;
s = [NSString stringWithData: preparsedContent
usingEncodingNamed: [[bodyInfo objectForKey:@"parameterList"]
objectForKey: @"charset"]];