wityCMS /helpers/fMailbox/fMailbox.php

Language PHP Lines 1525
MD5 Hash 2d4b8358cbd7c8dae8f5eafdde5a0aac
Repository https://github.com/Creatiwity/wityCMS.git View Raw File
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
<?php
/**
 * Retrieves and deletes messages from a email account via IMAP or POP3
 *
 * All headers, text and html content returned by this class are encoded in
 * UTF-8. Please see http://flourishlib.com/docs/UTF-8 for more information.
 *
 * @copyright  Copyright (c) 2010-2012 Will Bond
 * @author     Will Bond [wb] <will@flourishlib.com>
 * @license    http://flourishlib.com/license
 *
 * @package    Flourish
 * @link       http://flourishlib.com/fMailbox
 *
 * @version    1.0.0b18
 * @changes    1.0.0b18  Fixed a bug in ::fetchMessageSource() where IMAP connections would add an extra `\r\n` to the end of the source [wb, 2012-09-16]
 * @changes    1.0.0b17  Updated the class to be more forgiving when parsing the response for `STATUS` and `FETCH` IMAP commands [wb, 2012-09-15]
 * @changes    1.0.0b16  Added method ::fetchMessageSource() [wb, 2012-09-15]
 * @changes    1.0.0b15  Fixed handling of bounces with no headers [wb, 2012-09-15]
 * @changes    1.0.0b14  Added a workaround for iconv having issues in MAMP 1.9.4+ [wb, 2011-07-26]
 * @changes    1.0.0b13  Fixed handling of headers in relation to encoded-words being embedded inside of quoted strings [wb, 2011-07-26]
 * @changes    1.0.0b12  Enhanced the error checking in ::write() [wb, 2011-06-03]
 * @changes    1.0.0b11  Added code to work around PHP bug #42682 (http://bugs.php.net/bug.php?id=42682) where `stream_select()` doesn't work on 64bit machines from PHP 5.2.0 to 5.2.5, improved connectivity error handling and timeouts while reading data [wb, 2011-01-10]
 * @changes    1.0.0b10  Fixed ::parseMessage() to properly handle a header format edge case and properly set the `text` and `html` keys even when the email has an explicit `Content-disposition: inline` header [wb, 2010-11-25]
 * @changes    1.0.0b9   Fixed a bug in ::parseMessage() that could cause HTML alternate content to be included in the `inline` content array instead of the `html` element [wb, 2010-09-20]
 * @changes    1.0.0b8   Fixed ::parseMessage() to be able to handle non-text/non-html multipart parts that do not have a `Content-disposition` header [wb, 2010-09-18]
 * @changes    1.0.0b7   Fixed a typo in ::read() [wb, 2010-09-07]
 * @changes    1.0.0b6   Fixed a typo from 1.0.0b4 [wb, 2010-07-21]
 * @changes    1.0.0b5   Fixes for increased compatibility with various IMAP and POP3 servers, hacked around a bug in PHP 5.3 on Windows [wb, 2010-06-22]
 * @changes    1.0.0b4   Added code to handle emails without an explicit `Content-type` header [wb, 2010-06-04]
 * @changes    1.0.0b3   Added missing static method callback constants [wb, 2010-05-11]
 * @changes    1.0.0b2   Added the missing ::enableDebugging() [wb, 2010-05-05]
 * @changes    1.0.0b    The initial implementation [wb, 2010-05-05]
 */
class fMailbox
{
	const addSMIMEPair = 'fMailbox::addSMIMEPair';
	const parseMessage = 'fMailbox::parseMessage';
	const reset        = 'fMailbox::reset';


	/**
	 * S/MIME certificates and private keys for verification and decryption
	 *
	 * @var array
	 */
	static private $smime_pairs = array();


	/**
	 * Adds an S/MIME certificate, or certificate + private key pair for verification and decryption of S/MIME messages
	 *
	 * @param string       $email_address         The email address the certificate or private key is for
	 * @param fFile|string $certificate_file      The file the S/MIME certificate is stored in - required for verification and decryption
	 * @param fFile        $private_key_file      The file the S/MIME private key is stored in - required for decryption only
	 * @param string       $private_key_password  The password for the private key
	 * @return void
	 */
	static public function addSMIMEPair($email_address, $certificate_file, $private_key_file=NULL, $private_key_password=NULL)
	{
		if ($private_key_file !== NULL && !$private_key_file instanceof fFile) {
			$private_key_file = new fFile($private_key_file);
		}
		if (!$certificate_file instanceof fFile) {
			$certificate_file = new fFile($certificate_file);
		}
		self::$smime_pairs[strtolower($email_address)] = array(
			'certificate' => $certificate_file,
			'private_key' => $private_key_file,
			'password'    => $private_key_password
		);
	}


	/**
	 * Takes a date, removes comments and cleans up some common formatting inconsistencies
	 *
	 * @param string $date  The date to clean
	 * @return string  The cleaned date
	 */
	static private function cleanDate($date)
	{
		$date = preg_replace('#\([^)]+\)#', ' ', trim($date));
		$date = preg_replace('#\s+#', ' ', $date);
		$date = preg_replace('#(\d+)-([a-z]+)-(\d{4})#i', '\1 \2 \3', $date);
		$date = preg_replace('#^[a-z]+\s*,\s*#i', '', trim($date));
		return trim($date);
	}


	/**
	 * Decodes encoded-word headers of any encoding into raw UTF-8
	 *
	 * @param string $text  The header value to decode
	 * @return string  The decoded UTF-8
	 */
	static private function decodeHeader($text)
	{
		$parts = preg_split('#(=\?[^\?]+\?[QB]\?[^\?]+\?=)#i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

		$part_with_encoding = array();
		$output = '';
		foreach ($parts as $part) {
			if ($part === '') {
				continue;
			}

			if (preg_match_all('#=\?([^\?]+)\?([QB])\?([^\?]+)\?=#i', $part, $matches, PREG_SET_ORDER)) {
				foreach ($matches as $match) {
					if (strtoupper($match[2]) == 'Q') {
						$part_string = rawurldecode(strtr(
							$match[3],
							array(
								'=' => '%',
								'_' => ' '
							)
						));
					} else {
						$part_string = base64_decode($match[3]);
					}
					$lower_encoding = strtolower($match[1]);
					$last_key = count($part_with_encoding) - 1;
					if (isset($part_with_encoding[$last_key]) && $part_with_encoding[$last_key]['encoding'] == $lower_encoding) {
						$part_with_encoding[$last_key]['string'] .= $part_string;
					} else {
						$part_with_encoding[] = array('encoding' => $lower_encoding, 'string' => $part_string);
					}
				}

			} else {
				$last_key = count($part_with_encoding) - 1;
				if (isset($part_with_encoding[$last_key]) && $part_with_encoding[$last_key]['encoding'] == 'iso-8859-1') {
					$part_with_encoding[$last_key]['string'] .= $part;
				} else {
					$part_with_encoding[] = array('encoding' => 'iso-8859-1', 'string' => $part);
				}
			}
		}

		foreach ($part_with_encoding as $part) {
			$output .= self::iconv($part['encoding'], 'UTF-8', $part['string']);
		}

		return $output;
	}


	/**
	 * Handles an individual part of a multipart message
	 *
	 * @param array  $info       An array of information about the message
	 * @param array  $structure  An array describing the structure of the message
	 * @return array  The modified $info array
	 */
	static private function handlePart($info, $structure)
	{
		if ($structure['type'] == 'multipart') {
			foreach ($structure['parts'] as $part) {
				$info = self::handlePart($info, $part);
			}
			return $info;
		}

		if ($structure['type'] == 'application' && in_array($structure['subtype'], array('pkcs7-mime', 'x-pkcs7-mime'))) {
			$to = NULL;
			if (isset($info['headers']['to'][0])) {
				$to = $info['headers']['to'][0]['mailbox'];
				if (!empty($info['headers']['to'][0]['host'])) {
					$to .= '@' . $info['headers']['to'][0]['host'];
				}
			}
			if ($to && !empty(self::$smime_pairs[$to]['private_key'])) {
				if (self::handleSMIMEDecryption($info, $structure, self::$smime_pairs[$to])) {
					return $info;
				}
			}
		}

		if ($structure['type'] == 'application' && in_array($structure['subtype'], array('pkcs7-signature', 'x-pkcs7-signature'))) {
			$from = NULL;
			if (isset($info['headers']['from'])) {
				$from = $info['headers']['from']['mailbox'];
				if (!empty($info['headers']['from']['host'])) {
					$from .= '@' . $info['headers']['from']['host'];
				}
			}
			if ($from && !empty(self::$smime_pairs[$from]['certificate'])) {
				if (self::handleSMIMEVerification($info, $structure, self::$smime_pairs[$from])) {
					return $info;
				}
			}
		}

		$data = $structure['data'];

		if ($structure['encoding'] == 'base64') {
			$content = '';
			foreach (explode("\r\n", $data) as $line) {
				$content .= base64_decode($line);
			}
		} elseif ($structure['encoding'] == 'quoted-printable') {
			$content = quoted_printable_decode($data);
		} else {
			$content = $data;
		}

		if ($structure['type'] == 'text') {
			$charset = 'iso-8859-1';
			foreach ($structure['type_fields'] as $field => $value) {
				if (strtolower($field) == 'charset') {
					$charset = $value;
					break;
				}
			}
			$content = self::iconv($charset, 'UTF-8', $content);
			if ($structure['subtype'] == 'html') {
				$content = preg_replace('#(content=(["\'])text/html\s*;\s*charset=(["\']?))' . preg_quote($charset, '#') . '(\3\2)#i', '\1utf-8\4', $content);
			}
		}

		// This indicates a content-id which is used for multipart/related
		if ($structure['content_id']) {
			if (!isset($info['related'])) {
				$info['related'] = array();
			}
			$cid = $structure['content_id'][0] == '<' ? substr($structure['content_id'], 1, -1) : $structure['content_id'];
			$info['related']['cid:' . $cid] = array(
				'mimetype' => $structure['type'] . '/' . $structure['subtype'],
				'data'     => $content
			);
			return $info;
		}


		$has_disposition = !empty($structure['disposition']);
		$is_text         = $structure['type'] == 'text' && $structure['subtype'] == 'plain';
		$is_html         = $structure['type'] == 'text' && $structure['subtype'] == 'html';

		// If the part doesn't have a disposition and is not the default text or html, set the disposition to inline
		if (!$has_disposition && ((!$is_text || !empty($info['text'])) && (!$is_html || !empty($info['html'])))) {
			$is_web_image = $structure['type'] == 'image' && in_array($structure['subtype'], array('gif', 'png', 'jpeg', 'pjpeg'));
			$structure['disposition'] = $is_text || $is_html || $is_web_image ? 'inline' : 'attachment';
			$structure['disposition_fields'] = array();
			$has_disposition = TRUE;
		}


		// Attachments or inline content
		if ($has_disposition) {

			$filename = '';
			foreach ($structure['disposition_fields'] as $field => $value) {
				if (strtolower($field) == 'filename') {
					$filename = $value;
					break;
				}
			}
			foreach ($structure['type_fields'] as $field => $value) {
				if (strtolower($field) == 'name') {
					$filename = $value;
					break;
				}
			}

			// This automatically handles primary content that has a content-disposition header on it
			if ($structure['disposition'] == 'inline' && $filename === '') {
				if ($is_text && !isset($info['text'])) {
					$info['text'] = $content;
					return $info;
				}
				if ($is_html && !isset($info['html'])) {
					$info['html'] = $content;
					return $info;
				}
			}

			if (!isset($info[$structure['disposition']])) {
				$info[$structure['disposition']] = array();
			}

			$info[$structure['disposition']][] = array(
				'filename' => $filename,
				'mimetype' => $structure['type'] . '/' . $structure['subtype'],
				'data'     => $content
			);
			return $info;
		}

		if ($is_text) {
			$info['text'] = $content;
			return $info;
		}

		if ($is_html) {
			$info['html'] = $content;
			return $info;
		}
	}


	/**
	 * Tries to decrypt an S/MIME message using a private key
	 *
	 * @param array  &$info       The array of information about a message
	 * @param array  $structure   The structure of this part
	 * @param array  $smime_pair  An associative array containing an S/MIME certificate, private key and password
	 * @return boolean  If the message was decrypted
	 */
	static private function handleSMIMEDecryption(&$info, $structure, $smime_pair)
	{
		$plaintext_file  = tempnam('', '__fMailbox_');
		$ciphertext_file = tempnam('', '__fMailbox_');

		$headers   = array();
		$headers[] = "Content-Type: " . $structure['type'] . '/' . $structure['subtype'];
		$headers[] = "Content-Transfer-Encoding: " . $structure['encoding'];
		$header    = "Content-Disposition: " . $structure['disposition'];
		foreach ($structure['disposition_fields'] as $field => $value) {
			$header .= '; ' . $field . '="' . $value . '"';
		}
		$headers[] = $header;

		file_put_contents($ciphertext_file, join("\r\n", $headers) . "\r\n\r\n" . $structure['data']);

		$private_key = openssl_pkey_get_private(
			$smime_pair['private_key']->read(),
			$smime_pair['password']
		);
		$certificate = $smime_pair['certificate']->read();

		$result = openssl_pkcs7_decrypt($ciphertext_file, $plaintext_file, $certificate, $private_key);
		unlink($ciphertext_file);

		if (!$result) {
			unlink($plaintext_file);
			return FALSE;
		}

		$contents = file_get_contents($plaintext_file);
		$info['raw_message'] = $contents;
		$info = self::handlePart($info, self::parseStructure($contents));
		$info['decrypted'] = TRUE;

		unlink($plaintext_file);
		return TRUE;
	}



	/**
	 * Takes a message with an S/MIME signature and verifies it if possible
	 *
	 * @param array &$info       The array of information about a message
	 * @param array $structure
	 * @param array $smime_pair  An associative array containing an S/MIME certificate file
	 * @return boolean  If the message was verified
	 */
	static private function handleSMIMEVerification(&$info, $structure, $smime_pair)
	{
		$certificates_file = tempnam('', '__fMailbox_');
		$ciphertext_file   = tempnam('', '__fMailbox_');

		file_put_contents($ciphertext_file, $info['raw_message']);

		$result = openssl_pkcs7_verify(
			$ciphertext_file,
			PKCS7_NOINTERN | PKCS7_NOVERIFY,
			$certificates_file,
			array(),
			$smime_pair['certificate']->getPath()
		);
		unlink($ciphertext_file);
		unlink($certificates_file);

		if (!$result || $result === -1) {
			return FALSE;
		}

		$info['verified'] = TRUE;

		return TRUE;
	}


	/**
	 * This works around a bug in MAMP 1.9.4+ and PHP 5.3 where iconv()
	 * does not seem to properly assign the return value to a variable, but
	 * does work when returning the value.
	 *
	 * @param string $in_charset   The incoming character encoding
	 * @param string $out_charset  The outgoing character encoding
	 * @param string $string       The string to convert
	 * @return string  The converted string
	 */
	static private function iconv($in_charset, $out_charset, $string)
	{
		return iconv($in_charset, $out_charset, $string);
	}


	/**
	 * Joins parsed emails into a comma-delimited string
	 *
	 * @param array $emails  An array of emails split into personal, mailbox and host parts
	 * @return string  An comma-delimited list of emails
	 */
	static private function joinEmails($emails)
	{
		$output = '';
		foreach ($emails as $email) {
			if ($output) { $output .= ', '; }

			if (!isset($email[0])) {
				$email[0] = !empty($email['personal']) ? $email['personal'] : '';
				$email[2] = $email['mailbox'];
				$email[3] = !empty($email['host']) ? $email['host'] : '';
			}

			$address = $email[2];
			if (!empty($email[3])) {
				$address .= '@' . $email[3];
			}
			$output .= fEmail::combineNameEmail($email[0], $address);
		}
		return $output;
	}


	/**
	 * Parses a string representation of an email into the persona, mailbox and host parts
	 *
	 * @param  string $string  The email string to parse
	 * @return array  An associative array with the key `mailbox`, and possibly `host` and `personal`
	 */
	static private function parseEmail($string)
	{
		$email_regex = '((?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+|"[^"\\\\\n\r]+")(?:\.[ \t]*(?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+|"[^"\\\\\n\r]+"[ \t]*))*)@((?:[a-z0-9\\-]+\.)+[a-z]{2,}|\[(?:(?:[01]?\d?\d|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d?\d|2[0-4]\d|25[0-5])\])';
		$name_regex  = '((?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+[ \t]*|"[^"\\\\\n\r]+"[ \t]*)(?:\.?[ \t]*(?:[^\x00-\x20\(\)<>@,;:\\\\"\.\[\]]+[ \t]*|"[^"\\\\\n\r]+"[ \t]*))*)';

		if (preg_match('~^[ \t]*' . $name_regex . '[ \t]*<[ \t]*' . $email_regex . '[ \t]*>[ \t]*$~ixD', $string, $match)) {
			$match[1] = trim($match[1]);
			if ($match[1][0] == '"' && substr($match[1], -1) == '"') {
				$match[1] = substr($match[1], 1, -1);
			}
			return array(
				'personal' => self::decodeHeader($match[1]),
				'mailbox' => self::decodeHeader($match[2]),
				'host' => self::decodeHeader($match[3])
			);

		} elseif (preg_match('~^[ \t]*(?:<[ \t]*)?' . $email_regex . '(?:[ \t]*>)?[ \t]*$~ixD', $string, $match)) {
			return array(
				'mailbox' => self::decodeHeader($match[1]),
				'host' => self::decodeHeader($match[2])
			);

		// This handles the outdated practice of including the personal
		// part of the email in a comment after the email address
		} elseif (preg_match('~^[ \t]*(?:<[ \t]*)?' . $email_regex . '(?:[ \t]*>)?[ \t]*\(([^)]+)\)[ \t]*$~ixD', $string, $match)) {
			$match[3] = trim($match[1]);
			if ($match[3][0] == '"' && substr($match[3], -1) == '"') {
				$match[3] = substr($match[3], 1, -1);
			}

			return array(
				'personal' => self::decodeHeader($match[3]),
				'mailbox' => self::decodeHeader($match[1]),
				'host' => self::decodeHeader($match[2])
			);
		}

		if (strpos($string, '@') !== FALSE) {
			list ($mailbox, $host) = explode('@', $string, 2);
			return array(
				'mailbox' => self::decodeHeader($mailbox),
				'host' => self::decodeHeader($host)
			);
		}

		return array(
			'mailbox' => self::decodeHeader($string),
			'host' => ''
		);
	}


	/**
	 * Parses full email headers into an associative array
	 *
	 * @param  string $headers  The header to parse
	 * @param  string $filter   Remove any headers that match this
	 * @return array  The parsed headers
	 */
	static private function parseHeaders($headers, $filter=NULL)
	{
		$headers = trim($headers);
		if (!strlen($headers)) {
			return array();
		}
		$header_lines = preg_split("#\r\n(?!\s)#", $headers);

		$single_email_fields    = array('from', 'sender', 'reply-to');
		$multi_email_fields     = array('to', 'cc');
		$additional_info_fields = array('content-type', 'content-disposition');

		$headers = array();
		foreach ($header_lines as $header_line) {
			$header_line = preg_replace("#\r\n\s+#", '', $header_line);

			list ($header, $value) = preg_split('#:\s*#', $header_line, 2);
			$header = strtolower($header);

			if (strpos($header, $filter) !== FALSE) {
				continue;
			}

			$is_single_email          = in_array($header, $single_email_fields);
			$is_multi_email           = in_array($header, $multi_email_fields);
			$is_additional_info_field = in_array($header, $additional_info_fields);

			if ($is_additional_info_field) {
				$pieces = preg_split('#;\s*#', $value, 2);
				$value = $pieces[0];

				$headers[$header] = array('value' => self::decodeHeader($value));

				$fields = array();
				if (!empty($pieces[1])) {
					preg_match_all('#(\w+)=("([^"]+)"|([^\s;]+))(?=;|$)#', $pieces[1], $matches, PREG_SET_ORDER);
					foreach ($matches as $match) {
						$fields[$match[1]] = self::decodeHeader(!empty($match[4]) ? $match[4] : $match[3]);
					}
				}
				$headers[$header]['fields'] = $fields;

			} elseif ($is_single_email) {
				$headers[$header] = self::parseEmail($value);

			} elseif ($is_multi_email) {
				$strings = array();

				preg_match_all('#"[^"]+?"#', $value, $matches, PREG_SET_ORDER);
				foreach ($matches as $i => $match) {
					$strings[] = $match[0];
					$value = preg_replace('#' . preg_quote($match[0], '#') . '#', ':string' . sizeof($strings), $value, 1);
				}
				preg_match_all('#\([^)]+?\)#', $value, $matches, PREG_SET_ORDER);
				foreach ($matches as $i => $match) {
					$strings[] = $match[0];
					$value = preg_replace('#' . preg_quote($match[0], '#') . '#', ':string' . sizeof($strings), $value, 1);
				}

				$emails = explode(',', $value);
				array_map('trim', $emails);
				foreach ($strings as $i => $string) {
					$emails = preg_replace(
						'#:string' . ($i+1) . '\b#',
						strtr($string, array('\\' => '\\\\', '$' => '\\$')),
						$emails,
						1
					);
				}

				$headers[$header] = array();
				foreach ($emails as $email) {
					$headers[$header][] = self::parseEmail($email);
				}

			} elseif ($header == 'references') {
				$headers[$header] = array_map(array('fMailbox', 'decodeHeader'), preg_split('#(?<=>)\s+(?=<)#', $value));

			} elseif ($header == 'received') {
				if (!isset($headers[$header])) {
					$headers[$header] = array();
				}
				$headers[$header][] = preg_replace('#\s+#', ' ', self::decodeHeader($value));

			} else {
				$headers[$header] = self::decodeHeader($value);
			}
		}

		return $headers;
	}


	/**
	 * Parses a MIME message into an associative array of information
	 *
	 * The output includes the following keys:
	 *
	 *  - `'received'`: The date the message was received by the server
	 *  - `'headers'`: An associative array of mail headers, the keys are the header names, in lowercase
	 *
	 * And one or more of the following:
	 *
	 *  - `'text'`: The plaintext body
	 *  - `'html'`: The HTML body
	 *  - `'attachment'`: An array of attachments, each containing:
	 *   - `'filename'`: The name of the file
	 *   - `'mimetype'`: The mimetype of the file
	 *   - `'data'`: The raw contents of the file
	 *  - `'inline'`: An array of inline files, each containing:
	 *   - `'filename'`: The name of the file
	 *   - `'mimetype'`: The mimetype of the file
	 *   - `'data'`: The raw contents of the file
	 *  - `'related'`: An associative array of related files, such as embedded images, with the key `'cid:{content-id}'` and an array value containing:
	 *   - `'mimetype'`: The mimetype of the file
	 *   - `'data'`: The raw contents of the file
	 *  - `'verified'`: If the message contents were verified via an S/MIME certificate - if not verified the smime.p7s will be listed as an attachment
	 *  - `'decrypted'`: If the message contents were decrypted via an S/MIME private key - if not decrypted the smime.p7m will be listed as an attachment
	 *
	 * All values in `headers`, `text` and `body` will have been decoded to
	 * UTF-8. Files in the `attachment`, `inline` and `related` array will all
	 * retain their original encodings.
	 *
	 * @param string  $message           The full source of the email message
	 * @param boolean $convert_newlines  If `\r\n` should be converted to `\n` in the `text` and `html` parts the message
	 * @return array  The parsed email message - see method description for details
	 */
	static public function parseMessage($message, $convert_newlines=FALSE)
	{
		$info = array();
		list ($headers, $body)   = explode("\r\n\r\n", $message, 2);
		$parsed_headers          = self::parseHeaders($headers);
		$info['received']        = self::cleanDate(preg_replace('#^.*;\s*([^;]+)$#', '\1', $parsed_headers['received'][0]));
		$info['headers']         = array();
		foreach ($parsed_headers as $header => $value) {
			if (substr($header, 0, 8) == 'content-') {
				continue;
			}
			$info['headers'][$header] = $value;
		}
		$info['raw_headers'] = $headers;
		$info['raw_message'] = $message;

		$info = self::handlePart($info, self::parseStructure($body, $parsed_headers));
		unset($info['raw_message']);
		unset($info['raw_headers']);

		if ($convert_newlines) {
			if (isset($info['text'])) {
				$info['text'] = str_replace("\r\n", "\n", $info['text']);
			}
			if (isset($info['html'])) {
				$info['html'] = str_replace("\r\n", "\n", $info['html']);
			}
		}

		if (isset($info['text'])) {
			$info['text'] = preg_replace('#\r?\n$#D', '', $info['text']);
		}
		if (isset($info['html'])) {
			$info['html'] = preg_replace('#\r?\n$#D', '', $info['html']);
		}

		return $info;
	}


	/**
	 * Takes a response from an IMAP command and parses it into a
	 * multi-dimensional array
	 *
	 * @param string  $text       The IMAP command response
	 * @param boolean $top_level  If we are parsing the top level
	 * @return array  The parsed representation of the response text
	 */
	static private function parseResponse($text, $top_level=FALSE)
	{
		$regex = '[\\\\\w.\[\]]+|"([^"\\\\]+|\\\\"|\\\\\\\\)*"|\((?:(?1)[ \t]*)*\)';

		if (preg_match('#\{(\d+)\}#', $text, $match)) {
			$regex = '\{' . $match[1] . '\}\r\n.{' . ($match[1]) . '}|' . $regex;
		}

		preg_match_all('#(' . $regex . ')#s', $text, $matches, PREG_SET_ORDER);
		$output = array();
		foreach ($matches as $match) {
			if (substr($match[0], 0, 1) == '"') {
				$output[] = str_replace('\\"', '"', substr($match[0], 1, -1));
			} elseif (substr($match[0], 0, 1) == '(') {
				$output[] = self::parseResponse(substr($match[0], 1, -1));
			} elseif (substr($match[0], 0, 1) == '{') {
				$output[] = preg_replace('#^[^\r]+\r\n#', '', $match[0]);
			} else {
				$output[] = $match[0];
			}
		}

		if ($top_level) {
			$new_output = array();
			$total_size = count($output);
			for ($i = 0; $i < $total_size; $i = $i + 2) {
				$new_output[strtolower($output[$i])] = $output[$i+1];
			}
			$output = $new_output;
		}

		return $output;
	}


	/**
	 * Takes the raw contents of a MIME message and creates an array that
	 * describes the structure of the message
	 *
	 * @param string $data     The contents to get the structure of
	 * @param string $headers  The parsed headers for the message - if not present they will be extracted from the `$data`
	 * @return array  The multi-dimensional, associative array containing the message structure
	 */
	static private function parseStructure($data, $headers=NULL)
	{
		if (!$headers) {
			list ($headers, $data) = preg_split("#^\r\n|\r\n\r\n#", $data, 2);
			$headers = self::parseHeaders($headers);
		}

		if (!isset($headers['content-type'])) {
			$headers['content-type'] = array(
				'value'  => 'text/plain',
				'fields' => array()
			);
		}

		list ($type, $subtype) = explode('/', strtolower($headers['content-type']['value']), 2);

		if ($type == 'multipart') {
			$structure    = array(
				'type'    => $type,
				'subtype' => $subtype,
				'parts'   => array()
			);
			$boundary     = $headers['content-type']['fields']['boundary'];
			$start_pos    = strpos($data, '--' . $boundary) + strlen($boundary) + 4;
			$end_pos      = strrpos($data, '--' . $boundary . '--') - 2;
			$sub_contents = explode("\r\n--" . $boundary . "\r\n", substr(
				$data,
				$start_pos,
				$end_pos - $start_pos
			));
			foreach ($sub_contents as $sub_content) {
				$structure['parts'][] = self::parseStructure($sub_content);
			}

		} else {
			$structure = array(
				'type'               => $type,
				'type_fields'        => !empty($headers['content-type']['fields']) ? $headers['content-type']['fields'] : array(),
				'subtype'            => $subtype,
				'content_id'         => isset($headers['content-id']) ? $headers['content-id'] : NULL,
				'encoding'           => isset($headers['content-transfer-encoding']) ? strtolower($headers['content-transfer-encoding']) : '8bit',
				'disposition'        => isset($headers['content-disposition']) ? strtolower($headers['content-disposition']['value']) : NULL,
				'disposition_fields' => isset($headers['content-disposition']) ? $headers['content-disposition']['fields'] : array(),
				'data'               => $data
			);
		}

		return $structure;
	}


	/**
	 * Resets the configuration of the class
	 *
	 * @internal
	 *
	 * @return void
	 */
	static public function reset()
	{
		self::$smime_pairs = array();
	}


	/**
	 * Takes an associative array and unfolds the keys and values so that the
	 * result in an integer-indexed array of `0 => key1, 1 => value1, 2 => key2,
	 * 3 => value2, ...`.
	 *
	 * @param array $array  The array to unfold
	 * @return array  The unfolded array
	 */
	static private function unfoldAssociativeArray($array)
	{
		$new_array = array();
		foreach ($array as $key => $value) {
			$new_array[] = $key;
			$new_array[] = $value;
		}
		return $new_array;
	}


	/**
	 * A counter to use for generating command keys
	 *
	 * @var integer
	 */
	private $command_num = 1;

	/**
	 * The connection resource
	 *
	 * @var resource
	 */
	private $connection;

	/**
	 * If debugging has been enabled
	 *
	 * @var boolean
	 */
	private $debug;

	/**
	 * The server hostname or IP address
	 *
	 * @var string
	 */
	private $host;

	/**
	 * The password for the account
	 *
	 * @var string
	 */
	private $password;

	/**
	 * The port for the server
	 *
	 * @var integer
	 */
	private $port;

	/**
	 * If the connection to the server should be secure
	 *
	 * @var boolean
	 */
	private $secure;

	/**
	 * The timeout for the connection
	 *
	 * @var integer
	 */
	private $timeout = 5;

	/**
	 * The type of mailbox, `'imap'` or `'pop3'`
	 *
	 * @var string
	 */
	private $type;

	/**
	 * The username for the account
	 *
	 * @var string
	 */
	private $username;


	/**
	 * Configures the connection to the server
	 *
	 * Please note that the GMail POP3 server does not act like other POP3
	 * servers and the GMail IMAP server should be used instead. GMail POP3 only
	 * allows retrieving a message once - during future connections the email
	 * in question will no longer be available.
	 *
	 * @param  string  $type      The type of mailbox, `'pop3'` or `'imap'`
	 * @param  string  $host      The server hostname or IP address
	 * @param  string  $username  The user to log in as
	 * @param  string  $password  The user's password
	 * @param  integer $port      The port to connect via - only required if non-standard
	 * @param  boolean $secure    If SSL should be used for the connection - this requires the `openssl` extension
	 * @param  integer $timeout   The timeout to use when connecting
	 * @return fMailbox
	 */
	public function __construct($type, $host, $username, $password, $port=NULL, $secure=FALSE, $timeout=NULL)
	{
		if ($timeout === NULL) {
			$timeout = ini_get('default_socket_timeout');
		}

		$valid_types = array('imap', 'pop3');
		if (!in_array($type, $valid_types)) {
			throw new fProgrammerException(
				'The mailbox type specified, %1$s, in invalid. Must be one of: %2$s.',
				$type,
				join(', ', $valid_types)
			);
		}

		if ($port === NULL) {
			if ($type == 'imap') {
				$port = !$secure ? 143 : 993;
			} else {
				$port = !$secure ? 110 : 995;
			}
		}

		if ($secure && !extension_loaded('openssl')) {
			throw new fEnvironmentException(
				'A secure connection was requested, but the %s extension is not installed',
				'openssl'
			);
		}

		$this->type     = $type;
		$this->host     = $host;
		$this->username = $username;
		$this->password = $password;
		$this->port     = $port;
		$this->secure   = $secure;
		$this->timeout  = $timeout;
	}


	/**
	 * Disconnects from the server
	 *
	 * @return void
	 */
	public function __destruct()
	{
		$this->close();
	}


	/**
	 * Closes the connection to the server
	 *
	 * @return void
	 */
	public function close()
	{
		if (!$this->connection) {
			return;
		}

		if ($this->type == 'imap') {
			$this->write('LOGOUT');
		} else {
			$this->write('QUIT', 1);
		}

		$this->connection = NULL;
	}


	/**
	 * Connects to the server
	 *
	 * @return void
	 */
	private function connect()
	{
		if ($this->connection) {
			return;
		}

		fCore::startErrorCapture(E_WARNING);

		$this->connection = fsockopen(
			$this->secure ? 'tls://' . $this->host : $this->host,
			$this->port,
			$error_number,
			$error_string,
			$this->timeout
		);

		foreach (fCore::stopErrorCapture('#ssl#i') as $error) {
			throw new fConnectivityException('There was an error connecting to the server. A secure connection was requested, but was not available. Try a non-secure connection instead.');
		}

		if (!$this->connection) {
			throw new fConnectivityException('There was an error connecting to the server');
		}

		stream_set_timeout($this->connection, $this->timeout);


		if ($this->type == 'imap') {
			if (!$this->secure && extension_loaded('openssl')) {
				$response = $this->write('CAPABILITY');
				if (preg_match('#\bstarttls\b#i', $response[0])) {
					$this->write('STARTTLS');
					do {
						if (isset($res)) {
							sleep(0.1);
						}
						$res = stream_socket_enable_crypto($this->connection, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
					} while ($res === 0);
				}
			}

			$response = $this->write('LOGIN ' . $this->username . ' ' . $this->password);
			if (!$response || !preg_match('#^[^ ]+\s+OK#', $response[count($response)-1])) {
				throw new fValidationException(
					'The username and password provided were not accepted for the %1$s server %2$s on port %3$s',
					strtoupper($this->type),
					$this->host,
					$this->port
				);
			}
			$this->write('SELECT "INBOX"');

		} elseif ($this->type == 'pop3') {
			$response = $this->read(1);
			if (isset($response[0])) {
				if ($response[0][0] == '-') {
					throw new fConnectivityException(
						'There was an error connecting to the POP3 server %1$s on port %2$s',
						$this->host,
						$this->port
					);
				}
				preg_match('#<[^@]+@[^>]+>#', $response[0], $match);
			}

			if (!$this->secure && extension_loaded('openssl')) {
				$response = $this->write('STLS', 1);
				if ($response[0][0] == '+') {
					do {
						if (isset($res)) {
							sleep(0.1);
						}
						$res = stream_socket_enable_crypto($this->connection, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
					} while ($res === 0);
					if ($res === FALSE) {
						throw new fConnectivityException('Error establishing secure connection');
					}
				}
			}

			$authenticated = FALSE;
			if (isset($match[0])) {
				$response = $this->write('APOP ' . $this->username . ' ' . md5($match[0] . $this->password), 1);
				if (isset($response[0]) && $response[0][0] == '+') {
					$authenticated = TRUE;
				}
			}

			if (!$authenticated) {
				$response = $this->write('USER ' . $this->username, 1);
				if ($response[0][0] == '+') {
					$response = $this->write('PASS ' . $this->password, 1);
					if (isset($response[0][0]) && $response[0][0] == '+') {
						$authenticated = TRUE;
					}
				}
			}

			if (!$authenticated) {
				throw new fValidationException(
					'The username and password provided were not accepted for the %1$s server %2$s on port %3$s',
					strtoupper($this->type),
					$this->host,
					$this->port
				);
			}
		}
	}


	/**
	 * Deletes one or more messages from the server
	 *
	 * Passing more than one UID at a time is more efficient for IMAP mailboxes,
	 * whereas POP3 mailboxes will see no difference in performance.
	 *
	 * @param  integer|array $uid  The UID(s) of the message(s) to delete
	 * @return void
	 */
	public function deleteMessages($uid)
	{
		$this->connect();

		settype($uid, 'array');

		if ($this->type == 'imap') {
			$this->write('UID STORE ' . join(',', $uid) . ' +FLAGS (\\Deleted)');
			$this->write('EXPUNGE');

		} elseif ($this->type == 'pop3') {
			foreach ($uid as $id) {
				$this->write('DELE ' . $id, 1);
			}
		}
	}


	/**
	 * Sets if debug messages should be shown
	 *
	 * @param  boolean $flag  If debugging messages should be shown
	 * @return void
	 */
	public function enableDebugging($flag)
	{
		$this->debug = (boolean) $flag;
	}


	/**
	 * Retrieves a single message from the server
	 *
	 * The output includes the following keys:
	 *
	 *  - `'uid'`: The UID of the message
	 *  - `'received'`: The date the message was received by the server
	 *  - `'headers'`: An associative array of mail headers, the keys are the header names, in lowercase
	 *
	 * And one or more of the following:
	 *
	 *  - `'text'`: The plaintext body
	 *  - `'html'`: The HTML body
	 *  - `'attachment'`: An array of attachments, each containing:
	 *   - `'filename'`: The name of the file
	 *   - `'mimetype'`: The mimetype of the file
	 *   - `'data'`: The raw contents of the file
	 *  - `'inline'`: An array of inline files, each containing:
	 *   - `'filename'`: The name of the file
	 *   - `'mimetype'`: The mimetype of the file
	 *   - `'data'`: The raw contents of the file
	 *  - `'related'`: An associative array of related files, such as embedded images, with the key `'cid:{content-id}'` and an array value containing:
	 *   - `'mimetype'`: The mimetype of the file
	 *   - `'data'`: The raw contents of the file
	 *  - `'verified'`: If the message contents were verified via an S/MIME certificate - if not verified the smime.p7s will be listed as an attachment
	 *  - `'decrypted'`: If the message contents were decrypted via an S/MIME private key - if not decrypted the smime.p7m will be listed as an attachment
	 *
	 * All values in `headers`, `text` and `body` will have been decoded to
	 * UTF-8. Files in the `attachment`, `inline` and `related` array will all
	 * retain their original encodings.
	 *
	 * @param  integer $uid               The UID of the message to retrieve
	 * @param  boolean $convert_newlines  If `\r\n` should be converted to `\n` in the `text` and `html` parts the message
	 * @return array  The parsed email message - see method description for details
	 */
	public function fetchMessage($uid, $convert_newlines=FALSE)
	{
		$this->connect();

		$source = $this->fetchMessageSource($uid);

		$info = self::parseMessage($source, $convert_newlines);
		$info['uid'] = $uid;

		return $info;
	}


	/**
	 * Retrieves the raw source of a single message from the server
	 *
	 * This method is primarily useful for storing the raw source of a message.
	 * Normal use of fMailbox would involved calling ::fetchMessage(), which
	 * calls this method and then ::parseMessage().
	 *
	 * @param  integer $uid  The UID of the message to retrieve
	 * @return string  The raw message source of the email
	 */
	public function fetchMessageSource($uid)
	{
		$this->connect();

		if ($this->type == 'imap') {
			$response = $this->write('UID FETCH ' . $uid . ' (BODY[])');
			preg_match('#\{(\d+)\}$#', $response[0], $match);

			$message = '';
			foreach ($response as $i => $line) {
				if (!$i) { continue; }
				if (strlen($message) + strlen($line) + 2 > $match[1]) {
					$message .= substr($line . "\r\n", 0, $match[1] - strlen($message));
				} else {
					$message .= $line . "\r\n";
				}
			}

			// Removes the extra trailing \r\n added above to the last line
			return substr($message, 0, -2);

		} elseif ($this->type == 'pop3') {
			$response = $this->write('RETR ' . $uid);
			array_shift($response);
			$response = join("\r\n", $response);

			return $response;
		}
	}


	/**
	 * Gets a list of messages from the server
	 *
	 * The structure of the returned array is:
	 *
	 * {{{
	 * array(
	 *     (integer) {uid} => array(
	 *         'uid'         => (integer) {a unique identifier for this message on this server},
	 *         'received'    => (string) {date message was received},
	 *         'size'        => (integer) {size of message in bytes},
	 *         'date'        => (string) {date message was sent},
	 *         'from'        => (string) {the from header value},
	 *         'subject'     => (string) {the message subject},
	 *         'message_id'  => (string) {optional - the message-id header value, should be globally unique},
	 *         'to'          => (string) {optional - the to header value},
	 *         'in_reply_to' => (string) {optional - the in-reply-to header value}
	 *     ), ...
	 * )
	 * }}}
	 *
	 * All values will have been decoded to UTF-8.
	 *
	 * @param  integer $limit  The number of messages to retrieve
	 * @param  integer $page   The page of messages to retrieve
	 * @return array  A list of messages on the server - see method description for details
	 */
	public function listMessages($limit=NULL, $page=NULL)
	{
		$this->connect();

		if ($this->type == 'imap') {
			if (!$limit) {
				$start = 1;
				$end   = '*';
			} else {
				if (!$page) {
					$page = 1;
				}
				$start = ($limit * ($page-1)) + 1;
				$end   = $start + $limit - 1;
			}

			$total_messages = 0;
			$response = $this->write('STATUS "INBOX" (MESSAGES)');
			foreach ($response as $line) {
				if (preg_match('#^\s*\*\s+STATUS\s+"?INBOX"?\s+\((.*)\)\s*$#', $line, $match)) {
					$details = self::parseResponse($match[1], TRUE);
					$total_messages = $details['messages'];
				}
			}

			if ($start > $total_messages) {
				return array();
			}

			if ($end > $total_messages) {
				$end = $total_messages;
			}

			$output = array();
			$response = $this->write('FETCH ' . $start . ':' . $end . ' (UID INTERNALDATE RFC822.SIZE ENVELOPE)');
			foreach ($response as $line) {
				if (preg_match('#^\s*\*\s+(\d+)\s+FETCH\s+\((.*)\)\s*$#', $line, $match)) {
					$details = self::parseResponse($match[2], TRUE);
					$info    = array();

					$info['uid']      = $details['uid'];
					$info['received'] = self::cleanDate($details['internaldate']);
					$info['size']     = $details['rfc822.size'];

					$envelope = $details['envelope'];
					$info['date']    = $envelope[0] != 'NIL' ? $envelope[0] : '';
					$info['from']    = self::joinEmails($envelope[2]);
					if (preg_match('#=\?[^\?]+\?[QB]\?[^\?]+\?=#', $envelope[1])) {
						do {
							$last_subject = $envelope[1];
							$envelope[1] = preg_replace('#(=\?([^\?]+)\?[QB]\?[^\?]+\?=) (\s*=\?\2)#', '\1\3', $envelope[1]);
						} while ($envelope[1] != $last_subject);
						$info['subject'] = self::decodeHeader($envelope[1]);
					} else {
						$info['subject'] = $envelope[1] == 'NIL' ? '' : self::decodeHeader($envelope[1]);
					}
					if ($envelope[9] != 'NIL') {
						$info['message_id'] = $envelope[9];
					}
					if ($envelope[5] != 'NIL') {
						$info['to'] = self::joinEmails($envelope[5]);
					}
					if ($envelope[8] != 'NIL') {
						$info['in_reply_to'] = $envelope[8];
					}

					$output[$info['uid']] = $info;
				}
			}

		} elseif ($this->type == 'pop3') {
			if (!$limit) {
				$start = 1;
				$end   = NULL;
			} else {
				if (!$page) {
					$page = 1;
				}
				$start = ($limit * ($page-1)) + 1;
				$end   = $start + $limit - 1;
			}

			$total_messages = 0;
			$response = $this->write('STAT', 1);
			preg_match('#^\+OK\s+(\d+)\s+#', $response[0], $match);
			$total_messages = $match[1];

			if ($start > $total_messages) {
				return array();
			}

			if ($end === NULL || $end > $total_messages) {
				$end = $total_messages;
			}

			$sizes = array();
			$response = $this->write('LIST');
			array_shift($response);
			foreach ($response as $line) {
				preg_match('#^(\d+)\s+(\d+)$#', $line, $match);
				$sizes[$match[1]] = $match[2];
			}

			$output = array();
			for ($i = $start; $i <= $end; $i++) {
				$response = $this->write('TOP ' . $i . ' 1');
				array_shift($response);
				$value = array_pop($response);
				// Some servers add an extra blank line after the 1 requested line
				if (trim($value) == '') {
					array_pop($response);
				}

				$response = trim(join("\r\n", $response));
				$headers = self::parseHeaders($response);
				$output[$i] = array(
					'uid'      => $i,
					'received' => self::cleanDate(preg_replace('#^.*;\s*([^;]+)$#', '\1', $headers['received'][0])),
					'size'     => $sizes[$i],
					'date'     => $headers['date'],
					'from'     => self::joinEmails(array($headers['from'])),
					'subject'  => isset($headers['subject']) ? $headers['subject'] : ''
				);
				if (isset($headers['message-id'])) {
					$output[$i]['message_id'] = $headers['message-id'];
				}
				if (isset($headers['to'])) {
					$output[$i]['to'] = self::joinEmails($headers['to']);
				}
				if (isset($headers['in-reply-to'])) {
					$output[$i]['in_reply_to'] = $headers['in-reply-to'];
				}
			}
		}

		return $output;
	}


	/**
	 * Reads responses from the server
	 *
	 * @param  integer|string $expect  The expected number of lines of response or a regex of the last line
	 * @return array  The lines of response from the server
	 */
	private function read($expect=NULL)
	{
		$read     = array($this->connection);
		$write    = NULL;
		$except   = NULL;
		$response = array();

		// PHP 5.2.0 to 5.2.5 has a bug on amd64 linux where stream_select()
		// fails, so we have to fake it - http://bugs.php.net/bug.php?id=42682
		static $broken_select = NULL;
		if ($broken_select === NULL) {
			$broken_select = strpos(php_uname('m'), '64') !== FALSE && fCore::checkVersion('5.2.0') && !fCore::checkVersion('5.2.6');
		}

		// Fixes an issue with stream_select throwing a warning on PHP 5.3 on Windows
		if (fCore::checkOS('windows') && fCore::checkVersion('5.3.0')) {
			$select = @stream_select($read, $write, $except, $this->timeout);

		} elseif ($broken_select) {
			$broken_select_buffer = NULL;
			$start_time = microtime(TRUE);
			$i = 0;
			do {
				if ($i) {
					usleep(50000);
				}
				$char = fgetc($this->connection);
				if ($char != "\x00" && $char !== FALSE) {
					$broken_select_buffer = $char;
				}
				$i++;
			} while ($broken_select_buffer === NULL && microtime(TRUE) - $start_time < $this->timeout);
			$select = $broken_select_buffer !== NULL;

		} else {
			$select = stream_select($read, $write, $except, $this->timeout);
		}

		if ($select) {
			while (!feof($this->connection)) {
				$line = fgets($this->connection);
				if ($line === FALSE) {
					break;
				}
				$line = substr($line, 0, -2);

				// When we fake select, we have to handle what we've retrieved
				if ($broken_select && $broken_select_buffer !== NULL) {
					$line = $broken_select_buffer . $line;
					$broken_select_buffer = NULL;
				}

				$response[] = $line;

				// Automatically stop at the termination octet or a bad response
				if ($this->type == 'pop3' && ($line == '.' || (count($response) == 1 && $response[0][0] == '-'))) {
					break;
				}

				if ($expect !== NULL) {
					$matched_number = is_int($expect) && sizeof($response) == $expect;
					$matched_regex  = is_string($expect) && preg_match($expect, $line);
					if ($matched_number || $matched_regex) {
						break;
					}
				}
			}
		}
		if (fCore::getDebug($this->debug)) {
			fCore::debug("Received:\n" . join("\r\n", $response), $this->debug);
		}

		if ($this->type == 'pop3') {
			// Remove the termination octet
			if ($response && $response[sizeof($response)-1] == '.') {
				$response = array_slice($response, 0, -1);
			}
			// Remove byte-stuffing
			$lines = count($response);
			for ($i = 0; $i < $lines; $i++) {
				if (strlen($response[$i]) && $response[$i][0] == '.') {
					$response[$i] = substr($response[$i], 1);
				}
			}
		}

		return $response;
	}


	/**
	 * Sends commands to the IMAP or POP3 server
	 *
	 * @param  string  $command   The command to send
	 * @param  integer $expected  The number of lines or regex expected for a POP3 command
	 * @return array  The response from the server
	 */
	private function write($command, $expected=NULL)
	{
		if (!$this->connection) {
			throw new fProgrammerException('Unable to send data since the connection has already been closed');
		}

		if ($this->type == 'imap') {
			$identifier = 'a' . str_pad($this->command_num++, 4, '0', STR_PAD_LEFT);
			$command    = $identifier . ' ' . $command;
		}

		if (substr($command, -2) != "\r\n") {
			$command .= "\r\n";
		}

		if (fCore::getDebug($this->debug)) {
			fCore::debug("Sending:\n" . trim($command), $this->debug);
		}

		$res = fwrite($this->connection, $command);

		if ($res === FALSE || $res === 0) {
			throw new fConnectivityException(
				'Unable to write data to %1$s server %2$s on port %3$s',
				strtoupper($this->type),
				$this->host,
				$this->port
			);
		}

		if ($this->type == 'imap') {
			return $this->read('#^' . $identifier . '#');
		} elseif ($this->type == 'pop3') {
			return $this->read($expected);
		}
	}
}

/**
 * Copyright (c) 2010-2012 Will Bond <will@flourishlib.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Back to Top