-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththesslstore_plugin.php
More file actions
382 lines (338 loc) · 13.7 KB
/
thesslstore_plugin.php
File metadata and controls
382 lines (338 loc) · 13.7 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* TheSSLStore plugin handler
*/
class ThesslstorePlugin extends Plugin
{
/**
* Init
*/
public function __construct()
{
$this->loadConfig(dirname(__FILE__) . DS . 'config.json');
Language::loadLang("thesslstore_plugin", null, dirname(__FILE__) . DS . "language" . DS);
}
/**
* Returns the name of this plugin
*
* @return string The common name of this plugin
*/
public function getName()
{
return Language::_("TheSSLStorePlugin.name", true);
}
/**
* Performs any necessary bootstraping actions
*
* @param int $plugin_id The ID of the plugin being installed
*/
public function install($plugin_id)
{
Loader::loadModels($this, array('CronTasks'));
$this->addCronTasks($this->getCronTasks());
// Create Email group for expiration reminder email
Loader::loadModels($this, array("Emails", "EmailGroups", "Languages", "Permissions"));
$group = array(
'action' => "Thesslstore.expiration_reminder",
'type' => "client",
'plugin_dir' => "thesslstore",
'tags' => "{client.first_name},{service.name},{service.id},{servicelink},{days}"
);
// Add the custom group
$group_id = $this->EmailGroups->add($group);
// Fetch all currently-installed languages for this company, for which email templates should be created for
$languages = $this->Languages->getAll(Configure::get("Blesta.company_id"));
// Add the email template for each language
foreach ($languages as $language)
{
$data = array(
'email_group_id' => $group_id,
'company_id' => Configure::get('Blesta.company_id'),
'lang' => $language->code,
'from' => 'no-reply@mydomain.com',
'from_name' => 'My Company',
'subject' => 'Certificate Expiration Reminder',
'text' => 'Hello {client.first_name},
This is a reminder that your SSL Certificate ({service.name}) with service ID#{service.id} (http://{servicelink}) is set to expire in {days} days.
Please renew at your earliest convenience.
Thank you!',
'html' => '<p>Hello {client.first_name},</p>
<p>This is a reminder that your SSL Certificate ({service.name}) with service ID#{service.id} (<a href="http://{servicelink}">{servicelink}</a>) is set to expire in {days} days.</p>
<p>Please renew at your earliest convenience.</p>
<p>Thank you!</p>'
);
$this->Emails->add($data);
}
}
/**
* Performs migration of data from $current_version (the current installed version)
* to the given file set version
*
* @param string $current_version The current installed version of this plugin
* @param int $plugin_id The ID of the plugin being upgraded
*/
public function upgrade($current_version, $plugin_id)
{
// Upgrade if possible
if (version_compare($this->getVersion(), $current_version, '>')) {
// Handle the upgrade, set errors using $this->Input->setErrors() if any errors encountered
// Upgrade to 1.2.2
if (version_compare($current_version, '1.2.2', '<')) {
$this->upgrade1_2_2();
}
// Upgrade to 1.3.0
if (version_compare($current_version, '1.3.0', '<')) {
$this->upgrade1_3_0();
}
}
}
/**
* Upgrades to v1.2.2 of the plugin
* Changes email template tags, but leaves existing HTML/text content alone otherwise
*/
private function upgrade1_2_2()
{
// Update all email templates to change the tags
Loader::loadComponents($this, array('Record'));
Loader::loadModels($this, array('EmailGroups'));
if (($email_group = $this->EmailGroups->getByAction('Thesslstore.expiration_reminder'))) {
// Update the email template tags to change the package.name to service.name
$tags = '{client.first_name},{service.name},{service.id},{servicelink},{days}';
$this->Record->where('id', '=', $email_group->id)
->update('email_groups', array('tags' => $tags));
$emails = $this->Record->select(array('id', 'text', 'html'))
->from('emails')
->where('email_group_id', '=', $email_group->id)
->fetchAll();
// Set HTTP protocol for servicelink tag and replace the package.name tag in all emails
foreach ($emails as $email) {
$search = array('{servicelink}', '{package.name}', '{package.');
$replace = array('http://{servicelink}', '{service.name}', '{service.package.');
$vars = array(
'text' => str_replace($search, $replace, $email->text),
'html' => str_replace($search, $replace, $email->html)
);
if ($vars['html'] != $email->html || $vars['text'] != $email->text) {
$this->Record->where('id', '=', $email->id)->update('emails', $vars);
}
}
}
}
/**
* Upgrades to v1.3.0 of the plugin
* Removes service synchronization cron task that was moved to module
*/
private function upgrade1_3_0()
{
Loader::loadModels($this, ['CronTasks']);
// Remove the cron task
$cron_task = $this->CronTasks->getByKey('tss_order_sync', 'thesslstore', 'plugin');
if ($cron_task) {
$this->CronTasks->deleteTask($cron_task->id, 'plugin', 'thesslstore');
}
}
/**
* Performs any necessary cleanup actions
*
* @param int $plugin_id The ID of the plugin being uninstalled
* @param boolean $last_instance True if $plugin_id is the last instance across all companies for this plugin, false otherwise
*/
public function uninstall($plugin_id, $last_instance)
{
Loader::loadModels($this, array("Emails", "EmailGroups", "Languages"));
// Fetch the email template created by this plugin
$group = $this->EmailGroups->getByAction('Thesslstore.expiration_reminder');
// Delete all emails templates belonging to this plugin's email group and company
if ($group) {
$this->Emails->deleteAll($group->id, Configure::get("Blesta.company_id"));
if ($last_instance)
$this->EmailGroups->delete($group->id);
}
if (!isset($this->Record)) {
Loader::loadComponents($this, array('Record'));
}
Loader::loadModels(
$this,
array('CronTasks')
);
$cron_tasks = $this->getCronTasks();
if ($last_instance) {
// Remove the cron tasks
foreach ($cron_tasks as $task) {
$cron_task = $this->CronTasks->getByKey($task['key'], $task['dir'], $task['task_type']);
if ($cron_task) {
$this->CronTasks->deleteTask($cron_task->id, $task['task_type'], $task['dir']);
}
}
}
// Remove individual cron task runs
foreach ($cron_tasks as $task) {
$cron_task_run = $this->CronTasks->getTaskRunByKey($task['key'], $task['dir'], false, $task['task_type']);
if ($cron_task_run) {
$this->CronTasks->deleteTaskRun($cron_task_run->task_run_id);
}
}
}
public function cron($key)
{
if ($key == 'tss_expiration_reminder') {
$this->certificateExpirationReminder();
}
}
/**
* Certificate Expiration Reminder function
*/
private function certificateExpirationReminder()
{
Loader::loadModels($this, array('Clients', 'Emails', 'Services'));
Loader::loadHelpers($this, array('Date', 'Html'));
$this->Date->setTimezone('UTC', Configure::get('Blesta.company_timezone'));
$now = date('c');
$notice_days = 30;
$filters = array(
'renew_start_date' => $this->Clients->dateToUtc(
$this->Date->modify($now, '+' . $notice_days . ' days', 'Y-m-d 00:00:00')
),
'renew_end_date' => $this->Clients->dateToUtc(
$this->Date->modify($now, '+' . $notice_days . ' days', 'Y-m-d 23:59:59')
)
);
// Get the company hostname
$hostname = isset(Configure::get('Blesta.company')->hostname)
? Configure::get('Blesta.company')->hostname
: '';
$client_uri = $hostname . $this->getWebDirectory() . Configure::get('Route.client') . '/';
// Fetch all SSL Store module active/suspended services
$services = $this->getAllServiceIds($filters);
// Send the expiry notices
foreach($services as $service) {
// Fetch the service
if (!($service_obj = $this->Services->get($service->id))) {
continue;
}
// Fetch the client to email
if (!($client = $this->Clients->get($service_obj->client_id))) {
continue;
}
// Send the email
$tags = array(
'client' => $client,
'service' => $service_obj,
'servicelink' => $this->Html->safe($client_uri . 'services/manage/' . $service_obj->id . '/'),
'days' => $notice_days
);
$this->Emails->send(
'Thesslstore.expiration_reminder',
Configure::get('Blesta.company_id'),
$client->settings['language'],
$client->email,
$tags,
null,
null,
null,
array('to_client_id' => $client->id)
);
}
}
/**
* Attempts to add new cron tasks for this plugin
*
* @param array $tasks A list of cron tasks to add
*/
private function addCronTasks(array $tasks)
{
foreach ($tasks as $task) {
$task_id = $this->CronTasks->add($task);
if (!$task_id) {
$cron_task = $this->CronTasks->getByKey(
$task['key'],
$task['dir'],
$task['task_type']
);
if ($cron_task) {
$task_id = $cron_task->id;
}
}
if ($task_id) {
$task_vars = array('enabled' => $task['enabled']);
if ($task['type'] === "time") {
$task_vars['time'] = $task['type_value'];
} else {
$task_vars['interval'] = $task['type_value'];
}
$this->CronTasks->addTaskRun($task_id, $task_vars);
}
}
}
/**
* Retrieves cron tasks available to this plugin along with their default values
*
* @return array A list of cron tasks
*/
private function getCronTasks()
{
return array(
array(
'key' => 'tss_expiration_reminder',
'dir' => 'thesslstore',
'task_type' => 'plugin',
'name' => Language::_('TheSSLStorePlugin.getCronTasks.tss_expiration_reminder_name', true),
'description' => Language::_('TheSSLStorePlugin.getCronTasks.tss_expiration_reminder_desc', true),
'type' => 'time',
'type_value' => '00:00:00',
'enabled' => 1
)
);
}
/**
* Retrieves a list of all service IDs representing active/suspended SSL Store module services for this company
*
* @param array $filters An array of filter options including:
* - renew_start_date The service's renew date to search from
* - renew_end_date The service's renew date to search to
* @return array A list of stdClass objects containing:
* - id The ID of the service
*/
private function getAllServiceIds(array $filters = array())
{
Loader::loadComponents($this, array('Record'));
$this->Record->select(array('services.id'))
->from('services')
->on('service_fields.key', '=', 'thesslstore_order_id')
->innerJoin('service_fields', 'service_fields.service_id', '=', 'services.id', false)
->innerJoin('clients', 'clients.id', '=', 'services.client_id', false)
->innerJoin('client_groups', 'client_groups.id', '=', 'clients.client_group_id', false)
->where('services.status', 'in', array('active', 'suspended'))
->where('client_groups.company_id', '=', Configure::get('Blesta.company_id'));
if (!empty($filters['renew_start_date'])) {
$this->Record->where('services.date_renews', '>=', $filters['renew_start_date']);
}
if (!empty($filters['renew_end_date'])) {
$this->Record->where('services.date_renews', '<=', $filters['renew_end_date']);
}
return $this->Record->group(array('services.id'))
->fetchAll();
}
/**
* Retrieves the web directory
*
* @return string The web directory
*/
private function getWebDirectory()
{
$webdir = WEBDIR;
$is_cli = (empty($_SERVER['REQUEST_URI']));
// Set default webdir if running via CLI
if ($is_cli) {
Loader::loadModels($this, ['Settings']);
$root_web = $this->Settings->getSetting('root_web_dir');
if ($root_web) {
$webdir = str_replace(DS, '/', str_replace(rtrim($root_web->value, DS), '', ROOTWEBDIR));
if (!HTACCESS) {
$webdir .= 'index.php/';
}
}
}
return $webdir;
}
}