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 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * For more information visit: http://www.mxchange.org *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the Free Software *
35 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
37 ************************************************************************/
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
44 // Don't run on daily reset
45 if (isResetModeEnabled()) {
48 } elseif (!isExtensionActive('user')) {
49 // Abort here if extension user is not active
54 // Check for freed mail orders to send out
55 if (isExtensionActive('html_mail')) {
56 // 0 1 2 3 4 5 6 7 8 9 10
57 $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__);
59 // 0 1 2 3 4 5 6 7 8 9 10
60 $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__);
64 $cnt2 = '0'; $lastSentId = '0'; $cnt_back = array(0); $pointsBack = array(0);
65 if (SQL_NUMROWS($result_main) > 0) {
67 while ($DATA = SQL_FETCHARRAY($result_main, 0, false)) {
68 // Set mail order as 'active'. That means it will be sent out
69 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='ACTIVE' WHERE `id`=%s AND `data_type`='NEW' LIMIT 1",
70 array($DATA['id']), __FILE__, __LINE__);
72 // Check fetched data for HTML
74 if (isset($DATA['html_msg'])) $HTML = $DATA['html_msg'];
77 if (SQL_AFFECTEDROWS() == 1) {
78 // "Explode" all receivers into an array
79 if (ereg(';', $DATA['receivers'])) {
80 // There's more than one receiver in the list...
81 $RECEIVERS = explode(';', $DATA['receivers']);
82 } elseif (!empty($DATA['receivers'])) {
84 $RECEIVERS = array($DATA['receivers']);
87 $RECEIVERS = array(0);
91 // Now, if we are good little boys and girls Santa Claus left us some user-ids.
92 // We can now send mails to them...
93 foreach ($RECEIVERS as $key => $userid) {
95 //* DEBUG: */ outputHtml("*L:".__LINE__.'/'.SQL_NUMROWS($result_user)."*<br />");
96 if (fetchUserData($userid)) {
97 // Do we have a stats entry?
98 $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",
99 array($DATA['id'], $DATA['sender'], $DATA['timestamp']), __FILE__, __LINE__);
101 // If there's no stats entry add it!
102 //* DEBUG: */ outputHtml("!L:".__LINE__.'/'.SQL_NUMROWS($result_stats)."!<br />");
103 if (SQL_NUMROWS($result_stats) == '0') {
104 // No entry was found, so we add him!
105 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())",
106 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__);
108 // Receive it's id for the links table
109 $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",
110 array(bigintval($DATA['id']), bigintval($DATA['sender']), bigintval($DATA['timestamp'])), __FILE__, __LINE__);
113 //* DEBUG: */ outputHtml("!L:".__LINE__.'/'.SQL_NUMROWS($result_stats)."!<br />");
114 if (SQL_NUMROWS($result_stats) == 1) {
116 list($stats_id) = SQL_FETCHROW($result_stats);
118 // Mark this user as "spammed" ;-) And place a line for him...
119 //* DEBUG: */ outputHtml("?L:".__LINE__.'/'.$dummy.'/'.$key.'/'.$userid.'('.['sender'].")/".$DATA['id'].'/'.$stats_id."?<br />");
120 switch (removeReceiver($dummy, $key, bigintval($userid), bigintval($DATA['id']), bigintval($stats_id))) {
123 $DATA['stats_id'] = bigintval($stats_id);
124 $DATA['surname'] = getUserData('surname');
125 $DATA['family'] = getUserData('family');
126 $DATA['gender'] = translateGender(getUserData('gender'));
128 // Replace text variables
129 foreach ($GLOBALS['replacer'] as $key => $value) {
130 if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
135 'id' => $DATA['stats_id'],
136 'url' => $DATA['url'],
137 'sender_userid' => $DATA['sender'],
138 'category' => getCategory($DATA['cat_id']),
139 'time' => createFancyTime(getPaymentPoints($DATA['payment_id'], 'time')),
140 'points' => translateComma(getPaymentPoints($DATA['payment_id'], 'payment')),
141 'text' => $DATA['text']
144 // Load message template
145 $mailText = loadEmailTemplate('normal-mail', $content, bigintval($userid));
148 sendEmail(getUserData('email'), $DATA['subject'], $mailText, $HTML);
150 // Count sent mails...
151 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_sent=emails_sent+1 WHERE `userid`=%s LIMIT 1",
152 array(bigintval($DATA['sender'])), __FILE__, __LINE__);
154 if (getExtensionVersion('user') >= '0.1.4') {
155 // Update mails received for receiver
156 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_received=emails_received+1 WHERE `userid`=%s LIMIT 1",
157 array(bigintval($userid)), __FILE__, __LINE__);
160 // Update mediadata if version is 0.0.4 or higher
161 if (getExtensionVersion('mediadata') >= '0.0.4') {
162 // Update entry (or add missing)
163 //* DEBUG: */ outputHtml("*MEDIA/L:".__LINE__."*<br />");
164 updateMediadataEntry(array('total_send', 'normal_send'), 'add', 1);
167 // And count up the mail
168 $GLOBALS['pool_cnt']++;
169 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__.'/'.$GLOBALS['pool_cnt']."*<br />");
173 // Entry already found, but we still count one up!
174 $GLOBALS['pool_cnt']++;
175 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__.'/'.$GLOBALS['pool_cnt']."<br />");
180 // Do we have reached the maximum to send mails? || (getConfig('max_send') >= $GLOBALS['pool_cnt'])
181 //* DEBUG: */ outputHtml("*L:".__LINE__.'/'.$GLOBALS['pool_cnt'].">=".$DATA['target_send'].'/'.getConfig('max_send').">=".$GLOBALS['pool_cnt'].'/'.$lastSentId."!=".$DATA['id']."*<br />");
182 if ((($GLOBALS['pool_cnt'] >= $DATA['target_send'])) && ($lastSentId != $DATA['id'])) {
185 'sender_userid' => $DATA['sender'],
186 'category' => getCategory($DATA['cat_id']),
187 'text' => $DATA['text'],
188 'url' => $DATA['url'],
189 'expiration' => createFancyTime(getPaymentPoints($DATA['payment_id'], "time"))
192 // Yes we do, so we notify admin and sender about fully sent mail!
193 sendAdminNotification(getMessage('ADMIN_SUBJ_SEND_DONE'), 'done-admin', $content, $userid);
196 if (fetchUserData($DATA['sender'])) {
197 // Load email template
198 $mailText = loadEmailTemplate('done-member', $content, $DATA['sender']);
200 // Send it also waway
201 sendEmail(getUserData('email'), getMessage('MEMBER_SUBJ_SEND_DONE'), $mailText);
204 // Set status to SEND because we completely send it away
205 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
206 array(bigintval($DATA['id'])), __FILE__, __LINE__);
208 // Update send-completed-time
209 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `timestamp_send`=UNIX_TIMESTAMP() WHERE `pool_id`=%s LIMIT 1",
210 array(bigintval($DATA['id'])), __FILE__, __LINE__);
212 $lastSentId = $DATA['id']; $GLOBALS['pool_cnt'] = '0';
213 $cnt2 += $GLOBALS['pool_cnt'];
215 // Update mediadata if version is 0.0.4 or higher
216 if (getExtensionVersion('mediadata') >= '0.0.4') {
217 // Update entry (or add missing)
218 //* DEBUG: */ outputHtml("*MEDIA/L:".__LINE__."*<br />");
219 updateMediadataEntry(array('total_orders', 'normal_orders'), 'add', 1);
222 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__.'/'.$P."<br />");
225 // Do we have send maximum mails?
226 elseif (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || ($cnt2 >= getConfig('max_send'))) {
227 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
229 if ($GLOBALS['pool_cnt'] <= $DATA['target_send']) $add = ", target_send=target_send-".$GLOBALS['pool_cnt'];
230 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='NEW', receivers='%s'".$add." WHERE `id`=%s LIMIT 1",
231 array(implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
233 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__."*<br />");
238 SQL_FREERESULT($result_stats);
240 // User does not exists so we have add the sender's points back to sender's account
241 if (($RECEIVERS['id'] == '0') || (empty($RECEIVERS['id']))) {
243 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND' WHERE `id`=%s LIMIT 1",
244 array(bigintval($DATA['id'])), __FILE__, __LINE__);
246 // Is the userid set?
248 // User does not exists, pay points back
249 $points = getPaymentPoints($DATA['payment_id']);
250 addPointsDirectly('pool_payback', $DATA['sender'], $points);
252 // Add points together and remove user
253 $pointsBack[$DATA['sender']] += $points;
257 $cnt_back[$DATA['sender']]++;
260 // Remove entry from list
264 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `receivers`='%s' WHERE `id`=%s LIMIT 1",
265 array(implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
271 // Do we have points to "pay back"?
272 if ((count($pointsBack) > 0) && (!empty($pointsBack[0]))) {
273 // Walk through all points
274 foreach ($pointsBack as $userid => $PB) {
275 // Add points only when we have points left to add and a valid user id
276 if (($PB > 0) && ($userid > 0)) {
279 'points' => translateComma($PB)
282 // We have to pay back some points to the sender (we add them directly :-P)
283 if (fetchUserData($userid)) {
284 // User account does exists, so we can safely pay back!
285 $mailText = loadEmailTemplate('back-member', $content, bigintval($userid));
287 // Send mail out to member
288 sendEmail(getUserData('email'), getMessage('MEMBER_BACK_JACKPOT') . ' (' . $userid . ')', $mailText);
289 } elseif (isExtensionActive('jackpot')) {
291 addPointsToJackpot($PB);
293 // Send mail out to admin
294 sendAdminNotification(getMessage('ADMIN_BACK_JACKPOT') . ' (' . $userid . ')', 'back-admin', $content, 'admin');
302 SQL_FREERESULT($result_main);