add fixme
[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-2007 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 /* globals */
24 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
25 /* end globals */
26
27 /**
28 * @param string $msg message
29 */
30 function 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
39 /**
40 * @return array
41 */
42 function Mail_Fetch_Servers() {
43 global $data_dir, $username;
44
45 $mailfetch = array();
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;
49 }
50 $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
51
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']);
60 }
61 if ($mailfetch[$i]['pass'] == '') {
62 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
63 }
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'];
70 }
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);
75 }
76 return $mailfetch;
77 }
78
79 /**
80 * @param array $mailfetch
81 */
82 function Mail_Fetch_Select_Server($mailfetch) {
83 global $PHP_SELF;
84
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"' ) .
88 html_tag( 'tr' ) .
89 html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', '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']) .
96 '</option>' . "\n";
97 }
98 echo '</select>' .
99 '</td>' .
100 '</tr>';
101
102 //if password not set, ask for it
103 for ($i = 0;$i < $mailfetch['server_number'];$i++) {
104 if ($mailfetch[$i]['pass'] == '') {
105 echo html_tag( 'tr',
106 html_tag( 'td', _("Password for") . ' <b>' .
107 htmlspecialchars($mailfetch[$i]['alias']) .
108 '</b>: &nbsp; &nbsp; ',
109 'right' ) .
110 html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
111 );
112 }
113 }
114 echo html_tag( 'tr',
115 html_tag( 'td', '&nbsp;' ) .
116 html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
117 ) .
118 '</table></form>';
119 }
120
121 $mailfetch = Mail_Fetch_Servers();
122 displayPageHeader($color);
123
124 echo '<br />';
125
126 echo html_tag( 'table',
127 html_tag( 'tr',
128 html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
129 ) ,
130 'center', '', 'width="95%" cols="1"' );
131
132
133 /* there are no servers defined yet... */
134 if($mailfetch['server_number'] == 0) {
135 //FIXME: do not echo directly to browser -- use templates only
136 echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
137 echo makeInternalLink('plugins/mail_fetch/options.php',
138 _("Click here to go to the options page.") );
139 $oTemplate->display('footer.tpl');
140 exit();
141 }
142
143 // get $server_to_fetch from globals, if not set display a choice to the user
144 if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
145 Mail_Fetch_Select_Server($mailfetch);
146 $oTemplate->display('footer.tpl');
147 exit();
148 }
149
150 if ( $server_to_fetch == 'all' ) {
151 $i_start = 0;
152 $i_stop = $mailfetch['server_number'];
153 } else {
154 $i_start = $server_to_fetch;
155 $i_stop = $i_start+1;
156 }
157
158 for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
159 $mailfetch_server = $mailfetch[$i_loop]['server'];
160 $mailfetch_port = $mailfetch[$i_loop]['port'];
161 $mailfetch_user = $mailfetch[$i_loop]['user'];
162 $mailfetch_pass = $mailfetch[$i_loop]['pass'];
163 $mailfetch_lmos = $mailfetch[$i_loop]['lmos'];
164 $mailfetch_login = $mailfetch[$i_loop]['login'];
165 $mailfetch_uidl = $mailfetch[$i_loop]['uidl'];
166 $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
167 $mailfetch_auth = $mailfetch[$i_loop]['auth'];
168 $mailfetch_type = $mailfetch[$i_loop]['type'];
169
170 echo '<br />' .
171 html_tag( 'table',
172 html_tag( 'tr',
173 html_tag( 'td', '<b>' .
174 sprintf(_("Fetching from %s"),
175 htmlspecialchars($mailfetch[$i_loop]['alias'])) .
176 '</b>',
177 'center' ) ,
178 '', $color[9] ) ,
179 '', '', 'width="90%"' );
180
181 flush();
182
183 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
184 'port' => $mailfetch_port,
185 'auth' => $mailfetch_auth,
186 'tls' => $mailfetch_type,
187 'timeout' => 60));
188
189 if (!empty($pop3->error)) {
190 Mail_Fetch_Status($pop3->error);
191 continue;
192 }
193
194 Mail_Fetch_Status(_("Opening IMAP server"));
195 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
196
197 // check if destination folder is not set, is not subscribed and is not \noselect folder
198 if($mailfetch_subfolder == '' ||
199 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
200 $mailfetch_subfolder = 'INBOX';
201 }
202
203 Mail_Fetch_Status(_("Opening POP server"));
204
205 /* log into pop server*/
206 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
207 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
208 continue;
209 }
210
211 $aMsgStat = $pop3->command_stat();
212 if (is_bool($aMsgStat)) {
213 Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
214 continue;
215 }
216
217 $Count = $aMsgStat['count'];
218
219 $i = 1;
220
221 if ($Count>0) {
222 // If we leave messages on server, try using UIDL
223 if ($mailfetch_lmos == 'on') {
224 Mail_Fetch_Status(_("Fetching UIDL..."));
225 $msglist = $pop3->command_uidl();
226 if (is_bool($msglist)) {
227 Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error));
228 // User asked to leave messages on server, but we can't do that.
229 $pop3->command_quit();
230 continue;
231 // $mailfetch_lmos = 'off';
232 } else {
233 // calculate number of new messages
234 for ($j = 1; $j <= sizeof($msglist); $j++) {
235 // do strict comparison ('1111.10' should not be equal to '1111.100')
236 if ($msglist[$j] === $mailfetch_uidl) {
237 $i = $j+1;
238 break;
239 }
240 }
241 }
242 }
243 // fetch list of messages with LIST
244 // we can use else control, but we can also set $mailfetch_lmos
245 // to off if server does not support UIDL.
246 if ($mailfetch_lmos != 'on') {
247 Mail_Fetch_Status(_("Fetching list of messages..."));
248 $msglist = $pop3->command_list();
249 }
250 }
251
252 if ($Count < $i) {
253 Mail_Fetch_Status(_("Login OK: No new messages"));
254 $pop3->command_quit();
255 continue;
256 }
257 if ($Count == 0) {
258 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
259 $pop3->command_quit();
260 continue;
261 } else {
262 $newmsgcount = $Count - $i + 1;
263 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
264 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
265 }
266
267 if ($mailfetch_lmos == 'on') {
268 Mail_Fetch_Status(_("Leaving mail on server..."));
269 } else {
270 Mail_Fetch_Status(_("Deleting messages from server..."));
271 }
272
273 for (; $i <= $Count; $i++) {
274 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
275
276 if (!ini_get('safe_mode'))
277 set_time_limit(20); // 20 seconds per message max
278
279 $Message = $pop3->command_retr($i);
280
281 if (is_bool($Message)) {
282 Mail_Fetch_Status(htmlspecialchars($pop3->error));
283 continue;
284 }
285
286 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
287 $Line = fgets($imap_stream, 1024);
288 if (substr($Line, 0, 1) == '+') {
289 fputs($imap_stream, $Message);
290 fputs($imap_stream, "\r\n");
291 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
292 $response=(implode('',$response));
293 $message=(implode('',$message));
294 if ($response != 'OK') {
295 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
296 Mail_Fetch_Status(_("Closing POP"));
297 $pop3->command_quit();
298 Mail_Fetch_Status(_("Logging out from IMAP"));
299 sqimap_logout($imap_stream);
300
301 if ($mailfetch_lmos == 'on') {
302 Mail_Fetch_Status(_("Saving UIDL"));
303 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
304 }
305 exit;
306 } else {
307 Mail_Fetch_Status(_("Message appended to mailbox"));
308 }
309
310 if ($mailfetch_lmos != 'on') {
311 if( $pop3->command_dele($i) ) {
312 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
313 } else {
314 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
315 }
316 }
317 } else {
318 echo $Line;
319 Mail_Fetch_Status(_("Error Appending Message!"));
320 Mail_Fetch_Status(_("Closing POP"));
321 $pop3->command_quit();
322 Mail_Fetch_Status(_("Logging out from IMAP"));
323 sqimap_logout($imap_stream);
324
325 // not gurantee corect!
326 if ($mailfetch_lmos == 'on') {
327 Mail_Fetch_Status(_("Saving UIDL"));
328 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
329 }
330 exit;
331 }
332 }
333
334 Mail_Fetch_Status(_("Closing POP"));
335 $pop3->command_quit();
336 Mail_Fetch_Status(_("Logging out from IMAP"));
337 sqimap_logout($imap_stream);
338 if ($mailfetch_lmos == 'on' && is_array($msglist)) {
339 Mail_Fetch_Status(_("Saving UIDL"));
340 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
341 }
342
343 Mail_Fetch_Status(_("Done"));
344 }
345
346 $oTemplate->display('footer.tpl');
347