diff options
| author | modela bitweaver <spiderr@bitweaver.org> | 2025-04-02 06:19:49 -0400 |
|---|---|---|
| committer | modela bitweaver <spiderr@bitweaver.org> | 2025-04-02 06:19:49 -0400 |
| commit | e7b82920b6a5f6a04ddebfb375714c04d6d10f51 (patch) | |
| tree | 42bf57d9fd7083afeaee4028c3731d3f663d33f5 /includes | |
| parent | 701b4a29270f442b8c97568ac1712731f542b6d4 (diff) | |
| download | newsletters-e7b82920b6a5f6a04ddebfb375714c04d6d10f51.tar.gz newsletters-e7b82920b6a5f6a04ddebfb375714c04d6d10f51.tar.bz2 newsletters-e7b82920b6a5f6a04ddebfb375714c04d6d10f51.zip | |
move classes to includes/classes
Diffstat (limited to 'includes')
| -rw-r--r-- | includes/bit_setup_inc.php | 6 | ||||
| -rw-r--r-- | includes/classes/BitNewsletter.php | 421 | ||||
| -rw-r--r-- | includes/classes/BitNewsletterEdition.php | 246 | ||||
| -rw-r--r-- | includes/classes/BitNewsletterMailer.php | 366 |
4 files changed, 1036 insertions, 3 deletions
diff --git a/includes/bit_setup_inc.php b/includes/bit_setup_inc.php index 7bc8079..31acee0 100644 --- a/includes/bit_setup_inc.php +++ b/includes/bit_setup_inc.php @@ -18,7 +18,7 @@ if( $gBitSystem->isPackageActive( NEWSLETTERS_PKG_NAME ) ) { ); $gBitSystem->registerAppMenu( $menuHash ); if( isset( $_GET['ct'] ) && strlen( $_GET['ct'] ) == 32 ) { - require_once( NEWSLETTERS_PKG_PATH.'BitNewsletterMailer.php' ); + require_once( NEWSLETTERS_PKG_CLASS_PATH.'BitNewsletterMailer.php' ); BitNewsletterMailer::storeClickthrough( $_GET['ct'] ); } @@ -40,8 +40,8 @@ if( $gBitSystem->isPackageActive( NEWSLETTERS_PKG_NAME ) ) { function newsletters_user_register( &$pObject ) { if( !empty( $_REQUEST['newsletter_optin'] ) ) { // hidden flag to indicate at least one newsletter was displayed - require_once NEWSLETTERS_PKG_PATH.'BitNewsletter.php'; - require_once NEWSLETTERS_PKG_PATH.'BitNewsletterMailer.php'; + require_once NEWSLETTERS_PKG_CLASS_PATH.'BitNewsletter.php'; + require_once NEWSLETTERS_PKG_CLASS_PATH.'BitNewsletterMailer.php'; if( !empty( $_REQUEST['unsub_all'] ) ) { $subHash['unsubscribe_all'] = 'y'; diff --git a/includes/classes/BitNewsletter.php b/includes/classes/BitNewsletter.php new file mode 100644 index 0000000..b08d6d2 --- /dev/null +++ b/includes/classes/BitNewsletter.php @@ -0,0 +1,421 @@ +<?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; + } + + +} + diff --git a/includes/classes/BitNewsletterEdition.php b/includes/classes/BitNewsletterEdition.php new file mode 100644 index 0000000..37b418b --- /dev/null +++ b/includes/classes/BitNewsletterEdition.php @@ -0,0 +1,246 @@ +<?php +/** + * $Header$ + * + * @copyright (c) 2004-15 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$ + * + * Class that handles editions of newsletters + * @package newsletters + * + * @date created 2005/12/08 + * + * @author spiderr <spider@steelsun.com> + * + * @version $Revision$ + */ + +/** + * required setup + */ +require_once( NEWSLETTERS_PKG_CLASS_PATH.'BitNewsletter.php' ); +require_once( LIBERTY_PKG_CLASS_PATH.'LibertyMime.php' ); + +define( 'BITNEWSLETTEREDITION_CONTENT_TYPE_GUID', 'bitnewsletteredn' ); + +/** + * @package newsletters + */ +class BitNewsletterEdition extends LibertyMime { + function __construct( $pEditionId=NULL, $pContentId=NULL, $pNlId=NULL ) { + parent::__construct(); + $this->registerContentType( BITNEWSLETTEREDITION_CONTENT_TYPE_GUID, array( + 'content_type_guid' => BITNEWSLETTEREDITION_CONTENT_TYPE_GUID, + 'content_name' => 'Edition', + 'handler_class' => 'BitNewsletterEdition', + 'handler_package' => 'newsletters', + 'handler_file' => 'BitNewsletterEdition.php', + 'maintainer_url' => 'http://www.bitweaver.org' + ) ); + $this->mEditionId = $pEditionId; + $this->mContentId = $pContentId; + $this->mContentTypeGuid = BITNEWSLETTEREDITION_CONTENT_TYPE_GUID; + $this->mNewsletter = new BitNewsletter( $pNlId ); + + // Permission setup + //$this->mViewContentPerm = ''; + $this->mUpdateContentPerm = 'p_newsletters_create_editions'; + $this->mAdminContentPerm = 'p_newsletters_admin'; + } + + function verify( &$pParamHash ) { + + if( @$this->verifyId( $pParamHash['nl_content_id'] ) ) { + $pParamHash['edition_store']["nl_content_id"] = $pParamHash['nl_content_id']; + } else { + $this->mErrors['nl_content_id'] = tra( 'No newsletter was selected for this edition.' ); + } + $pParamHash['edition_store']['is_draft'] = !empty( $pParamHash['is_draft'] ) ? 'y' : NULL; + $pParamHash['edition_store']['reply_to'] = !empty( $pParamHash['reply_to'] ) ? $pParamHash['reply_to'] : NULL; + + return( count( $this->mErrors ) == 0 ); + } + + function store( &$pParamHash ) { + if( $this->verify( $pParamHash ) ) { + $this->mDb->StartTrans(); + if( parent::store( $pParamHash ) ) { + if( $this->mEditionId ) { + $result = $this->mDb->associateUpdate( BIT_DB_PREFIX."newsletters_editions", $pParamHash['edition_store'], array ( "edition_id" => $this->mEditionId ) ); + } else { + $pParamHash['edition_store']['content_id'] = $pParamHash['content_id']; + $result = $this->mDb->associateInsert( BIT_DB_PREFIX."newsletters_editions", $pParamHash['edition_store'] ); + } + $this->mDb->CompleteTrans(); + $this->load(); + } else { + $this->mDb->RollbackTrans(); + } + } + return( count( $this->mErrors ) == 0 ); + } + + function load( $pContentId = NULL, $pPluginParams = NULL ) { + if( $this->verifyId( $this->mEditionId ) || $this->verifyId( $this->mContentId ) ) { + global $gBitSystem; + + $bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = ''; + + $lookupColumn = $this->verifyId( $this->mEditionId )? 'edition_id' : 'content_id'; + $lookupId = $this->verifyId( $this->mEditionId )? $this->mEditionId : $this->mContentId; + array_push( $bindVars, $lookupId ); + + $this->getServicesSql( 'content_load_function', $selectSql, $joinSql, $whereSql, $bindVars ); + + $query = "SELECT ne.*, lc.* + FROM `".BIT_DB_PREFIX."newsletters_editions` ne + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( ne.`content_id`=lc.`content_id` ) + WHERE ne.`$lookupColumn`=? $whereSql"; + if ( $result = $this->mDb->query($query,$bindVars) ) { + $this->mInfo = $result->fetchRow(); + $this->mEditionId = $this->mInfo['edition_id']; + $this->mContentId = $this->mInfo['content_id']; + LibertyMime::load(); + $this->mNewsletter = new BitNewsletter( NULL, $this->mInfo['nl_content_id'] ); + $this->mNewsletter->load(); + } else { + unset( $this->mEditionId ); + } + } + return( count( $this->mInfo ) ); + } + + function isValid() { + return( $this->verifyId( $this->mEditionId ) ); + } + + /** + * Generate a valid url for the Newsletter Edition + * + * @param object PostId of the item to use + * @return object Url String + */ + public static function getDisplayUrlFromHash( &$pParamHash ) { + $ret = NULL; + global $gBitSystem; + if( BitBase::verifyId( $pParamHash['edition_id'] ) ) { + if( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) { + $ret = NEWSLETTERS_PKG_URL.'edition/'.$pParamHash['edition_id']; + } else { + $ret = NEWSLETTERS_PKG_URL.'edition.php?edition_id='.$pParamHash['edition_id']; + } + } else { + $ret = NEWSLETTERS_PKG_URL.'edition.php'; + } + return $ret; + } + + + function getList( &$pListHash ) { + global $gBitDb; + + $bindVars = array(); + parent::prepGetList( $pListHash ); + $mid = ''; + + if( @BitBase::verifyId( $pListHash['nl_id'] ) ) { + $mid .= (empty( $mid ) ? 'WHERE' : 'AND').' n.nl_id=? '; + $bindVars[] = $pListHash['nl_id']; + } + + if( $pListHash['find'] ) { + $findesc = '%' . $pListHash['find'] . '%'; + $mid .= (empty( $mid ) ? 'WHERE' : 'AND').' (lc.`title` like ? or lc.`data` like ?)'; + $bindVars[] = $findesc; + $bindVars[] = $findesc; + } + + $query = "SELECT `edition_id` AS `hash_key`, ne.*, lc.*, lc2.`title` AS `newsletter_title` + FROM `".BIT_DB_PREFIX."newsletters_editions` ne + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id`=ne.`content_id` ) + INNER JOIN `".BIT_DB_PREFIX."newsletters` n ON( ne.`nl_content_id`=n.`content_id` ) + LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc2 ON( n.`content_id`=lc2.`content_id` ) + $mid ORDER BY ".$gBitDb->convertSortmode( $pListHash['sort_mode'] ); + $query_cant = "select count(*) from `".BIT_DB_PREFIX."newsletters` n INNER JOIN `".BIT_DB_PREFIX."newsletters_editions` ne ON(n.`content_id`=ne.`nl_content_id`) $mid"; + $ret = $gBitDb->getAssoc( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] ); + foreach( array_keys( $ret ) as $k ) { + $ret[$k]['display_url'] = BitNewsletterEdition::getDisplayUrlFromHash( $ret[$k] ); + // remove formating tags + $data = preg_replace( '/{[^{}]*}/', '', $ret[$k]['data'] ); + } + $pListHash['total_records'] = $gBitDb->getOne( $query_cant, $bindVars ); + $pListHash['block_pages'] = 5; + $pListHash['total_pages'] = ceil( $pListHash['total_records'] / $pListHash['max_records'] ); + $pListHash['current_page'] = (!empty( $pListHash['offset'] ) ? floor( $pListHash['offset'] / $pListHash['max_records'] ) + 1 : 1 ); + + return $ret; + } + + function expunge() { + $ret = FALSE; + if( $this->isValid() ) { + $this->mDb->StartTrans(); + $query = "DELETE FROM `".BIT_DB_PREFIX."newsletters_editions` WHERE `content_id`=?"; + $result = $this->mDb->query( $query, array( $this->mContentId ) ); + if( LibertyMime::expunge() ) { + $ret = TRUE; + $this->mDb->CompleteTrans(); + } else { + $this->mDb->RollbackTrans(); + } + } + return $ret; + } + + function isDraft() { + return( $this->getField( 'is_draft' ) ); + } + + function getRecipients( $pGroupArray, $validated = TRUE, $pRequeue = FALSE ) { + global $gBitUser; + $ret = array(); + if( is_array( $pGroupArray ) ) { + foreach( $pGroupArray as $groupId ) { + $ret = array_merge( $ret, $gBitUser->getGroupUserData( $groupId, array( 'email', 'uu.user_id', 'login', 'real_name' ) ) ); + } + + if ( array_search( 'send_subs', $pGroupArray ) !== false ) { + $valid = ""; + $bindvars = array( $this->mNewsletter->mNewsletterId ); + if ($validated) { + $valid = " AND `is_valid`=?"; + $bindvars[] = 'y'; + } + $query = "SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions` + WHERE `content_id`=? AND `unsubscribe_date` IS NULL AND `unsubscribe_all` IS NULL".$valid; + $subs = $this->mDb->getArray( $query, $bindvars ); + foreach( $subs as $sub) { + if (!isset($ret[$sub['email']])) + $ret[$sub['email']] = $sub; + } + } + if( !$pRequeue ) { + $query = "SELECT `email`, `user_id` FROM `".BIT_DB_PREFIX."mail_queue` WHERE `content_id`=?"; + if( $dupes = $this->mDb->getAssoc( $query, array( $this->mContentId ) ) ) { + $ret = array_diff_keys( $ret, $dupes ); + } + } + } + return $ret; + } + + function render() { + global $gBitSmarty; + $ret = NULL; + if( $this->isValid() ) { + $gBitSmarty->assignByRef( 'gContent', $this ); + $ret = $gBitSmarty->fetch( 'bitpackage:newsletters/view_edition.tpl' ); + } + return $ret; + } +} + +?> diff --git a/includes/classes/BitNewsletterMailer.php b/includes/classes/BitNewsletterMailer.php new file mode 100644 index 0000000..18f298c --- /dev/null +++ b/includes/classes/BitNewsletterMailer.php @@ -0,0 +1,366 @@ +<?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$ + * + * Class that handles editions of newsletters + * @package newsletters + * + * @date created 2005/12/08 + * + * @author spiderr <spider@steelsun.com> + * + * @version $Revision$ + */ + +/** + * required setup + */ +require_once( NEWSLETTERS_PKG_CLASS_PATH.'BitNewsletter.php' ); +require_once( UTIL_PKG_INCLUDE_PATH.'phpmailer/class.phpmailer.php' ); + +/** + * @package newsletters + */ +class BitNewsletterMailer { + // Set default variables for all new objects + var $From; + var $FromName; + var $Host; + var $Mailer; // Alternative to IsSMTP() + var $WordWrap; + function BitNewsletterMailer () { + global $gBitDb, $gBitSystem, $gBitLanguage; + $this->mDb = $gBitDb; + } + + // Replace the default error_handler + function error_handler( $msg ) { + global $gBitDb; + bit_error_handler( NULL, NULL, NULL, "FULFILLMENT ERROR: MISSSING PDF for ORDER $pOrderId CID ".$prod->mInfo['related_content_id'], $pdfInfo['pdf_file'], '', $prod->mDb ); + print("My Site Error"); + print("Description:"); + printf("%s", $msg); + exit; + } + + function isRecipientQueued( $pRecipientMixed, $pContentId ) { + if( BitBase::verifyId( $pRecipientMixed ) ) { + $lookupCol = 'user_id'; + } else { + $lookupCol = 'email'; + } + return( $this->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."mail_queue` WHERE `content_id`=? AND `$lookupCol`=?", array( $pContentId, $pRecipientMixed ) ) ); + } + + function queueRecipients( $pContentId, $pNewsletterContentId, $pRecipients, $pRequeue=FALSE ) { + $ret = 0; + if( !empty( $pRecipients ) && BitBase::verifyId( $pContentId ) ) { + $queueTime = time(); + foreach( array_keys( $pRecipients ) AS $email ) { + $unsub = $this->getUnsubscription( $email, $pNewsletterContentId ); + $lookup = !empty( $pRecipients[$email]['user_id'] ) ? $pRecipients[$email]['user_id'] : $email; + if( empty( $unsub ) && !$this->isRecipientQueued( $lookup, $pContentId ) ) { + $insertHash['mail_queue_id'] = $this->mDb->GenID( 'mail_queue_id' ); + $insertHash['email'] = $email; + if( !empty( $pRecipients[$email]['user_id'] ) ) { + $insertHash['user_id'] = $pRecipients[$email]['user_id']; + } + $insertHash['content_id'] = $pContentId; + $insertHash['nl_content_id'] = $pNewsletterContentId; + $insertHash['queue_date'] = $queueTime; + $this->mDb->associateInsert( BIT_DB_PREFIX.'mail_queue', $insertHash ); + $ret++; + } elseif( empty( $unsub ) && $pRequeue ) { + $bindVars = array( $queueTime, $pContentId ); + if( !empty( $pRecipients[$email]['user_id'] ) ) { + $lookupCol = 'user_id'; + $bindVars[] = $pRecipients[$email]['user_id']; + } else { + $lookupCol = 'email'; + $bindVars[] = $email; + } + $rs = $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `queue_date`=?, `begin_date`=NULL, `sent_date`=NULL, `last_read_date`=NULL, `mail_error`=NULL, `hits`=0 WHERE `content_id`=? AND `$lookupCol`=?", array( $bindVars ) ); + $ret++; + } + } + } + return $ret; + } + + + function tendQueue() { + $this->mDb->StartTrans(); + $query = "SELECT * + FROM `".BIT_DB_PREFIX."mail_queue` mq + WHERE `sent_date` IS NULL AND `mail_error` IS NULL + ORDER BY `queue_date`,`user_id`,`email` ".$this->mDb->SQLForUpdate(); + if( $rs = $this->mDb->query( $query ) ) { + while( $pick = $rs->fetchRow() ) { + $this->sendQueue( $pick ); + $this->mDb->CompleteTrans(); + $this->mDb->StartTrans(); + } + } + $this->mDb->CompleteTrans(); + } + + function sendQueue( $pQueueMixed ) { + global $gBitSmarty, $gBitSystem, $gBitLanguage; + static $body = array(); + if( is_array( $pQueueMixed ) ) { + $pick = $pQueueMixed; + } elseif( is_numeric( $pQueueMixed ) ) { + $pick = $this->mDb->GetRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_queue` mq WHERE `mail_queue_id` = ? ".$this->mDb->SQLForUpdate(), array( $pQueueMixed ) ); + } + + if( !empty( $pick ) ) { + $startTime = microtime( TRUE ); + $this->mDb->query( "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `begin_date`=? WHERE `mail_queue_id` = ? ", array( time(), $pick['mail_queue_id'] ) ); + if( !empty( $pick['user_id'] ) ) { + $userHash = $this->mDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."users_users` WHERE `user_id`=?", array( $pick['user_id'] ) ); + $pick['full_name'] = BitUser::getDisplayName( FALSE, $userHash ); + } else { + $pick['full_name'] = NULL; + } + if( !isset( $body[$pick['content_id']] ) ) { + $gBitSmarty->assign( 'sending', TRUE ); + // We only support sending of newsletters currently + $content = new BitNewsletterEdition( NULL, $pick['content_id'] ); + if( $content->load() ) { + $body[$pick['content_id']]['body'] = $content->render(); + $body[$pick['content_id']]['subject'] = $content->getTitle(); + $body[$pick['content_id']]['reply_to'] = $content->getField( 'reply_to', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) ); + $body[$pick['content_id']]['object'] = $content; + } else { + bit_error_log( $this->mErrors ); + } +// $content[$pick['content_id']] = LibertyBase::getLibertyObject(); + } + + print "[ $pick[mail_queue_id] ] $pick[content_id] TO: $pick[email]\t"; + $unsub = $this->getUnsubscription( $pick['email'], $pick['nl_content_id'] ); + if( !empty( $unsub ) ) { + print " SKIPPED (unsubscribed) <br/>\n"; + $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?", array( $pick['mail_queue_id'] ) ); + } elseif( !empty( $body[$pick['content_id']] ) ) { + $pick['url_code'] = md5( $pick['content_id'].$pick['email'].$pick['queue_date'] ); + $unsub = ''; + if( $body[$pick['content_id']]['object']->mNewsletter->getField('unsub_msg') ) { + $gBitSmarty->assign( 'url_code', $pick['url_code'] ); + } + $gBitSystem->preDisplay(''); + $gBitSmarty->assign( 'sending', TRUE ); + $gBitSmarty->assign( 'unsubMessage', $unsub ); + $gBitSmarty->assign( 'trackCode', $pick['url_code'] ); + $gBitSmarty->assign( 'mid', 'bitpackage:newsletters/view_edition.tpl' ); + $htmlBody = $gBitSmarty->fetch( 'bitpackage:newsletters/mail_edition.tpl' ); + $htmlBody = bit_add_clickthrough( $htmlBody, $pick['url_code'] ); + + $mailer = new PHPMailer(); + if( $gBitSystem->getConfig( 'bitmailer_errors_to' ) ) { + $mailer->Sender = $gBitSystem->getConfig( 'bitmailer_errors_to' ); + $mailer->addCustomHeader( "Errors-To: ".$gBitSystem->getConfig( 'bitmailer_errors_to' ) ); + } + $mailer->From = $gBitSystem->getConfig( 'bitmailer_sender_email', $gBitSystem->getConfig( 'site_sender_email', $_SERVER['SERVER_ADMIN'] ) ); + $mailer->FromName = $gBitSystem->getConfig( 'bitmailer_from', $gBitSystem->getConfig( 'site_title' ) ); + $mailer->Host = $gBitSystem->getConfig( 'bitmailer_servers', $gBitSystem->getConfig( 'kernel_server_name', '127.0.0.1' ) ); + $mailer->Mailer = $gBitSystem->getConfig( 'bitmailer_protocol', 'smtp' ); // Alternative to IsSMTP() + $mailer->CharSet = 'UTF-8'; + if( $gBitSystem->getConfig( 'bitmailer_smtp_username' ) ) { + $mailer->SMTPAuth = TRUE; + $mailer->Username = $gBitSystem->getConfig( 'bitmailer_smtp_username' ); + } + if( $gBitSystem->getConfig( 'bitmailer_smtp_password' ) ) { + $mailer->Password = $gBitSystem->getConfig( 'bitmailer_smtp_password' ); + } + $mailer->WordWrap = $gBitSystem->getConfig( 'bitmailer_word_wrap', 75 ); + if( !$mailer->SetLanguage( $gBitLanguage->getLanguage(), UTIL_PKG_INCLUDE_PATH.'phpmailer/language/' ) ) { + $mailer->SetLanguage( 'en' ); + } + $mailer->ClearReplyTos(); + $mailer->AddReplyTo( $body[$pick['content_id']]['reply_to'], $gBitSystem->getConfig( 'bitmailer_from' ) ); + $mailer->Body = $htmlBody; + $mailer->Subject = $body[$pick['content_id']]['subject']; + $mailer->IsHTML( TRUE ); + $mailer->AltBody = ''; + $mailer->AddAddress( $pick['email'], $pick["full_name"] ); + if( $mailer->Send() ) { + print " SENT ".round( microtime( TRUE ) - $startTime, 2)." secs<br/>\n"; flush(); + $updateQuery = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `sent_date`=?,`url_code`=? WHERE `content_id`=? AND `email`=?"; + $this->mDb->query( $updateQuery, array( time(), $pick['url_code'], $pick['content_id'], $pick['email'] ) ); + } else { + $updateQuery = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `mail_error`=?,`sent_date`=? WHERE `content_id`=? AND `email`=?"; + $this->mDb->query( $updateQuery, array( $mailer->ErrorInfo, time(), $pick['content_id'], $pick['email'] ) ); + $pick['error'] = $mailer->ErrorInfo; + $this->logError( $pick ); + } + } + } + } + + function trackMail( $pUrlCode ) { + global $gBitDb; + $query = "UPDATE `".BIT_DB_PREFIX."mail_queue` SET `hits`=`hits`+1, `last_read_date`=?, `last_read_ip`=? WHERE `url_code`=? "; + $gBitDb->query( $query, array( time(), $_SERVER['REMOTE_ADDR'], $pUrlCode ) ); + } + + function logError( $pInfo ) { + if( !empty( $pInfo['url_code'] ) && !$this->mDb->getOne( "SELECT `url_code` FROM `".BIT_DB_PREFIX."mail_errors` WHERE `url_code`=?", array( $pInfo['url_code'] ) ) ) { + $store['url_code'] = $pInfo['url_code']; + $store['user_id'] = !empty( $pInfo['user_id'] ) ? $pInfo['user_id'] : NULL; + $store['content_id'] = !empty( $pInfo['content_id'] ) ? $pInfo['content_id'] : NULL; + $store['email'] = !empty( $pInfo['email'] ) ? $pInfo['email'] : NULL; + $store['error_message'] = $pInfo['error']; + $store['error_date'] = time(); + $this->mDb->associateInsert( BIT_DB_PREFIX."mail_errors", $store ); + } + print "ERROR: ".$pInfo['error']."\n"; + } + + // Looks up the code from the url to determine if the unsubscribe URL is valid. + // Can be statically called + function lookupSubscription( $pLookup ) { + global $gBitDb; + $ret = NULL; + if( is_array( $pLookup ) ) { + $query = "SELECT mq.*, lc.title, tct.*, uu.`real_name`, uu.`login`, uu.`email` FROM `".BIT_DB_PREFIX."mail_queue` mq + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( mq.`content_id`=lc.`content_id` ) + INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` tct ON( tct.`content_type_guid`=lc.`content_type_guid` ) + LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( mq.`user_id`=uu.`user_id` ) + WHERE mq.`".key( $pLookup )."`=? "; + $ret = $gBitDb->getRow( $query, array( current( $pLookup ) ) ); + } + return( $ret ); + } + + // Accepts a single row has containing the column of mail_subscriptions as the key to lookup the unsubscription info + // Can be statically called + function getUnsubscriptions( $pMixed ) { + global $gBitDb; + $ret = NULL; + if( is_array( $pMixed ) ) { + $col = key( $pMixed ); + $bindVars[] = current( $pMixed ); + $query = "SELECT ms.`content_id` AS `hash_key`, ms.*, uu.*, lc.title + FROM `".BIT_DB_PREFIX."mail_subscriptions` ms + LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON( ms.`user_id`=uu.`user_id` ) + LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( ms.`content_id`=lc.`content_id` ) + WHERE ms.`$col`=? "; + $ret = $gBitDb->getAssoc( $query, $bindVars ); + } + return( $ret ); + } + + function getUnsubscription( $pEmail, $pNewsletterContentId ) { + global $gBitDb; + return $gBitDb->getRow( "SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions` ms LEFT JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`user_id`=ms.`user_id`) WHERE (ms.`content_id`=? OR `unsubscribe_all`='y') AND (ms.`email`=? OR uu.`email`=?)", array( $pNewsletterContentId, $pEmail, $pEmail ) ); + } + + function storeSubscriptions( $pSubHash ) { + global $gBitSystem, $gBitDb; + $ret = FALSE; + $query = "delete from `".BIT_DB_PREFIX."mail_subscriptions` where `".key( $pSubHash['sub_lookup'] )."`=?"; + $result = $gBitDb->query($query, array( current( $pSubHash['sub_lookup'] ) ) ); + $ret = TRUE; + if( !empty( $pSubHash['unsub_content'] ) ) { + foreach( $pSubHash['unsub_content'] as $conId ) { + $storeHash = array(); + $storeHash['content_id'] = $conId; + $storeHash['unsubscribe_all'] = !empty( $pSubHash['unsubscribe_all'] ) ? 'y' : NULL; + $storeHash['unsubscribe_date'] = time(); + $storeHash[key( $pSubHash['sub_lookup'] )] = current( $pSubHash['sub_lookup'] ); + if( !empty( $pSubHash['response_content_id'] ) ) { + $storeHash['response_content_id'] = $pSubHash['response_content_id']; + } + $gBitDb->associateInsert( BIT_DB_PREFIX."mail_subscriptions", $storeHash ); + } + } elseif( !empty( $pSubHash['unsubscribe_all'] ) ) { + // unsub all with no reference content_id + $storeHash = array(); + $storeHash['unsubscribe_all'] = !empty( $pSubHash['unsubscribe_all'] ) ? 'y' : NULL; + $storeHash['unsubscribe_date'] = time(); + $storeHash[key( $pSubHash['sub_lookup'] )] = current( $pSubHash['sub_lookup'] ); + if( !empty( $pSubHash['response_content_id'] ) ) { + $storeHash['response_content_id'] = $pSubHash['response_content_id']; + } + $gBitDb->associateInsert( BIT_DB_PREFIX."mail_subscriptions", $storeHash ); + } + return $ret; + } + + function getQueue( &$pListHash ) { + $ret = array(); + + LibertyContent::prepGetList( $pListHash ); + + $query = "SELECT mq.`mail_queue_id` AS `hash_key`, mq.*, lc.`title`, lc2.`title` AS newsletter_title + FROM `".BIT_DB_PREFIX."mail_queue` mq + INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lc.content_id=mq.content_id) + LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc2 ON (lc2.content_id=mq.nl_content_id) + WHERE begin_date IS NULL + ORDER BY queue_date"; + if( $rs = $this->mDb->query( $query ) ) { + $ret = $rs->getAssoc(); + } + + return $ret; + } + + function expungeQueueRow( $pQueueId ) { + if( BitBase::verifyId( $pQueueId ) ) { + $this->mDb->query( "DELETE FROM `".BIT_DB_PREFIX."mail_queue` WHERE `mail_queue_id`=?", array( $pQueueId ) ); + } + } + + function storeClickthrough( $pUrlCode ) { + global $gBitDb; + + $uri = substr( preg_replace( '/[&\?]?ct=[a-z0-9]{32}/', '', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] ), 0, 250 ); + $query = "SELECT mc.`clicks`, mq.`content_id`, mq.`user_id`, mc.`clicked_url` FROM `".BIT_DB_PREFIX."mail_queue` mq + LEFT JOIN `".BIT_DB_PREFIX."mail_clickthrough` mc ON (mq.`user_id`=mc.`user_id` AND mq.`content_id`=mc.`content_id` AND mc.`clicked_url`=?) + WHERE `url_code`=?"; + if( $row = $gBitDb->getRow( $query, array( $uri, $pUrlCode ) ) ) { + if( $row['clicked_url'] ) { + $gBitDb->query( "UPDATE `".BIT_DB_PREFIX."mail_clickthrough` SET `clicks`=`clicks`+1 WHERE `user_id`=? AND `content_id`=? AND `clicked_url`=? ", array( $row['user_id'], $row['content_id'], $row['clicked_url'] ) ); + } else { + $row['clicks'] = 1; + $row['clicked_url'] = $uri; + $gBitDb->associateInsert( BIT_DB_PREFIX.'mail_clickthrough', $row ); + } + } + } +} + +// This will insert a ticket on all template URL's that have GET parameters. +function bit_add_clickthrough( $pSource, $pUrlCode ) { + global $gBitUser, $gUrlCode; + + $gUrlCode = $pUrlCode; + + $pcre = '%href[\s]*=[\s]*["\']*((http|https)://[^\s"\']+?)(.*)%sU'; + + $ret = preg_replace_callback( $pcre, 'process_clickthrough_match', $pSource ); + + return $ret; + +} + + +function process_clickthrough_match( $matches ) { + global $gUrlCode; + $ret = $matches[0]; + if( strpos( $matches[0], '?' ) ) { + $ret .= '&ct='.$gUrlCode; + } else { + $ret .= '?ct='.$gUrlCode; + } + return $ret; +} + +?> |
