fooRequestElementBar() functions renamed, adding of request parameters added:
[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  * 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 } // END - if
43
44 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
45 function addTurboBonus ($mid, $userid, $type) {
46         // Shall we add bonus points?
47         if (getConfig('bonus_active') != 'Y') return false;
48
49         // Init variables
50         $sql = ''; $bonus = '0'; $mail = '0'; $column = '';
51
52         // Select SQL command
53         switch ($type) {
54                 case 'bonusid':
55                         $column = 'bonus_id';
56                         $bonus = $mid;
57                         break;
58
59                 case 'mailid' :
60                         $column = 'mail_id';
61                         $mail = $mid;
62                         break;
63
64                 default:
65                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
66                         break;
67         }
68
69         // Is a column name set?
70         if (empty($column)) {
71                 // No, then abort here
72                 return false;
73         } // END - if
74
75         // Check for entry
76         $rank = countSumTotalData($mid, 'bonus_turbo', 'id', $column, true) + 1;
77
78         // Which rank?
79         if ($rank == 1) {
80                 // First rank!
81                 $points = getConfig('turbo_bonus');
82         } else {
83                 // Anything else so let's explode all entered rank points
84                 $test = explode(';', getConfig('turbo_rates'));
85                 if (!empty($test[$rank - 2])) {
86                         // Level found
87                         $points = $test[$rank - 2];
88                 } else {
89                         // Level not found!
90                         $points = '0.00000';
91                 }
92         }
93
94         // Add points to his account directly
95         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `turbo_bonus`=`turbo_bonus`+%s WHERE `userid`=%s LIMIT 1",
96                 array(
97                         bigintval($userid),
98                         $points
99                 ), __FUNCTION__, __LINE__);
100
101         // Rember this whole data for displaying ranking list
102         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())",
103                 array(
104                         bigintval($userid),
105                         bigintval($mail),
106                         bigintval($bonus),
107                         $rank,
108                         $points
109                 ), __FUNCTION__, __LINE__);
110
111         if ((getExtensionVersion('bonus') >= '0.3.5') && (getConfig('bonus_mode') != "ADD") && ($points > 0)) handleBonusPoints($points);
112 }
113
114 //
115 function addBonusRanks ($data, $type, $userid) {
116         // Init variables
117         $self = false; $OUT = ''; $GLOBALS['ranking_content'] = array();
118
119         // Clear rankings by default
120         $GLOBALS['ranking_content']['rankings'] = '';
121
122         // How many ranks do we have?
123         $ranks = count(explode(';', getConfig('turbo_rates'))) + 1;
124
125         // Load current user's data
126         $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
127                 array(
128                         $type,
129                         $data,
130                         $userid
131                 ), __FUNCTION__, __LINE__);
132
133         // Entry found?
134         if (SQL_NUMROWS($result) == 1) {
135                 // Load data
136                 $GLOBALS['ranking_content'] = merge_array($GLOBALS['ranking_content'], SQL_FETCHARRAY($result));
137
138                 // Remember all values for later use
139                 $self  = true;
140
141                 // Transfer data to template
142                 $GLOBALS['ranking_content']['yr_level']  = $GLOBALS['ranking_content']['level'];
143                 $GLOBALS['ranking_content']['yr_points'] = translateComma($GLOBALS['ranking_content']['points']);
144                 $GLOBALS['ranking_content']['yr_tmark']  = generateDateTime($GLOBALS['ranking_content']['timemark'], 1);
145
146                 // Load template
147                 $GLOBALS['ranking_content']['own'] = loadTemplate('show_bonus_yr', true, $GLOBALS['ranking_content']);
148         } // END - if
149
150         // Load rankings
151         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE %s=%s ORDER BY `level` ASC LIMIT {?bonus_lines?}",
152                 array($type, $data), __FUNCTION__, __LINE__);
153         if (SQL_NUMROWS($result) > 0) {
154                 // Start generating the ranking list
155                 $max = SQL_NUMROWS($result);
156
157                 // Init variables
158                 $SW = 2;
159
160                 // Output all ranks (levels)
161                 for ($rank = 1; $rank <= $max; $rank++) {
162                         // Load data
163                         $result_users = SQL_QUERY_ESC("SELECT
164         `userid`, `points`
165 FROM
166         `{?_MYSQL_PREFIX?}_bonus_turbo`
167 WHERE
168         `%s`=%s AND
169         `level`=%s
170 LIMIT 1",
171                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
172
173                         // Nothing found by default
174                         $rows['userid'] = '---';
175                         $rows['points'] = '---';
176
177                         // Are you one of them?
178                         if (SQL_NUMROWS($result_users) == 1) {
179                                 // Load data
180                                 $rows = merge_array($rows, SQL_FETCHARRAY($result_users));
181
182                                 // Is ext-nickname active?
183                                 if (isExtensionActive('nickname')) {
184                                         // Then get the nickname
185                                         $nick = getNickname($rows['userid']);
186
187                                         // Is it not empty? Then use it
188                                         if (!empty($nick)) $rows['userid'] = $nick;
189                                 } // END - if
190
191                                 // Translate comma
192                                 $rows['points'] = translateComma($rows['points']);
193                         } // END - if
194
195                         // Free result
196                         SQL_FREERESULT($result_users);
197
198                         // Add more
199                         $rows['rank'] = $rank;
200                         $rows['sw']   = $SW;
201
202                         // Output row
203                         $OUT .= "<tr>
204   <td class=\"bonus_rank_".$rows['rank']." bottom switch_sw".$rows['sw']."\">&nbsp;".$rows['rank'].".</td>
205   <td class=\"bonus_rank_".$rows['rank']." bottom switch_sw".$rows['sw']."\" align=\"center\">".$rows['userid']."</td>
206   <td class=\"bonus_rank_".$rows['rank']." bottom switch_sw".$rows['sw']."\" align=\"center\">".$rows['points']."</td>
207 </tr>\n";
208
209                         // Switch color
210                         $SW = 3 - $SW;
211                 } // END - for
212
213                 if ($self === false) {
214                         // If current user was not found set constant
215                         // @TODO Try to find a way for rewriting this constant
216                         $GLOBALS['ranking_content']['rankings'] = getMessage('BONUS_RANK_YOU_ARE_404');
217                 } // END - if
218         } else {
219                 // No entries found!
220                 $OUT = "<tr>
221   <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom\">
222     <div class=\"guest_failed\">".getMaskedMessage('BONUS_NO_RANKS', $data)."</div>
223   </td>
224 </tr>\n";
225         }
226
227         // Retutn content
228         return $OUT;
229 }
230
231 //
232 function handleBonusPoints ($mode) {
233         // Shall we add bonus points?
234         if (getConfig('bonus_active') != 'Y') return;
235
236         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
237         if ((getConfig('bonus_mode') == 'UID') && (getConfig('bonus_userid') == '0') && (isExtensionActive('jackpot'))) {
238                 // Update database & config
239                 updateConfiguration('bonus_mode', 'JACKPOT');
240         } // END - if
241
242         if ($mode == 'login_bonus') {
243                 // Login bonus detected
244                 $points = getConfig('login_bonus');
245         } else {
246                 // Direct points supplied
247                 $points = $mode;
248         }
249
250         // Check his amount first
251         $total = countSumTotalData(getConfig('bonus_userid'), 'user_points', 'points') - countSumTotalData(getConfig('bonus_userid'), 'user_data', 'used_points');
252
253         // Subtract points from...
254         switch (getConfig('bonus_mode')) {
255                 case 'JACKPOT': // ... jackpot
256                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) == -1) && (getConfig('bonus_userid') > 0)) {
257                                 if ($total >= $points) {
258                                         // Subtract points from userid's account
259                                         subtractPointsFromJackpot('bonus_payout_jackpot', getConfig('bonus_userid'), $points);
260                                 } // END - if
261                         } // END - if
262                         break;
263
264                 case 'UID': // ... userid's account
265                         if ($total >= $points) {
266                                 // Subtract points from userid's account
267                                 subtractPoints('bonus_payout_userid', getConfig('bonus_userid'), $points);
268                         } elseif (isExtensionActive('jackpot')) {
269                                 // Try to subtract from jackpot
270                                 $dummy = subtractPointsFromJackpot($points);
271                         }
272                         break;
273         } // END - switch
274 }
275
276 // Purges expired fast-click bonus entries
277 function purgeExpiredTurboBonus() {
278         // Remove entries
279         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `timemark` < (UNIX_TIMESTAMP() - {?bonus_timeout?})", __FUNCTION__, __LINE__);
280
281         if (SQL_AFFECTEDROWS() > 0) {
282                 // Send out email to admin
283                 sendAdminNotification(getMessage('AUTOPURGE_ADMIN_TURBO_SUBJECT'), 'admin_autopurge_turbo', SQL_AFFECTEDROWS(), '');
284         } // END - if
285 }
286
287 ///////////////////////////////////////////////////////////////////////////////
288 //                              Only filter functions
289 ///////////////////////////////////////////////////////////////////////////////
290
291 // Filter for adding login bonus to the user's account
292 function FILTER_ADD_LOGIN_BONUS () {
293         // Is the user data valid?
294         if (!isMember()) {
295                 // Do only run for logged in members
296                 debug_report_bug('Please only run this filter for logged in users.');
297         } // END - if
298
299         // Bonus is not given by default ;-)
300         $bonus = false;
301         if ((isExtensionInstalledAndNewer('sql_patches', '0.2.8')) && (getConfig('bonus_active') == 'Y') && (getConfig('bonus_login_yn') == 'Y')) {
302                 // Update last login if far enougth away
303                 // @TODO This query isn't right, it will only update if the user was for a longer time away!
304                 SQL_QUERY_ESC("UPDATE
305         `{?_MYSQL_PREFIX?}_user_data`
306 SET
307         `last_login`=UNIX_TIMESTAMP()
308 WHERE
309         `userid`=%s AND
310         `last_login` < (UNIX_TIMESTAMP() - {?login_timeout?})
311 LIMIT 1",
312                         array(
313                                 getMemberId()
314                         ), __FILE__, __LINE__
315                 );
316
317                 // Updated entry?
318                 $bonus = (SQL_AFFECTEDROWS() == 1);
319         } // END - if
320
321         if (($bonus === true) && (getRequestParameter('mode') == 'bonus')) {
322                 // Output message with added points
323                 $GLOBALS['message'] .= '<div class="tiny">' . getMessage('BONUS_LOGIN_BONUS_ADDED') . '</div>';
324         } elseif (isExtensionActive('bonus')) {
325                 // No login bonus added!
326                 $GLOBALS['message'] .= '<div class="member_failed">{--BONUS_LOGIN_BONUS_NOT_ADDED--}</div>';
327         }
328 }
329
330 // [EOF]
331 ?>