The yearly copyright-update commit. 2009, 2010 are now copyrighted on the developer...
[mailer.git] / inc / pool / pool-bonus.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 }
44
45 // Don't run on daily reset
46 if (isResetModeEnabled()) {
47         // Skip here
48         return false;
49 } elseif (!isExtensionActive('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 (isExtensionActive('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                         // Message is active in queue
71                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='QUEUE' WHERE `id`=%s LIMIT 1",
72                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
73
74                         // "Explode" all receivers into an array
75                         if (ereg(';', $DATA['receivers'])) {
76                                 // There's more than one receiver in the list...
77                                 $receiverS = explode(';', $DATA['receivers']);
78                         } elseif (!empty($DATA['points'])) {
79                                 // Only one user left
80                                 $receiverS = array($DATA['receivers']);
81                         } else {
82                                 // No users left
83                                 $receiverS = array(0);
84                         }
85                         $dummy = $receiverS;
86
87                         // Now, if we are good little boys and girls Santa Claus left us some user-ids.
88                         // We can now send mails to them...
89                         foreach ($receiverS as $key => $userid) {
90                                 // Load personal data
91                                 if (fetchUserData($userid)) {
92                                         // The final receiver does exists so we can continue...
93                                         //* DEBUG: */ print("OK!/L:".__LINE__."<br />");
94
95                                         // Mark this user as "spammed" ;-) And place a line for him...
96                                         if (removeReceiver($dummy, $key, $userid, $DATA['id'], $DATA['id'], true) == 'done') {
97                                                 // Replace text variables
98                                                 foreach ($GLOBALS['replacer'] as $key => $value) {
99                                                         if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
100                                                 } // END - foreach
101
102                                                 // Prepare content
103                                                 $content = array(
104                                                         'id'       => $DATA['id'],
105                                                         'url'      => $DATA['url'],
106                                                         'time'     => createFancyTime($DATA['time']),
107                                                         'points'   => translateComma($DATA['points']),
108                                                         'category' => getCategory($DATA['cat_id']),
109                                                         'text'     => $DATA['text']
110                                                 );
111
112                                                 // Prepare the mail
113                                                 $mailText = loadEmailTemplate('bonus-mail', $content, $userid);
114
115                                                 // Send mail away
116                                                 if (isset($DATA['html_msg'])) {
117                                                         // Send HTML?
118                                                         sendEmail(getUserData('email'), $DATA['subject'], $mailText, $DATA['html_msg']);
119                                                 } else {
120                                                         // No HTML mail!
121                                                         sendEmail(getUserData('email'), $DATA['subject'], $mailText);
122                                                 }
123
124                                                 // Count one up and remove entry from dummy array
125                                                 $GLOBALS['pool_cnt']++; unset($dummy[$key]);
126
127                                                 if (getExtensionVersion('user') >= '0.1.4') {
128                                                         // Update mails received for receiver
129                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
130                                                         array(bigintval($userid)), __FILE__, __LINE__);
131                                                 } // END - if
132
133                                                 // Do we have send maximum mails?
134                                                 if (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || (countSelection($dummy) == '0')) {
135                                                         // Yes, we have
136                                                         //* DEBUG: */ print("*EXIT/L:".__LINE__."<br />");
137                                                         break;
138                                                 } // END - if
139                                         } // END - if
140                                 } // END - if
141                         } // END - foreach
142
143                         // Update mediadata if version is 0.0.4 or higher
144                         if (getExtensionVersion('mediadata') >= '0.0.4') {
145                                 // Update entry (or add missing
146                                 $P = $GLOBALS['pool_cnt'];
147                                 if (!empty($cnt2) && empty($GLOBALS['pool_cnt'])) $P = $cnt2;
148                                 //* DEBUG: */ print("+MEDIA/L:".__LINE__.'/'.$P."+<br />");
149                                 updateMediadataEntry(array('total_send', 'bonus_send'), 'add', $P);
150                         } // END - if
151
152                         // Close sending system
153                         //* DEBUG: */ print("-L:".__LINE__.'/'.countSelection($dummy)."-<br />");
154                         if (countSelection($dummy) == '0') {
155                                 // Queue reached!
156                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
157                                 array(bigintval($DATA['id'])), __FILE__, __LINE__);
158                                 //* DEBUG: */ print("*L:".__LINE__."*<br />");
159
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: */ print("*MEDIA/L:".__LINE__."*<br />");
164                                         updateMediadataEntry(array('total_orders', 'bonus_orders'), 'add', 1);
165                                 } // END - if
166                         } elseif ($GLOBALS['pool_cnt'] >= getConfig('max_send')) {
167                                 // Update bonus pool
168                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='NEW', `target_send`=%s, `receivers`='%s' WHERE `id`=%s LIMIT 1",
169                                 array(countSelection($dummy), implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
170                                 //* DEBUG: */ print("*L:".__LINE__."<pre>");
171                                 //* DEBUG: */ print(print_r($dummy, true));
172                                 //* DEBUG: */ print("</pre>\n!!!<br />");
173                                 break;
174                         }
175                 }
176         }
177
178         // Free memory
179         SQL_FREERESULT($result_bonus);
180
181         // Remove variable
182         unset($mailText);
183 }
184
185 //
186 ?>