/
usernotify.php
225 lines (182 loc) · 7.35 KB
/
usernotify.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
<?php
/*
* @package User Notify System Plugin
* @copyright (C) 2016 RJCreations. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
class PlgSystemUsernotify extends JPlugin
{
protected $cparms = null; // com_usernotify params
protected $useQ = true; // use the notification queueing method
protected $qdb; // db instance for queue
protected $_db; // convenience db instance
public function __construct (&$subject, $config)
{
parent::__construct($subject, $config);
// check for com_usernotify -- nothing to do if it is not installed
if (JComponentHelper::isInstalled('com_usernotify')) {
// get the com_usernotify options
$this->cparms = JComponentHelper::getParams('com_usernotify');
// get queue setting
$this->useQ = $this->cparms->get('useQ', 1);
// get the database
$this->_db = JFactory::getDbo();
// setup some logging
if (JDEBUG) { JLog::addLogger(array('text_file'=>'com_usernotify.log.php'), JLog::ALL, array('com_usernotify')); }
}
}
public function onContentBeforeSave ($context, $article, $isNew)
{
// immediately return if nothing to do
if ($this->ignoreEvent($context)) return true;
$this->ldump(array('BS::','context'=>$context,'title'=>$article->title,'state'=>$article->state,'publish_up'=>$article->publish_up,'isNew'=>$isNew));
return true;
}
public function onContentAfterSave ($context, $article, $isNew)
{
// immediately return if not published or if not from a watched extension
if (empty($article->state) || ($article->state != 1) || $this->ignoreEvent($context)) return true;
// get the category's notification configuration
$ccfg = $this->getCatCfg($article->catid);
// if not a configured category, no need to continue
if (!$ccfg) return true;
// unset($ccfg['checked_out']);unset($ccfg['checked_out_time']);
$this->ldump(array('AS::','context'=>$context,'catid'=>$article->catid,'title'=>$article->title,'state'=>$article->state,'created'=>$article->created,'modified'=>$article->modified,'publish_up'=>$article->publish_up,'isNew'=>$isNew,'ccfg'=>$ccfg));
// determine if it is an updated save (originally published more than a day ago)
$upd = (time() - strtotime($article->publish_up) > 86400);
// check that this type notification is enabled in the component
if (($upd && $this->cparms->get('upd', 0) == 0) || (!$upd && $this->cparms->get('pub', 0) == 0)) return true;
$this->affectNotifications($ccfg, $article, $upd);
return true;
}
public function onContentChangeState ($context, $pks, $value)
{
// immediately return if nothing to do
if ($value == 0 || $this->ignoreEvent($context)) return true;
$this->ldump(array('CS::','context'=>$context,'pks'=>$pks,'val'=>$value)); //return true;
$this->_db->setQuery('SELECT id,title,state,catid,publish_up FROM #__content WHERE id IN ('.implode(',', $pks).')');
$lst = $this->_db->loadAssocList();
foreach ($lst as $a) {
// if ($a['state'] == 0) continue;
// if (time() - strtotime($a['publish_up']) > 10) continue;
// get the category's notification configuration
$ccfg = $this->getCatCfg($a['catid']);
// if not a configured category, move on
if (!$ccfg) continue;
$this->affectNotifications($ccfg, (object) $a);
$this->ldump($a);
}
return true;
}
// clear some db items when a user is deleted
public function onUserAfterDelete ($user, $success, $msg)
{
// immediately return success if no com_usernotify
if (!$this->cparms) return $success;
if ($success) {
$db = JFactory::getDbo();
$where = $db->quoteName('uid') . ' = ' . (int)$user['id'];
$query = $db->getQuery(true)
->delete($db->quoteName('#__usernotify_u'))
->where($where);
$db->setQuery($query)->execute();
}
return $success;
}
private function getCatCfg ($cid)
{
$this->_db->setQuery('SELECT * FROM #__usernotify_c WHERE cid='.$cid);
$ccfg = $this->_db->loadAssoc();
return $ccfg;
}
private function ignoreEvent ($cntx)
{
// ignore if no com_usernotify
if (!$this->cparms) return true;
$this->ldump(array($cntx));
// extract the components that are being watched
$targs = $this->cparms->get('target', array());
// ignore if the context is not for a watched component
$ctxp = explode('.', $cntx);
if (!in_array($ctxp[0], $targs)) return true;
return false;
}
private function affectNotifications ($ccfg, $article, $upd=false)
{
ob_start();
require_once 'helper.php';
$recips = PlgUserNotifyHelper::getRecipients($ccfg, true, $upd);
$msg = PlgUserNotifyHelper::getComposedEmail($this->cparms, $ccfg, $article, true, $upd);
if ($this->useQ) {
$qfil = JPATH_SITE.'/tmp/unotifyqueue.sqlite';
$init = !file_exists($qfil);
$this->qdb = JDatabaseDriver::getInstance(array('driver'=>'sqlite','database'=>$qfil));
$this->qdb->setDebug(7);
$this->qdb->connect();
if ($init) {
$this->qdb->setQuery('CREATE TABLE `queue` (`q_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `addr` TEXT, `subj` TEXT, `body` TEXT)')->execute();
}
$this->qdb->setQuery('BEGIN TRANSACTION')->execute();
}
foreach ($recips as $recip) {
$this->sendNotice($recip['alt_email'] ?: $recip['email'], $this->cparms->get('subject', 'Notification'), $msg);
}
if ($this->useQ) {
$this->qdb->setQuery('COMMIT')->execute();
$this->qdb->disconnect();
}
$this->ldump(array('OB::'=>ob_get_contents()));
ob_end_clean();
}
private function sendNotice ($addr, $subj, $body)
{
if ($this->useQ) {
$addr = $this->qdb->quote($addr);
$subj = $this->qdb->quote($subj);
$body = $this->qdb->quote($body);
$this->qdb->setQuery('INSERT INTO `queue` (`addr`,`subj`,`body`) VALUES ('.$addr.','.$subj.','.$body.')')->execute();
return;
}
$mailer = JFactory::getMailer();
$mailer->XMailer = 'Joomla UserNotify';
// $mailer->useSendmail();
$mailer->addRecipient($addr);
$mailer->setSubject($subj);
$mailer->isHTML(true);
// $mailer->Encoding = 'base64';
$mailer->setBody('<!DOCTYPE html><html><head></head><body>'.$body.'</body></html>');
// $mailer->AltBody = JMailHelper::cleanText(strip_tags($body));
$mailer->AltBody = htmlspecialchars_decode(strip_tags($body), ENT_QUOTES);
// $mailer->AddEmbeddedImage( JPATH_ROOT.'/images/rjcreationslogo.png', 'logo_id', 'rjcreationslogo.png', 'base64', 'image/png' );
if (!$mailer->Send()) $this->ldump(array('Mailing failed: ',$mailer->ErrorInfo));
}
private function sendAlerts ($evt, $atime)
{
$ausrs = explode(',',$evt['alert_user']);
$toTime = $evt['rec_type'] ? ($evt['t_start'] + $evt['event_length']) : $evt['t_end'];
$evtTime = $this->formattedDateTime($evt['t_start'], $toTime);
$lb = "\n"; $lbb = "\n\n";
if ($evt['alert_meth'] & 1) { //email
$surl = $this->config->live_site;
$body = sprintf(JText::_('COM_USERSCHED_ALERT_BLURB'), $this->config->sitename, date('D j F Y g:ia'), $lb, $surl, $lb);
$body .= $evtTime . $lbb;
$body .= $evt['text'];
$this->sendAlert('email', 'Calendar Alert', $body, $ausrs);
}
if ($evt['alert_meth'] & 2) { //SMS
$splt = explode($lb,$evt['text'],2);
$body = $evtTime . $lbb;
$body .= isset($splt[1]) ? $splt[1] : '';
$this->sendAlert('sms', 'Calendar Alert -- '.$splt[0], $body, $ausrs);
}
}
private function ldump ($vars)
{
$dmp = '';
foreach ($vars as $n=>$var) {
$dmp .= $n.' => '.print_r($var, true)."\n";
}
file_put_contents('plg_usernotify.dump.log', $dmp."\n", FILE_APPEND);
}
}