86d7f83f9ac434ab007f6599f7ebf948ffb34660
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
41 function BONUS_ADD_TURBO_POINTS($mid, $uid, $type) {
42         global $_CONFIG;
43
44         // Shall we add bonus points?
45         if ($_CONFIG['bonus_active'] == "N") return false;
46
47         // Init variables
48         $SQL = ""; $bonys = 0; $mail = 0; $column = "";
49
50         // Select SQL command
51         switch ($type)
52         {
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                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
65                 break;
66         }
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         $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt FROM  "._MYSQL_PREFIX."_bonus_turbo WHERE userid=%s AND %s=%s LIMIT 1",
76                 array($uid, $column, $mid), __FILE__, __LINE__);
77
78         // Load clicks from table as current rank
79         list($rank) = SQL_FETCHROW($result);
80
81         // Add one line
82         $rank++;
83
84         // Free result
85         SQL_FREERESULT($result);
86
87         // Which rank?
88         if ($rank == 1) {
89                 // First rank!
90                 $points = $_CONFIG['turbo_bonus'];
91         } else {
92                 // Anything else so let's explode all entered rank points
93                 $test = explode(";", $_CONFIG['turbo_rates']);
94                 if (!empty($test[$rank - 2])) {
95                         // Level found
96                         $points = $test[$rank - 2];
97                 } else {
98                         // Level not found!
99                         $points = "0.00000";
100                 }
101         }
102
103         // Add points to his account directly
104         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET turbo_bonus=turbo_bonus+".$points." WHERE userid=%s LIMIT 1",
105                 array(bigintval($uid)), __FILE__, __LINE__);
106
107         // Rember this whole data for displaying ranking list
108         $result = 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())",
109                 array(bigintval($uid), bigintval($mail), bigintval($bonus), $rank, $points), __FILE__, __LINE__);
110
111         if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD") && ($points > 0)) BONUS_POINTS_HANDLER($points);
112 }
113 //
114 function BONUS_MAKE_RANK_ROWS ($data, $type, $uid) {
115         global $_CONFIG;
116         $self = false; $OUT = "";
117
118         // How many ranks do we have?
119         $ranks = sizeof(explode(";", $_CONFIG['turbo_rates'])) + 1;
120
121         // Load current user's data
122         $result = SQL_QUERY_ESC("SELECT level, points, timemark FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s AND userid=%s LIMIT 1",
123                 array($type, $data, $uid), __FILE__, __LINE__);
124         if (SQL_NUMROWS($result) == 1) {
125                 // Load data
126                 list($rank, $points, $mark) = SQL_FETCHROW($result);
127
128                 // Remember all values for later use
129                 $self  = true; $points = TRANSLATE_COMMA($points);
130
131                 // Transfer data to template
132                 define('__YR_LEVEL' , $rank);
133                 define('__YR_POINTS', $points);
134                 define('__YR_TMARK' , MAKE_DATETIME($mark, "1"));
135
136                 // Load template
137                 define('__YOUR_RANKING_LINE', LOAD_TEMPLATE("show_bonus_yr", true));
138         } // END - if
139
140         // Load rankings
141         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s ORDER BY level LIMIT %s",
142                 array($type, $data, $_CONFIG['bonus_lines']), __FILE__, __LINE__);
143         if (SQL_NUMROWS($result) > 0) {
144                 // Start generating the ranking list
145                 $max = SQL_NUMROWS($result);
146
147                 $SW = 2;
148                 for ($rank = 1; $rank <= $max; $rank++) {
149                         // Load data
150                         $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s AND level=%s LIMIT 1",
151                                 array($type, $data, $rank), __FILE__, __LINE__);
152
153                         // Nothing found by default
154                         $userid = "---"; $points = "---";
155
156                         // Are you one of them?
157                         if (SQL_NUMROWS($result_users) == 1) {
158                                 // Load data
159                                 list($userid, $points, $mark) = SQL_FETCHROW($result_users);
160
161                                 // Translate comma
162                                 $points = TRANSLATE_COMMA($points);
163                         } // END - if
164
165                         // Output row
166                         $OUT .= "<TR>
167   <TD class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\">&nbsp;".$rank.".</TD>
168   <TD class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$userid."</TD>
169   <TD class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$points."</TD>
170 </TR>\n";
171                         $SW = 3 - $SW;
172                 } // END - for
173
174                 if (!$self) {
175                         // If current user was not found set constant
176                         define('__YOUR_RANKING_LINE', BONUS_RANK_YOU_ARE_NOT_FOUND);
177                 } // END - if
178         } else {
179                 // No entries found!
180                 $OUT = "<TR>
181   <TD colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
182     <STRONG class=\"guest_failed\">".BONUS_NO_RANKS_1.$data.BONUS_NO_RANKS_2."</STRONG>
183   </TD>
184 </TR>\n";
185                 define('__YOUR_RANKING_LINE', "");
186         }
187         return $OUT;
188 }
189 //
190 function BONUS_POINTS_HANDLER($MODE) {
191         global $_CONFIG;
192
193         // Shall we add bonus points?
194         if ($_CONFIG['bonus_active'] == "N") return;
195
196         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
197         if (($_CONFIG['bonus_mode'] == "UID") && ($_CONFIG['bonus_uid'] == "0")) {
198                 // Update database
199                 UPDATE_CONFIG(array('bonus_mode'), array("JACKPOT"));
200
201                 // Update configuration
202                 $_CONFIG['bonus_mode'] = "JACKPOT";
203         } // END - if
204
205         if ($MODE == "login_bonus") {
206                 // Login bonus detected
207                 $points = $_CONFIG['login_bonus'];
208         } else {
209                 // Direct points supplied
210                 $points = $MODE;
211         }
212
213         // Subtract points from...
214         switch ($_CONFIG['bonus_mode'])
215         {
216         case "JACKPOT": // ... jackpot
217                 if ((SUB_JACKPOT($points) == -1) && ($_CONFIG['bonus_uid'] > 0)) {
218                         // Check points amount first...
219                         $TOTAL = GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_points", "points") - GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_data", "used_points");
220                         if ($TOTAL >= $points) {
221                                 // Subtract points from userid's account
222                                 SUB_POINTS("bonus_payout_jackpot", $_CONFIG['bonus_uid'], $points);
223                         } // END - if
224                 } // END - if
225                 break;
226
227         case "UID": // ... userid's account
228                 // Check his amount first
229                 $TOTAL = GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_points", "points") - GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_data", "used_points");
230                 if ($TOTAL >= $points) {
231                         // Subtract points from userid's account
232                         SUB_POINTS("bonus_payout_uid", $_CONFIG['bonus_uid'], $points);
233                 } else {
234                         // Try to subtract from jackpot
235                         $dummy = SUB_JACKPOT($points);
236                 }
237                 break;
238         }
239 }
240 //
241 function BONUS_PURGE_EXPIRED_TURBO_BONUS()
242 {
243         global $_CONFIG;
244         // Remove entries
245         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_bonus_turbo WHERE timemark < (UNIX_TIMESTAMP() - ".$_CONFIG['bonus_timeout'].")", __FILE__, __LINE__);
246         if (SQL_AFFECTEDROWS() > 0) {
247                 // Send out email to admin
248                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_TURBO_SUBJECT, "admin_autopurge_turbo", $DELETED, "");
249         }
250 }
251 //
252 ?>