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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
<?php
/**
* Piwik - 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\ScheduledReports;
use Piwik\Access;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Db;
use Piwik\DbHelper;
use Piwik\Piwik;
use Piwik\Plugins\ScheduledReports\API as APIScheduledReports;
class SubscriptionModel
{
private static $rawPrefix = 'report_subscriptions';
private $table;
public function __construct()
{
$this->table = Common::prefixTable(self::$rawPrefix);
}
public function unsubscribe($token)
{
$details = $this->getSubscription($token);
if (empty($details)) {
return false;
}
$email = $details['email'];
$report = Access::doAsSuperUser(function() use ($details) {
$reports = Request::processRequest('ScheduledReports.getReports', array(
'idReport' => $details['idreport'],
));
return reset($reports);
});
if (empty($report)) {
// if the report isn't found, remove subscription as it isn't active anymore
$this->removeSubscription($token);
return false;
}
$reportParameters = $report['parameters'];
$emailFound = false;
if (!empty($reportParameters['additionalEmails'])) {
$additionalEmails = $reportParameters['additionalEmails'];
$filteredEmails = [];
foreach ($additionalEmails as $additionalEmail) {
if ($additionalEmail == $email) {
$emailFound = true;
continue;
}
$filteredEmails[] = $additionalEmail;
}
if ($emailFound) {
$report['parameters']['additionalEmails'] = $filteredEmails;
}
}
if ($reportParameters['emailMe']) {
$login = $report['login'];
$userModel = new \Piwik\Plugins\UsersManager\Model();
$userData = $userModel->getUser($login);
if ($userData['email'] == $email) {
$emailFound = true;
$report['parameters']['emailMe'] = false;
}
}
if ($emailFound) {
$reportModel = new Model();
$reportModel->updateReport($report['idreport'], array(
'parameters' => json_encode($report['parameters'])
));
// Reset the cache manually since we didn't call the API method which would do it for us
APIScheduledReports::$cache = array();
Piwik::postEvent('Report.unsubscribe', [$report['idreport'], $email]);
$this->removeSubscription($token);
}
return $emailFound;
}
public function getReportSubscriptions($idReport, $includeUnsubscribed = false)
{
$query = 'SELECT * FROM ' . $this->table . ' WHERE idreport = ?';
if (!$includeUnsubscribed) {
$query .= ' AND ts_unsubscribed IS NULL';
}
return $this->getDb()->fetchAll($query, [$idReport]);
}
public function getSubscription($token)
{
return $this->getDb()->fetchRow('SELECT * FROM ' . $this->table . ' WHERE token = ?', [$token]);
}
public function updateReportSubscriptions($idReport, $emails)
{
$availableSubscriptions = $this->getReportSubscriptions($idReport);
$availableEmails = array_column($availableSubscriptions, 'email');
// remove available subscriptions that aren't present anymore
foreach ($availableSubscriptions as $availableSubscription) {
if (!in_array($availableSubscription['email'], $emails) && !empty($availableSubscription['token'])) {
$this->removeSubscription($availableSubscription['token']);
}
}
$emails = array_unique($emails);
// add new subscriptions
foreach ($emails as $email) {
while($token = $this->generateToken($email)) {
if (!$this->tokenExists($token)) {
break;
}
}
if (!in_array($email, $availableEmails)) {
$subscription = [
'idreport' => $idReport,
'token' => $token,
'email' => $email
];
// remove possible "unsubscribe" entry
$this->getDb()->query('DELETE FROM ' . $this->table . ' WHERE idreport = ? AND email = ?', [$idReport, $email]);
$this->getDb()->insert($this->table, $subscription);
}
}
}
private function removeSubscription($token)
{
$this->getDb()->query('UPDATE ' . $this->table . ' SET token = NULL, ts_unsubscribed = NOW() WHERE token = ?', [$token]);
}
private function generateToken($email)
{
return substr(Common::hash($email . time() . Common::getRandomString(5)), 0, 100);
}
private function tokenExists($token)
{
return !!$this->getDb()->fetchOne('SELECT token FROM ' . $this->table . ' WHERE token = ?', [$token]);
}
private function getDb()
{
return Db::get();
}
public static function install()
{
$reportTable = "`idreport` INT(11) NOT NULL,
`token` VARCHAR(100) NULL,
`email` VARCHAR(100) NOT NULL,
`ts_subscribed` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`ts_unsubscribed` TIMESTAMP NULL,
PRIMARY KEY (`idreport`, `email`),
UNIQUE INDEX `unique_token` (`token`)";
DbHelper::createTable(self::$rawPrefix, $reportTable);
}
}
|