-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.php
109 lines (81 loc) · 2.67 KB
/
bootstrap.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
<?php
// API
$this->module("mailqueue")->extend([
// what tablename to use in the datastore
"tablename" => "mailqueue",
// constants
"STATUS_PENDING" => "pending",
"STATUS_SENT" => "sent",
/* Adds an email to be sent to the queue */
"add" => function($to, $subject, $message) use($app) {
$data = [
"to" => $to,
"subject" => $subject,
"message" => $message,
"status" => $this->STATUS_PENDING
];
$app->module("datastore")->save_entry($this->tablename, $data);
},
/* Add several emails to the queue.
* $bulkdata = [
* ["to" => "[email protected]", "subject" => "Hi", "message" => "Hello World"],
* ...
* ];
*/
"addBulk" => function($bulkdata) use($app) {
foreach ($bulkdata as $entry) {
$this->add($entry['to'], $entry['subject'], $entry['message']);
}
},
/*
* Check if the queue is empty, meaning there ae no pending emails.
*/
"isEmpty" => function() use($app) {
return $this->count() === 0;
},
/*
* Returns the number of pending emails
*/
"count" => function() use($app) {
return count($this->getPending());
},
/* Get all pending emails */
"getPending" => function($limit=null) use($app) {
$filter = ["status" => $this->STATUS_PENDING];
$params = ["filter" => $filter];
if($limit !== null) {
$params["limit"] = $limit;
}
return $app->module("datastore")->find($this->tablename, $params)->toArray();
},
/*
* Processes the given number of pending entries and sends emails out
*/
"process" => function($step=10) use($app) {
$entries = $this->getPending($limit = $step);
foreach ($entries as $entry) {
// send
$app->mailer->mail($entry['to'], $entry['subject'], $entry['message']);
// update entry status
$entry['status'] = $this->STATUS_SENT;
$app->module("datastore")->save_entry($this->tablename, $entry);
}
return count($entries);
}
]);
$this->module("datastore")->extend([
"get_or_create_datastore" => function($name) use($app) {
$datastore = $this->get_datastore($name);
if (!$datastore) {
$datastore = [
"name" => $name,
"modified" => time()
];
$datastore["created"] = $datastore["modified"];
$app->db->save("common/datastore", $datastore);
}
return $datastore;
}
]);
// ADMIN
if (COCKPIT_ADMIN && !COCKPIT_REST) include_once(__DIR__.'/admin.php');