Add class attribute to template
[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 /**
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, 'None');
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 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');
139 exit();
140 }
141
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');
146 exit();
147 }
148
149 if ( $server_to_fetch == 'all' ) {
150 $i_start = 0;
151 $i_stop = $mailfetch['server_number'];
152 } else {
153 $i_start = $server_to_fetch;
154 $i_stop = $i_start+1;
155 }
156
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'];
168
169 echo '<br />' .
170 html_tag( 'table',
171 html_tag( 'tr',
172 html_tag( 'td', '<b>' .
173 sprintf(_("Fetching from %s"),
174 htmlspecialchars($mailfetch[$i_loop]['alias'])) .
175 '</b>',
176 'center' ) ,
177 '', $color[9] ) ,
178 '', '', 'width="90%"' );
179
180 flush();
181
182 $pop3 = new mail_fetch(array('host' => $mailfetch_server,
183 'port' => $mailfetch_port,
184 'auth' => $mailfetch_auth,
185 'tls' => $mailfetch_type,
186 'timeout' => 60));
187
188 if (!empty($pop3->error)) {
189 Mail_Fetch_Status($pop3->error);
190 continue;
191 }
192
193 Mail_Fetch_Status(_("Opening IMAP server"));
194 $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
195
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';
200 }
201
202 Mail_Fetch_Status(_("Opening POP server"));
203
204 /* log into pop server*/
205 if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
206 Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
207 continue;
208 }
209
210 $aMsgStat = $pop3->command_stat();
211 if (is_bool($aMsgStat)) {
212 Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
213 continue;
214 }
215
216 $Count = $aMsgStat['count'];
217
218 $i = 1;
219
220 if ($Count>0) {
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();
229 continue;
230 // $mailfetch_lmos = 'off';
231 } else {
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) {
236 $i = $j+1;
237 break;
238 }
239 }
240 }
241 }
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();
248 }
249 }
250
251 if ($Count < $i) {
252 Mail_Fetch_Status(_("Login OK: No new messages"));
253 $pop3->command_quit();
254 continue;
255 }
256 if ($Count == 0) {
257 Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
258 $pop3->command_quit();
259 continue;
260 } else {
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));
264 }
265
266 if ($mailfetch_lmos == 'on') {
267 Mail_Fetch_Status(_("Leaving mail on server..."));
268 } else {
269 Mail_Fetch_Status(_("Deleting messages from server..."));
270 }
271
272 for (; $i <= $Count; $i++) {
273 Mail_Fetch_Status(sprintf(_("Fetching message %s."), $i));
274
275 if (!ini_get('safe_mode'))
276 set_time_limit(20); // 20 seconds per message max
277
278 $Message = $pop3->command_retr($i);
279
280 if (is_bool($Message)) {
281 Mail_Fetch_Status(htmlspecialchars($pop3->error));
282 continue;
283 }
284
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);
299
300 if ($mailfetch_lmos == 'on') {
301 Mail_Fetch_Status(_("Saving UIDL"));
302 setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
303 }
304 exit;
305 } else {
306 Mail_Fetch_Status(_("Message appended to mailbox"));
307 }
308
309 if ($mailfetch_lmos != 'on') {
310 if( $pop3->command_dele($i) ) {
311 Mail_Fetch_Status(sprintf(_("Message %d deleted from remote server!"), $i));
312 } else {
313 Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
314 }
315 }
316 } else {
317 echo $Line;
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);
323
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]);
328 }
329 exit;
330 }
331 }
332
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));
340 }
341
342 Mail_Fetch_Status(_("Done"));
343 }
344
345 $oTemplate->display('footer.tpl');
346