- Beg link does now (not yet implemented but prepared) log all referal URLs. It
[mailer.git] / inc / libs / bonus_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/10/2004 *
4  * ===================                          Last change: 03/18/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : bonus_functions.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Special functions for bonus extension            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Spezielle Funktion fuer bonus-Erweiterung        *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
44 function addTurboBonus ($mid, $userid, $type) {
45         // Shall we add bonus points?
46         if (!isBonusRallyeActive()) return false;
47
48         // Init variables
49         $sql = ''; $bonus = '0'; $mail = '0'; $column = '';
50
51         // Select SQL command
52         switch ($type) {
53                 case 'bonusid':
54                         $column = 'bonus_id';
55                         $bonus = $mid;
56                         break;
57
58                 case 'mailid' :
59                         $column = 'mail_id';
60                         $mail = $mid;
61                         break;
62
63                 default:
64                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
65                         break;
66         } // END - switch
67
68         // Is a column name set?
69         if (empty($column)) {
70                 // No, then abort here
71                 return false;
72         } // END - if
73
74         // Check for entry
75         $rank = countSumTotalData($mid, 'bonus_turbo', 'id', $column, true) + 1;
76
77         // Which rank?
78         if ($rank == 1) {
79                 // First rank!
80                 $points = getConfig('turbo_bonus');
81         } else {
82                 // Anything else so let's explode all entered rank points
83                 $test = explode(';', getConfig('turbo_rates'));
84                 if (!empty($test[$rank - 2])) {
85                         // Level found
86                         $points = $test[$rank - 2];
87                 } else {
88                         // Level not found!
89                         $points = '0.00000';
90                 }
91         }
92
93         // Add points to his account directly
94         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `turbo_bonus`=`turbo_bonus`+%s WHERE `userid`=%s LIMIT 1",
95                 array(
96                         bigintval($userid),
97                         $points
98                 ), __FUNCTION__, __LINE__);
99
100         // Rember this whole data for displaying ranking list
101         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_bonus_turbo` (`userid`, `mail_id`, `bonus_id`, `level`, `points`, `timemark`) VALUES (%s, %s, %s, %s, %s, UNIX_TIMESTAMP())",
102                 array(
103                         bigintval($userid),
104                         bigintval($mail),
105                         bigintval($bonus),
106                         $rank,
107                         $points
108                 ), __FUNCTION__, __LINE__);
109
110         if ((isExtensionInstalledAndNewer('bonus', '0.3.5')) && (getBonusMode() != 'ADD') && ($points > 0)) handleBonusPoints($points);
111 }
112
113 //
114 function addBonusRanks ($data, $type, $userid) {
115         // Init variables
116         $self = false; $OUT = ''; $GLOBALS['ranking_content'] = array();
117
118         // Clear rankings by default
119         $GLOBALS['ranking_content']['rankings'] = '';
120
121         // How many ranks do we have?
122         $ranks = count(explode(';', getConfig('turbo_rates'))) + 1;
123
124         // Load current user's data
125         $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
126                 array(
127                         $type,
128                         $data,
129                         bigintval($userid)
130                 ), __FUNCTION__, __LINE__);
131
132         // Entry found?
133         if (SQL_NUMROWS($result) == 1) {
134                 // Load data
135                 $GLOBALS['ranking_content'] = merge_array($GLOBALS['ranking_content'], SQL_FETCHARRAY($result));
136
137                 // Remember all values for later use
138                 $self  = true;
139
140                 // Transfer data to template
141                 $GLOBALS['ranking_content']['timemark'] = generateDateTime($GLOBALS['ranking_content']['timemark'], 1);
142
143                 // Load template
144                 $GLOBALS['ranking_content']['own'] = loadTemplate('show_bonus_yr', true, $GLOBALS['ranking_content']);
145         } // END - if
146
147         // Load rankings
148         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s ORDER BY `level` ASC LIMIT {?bonus_lines?}",
149                 array($type, $data), __FUNCTION__, __LINE__);
150         if (!SQL_HASZERONUMS($result)) {
151                 // Output all ranks (levels)
152                 for ($rank = 1; $rank <= SQL_NUMROWS($result); $rank++) {
153                         // Load data
154                         $result_users = SQL_QUERY_ESC("SELECT
155         `userid`, `points`
156 FROM
157         `{?_MYSQL_PREFIX?}_bonus_turbo`
158 WHERE
159         `%s`=%s AND
160         `level`=%s
161 LIMIT 1",
162                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
163
164                         // Nothing found by default
165                         $rows['userid'] = '---';
166                         $rows['points'] = '---';
167
168                         // Are you one of them?
169                         if (SQL_NUMROWS($result_users) == 1) {
170                                 // Load data
171                                 $rows = merge_array($rows, SQL_FETCHARRAY($result_users));
172
173                                 // Is ext-nickname active?
174                                 if (isExtensionActive('nickname')) {
175                                         // Then get the nickname
176                                         $nick = getNickname($rows['userid']);
177
178                                         // Is it not empty? Then use it
179                                         if (!empty($nick)) $rows['userid'] = $nick;
180                                 } // END - if
181                         } // END - if
182
183                         // Free result
184                         SQL_FREERESULT($result_users);
185
186                         // Add more
187                         $rows['rank'] = $rank;
188
189                         // Load row template
190                         $OUT .= loadTemplate('member_bonus_turbo_row', true, $rows);
191                 } // END - for
192
193                 if ($self === false) {
194                         // If current user was not found set constant
195                         $GLOBALS['ranking_content']['rankings'] = '{--MEMBER_BONUS_RANK_YOU_ARE_404--}';
196                 } // END - if
197         } else {
198                 // No entries found!
199                 // @TODO Move this HTML to a template
200                 $OUT = '<tr>
201   <td colspan="3" align="center" height="30" class="bottom">
202     <div class="notice">' . getMaskedMessage('MEMBER_BONUS_NO_RANKS', $data) . '</div>
203   </td>
204 </tr>';
205         }
206
207         // Retutn content
208         return $OUT;
209 }
210
211 //
212 function handleBonusPoints ($mode) {
213         // Shall we add bonus points?
214         if (!isBonusRallyeActive()) return;
215
216         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
217         if ((getBonusMode() == 'UID') && (getBonusUserId() == '0') && (isExtensionActive('jackpot'))) {
218                 // Update database & config
219                 updateConfiguration('bonus_mode', 'JACKPOT');
220         } // END - if
221
222         if ($mode == 'login_bonus') {
223                 // Login bonus detected
224                 $points = getConfig('login_bonus');
225         } else {
226                 // Direct points supplied
227                 $points = $mode;
228         }
229
230         // Check his amount first
231         $total = getTotalPoints(getBonusUserId());
232
233         // Subtract points from...
234         switch (getBonusMode()) {
235                 case 'JACKPOT': // ... jackpot
236                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) == -1) && (isValidUserId(getBonusUserId()))) {
237                                 if ($total >= $points) {
238                                         // Subtract points from userid's account
239                                         subtractPointsFromJackpot('bonus_payout_jackpot', getBonusUserId(), $points);
240                                 } // END - if
241                         } // END - if
242                         break;
243
244                 case 'UID': // ... userid's account
245                         if ($total >= $points) {
246                                 // Subtract points from userid's account
247                                 subtractPoints('bonus_payout_userid', getBonusUserId(), $points);
248                         } elseif (isExtensionActive('jackpot')) {
249                                 // Try to subtract from jackpot
250                                 $dummy = subtractPointsFromJackpot($points);
251                         }
252                         break;
253
254                 default: // This should not happen
255                         debug_report_bug(__FUNCTION__, __LINE__, 'Invalid config entry ' . getBonusMode() . ' detected.');
256                         break;
257         } // END - switch
258 }
259
260 // Purges expired fast-click bonus entries
261 function purgeExpiredTurboBonus() {
262         // Remove entries
263         $result = SQL_QUERY('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `timemark` < (UNIX_TIMESTAMP() - {?bonus_timeout?})', __FUNCTION__, __LINE__);
264
265         if (!SQL_HASZEROAFFECTED()) {
266                 // Send out email to admin
267                 sendAdminNotification('{--ADMIN_AUTOPURGE_TURBO_SUBJECT--}', 'admin_autopurge_turbo', SQL_AFFECTEDROWS());
268         } // END - if
269 }
270
271 //-----------------------------------------------------------------------------
272 //                                Filter Functions
273 //-----------------------------------------------------------------------------
274
275 // Filter for adding login bonus to the user's account
276 function FILTER_ADD_LOGIN_BONUS () {
277         // Is the user data valid?
278         if (!isMember()) {
279                 // Do only run for logged in members
280                 debug_report_bug(__FUNCTION__, __LINE__, 'Please only run this filter for logged in users.');
281         } // END - if
282
283         // Bonus is not given by default ;-)
284         $bonus = false;
285         if ((isExtensionInstalledAndNewer('sql_patches', '0.2.8')) && (isBonusRallyeActive()) && (getConfig('bonus_login_yn') == 'Y')) {
286                 // Update last login if far enougth away
287                 // @TODO This query isn't right, it will only update if the user was for a longer time away!
288                 SQL_QUERY_ESC('UPDATE
289         `{?_MYSQL_PREFIX?}_user_data`
290 SET
291         `last_login`=UNIX_TIMESTAMP()
292 WHERE
293         `userid`=%s AND
294         `last_login` < (UNIX_TIMESTAMP() - {?login_timeout?})
295 LIMIT 1',
296                         array(
297                                 getMemberId()
298                         ), __FUNCTION__, __LINE__
299                 );
300
301                 // Updated entry?
302                 $bonus = (!SQL_HASZEROAFFECTED());
303         } // END - if
304
305         if (($bonus === true) && (getRequestParameter('mode') == 'bonus')) {
306                 // Output message with added points
307                 $GLOBALS['message'] .= '<div class="tiny">{--MEMBER_BONUS_LOGIN_BONUS_ADDED--}</div>';
308         } elseif (isExtensionActive('bonus')) {
309                 // No login bonus added!
310                 $GLOBALS['message'] .= '<div class="notice">{--MEMBER_BONUS_LOGIN_BONUS_NOT_ADDED--}</div>';
311         }
312 }
313
314 //-----------------------------------------------------------------------------
315 //                             Wrapper Functions
316 //-----------------------------------------------------------------------------
317
318 // Determines wether the "bonus rallye" is active
319 function isBonusRallyeActive () {
320         // Do we have cache?
321         if (!isset($GLOBALS['bonus_rallye_active'])) {
322                 // Just determine it
323                 $GLOBALS['bonus_rallye_active'] = (getConfig('bonus_active') == 'Y');
324         } // END - if
325
326         // Return cache
327         return $GLOBALS['bonus_rallye_active'];
328 }
329
330 // Determines wether the "bonus new_member_notify" is active
331 function isBonusNewMemberNotifyEnabled () {
332         // Do we have cache?
333         if (!isset($GLOBALS['bonus_new_member_notify_active'])) {
334                 // Just determine it
335                 $GLOBALS['bonus_new_member_notify_active'] = (getConfig('bonus_new_member_notify') == 'Y');
336         } // END - if
337
338         // Return cache
339         return $GLOBALS['bonus_new_member_notify_active'];
340 }
341
342 // Getter for bonus_timeout
343 function getBonusTimeout () {
344         // Do we have cache?
345         if (!isset($GLOBALS[__FUNCTION__])) {
346                 // Determine it
347                 $GLOBALS[__FUNCTION__] = getConfig('bonus_timeout');
348         } // END - if
349
350         // Return cache
351         return $GLOBALS[__FUNCTION__];
352 }
353
354 // Getter for bonus_mode
355 function getBonusMode () {
356         // Do we have cache?
357         if (!isset($GLOBALS[__FUNCTION__])) {
358                 // Determine it
359                 $GLOBALS[__FUNCTION__] = getConfig('bonus_mode');
360         } // END - if
361
362         // Return cache
363         return $GLOBALS[__FUNCTION__];
364 }
365
366 // [EOF]
367 ?>