forked from petewarden/handmadeimap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handmadeimap.php
executable file
·1029 lines (866 loc) · 33.8 KB
/
handmadeimap.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
/**
* This module implements a subset of the IMAP protocol, using PHP's socket
* interface rather than relying on an extension like php-imap. Why would
* you want to do such a thing?
*
* o This supports pulling down all 'to' recipients when grabbing just headers
* o You can use it to log into ordinary Yahoo accounts, using their mangled
* version of IMAP:
* http://groups.google.com/group/mozilla.dev.apps.thunderbird/browse_thread/thread/546356554c73f8ca
* o It's a lot more hackable to support extensions like oAuth for Gmail:
* http://sites.google.com/site/oauthgoog/Home/oauthimap
*
* Unfortunately it doesn't implement the complete IMAP protocol, only the
* pieces I've needed for my projects. This includes logging in, getting
* lists of mailboxes, messages since a certain date and complete header
* information, but not downloading actual message bodies.
*
* To extend it yourself, check out the RFC spec at:
* http://www.faqs.org/rfcs/rfc3501.html
* The basic process is sending a text command using
* handmade_send_command() and getting the result back from
* handmadeimap_get_command_result().
*
* It uses globals for error checking and parsing, so interleaving commands for
* different connections may cause problems.
*
Licensed under the 2-clause (ie no advertising requirement) BSD license,
making it easy to reuse for commercial or GPL projects:
(c) Pete Warden <pete@petewarden.com> http://petewarden.typepad.com/ - Mar 11th 2010
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
require_once("peteutils.php");
// Each client command requires a unique ID, so that the server can reference it when
// returning results. I use an incrementing counter to generate the ID
$g_currentidcounter = 0;
// These two arrays contain the data returned from the server in response to our
// client commands. The result one is an associative map linking a command id to the
// information returned for that command. I'd originally planned to handle firing off
// multiple simultaneous outstanding commands, but that became complex to support and
// wasn't needed for my usage. Instead, it supports an 'issue command 1', 'get results
// of command 1', 'issue command 2', 'get results of command 2', ... pattern on the
// caller's side, anything more complex may end up confusing it!
// The $g_resultlines entries consist of a two-entry array, 'infoline' which contains
// the data the server returned for that command and 'resultline' which holds any other
// information like error messages that the server returned for that call.
$g_resultlines = array();
$g_infolines = array();
// This function implements the basic client-calling primitive of IMAP, sending the
// given string as a command pre-pended with a unique ID for the server to use as a
// reference in the results it returns
function handmadeimap_send_command($connection, $command)
{
global $g_currentidcounter;
$commandid = "A".$g_currentidcounter;
$g_currentidcounter += 1;
$commandline = "$commandid $command\r\n";
fwrite($connection, $commandline);
pete_log('handmadeimap', "Sent '$commandline'");
return $commandid;
}
// Probably the ugliest function of the whole project, this started off simple but
// ended up with a whole lot of scar tissue as I fixed bugs related to issues like
// escaping string literals. Its basic form is a polling loop that sits waiting for
// the server to return data. As data is returned it's associated with the command
// ID, and parsed into data and information strings for that client call. Once a
// complete result has been returned for the given command, the function returns with
// the results. To detect an error, you need to call handmadeimap_was_command_ok()
function handmadeimap_get_command_result($connection, $commandid)
{
global $g_resultlines;
global $g_infolines;
$g_infolines = array();
$insidequotes = false;
$insideliteral = false;
$chewingliteral = false;
$literallength = 0;
$outputline = '';
$previouschar = '';
while (!feof($connection))
{
if (isset($g_resultlines[$commandid]))
{
return $g_resultlines[$commandid];
}
while (!feof($connection)) {
$outputchar = fgetc($connection);
if ($outputchar===FALSE)
break;
if ($chewingliteral)
{
$literallength -= 1;
if ($outputchar=='"')
$outputline .= '\\';
if ($literallength<1)
{
$chewingliteral = false;
$outputline .= $outputchar;
$outputline .= '"';
}
else if (($outputchar!=chr(10))&&($outputchar!=chr(13)))
{
$outputline .= $outputchar;
}
}
else
{
if ($outputchar=='"')
{
$insidequotes = !$insidequotes;
$outputline .= $outputchar;
}
else if (!$insidequotes && ($outputchar=='{'))
{
$insideliteral = true;
$literallength = 0;
}
else if ($insideliteral && ctype_digit($outputchar))
{
$literallength *= 10;
$literallength += $outputchar;
}
else if ($insideliteral && ($outputchar=='}'))
{
$literallength += 2;
$chewingliteral = true;
$outputline .= '"';
}
else if (($outputchar==chr(10))&&($previouschar==chr(13)))
{
$outputline = trim($outputline);
pete_log('handmadeimap', "Received '$outputline'");
$spacearray = explode(" ", $outputline);
$resultid = $spacearray[0];
if ($resultid=='*')
{
$g_infolines[] = $outputline;
}
else
{
$g_resultlines[$resultid] = array(
'resultline' => $outputline,
'infolines' => $g_infolines,
);
}
$outputline = '';
$insidequotes = false;
$insideliteral = false;
$chewingliteral = false;
$literallength = 0;
}
else
{
$outputline .= $outputchar;
}
}
$previouschar = $outputchar;
}
time_nanosleep(0, 500000);
}
return null;
}
// Checks the command's result to see if it succeeded or failed
function handmadeimap_was_command_ok($commandresult)
{
if (!isset($commandresult))
return false;
if ($commandresult=='')
return false;
$commandparts = explode(' ', $commandresult);
if (count($commandparts)<2)
return false;
return ($commandparts[1]=='OK');
}
// A set of utility functions that the module uses to provide access to a human-
// readable error string in the event that something goes wrong
$g_handmadeimap_error = null;
function handmadeimap_set_error($error)
{
global $g_handmadeimap_error;
$g_handmadeimap_error = $error;
}
function handmadeimap_get_error()
{
global $g_handmadeimap_error;
return $g_handmadeimap_error;
}
function handmadeimap_was_ok()
{
return (handmadeimap_get_error()==null);
}
// Returns a socket connection to the given mail server
function handmadeimap_open_connection($serverurl, $serverport)
{
$fp = fsockopen($serverurl, $serverport, $errno, $errstr, 30);
if ($fp==null)
{
handmadeimap_set_error($errstr);
return null;
}
stream_set_blocking($fp,0);
return $fp;
}
// Closes the mail server connection
function handmadeimap_close_connection($connection)
{
$loginid = handmadeimap_send_command($connection, "LOGOUT");
$loginresult = handmadeimap_get_command_result($connection, $loginid);
$loginwasok = handmadeimap_was_command_ok($loginresult['resultline']);
if (!$loginwasok)
handmadeimap_set_error("LOGOUT failed with '".$loginresult['resultline']."'");
fclose($connection);
}
// Sends the strange command that Yahoo needs for access to its IMAP email accounts. See
// http://groups.google.com/group/mozilla.dev.apps.thunderbird/browse_thread/thread/546356554c73f8ca
function handmadeimap_yahoo_command($connection)
{
$yahoocommandid = handmadeimap_send_command($connection, 'ID ("GUID" "1")');
$yahoocommandresult = handmadeimap_get_command_result($connection, $yahoocommandid);
$yahoocommandwasok = handmadeimap_was_command_ok($yahoocommandresult['resultline']);
if (!$yahoocommandwasok)
handmadeimap_set_error("Yahoo Command failed with '".$yahoocommandresult['resultline']."'");
}
// Performs a standard LOGIN to the mail server
function handmadeimap_login($connection, $user, $password)
{
$loginid = handmadeimap_send_command($connection, "LOGIN $user $password");
$loginresult = handmadeimap_get_command_result($connection, $loginid);
$loginwasok = handmadeimap_was_command_ok($loginresult['resultline']);
if (!$loginwasok)
handmadeimap_set_error("LOGIN failed with '".$loginresult['resultline']."'");
}
// Performs a LOGIN using Google's XOAuth extension, as documented here:
// http://sites.google.com/site/oauthgoog/Home/oauthimap
function handmadeimap_login_xoauth($connection, $loginstring)
{
$loginid = handmadeimap_send_command($connection, "AUTHENTICATE XOAUTH $loginstring");
$loginresult = handmadeimap_get_command_result($connection, $loginid);
$loginwasok = handmadeimap_was_command_ok($loginresult['resultline']);
if (!$loginwasok)
handmadeimap_set_error("LOGIN failed with '".$loginresult['resultline']."'");
}
// Returns the capabilities exposed by this server
function handmadeimap_capability($connection)
{
$capabilityid = handmadeimap_send_command($connection, "CAPABILITY");
$capabilityresult = handmadeimap_get_command_result($connection, $capabilityid);
$capabilitywasok = handmadeimap_was_command_ok($capabilityresult['resultline']);
if (!$capabilitywasok)
handmadeimap_set_error("CAPABILITY failed with '".$capabilityresult['resultline']."'");
}
// Lists the folders available on this account
function handmadeimap_list($connection)
{
$listid = handmadeimap_send_command($connection, 'LIST "" "*"');
$listresult = handmadeimap_get_command_result($connection, $listid);
$listwasok = handmadeimap_was_command_ok($listresult['resultline']);
if (!$listwasok)
handmadeimap_set_error("LIST failed with '".$listresult['resultline']."'");
$result = array();
foreach ($listresult['infolines'] as $infoline)
{
print "$infoline\n";
}
return $result;
}
// Chooses a folder for subsequent operations to act upon, and returns any other
// information that the server returns about that folder
function handmadeimap_select($connection, $mailboxname)
{
$selectid = handmadeimap_send_command($connection, 'SELECT "'.$mailboxname.'"');
$selectresult = handmadeimap_get_command_result($connection, $selectid);
$selectwasok = handmadeimap_was_command_ok($selectresult['resultline']);
if (!$selectwasok)
handmadeimap_set_error("SELECT failed with '".$selectresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($selectresult['infolines'] as $infoline)
{
$infoparts = explode(' ', $infoline);
if (isset($infoparts[2]))
{
if ($infoparts[1]=='FLAGS')
{
// do nothing
}
else if ($infoparts[2]=='EXISTS')
{
$result['totalcount'] = $infoparts[1];
}
else if ($infoparts[2]=='RECENT')
{
$result['recentcount'] = $infoparts[1];
}
}
}
return $result;
}
// Pulls out a list of message indices that were delivered after the given date
function handmadeimap_search_since_date($connection, $date)
{
$searchid = handmadeimap_send_command($connection, 'SEARCH SINCE '.$date);
$searchresult = handmadeimap_get_command_result($connection, $searchid);
$searchwasok = handmadeimap_was_command_ok($searchresult['resultline']);
if (!$searchwasok)
handmadeimap_set_error("SEARCH failed with '".$searchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($searchresult['infolines'] as $infoline)
{
$parts = split(' ', $infoline);
// Remove "* SEARCH"
unset($parts[0]);
unset($parts[1]);
$result = array_merge($result, $parts);
}
return $result;
}
// Finds the index of the earliest message after the given timestamp (only accurate to +- 24 hours)
function handmadeimap_earliest_index_since_time($connection, $unixtime, $totalcount)
{
// The IMAP search ignores the time zone, so bump the time backwards by a day to avoid missing any
$conservativetime = ($unixtime-24*60*60);
$date = date( 'd-M-Y', $conservativetime );
$messageindices = handmadeimap_search_since_date($connection, $date);
$minindex = $totalcount;
foreach ($messageindices as $index)
$minindex = min($minindex, $index);
return $minindex;
}
// IMAP result data is returned in a data structure defined by nested parentheses, in a lisp-esque manner.
// The following functions implement a primitive parser, mostly used to turn ENVELOPE results into usable
// information by the rest of the engine.
function pull_string($line)
{
$result = '';
$countline = strlen($line);
$index = 0;
while ($index<$countline)
{
$char = $line{$index};
if (ctype_space($char))
return $result;
$result .= $char;
$index += 1;
}
return $result;
}
function pull_quoted($line)
{
$result = '';
$countline = strlen($line);
$index = 1;
$lastchar = '';
$charbeforelast = '';
while ($index<$countline)
{
$char = $line{$index};
if (($char=='"')&&($lastchar!='\\'))
return $result;
$result .= $char;
$index += 1;
$lastchar = $char;
}
echo "Missing end quotes for $line";
return $result;
}
function pull_parens($line)
{
$result = '';
$parensdepth = 0;
$isinquotes = false;
$countline = strlen($line);
$index = 1;
$lastchar='';
while ($index<$countline)
{
$char = $line{$index};
if ($isinquotes)
{
if (($char=='"')&&($lastchar!='\\'))
{
$isinquotes = false;
}
}
else
{
if (($char=='"')&&($lastchar!='\\'))
{
$isinquotes = true;
}
else if ($char=='(')
{
$parensdepth += 1;
}
else if ($char==')')
{
$parensdepth -= 1;
if ($parensdepth<0)
return $result;
}
}
$result .= $char;
$index += 1;
$lastchar = $char;
}
global $g_handmadeimap_currentline;
echo "Missing end parens for $g_handmadeimap_currentline";
return $result;
}
function string_to_expression_tree($line)
{
$result = array();
$countline = strlen($line);
$index = 0;
while ($index<$countline)
{
$char = $line[$index];
if ($char=='"')
{
$currentchunk = pull_quoted(substr($line, $index));
$result[] = $currentchunk;
$index += strlen($currentchunk)+2;
}
else if ($char=='(')
{
$currentchunk = pull_parens(substr($line, $index));
$result[] = string_to_expression_tree($currentchunk);
$index += strlen($currentchunk)+2;
}
else if (ctype_space($char))
{
$index += 1;
}
else
{
$currentchunk = pull_string(substr($line, $index));
$result[] = $currentchunk;
$index += strlen($currentchunk);
}
}
return $result;
}
function get_addresses_from_tree($tree)
{
if (is_string($tree)&&($tree=="NIL"))
return array();
if (!is_array($tree))
{
error_log("Not an array: $tree");
global $g_handmadeimap_currentline;
error_log($g_handmadeimap_currentline);
}
$result = array();
foreach ($tree as $current)
{
$currentdisplay = $current[0];
$currentusername = $current[2];
$currentdomain = $current[3];
if ($currentdisplay=="NIL")
$currentdisplay = "";
$currentaddress = $currentusername."@".$currentdomain;
$result[] = array(
'display' => $currentdisplay,
'address' => $currentaddress,
);
}
return $result;
}
$g_handmadeimap_currentline = '';
function handmadeimap_parse_envelope_line($infoline)
{
global $g_handmadeimap_currentline;
$g_handmadeimap_currentline = $infoline;
$infotree = string_to_expression_tree($infoline);
$messagenumber = $infotree[1];
$envelopetree = $infotree[3][1];
$date = $envelopetree[0];
$subject = $envelopetree[1];
$fromtree = $envelopetree[2];
$fromlist = get_addresses_from_tree($fromtree);
$sendertree = $envelopetree[3];
$senderlist = get_addresses_from_tree($sendertree);
$replytotree = $envelopetree[4];
$replytolist = get_addresses_from_tree($replytotree);
$totree = $envelopetree[5];
$tolist = get_addresses_from_tree($totree);
$cctree = $envelopetree[6];
$cclist = get_addresses_from_tree($cctree);
$bcctree = $envelopetree[7];
$bcclist = get_addresses_from_tree($bcctree);
$inreplyto = $envelopetree[8];
$messageuid = $envelopetree[9];
return array(
'messagenumber' => $messagenumber,
'date' => $date,
'subject' => $subject,
'from' => $fromlist,
'sender' => $senderlist,
'replyto' => $replytolist,
'to' => $tolist,
'cc' => $cclist,
'bcc' => $bcclist,
'inreplyto' => $inreplyto,
'messageuid' => $messageuid,
);
}
// This function pulls down the message information for the given indices and parses
// the returned strings into usable data structures
function handmadeimap_fetch_envelopes($connection, $firstindex, $lastindex)
{
$fetchcommand = "FETCH $firstindex:$lastindex (ENVELOPE)";
$fetchid = handmadeimap_send_command($connection, $fetchcommand);
$fetchresult = handmadeimap_get_command_result($connection, $fetchid);
$fetchwasok = handmadeimap_was_command_ok($fetchresult['resultline']);
if (!$fetchwasok)
handmadeimap_set_error("FETCH failed with '".$fetchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($fetchresult['infolines'] as $infoline)
$result[] = handmadeimap_parse_envelope_line($infoline);
return $result;
}
function create_envelope_recipients_xml($recipients, $role)
{
$messagexml = "";
if (!$recipients)
return $messagexml;
foreach ($recipients as $object) {
$display = $object['display'];
$address = $object['address'];
$messagexml .= "<recipient>\n";
$messagexml .= "<address>".$address."</address>\n";
$messagexml .= "<display>".make_cdata($display)."</display>\n";
$messagexml .= "<role>".$role."</role>\n";
$messagexml .= "</recipient>\n";
}
return $messagexml;
}
// Given a set of 'envelopes' containing the information for some messages, return
// an XML string representing that message data
function envelopes_to_xml($envelopes, $sentorreceived)
{
$messagexml = '';
foreach ($envelopes as $headerinfo)
{
$subject = $headerinfo["subject"];
$imapdate = $headerinfo["date"];
$phpdate = strtotime($imapdate);
$date = date( 'Y-m-d H:i:s', $phpdate );
$from = $headerinfo["from"];
$fromcomponents = $from[0];
$fromaddress = $fromcomponents["address"];
$fromdisplay = $fromcomponents["display"];
$sourcefolder = $sentorreceived;
$to = $headerinfo["to"];
$cc = $headerinfo["cc"];
$bcc = $headerinfo["bcc"];
$contenttext = "";
$contenthtml = "";
$sourceuid = $headerinfo['messageuid'];
$messagexml .= "<message>\n";
$messagexml .= make_tag("messageuid", uniqid("",TRUE));
$messagexml .= make_cdatatag("sourceuid", $sourceuid);
$messagexml .= make_cdatatag("subject", $subject);
$messagexml .= make_cdatatag("fromaddress", $fromaddress);
$messagexml .= make_cdatatag("fromdisplay", $fromdisplay);
$messagexml .= make_tag("deliverytime", $date);
$messagexml .= "<recipients>\n";
$messagexml .= create_envelope_recipients_xml($to,"to");
$messagexml .= create_envelope_recipients_xml($cc,"cc");
$messagexml .= create_envelope_recipients_xml($bcc,"bcc");
$messagexml .= "</recipients>\n";
$messagexml .= make_cdatatag("contenttext", $contenttext);
$messagexml .= make_cdatatag("contenthtml", $contenthtml);
$messagexml .= make_tag("sourcefolder", $sourcefolder);
$messagexml .= "</message>\n";
}
return $messagexml;
}
// Given a set of 'envelopes' containing the information for some messages, return
// a PHP data structure representing that message data. It looks something like this:
// array(
// [0] => array(
// 'subject' => 'Hello Pete',
// 'timestamp' => 1234567890,
// 'from' => array('address' => 'pete@mailana.com', 'display' => 'Pete Warden'),
// 'sourcefolder' => 'received',
// 'to' => array( [0] => array( 'address' => 'pete@petewarden.com', 'display' => 'Pete Warden')),
// 'cc' => array( [0] => array( 'address' => 'bob@example.com', 'display' => 'Bob Example')),
// 'bcc' => array(),
// 'sourceuid' => 'ABCDEF012345679',
// )
// ...
// );
//
// The timestamp is in standard UNIX format, seconds from 1970. The sourceuid is the
// unique identifier the ISP has given the message, and the sourcefolder is a way for
// the calling code to keep track of whether you're fetching messages from a sent or
// a received message folder
function envelopes_to_data($envelopes, $sentorreceived)
{
$result = array();
foreach ($envelopes as $headerinfo)
{
$subject = $headerinfo["subject"];
$imapdate = $headerinfo["date"];
$phpdate = strtotime($imapdate);
$from = $headerinfo["from"];
$fromcomponents = $from[0];
$fromaddress = $fromcomponents["address"];
$fromdisplay = $fromcomponents["display"];
$sourcefolder = $sentorreceived;
$to = $headerinfo["to"];
$cc = $headerinfo["cc"];
$bcc = $headerinfo["bcc"];
$sourceuid = $headerinfo['messageuid'];
$result[] = array(
'subject' => $subject,
'timestamp' => $phpdate,
'from' => array('address' => $fromaddress, 'display' => $fromdisplay),
'sourcefolder' => $sourcefolder,
'to' => $to,
'cc' => $cc,
'bcc' => $bcc,
'sourceuid' => $sourceuid,
);
}
return $result;
}
// Runs the envelope parsing code through a test message that originally caused a lot of problems,
// thanks to its heavy use of quoted literals and parentheses
function handmadeimap_test_envelope_parsing()
{
$testline = '* 123 FETCH (ENVELOPE ("Fri, 05 Jun 2009 06:39:46 -0400 (EDT)" "ViewSonic 24\" 1080p LCD $229.99, Flip Video Mino 60M Camcorder $89.99, Plantronics Bluetooth Headset $16.99,.." (("Buy.com Deals" NIL "buy.com_offers" "enews.buy.com")) (("Buy.com Deals" NIL "buy.com_offers" "enews.buy.com")) ((NIL NIL "Buycom-s7ou4ipo6sh" "checkout.l.google.com")) ((NIL NIL "Christophe-b7ou4ipo6sh" "checkout.l.google.com")) NIL NIL NIL "<22389195.945191244198463993.JavaMail.sierra-prod@cgl35.prod.google.com>"))';
$testresult = handmadeimap_parse_envelope_line($testline);
print_r($testresult);
}
/**
* Searches messages bodies with gmail-specific extension to SEARCH.
* @see http://code.google.com/apis/gmail/imap/#x-gm-raw
* @param resource $connection Connection resource (returned from handmadeimap_open_connection())
* @param string $pattern String to search for.
* @return array Array containing ids of matching messages.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_search_gmail_message($connection, $pattern)
{
$searchid = handmadeimap_send_command($connection, 'SEARCH X-GM-RAW "'.$pattern.'"');
$searchresult = handmadeimap_get_command_result($connection, $searchid);
$searchwasok = handmadeimap_was_command_ok($searchresult['resultline']);
if (!$searchwasok)
handmadeimap_set_error("SEARCH failed with '".$searchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($searchresult['infolines'] as $infoline)
{
$parts = explode(' ', $infoline);
// Remove "* SEARCH"
unset($parts[0]);
unset($parts[1]);
$result = array_merge($result, $parts);
}
return $result;
}
/**
* Fetches body of a single message.
* @param resource $connection Connection resource.
* @param int $messageindex Id of the message.
* @return string Raw message body.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_fetch_message_body($connection, $messageindex)
{
$fetchcommand = "FETCH $messageindex (BODY[TEXT])";
$fetchid = handmadeimap_send_command($connection, $fetchcommand);
$fetchresult = handmadeimap_get_command_result($connection, $fetchid);
$fetchwasok = handmadeimap_was_command_ok($fetchresult['resultline']);
if (!$fetchwasok)
handmadeimap_set_error("FETCH failed with '".$fetchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = "";
foreach ($fetchresult['infolines'] as $infoline){
$result .= str_replace("* $messageindex FETCH (BODY[TEXT] ", "", $infoline);
}
$result = quoted_printable_decode($result);
if(preg_match("/Content-Transfer-Encoding: base64(.*)/", $result, $match)){
return base64_decode($match[1]);
}
return $result;
}
/**
* Fetches size of the message according to RFC 822.
* @param resource $connection Connection resource.
* @param int $messageindex Id of the message.
* @return string Message size.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_fetch_size($connection, $messageindex){
$fetchcommand = "FETCH $messageindex RFC822.SIZE";
$fetchid = handmadeimap_send_command($connection, $fetchcommand);
$fetchresult = handmadeimap_get_command_result($connection, $fetchid);
$fetchwasok = handmadeimap_was_command_ok($fetchresult['resultline']);
if (!$fetchwasok)
handmadeimap_set_error("FETCH failed with '".$fetchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = "";
foreach ($fetchresult['infolines'] as $infoline){
$result = str_replace("* $messageindex FETCH (RFC822.SIZE ", "", $infoline);
$result = str_replace(")", "", $result);
}
return $result;
}
/**
* Parses body messages returned from FETCH - removes "* xxx FETCH ...".
* @param int $index Index of the message that was fetched.
* @param string $message Raw message.
* @return string Cleared message.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_parse_message_body($index, $message){
$message = str_replace("* $index FETCH (BODY[TEXT] ", "", $message);
return $message;
}
/**
* Fetches envelope of a single message.
* @param resource $connection Connection resource.
* @param int $messageindex Id of the message.
* @return array Information from the envelope.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_fetch_envelope($connection, $messageindex)
{
$fetchcommand = "FETCH $messageindex (ENVELOPE)";
$fetchid = handmadeimap_send_command($connection, $fetchcommand);
$fetchresult = handmadeimap_get_command_result($connection, $fetchid);
$fetchwasok = handmadeimap_was_command_ok($fetchresult['resultline']);
if (!$fetchwasok)
handmadeimap_set_error("FETCH failed with '".$fetchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($fetchresult['infolines'] as $infoline)
$result[] = handmadeimap_parse_envelope_line($infoline);
return $result;
}
/**
* Fetches flags and envelope of a single message.
* @param resource $connection Connection resource.
* @param int $messageindex Id of the message.
* @return array Basic information, parsed by handmadeimap_parse_flags_line()
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_fetch_flags($connection, $messageindex)
{
$fetchcommand = "FETCH $messageindex (FLAGS ENVELOPE)";
$fetchid = handmadeimap_send_command($connection, $fetchcommand);
$fetchresult = handmadeimap_get_command_result($connection, $fetchid);
$fetchwasok = handmadeimap_was_command_ok($fetchresult['resultline']);
if (!$fetchwasok)
handmadeimap_set_error("FETCH failed with '".$fetchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($fetchresult['infolines'] as $infoline)
$result[] = handmadeimap_parse_flags_line($infoline);
return $result;
}
/**
* Parses flags from the message and returns basic information.
* @param string $line Fetched line.
* @return array Message id, sender, flags and date sent.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_parse_flags_line($line){
$line = string_to_expression_tree($line);
$from = $line[3][3][2][0][0];
if($from == "NIL"){
$from = $line[3][3][2][0][3];
}
$r = array(
"msg_id" => $line[1],
"from" => $from,
"flags" => $line[3][1],
"date" => $line[3][3][0]
);
return $r;
}
/**
* Searches unseen messages sent after given date.
* @param resource $connection Connection resource.
* @param int $messageindex Id of the message.
* @return array List of the messages ids.
* @author Paulina Budzoń <paulina.budzon@gmail.com>
*/
function handmadeimap_search_unread_since_date($connection, $date)
{
$searchid = handmadeimap_send_command($connection, 'SEARCH UNSEEN SINCE '.$date);
$searchresult = handmadeimap_get_command_result($connection, $searchid);
$searchwasok = handmadeimap_was_command_ok($searchresult['resultline']);
if (!$searchwasok)
handmadeimap_set_error("SEARCH failed with '".$searchresult['resultline']."'");
else
handmadeimap_set_error(null);
$result = array();
foreach ($searchresult['infolines'] as $infoline)
{
$parts = split(' ', $infoline);
// Remove "* SEARCH"
unset($parts[0]);
unset($parts[1]);
$result = array_merge($result, $parts);
}
return $result;
}
/**
* Fetches bodies of given messages.
* @param resource $connection Connection resource.
* @param array $messagesindex Ids of the messages to fetch.