8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @subpackage mail_fetch
16 * Include the SquirrelMail initialization file.
18 require('../../include/init.php');
20 include_once(SM_PATH
. 'functions/imap_general.php');
21 include_once(SM_PATH
. 'plugins/mail_fetch/functions.php' );
24 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
28 * @param string $msg message
30 function Mail_Fetch_Status($msg) {
31 echo html_tag( 'table',
33 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
35 '', '', 'width="90%"' );
42 function Mail_Fetch_Servers() {
43 global $data_dir, $username;
46 $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
47 if (!isset($mailfetch['server_number']) ||
($mailfetch['server_number'] < 1)) {
48 $mailfetch['server_number'] = 0;
50 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
52 for ($i = 0; $i < $mailfetch['server_number']; $i++
) {
53 $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
54 $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i");
55 $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i");
56 $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i");
57 $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i");
58 if($mailfetch['cypher'] == 'on') {
59 $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
61 if ($mailfetch[$i]['pass'] == '') {
62 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST
);
64 $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i");
65 $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i");
66 $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i");
67 $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
68 if($mailfetch[$i]['alias'] == '') {
69 $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
71 // Authentication type (added in 1.5.2)
72 $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER
);
73 // Connection type (added in 1.5.2)
74 $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN
);
80 * @param array $mailfetch
82 function Mail_Fetch_Select_Server($mailfetch) {
85 echo '<font size="-5"><br /></font>' .
86 '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
87 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
89 html_tag( 'td', _("Select Server:") . ' ', 'right' ) .
90 html_tag( 'td', '', 'left' ) .
91 '<select name="server_to_fetch" size="1">' .
92 '<option value="all" selected="selected">..' . _("All") . "...\n";
93 for ($i = 0;$i < $mailfetch['server_number'];$i++
) {
94 echo "<option value=\"$i\">" .
95 htmlspecialchars($mailfetch[$i]['alias']) .
102 //if password not set, ask for it
103 for ($i = 0;$i < $mailfetch['server_number'];$i++
) {
104 if ($mailfetch[$i]['pass'] == '') {
106 html_tag( 'td', _("Password for") . ' <b>' .
107 htmlspecialchars($mailfetch[$i]['alias']) .
108 '</b>: ',
110 html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
115 html_tag( 'td', ' ' ) .
116 html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
121 $mailfetch = Mail_Fetch_Servers();
122 displayPageHeader($color, 'None');
126 echo html_tag( 'table',
128 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
130 'center', '', 'width="95%" cols="1"' );
133 /* there are no servers defined yet... */
134 if($mailfetch['server_number'] == 0) {
135 echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
136 displayInternalLink('plugins/mail_fetch/options.php',
137 _("Click here to go to the options page.") );
138 $oTemplate->display('footer.tpl');
142 // get $server_to_fetch from globals, if not set display a choice to the user
143 if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST
) ) {
144 Mail_Fetch_Select_Server($mailfetch);
145 $oTemplate->display('footer.tpl');
149 if ( $server_to_fetch == 'all' ) {
151 $i_stop = $mailfetch['server_number'];
153 $i_start = $server_to_fetch;
154 $i_stop = $i_start+
1;
157 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++
) {
158 $mailfetch_server = $mailfetch[$i_loop]['server'];
159 $mailfetch_port = $mailfetch[$i_loop]['port'];
160 $mailfetch_user = $mailfetch[$i_loop]['user'];
161 $mailfetch_pass = $mailfetch[$i_loop]['pass'];
162 $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
163 $mailfetch_login = $mailfetch[$i_loop]['login'];
164 $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
165 $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
166 $mailfetch_auth = $mailfetch[$i_loop]['auth'];
167 $mailfetch_type = $mailfetch[$i_loop]['type'];
172 html_tag( 'td', '<b>' .
173 sprintf(_("Fetching from %s"),
174 htmlspecialchars($mailfetch[$i_loop]['alias'])) .
178 '', '', 'width="90%"' );
182 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
183 'port' => $mailfetch_port,
184 'auth' => $mailfetch_auth,
185 'tls' => $mailfetch_type,
188 if (!empty($pop3->error
)) {
189 Mail_Fetch_Status($pop3->error
);
193 Mail_Fetch_Status(_("Opening IMAP server"));
194 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
196 // check if destination folder is not set, is not subscribed and is not \noselect folder
197 if($mailfetch_subfolder == '' ||
198 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
199 $mailfetch_subfolder = 'INBOX';
202 Mail_Fetch_Status(_("Opening POP server"));
204 /* log into pop server*/
205 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
206 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error
));
210 $aMsgStat = $pop3->command_stat();
211 if (is_bool($aMsgStat)) {
212 Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error
) );
216 $Count = $aMsgStat['count'];
221 // If we leave messages on server, try using UIDL
222 if ($mailfetch_lmos == 'on') {
223 Mail_Fetch_Status(_("Fetching UIDL..."));
224 $msglist = $pop3->command_uidl();
225 if (is_bool($msglist)) {
226 Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error
));
227 // User asked to leave messages on server, but we can't do that.
228 $pop3->command_quit();
230 // $mailfetch_lmos = 'off';
232 // calculate number of new messages
233 for ($j = 1; $j <= sizeof($msglist); $j++
) {
234 // do strict comparison ('1111.10' should not be equal to '1111.100')
235 if ($msglist[$j] === $mailfetch_uidl) {
242 // fetch list of messages with LIST
243 // we can use else control, but we can also set $mailfetch_lmos
244 // to off if server does not support UIDL.
245 if ($mailfetch_lmos != 'on') {
246 Mail_Fetch_Status(_("Fetching list of messages..."));
247 $msglist = $pop3->command_list();
252 Mail_Fetch_Status(_("Login OK: No new messages"));
253 $pop3->command_quit();
257 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
258 $pop3->command_quit();
261 $newmsgcount = $Count - $i +
1;
262 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
263 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
266 if ($mailfetch_lmos == 'on') {
267 Mail_Fetch_Status(_("Leaving mail on server..."));
269 Mail_Fetch_Status(_("Deleting messages from server..."));
272 for (; $i <= $Count; $i++
) {
273 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
275 if (!ini_get('safe_mode'))
276 set_time_limit(20); // 20 seconds per message max
278 $Message = $pop3->command_retr($i);
280 if (is_bool($Message)) {
281 Mail_Fetch_Status(htmlspecialchars($pop3->error
));
285 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
286 $Line = fgets($imap_stream, 1024);
287 if (substr($Line, 0, 1) == '+') {
288 fputs($imap_stream, $Message);
289 fputs($imap_stream, "\r\n");
290 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
291 $response=(implode('',$response));
292 $message=(implode('',$message));
293 if ($response != 'OK') {
294 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
295 Mail_Fetch_Status(_("Closing POP"));
296 $pop3->command_quit();
297 Mail_Fetch_Status(_("Logging out from IMAP"));
298 sqimap_logout($imap_stream);
300 if ($mailfetch_lmos == 'on') {
301 Mail_Fetch_Status(_("Saving UIDL"));
302 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
306 Mail_Fetch_Status(_("Message appended to mailbox"));
309 if ($mailfetch_lmos != 'on') {
310 if( $pop3->command_dele($i) ) {
311 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
313 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error
) );
318 Mail_Fetch_Status(_("Error Appending Message!"));
319 Mail_Fetch_Status(_("Closing POP"));
320 $pop3->command_quit();
321 Mail_Fetch_Status(_("Logging out from IMAP"));
322 sqimap_logout($imap_stream);
324 // not gurantee corect!
325 if ($mailfetch_lmos == 'on') {
326 Mail_Fetch_Status(_("Saving UIDL"));
327 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
333 Mail_Fetch_Status(_("Closing POP"));
334 $pop3->command_quit();
335 Mail_Fetch_Status(_("Logging out from IMAP"));
336 sqimap_logout($imap_stream);
337 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
338 Mail_Fetch_Status(_("Saving UIDL"));
339 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
342 Mail_Fetch_Status(_("Done"));
345 $oTemplate->display('footer.tpl');