]> git.mxchange.org Git - mailer.git/blob - inc/pool/pool-user.php
Hack for old/new mixtures in email addresses ('{DOT}' vs. '.')
[mailer.git] / inc / pool / pool-user.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-user.php                                    *
8  * -------------------------------------------------------------------- *
9  * Short description : Sends queued user mails from the pool            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sendet freigegebene Mitglieder-Mails aus 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  * 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         die();
42 }
43
44 // Don't run on daily reset
45 if (isResetModeEnabled()) {
46         // Skip here
47         return false;
48 } elseif (!isExtensionActive('user')) {
49         // Abort here if extension user is not active
50         return false;
51 }
52
53 // Need this here
54 // Check for freed mail orders to send out
55 if (isExtensionActive('html_mail')) {
56         //                                0     1        2      3       4          5            6      7        8          9       10
57         $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__);
58 } else {
59         //                                0     1        2      3       4          5            6      7        8          9    10
60         $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__);
61 }
62
63 // Reset variables
64 $cnt2 = '0'; $lastSentId = '0'; $cnt_back = array(0); $pointsBack = array(0);
65 if (SQL_NUMROWS($result_main) > 0) {
66         // Parse all mails
67         while ($DATA = SQL_FETCHARRAY($result_main, 0, false)) {
68                 // Set mail order as 'active'. That means it will be sent out
69                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='ACTIVE' WHERE `id`=%s AND `data_type`='NEW' LIMIT 1",
70                         array($DATA['id']), __FILE__, __LINE__);
71
72                 // Check fetched data for HTML
73                 $HTML = 'N';
74                 if (isset($DATA['html_msg'])) $HTML = $DATA['html_msg'];
75
76                 // Entry updated?
77                 if (SQL_AFFECTEDROWS() == 1) {
78                         // "Explode" all receivers into an array
79                         if (ereg(';', $DATA['receivers'])) {
80                                 // There's more than one receiver in the list...
81                                 $RECEIVERS = explode(';', $DATA['receivers']);
82                         } elseif (!empty($DATA['receivers'])) {
83                                 // Only one user left
84                                 $RECEIVERS = array($DATA['receivers']);
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 Claus left us some user-ids.
92                         // We can now send mails to them...
93                         foreach ($RECEIVERS as $key => $userid) {
94                                 // Lookup user id
95                                 //* DEBUG: */ outputHtml("*L:".__LINE__.'/'.SQL_NUMROWS($result_user)."*<br />");
96                                 if (fetchUserData($userid)) {
97                                         // Do we have a stats entry?
98                                         $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",
99                                                 array($DATA['id'], $DATA['sender'], $DATA['timestamp']), __FILE__, __LINE__);
100
101                                         // If there's no stats entry add it!
102                                         //* DEBUG: */ outputHtml("!L:".__LINE__.'/'.SQL_NUMROWS($result_stats)."!<br />");
103                                         if (SQL_NUMROWS($result_stats) == '0') {
104                                                 // No entry was found, so we add him!
105                                                 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())",
106                                                         array(bigintval($DATA['id']), bigintval($DATA['sender']), bigintval($DATA['cat_id']), bigintval($DATA['payment_id']), $DATA['subject'], $DATA['url'], $DATA['target_send'], bigintval($DATA['timestamp'])), __FILE__, __LINE__);
107
108                                                 // Receive it's id for the links table
109                                                 $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",
110                                                         array(bigintval($DATA['id']), bigintval($DATA['sender']), bigintval($DATA['timestamp'])), __FILE__, __LINE__);
111                                         } // END - if
112
113                                         //* DEBUG: */ outputHtml("!L:".__LINE__.'/'.SQL_NUMROWS($result_stats)."!<br />");
114                                         if (SQL_NUMROWS($result_stats) == 1) {
115                                                 // We got one!
116                                                 list($stats_id) = SQL_FETCHROW($result_stats);
117
118                                                 // Mark this user as "spammed" ;-) And place a line for him...
119                                                 //* DEBUG: */ outputHtml("?L:".__LINE__.'/'.$dummy.'/'.$key.'/'.$userid.'('.['sender'].")/".$DATA['id'].'/'.$stats_id."?<br />");
120                                                 switch (removeReceiver($dummy, $key, bigintval($userid), bigintval($DATA['id']), bigintval($stats_id))) {
121                                                         case 'done':
122                                                                 // Prepare the mail
123                                                                 $DATA['stats_id'] = bigintval($stats_id);
124                                                                 $DATA['surname']  = getUserData('surname');
125                                                                 $DATA['family']   = getUserData('family');
126                                                                 $DATA['gender']   = translateGender(getUserData('gender'));
127
128                                                                 // Replace text variables
129                                                                 foreach ($GLOBALS['replacer'] as $key => $value) {
130                                                                         if (isset($DATA[$key])) $DATA['text'] = str_replace($value, $DATA[$key], $DATA['text']);
131                                                                 } // END - if
132
133                                                                 // Prepare content
134                                                                 $content = array(
135                                                                         'id'            => $DATA['stats_id'],
136                                                                         'url'           => $DATA['url'],
137                                                                         'sender_userid' => $DATA['sender'],
138                                                                         'category'      => getCategory($DATA['cat_id']),
139                                                                         'time'          => createFancyTime(getPaymentPoints($DATA['payment_id'], 'time')),
140                                                                         'points'        => translateComma(getPaymentPoints($DATA['payment_id'], 'payment')),
141                                                                         'text'          => $DATA['text']
142                                                                 );
143
144                                                                 // Load message template
145                                                                 $mailText = loadEmailTemplate('normal-mail', $content, bigintval($userid));
146
147                                                                 // Send mail away
148                                                                 sendEmail(getUserData('email'), $DATA['subject'], $mailText, $HTML);
149
150                                                                 // Count sent mails...
151                                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_sent=emails_sent+1 WHERE `userid`=%s LIMIT 1",
152                                                                         array(bigintval($DATA['sender'])), __FILE__, __LINE__);
153
154                                                                 if (getExtensionVersion('user') >= '0.1.4') {
155                                                                         // Update mails received for receiver
156                                                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET emails_received=emails_received+1 WHERE `userid`=%s LIMIT 1",
157                                                                                 array(bigintval($userid)), __FILE__, __LINE__);
158                                                                 } // END - if
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: */ outputHtml("*MEDIA/L:".__LINE__."*<br />");
164                                                                         updateMediadataEntry(array('total_send', 'normal_send'), 'add', 1);
165                                                                 } // END - if
166
167                                                                 // And count up the mail
168                                                                 $GLOBALS['pool_cnt']++;
169                                                                 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__.'/'.$GLOBALS['pool_cnt']."*<br />");
170                                                                 break;
171
172                                                         case 'already':
173                                                                 // Entry already found, but we still count one up!
174                                                                 $GLOBALS['pool_cnt']++;
175                                                                 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__.'/'.$GLOBALS['pool_cnt']."<br />");
176                                                                 break;
177                                                 }
178                                         }
179
180                                         // Do we have reached the maximum to send mails? || (getConfig('max_send') >= $GLOBALS['pool_cnt'])
181                                         //* DEBUG: */ outputHtml("*L:".__LINE__.'/'.$GLOBALS['pool_cnt'].">=".$DATA['target_send'].'/'.getConfig('max_send').">=".$GLOBALS['pool_cnt'].'/'.$lastSentId."!=".$DATA['id']."*<br />");
182                                         if ((($GLOBALS['pool_cnt'] >= $DATA['target_send'])) && ($lastSentId != $DATA['id'])) {
183                                                 // Prepare content
184                                                 $content = array(
185                                                         'sender_userid' => $DATA['sender'],
186                                                         'category'      => getCategory($DATA['cat_id']),
187                                                         'text'          => $DATA['text'],
188                                                         'url'           => $DATA['url'],
189                                                         'expiration'    => createFancyTime(getPaymentPoints($DATA['payment_id'], "time"))
190                                                 );
191
192                                                 // Yes we do, so we notify admin and sender about fully sent mail!
193                                                 sendAdminNotification(getMessage('ADMIN_SUBJ_SEND_DONE'), 'done-admin', $content, $userid);
194
195                                                 // Get sender's data
196                                                 if (fetchUserData($DATA['sender'])) {
197                                                         // Load email template
198                                                         $mailText = loadEmailTemplate('done-member', $content, $DATA['sender']);
199
200                                                         // Send it also waway
201                                                         sendEmail(getUserData('email'), getMessage('MEMBER_SUBJ_SEND_DONE'), $mailText);
202                                                 } // END - if
203
204                                                 // Set status to SEND because we completely send it away
205                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND', `target_send`=0, `receivers`='' WHERE `id`=%s LIMIT 1",
206                                                         array(bigintval($DATA['id'])), __FILE__, __LINE__);
207
208                                                 // Update send-completed-time
209                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `timestamp_send`=UNIX_TIMESTAMP() WHERE `pool_id`=%s LIMIT 1",
210                                                         array(bigintval($DATA['id'])), __FILE__, __LINE__);
211
212                                                 $lastSentId = $DATA['id']; $GLOBALS['pool_cnt'] = '0';
213                                                 $cnt2 += $GLOBALS['pool_cnt'];
214
215                                                 // Update mediadata if version is 0.0.4 or higher
216                                                 if (getExtensionVersion('mediadata') >= '0.0.4') {
217                                                         // Update entry (or add missing)
218                                                         //* DEBUG: */ outputHtml("*MEDIA/L:".__LINE__."*<br />");
219                                                         updateMediadataEntry(array('total_orders', 'normal_orders'), 'add', 1);
220                                                 } // END - if
221
222                                                 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__.'/'.$P."<br />");
223                                                 break;
224                                         }
225                                         // Do we have send maximum mails?
226                                         elseif (($GLOBALS['pool_cnt'] >= getConfig('max_send')) || ($cnt2 >= getConfig('max_send'))) {
227                                                 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
228                                                 $add = '';
229                                                 if ($GLOBALS['pool_cnt'] <= $DATA['target_send']) $add = ", target_send=target_send-".$GLOBALS['pool_cnt'];
230                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='NEW', receivers='%s'".$add." WHERE `id`=%s LIMIT 1",
231                                                         array(implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
232
233                                                 //* DEBUG: */ outputHtml("*EXIT/L:".__LINE__."*<br />");
234                                                 break;
235                                         }
236
237                                         // Free result
238                                         SQL_FREERESULT($result_stats);
239                                 } else {
240                                         // User does not exists so we have add the sender's points back to sender's account
241                                         if (($RECEIVERS['id'] == '0') || (empty($RECEIVERS['id']))) {
242                                                 // List was empty
243                                                 SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='SEND' WHERE `id`=%s LIMIT 1",
244                                                         array(bigintval($DATA['id'])), __FILE__, __LINE__);
245                                         } else {
246                                                 // Is the userid set?
247                                                 if ($userid > 0) {
248                                                         // User does not exists, pay points back
249                                                         $points = getPaymentPoints($DATA['payment_id']);
250                                                         addPointsDirectly('pool_payback', $DATA['sender'], $points);
251
252                                                         // Add points together and remove user
253                                                         $pointsBack[$DATA['sender']] += $points;
254                                                 } // END - if
255
256                                                 // Count up
257                                                 $cnt_back[$DATA['sender']]++;
258                                         }
259
260                                         // Remove entry from list
261                                         unset($dummy[$key]);
262
263                                         // Update receivers
264                                         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `receivers`='%s' WHERE `id`=%s LIMIT 1",
265                                                 array(implode(';', $dummy), bigintval($DATA['id'])), __FILE__, __LINE__);
266                                 }
267                         }
268                 }
269         }
270
271         // Do we have points to "pay back"?
272         if ((count($pointsBack) > 0) && (!empty($pointsBack[0]))) {
273                 // Walk through all points
274                 foreach ($pointsBack as $userid => $PB) {
275                         // Add points only when we have points left to add and a valid user id
276                         if (($PB > 0) && ($userid > 0)) {
277                                 // Prepare content
278                                 $content = array(
279                                         'points' => translateComma($PB)
280                                 );
281
282                                 // We have to pay back some points to the sender (we add them directly :-P)
283                                 if (fetchUserData($userid)) {
284                                         // User account does exists, so we can safely pay back!
285                                         $mailText = loadEmailTemplate('back-member', $content, bigintval($userid));
286
287                                         // Send mail out to member
288                                         sendEmail(getUserData('email'), getMessage('MEMBER_BACK_JACKPOT') . ' (' . $userid . ')', $mailText);
289                                 } elseif (isExtensionActive('jackpot')) {
290                                         // Add to jackpot
291                                         addPointsToJackpot($PB);
292
293                                         // Send mail out to admin
294                                         sendAdminNotification(getMessage('ADMIN_BACK_JACKPOT') . ' (' . $userid . ')', 'back-admin', $content, 'admin');
295                                 }
296                         } // END - if
297                 } // END - foreach
298         } // END - if
299 }
300
301 // Free memory
302 SQL_FREERESULT($result_main);
303
304 // Remove variable
305 unset($mailText);
306
307 //
308 ?>