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, 2010 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()) {
47 } elseif (!isExtensionActive('user')) {
48 // Abort here if extension user is not active
53 // Check for freed mail orders to send out
54 if (isExtensionActive('html_mail')) {
55 // 0 1 2 3 4 5 6 7 8 9 10
56 $result_main = SQL_QUERY("SELECT id, sender, subject, text, receivers, payment_id, timestamp, url, target_send, cat_id, html_msg FROM `{?_MYSQL_PREFIX?}_pool` WHERE `data_type`='NEW' ORDER BY timestamp DESC", __FILE__, __LINE__);
58 // 0 1 2 3 4 5 6 7 8 9 10
59 $result_main = SQL_QUERY("SELECT id, sender, subject, text, receivers, payment_id, timestamp, url, target_send, cat_id, id FROM `{?_MYSQL_PREFIX?}_pool` WHERE `data_type`='NEW' ORDER BY timestamp DESC", __FILE__, __LINE__);
63 $count2 = '0'; $lastSentId = '0'; $count_back = array(0); $pointsBack = array(0);
64 if (!SQL_HASZERONUMS($result_main)) {
66 while ($DATA = SQL_FETCHARRAY($result_main, 0, false)) {
67 // Set mail order as 'active'. That means it will be sent out
68 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='ACTIVE' WHERE `id`=%s AND `data_type`='NEW' LIMIT 1",
69 array($DATA['id']), __FILE__, __LINE__);
71 // Check fetched data for HTML
73 if (isset($DATA['html_msg'])) $HTML = $DATA['html_msg'];
76 if (!SQL_HASZEROAFFECTED()) {
77 // "Explode" all receivers into an array
78 if (isInString(';', $DATA['receivers'])) {
79 // There's more than one receiver in the list...
80 $receiverS = explode(';', $DATA['receivers']);
81 } elseif (!empty($DATA['receivers'])) {
83 $receiverS = array($DATA['receivers']);
86 $receiverS = array(0);
90 // Now, if we are good little boys and girls Santa Claus left us some user-ids.
91 // We can now send mails to them...
92 foreach ($receiverS as $key => $userid) {
94 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.SQL_NUMROWS($result_user).'*');
95 if (fetchUserData($userid)) {
96 // Do we have a stats entry?
97 $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",
98 array($DATA['id'], $DATA['sender'], $DATA['timestamp']), __FILE__, __LINE__);
100 // If there's no stats entry add it!
101 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.SQL_NUMROWS($result_stats).'!');
102 if (SQL_HASZERONUMS($result_stats)) {
103 // No entry was found, so we add him!
104 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())",
105 array(bigintval($DATA['id']), bigintval($DATA['sender']), bigintval($DATA['cat_id']), bigintval($DATA['payment_id']), $DATA['subject'], $DATA['url'], $DATA['target_send'], bigintval($DATA['timestamp'])), __FILE__, __LINE__);
107 // Receive it's id for the links table
108 $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",
109 array(bigintval($DATA['id']), bigintval($DATA['sender']), bigintval($DATA['timestamp'])), __FILE__, __LINE__);
112 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.SQL_NUMROWS($result_stats).'!');
113 if (SQL_NUMROWS($result_stats) == 1) {
115 list($stats_id) = SQL_FETCHROW($result_stats);
117 // Mark this user as "spammed" ;-) And place a line for him...
118 //* DEBUG: */ debugOutput('?L:'.__LINE__.'/'.$dummy.'/'.$key.'/'.$userid.'('.['sender'].')/'.$DATA['id'].'/'.$stats_id.'?');
119 switch (removeReceiver($dummy, $key, bigintval($userid), bigintval($DATA['id']), bigintval($stats_id))) {
122 $DATA['stats_id'] = bigintval($stats_id);
124 // Replace text variables
125 foreach ($GLOBALS['replacer'] as $key => $value) {
126 if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
130 $DATA['time'] = getPaymentPoints($DATA['payment_id'], 'time');
131 $DATA['points'] = getPaymentPoints($DATA['payment_id'], 'payment');
133 // Load message template
134 $mailText = loadEmailTemplate('normal-mail', $DATA, bigintval($userid));
137 sendEmail(getUserData('email'), $DATA['subject'], $mailText, $HTML);
139 // Count sent mails...
140 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_sent=emails_sent+1 WHERE `userid`=%s LIMIT 1",
141 array(bigintval($DATA['sender'])), __FILE__, __LINE__);
143 if (isExtensionInstalledAndNewer('user', '0.1.4')) {
144 // Update mails received for receiver
145 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
146 array(bigintval($userid)), __FILE__, __LINE__);
149 // Update mediadata if version is 0.0.4 or higher
150 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
151 // Update entry (or add missing)
152 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
153 updateMediadataEntry(array('total_send', 'normal_send'), 'add', 1);
156 // And count up the mail
157 $GLOBALS['pool_cnt']++;
158 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'*');
162 // Entry already found, but we still count one up!
163 $GLOBALS['pool_cnt']++;
164 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt']);
169 // Do we have reached the maximum to send mails? || (getConfig('max_send') >= $GLOBALS['pool_cnt'])
170 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'>='.$DATA['target_send'].'/'.getConfig('max_send').'>='.$GLOBALS['pool_cnt'].'/'.$lastSentId.'!='.$DATA['id'].'*');
171 if ((($GLOBALS['pool_cnt'] >= $DATA['target_send'])) && ($lastSentId != $DATA['id'])) {
174 'sender_userid' => $DATA['sender'],
175 'cat_id' => $DATA['cat_id'],
176 'text' => $DATA['text'],
177 'url' => $DATA['url'],
178 'expiration' => '{%pipe,createFancyTime=' . getPaymentPoints($DATA['payment_id'], 'time') . '%}'
181 // Yes we do, so we notify admin and sender about fully sent mail!
182 sendAdminNotification('{--ADMIN_SEND_DONE_SUBJECT--}', 'done-admin', $content, $userid);
185 if (fetchUserData($DATA['sender'])) {
186 // Load email template
187 $mailText = loadEmailTemplate('done-member', $content, $DATA['sender']);
189 // Send it also waway
190 sendEmail(getUserData('email'), '{--MEMBER_SEND_DONE_SUBJECT--}', $mailText);
193 // Set status to SEND because we completely send it away
194 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
195 array(bigintval($DATA['id'])), __FILE__, __LINE__);
197 // Update send-completed-time
198 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `timestamp_send`=UNIX_TIMESTAMP() WHERE `pool_id`=%s LIMIT 1",
199 array(bigintval($DATA['id'])), __FILE__, __LINE__);
201 $lastSentId = $DATA['id']; $GLOBALS['pool_cnt'] = '0';
202 $count2 += $GLOBALS['pool_cnt'];
204 // Update mediadata if version is 0.0.4 or higher
205 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
206 // Update entry (or add missing)
207 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
208 updateMediadataEntry(array('total_orders', 'normal_orders'), 'add', 1);
211 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$P);
214 // Do we have send maximum mails?
215 elseif (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || ($count2 >= getConfig('max_send'))) {
216 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
218 if ($GLOBALS['pool_cnt'] <= $DATA['target_send']) $add = ", target_send=target_send-".$GLOBALS['pool_cnt'];
219 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='NEW', receivers='%s'" . $add . " WHERE `id`=%s LIMIT 1",
221 implode(';', $dummy),
222 bigintval($DATA['id'])
223 ), __FILE__, __LINE__);
225 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'*');
230 SQL_FREERESULT($result_stats);
232 // User does not exists so we have add the sender's points back to sender's account
233 if (($receiverS['id'] == '0') || (empty($receiverS['id']))) {
235 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND' WHERE `id`=%s LIMIT 1",
236 array(bigintval($DATA['id'])), __FILE__, __LINE__);
238 // Is the userid set?
239 if (isValidUserId($userid)) {
240 // User does not exists, pay points back
241 $points = getPaymentPoints($DATA['payment_id']);
242 addPointsDirectly('pool_payback', $DATA['sender'], $points);
244 // Add points together and remove user
245 $pointsBack[$DATA['sender']] += $points;
249 $count_back[$DATA['sender']]++;
252 // Remove entry from list
256 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `receivers`='%s' WHERE `id`=%s LIMIT 1",
257 array(implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
263 // Do we have points to "pay back"?
264 if ((count($pointsBack) > 0) && (!empty($pointsBack[0]))) {
265 // Walk through all points
266 foreach ($pointsBack as $userid => $PB) {
267 // Add points only when we have points left to add and a valid user id
268 if (($PB > 0) && (isValidUserId($userid))) {
274 // We have to pay back some points to the sender (we add them directly :-P)
275 if (fetchUserData($userid)) {
276 // User account does exists, so we can safely pay back!
277 $mailText = loadEmailTemplate('back-member', $content, bigintval($userid));
279 // Send mail out to member
280 sendEmail(getUserData('email'), '{--MEMBER_BACK_JACKPOT--}' . ' (' . $userid . ')', $mailText);
281 } elseif (isExtensionActive('jackpot')) {
283 addPointsToJackpot($PB);
285 // Send mail out to admin
286 sendAdminNotification('{--ADMIN_BACK_JACKPOT--}' . ' (' . $userid . ')', 'back-admin', $content, 'admin');
294 SQL_FREERESULT($result_main);