-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotify.php
158 lines (141 loc) · 6.29 KB
/
notify.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
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
<?php
require_once 'vendor/autoload.php';
require __DIR__ . '/common.php';
set_time_limit(0);
use Mailgun\Mailgun;
// Load all of the software we may notify about
$classes = array();
$dir = new DirectoryIterator('software');
foreach ($dir as $fileinfo)
{
if (!$fileinfo->isDot() && $fileinfo->getExtension() == 'php')
{
include_once('software' . DIRECTORY_SEPARATOR . $fileinfo->getFilename());
}
}
// Get the software updates
$updates = array();
$sql = 'SELECT software, branch, version, last_sent_user FROM ' . NOTIFICATIONS_TABLE;
foreach ($db->query($sql) as $row)
{
$updates[$row['software']][$row['branch']] = [
'version' => $row['version'],
'last_sent_user'=> $row['last_sent_user'],
];
}
foreach ($updates as $software => $branches)
{
$class = new $software();
foreach ($branches as $branch => $data)
{
$version = $data['version'];
$last_user = $data['last_sent_user'];
// Get the announcement URL
$sql = 'SELECT announcement FROM ' . VERSION_TABLE . ' WHERE software = :software AND branch = :branch AND version = :version';
$sth = $db->prepare($sql);
$result = $sth->execute(array(':software' => $software, ':branch' => $branch, ':version' => $version));
if ($result === false)
{
print_r($sth->debugDumpParams());
throw new RuntimeException($sth->errorInfo());
}
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($row !== false && !empty($row['announcement']))
{
$site = $row['announcement'];
}
else
{
$site = $class::$homepage;
}
// Find the users who want to be notified about either this branch or all updates
// We only need to prepare the query once
$sql = 'SELECT u.id, u.subscriber_token, u.subscription_email FROM ' . SUBSCRIPTION_TABLE . ' s
JOIN ' . USER_TABLE . ' u ON (u.id = s.user_id)
WHERE s.software = :software
AND (s.branch = :branch OR s.branch IS NULL)
AND u.id > :last_user
ORDER BY u.id LIMIT 500';
$sth = $db->prepare($sql);
while (true)
{
$result = $sth->execute(array(':software' => $software, ':branch' => $branch, ':last_user' => $last_user));
if ($result === false)
{
print_r($sth->debugDumpParams());
throw new RuntimeException($sth->errorInfo());
}
if ($sth->rowCount() == 0)
{
$sql = 'DELETE FROM ' . NOTIFICATIONS_TABLE . ' WHERE software = :software AND branch = :branch AND version = :version';
$uph = $db->prepare($sql);
$result = $uph->execute(array(':software' => $software, ':branch' => $branch, ':version' => $version));
if ($result === false)
{
print_r($uph->debugDumpParams());
throw new RuntimeException($uph->errorInfo());
}
break;
}
$user_templates = array();
$user_emails = array();
$last_user_seen = $last_user;
while (($row = $sth->fetch(PDO::FETCH_ASSOC)) != false)
{
$last_user_seen = $row['id'];
$user_emails[] = $row['subscription_email'];
$user_templates[$row['subscription_email']] = [
'user_id' => $row['id'],
'sub_email' => $row['subscription_email'],
'sub_token' => $row['subscriber_token'],
];
}
// Build the email to send
$unsub_address = "https://versioncheck.net/unsubscribe.php?" .
"sub_token=%recipient.sub_token%" .
"&user=%recipient.user_id%" .
"&sub_email=%recipient.sub_email%" .
"&software=$software" .
"&branch=$branch";
$message = "Version $version of {$class::$name} was just released. Visit their site for more information: $site\r\n\r\n" .
"Thanks for using versioncheck.net!\r\n\r\n\r\n" .
"You have received this message because you are subscribed to notifications for {$class::$name}. If you no longer wish to " .
"receive notifications for this software, you can unsubcribe here: $unsub_address";
$html_message = <<<HERE
<!DOCTYPE html>
<p>Version $version of {$class::$name} was just released. Visit their site for more information: <a href="$site">$site</a></p>
<p>Thanks for using <a href="https://versioncheck.net">versioncheck.net</a>!</p>
<p><small>You have received this message because you are subscribed to notifications for {$class::$name}. If you no longer wish to
receive notifications for this software, you can unsubcribe here: <a href="$unsub_address">Unsubscribe</a></small></p>
HERE;
// Set up the MailGun client
$mgClient = Mailgun::create($provider_configs['mailgun']['api_key'], $provider_configs['mailgun']['endpoint']);
$domain = 'mg.versioncheck.net';
$params = array(
'from' => 'VersionCheck <[email protected]>',
'to' => $user_emails,
'subject' => "{$class::$name} $version Released",
'text' => $message,
'html' => $html_message,
'recipient-variables' => json_encode($user_templates)
);
// Send the emails
$mgClient->messages()->send($domain, $params);
// Update the last_user_sent field before we grab the next batch
$sql = 'UPDATE ' . NOTIFICATIONS_TABLE . ' SET last_sent_user = :user_id WHERE software = :software AND branch = :branch AND version = :version';
$uph = $db->prepare($sql);
$result = $uph->execute(array(
':user_id' => $last_user_seen,
':software' => $software,
':branch' => $branch,
':version' => $version
));
if ($result === false)
{
print_r($uph->debugDumpParams());
throw new RuntimeException(var_export($uph->errorInfo(), true));
}
$last_user = $last_user_seen;
}
}
}