Project

General

Profile

Scripts » dump-messages.php

Εξαγωγή όλων των μηνυμάτων και συνημμένων αρχείων ενός μαθήματος - Αλέξανδρος Διαμαντίδης, 23/06/2017 15:33

 
1
<?php
2
require_once 'include/baseTheme.php';
3

    
4
if (!isset($argv[1])) {
5
    die("Usage: $argv[0] <Course Code>");
6
}
7

    
8
$course_code = $argv[1];
9
$course_id = Database::get()->querySingle('SELECT id FROM course WHERE code = ?s', $course_code);
10
$target_dir = $course_code . '_messages';
11

    
12
if (!file_exists($target_dir)) {
13
    mkdir($target_dir);
14
}
15

    
16
$f = fopen($target_dir . '/index.html', 'w');
17
fwrite($f, "
18
<!DOCTYPE HTML>
19
<html class=gradient>
20
<head>
21
  <title>Course messages</title>
22
  <meta charset='utf-8'>
23
  <style>
24
    body { background-color: #eee; }
25
    .label { float: left; width: 5em; color: #888; text-align: right; padding-right: .5em; clear: left; }
26
    .value { float: left; }
27
    .message { margin-top: 1em; margin-bottom: 1em; border: 2px solid #888; background-color: #fff; padding: .5em; max-width: 30em; }
28
    .header { background-color: #eee; }
29
    .body { clear: both; margin-left: 5em; padding-top: .5em; }
30
  </style>
31
</head>
32
<body>");
33

    
34
Database::get()->queryFunc('SELECT dropbox_msg.id, author_id, subject, body,
35
                                   FROM_UNIXTIME(timestamp) AS ts,
36
                                   givenname, surname, email
37
                                FROM dropbox_msg, user
38
                                WHERE course_id = ?d AND
39
                                      user.id = author_id
40
                                ORDER BY timestamp',
41
    function ($item) use ($f, $webDir, $course_code, $target_dir) {
42
        $recipients = Database::get()->queryArray('SELECT recipient_id, givenname, surname, email
43
            FROM dropbox_index, user
44
            WHERE recipient_id = user.id AND
45
                  msg_id = ?d', $item->id);
46
        $to_lines = '';
47
        array_map(function ($r) use ($item, &$to_lines) {
48
            if ($r->recipient_id == $item->author_id) {
49
                return;
50
            } else {
51
                if (!empty($to_lines)) {
52
                    $to_lines .= '<br>';
53
                }
54
                $to_lines .= q($r->givenname . ' ' . $r->surname . ' ' . ' <' . $r->email . '>');
55
            }
56
        }, $recipients);
57
        $files = Database::get()->queryArray('SELECT *
58
            FROM dropbox_attachment
59
            WHERE msg_id = ?d
60
            ORDER BY id', $item->id);
61
        $file_lines = '';
62
        if ($files) {
63
            foreach ($files as $file) {
64
                $target = $file->real_filename;
65
                $extension = get_file_extension($file->real_filename);
66
                $basename = preg_replace('/\.' . preg_quote($extension) . '$/', '', $target);
67
                $i = 0;
68
                while (attachment_exists("$target.$extension")) {
69
                    $i++;
70
                    $target = "$basename-$i.$extension";
71
                }
72
                copy("$webDir/courses/$course_code/dropbox/" . $file->filename, $target_dir . '/' . $target);
73
                $file_lines .= "<div class=label>Files:</div>
74
                    <div class=value><a href='" . q($target) . "'>" .
75
                        q($file->real_filename) . "</a></div>";
76
            }
77
            $file_lines = $file_lines;
78
        }
79
        fwrite($f, "
80
<div class=message>
81
    <div class=header>
82
        <div class=label>From:</div>
83
        <div class=value>" .
84
            q($item->givenname . ' ' . $item->surname . ' <' . $item->email . '>') . "</div>
85
        <div class=label>Subject:</div>
86
        <div class=value>" . q($item->subject) . "</div>
87
        <div class=label>Date:</div>
88
        <div class=value>" . $item->ts . "</div>
89
        <div class=label>To:</div>
90
        <div class=value>$to_lines</div>
91
        $file_lines
92
    </div>
93
    <div class=body>{$item->body}</div>
94
</div>");
95
    }, $course_id);
96

    
97
fwrite($f, "</body></html>\n");
98

    
99
function attachment_exists($name) {
100
    static $attachments;
101

    
102
    $lc = mb_strtolower($name, 'UTF-8');
103
    if (isset($attachments[$lc])) {
104
        return true;
105
    }
106
    $attachments[$lc] = true;
107
    return false;
108
}
(5-5/5)