changed git call from https to git readonly
[atutor.git] / mods / wiki / tools / cron.d / S92mimebackup.php
1 <?php
2 /*
3    Sends out a backup MIME/multipart archive containing only the latest
4    versions of all text pages. This cannot be easily deciphered by an
5    ordinary mail reader, and there is yet no commandline utility for
6    this kind of mime archives; but it is anyhow a good format for backup
7    purposes (because an import tool is quickly made).
8    Please see also "S92binbackup" which sends backups already useful with
9    the t_transfer utility.
10 */
11
12 // define("MIMEBACKUP_TO", "you@example.com");   // who gets the backup
13
14
15 #-- go
16 if (defined("MIMEBACKUP_TO")) {
17
18    #-- collect pages
19    $parts = array();
20    echo "[$cron]: Searching _TEXT pages to generate MIME/multipart backup mail...\n";
21
22    #-- search for _TEXT pages
23    $result = ewiki_db::GETALL(array("id", "version", "flags"));
24    while ($row = $result->get(0, 0x1037)) {
25
26       $row = ewiki_db::GET($row["id"]);
27       if (!$row || !$row["id"] || !$row["content"]) {
28          continue;
29       }
30
31       #-- add (we could run out of memory with this!)
32       $parts[] = $row;
33    }
34    echo "[$cron]: Found " . count($parts) . " pages\n";
35
36    #-- send
37    $BND = "cut-here-".md5(time())."-cut-here";
38    $mail = "--$BND\n"
39       ."Content-Type: text/plain\n"
40       ."\n"
41       ."This is a backup of " . EWIKI_NAME . ". All text pages are contained\n"
42       ."in this mail as MIME/multipart embedded files; use a better mail reader\n"
43       ."if you cannot decompose it easily.\n"
44       ."\n--$BND\n"
45       . multipart_parallel($parts, $boundary, $inj_header)
46       . "\n--$BND--\n";
47    unset($parts);
48    echo "[$cron]: Sending backup data to " . MIMEBACKUP_TO . "\n";
49    mail(
50       MIMEBACKUP_TO,
51       "[backup] " . EWIKI_NAME . ":",
52       $mail,
53       "Content-Type: multipart/mixed; boundary=$BND\n"
54       ."X-Mailer: $ewiki_config[ua] run-parts/$cron\n"
55       ."From: ewiki@$_SERVER[SERVER_NAME]\n"
56       ."Reply-To: trashbin@example.com\n"
57    );
58    unset($mail);
59
60 }
61
62
63
64 #-- produces a (probably invalid) multipart stream
65 function multipart_parallel(&$parts) {
66    global $ewiki_t;
67    $boundary = preg_replace("/(........)/", '$1-', md5(serialize($parts)))
68              . "ewiki-mimebackup";   //@FIXME: must be checked for being unique
69    $text = "";
70    foreach ($parts as $i=>$row) {
71       $fn = urlencode($row["id"]);
72       $created = gmstrftime($ewiki_t["C"]["DATE"], $row["created"]);
73       $lm = gmstrftime($ewiki_t["C"]["DATE"], $row["lastmodified"]);
74       $text .= "--$boundary\n"
75              . "Content-Type: text/x-wiki; variant=ewiki; charset=iso-8859-1\n"
76              . "Content-Disposition: inline; filename=\"$fn\"\n"
77              . "X-Flags: $row[flags]\n"
78              . "Content-Version: $row[version]\n"  // HTTP header, but hey!
79              . "X-Created: $created\n"
80              . "Last-Modified: $lm\n"  // also only allowed in HTTP exactly
81              . "\n"
82              . $row["content"]
83              . "\n";
84       unset($parts[$i]);
85    }
86    $text .= "--$boundary--\n";
87
88    #-- encode
89    $text = wordwrap(base64_encode(gzencode($text)), 78, "\n", 1);
90
91    #-- prep headers
92    $text
93          = "Content-Type: x-multipart/parallel; boundary=$boundary\n"
94          . "Content-Disposition: attachment; filename=".EWIKI_NAME.".mar.gz\n"
95          . "Content-Encoding: gzip\n"
96          . "Content-Transfer-Encoding: base64\n"
97          . "\n" . $text;
98
99    return($text);
100 }  
101
102
103 ?>