-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.php
More file actions
136 lines (109 loc) · 3.9 KB
/
index.php
File metadata and controls
136 lines (109 loc) · 3.9 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/*******************************************************************************
*
* Put your settings here
*
*/
// Toggle verbose debug output to STDOUT
define('DEBUG', 0);
// SMTP Settings
define('SMTP_HOST', 'smtp.example.com');
define('SMTP_USER', 'your-user-name');
define('SMTP_PASS', 'your-smtp-password');
define('SMTP_PORT', 587);
define('SMTP_SECURE', 'tls'); // 'ssl' or 'tls'
// Sender of the crash report
define('SENDER_EMAIL', 'crashreport@example.com');
define('SENDER_NAME', 'Crash Report Mailer');
define('SEND_CC_TO_USER', true);
// Recipient of the crash report (you)
define('SUPPORT_EMAIL', 'support@example.com');
define('SUPPORT_NAME', 'Amazing App Inc. Support');
// You can provide test data to see if your email settings work at the bottom
// of this file.
/*
* </End of Settings>
*
*****************************************************************************/
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/PHPMailer/src/Exception.php';
require 'vendor/PHPMailer/src/PHPMailer.php';
require 'vendor/PHPMailer/src/SMTP.php';
function postString($key) {
return array_key_exists($key, $_POST) ? $_POST[$key] : '';
}
function clean($userData) {
$userData = htmlspecialchars($userData, ENT_IGNORE, 'utf-8');
$userData = strip_tags($userData);
$userData = trim($userData);
return $userData;
}
function isEmpty($var) {
return !isset($var) || strlen(trim($var)) == 0;
}
function sendEmailForReportAsFilenameForSender($path, $filename, $app, $userProvidedDetails = '', $userEmail = '') {
$mail = new PHPMailer(true);
//Server settings
if (DEBUG) {
$mail->Debugoutput = 'echo';
$mail->SMTPDebug = 4;
}
$mail->isSMTP();
$mail->Host = SMTP_HOST;
$mail->SMTPAuth = True;
$mail->Username = SMTP_USER;
$mail->Password = SMTP_PASS;
$mail->SMTPSecure = SMTP_SECURE;
$mail->Port = SMTP_PORT;
//Recipients
$mail->setFrom(SENDER_EMAIL, SENDER_NAME);
$mail->addAddress(SUPPORT_EMAIL, SUPPORT_NAME);
if (!isEmpty($userEmail) && SEND_CC_TO_USER) {
$mail->addCC($userEmail);
}
// Attachments
$mail->addAttachment($path, $filename);
// Content
$mail->CharSet = PHPMailer::CHARSET_UTF8;
$mail->Subject = $app . ' crash log' . (!isEmpty($userEmail) ? ' from ' . clean($userEmail) : '');
$message = '<b>Processed on:</b> ' . date("Y-m-d H:i:s") . "<br>\r\n"
. '<b>App:</b> ' . $app . "<br>\r\n"
. '<b>Sender:</b> ' . (!isEmpty($userEmail) ? clean($userEmail) : 'unknown') . "<br>\r\n<br>\r\n"
. (!isEmpty($userProvidedDetails) ? "<b>User-provided details:</b>\r\n<pre>" . $userProvidedDetails . "</pre><br>\r\n" : '');
$mail->Body = $message;
$mail->AltBody = $message;
$mail->send();
}
// Collect request data
$crashlog = postString('crashlog');
$userProvidedDetails = clean(postString('userProvidedDetails'));
$app = clean($_SERVER['HTTP_USER_AGENT']);
/*
// To test sending email from this script via `php index.php`, provide replacement data
$crashlog = "test crash log content";
$app = "test app 2000";
*/
if (isEmpty($crashlog) || isEmpty($app)) {
header('X-PHP-Response-Code: 401', true, 401);
die();
}
$logIsJSON = $crashlog[0] == '{';
$filename = date("Y-m-d H.i.s") . ' ' . $app . ($logIsJSON ? '.ips' : '.crash');
$userEmail = postString('userEmail');
$tmpfile = tmpfile();
try {
// Write report to file
fwrite($tmpfile, $crashlog);
fseek($tmpfile, 0);
$path = stream_get_meta_data($tmpfile)['uri'];
sendEmailForReportAsFilenameForSender($path, $filename, $app, $userProvidedDetails, $userEmail);
} catch (Exception $e) { // PHPMailer exception
header('X-PHP-Response-Code: 400', true, 400);
echo($e->getMessage());
} catch (\Exception $e) { // Global PHP exception
header('X-PHP-Response-Code: 400', true, 400);
echo $e->getMessage();
} finally {
fclose($tmpfile);
}