2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 12/12/2008 *
4 * =================== Last change: 12/12/2008 *
6 * -------------------------------------------------------------------- *
7 * File : pool-user.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Sends queued user mails from the pool *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Sendet freigegebene Mitglieder-Mails aus Pool *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2011 by Mailer Developer Team *
20 * For more information visit: http://www.mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Don't run on daily reset
44 if ((isResetModeEnabled()) || (!isHtmlOutputMode())) {
47 } elseif (!isExtensionActive('user')) {
48 // Abort here if extension user is not active
55 // Check for freed mail orders to send out
56 if (isExtensionActive('html_mail')) {
58 $HTML = ', `html_msg`';
62 $result_main = SQL_QUERY("SELECT
63 `id`, `sender` AS `sender_userid`, `subject`, `text`, `receivers`,
64 `payment_id`, `timestamp`, `url`, `target_send`, `cat_id`
67 `{?_MYSQL_PREFIX?}_pool`
71 `timestamp` DESC", __FILE__, __LINE__);
76 $count_back = array(0);
77 $pointsBack = array(0);
79 if (!SQL_HASZERONUMS($result_main)) {
81 while ($DATA = SQL_FETCHARRAY($result_main, 0, false)) {
82 // Set mail order as 'active'. That means it will be sent out
83 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='ACTIVE' WHERE `id`=%s AND `data_type`='NEW' LIMIT 1",
84 array($DATA['id']), __FILE__, __LINE__);
86 // Check fetched data for HTML
88 if (isset($DATA['html_msg'])) $isHtml = $DATA['html_msg'];
91 if (!SQL_HASZEROAFFECTED()) {
92 // "Explode" all receivers into an array
93 if (isInString(';', $DATA['receivers'])) {
94 // There's more than one receiver in the list...
95 $receivers = explode(';', $DATA['receivers']);
96 } elseif (!empty($DATA['receivers'])) {
98 $receivers = array($DATA['receivers']);
101 $receivers = array(0);
105 // Now, if we are good little boys and girls Santa Claus left us some user-ids.
106 // We can now send mails to them...
107 foreach ($receivers as $key => $userid) {
109 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.SQL_NUMROWS($result_user).'*');
110 if (fetchUserData($userid)) {
111 // Do we have a stats entry?
112 $result_stats = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s AND `userid`=%s AND timestamp_ordered='%s' LIMIT 1",
113 array($DATA['id'], $DATA['sender_userid'], $DATA['timestamp']), __FILE__, __LINE__);
115 // If there's no stats entry add it!
116 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.SQL_NUMROWS($result_stats).'!');
117 if (SQL_HASZERONUMS($result_stats)) {
118 // No entry was found, so we add him!
119 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_stats` (`pool_id` , `userid`, `cat_id`, `payment_id`, `subject`, `url` , `max_rec` , `timestamp_ordered`, `timestamp_sstart`) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s' , UNIX_TIMESTAMP())",
120 array(bigintval($DATA['id']), bigintval($DATA['sender_userid']), bigintval($DATA['cat_id']), bigintval($DATA['payment_id']), $DATA['subject'], $DATA['url'], $DATA['target_send'], bigintval($DATA['timestamp'])), __FILE__, __LINE__);
122 // Receive it's id for the links table
123 $result_stats = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s AND `userid`=%s AND timestamp_ordered='%s' LIMIT 1",
124 array(bigintval($DATA['id']), bigintval($DATA['sender_userid']), bigintval($DATA['timestamp'])), __FILE__, __LINE__);
127 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.SQL_NUMROWS($result_stats).'!');
128 if (SQL_NUMROWS($result_stats) == 1) {
130 list($stats_id) = SQL_FETCHROW($result_stats);
132 // Remove receiver from list
133 $status = removeReceiver($dummy, $key, bigintval($userid), bigintval($DATA['id']), bigintval($stats_id));
135 //* DEBUG: */ debugOutput('?L:'.__LINE__.'/'.$dummy.'/'.$key.'/'.$userid.'('.['sender_userid'].')/'.$DATA['id'].'/'.$stats_id.'?');
139 $DATA['stats_id'] = bigintval($stats_id);
142 $DATA['time'] = getPaymentPoints($DATA['payment_id'], 'time');
143 $DATA['points'] = getPaymentPoints($DATA['payment_id'], 'payment');
145 // Load message template
146 $mailText = loadEmailTemplate('member_user_pool_normal', $DATA, bigintval($userid));
149 sendEmail(getUserData('userid'), $DATA['subject'], $mailText, $isHtml);
151 // Count sent mails...
152 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_sent`=`emails_sent`+1 WHERE `userid`=%s LIMIT 1",
153 array(bigintval($DATA['sender_userid'])), __FILE__, __LINE__);
155 if (isExtensionInstalledAndNewer('user', '0.1.4')) {
156 // Update mails received for receiver
157 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
158 array(bigintval($userid)), __FILE__, __LINE__);
161 // Update mediadata if version is 0.0.4 or higher
162 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
163 // Update entry (or add missing)
164 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
165 updateMediadataEntry(array('total_send', 'normal_send'), 'add', 1);
168 // And count up the mail
169 $GLOBALS['pool_cnt']++;
170 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'*');
174 // Entry already found, but we still count one up!
175 $GLOBALS['pool_cnt']++;
176 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt']);
179 default: // Unknown return type
180 logDebugMessage(__FILE__, __LINE__, 'Unknown status ' . $status . ' detected. pool_id=' . $DATA['id'] . ',sender=' . $DATA['sender_userid'] . ',stats_id=' . $stats_id);
185 // Do we have reached the maximum to send mails? || (getConfig('max_send') >= $GLOBALS['pool_cnt'])
186 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'>='.$DATA['target_send'].'/'.getConfig('max_send').'>='.$GLOBALS['pool_cnt'].'/'.$lastSentId.'!='.$DATA['id'].'*');
187 if ((($GLOBALS['pool_cnt'] >= $DATA['target_send'])) && ($lastSentId != $DATA['id'])) {
190 'sender_userid' => $DATA['sender_userid'],
191 'cat_id' => $DATA['cat_id'],
192 'text' => $DATA['text'],
193 'url' => $DATA['url'],
194 'expiration' => '{%pipe,createFancyTime=' . getPaymentPoints($DATA['payment_id'], 'time') . '%}'
197 // Yes we do, so we notify admin and sender about fully sent mail!
198 sendAdminNotification('{--ADMIN_SEND_DONE_SUBJECT--}', 'admin_user_pool_done', $content, $userid);
201 if (fetchUserData($DATA['sender_userid'])) {
202 // Load email template
203 $mailText = loadEmailTemplate('member_user_pool_done', $content, $DATA['sender_userid']);
205 // Send it also waway
206 sendEmail(getUserData('userid'), '{--MEMBER_SEND_DONE_SUBJECT--}', $mailText);
209 // Set status to SEND because we completely send it away
210 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
211 array(bigintval($DATA['id'])), __FILE__, __LINE__);
213 // Update send-completed-time
214 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `timestamp_send`=UNIX_TIMESTAMP() WHERE `pool_id`=%s LIMIT 1",
215 array(bigintval($DATA['id'])), __FILE__, __LINE__);
217 $lastSentId = $DATA['id']; $GLOBALS['pool_cnt'] = '0';
218 $count2 += $GLOBALS['pool_cnt'];
220 // Update mediadata if version is 0.0.4 or higher
221 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
222 // Update entry (or add missing)
223 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
224 updateMediadataEntry(array('total_orders', 'normal_orders'), 'add', 1);
227 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$P);
230 // Do we have send maximum mails?
231 elseif (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || ($count2 >= getConfig('max_send'))) {
232 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
234 if ($GLOBALS['pool_cnt'] <= $DATA['target_send']) $add = ", target_send=target_send-".$GLOBALS['pool_cnt'];
235 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='NEW', receivers='%s'" . $add . " WHERE `id`=%s LIMIT 1",
237 implode(';', $dummy),
238 bigintval($DATA['id'])
239 ), __FILE__, __LINE__);
241 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'*');
246 SQL_FREERESULT($result_stats);
248 // User does not exists so we have add the sender's points back to sender's account
249 if (($receivers['id'] == '0') || (empty($receivers['id']))) {
251 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND' WHERE `id`=%s LIMIT 1",
252 array(bigintval($DATA['id'])), __FILE__, __LINE__);
254 // Is the userid set?
255 if (isValidUserId($userid)) {
256 // User does not exists, pay points back
257 $points = getPaymentPoints($DATA['payment_id']);
258 addPointsThroughReferalSystem('pool_payback', $DATA['sender_userid'], $points);
260 // Add points together and remove user
261 $pointsBack[$DATA['sender_userid']] += $points;
265 $count_back[$DATA['sender_userid']]++;
268 // Remove entry from list
272 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `receivers`='%s' WHERE `id`=%s LIMIT 1",
273 array(implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
279 // Do we have points to "pay back"?
280 if ((count($pointsBack) > 0) && (!empty($pointsBack[0]))) {
281 // Walk through all points
282 foreach ($pointsBack as $userid => $PB) {
283 // Add points only when we have points left to add and a valid user id
284 if (($PB > 0) && (isValidUserId($userid))) {
290 // We have to pay back some points to the sender (we add them directly :-P)
291 if (fetchUserData($userid)) {
292 // User account does exists, so we can safely pay back!
293 $mailText = loadEmailTemplate('member_user_pool_back', $content, bigintval($userid));
295 // Send mail out to member
296 sendEmail($userid, '{--MEMBER_BACK_JACKPOT--}' . ' (' . $userid . ')', $mailText);
297 } elseif (isExtensionActive('jackpot')) {
299 addPointsToJackpot($PB);
301 // Send mail out to admin
302 sendAdminNotification('{--ADMIN_BACK_JACKPOT--}' . ' (' . $userid . ')', 'admin_user_pool_back', $content, 'admin');
310 SQL_FREERESULT($result_main);