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