diff --git a/contrib/export-attachments/1.php b/contrib/export-attachments/1.php new file mode 100644 index 0000000..c8902dd --- /dev/null +++ b/contrib/export-attachments/1.php @@ -0,0 +1,46 @@ +load->model('domain/domain'); +$loader->load->model('search/search'); +$loader->load->model('search/message'); +$loader->load->model('message/attachment'); + + +$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PREFIX); +Registry::set('db', $db); + +Registry::set('auditor_user', 1); + +$outdir = "/path/to/attachments"; +$limit = 1000; + +$domain = new ModelDomainDomain(); +$attachment = new ModelMessageAttachment(); +$message = new ModelSearchMessage(); + +$domains = $domain->get_mapped_domains(); + + +for($i=1; $i<$limit; $i++) { + $a = $attachment->get_attachment_by_id($i); + $m = $message->get_message_addresses_by_piler_id($a['piler_id'], $domains); + + $attachment->dump_attachment($outdir, "out", $m['sender'], $i, $a); + + foreach($m['rcpt'] as $rcpt) { + $attachment->dump_attachment($outdir, "in", $rcpt, $i, $a); + } +} diff --git a/webui/model/message/attachment.php b/webui/model/message/attachment.php index e3cacf2..2253428 100644 --- a/webui/model/message/attachment.php +++ b/webui/model/message/attachment.php @@ -6,7 +6,7 @@ public function get_attachment_by_id($id = 0) { if($id <= 0) { return []; } - $query = $this->db->query("SELECT id, piler_id, attachment_id, name, type FROM " . TABLE_ATTACHMENT . " WHERE id=?", [$id]); + $query = $this->db->query("SELECT id, piler_id, attachment_id, name, type, ptr FROM " . TABLE_ATTACHMENT . " WHERE id=?", [$id]); if(isset($query->row)) { if($query->row['ptr'] > 0) { @@ -77,4 +77,28 @@ return $images; } + + public function dump_attachment($basedir='', $in_or_out="in", $email='', $id=0, $attachment=[]) { + if($basedir == '' || $email == '') { + return; + } + + $dir = sprintf("%s/%s/%s", $basedir, $email, $in_or_out); + + if(!is_dir($dir)) { + if(!mkdir($dir, 0700, true)) { + die("Failed to create folder $dir"); + } + } + + $fname = sprintf("%s/%d-%s", $dir, $id, $attachment['filename']); + $fp = fopen($fname, "w+"); + if($fp) { + fwrite($fp, $attachment['attachment']); + fclose($fp); + } else { + syslog(LOG_INFO, "ERROR: could not write $fname"); + } + } + } diff --git a/webui/model/search/message.php b/webui/model/search/message.php index 5e67bff..2351a1d 100644 --- a/webui/model/search/message.php +++ b/webui/model/search/message.php @@ -296,6 +296,30 @@ } + public function get_message_addresses_by_piler_id($piler_id='', $domains=[]) { + $id = 0; + $sender = ''; + $rcpt = []; + + $query = $this->db->query("SELECT id, `from`, `fromdomain` FROM " . TABLE_META . " WHERE piler_id=?", [$piler_id]); + if(isset($query->row)) { + $id = $query->row['id']; + if(in_array($query->row['fromdomain'], $domains)) { + $sender = $query->row['from']; + } + } + + $query = $this->db->query("SELECT `to`, `todomain` FROM " . TABLE_RCPT . " WHERE id=?", [$id]); + foreach($query->rows as $row) { + if(in_array($row['todomain'], $domains)) { + $rcpt[] = $row['to']; + } + } + + return ['sender' => $sender, 'rcpt' => $rcpt]; + } + + public function get_attachment_by_id($id = 0) { if($id <= 0) { return array(); } @@ -459,7 +483,7 @@ foreach ($ids as $id) { $query = $this->db->query("INSERT INTO " . TABLE_TAG . " (id, uid, tag) VALUES(?,?,?)", array($id, $uid, $tag)); } - } + } }