2 /************************************************************************
3 * MXChange v0.2.1 Start: 12/12/2008 *
4 * =============== Last change: 12/12/2008 *
6 * -------------------------------------------------------------------- *
7 * File : pool-bonus.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Sends queued bonus mails from the pool *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Sendet freigegebene Bonus-Mails aus dem 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('bonus')) {
49 // Abort if extension bonus is not active
54 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
55 if ($GLOBALS['pool_cnt'] < getConfig('max_send')) {
56 // Do we need to send out bonus mails?
57 if (isExtensionActive('html_mail')) {
58 // 0 1 2 3 4 5 6 7 8 9 10 11
59 $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__);
61 // 0 1 2 3 4 5 6 7 8 9 10
62 $result_bonus = SQL_QUERY("SELECT `id`, `subject`, `text`, `receivers`, `points`, `time`, `data_type`, `timestamp`, `url`, `cat_id`, `target_send` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='NEW' ORDER BY `timestamp` DESC", __FILE__, __LINE__);
65 if (SQL_NUMROWS($result_bonus) > 0) {
66 // Send these mails away...
68 while ($DATA = SQL_FETCHARRAY($result_bonus)) {
69 // Message is active in queue
70 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
71 array(bigintval($DATA['id'])), __FILE__, __LINE__);
73 // "Explode" all receivers into an array
74 if (ereg(';', $DATA['receivers'])) {
75 // There's more than one receiver in the list...
76 $RECEIVERS = explode(';', $DATA['receivers']);
77 } elseif (!empty($DATA['points'])) {
79 $RECEIVERS = array($DATA['receivers']);
82 $RECEIVERS = array(0);
86 // Now, if we are good little boys and girls Santa left us some user-ids.
87 // We can now send mails to them...
88 foreach ($RECEIVERS as $key => $userid) {
90 //* DEBUG: */ print("*L:".__LINE__.'/'.$userid."*<br />");
91 $result_user = SQL_QUERY_ESC("SELECT `surname`, `family`, `email` FROM `{?_MYSQL_PREFIX?}_user_data` WHERE `userid`=%s LIMIT 1",
92 array(bigintval($userid)), __FILE__, __LINE__);
94 // Is his data available?
95 if (SQL_NUMROWS($result_user) == 1) {
96 // The final receiver does exists so we can continue...
97 list($surname, $family, $email) = SQL_FETCHROW($result_user);
98 //* DEBUG: */ print("OK!/L:".__LINE__."<br />");
100 // Mark this user as "spammed" ;-) And place a line for him...
101 if (removeReceiver($dummy, $key, $userid, $DATA['id'], $DATA['id'], true) == 'done') {
102 // Replace text variables
103 foreach ($GLOBALS['replacer'] as $key => $value) {
104 if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
110 'url' => $DATA['url'],
111 'time' => createFancyTime($DATA['time']),
112 'points' => translateComma($DATA['points']),
113 'category' => getCategory($DATA['cat_id']),
114 'text' => $DATA['text']
118 $mailText = loadEmailTemplate('bonus-mail', $content, $userid);
121 if (isset($DATA['html_msg'])) {
123 sendEmail($email, $DATA['subject'], $mailText, $DATA['html_msg']);
126 sendEmail($email, $DATA['subject'], $mailText);
129 // Count one up and remove entry from dummy array
130 $GLOBALS['pool_cnt']++; unset($dummy[$key]);
132 if (getExtensionVersion('user') >= '0.1.4') {
133 // Update mails received for receiver
134 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_received=emails_received+1 WHERE `userid`=%s LIMIT 1",
135 array(bigintval($userid)), __FILE__, __LINE__);
138 // Do we have send maximum mails?
139 if (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || (countSelection($dummy) == 0)) {
141 //* DEBUG: */ print("*EXIT/L:".__LINE__."<br />");
148 SQL_FREERESULT($result_user);
151 // Update mediadata if version is 0.0.4 or higher
152 if (getExtensionVersion('mediadata') >= '0.0.4') {
153 // Update entry (or add missing
154 $P = $GLOBALS['pool_cnt'];
155 if (!empty($cnt2) && empty($GLOBALS['pool_cnt'])) $P = $cnt2;
156 //* DEBUG: */ print("+MEDIA/L:".__LINE__.'/'.$P."+<br />");
157 updateMediadataEntry(array("total_send", "bonus_send"), 'add', $P);
160 // Close sending system
161 //* DEBUG: */ print("-L:".__LINE__.'/'.countSelection($dummy)."-<br />");
162 if (countSelection($dummy) == 0) {
164 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
165 array(bigintval($DATA['id'])), __FILE__, __LINE__);
166 //* DEBUG: */ print("*L:".__LINE__."*<br />");
168 // Update mediadata if version is 0.0.4 or higher
169 if (getExtensionVersion('mediadata') >= '0.0.4') {
170 // Update entry (or add missing)
171 //* DEBUG: */ print("*MEDIA/L:".__LINE__."*<br />");
172 updateMediadataEntry(array("total_orders", "bonus_orders"), 'add', 1);
174 } elseif ($GLOBALS['pool_cnt'] >= getConfig('max_send')) {
176 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='NEW', `target_send`=%s, `receivers`='%s' WHERE `id`=%s LIMIT 1",
177 array(countSelection($dummy), implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
178 //* DEBUG: */ print("*L:".__LINE__."<pre>");
179 //* DEBUG: */ print(print_r($dummy, true));
180 //* DEBUG: */ print("</pre>\n!!!<br />");
187 SQL_FREERESULT($result_bonus);