dd2db317690333a99a7746890ee928cf0d04c85d
8 * @copyright 1999-2014 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' );
23 // don't load this page if this plugin is not enabled
26 if (!in_array('mail_fetch', $plugins)) exit;
29 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
30 global $imap_stream_options; // in case not defined in config
34 * @param string $msg message
36 function Mail_Fetch_Status($msg) {
37 echo html_tag( 'table',
39 html_tag( 'td', sm_encode_html_special_chars( $msg ) , 'left' )
41 '', '', 'width="90%"' );
48 function Mail_Fetch_Servers() {
49 global $data_dir, $username;
52 $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
53 if (!isset($mailfetch['server_number']) ||
($mailfetch['server_number'] < 1)) {
54 $mailfetch['server_number'] = 0;
56 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
58 for ($i = 0; $i < $mailfetch['server_number']; $i++
) {
59 $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
60 $mailfetch[$i]['port'] = getPref($data_dir, $username, "mailfetch_port_$i");
61 $mailfetch[$i]['alias'] = getPref($data_dir, $username, "mailfetch_alias_$i");
62 $mailfetch[$i]['user'] = getPref($data_dir, $username, "mailfetch_user_$i");
63 $mailfetch[$i]['pass'] = getPref($data_dir, $username, "mailfetch_pass_$i");
64 if($mailfetch['cypher'] == 'on') {
65 $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
67 if ($mailfetch[$i]['pass'] == '') {
68 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST
);
70 $mailfetch[$i]['lmos'] = getPref($data_dir, $username, "mailfetch_lmos_$i");
71 $mailfetch[$i]['login'] = getPref($data_dir, $username, "mailfetch_login_$i");
72 $mailfetch[$i]['uidl'] = getPref($data_dir, $username, "mailfetch_uidl_$i");
73 $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
74 if($mailfetch[$i]['alias'] == '') {
75 $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
77 // Authentication type (added in 1.5.2)
78 $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER
);
79 // Connection type (added in 1.5.2)
80 $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN
);
86 * @param array $mailfetch
88 function Mail_Fetch_Select_Server($mailfetch) {
91 echo '<font size="-5"><br /></font>' .
92 '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
93 html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
95 html_tag( 'td', _("Select Server:") . ' ', 'right' ) .
96 html_tag( 'td', '', 'left' ) .
97 '<select name="server_to_fetch" size="1">' .
98 '<option value="all" selected="selected">..' . _("All") . "...\n";
99 for ($i = 0;$i < $mailfetch['server_number'];$i++
) {
100 echo "<option value=\"$i\">" .
101 sm_encode_html_special_chars($mailfetch[$i]['alias']) .
108 //if password not set, ask for it
109 for ($i = 0;$i < $mailfetch['server_number'];$i++
) {
110 if ($mailfetch[$i]['pass'] == '') {
112 html_tag( 'td', _("Password for") . ' <b>' .
113 sm_encode_html_special_chars($mailfetch[$i]['alias']) .
114 '</b>: ',
116 html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
121 html_tag( 'td', ' ' ) .
122 html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
127 $mailfetch = Mail_Fetch_Servers();
128 displayPageHeader($color);
132 echo html_tag( 'table',
134 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
136 'center', '', 'width="95%" cols="1"' );
139 /* there are no servers defined yet... */
140 if($mailfetch['server_number'] == 0) {
141 //FIXME: do not echo directly to browser -- use templates only
142 echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
143 echo makeInternalLink('plugins/mail_fetch/options.php',
144 _("Click here to go to the options page.") );
145 $oTemplate->display('footer.tpl');
149 // get $server_to_fetch from globals, if not set display a choice to the user
150 if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST
) ) {
151 Mail_Fetch_Select_Server($mailfetch);
152 $oTemplate->display('footer.tpl');
156 if ( $server_to_fetch == 'all' ) {
158 $i_stop = $mailfetch['server_number'];
160 $i_start = $server_to_fetch;
161 $i_stop = $i_start+
1;
164 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++
) {
165 $mailfetch_server = $mailfetch[$i_loop]['server'];
166 $mailfetch_port = $mailfetch[$i_loop]['port'];
167 $mailfetch_user = $mailfetch[$i_loop]['user'];
168 $mailfetch_pass = $mailfetch[$i_loop]['pass'];
169 $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
170 $mailfetch_login = $mailfetch[$i_loop]['login'];
171 $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
172 $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
173 $mailfetch_auth = $mailfetch[$i_loop]['auth'];
174 $mailfetch_type = $mailfetch[$i_loop]['type'];
179 html_tag( 'td', '<b>' .
180 sprintf(_("Fetching from %s"),
181 sm_encode_html_special_chars($mailfetch[$i_loop]['alias'])) .
185 '', '', 'width="90%"' );
189 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
190 'port' => $mailfetch_port,
191 'auth' => $mailfetch_auth,
192 'tls' => $mailfetch_type,
195 if (!empty($pop3->error
)) {
196 Mail_Fetch_Status($pop3->error
);
200 Mail_Fetch_Status(_("Opening IMAP server"));
201 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10, $imap_stream_options);
203 // check if destination folder is not set, is not subscribed and is not \noselect folder
204 if($mailfetch_subfolder == '' ||
205 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
206 $mailfetch_subfolder = 'INBOX';
209 Mail_Fetch_Status(_("Opening POP server"));
211 /* log into pop server*/
212 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
213 Mail_Fetch_Status(_("Login Failed:") . ' ' . sm_encode_html_special_chars($pop3->error
));
217 $aMsgStat = $pop3->command_stat();
218 if (is_bool($aMsgStat)) {
219 Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . sm_encode_html_special_chars($pop3->error
) );
223 $Count = $aMsgStat['count'];
228 // If we leave messages on server, try using UIDL
229 if ($mailfetch_lmos == 'on') {
230 Mail_Fetch_Status(_("Fetching UIDL..."));
231 $msglist = $pop3->command_uidl();
232 if (is_bool($msglist)) {
233 Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.sm_encode_html_special_chars($pop3->error
));
234 // User asked to leave messages on server, but we can't do that.
235 $pop3->command_quit();
237 // $mailfetch_lmos = 'off';
239 // calculate number of new messages
240 for ($j = 1; $j <= sizeof($msglist); $j++
) {
241 // do strict comparison ('1111.10' should not be equal to '1111.100')
242 if ($msglist[$j] === $mailfetch_uidl) {
249 // fetch list of messages with LIST
250 // we can use else control, but we can also set $mailfetch_lmos
251 // to off if server does not support UIDL.
252 if ($mailfetch_lmos != 'on') {
253 Mail_Fetch_Status(_("Fetching list of messages..."));
254 $msglist = $pop3->command_list();
259 Mail_Fetch_Status(_("Login OK: No new messages"));
260 $pop3->command_quit();
264 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
265 $pop3->command_quit();
268 $newmsgcount = $Count - $i +
1;
269 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
270 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
273 if ($mailfetch_lmos == 'on') {
274 Mail_Fetch_Status(_("Leaving messages on server..."));
276 Mail_Fetch_Status(_("Deleting messages from server..."));
279 for (; $i <= $Count; $i++
) {
280 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
282 if (!ini_get('safe_mode'))
283 set_time_limit(20); // 20 seconds per message max
285 $Message = $pop3->command_retr($i);
287 if (is_bool($Message)) {
288 Mail_Fetch_Status(sm_encode_html_special_chars($pop3->error
));
292 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
293 $Line = fgets($imap_stream, 1024);
294 if (substr($Line, 0, 1) == '+') {
295 fputs($imap_stream, $Message);
296 fputs($imap_stream, "\r\n");
297 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
298 $response=(implode('',$response));
299 $message=(implode('',$message));
300 if ($response != 'OK') {
301 Mail_Fetch_Status(_("Error Appending Message!")." ".sm_encode_html_special_chars($message) );
302 Mail_Fetch_Status(_("Closing POP"));
303 $pop3->command_quit();
304 Mail_Fetch_Status(_("Logging out from IMAP"));
305 sqimap_logout($imap_stream);
307 if ($mailfetch_lmos == 'on') {
308 Mail_Fetch_Status(_("Saving UIDL"));
309 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
313 Mail_Fetch_Status(_("Message appended to mailbox"));
316 if ($mailfetch_lmos != 'on') {
317 if( $pop3->command_dele($i) ) {
318 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
320 Mail_Fetch_Status(_("Delete failed:") . sm_encode_html_special_chars($pop3->error
) );
325 Mail_Fetch_Status(_("Error Appending Message!"));
326 Mail_Fetch_Status(_("Closing POP"));
327 $pop3->command_quit();
328 Mail_Fetch_Status(_("Logging out from IMAP"));
329 sqimap_logout($imap_stream);
331 // not gurantee corect!
332 if ($mailfetch_lmos == 'on') {
333 Mail_Fetch_Status(_("Saving UIDL"));
334 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
340 Mail_Fetch_Status(_("Closing POP"));
341 $pop3->command_quit();
342 Mail_Fetch_Status(_("Logging out from IMAP"));
343 sqimap_logout($imap_stream);
344 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
345 Mail_Fetch_Status(_("Saving UIDL"));
346 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
349 Mail_Fetch_Status(_("Done"));
352 $oTemplate->display('footer.tpl');