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