109e740f1dfaed1f7a84b420c44fcf3d0677537a
[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 - 2008 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         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), '/inc') + 4) . '/security.php';
42         require($INC);
43 }
44
45 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
46 function BONUS_ADD_TURBO_POINTS ($mid, $uid, $type) {
47         // Shall we add bonus points?
48         if (getConfig('bonus_active') != 'Y') return false;
49
50         // Init variables
51         $sql = ''; $bonys = 0; $mail = 0; $column = '';
52
53         // Select SQL command
54         switch ($type)
55         {
56         case 'bonusid':
57                 $column = 'bonus_id';
58                 $bonus = $mid;
59                 break;
60
61         case 'mailid' :
62                 $column = 'mail_id';
63                 $mail = $mid;
64                 break;
65
66         default:
67                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
68                 break;
69         }
70
71         // Is a column name set?
72         if (empty($column)) {
73                 // No, then abort here
74                 return false;
75         } // END - if
76
77         // Check for entry
78         $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt FROM  `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE userid=%s AND %s=%s LIMIT 1",
79                 array($uid, $column, $mid), __FUNCTION__, __LINE__);
80
81         // Load clicks from table as current rank
82         list($rank) = SQL_FETCHROW($result);
83
84         // Add one line
85         $rank++;
86
87         // Free result
88         SQL_FREERESULT($result);
89
90         // Which rank?
91         if ($rank == 1) {
92                 // First rank!
93                 $points = getConfig('turbo_bonus');
94         } else {
95                 // Anything else so let's explode all entered rank points
96                 $test = explode(';', getConfig('turbo_rates'));
97                 if (!empty($test[$rank - 2])) {
98                         // Level found
99                         $points = $test[$rank - 2];
100                 } else {
101                         // Level not found!
102                         $points = "0.00000";
103                 }
104         }
105
106         // Add points to his account directly
107         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET turbo_bonus=turbo_bonus+".$points." WHERE userid=%s LIMIT 1",
108                 array(bigintval($uid)), __FUNCTION__, __LINE__);
109
110         // Rember this whole data for displaying ranking list
111         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())",
112                 array(bigintval($uid), bigintval($mail), bigintval($bonus), $rank, $points), __FUNCTION__, __LINE__);
113
114         if ((GET_EXT_VERSION('bonus') >= '0.3.5') && (getConfig('bonus_mode') != "ADD") && ($points > 0)) BONUS_POINTS_HANDLER($points);
115 }
116 //
117 function BONUS_MAKE_RANK_ROWS ($data, $type, $uid) {
118         $self = false; $OUT = '';
119
120         // How many ranks do we have?
121         $ranks = count(explode(';', getConfig('turbo_rates'))) + 1;
122
123         // Load current user's data
124         $result = SQL_QUERY_ESC("SELECT level, points, timemark FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s AND `userid`=%s LIMIT 1",
125                 array($type, $data, $uid), __FUNCTION__, __LINE__);
126         if (SQL_NUMROWS($result) == 1) {
127                 // Load data
128                 list($rank, $points, $mark) = SQL_FETCHROW($result);
129
130                 // Remember all values for later use
131                 $self  = true; $points = translateComma($points);
132
133                 // Transfer data to template
134                 define('__YR_LEVEL' , $rank);
135                 define('__YR_POINTS', $points);
136                 define('__YR_TMARK' , generateDateTime($mark, "1"));
137
138                 // Load template
139                 define('__YOUR_RANKING_LINE', LOAD_TEMPLATE("show_bonus_yr", true));
140         } // END - if
141
142         // Load rankings
143         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s ORDER BY level LIMIT %s",
144                 array($type, $data, getConfig('bonus_lines')), __FUNCTION__, __LINE__);
145         if (SQL_NUMROWS($result) > 0) {
146                 // Start generating the ranking list
147                 $max = SQL_NUMROWS($result);
148
149                 $SW = 2;
150                 for ($rank = 1; $rank <= $max; $rank++) {
151                         // Load data
152                         $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s AND level=%s LIMIT 1",
153                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
154
155                         // Nothing found by default
156                         $userid = '---'; $points = '---';
157
158                         // Are you one of them?
159                         if (SQL_NUMROWS($result_users) == 1) {
160                                 // Load data
161                                 list($userid, $points, $mark) = SQL_FETCHROW($result_users);
162
163                                 // Translate comma
164                                 $points = translateComma($points);
165                         } // END - if
166
167                         // Output row
168                         $OUT .= "<tr>
169   <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\">&nbsp;".$rank.".</td>
170   <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$userid."</td>
171   <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$points."</td>
172 </tr>\n";
173                         $SW = 3 - $SW;
174                 } // END - for
175
176                 if (!$self) {
177                         // If current user was not found set constant
178                         define('__YOUR_RANKING_LINE', BONUS_RANK_YOU_ARE_NOT_FOUND);
179                 } // END - if
180         } else {
181                 // No entries found!
182                 $OUT = "<tr>
183   <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
184     <div class=\"guest_failed\">".sprintf(getMessage('BONUS_NO_RANKS'), $data)."</div>
185   </td>
186 </tr>\n";
187                 define('__YOUR_RANKING_LINE', '');
188         }
189         return $OUT;
190 }
191 //
192 function BONUS_POINTS_HANDLER ($mode) {
193         // Shall we add bonus points?
194         if (getConfig('bonus_active') != 'Y') return;
195
196         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
197         if ((getConfig('bonus_mode') == "UID") && (getConfig('bonus_uid') == '0')) {
198                 // Update database & config
199                 updateConfiguration('bonus_mode', 'JACKPOT');
200         } // END - if
201
202         if ($mode == "login_bonus") {
203                 // Login bonus detected
204                 $points = getConfig('login_bonus');
205         } else {
206                 // Direct points supplied
207                 $points = $mode;
208         }
209
210         // Subtract points from...
211         switch (getConfig('bonus_mode'))
212         {
213         case 'JACKPOT': // ... jackpot
214                 if ((SUB_JACKPOT($points) == -1) && (getConfig('bonus_uid') > 0)) {
215                         // Check points amount first...
216                         $total = GET_TOTAL_DATA(getConfig('bonus_uid'), "user_points", "points") - GET_TOTAL_DATA(getConfig('bonus_uid'), "user_data", "used_points");
217                         if ($total >= $points) {
218                                 // Subtract points from userid's account
219                                 SUB_POINTS("bonus_payout_jackpot", getConfig('bonus_uid'), $points);
220                         } // END - if
221                 } // END - if
222                 break;
223
224         case "UID": // ... userid's account
225                 // Check his amount first
226                 $total = GET_TOTAL_DATA(getConfig('bonus_uid'), "user_points", "points") - GET_TOTAL_DATA(getConfig('bonus_uid'), "user_data", "used_points");
227                 if ($total >= $points) {
228                         // Subtract points from userid's account
229                         SUB_POINTS("bonus_payout_uid", getConfig('bonus_uid'), $points);
230                 } else {
231                         // Try to subtract from jackpot
232                         $dummy = SUB_JACKPOT($points);
233                 }
234                 break;
235         }
236 }
237
238 //
239 function BONUS_PURGE_EXPIRED_TURBO_BONUS() {
240         // Remove entries
241         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE timemark < (UNIX_TIMESTAMP() - ".getConfig('bonus_timeout').")", __FUNCTION__, __LINE__);
242         if (SQL_AFFECTEDROWS() > 0) {
243                 // Send out email to admin
244                 sendAdminNotification(AUTOPURGE_ADMIN_TURBO_SUBJECT, "admin_autopurge_turbo", SQL_AFFECTEDROWS(), '');
245         }
246 }
247 //
248 ?>