Official Mail_Fetch version
[squirrelmail.git] / plugins / mail_fetch / fetch.php
1 <?php
2 /*
3 * Mail Fetch
4 *
5 */
6
7 chdir('..');
8 require_once('../src/validate.php');
9 require_once('../functions/page_header.php');
10 require_once('../functions/imap.php');
11 require_once('../src/load_prefs.php');
12 require_once('../plugins/mail_fetch/class.POP3.php');
13 require_once('../functions/i18n.php');
14 require_once( '../plugins/mail_fetch/functions.php' );
15
16
17 function Mail_Fetch_Status($msg) {
18 echo '<table width=90%><tr><td>' .
19 htmlspecialchars( $msg ) .
20 '</td></tr></table>';
21 flush();
22 }
23 displayPageHeader($color, 'None');
24
25 $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
26 if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
27 $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
28 if ($mailfetch_server_number<1) $mailfetch_server_number=0;
29 for ($i=0;$i<$mailfetch_server_number;$i++) {
30 $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
31 $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
32 $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
33 $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
34 $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
35 $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
36 $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
37 $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
38 if( $mailfetch_cypher == 'on' ) {
39 $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
40 }
41 }
42
43 echo '<br><center>';
44
45 echo '<TABLE WIDTH=95% COLS=1 ALIGN=CENTER>' .
46 "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER><b>" . _("Remote POP server Fetching Mail") . '</b></TD></TR>' .
47 '</TABLE>';
48
49 if (!isset( $server_to_fetch ) ) {
50
51 echo '<font size=-5><BR></font>' .
52 "<form action=\"$PHP_SELF\" METHOD=POST TARGET=_self>" .
53 '<TABLE WIDTH=70% COLS=2 ALIGN=CENTER>' .
54 '<TR>' .
55 '<TD ALIGN=RIGHT>' . _("Select Server:") . ' &nbsp; &nbsp; </TD>' .
56 '<TD><SELECT NAME=server_to_fetch SIZE=1>' .
57 '<OPTION VALUE="all" SELECTED>..' . _("All") . "...\n";
58 for ($i=0;$i<$mailfetch_server_number;$i++) {
59 echo "<OPTION VALUE=\"$i\">" .
60 (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]);
61 }
62 echo '</SELECT>' .
63 '</TD>' .
64 '</TR>';
65
66 //if password not set, ask for it
67 for ($i=0;$i<$mailfetch_server_number;$i++) {
68 if ($mailfetch_pass_[$i]=="") {
69 echo "<tr>" .
70 '<TD ALIGN=RIGHT>' . _("Password for") . ' <B>' . (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) . '</B>: &nbsp; &nbsp; </TD>' .
71 "<TD><INPUT TYPE=PASSWORD NAME=pass_$i></TD>" .
72 "</TR>";
73 }
74 }
75 echo '<TR>' .
76 '<TD>&nbsp;</TD>' .
77 '<TD><input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '"></TD>'.
78 '</TR>' .
79 '</TABLE></form>';
80 exit();
81 }
82
83 if ( $server_to_fetch == 'all' ) {
84 $i_start = 0;
85 $i_stop = $mailfetch_server_number;
86 } else {
87 $i_start = $server_to_fetch;
88 $i_stop = $i_start+1;
89 }
90
91 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
92 $mailfetch_server=$mailfetch_server_[$i_loop];
93 $mailfetch_user=$mailfetch_user_[$i_loop];
94 if ($mailfetch_pass_[$i_loop]=="") {
95 $tmp="pass_$i_loop";
96 $mailfetch_pass=$$tmp;
97 } else {
98 $mailfetch_pass=$mailfetch_pass_[$i_loop];
99 }
100 $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
101 $mailfetch_login=$mailfetch_login_[$i_loop];
102 $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
103 $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
104
105
106 $pop3 = new POP3($mailfetch_server, 60);
107
108 echo "<br><table width=90%><tr bgcolor=\"$color[9]\"><td><b>" .
109 _("Fetching from ") .
110 (($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop]) .
111 "</b></td></tr></table>";
112
113 flush();
114
115 if (!$pop3->connect($mailfetch_server)) {
116 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
117 continue;
118 }
119
120 Mail_Fetch_Status(_("Opening IMAP server"));
121 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
122
123 Mail_Fetch_Status(_("Opening POP server"));
124 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
125 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
126 Mail_Fetch_Status(_("Login Failed:") . $pop3->ERROR );
127 continue;
128 }
129
130 // register_shutdown_function($pop3->quit());
131
132 $msglist = $pop3->uidl();
133
134 $i = 1;
135 for ($j = 1; $j < sizeof($msglist); $j++) {
136 if ($msglist["$j"] == $mailfetch_uidl) {
137 $i = $j+1;
138 break;
139 }
140 }
141
142 if ($Count < $i) {
143 Mail_Fetch_Status(_("Login OK: No new messages"));
144 $pop3->quit();
145 continue;
146 }
147 if ($Count == 0) {
148 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
149 $pop3->quit();
150 continue;
151 } else {
152 $newmsgcount = $Count - $i + 1;
153 Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
154 }
155
156 Mail_Fetch_Status(_("Fetching UIDL..."));
157 // Faster to get them all at once
158 $mailfetch_uidl = $pop3->uidl();
159
160 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
161 Mail_Fetch_Status(_("Server does not support UIDL."));
162
163 if ($mailfetch_lmos == 'on') {
164 Mail_Fetch_Status(_("Leaving Mail on Server..."));
165 } else {
166 Mail_Fetch_Status(_("Deleting messages from server..."));
167 }
168
169 for (; $i <= $Count; $i++) {
170 Mail_Fetch_Status(_("Fetching message ") . "$i" );
171 set_time_limit(20); // 20 seconds per message max
172 $Message = "";
173 $MessArray = $pop3->get($i);
174
175 if ( (!$MessArray) or (gettype($MessArray) != "array")) {
176 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
177 continue 2;
178 }
179
180 while (list($lineNum, $line) = each ($MessArray)) {
181 $Message .= $line;
182 }
183
184 if ($mailfetch_subfolder=="") {
185 fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
186 } else {
187 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
188 }
189 $Line = fgets($imap_stream, 1024);
190 if (substr($Line, 0, 1) == '+') {
191 fputs($imap_stream, $Message);
192 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
193 Mail_Fetch_Status(_("Message appended to mailbox"));
194
195 if ($mailfetch_lmos != 'on') {
196 if( $pop3->delete($i) ) {
197 Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
198 } else {
199 Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
200 }
201 }
202 } else {
203 echo "$Line";
204 Mail_Fetch_Status(_("Error Appending Message!"));
205 }
206 }
207
208 Mail_Fetch_Status(_("Closing POP"));
209 $pop3->quit();
210 Mail_Fetch_Status(_("Logging out from IMAP"));
211 sqimap_logout($imap_stream);
212 if (is_array($mailfetch_uidl)) {
213 Mail_Fetch_Status(_("Saving UIDL"));
214 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
215 }
216
217 Mail_Fetch_Status(_("Done"));
218
219 }
220
221 ?>
222 </center>
223 </body>
224 </html>