-
Notifications
You must be signed in to change notification settings - Fork 28
/
archive.php
54 lines (47 loc) · 1.56 KB
/
archive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
chdir(dirname(__FILE__));
include('include.php');
$last_fn = dirname(__FILE__).'/last.txt';
if(file_exists($last_fn)) {
$last = file_get_contents($last_fn);
} else {
$last = 0;
}
$query = query_messages_since($db, $last);
$last_timestamp = 0;
while($line = $query->fetch(PDO::FETCH_ASSOC)) {
$fn = filename_for_message($line['contact'], $line['date']);
echo $fn."\n";
if(!file_exists(dirname($fn))) {
mkdir(dirname($fn));
}
if(!file_exists($fn)) {
file_put_contents($fn, html_template());
}
$attachment_query = $db->query('SELECT attachment.*
FROM attachment
JOIN message_attachment_join ON message_attachment_join.attachment_id=attachment.ROWID
WHERE message_attachment_join.message_id = ' . $line['ROWID']);
$attachments = array();
while($attachment = $attachment_query->fetch(PDO::FETCH_ASSOC)) {
$attachments[] = $attachment;
}
if(!entry_exists($line, $attachments, $fn)) {
$fp = fopen($fn, 'a');
$log = format_line($line, $attachments);
fwrite($fp, $log."\n");
fclose($fp);
echo date('c', $line['date']) . "\t" . $line['contact'] . "\t" . $line['text'] . "\n";
foreach($attachments as $at) {
$imgsrc = attachment_folder($line['contact'], $line['date']) . $at['transfer_name'];
if(!file_exists(dirname($imgsrc)))
mkdir(dirname($imgsrc));
copy(str_replace('~/',$_SERVER['HOME'].'/',$at['filename']), $imgsrc);
}
}
if($line['date'] > $last_timestamp) {
$last_timestamp = $line['date'];
}
}
if($last_timestamp > 0)
file_put_contents($last_fn, $last_timestamp);