[WIP] Added script to dump all attachments to in/out dirs

Signed-off-by: Janos SUTO <sj@acts.hu>
This commit is contained in:
Janos SUTO 2021-04-04 07:40:16 +02:00
parent f71146685d
commit d7d0f6cbbc
3 changed files with 96 additions and 2 deletions

View File

@ -0,0 +1,46 @@
<?php
require_once("config.php");
require(DIR_SYSTEM . "/startup.php");
$request = new Request();
Registry::set("request", $request);
$start = NULL;
$loader = new Loader();
Registry::set('load', $loader);
$loader->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);
}
}

View File

@ -6,7 +6,7 @@ class ModelMessageAttachment extends Model {
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 @@ class ModelMessageAttachment extends Model {
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");
}
}
}

View File

@ -296,6 +296,30 @@ class ModelSearchMessage extends Model {
}
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 @@ class ModelSearchMessage extends Model {
foreach ($ids as $id) {
$query = $this->db->query("INSERT INTO " . TABLE_TAG . " (id, uid, tag) VALUES(?,?,?)", array($id, $uid, $tag));
}
}
}
}