-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitNewsletter.php
421 lines (375 loc) · 15.1 KB
/
BitNewsletter.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
<?php
/**
* $Header$
*
* @copyright (c) 2004 bitweaver.org
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
*
* $Id$
*
* Virtual base class (as much as one can have such things in PHP) for all
* derived tikiwiki classes that require database access.
* @package newsletters
*
* @date created 2004/10/20
*
* @author drewslater <andrew@andrewslater.com>, spiderr <spider@steelsun.com>
*
* @version $Revision$
*/
/**
* required setup
*/
require_once( LIBERTY_PKG_CLASS_PATH.'LibertyContent.php' );
require_once( NEWSLETTERS_PKG_CLASS_PATH.'BitNewsletterEdition.php' );
define( 'BITNEWSLETTER_CONTENT_TYPE_GUID', 'bitnewsletter' );
/**
* @package newsletters
*/
class BitNewsletter extends LibertyContent {
function __construct( $pNlId=NULL, $pContentId=NULL ) {
parent::__construct();
$this->registerContentType( BITNEWSLETTER_CONTENT_TYPE_GUID, array(
'content_type_guid' => BITNEWSLETTER_CONTENT_TYPE_GUID,
'content_name' => 'Newsletter',
'handler_class' => 'BitNewsletter',
'handler_package' => 'newsletters',
'handler_file' => 'BitNewsletter.php',
'maintainer_url' => 'http://www.bitweaver.org'
) );
$this->mNewsletterId = $this->verifyId( $pNlId ) ? $pNlId : NULL;
$this->mContentId = $pContentId;
$this->mContentTypeGuid = BITNEWSLETTER_CONTENT_TYPE_GUID;
// Permission setup
//$this->mViewContentPerm = '';
$this->mUpdateContentPerm = 'p_newsletters_create';
$this->mAdminContentPerm = 'p_newsletters_admin';
}
function load( $pContentId = NULL, $pPluginParams = NULL ) {
if( $this->verifyId( $this->mNewsletterId ) || $this->verifyId( $this->mContentId ) ) {
global $gBitSystem;
$bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = '';
$lookupColumn = $this->verifyId( $this->mNewsletterId ) ? 'nl_id' : 'content_id';
$bindVars[] = $this->verifyId( $this->mNewsletterId )? $this->mNewsletterId : $this->mContentId;
$this->getServicesSql( 'content_load_function', $selectSql, $joinSql, $whereSql, $bindVars );
/* if( $pUserId ) {
error_log( 'BitNewsleters: user id loading not implemented yet' );
$whereSql = "";
$joinSql = "";
}
*/
$query = "SELECT * $selectSql
FROM `".BIT_DB_PREFIX."newsletters` n
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( n.`content_id`=lc.`content_id` )
$joinSql
WHERE n.`$lookupColumn`=? $whereSql";
$result = $this->mDb->query($query,$bindVars);
if ($result->numRows()) {
$this->mInfo = $result->fetchRow();
$this->mNewsletterId = $this->mInfo['nl_id'];
$this->mContentId = $this->mInfo['content_id'];
}
}
return( count( $this->mInfo ) );
}
function loadEditions() {
if( $this->isValid() ) {
$this->mEditions = $this->getEditions();
}
}
function store( &$pParamHash ) { //$nl_id, $name, $description, $allow_user_sub, $allow_any_sub, $unsub_msg, $validate_addr) {
if( $this->verify( $pParamHash ) ) {
$this->mDb->StartTrans();
if( parent::store( $pParamHash ) ) {
if( $this->mNewsletterId ) {
$result = $this->mDb->associateUpdate( BIT_DB_PREFIX."newsletters", $pParamHash['newsletter_store'], array ( "nl_id" => $this->mNewsletterId ) );
} else {
$pParamHash['newsletter_store']['content_id'] = $pParamHash['content_id'];
$result = $this->mDb->associateInsert( BIT_DB_PREFIX."newsletters", $pParamHash['newsletter_store'] );
}
$this->mDb->CompleteTrans();
} else {
$this->mDb->RollbackTrans();
}
}
return( count( $this->mErrors ) == 0 );
}
function verify( &$pParamHash ) {
// It is possible a derived class set this to something different
if( empty( $pParamHash['content_type_guid'] ) ) {
$pParamHash['content_type_guid'] = $this->mContentTypeGuid;
}
$pParamHash['newsletter_store']["allow_user_sub"] = (isset($pParamHash["allow_user_sub"]) && $pParamHash["allow_user_sub"] == 'on') ? 'y' : 'n';
$pParamHash['newsletter_store']["allow_any_sub"] = (isset($pParamHash["allow_any_sub"]) && $pParamHash["allow_any_sub"] == 'on') ? 'y': 'n';
$pParamHash['newsletter_store']["unsub_msg"] = (isset($pParamHash["unsub_msg"]) && $pParamHash["unsub_msg"] == 'on') ? 'y' : 'n';
$pParamHash['newsletter_store']["validate_addr"] = (isset($pParamHash["validate_addr"]) && $pParamHash["validate_addr"] == 'on') ? 'y' : 'n';
return( count( $this->mErrors ) == 0 );
}
function getSubscriberInfo( $pLookup ) {
$ret = array();
if( $this->isValid() ) {
$bindVars = array();
$whereSql = '';
if( !empty( $pLookup['email'] ) ) {
$whereSql .= ' AND `email`=? ` ';
$bindVars[] = $pLookup['email'];
}
if( !empty( $pLookup['user_id'] ) ) {
$whereSql .= ' AND `user_id`=? ` ';
$bindVars[] = $pLookup['user_id'];
}
$whereSql = preg_replace( '/^[\s]AND/', '', $whereSql );
$query = "SELECT `content_id` AS `hash_key`, ms.* from `".BIT_DB_PREFIX."mail_subscriptions` ms WHERE $whereSql ";
if( $res = $this->mDb->query( $query, $bindVars ) ) {
$ret = $res->GetAssoc();
}
}
return $ret;
}
function getSubscribers( $pAll=FALSE) {
$ret = array();
if( $this->isValid() ) {
$whereSql = $pAll ? '' : ' `unsubscribe_date` is NULL AND ';
$query = "select * from `".BIT_DB_PREFIX."mail_subscriptions` WHERE $whereSql `content_id`=?";
if( $res = $this->mDb->query( $query, array( $this->mContentId ) ) ) {
$ret = $res->GetRows();
}
}
return $ret;
}
function removeSubscription( $email, $notify = FALSE, $del_record = FALSE ) {
if ($del_record) {
$this->mDb->query("DELETE FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `content_id`=? AND `email`=?", array($this->mContentId, $email));
} else {
$urlCode = $this->mDb->getOne("select `sub_code` from `".BIT_DB_PREFIX."mail_subscriptions` where `content_id`=? and `email`=?", array($this->mContentId, $email));
$this->unsubscribe($urlCode, $notify);
}
}
function subscribe( $pSubscribeHash ) { // $notify = FALSE, $remind = FALSE ) {
$ret = FALSE;
if( $this->isValid() ) {
global $gBitSystem;
global $gBitSmarty;
global $gBitUser;
// Check for duplicates
$all_subs = $this->getSubscribers( TRUE );
$duplicate = FALSE;
foreach($all_subs as $sub) {
if( $sub['email'] == $pSubscribeHash['email'] ) {
$duplicate = TRUE;
$urlCode = $sub['sub_code'];
} elseif( !empty( $pSubscribeHash['user_id'] ) && $sub['user_id'] == $pSubscribeHash['user_id'] ) {
}
}
$urlCode = (!$duplicate) ? md5( BitUser::genPass() ) : $urlCode;
$now = date("U");
// Generate a code and store it and send an email with the
// URL to confirm the subscription put valid as 'n'
if (!$duplicate) {
if( @BitBase::verifyId( $pSubscribeHash['user_id'] ) ) {
// we have user_id subscribing, use the id, NULL the email
$subUserId = $pSubscribeHash['user_id'];
$subEmail = NULL;
} else {
// we have user_id subscribing, use the id, NULL the email
$subUserId = NULL;
$subEmail = $pSubscribeHash['email'];
}
$query = "insert into `".BIT_DB_PREFIX."mail_subscriptions` (`content_id`, `user_id`, `email`,`sub_code`,`is_valid`,`subscribed_date`) VALUES (?,?,?,?,?,?)";
$result = $this->mDb->query( $query, array( $this->mContentId, $subUserId, $subEmail, $urlCode, 'n', (int)$now ) );
}
if( ( !empty( $pSubscribeHash['notify'] ) && $this->getField( 'validate_addr' ) == 'y') || !empty( $pSubscribeHash['remind'] ) ) {
// Generate a code and store it and send an email with the
$gBitSmarty->assign( 'sub_code', $urlCode );
$mail_data = $gBitSmarty->fetch('bitpackage:newsletters/confirm_newsletter_subscription.tpl');
@mail($email, tra('Newsletter subscription information at') . ' ' . $gBitSystem->getConfig( "bitmailer_from" ), $mail_data,
"From: " . $gBitSystem->getConfig( "sender_email" ) . "\r\nContent-type: text/plain;charset=utf-8\r\n");
}
$ret = TRUE;
}
return $ret;
}
function unsubscribe( $pMixed, $notify = TRUE ) {
global $gBitSystem;
global $gBitSmarty;
global $gBitUser;
$ret = FALSE;
$now = date("U");
if( is_numeric( $pMixed ) ) {
$query = "SELECT `content_id` FROM `".BIT_DB_PREFIX."newsletters` WHERE `nl_id`=?";
if( $subRow['content_id'] = $this->mDb->getOne( $query, array( $pMixed ) ) ) {
$subRow['col_name'] = 'user_id';
$subRow['col_val'] = $gBitUser->mUserId;
}
} elseif( is_string( $pMixed ) ) {
$query = "SELECT * FROM `".BIT_DB_PREFIX."mail_queue` WHERE `url_code`=?";
if( $subRow = $this->mDb->getRow( $query, array( $pMixed ) ) ) {
$subRow['col_name'] = !empty( $subRow['user_id'] ) ? 'user_id' : 'email';
$subRow['col_val'] = !empty( $subRow['user_id'] ) ? $subRow['user_id'] : $subRow['email'];
}
}
if( !empty( $subRow ) ) {
$this->mContentId = $subRow['content_id'];
$this->load();
if( $this->mDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `$subRow[col_name]`=?", array( $subRow['col_val'] ) ) ) {
$query = "UPDATE `".BIT_DB_PREFIX."mail_subscriptions` SET `unsubscribe_date`=?, `content_id`=? WHERE `$subRow[col_name]`=? AND `unsubscribe_date` IS NULL";
} else {
$query = "INSERT INTO `".BIT_DB_PREFIX."mail_subscriptions` (`unsubscribe_date`,`content_id`,`$subRow[col_name]`) VALUES(?,?,?)";
}
$result = $this->mDb->query( $query, array( $now, $subRow['content_id'], $subRow['col_val'] ) );
if( $notify ) {
// Now send a bye bye email
$gBitSmarty->assign('sub_code', $res["sub_code"]);
$mail_data = $gBitSmarty->fetch('bitpackage:newsletters/newsletter_byebye.tpl');
@mail($res["email"], tra('Thank you from') . ' ' . $gBitSystem->getConfig( "bitmailer_from" ), $mail_data,
"From: " . $gBitSystem->getConfig( "sender_email" ) . "\r\nContent-type: text/plain;charset=utf-8\r\n");
}
$ret = TRUE;
}
return $ret;
}
/*
function add_all_users($nl_id) {
$query = "select `email` from `".BIT_DB_PREFIX."users_users`";
$result = $this->mDb->query($query,array());
while ($res = $result->fetchRow()) {
$email = $res["email"];
if (!empty($email)) {
$this->newsletter_subscribe($nl_id, $email);
}
}
}
function updateUsers() {
if( $this->isValid() ) {
$users = $this->mDb->getOne( "select count(*) from `".BIT_DB_PREFIX."mail_subscriptions` where `nl_id`=?", array( $this->mNewsletterId ) );
$query = "update `".BIT_DB_PREFIX."newsletters` set `users`=? where `nl_id`=?";
$result = $this->mDb->query( $query, array( $users, $this->mNewsletterId ) );
}
}
*/
public static function getList( &$pListHash ) {
global $gBitDb;
if ( empty( $pParamHash["sort_mode"] ) ) {
$pListHash['sort_mode'] = 'created_desc';
}
BitBase::prepGetList( $pListHash );
$bindVars = array();
$joinSql = '';
$mid = '';
if( @BitBase::verifyId( $pListHash['nl_id'] ) ) {
$mid .= ' AND n.nl_id=? ';
$bindVars[] = $pListHash['nl_id'];
}
if( !empty( $pListHash['find'] ) ) {
$findesc = '%' . $pListHash['find'] . '%';
$mid .= " AND (`name` like ? or `description` like ?)";
$bindVars[] = $findesc;
$bindVars[] = $findesc;
}
if( !empty( $pListHash['registration_optin'] ) ) {
$joinSql = " INNER JOIN `".BIT_DB_PREFIX."liberty_content_prefs` lcp ON (lcp.`content_id`=n.`content_id` AND lcp.`pref_name`='registration_optin' AND lcp.`pref_value`='y') ";
}
$query = "SELECT *
FROM `".BIT_DB_PREFIX."newsletters` n INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( n.`content_id`=lc.`content_id`)
$joinSql
WHERE n.`content_id`=lc.`content_id` $mid
ORDER BY ".$gBitDb->convertSortmode( $pListHash['sort_mode'] );
$result = $gBitDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
$query_cant = "select count(*) from `".BIT_DB_PREFIX."newsletters` $mid";
$ret = array();
while( $res = $result->fetchRow() ) {
$res['display_url'] = BitNewsletter::getDisplayUrlFromHash( $res );
$res["confirmed"] = $gBitDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `unsubscribe_date` IS NULL and `content_id`=?",array( (int)$res['content_id'] ) );
$res["unsub_count"] = $gBitDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `content_id`=?",array( (int)$res['content_id'] ) );
$ret[$res['content_id']] = $res;
}
return $ret;
}
/* function list_newsletter_subscriptions($nl_id, $offset, $maxRecords, $sort_mode, $find) {
$bindVars = array((int)$nl_id);
if ($find) {
$findesc = '%' . $find . '%';
$mid = " where `nl_id`=? and (`name` like ? or `description` like ?)";
$bindVars[] = $findesc;
$bindVars[] = $findesc;
} else {
$mid = " where `nl_id`=? ";
}
$query = "select * from `".BIT_DB_PREFIX."mail_subscriptions` $mid order by ".$this->mDb->convertSortmode("$sort_mode");
$query_cant = "select count(*) from mail_subscriptions $mid";
$result = $this->mDb->query($query,$bindVars,$maxRecords,$offset);
$cant = $this->mDb->getOne($query_cant,$bindVars);
$ret = array();
while ($res = $result->fetchRow()) {
$ret[] = $res;
}
$retval = array();
$retval["data"] = $ret;
$retval["cant"] = $cant;
return $retval;
}
*/
function expunge() {
$ret = FALSE;
if( $this->isValid() ) {
$this->mDb->StartTrans();
$query = "DELETE FROM `".BIT_DB_PREFIX."newsletters` where `nl_id`=?";
$result = $this->mDb->query( $query, array( $this->mNewsletterId ) );
// Clear out all individual subscriptions/unsubscriptions, but preserve the unsubscribe_all's
$query = "DELETE FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `content_id`=? AND `unsubscribe_all` IS NOT NULL";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$query = "UPDATE `".BIT_DB_PREFIX."mail_subscriptions` SET `content_id`=NULL WHERE `content_id`=? AND `unsubscribe_all` IS NOT NULL";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
if( parent::expunge() ) {
$ret = TRUE;
$this->mDb->CompleteTrans();
} else {
$this->mDb->RollbackTrans();
}
}
return $ret;
}
function isValid() {
return( $this->verifyId( $this->mNewsletterId ) );
}
/**
* Generate a valid url for the Newsletter
*
* @param object $pNewsletterId of the item to use
* @return object Url String
*/
public static function getDisplayUrlFromHash( &$pParamHash ) {
global $gBitSystem;
$ret = NULL;
if( BitBase::verifyId( $pParamHash['nl_id'] ) ) {
if( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) {
$ret = NEWSLETTERS_PKG_URL.$pParamHash['nl_id'];
} else {
$ret = NEWSLETTERS_PKG_URL.'index.php?nl_id='.$pParamHash['nl_id'];
}
} else {
$ret = NEWSLETTERS_PKG_URL.'index.php';
}
return $ret;
}
function getEditions( $pNewsletterId = NULL ) {
$ret = array();
if( empty( $pNewsletterId ) ) {
$nlId = $this->mNewsletterId;
} elseif( BitBase::verifyId( $pNewsletterId ) ) {
$nlId = $pNewsletterId;
}
if( !empty( $nlId ) ) {
$listHash = array( 'nl_id' => $nlId );
$ret = BitNewsletterEdition::getList( $listHash );
}
return $ret;
}
function getUserSubscriptions( $pUserId, $pEmail ) {
global $gBitDb;
$query = "SELECT `content_id` AS hash_key, ms.* FROM `".BIT_DB_PREFIX."mail_subscriptions` ms WHERE `user_id`=? OR `email`=?";
$ret = $gBitDb->getAssoc( $query, array( $pUserId, $pEmail ) );
return $ret;
}
}