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