forked from arunshekher/mentions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MentionsNotification.php
828 lines (657 loc) · 17.1 KB
/
MentionsNotification.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
<?php
if ( ! defined('e107_INIT')) {
exit;
}
class MentionsNotification extends Mentions
{
private $mentioneeData;
private $mentions;
private $mentioner;
private $date;
private $tag;
private $title;
private $link;
private $commentType;
private $mail;
private $eventData;
/**
* MentionsNotification constructor.
*
*/
public function __construct()
{
Mentions::__construct();
// $this->mail = e107::getEmail();
}
/**
* Sets the properties for MentionsNotification object.
*
* @param array $properties
*/
private function setProperties(array $properties)
{
foreach ($properties as $name => $value) {
if (null !== $value) {
$this->$name = $value;
}
}
}
/**
* Sets tag - an identifier that helps the ensuing member methods differentiate
* - between what they are dealing with - 'forum', 'chatbox' or 'comment'
* @param mixed $tag
*/
public function setTag($tag)
{
$this->tag = $tag;
}
/**
* Sets eventData
* @param mixed $eventData
* @param bool $appendFlag
*/
public function setEventData($eventData, $appendFlag = false)
{
if ( ! $appendFlag) {
$this->eventData = $eventData;
} else {
$this->eventData = array_merge($this->eventData, $eventData);
}
}
/**
* Sets comment type
* @param mixed $commentType
*/
public function setCommentType($commentType)
{
$this->commentType = $commentType;
}
/**
* Public static alias for MentionsNotification::perform()
*
*/
public static function execute()
{
$notification = new MentionsNotification;
$notification->perform();
}
/**
* Listens to 'user_chatbox_post_created', 'user_comment_posted'
* - and 'user_forum_post_created' event triggers and performs
* - mentions notification by e-mail.
*
*/
private function perform()
{
if (USER && $this->prefs['mentions_active']
&& (strtolower($_SERVER['REQUEST_METHOD']) === 'post'
|| e_AJAX_REQUEST)) {
if ($this->prefs['notify_chatbox_mentions']) {
e107::getEvent()->register('user_chatbox_post_created',
['MentionsNotification', 'chatbox']);
}
if ($this->prefs['notify_comment_mentions']
&& ($this->prefs['mentions_contexts'] === 2
|| $this->prefs['mentions_contexts'] === 3)) {
e107::getEvent()->register('user_comment_posted',
['MentionsNotification', 'comment']);
}
if ($this->prefs['notify_forum_mentions']) {
// forum 'thread' and 'reply' covered - for now
e107::getEvent()->register('user_forum_post_created',
['MentionsNotification', 'forum']);
}
}
}
/**
* Does 'Chatbox' mentions notifications using 'user_chatbox_post_created'
* - event data.
*
* @param array $data
* 'Chatbox' event trigger _POST data
* @return bool
* Returns false if no mention in 'cmessage'.
*/
public function chatbox($data)
{
// set tag - chatbox
$this->setTag(LAN_MENTIONS_TAG_CHATBOX);
// Debug
// $this->log($data, 'chatbox-event-data');
// if no mentions abort
if ( ! $this->hasAtSign($data['cmessage'])) {
return false;
}
$mentions = $this->fetchAllMentions($data['cmessage']);
if ($mentions) {
$this->setProperties([
'mentions' => $mentions,
'mentioner' => USERNAME,
'date' => $this->getDate($data['datestamp']),
'link' => $this->getContentLink()
]);
// notify
$this->notifyAll();
}
unset($mentions);
}
/**
* Does comments mentions notifications using 'user_comment_posted'
* - event data.
*
* @param array $data
* 'Comments' event trigger _POST data
*
* @return bool
* Returns false if no mention in 'comment_comment'.
* @todo revisit to tidy up the sequence of routines
*/
public function comment($data)
{
// set tag - comment
$this->setTag(LAN_MENTIONS_TAG_COMMENT);
// Debug
// $this->log($data, 'comments-event-data');
// if no '@' signs or if comment is blocked - abort
$hasAt = $this->hasAtSign($data['comment_comment']);
if ( ! $hasAt || $data['comment_blocked'] ) {
return false;
}
// get mentions
$mentions = $this->fetchAllMentions($data['comment_comment']);
if ($mentions) {
$this->setCommentType($this->solveCommentType($data['comment_type']));
$this->setEventData($data);
$this->setProperties([
'mentions' => $mentions,
'mentioner' => $data['comment_author_name'],
'date' => $this->getDate($data['comment_datestamp']),
'title' => $data['comment_subject'],
'link' => $this->getContentLink()
]);
// notify
$this->notifyAll();
}
unset($mentions);
}
/**
* Does forum mentions notifications using 'user_forum_post_created'
* - event data.
*
* @param array $data
* Forum event trigger _POST data
* @return bool
* Returns false if no mention in 'post_entry'.
* @todo revisit to tidy up the sequence of routines
*/
public function forum($data)
{
// set tag - forum
$this->setTag(LAN_MENTIONS_TAG_FORUM);
// Debug
// $this->log($data, 'forum-event-data');
// if no mentions abort
if ( ! $this->hasAtSign($data['post_entry'])) {
return false;
}
// get mentions
$mentions = $this->fetchAllMentions($data['post_entry']);
if ($mentions) {
$this->setEventData($data);
$this->setProperties([
'mentions' => $mentions,
'mentioner' => USERNAME,
'date' => $this->getDate($data['post_datestamp'])
]);
// get more forum data
$forumInfo = $this->getRequisiteForumData($data['post_thread']);
$this->setEventData($forumInfo, true);
$title = $forumInfo['thread_name'];
$link = $this->getContentLink();
// set more properties
$this->setProperties([
'title' => $title,
'link' => $link
]);
// notify
$this->notifyAll();
}
unset($mentions);
}
/**
* Checks if the string passed in has an '@' sign
*
* @param string $input
* String passed in as argument
* @return bool
*/
protected function hasAtSign($input)
{
return strpos($input, '@') !== false;
}
/**
* Fetches all mentions in the message
*
* @param string $message
* String to match for mentions
* @return array|null
*/
private function fetchAllMentions($message)
{
$pattern = '/(?<=\W|^)@([a-z0-9_.]*)/mis';
if (preg_match_all($pattern, $message, $matches) !== false) {
return $matches[0] ?: null;
}
return null;
}
/**
* Filters mentions array for duplicates and 'mentioner' themselves.
* @param $mentions
* Mentions array passed by reference.
* @return array
* Filtered array.
*/
private function filterMentions(&$mentions)
{
$mentions = array_values(array_unique($mentions, SORT_STRING));
foreach ($mentions as $key => $value) {
if ($this->mentioner === $this->stripAtFrom($value)) {
unset($mentions[$key]);
}
}
return $mentions;
}
/**
* Prepares mention date
*
* @param integer $date
* Timestamp to be formatted
* @param string $format
* Format type identifier - short | long | relative
* @return string
* Formatted date as html
*/
private function getDate($date, $format = 'long')
{
return $this->parse->toDate($date, $format);
}
/**
* Notify each and every 'mentionee' in _POST data
* - except the 'mentioner' themself
*
*/
private function notifyAll()
{
$mentions = $this->mentions;
if (null === $mentions || $mentions !== (array)$mentions) {
return false;
}
// filter mentions for duplicates and 'mentioner' themself
$uniqueMentions = $this->filterMentions($mentions);
// Debug
$this->log($uniqueMentions, 'unique-mentions');
$maxEmails = $this->prefs['max_emails'];
for ($i = 0; $i < $maxEmails; $i++ ) {
// being paranoid - the 'mentioner' should NEVER get an email.
$mentionee = $this->stripAtFrom($uniqueMentions[$i]);
if ($this->mentioner === $mentionee) {
continue;
}
// get 'mentionee' details - email, username, userid
$this->mentioneeData = $this->getUserData($uniqueMentions[$i]);
// send email
if (null !== $this->mentioneeData['user_email']
&& null !== $this->mentioneeData['user_name']) {
$this->dispatchEmail();
continue;
}
}
}
/**
* Dispatches email to the mentioned user
*
* @return bool
* true if success false if failure.
*/
private function dispatchEmail()
{
$mail = e107::getEmail();
$emailContent = [
'email_subject' => $this->emailSubject(),
'send_html' => true,
'email_body' => $this->emailBody(),
'template' => 'default',
'e107_header' => $this->mentioneeData['user_id'],
'extra_header' => 'X-e107-Plugin : Mentions-Plugin-v'
];
$userEmail = $this->mentioneeData['user_email'];
$userName = $this->mentioneeData['user_name'];
// send email
$emailSent = $mail->sendEmail($userEmail, $userName, $emailContent);
// Debug
$this->log($emailContent, 'email-content-array-log');
if (true === $emailSent) {
$emailContent = null;
$mail = null;
//unset($emailContent, $mail);
return $emailSent;
}
// Debug
$this->log($emailSent, 'email-send-error-log');
return false;
}
/**
* Parses and returns email body
*
* @return string
*/
private function emailBody()
{
$bodyVars = [
'MENTIONEE' => $this->mentioneeData['user_name'],
'MENTIONER' => $this->mentioner,
'MENTION_TEXT' => $this->emailText(),
];
return $this->parse->simpleParse($this->emailTemplate(), $bodyVars);
}
/**
* Returns email content html
*
* @return string
* Html for email content
*/
private function emailTemplate()
{
$template = e107::getTemplate('mentions', 'email');
if (empty($template)) {
$template = '<div>
<p>' . LAN_MENTIONS_EMAIL_HELLO . ' {MENTIONEE},</p>
<p>{MENTION_TEXT}</p>
</div>';
}
return $template;
}
/**
* Returns mention 'email notification citation' based on content tag
*
* @return string
* Notification email passage/citation.
* @internal param string $tag
* Tag name of the 'content type' for which the email text is requested.
*/
private function emailText()
{
switch ($this->tag) {
case LAN_MENTIONS_TAG_CHATBOX:
return $this->chatboxMailText();
break;
case LAN_MENTIONS_TAG_COMMENT:
return $this->commentMailText();
break;
case LAN_MENTIONS_TAG_FORUM:
return $this->forumMailText();
break;
default:
return $this->parse->lanVars(LAN_MENTIONS_EMAIL_VERSE_UNRESOLVED,
$this->mentioner);
break;
}
}
/**
* Parses and returns text for 'forum' notification email.
* @return string
* Parsed text passage for 'forum' notification email.
*/
private function forumMailText()
{
$link = '<a href="' . $this->link . '">\'' . $this->title . '\'</a>';
$vars = [
'user' => $this->mentioner,
'tag' => $this->tag,
'date' => $this->date,
'link' => $link,
];
return $this->parse->lanVars(LAN_MENTIONS_EMAIL_VERSE_FORUM_NEW, $vars);
}
/**
* Parses and returns text for 'comment' notification email.
* @return string
* Parsed text passage for 'comment' notification email.
*/
private function commentMailText()
{
if (empty($this->link)) {
$link = $this->title;
} else {
$link = '<a href="' . $this->link . '">\'' . $this->title . '\'</a>';
}
$vars = [
'user' => $this->mentioner,
'tag' => $this->tag,
'date' => $this->date,
'type' => $this->commentType,
'title' => $this->title,
'link' => $link
];
return $this->parse->lanVars(LAN_MENTIONS_EMAIL_VERSE_COMMENT_NEW, $vars);
}
/**
* Parses and returns text for 'chatbox' notification email.
* @return string
* Parsed text passage for 'chatbox' notification email.
*/
private function chatboxMailText()
{
$link = '<a href="' . $this->link . '">' . $this->tag . '</a>';
$vars = [
'user' => $this->mentioner,
'date' => $this->date,
'link' => $link
];
return $this->parse->lanVars(LAN_MENTIONS_EMAIL_VERSE_CHATBOX_NEW, $vars);
}
/**
* Fetches the subject line for email based on plugin preference
*
* @return string
* Email subject line.
*/
public function emailSubject()
{
$subjectLine = trim($this->prefs['email_subject_line']);
if (null !== $subjectLine && $subjectLine !== '') {
return str_replace('{MENTIONER}', $this->mentioner, $subjectLine);
}
return LAN_MENTIONS_EMAIL_SUBJECTLINE . $this->mentioner;
}
/**
* Decides comment's inheritor's 'name' from 'comment_type'
*
* @param mixed $input
* 'comment_type' obtained from event data
* @return string
* The name of 'comment_type' in words.
*/
protected function solveCommentType($input)
{
if (ctype_digit($input)) {
return $this->nameCommentType($input);
}
return $input;
}
/**
* Returns 'comment_type' name based on the current
* - e107 numerical 'comment_types' specification.
*
* @param int $number
*
* @return string
* 'comment_type' string
*/
private function nameCommentType($number)
{
$number = (int)$number;
switch ($number) {
case 0:
return LAN_MENTIONS_COMMENT_NEWS;
case 4:
return LAN_MENTIONS_COMMENT_POLL;
case 2:
return LAN_MENTIONS_COMMENT_DOWNLOADS;
default:
return LAN_MENTIONS_COMMENT_UNKNOWN;
}
}
/**
* Get forum thread 'title' and other requisite data from 'forum' &
* - 'forum_thread' tables
*
* @param integer $thread_id
* Id of forum thread.
* @return array|bool
* Data array or false on error.
*/
private function getRequisiteForumData($thread_id)
{
$sql = e107::getDb();
$thread_id = (int)$thread_id;
$query = "SELECT f.forum_sef, f.forum_id, ft.thread_id, ft.thread_name FROM `#forum` AS f "
. "LEFT JOIN `#forum_thread` AS ft ON f.forum_id = ft.thread_forum_id "
. " WHERE ft.thread_id = {$thread_id} ";
$result = $sql->gen($query);
if ($result) {
return $sql->fetch($result);
}
return $result;
}
/**
* Returns the content entity links for notification email.
*
* @return string
* html markup for the content link
*/
private function getContentLink()
{
switch ($this->tag) {
case LAN_MENTIONS_TAG_CHATBOX:
return SITEURLBASE . e_PLUGIN_ABS . 'chatbox_menu/chat.php';
break;
case LAN_MENTIONS_TAG_COMMENT: // news, downloads, polls, and other third party plugins
return $this->getCommentItemLink();
break;
case LAN_MENTIONS_TAG_FORUM:
return $this->getForumItemLink();
break;
default:
return '[unresolved]';
break;
}
}
/**
* Parse and return 'forum' item link
* @return string
* Forum item URL
*/
private function getForumItemLink()
{
$data = [
'forum_sef' => $this->eventData['forum_sef'],
'thread_id' => $this->eventData['thread_id'],
'thread_sef' => $this->createSlug($this->eventData['thread_name'])
];
$opt = $this->getLinkOptions('forum');
return e107::url('forum', 'topic', $data, $opt);
}
/**
* Parse and return 'comment' items link based on comment type.
* @return string
* Comment item URL
*/
private function getCommentItemLink()
{
$type = $this->commentType;
$opt = $this->getLinkOptions($type);
switch ($type) {
case LAN_MENTIONS_COMMENT_NEWS: // news
$news = [
'news_id' => $this->eventData['comment_item_id'],
'news_sef' => $this->createSlug($this->eventData['comment_subject'])
];
return e107::getUrl()->create('news/view/item', $news, $opt);
//return e107::url('news/view', 'item', $news, ['mode' => full]); // todo: find out if this will work for news
break;
case LAN_MENTIONS_COMMENT_DOWNLOADS: // downloads
$download = [
'download_id' => $this->eventData['comment_item_id'],
'download_sef' => $this->createSlug($this->eventData['comment_subject'])
];
return e107::url('download', 'item', $download, $opt);
break;
case LAN_MENTIONS_COMMENT_POLL: // poll
// does not support on the fly url generation now I suppose - plugin has no e_url addon
return SITEURLBASE . e_PLUGIN_ABS . 'poll/oldpolls.php?' . $this->eventData['comment_item_id'];
break;
case 'page':
$page = [
'page_id' => $this->eventData['comment_item_id'],
'page_title' => $this->eventData['comment_subject'],
'page_sef' => $this->createSlug($this->eventData['comment_subject'])
];
return e107::getUrl()->create('page/view', $page, $opt);
break;
case LAN_MENTIONS_COMMENT_UNKNOWN:
return SITEURLBASE;
break;
default:
return null;
}
}
/**
* Create slug from title
* @param string $title
* @param string $type
*
* @return string
* The slug string
*/
private function createSlug($title, $type = null)
{
$type = $type ?: e107::getPref('url_sef_translate');
return eHelper::title2sef($title, $type);
}
/**
* Returns options for link creation based on core URL preference.
*
* @param string $type
*
* @return array
* URL creation options array
*/
private function getLinkOptions($type)
{
$coreUrlPref = e107::getPref('e_url_list');
switch ($type) {
case 'news':
return [ 'full' => true ];
break;
case 'page':
return [ 'full' => true ];
break;
case 'downloads':
if ($coreUrlPref['download']) {
return [ 'mode' => 'full',
'legacy' => false ];
}
break;
case 'forum':
if ($coreUrlPref['forum']) {
return [
'mode' => 'full',
'query' => ['last' => 1],
'legacy' => false ];
}
break;
}
return [ 'mode' => 'full', 'legacy' => true ];
}
}