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