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