-
Notifications
You must be signed in to change notification settings - Fork 7
/
BanIpNotificationEmail.php
99 lines (83 loc) · 3.07 KB
/
BanIpNotificationEmail.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
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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TrackingSpamPrevention;
use Piwik\Common;
use Piwik\Log;
use Piwik\Mail;
use Piwik\Piwik;
use Piwik\SettingsPiwik;
class BanIpNotificationEmail
{
public function send($ipRange, $ip, $email, $maxActionsAllowed, $locationData, $nowDateTime)
{
if (empty($email) || !Piwik::isValidEmailString($email)) {
return;
}
$mail = new Mail();
$mail->addTo($email);
$mail->setSubject('An IP was banned as too many actions were tracked.');
$mail->setDefaultFromPiwik();
if (
empty($mail->getFromName()) || in_array($mail->getFromName(), [
'CoreHome_WebAnalyticsReports',
'TagManager_MatomoTagName'
])
) {
$mail->setFrom($mail->getFrom(), 'Web Analytics Reports');
}
$mailBody = 'This is for your information. The following IP was banned because visit tried to track more than ' . Common::sanitizeInputValue($maxActionsAllowed) . ' actions:';
$mailBody .= PHP_EOL . PHP_EOL . '"' . Common::sanitizeInputValue($ipRange) . '"' . PHP_EOL;
$instanceId = SettingsPiwik::getPiwikInstanceId();
$matomoUrl = SettingsPiwik::getPiwikUrl();
if (!empty($matomoUrl)) {
$url = parse_url($matomoUrl);
$matomoHost = $url['host'];
}
if (!empty($_GET)) {
$get = $_GET;
if (isset($get['token_auth'])) {
$get['token_auth'] = 'XYZANONYMIZED';
}
} else {
$get = [];
}
if (!empty($_POST)) {
$post = $_POST;
if (isset($post['token_auth'])) {
$post['token_auth'] = 'XYZANONYMIZED';
}
} else {
$post = [];
}
if (!empty($instanceId)) {
$mailBody .= PHP_EOL . 'Instance ID: ' . Common::sanitizeInputValue($instanceId);
}
if (!empty($matomoHost)) {
$mailBody .= PHP_EOL . 'URL: ' . Common::sanitizeInputValue($matomoHost);
}
$mailBody .= PHP_EOL . 'Current date (UTC): ' . Common::sanitizeInputValue($nowDateTime) . '
IP as detected in header: ' . Common::sanitizeInputValue($ip) . '
GET request info: ' . json_encode($get) . '
POST request info: ' . json_encode($post) . PHP_EOL;
if (!empty($locationData)) {
$mailBody .= 'Geo IP info: ' . json_encode($locationData) . PHP_EOL;
}
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
$mailBody .= 'User Agent: ' . Common::sanitizeInputValue($_SERVER['HTTP_USER_AGENT']) . PHP_EOL;
}
$mail->setBodyText($mailBody);
$testMode = (defined('PIWIK_TEST_MODE') && PIWIK_TEST_MODE);
if ($testMode) {
Log::info($mail->getSubject() . ':' . $mail->getBodyText());
} else {
$mail->send();
}
$a = $mail->getBodyText();
return $mail->getBodyText();
}
}