d622d38a |
1 | <?php |
4b4abf93 |
2 | |
8d6a115b |
3 | /** |
4 | * mail_fetch/fetch.php |
5 | * |
8d6a115b |
6 | * Fetch code. |
7 | * |
47ccfad4 |
8 | * @copyright © 1999-2006 The SquirrelMail Project Team |
4b4abf93 |
9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
4f51df66 |
10 | * @version $Id$ |
ea5f4b8e |
11 | * @package plugins |
12 | * @subpackage mail_fetch |
8d6a115b |
13 | */ |
d3c89357 |
14 | |
202bcbcc |
15 | /** |
16 | * Include the SquirrelMail initialization file. |
17 | */ |
18 | require('../../include/init.php'); |
d3c89357 |
19 | |
202bcbcc |
20 | include_once(SM_PATH . 'functions/imap_general.php'); |
5c89bd63 |
21 | include_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php'); |
22 | include_once(SM_PATH . 'plugins/mail_fetch/functions.php' ); |
d3c89357 |
23 | |
91e0dccc |
24 | /* globals */ |
3c66c567 |
25 | sqgetGlobalVar('username', $username, SQ_SESSION); |
26 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
27 | sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); |
28 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
3c66c567 |
29 | /* end globals */ |
86bb8549 |
30 | |
5c89bd63 |
31 | function Mail_Fetch_Status($msg) { |
32 | echo html_tag( 'table', |
33 | html_tag( 'tr', |
34 | html_tag( 'td', htmlspecialchars( $msg ) , 'left' ) |
35 | ), |
36 | '', '', 'width="90%"' ); |
37 | flush(); |
38 | } |
39 | |
40 | function Mail_Fetch_Servers() { |
41 | global $data_dir, $username; |
42 | |
43 | $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number"); |
44 | if (!isset($mailfetch['server_number']) || ($mailfetch['server_number'] < 1)) { |
45 | $mailfetch['server_number'] = 0; |
d3c89357 |
46 | } |
5c89bd63 |
47 | $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher"); |
48 | for ($i = 0; $i < $mailfetch['server_number']; $i++) { |
49 | $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i"); |
50 | $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i"); |
51 | $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i"); |
52 | $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i"); |
53 | $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i"); |
54 | if($mailfetch['cypher'] == 'on') { |
55 | $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']); |
d622d38a |
56 | } |
5c89bd63 |
57 | if ($mailfetch[$i]['pass'] == '') { |
58 | sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST); |
91e0dccc |
59 | } |
5c89bd63 |
60 | $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i"); |
61 | $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i"); |
62 | $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i"); |
63 | $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i"); |
64 | if($mailfetch[$i]['alias'] == '') { |
65 | $mailfetch[$i]['alias'] == $mailfetch[$i]['server']; |
d622d38a |
66 | } |
9e56a6ad |
67 | } |
5c89bd63 |
68 | return $mailfetch; |
69 | } |
70 | |
71 | function Mail_Fetch_Select_Server($mailfetch) { |
72 | global $PHP_SELF; |
73 | |
74 | echo '<font size="-5"><br /></font>' . |
75 | '<form action="'.$PHP_SELF.'" method="post" target="_self">' . |
76 | html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) . |
77 | html_tag( 'tr' ) . |
78 | html_tag( 'td', _("Select Server:") . ' ', 'right' ) . |
79 | html_tag( 'td', '', 'left' ) . |
80 | '<select name="server_to_fetch" size="1">' . |
81 | '<option value="all" selected="selected">..' . _("All") . "...\n"; |
82 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
83 | echo "<option value=\"$i\">" . |
84 | htmlspecialchars($mailfetch[$i]['alias']) . |
85 | '</option>' . "\n"; |
d622d38a |
86 | } |
5c89bd63 |
87 | echo '</select>' . |
88 | '</td>' . |
89 | '</tr>'; |
90 | |
91 | //if password not set, ask for it |
92 | for ($i = 0;$i < $mailfetch['server_number'];$i++) { |
93 | if ($mailfetch[$i]['pass'] == '') { |
94 | echo html_tag( 'tr', |
95 | html_tag( 'td', _("Password for") . ' <b>' . |
96 | htmlspecialchars($mailfetch[$i]['alias']) . |
97 | '</b>: ', |
98 | 'right' ) . |
99 | html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' ) |
100 | ); |
101 | } |
d622d38a |
102 | } |
5c89bd63 |
103 | echo html_tag( 'tr', |
104 | html_tag( 'td', ' ' ) . |
105 | html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' ) |
106 | ) . |
107 | '</table></form>'; |
108 | } |
109 | |
110 | $mailfetch = Mail_Fetch_Servers(); |
111 | displayPageHeader($color, 'None'); |
112 | |
f265009a |
113 | echo '<br /><div style="text-align: center;">'; |
5c89bd63 |
114 | |
115 | echo html_tag( 'table', |
116 | html_tag( 'tr', |
117 | html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] ) |
118 | ) , |
119 | 'center', '', 'width="95%" cols="1"' ); |
120 | |
121 | |
122 | /* there are no servers defined yet... */ |
123 | if($mailfetch['server_number'] == 0) { |
124 | echo '<p>' . _("No POP3 servers configured yet.") . '</p>'; |
125 | displayInternalLink('plugins/mail_fetch/options.php', |
126 | _("Click here to go to the options page.") ); |
127 | echo '</body></html>'; |
128 | exit(); |
129 | } |
130 | |
131 | // get $server_to_fetch from globals, if not set display a choice to the user |
132 | if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) { |
133 | Mail_Fetch_Select_Server($mailfetch); |
134 | exit(); |
135 | } |
136 | |
137 | if ( $server_to_fetch == 'all' ) { |
138 | $i_start = 0; |
139 | $i_stop = $mailfetch['server_number']; |
140 | } else { |
141 | $i_start = $server_to_fetch; |
142 | $i_stop = $i_start+1; |
143 | } |
144 | |
145 | for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) { |
146 | $mailfetch_server = $mailfetch[$i_loop]['server']; |
147 | $mailfetch_port = $mailfetch[$i_loop]['port']; |
148 | $mailfetch_user = $mailfetch[$i_loop]['user']; |
149 | $mailfetch_pass = $mailfetch[$i_loop]['pass']; |
150 | $mailfetch_lmos = $mailfetch[$i_loop]['lmos']; |
151 | $mailfetch_login = $mailfetch[$i_loop]['login']; |
152 | $mailfetch_uidl = $mailfetch[$i_loop]['uidl']; |
153 | $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder']; |
154 | |
155 | $pop3 = new POP3($mailfetch_server, 60); |
156 | |
157 | echo '<br />' . |
4cf43843 |
158 | html_tag( 'table', |
159 | html_tag( 'tr', |
3c021d16 |
160 | html_tag( 'td', '<b>' . |
161 | sprintf(_("Fetching from %s"), |
162 | htmlspecialchars($mailfetch[$i_loop]['alias'])) . |
4cf43843 |
163 | '</b>', |
164 | 'center' ) , |
165 | '', $color[9] ) , |
166 | '', '', 'width="90%"' ); |
8b56b0d9 |
167 | |
5c89bd63 |
168 | flush(); |
8b56b0d9 |
169 | |
5c89bd63 |
170 | if (!$pop3->connect($mailfetch_server,$mailfetch_port)) { |
3c021d16 |
171 | Mail_Fetch_Status($pop3->ERROR ); |
5c89bd63 |
172 | continue; |
173 | } |
8b56b0d9 |
174 | |
5c89bd63 |
175 | Mail_Fetch_Status(_("Opening IMAP server")); |
176 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); |
8b56b0d9 |
177 | |
5c89bd63 |
178 | // check if destination folder is not set, is not subscribed and is not \noselect folder |
179 | if($mailfetch_subfolder == '' || |
180 | ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) { |
181 | $mailfetch_subfolder = 'INBOX'; |
182 | } |
5f438206 |
183 | |
5c89bd63 |
184 | Mail_Fetch_Status(_("Opening POP server")); |
185 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
186 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
187 | Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) ); |
188 | continue; |
189 | } |
8b56b0d9 |
190 | |
5c89bd63 |
191 | // register_shutdown_function($pop3->quit()); |
8b56b0d9 |
192 | |
5c89bd63 |
193 | $msglist = $pop3->uidl(); |
09e47788 |
194 | |
5c89bd63 |
195 | $i = 1; |
196 | for ($j = 1; $j < sizeof($msglist); $j++) { |
197 | if ($msglist[$j] == $mailfetch_uidl) { |
198 | $i = $j+1; |
199 | break; |
d622d38a |
200 | } |
5c89bd63 |
201 | } |
d622d38a |
202 | |
5c89bd63 |
203 | if ($Count < $i) { |
204 | Mail_Fetch_Status(_("Login OK: No new messages")); |
205 | $pop3->quit(); |
206 | continue; |
207 | } |
208 | if ($Count == 0) { |
209 | Mail_Fetch_Status(_("Login OK: Inbox EMPTY")); |
210 | $pop3->quit(); |
211 | continue; |
212 | } else { |
213 | $newmsgcount = $Count - $i + 1; |
214 | Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message", |
215 | "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount)); |
216 | } |
8b56b0d9 |
217 | |
5c89bd63 |
218 | Mail_Fetch_Status(_("Fetching UIDL...")); |
219 | // Faster to get them all at once |
220 | $mailfetch_uidl = $pop3->uidl(); |
8b56b0d9 |
221 | |
5c89bd63 |
222 | if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on') |
223 | Mail_Fetch_Status(_("Server does not support UIDL.")); |
d622d38a |
224 | |
5c89bd63 |
225 | if ($mailfetch_lmos == 'on') { |
226 | Mail_Fetch_Status(_("Leaving mail on server...")); |
227 | } else { |
228 | Mail_Fetch_Status(_("Deleting messages from server...")); |
229 | } |
8b56b0d9 |
230 | |
5c89bd63 |
231 | for (; $i <= $Count; $i++) { |
3c021d16 |
232 | Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i)); |
05b06d34 |
233 | |
5c89bd63 |
234 | if (!ini_get('safe_mode')) |
235 | set_time_limit(20); // 20 seconds per message max |
236 | $Message = ''; |
237 | $MessArray = $pop3->get($i); |
8b56b0d9 |
238 | |
5c89bd63 |
239 | while ( (!$MessArray) or (gettype($MessArray) != "array")) { |
3c021d16 |
240 | Mail_Fetch_Status($pop3->ERROR); |
5c89bd63 |
241 | // re-connect pop3 |
242 | Mail_Fetch_Status(_("Server error. Disconnect")); |
243 | $pop3->quit(); |
244 | Mail_Fetch_Status(_("Reconnect from dead connection")); |
245 | if (!$pop3->connect($mailfetch_server)) { |
3c021d16 |
246 | Mail_Fetch_Status($pop3->ERROR ); |
5c89bd63 |
247 | Mail_Fetch_Status(_("Saving UIDL")); |
248 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
249 | |
250 | continue; |
d622d38a |
251 | } |
5c89bd63 |
252 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
253 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
254 | Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR ); |
255 | Mail_Fetch_Status(_("Saving UIDL")); |
256 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
8b56b0d9 |
257 | |
5c89bd63 |
258 | continue; |
259 | } |
3c021d16 |
260 | Mail_Fetch_Status(sprintf(_("Refetching message %s."), $i)); |
5c89bd63 |
261 | $MessArray = $pop3->get($i); |
8b56b0d9 |
262 | |
5c89bd63 |
263 | } // end while |
264 | |
265 | while (list($lineNum, $line) = each ($MessArray)) { |
266 | $Message .= $line; |
267 | } |
268 | |
269 | fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n"); |
270 | $Line = fgets($imap_stream, 1024); |
271 | if (substr($Line, 0, 1) == '+') { |
272 | fputs($imap_stream, $Message); |
273 | fputs($imap_stream, "\r\n"); |
274 | sqimap_read_data($imap_stream, "A3$i", false, $response, $message); |
275 | $response=(implode('',$response)); |
276 | $message=(implode('',$message)); |
277 | if ($response != 'OK') { |
278 | Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) ); |
09e47788 |
279 | Mail_Fetch_Status(_("Closing POP")); |
280 | $pop3->quit(); |
281 | Mail_Fetch_Status(_("Logging out from IMAP")); |
282 | sqimap_logout($imap_stream); |
283 | |
09e47788 |
284 | Mail_Fetch_Status(_("Saving UIDL")); |
285 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
286 | exit; |
5c89bd63 |
287 | } else { |
288 | Mail_Fetch_Status(_("Message appended to mailbox")); |
d622d38a |
289 | } |
8b56b0d9 |
290 | |
5c89bd63 |
291 | if ($mailfetch_lmos != 'on') { |
292 | if( $pop3->delete($i) ) { |
293 | Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i)); |
294 | } else { |
295 | Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) ); |
296 | } |
297 | } |
298 | } else { |
299 | echo $Line; |
300 | Mail_Fetch_Status(_("Error Appending Message!")); |
301 | Mail_Fetch_Status(_("Closing POP")); |
302 | $pop3->quit(); |
303 | Mail_Fetch_Status(_("Logging out from IMAP")); |
304 | sqimap_logout($imap_stream); |
305 | |
306 | // not gurantee corect! |
d622d38a |
307 | Mail_Fetch_Status(_("Saving UIDL")); |
5c89bd63 |
308 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
309 | exit; |
d622d38a |
310 | } |
5c89bd63 |
311 | } |
8b56b0d9 |
312 | |
5c89bd63 |
313 | Mail_Fetch_Status(_("Closing POP")); |
314 | $pop3->quit(); |
315 | Mail_Fetch_Status(_("Logging out from IMAP")); |
316 | sqimap_logout($imap_stream); |
317 | if (is_array($mailfetch_uidl)) { |
318 | Mail_Fetch_Status(_("Saving UIDL")); |
319 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl)); |
320 | } |
d622d38a |
321 | |
5c89bd63 |
322 | Mail_Fetch_Status(_("Done")); |
323 | } |
d622d38a |
324 | ?> |
f265009a |
325 | </div> |
dcc1cc82 |
326 | </body> |
4f51df66 |
327 | </html> |