<? // установка параметров подключения $serv = 'pop3.mail.ru'; $port = 110; $user = 'mylog'; $pass = 'mypass'; // назначение фильтра $target = array('anybody@mailbox.ru', 'everybody@mailbox.ru'); // подключение базы данных mysql_connect('localhost','login','password'); mysql_select_db('database');
// класс pop3 class POP3 { var $totaille = 0 ; var $banner = ''; var $dial = Array(); function pop_connect($str_Host = 'localhost', $int_Port = 110) { $soket = fsockopen ($str_Host, $int_Port, $int_Err, $str_Err); if(!$soket) { global $str_Err,$int_Err; return FALSE; } $this->banner = ($this->dial[] = fgets($soket,100)); return $soket; } function pop_login($soket, $str_User, $str_Pass) { if(!$soket) { return -1; } $qu = "USER $str_User\r\n"; fputs($soket, $qu); $this -> dial[] = '-> '.$qu; if(eregi('-ERR', ($this -> dial[] = '<- '.fgets($soket,100)))) return -2; $qu = "PASS $str_Pass\r\n"; fputs($soket, $qu); $this -> dial[] = '-> '.str_replace($str_Pass,"[Pass]",$qu); if(eregi('-ERR', ($this -> dial[] = '<- '.fgets($soket,100)))) return -3; $qu = "STAT\r\n"; fputs($soket, $qu); $this -> dial[] = '-> '.$qu; $r = split(" ", ($this -> dial[] = '<- '.fgets($soket,100))); if($r[1] == '-ERR') return -4; $this -> totaille = $r[3]; return $r[2]; } function pop_list($soket,$count) { if(!$soket) { return -1; } $qu = "LIST\r\n"; fputs($soket, $qu); $this->dial[] = '-> '.$qu; if(substr(($this->dial[] = '<- '.fgets($soket, 200)), 3, 7) == '-ERR') return -2; $rep[0] = $count; for($j = 1; $j <= $count; $j++) { if($j > $count) { break; } $line = fgets($soket, 500); $line = split(' ',$line); if($line[0] != $j) { $rep[$j] = 'deleted'; } else { $rep[$j] = $line[1]; } unset($line); } return $rep; } function pop_top($soket, $int_Id) { if(!$soket) { global $PHP_SELF; return -1; } $rep = ''; $all = '' ; $qu = "TOP $int_Id 0\r\n"; fputs($soket, $qu); $this->dial[] = '-> '.$qu; if(eregi('-ERR', ($this->dial[] = '<- '.fgets($soket, 200)))) return -2; $line = fgets($soket,200); while(!ereg("^\.\r\n", $line)) { $tmp = explode(':',$line,2); if(!empty($tmp[1])) { $ar_Headers[trim(strtolower($tmp[0]))] = trim($tmp[1]); } $rep .= $line; $all .= $line; $line = fgets($soket,200); if(empty($line)) { break; } $rep = ''; $tmp = array(); } $ar_Headers['all'] = $all; return $ar_Headers; } function pop_get($soket, $int_Id) { if(!$soket) { return -1; } $qu = "RETR $int_Id\r\n"; fputs($soket, $qu); $this->dial[] = '-> '.$qu; if(eregi('-ERR', ($this->dial[] = '<- '.fgets($soket, 200)))) return -2; $line = fgets($soket, 2000); $ft_a = ''; for($h=0; !ereg("^\.\r\n", $line); $h++) { $ar_Rep[$h] = $line; $line = fgets($soket, 2000); // <!-- остановка на text/html if(!empty($ft_a)) { $h--; } else { $ft_a = strstr($line, 'text/html'); } // --> if(empty($line)) { break; }} return $ar_Rep; } function pop_quit($soket) { if(!$soket) return -1; $qu = "QUIT\r\n"; fputs($soket, $qu); $this->dial[] = '-> '.$qu; $this->dial[] = '<- '.fgets($soket,200); fclose($soket); return TRUE; }} // функция выборки из базы данных function select_from_db($q) { $r = mysql_query($q); $n = mysql_num_rows($r); for($i = 0; $i < $n; $i++) { $s[$i] = mysql_fetch_array($r); } if(!empty($s)) return $s; else return ''; } set_magic_quotes_runtime(0); set_time_limit(120);
// подключение $POP3 = new POP3(); $soket = $POP3->pop_connect($serv, $port); $count = $POP3->pop_login($soket, $user, $pass); // чтение заголовков $liste = $POP3->pop_list($soket,$count); $ft = 0; for($i = $count; $i > 1; $i--) { $message = $POP3->pop_top($soket, $i); $k6 = 0; // реализация фильтра if(!strstr($message['from'], $target[0]) and !strstr($message['from'], $target[1])) continue; if(!empty($message['date'])) { $get_dat = urlencode($message['date']); $datarr[$i] = $get_dat; for($j = 0; $j < $i; $j++) { if($get_dat == $datarr[$j]) $k6 = 1; } if($k6 == 1) continue; $sql = "select date from table where date='$get_dat'"; $res = select_from_db($sql); if(!empty($res)) continue; // чтение письма $msgNum = $i; $to = $message['to']; $bound = substr($message['all'], strpos($message['all'], 'boundary="') + 10, 90); $bound = trim(str_replace(")", '\)', str_replace("(", '\(', substr($bound, 0, strpos($bound,'"'))))); $sujet = $message['subject']; if(!$sujet) { $sujet = ''; } $to = str_replace('<','',str_replace('>','',$to)); $ar_Msg = $POP3->pop_get($soket, $msgNum); $ok = false; $body = ''; for($i1 = 0; @$ar_Msg[$i1]; $i1++) { $k = $ar_Msg[$i1]; $a = strpos($k, 'text/html'); if($a > 0) break; if($ok === false) { if($k == "\r\n") { $ok = true; }} else { $body .= $k; }} // конвертация письма if($bound) { if(@eregi($bound, $body)) { $bd = spliti("(-)*".$bound, $body); for($i2 = 1, $body = '', $pj = Array(); isset($bd[$i2]); $i2++) { if(!eregi("Content-Type",$bd[$i2])) continue; if(!eregi("Content-Disposition:.?attachment", $bd[$i2])) { if(eregi("Encoding:.?base64",$bd[$i2])) $b64 = 1; else $b64 = 0; $t = trim(str_replace('=20', '', $bd[$i2])); $t = trim(substr($t, strpos($t, "\r\n\r\n"), strlen($t))); if($b64) $t = base64_decode($t); $body .= eregi_replace("=0A", chr(hexdec("0A")),$t); $body .= "<!-- NextPart @ Mime (quelle daube) -->\r\n\r\n"; } }}} if(!strstr($body, 'TA TURNE-TRANS')) continue; $body = convert_cyr_string($body, 'koi8-r', 'windows-1251'); // сохранение письма $mft[$ft] = $get_dat; $fp = fopen('data/'.$mft[$ft].'.dat', 'w'); $res = fwrite($fp, $body); fclose($fp); $ft++; } $POP3->pop_quit($soket); // начало обработки include('search.php'); ?>
|