|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* @copyright 2015 LibreWorks contributors |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A Swift spool that uses PDO. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2015 LibreWorks contributors |
|
12
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
13
|
|
|
*/ |
|
14
|
|
|
class Swift_PdoSpool extends Swift_ConfigurableSpool |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \PDO The database driver |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $pdo; |
|
20
|
|
|
protected $table; |
|
21
|
|
|
protected $pkey; |
|
22
|
|
|
protected $messageField; |
|
23
|
|
|
protected $timeField; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Creates a new PdoSpool |
|
27
|
|
|
* |
|
28
|
|
|
* @param \PDO $pdo The database driver |
|
29
|
|
|
* @param string $table The table name |
|
30
|
|
|
* @param string $pkey The primary key field |
|
31
|
|
|
* @param string $messageField The field to add serialized message data |
|
32
|
|
|
* @param string $timeField The integer field to store message timestamp |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function __construct(\PDO $pdo, $table, $pkey, $messageField, $timeField) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$this->pdo = $pdo; |
|
37
|
2 |
|
$this->table = $this->checkBlank($table); |
|
38
|
2 |
|
$this->pkey = $this->checkBlank($pkey); |
|
39
|
2 |
|
$this->messageField = $this->checkBlank($messageField); |
|
40
|
2 |
|
$this->timeField = $this->checkBlank($timeField); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
private function checkBlank($value) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$value = trim($value); |
|
46
|
1 |
|
if (strlen($value) == 0) { |
|
47
|
1 |
|
throw new \InvalidArgumentException("This parameter cannot be blank"); |
|
48
|
|
|
} |
|
49
|
1 |
|
return $value; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Tests if this Spool mechanism has started. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function isStarted() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Starts this Spool mechanism. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function start() |
|
66
|
|
|
{ |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Stops this Spool mechanism. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function stop() |
|
73
|
|
|
{ |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Queues a message. |
|
78
|
|
|
* |
|
79
|
|
|
* @param Swift_Mime_SimpleMessage $message The message to store |
|
80
|
|
|
* |
|
81
|
|
|
* @throws Swift_IoException |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function queueMessage(Swift_Mime_SimpleMessage $message) |
|
86
|
|
|
{ |
|
87
|
|
|
try { |
|
88
|
1 |
|
$stmt = $this->pdo->prepare("INSERT INTO " . $this->table . " (" |
|
89
|
1 |
|
. $this->messageField . ") VALUES (?)"); |
|
90
|
1 |
|
$stmt->bindValue(1, serialize($message), PDO::PARAM_STR); |
|
91
|
1 |
|
$stmt->execute(); |
|
92
|
1 |
|
return true; |
|
93
|
|
|
} catch (\Exception $e) { |
|
94
|
|
|
throw new Swift_IoException("Could not enqueue message", 0, $e); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Execute a recovery if for any reason a process is sending for too long. |
|
100
|
|
|
* |
|
101
|
|
|
* @param int $timeout in second Defaults is for very slow smtp responses |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function recover($timeout = 900) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
try { |
|
106
|
1 |
|
$stmt = $this->pdo->prepare('UPDATE ' . $this->table . ' SET ' |
|
107
|
1 |
|
. $this->timeField . ' = NULL WHERE ' . $this->timeField . ' <= ?'); |
|
108
|
1 |
|
$stmt->bindValue(1, time() - 900, PDO::PARAM_INT); |
|
109
|
1 |
|
$stmt->execute(); |
|
110
|
|
|
} catch (\Exception $e) { |
|
111
|
|
|
throw new Swift_IoException("Could not update email sent on date", 0, $e); |
|
112
|
|
|
} |
|
113
|
1 |
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sends messages using the given transport instance. |
|
117
|
|
|
* |
|
118
|
|
|
* @param Swift_Transport $transport A transport instance |
|
119
|
|
|
* @param string[] $failedRecipients An array of failures by-reference |
|
120
|
|
|
* |
|
121
|
|
|
* @return int The number of sent e-mails |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function flushQueue(Swift_Transport $transport, &$failedRecipients = null) |
|
124
|
|
|
{ |
|
125
|
1 |
|
if (!$transport->isStarted()) { |
|
126
|
|
|
$transport->start(); |
|
127
|
|
|
} |
|
128
|
1 |
|
$limit = $this->getMessageLimit(); |
|
129
|
1 |
|
$limit = $limit > 0 ? $limit : null; |
|
130
|
1 |
|
$failedRecipients = (array) $failedRecipients; |
|
131
|
1 |
|
$count = 0; |
|
132
|
1 |
|
$time = time(); |
|
133
|
1 |
|
$timeLimit = $this->getTimeLimit(); |
|
134
|
|
|
try { |
|
135
|
1 |
|
$results = $this->pdo->query('SELECT ' . $this->pkey . ' as pkey, ' |
|
136
|
1 |
|
. $this->messageField . ' as email FROM ' . $this->table . ' WHERE ' |
|
137
|
1 |
|
. $this->timeField . ' IS NULL'); |
|
138
|
1 |
|
$ustmt = $this->pdo->prepare('UPDATE ' . $this->table . ' SET ' |
|
139
|
1 |
|
. $this->timeField . ' = ? WHERE ' . $this->pkey . ' = ?'); |
|
140
|
1 |
|
$dstmt = $this->pdo->prepare('DELETE FROM ' . $this->table . ' WHERE ' |
|
141
|
1 |
|
. $this->pkey . ' = ?'); |
|
142
|
1 |
|
foreach ($results->fetchAll(PDO::FETCH_COLUMN) as $result) { |
|
143
|
1 |
|
$id = $result[0]; |
|
144
|
1 |
|
$ustmt->execute(array(time(), $result[0])); |
|
145
|
1 |
|
$message = unserialize($result[1]); |
|
146
|
1 |
|
$count += $transport->send($message, $failedRecipients); |
|
|
|
|
|
|
147
|
1 |
|
$dstmt->execute(array($id)); |
|
148
|
1 |
|
if ($limit && $count >= $limit) { |
|
|
|
|
|
|
149
|
|
|
break; |
|
150
|
|
|
} |
|
151
|
1 |
|
if ($timeLimit && (time() - $time) >= $timeLimit) { |
|
152
|
1 |
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
1 |
|
return $count; |
|
156
|
|
|
} catch (\Exception $e) { |
|
157
|
|
|
throw new Swift_IoException("Could not access database", 0, $e); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.