Removing trailing spaces from strings.
[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-2006 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 /** @ignore */
16 define('SM_PATH','../../');
17
18 require_once(SM_PATH . 'include/validate.php');
19 include_once(SM_PATH . 'functions/imap.php');
20 include_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php');
21 include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
22
23 /* globals */
24 sqgetGlobalVar('username', $username, SQ_SESSION);
25 sqgetGlobalVar('key', $key, SQ_COOKIE);
26 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
27 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
28 /* end globals */
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 function 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;
45 }
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']);
55 }
56 if ($mailfetch[$i]['pass'] == '') {
57 sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
58 }
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'];
65 }
66 }
67 return $mailfetch;
68 }
69
70 function 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";
85 }
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 }
101 }
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();
110 displayPageHeader($color, 'None');
111
112 echo '<br /><center>';
113
114 echo 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... */
122 if($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
131 if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
132 Mail_Fetch_Select_Server($mailfetch);
133 exit();
134 }
135
136 if ( $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
144 for ($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 />' .
157 html_tag( 'table',
158 html_tag( 'tr',
159 html_tag( 'td', '<b>' .
160 sprintf(_("Fetching from %s"),
161 htmlspecialchars($mailfetch[$i_loop]['alias'])) .
162 '</b>',
163 'center' ) ,
164 '', $color[9] ) ,
165 '', '', 'width="90%"' );
166
167 flush();
168
169 if (!$pop3->connect($mailfetch_server,$mailfetch_port)) {
170 Mail_Fetch_Status($pop3->ERROR );
171 continue;
172 }
173
174 Mail_Fetch_Status(_("Opening IMAP server"));
175 $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
176
177 // check if destination folder is not set, is not subscribed and is not \noselect folder
178 if($mailfetch_subfolder == '' ||
179 ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
180 $mailfetch_subfolder = 'INBOX';
181 }
182
183 Mail_Fetch_Status(_("Opening POP server"));
184 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
185 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
186 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->ERROR) );
187 continue;
188 }
189
190 // register_shutdown_function($pop3->quit());
191
192 $msglist = $pop3->uidl();
193
194 $i = 1;
195 for ($j = 1; $j < sizeof($msglist); $j++) {
196 if ($msglist[$j] == $mailfetch_uidl) {
197 $i = $j+1;
198 break;
199 }
200 }
201
202 if ($Count < $i) {
203 Mail_Fetch_Status(_("Login OK: No new messages"));
204 $pop3->quit();
205 continue;
206 }
207 if ($Count == 0) {
208 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
209 $pop3->quit();
210 continue;
211 } else {
212 $newmsgcount = $Count - $i + 1;
213 Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s message",
214 "Login OK: Inbox contains %s messages",$newmsgcount), $newmsgcount));
215 }
216
217 Mail_Fetch_Status(_("Fetching UIDL..."));
218 // Faster to get them all at once
219 $mailfetch_uidl = $pop3->uidl();
220
221 if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
222 Mail_Fetch_Status(_("Server does not support UIDL."));
223
224 if ($mailfetch_lmos == 'on') {
225 Mail_Fetch_Status(_("Leaving mail on server..."));
226 } else {
227 Mail_Fetch_Status(_("Deleting messages from server..."));
228 }
229
230 for (; $i <= $Count; $i++) {
231 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
232
233 if (!ini_get('safe_mode'))
234 set_time_limit(20); // 20 seconds per message max
235 $Message = '';
236 $MessArray = $pop3->get($i);
237
238 while ( (!$MessArray) or (gettype($MessArray) != "array")) {
239 Mail_Fetch_Status($pop3->ERROR);
240 // re-connect pop3
241 Mail_Fetch_Status(_("Server error. Disconnect"));
242 $pop3->quit();
243 Mail_Fetch_Status(_("Reconnect from dead connection"));
244 if (!$pop3->connect($mailfetch_server)) {
245 Mail_Fetch_Status($pop3->ERROR );
246 Mail_Fetch_Status(_("Saving UIDL"));
247 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
248
249 continue;
250 }
251 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
252 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
253 Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
254 Mail_Fetch_Status(_("Saving UIDL"));
255 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
256
257 continue;
258 }
259 Mail_Fetch_Status(sprintf(_("Refetching message %s."), $i));
260 $MessArray = $pop3->get($i);
261
262 } // end while
263
264 while (list($lineNum, $line) = each ($MessArray)) {
265 $Message .= $line;
266 }
267
268 fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
269 $Line = fgets($imap_stream, 1024);
270 if (substr($Line, 0, 1) == '+') {
271 fputs($imap_stream, $Message);
272 fputs($imap_stream, "\r\n");
273 sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
274 $response=(implode('',$response));
275 $message=(implode('',$message));
276 if ($response != 'OK') {
277 Mail_Fetch_Status(_("Error Appending Message!")." ".htmlspecialchars($message) );
278 Mail_Fetch_Status(_("Closing POP"));
279 $pop3->quit();
280 Mail_Fetch_Status(_("Logging out from IMAP"));
281 sqimap_logout($imap_stream);
282
283 Mail_Fetch_Status(_("Saving UIDL"));
284 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
285 exit;
286 } else {
287 Mail_Fetch_Status(_("Message appended to mailbox"));
288 }
289
290 if ($mailfetch_lmos != 'on') {
291 if( $pop3->delete($i) ) {
292 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
293 } else {
294 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->ERROR) );
295 }
296 }
297 } else {
298 echo $Line;
299 Mail_Fetch_Status(_("Error Appending Message!"));
300 Mail_Fetch_Status(_("Closing POP"));
301 $pop3->quit();
302 Mail_Fetch_Status(_("Logging out from IMAP"));
303 sqimap_logout($imap_stream);
304
305 // not gurantee corect!
306 Mail_Fetch_Status(_("Saving UIDL"));
307 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
308 exit;
309 }
310 }
311
312 Mail_Fetch_Status(_("Closing POP"));
313 $pop3->quit();
314 Mail_Fetch_Status(_("Logging out from IMAP"));
315 sqimap_logout($imap_stream);
316 if (is_array($mailfetch_uidl)) {
317 Mail_Fetch_Status(_("Saving UIDL"));
318 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
319 }
320
321 Mail_Fetch_Status(_("Done"));
322 }
323 ?>
324 </center>
325 </body>
326 </html>