db->query("SELECT `size`, `hlen`, `digest`, `bodydigest`,`attachments` FROM " . TABLE_META . " WHERE piler_id=?", array($id));
$digest = $q->row['digest'];
$bodydigest = $q->row['bodydigest'];
$size = $q->row['size'];
$hlen = $q->row['hlen'];
$attachments = $q->row['attachments'];
$data = $this->get_raw_message($id);
$_digest = openssl_digest($data, "SHA256");
$_bodydigest = openssl_digest(substr($data, $hlen), "SHA256");
$data = '';
if($_digest == $digest && $_bodydigest == $bodydigest) { return 1; }
return 0;
}
public function get_file_size($sd, $id = '') {
fputs($sd, "STAT $id\r\n");
$s = fgets($sd, 8192);
$a = explode(" ", $s);
if(isset($a[2]) && $a[2] > 0) { return $a[2]; }
return 0;
}
public function read_file($sd, $f = '', $size = 0) {
global $start;
$s = '';
$len = 0;
if($size <= 0) { return $s; }
fputs($sd, "RETR $f\r\n");
while(!safe_feof($sd, $start) && (microtime(true) - $start) < PILERGETD_TIMEOUT) {
$s .= fread($sd, PILERGETD_READ_LENGTH);
$len += PILERGETD_READ_LENGTH;
if($len >= $size) break;
}
return $s;
}
public function connect_to_pilergetd() {
if(PILERGETD_HOST) {
$sd = fsockopen(PILERGETD_HOST, PILERGETD_PORT);
if(!$sd) { return FALSE; }
$l = fgets($sd, 4096);
if(substr(PILERGETD_HOST, 0, 6) == 'ssl://') {
fputs($sd, "AUTH " . PILERGETD_PASSWORD . "\r\n");
$l = fgets($sd, 4096);
}
Registry::set('sd', $sd);
}
}
public function disconnect_from_pilergetd() {
if(PILERGETD_HOST) {
$sd = Registry::get('sd');
fputs($sd, "QUIT\r\n");
fclose($sd);
}
}
public function get_raw_message($id = '') {
$s = '';
if($id == '' || !preg_match("/^([0-9a-f]+)$/", $id)) { return $s; }
if(PILERGETD_HOST) {
$sd = Registry::get('sd');
fputs($sd, "MESSAGE $id\r\n");
$l = fgets($sd, 8192);
$message = explode(" ", rtrim($l));
while(list($k, $v) = each($message)) {
if($k == 0) {
$size = $this->get_file_size($sd, $v);
$s = $this->read_file($sd, $v, $size);
$s = gzuncompress($s);
}
else {
$size = $this->get_file_size($sd, $v);
$a = $this->read_file($sd, $v, $size);
$a = gzuncompress($a);
$repl = "ATTACHMENT_POINTER_" . $id . ".a" . $k . "_XXX_PILER";
$s = preg_replace("/$repl/", $a, $s);
$a = '';
}
}
}
else {
$handle = popen(DECRYPT_BINARY . " $id", "r");
while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){
$s .= $buf;
}
pclose($handle);
}
return $s;
}
public function get_attachment($piler_id = '', $attachment_id = '') {
$data = '';
if($piler_id == '' || $attachment_id == '' || !preg_match("/^([0-9a-f]+)$/", $piler_id) || !preg_match("/^([0-9m]+)$/", $attachment_id)) { return $data; }
if(PILERGETD_HOST) {
$sd = fsockopen(PILERGETD_HOST, PILERGETD_PORT);
if(!$sd) { return $data; }
$l = fgets($sd, 4096);
$size = $this->get_file_size($sd, $piler_id . ".a" . $attachment_id);
$data = $this->read_file($sd, $piler_id . ".a" . $attachment_id, $size);
$data = gzuncompress($data);
fclose($sd);
}
else {
$handle = popen(DECRYPT_ATTACHMENT_BINARY . " $piler_id $attachment_id", "r");
while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){
$data .= $buf;
}
pclose($handle);
}
/* check if it's a base64 encoded stuff */
$s = substr($data, 0, 4096);
$s = preg_replace("/(\r|\n)/", "", $s);
if(!preg_match("/\s/", $s)) {
return base64_decode(preg_replace("/\s/", "", $data));
}
return $data;
}
public function get_message_headers($id = '') {
$data = '';
$this->connect_to_pilergetd();
$msg = $this->get_raw_message($id);
$this->disconnect_from_pilergetd();
$this->remove_journal($msg);
$pos = strpos($msg, "\n\r\n");
if($pos == false) {
$pos = strpos($msg, "\n\n");
}
if($pos == false) { return $msg; }
$data = substr($msg, 0, $pos);
$msg = '';
$data = preg_replace("/\", "<", $data);
$data = preg_replace("/\>/", ">", $data);
return $data;
}
public function remove_journal(&$msg = '') {
$p = $q = '';
$boundary = '';
$hdr = substr($msg, 0, 4096);
$s = preg_split("/\n/", $hdr);
while(list($k, $v) = each($s)) {
if(preg_match("/boundary\s{0,}=\s{0,}\"{0,}([\w\_\-\@\.]+)\"{0,}/i", $v, $m)) {
if(isset($m[1])) { $boundary = $m[1]; break; }
}
}
$p = strstr($msg, "\nX-MS-Journal-Report:");
if($p) {
$msg = '';
$q = strstr($p, "Received: from");
if($q) {
$p = '';
$msg = $q;
$q = '';
}
else {
$msg = $p;
$p = '';
}
}
if($boundary) { $msg = substr($msg, 0, strlen($msg) - strlen($boundary) - 6); }
}
public function extract_message($id = '', $terms = '') {
$header = "";
$body_chunk = "";
$is_header = 1;
$state = "UNDEF";
$b = array();
$boundary = array();
$text_plain = 1;
$text_html = 0;
$charset = "";
$qp = $base64 = 0;
$has_text_plain = 0;
$rfc822 = 0;
$_1st_header = 1;
$from = $to = $subject = $date = $message = "";
$this->connect_to_pilergetd();
$msg = $this->get_raw_message($id);
$this->disconnect_from_pilergetd();
$this->remove_journal($msg);
$a = explode("\n", $msg); $msg = "";
while(list($k, $l) = each($a)){
$l .= "\n";
if(($l[0] == "\r" && $l[1] == "\n" && $is_header == 1) || ($l[0] == "\n" && $is_header == 1) ){
$is_header = $_1st_header = 0;
if($rfc822 == 1) { $rfc822 = 0; $is_header = 1; }
}
if($is_header == 1 && preg_match("/^Content-Type:/i", $l)) $state = "CONTENT_TYPE";
if($is_header == 1 && preg_match("/^Content-Transfer-Encoding:/i", $l)) $state = "CONTENT_TRANSFER_ENCODING";
if($state == "CONTENT_TYPE"){
$x = strstr($l, "boundary");
if($x){
$x = preg_replace("/boundary =/", "boundary=", $x);
$x = preg_replace("/boundary= /", "boundary=", $x);
$x = preg_replace("/\"\;{0,1}/", "", $x);
$x = preg_replace("/\'/", "", $x);
$b = explode("boundary=", $x);
array_push($boundary, rtrim($b[count($b)-1]));
}
if(preg_match("/charset/i", $l)){
$types = explode(";", $l);
foreach ($types as $type){
if(preg_match("/charset/i", $type)){
$type = preg_replace("/[\"\'\ ]/", "", $type);
$x = explode("=", $type);
$charset = $x[1];
}
}
}
if(strstr($l, "message/rfc822")) { $rfc822 = 1; }
if(strstr($l, "text/plain")){ $text_plain = 1; $has_text_plain = 1; }
if(strstr($l, "text/html")){ $text_html = 1; $text_plain = 0; }
}
if($state == "CONTENT_TRANSFER_ENCODING"){
if(preg_match("/quoted-printable/i", $l)){ $qp = 1; }
if(preg_match("/base64/i", $l)){ $base64 = 1; }
}
if($is_header == 1){
if($l[0] != " " && $l[0] != "\t"){ $state = "UNDEF"; }
if(preg_match("/^From:/i", $l)){ $state = "FROM"; }
if(preg_match("/^To:/i", $l) || preg_match("/^Cc:/i", $l)){ $state = "TO"; }
if(preg_match("/^Date:/i", $l)){ $state = "DATE"; }
if(preg_match("/^Subject:/i", $l)){ $state = "SUBJECT"; }
if(preg_match("/^Content-Type:/", $l)){ $state = "CONTENT_TYPE"; }
if(preg_match("/^Content-Disposition:/", $l)){ $state = "CONTENT_DISPOSITION"; }
$l = preg_replace("/", "<", $l);
$l = preg_replace("/>/", ">", $l);
if($_1st_header == 1) {
if($state == "FROM"){ $from .= preg_replace("/\r|\n/", "", $l); }
if($state == "TO"){ $to .= preg_replace("/\r|\n/", "", $l); }
if($state == "SUBJECT"){ $subject .= preg_replace("/\r|\n/", "", $l); }
if($state == "DATE"){ $date .= preg_replace("/\r|\n/", "", $l); }
}
}
else {
if($this->check_boundary($boundary, $l) == 1){
if($text_plain == 1 || $has_text_plain == 0) {
$message .= $this->flush_body_chunk($body_chunk, $charset, $qp, $base64, $text_plain, $text_html);
}
$text_plain = $text_html = $qp = $base64 = 0;
$charset = $body_chunk = "";
$is_header = 1;
continue;
}
else if(($l[0] == "\r" && $l[1] == "\n") || $l[0] == "\n"){
$state = "BODY";
$body_chunk .= $l;
}
else if($state == "BODY"){
if($text_plain == 1 || $text_html == 1){ $body_chunk .= $l; }
}
}
}
if($body_chunk && ($text_plain == 1 || $has_text_plain == 0) ){
$message .= $this->flush_body_chunk($body_chunk, $charset, $qp, $base64, $text_plain, $text_html);
}
return array('from' => $this->decode_my_str($from),
'to' => $this->decode_my_str($to),
'subject' => $this->highlight_search_terms($this->decode_my_str($subject), $terms),
'date' => $this->decode_my_str($date),
'message' => $this->highlight_search_terms($message, $terms)
);
}
private function highlight_search_terms($s = '', $terms = array()) {
$terms = explode(" ", $terms);
if(count($terms) <= 0) { return $s; }
while(list($k, $v) = each($terms)) {
$s = preg_replace("/$v/i", "$v", $s);
}
return $s;
}
private function check_boundary($boundary, $line) {
for($i=0; $iqp_decode($chunk);
}
if($base64 == 1){
$chunk = base64_decode($chunk);
}
if($charset && !preg_match("/utf-8/i", $charset)){
$s = @iconv($charset, 'utf-8', $chunk);
if($s) { $chunk = $s; $s = ''; }
}
if($text_plain == 1){
$chunk = preg_replace("/", "<", $chunk);
$chunk = preg_replace("/>/", ">", $chunk);
$chunk = preg_replace("/\n/", "
\n", $chunk);
$chunk = "\n" . $this->print_nicely($chunk);
}
if($text_html == 1){
$chunk = preg_replace("/\