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
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setTemplateDirectory($directory) |
39
|
|
|
{ |
40
|
|
|
if (file_exists($directory) === false) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->templateDirectory = $directory; |
45
|
|
|
return file_exists($this->templateDirectory); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Loads a template from the template directory. |
50
|
|
|
* @param $template |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
public function loadTemplate($template) |
55
|
|
|
{ |
56
|
|
|
$targetTemplate = $this->templateDirectory . "$template.html"; |
57
|
|
|
|
58
|
|
|
if (file_exists($this->templateDirectory) === false) { |
59
|
|
|
throw new Exception("Template directory not set!"); |
60
|
|
|
} |
61
|
|
|
if (file_exists($targetTemplate) === false) { |
62
|
|
|
throw new Exception("Requested template does not exist in $targetTemplate"); |
63
|
|
|
} |
64
|
|
|
if (is_readable($targetTemplate) === false) { |
65
|
|
|
throw new Exception("Requested template is not readable ($targetTemplate)"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->template = file_get_contents($targetTemplate); |
69
|
|
|
|
70
|
|
|
return strlen($this->template) > 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setFromName($name) |
74
|
|
|
{ |
75
|
|
|
$this->fromName = $name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Validates an email passed to it, and if valid, sets the from address for the email notification. |
80
|
|
|
* @param $email |
81
|
|
|
* @return bool |
82
|
|
|
* @throws Exception |
83
|
|
|
*/ |
84
|
|
|
|
85
|
|
|
public function setFromAddress($email) |
86
|
|
|
{ |
87
|
|
|
$email = filter_var($email, FILTER_VALIDATE_EMAIL); |
88
|
|
|
|
89
|
|
|
if ($email === false) { |
90
|
|
|
throw new Exception("$email is not a valid email address!"); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->fromAddress = $email; |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setSubjectTemplate($subject) |
99
|
|
|
{ |
100
|
|
|
$this->subjectTemplate = $subject; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Adds a Find And Replace Template pair. These will be used to find {{ TEMPLATETAGS }} and perform a substitution. |
105
|
|
|
* @param $find |
106
|
|
|
* @param $replace |
107
|
|
|
*/ |
108
|
|
|
|
109
|
|
|
public function addFart($find, $replace) |
110
|
|
|
{ |
111
|
|
|
$this->fartDictionary[$find] = $replace; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns unmatched, unreplaced tags from the template. |
116
|
|
|
* @return mixed |
117
|
|
|
* @throws Exception |
118
|
|
|
*/ |
119
|
|
|
|
120
|
|
|
public function getTemplateTags() |
121
|
|
|
{ |
122
|
|
|
if (strlen($this->template) == 0) { |
123
|
|
|
throw new Exception("Template not set!"); |
124
|
|
|
} |
125
|
|
|
return $this->getTags($this->template); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getBodyTags() |
129
|
|
|
{ |
130
|
|
|
return $this->getTags($this->body); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getTags($body) |
134
|
|
|
{ |
135
|
|
|
$matches = []; |
136
|
|
|
preg_match_all($this->tagPattern, $body, $matches, PREG_PATTERN_ORDER); |
137
|
|
|
return $matches[0]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getHooks($body) |
141
|
|
|
{ |
142
|
|
|
$matches = []; |
143
|
|
|
preg_match_all($this->hookPattern, $body, $matches, PREG_PATTERN_ORDER); |
144
|
|
|
return $matches[0]; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function makeTag($find) |
148
|
|
|
{ |
149
|
|
|
return sprintf("{{%s}}", strtoupper($find)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function matchFind($tag, $find) |
153
|
|
|
{ |
154
|
|
|
//Remove spaces |
155
|
|
|
$tag = str_replace(" ", '', $tag); |
156
|
|
|
//Decorate the find |
157
|
|
|
$find = $this->makeTag($find); |
158
|
|
|
return (strcmp($tag, $find) === 0); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $body |
163
|
|
|
* @throws Exception |
164
|
|
|
*/ |
165
|
|
|
|
166
|
|
|
private function doFart($body) |
167
|
|
|
{ |
168
|
|
|
$tags = $this->getTemplateTags(); |
169
|
|
|
foreach ($tags as $tag) { |
170
|
|
|
foreach ($this->fartDictionary as $find => $replace) { |
171
|
|
|
if ($this->matchFind($tag, $find)) { |
172
|
|
|
$body = str_replace($tag, $replace, $body); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
$tags = $this->getBodyTags(); |
177
|
|
|
|
178
|
|
|
if (count($tags) > 0) { |
179
|
|
|
$this->doFart($this->body); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $body; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function render() |
186
|
|
|
{ |
187
|
|
|
$this->body = $this->doFart($this->template); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function addHook($hook, $callback) |
191
|
|
|
{ |
192
|
|
|
$this->hooks[$hook] = $callback; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getLabel($subject) |
196
|
|
|
{ |
197
|
|
|
$matches = []; |
198
|
|
|
|
199
|
|
|
switch ($this->getTagType($subject)) { |
200
|
|
|
case 'tag': |
201
|
|
|
preg_match($this->tagPattern, $subject, $matches); |
202
|
|
|
break; |
203
|
|
|
case 'hook': |
204
|
|
|
preg_match($this->hookPattern, $subject, $matches); |
205
|
|
|
break; |
206
|
|
|
default: |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $matches[1]; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getTagType($tag) |
214
|
|
|
{ |
215
|
|
|
if (preg_match($this->tagPattern, $tag) > 0) { |
216
|
|
|
return 'tag'; |
217
|
|
|
} |
218
|
|
|
if (preg_match($this->hookPattern, $tag) > 0) { |
219
|
|
|
return 'hook'; |
220
|
|
|
} |
221
|
|
|
return false; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param $hook |
226
|
|
|
* @return mixed |
227
|
|
|
* @throws Exception |
228
|
|
|
*/ |
229
|
|
|
|
230
|
|
|
public function renderHook($hook) |
231
|
|
|
{ |
232
|
|
|
|
233
|
|
|
$TagFactory = new TagFactory(); |
234
|
|
|
$Tag = $TagFactory->getTag($hook); |
235
|
|
|
$action = $Tag->getLabel(); |
236
|
|
|
|
237
|
|
|
//1. Replace template tags here. |
238
|
|
|
if(count($Tag->getArgs()) > 0) { |
239
|
|
|
$args = array_map([$this,'doFart'], $Tag->getArgs()); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
//2. Lookup the callback in the hooks dictionary. |
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 in " . __FILE__ . ":" . __LINE__); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return (count($Tag->getArgs()) == 0 ? $this->$callback() : $this->$callback($args)); |
|
|
|
|
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
|
|
|
} |
280
|
|
|
|