2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 12/12/2008 *
4 * =================== Last change: 12/12/2008 *
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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2013 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Don't run on daily reset
44 if ((isResetModeEnabled()) || (!isHtmlOutputMode())) {
47 } elseif ((!isExtensionActive('user')) || (!isExtensionActive('other'))) {
48 // Abort here if extension user/other are not active
55 // Check for freed mail orders to send out
56 if (isExtensionActive('html_mail')) {
58 $HTML = ',`html_msg`';
62 $result_main = sqlQuery("SELECT
64 `sender` AS `sender_userid`,
75 `{?_MYSQL_PREFIX?}_pool`
79 `timestamp` DESC", __FILE__, __LINE__);
84 $count_back = array(0);
85 $pointsBack = array(0);
87 if (!ifSqlHasZeroNums($result_main)) {
89 while ($mailData = sqlFetchArray($result_main)) {
90 // Set mail order as 'active'. That means it will be sent out
91 updatePoolDataById($mailData['id'], 'data_type', 'ACTIVE', NULL, " AND `data_type`='NEW'");
93 // Check fetched data for HTML
95 if (isset($mailData['html_msg'])) {
96 $isHtml = $mailData['html_msg'];
100 if (!ifSqlHasZeroAffectedRows()) {
101 // "Explode" all receivers into an array
102 if (isInString(';', $mailData['receivers'])) {
103 // There's more than one receiver in the list...
104 $receivers = explode(';', $mailData['receivers']);
105 } elseif (!empty($mailData['receivers'])) {
106 // Only one user left
107 $receivers = array($mailData['receivers']);
110 $receivers = array(0);
112 $temporaryReceivers = $receivers;
114 // Now, if we are good little boys and girls Santa Claus left us some user-ids.
115 // We can now send mails to them...
116 foreach ($receivers as $key => $userid) {
118 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.sqlNumRows($result_user).'*');
119 if (fetchUserData($userid)) {
120 // Is there a stats entry?
121 $result_stats = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s AND `userid`=%s AND timestamp_ordered='%s' LIMIT 1",
122 array($mailData['id'], $mailData['sender_userid'], $mailData['timestamp']), __FILE__, __LINE__);
124 // If there's no stats entry add it!
125 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.sqlNumRows($result_stats).'!');
126 if (ifSqlHasZeroNums($result_stats)) {
127 // No entry was found, so we add him!
128 sqlQueryEscaped("INSERT INTO
129 `{?_MYSQL_PREFIX?}_user_stats`
152 bigintval($mailData['id']),
153 bigintval($mailData['sender_userid']),
154 bigintval($mailData['cat_id']),
155 bigintval($mailData['payment_id']),
156 $mailData['subject'],
158 bigintval($mailData['target_send']),
159 bigintval($mailData['timestamp'])
160 ), __FILE__, __LINE__);
162 // Receive it's id for the links table
163 $result_stats = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `pool_id`=%s AND `userid`=%s AND timestamp_ordered='%s' LIMIT 1",
164 array(bigintval($mailData['id']), bigintval($mailData['sender_userid']), bigintval($mailData['timestamp'])), __FILE__, __LINE__);
167 //* DEBUG: */ debugOutput('!L:'.__LINE__.'/'.sqlNumRows($result_stats).'!');
168 if (sqlNumRows($result_stats) == 1) {
170 list($stats_id) = sqlFetchRow($result_stats);
172 // Remove receiver from list
173 $status = removeReceiver($temporaryReceivers, $key, bigintval($userid), bigintval($mailData['id']), bigintval($stats_id));
175 //* DEBUG: */ debugOutput('?L:'.__LINE__.'/'.$temporaryReceivers.'/'.$key.'/'.$userid.'('.['sender_userid'].')/'.$mailData['id'].'/'.$stats_id.'?');
179 $mailData['stats_id'] = bigintval($stats_id);
181 // Load message template
182 $mailText = loadEmailTemplate('member_user_pool_normal', $mailData, bigintval($userid));
185 sendEmail(getUserData('userid'), $mailData['subject'], $mailText, $isHtml);
187 // Count sent mails...
188 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_sent`=`emails_sent`+1 WHERE `userid`=%s LIMIT 1",
189 array(bigintval($mailData['sender_userid'])), __FILE__, __LINE__);
191 if (isExtensionInstalledAndNewer('user', '0.1.4')) {
192 // Update mails received for receiver
193 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `emails_received`=`emails_received`+1 WHERE `userid`=%s LIMIT 1",
194 array(bigintval($userid)), __FILE__, __LINE__);
197 // Update mediadata if version is 0.0.4 or higher
198 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
199 // Update entry (or add missing)
200 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
201 updateMediadataEntry(array('total_send', 'normal_send'), 'add', 1);
204 // And count up the mail
205 $GLOBALS['pool_cnt']++;
206 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'*');
210 // Entry already found, but we still count one up!
211 $GLOBALS['pool_cnt']++;
212 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$GLOBALS['pool_cnt']);
215 default: // Unknown return type
216 logDebugMessage(__FILE__, __LINE__, 'Unknown status ' . $status . ' detected. pool_id=' . $mailData['id'] . ',sender=' . $mailData['sender_userid'] . ',stats_id=' . $stats_id);
221 // Is there reached the maximum to send mails? || (getMaxSend() >= $GLOBALS['pool_cnt'])
222 //* DEBUG: */ debugOutput('*L:'.__LINE__.'/'.$GLOBALS['pool_cnt'].'>='.$mailData['target_send'].'/'.getMaxSend().'>='.$GLOBALS['pool_cnt'].'/'.$lastSentId.'!='.$mailData['id'].'*');
223 if ((($GLOBALS['pool_cnt'] >= $mailData['target_send'])) && ($lastSentId != $mailData['id'])) {
226 'sender_userid' => $mailData['sender_userid'],
227 'cat_id' => $mailData['cat_id'],
228 'text' => $mailData['text'],
229 'url' => $mailData['url'],
230 'expiration' => '{%pipe,getPaymentTime,createFancyTime=' . $mailData['payment_id'] . '%}'
233 // Yes we do, so we notify admin and sender about fully sent mail!
234 sendAdminNotification('{--ADMIN_SEND_DONE_SUBJECT--}', 'admin_user_pool_done', $content, $userid);
237 if (fetchUserData($mailData['sender_userid'])) {
238 // Load email template
239 $mailText = loadEmailTemplate('member_user_pool_done', $content, $mailData['sender_userid']);
241 // Send it also waway
242 sendEmail(getUserData('userid'), '{--MEMBER_SEND_DONE_SUBJECT--}', $mailText);
245 // Set status to SEND because we completely send it away
246 updatePoolDataById($mailData['id'], NULL, array('data_type' => 'SEND', 'target_send' => '0', 'receivers' => ''));
248 // Update send-completed-time
249 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_stats` SET `timestamp_send`=UNIX_TIMESTAMP() WHERE `pool_id`=%s LIMIT 1",
250 array(bigintval($mailData['id'])), __FILE__, __LINE__);
252 $lastSentId = $mailData['id']; $GLOBALS['pool_cnt'] = '0';
253 $count2 += $GLOBALS['pool_cnt'];
255 // Update mediadata if version is 0.0.4 or higher
256 if (isExtensionInstalledAndNewer('mediadata', '0.0.4')) {
257 // Update entry (or add missing)
258 //* DEBUG: */ debugOutput('*MEDIA/L:'.__LINE__.'*');
259 updateMediadataEntry(array('total_orders', 'normal_orders'), 'add', 1);
262 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'/'.$P);
265 // Is there send maximum mails?
266 elseif (($GLOBALS['pool_cnt'] >= getMaxSend()) || ($count2 >= getMaxSend())) {
267 // There are some mails left to send for next round, so we reset the status back to NEW (=still not fully delivered)
269 if ($GLOBALS['pool_cnt'] <= $mailData['target_send']) $add = ',`target_send`=`target_send`-' . $GLOBALS['pool_cnt'];
270 updatePoolDataById($mailData['id'], NULL, array('data_type' => 'NEW', 'receivers' => implode(';', $temporaryReceivers)), NULL, '', $add);
272 //* DEBUG: */ debugOutput('*EXIT/L:'.__LINE__.'*');
277 sqlFreeResult($result_stats);
279 // User does not exists so we have add the sender's points back to sender's account
280 if (($receivers['id'] == '0') || (empty($receivers['id']))) {
282 updatePoolDataById($mailData['id'], 'data_type', 'SEND');
284 // Is the userid set?
285 if (isValidId($userid)) {
286 // User does not exists, pay points back
287 initReferralSystem();
288 addPointsThroughReferralSystem('pool_payback', $mailData['sender_userid'], getPaymentPrice($mailData['payment_id']));
290 // Add points together and remove user
291 $pointsBack[$mailData['sender_userid']] += getPaymentPrice($mailData['payment_id']);
295 $count_back[$mailData['sender_userid']]++;
298 // Remove entry from list
299 unset($temporaryReceivers[$key]);
302 updatePoolData($mailData['id'], 'receivers', implode(';', $temporaryReceivers));
308 // Is there points to "pay back"?
309 if ((isFilledArray($pointsBack)) && (!empty($pointsBack[0]))) {
310 // Walk through all points
311 foreach ($pointsBack as $userid => $PB) {
312 // Add points only when we have points left to add and a valid user id
313 if (($PB > 0) && (isValidId($userid))) {
319 // We have to pay back some points to the sender (we add them directly :-P)
320 if (fetchUserData($userid)) {
321 // User account does exists, so we can safely pay back!
322 $mailText = loadEmailTemplate('member_user_pool_back', $content, bigintval($userid));
324 // Send mail out to member
325 sendEmail($userid, '{--MEMBER_BACK_JACKPOT--}' . ' (' . $userid . ')', $mailText);
326 } elseif (isExtensionActive('jackpot')) {
328 addPointsToJackpot($PB);
330 // Send mail out to admin
331 sendAdminNotification('{--ADMIN_BACK_JACKPOT--}' . ' (' . $userid . ')', 'admin_user_pool_back', $content, 'admin');
339 sqlFreeResult($result_main);