Ticket #1439: logcollect-server.php

File logcollect-server.php, 4.5 KB (added by wadeb, 15 years ago)

latest server

Line 
1<?php
2// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3// Sugar Log Collector v1.0
4// by Wade Brainerd <wadetb@gmail.com>
5// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6
7$email_list = 'sugar-reports@lists.sugarlabs.org';
8$download_url = 'http://logcollect.sugarlabs.org/logcollect';
9
10function tableExists($db, $name)
11{
12    $result = $db->query("select count(*) from sqlite_master where name='$name'");
13    return $result->fetchColumn() > 0;
14}
15
16$basepath = 'logcollect';
17@mkdir($basepath);
18
19// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20// Log the query.
21/*
22$logfile = fopen("$basepath/logcollect-server.log", "w+");
23if (!$logfile) die;
24flock($logfile, 2);
25fwrite($logfile, var_export($_POST, true) . "\n");
26fwrite($logfile, var_export($_FILES, true) . "\n");
27fclose($logfile);
28*/
29
30// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
31// Record the event in the database.
32$logdb = new PDO("sqlite:$basepath/logcollect.db");
33
34if (!tableExists($logdb, "logs")) {
35    $logdb->query("create table logs(id integer primary key, timestamp text)");
36}
37
38$logdb->query("insert into logs(timestamp) values (datetime())");
39$id = $logdb->lastInsertId();
40
41print "ID=$id\n";
42
43$logdb = null;
44
45// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46// Copy the attached file into the logs directory using the id as the filename.
47if ($_FILES['logs'])
48{
49    copy($_FILES['logs']['tmp_name'], "$basepath/sugar-report-$id.zip");
50}
51
52// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53// Extract logs and scan for exceptions and other errors to paste into the body of the email.
54// TODO: Also extract general information such as Sugar version, distro, activity versions.
55if ($_FILES['logs']) {
56        $za = new ZipArchive();
57        $za->open($_FILES['logs']['tmp_name']);
58
59        $errors = '';
60
61        for ($i=0; $i<$za->numFiles; $i++)
62        {
63                $name = $za->getNameIndex($i);
64                $fp = $za->getStream($name);
65
66                $banner = false;
67
68                while (!feof($fp))
69                {
70                        $line = fgets($fp);
71                       
72                        // Python exceptions begin with Traceback and continue until the first
73                        // line that doesn't begin with a space.
74                        if (preg_match("/.*Traceback \(most recent call last\)\:.*/", $line))
75                        {
76                                if ($banner == false)
77                                {
78                                        $errors .= "[$name]\n\n";
79                                        $banner = true;
80                                }
81
82                                $errors .= $line;
83                                while (!feof($fp))
84                                {
85                                        $line = fgets($fp);
86                                        $errors .= $line;
87
88                                        if ($line[0] != ' ')
89                                                break;
90                                }
91                                $errors .= "\n";
92                        }
93
94                        // Errors are single lines with a float timestamp, ERROR, then the text.
95                        if (preg_match("/[0-9\.]* ERROR .*/", $line))
96                        {
97                                if ($banner == false)
98                                {
99                                        $errors .= "[$name]\n\n";
100                                        $banner = true;
101                                }
102
103                                $errors .= $line;
104                        }
105                }
106               
107                fclose($fp);
108        }
109
110        $za = null;
111}
112
113// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
114// Notify the email list.
115$tmp_name = $_FILES['logs']['tmp_name'];
116$type = $_FILES['logs']['type'];
117
118$to = $email_list;
119$subject = "[Sugar Report] ID $id";
120$from = 'Sugar Report Collector <sugar-reports@lists.sugarlabs.org>';
121
122$mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
123
124$message = "A Sugar user submitted this problem using the 'Report a problem' control panel.\n";
125$message .= "\nThe user's description of the problem is:\n";
126$message .= "\n----\n" . $_POST['notes'] . "\n----\n";
127
128if ($errors)
129{
130    $message .= "\nThe following errors were detected in the logs:\n\n";
131    $message .= $errors;
132}
133
134$message .= "\nAn archive containing system information, Sugar logs and activity logs is available here:\n";
135$message .= "$download_url/sugar-report-{$id}.zip";
136
137if (file_exists($tmp_name) and is_uploaded_file($tmp_name))
138{
139    $file = fopen($tmp_name, 'rb');
140    $data = fread($file, filesize($tmp_name));
141    fclose($file);
142
143    $data = chunk_split(base64_encode($data));
144}
145
146$headers = "From: $from\r\n" .
147    "MIME-Version: 1.0\r\n" .
148    "Content-Type: multipart/mixed;\r\n" .
149    " boundary=\"{$mime_boundary}\"";
150
151$message = "This is a multi-part message in MIME format.\n\n" .
152    "--{$mime_boundary}\n" .
153    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
154    "Content-Transfer-Encoding: 7bit\n\n" .
155    $message . "\n\n";
156
157/*
158$message .= "--{$mime_boundary}\n" .
159    "Content-Type: {$type};\n" .
160    " name=\"sugar-report-{$id}.zip\"\n" .
161    "Content-Transfer-Encoding: base64\n\n" .
162    $data . "\n\n" .
163    "--{$mime_boundary}--\n";
164*/
165
166mail($to, $subject, $message, $headers);
167
168print "OK\n";
169?>