2 /************************************************************************
3 * MXChange v0.2.1 Start: 11/08/2003 *
4 * =============== Last change: 07/01/2005 *
6 * -------------------------------------------------------------------- *
7 * File : pool-update.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Sends queued mails from the pool *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Sendet freigegebene Mails aus den Pool *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
23 * This program is distributed in the hope that it will be useful, *
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
26 * GNU General Public License for more details. *
28 * You should have received a copy of the GNU General Public License *
29 * along with this program; if not, write to the Free Software *
30 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
32 ************************************************************************/
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 // Test html extensions once
42 $HTML_EXT = EXT_IS_ACTIVE("html_mail", true);
44 // Check for freed mail orders to send out
47 // 0 1 2 3 4 5 6 7 8 9 10
48 $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__);
52 // 0 1 2 3 4 5 6 7 8 9 10
53 $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__);
57 $cnt = "0"; $cnt2 = "0"; $LAST_SENT_ID = "0"; $cnt_back = array("0"); $points_BACK = array("0");
58 if (SQL_NUMROWS($result_main) > 0)
60 while ($DATA = SQL_FETCHROW($result_main))
62 // Check fetched data for HTML
63 $HTML = $DATA[10]; if ($HTML == $DATA[0]) $HTML = "N";
65 // Compile URL and subject line
66 $DATA[7] = COMPILE_CODE($DATA[7]);
68 // Set mail order as "active". That means it will be sent out
69 $result_active = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='ACTIVE' WHERE id=%s AND data_type='NEW' LIMIT 1",
70 array($DATA[0]), __FILE__, __LINE__);
71 if (SQL_AFFECTEDROWS($result_active) == 1)
73 // "Explode" all receivers into an array
74 if (ereg(";", $DATA[4]))
76 // There's more than one receiver in the list...
77 $RECEIVERS = explode(";", $DATA[4]);
79 elseif (!empty($DATA[4]))
82 $RECEIVERS = array($DATA[4]);
87 $RECEIVERS = array("0");
91 // Now, if we are good little boys and girls Santa left us some user-ids.
92 // We can now send mails to them...
93 foreach ($RECEIVERS as $key=>$uid)
96 $result_user = SQL_QUERY_ESC("SELECT sex, surname, family, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
97 array(bigintval($uid)), __FILE__, __LINE__);
99 // Is his data available?
100 //* DEBUG: */ echo "*L:".__LINE__."/".SQL_NUMROWS($result_user)."*<br />";
101 if (SQL_NUMROWS($result_user) == 1)
103 // The final receiver does exists so we can continue...
104 list($salut, $sname, $fname, $email) = SQL_FETCHROW($result_user);
107 SQL_FREERESULT($result_user);
109 // Do we have a stats entry?
110 $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",
111 array($DATA[0], $DATA[1], $DATA[6]), __FILE__, __LINE__);
113 // If there's no stats entry add it!
114 //* DEBUG: */ echo "!L:".__LINE__."/".SQL_NUMROWS($result_stats)."!<br />";
115 if (SQL_NUMROWS($result_stats) == 0)
117 // No entry was found, so we add him!
118 $result_stats = 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())",
119 array(bigintval($DATA[0]), bigintval($DATA[1]), bigintval($DATA[9]), bigintval($DATA[5]), $DATA[2], $DATA[7], $DATA[8], bigintval($DATA[6])), __FILE__, __LINE__);
121 // Receive it's ID for the links table
122 $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",
123 array(bigintval($DATA[0]), bigintval($DATA[1]), bigintval($DATA[6])), __FILE__, __LINE__);
126 //* DEBUG: */ echo "!L:".__LINE__."/".SQL_NUMROWS($result_stats)."!<br />";
127 if (SQL_NUMROWS($result_stats) == 1)
130 list($stats_id) = SQL_FETCHROW($result_stats);
131 SQL_FREERESULT($result_stats);
133 // Mark this user as "spammed" ;-) And place a line for him...
134 //* DEBUG: */ echo "?L:".__LINE__."/".$dummy."/".$key."/".$uid."(".$DATA[1].")/".$DATA[0]."/".$stats_id."?<br />";
135 switch (REMOVE_RECEIVER($dummy, $key, bigintval($uid), bigintval($DATA[0]), bigintval($stats_id)))
139 $DATA[11] = bigintval($stats_id);
142 $DATA[14] = TRANSLATE_SEX($salut);
144 // Load message template
145 $msg = LOAD_EMAIL_TEMPLATE("normal-mail", $DATA[3], bigintval($uid));
148 SEND_EMAIL($email, $DATA[2], $msg, $HTML);
150 // Count sent mails...
151 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET emails_sent=emails_sent+1 WHERE userid=%s LIMIT 1",
152 array(bigintval($DATA[1])), __FILE__, __LINE__);
154 if (GET_EXT_VERSION("user") >= "0.1.4")
156 // Update mails received for receiver
157 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET emails_received=emails_received+1 WHERE userid=%s LIMIT 1",
158 array(bigintval($uid)), __FILE__, __LINE__);
161 // Update mediadata if version is 0.0.4 or higher
162 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
164 // Update entry (or add missing)
165 //* DEBUG: */ echo "*MEDIA/L:".__LINE__."*<br />";
166 MEDIA_UPDATE_ENTRY(array("total_send", "normal_send"), "add", 1);
169 // And count up the mail
171 //* DEBUG: */ echo"*EXIT/L:".__LINE__."/".$cnt."*<br />";
175 // Entry already found, but we still count one up!
177 //* DEBUG: */ echo"*EXIT/L:".__LINE__."/".$cnt."<br />";
182 // Do we have reached the maximum to send mails? || ($_CONFIG['max_send'] >= $cnt)
183 //* DEBUG: */ echo "*L:".__LINE__."/".$cnt.">=".$DATA[8]."/".$_CONFIG['max_send'].">=".$cnt."/".$LAST_SENT_ID."!=".$DATA[0]."*<br />";
184 if ((($cnt >= $DATA[8])) && ($LAST_SENT_ID != $DATA[0]))
186 // Yes we do, so we notify admin and sender about fully sent mail!
187 SEND_ADMIN_NOTIFICATION(ADMIN_SUBJ_SEND_DONE, "done-admin", $DATA[3], $uid);
190 $result_sender = SQL_QUERY_ESC("SELECT surname, family, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
191 array(bigintval($DATA[1])), __FILE__, __LINE__);
192 if (SQL_NUMROWS($result_sender) == 1)
194 // Load data and prepare mail
195 list($sname, $fname, $email) = SQL_FETCHROW($result_sender);
196 SQL_FREERESULT($result_sender);
197 $msg = LOAD_EMAIL_TEMPLATE("done-member", $DATA[3], $DATA[1]);
199 // Send it also waway
200 SEND_EMAIL($email, MEMBER_SUBJ_SEND_DONE, $msg);
203 // Set status to SEND because we completely send it away
204 $result_done = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='SEND', target_send='0', receivers='' WHERE id=%s LIMIT 1",
205 array(bigintval($DATA[0])), __FILE__, __LINE__);
207 // Update send-completed-time
208 $result_user = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_stats SET timestamp_send=UNIX_TIMESTAMP() WHERE pool_id=%s LIMIT 1",
209 array(bigintval($DATA[0])), __FILE__, __LINE__);
211 $LAST_SENT_ID = $DATA[0]; $cnt = "0";
214 // Update mediadata if version is 0.0.4 or higher
215 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
217 // Update entry (or add missing)
218 //* DEBUG: */ echo "*MEDIA/L:".__LINE__."*<br />";
219 MEDIA_UPDATE_ENTRY(array("total_orders", "normal_orders"), "add", 1);
222 //* DEBUG: */ echo"*EXIT/L:".__LINE__."/".$P."<br />";
225 // Do we have send maximum mails?
226 elseif (($cnt >= $_CONFIG['max_send']) || ($cnt2 >= $_CONFIG['max_send']))
228 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
230 if ($cnt <= $DATA[8]) $ADD = ", target_send=target_send-".$cnt;
231 $result_queue = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='NEW', receivers='%s'".$ADD." WHERE id=%s LIMIT 1",
232 array(implode(";", $dummy), bigintval($DATA[0])), __FILE__, __LINE__);
234 //* DEBUG: */ echo"*EXIT/L:".__LINE__."*<br />";
240 // User does not exists so we have add the sender's points back to sender's account
241 if (($RECEIVERS[0] == "0") || (empty($RECEIVERS[0])))
244 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='SEND' WHERE id=%s LIMIT 1",
245 array(bigintval($DATA[0])), __FILE__, __LINE__);
251 // User does not exists, pay points back
252 $points = GET_PAY_POINTS($DATA[5]);
253 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points SET points=points+%s WHERE userid=%s AND ref_depth=0 LIMIT 1",
254 array($points, bigintval($DATA[1])), __FILE__, __LINE__);
256 // Update mediadata as well
257 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
260 //* DEBUG: */ echo "*MEDIA/L:".__LINE__."/".$points."*<br />";
261 MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);
264 // Add points together and remove user
265 $points_BACK[$DATA[1]] += $points;
267 $cnt_back[$DATA[1]]++;
269 // Remove entry from list
273 $result_queue = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET receivers='%s' WHERE id=%s LIMIT 1",
274 array(implode(";", $dummy), bigintval($DATA[0])), __FILE__, __LINE__);
279 if ((sizeof($points_BACK) > 0) && (!empty($points_BACK[0])))
281 foreach ($points_BACK as $uid=>$PB)
283 // Add points only when we have points left to add and a valid user ID
284 if (($PB > 0) && ($uid > 0))
286 // We have to pay back some points to the sender (we add them directly :-P)
287 $result = SQL_QUERY_ESC("SELECT email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
288 array(bigintval($uid)), __FILE__, __LINE__);
289 $DATA[10] = $PB; $DATA[11] = $cnt_back[$uid];
290 if (SQL_NUMROWS($result) == 1)
292 list($email) = SQL_FETCHROW($result);
293 SQL_FREERESULT($result);
295 // User account does exists, so we can safely pay back!
296 $msg = LOAD_EMAIL_TEMPLATE("back-member", "", bigintval($uid));
298 // Send mail out to member
299 SEND_EMAIL($email, MEMBER_BACK_JACKPOT." (".$uid.")", $msg);
306 // Send mail out to admin
307 SEND_ADMIN_NOTIFICATION(ADMIN_BACK_JACKPOT." (".$uid.")", "back-admin", "", "admin");
315 SQL_FREERESULT($result_main);
317 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
318 if ((EXT_IS_ACTIVE("bonus") && ($cnt < $_CONFIG['max_send'])))
320 // Do we need to send out bonus mails?
323 // 0 1 2 3 4 5 6 7 8 9 10 11
324 $result_bonus = SQL_QUERY("SELECT id, subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, html_msg FROM "._MYSQL_PREFIX."_bonus WHERE data_type='NEW' ORDER BY timestamp DESC", __FILE__, __LINE__);
328 // 0 1 2 3 4 5 6 7 8 9 10 11
329 $result_bonus = SQL_QUERY("SELECT id, subject, text, receivers, points, time, data_type, timestamp, url, cat_id, target_send, 'N' FROM "._MYSQL_PREFIX."_bonus WHERE data_type='NEW' ORDER BY timestamp DESC", __FILE__, __LINE__);
332 if (SQL_NUMROWS($result_bonus) > 0)
334 // Send these mails away...
336 while ($DATA = SQL_FETCHROW($result_bonus))
339 $DATA[8] = COMPILE_CODE($DATA[8]);
341 // Message is active in queue
342 $result_queue = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='QUEUE' WHERE id=%s LIMIT 1",
343 array(bigintval($DATA[0])), __FILE__, __LINE__);
345 // "Explode" all receivers into an array
346 if (ereg(";", $DATA[3]))
348 // There's more than one receiver in the list...
349 $RECEIVERS = explode(";", $DATA[3]);
351 elseif (!empty($DATA[4]))
353 // Only one user left
354 $RECEIVERS = array($DATA[3]);
359 $RECEIVERS = array("0");
363 // Now, if we are good little boys and girls Santa left us some user-ids.
364 // We can now send mails to them...
365 foreach ($RECEIVERS as $key=>$uid)
367 // Load personal data
368 //* DEBUG: */ echo "*L:".__LINE__."/".$uid."*<br />";
369 $result_user = SQL_QUERY_ESC("SELECT surname, family, email FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1",
370 array(bigintval($uid)), __FILE__, __LINE__);
372 // Is his data available?
373 if (SQL_NUMROWS($result_user) == 1)
375 // The final receiver does exists so we can continue...
376 list($sname, $fname, $email) = SQL_FETCHROW($result_user);
377 //* DEBUG: */ echo "OK!/L:".__LINE__."<br />";
379 // Mark this user as "spammed" ;-) And place a line for him...
380 if (REMOVE_RECEIVER($dummy, $key, $uid, $DATA[0], $DATA[0], true) == "done")
383 $msg = LOAD_EMAIL_TEMPLATE("bonus-mail", $DATA[2], $uid);
386 SEND_EMAIL($email, $DATA[1], $msg, $DATA[11]);
388 // Count one up and remove entry from dummy array
389 $cnt++; unset($dummy[$key]);
391 if (GET_EXT_VERSION("user") >= "0.1.4")
393 // Update mails received for receiver
394 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET emails_received=emails_received+1 WHERE userid=%s LIMIT 1",
395 array(bigintval($uid)), __FILE__, __LINE__);
398 // Do we have send maximum mails?
399 if (($cnt >= $_CONFIG['max_send']) || (SELECTION_COUNT($dummy) == 0))
402 //* DEBUG: */ echo "*EXIT/L:".__LINE__."<br />";
409 SQL_FREERESULT($result_user);
412 // Update mediadata if version is 0.0.4 or higher
413 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
415 // Update entry (or add missing
417 if (!empty($cnt2) && empty($cnt)) $P = $cnt2;
418 //* DEBUG: */ echo "+MEDIA/L:".__LINE__."/".$P."+<br />";
419 MEDIA_UPDATE_ENTRY(array("total_send", "bonus_send"), "add", $P);
422 // Close sending system
423 //* DEBUG: */ echo "-L:".__LINE__."/".SELECTION_COUNT($dummy)."-<br />";
424 if (SELECTION_COUNT($dummy) == 0)
427 $result_done = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='SEND', target_send='0', receivers='' WHERE id=%s LIMIT 1",
428 array(bigintval($DATA[0])), __FILE__, __LINE__);
429 //* DEBUG: */ echo "*L:".__LINE__."*<br />";
431 // Update mediadata if version is 0.0.4 or higher
432 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
434 // Update entry (or add missing)
435 //* DEBUG: */ echo "*MEDIA/L:".__LINE__."*<br />";
436 MEDIA_UPDATE_ENTRY(array("total_orders", "bonus_orders"), "add", 1);
439 elseif ($cnt >= $_CONFIG['max_send'])
442 $result_done = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='NEW', target_send=%s, receivers='%s' WHERE id=%s LIMIT 1",
443 array(SELECTION_COUNT($dummy), implode(";", $dummy), bigintval($DATA[0])), __FILE__, __LINE__);
444 //* DEBUG: */ echo "*L:".__LINE__."<PRE>";
445 //* DEBUG: */ print_r($dummy);
446 //* DEBUG: */ echo "</PRE>\n!!!<br />";
453 SQL_FREERESULT($result_bonus);
456 // Remove message (IMPORTANT!)