Fix for undefined variable cnt in mails sending pool (Closed: #0000003)
[mailer.git] / inc / pool / pool-bonus.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 12/12/2008 *
4  * ===============                              Last change: 12/12/2008 *
5  *                                                                      *
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  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
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.                                  *
22  *                                                                      *
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.                         *
27  *                                                                      *
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,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Don't run on daily reset
41 if (defined('__DAILY_RESET')) {
42         // Skip here
43         return false;
44 } elseif (!EXT_IS_ACTIVE("bonus")) {
45         // Abort if extension bonus is not active
46         return false;
47 }
48
49 // Only send bonus mail when bonus extension is active and maximum send-mails is not reached
50 if ($GLOBALS['pool_cnt'] < getConfig('max_send')) {
51         // Do we need to send out bonus mails?
52         if (EXT_IS_ACTIVE("html_mail")) {
53                 //                                 0     1       2        3        4      5       6          7       8      9         10         11
54                 $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__);
55         } else {
56                 //                                 0     1       2        3        4      5       6          7       8      9         10
57                 $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__);
58         }
59
60         if (SQL_NUMROWS($result_bonus) > 0) {
61                 // Send these mails away...
62                 $cnt2 = "";
63                 while ($DATA = SQL_FETCHARRAY($result_bonus)) {
64                         // Compile URL
65                         $DATA['url'] = COMPILE_CODE($DATA['url']);
66
67                         // Message is active in queue
68                         $result_queue = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='QUEUE' WHERE id=%s LIMIT 1",
69                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
70
71                         // "Explode" all receivers into an array
72                         if (ereg(";", $DATA['receivers'])) {
73                                 // There's more than one receiver in the list...
74                                 $RECEIVERS = explode(";", $DATA['receivers']);
75                         } elseif (!empty($DATA['points'])) {
76                                 // Only one user left
77                                 $RECEIVERS = array($DATA['receivers']);
78                         } else {
79                                 // No users left
80                                 $RECEIVERS = array("0");
81                         }
82                         $dummy = $RECEIVERS;
83
84                         // Now, if we are good little boys and girls Santa left us some user-ids.
85                         // We can now send mails to them...
86                         foreach ($RECEIVERS as $key => $uid) {
87                                 // Load personal data
88                                 //* DEBUG: */ echo "*L:".__LINE__."/".$uid."*<br />";
89                                 $result_user = SQL_QUERY_ESC("SELECT surname, family, email FROM `"._MYSQL_PREFIX."_user_data` WHERE userid=%s LIMIT 1",
90                                         array(bigintval($uid)), __FILE__, __LINE__);
91
92                                 // Is his data available?
93                                 if (SQL_NUMROWS($result_user) == 1) {
94                                         // The final receiver does exists so we can continue...
95                                         list($sname, $fname, $email) = SQL_FETCHROW($result_user);
96                                         //* DEBUG: */ echo "OK!/L:".__LINE__."<br />";
97
98                                         // Mark this user as "spammed" ;-) And place a line for him...
99                                         if (REMOVE_RECEIVER($dummy, $key, $uid, $DATA['id'], $DATA['id'], true) == "done") {
100                                                 // Replace text variables
101                                                 foreach ($REPLACER as $key => $value) {
102                                                         if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
103                                                 } // END - foreach
104
105                                                 // Prepare content
106                                                 $content = array(
107                                                         'id'       => $DATA['id'],
108                                                         'url'      => $DATA['url'],
109                                                         'time'     => CREATE_FANCY_TIME($DATA['time']),
110                                                         'points'   => TRANSLATE_COMMA($DATA['points']),
111                                                         'category' => GET_CATEGORY($DATA['cat_id']),
112                                                         'text'     => $DATA['text']
113                                                 );
114
115                                                 // Prepare the mail
116                                                 $mailText = LOAD_EMAIL_TEMPLATE("bonus-mail", $content, $uid);
117
118                                                 // Send mail away
119                                                 if (isset($DATA['html_msg'])) {
120                                                         // Send HTML?
121                                                         SEND_EMAIL($email, $DATA['subject'], $mailText, $DATA['html_msg']);
122                                                 } else {
123                                                         // No HTML mail!
124                                                         SEND_EMAIL($email, $DATA['subject'], $mailText);
125                                                 }
126
127                                                 // Count one up and remove entry from dummy array
128                                                 $GLOBALS['pool_cnt']++; unset($dummy[$key]);
129
130                                                 if (GET_EXT_VERSION("user") >= "0.1.4") {
131                                                         // Update mails received for receiver
132                                                         $result = SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_user_data` SET emails_received=emails_received+1 WHERE userid=%s LIMIT 1",
133                                                                 array(bigintval($uid)), __FILE__, __LINE__);
134                                                 } // END - if
135
136                                                 // Do we have send maximum mails?
137                                                 if (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || (SELECTION_COUNT($dummy) == 0)) {
138                                                         // Yes, we have
139                                                         //* DEBUG: */ echo "*EXIT/L:".__LINE__."<br />";
140                                                         break;
141                                                 } // END - if
142                                         } // END - if
143                                 } // END - if
144
145                                 // Free some memory
146                                 SQL_FREERESULT($result_user);
147                         }
148
149                         // Update mediadata if version is 0.0.4 or higher
150                         if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
151                                 // Update entry (or add missing
152                                 $P = $GLOBALS['pool_cnt'];
153                                 if (!empty($cnt2) && empty($GLOBALS['pool_cnt'])) $P = $cnt2;
154                                 //* DEBUG: */ echo "+MEDIA/L:".__LINE__."/".$P."+<br />";
155                                 MEDIA_UPDATE_ENTRY(array("total_send", "bonus_send"), "add", $P);
156                         } // END - if
157
158                         // Close sending system
159                         //* DEBUG: */ echo "-L:".__LINE__."/".SELECTION_COUNT($dummy)."-<br />";
160                         if (SELECTION_COUNT($dummy) == 0) {
161                                 // Queue reached!
162                                 $result_done = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='SEND', target_send='0', receivers='' WHERE id=%s LIMIT 1",
163                                         array(bigintval($DATA['id'])), __FILE__, __LINE__);
164                                 //* DEBUG: */ echo "*L:".__LINE__."*<br />";
165
166                                 // Update mediadata if version is 0.0.4 or higher
167                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
168                                         // Update entry (or add missing)
169                                         //* DEBUG: */ echo "*MEDIA/L:".__LINE__."*<br />";
170                                         MEDIA_UPDATE_ENTRY(array("total_orders", "bonus_orders"), "add", 1);
171                                 } // END - if
172                         } elseif ($GLOBALS['pool_cnt'] >= getConfig('max_send')) {
173                                 // Update bonus pool
174                                 $result_done = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='NEW', target_send=%s, receivers='%s' WHERE id=%s LIMIT 1",
175                                  array(SELECTION_COUNT($dummy), implode(";", $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
176                                 //* DEBUG: */ echo "*L:".__LINE__."<PRE>";
177                                 //* DEBUG: */ print_r($dummy);
178                                 //* DEBUG: */ echo "</PRE>\n!!!<br />";
179                                 break;
180                         }
181                 }
182         }
183
184         // Free memory
185         SQL_FREERESULT($result_bonus);
186
187         // Remove variable
188         unset($mailText);
189 }
190
191 //
192 ?>