From 8f59372cf4b2847188e826ba3f528d11cb9aa926 Mon Sep 17 00:00:00 2001 From: SJ Date: Thu, 23 Jan 2014 21:39:47 +0100 Subject: [PATCH] automated search support --- CREDITS | 3 +- util/automated-search.php | 206 ++++++++++++++++++ webui/config.php | 4 +- webui/controller/message/view.php | 6 + webui/controller/search/helper.php | 84 +------ webui/language/de/messages.php | 1 + webui/language/en/messages.php | 1 + webui/language/es/messages.php | 1 + webui/language/hu/messages.iso-8859-2.php | 1 + webui/language/hu/messages.php | 1 + webui/language/pt/messages.php | 1 + webui/model/search/search.php | 99 +++++++++ .../theme/default/templates/message/auto.tpl | 99 +++++++++ .../theme/default/templates/search/auto.tpl | 68 ++++++ 14 files changed, 490 insertions(+), 85 deletions(-) create mode 100644 util/automated-search.php create mode 100644 webui/view/theme/default/templates/message/auto.tpl create mode 100644 webui/view/theme/default/templates/search/auto.tpl diff --git a/CREDITS b/CREDITS index 04c2d03b..1cbce31b 100644 --- a/CREDITS +++ b/CREDITS @@ -4,4 +4,5 @@ The FSF.hu Foundation (http://fsf.hu/) supported and donated piler within the Nemeth Adam reviewed the web interface, and gave lots of useful hints and insights to improve the web ui of piler. -Remi Smith improved the restricted auditor feature for a better multitenancy. +Remi Smith improved the restricted auditor feature for a better multitenancy, +and invented the default theme. diff --git a/util/automated-search.php b/util/automated-search.php new file mode 100644 index 00000000..234727f5 --- /dev/null +++ b/util/automated-search.php @@ -0,0 +1,206 @@ +set("username", "system"); +$session->set("uid", 1); +$session->set("admin_user", 2); +$session->set("email", "system@local"); +$session->set("domain", "local"); +$session->set("emails", array("system@local")); +$session->set("pagelen", $page_len); + +require(DIR_SYSTEM . "/startup.php"); + +$request = new Request(); +Registry::set("request", $request); + + +Registry::set('document', new Document()); + + +$start = NULL; + + +$loader = new Loader(); +Registry::set('load', $loader); + + +$language = new Language(); +Registry::set('language', $language); + +extract($language->data); + +if(ENABLE_SYSLOG == 1) { openlog("piler-automated-search", LOG_PID, LOG_MAIL); } + + +/* check if user has authenticated himself. If not, we send him to login */ + +Registry::set('username', "system"); +Registry::set('admin_user', 0); +Registry::set('auditor_user', 1); +Registry::set('readonly_admin', 0); + + +$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PREFIX); +Registry::set('DB_DATABASE', DB_DATABASE); + +Registry::set('db', $db); + +Registry::set('DB_DRIVER', DB_DRIVER); + +$sphx = new DB(SPHINX_DRIVER, SPHINX_HOSTNAME, "", "", SPHINX_DATABASE, ""); +Registry::set('sphx', $sphx); + + +if(MEMCACHED_ENABLED) { + $memcache = new Memcache(); + foreach ($memcached_servers as $m){ + $memcache->addServer($m[0], $m[1]); + } + + Registry::set('memcache', $memcache); +} + +Registry::set('counters', $counters); +Registry::set('langs', $langs); +Registry::set('paging', $paging); +Registry::set('themes', $themes); +Registry::set('letters', $letters); +Registry::set('ldap_types', array("AD", "iredmail", "lotus", "zimbra", LDAP_TYPE_GENERIC)); +Registry::set('health_smtp_servers', $health_smtp_servers); +Registry::set('partitions_to_monitor', $partitions_to_monitor); +Registry::set('actions', $actions); +Registry::set('import_status', $import_status); + + +$data = array( + 'page' => 0, + 'sort' => $sort, + 'order' => $order, + 'type' => 'search', + 'search' => $search_expression, + 'searchtype' => 'expert' + ); + +$loader->model('search/search'); +$loader->model('search/message'); +$loader->model('mail/mail'); + +$search = new ModelSearchSearch(); +$mail = new ModelMailMail(); + +$a = $search->preprocess_post_expert_request($data); + +if(isset($options['yesterday']) || isset($options['y']) ) +{ + $a['date1'] = $a['date2'] = date("Y.m.d", time() - 86400); +} + +list ($n, $total_found, $all_ids, $messages) = $search->search_messages($a, $page); + +if($dry_run == 0) +{ + $msg = "From: " . SMTP_FROMADDR . EOL; + $msg .= "To: " . ADMIN_EMAIL . EOL; + $msg .= "Subject: =?UTF-8?Q?" . preg_replace("/\n/", "", my_qp_encode($title)) . "?=" . EOL; + $msg .= "Message-ID: <" . generate_random_string(25) . '@' . SITE_NAME . ">" . EOL; + $msg .= "MIME-Version: 1.0" . EOL; + $msg .= "Content-Type: text/html; charset=\"utf-8\"" . EOL; + $msg .= EOL . EOL; + + ob_start(); + include($webuidir . "/view/theme/default/templates/search/auto.tpl"); + $msg .= ob_get_contents(); + + ob_end_clean(); + + $x = $mail->send_smtp_email(SMARTHOST, SMARTHOST_PORT, SMTP_DOMAIN, SMTP_FROMADDR, $automated_search_recipients, $msg); +} +else { + print "search = $search_expression\n"; + print_r($all_ids); + print EOL; +} + + + +function display_help() { + $phpself = basename(__FILE__); + + echo("\nUsage: $phpself --webui [PATH] --search '[SEARCH EXPRESSION]' [OPTIONS...]\n\n"); + echo("\nThe results go to the recipients defined in \$automated_search_recipients, see config-site.php\n\n"); + + echo("\t--webui=\"[REQUIRED: path to the piler webui directory]\"\n"); + echo("\t--search=\"[REQUIRED: the search expression]\"\n\n"); + echo("options:\n"); + echo("\t-y | --yesterday: Search \"yesterday\"\n"); + echo("\t-d | --dry-run: Only print the found IDs\n"); + echo("\t-h | --help: Prints this help screen and exits\n"); +} + + +?> diff --git a/webui/config.php b/webui/config.php index 9c863955..9bdf47ba 100644 --- a/webui/config.php +++ b/webui/config.php @@ -259,6 +259,8 @@ $paging = array( 50 ); +$automated_search_recipients = array(); + $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); @@ -287,7 +289,7 @@ if(MOBILE_DEVICE == 1 || OUTLOOK == 1) { $config['THEME'] = 'mobile'; } // make sure auditors are restricted in a saas environment if($config['ENABLE_SAAS'] == 1) { $config['RESTRICTED_AUDITOR'] = 1; } -if($session->get("username") == 'auditor@local') { $config['RESTRICTED_AUDITOR'] = 0; } +if($session->get("username") == 'auditor@local' || isset($_SERVER['argv'][2]) ) { $config['RESTRICTED_AUDITOR'] = 0; } diff --git a/webui/controller/message/view.php b/webui/controller/message/view.php index 043980f2..561f4bd1 100644 --- a/webui/controller/message/view.php +++ b/webui/controller/message/view.php @@ -32,6 +32,12 @@ class ControllerMessageView extends Controller { $this->data['search'] = $this->request->post['search']; } + if(substr($this->data['id'], 0, 1) == 'a') { + $this->template = "message/auto.tpl"; + $this->data['id'] = substr($this->data['id'], 1, 200); + } + + if(!verify_piler_id($this->data['id'])) { AUDIT(ACTION_UNKNOWN, '', '', $this->data['id'], 'unknown id: ' . $this->data['id']); die("invalid id: " . $this->data['id']); diff --git a/webui/controller/search/helper.php b/webui/controller/search/helper.php index a8f28373..902cc39a 100644 --- a/webui/controller/search/helper.php +++ b/webui/controller/search/helper.php @@ -35,7 +35,6 @@ class ControllerSearchHelper extends Controller { $this->load->model('search/message'); $this->load->model('user/user'); - $this->data['page'] = 0; if(isset($this->request->post['page'])) { $this->data['page'] = $this->request->post['page']; } @@ -46,7 +45,7 @@ class ControllerSearchHelper extends Controller { if($this->request->post['searchtype'] == 'expert'){ if(isset($this->request->post['search']) && preg_match("/(from|to|subject|body|direction|d|size|date1|date2|attachment|a|tag|note|id)\:/", $this->request->post['search'])) { - $this->preprocess_post_expert_request($this->request->post); + $this->a = $this->model_search_search->preprocess_post_expert_request($this->request->post); } else { $this->naive_preprocess_post_expert_request($this->request->post); @@ -164,87 +163,6 @@ class ControllerSearchHelper extends Controller { } - private function preprocess_post_expert_request($data = array()) { - $token = 'match'; - $ndate = 0; - $match = array(); - - if(!isset($data['search'])) { return; } - - $s = preg_replace("/:/", ": ", $data['search']); - $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); - $b = explode(" ", $s); - - while(list($k, $v) = each($b)) { - if($v == '') { continue; } - - if($v == 'from:') { $token = 'match'; $this->a['match'][] = '@from'; continue; } - else if($v == 'to:') { $token = 'match'; $this->a['match'][] = '@to'; continue; } - else if($v == 'subject:') { $token = 'match'; $this->a['match'][] = '@subject'; continue; } - else if($v == 'body:') { $token = 'match'; $this->a['match'][] = '@body'; continue; } - else if($v == 'direction:' || $v == 'd:') { $token = 'direction'; continue; } - else if($v == 'size:') { $token = 'size'; continue; } - else if($v == 'date1:') { $token = 'date1'; continue; } - else if($v == 'date2:') { $token = 'date2'; continue; } - else if($v == 'attachment:' || $v == 'a:') { $token = 'match'; $this->a['match'][] = '@attachment_types'; continue; } - else if($v == 'size') { $token = 'size'; continue; } - else if($v == 'tag:') { $token = 'tag'; continue; } - else if($v == 'note:') { $token = 'note'; continue; } - else if($v == 'ref:') { $token = 'ref'; continue; } - else if($v == 'id:') { $token = 'id'; continue; } - else { - if(preg_match("/\d{4}\-\d{1,2}\-\d{1,2}/", $v) || preg_match("/\d{1,2}\/\d{1,2}\/\d{4}/", $v)) { - $ndate++; - $this->a["date$ndate"] = $v; - } - } - - - if($token == 'match') { $this->a['match'][] = $v; } - else if($token == 'date1') { $this->a['date1'] = ' ' . $v; } - else if($token == 'date2') { $this->a['date2'] = ' ' . $v; } - else if($token == 'tag') { $this->a['tag'] .= ' ' . $v; } - else if($token == 'note') { $this->a['note'] .= ' ' . $v; } - else if($token == 'ref') { $this->a['ref'] = ' ' . $v; } - else if($token == 'id') { $this->a['id'] .= ' ' . $v; } - - else if($token == 'direction') { - if($v == 'inbound') { $this->a['direction'] = "0"; } - else if($v == 'outbound') { $this->a['direction'] = 2; } - else if($v == 'internal') { $this->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; } - - $this->a['size'] .= ' ' . $o . $v; - } - - } - - } - - } ?> diff --git a/webui/language/de/messages.php b/webui/language/de/messages.php index 63b03177..d99b2b53 100644 --- a/webui/language/de/messages.php +++ b/webui/language/de/messages.php @@ -471,5 +471,6 @@ $_['text_qr_code'] = "QR"; $_['text_refresh_qr_code'] = "Refresh QR code"; $_['text_print_message'] = "Print"; $_['text_forward_selected_emails_to'] = "Forward selected emails to"; +$_['text_search_expression'] = "Search expression"; ?> diff --git a/webui/language/en/messages.php b/webui/language/en/messages.php index 3f9c1c26..9b45df37 100644 --- a/webui/language/en/messages.php +++ b/webui/language/en/messages.php @@ -303,6 +303,7 @@ $_['text_search'] = "Search"; $_['text_search2'] = "search"; $_['text_search_emails'] = "Search email addresses"; $_['text_search_email_to_add'] = "Search email to add"; +$_['text_search_expression'] = "Search expression"; $_['text_search_folders'] = "Search folders"; $_['text_search_folder_to_add'] = "Search folder to add"; $_['text_search_groups'] = "Search groups"; diff --git a/webui/language/es/messages.php b/webui/language/es/messages.php index 35eeee83..2e3185cc 100644 --- a/webui/language/es/messages.php +++ b/webui/language/es/messages.php @@ -470,5 +470,6 @@ $_['text_qr_code'] = "QR"; $_['text_refresh_qr_code'] = "Refresh QR code"; $_['text_print_message'] = "Print"; $_['text_forward_selected_emails_to'] = "Forward selected emails to"; +$_['text_search_expression'] = "Search expression"; ?> diff --git a/webui/language/hu/messages.iso-8859-2.php b/webui/language/hu/messages.iso-8859-2.php index 52216415..7a683e08 100644 --- a/webui/language/hu/messages.iso-8859-2.php +++ b/webui/language/hu/messages.iso-8859-2.php @@ -305,6 +305,7 @@ $_['text_search'] = "Keres $_['text_search2'] = "keresés"; $_['text_search_emails'] = "Email címek keresése"; $_['text_search_email_to_add'] = "írja be az email cím elejét"; +$_['text_search_expression'] = "Keresési kifejezés"; $_['text_search_folders'] = "Mappák keresése"; $_['text_search_folder_to_add'] = "Írja be a mappa elejét"; $_['text_search_groups'] = "Csoportok keresése"; diff --git a/webui/language/hu/messages.php b/webui/language/hu/messages.php index c46a5fe9..cf2d3c6a 100644 --- a/webui/language/hu/messages.php +++ b/webui/language/hu/messages.php @@ -305,6 +305,7 @@ $_['text_search'] = "KeresĂ©s"; $_['text_search2'] = "keresĂ©s"; $_['text_search_emails'] = "Email cĂ­mek keresĂ©se"; $_['text_search_email_to_add'] = "Ă­rja be az email cĂ­m elejĂ©t"; +$_['text_search_expression'] = "KeresĂ©si kifejezĂ©s"; $_['text_search_folders'] = "Mappák keresĂ©se"; $_['text_search_folder_to_add'] = "ĂŤrja be a mappa elejĂ©t"; $_['text_search_groups'] = "Csoportok keresĂ©se"; diff --git a/webui/language/pt/messages.php b/webui/language/pt/messages.php index 1893c9f9..05f32bb8 100644 --- a/webui/language/pt/messages.php +++ b/webui/language/pt/messages.php @@ -460,5 +460,6 @@ $_['text_invalid_pin_code'] = "Pin code inválido"; $_['text_qr_code'] = "QR"; $_['text_refresh_qr_code'] = "Renovar cĂłdigo QR"; $_['text_forward_selected_emails_to'] = "Forward selected emails to"; +$_['text_search_expression'] = "Search expression"; ?> diff --git a/webui/model/search/search.php b/webui/model/search/search.php index 2efc9784..63a8de57 100644 --- a/webui/model/search/search.php +++ b/webui/model/search/search.php @@ -307,6 +307,105 @@ class ModelSearchSearch extends Model { } + 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' => '', + 'match' => array() + ); + + if(!isset($data['search'])) { return $a; } + + $s = preg_replace("/:/", ": ", $data['search']); + $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); + $b = explode(" ", $s); + + while(list($k, $v) = each($b)) { + if($v == '') { continue; } + + if($v == 'from:') { $token = 'match'; $a['match'][] = '@from'; continue; } + else if($v == 'to:') { $token = 'match'; $a['match'][] = '@to'; continue; } + 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 == 'size:') { $token = 'size'; continue; } + else if($v == 'date1:') { $token = 'date1'; continue; } + else if($v == 'date2:') { $token = 'date2'; continue; } + else if($v == 'attachment:' || $v == 'a:') { $token = 'match'; $a['match'][] = '@attachment_types'; continue; } + else if($v == 'size') { $token = 'size'; continue; } + else if($v == 'tag:') { $token = 'tag'; continue; } + else if($v == 'note:') { $token = 'note'; continue; } + else if($v == 'ref:') { $token = 'ref'; continue; } + else if($v == 'id:') { $token = 'id'; continue; } + else { + 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($token == 'date1') { $a['date1'] = ' ' . $v; } + else if($token == 'date2') { $a['date2'] = ' ' . $v; } + else if($token == 'tag') { $a['tag'] .= ' ' . $v; } + else if($token == 'note') { $a['note'] .= ' ' . $v; } + else if($token == 'ref') { $a['ref'] = ' ' . $v; } + else if($token == 'id') { $a['id'] .= ' ' . $v; } + + 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; + } + + private function get_sphinx_id_list($s = '', $sphx_table = '', $field = '') { $id_list = ''; diff --git a/webui/view/theme/default/templates/message/auto.tpl b/webui/view/theme/default/templates/message/auto.tpl new file mode 100644 index 00000000..eef87f27 --- /dev/null +++ b/webui/view/theme/default/templates/message/auto.tpl @@ -0,0 +1,99 @@ + + + + + <?php print $title; ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +

+   | + +   | + + | + +   + + |   + + + |   + + |   + + + +

+
+ +
+  : + " /> + +
+ + +
+ +
+
+
+
+
+ +   + 1) { ?>|
+
+
+ +
+ + +

+ + + +
+ + + +
+ + + diff --git a/webui/view/theme/default/templates/search/auto.tpl b/webui/view/theme/default/templates/search/auto.tpl new file mode 100644 index 00000000..163ead35 --- /dev/null +++ b/webui/view/theme/default/templates/search/auto.tpl @@ -0,0 +1,68 @@ + + + + + <?php print $title; ?> + + + + + + + + +

:
+ :

+ + 0) { ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  + + + + + + + + + +
. !  0) { ?>+ 
+ + +
+ + + + +