added the webui to the tarball

This commit is contained in:
SJ
2012-02-08 23:14:28 +01:00
parent 79cdeed1b6
commit 1211e9a39c
272 changed files with 26456 additions and 11 deletions

View File

@ -0,0 +1,420 @@
<?php
class ModelSearchMessage extends Model {
public function get_store_path($id = '') {
if($id == '') { return ''; }
$len = strlen($id);
return DIR_STORE . "/" . substr($id, $len-6, 2) . "/" . substr($id, $len-4, 2) . "/" . substr($id, $len-2, 2) . "/" . $id;
}
public function verify_message($id = '') {
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'];
$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_raw_message($id = '') {
$data = '';
if($id == '' || !preg_match("/^([0-9a-f]+)$/", $id)) { return $data; }
$handle = popen(DECRYPT_BINARY . " $id", "r");
while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){
$data .= $buf;
}
pclose($handle);
return $data;
}
public function get_message_headers($id = '') {
$data = '';
//$f = $this->get_store_path($id);
//$msg = $this->decrypt_and_uncompress_file($f.".m");
$msg = $this->get_raw_message($id);
$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("/\</", "&lt;", $data);
$data = preg_replace("/\>/", "&gt;", $data);
return $data;
}
public function extract_message($id = '') {
$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;
$from = $to = $subject = $date = $message = "";
$msg = $this->get_raw_message($id);
//print "a: $msg\n";
$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 = 0;
}
if(preg_match("/^Content-Type:/i", $l)) $state = "CONTENT_TYPE";
if(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("/\"/", "", $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, "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(strstr($l, "quoted-printable")){ $qp = 1; }
if(strstr($l, "base64")){ $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"; }
$l = preg_replace("/</", "&lt;", $l);
$l = preg_replace("/>/", "&gt;", $l);
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 = "";
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->decode_my_str($subject),
'date' => $this->decode_my_str($date),
'message' => $message
);
}
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);
}
if(!preg_match("/utf-8/i", $charset)){
$chunk = utf8_encode($chunk);
}
if($text_plain == 1){
$chunk = preg_replace("/</", "&lt;", $chunk);
$chunk = preg_replace("/>/", "&gt;", $chunk);
//$chunk = "<pre>\n" . $this->print_nicely($chunk) . "</pre>\n";
$chunk = preg_replace("/\n/", "<br />\n", $chunk);
$chunk = "\n" . $this->print_nicely($chunk);
}
if($text_html == 1){
$chunk = preg_replace("/\<style([^\>]+)\>([\w\W]+)\<\/style\>/i", "", $chunk);
if(ENABLE_REMOTE_IMAGES == 0) {
$chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\"([^\"]+)/", "style=\"xxxx", $chunk);
$chunk = preg_replace("/style([\s]{0,}=[\s]{0,})\'([^\']+)/", "style=\'xxxx", $chunk);
}
$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);
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);
}
if(!preg_match("/utf-8/i", $what)){
$s = utf8_encode($s);
}
}
else {
$s = utf8_encode($what);
}
return $s;
}
public function get_message_tag($id = '', $uid = 0) {
if($id == '' || $uid <= 0) { return ''; }
$query = $this->db->query("SELECT `tag` FROM " . TABLE_TAG . "," . TABLE_META . " WHERE " . TABLE_TAG . ".id=" . TABLE_META . ".id AND uid=? AND piler_id=?", array($uid, $id));
if(isset($query->row['tag'])) { return $query->row['tag']; }
return '';
}
public function add_message_tag($id = '', $uid = 0, $tag = '') {
if($id == '' || $uid <= 0) { return 0; }
$query = $this->db->query("SELECT `id` FROM " . TABLE_META . " WHERE piler_id=?", array($id));
if(isset($query->row['id']) && $query->row['id'] > 0) {
$id = $query->row['id'];
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));
}
}
return 1;
}
return 0;
}
}
?>

View File

@ -0,0 +1,502 @@
<?php
class ModelSearchSearch extends Model {
public function search_messages($data = array(), $search_type = SIMPLE_SEARCH, $page = 0) {
$one_page_of_ids = array();
$total_hits = 0;
$sort = "sent";
$order = "DESC";
$sortorder = "ORDER BY sent DESC";
$cache_key = "";
$q = "";
$s = "";
while(list($k,$v) = each($data)) {
if($v) { $s .= '&' . $k . '=' . $v; }
}
if($s) { $s = substr($s, 1, strlen($s)); }
AUDIT(ACTION_SEARCH, '', '', 0, $s);
if($data['sort'] == "sent") { $sort = "sent"; }
if($data['sort'] == "size") { $sort = "size"; }
if($data['sort'] == "from") { $sort = "from"; }
if($data['sort'] == "subj") { $sort = "subject"; }
if($data['order'] == 1) { $order = "ASC"; }
$sortorder = "ORDER BY `$sort` $order";
$m = array();
if(MEMCACHED_ENABLED) {
$cache_key = $this->make_cache_file_name($data, $sortorder);
$memcache = Registry::get('memcache');
$m = $memcache->get($cache_key);
}
if(isset($m['ids'])) {
$all_ids = $m['ids'];
} else {
if($search_type == SIMPLE_SEARCH) {
$conditions = $this->assemble_simple_query_conditions($data);
}
else {
$conditions = $this->assemble_advanced_query_conditions($data);
}
$all_ids = $this->query_all_possible_IDs($data, $conditions, $sort, $order, $sortorder, $cache_key);
}
$total_hits = count($all_ids);
$data['page_len'] = get_page_length();
if($total_hits > 0) {
$i = 0;
foreach($all_ids as $id) {
if($i >= $data['page_len'] * $page && $i < $data['page_len'] * ($page+1) ) {
array_push($one_page_of_ids, $id);
if($q) { $q .= ",?"; } else { $q = "?"; }
}
$i++;
}
}
return array($total_hits, $this->get_meta_data($one_page_of_ids, $q, $sortorder));
}
private function assemble_advanced_query_conditions($data = array()) {
$f1 = $f2 = $t1 = $t2 = $fd = $td = '';
$incoming = $outgoing = '';
$email = $match = '';
$n_fc = $n_tc = 0;
$data['f_from'] = $this->fix_email_address_for_sphinx($data['f_from']);
$data['o_from'] = $this->fix_email_address_for_sphinx($data['o_from']);
$data['f_to'] = $this->fix_email_address_for_sphinx($data['f_to']);
$data['o_to'] = $this->fix_email_address_for_sphinx($data['o_to']);
$data['from_domain'] = $this->fix_email_address_for_sphinx($data['from_domain']);
$data['to_domain'] = $this->fix_email_address_for_sphinx($data['to_domain']);
$data['body'] = $this->fixup_sphinx_operators($data['body']);
$data['subject'] = $this->fixup_sphinx_operators($data['subject']);
if(Registry::get('admin_user') == 1 || Registry::get('auditor_user') == 1) {
if($data['f_from']) { $f1 .= "|" . $data['f_from']; $n_fc++; }
if($data['o_from']) { $f1 .= "|" . $data['o_from']; $n_fc++; }
if($data['from_domain']) { $fd .= "(@fromdomain " . $data['from_domain'] . ")"; $n_fc++; }
if($data['f_to']) { $t1 .= "|" . $data['f_to']; $n_tc++; }
if($data['o_to']) { $t1 .= "|" . $data['o_to']; $n_tc++; }
if($data['to_domain']) { $td .= "(@todomain " . $data['to_domain'] . ")"; $n_tc++; }
if($f1) { $f1 = "(@from " . substr($f1, 1, strlen($f1)) . ")"; }
if($t1) { $t1 = "(@to " . substr($t1, 1, strlen($t1)) . ")"; }
}
else {
$all_your_addresses = $this->get_all_your_address();
if($data['f_from']) { $f1 = "(@from " . $data['f_from'] . " @to $all_your_addresses)"; $n_fc++; }
if($data['o_from']) { $f2 = "(@from " . $data['o_from'] . ")"; $n_fc++; }
if($data['from_domain']) { $fd = "(@fromdomain " . $data['from_domain'] . " @to $all_your_addresses)"; $n_fc++; }
if($data['f_to']) { $t1 = "(@to " . $data['f_to'] . " @from $all_your_addresses)"; $n_tc++; }
if($data['o_to']) { $t2 = "(@to " . $data['o_to'] . ")"; $n_tc++; }
if($data['to_domain']) { $td = "(@todomain " . $data['to_domain'] . " @from $all_your_addresses)"; $n_tc++; }
if($n_fc == 0 && $n_tc == 0 && $data['from_domain'] == '' && $data['to_domain'] == '') {
if($data['direction'] == 2) {
$f1 = " (@from " . $all_your_addresses . ")";
} else {
$t1 = " (@to " . $all_your_addresses . ")";
}
}
}
if($f1) { $incoming .= "|$f1"; }
if($f2) { $incoming .= "|$f2"; }
if($fd) { $incoming .= "|$fd"; }
if($t1) { $outgoing .= "|$t1"; }
if($t2) { $outgoing .= "|$t2"; }
if($td) { $outgoing .= "|$td"; }
if($incoming) { $incoming = substr($incoming, 1, strlen($incoming)); if($n_fc > 1) { $incoming = "($incoming)"; } }
if($outgoing) { $outgoing = substr($outgoing, 1, strlen($outgoing)); if($n_tc > 1) { $outgoing = "($outgoing)"; } }
if($incoming) {
$email = $incoming;
if($outgoing) { $email = $incoming . " & " . $outgoing; }
} else if($outgoing) {
$email = $outgoing;
}
if($email) { $match = $email; }
if($data['body']) { if($match) { $match .= " & "; } $match .= "(@body " . $data['body'] . ") "; }
if($data['subject']) { if($match) { $match .= " & "; } $match .= "(@subject " . $data['subject'] . ") "; }
if($data['attachment_type']) { if($match) { $match .= " & "; } $match .= "(@attachment_types " . $data['attachment_type'] . ") "; }
return $match;
}
private function assemble_simple_query_conditions($data = array(), $sort = 'sent', $order = 'DESC', $sortorder = '', $cache_key = '') {
$email = $match = "";
if(Registry::get('admin_user') == 0 && Registry::get('auditor_user') == 0) {
$all_your_addresses = $this->get_all_your_address();
if(isset($data['from'])) { $data['from'] = fix_email_address($data['from']); }
if(isset($data['to'])) { $data['to'] = fix_email_address($data['to']); }
// missing from address
if(!isset($data['from'])) {
if(isset($data['to']) && !strstr($data['to'], '@')) { $email = "@from $all_your_addresses @todomain " . $this->fix_email_address_for_sphinx($data['to']); }
else if(!isset($data['to'])) { $email = "@to $all_your_addresses"; }
else if(!in_array($data['to'], $_SESSION['emails'])) { $email = "@from $all_your_addresses @to " . $this->fix_email_address_for_sphinx($data['to']); }
else { $email = "@to " . $this->fix_email_address_for_sphinx($data['to']); }
}
// missing to address
else if(!isset($data['to'])) {
if(isset($data['from']) && !strstr($data['from'], '@')) { $email = "@to $all_your_addresses @fromdomain " . $this->fix_email_address_for_sphinx($data['from']); }
else if(!in_array($data['from'], $_SESSION['emails'])) { $email = "@to $all_your_addresses @from " . $this->fix_email_address_for_sphinx($data['from']); }
else { $email = "@from " . $this->fix_email_address_for_sphinx($data['from']); }
}
else if(isset($data['from']) && isset($data['to'])) {
if(
(!in_array($data['from'], $_SESSION['emails']) && in_array($data['to'], $_SESSION['emails'])) ||
(!in_array($data['to'], $_SESSION['emails']) && in_array($data['from'], $_SESSION['emails']))
) {
$email = "@from " . $this->fix_email_address_for_sphinx($data['from']) . " @to " . $this->fix_email_address_for_sphinx($data['to']);
}
else {
$email = " @to INVALID ";
}
}
}
else {
if(isset($data['from'])) {
if(strstr($data['from'], '@')) { $match .= " @from " . $this->fix_email_address_for_sphinx($data['from']); }
else { $match .= " @fromdomain " . $this->fix_email_address_for_sphinx($data['from']); }
}
if(isset($data['to'])) {
if(strstr($data['to'], '@')) { $match .= " @to " . $this->fix_email_address_for_sphinx($data['to']); }
else { $match .= " @todomain " . $this->fix_email_address_for_sphinx($data['to']); }
}
}
if(isset($data['subject'])) {
$data['subject'] = $this->fixup_sphinx_operators($data['subject']);
$match .= " @(subject,body) " . $data['subject'];
}
if($email) { $match = " $email " . $match; }
return $match;
}
private function query_all_possible_IDs($data = array(), $conditions = '', $sort = 'sent', $order = 'DESC', $sortorder = '', $cache_key = '') {
$ids = array();
$direction = $size = '';
$tag_id_list = '';
if($data['sort'] == 'from' || $data['sort'] == 'subj') { $sortorder = ''; }
$date = fixup_date_condition('sent', $data['date1'], $data['date2']);
if($date) { $date .= " AND "; }
if(isset($data['direction']) && $data['direction'] != '') { $direction = "direction = " . $data['direction'] . " AND "; }
if(isset($data['size']) && $data['size']) {
$data['size'] = preg_replace("/\s/", "", $data['size']);
if(preg_match("/^(\>|\<)\={0,}\d{1,}$/", $data['size'])) { $size = "size " . $data['size'] . " AND "; }
}
if($data['tag']) {
$tag_id_list = " AND id IN (0";
$data['tag'] = $this->fixup_sphinx_operators($data['tag']);
$aa = $this->sphx->query("SELECT id FROM " . SPHINX_TAG_INDEX . " WHERE uid=" . $_SESSION['uid'] . " AND MATCH('@tag " . $data['tag'] . " ') ");
foreach($aa->rows as $a) {
$tag_id_list .= "," . $a['id'];
}
$tag_id_list .= ") ";
}
$query = $this->sphx->query("SELECT id FROM " . SPHINX_MAIN_INDEX . " WHERE $date $direction $size MATCH('$conditions') $tag_id_list $sortorder LIMIT 0," . MAX_SEARCH_HITS);
//print $query->query; print "<p>" . $query->exec_time . "</p>\n";
/*
* build an id list
*/
$q = "";
foreach($query->rows as $a) {
array_push($ids, $a['id']);
if($q) { $q .= ",?"; }
else { $q = "?"; }
}
/*
* if the query was requested to be sorted by sender or subject, then sphinx cannot do
* that, so we assemble the list of all sphinx IDs matching the query
*/
if($data['sort'] == 'from' || $data['sort'] == 'subj') {
$query = $this->db->query("SELECT id FROM " . TABLE_META . " WHERE id IN ($q) ORDER BY `$sort` $order", $ids);
//print $query->query . ", exec: " . $query->exec_time . "<p/>\n";
$ids = array();
foreach($query->rows as $q) {
array_push($ids, $q['id']);
}
}
if(MEMCACHED_ENABLED && $cache_key) {
$memcache = Registry::get('memcache');
$memcache->add($cache_key, array('ts' => time(), 'total_hits' => count($ids), 'ids' => $ids), 0, MEMCACHED_TTL);
}
return $ids;
}
private function get_meta_data($ids = array(), $q = '', $sortorder = '') {
$messages = array();
$tag = array();
if(count($ids) == 0) return $messages;
$query = $this->db->query("SELECT `id`, `from`, `subject`, `piler_id`, `reference`, `size`, `spam`, `sent`, `arrived`, `attachments` FROM `" . TABLE_META . "` WHERE `id` IN ($q) $sortorder", $ids);
if(isset($query->rows)) {
$tags = $this->db->query("SELECT `id`, `tag` FROM `" . TABLE_TAG . "` WHERE `id` IN ($q)", $ids);
foreach ($tags->rows as $t) {
$tag[$t['id']] = $t['tag'];
}
$lang = Registry::get('language');
foreach($query->rows as $m) {
$m['shortfrom'] = make_short_string($m['from'], MAX_CGI_FROM_SUBJ_LEN);
if($m['subject'] == "") { $m['subject'] = "&lt;" . $lang->data['text_no_subject'] . "&gt;"; }
$m['subject'] = escape_gt_lt_quote_symbols($m['subject']);
$m['shortsubject'] = make_short_string($m['subject'], MAX_CGI_FROM_SUBJ_LEN);
$m['date'] = date(SEARCH_HIT_DATE_FORMAT, $m['sent']);
$m['size'] = nice_size($m['size']);
/*
* verifying 20 messages takes some time, still it's useful
*/
if(ENABLE_ON_THE_FLY_VERIFICATION == 1) {
$m['verification'] = $this->model_search_message->verify_message($m['piler_id']);
}
if(isset($tag[$m['id']])) { $m['tag'] = $tag[$m['id']]; } else { $m['tag'] = ''; }
array_push($messages, $m);
}
}
return $messages;
}
public function get_message_recipients($id = '') {
$rcpt = array();
if(Registry::get('admin_user') == 0 && Registry::get('auditor_user') == 0) { return $rcpt; }
$query = $this->db->query("SELECT `to` FROM " . VIEW_MESSAGES . " WHERE piler_id=?", array($id));
foreach($query->rows as $q) {
array_push($rcpt, $q['to']);
}
return $rcpt;
}
private function get_all_your_address() {
$s = '';
while(list($k, $v) = each($_SESSION['emails'])) {
if($s) { $s .= '| ' . $this->fix_email_address_for_sphinx($v); }
else { $s = $this->fix_email_address_for_sphinx($v); }
}
return $s;
}
public function check_your_permission_by_piler_id($id = '') {
$q = '';
$arr = $a = array();
if($id == '') { return 0; }
if(Registry::get('admin_user') == 1 || Registry::get('auditor_user') == 1) { return 1; }
array_push($arr, $id);
while(list($k, $v) = each($_SESSION['emails'])) {
if(validemail($v) == 1) {
$q .= ",?";
array_push($a, $v);
}
}
$q = preg_replace("/^\,/", "", $q);
$arr = array_merge($arr, $a, $a);
$query = $this->db->query("SELECT * FROM " . VIEW_MESSAGES . " WHERE piler_id=? AND ( `from` IN ($q) OR `to` IN ($q) )", $arr);
if(isset($query->row['id'])) { return 1; }
return 0;
}
public function remove_message($id = '') {
if($id == '') { return 0; }
if(Registry::get('admin_user') == 0) { return 0; }
$query = $this->db->query("UPDATE " . TABLE_META . " SET deleted=1 WHERE piler_id=?", array($id));
return $this->db->countAffected();
}
private function fix_email_address_for_sphinx($email = '') {
return preg_replace("/[\@\.\+\-]/", "X", $email);
}
public function get_search_terms() {
$query = $this->db->query("SELECT term FROM " . TABLE_SEARCH . " where email=? ORDER BY ts DESC", array($_SESSION['email']));
if(isset($query->rows)) { return $query->rows; }
return array();
}
public function add_search_term($term = '') {
if($term == '') { return 0; }
if($this->update_search_term($term) == 0) {
AUDIT(ACTION_SAVE_SEARCH, '', '', '', $term);
$query = $this->db->query("INSERT INTO " . TABLE_SEARCH . " (email, ts, term) VALUES(?,?,?)", array($_SESSION['email'], time(), $term));
}
return 1;
}
public function update_search_term($term = '') {
if($term == '') { return 0; }
AUDIT(ACTION_SEARCH, '', '', '', $term);
$query = $this->db->query("UPDATE " . TABLE_SEARCH . " SET ts=? WHERE term=? AND email=?", array(time(), $term, $_SESSION['email']));
return $this->db->countAffected();
}
private function fixup_sphinx_operators($s = '') {
if($s == '') { return $s; }
$s = preg_replace("/ OR /", "|", $s);
$a = explode(" ", $s);
$s = '';
while(list($k, $v) = each($a)) {
if(substr($v, 0, 4) == 'http') {
$v = preg_replace("/http(s){0,1}\:\/\//", "__URL__", $v);
$b = explode("/", $v);
$s .= ' ' . $this->fix_email_address_for_sphinx($b[0]);
}
else {
$s .= ' ' . $v;
}
}
return $s;
}
private function make_cache_file_name($data = array(), $sortorder = '') {
return sha1($_SESSION['email'] . "/" . join("*", $data) . "-" . (NOW - NOW % 3600) . "-" . $sortorder);
}
}
?>