99e50f68452af90ff99e01cee3ab282f571615e6
[mailer.git] / inc / pool / pool-user.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 12/12/2008 *
4  * ===============                              Last change: 12/12/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : pool-user.php                                    *
8  * -------------------------------------------------------------------- *
9  * Short description : Sends queued user mails from the pool            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sendet freigegebene Mitglieder-Mails aus 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("user")) {
45         // Abort here if extension user is not active
46         return false;
47 }
48
49 // Need this here
50 global $REPLACER;
51
52 // Check for freed mail orders to send out
53 if (EXT_IS_ACTIVE("html_mail")) {
54         //                                0     1        2      3       4          5            6      7        8          9       10
55         $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__);
56 } else {
57         //                                0     1        2      3       4          5            6      7        8          9    10
58         $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__);
59 }
60
61 // Reset variables
62 $cnt2 = 0; $LAST_SENT_ID = 0; $cnt_back = array("0"); $pointsBack = array("0");
63 if (SQL_NUMROWS($result_main) > 0) {
64         // Parse all mails
65         while ($DATA = SQL_FETCHROW($result_main)) {
66                 // Set mail order as "active". That means it will be sent out
67                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET data_type='ACTIVE' WHERE id=%s AND data_type='NEW' LIMIT 1",
68                         array($DATA[0]), __FILE__, __LINE__);
69
70                 // Check fetched data for HTML
71                 $HTML = $DATA[10]; if ($HTML == $DATA[0]) $HTML = "N";
72
73                 // Compile URL and subject line
74                 $DATA[7] = COMPILE_CODE($DATA[7]);
75
76                 // Entry updated?
77                 if (SQL_AFFECTEDROWS() == 1) {
78                         // "Explode" all receivers into an array
79                         if (ereg(";", $DATA[4])) {
80                                 // There's more than one receiver in the list...
81                                 $RECEIVERS = explode(";", $DATA[4]);
82                         } elseif (!empty($DATA[4])) {
83                                 // Only one user left
84                                 $RECEIVERS = array($DATA[4]);
85                         } else {
86                                 // No users left
87                                 $RECEIVERS = array("0");
88                         }
89                         $dummy = $RECEIVERS;
90
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) {
94                                 // Lookup user ID
95                                 $result_user = SQL_QUERY_ESC("SELECT gender, 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                                 //* DEBUG: */ echo "*L:".__LINE__."/".SQL_NUMROWS($result_user)."*<br />";
100                                 if (SQL_NUMROWS($result_user) == 1) {
101                                         // The final receiver does exists so we can continue...
102                                         list($gender, $sname, $fname, $email) = SQL_FETCHROW($result_user);
103
104                                         // Free memory
105                                         SQL_FREERESULT($result_user);
106
107                                         // Do we have a stats entry?
108                                         $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",
109                                                 array($DATA[0], $DATA[1], $DATA[6]), __FILE__, __LINE__);
110
111                                         // If there's no stats entry add it!
112                                         //* DEBUG: */ echo "!L:".__LINE__."/".SQL_NUMROWS($result_stats)."!<br />";
113                                         if (SQL_NUMROWS($result_stats) == 0) {
114                                                 // No entry was found, so we add him!
115                                                 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())",
116                                                         array(bigintval($DATA[0]), bigintval($DATA[1]), bigintval($DATA[9]), bigintval($DATA[5]), $DATA[2], $DATA[7], $DATA[8], bigintval($DATA[6])), __FILE__, __LINE__);
117
118                                                 // Receive it's ID for the links table
119                                                 $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",
120                                                         array(bigintval($DATA[0]), bigintval($DATA[1]), bigintval($DATA[6])), __FILE__, __LINE__);
121                                         } // END - if
122
123                                         //* DEBUG: */ echo "!L:".__LINE__."/".SQL_NUMROWS($result_stats)."!<br />";
124                                         if (SQL_NUMROWS($result_stats) == 1) {
125                                                 // We got one!
126                                                 list($stats_id) = SQL_FETCHROW($result_stats);
127
128                                                 // Mark this user as "spammed" ;-) And place a line for him...
129                                                 //* DEBUG: */ echo "?L:".__LINE__."/".$dummy."/".$key."/".$uid."(".$DATA[1].")/".$DATA[0]."/".$stats_id."?<br />";
130                                                 switch (REMOVE_RECEIVER($dummy, $key, bigintval($uid), bigintval($DATA[0]), bigintval($stats_id)))
131                                                 {
132                                                 case "done":
133                                                         // Prepare the mail
134                                                         $DATA[11] = bigintval($stats_id);
135                                                         $DATA[12] = $sname;
136                                                         $DATA[13] = $fname;
137                                                         $DATA[14] = TRANSLATE_GENDER($gender);
138
139                                                         // Replace text variables
140                                                         foreach ($REPLACER as $key => $value) {
141                                                                 if (isset($DATA[$key])) $DATA[3] = str_replace($value, $DATA[$key], $DATA[3]);
142                                                         } // END - if
143
144                                                         // Prepare content
145                                                         $content = array(
146                                                                 'id'         => $DATA[11],
147                                                                 'url'        => $DATA[7],
148                                                                 'sender_uid' => $DATA[1],
149                                                                 'category'   => GET_CATEGORY($DATA[9]),
150                                                                 'time'       => CREATE_FANCY_TIME(GET_PAY_POINTS($DATA[5], "time")),
151                                                                 'points'     => TRANSLATE_COMMA(GET_PAY_POINTS($DATA[5], "payment")),
152                                                                 'text'       => $DATA[3]
153                                                         );
154
155                                                         // Load message template
156                                                         $mailText = LOAD_EMAIL_TEMPLATE("normal-mail", $content, bigintval($uid));
157
158                                                         // Send mail away
159                                                         SEND_EMAIL($email, $DATA[2], $mailText, $HTML);
160
161                                                         // Count sent mails...
162                                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET emails_sent=emails_sent+1 WHERE userid=%s LIMIT 1",
163                                                                 array(bigintval($DATA[1])), __FILE__, __LINE__);
164
165                                                         if (GET_EXT_VERSION("user") >= "0.1.4") {
166                                                                 // Update mails received for receiver
167                                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET emails_received=emails_received+1 WHERE userid=%s LIMIT 1",
168                                                                         array(bigintval($uid)), __FILE__, __LINE__);
169                                                         } // END - if
170
171                                                         // Update mediadata if version is 0.0.4 or higher
172                                                         if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
173                                                                 // Update entry (or add missing)
174                                                                 //* DEBUG: */ echo "*MEDIA/L:".__LINE__."*<br />";
175                                                                 MEDIA_UPDATE_ENTRY(array("total_send", "normal_send"), "add", 1);
176                                                         } // END - if
177
178                                                         // And count up the mail
179                                                         $GLOBALS['pool_cnt']++;
180                                                         //* DEBUG: */ echo"*EXIT/L:".__LINE__."/".$GLOBALS['pool_cnt']."*<br />";
181                                                         break;
182
183                                                 case "already":
184                                                         // Entry already found, but we still count one up!
185                                                         $GLOBALS['pool_cnt']++;
186                                                         //* DEBUG: */ echo"*EXIT/L:".__LINE__."/".$GLOBALS['pool_cnt']."<br />";
187                                                         break;
188                                                 }
189                                         }
190
191                                         // Do we have reached the maximum to send mails? || (getConfig('max_send') >= $GLOBALS['pool_cnt'])
192                                         //* DEBUG: */ echo "*L:".__LINE__."/".$GLOBALS['pool_cnt'].">=".$DATA[8]."/".getConfig('max_send').">=".$GLOBALS['pool_cnt']."/".$LAST_SENT_ID."!=".$DATA[0]."*<br />";
193                                         if ((($GLOBALS['pool_cnt'] >= $DATA[8])) && ($LAST_SENT_ID != $DATA[0])) {
194                                                 // Prepare content
195                                                 $content = array(
196                                                         'sender_uid' => $DATA[1],
197                                                         'category'   => GET_CATEGORY($DATA[9]),
198                                                         'text'       => $DATA[3],
199                                                         'url'        => $DATA[7],
200                                                         'expiration' => CREATE_FANCY_TIME(GET_PAY_POINTS($DATA[5], "time"))
201                                                 );
202
203                                                 // Yes we do, so we notify admin and sender about fully sent mail!
204                                                 SEND_ADMIN_NOTIFICATION(ADMIN_SUBJ_SEND_DONE, "done-admin", $content, $uid);
205
206                                                 // Get sender's data
207                                                 $result_sender = SQL_QUERY_ESC("SELECT surname, family, email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
208                                                         array(bigintval($DATA[1])), __FILE__, __LINE__);
209
210                                                 // Is the sender found?
211                                                 if (SQL_NUMROWS($result_sender) == 1) {
212                                                         // Load data and prepare mail
213                                                         list($sname, $fname, $email) = SQL_FETCHROW($result_sender);
214
215                                                         // Load email template
216                                                         $mailText = LOAD_EMAIL_TEMPLATE("done-member", $content, $DATA[1]);
217
218                                                         // Send it also waway
219                                                         SEND_EMAIL($email, MEMBER_SUBJ_SEND_DONE, $mailText);
220                                                 } // END - if
221
222                                                 // Free result
223                                                 SQL_FREERESULT($result_sender);
224
225                                                 // Set status to SEND because we completely send it away
226                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET data_type='SEND', target_send='0', receivers='' WHERE id=%s LIMIT 1",
227                                                         array(bigintval($DATA[0])), __FILE__, __LINE__);
228
229                                                 // Update send-completed-time
230                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_stats` SET timestamp_send=UNIX_TIMESTAMP() WHERE pool_id=%s LIMIT 1",
231                                                         array(bigintval($DATA[0])), __FILE__, __LINE__);
232
233                                                 $LAST_SENT_ID = $DATA[0]; $GLOBALS['pool_cnt'] = 0;
234                                                 $cnt2 += $GLOBALS['pool_cnt'];
235
236                                                 // Update mediadata if version is 0.0.4 or higher
237                                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4") {
238                                                         // Update entry (or add missing)
239                                                         //* DEBUG: */ echo "*MEDIA/L:".__LINE__."*<br />";
240                                                         MEDIA_UPDATE_ENTRY(array("total_orders", "normal_orders"), "add", 1);
241                                                 } // END - if
242
243                                                 //* DEBUG: */ echo"*EXIT/L:".__LINE__."/".$P."<br />";
244                                                 break;
245                                         }
246                                         // Do we have send maximum mails?
247                                          elseif (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || ($cnt2 >= getConfig('max_send'))) {
248                                                 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
249                                                 $ADD = "";
250                                                 if ($GLOBALS['pool_cnt'] <= $DATA[8]) $ADD = ", target_send=target_send-".$GLOBALS['pool_cnt'];
251                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET data_type='NEW', receivers='%s'".$ADD." WHERE id=%s LIMIT 1",
252                                                  array(implode(";", $dummy), bigintval($DATA[0])), __FILE__, __LINE__);
253
254                                                 //* DEBUG: */ echo"*EXIT/L:".__LINE__."*<br />";
255                                                 break;
256                                         }
257
258                                         // Free result
259                                         SQL_FREERESULT($result_stats);
260                                 } else {
261                                         // User does not exists so we have add the sender's points back to sender's account
262                                         if (($RECEIVERS[0] == "0") || (empty($RECEIVERS[0]))) {
263                                                 // List was empty
264                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET data_type='SEND' WHERE id=%s LIMIT 1",
265                                                  array(bigintval($DATA[0])), __FILE__, __LINE__);
266                                         } else {
267                                                 // Is the userid set?
268                                                 if ($uid > 0) {
269                                                         // User does not exists, pay points back
270                                                         $points = GET_PAY_POINTS($DATA[5]);
271                                                         ADD_POINTS_REFSYSTEM("pool_payback", $DATA[1], $points, false, "0", false, "direct");
272
273                                                         // Add points together and remove user
274                                                         $pointsBack[$DATA[1]] += $points;
275                                                 } // END - if
276
277                                                 // Count up
278                                                 $cnt_back[$DATA[1]]++;
279                                         }
280
281                                         // Remove entry from list
282                                         unset($dummy[$key]);
283
284                                         // Update receivers
285                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET receivers='%s' WHERE id=%s LIMIT 1",
286                                                 array(implode(";", $dummy), bigintval($DATA[0])), __FILE__, __LINE__);
287                                 }
288                         }
289                 }
290         }
291
292         // Do we have points to "pay back"?
293         if ((sizeof($pointsBack) > 0) && (!empty($pointsBack[0]))) {
294                 // Walk through all points
295                 foreach ($pointsBack as $uid => $PB) {
296                         // Add points only when we have points left to add and a valid user ID
297                         if (($PB > 0) && ($uid > 0)) {
298                                 // Prepare content
299                                 $content = array(
300                                         'points' => TRANSLATE_COMMA($DATA[10])
301                                 );
302
303                                 // We have to pay back some points to the sender (we add them directly :-P)
304                                 $result = SQL_QUERY_ESC("SELECT email FROM `{!_MYSQL_PREFIX!}_user_data` WHERE userid=%s AND status='CONFIRMED' LIMIT 1",
305                                         array(bigintval($uid)), __FILE__, __LINE__);
306                                 $DATA[10] = $PB; $DATA[11] = $cnt_back[$uid];
307
308                                 // User found?
309                                 if (SQL_NUMROWS($result) == 1) {
310                                         list($email) = SQL_FETCHROW($result);
311                                         SQL_FREERESULT($result);
312
313                                         // User account does exists, so we can safely pay back!
314                                         $mailText = LOAD_EMAIL_TEMPLATE("back-member", $content, bigintval($uid));
315
316                                         // Send mail out to member
317                                         SEND_EMAIL($email, MEMBER_BACK_JACKPOT." (".$uid.")", $mailText);
318                                 } else {
319                                         // Add to jackpot
320                                         ADD_JACKPOT($PB);
321
322                                         // Send mail out to admin
323                                         SEND_ADMIN_NOTIFICATION(ADMIN_BACK_JACKPOT." (".$uid.")", "back-admin", $content, "admin");
324                                 }
325                         } // END - if
326                 } // END - foreach
327         } // END - if
328 }
329
330 // Free memory
331 SQL_FREERESULT($result_main);
332
333 // Remove variable
334 unset($mailText);
335
336 //
337 ?>