From 8cf4a1b7599de1b49dc5957e8d231870ddfe4e35 Mon Sep 17 00:00:00 2001 From: SJ Date: Tue, 9 Apr 2013 15:02:10 +0200 Subject: [PATCH] added detach support for the gui --- src/cfg.c | 2 +- webui/config.php | 4 + webui/controller/message/bulkrestore.php | 3 + webui/controller/message/download.php | 4 +- webui/controller/message/restore.php | 3 + webui/index.php | 3 + webui/model/message/restore.php | 5 + webui/model/search/message.php | 128 ++++++++++++++++++++--- webui/model/search/search.php | 6 ++ webui/system/misc.php | 7 ++ 10 files changed, 149 insertions(+), 16 deletions(-) diff --git a/src/cfg.c b/src/cfg.c index 620c2e27..a149a9eb 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -91,7 +91,7 @@ struct _parse_rule config_parse_rules[] = { "pidfile", "string", (void*) string_parser, offsetof(struct __config, pidfile), PIDFILE, MAXVAL-1}, { "piler_header_field", "string", (void*) string_parser, offsetof(struct __config, piler_header_field), "", MAXVAL-1}, { "pilergetd_listen_addr", "string", (void*) string_parser, offsetof(struct __config, pilergetd_listen_addr), "127.0.0.1", MAXVAL-1}, - { "pilergetd_listen_port", "integer", (void*) int_parser, offsetof(struct __config, pilergetd_listen_port), "10026", sizeof(int)}, + { "pilergetd_listen_port", "integer", (void*) int_parser, offsetof(struct __config, pilergetd_listen_port), "10091", sizeof(int)}, { "pilergetd_pidfile", "string", (void*) string_parser, offsetof(struct __config, pilergetd_pidfile), PILERGETD_PIDFILE, MAXVAL-1}, { "queuedir", "string", (void*) string_parser, offsetof(struct __config, queuedir), QUEUE_DIR, MAXVAL-1}, { "server_id", "integer", (void*) int_parser, offsetof(struct __config, server_id), "0", sizeof(int)}, diff --git a/webui/config.php b/webui/config.php index c13763de..de629456 100644 --- a/webui/config.php +++ b/webui/config.php @@ -8,6 +8,10 @@ $config = array(); * variable to be overridden in config-site.php */ +$config['PILERGETD_HOST'] = ''; +$config['PILERGETD_PORT'] = 10091; +$config['PILERGETD_READ_LENGTH'] = 8192; +$config['PILERGETD_TIMEOUT'] = 5; $config['BRANDING_TEXT'] = ''; $config['BRANDING_URL'] = ''; diff --git a/webui/controller/message/bulkrestore.php b/webui/controller/message/bulkrestore.php index c3e12508..e451a4fa 100644 --- a/webui/controller/message/bulkrestore.php +++ b/webui/controller/message/bulkrestore.php @@ -51,6 +51,8 @@ class ControllerMessageBulkrestore extends Controller { } + $this->model_search_message->connect_to_pilergetd(); + foreach($idlist as $id) { AUDIT(ACTION_RESTORE_MESSAGE, '', '', $id, ''); @@ -88,6 +90,7 @@ class ControllerMessageBulkrestore extends Controller { } } + $this->model_search_message->disconnect_from_pilergetd(); if(ENABLE_IMAP_AUTH == 1) { $this->model_mail_mail->disconnect_imap(); } diff --git a/webui/controller/message/download.php b/webui/controller/message/download.php index 27825c07..3567a0c5 100644 --- a/webui/controller/message/download.php +++ b/webui/controller/message/download.php @@ -40,8 +40,10 @@ class ControllerMessageDownload extends Controller { header("Content-Disposition: attachment; filename=" . $this->data['piler_id'] . ".eml"); header("Content-Transfer-Encoding: binary\n"); - + $this->model_search_message->connect_to_pilergetd(); $msg = $this->model_search_message->get_raw_message($this->data['piler_id']); + $this->model_search_message->disconnect_from_pilergetd(); + $this->model_search_message->remove_journal($msg); print $msg; } diff --git a/webui/controller/message/restore.php b/webui/controller/message/restore.php index d6f8f732..718f4d35 100644 --- a/webui/controller/message/restore.php +++ b/webui/controller/message/restore.php @@ -64,7 +64,10 @@ class ControllerMessageRestore extends Controller { $this->data['piler_id'] = $this->model_search_message->get_piler_id_by_id($this->data['id']); + $this->model_search_message->connect_to_pilergetd(); $msg = $this->model_search_message->get_raw_message($this->data['piler_id']); + $this->model_search_message->disconnect_from_pilergetd(); + $this->model_search_message->remove_journal($msg); if(ENABLE_IMAP_AUTH == 1) { diff --git a/webui/index.php b/webui/index.php index e560d707..769e90f7 100644 --- a/webui/index.php +++ b/webui/index.php @@ -23,6 +23,9 @@ Registry::set("request", $request); Registry::set('document', new Document()); +$start = NULL; + + $loader = new Loader(); Registry::set('load', $loader); diff --git a/webui/model/message/restore.php b/webui/model/message/restore.php index f1db78c2..b1b878da 100644 --- a/webui/model/message/restore.php +++ b/webui/model/message/restore.php @@ -13,15 +13,20 @@ class ModelMessageRestore extends Model { if($zip->open($filename, ZIPARCHIVE::CREATE) != true) { exit("cannot open <$filename>\n"); } + $this->model_search_message->connect_to_pilergetd(); + foreach($idlist as $id) { $piler_id = $this->model_search_message->get_piler_id_by_id($id); $rawemail = $this->model_search_message->get_raw_message($piler_id); + $zip->addFromString($piler_id . ".eml", $rawemail); AUDIT(ACTION_DOWNLOAD_MESSAGE, '', '', $id, ''); } + $this->model_search_message->disconnect_from_pilergetd(); + $zip->close(); diff --git a/webui/model/search/message.php b/webui/model/search/message.php index 3c2ef85e..c3fc7896 100644 --- a/webui/model/search/message.php +++ b/webui/model/search/message.php @@ -27,35 +27,131 @@ class ModelSearchMessage extends Model { } - public function get_raw_message($id = '') { - $data = ''; + public function get_file_size($sd, $id = '') { + fputs($sd, "STAT $id\r\n"); + $s = fgets($sd, 8192); - if($id == '' || !preg_match("/^([0-9a-f]+)$/", $id)) { return $data; } + $a = explode(" ", $s); + if(isset($a[2]) && $a[2] > 0) { return $a[2]; } - $handle = popen(DECRYPT_BINARY . " $id", "r"); + return 0; + } - while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){ - $data .= $buf; + + 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; } - pclose($handle); + return $s; + } - return $data; + + public function connect_to_pilergetd() { + if(PILERGETD_HOST) { + $sd = fsockopen(PILERGETD_HOST, PILERGETD_PORT); + if(!$sd) { return FALSE; } + + $l = fgets($sd, 4096); + + Registry::set('sd', $sd); + } + } + + + public function disconnect_from_pilergetd() { + if(PILERGETD_HOST) { + $sd = Registry::get('sd'); + + fputs($sd, "QUIT\r\n"); + + fclose($sd); + } + } + + + public function get_raw_message($id = '') { + $s = ''; + + if($id == '' || !preg_match("/^([0-9a-f]+)$/", $id)) { return $s; } + + if(PILERGETD_HOST) { + + $sd = Registry::get('sd'); + + fputs($sd, "MESSAGE $id\r\n"); + + $l = fgets($sd, 8192); + $message = explode(" ", rtrim($l)); + + while(list($k, $v) = each($message)) { + if($k == 0) { + $size = $this->get_file_size($sd, $v); + + $s = $this->read_file($sd, $v, $size); + $s = gzuncompress($s); + } + else { + $size = $this->get_file_size($sd, $v); + $a = $this->read_file($sd, $v, $size); + $a = gzuncompress($a); + + $repl = "ATTACHMENT_POINTER_" . $id . ".a" . $k . "_XXX_PILER"; + + $s = preg_replace("/$repl/", $a, $s); + + $a = ''; + } + } + } + else { + $handle = popen(DECRYPT_BINARY . " $id", "r"); + while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){ + $s .= $buf; + } + pclose($handle); + } + + + return $s; } public function get_attachment($piler_id = '', $attachment_id = '') { $data = ''; - if($piler_id == '' || $attachment_id == '' || !preg_match("/^([0-9a-f]+)$/", $piler_id) || !preg_match("/^([0-9]+)$/", $attachment_id)) { return $data; } + if($piler_id == '' || $attachment_id == '' || !preg_match("/^([0-9a-f]+)$/", $piler_id) || !preg_match("/^([0-9m]+)$/", $attachment_id)) { return $data; } - $handle = popen(DECRYPT_ATTACHMENT_BINARY . " $piler_id $attachment_id", "r"); + if(PILERGETD_HOST) { + $sd = fsockopen(PILERGETD_HOST, PILERGETD_PORT); + if(!$sd) { return $data; } - while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){ - $data .= $buf; + $l = fgets($sd, 4096); + + $size = $this->get_file_size($sd, $piler_id . ".a" . $attachment_id); + + $data = $this->read_file($sd, $piler_id . ".a" . $attachment_id, $size); + $data = gzuncompress($data); + + fclose($sd); + } + else { + $handle = popen(DECRYPT_BINARY . " $id", "r"); + while(($buf = fread($handle, DECRYPT_BUFFER_LENGTH))){ + $data .= $buf; + } + pclose($handle); } - - pclose($handle); /* check if it's a base64 encoded stuff */ @@ -73,7 +169,9 @@ class ModelSearchMessage extends Model { public function get_message_headers($id = '') { $data = ''; + $this->connect_to_pilergetd(); $msg = $this->get_raw_message($id); + $this->disconnect_from_pilergetd(); $this->remove_journal($msg); @@ -143,7 +241,9 @@ class ModelSearchMessage extends Model { $from = $to = $subject = $date = $message = ""; + $this->connect_to_pilergetd(); $msg = $this->get_raw_message($id); + $this->disconnect_from_pilergetd(); $this->remove_journal($msg); diff --git a/webui/model/search/search.php b/webui/model/search/search.php index d507a2a5..1e3937d3 100644 --- a/webui/model/search/search.php +++ b/webui/model/search/search.php @@ -493,6 +493,9 @@ class ModelSearchSearch extends Model { $lang = Registry::get('language'); + + $this->model_search_message->connect_to_pilergetd(); + foreach($query->rows as $m) { $m['shortfrom'] = make_short_string($m['from'], MAX_CGI_FROM_SUBJ_LEN); $m['from'] = escape_gt_lt_quote_symbols($m['from']); @@ -526,6 +529,9 @@ class ModelSearchSearch extends Model { array_push($messages, $m); } + + $this->model_search_message->disconnect_from_pilergetd(); + } if(MEMCACHED_ENABLED) { diff --git a/webui/system/misc.php b/webui/system/misc.php index b0cdb1ae..bf8a2f67 100644 --- a/webui/system/misc.php +++ b/webui/system/misc.php @@ -484,4 +484,11 @@ function fix_evolution_mime_name_crap($s = '') { } +function safe_feof($fp, &$start = NULL) { + $start = microtime(true); + return feof($fp); +} + + + ?>