68543dc05af460b7da3b2c0427de8766f5bc7ba1
[mailer.git] / inc / libs / bonus_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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(bigintval($userid), $points), __FUNCTION__, __LINE__);
97
98         // Rember this whole data for displaying ranking list
99         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())",
100                 array(
101                         bigintval($userid),
102                         bigintval($mail),
103                         bigintval($bonus),
104                         $rank,
105                         $points
106                 ), __FUNCTION__, __LINE__);
107
108         if ((getExtensionVersion('bonus') >= '0.3.5') && (getConfig('bonus_mode') != "ADD") && ($points > 0)) handleBonusPoints($points);
109 }
110
111 //
112 function addBonusRanks ($data, $type, $userid) {
113         // Init variables
114         $self = false; $OUT = ''; $GLOBALS['ranking_content'] = array();
115
116         // Clear rankings by default
117         $GLOBALS['ranking_content']['rankings'] = '';
118
119         // How many ranks do we have?
120         $ranks = count(explode(';', getConfig('turbo_rates'))) + 1;
121
122         // Load current user's data
123         $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
124                 array($type, $data, $userid), __FUNCTION__, __LINE__);
125         if (SQL_NUMROWS($result) == 1) {
126                 // Load data
127                 $GLOBALS['ranking_content'] = merge_array($GLOBALS['ranking_content'], SQL_FETCHARRAY($result));
128
129                 // Remember all values for later use
130                 $self  = true;
131
132                 // Transfer data to template
133                 $GLOBALS['ranking_content']['yr_level']  = $GLOBALS['ranking_content']['level'];
134                 $GLOBALS['ranking_content']['yr_points'] = translateComma($GLOBALS['ranking_content']['points']);
135                 $GLOBALS['ranking_content']['yr_tmark']  = generateDateTime($GLOBALS['ranking_content']['timemark'], '1');
136
137                 // Load template
138                 $GLOBALS['ranking_content']['own'] = loadTemplate('show_bonus_yr', true, $GLOBALS['ranking_content']);
139         } // END - if
140
141         // Load rankings
142         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE %s=%s ORDER BY `level` ASC LIMIT %s",
143                 array($type, $data, getConfig('bonus_lines')), __FUNCTION__, __LINE__);
144         if (SQL_NUMROWS($result) > 0) {
145                 // Start generating the ranking list
146                 $max = SQL_NUMROWS($result);
147
148                 // Init variables
149                 $SW = 2;
150
151                 // Output all ranks (levels)
152                 for ($rank = 1; $rank <= $max; $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
182                                 // Translate comma
183                                 $rows['points'] = translateComma($rows['points']);
184                         } // END - if
185
186                         // Free result
187                         SQL_FREERESULT($result_users);
188
189                         // Add more
190                         $rows['rank'] = $rank;
191                         $rows['sw']   = $SW;
192
193                         // Output row
194                         $OUT .= "<tr>
195   <td class=\"bonus_rank_".$rows['rank']." bottom2 switch_sw".$rows['sw']."\">&nbsp;".$rows['rank'].".</td>
196   <td class=\"bonus_rank_".$rows['rank']." bottom2 switch_sw".$rows['sw']."\" align=\"center\">".$rows['userid']."</td>
197   <td class=\"bonus_rank_".$rows['rank']." bottom2 switch_sw".$rows['sw']."\" align=\"center\">".$rows['points']."</td>
198 </tr>\n";
199
200                         // Switch color
201                         $SW = 3 - $SW;
202                 } // END - for
203
204                 if ($self === false) {
205                         // If current user was not found set constant
206                         // @TODO Try to find a way for rewriting this constant
207                         $GLOBALS['ranking_content']['rankings'] = getMessage('BONUS_RANK_YOU_ARE_404');
208                 } // END - if
209         } else {
210                 // No entries found!
211                 $OUT = "<tr>
212   <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
213     <div class=\"guest_failed\">".sprintf(getMessage('BONUS_NO_RANKS'), $data)."</div>
214   </td>
215 </tr>\n";
216         }
217
218         // Retutn content
219         return $OUT;
220 }
221
222 //
223 function handleBonusPoints ($mode) {
224         // Shall we add bonus points?
225         if (getConfig('bonus_active') != 'Y') return;
226
227         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
228         if ((getConfig('bonus_mode') == 'UID') && (getConfig('bonus_userid') == '0') && (isExtensionActive('jackpot'))) {
229                 // Update database & config
230                 updateConfiguration('bonus_mode', 'JACKPOT');
231         } // END - if
232
233         if ($mode == 'login_bonus') {
234                 // Login bonus detected
235                 $points = getConfig('login_bonus');
236         } else {
237                 // Direct points supplied
238                 $points = $mode;
239         }
240
241         // Subtract points from...
242         switch (getConfig('bonus_mode')) {
243                 case 'JACKPOT': // ... jackpot
244                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) == -1) && (getConfig('bonus_userid') > 0)) {
245                                 // Check points amount first...
246                                 $total = countSumTotalData(getConfig('bonus_userid'), 'user_points', 'points') - countSumTotalData(getConfig('bonus_userid'), 'user_data', 'used_points');
247                                 if ($total >= $points) {
248                                         // Subtract points from userid's account
249                                         subtractPoints('bonus_payout_jackpot', getConfig('bonus_userid'), $points);
250                                 } // END - if
251                         } // END - if
252                         break;
253
254                 case 'UID': // ... userid's account
255                         // Check his amount first
256                         $total = countSumTotalData(getConfig('bonus_userid'), 'user_points', 'points') - countSumTotalData(getConfig('bonus_userid'), 'user_data', 'used_points');
257                         if ($total >= $points) {
258                                 // Subtract points from userid's account
259                                 subtractPoints('bonus_payout_userid', getConfig('bonus_userid'), $points);
260                         } elseif (isExtensionActive('jackpot')) {
261                                 // Try to subtract from jackpot
262                                 $dummy = subtractPointsFromJackpot($points);
263                         }
264                         break;
265         }
266 }
267
268 // Purges expired fast-click bonus entries
269 function purgeExpiredTurboBonus() {
270         // Remove entries
271         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `timemark` < (UNIX_TIMESTAMP() - {?bonus_timeout?})", __FUNCTION__, __LINE__);
272
273         if (SQL_AFFECTEDROWS() > 0) {
274                 // Send out email to admin
275                 sendAdminNotification(getMessage('AUTOPURGE_ADMIN_TURBO_SUBJECT'), 'admin_autopurge_turbo', SQL_AFFECTEDROWS(), '');
276         } // END - if
277 }
278
279 //
280 ?>