piler/webui/model/search/search.php

935 lines
30 KiB
PHP
Raw Normal View History

2012-02-08 23:14:28 +01:00
<?php
class ModelSearchSearch extends Model {
2012-09-06 15:27:20 +02:00
public function search_messages($data = array(), $page = 0) {
2012-02-08 23:14:28 +01:00
$one_page_of_ids = array();
$total_hits = 0;
2013-12-23 12:12:18 +01:00
$total_found = 0;
2012-02-08 23:14:28 +01:00
$sort = "sent";
$order = "DESC";
$sortorder = "ORDER BY sent DESC";
$cache_key = "";
$q = "";
$s = "";
2012-03-10 14:52:50 +01:00
$all_ids_csv = "";
2012-02-08 23:14:28 +01:00
2013-09-30 16:40:01 +02:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
while(list($k,$v) = each($data)) {
if($v) { if(is_array($v)) { $v = implode(" ", $v); } $s .= '&' . $k . '=' . $v; }
2012-02-08 23:14:28 +01:00
}
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";
2015-08-11 15:16:13 +02:00
// TODO: check if data is cached
2012-02-08 23:14:28 +01:00
2015-08-11 15:16:13 +02:00
if(isset($data['ref']) && $data['ref']){
list ($total_hits, $ids) = $this->query_all_possible_IDs_by_reference($data['ref'], $page);
}
else {
list ($total_hits, $ids) = $this->get_results($data, $sort, $order, $sortorder, $page);
2012-02-08 23:14:28 +01:00
}
2015-08-11 15:16:13 +02:00
$current_hits = count($ids);
2012-02-08 23:14:28 +01:00
2015-08-11 15:16:13 +02:00
if($current_hits > 0) {
$session->set('last_search', serialize($ids));
2013-09-30 16:40:01 +02:00
} else {
$session->set('last_search', '');
}
2015-08-11 15:16:13 +02:00
return array($current_hits, $total_hits, implode(",", $ids), $this->get_meta_data($ids, $sortorder));
2012-02-08 23:14:28 +01:00
}
private function assemble_email_address_filter() {
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
if(Registry::get('auditor_user') == 1) {
2013-01-05 16:42:36 +01:00
if(RESTRICTED_AUDITOR == 1) {
$session_domains = $this->fix_email_address_for_sphinx($session->get('auditdomains'));
2013-01-05 16:42:36 +01:00
$sd = $this->fix_email_address_for_sphinx($session->get('domain'));
2012-02-08 23:14:28 +01:00
2015-04-16 13:54:57 +02:00
foreach ($session_domains as $d) {
if($d) { $sd .= '|' . $d; }
}
$sd = preg_replace("/^\|/", "", $sd);
return sprintf(" (%s %s | %s %s ) ", TODOMAIN_TOKEN, $sd, FROMDOMAIN_TOKEN, $sd);
}
else { return ""; }
2012-09-06 15:27:20 +02:00
}
2012-02-08 23:14:28 +01:00
if(ENABLE_FOLDER_RESTRICTIONS == 1) { return ""; }
2012-02-08 23:14:28 +01:00
$all_your_addresses = $this->get_all_your_address("emails");
$all_your_wildcard_domains = $this->get_all_your_address("wildcard_domains");
if($all_your_wildcard_domains) {
return sprintf(" ( (%s %s) | (%s %s) | (%s %s) | (%s %s) ) ", FROM_TOKEN, $all_your_addresses, TO_TOKEN, $all_your_addresses, FROMDOMAIN_TOKEN, $all_your_wildcard_domains, TODOMAIN_TOKEN, $all_your_wildcard_domains);
} else {
return sprintf(" ( (%s %s) | (%s %s) ) ", FROM_TOKEN, $all_your_addresses, TO_TOKEN, $all_your_addresses);
}
2012-09-06 15:27:20 +02:00
}
2015-08-11 15:16:13 +02:00
private function get_results($data = array(), $sort = 'sent', $order = 'DESC', $sortorder = '', $page = 0) {
2012-09-06 15:27:20 +02:00
$ids = array();
$__folders = array();
$match = '';
$direction = $attachment = $size = $folders = '';
$tag_id_list = '';
$a = "";
$id = "";
2015-08-11 15:16:13 +02:00
$offset = 0;
$total_sphx_hits = $num_rows = 0;
$fields = ["@(subject,body)", FROM_TOKEN, TO_TOKEN, "@subject", "@body", "@attachment_types"];
2012-02-08 23:14:28 +01:00
2015-08-11 15:16:13 +02:00
$pagelen = get_page_length();
$offset = $page * $pagelen;
$emailfilter = $this->assemble_email_address_filter();
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
$i = 0;
while(list($k, $v) = each($data['match'])) {
if($v == "@attachment_types") {
list($k, $v) = each($data['match']);
$i++;
if($v == "any") {
$data['match'][$i-1] = "";
$data['match'][$i] = "";
2014-10-22 10:17:28 +02:00
}
if($a == '') {
$a = "attachments > 0 AND ";
}
}
if(substr($v, 0, 7) == "http://") { $v = preg_replace("/\./", "X", $v); $data['match'][$i] = preg_replace("/http\:\/\//", "__URL__", $v); }
if(!in_array($v, $fields) && $i > 0 && strchr($v, "@")) {
2012-02-08 23:14:28 +01:00
if(substr($v, 0, 1) == "@") {
$v = substr($v, 1, strlen($v)-1);
if($data['match'][$i-1] == FROM_TOKEN) { $data['match'][$i-1] = FROMDOMAIN_TOKEN; }
if($data['match'][$i-1] == TO_TOKEN) { $data['match'][$i-1] = TODOMAIN_TOKEN; }
}
$data['match'][$i] = $this->fix_email_address_for_sphinx($v);
}
$i++;
}
2012-02-08 23:14:28 +01:00
2015-06-02 13:17:27 +02:00
if(SEARCH_QUERY_QUOTING == 1) {
$quoting = 0;
for($i=2; $i<count($data['match']); $i++) {
if(preg_match("/^\"/", $data['match'][$i])) {
$quoting = 1;
}
if($quoting == 0) {
$data['match'][$i] = '"' . $data['match'][$i] . '"';
}
if(preg_match("/\"$/", $data['match'][$i])) {
$quoting = 0;
}
}
}
$match = implode(" ", $data['match']);
2012-02-08 23:14:28 +01:00
if(Registry::get('auditor_user') == 1 && RESTRICTED_AUDITOR == 0 && $data['raw']) {
$match .= $data['raw'];
}
if($emailfilter) {
if(strlen($match) > 2) { $match = "( $match ) & $emailfilter"; }
else { $match = $emailfilter; }
2012-09-06 15:27:20 +02:00
}
2012-02-08 23:14:28 +01:00
2013-10-07 15:43:50 +02:00
if($match == " ") { $match = ""; }
2012-02-08 23:14:28 +01:00
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(isset($data['attachment_type']) && strstr($data['attachment_type'], 'any')) { $a = "attachments > 0 AND "; }
2012-09-06 15:27:20 +02:00
else if(isset($data['has_attachment']) && $data['has_attachment'] == 1) { $attachment = "attachments > 0 AND "; }
2012-02-08 23:14:28 +01:00
2013-09-09 15:10:30 +02:00
if(isset($data['id']) && $data['id']) {
$data['id'] = preg_replace("/ /", "," , substr($data['id'], 1, strlen($data['id'])));
$id = " id IN (" . $data['id'] . ") AND ";
}
2012-09-06 15:27:20 +02:00
if(ENABLE_FOLDER_RESTRICTIONS == 1) {
$s = explode(" ", $data['folders']);
while(list($k,$v) = each($s)) {
2013-11-18 19:24:33 +01:00
if(in_array($v, $session->get("folders"))) {
2012-09-06 15:27:20 +02:00
array_push($__folders, $v);
}
2012-02-08 23:14:28 +01:00
}
2012-09-14 11:04:17 +02:00
if(count($__folders) > 0) {
$folders = "folder IN (" . implode(",", $__folders) . ") AND ";
}
2013-03-31 14:50:21 +02:00
else {
2013-11-18 19:24:33 +01:00
$folders = "folder IN (" . implode(",", $session->get("folders")) . ") AND ";
2013-03-31 14:50:21 +02:00
}
2012-09-06 15:27:20 +02:00
}
if(isset($data['tag']) && $data['tag']) {
2015-08-11 15:16:13 +02:00
list ($total_found, $num_rows, $id_list) = $this->get_sphinx_id_list($data['tag'], SPHINX_TAG_INDEX, 'tag', $page);
$query = $this->sphx->query("SELECT id FROM " . SPHINX_MAIN_INDEX . " WHERE $folders id IN ($id_list) $sortorder LIMIT 0,$pagelen OPTION max_matches=" . MAX_SEARCH_HITS);
2012-09-06 15:27:20 +02:00
}
else if(isset($data['note']) && $data['note']) {
2015-08-11 15:16:13 +02:00
list ($total_found, $num_rows, $id_list) = $this->get_sphinx_id_list($data['note'], SPHINX_NOTE_INDEX, 'note', $page);
$query = $this->sphx->query("SELECT id FROM " . SPHINX_MAIN_INDEX . " WHERE $folders id IN ($id_list) $sortorder LIMIT 0,$pagelen OPTION max_matches=" . MAX_SEARCH_HITS);
2012-02-10 14:06:00 +01:00
}
2015-09-18 14:56:09 +02:00
else if(ENABLE_FOLDER_RESTRICTIONS == 1 && isset($data['extra_folders']) && strlen($data['extra_folders']) > 0) {
$query = $this->sphx->query("SELECT id FROM " . SPHINX_MAIN_INDEX . " WHERE $a $id $date $attachment $direction $size folder IN (" . preg_replace("/ /", ",", $data['extra_folders']) . ") AND MATCH('$match') $sortorder LIMIT $offset,$pagelen OPTION max_matches=" . MAX_SEARCH_HITS);
$total_found = $query->total_found;
$num_rows = $query->num_rows;
2012-09-15 15:30:35 +02:00
}
2012-02-10 14:06:00 +01:00
else {
2015-08-11 15:16:13 +02:00
$query = $this->sphx->query("SELECT id FROM " . SPHINX_MAIN_INDEX . " WHERE $a $id $date $attachment $direction $size $folders MATCH('$match') $sortorder LIMIT $offset,$pagelen OPTION max_matches=" . MAX_SEARCH_HITS);
$total_found = $query->total_found;
$num_rows = $query->num_rows;
2012-02-08 23:14:28 +01:00
}
/*
* build an id list
*/
$q = "";
2012-07-06 15:02:23 +02:00
if(isset($query->rows)) {
foreach($query->rows as $a) {
2015-04-13 16:23:52 +02:00
if(isset($a['mid'])) { array_push($ids, $a['mid']); }
else { array_push($ids, $a['id']); }
2012-02-08 23:14:28 +01:00
2012-07-06 15:02:23 +02:00
if($q) { $q .= ",?"; }
else { $q = "?"; }
}
2012-02-08 23:14:28 +01:00
}
/*
* 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') {
2013-12-23 12:12:18 +01:00
$fs_query = $this->db->query("SELECT id FROM " . TABLE_META . " WHERE id IN ($q) ORDER BY `$sort` $order", $ids);
2012-02-08 23:14:28 +01:00
$ids = array();
2013-12-23 12:12:18 +01:00
foreach($fs_query->rows as $q) {
2012-02-08 23:14:28 +01:00
array_push($ids, $q['id']);
}
}
2015-08-11 15:16:13 +02:00
// TODO: add caching if necessary
2012-02-08 23:14:28 +01:00
2013-12-23 12:12:18 +01:00
return array($total_found, $ids);
2012-02-08 23:14:28 +01:00
}
2015-08-11 15:16:13 +02:00
private function query_all_possible_IDs_by_reference($reference = '', $page = 0) {
2012-02-10 14:06:00 +01:00
$ids = array();
2015-08-11 15:16:13 +02:00
$offset = 0;
2012-02-10 14:06:00 +01:00
if($reference == '') { return $ids; }
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2015-08-11 15:16:13 +02:00
$pagelen = get_page_length();
$offset = $page * $pagelen;
$query = $this->db->query("SELECT id FROM " . TABLE_META . " WHERE message_id=? OR reference=? ORDER BY id DESC LIMIT $offset,$pagelen", array($reference, $reference));
2012-02-10 14:06:00 +01:00
foreach($query->rows as $q) {
2014-08-04 22:19:03 +02:00
if($this->check_your_permission_by_id($q['id'])) {
array_push($ids, $q['id']);
}
2012-02-10 14:06:00 +01:00
}
2012-09-06 15:27:20 +02:00
if(ENABLE_FOLDER_RESTRICTIONS == 1) {
$query = $this->sphx->query("SELECT id, folder FROM " . SPHINX_MAIN_INDEX . " WHERE id IN (" . implode(",", $ids) . ")");
$ids = array();
foreach($query->rows as $q) {
2013-11-18 19:24:33 +01:00
if(isset($q['folder']) && in_array($q['folder'], $session->get("folders"))) { array_push($ids, $q['id']); }
2012-09-06 15:27:20 +02:00
}
}
2013-12-23 12:12:18 +01:00
$total_found = count($ids);
2012-09-06 15:27:20 +02:00
2015-08-11 15:16:13 +02:00
if($total_found >= $pagelen) {
$query = $this->db->query("SELECT count(*) AS num FROM " . TABLE_META . " WHERE message_id=? OR reference=?", array($reference, $reference));
$total_found = $query->row['num'];
2012-02-10 14:06:00 +01:00
}
2015-08-11 15:16:13 +02:00
// TODO: add caching if necessary
2013-12-23 12:12:18 +01:00
return array($total_found, $ids);
2012-02-10 14:06:00 +01:00
}
2014-01-23 21:39:47 +01:00
public function preprocess_post_expert_request($data = array()) {
$token = 'match';
$ndate = 0;
$match = array();
$a = array(
'date1' => '',
'date2' => '',
'direction' => '',
'size' => '',
'attachment_type' => '',
'tag' => '',
'note' => '',
'ref' => '',
'folders' => '',
'extra_folders' => '',
'id' => '',
'raw' => '',
2014-01-23 21:39:47 +01:00
'match' => array()
);
if(!isset($data['search'])) { return $a; }
2014-06-12 15:07:34 +02:00
$s = preg_replace("/https{0,1}:/", "httpX", $data['search']);
$s = preg_replace("/:/", ": ", $s);
2014-01-23 21:39:47 +01:00
$s = preg_replace("/,/", " ", $s);
$s = preg_replace("/\(/", "( ", $s);
$s = preg_replace("/\)/", ") ", $s);
$s = preg_replace("/OR/", "|", $s);
$s = preg_replace("/AND/", "", $s);
$s = preg_replace("/\s{1,}/", " ", $s);
2014-06-12 15:07:34 +02:00
$s = preg_replace("/httpX/", "http:", $s);
2014-01-23 21:39:47 +01:00
$b = explode(" ", $s);
while(list($k, $v) = each($b)) {
if($v == '') { continue; }
if($v == 'from:') { $token = 'match'; $a['match'][] = FROM_TOKEN; continue; }
else if($v == 'to:') { $token = 'match'; $a['match'][] = TO_TOKEN; continue; }
2014-01-23 21:39:47 +01:00
else if($v == 'subject:') { $token = 'match'; $a['match'][] = '@subject'; continue; }
else if($v == 'body:') { $token = 'match'; $a['match'][] = '@body'; continue; }
else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; }
else if($v == 'attachment:' || $v == 'a:') { $token = 'match'; $a['match'][] = '@attachment_types'; continue; }
else if(in_array($v, ['size:', 'date1:', 'date2:', 'tag:', 'note:', 'ref:', 'id:', 'raw:'])) {
$token = substr($v, 0, strlen($v)-1); continue;
}
2015-03-19 15:05:14 +01:00
else if($token != 'date1' && $token != 'date2') {
2014-01-23 21:39:47 +01:00
if(preg_match("/\d{4}\-\d{1,2}\-\d{1,2}/", $v) || preg_match("/\d{1,2}\/\d{1,2}\/\d{4}/", $v)) {
$ndate++;
$a["date$ndate"] = $v;
}
}
if($token == 'match') { $a['match'][] = $v; }
else if(in_array($token, ['date1', 'date2', 'ref', 'tag', 'note', 'id', 'raw'])) { $a[$token] .= ' ' . $v; }
2014-01-23 21:39:47 +01:00
else if($token == 'direction') {
if($v == 'inbound') { $a['direction'] = "0"; }
else if($v == 'outbound') { $a['direction'] = 2; }
else if($v == 'internal') { $a['direction'] = 1; }
}
else if($token == 'size') {
$o = substr($v, 0, 1);
if($o == '<' || $o == '>') {
$v = substr($v, 1, strlen($v));
$o1 = substr($v, 0, 1);
if($o1 == '=') {
$v = substr($v, 1, strlen($v));
$o .= $o1;
}
}
else { $o = ''; }
$s = explode("k", $v);
if($s[0] != $v) { $v = $s[0] * 1000; }
$s = explode("M", $v);
if($s[0] != $v) { $v = $s[0] * 1000000; }
$a['size'] .= ' ' . $o . $v;
}
}
$a['sort'] = $data['sort'];
$a['order'] = $data['order'];
return $a;
}
2015-08-11 15:16:13 +02:00
private function get_sphinx_id_list($s = '', $sphx_table = '', $field = '', $page = 0) {
2012-09-06 15:27:20 +02:00
$id_list = '';
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2015-08-11 15:16:13 +02:00
$pagelen = get_page_length();
$offset = $page * $pagelen;
2012-09-06 15:27:20 +02:00
$s = $this->fixup_sphinx_operators($s);
2015-08-11 15:16:13 +02:00
$q = $this->sphx->query("SELECT iid FROM $sphx_table WHERE uid=" . $session->get("uid") . " AND MATCH('@$field $s') LIMIT $offset,$pagelen OPTION max_matches=" . MAX_SEARCH_HITS);
2012-09-06 15:27:20 +02:00
foreach($q->rows as $a) {
2015-04-09 16:57:15 +02:00
$id_list .= "," . $a['iid'];
2012-09-06 15:27:20 +02:00
}
if($id_list) { $id_list = substr($id_list, 1, strlen($id_list)); }
2014-09-22 11:21:40 +02:00
if($id_list == '') { $id_list = "-1"; }
2015-08-11 15:16:13 +02:00
return array($q->total_found, $q->num_rows, $id_list);
2012-09-06 15:27:20 +02:00
}
2015-08-11 15:16:13 +02:00
private function get_sphinx_id_list_by_extra_folders($extra_folders = '', $page = 0) {
2012-09-15 15:30:35 +02:00
$id_list = '';
$q = '';
$__folders = array();
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2015-08-11 15:16:13 +02:00
$pagelen = get_page_length();
$offset = $page * $pagelen;
2012-09-15 15:30:35 +02:00
$s = explode(" ", $extra_folders);
while(list($k,$v) = each($s)) {
2013-11-18 19:24:33 +01:00
if(in_array($v, $session->get("extra_folders")) && is_numeric($v)) {
2012-09-15 15:30:35 +02:00
array_push($__folders, $v);
if($q) { $q .= ",?"; }
else { $q = "?"; }
}
}
2015-08-11 15:16:13 +02:00
$q = $this->db->query("SELECT iid FROM " . TABLE_FOLDER_MESSAGE . " WHERE folder_id IN ($q) $offset,$pagelen", $__folders);
2012-09-15 15:30:35 +02:00
foreach($q->rows as $a) {
2014-09-22 11:13:55 +02:00
$id_list .= "," . $a['iid'];
2012-09-15 15:30:35 +02:00
}
if($id_list) { $id_list = substr($id_list, 1, strlen($id_list)); }
2015-08-11 15:16:13 +02:00
return array($q->total_found, $q->num_rows, $id_list);
2012-09-15 15:30:35 +02:00
}
2015-08-11 15:16:13 +02:00
private function get_meta_data($ids = array(), $sortorder = '') {
2012-02-08 23:14:28 +01:00
$messages = array();
2013-01-14 14:46:34 +01:00
$rcpt = $srcpt = array();
2012-02-08 23:14:28 +01:00
$tag = array();
2012-09-06 15:27:20 +02:00
$note = array();
$private = [];
$deleted = [];
$marked_for_removal = [];
2015-08-11 15:16:13 +02:00
$q = '';
global $SUPPRESS_RECIPIENTS;
2012-02-08 23:14:28 +01:00
if(count($ids) == 0) return $messages;
2015-08-11 15:16:13 +02:00
// TODO: check if data in cache
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2015-08-11 15:16:13 +02:00
$q = str_repeat(",?", count($ids));
$q = substr($q, 1, strlen($q));
2013-01-14 14:46:34 +01:00
$query = $this->db->query("SELECT `id`, `to` FROM `" . TABLE_RCPT . "` WHERE `id` IN ($q)", $ids);
if(isset($query->rows)) {
foreach($query->rows as $r) {
if(!isset($rcpt[$r['id']])) { $rcpt[$r['id']] = []; }
if(Registry::get('auditor_user') == 1 || !in_array($r['to'], $SUPPRESS_RECIPIENTS)) {
array_push($rcpt[$r['id']], $r['to']);
2013-01-14 14:46:34 +01:00
}
}
}
$query = $this->db->query("SELECT `id`, `from`, `subject`, `piler_id`, `reference`, `retained`, `size`, `spam`, `sent`, `arrived`, `attachments` FROM `" . TABLE_META . "` WHERE `id` IN ($q) $sortorder", $ids);
2012-02-08 23:14:28 +01:00
if(isset($query->rows)) {
$s = $this->db->query("SELECT `id` FROM `" . TABLE_PRIVATE . "` WHERE id IN ($q)", $ids);
2016-02-10 14:57:30 +01:00
foreach ($s->rows as $p) {
2016-02-10 14:57:30 +01:00
$private[$p['id']] = 1;
}
if(ENABLE_DELETE) {
$s = $this->db->query("SELECT `id`, `deleted` FROM `" . TABLE_DELETED . "` WHERE id IN ($q)", $ids);
foreach ($s->rows as $p) {
if($p['id'] == 1) {
$deleted[$p['id']] = 1;
}
$marked_for_removal[$p['id']] = 1;
}
}
2013-11-18 19:24:33 +01:00
array_unshift($ids, (int)$session->get("uid"));
2012-04-27 14:39:10 +02:00
$tags = $this->db->query("SELECT `id`, `tag` FROM `" . TABLE_TAG . "` WHERE `uid`=? AND `id` IN ($q)", $ids);
2012-02-08 23:14:28 +01:00
foreach ($tags->rows as $t) {
$tag[$t['id']] = $t['tag'];
}
2012-09-06 15:27:20 +02:00
$notes = $this->db->query("SELECT `id`, `note` FROM " . TABLE_NOTE . " WHERE `uid`=? AND `id` IN ($q)", $ids);
foreach ($notes->rows as $n) {
$note[$n['id']] = $n['note'];
}
2012-02-08 23:14:28 +01:00
$lang = Registry::get('language');
2013-04-09 15:02:10 +02:00
2012-02-08 23:14:28 +01:00
foreach($query->rows as $m) {
// We mark it as deleted even if it's only marked for removal
if(ENABLE_DELETE == 1 && ($m['retained'] < NOW || isset($deleted[$m['id']])) ) $m['deleted'] = 1; else $m['deleted'] = 0;
if(ENABLE_DELETE == 1 && isset($marked_for_removal[$m['id']])) $m['marked_for_removal'] = 1; else $m['marked_for_removal'] = 0;
2012-02-08 23:14:28 +01:00
$m['shortfrom'] = make_short_string($m['from'], MAX_CGI_FROM_SUBJ_LEN);
2013-02-11 20:24:19 +01:00
$m['from'] = escape_gt_lt_quote_symbols($m['from']);
isset($rcpt[$m['id']]) ? $m['to'] = $rcpt[$m['id']] : $m['to'] = '';
$m['shortto'] = make_short_string($this->get_preferred_recipient($rcpt[$m['id']]), MAX_CGI_FROM_SUBJ_LEN);
2013-02-11 20:24:19 +01:00
$m['to'] = escape_gt_lt_quote_symbols($m['to']);
2012-02-08 23:14:28 +01:00
2013-01-14 14:46:34 +01:00
2012-02-08 23:14:28 +01:00
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(DATE_TEMPLATE, $m['sent']);
2012-02-08 23:14:28 +01:00
$m['size'] = nice_size($m['size']);
2013-11-18 19:24:33 +01:00
in_array($m['from'], $session->get("emails")) ? $m['yousent'] = 1 : $m['yousent'] = 0;
2012-02-08 23:14:28 +01:00
/*
* verifying 20 messages takes some time, still it's useful
*/
if(ENABLE_ON_THE_FLY_VERIFICATION == 1) {
2013-10-30 23:40:34 +01:00
$data = $this->model_search_message->get_raw_message($m['piler_id']);
$m['verification'] = $this->model_search_message->verify_message($m['piler_id'], $data);
$data = '';
2012-02-08 23:14:28 +01:00
}
if(isset($tag[$m['id']])) { $m['tag'] = $tag[$m['id']]; } else { $m['tag'] = ''; }
2012-09-06 15:27:20 +02:00
if(isset($note[$m['id']])) { $m['note'] = $note[$m['id']]; } else { $m['note'] = ''; }
2012-02-08 23:14:28 +01:00
2013-04-06 11:32:17 +02:00
$m['note'] = preg_replace("/\"/", "*", strip_tags($m['note']));
$m['tag'] = preg_replace("/\"/", "*", strip_tags($m['tag']));
2013-02-11 20:24:19 +01:00
2016-02-10 14:57:30 +01:00
if(isset($private[$m['id']])) { $m['private'] = 1; } else { $m['private'] = 0; }
2012-02-08 23:14:28 +01:00
array_push($messages, $m);
}
2013-04-09 15:02:10 +02:00
2012-02-08 23:14:28 +01:00
}
2015-08-11 15:16:13 +02:00
// TODO: add caching if necessary
2012-12-04 22:34:15 +01:00
2012-02-08 23:14:28 +01:00
return $messages;
}
private function get_preferred_recipient($arr = []) {
$result = '';
$session = Registry::get('session');
$group_emails = $session->get('group_emails');
$user_emails = $session->get('user_emails');
if(count($arr) < 2 || (!$group_emails && !$user_emails) ) { return $arr[0]; }
foreach ($arr as $a) {
if($result == '' && in_array($a, $group_emails)) { $result = $a; }
if(in_array($a, $user_emails)) { $result = $a; }
}
if($result == '') { $result = $arr[0]; }
return $result;
}
public function get_message_addresses_in_my_domain($id = '') {
$addr = array();
$domains = array();
if(Registry::get('auditor_user') == 0) { return $addr; }
$query = $this->db->query("SELECT `domain` FROM " . TABLE_DOMAIN);
foreach($query->rows as $q) {
array_push($domains, $q['domain']);
}
$query = $this->db->query("SELECT `from`, `to` FROM " . VIEW_MESSAGES . " WHERE id=?", array($id));
2013-02-11 20:24:19 +01:00
if(isset($query->row['from'])) {
foreach ($domains as $domain) {
if(preg_match("/\@$domain$/", $query->row['from'])) { array_push($addr, $query->row['from']); }
}
}
foreach($query->rows as $q) {
$mydomain = 0;
foreach ($domains as $domain) {
if(preg_match("/\@$domain$/", $q['to'])) { $mydomain = 1; break; }
}
if($mydomain == 1) {
if(!in_array($q['to'], $addr)) { array_push($addr, $q['to']); }
}
}
return $addr;
}
private function get_all_your_address($session_var) {
2012-02-08 23:14:28 +01:00
$s = '';
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
$emails = $session->get($session_var);
2013-11-18 19:24:33 +01:00
if(!$emails) { return $s; }
2013-11-18 19:24:33 +01:00
while(list($k, $v) = each($emails)) {
2012-02-08 23:14:28 +01:00
if($s) { $s .= '| ' . $this->fix_email_address_for_sphinx($v); }
else { $s = $this->fix_email_address_for_sphinx($v); }
}
return $s;
}
private function get_wildcard_domains($arr=[]) {
$query_suffix = '';
$results = $arr;
$session = Registry::get('session');
$wildcard_domains = $session->get('wildcard_domains');
if($wildcard_domains) {
$q = str_repeat('?,', count($wildcard_domains));
$q = trim($q, ',');
$results = array_merge($results, $wildcard_domains, $wildcard_domains);
$query_suffix = "OR fromdomain IN ($q) OR todomain IN ($q)";
}
return [$results, $query_suffix];
}
2012-09-06 15:27:20 +02:00
public function check_your_permission_by_id($id = '') {
2012-02-08 23:14:28 +01:00
$q = '';
$arr = $a = array();
if($id == '') { return 0; }
if(Registry::get('data_officer') == 1) {
$query = $this->db->query("SELECT id FROM " . TABLE_DELETED . " WHERE deleted=-1 AND id=?", array($id));
if(!isset($query->row['id'])) {
return 0;
}
}
if((Registry::get('auditor_user') == 1 || Registry::get('data_officer') == 1) && RESTRICTED_AUDITOR == 0) { return 1; }
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
array_push($arr, $id);
if((Registry::get('auditor_user') == 1 || Registry::get('data_officer') == 1) && RESTRICTED_AUDITOR == 1) {
2013-11-18 19:24:33 +01:00
if(validdomain($session->get("domain")) == 1) {
2013-02-16 13:54:41 +01:00
$q .= ",?";
2013-11-18 19:24:33 +01:00
array_push($a, $session->get("domain"));
2013-02-16 13:54:41 +01:00
}
2013-11-18 19:24:33 +01:00
$auditdomains = $session->get("auditdomains");
while(list($k, $v) = each($auditdomains)) {
2013-02-16 13:54:41 +01:00
if(validdomain($v) == 1 && !in_array($v, $a)) {
2013-01-05 16:42:36 +01:00
$q .= ",?";
array_push($a, $v);
}
}
}
else {
2016-02-10 14:57:30 +01:00
$query = $this->db->query("SELECT id FROM " . TABLE_PRIVATE . " WHERE id=?", array($id));
if(isset($query->row['id'])) {
return 0;
}
2013-11-18 19:24:33 +01:00
$emails = $session->get("emails");
while(list($k, $v) = each($emails)) {
if(validemail($v) == 1) {
$q .= ",?";
array_push($a, $v);
}
2012-02-08 23:14:28 +01:00
}
}
2013-01-05 16:42:36 +01:00
$q = preg_replace("/^\,/", "", $q);
2012-02-08 23:14:28 +01:00
$arr = array_merge($arr, $a, $a);
2012-09-06 15:27:20 +02:00
if(ENABLE_FOLDER_RESTRICTIONS == 1) {
$query = $this->sphx->query("SELECT folder FROM " . SPHINX_MAIN_INDEX . " WHERE id=" . (int)$id);
2013-11-18 19:24:33 +01:00
if(isset($query->row['folder']) && in_array($query->row['folder'], $session->get("folders"))) { return 1; }
2012-09-06 15:27:20 +02:00
}
else {
if(Registry::get('auditor_user') == 1 && RESTRICTED_AUDITOR == 1) {
$query = $this->db->query("SELECT id FROM " . VIEW_MESSAGES . " WHERE id=? AND ( `fromdomain` IN ($q) OR `todomain` IN ($q) )", $arr);
} else {
[$arr, $query_suffix] = $this->get_wildcard_domains($arr);
$query = $this->db->query("SELECT id FROM " . VIEW_MESSAGES . " WHERE id=? AND ( `from` IN ($q) OR `to` IN ($q) $query_suffix )", $arr);
}
2012-09-06 15:27:20 +02:00
if(isset($query->row['id'])) { return 1; }
}
2012-02-08 23:14:28 +01:00
return 0;
}
2012-03-10 14:52:50 +01:00
public function check_your_permission_by_id_list($id = array()) {
$q = $q2 = '';
2016-02-16 15:37:34 +01:00
$arr = $parr = $a = $result = array();
2012-03-10 14:52:50 +01:00
2012-09-06 15:27:20 +02:00
if(count($id) < 1) { return $result; }
2012-03-10 14:52:50 +01:00
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-03-10 14:52:50 +01:00
$arr = $id;
for($i=0; $i<count($id); $i++) {
$q2 .= ",?";
}
2012-09-06 15:27:20 +02:00
2012-03-10 14:52:50 +01:00
$q2 = preg_replace("/^\,/", "", $q2);
if(Registry::get('auditor_user') == 1 && RESTRICTED_AUDITOR == 1) {
2013-11-18 19:24:33 +01:00
if(validdomain($session->get("domain")) == 1) {
2013-02-16 13:54:41 +01:00
$q .= ",?";
2013-11-18 19:24:33 +01:00
array_push($a, $session->get("domain"));
2013-02-16 13:54:41 +01:00
}
2013-11-18 19:24:33 +01:00
$auditdomains = $session->get("auditdomains");
while(list($k, $v) = each($auditdomains)) {
2013-02-16 13:54:41 +01:00
if(validdomain($v) == 1 && !in_array($v, $a)) {
2013-01-05 16:42:36 +01:00
$q .= ",?";
array_push($a, $v);
}
}
}
else {
if(Registry::get('auditor_user') == 0) {
2013-11-18 19:24:33 +01:00
$emails = $session->get("emails");
while(list($k, $v) = each($emails)) {
if(validemail($v) == 1) {
$q .= ",?";
array_push($a, $v);
}
}
2012-03-10 14:52:50 +01:00
}
}
2013-01-05 16:42:36 +01:00
$q = preg_replace("/^\,/", "", $q);
2012-03-10 14:52:50 +01:00
if(Registry::get('auditor_user') == 1 && RESTRICTED_AUDITOR == 0 && ENABLE_FOLDER_RESTRICTIONS == 0) {
2012-09-06 15:27:20 +02:00
$query = $this->db->query("SELECT id FROM `" . TABLE_META . "` WHERE `id` IN ($q2)", $arr);
}
else {
2012-03-10 14:52:50 +01:00
2012-09-06 15:27:20 +02:00
if(ENABLE_FOLDER_RESTRICTIONS == 1) {
$query = $this->sphx->query("SELECT id, folder FROM " . SPHINX_MAIN_INDEX . " WHERE id IN (" . implode(",", $id) . ")");
2012-09-06 15:27:20 +02:00
}
else {
$arr = array_merge($arr, $a, $a);
if(Registry::get('auditor_user') == 1 && RESTRICTED_AUDITOR == 1) {
$query = $this->db->query("SELECT id FROM `" . VIEW_MESSAGES . "` WHERE `id` IN ($q2) AND ( `fromdomain` IN ($q) OR `todomain` IN ($q) )", $arr);
} else {
2016-02-16 15:37:34 +01:00
$query = $this->db->query("SELECT id FROM " . TABLE_PRIVATE . " WHERE `id` IN ($q2)", $id);
if($query->num_rows > 0) {
foreach ($query->rows as $r) {
array_push($parr, $r['id']);
}
}
[$arr, $query_suffix] = $this->get_wildcard_domains($arr);
$query = $this->db->query("SELECT id FROM `" . VIEW_MESSAGES . "` WHERE `id` IN ($q2) AND ( `from` IN ($q) OR `to` IN ($q) $query_suffix)", $arr);
}
2012-09-06 15:27:20 +02:00
}
}
2012-03-10 14:52:50 +01:00
if($query->num_rows > 0) {
foreach ($query->rows as $q) {
2012-09-06 15:27:20 +02:00
if(ENABLE_FOLDER_RESTRICTIONS == 1) {
2013-11-18 19:24:33 +01:00
if(in_array($q['folder'], $session->get("folders"))) { array_push($result, $q['id']); }
2012-09-06 15:27:20 +02:00
}
2016-02-16 15:37:34 +01:00
else if(!in_array($q['id'], $result) && !in_array($q['id'], $parr)) {
2012-09-06 15:27:20 +02:00
array_push($result, $q['id']);
}
2012-03-10 14:52:50 +01:00
}
}
2012-09-06 15:27:20 +02:00
return $result;
2012-03-10 14:52:50 +01:00
}
public function fix_email_address_for_sphinx($email = '') {
2012-04-27 14:39:10 +02:00
$email = preg_replace("/\|@/", "|", $email);
return preg_replace("/[\@\.\+\-\_]/", "X", $email);
2012-02-08 23:14:28 +01:00
}
public function get_search_terms() {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$query = $this->db->query("SELECT term, ts FROM " . TABLE_SEARCH . " WHERE email=? ORDER BY ts DESC", array($session->get("email")));
2012-02-08 23:14:28 +01:00
if(isset($query->rows)) { return $query->rows; }
return array();
}
public function add_search_term($term = '') {
if($term == '') { return 0; }
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2013-03-24 10:02:34 +01:00
parse_str($term, $s);
if(!isset($s['search']) || $s['search'] == '') { return 0; }
2012-02-08 23:14:28 +01:00
if($this->update_search_term($term) == 0) {
AUDIT(ACTION_SAVE_SEARCH, '', '', '', $term);
2013-11-18 19:24:33 +01:00
$query = $this->db->query("INSERT INTO " . TABLE_SEARCH . " (email, ts, term) VALUES(?,?,?)", array($session->get("email"), time(), $term));
2012-02-08 23:14:28 +01:00
}
return 1;
}
public function update_search_term($term = '') {
if($term == '') { return 0; }
AUDIT(ACTION_SEARCH, '', '', '', $term);
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
$query = $this->db->query("UPDATE " . TABLE_SEARCH . " SET ts=? WHERE term=? AND email=?", array(time(), $term, $session->get("email")));
2012-02-08 23:14:28 +01:00
return $this->db->countAffected();
2013-07-27 13:10:48 +02:00
}
2013-08-01 22:28:42 +02:00
public function remove_search_term($ts = 0) {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
$query = $this->db->query("DELETE FROM " . TABLE_SEARCH . " WHERE email=? AND ts=?", array($session->get("email"), $ts));
2012-02-08 23:14:28 +01:00
}
private function fixup_meta_characters($s = '') {
if($s == '') { return $s; }
$s = preg_replace("/\'/", ' ', $s);
$s = preg_replace("/\./", ' ', $s);
return $s;
}
2012-02-08 23:14:28 +01:00
private function fixup_sphinx_operators($s = '') {
if($s == '') { return $s; }
$s = preg_replace("/ OR /", "|", $s);
2012-02-10 14:06:00 +01:00
$s = preg_replace("/(\-)/", " ", $s);
2012-09-06 15:27:20 +02:00
$s = preg_replace("/\'/", '"', $s);
2012-02-08 23:14:28 +01:00
$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;
}
}