2012-02-08 23:14:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ModelSearchMessage extends Model {
|
|
|
|
|
2013-11-09 14:13:09 +01:00
|
|
|
public $encoding_aliases = array(
|
|
|
|
'GB2312' => 'GBK',
|
|
|
|
'GB231280' => 'GBK'
|
|
|
|
);
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-10-30 23:40:34 +01:00
|
|
|
public function verify_message($id = '', $data = '') {
|
2012-02-08 23:14:28 +01:00
|
|
|
if($id == '') { return 0; }
|
|
|
|
|
|
|
|
$q = $this->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'];
|
|
|
|
|
|
|
|
$_digest = openssl_digest($data, "SHA256");
|
|
|
|
$_bodydigest = openssl_digest(substr($data, $hlen), "SHA256");
|
|
|
|
|
|
|
|
if($_digest == $digest && $_bodydigest == $bodydigest) { return 1; }
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
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);
|
|
|
|
|
2013-04-18 16:49:18 +02:00
|
|
|
if(substr(PILERGETD_HOST, 0, 6) == 'ssl://') {
|
|
|
|
fputs($sd, "AUTH " . PILERGETD_PASSWORD . "\r\n");
|
|
|
|
$l = fgets($sd, 4096);
|
|
|
|
}
|
2013-04-09 15:15:59 +02:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
Registry::set('sd', $sd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function disconnect_from_pilergetd() {
|
|
|
|
if(PILERGETD_HOST) {
|
|
|
|
$sd = Registry::get('sd');
|
|
|
|
|
|
|
|
fputs($sd, "QUIT\r\n");
|
|
|
|
|
|
|
|
fclose($sd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
public function get_raw_message($id = '') {
|
2013-04-09 15:02:10 +02:00
|
|
|
$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);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$repl = "ATTACHMENT_POINTER_" . $id . ".a" . $k . "_XXX_PILER";
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$s = preg_replace("/$repl/", $a, $s);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$a = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$handle = popen(DECRYPT_BINARY . " $id", "r");
|
2013-11-09 14:13:09 +01:00
|
|
|
while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))) {
|
2013-04-09 15:02:10 +02:00
|
|
|
$s .= $buf;
|
|
|
|
}
|
|
|
|
pclose($handle);
|
2013-11-09 14:13:09 +01:00
|
|
|
|
|
|
|
if($s == '') {
|
|
|
|
$handle = popen(DECRYPT_BINARY . " $id nocrypt", "r");
|
|
|
|
while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))) {
|
|
|
|
$s .= $buf;
|
|
|
|
}
|
|
|
|
pclose($handle);
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
return $s;
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-04 10:00:54 +02:00
|
|
|
public function get_attachment($piler_id = '', $attachment_id = '') {
|
|
|
|
$data = '';
|
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
if($piler_id == '' || $attachment_id == '' || !preg_match("/^([0-9a-f]+)$/", $piler_id) || !preg_match("/^([0-9m]+)$/", $attachment_id)) { return $data; }
|
2012-10-04 10:00:54 +02:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
if(PILERGETD_HOST) {
|
|
|
|
$sd = fsockopen(PILERGETD_HOST, PILERGETD_PORT);
|
|
|
|
if(!$sd) { return $data; }
|
2012-10-04 10:00:54 +02:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$l = fgets($sd, 4096);
|
2012-10-04 10:00:54 +02:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$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 {
|
2013-05-21 14:33:32 +02:00
|
|
|
$handle = popen(DECRYPT_ATTACHMENT_BINARY . " $piler_id $attachment_id", "r");
|
2013-04-09 15:02:10 +02:00
|
|
|
while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){
|
|
|
|
$data .= $buf;
|
|
|
|
}
|
|
|
|
pclose($handle);
|
|
|
|
}
|
2012-10-04 10:00:54 +02:00
|
|
|
|
2013-03-24 10:02:34 +01:00
|
|
|
/* check if it's a base64 encoded stuff */
|
2012-10-04 10:00:54 +02:00
|
|
|
|
2013-03-24 10:02:34 +01:00
|
|
|
$s = substr($data, 0, 4096);
|
|
|
|
$s = preg_replace("/(\r|\n)/", "", $s);
|
|
|
|
|
2013-03-24 23:49:52 +01:00
|
|
|
if(!preg_match("/\s/", $s)) {
|
2013-03-24 10:02:34 +01:00
|
|
|
return base64_decode(preg_replace("/\s/", "", $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
2012-10-04 10:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
public function get_message_headers($id = '') {
|
|
|
|
$data = '';
|
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$this->connect_to_pilergetd();
|
2012-02-08 23:14:28 +01:00
|
|
|
$msg = $this->get_raw_message($id);
|
2013-04-09 15:02:10 +02:00
|
|
|
$this->disconnect_from_pilergetd();
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-07-12 11:14:09 +02:00
|
|
|
$has_journal = $this->remove_journal($msg);
|
2013-01-27 21:43:42 +01:00
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
$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);
|
|
|
|
|
2013-07-12 11:14:09 +02:00
|
|
|
return array('headers' => $data, 'has_journal' => $has_journal);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function get_message_journal($id = '') {
|
|
|
|
$data = '< >';
|
|
|
|
$boundary = '';
|
|
|
|
|
|
|
|
$this->connect_to_pilergetd();
|
|
|
|
$msg = $this->get_raw_message($id);
|
|
|
|
$this->disconnect_from_pilergetd();
|
|
|
|
|
|
|
|
$hdr = substr($msg, 0, 8192);
|
|
|
|
|
|
|
|
$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:");
|
|
|
|
$msg = '';
|
|
|
|
|
|
|
|
if($p) {
|
|
|
|
|
|
|
|
$s = preg_split("/\n/", $p);
|
|
|
|
|
|
|
|
$i=0; $j=0; $data = '';
|
|
|
|
|
|
|
|
while(list($k, $v) = each($s)) {
|
|
|
|
if(strstr($v, $boundary)) { $i++; }
|
|
|
|
if($i > 0 && preg_match("/^\s{1,}$/", $v)) { $j++; }
|
|
|
|
|
|
|
|
if($j == 1) {
|
|
|
|
$data .= "$v\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($i >= 2) { break; }
|
|
|
|
}
|
|
|
|
|
|
|
|
$p = '';
|
|
|
|
|
|
|
|
$data = preg_replace("/\</", "<", $data);
|
|
|
|
$data = preg_replace("/\>/", ">", $data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-27 21:43:42 +01:00
|
|
|
public function remove_journal(&$msg = '') {
|
|
|
|
$p = $q = '';
|
|
|
|
$boundary = '';
|
2013-07-12 11:14:09 +02:00
|
|
|
$has_journal = 0;
|
2013-01-27 21:43:42 +01:00
|
|
|
|
|
|
|
$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:");
|
2013-12-19 13:24:27 +01:00
|
|
|
|
2013-01-27 21:43:42 +01:00
|
|
|
if($p) {
|
2013-07-12 11:14:09 +02:00
|
|
|
$has_journal = 1;
|
|
|
|
|
2013-01-27 21:43:42 +01:00
|
|
|
$msg = '';
|
|
|
|
$q = strstr($p, "Received: from");
|
|
|
|
if($q) {
|
|
|
|
$p = '';
|
|
|
|
$msg = $q;
|
|
|
|
$q = '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$msg = $p;
|
|
|
|
$p = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 11:14:09 +02:00
|
|
|
if($boundary) {
|
|
|
|
$msg = substr($msg, 0, strlen($msg) - strlen($boundary) - 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_journal;
|
2013-01-27 21:43:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-29 17:59:57 +02:00
|
|
|
public function extract_message($id = '', $terms = '') {
|
2012-02-08 23:14:28 +01:00
|
|
|
$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;
|
2013-03-28 16:39:36 +01:00
|
|
|
$rfc822 = 0;
|
2013-03-29 09:17:19 +01:00
|
|
|
$_1st_header = 1;
|
2013-10-30 23:40:34 +01:00
|
|
|
$verification = 1;
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2014-06-25 21:53:23 +02:00
|
|
|
$from = $to = $subject = $date = $text_message = $html_message = "";
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$this->connect_to_pilergetd();
|
2012-02-08 23:14:28 +01:00
|
|
|
$msg = $this->get_raw_message($id);
|
2013-10-30 23:40:34 +01:00
|
|
|
|
|
|
|
if(ENABLE_ON_THE_FLY_VERIFICATION == 0) {
|
|
|
|
$verification = $this->verify_message($id, $msg);
|
|
|
|
}
|
|
|
|
|
2013-04-09 15:02:10 +02:00
|
|
|
$this->disconnect_from_pilergetd();
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-07-12 11:14:09 +02:00
|
|
|
$has_journal = $this->remove_journal($msg);
|
2013-01-27 21:43:42 +01:00
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
$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) ){
|
2013-03-29 09:17:19 +01:00
|
|
|
$is_header = $_1st_header = 0;
|
2013-03-28 16:39:36 +01:00
|
|
|
|
|
|
|
if($rfc822 == 1) { $rfc822 = 0; $is_header = 1; }
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
2013-03-25 14:48:58 +01:00
|
|
|
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";
|
2012-02-08 23:14:28 +01:00
|
|
|
|
|
|
|
if($state == "CONTENT_TYPE"){
|
2014-06-28 11:19:04 +02:00
|
|
|
|
2014-06-25 21:53:23 +02:00
|
|
|
$x = stristr($l, "boundary");
|
2012-02-08 23:14:28 +01:00
|
|
|
if($x){
|
2014-06-28 11:19:04 +02:00
|
|
|
|
2014-08-13 14:09:25 +02:00
|
|
|
$s1 = explode(";", $x);
|
|
|
|
$x = $s1[0];
|
|
|
|
|
2014-06-28 11:19:04 +02:00
|
|
|
$x = preg_replace("/boundary\s{0,}=\s{0,}/i", "boundary=", $x);
|
|
|
|
//$x = preg_replace("/boundary= /i", "boundary=", $x);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-03-25 14:48:58 +01:00
|
|
|
$x = preg_replace("/\"\;{0,1}/", "", $x);
|
2012-02-08 23:14:28 +01:00
|
|
|
$x = preg_replace("/\'/", "", $x);
|
|
|
|
|
|
|
|
$b = explode("boundary=", $x);
|
|
|
|
|
2014-06-25 21:53:23 +02:00
|
|
|
$__boundary = rtrim($b[count($b)-1]);
|
|
|
|
|
|
|
|
if($__boundary) { array_push($boundary, $__boundary); }
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2013-11-09 14:13:09 +01:00
|
|
|
$charset = rtrim(strtoupper($x[1]));
|
|
|
|
|
|
|
|
if(isset($this->encoding_aliases[$charset])) { $charset = $this->encoding_aliases[$charset]; }
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 16:39:36 +01:00
|
|
|
if(strstr($l, "message/rfc822")) { $rfc822 = 1; }
|
|
|
|
|
2014-06-25 21:53:23 +02:00
|
|
|
if(stristr($l, "text/plain")){ $text_plain = 1; $text_html = 0; $has_text_plain = 1; }
|
|
|
|
if(stristr($l, "text/html")){ $text_html = 1; $text_plain = 0; }
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($state == "CONTENT_TRANSFER_ENCODING"){
|
2012-11-28 14:30:58 +01:00
|
|
|
if(preg_match("/quoted-printable/i", $l)){ $qp = 1; }
|
|
|
|
if(preg_match("/base64/i", $l)){ $base64 = 1; }
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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"; }
|
2013-03-28 16:39:36 +01:00
|
|
|
if(preg_match("/^Content-Disposition:/", $l)){ $state = "CONTENT_DISPOSITION"; }
|
2012-02-08 23:14:28 +01:00
|
|
|
|
|
|
|
$l = preg_replace("/</", "<", $l);
|
|
|
|
$l = preg_replace("/>/", ">", $l);
|
|
|
|
|
2013-03-29 09:17:19 +01:00
|
|
|
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); }
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if($this->check_boundary($boundary, $l) == 1){
|
|
|
|
|
|
|
|
if($text_plain == 1 || $has_text_plain == 0) {
|
2014-06-25 21:53:23 +02:00
|
|
|
$text_message .= $this->flush_body_chunk($body_chunk, $charset, $qp, $base64, $text_plain, $text_html);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($text_html == 1) {
|
|
|
|
$html_message .= $this->flush_body_chunk($body_chunk, $charset, $qp, $base64, $text_plain, $text_html);
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$text_plain = $text_html = $qp = $base64 = 0;
|
|
|
|
|
|
|
|
$charset = $body_chunk = "";
|
|
|
|
|
2013-03-25 14:48:58 +01:00
|
|
|
$is_header = 1;
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-25 21:53:23 +02:00
|
|
|
if($body_chunk) {
|
|
|
|
if($text_plain == 1 || $has_text_plain == 0) {
|
|
|
|
$text_message .= $this->flush_body_chunk($body_chunk, $charset, $qp, $base64, $text_plain, $text_html);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($text_html == 1) {
|
|
|
|
$html_message .= $this->flush_body_chunk($body_chunk, $charset, $qp, $base64, $text_plain, $text_html);
|
|
|
|
}
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-25 21:53:23 +02:00
|
|
|
if(strlen($html_message) > 20) {
|
2014-06-28 11:19:04 +02:00
|
|
|
$message = $this->highlight_search_terms($html_message, $terms, 1);
|
2014-06-25 21:53:23 +02:00
|
|
|
} else {
|
|
|
|
$message = $this->highlight_search_terms($text_message, $terms);
|
|
|
|
}
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
return array('from' => $this->decode_my_str($from),
|
|
|
|
'to' => $this->decode_my_str($to),
|
2013-06-29 17:59:57 +02:00
|
|
|
'subject' => $this->highlight_search_terms($this->decode_my_str($subject), $terms),
|
2012-02-08 23:14:28 +01:00
|
|
|
'date' => $this->decode_my_str($date),
|
2014-06-25 21:53:23 +02:00
|
|
|
'message' => $message,
|
2013-10-30 23:40:34 +01:00
|
|
|
'has_journal' => $has_journal,
|
|
|
|
'verification' => $verification
|
2012-02-08 23:14:28 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-28 11:19:04 +02:00
|
|
|
private function highlight_search_terms($s = '', $terms = '', $html = 0) {
|
2013-10-05 11:34:06 +02:00
|
|
|
$fields = array("from:", "to:", "subject:", "body:");
|
|
|
|
|
2014-08-18 15:35:25 +02:00
|
|
|
$terms = preg_replace("/(\'|\"|\=|\>|\<)/", "", $terms);
|
2013-07-03 10:20:29 +02:00
|
|
|
|
2014-06-28 11:19:04 +02:00
|
|
|
$a = explode(" ", $terms);
|
|
|
|
$terms = array();
|
|
|
|
|
|
|
|
while(list($k, $v) = each($a)) {
|
|
|
|
if(strlen($v) >= 3 && !in_array($v, $fields)) {
|
2014-08-18 15:35:25 +02:00
|
|
|
//$v = preg_replace("/\W/", "", $v);
|
|
|
|
if($v) { array_push($terms, $v); }
|
2014-06-28 11:19:04 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-29 17:59:57 +02:00
|
|
|
|
|
|
|
if(count($terms) <= 0) { return $s; }
|
|
|
|
|
2013-10-05 11:34:06 +02:00
|
|
|
|
2014-06-28 11:19:04 +02:00
|
|
|
if($html == 0) {
|
|
|
|
while(list($k, $v) = each($terms)) {
|
|
|
|
$s = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
2013-10-05 11:34:06 +02:00
|
|
|
|
2013-07-03 10:20:29 +02:00
|
|
|
|
2014-06-28 11:19:04 +02:00
|
|
|
$tokens = preg_split("/\</", $s);
|
|
|
|
$s = '';
|
|
|
|
|
|
|
|
while(list($k, $token) = each($tokens)) {
|
|
|
|
|
|
|
|
$pos = strpos($token, ">");
|
|
|
|
if($pos > 0) {
|
|
|
|
$len = strlen($token);
|
|
|
|
|
|
|
|
$s .= '<' . substr($token, 0, $pos) . '>';
|
|
|
|
|
|
|
|
if($len > $pos+1) {
|
|
|
|
$str = substr($token, $pos+1, $len);
|
|
|
|
|
|
|
|
reset($terms);
|
|
|
|
while(list($k, $v) = each($terms)) {
|
|
|
|
$str = preg_replace("/$v/i", "<span class=\"message_highlight\">$v</span>", $str);
|
|
|
|
}
|
|
|
|
|
|
|
|
$s .= $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-06-29 17:59:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
private function check_boundary($boundary, $line) {
|
|
|
|
|
|
|
|
for($i=0; $i<count($boundary); $i++){
|
|
|
|
if(strstr($line, $boundary[$i])){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function flush_body_chunk($chunk, $charset, $qp, $base64, $text_plain, $text_html) {
|
|
|
|
|
|
|
|
if($qp == 1){
|
|
|
|
$chunk = $this->qp_decode($chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($base64 == 1){
|
|
|
|
$chunk = base64_decode($chunk);
|
|
|
|
}
|
|
|
|
|
2012-08-25 15:01:19 +02:00
|
|
|
if($charset && !preg_match("/utf-8/i", $charset)){
|
2013-11-09 14:13:09 +01:00
|
|
|
$s = @iconv($charset, 'utf-8' . '//IGNORE', $chunk);
|
2012-09-09 23:18:30 +02:00
|
|
|
if($s) { $chunk = $s; $s = ''; }
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if($text_plain == 1){
|
|
|
|
$chunk = preg_replace("/</", "<", $chunk);
|
|
|
|
$chunk = preg_replace("/>/", ">", $chunk);
|
|
|
|
|
|
|
|
$chunk = preg_replace("/\n/", "<br />\n", $chunk);
|
|
|
|
$chunk = "\n" . $this->print_nicely($chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($text_html == 1){
|
2014-06-28 11:19:04 +02:00
|
|
|
$chunk = preg_replace("/\<style\>([\w\W]+)\<\/style\>/i", "", $chunk);
|
2012-02-08 23:14:28 +01:00
|
|
|
|
|
|
|
if(ENABLE_REMOTE_IMAGES == 0) {
|
|
|
|
$chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\"([^\"]+)/", "style=\"xxxx", $chunk);
|
2014-06-28 11:19:04 +02:00
|
|
|
$chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\'([^\']+)/", "style='xxxx", $chunk);
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$chunk = preg_replace("/\<body ([\w\s\;\"\'\#\d\:\-\=]+)\>/i", "<body>", $chunk);
|
|
|
|
|
|
|
|
if(ENABLE_REMOTE_IMAGES == 0) { $chunk = preg_replace("/\<img([^\>]+)\>/i", "<img src=\"" . REMOTE_IMAGE_REPLACEMENT . "\" />", $chunk); }
|
|
|
|
|
|
|
|
/* prevent scripts in the HTML part */
|
|
|
|
|
|
|
|
$chunk = preg_replace("/document\.write/", "document.writeee", $chunk);
|
|
|
|
$chunk = preg_replace("/<\s{0,}script([\w\W]+)\/script\s{0,}\>/i", "<!-- disabled javascript here -->", $chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function print_nicely($chunk) {
|
|
|
|
$k = 0;
|
|
|
|
$nice_chunk = "";
|
|
|
|
|
|
|
|
$x = explode(" ", $chunk);
|
|
|
|
|
|
|
|
for($i=0; $i<count($x); $i++){
|
|
|
|
$nice_chunk .= "$x[$i] ";
|
|
|
|
$k += strlen($x[$i]);
|
|
|
|
|
|
|
|
if(strstr($x[$i], "\n")){ $k = 0; }
|
|
|
|
|
|
|
|
if($k > 70){ $nice_chunk .= "\n"; $k = 0; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nice_chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function NiceSize($size) {
|
|
|
|
if($size < 1000) return "1k";
|
|
|
|
if($size < 100000) return round($size/1000) . "k";
|
|
|
|
|
|
|
|
return sprintf("%.1f", $size/1000000) . "M";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function qp_decode($l) {
|
|
|
|
$res = "";
|
|
|
|
$c = "";
|
|
|
|
|
|
|
|
if($l == ""){ return ""; }
|
|
|
|
|
|
|
|
/* remove soft breaks at the end of lines */
|
|
|
|
|
|
|
|
if(preg_match("/\=\r\n/", $l)){ $l = preg_replace("/\=\r\n/", "", $l); }
|
|
|
|
if(preg_match("/\=\n/", $l)){ $l = preg_replace("/\=\n/", "", $l); }
|
|
|
|
|
|
|
|
for($i=0; $i<strlen($l); $i++){
|
|
|
|
$c = $l[$i];
|
|
|
|
|
|
|
|
if($c == '=' && ctype_xdigit($l[$i+1]) && ctype_xdigit($l[$i+2])){
|
|
|
|
$a = $l[$i+1];
|
|
|
|
$b = $l[$i+2];
|
|
|
|
|
|
|
|
$c = chr(16*hexdec($a) + hexdec($b));
|
|
|
|
|
|
|
|
$i += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res .= $c;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function decode_my_str($what = '') {
|
|
|
|
$result = "";
|
|
|
|
|
|
|
|
$what = rtrim($what);
|
|
|
|
|
|
|
|
$a = preg_split("/\s/", $what);
|
|
|
|
|
|
|
|
while(list($k, $v) = each($a)){
|
|
|
|
$x = preg_match("/\?\=$/", $v);
|
|
|
|
|
|
|
|
if( ($x == 0 && $k > 0) || ($x == 1 && $k == 1) ){
|
|
|
|
$result .= " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$result .= $this->fix_encoded_string($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function fix_encoded_string($what = '') {
|
|
|
|
$s = "";
|
|
|
|
|
|
|
|
$what = rtrim($what, "\"\r\n");
|
|
|
|
$what = ltrim($what, "\"");
|
|
|
|
|
|
|
|
if(preg_match("/^\=\?/", $what) && preg_match("/\?\=$/", $what)){
|
|
|
|
$what = preg_replace("/^\=\?/", "", $what);
|
|
|
|
$what = preg_replace("/\?\=$/", "", $what);
|
|
|
|
|
2013-11-09 14:13:09 +01:00
|
|
|
$encoding = strtoupper(substr($what, 0, strpos($what, '?')));
|
|
|
|
if(isset($this->encoding_aliases[$encoding])) { $encoding = $this->encoding_aliases[$encoding]; }
|
2012-08-01 21:53:01 +02:00
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
if(preg_match("/\?Q\?/i", $what)){
|
|
|
|
$x = preg_replace("/^([\w\-]+)\?Q\?/i", "", $what);
|
|
|
|
|
|
|
|
$s = quoted_printable_decode($x);
|
|
|
|
$s = preg_replace("/_/", " ", $s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(preg_match("/\?B\?/i", $what)){
|
|
|
|
$x = preg_replace("/^([\w\-]+)\?B\?/i", "", $what);
|
|
|
|
|
|
|
|
$s = base64_decode($x);
|
|
|
|
$s = preg_replace('/\0/', "*", $s);
|
|
|
|
}
|
|
|
|
|
2012-08-01 21:53:01 +02:00
|
|
|
if(!preg_match("/utf-8/i", $encoding)){
|
2013-11-09 14:13:09 +01:00
|
|
|
$s = iconv($encoding, 'utf-8' . '//IGNORE', $s);
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$s = utf8_encode($what);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-06 15:27:20 +02:00
|
|
|
public function get_piler_id_by_id($id = 0) {
|
|
|
|
$query = $this->db->query("SELECT `piler_id` FROM `" . TABLE_META . "` WHERE id=?", array($id));
|
|
|
|
if(isset($query->row['piler_id'])) { return $query->row['piler_id']; }
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-04 10:00:54 +02:00
|
|
|
public function get_id_by_piler_id($piler_id = '') {
|
|
|
|
if($piler_id == '') { return -1; }
|
|
|
|
|
|
|
|
$query = $this->db->query("SELECT `id` FROM `" . TABLE_META . "` WHERE piler_id=?", array($piler_id));
|
|
|
|
if(isset($query->row['id'])) { return $query->row['id']; }
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function get_attachment_by_id($id = 0) {
|
|
|
|
if($id <= 0) { return array(); }
|
|
|
|
|
|
|
|
$query = $this->db->query("SELECT id, piler_id, attachment_id, name, type, ptr FROM " . TABLE_ATTACHMENT . " WHERE id=?", array($id));
|
|
|
|
|
|
|
|
if(isset($query->row)) {
|
|
|
|
$metaid = $this->get_id_by_piler_id($query->row['piler_id']);
|
|
|
|
|
|
|
|
if($metaid > 0 && $this->model_search_search->check_your_permission_by_id($metaid) == 1) {
|
|
|
|
if($query->row['ptr'] > 0) {
|
|
|
|
$query = $this->db->query("SELECT id, piler_id, attachment_id, name, type FROM " . TABLE_ATTACHMENT . " WHERE id=?", array($query->row['ptr']));
|
|
|
|
}
|
|
|
|
|
|
|
|
$attachment = $this->get_attachment($query->row['piler_id'], $query->row['attachment_id']);
|
|
|
|
|
2013-03-23 19:14:36 +01:00
|
|
|
return array('filename' => fix_evolution_mime_name_crap($query->row['name']), 'piler_id' => $query->row['piler_id'], 'attachment' => $attachment);
|
2012-10-04 10:00:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function get_attachment_list($piler_id = 0) {
|
|
|
|
$data = array();
|
|
|
|
|
|
|
|
if($piler_id == '') { return array(); }
|
|
|
|
|
|
|
|
$query = $this->db->query("SELECT id, name, type, ptr FROM " . TABLE_ATTACHMENT . " WHERE piler_id=?", array($piler_id));
|
|
|
|
|
|
|
|
if(!isset($query->rows)) { return array(); }
|
|
|
|
|
|
|
|
foreach($query->rows as $q) {
|
2013-03-23 19:14:36 +01:00
|
|
|
$q['name'] = fix_evolution_mime_name_crap($q['name']);
|
2012-10-04 10:00:54 +02:00
|
|
|
array_push($data, $q);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-16 12:57:29 +01:00
|
|
|
public function is_message_spam($id = 0) {
|
|
|
|
$spam = 0;
|
|
|
|
|
2014-01-16 12:59:56 +01:00
|
|
|
if($id > 0 && DEFAULT_RETENTION > 30) {
|
2014-01-16 12:57:29 +01:00
|
|
|
$query = $this->db->query("SELECT spam FROM " . TABLE_META . " WHERE id=?", array($id));
|
|
|
|
|
|
|
|
if(isset($query->row['spam'])) { $spam = $query->row['spam']; }
|
|
|
|
}
|
|
|
|
|
|
|
|
return $spam;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function not_spam($id = 0) {
|
|
|
|
|
|
|
|
if($id > 0) {
|
|
|
|
$query = $this->db->query("UPDATE " . TABLE_META . " SET spam=0, retained=? WHERE id=?", array(NOW + (DEFAULT_RETENTION*86400), $id));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
public function get_message_tag($id = '', $uid = 0) {
|
|
|
|
if($id == '' || $uid <= 0) { return ''; }
|
|
|
|
|
2012-09-06 15:27:20 +02:00
|
|
|
$query = $this->db->query("SELECT `tag` FROM " . TABLE_TAG . " WHERE uid=? AND id=?", array($uid, $id));
|
2012-02-08 23:14:28 +01:00
|
|
|
|
2013-02-11 20:24:19 +01:00
|
|
|
if(isset($query->row['tag'])) { return strip_tags($query->row['tag']); }
|
2012-02-08 23:14:28 +01:00
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function add_message_tag($id = '', $uid = 0, $tag = '') {
|
|
|
|
if($id == '' || $uid <= 0) { return 0; }
|
|
|
|
|
2012-09-06 15:27:20 +02:00
|
|
|
if($tag == '') {
|
|
|
|
$query = $this->db->query("DELETE FROM " . TABLE_TAG . " WHERE uid=? AND id=?", array($uid, $id));
|
|
|
|
} else {
|
|
|
|
$query = $this->db->query("UPDATE " . TABLE_TAG . " SET tag=? WHERE uid=? AND id=?", array($tag, $uid, $id));
|
|
|
|
if($this->db->countAffected() == 0) {
|
|
|
|
$query = $this->db->query("INSERT INTO " . TABLE_TAG . " (id, uid, tag) VALUES(?,?,?)", array($id, $uid, $tag));
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 15:27:20 +02:00
|
|
|
return 1;
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-10 14:52:50 +01:00
|
|
|
public function bulk_add_message_tag($ids = array(), $uid = 0, $tag = '', $q = '') {
|
|
|
|
$arr = array_merge(array($uid), $ids);
|
|
|
|
|
|
|
|
$query = $this->db->query("DELETE FROM " . TABLE_TAG . " WHERE uid=? AND id IN ($q)", $arr);
|
|
|
|
|
|
|
|
if($tag) {
|
|
|
|
foreach ($ids as $id) {
|
|
|
|
$query = $this->db->query("INSERT INTO " . TABLE_TAG . " (id, uid, tag) VALUES(?,?,?)", array($id, $uid, $tag));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 15:27:20 +02:00
|
|
|
|
|
|
|
public function get_message_note($id = '', $uid = 0) {
|
|
|
|
if($id == '' || $uid <= 0) { return ''; }
|
|
|
|
|
|
|
|
$query = $this->db->query("SELECT `note` FROM " . TABLE_NOTE . " WHERE uid=? AND id=?", array($uid, $id));
|
|
|
|
|
2013-02-11 20:24:19 +01:00
|
|
|
if(isset($query->row['note'])) { return strip_tags(urldecode($query->row['note'])); }
|
2012-09-06 15:27:20 +02:00
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function add_message_note($id = '', $uid = 0, $note = '') {
|
|
|
|
if($id == '' || $uid <= 0) { return 0; }
|
|
|
|
|
|
|
|
if($note == '') {
|
|
|
|
$query = $this->db->query("DELETE FROM " . TABLE_NOTE . " WHERE uid=? AND id=?", array($uid, $id));
|
|
|
|
} else {
|
|
|
|
$query = $this->db->query("UPDATE " . TABLE_NOTE . " SET note=? WHERE uid=? AND id=?", array($note, $uid, $id));
|
|
|
|
if($this->db->countAffected() == 0) {
|
|
|
|
$query = $this->db->query("INSERT INTO " . TABLE_NOTE . " (id, uid, note) VALUES(?,?,?)", array($id, $uid, $note));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-08 23:14:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|