1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* {CLASS SUMMARY} |
4
|
|
|
* |
5
|
|
|
* Date: 7/26/18 |
6
|
|
|
* Time: 6:54 PM |
7
|
|
|
* @author Michael Munger <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace HPHIO\Farret; |
11
|
|
|
|
12
|
|
|
use \Exception; |
13
|
|
|
|
14
|
|
|
class Notif |
15
|
|
|
{ |
16
|
|
|
private $tagPattern = '/\{{2} {0,2}([A-Z]+) {0,2}\}{2}/'; |
17
|
|
|
private $hookPattern = '/(?:{\%\s{0,})([A-Z]+)\s{0,}((\|(?:{{){0,1}[A-Za-z0-9-]+(?:}}){0,1}\s{0,}){0,})(?:\%})/'; |
18
|
|
|
|
19
|
|
|
public $templateDirectory = null; |
20
|
|
|
public $template = null; |
21
|
|
|
public $fromName = null; |
22
|
|
|
public $fromAddress = null; |
23
|
|
|
public $subjectTemplate = null; |
24
|
|
|
public $body = null; |
25
|
|
|
|
26
|
|
|
public $to = []; |
27
|
|
|
public $cc = []; |
28
|
|
|
public $bcc = []; |
29
|
|
|
public $fartDictionary = []; |
30
|
|
|
|
31
|
|
|
public $hooks = []; |
32
|
|
|
|
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->addHook('DATE', 'getDate'); |
36
|
|
|
$this->addHook('HASH', 'hash'); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setBody($body) { |
41
|
|
|
$this->body = $body; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getBody() { |
45
|
|
|
return $this->body; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setTemplate($template) { |
49
|
|
|
$this->template = $template; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getTemplate() { |
53
|
|
|
return $this->template; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setTemplateDirectory($directory) |
57
|
|
|
{ |
58
|
|
|
if (file_exists($directory) === false) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->templateDirectory = $directory; |
63
|
|
|
return file_exists($this->templateDirectory); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Loads a template from the template directory. |
68
|
|
|
* @param $template |
69
|
|
|
* @throws Exception |
70
|
|
|
*/ |
71
|
|
|
|
72
|
|
|
public function loadTemplate($template) |
73
|
|
|
{ |
74
|
|
|
$targetTemplate = $this->templateDirectory . "$template.html"; |
75
|
|
|
|
76
|
|
|
if (file_exists($this->templateDirectory) === false) { |
77
|
|
|
throw new Exception("Template directory not set!"); |
78
|
|
|
} |
79
|
|
|
if (file_exists($targetTemplate) === false) { |
80
|
|
|
throw new Exception("Requested template does not exist in $targetTemplate"); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->template = file_get_contents($targetTemplate); |
84
|
|
|
|
85
|
|
|
return strlen($this->template) > 0; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function setFromName($name) |
89
|
|
|
{ |
90
|
|
|
$this->fromName = $name; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validates an email passed to it, and if valid, sets the from address for the email notification. |
95
|
|
|
* @param $email |
96
|
|
|
* @return bool |
97
|
|
|
* @throws Exception |
98
|
|
|
*/ |
99
|
|
|
|
100
|
|
|
public function setFromAddress($email) |
101
|
|
|
{ |
102
|
|
|
$email = filter_var($email, FILTER_VALIDATE_EMAIL); |
103
|
|
|
|
104
|
|
|
if ($email === false) { |
105
|
|
|
throw new Exception("$email is not a valid email address!"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->fromAddress = $email; |
109
|
|
|
|
110
|
|
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function setSubjectTemplate($subject) |
114
|
|
|
{ |
115
|
|
|
$this->subjectTemplate = $subject; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Adds a Find And Replace Template pair. These will be used to find {{ TEMPLATETAGS }} and perform a substitution. |
120
|
|
|
* @param $find |
121
|
|
|
* @param $replace |
122
|
|
|
*/ |
123
|
|
|
|
124
|
|
|
public function addFart($find, $replace) |
125
|
|
|
{ |
126
|
|
|
$this->fartDictionary[$find] = $replace; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns unmatched, unreplaced tags from the template. |
131
|
|
|
* @return mixed |
132
|
|
|
* @throws Exception |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
public function getTemplateTags() |
136
|
|
|
{ |
137
|
|
|
if (strlen($this->template) == 0) { |
138
|
|
|
throw new Exception("Template not set!"); |
139
|
|
|
} |
140
|
|
|
return $this->getTags($this->template); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getBodyTags() |
144
|
|
|
{ |
145
|
|
|
return $this->getTags($this->body); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getTags($body) |
149
|
|
|
{ |
150
|
|
|
$matches = []; |
151
|
|
|
preg_match_all($this->tagPattern, $body, $matches, PREG_PATTERN_ORDER); |
152
|
|
|
|
153
|
|
|
$TagFactory = new TagFactory(); |
154
|
|
|
$buffer = []; |
155
|
|
|
|
156
|
|
|
foreach($matches[0] as $match) { |
157
|
|
|
$tag = $TagFactory->getTag($match); |
158
|
|
|
$buffer[] = $tag; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $buffer; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getHooks($body) |
165
|
|
|
{ |
166
|
|
|
$matches = []; |
167
|
|
|
preg_match_all($this->hookPattern, $body, $matches, PREG_PATTERN_ORDER); |
168
|
|
|
return $matches[0]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function makeTag($find) |
172
|
|
|
{ |
173
|
|
|
return sprintf("{{%s}}", strtoupper($find)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function matchFind($tag, $find) |
177
|
|
|
{ |
178
|
|
|
//Remove spaces |
179
|
|
|
$tag = str_replace(" ", '', $tag); |
180
|
|
|
//Decorate the find |
181
|
|
|
$find = $this->makeTag($find); |
182
|
|
|
return (strcmp($tag, $find) === 0); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $body |
187
|
|
|
* @throws Exception |
188
|
|
|
*/ |
189
|
|
|
|
190
|
|
|
private function doFart($body) |
191
|
|
|
{ |
192
|
|
|
$tags = $this->getTemplateTags(); |
193
|
|
|
foreach ($tags as $tag) { |
194
|
|
|
|
195
|
|
|
$tag->fart($this->fartDictionary); |
196
|
|
|
|
197
|
|
|
if(strpos($body, $tag->getTag()) === false) { |
198
|
|
|
continue; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$body = str_replace($tag->getTag(), $tag->getReplacement(), $body); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$tags = $this->getTags($body); |
205
|
|
|
|
206
|
|
|
if (count($tags) > 0) { |
207
|
|
|
$body = $this->doFart($body); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $body; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function render() |
214
|
|
|
{ |
215
|
|
|
$this->body = $this->doFart($this->template); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function addHook($hook, $callback) |
219
|
|
|
{ |
220
|
|
|
$this->hooks[$hook] = $callback; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $hook |
225
|
|
|
* @return mixed |
226
|
|
|
* @throws Exception |
227
|
|
|
*/ |
228
|
|
|
|
229
|
|
|
public function renderHook($hook) |
230
|
|
|
{ |
231
|
|
|
|
232
|
|
|
$TagFactory = new TagFactory(); |
233
|
|
|
$Tag = $TagFactory->getTag($hook); |
234
|
|
|
$Tag->fart($this->fartDictionary); |
235
|
|
|
$action = $Tag->getLabel(); |
236
|
|
|
|
237
|
|
|
//2. Lookup the callback in the hooks dictionary. |
238
|
|
|
|
239
|
|
|
if(isset($this->hooks[$action]) === false) { |
240
|
|
|
throw new Exception('The callback you requested is not registered in the notification hooks.'); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
$callback = $this->hooks[$action]; |
244
|
|
|
|
245
|
|
|
if (method_exists($this, $callback) === false) { |
246
|
|
|
throw new Exception("Hook method does not exist! Cannot execute $callback."); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return (count($Tag->getArgs()) == 0 ? $this->$callback() : $this->$callback($Tag->getArgs())); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function getDate($formatArray) |
253
|
|
|
{ |
254
|
|
|
$now = new \DateTime(); |
255
|
|
|
return $now->format($formatArray[0]); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function getCurrentMonth() |
259
|
|
|
{ |
260
|
|
|
return $this->getDate(["m"]); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function getCurrentDay() |
264
|
|
|
{ |
265
|
|
|
return $this->getDate(["d"]); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public function getCurrentYear() |
269
|
|
|
{ |
270
|
|
|
return $this->getDate(["Y"]); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function getArgs($hook) { |
274
|
|
|
$Factory = new TagFactory(); |
275
|
|
|
$Tag = $Factory->getTag($hook); |
276
|
|
|
return $Tag->getArgs(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function hash($args) { |
280
|
|
|
$buffer = ''; |
281
|
|
|
for($x = 0; $x < count($args); $x++ ) { |
|
|
|
|
282
|
|
|
$buffer = md5($buffer.$args[$x]); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $buffer; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: