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 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program 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 both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: Rex Logan <veebert@dimensional.com> |
| Mark Musone <musone@chek.com> |
| Brian Wang <brian@vividnet.com> |
| Kaj-Michael Lang <milang@tal.org> |
| Antoni Pamies Olive <toni@readysoft.net> |
| Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Chuck Hagenbuch <chagenbu@wso.williams.edu> |
| Andrew Skalski <askalski@chek.com> |
+----------------------------------------------------------------------+
*/
/* $Id: imap.c,v 1.90 2000/09/30 17:32:44 sas Exp $ */
#define IMAP41
#ifdef ERROR
#undef ERROR
#endif
#if !(WIN32|WINNT)
#include "config.h"
#endif
#include "php.h"
#include "internal_functions.h"
#if COMPILE_DL
#include "dl/phpdl.h"
#undef HAVE_IMAP
#define HAVE_IMAP 1
#endif
#if HAVE_IMAP
#include <time.h>
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include "php3_list.h"
#include "imap.h"
#include "mail.h"
#include "rfc822.h"
#include "utf8.h"
#include "modules.h"
#if (WIN32|WINNT)
#include "winsock.h"
#include "win32/imap_sendmail.h"
#endif
#ifdef IMAP41
#define LSIZE text.size
#define LTEXT text.data
#define DTYPE int
#define CONTENT_PART nested.part
#define CONTENT_MSG_BODY nested.msg->body
#define IMAPVER "Imap 4R1"
#else
#define LSIZE size
#define LTEXT text
#define DTYPE char
#define CONTENT_PART contents.part
#define CONTENT_MSG_BODY contents.msg.body
#define IMAPVER "Imap 4"
#endif
#define PHP_EXPUNGE 32768
/* type casts left out, put here to remove warnings in
msvc
*/
extern void rfc822_date(char *date);
extern char *cpystr(const char *string);
extern unsigned long find_rightmost_bit (unsigned long *valptr);
extern void fs_give (void **block);
extern void *fs_get (size_t size);
int add_assoc_object(pval *arg, char *key, pval tmp);
int add_next_index_object(pval *arg, pval tmp);
void imap_add_body( pval *arg, BODY *body );
int _php3_imap_mail(char *to, char *subject, char *message, char *headers, char *cc, char *bcc, char *rpath);
typedef struct php3_imap_le_struct {
MAILSTREAM *imap_stream;
long flags;
#ifdef OP_RELOGIN
/* AJS: busy flag for persistent connections, pointers for chaining */
struct php3_imap_le_struct *next;
struct php3_imap_le_struct **prev;
char busy;
#endif
} pils;
typedef struct php3_imap_mailbox_struct {
SIZEDTEXT text;
DTYPE delimiter;
long attributes;
struct php3_imap_mailbox_struct *next;
} FOBJECTLIST;
typedef struct php3_imap_error_struct {
SIZEDTEXT text;
long errflg;
struct php3_imap_error_struct *next;
} ERRORLIST;
typedef struct php3_imap_message_struct {
unsigned long msgid;
struct php3_imap_message_struct *next;
} MESSAGELIST;
void mail_close_it (pils *imap_le_struct);
#ifdef OP_RELOGIN
/* AJS: close persistent connection */
void mail_userlogout_it (pils *imap_le_struct);
void mail_nuke_chain (pils **headp);
#endif
/*
* this array should be set up as:
* {"PHPScriptFunctionName",dllFunctionName,1}
*/
function_entry imap_functions[] = {
{"imap_open", php3_imap_open, NULL},
{"imap_popen", php3_imap_popen, NULL},
{"imap_reopen", php3_imap_reopen, NULL},
{"imap_num_msg", php3_imap_num_msg, NULL},
{"imap_num_recent", php3_imap_num_recent, NULL},
{"imap_headers", php3_imap_headers, NULL},
{"imap_header", php3_imap_headerinfo, NULL},
{"imap_headerinfo", php3_imap_headerinfo, NULL},
{"imap_body", php3_imap_body, NULL},
{"imap_fetchstructure", php3_imap_fetchstructure, NULL},
{"imap_fetchbody", php3_imap_fetchbody, NULL},
{"imap_expunge", php3_imap_expunge, NULL},
{"imap_delete", php3_imap_delete, NULL},
{"imap_undelete", php3_imap_undelete, NULL},
{"imap_check", php3_imap_check, NULL},
{"imap_close", php3_imap_close, NULL},
{"imap_mail_copy", php3_imap_mail_copy, NULL},
{"imap_mail_move", php3_imap_mail_move, NULL},
{"imap_createmailbox", php3_imap_createmailbox, NULL},
{"imap_renamemailbox", php3_imap_renamemailbox, NULL},
{"imap_deletemailbox", php3_imap_deletemailbox, NULL},
{"imap_listmailbox", php3_imap_list, NULL},
{"imap_getmailboxes", php3_imap_list_full, NULL},
{"imap_scanmailbox", php3_imap_listscan, NULL},
{"imap_listsubscribed", php3_imap_lsub, NULL},
{"imap_getsubscribed", php3_imap_lsub_full, NULL},
{"imap_subscribe", php3_imap_subscribe, NULL},
{"imap_unsubscribe", php3_imap_unsubscribe, NULL},
{"imap_append", php3_imap_append, NULL},
{"imap_ping", php3_imap_ping, NULL},
{"imap_base64", php3_imap_base64, NULL},
{"imap_qprint", php3_imap_qprint, NULL},
{"imap_8bit", php3_imap_8bit, NULL},
{"imap_binary", php3_imap_binary, NULL},
{"imap_mailboxmsginfo", php3_imap_mailboxmsginfo, NULL},
{"imap_rfc822_write_address", php3_imap_rfc822_write_address, NULL},
{"imap_rfc822_parse_adrlist", php3_imap_rfc822_parse_adrlist, NULL},
{"imap_setflag_full", php3_imap_setflag_full, NULL},
{"imap_clearflag_full", php3_imap_clearflag_full, NULL},
{"imap_sort", php3_imap_sort, NULL},
{"imap_fetchheader", php3_imap_fetchheader, NULL},
{"imap_fetchtext", php3_imap_body, NULL},
{"imap_uid", php3_imap_uid, NULL},
{"imap_msgno",php3_imap_msgno, NULL},
{"imap_list", php3_imap_list, NULL},
{"imap_scan", php3_imap_listscan, NULL},
{"imap_lsub", php3_imap_lsub, NULL},
{"imap_create", php3_imap_createmailbox, NULL},
{"imap_rename", php3_imap_renamemailbox, NULL},
{"imap_status", php3_imap_status, NULL},
{"imap_bodystruct", php3_imap_bodystruct, NULL},
{"imap_fetch_overview", php3_imap_fetch_overview, NULL},
{"imap_mail_compose", php3_imap_mail_compose, NULL},
{"imap_alerts", php3_imap_alerts, NULL},
{"imap_errors", php3_imap_errors, NULL},
{"imap_last_error", php3_imap_last_error, NULL},
{"imap_mail", php3_imap_mail, NULL},
{"imap_search", php3_imap_search, NULL},
{"imap_utf8", php3_imap_utf8, NULL},
{"imap_utf7_decode", php3_imap_utf7_decode, NULL},
{"imap_utf7_encode", php3_imap_utf7_encode, NULL},
{"imap_mime_header_decode", php3_imap_mime_header_decode, NULL},
{NULL, NULL, NULL}
};
#ifdef OP_RELOGIN
#define IS_STREAM(ind_type) ((ind_type)==le_imap || (ind_type)==le_pimap)
#else
#define IS_STREAM(ind_type) ((ind_type)==le_imap)
#endif
php3_module_entry php3_imap_module_entry = {
IMAPVER, imap_functions, imap_init, NULL, imap_init_request, imap_end_request, imap_info, 0, 0, 0, NULL
};
#if COMPILE_DL
DLEXPORT php3_module_entry *get_module(void) { return &php3_imap_module_entry; }
#endif
/* Determines how mm_list() and mm_lsub() are to return their results. */
typedef enum {
FLIST_ARRAY,
FLIST_OBJECT
} folderlist_style_t;
/*
I believe since this global is used ONLY within this module,
and nothing will link to this module, we can use the simple
thread local storage
*/
int le_imap;
#ifdef OP_RELOGIN
/* AJS: persistent connection type, chain pointer type */
int le_pimap;
int le_pimapchain;
#endif
char imap_user[80]="";
char imap_password[80]="";
#if HAVE_IMSP
extern char imsp_user[80];
extern char imsp_password[80];
#endif
STRINGLIST *imap_folders=NIL;
STRINGLIST *imap_sfolders=NIL;
STRINGLIST *imap_alertstack=NIL;
ERRORLIST *imap_errorstack=NIL;
MESSAGELIST *imap_messages=NIL;
FOBJECTLIST *imap_folder_objects=NIL;
FOBJECTLIST *imap_sfolder_objects=NIL;
folderlist_style_t folderlist_style = FLIST_ARRAY;
long status_flags;
unsigned long status_messages;
unsigned long status_recent;
unsigned long status_unseen;
unsigned long status_uidnext;
unsigned long status_uidvalidity;
#ifdef SA_QUOTA
unsigned long status_quota;
#endif
#ifdef SA_QUOTA_ALL
unsigned long status_quota_all;
#endif
void
mail_close_it (pils *imap_le_struct)
{
mail_close_full (imap_le_struct->imap_stream,imap_le_struct->flags);
efree(imap_le_struct);
}
#ifdef OP_RELOGIN
/* AJS: stream close functions for persistent connections */
void
mail_userlogout_it (pils *imap_le_struct)
{
/* Close this user's session, putting the stream back
* into AUTHENTICATE state. (Note that IMAP does not
* support this behavior... yet)
*/
imap_le_struct->busy = 0;
mail_close_full(imap_le_struct->imap_stream,
imap_le_struct->flags | CL_HALF);
}
void
mail_nuke_chain (pils **headp)
{
pils *node, *next;
for (node = *headp; node; node = next) {
next = node->next;
mail_close(node->imap_stream);
free(node);
}
free(headp);
}
#endif
inline int add_assoc_object(pval *arg, char *key, pval tmp)
{
return _php3_hash_update(arg->value.ht, key, strlen(key)+1, (void *) &tmp, sizeof(pval), NULL);
}
inline int add_next_index_object(pval *arg, pval tmp)
{
return _php3_hash_next_index_insert( arg->value.ht, (void *) &tmp, sizeof(pval), NULL);
}
/* Mail instantiate FOBJECTLIST
* Returns: new FOBJECTLIST list
* Author: CJH
*/
FOBJECTLIST *mail_newfolderobjectlist (void) {
return (FOBJECTLIST *) memset(fs_get(sizeof(FOBJECTLIST)),0,
sizeof(FOBJECTLIST));
}
/* Mail garbage collect FOBJECTLIST
* Accepts: pointer to FOBJECTLIST pointer
* Author: CJH
*/
void mail_free_foblist (FOBJECTLIST **foblist)
{
if (*foblist) { /* only free if exists */
if ((*foblist)->text.data) fs_give ((void **) &(*foblist)->text.data);
mail_free_foblist (&(*foblist)->next);
fs_give ((void **) foblist); /* return string to free storage */
}
}
/* Mail instantiate ERRORLIST
* Returns: new ERRORLIST list
* Author: CJH
*/
ERRORLIST *mail_newerrorlist (void) {
return (ERRORLIST *) memset(fs_get(sizeof(ERRORLIST)),0,
sizeof(ERRORLIST));
}
/* Mail garbage collect FOBJECTLIST
* Accepts: pointer to FOBJECTLIST pointer
* Author: CJH
*/
void mail_free_errorlist (ERRORLIST **errlist)
{
if (*errlist) { /* only free if exists */
if ((*errlist)->text.data) fs_give ((void **) &(*errlist)->text.data);
mail_free_errorlist (&(*errlist)->next);
fs_give ((void **) errlist); /* return string to free storage */
}
}
/* Mail instantiate MESSAGELIST
* Returns: new MESSAGELIST list
* Author: CJH
*/
MESSAGELIST *mail_newmessagelist (void)
{
return (MESSAGELIST *) memset(fs_get(sizeof(MESSAGELIST)),0,
sizeof(MESSAGELIST));
}
/* Mail garbage collect MESSAGELIST
* Accepts: pointer to MESSAGELIST pointer
* Author: CJH
*/
void mail_free_messagelist (MESSAGELIST **msglist)
{
if (*msglist) { /* only free if exists */
mail_free_messagelist (&(*msglist)->next);
fs_give ((void **) msglist); /* return string to free storage */
}
}
/* Author: CJH */
int imap_init_request (INIT_FUNC_ARGS) {
imap_errorstack = NIL;
imap_alertstack = NIL;
return SUCCESS;
}
/* Author: CJH */
int imap_end_request () {
ERRORLIST *ecur = NIL;
STRINGLIST *acur = NIL;
if (imap_errorstack != NIL) {
/* output any remaining errors at their original error level */
ecur = imap_errorstack;
while (ecur != NIL) {
php3_error(E_WARNING, "errflg=%d, text=%s", ecur->errflg, ecur->LTEXT);
ecur = ecur->next;
}
mail_free_errorlist(&imap_errorstack);
}
if (imap_alertstack != NIL) {
/* output any remaining alerts at E_NOTICE level */
acur = imap_alertstack;
while (acur != NIL) {
php3_error(E_NOTICE, "%s", acur->LTEXT);
acur = acur->next;
}
mail_free_stringlist(&imap_alertstack);
}
return SUCCESS;
}
void imap_info(void)
{
php3_printf("Imap Support enabled<br>");
php3_printf("<table>");
php3_printf("<tr><td>Imap c-client Version:</td>");
#ifdef IMAP41
php3_printf("<td>Imap 4.1</td>");
#else
php3_printf("<td>Imap 4.0</td>");
#endif
php3_printf("</tr></table>");
}
int imap_init(INIT_FUNC_ARGS)
{
unsigned long sa_all = SA_MESSAGES | SA_RECENT | SA_UNSEEN |
SA_UIDNEXT | SA_UIDVALIDITY;
#if !(WIN32|WINNT)
mail_link(&unixdriver); /* link in the unix driver */
#endif
mail_link (&imapdriver); /* link in the imap driver */
mail_link (&nntpdriver); /* link in the nntp driver */
mail_link (&pop3driver); /* link in the pop3 driver */
#if !(WIN32|WINNT)
mail_link (&mhdriver); /* link in the mh driver */
mail_link (&mxdriver); /* link in the mx driver */
#endif
mail_link (&mbxdriver); /* link in the mbx driver */
mail_link (&tenexdriver); /* link in the tenex driver */
mail_link (&mtxdriver); /* link in the mtx driver */
#if !(WIN32|WINNT)
mail_link (&mmdfdriver); /* link in the mmdf driver */
mail_link (&newsdriver); /* link in the news driver */
mail_link (&philedriver); /* link in the phile driver */
#endif
mail_link (&dummydriver); /* link in the dummy driver */
auth_link (&auth_log); /* link in the log authenticator */
/* lets allow NIL */
REGISTER_MAIN_LONG_CONSTANT("NIL", NIL, CONST_PERSISTENT | CONST_CS);
/* Open Options */
REGISTER_MAIN_LONG_CONSTANT("OP_DEBUG", OP_DEBUG, CONST_PERSISTENT | CONST_CS);
/* debug protocol negotiations */
REGISTER_MAIN_LONG_CONSTANT("OP_READONLY", OP_READONLY, CONST_PERSISTENT | CONST_CS);
/* read-only open */
REGISTER_MAIN_LONG_CONSTANT("OP_ANONYMOUS", OP_ANONYMOUS, CONST_PERSISTENT | CONST_CS);
/* anonymous open of newsgroup */
REGISTER_MAIN_LONG_CONSTANT("OP_SHORTCACHE", OP_SHORTCACHE, CONST_PERSISTENT | CONST_CS);
/* short (elt-only) caching */
REGISTER_MAIN_LONG_CONSTANT("OP_SILENT", OP_SILENT, CONST_PERSISTENT | CONST_CS);
/* don't pass up events (internal use) */
REGISTER_MAIN_LONG_CONSTANT("OP_PROTOTYPE", OP_PROTOTYPE, CONST_PERSISTENT | CONST_CS);
/* return driver prototype */
REGISTER_MAIN_LONG_CONSTANT("OP_HALFOPEN", OP_HALFOPEN, CONST_PERSISTENT | CONST_CS);
/* half-open (IMAP connect but no select) */
REGISTER_MAIN_LONG_CONSTANT("OP_EXPUNGE", OP_EXPUNGE, CONST_PERSISTENT | CONST_CS);
/* silently expunge recycle stream */
REGISTER_MAIN_LONG_CONSTANT("OP_SECURE", OP_SECURE, CONST_PERSISTENT | CONST_CS);
/* don't do non-secure authentication */
/*
PHP re-assigns CL_EXPUNGE a custom value that can be used as part of the imap_open() bitfield
because it seems like a good idea to be able to indicate that the mailbox should be
automatically expunged during imap_open in case the script get interrupted and it doesn't get
to the imap_close() where this option is normally placed. If the c-client library adds other
options and the value for this one conflicts, simply make PHP_EXPUNGE higher at the top of
this file
*/
REGISTER_MAIN_LONG_CONSTANT("CL_EXPUNGE", PHP_EXPUNGE, CONST_PERSISTENT | CONST_CS);
/* expunge silently */
/* Fetch options */
REGISTER_MAIN_LONG_CONSTANT("FT_UID", FT_UID, CONST_PERSISTENT | CONST_CS);
/* argument is a UID */
REGISTER_MAIN_LONG_CONSTANT("FT_PEEK", FT_PEEK, CONST_PERSISTENT | CONST_CS);
/* peek at data */
REGISTER_MAIN_LONG_CONSTANT("FT_NOT", FT_NOT, CONST_PERSISTENT | CONST_CS);
/* NOT flag for header lines fetch */
REGISTER_MAIN_LONG_CONSTANT("FT_INTERNAL", FT_INTERNAL, CONST_PERSISTENT | CONST_CS);
/* text can be internal strings */
REGISTER_MAIN_LONG_CONSTANT("FT_PREFETCHTEXT", FT_PREFETCHTEXT, CONST_PERSISTENT | CONST_CS);
/* IMAP prefetch text when fetching header */
/* Flagging options */
REGISTER_MAIN_LONG_CONSTANT("ST_UID", ST_UID, CONST_PERSISTENT | CONST_CS);
/* argument is a UID sequence */
REGISTER_MAIN_LONG_CONSTANT("ST_SILENT", ST_SILENT, CONST_PERSISTENT | CONST_CS);
/* don't return results */
REGISTER_MAIN_LONG_CONSTANT("ST_SET", ST_SET, CONST_PERSISTENT | CONST_CS);
/* set vs. clear */
/* Copy options */
REGISTER_MAIN_LONG_CONSTANT("CP_UID", CP_UID, CONST_PERSISTENT | CONST_CS);
/* argument is a UID sequence */
REGISTER_MAIN_LONG_CONSTANT("CP_MOVE", CP_MOVE, CONST_PERSISTENT | CONST_CS);
/* delete from source after copying */
/* Search/sort options */
REGISTER_MAIN_LONG_CONSTANT("SE_UID", SE_UID, CONST_PERSISTENT | CONST_CS);
/* return UID */
REGISTER_MAIN_LONG_CONSTANT("SE_FREE", SE_FREE, CONST_PERSISTENT | CONST_CS);
/* free search program after finished */
REGISTER_MAIN_LONG_CONSTANT("SE_NOPREFETCH", SE_NOPREFETCH, CONST_PERSISTENT | CONST_CS);
/* no search prefetching */
REGISTER_MAIN_LONG_CONSTANT("SO_FREE", SO_FREE, CONST_PERSISTENT | CONST_CS);
/* free sort program after finished */
REGISTER_MAIN_LONG_CONSTANT("SO_NOSERVER", SO_NOSERVER, CONST_PERSISTENT | CONST_CS);
/* don't do server-based sort */
/* Status options */
REGISTER_MAIN_LONG_CONSTANT("SA_MESSAGES",SA_MESSAGES , CONST_PERSISTENT | CONST_CS);
/* number of messages */
REGISTER_MAIN_LONG_CONSTANT("SA_RECENT", SA_RECENT, CONST_PERSISTENT | CONST_CS);
/* number of recent messages */
REGISTER_MAIN_LONG_CONSTANT("SA_UNSEEN",SA_UNSEEN , CONST_PERSISTENT | CONST_CS);
/* number of unseen messages */
REGISTER_MAIN_LONG_CONSTANT("SA_UIDNEXT", SA_UIDNEXT, CONST_PERSISTENT | CONST_CS);
/* next UID to be assigned */
REGISTER_MAIN_LONG_CONSTANT("SA_UIDVALIDITY",SA_UIDVALIDITY , CONST_PERSISTENT | CONST_CS);
/* UID validity value */
#ifdef SA_QUOTA
sa_all |= SA_QUOTA;
REGISTER_MAIN_LONG_CONSTANT("SA_QUOTA",SA_QUOTA , CONST_PERSISTENT | CONST_CS);
/* Disk space taken up by mailbox. */
#endif
#ifdef SA_QUOTA_ALL
sa_all |= SA_QUOTA_ALL;
REGISTER_MAIN_LONG_CONSTANT("SA_QUOTA_ALL",SA_QUOTA_ALL , CONST_PERSISTENT | CONST_CS);
/* Disk space taken up by all mailboxes owned by user. */
#endif
REGISTER_MAIN_LONG_CONSTANT("SA_ALL", sa_all, CONST_PERSISTENT | CONST_CS);
/* get all status information */
/* Bits for mm_list() and mm_lsub() */
REGISTER_MAIN_LONG_CONSTANT("LATT_NOINFERIORS",LATT_NOINFERIORS , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("LATT_NOSELECT", LATT_NOSELECT, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("LATT_MARKED", LATT_MARKED, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("LATT_UNMARKED",LATT_UNMARKED , CONST_PERSISTENT | CONST_CS);
/* Sort functions */
REGISTER_MAIN_LONG_CONSTANT("SORTDATE",SORTDATE , CONST_PERSISTENT | CONST_CS);
/* date */
REGISTER_MAIN_LONG_CONSTANT("SORTARRIVAL",SORTARRIVAL , CONST_PERSISTENT | CONST_CS);
/* arrival date */
REGISTER_MAIN_LONG_CONSTANT("SORTFROM",SORTFROM , CONST_PERSISTENT | CONST_CS);
/* from */
REGISTER_MAIN_LONG_CONSTANT("SORTSUBJECT",SORTSUBJECT , CONST_PERSISTENT | CONST_CS);
/* subject */
REGISTER_MAIN_LONG_CONSTANT("SORTTO",SORTTO , CONST_PERSISTENT | CONST_CS);
/* to */
REGISTER_MAIN_LONG_CONSTANT("SORTCC",SORTCC , CONST_PERSISTENT | CONST_CS);
/* cc */
REGISTER_MAIN_LONG_CONSTANT("SORTSIZE",SORTSIZE , CONST_PERSISTENT | CONST_CS);
/* size */
REGISTER_MAIN_LONG_CONSTANT("TYPETEXT",TYPETEXT , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEMULTIPART",TYPEMULTIPART , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEMESSAGE",TYPEMESSAGE , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEAPPLICATION",TYPEAPPLICATION , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEAUDIO",TYPEAUDIO , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEIMAGE",TYPEIMAGE , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEVIDEO",TYPEVIDEO , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("TYPEOTHER",TYPEOTHER , CONST_PERSISTENT | CONST_CS);
/* TYPETEXT unformatted text
TYPEMULTIPART multiple part
TYPEMESSAGE encapsulated message
TYPEAPPLICATION application data
TYPEAUDIO audio
TYPEIMAGE static image (GIF, JPEG, etc.)
TYPEVIDEO video
TYPEOTHER unknown
*/
REGISTER_MAIN_LONG_CONSTANT("ENC7BIT",ENC7BIT , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("ENC8BIT",ENC8BIT , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("ENCBINARY",ENCBINARY , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("ENCBASE64",ENCBASE64, CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("ENCQUOTEDPRINTABLE",ENCQUOTEDPRINTABLE , CONST_PERSISTENT | CONST_CS);
REGISTER_MAIN_LONG_CONSTANT("ENCOTHER",ENCOTHER , CONST_PERSISTENT | CONST_CS);
/*
ENC7BIT 7 bit SMTP semantic data
ENC8BIT 8 bit SMTP semantic data
ENCBINARY 8 bit binary data
ENCBASE64 base-64 encoded data
ENCQUOTEDPRINTABLE human-readable 8-as-7 bit data
ENCOTHER unknown
*/
le_imap = register_list_destructors(mail_close_it,NULL);
#ifdef OP_RELOGIN
/* AJS: destructors for persistent connections */
le_pimap = register_list_destructors(mail_userlogout_it, NULL);
le_pimapchain = register_list_destructors(NULL, mail_nuke_chain);
#endif
return SUCCESS;
}
void php3_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
pval *mailbox;
pval *user;
pval *passwd;
pval *options;
MAILSTREAM *imap_stream;
pils *imap_le_struct;
long flags=NIL;
long cl_flags=NIL;
#ifdef OP_RELOGIN
NETMBX netmbx;
char *hashed_details = NULL;
int hashed_details_length = 0;
#endif
int ind;
int myargc=ARG_COUNT(ht);
if (myargc <3 || myargc >4 || getParameters(ht, myargc, &mailbox,&user,&passwd,&options) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(mailbox);
convert_to_string(user);
convert_to_string(passwd);
if(myargc ==4) {
convert_to_long(options);
flags = options->value.lval;
if(flags & PHP_EXPUNGE) {
cl_flags = CL_EXPUNGE;
flags ^= PHP_EXPUNGE;
}
}
strcpy(imap_user,user->value.str.val);
strcpy(imap_password,passwd->value.str.val);
#ifdef OP_RELOGIN
/* AJS: persistent connection handling */
/* Cannot use a persistent connection if we cannot parse
* out the server's hostname.
*/
if ( persistent &&
!mail_valid_net_parse(mailbox->value.str.val, &netmbx))
{
persistent = 0;
}
imap_stream = NIL;
if (persistent) {
list_entry *le = NULL;
list_entry new_le;
pils **headp, *node;
int need_update = 0;
hashed_details_length = sizeof("imap_") + strlen(netmbx.host);
hashed_details = (char*) emalloc(hashed_details_length);
sprintf(hashed_details, "imap_%s", netmbx.host);
/* Check for an existing connection. */
if ( (_php3_hash_find(plist,
hashed_details,
hashed_details_length,
(void*) &le) == FAILURE) ||
(le->type != le_pimapchain))
{
le = NULL;
}
/* Re-use existing connection if available. */
node = NULL;
headp = NULL;
if (le) {
headp = (pils**) le->ptr;
/* find a non-busy connection */
for (node=*headp; node; node=node->next)
if (!node->busy)
break;
}
/* If we found a node, do a relogin. */
if (node) {
imap_stream = mail_open(
node->imap_stream,
mailbox->value.str.val,
flags | OP_RELOGIN);
if (imap_stream) {
/* Ping the stream to see if it is
* still good.
*/
if (!mail_ping(imap_stream)) {
mail_close(imap_stream);
imap_stream = NIL;
}
}
}
/* Get a fresh stream if we don't have one yet. */
if (imap_stream == NIL) {
/* Open a new connection. */
imap_stream = mail_open(
NIL,
mailbox->value.str.val,
flags | OP_RELOGIN);
}
/* Do we have a stream yet? If not, bail. */
if (imap_stream == NIL) {
if (node) {
/* unlink the node */
if ((*node->prev = node->next))
node->next->prev = node->prev;
free(node);
/* delete the hash entry if empty */
if (*headp == NULL)
_php3_hash_del(plist,
hashed_details,
hashed_details_length);
}
efree(hashed_details);
RETURN_FALSE;
}
/* Allocate a new node if none. */
if (node == NULL) {
/* Alloc new hash entry. */
node = malloc(sizeof(pils));
if (node == NULL) {
efree(hashed_details);
RETURN_FALSE;
}
/* Allocate headp if it does not exist. */
if (headp == NULL) {
headp = calloc(1, sizeof(*headp));
need_update = 1;
}
node->prev = headp;
node->next = *headp;
*headp = node;
}
/* Initialize the node. */
node->busy = 1;
node->imap_stream = imap_stream;
node->flags = cl_flags;
/* Update the hash. */
new_le.type = le_pimapchain;
new_le.ptr = headp;
if ( need_update &&
_php3_hash_update(plist, hashed_details,
hashed_details_length, &new_le,
sizeof(new_le), NULL) == FAILURE)
{
/* unlink and free the new node */
if ((*node->prev = node->next))
node->next->prev = node->prev;
mail_close(node->imap_stream);
free(node);
free(headp);
efree(hashed_details);
RETURN_FALSE;
}
efree(hashed_details);
imap_le_struct = node;
}
else {
#endif
imap_stream = mail_open(NIL, mailbox->value.str.val, flags);
if (imap_stream == NIL) {
php3_error(E_WARNING,
"Couldn't open stream %s\n",
mailbox->value.str.val);
RETURN_FALSE;
}
imap_le_struct = emalloc(sizeof(pils));
imap_le_struct->imap_stream = imap_stream;
imap_le_struct->flags = cl_flags;
#ifdef OP_RELOGIN
}
if (persistent)
ind = php3_list_insert(imap_le_struct, le_pimap);
else
#endif
ind = php3_list_insert(imap_le_struct, le_imap);
RETURN_LONG(ind);
}
/* {{{ proto int imap_open(string mailbox, string user, string password [, int options])
Open an IMAP stream to a mailbox */
void php3_imap_open(INTERNAL_FUNCTION_PARAMETERS)
{
php3_imap_do_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto int imap_popen(string mailbox, string user, string password [, int options])
Open an IMAP stream to a mailbox */
void php3_imap_popen(INTERNAL_FUNCTION_PARAMETERS)
{
#ifdef OP_RELOGIN
php3_imap_do_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
#else
php3_error(E_WARNING, "Persistent IMAP connections are not yet supported.\n");
RETURN_FALSE;
#endif
}
/* }}} */
/* {{{ proto int imap_reopen(int stream_id, string mailbox [, int options])
Reopen IMAP stream to new mailbox */
void php3_imap_reopen(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
pval *mailbox;
pval *options;
MAILSTREAM *imap_stream;
pils *imap_le_struct;
int ind, ind_type;
long flags=NIL;
long cl_flags=NIL;
int myargc=ARG_COUNT(ht);
if (myargc<2 || myargc>3 || getParameters(ht,myargc,&streamind, &mailbox, &options) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
convert_to_string(mailbox);
if(myargc == 3) {
convert_to_long(options);
flags = options->value.lval;
if(flags & PHP_EXPUNGE) {
cl_flags = CL_EXPUNGE;
flags ^= PHP_EXPUNGE;
}
imap_le_struct->flags = cl_flags;
}
imap_stream = mail_open(imap_le_struct->imap_stream, mailbox->value.str.val, flags);
if (imap_stream == NIL) {
php3_error(E_WARNING,"Couldn't re-open stream\n");
RETURN_FALSE;
}
imap_le_struct->imap_stream = imap_stream;
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int imap_append(int stream_id, string folder, string message [, string flags])
Append a string message to a specified mailbox */
void php3_imap_append(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind,*folder, *message,*flags;
int ind, ind_type;
pils *imap_le_struct;
STRING st;
int myargc=ARG_COUNT(ht);
if ( myargc < 3 || myargc > 4 || getParameters( ht, myargc, &streamind, &folder, &message,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(folder);
convert_to_string(message);
if (myargc == 4) convert_to_string(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find( ind, &ind_type );
if ( !imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
INIT (&st,mail_string,(void *) message->value.str.val,message->value.str.len);
if(mail_append_full(imap_le_struct->imap_stream, folder->value.str.val,myargc==4?flags->value.str.val:NIL,NIL,&st))
{
RETURN_TRUE;
}
else
{
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int imap_num_msg(int stream_id)
Gives the number of messages in the current mailbox */
void php3_imap_num_msg(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
RETURN_LONG(imap_le_struct->imap_stream->nmsgs);
}
/* }}} */
/* {{{ proto int imap_ping(int stream_id)
Check if the IMAP stream is still active */
void php3_imap_ping(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long( streamind );
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find( ind, &ind_type );
if ( !imap_le_struct || !IS_STREAM(ind_type)) {
php3_error( E_WARNING, "Unable to find stream pointer" );
RETURN_FALSE;
}
RETURN_LONG( mail_ping( imap_le_struct->imap_stream ) );
}
/* }}} */
/* {{{ proto int imap_num_recent(int stream_id)
Gives the number of recent messages in current mailbox */
void php3_imap_num_recent(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
RETURN_LONG(imap_le_struct->imap_stream->recent);
}
/* }}} */
/* {{{ proto int imap_expunge(int stream_id)
Delete all messages marked for deletion */
void php3_imap_expunge(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
mail_expunge (imap_le_struct->imap_stream);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto int imap_close(int stream_id [, int options])
Close an IMAP stream */
void php3_imap_close(INTERNAL_FUNCTION_PARAMETERS)
{
pval *options, *streamind;
int ind, ind_type;
pils *imap_le_struct=NULL;
int myargcount=ARG_COUNT(ht);
long flags = NIL;
if (myargcount < 1 || myargcount > 2 || getParameters(ht, myargcount, &streamind, &options) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if(myargcount==2) {
convert_to_long(options);
flags = options->value.lval;
/* Do the translation from PHP's internal PHP_EXPUNGE define to c-client's CL_EXPUNGE */
if(flags & PHP_EXPUNGE) {
flags ^= PHP_EXPUNGE;
flags |= CL_EXPUNGE;
}
imap_le_struct->flags = flags;
}
php3_list_delete(ind);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array imap_headers(int stream_id)
Returns headers for all messages in a mailbox */
void php3_imap_headers(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
int ind, ind_type;
unsigned long i;
char *t;
unsigned int msgno;
pils *imap_le_struct;
char tmp[MAILTMPLEN];
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
/* Initialize return array */
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
for (msgno = 1; msgno <= imap_le_struct->imap_stream->nmsgs; msgno++) {
MESSAGECACHE * cache = mail_elt (imap_le_struct->imap_stream,msgno);
mail_fetchstructure (imap_le_struct->imap_stream,msgno,NIL);
tmp[0] = cache->recent ? (cache->seen ? 'R': 'N') : ' ';
tmp[1] = (cache->recent | cache->seen) ? ' ' : 'U';
tmp[2] = cache->flagged ? 'F' : ' ';
tmp[3] = cache->answered ? 'A' : ' ';
tmp[4] = cache->deleted ? 'D' : ' ';
tmp[5] = cache->draft ? 'X' : ' ';
sprintf (tmp+5,"%4ld) ",cache->msgno);
mail_date (tmp+11,cache);
tmp[17] = ' ';
tmp[18] = '\0';
mail_fetchfrom (tmp+18,imap_le_struct->imap_stream,msgno,(long) 20);
strcat (tmp," ");
if ((i = cache->user_flags)) {
strcat (tmp,"{");
while (i) {
strcat (tmp,imap_le_struct->imap_stream->user_flags[find_rightmost_bit (&i)]);
if (i) strcat (tmp," ");
}
strcat (tmp,"} ");
}
mail_fetchsubject(t=tmp+strlen(tmp),imap_le_struct->imap_stream,msgno,(long)25);
sprintf (t+=strlen(t)," (%ld chars)",cache->rfc822_size);
add_next_index_string(return_value,tmp,1);
}
}
/* }}} */
/* {{{ proto string imap_fetchtext(int stream_id, int msg_no [, int options])
An alias for imap_body */
/* }}} */
/* {{{ proto string imap_body(int stream_id, int msg_no [, int options])
Read the message body */
void php3_imap_body(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, * msgno, *flags;
int ind, ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if (myargc <2 || myargc > 3 || getParameters(ht,myargc,&streamind,&msgno,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msgno);
if(myargc == 3) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
RETVAL_STRING(mail_fetchtext_full (imap_le_struct->imap_stream,msgno->value.lval,NIL,myargc == 3 ? flags->value.lval : NIL),1);
}
/* }}} */
/* {{{ proto string imap_fetchtext_full(int stream_id, int msg_no [, int options])
Read the body of a message */
void php3_imap_fetchtext_full(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, * msgno, *flags;
int ind, ind_type;
pils *imap_le_struct;
int myargcount = ARG_COUNT(ht);
if (myargcount >3 || myargcount <2 || getParameters(ht,myargcount,&streamind,&msgno,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msgno);
if (myargcount == 3) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
RETVAL_STRING(mail_fetchtext_full (imap_le_struct->imap_stream,msgno->value.lval,NIL,myargcount==3?flags->value.lval:NIL),1);
}
/* }}} */
/* {{{ proto int imap_mail_copy(int stream_id, int msg_no, string mailbox [, int options])
Copy specified message to a mailbox */
void php3_imap_mail_copy(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind,*seq, *folder, *options;
int ind, ind_type;
pils *imap_le_struct;
int myargcount = ARG_COUNT(ht);
if (myargcount > 4 || myargcount < 3
|| getParameters(ht,myargcount,&streamind,&seq,&folder,&options) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(seq);
convert_to_string(folder);
if(myargcount == 4) convert_to_long(options);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_copy_full(imap_le_struct->imap_stream,seq->value.str.val,folder->value.str.val,myargcount == 4 ? options->value.lval : NIL)==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool imap_mail_move(int stream_id, int msg_no, string mailbox [, int options])
Move specified message to a mailbox */
void php3_imap_mail_move(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind,*seq, *folder , *options;
int ind, ind_type;
pils *imap_le_struct;
int myargcount = ARG_COUNT(ht);
if (myargcount > 4 || myargcount < 3
|| getParameters(ht,myargcount,&streamind,&seq,&folder,&options) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(seq);
convert_to_string(folder);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_copy_full(imap_le_struct->imap_stream,seq->value.str.val,folder->value.str.val,myargcount == 4 ? ( options->value.lval | CP_MOVE ) : CP_MOVE )==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int imap_create(int stream_id, string mailbox)
An alias for imap_createmailbox */
/* }}} */
/* {{{ proto int imap_createmailbox(int stream_id, string mailbox)
Create a new mailbox */
void php3_imap_createmailbox(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *folder;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht)!=2 || getParameters(ht,2,&streamind,&folder) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(folder);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_create(imap_le_struct->imap_stream,folder->value.str.val)==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int imap_rename(int stream_id, string old_name, string new_name)
An alias for imap_renamemailbox */
/* }}} */
/* {{{ proto int imap_renamemailbox(int stream_id, string old_name, string new_name)
Rename a mailbox */
void php3_imap_renamemailbox(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *old, *new;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht)!=3 || getParameters(ht,3,&streamind,&old,&new)==FAILURE){
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(old);
convert_to_string(new);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_rename(imap_le_struct->imap_stream,old->value.str.val,new->value.str.val)==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool imap_deletemailbox(int stream_id, string mailbox)
Delete a mailbox */
void php3_imap_deletemailbox(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *folder;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht)!=2 || getParameters(ht,2,&streamind,&folder) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(folder);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_delete(imap_le_struct->imap_stream,folder->value.str.val)==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto array imap_listmailbox(int stream_id, string ref, string pattern)
An alias for imap_list */
/* }}} */
/* {{{ proto array imap_list(int stream_id, string ref, string pattern)
Read the list of mailboxes */
void php3_imap_list(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *ref, *pat;
int ind, ind_type;
pils *imap_le_struct;
STRINGLIST *cur=NIL;
/* set flag for normal, old mailbox list */
folderlist_style = FLIST_ARRAY;
if (ARG_COUNT(ht)!=3
|| getParameters(ht,3,&streamind,&ref,&pat) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(ref);
convert_to_string(pat);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
imap_folders = NIL;
mail_list(imap_le_struct->imap_stream,ref->value.str.val,pat->value.str.val);
if (imap_folders == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur=imap_folders;
while (cur != NIL ) {
add_next_index_string(return_value,cur->LTEXT,1);
cur=cur->next;
}
mail_free_stringlist (&imap_folders);
}
/* }}} */
/* {{{ proto array imap_getmailboxes(int stream_id, string ref, string pattern)
Reads the list of mailboxes and returns a full array of objects containing name, attributes, and delimiter. */
/* Author: CJH */
void php3_imap_list_full(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *ref, *pat, mboxob;
int ind, ind_type;
pils *imap_le_struct;
FOBJECTLIST *cur=NIL;
char *delim=NIL;
/* set flag for new, improved array of objects mailbox list */
folderlist_style = FLIST_OBJECT;
if (ARG_COUNT(ht)!=3
|| getParameters(ht,3,&streamind,&ref,&pat) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(ref);
convert_to_string(pat);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
imap_folder_objects = NIL;
mail_list(imap_le_struct->imap_stream,ref->value.str.val,pat->value.str.val);
if (imap_folder_objects == NIL) {
RETURN_FALSE;
}
array_init(return_value);
delim = emalloc(2 * sizeof(char));
cur=imap_folder_objects;
while (cur != NIL ) {
object_init(&mboxob);
add_property_string(&mboxob, "name", cur->LTEXT,1);
add_property_long(&mboxob, "attributes", cur->attributes);
#ifdef IMAP41
delim[0] = (char)cur->delimiter;
delim[1] = 0;
add_property_string(&mboxob, "delimiter", delim, 1);
#else
add_property_string(&mboxob, "delimiter", cur->delimiter, 1);
#endif
add_next_index_object(return_value, mboxob);
cur=cur->next;
}
mail_free_foblist(&imap_folder_objects);
efree(delim);
folderlist_style = FLIST_ARRAY; /* reset to default */
}
/* }}} */
/* {{{ proto array imap_scanmailbox(int stream_id, string ref, string pattern, string content)
An alias for imap_scan */
/* }}} */
/* {{{ proto array imap_scan(int stream_id, string ref, string pattern, string content)
Read list of mailboxes containing a certain string */
void php3_imap_listscan(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *ref, *pat, *content;
int ind, ind_type;
pils *imap_le_struct;
STRINGLIST *cur=NIL;
if (ARG_COUNT(ht)!=4 || getParameters(ht,4,&streamind,&ref,&pat,&content) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(ref);
convert_to_string(pat);
convert_to_string(content);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
imap_folders = NIL;
mail_scan(imap_le_struct->imap_stream,ref->value.str.val,pat->value.str.val,content->value.str.val);
if (imap_folders == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur=imap_folders;
while (cur != NIL ) {
add_next_index_string(return_value,cur->LTEXT,1);
cur=cur->next;
}
mail_free_stringlist (&imap_folders);
}
/* }}} */
/* {{{ proto object imap_check(int stream_id)
Get mailbox properties */
void php3_imap_check(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
int ind, ind_type;
pils *imap_le_struct;
char date[100];
if (ARG_COUNT(ht)!=1 || getParameters(ht,1,&streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if (mail_ping (imap_le_struct->imap_stream) == NIL ) {
RETURN_FALSE;
}
if (imap_le_struct->imap_stream && imap_le_struct->imap_stream->mailbox) {
rfc822_date (date);
object_init(return_value);
add_property_string(return_value,"Date",date,1);
add_property_string(return_value,"Driver",imap_le_struct->imap_stream->dtb->name,1);
add_property_string(return_value,"Mailbox",imap_le_struct->imap_stream->mailbox,1);
add_property_long(return_value,"Nmsgs",imap_le_struct->imap_stream->nmsgs);
add_property_long(return_value,"Recent",imap_le_struct->imap_stream->recent);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int imap_delete(int stream_id, int msg_no)
Mark a message for deletion */
void php3_imap_delete(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *sequence, *flags;
int ind, ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if ( myargc < 2 || myargc > 3 || getParameters(ht,myargc,&streamind,&sequence,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(sequence);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
mail_setflag_full(imap_le_struct->imap_stream,sequence->value.str.val,"\\DELETED",myargc == 3 ? flags->value.lval : NIL);
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto int imap_undelete(int stream_id, int msg_no)
Remove the delete flag from a message */
void php3_imap_undelete(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, * sequence, *flags;
int ind, ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if ( myargc < 2 || myargc > 3 || getParameters(ht,myargc,&streamind,&sequence,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(sequence);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
mail_clearflag_full(imap_le_struct->imap_stream,sequence->value.str.val,"\\DELETED",myargc == 3 ? flags->value.lval : NIL);
RETVAL_TRUE;
}
/* }}} */
/* {{{ proto object imap_headerinfo(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
An alias for imap_header */
/* }}} */
/* {{{ proto object imap_header(int stream_id, int msg_no [, int from_length [, int subject_length [, string default_host]]])
Read the header of the message */
void php3_imap_headerinfo(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, * msgno,to,tovals,from,fromvals,reply_to,reply_tovals,sender;
pval *fromlength;
pval *subjectlength;
pval sendervals,return_path,return_pathvals;
pval cc,ccvals,bcc,bccvals;
pval *defaulthost;
int ind, ind_type;
unsigned long length;
pils *imap_le_struct;
MESSAGECACHE * cache;
char *mystring;
char dummy[2000];
char fulladdress[MAILTMPLEN],tempaddress[MAILTMPLEN];
ENVELOPE *en;
ADDRESS *addresstmp,*addresstmp2;
int myargc;
myargc=ARG_COUNT(ht);
if (myargc<2 || myargc>5 || getParameters(ht,myargc,&streamind,&msgno,&fromlength,&subjectlength,&defaulthost)==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msgno);
if(myargc >= 3) convert_to_long(fromlength); else fromlength=0x00;;
if(myargc >= 4) convert_to_long(subjectlength); else subjectlength=0x00;
if(myargc == 5) convert_to_string(defaulthost);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if(msgno->value.lval && msgno->value.lval <= imap_le_struct->imap_stream->nmsgs && mail_fetchstructure (imap_le_struct->imap_stream,msgno->value.lval,NIL))
cache = mail_elt (imap_le_struct->imap_stream,msgno->value.lval);
else
RETURN_FALSE;
object_init(return_value);
mystring=mail_fetchheader_full(imap_le_struct->imap_stream,msgno->value.lval,NIL,&length,NIL);
if(myargc ==5)
rfc822_parse_msg (&en,NULL,mystring,length,NULL,defaulthost->value.str.val,NIL);
else
rfc822_parse_msg (&en,NULL,mystring,length,NULL,"UNKNOWN",NIL);
if (en->remail) add_property_string(return_value,"remail",en->remail,1);
if (en->date) add_property_string(return_value,"date",en->date,1);
if (en->date) add_property_string(return_value,"Date",en->date,1);
if (en->subject) add_property_string(return_value,"subject",en->subject,1);
if (en->subject) add_property_string(return_value,"Subject",en->subject,1);
if (en->in_reply_to) add_property_string(return_value,"in_reply_to",en->in_reply_to,1);
if (en->message_id) add_property_string(return_value,"message_id",en->message_id,1);
if (en->newsgroups) add_property_string(return_value,"newsgroups",en->newsgroups,1);
if (en->followup_to) add_property_string(return_value,"followup_to",en->followup_to,1);
if (en->references) add_property_string(return_value,"references",en->references,1);
if (en->to) {
int ok=1;
addresstmp=en->to;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "toaddress", fulladdress, 1);
addresstmp=en->to;
array_init( &to );
do {
object_init( &tovals);
if(addresstmp->personal) add_property_string( &tovals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &tovals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &tovals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &tovals, "host", addresstmp->host, 1);
add_next_index_object( &to, tovals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "to", to );
}
if(en->from)
{
int ok=1;
addresstmp=en->from;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "fromaddress", fulladdress, 1);
addresstmp=en->from;
array_init( &from );
do {
object_init( &fromvals);
if(addresstmp->personal) add_property_string( &fromvals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &fromvals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &fromvals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &fromvals, "host", addresstmp->host, 1);
add_next_index_object( &from, fromvals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "from", from );
}
if(en->cc)
{
int ok=1;
addresstmp=en->cc;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "ccaddress", fulladdress, 1);
addresstmp=en->cc;
array_init( &cc );
do {
object_init( &ccvals);
if(addresstmp->personal) add_property_string( &ccvals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &ccvals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &ccvals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &ccvals, "host", addresstmp->host, 1);
add_next_index_object( &cc, ccvals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "cc", cc );
}
if(en->bcc)
{
int ok=1;
addresstmp=en->bcc;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "bccaddress", fulladdress, 1);
addresstmp=en->bcc;
array_init( &bcc );
do {
object_init( &bccvals);
if(addresstmp->personal) add_property_string( &bccvals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &bccvals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &bccvals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &bccvals, "host", addresstmp->host, 1);
add_next_index_object( &bcc, bccvals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "bcc", bcc );
}
if(en->reply_to)
{
int ok=1;
addresstmp=en->reply_to;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "reply_toaddress", fulladdress, 1);
addresstmp=en->reply_to;
array_init( &reply_to );
do {
object_init( &reply_tovals);
if(addresstmp->personal) add_property_string( &reply_tovals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &reply_tovals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &reply_tovals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &reply_tovals, "host", addresstmp->host, 1);
add_next_index_object( &reply_to, reply_tovals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "reply_to", reply_to );
}
if(en->sender)
{
int ok=1;
addresstmp=en->sender;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "senderaddress", fulladdress, 1);
addresstmp=en->sender;
array_init( &sender );
do {
object_init( &sendervals);
if(addresstmp->personal) add_property_string( &sendervals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &sendervals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &sendervals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &sendervals, "host", addresstmp->host, 1);
add_next_index_object( &sender, sendervals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "sender", sender );
}
if(en->return_path)
{
int ok=1;
addresstmp=en->return_path;
fulladdress[0]=0x00;
while(ok && addresstmp) /* while length < 1000 and we are not at the end of the list */
{
addresstmp2=addresstmp->next; /* save the pointer to the next address */
addresstmp->next=NULL; /* make this address the only one now. */
tempaddress[0]=0x00; /* reset tempaddress buffer */
rfc822_write_address(tempaddress,addresstmp); /* ok, write the address into tempaddress string */
if((strlen(tempaddress) + strlen(fulladdress)) < 1000) /* is the new address + total address < 1000 */
{ /* yes */
if(strlen(fulladdress)) strcat(fulladdress,","); /* put in a comma */
strcat(fulladdress,tempaddress); /* put in the new address */
}
else /* no */
{
ok=0; /* stop looping */
strcat(fulladdress,", ...");
}
addresstmp->next=addresstmp2; /* reset the pointer to the next address first! */
addresstmp=addresstmp->next;
}
if(fulladdress) add_property_string( return_value, "return_pathaddress", fulladdress, 1);
addresstmp=en->return_path;
array_init( &return_path );
do {
object_init( &return_pathvals);
if(addresstmp->personal) add_property_string( &return_pathvals, "personal", addresstmp->personal, 1);
if(addresstmp->adl) add_property_string( &return_pathvals, "adl", addresstmp->adl, 1);
if(addresstmp->mailbox) add_property_string( &return_pathvals, "mailbox", addresstmp->mailbox, 1);
if(addresstmp->host) add_property_string( &return_pathvals, "host", addresstmp->host, 1);
add_next_index_object( &return_path, return_pathvals );
} while ( (addresstmp = addresstmp->next) );
add_assoc_object( return_value, "return_path", return_path );
}
add_property_string(return_value,"Recent",cache->recent ? (cache->seen ? "R": "N") : " ",1);
add_property_string(return_value,"Unseen",(cache->recent | cache->seen) ? " " : "U",1);
add_property_string(return_value,"Flagged",cache->flagged ? "F" : " ",1);
add_property_string(return_value,"Answered",cache->answered ? "A" : " ",1);
add_property_string(return_value,"Deleted",cache->deleted ? "D" : " ",1);
add_property_string(return_value,"Draft",cache->draft ? "X" : " ",1);
sprintf (dummy,"%4ld",cache->msgno);
add_property_string(return_value,"Msgno",dummy,1);
mail_date (dummy,cache);
add_property_string(return_value,"MailDate",dummy,1);
sprintf (dummy,"%ld",cache->rfc822_size);
add_property_string(return_value,"Size",dummy,1);
add_property_long(return_value,"udate",mail_longdate(cache));
if(en->from && fromlength)
{
fulladdress[0]=0x00;
mail_fetchfrom(fulladdress,imap_le_struct->imap_stream,msgno->value.lval,fromlength->value.lval);
add_property_string(return_value,"fetchfrom",fulladdress,1);
}
if(en->subject && subjectlength)
{
fulladdress[0]=0x00;
mail_fetchsubject(fulladdress,imap_le_struct->imap_stream,msgno->value.lval,subjectlength->value.lval);
add_property_string(return_value,"fetchsubject",fulladdress,1);
}
}
/* }}} */
/* KMLANG */
/* {{{ proto array imap_listsubscribed(int stream_id, string ref, string pattern)
An alias for imap_lsub */
/* }}} */
/* {{{ proto array imap_lsub(int stream_id, string ref, string pattern)
Return a list of subscribed mailboxes */
void php3_imap_lsub(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *ref, *pat;
int ind, ind_type;
pils *imap_le_struct;
STRINGLIST *cur=NIL;
/* set flag for normal, old mailbox list */
folderlist_style = FLIST_ARRAY;
if (ARG_COUNT(ht)!=3 || getParameters(ht,3,&streamind,&ref,&pat) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(ref);
convert_to_string(pat);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
imap_sfolders = NIL;
mail_lsub(imap_le_struct->imap_stream,ref->value.str.val,pat->value.str.val);
if (imap_sfolders == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur=imap_sfolders;
while (cur != NIL ) {
add_next_index_string(return_value,cur->LTEXT,1);
cur=cur->next;
}
mail_free_stringlist (&imap_sfolders);
}
/* }}} */
/* {{{ proto array imap_getsubscribed(int stream_id, string ref, string pattern)
Return a list of subscribed mailboxes */
/* Author: CJH */
void php3_imap_lsub_full(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *ref, *pat, mboxob;
int ind, ind_type;
pils *imap_le_struct;
FOBJECTLIST *cur=NIL;
char *delim=NIL;
delim = emalloc(2 * sizeof(char));
/* set flag for new, improved array of objects list */
folderlist_style = FLIST_OBJECT;
if (ARG_COUNT(ht)!=3 || getParameters(ht,3,&streamind,&ref,&pat) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(ref);
convert_to_string(pat);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
imap_sfolder_objects = NIL;
mail_lsub(imap_le_struct->imap_stream,ref->value.str.val,pat->value.str.val);
if (imap_sfolder_objects == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur=imap_sfolder_objects;
while (cur != NIL ) {
object_init(&mboxob);
add_property_string(&mboxob, "name", cur->LTEXT, 1);
add_property_long(&mboxob, "attributes", cur->attributes);
#ifdef IMAP41
delim[0] = (char)cur->delimiter;
delim[1] = 0;
add_property_string(&mboxob, "delimiter", delim, 1);
#else
add_property_string(&mboxob, "delimiter", cur->delimiter, 1);
#endif
add_next_index_object(return_value, mboxob);
cur=cur->next;
}
mail_free_foblist (&imap_sfolder_objects);
efree(delim);
folderlist_style = FLIST_ARRAY; /* reset to default */
}
/* }}} */
/* {{{ proto int imap_subscribe(int stream_id, string mailbox)
Subscribe to a mailbox */
void php3_imap_subscribe(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *folder;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht)!=2 || getParameters(ht,2,&streamind,&folder) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(folder);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_subscribe(imap_le_struct->imap_stream,folder->value.str.val)==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int imap_unsubscribe(int stream_id, string mailbox)
Unsubscribe from a mailbox */
void php3_imap_unsubscribe(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *folder;
int ind, ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if (myargc !=2 || getParameters(ht,myargc,&streamind,&folder) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(folder);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ( mail_unsubscribe(imap_le_struct->imap_stream,folder->value.str.val)==T ) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
void imap_add_body( pval *arg, BODY *body )
{
pval parametres, param, dparametres, dparam;
PARAMETER *par, *dpar;
PART *part;
if(body->type) add_property_long( arg, "type", body->type );
if(body->encoding) add_property_long( arg, "encoding", body->encoding );
if ( body->subtype ){
add_property_long( arg, "ifsubtype", 1 );
add_property_string( arg, "subtype", body->subtype, 1 );
} else {
add_property_long( arg, "ifsubtype", 0 );
}
if ( body->description ){
add_property_long( arg, "ifdescription", 1 );
add_property_string( arg, "description", body->description, 1 );
} else {
add_property_long( arg, "ifdescription", 0 );
}
if ( body->id ){
add_property_long( arg, "ifid", 1 );
add_property_string( arg, "id", body->id, 1 );
} else {
add_property_long( arg, "ifid", 0 );
}
if(body->size.lines) add_property_long( arg, "lines", body->size.lines );
if(body->size.bytes) add_property_long( arg, "bytes", body->size.bytes );
#ifdef IMAP41
if ( body->disposition.type ){
add_property_long( arg, "ifdisposition", 1);
add_property_string( arg, "disposition", body->disposition.type, 1);
} else {
add_property_long( arg, "ifdisposition", 0);
}
if ( body->disposition.parameter ) {
dpar = body->disposition.parameter;
add_property_long( arg, "ifdparameters", 1);
array_init( &dparametres );
do {
object_init( &dparam );
add_property_string( &dparam, "attribute", dpar->attribute, 1);
add_property_string( &dparam, "value", dpar->value, 1);
add_next_index_object( &dparametres, dparam );
} while ( (dpar = dpar->next) );
add_assoc_object( arg, "dparameters", dparametres );
} else {
add_property_long( arg, "ifdparameters", 0);
}
#endif
if ( (par = body->parameter) ) {
add_property_long( arg, "ifparameters", 1 );
array_init( ¶metres );
do {
object_init( ¶m );
if(par->attribute) add_property_string( ¶m, "attribute", par->attribute, 1 );
if(par->value) add_property_string( ¶m, "value", par->value, 1 );
add_next_index_object( ¶metres, param );
} while ( (par = par->next) );
} else {
object_init(¶metres);
add_property_long( arg, "ifparameters", 0 );
}
add_assoc_object( arg, "parameters", parametres );
/* multipart message ? */
if ( body->type == TYPEMULTIPART ) {
array_init( ¶metres );
for ( part = body->CONTENT_PART; part; part = part->next ) {
object_init( ¶m );
imap_add_body( ¶m, &part->body );
add_next_index_object( ¶metres, param );
}
add_assoc_object( arg, "parts", parametres );
}
/* encapsulated message ? */
if ((body->type == TYPEMESSAGE) && (!strcasecmp(body->subtype, "rfc822"))) {
body = body->CONTENT_MSG_BODY;
array_init(¶metres);
object_init( ¶m );
imap_add_body( ¶m, body );
add_next_index_object(¶metres, param );
add_assoc_object( arg, "parts", parametres );
}
}
/* {{{ proto object imap_fetchstructure(int stream_id, int msg_no [, int options])
Read the full structure of a message */
void php3_imap_fetchstructure(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *msgno,*flags;
int ind, ind_type;
pils *imap_le_struct;
BODY *body;
int myargc=ARG_COUNT(ht);
if ( myargc < 2 || myargc > 3 || getParameters( ht, myargc, &streamind, &msgno ,&flags) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_long( streamind );
convert_to_long( msgno );
if (msgno->value.lval < 1) {
RETURN_FALSE;
}
if(myargc == 3) convert_to_long(flags);
object_init(return_value);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find( ind, &ind_type );
if ( !imap_le_struct || !IS_STREAM(ind_type)) {
php3_error( E_WARNING, "Unable to find stream pointer" );
RETURN_FALSE;
}
mail_fetchstructure_full( imap_le_struct->imap_stream, msgno->value.lval, &body ,myargc == 3 ? flags->value.lval : NIL);
if ( !body ) {
php3_error( E_WARNING, "No body information available" );
RETURN_FALSE;
}
imap_add_body( return_value, body );
}
/* }}} */
/* {{{ proto string imap_fetchbody(int stream_id, int msg_no, string section [, int options])
Get a specific body section */
void php3_imap_fetchbody(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *msgno, *sec,*flags;
int ind, ind_type;
pils *imap_le_struct;
char *body;
unsigned long len;
int myargc=ARG_COUNT(ht);
if(myargc < 3 || myargc >4 || getParameters( ht, myargc, &streamind, &msgno, &sec,&flags ) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_long( streamind );
convert_to_long( msgno );
convert_to_string( sec );
if(myargc == 4) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find( ind, &ind_type );
if ( !imap_le_struct || !IS_STREAM(ind_type)) {
php3_error( E_WARNING, "Unable to find stream pointer" );
RETURN_FALSE;
}
body = mail_fetchbody_full( imap_le_struct->imap_stream, msgno->value.lval, sec->value.str.val, &len,myargc == 4 ? flags->value.lval : NIL );
if ( !body ) {
php3_error( E_WARNING, "No body information available" );
RETURN_FALSE;
}
RETVAL_STRINGL( body ,len,1);
}
/* }}} */
/* {{{ proto string imap_base64(string text)
Decode BASE64 encoded text */
void php3_imap_base64(INTERNAL_FUNCTION_PARAMETERS)
{
pval *text;
char *decode;
unsigned long newlength;
if ( ARG_COUNT(ht) != 1 || getParameters( ht, 1, &text) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string( text );
object_init(return_value);
decode = (char *) rfc822_base64((unsigned char *) text->value.str.val, text->value.str.len,&newlength);
RETVAL_STRINGL(decode,newlength,1);
}
/* }}} */
/* {{{ proto string imap_qprint(string text)
Convert a quoted-printable string to an 8-bit string */
void php3_imap_qprint(INTERNAL_FUNCTION_PARAMETERS)
{
pval *text;
char *decode;
unsigned long newlength;
if ( ARG_COUNT(ht) != 1 || getParameters( ht, 1, &text) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string( text );
/* object_init(return_value); Why is this here? -RL */
decode = (char *) rfc822_qprint((unsigned char *) text->value.str.val, text->value.str.len,&newlength);
RETVAL_STRINGL(decode,newlength,1);
}
/* }}} */
/* {{{ proto string imap_8bit(string text)
Convert an 8-bit string to a quoted-printable string */
void php3_imap_8bit(INTERNAL_FUNCTION_PARAMETERS)
{
pval *text;
char *decode;
unsigned long newlength;
if ( ARG_COUNT(ht) != 1 || getParameters( ht, 1, &text) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string( text );
object_init(return_value);
decode = (char *) rfc822_8bit((unsigned char *) text->value.str.val, text->value.str.len,&newlength);
RETVAL_STRINGL(decode,newlength,1);
}
/* }}} */
/* {{{ proto string imap_binary(string text)
Convert an 8bit string to a base64 string */
void php3_imap_binary(INTERNAL_FUNCTION_PARAMETERS)
{
pval *text;
unsigned long len;
if ( ARG_COUNT(ht) != 1 || getParameters( ht, 1, &text) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string(text);
RETVAL_STRINGL(rfc822_binary(text->value.str.val,text->value.str.len,&len),len,1);
}
/* }}} */
/* {{{ proto array imap_mailboxmsginfo(int stream_id)
Returns info about the current mailbox in an associative array */
void php3_imap_mailboxmsginfo(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
char date[100];
int ind, ind_type;
unsigned int msgno;
pils *imap_le_struct;
unsigned unreadmsg,msize;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &streamind) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if(!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
/* Initialize return array */
if(object_init(return_value) == FAILURE) {
RETURN_FALSE;
}
unreadmsg = 0;
msize = 0;
for (msgno = 1; msgno <= imap_le_struct->imap_stream->nmsgs; msgno++) {
MESSAGECACHE * cache = mail_elt (imap_le_struct->imap_stream,msgno);
mail_fetchstructure (imap_le_struct->imap_stream,msgno,NIL);
unreadmsg = cache->recent ? (cache->seen ? unreadmsg : unreadmsg++) : unreadmsg;
unreadmsg = (cache->recent | cache->seen) ? unreadmsg : unreadmsg++;
msize = msize + cache->rfc822_size;
}
add_property_long(return_value,"Unread",unreadmsg);
add_property_long(return_value,"Nmsgs",imap_le_struct->imap_stream->nmsgs);
add_property_long(return_value,"Size",msize);
rfc822_date (date);
add_property_string(return_value,"Date",date,1);
add_property_string(return_value,"Driver",imap_le_struct->imap_stream->dtb->name,1);
add_property_string(return_value,"Mailbox",imap_le_struct->imap_stream->mailbox,1);
add_property_long(return_value,"Recent",imap_le_struct->imap_stream->recent);
}
/* }}} */
/* {{{ proto string imap_rfc822_write_address(string mailbox, string host, string personal)
Returns a properly formatted email address given the mailbox, host, and personal info */
void php3_imap_rfc822_write_address(INTERNAL_FUNCTION_PARAMETERS)
{
pval *mailbox,*host,*personal;
ADDRESS *addr;
char string[MAILTMPLEN];
int argc;
argc=ARG_COUNT(ht);
if ( argc != 3 || getParameters( ht, argc, &mailbox,&host,&personal) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string(mailbox);
convert_to_string(host);
convert_to_string(personal);
addr=mail_newaddr();
if(mailbox) addr->mailbox = cpystr(mailbox->value.str.val);
if(host) addr->host = cpystr(host->value.str.val);
if(personal) addr->personal = cpystr(personal->value.str.val);
addr->next=NIL;
addr->error=NIL;
addr->adl=NIL;
string[0]=0x00;
rfc822_write_address(string,addr);
RETVAL_STRING(string,1);
}
/* }}} */
/* {{{ proto array imap_rfc822_parse_adrlist(string address_string, string default_host)
Parses an address string */
void php3_imap_rfc822_parse_adrlist(INTERNAL_FUNCTION_PARAMETERS)
{
pval *string,*defaulthost,tovals;
ADDRESS *addresstmp;
ENVELOPE *env;
int argc;
env=mail_newenvelope();
argc=ARG_COUNT(ht);
if ( argc != 2 || getParameters( ht, argc, &string,&defaulthost) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string(string);
convert_to_string(defaulthost);
rfc822_parse_adrlist(&env->to,string->value.str.val,defaulthost->value.str.val);
if(array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
addresstmp=env->to;
if(addresstmp) do {
object_init(&tovals);
if(addresstmp->mailbox) add_property_string(&tovals,"mailbox",addresstmp->mailbox,1);
if(addresstmp->host) add_property_string(&tovals,"host",addresstmp->host,1);
if(addresstmp->personal) add_property_string(&tovals,"personal",addresstmp->personal,1);
if(addresstmp->adl) add_property_string(&tovals,"adl",addresstmp->adl,1);
add_next_index_object(return_value, tovals);
} while ((addresstmp = addresstmp->next));
}
/* }}} */
/* {{{ proto string imap_utf8(string string)
Convert a string to UTF-8 */
void php3_imap_utf8(INTERNAL_FUNCTION_PARAMETERS)
{
pval *string;
int argc;
SIZEDTEXT src,dest;
src.data=NULL;
src.size=0;
dest.data=NULL;
dest.size=0;
argc=ARG_COUNT(ht);
if ( argc != 1 || getParameters( ht, argc, &string) == FAILURE ) {
WRONG_PARAM_COUNT;
}
convert_to_string(string);
cpytxt(&src,string->value.str.val,string->value.str.len);
utf8_mime2text(&src,&dest);
RETURN_STRINGL(dest.data,strlen(dest.data),1);
}
/* }}} */
/* macros for the modified utf7 conversion functions */
/* author: Andrew Skalski <askalski@chek.com> */
/* tests `c' and returns true if it is a special character */
#define SPECIAL(c) ((c) <= 0x1f || (c) >= 0x7f)
/* validate a modified-base64 character */
#define B64CHAR(c) (isalnum(c) || (c) == '+' || (c) == ',')
/* map the low 64 bits of `n' to the modified-base64 characters */
#define B64(n) ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
"abcdefghijklmnopqrstuvwxyz0123456789+,"[(n) & 0x3f])
/* map the modified-base64 character `c' to its 64 bit value */
#define UNB64(c) ((c) == '+' ? 62 : (c) == ',' ? 63 : (c) >= 'a' ? \
(c) - 71 : (c) >= 'A' ? (c) - 65 : (c) + 4)
/* {{{ proto string imap_utf7_decode(string buf)
Decode a modified UTF-7 string */
void php3_imap_utf7_decode(INTERNAL_FUNCTION_PARAMETERS)
{
/* author: Andrew Skalski <askalski@chek.com> */
int argc;
pval *arg;
const unsigned char *in, *inp, *endp;
unsigned char *out, *outp;
int inlen, outlen;
enum { ST_NORMAL, /* printable text */
ST_DECODE0, /* encoded text rotation... */
ST_DECODE1,
ST_DECODE2,
ST_DECODE3
} state;
/* collect arguments */
argc = ARG_COUNT(ht);
if (argc != 1 || getParameters(ht, argc, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
in = (const unsigned char*) arg->value.str.val;
inlen = arg->value.str.len;
/* validate and compute length of output string */
outlen = 0;
state = ST_NORMAL;
for (endp = (inp = in) + inlen; inp < endp; inp++) {
if (state == ST_NORMAL) {
/* process printable character */
if (SPECIAL(*inp)) {
php3_error(E_WARNING, "imap_utf7_decode: "
"Invalid modified UTF-7 character: "
"`%c'", *inp);
RETURN_FALSE;
}
else if (*inp != '&') {
outlen++;
}
else if (inp + 1 == endp) {
php3_error(E_WARNING, "imap_utf7_decode: "
"Unexpected end of string");
RETURN_FALSE;
}
else if (inp[1] != '-') {
state = ST_DECODE0;
}
else {
outlen++;
inp++;
}
}
else if (*inp == '-') {
/* return to NORMAL mode */
if (state == ST_DECODE1) {
php3_error(E_WARNING, "imap_utf7_decode: "
"Stray modified base64 character: "
"`%c'", *--inp);
RETURN_FALSE;
}
state = ST_NORMAL;
}
else if (!B64CHAR(*inp)) {
php3_error(E_WARNING, "imap_utf7_decode: "
"Invalid modified base64 character: "
"`%c'", *inp);
RETURN_FALSE;
}
else {
switch (state) {
case ST_DECODE3:
outlen++;
state = ST_DECODE0;
break;
case ST_DECODE2:
case ST_DECODE1:
outlen++;
case ST_DECODE0:
state++;
case ST_NORMAL:
break;
}
}
}
/* enforce end state */
if (state != ST_NORMAL) {
php3_error(E_WARNING, "imap_utf7_decode: "
"Unexpected end of string");
RETURN_FALSE;
}
/* allocate output buffer */
if ((out = emalloc(outlen + 1)) == NULL) {
php3_error(E_WARNING, "imap_utf7_decode: "
"Unable to allocate result string");
RETURN_FALSE;
}
/* decode input string */
outp = out;
state = ST_NORMAL;
for (endp = (inp = in) + inlen; inp < endp; inp++) {
if (state == ST_NORMAL) {
if (*inp == '&' && inp[1] != '-') {
state = ST_DECODE0;
}
else if ((*outp++ = *inp) == '&') {
inp++;
}
}
else if (*inp == '-') {
state = ST_NORMAL;
}
else {
/* decode input character */
switch (state) {
case ST_DECODE0:
*outp = UNB64(*inp) << 2;
state = ST_DECODE1;
break;
case ST_DECODE1:
outp[1] = UNB64(*inp);
*outp++ |= outp[1] >> 4;
*outp <<= 4;
state = ST_DECODE2;
break;
case ST_DECODE2:
outp[1] = UNB64(*inp);
*outp++ |= outp[1] >> 2;
*outp <<= 6;
state = ST_DECODE3;
break;
case ST_DECODE3:
*outp++ |= UNB64(*inp);
state = ST_DECODE0;
case ST_NORMAL:
break;
}
}
}
*outp = 0;
#if DEBUG
/* warn if we computed outlen incorrectly */
if (outp - out != outlen) {
php3_error(E_WARNING,
"imap_utf7_decode: outp - out [%d] != outlen [%d]",
outp - out, outlen);
}
#endif
RETURN_STRINGL(out, outlen, 0);
}
/* }}} */
/* {{{ proto string imap_utf7_encode(string buf)
Encode a string in modified UTF-7 */
void php3_imap_utf7_encode(INTERNAL_FUNCTION_PARAMETERS)
{
/* author: Andrew Skalski <askalski@chek.com> */
int argc;
pval *arg;
const unsigned char *in, *inp, *endp;
unsigned char *out, *outp;
int inlen, outlen;
enum { ST_NORMAL, /* printable text */
ST_ENCODE0, /* encoded text rotation... */
ST_ENCODE1,
ST_ENCODE2
} state;
/* collect arguments */
argc = ARG_COUNT(ht);
if (argc != 1 || getParameters(ht, argc, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
in = (const unsigned char*) arg->value.str.val;
inlen = arg->value.str.len;
/* compute the length of the result string */
outlen = 0;
state = ST_NORMAL;
endp = (inp = in) + inlen;
while (inp < endp) {
if (state == ST_NORMAL) {
if (SPECIAL(*inp)) {
state = ST_ENCODE0;
outlen++;
}
else if (*inp++ == '&') {
outlen++;
}
outlen++;
}
else if (!SPECIAL(*inp)) {
state = ST_NORMAL;
}
else {
/* ST_ENCODE0 -> ST_ENCODE1 - two chars
* ST_ENCODE1 -> ST_ENCODE2 - one char
* ST_ENCODE2 -> ST_ENCODE0 - one char
*/
if (state == ST_ENCODE2) {
state = ST_ENCODE0;
}
else if (state++ == ST_ENCODE0) {
outlen++;
}
outlen++;
inp++;
}
}
/* allocate output buffer */
if ((out = emalloc(outlen + 1)) == NULL) {
php3_error(E_WARNING, "imap_utf7_encode: "
"Unable to allocate result string");
RETURN_FALSE;
}
/* encode input string */
outp = out;
state = ST_NORMAL;
endp = (inp = in) + inlen;
while (inp < endp || state != ST_NORMAL) {
if (state == ST_NORMAL) {
if (SPECIAL(*inp)) {
/* begin encoding */
*outp++ = '&';
state = ST_ENCODE0;
}
else if ((*outp++ = *inp++) == '&') {
*outp++ = '-';
}
}
else if (inp == endp || !SPECIAL(*inp)) {
/* flush overflow and terminate region */
if (state != ST_ENCODE0) {
*outp++ = B64(*outp);
}
*outp++ = '-';
state = ST_NORMAL;
}
else {
/* encode input character */
switch (state) {
case ST_ENCODE0:
*outp++ = B64(*inp >> 2);
*outp = *inp++ << 4;
state = ST_ENCODE1;
break;
case ST_ENCODE1:
*outp++ = B64(*outp | *inp >> 4);
*outp = *inp++ << 2;
state = ST_ENCODE2;
break;
case ST_ENCODE2:
*outp++ = B64(*outp | *inp >> 6);
*outp++ = B64(*inp++);
state = ST_ENCODE0;
case ST_NORMAL:
break;
}
}
}
*outp = 0;
#if DEBUG
/* warn if we computed outlen incorrectly */
if (outp - out != outlen) {
php3_error(E_WARNING,
"imap_utf7_encode: outp - out [%d] != outlen [%d]",
outp - out, outlen);
}
#endif
RETURN_STRINGL(out, outlen, 0);
}
/* }}} */
#undef SPECIAL
#undef B64CHAR
#undef B64
#undef UNB64
/* {{{ proto object imap_mime_header_decode(string str)
Decode mime header element in accordance with RFC 2047 and return array of objects containing 'charset' encoding and decoded 'text' */
void php3_imap_mime_header_decode(INTERNAL_FUNCTION_PARAMETERS)
{
/* string, string-endp, charset, charset-endp,
* encoding, encoding-endp, text, text-endp,
* language separator, marked start of unencoded text */
unsigned char *s, *se, *cs, *ce, *e, *ee, *t, *te, *ls, *mark;
/* decoded text */
SIZEDTEXT decoded;
int argc;
pval *arg, obj;
/* collect arguments */
argc = ARG_COUNT(ht);
if (argc != 1 || getParameters(ht, argc, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
s = arg->value.str.val;
se = s + arg->value.str.len;
/* init return value */
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
/* conversion routine derived from Mark Crispin's c-client library
* function utf_mime2text()
*/
#define MINENCWORD 9
for (mark = s; s < se; s++) {
/* try to tokenize as a mime2 encoded word */
if ((se - s) > MINENCWORD && *s == '=' && s[1] == '?' &&
(cs = mime2_token(s + 2, se, &ce)) &&
(e = mime2_token(ce + 1, se, &ee)) &&
(t = mime2_text(e + 2, se, &te)))
{
/* try to decode the text */
if (!mime2_decode(e, t, te, &decoded)) {
/* skip over the block */
s = te + 1;
continue;
}
/* first, flush out any non-encoded text */
if (mark < s) {
object_init(&obj);
add_property_string(&obj,
"charset", "default", 1);
add_property_stringl(&obj,
"text", mark, s - mark, 1);
add_next_index_object(return_value, obj);
}
/* advance the string pointer and the mark */
s = te + 1;
mark = s + 1;
/* tie off charset and remove language specifier */
*ce = '\0';
if (ls = strchr(cs, '*')) *ls = '\0';
/* flush decoded text */
object_init(&obj);
add_property_string(&obj, "charset", cs, 1);
add_property_stringl(&obj, "text",
decoded.data, decoded.size, 1);
add_next_index_object(return_value, obj);
/* restore charset and free decoded data */
if (ls) *ls = '*';
*ce = '?';
fs_give((void**) &decoded.data);
/* skip trailing whitespace; handle continuations */
for (t = s + 1; t < se && (*t == ' ' || *t == '\t');
t++);
if (t < se - MINENCWORD) switch (*t) {
case '=': /* encoded block? */
if (t[1] == '?') s = t - 1;
break;
case '\r': /* CR */
if (t[1] == '\n') t++;
case '\n': /* LF */
if (t[1] == ' ' || t[1] == '\t') {
/* eat up leading spaces/tabs */
do t++;
while (t < se - MINENCWORD &&
(t[1] == ' ' || t[1] == '\t'));
/* found something interesting */
if (t < se - MINENCWORD &&
t[1] == '=' && t[2] == '?')
{
s = t;
}
}
}
}
}
#undef MINENCWORD
/* flush out remaining non-encoded text */
if (mark < se) {
object_init(&obj);
add_property_string(&obj, "charset", "default", 1);
add_property_stringl(&obj, "text", mark, se - mark, 1);
add_next_index_object(return_value, obj);
}
}
/* }}} */
/* {{{ proto int imap_setflag_full(int stream_id, string sequence, string flag [, int options])
Sets flags on messages */
void php3_imap_setflag_full(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
pval *sequence;
pval *flag;
pval *flags;
int ind,ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if (myargc < 3 || myargc > 4 || getParameters(ht, myargc, &streamind,&sequence,&flag,&flags) ==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(sequence);
convert_to_string(flag);
if(myargc==4) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if(!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
mail_setflag_full(imap_le_struct->imap_stream,sequence->value.str.val,flag->value.str.val,myargc == 4 ? flags->value.lval : NIL);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void imap_clearflag_full(int stream_id, string sequence, string flag [, int options])
Clears flags on messages */
void php3_imap_clearflag_full(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
pval *sequence;
pval *flag;
pval *flags;
int ind,ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if (myargc < 3 || myargc > 4 || getParameters(ht, myargc, &streamind,&sequence,&flag,&flags) ==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(sequence);
convert_to_string(flag);
if(myargc==4) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if(!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
mail_clearflag_full(imap_le_struct->imap_stream,sequence->value.str.val,flag->value.str.val,myargc == 4 ? flags->value.lval : NIL);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array imap_sort(int stream_id, int criteria, int reverse [, int options])
Sort an array of message headers */
void php3_imap_sort(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind;
pval *pgm;
pval *rev;
pval *flags;
int ind,ind_type;
unsigned long *slst,*sl;
SORTPGM *mypgm=NIL;
SEARCHPGM *spg=NIL;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if (myargc < 3 || myargc > 4 || getParameters(ht, myargc, &streamind,&pgm,&rev,&flags) ==FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(rev);
convert_to_long(pgm);
if(pgm->value.lval>SORTSIZE) {
php3_error(E_WARNING, "Unrecognized sort criteria");
RETURN_FALSE;
}
if(myargc==4) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if(!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
spg = mail_newsearchpgm();
mypgm=mail_newsortpgm();
mypgm->reverse=rev->value.lval;
mypgm->function=pgm->value.lval;
mypgm->next=NIL;
array_init(return_value);
slst=mail_sort(imap_le_struct->imap_stream,NIL,spg,mypgm,myargc == 4 ? flags->value.lval:NIL);
for (sl = slst; *sl; sl++) {
add_next_index_long(return_value,*sl);
}
fs_give ((void **) &slst);
}
/* }}} */
/* {{{ proto string imap_fetchheader(int stream_id, int msg_no [, int options])
Get the full unfiltered header for a message */
void php3_imap_fetchheader(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *msgno, *flags;
int ind, ind_type, msgindex;
pils *imap_le_struct;
int myargc = ARG_COUNT(ht);
if (myargc < 2 || myargc > 3 || getParameters(ht,myargc,&streamind,&msgno,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msgno);
if (myargc == 3) convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if ((myargc == 3) && (flags->value.lval & FT_UID)) {
/* This should be cached; if it causes an extra RTT to the
IMAP server, then that's the price we pay for making sure
we don't crash. */
msgindex = mail_msgno(imap_le_struct->imap_stream, msgno->value.lval);
} else {
msgindex = msgno->value.lval;
}
if ((msgindex < 1) || ((unsigned) msgindex > imap_le_struct->imap_stream->nmsgs)) {
php3_error(E_WARNING, "Bad message number");
RETURN_FALSE;
}
RETVAL_STRING(mail_fetchheader_full (imap_le_struct->imap_stream,msgno->value.lval,NIL,NIL,myargc == 3 ? flags->value.lval : NIL),1);
}
/* }}} */
/* {{{ proto int imap_uid(int stream_id, int msg_no)
Get the unique message id associated with a standard sequential message number */
void php3_imap_uid(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *msgno;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht) != 2 || getParameters(ht,2,&streamind,&msgno) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msgno);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
RETURN_LONG(mail_uid(imap_le_struct->imap_stream, msgno->value.lval));
}
/* }}} */
/* {{{ proto int imap_msgno(int stream_id, int unique_msg_id)
Get the sequence number associated with a UID */
void php3_imap_msgno(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *msgno;
int ind, ind_type;
pils *imap_le_struct;
if (ARG_COUNT(ht) != 2 || getParameters(ht,2,&streamind,&msgno) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msgno);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
RETURN_LONG(mail_msgno(imap_le_struct->imap_stream, msgno->value.lval));
}
/* }}} */
/* {{{ proto object imap_status(int stream_id, string mailbox, int options)
Get status info from a mailbox */
void php3_imap_status(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *mbx, *flags;
int ind, ind_type;
pils *imap_le_struct;
int myargc=ARG_COUNT(ht);
if (myargc != 3 || getParameters(ht,myargc,&streamind,&mbx,&flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(mbx);
convert_to_long(flags);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if(object_init(return_value) == FAILURE){
RETURN_FALSE;
}
if(mail_status(imap_le_struct->imap_stream, mbx->value.str.val, flags->value.lval))
{
add_property_long(return_value,"flags",status_flags);
if(status_flags & SA_MESSAGES) add_property_long(return_value,"messages",status_messages);
if(status_flags & SA_RECENT) add_property_long(return_value,"recent",status_recent);
if(status_flags & SA_UNSEEN) add_property_long(return_value,"unseen",status_unseen);
if(status_flags & SA_UIDNEXT) add_property_long(return_value,"uidnext",status_uidnext);
if(status_flags & SA_UIDVALIDITY) add_property_long(return_value,"uidvalidity",status_uidvalidity);
#ifdef SA_QUOTA
if(status_flags & SA_QUOTA) add_property_long(return_value,"quota",status_quota);
#endif
#ifdef SA_QUOTA
if(status_flags & SA_QUOTA_ALL) add_property_long(return_value,"quota_all",status_quota_all);
#endif
}
else
{
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto object imap_bodystruct(int stream_id, int msg_no, int section)
Read the structure of a specified body section of a specific message */
void php3_imap_bodystruct(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *msg, *section;
int ind, ind_type;
pils *imap_le_struct;
pval parametres, param, dparametres, dparam;
PARAMETER *par, *dpar;
BODY *body;
int myargc=ARG_COUNT(ht);
if (myargc != 3 || getParameters(ht,myargc,&streamind,&msg,§ion) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_long(msg);
convert_to_string(section);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
if(object_init(return_value) == FAILURE){
RETURN_FALSE;
}
body=mail_body(imap_le_struct->imap_stream, msg->value.lval, section->value.str.val);
if(body->type) add_property_long( return_value, "type", body->type );
if(body->encoding) add_property_long( return_value, "encoding", body->encoding );
if ( body->subtype ){
add_property_long( return_value, "ifsubtype", 1 );
add_property_string( return_value, "subtype", body->subtype, 1 );
} else {
add_property_long( return_value, "ifsubtype", 0 );
}
if ( body->description ){
add_property_long( return_value, "ifdescription", 1 );
add_property_string( return_value, "description", body->description, 1 );
} else {
add_property_long( return_value, "ifdescription", 0 );
}
if ( body->id ){
add_property_long( return_value, "ifid", 1 );
add_property_string( return_value, "id", body->id, 1 );
} else {
add_property_long( return_value, "ifid", 0 );
}
if(body->size.lines) add_property_long( return_value, "lines", body->size.lines );
if(body->size.bytes) add_property_long( return_value, "bytes", body->size.bytes );
#ifdef IMAP41
if ( body->disposition.type ){
add_property_long( return_value, "ifdisposition", 1);
add_property_string( return_value, "disposition", body->disposition.type, 1);
} else {
add_property_long( return_value, "ifdisposition", 0);
}
if ( body->disposition.parameter ) {
dpar = body->disposition.parameter;
add_property_long( return_value, "ifdparameters", 1);
array_init( &dparametres );
do {
object_init( &dparam );
add_property_string( &dparam, "attribute", dpar->attribute, 1);
add_property_string( &dparam, "value", dpar->value, 1);
add_next_index_object( &dparametres, dparam );
} while ( (dpar = dpar->next) );
add_assoc_object( return_value, "dparameters", dparametres );
} else {
add_property_long( return_value, "ifdparameters", 0);
}
#endif
if ( (par = body->parameter) ) {
add_property_long( return_value, "ifparameters", 1 );
array_init( ¶metres );
do {
object_init( ¶m );
if(par->attribute) add_property_string( ¶m, "attribute", par->attribute, 1 );
if(par->value) add_property_string( ¶m, "value", par->value, 1 );
add_next_index_object( ¶metres, param );
} while ( (par = par->next) );
} else {
object_init(¶metres);
add_property_long( return_value, "ifparameters", 0 );
}
add_assoc_object( return_value, "parameters", parametres );
}
/* }}} */
/* {{{ proto array imap_fetch_overview(int stream_id, int msg_no)
Read an overview of the information in the headers of the given message */
void php3_imap_fetch_overview(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *sequence;
int ind, ind_type;
pils *imap_le_struct;
pval myoverview;
char address[MAILTMPLEN];
int myargc=ARG_COUNT(ht);
if (myargc != 2 || getParameters(ht,myargc,&streamind,&sequence) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(sequence);
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
array_init(return_value);
if (mail_uid_sequence (imap_le_struct->imap_stream,(char *)sequence)) {
MESSAGECACHE *elt;
ENVELOPE *env;
unsigned long i;
for (i = 1; i <= imap_le_struct->imap_stream->nmsgs; i++)
if (((elt = mail_elt (imap_le_struct->imap_stream,i))->sequence) &&
(env = mail_fetch_structure (imap_le_struct->imap_stream,i,NIL,NIL))) {
object_init(&myoverview);
add_property_string(&myoverview,"subject",env->subject,1);
env->from->next=NULL;
rfc822_write_address(address,env->from);
add_property_string(&myoverview,"from",address,1);
add_property_string(&myoverview,"date",env->date,1);
add_property_string(&myoverview,"message_id",env->message_id,1);
add_property_string(&myoverview,"references",env->references,1);
add_property_long(&myoverview,"size",elt->rfc822_size);
add_property_long(&myoverview,"uid",mail_uid(imap_le_struct->imap_stream,i));
add_property_long(&myoverview,"msgno",i);
add_property_long(&myoverview,"recent",elt->recent);
add_property_long(&myoverview,"flagged",elt->flagged);
add_property_long(&myoverview,"answered",elt->answered);
add_property_long(&myoverview,"deleted",elt->deleted);
add_property_long(&myoverview,"seen",elt->seen);
add_property_long(&myoverview,"draft",elt->draft);
add_next_index_object(return_value,myoverview);
}
}
}
/* }}} */
/* {{{ proto string imap_mail_compose(array envelope, array body)
Create a MIME message based on given envelope and body sections */
void php3_imap_mail_compose(INTERNAL_FUNCTION_PARAMETERS)
{
pval *envelope, *body;
char *key;
pval *data,*pvalue;
ulong ind;
char *cookie = NIL;
ENVELOPE *env;
BODY *bod=NULL,*topbod=NULL;
PART *mypart=NULL,*toppart=NULL,*part;
PARAMETER *param;
char tmp[8*MAILTMPLEN],*mystring=NULL,*t,*tempstring;
int myargc=ARG_COUNT(ht);
if (myargc != 2 || getParameters(ht,myargc,&envelope,&body) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_array(envelope);
convert_to_array(body);
env=mail_newenvelope();
if(_php3_hash_find(envelope->value.ht,"remail",sizeof("remail"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
env->remail=cpystr(pvalue->value.str.val);
}
if(_php3_hash_find(envelope->value.ht,"return_path",sizeof("return_path"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
rfc822_parse_adrlist (&env->return_path,pvalue->value.str.val,"NO HOST");
}
if(_php3_hash_find(envelope->value.ht,"date",sizeof("date"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
env->date=cpystr(pvalue->value.str.val);
}
if(_php3_hash_find(envelope->value.ht,"from",sizeof("from"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
rfc822_parse_adrlist (&env->from,pvalue->value.str.val,"NO HOST");
}
if(_php3_hash_find(envelope->value.ht,"reply_to",sizeof("reply_to"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
rfc822_parse_adrlist (&env->reply_to,pvalue->value.str.val,"NO HOST");
}
if(_php3_hash_find(envelope->value.ht,"to",sizeof("to"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
rfc822_parse_adrlist (&env->to,pvalue->value.str.val,"NO HOST");
}
if(_php3_hash_find(envelope->value.ht,"cc",sizeof("cc"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
rfc822_parse_adrlist (&env->cc,pvalue->value.str.val,"NO HOST");
}
if(_php3_hash_find(envelope->value.ht,"bcc",sizeof("bcc"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
rfc822_parse_adrlist (&env->bcc,pvalue->value.str.val,"NO HOST");
}
if(_php3_hash_find(envelope->value.ht,"message_id",sizeof("message_id"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
env->message_id=cpystr(pvalue->value.str.val);
}
_php3_hash_internal_pointer_reset(body->value.ht);
_php3_hash_get_current_key(body->value.ht, &key, &ind);
_php3_hash_get_current_data(body->value.ht, (void **) &data);
if(data->type == IS_ARRAY)
{
bod=mail_newbody();
topbod=bod;
if(_php3_hash_find(data->value.ht,"type",sizeof("type"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->type=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"encoding",sizeof("encoding"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->encoding=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"subtype",sizeof("subtype"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->subtype=cpystr(pvalue->value.str.val);
}
if(_php3_hash_find(data->value.ht,"id",sizeof("id"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->id=cpystr(pvalue->value.str.val);
}
if(_php3_hash_find(data->value.ht,"contents.data",sizeof("contents.data"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->contents.text.data=(char *)fs_get(pvalue->value.str.len);
memcpy(bod->contents.text.data,pvalue->value.str.val,pvalue->value.str.len+1);
bod->contents.text.size=pvalue->value.str.len;
}
if(_php3_hash_find(data->value.ht,"lines",sizeof("lines"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->size.lines=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"bytes",sizeof("bytes"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->size.bytes=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"md5",sizeof("md5"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->md5=cpystr(pvalue->value.str.val);
}
}
_php3_hash_move_forward(body->value.ht);
while(_php3_hash_get_current_data(body->value.ht, (void **) &data)==SUCCESS)
{
_php3_hash_get_current_key(body->value.ht, &key, &ind);
if(data->type == IS_ARRAY)
{
if(!toppart)
{
bod->nested.part=mail_newbody_part();
mypart=bod->nested.part;
toppart=mypart;
bod=&mypart->body;
}
else
{
mypart->next=mail_newbody_part();
mypart=mypart->next;
bod=&mypart->body;
}
if(_php3_hash_find(data->value.ht,"type",sizeof("type"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->type=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"encoding",sizeof("encoding"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->encoding=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"subtype",sizeof("subtype"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->subtype=cpystr(pvalue->value.str.val);
}
if(_php3_hash_find(data->value.ht,"id",sizeof("id"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->id=cpystr(pvalue->value.str.val);
}
if(_php3_hash_find(data->value.ht,"contents.data",sizeof("contents.data"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->contents.text.data=(char *)fs_get(pvalue->value.str.len);
memcpy(bod->contents.text.data,pvalue->value.str.val,pvalue->value.str.len+1);
bod->contents.text.size=pvalue->value.str.len;
}
if(_php3_hash_find(data->value.ht,"lines",sizeof("lines"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->size.lines=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"bytes",sizeof("bytes"),(void **) &pvalue)== SUCCESS){
convert_to_long(pvalue);
bod->size.bytes=pvalue->value.lval;
}
if(_php3_hash_find(data->value.ht,"md5",sizeof("md5"),(void **) &pvalue)== SUCCESS){
convert_to_string(pvalue);
bod->md5=cpystr(pvalue->value.str.val);
}
_php3_hash_move_forward(body->value.ht);
}
}
rfc822_encode_body_7bit (env,topbod);
rfc822_header (tmp,env,topbod);
mystring=emalloc(strlen(tmp)+1);
strcpy(mystring,tmp);
bod=topbod;
switch (bod->type) {
case TYPEMULTIPART: /* multipart gets special handling */
part = bod->nested.part; /* first body part */
/* find cookie */
for (param = bod->parameter; param && !cookie; param = param->next)
if (!strcmp (param->attribute,"BOUNDARY")) cookie = param->value;
if (!cookie) cookie = "-"; /* yucky default */
do { /* for each part */
/* build cookie */
sprintf (t=tmp,"--%s\015\012",cookie);
/* append mini-header */
rfc822_write_body_header (&t,&part->body);
strcat (t,"\015\012"); /* write terminating blank line */
/* output cookie, mini-header, and contents */
tempstring=emalloc(strlen(mystring)+strlen(tmp)+1);
strcpy(tempstring,mystring);
efree(mystring);
mystring=tempstring;
strcat(mystring,tmp);
bod=&part->body;
tempstring=emalloc(strlen(bod->contents.text.data)+strlen(mystring)+1);
strcpy(tempstring,mystring);
efree(mystring);
mystring=tempstring;
strcat(mystring,bod->contents.text.data);
} while ((part = part->next));/* until done */
/* output trailing cookie */
sprintf(tmp,"--%s--",cookie);
tempstring=emalloc(strlen(tmp)+strlen(mystring)+1);
strcpy(tempstring,mystring);
efree(mystring);
mystring=tempstring;
strcat(mystring,tmp);
break;
default: /* all else is text now */
tempstring=emalloc(strlen(bod->contents.text.data)+strlen(mystring)+1);
strcpy(tempstring,mystring);
efree(mystring);
mystring=tempstring;
strcat(mystring,bod->contents.text.data);
break;
}
RETURN_STRINGL(mystring,strlen(mystring),1);
}
/* }}} */
/* {{{ proto string imap_alerts(void)
Returns an array of all IMAP alerts that have been generated */
/* Returns an array of all IMAP alerts that have been generated either
since the last page load, or since the last imap_alerts() call,
whichever came last. The alert stack is cleared after imap_alerts()
is called. */
/* Author: CJH */
void php3_imap_alerts(INTERNAL_FUNCTION_PARAMETERS)
{
STRINGLIST *cur=NIL;
int arg_count = ARG_COUNT(ht);
if (arg_count > 0) {
WRONG_PARAM_COUNT;
}
if (imap_alertstack == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur = imap_alertstack;
while (cur != NIL) {
add_next_index_string(return_value,cur->LTEXT,1);
cur = cur->next;
}
mail_free_stringlist(&imap_alertstack);
imap_alertstack = NIL;
}
/* }}} */
/* {{{ proto array imap_errors(void)
Returns an array of all IMAP errors generated */
/* Returns an array of all IMAP errors generated either since the last
page load, or since the last imap_errors() call, whichever came
last. The error stack is cleared after imap_errors() is called. */
/* Author: CJH */
void php3_imap_errors(INTERNAL_FUNCTION_PARAMETERS)
{
ERRORLIST *cur=NIL;
int arg_count = ARG_COUNT(ht);
if (arg_count > 0) {
WRONG_PARAM_COUNT;
}
if (imap_errorstack == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur = imap_errorstack;
while (cur != NIL) {
add_next_index_string(return_value,cur->LTEXT,1);
cur = cur->next;
}
mail_free_errorlist(&imap_errorstack);
imap_errorstack = NIL;
}
/* }}} */
/* {{{ proto string imap_last_error(void)
Returns the last error that was generated by an IMAP function. The error stack is NOT cleared after this call. */
/* Author: CJH */
void php3_imap_last_error(INTERNAL_FUNCTION_PARAMETERS)
{
ERRORLIST *cur=NIL;
int arg_count = ARG_COUNT(ht);
if (arg_count > 0) {
WRONG_PARAM_COUNT;
}
if (imap_errorstack == NIL) {
RETURN_FALSE;
}
cur = imap_errorstack;
while (cur != NIL) {
if (cur->next == NIL) {
RETURN_STRING(cur->LTEXT, 1);
}
cur = cur->next;
}
}
/* }}} */
/* {{{ proto int imap_mail(string to, string subject, string message [, string additional_headers [, string cc [, string bcc [, string rpath]]]])
Send an email message */
void php3_imap_mail(INTERNAL_FUNCTION_PARAMETERS)
{
pval *argv[6];
char *to=NULL, *message=NULL, *headers=NULL, *subject=NULL, *cc=NULL, *bcc=NULL, *rpath=NULL;
int argc;
TLS_VARS;
argc = ARG_COUNT(ht);
if (argc < 3 || argc > 7 || getParametersArray(ht, argc, argv) == FAILURE) {
WRONG_PARAM_COUNT;
}
/* To: */
convert_to_string(argv[0]);
if (argv[0]->value.str.val) {
to = argv[0]->value.str.val;
} else {
php3_error(E_WARNING, "No to field in mail command");
RETURN_FALSE;
}
/* Subject: */
convert_to_string(argv[1]);
if (argv[1]->value.str.val) {
subject = argv[1]->value.str.val;
} else {
php3_error(E_WARNING, "No subject field in mail command");
RETURN_FALSE;
}
/* message body */
convert_to_string(argv[2]);
if (argv[2]->value.str.val) {
message = argv[2]->value.str.val;
} else {
/* this is not really an error, so it is allowed. */
php3_error(E_WARNING, "No message string in mail command");
message = NULL;
}
/* other headers */
if (argc > 3) {
convert_to_string(argv[3]);
headers = argv[3]->value.str.val;
}
/* cc */
if (argc > 4) {
convert_to_string(argv[4]);
cc = argv[4]->value.str.val;
}
/* bcc */
if (argc > 5) {
convert_to_string(argv[5]);
bcc = argv[5]->value.str.val;
}
/* rpath */
if (argc > 6) {
convert_to_string(argv[6]);
rpath = argv[6]->value.str.val;
}
if (_php3_imap_mail(to, subject, message, headers, cc, bcc, rpath)){
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
int _php3_imap_mail(char *to, char *subject, char *message, char *headers, char *cc, char *bcc, char* rpath)
{
#if MSVC5
int tsm_err;
#else
FILE *sendmail;
int ret;
#endif
TLS_VARS;
#if MSVC5
if (imap_TSendMail(php3_ini.smtp,&tsm_err,headers,subject,to,message,cc,bcc,rpath) != SUCCESS){
php3_error(E_WARNING, "%s", GetSMErrorText(tsm_err));
return 0;
}
#else
if (!php3_ini.sendmail_path) {
return 0;
}
sendmail = popen(php3_ini.sendmail_path, "w");
if (sendmail) {
fprintf(sendmail, "To: %s\n", to);
if (cc && cc[0]) fprintf(sendmail, "Cc: %s\n", cc);
if (bcc && bcc[0]) fprintf(sendmail, "Bcc: %s\n", bcc);
fprintf(sendmail, "Subject: %s\n", subject);
if (headers != NULL) {
fprintf(sendmail, "%s\n", headers);
}
fprintf(sendmail, "\n%s\n", message);
ret = pclose(sendmail);
if (ret == -1) {
return 0;
} else {
return 1;
}
} else {
php3_error(E_WARNING, "Could not execute mail delivery program");
return 0;
}
#endif
return 1;
}
/* {{{ proto array imap_search(int stream_id, string criteria [, long flags])
Return a list of messages matching the criteria */
void php3_imap_search(INTERNAL_FUNCTION_PARAMETERS)
{
pval *streamind, *criteria, *search_flags;
int ind, ind_type, args;
pils *imap_le_struct;
long flags;
MESSAGELIST *cur;
args = ARG_COUNT(ht);
if (args < 2 || args > 3 || getParameters(ht, args, &streamind, &criteria, &search_flags) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(streamind);
convert_to_string(criteria);
if (args == 2) {
flags = SE_FREE;
} else {
convert_to_long(search_flags);
flags = search_flags->value.lval;
}
ind = streamind->value.lval;
imap_le_struct = (pils *)php3_list_find(ind, &ind_type);
if (!imap_le_struct || !IS_STREAM(ind_type)) {
php3_error(E_WARNING, "Unable to find stream pointer");
RETURN_FALSE;
}
imap_messages = NIL;
mail_search_full(imap_le_struct->imap_stream, NIL, mail_criteria(criteria->value.str.val), flags);
if (imap_messages == NIL) {
RETURN_FALSE;
}
array_init(return_value);
cur = imap_messages;
while (cur != NIL) {
add_next_index_long(return_value, cur->msgid);
cur = cur->next;
}
mail_free_messagelist(&imap_messages);
}
/* }}} */
/* Interfaces to C-client */
void mm_searched (MAILSTREAM *stream,unsigned long number)
{
MESSAGELIST *cur = NIL;
if (imap_messages == NIL) {
imap_messages = mail_newmessagelist();
imap_messages->msgid = number;
imap_messages->next = NIL;
} else {
cur = imap_messages;
while (cur->next != NIL) {
cur = cur->next;
}
cur->next = mail_newmessagelist();
cur = cur->next;
cur->msgid = number;
cur->next = NIL;
}
}
void mm_exists (MAILSTREAM *stream,unsigned long number)
{
}
void mm_expunged (MAILSTREAM *stream,unsigned long number)
{
}
void mm_flags (MAILSTREAM *stream,unsigned long number)
{
}
/* Author: CJH */
void mm_notify (MAILSTREAM *stream,char *string, long errflg)
{
STRINGLIST *cur = NIL;
if (strncmp(string, "[ALERT] ", 8) == 0) {
if (imap_alertstack == NIL) {
imap_alertstack = mail_newstringlist();
imap_alertstack->LSIZE = strlen(imap_alertstack->LTEXT = cpystr(string));
imap_alertstack->next = NIL;
} else {
cur = imap_alertstack;
while (cur->next != NIL ) {
cur = cur->next;
}
cur->next = mail_newstringlist ();
cur = cur->next;
cur->LSIZE = strlen(cur->LTEXT = cpystr(string));
cur->next = NIL;
}
}
}
void mm_list (MAILSTREAM *stream,DTYPE delimiter,char *mailbox,long attributes)
{
STRINGLIST *cur=NIL;
FOBJECTLIST *ocur=NIL;
if (folderlist_style == FLIST_OBJECT) {
/* build up a the new array of objects */
/* Author: CJH */
if (imap_folder_objects == NIL) {
imap_folder_objects = mail_newfolderobjectlist();
imap_folder_objects->LSIZE=strlen(imap_folder_objects->LTEXT=cpystr(mailbox));
imap_folder_objects->delimiter = delimiter;
imap_folder_objects->attributes = attributes;
imap_folder_objects->next = NIL;
} else {
ocur=imap_folder_objects;
while (ocur->next != NIL) {
ocur=ocur->next;
}
ocur->next=mail_newfolderobjectlist();
ocur=ocur->next;
ocur->LSIZE = strlen(ocur->LTEXT = cpystr(mailbox));
ocur->delimiter = delimiter;
ocur->attributes = attributes;
ocur->next = NIL;
}
} else {
/* build the old imap_folders variable to allow old imap_listmailbox() to work */
if (!(attributes & LATT_NOSELECT)) {
if (imap_folders == NIL) {
imap_folders=mail_newstringlist();
imap_folders->LSIZE=strlen(imap_folders->LTEXT=cpystr(mailbox));
imap_folders->next=NIL;
} else {
cur=imap_folders;
while (cur->next != NIL ) {
cur=cur->next;
}
cur->next=mail_newstringlist ();
cur=cur->next;
cur->LSIZE = strlen (cur->LTEXT = cpystr (mailbox));
cur->next = NIL;
}
}
}
}
void mm_lsub (MAILSTREAM *stream,DTYPE delimiter,char *mailbox,long attributes)
{
STRINGLIST *cur=NIL;
FOBJECTLIST *ocur=NIL;
if (folderlist_style == FLIST_OBJECT) {
/* build the array of objects */
/* Author: CJH */
if (imap_sfolder_objects == NIL) {
imap_sfolder_objects = mail_newfolderobjectlist();
imap_sfolder_objects->LSIZE=strlen(imap_sfolder_objects->LTEXT=cpystr(mailbox));
imap_sfolder_objects->delimiter = delimiter;
imap_sfolder_objects->attributes = attributes;
imap_sfolder_objects->next = NIL;
} else {
ocur=imap_sfolder_objects;
while (ocur->next != NIL) {
ocur=ocur->next;
}
ocur->next=mail_newfolderobjectlist();
ocur=ocur->next;
ocur->LSIZE=strlen(ocur->LTEXT = cpystr(mailbox));
ocur->delimiter = delimiter;
ocur->attributes = attributes;
ocur->next = NIL;
}
} else {
/* build the old simple array for imap_listsubscribed() */
if (imap_sfolders == NIL) {
imap_sfolders=mail_newstringlist();
imap_sfolders->LSIZE=strlen(imap_sfolders->LTEXT=cpystr(mailbox));
imap_sfolders->next=NIL;
} else {
cur=imap_sfolders;
while (cur->next != NIL ) {
cur=cur->next;
}
cur->next=mail_newstringlist ();
cur=cur->next;
cur->LSIZE = strlen (cur->LTEXT = cpystr (mailbox));
cur->next = NIL;
}
}
}
void mm_status (MAILSTREAM *stream,char *mailbox,MAILSTATUS *status)
{
status_flags=status->flags;
if(status_flags & SA_MESSAGES) status_messages=status->messages;
if(status_flags & SA_RECENT) status_recent=status->recent;
if(status_flags & SA_UNSEEN) status_unseen=status->unseen;
if(status_flags & SA_UIDNEXT) status_uidnext=status->uidnext;
if(status_flags & SA_UIDVALIDITY) status_uidvalidity=status->uidvalidity;
#ifdef SA_QUOTA
if(status_flags & SA_QUOTA) status_quota=status->quota;
#endif
#ifdef SA_QUOTA_ALL
if(status_flags & SA_QUOTA_ALL) status_quota_all=status->quota_all;
#endif
}
void mm_log (char *string,long errflg)
{
ERRORLIST *cur = NIL;
/* Author: CJH */
if (errflg != NIL) { /* CJH: maybe put these into a more comprehensive log for debugging purposes? */
if (imap_errorstack == NIL) {
imap_errorstack = mail_newerrorlist();
imap_errorstack->LSIZE = strlen(imap_errorstack->LTEXT = cpystr(string));
imap_errorstack->errflg = errflg;
imap_errorstack->next = NIL;
} else {
cur = imap_errorstack;
while (cur->next != NIL ) {
cur = cur->next;
}
cur->next = mail_newerrorlist ();
cur = cur->next;
cur->LSIZE = strlen(cur->LTEXT = cpystr(string));
cur->errflg = errflg;
cur->next = NIL;
}
}
}
void mm_dlog (char *string)
{
/* CJH: this is for debugging; it might be useful to allow setting
the stream to debug mode and capturing this somewhere - syslog?
php debugger? */
}
/* CJH: check the service name in order to allow different IMSP
usernames & passwords */
void mm_login (NETMBX *mb,char *user,char *pwd,long trial)
{
#if HAVE_IMSP
if (*mb->service && strcmp(mb->service, "imsp") == 0) {
if (*mb->user) {
strcpy(user, mb->user);
} else {
strcpy(user, imsp_user);
}
strcpy (pwd,imsp_password);
} else {
#endif
if (*mb->user) {
strcpy(user, mb->user);
} else {
strcpy(user, imap_user);
}
strcpy(pwd, imap_password);
#if HAVE_IMSP
}
#endif
}
void mm_critical (MAILSTREAM *stream)
{
}
void mm_nocritical (MAILSTREAM *stream)
{
}
long mm_diskerror (MAILSTREAM *stream,long errcode,long serious)
{
return 1;
}
void mm_fatal (char *string)
{
}
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|