| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * mail_fetch/fetch.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * Fetch code. |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | define('SM_PATH','../../'); |
| 15 | |
| 16 | require_once(SM_PATH . 'include/validate.php'); |
| 17 | require_once(SM_PATH . 'functions/page_header.php'); |
| 18 | require_once(SM_PATH . 'functions/imap.php'); |
| 19 | require_once(SM_PATH . 'include/load_prefs.php'); |
| 20 | require_once(SM_PATH . 'plugins/mail_fetch/class.POP3.php'); |
| 21 | require_once(SM_PATH . 'plugins/mail_fetch/functions.php' ); |
| 22 | require_once(SM_PATH . 'functions/html.php' ); |
| 23 | |
| 24 | /* globals */ |
| 25 | $username = $_SESSION['username']; |
| 26 | $key = $_COOKIE['key']; |
| 27 | $onetimepad = $_SESSION['onetimepad']; |
| 28 | $delimter = $_SESSION['delimiter']; |
| 29 | |
| 30 | /* This form, like the advanced identities form |
| 31 | uses dynamic post variable names so we need |
| 32 | to extract the whole $_POST array to make |
| 33 | things work |
| 34 | */ |
| 35 | |
| 36 | extract($_POST); |
| 37 | /* end globals */ |
| 38 | |
| 39 | function Mail_Fetch_Status($msg) { |
| 40 | echo html_tag( 'table', |
| 41 | html_tag( 'tr', |
| 42 | html_tag( 'td', htmlspecialchars( $msg ) , 'left' ) |
| 43 | ), |
| 44 | '', '', 'width="90%"' ); |
| 45 | flush(); |
| 46 | } |
| 47 | |
| 48 | displayPageHeader($color, 'None'); |
| 49 | |
| 50 | $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number"); |
| 51 | if (!isset($mailfetch_server_number)) $mailfetch_server_number=0; |
| 52 | $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher"); |
| 53 | if ($mailfetch_server_number<1) $mailfetch_server_number=0; |
| 54 | for ($i=0;$i<$mailfetch_server_number;$i++) { |
| 55 | $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i"); |
| 56 | $mailfetch_port_[$i] = getPref($data_dir, $username, "mailfetch_port_$i"); |
| 57 | $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i"); |
| 58 | $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i"); |
| 59 | $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i"); |
| 60 | $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i"); |
| 61 | $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i"); |
| 62 | $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i"); |
| 63 | $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i"); |
| 64 | if( $mailfetch_cypher == 'on' ) { |
| 65 | $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | echo '<br><center>'; |
| 70 | |
| 71 | echo html_tag( 'table', |
| 72 | html_tag( 'tr', |
| 73 | html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] ) |
| 74 | ) , |
| 75 | 'center', '', 'width="95%" cols="1"' ); |
| 76 | |
| 77 | if (!isset( $server_to_fetch ) ) { |
| 78 | |
| 79 | echo '<font size=-5><br></font>' . |
| 80 | "<form action=\"$PHP_SELF\" method=\"post\" target=\"_self\">" . |
| 81 | html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) . |
| 82 | html_tag( 'tr' ) . |
| 83 | html_tag( 'td', _("Select Server:") . ' ', 'right' ) . |
| 84 | html_tag( 'td', '', 'left' ) . |
| 85 | '<select name="server_to_fetch" size="1">' . |
| 86 | '<option value="all" selected>..' . _("All") . "...\n"; |
| 87 | for ($i=0;$i<$mailfetch_server_number;$i++) { |
| 88 | echo "<option value=\"$i\">" . |
| 89 | htmlspecialchars((($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i])) . |
| 90 | '</option>' . "\n"; |
| 91 | } |
| 92 | echo '</select>' . |
| 93 | '</td>' . |
| 94 | '</tr>'; |
| 95 | |
| 96 | //if password not set, ask for it |
| 97 | for ($i=0;$i<$mailfetch_server_number;$i++) { |
| 98 | if ($mailfetch_pass_[$i]=='') { |
| 99 | echo html_tag( 'tr', |
| 100 | html_tag( 'td', _("Password for") . ' <b>' . |
| 101 | htmlspecialchars((($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i])) . |
| 102 | '</b>: ', |
| 103 | 'right' ) . |
| 104 | html_tag( 'td', '<input type="password" name="pass_' . $i , '">', 'left' ) |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | echo html_tag( 'tr', |
| 109 | html_tag( 'td', ' ' ) . |
| 110 | html_tag( 'td', '<input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '">', 'left' ) |
| 111 | ) . |
| 112 | '</table></form>'; |
| 113 | exit(); |
| 114 | } |
| 115 | |
| 116 | if ( $server_to_fetch == 'all' ) { |
| 117 | $i_start = 0; |
| 118 | $i_stop = $mailfetch_server_number; |
| 119 | } else { |
| 120 | $i_start = $server_to_fetch; |
| 121 | $i_stop = $i_start+1; |
| 122 | } |
| 123 | |
| 124 | for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) { |
| 125 | $mailfetch_server=$mailfetch_server_[$i_loop]; |
| 126 | $mailfetch_port=$mailfetch_port_[$i_loop]; |
| 127 | $mailfetch_user=$mailfetch_user_[$i_loop]; |
| 128 | if ($mailfetch_pass_[$i_loop]=="") { |
| 129 | $tmp="pass_$i_loop"; |
| 130 | $mailfetch_pass=$$tmp; |
| 131 | } else { |
| 132 | $mailfetch_pass=$mailfetch_pass_[$i_loop]; |
| 133 | } |
| 134 | $mailfetch_lmos=$mailfetch_lmos_[$i_loop]; |
| 135 | $mailfetch_login=$mailfetch_login_[$i_loop]; |
| 136 | $mailfetch_uidl=$mailfetch_uidl_[$i_loop]; |
| 137 | $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop]; |
| 138 | |
| 139 | |
| 140 | $pop3 = new POP3($mailfetch_server, 60); |
| 141 | |
| 142 | echo '<br>' . |
| 143 | html_tag( 'table', |
| 144 | html_tag( 'tr', |
| 145 | html_tag( 'td', '<b>' . _("Fetching from ") . |
| 146 | htmlspecialchars((($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop])) . |
| 147 | '</b>', |
| 148 | 'center' ) , |
| 149 | '', $color[9] ) , |
| 150 | '', '', 'width="90%"' ); |
| 151 | |
| 152 | flush(); |
| 153 | |
| 154 | if (!$pop3->connect($mailfetch_server,$mailfetch_port)) { |
| 155 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR ); |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | Mail_Fetch_Status(_("Opening IMAP server")); |
| 160 | $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10); |
| 161 | |
| 162 | Mail_Fetch_Status(_("Opening POP server")); |
| 163 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
| 164 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
| 165 | Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR ); |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | // register_shutdown_function($pop3->quit()); |
| 170 | |
| 171 | $msglist = $pop3->uidl(); |
| 172 | |
| 173 | $i = 1; |
| 174 | for ($j = 1; $j < sizeof($msglist); $j++) { |
| 175 | if ($msglist["$j"] == $mailfetch_uidl) { |
| 176 | $i = $j+1; |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if ($Count < $i) { |
| 182 | Mail_Fetch_Status(_("Login OK: No new messages")); |
| 183 | $pop3->quit(); |
| 184 | continue; |
| 185 | } |
| 186 | if ($Count == 0) { |
| 187 | Mail_Fetch_Status(_("Login OK: Inbox EMPTY")); |
| 188 | $pop3->quit(); |
| 189 | continue; |
| 190 | } else { |
| 191 | $newmsgcount = $Count - $i + 1; |
| 192 | Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages")); |
| 193 | } |
| 194 | |
| 195 | Mail_Fetch_Status(_("Fetching UIDL...")); |
| 196 | // Faster to get them all at once |
| 197 | $mailfetch_uidl = $pop3->uidl(); |
| 198 | |
| 199 | if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on') |
| 200 | Mail_Fetch_Status(_("Server does not support UIDL.")); |
| 201 | |
| 202 | if ($mailfetch_lmos == 'on') { |
| 203 | Mail_Fetch_Status(_("Leaving Mail on Server...")); |
| 204 | } else { |
| 205 | Mail_Fetch_Status(_("Deleting messages from server...")); |
| 206 | } |
| 207 | |
| 208 | for (; $i <= $Count; $i++) { |
| 209 | Mail_Fetch_Status(_("Fetching message ") . "$i" ); |
| 210 | set_time_limit(20); // 20 seconds per message max |
| 211 | $Message = ""; |
| 212 | $MessArray = $pop3->get($i); |
| 213 | |
| 214 | while ( (!$MessArray) or (gettype($MessArray) != "array")) { |
| 215 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR); |
| 216 | // re-connect pop3 |
| 217 | Mail_Fetch_Status(_("Server error...Disconnect")); |
| 218 | $pop3->quit(); |
| 219 | Mail_Fetch_Status(_("Reconnect from dead connection")); |
| 220 | if (!$pop3->connect($mailfetch_server)) { |
| 221 | Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR ); |
| 222 | Mail_Fetch_Status(_("Saving UIDL")); |
| 223 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
| 224 | |
| 225 | continue; |
| 226 | } |
| 227 | $Count = $pop3->login($mailfetch_user, $mailfetch_pass); |
| 228 | if (($Count == false || $Count == -1) && $pop3->ERROR != '') { |
| 229 | Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR ); |
| 230 | Mail_Fetch_Status(_("Saving UIDL")); |
| 231 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
| 232 | |
| 233 | continue; |
| 234 | } |
| 235 | Mail_Fetch_Status(_("Refetching message ") . "$i" ); |
| 236 | $MessArray = $pop3->get($i); |
| 237 | |
| 238 | } // end while |
| 239 | |
| 240 | while (list($lineNum, $line) = each ($MessArray)) { |
| 241 | $Message .= $line; |
| 242 | } |
| 243 | |
| 244 | if ($mailfetch_subfolder=="") { |
| 245 | fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n"); |
| 246 | } else { |
| 247 | fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n"); |
| 248 | } |
| 249 | $Line = fgets($imap_stream, 1024); |
| 250 | if (substr($Line, 0, 1) == '+') { |
| 251 | fputs($imap_stream, $Message); |
| 252 | sqimap_read_data($imap_stream, "A3$i", false, $response, $message); |
| 253 | if ( $response <> 'OK' ) { |
| 254 | Mail_Fetch_Status(_("Error Appending Message!")." ".$message ); |
| 255 | Mail_Fetch_Status(_("Closing POP")); |
| 256 | $pop3->quit(); |
| 257 | Mail_Fetch_Status(_("Logging out from IMAP")); |
| 258 | sqimap_logout($imap_stream); |
| 259 | |
| 260 | Mail_Fetch_Status(_("Saving UIDL")); |
| 261 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
| 262 | exit; |
| 263 | } else { |
| 264 | Mail_Fetch_Status(_("Message appended to mailbox")); |
| 265 | } |
| 266 | |
| 267 | if ($mailfetch_lmos != 'on') { |
| 268 | if( $pop3->delete($i) ) { |
| 269 | Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!")); |
| 270 | } else { |
| 271 | Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR ); |
| 272 | } |
| 273 | } |
| 274 | } else { |
| 275 | echo "$Line"; |
| 276 | Mail_Fetch_Status(_("Error Appending Message!")); |
| 277 | Mail_Fetch_Status(_("Closing POP")); |
| 278 | $pop3->quit(); |
| 279 | Mail_Fetch_Status(_("Logging out from IMAP")); |
| 280 | sqimap_logout($imap_stream); |
| 281 | |
| 282 | // not gurantee corect! |
| 283 | Mail_Fetch_Status(_("Saving UIDL")); |
| 284 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]); |
| 285 | exit; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | Mail_Fetch_Status(_("Closing POP")); |
| 290 | $pop3->quit(); |
| 291 | Mail_Fetch_Status(_("Logging out from IMAP")); |
| 292 | sqimap_logout($imap_stream); |
| 293 | if (is_array($mailfetch_uidl)) { |
| 294 | Mail_Fetch_Status(_("Saving UIDL")); |
| 295 | setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl)); |
| 296 | } |
| 297 | |
| 298 | Mail_Fetch_Status(_("Done")); |
| 299 | |
| 300 | } |
| 301 | |
| 302 | ?> |
| 303 | </center> |
| 304 | </body> |
| 305 | </html> |