64bfb0a8733d511c9bcc81c33dff65c0e4296127
[squirrelmail.git] / plugins / mail_fetch / fetch.php
1 <?php
2
3 /**
4 * mail_fetch/fetch.php
5 *
6 * Fetch code.
7 *
8 * @copyright 1999-2017 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/functions.php' );
22
23 // don't load this page if this plugin is not enabled
24 //
25 global $plugins;
26 if (!in_array('mail_fetch', $plugins)) exit;
27
28 /* globals */
29 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
30 global $imap_stream_options; // in case not defined in config
31 /* end globals */
32
33 /**
34 * @param string $msg message
35 */
36 function Mail_Fetch_Status($msg) {
37 echo html_tag( 'table',
38 html_tag( 'tr',
39 html_tag( 'td', sm_encode_html_special_chars( $msg ) , 'left' )
40 ),
41 '', '', 'width="90%"' );
42 flush();
43 }
44
45 /**
46 * @return array
47 */
48 function Mail_Fetch_Servers() {
49 global $data_dir, $username;
50
51 $mailfetch = array();
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;
55 }
56 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
57
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']);
66 }
67 if ($mailfetch[$i]['pass'] == '') {
68 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
69 }
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'];
76 }
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);
81 }
82 return $mailfetch;
83 }
84
85 /**
86 * @param array $mailfetch
87 */
88 function Mail_Fetch_Select_Server($mailfetch) {
89 global $PHP_SELF;
90
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"' ) .
94 html_tag( 'tr' ) .
95 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', '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']) .
102 '</option>' . "\n";
103 }
104 echo '</select>' .
105 '</td>' .
106 '</tr>';
107
108 //if password not set, ask for it
109 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
110 if ($mailfetch[$i]['pass'] == '') {
111 echo html_tag( 'tr',
112 html_tag( 'td', _("Password for") . ' <b>' .
113 sm_encode_html_special_chars($mailfetch[$i]['alias']) .
114 '</b>: &nbsp; &nbsp; ',
115 'right' ) .
116 html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
117 );
118 }
119 }
120 echo html_tag( 'tr',
121 html_tag( 'td', '&nbsp;' ) .
122 html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
123 ) .
124 '</table></form>';
125 }
126
127 $mailfetch = Mail_Fetch_Servers();
128 displayPageHeader($color);
129
130 echo '<br />';
131
132 echo html_tag( 'table',
133 html_tag( 'tr',
134 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
135 ) ,
136 'center', '', 'width="95%" cols="1"' );
137
138
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');
146 exit();
147 }
148
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');
153 exit();
154 }
155
156 if ( $server_to_fetch == 'all' ) {
157 $i_start = 0;
158 $i_stop = $mailfetch['server_number'];
159 } else {
160 $i_start = $server_to_fetch;
161 $i_stop = $i_start+1;
162 }
163
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'];
175
176 echo '<br />' .
177 html_tag( 'table',
178 html_tag( 'tr',
179 html_tag( 'td', '<b>' .
180 sprintf(_("Fetching from %s"),
181 sm_encode_html_special_chars($mailfetch[$i_loop]['alias'])) .
182 '</b>',
183 'center' ) ,
184 '', $color[9] ) ,
185 '', '', 'width="90%"' );
186
187 flush();
188
189 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
190 'port' => $mailfetch_port,
191 'auth' => $mailfetch_auth,
192 'tls' => $mailfetch_type,
193 'timeout' => 60));
194
195 if (!empty($pop3->error)) {
196 Mail_Fetch_Status($pop3->error);
197 continue;
198 }
199
200 Mail_Fetch_Status(_("Opening IMAP server"));
201 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10, $imap_stream_options);
202
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';
207 }
208
209 Mail_Fetch_Status(_("Opening POP server"));
210
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));
214 continue;
215 }
216
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) );
220 continue;
221 }
222
223 $Count = $aMsgStat['count'];
224
225 $i = 1;
226
227 if ($Count>0) {
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();
236 continue;
237 // $mailfetch_lmos = 'off';
238 } else {
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) {
243 $i = $j+1;
244 break;
245 }
246 }
247 }
248 }
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();
255 }
256 }
257
258 if ($Count < $i) {
259 Mail_Fetch_Status(_("Login OK: No new messages"));
260 $pop3->command_quit();
261 continue;
262 }
263 if ($Count == 0) {
264 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
265 $pop3->command_quit();
266 continue;
267 } else {
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));
271 }
272
273 if ($mailfetch_lmos == 'on') {
274 Mail_Fetch_Status(_("Leaving messages on server..."));
275 } else {
276 Mail_Fetch_Status(_("Deleting messages from server..."));
277 }
278
279 for (; $i <= $Count; $i++) {
280 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
281
282 if (!ini_get('safe_mode'))
283 set_time_limit(20); // 20 seconds per message max
284
285 $Message = $pop3->command_retr($i);
286
287 if (is_bool($Message)) {
288 Mail_Fetch_Status(sm_encode_html_special_chars($pop3->error));
289 continue;
290 }
291
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);
306
307 if ($mailfetch_lmos == 'on') {
308 Mail_Fetch_Status(_("Saving UIDL"));
309 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
310 }
311 exit;
312 } else {
313 Mail_Fetch_Status(_("Message appended to mailbox"));
314 }
315
316 if ($mailfetch_lmos != 'on') {
317 if( $pop3->command_dele($i) ) {
318 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
319 } else {
320 Mail_Fetch_Status(_("Delete failed:") . sm_encode_html_special_chars($pop3->error) );
321 }
322 }
323 } else {
324 echo $Line;
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);
330
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]);
335 }
336 exit;
337 }
338 }
339
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));
347 }
348
349 Mail_Fetch_Status(_("Done"));
350 }
351
352 $oTemplate->display('footer.tpl');