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://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 ($mailData = 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($mailData['id']), __FILE__, __LINE__);
86 // Check fetched data for HTML
88 if (isset($mailData['html_msg'])) $isHtml = $mailData['html_msg'];
91 if (!SQL_HASZEROAFFECTED()) {
92 // "Explode" all receivers into an array
93 if (isInString(';', $mailData['receivers'])) {
94 // There's more than one receiver in the list...
95 $receivers = explode(';', $mailData['receivers']);
96 } elseif (!empty($mailData['receivers'])) {
98 $receivers = array($mailData['receivers']);
101 $receivers = array(0);
103 $temporaryReceivers = $receivers;
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($mailData['id'], $mailData['sender_userid'], $mailData['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
120 `{?_MYSQL_PREFIX?}_user_stats`
143 bigintval($mailData['id']),
144 bigintval($mailData['sender_userid']),
145 bigintval($mailData['cat_id']),
146 bigintval($mailData['payment_id']),
147 $mailData['subject'],
149 bigintval($mailData['target_send']),
150 bigintval($mailData['timestamp'])
151 ), __FILE__, __LINE__);
153 // Receive it's id for the links table
154 $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",
155 array(bigintval($mailData['id']), bigintval($mailData['sender_userid']), bigintval($mailData['timestamp'])), __FILE__, __LINE__);
158 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.SQL_NUMROWS($result_stats).'!');
159 if (SQL_NUMROWS($result_stats) == 1) {
161 list($stats_id) = SQL_FETCHROW($result_stats);
163 // Remove receiver from list
164 $status = removeReceiver($temporaryReceivers, $key, bigintval($userid), bigintval($mailData['id']), bigintval($stats_id));
166 //* DEBUG: */ debugOutput('?L:'.__LINE__.'/'.$temporaryReceivers.'/'.$key.'/'.$userid.'('.['sender_userid'].')/'.$mailData['id'].'/'.$stats_id.'?');
170 $mailData['stats_id'] = bigintval($stats_id);
173 $mailData['time'] = getPaymentPoints($mailData['payment_id'], 'time');
174 $mailData['points'] = getPaymentPoints($mailData['payment_id'], 'payment');
176 // Load message template
177 $mailText = loadEmailTemplate('member_user_pool_normal', $mailData, bigintval($userid));
180 sendEmail(getUserData('userid'), $mailData['subject'], $mailText, $isHtml);
182 // Count sent mails...
183 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_sent`=`emails_sent`+1 WHERE `userid`=%s LIMIT 1",
184 array(bigintval($mailData['sender_userid'])), __FILE__, __LINE__);
186 if (isExtensionInstalledAndNewer('user', '0.1.4')) {
187 // Update mails received for receiver
188 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
189 array(bigintval($userid)), __FILE__, __LINE__);
192 // Update mediadata if version is 0.0.4 or higher
193 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
194 // Update entry (or add missing)
195 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
196 updateMediadataEntry(array('total_send', 'normal_send'), 'add', 1);
199 // And count up the mail
200 $GLOBALS['pool_cnt']++;
201 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'*');
205 // Entry already found, but we still count one up!
206 $GLOBALS['pool_cnt']++;
207 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt']);
210 default: // Unknown return type
211 logDebugMessage(__FILE__, __LINE__, 'Unknown status ' . $status . ' detected. pool_id=' . $mailData['id'] . ',sender=' . $mailData['sender_userid'] . ',stats_id=' . $stats_id);
216 // Do we have reached the maximum to send mails? || (getConfig('max_send') >= $GLOBALS['pool_cnt'])
217 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'>='.$mailData['target_send'].'/'.getConfig('max_send').'>='.$GLOBALS['pool_cnt'].'/'.$lastSentId.'!='.$mailData['id'].'*');
218 if ((($GLOBALS['pool_cnt'] >= $mailData['target_send'])) && ($lastSentId != $mailData['id'])) {
221 'sender_userid' => $mailData['sender_userid'],
222 'cat_id' => $mailData['cat_id'],
223 'text' => $mailData['text'],
224 'url' => $mailData['url'],
225 'expiration' => '{%pipe,createFancyTime=' . getPaymentPoints($mailData['payment_id'], 'time') . '%}'
228 // Yes we do, so we notify admin and sender about fully sent mail!
229 sendAdminNotification('{--ADMIN_SEND_DONE_SUBJECT--}', 'admin_user_pool_done', $content, $userid);
232 if (fetchUserData($mailData['sender_userid'])) {
233 // Load email template
234 $mailText = loadEmailTemplate('member_user_pool_done', $content, $mailData['sender_userid']);
236 // Send it also waway
237 sendEmail(getUserData('userid'), '{--MEMBER_SEND_DONE_SUBJECT--}', $mailText);
240 // Set status to SEND because we completely send it away
241 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND',`target_send`=0,`receivers`='' WHERE `id`=%s LIMIT 1",
242 array(bigintval($mailData['id'])), __FILE__, __LINE__);
244 // Update send-completed-time
245 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `timestamp_send`=UNIX_TIMESTAMP() WHERE `pool_id`=%s LIMIT 1",
246 array(bigintval($mailData['id'])), __FILE__, __LINE__);
248 $lastSentId = $mailData['id']; $GLOBALS['pool_cnt'] = '0';
249 $count2 += $GLOBALS['pool_cnt'];
251 // Update mediadata if version is 0.0.4 or higher
252 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
253 // Update entry (or add missing)
254 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
255 updateMediadataEntry(array('total_orders', 'normal_orders'), 'add', 1);
258 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$P);
261 // Do we have send maximum mails?
262 elseif (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || ($count2 >= getConfig('max_send'))) {
263 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
265 if ($GLOBALS['pool_cnt'] <= $mailData['target_send']) $add = ", target_send=target_send-".$GLOBALS['pool_cnt'];
266 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='NEW', receivers='%s'" . $add . " WHERE `id`=%s LIMIT 1",
268 implode(';', $temporaryReceivers),
269 bigintval($mailData['id'])
270 ), __FILE__, __LINE__);
272 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'*');
277 SQL_FREERESULT($result_stats);
279 // User does not exists so we have add the sender's points back to sender's account
280 if (($receivers['id'] == '0') || (empty($receivers['id']))) {
282 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND' WHERE `id`=%s LIMIT 1",
283 array(bigintval($mailData['id'])), __FILE__, __LINE__);
285 // Is the userid set?
286 if (isValidUserId($userid)) {
287 // User does not exists, pay points back
288 $points = getPaymentPoints($mailData['payment_id']);
289 initReferralSystem();
290 addPointsThroughReferralSystem('pool_payback', $mailData['sender_userid'], $points);
292 // Add points together and remove user
293 $pointsBack[$mailData['sender_userid']] += $points;
297 $count_back[$mailData['sender_userid']]++;
300 // Remove entry from list
301 unset($temporaryReceivers[$key]);
304 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `receivers`='%s' WHERE `id`=%s LIMIT 1",
305 array(implode(';', $temporaryReceivers), bigintval($mailData['id'])), __FILE__, __LINE__);
311 // Do we have points to "pay back"?
312 if ((count($pointsBack) > 0) && (!empty($pointsBack[0]))) {
313 // Walk through all points
314 foreach ($pointsBack as $userid => $PB) {
315 // Add points only when we have points left to add and a valid user id
316 if (($PB > 0) && (isValidUserId($userid))) {
322 // We have to pay back some points to the sender (we add them directly :-P)
323 if (fetchUserData($userid)) {
324 // User account does exists, so we can safely pay back!
325 $mailText = loadEmailTemplate('member_user_pool_back', $content, bigintval($userid));
327 // Send mail out to member
328 sendEmail($userid, '{--MEMBER_BACK_JACKPOT--}' . ' (' . $userid . ')', $mailText);
329 } elseif (isExtensionActive('jackpot')) {
331 addPointsToJackpot($PB);
333 // Send mail out to admin
334 sendAdminNotification('{--ADMIN_BACK_JACKPOT--}' . ' (' . $userid . ')', 'admin_user_pool_back', $content, 'admin');
342 SQL_FREERESULT($result_main);