1310396b9741c6c0943e907e148b90ebe82d681e
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
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 {
43         global $_CONFIG;
44
45         // Shall we add bonus points?
46         if ($_CONFIG['bonus_active'] == 'N') return;
47
48         // Select SQL command
49         $SQL = "";
50         switch ($type)
51         {
52         case "bonusid":
53                 $result = SQL_QUERY_ESC("SELECT clicks FROM "._MYSQL_PREFIX."_bonus WHERE id=%d LIMIT 1",
54                  array($mid), __FILE__, __LINE__);
55                 $bonus = $mid; $mail = "0";
56                 break;
57
58         case "mailid" :
59                 $result = SQL_QUERY_ESC("SELECT clicks FROM "._MYSQL_PREFIX."_user_stats WHERE id=%d LIMIT 1",
60                  array($mid), __FILE__, __LINE__);
61                 $bonus = "0"; $mail = $mid;
62                 break;
63         }
64
65         // Load clicks from table as current rank
66         list($rank) = SQL_FETCHROW($result);
67
68         if ($rank == 1)
69         {
70                 // First rank!
71                 $rank = 1;
72                 $points = $_CONFIG['turbo_bonus'];
73         }
74          else
75         {
76                 // Anything else so let's explode all entered rank points
77                 $test = explode(";", $_CONFIG['bonus_rates']);
78                 if (!empty($test[$rank - 2]))
79                 {
80                         // Level found
81                         $points = $test[$rank - 2];
82                 }
83                  else
84                 {
85                         // Level not found!
86                         $points = "0.00000";
87                 }
88         }
89
90         // Add points to his account directly
91         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET turbo_bonus=turbo_bonus+".$points." WHERE userid=%d LIMIT 1",
92          array(bigintval($uid)), __FILE__, __LINE__);
93
94         // Rember this whole data for displaying ranking list
95         $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_bonus_turbo (userid, mail_id, bonus_id, level, points, timemark) VALUES ('%s', '%s', '%s', '".$rank."', '".$points."', UNIX_TIMESTAMP())",
96          array(bigintval($uid), bigintval($mail), bigintval($bonus)), __FILE__, __LINE__);
97
98         if ((GET_EXT_VERSION("bonus") >= "0.3.5") && ($_CONFIG['bonus_mode'] != "ADD") && ($points > 0)) BONUS_POINTS_HANDLER($points);
99 }
100 //
101 function BONUS_MAKE_RANK_ROWS($data, $type, $uid)
102 {
103         global $_CONFIG;
104         $self = false; $OUT = "";
105
106         // How many ranks do we have?
107         $ranks = sizeof(explode(";", $_CONFIG['bonus_rates'])) + 1;
108
109         // Load current user's data
110         $result = SQL_QUERY_ESC("SELECT level, points, timemark FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s AND userid=%d LIMIT 1",
111          array($type, $data, $uid), __FILE__, __LINE__);
112         if (SQL_NUMROWS($result) == 1)
113         {
114                 // Load data
115                 list($rank, $points, $mark) = SQL_FETCHROW($result);
116
117                 // Remember all values for later use
118                 $self  = true; $points = TRANSLATE_COMMA($points);
119
120                 // Transfer data to template
121                 define('__YR_LEVEL' , $rank);
122                 define('__YR_POINTS', $points);
123                 define('__YR_TMARK' , MAKE_DATETIME($mark, "1"));
124
125                 // Load template
126                 define('__YOUR_RANKING_LINE', LOAD_TEMPLATE("show_bonus_yr", true));
127         }
128
129         // Load rankings
130         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s ORDER BY level LIMIT %s",
131          array($type, $data, $_CONFIG['bonus_lines']), __FILE__, __LINE__);
132         if (SQL_NUMROWS($result) > 0)
133         {
134                 // Start generating the ranking list
135                 $max = SQL_NUMROWS($result);
136
137                 $SW = 2;
138                 for ($rank = 1; $rank <= $max; $rank++)
139                 {
140                         // Load data
141                         $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM "._MYSQL_PREFIX."_bonus_turbo WHERE %s=%s AND level='%s' LIMIT 1",
142                          array($type, $data, $rank), __FILE__, __LINE__);
143                         list($userid, $points, $mark) = SQL_FETCHROW($result_users);
144
145                         // Are you one of them?
146                         if (empty($userid))
147                         {
148                                 // Nothing found
149                                 $userid = "---"; $points = "---";
150                         }
151                          else
152                         {
153                                 // Translate comma
154                                 $points = TRANSLATE_COMMA($points);
155                         }
156
157                         // Output row
158                         $OUT .= "<TR>
159   <TD class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\">&nbsp;".$rank.".</TD>
160   <TD class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$userid."</TD>
161   <TD class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$points."</TD>
162 </TR>\n";
163                         $SW = 3 - $SW;
164                 }
165                 if (!$self)
166                 {
167                         // If current user was not found set constant
168                         define('__YOUR_RANKING_LINE', BONUS_RANK_YOU_ARE_NOT_FOUND);
169                 }
170         }
171          else
172         {
173                 // No entries found!
174                 $OUT = "<TR>
175   <TD colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
176     <STRONG class=\"guest_failed\">".BONUS_NO_RANKS_1.$data.BONUS_NO_RANKS_2."</STRONG>
177   </TD>
178 </TR>\n";
179                 define('__YOUR_RANKING_LINE', "");
180         }
181         return $OUT;
182 }
183 //
184 function BONUS_POINTS_HANDLER($MODE)
185 {
186         global $_CONFIG;
187
188         // Shall we add bonus points?
189         if ($_CONFIG['bonus_active'] == 'N') return;
190
191         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
192         if (($_CONFIG['bonus_mode'] == "UID") && ($_CONFIG['bonus_uid'] == "0")) $_CONFIG['bonus_mode'] = "JACKPOT";
193
194         if ($MODE == "login_bonus")
195         {
196                 // Login bonus detected
197                 $points = $_CONFIG['login_bonus'];
198         }
199          else
200         {
201                 // Direct points supplied
202                 $points = $MODE;
203         }
204
205         // Subtract points from...
206         switch ($_CONFIG['bonus_mode'])
207         {
208         case "JACKPOT": // ... jackpot
209                 if ((SUB_JACKPOT($points) == -1) && ($_CONFIG['bonus_uid'] > 0))
210                 {
211                         // Check points amount first...
212                         $TOTAL = GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_points", "points") - GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_data", "used_points");
213                         if ($TOTAL >= $points)
214                         {
215                                 // Subtract points from userid's account
216                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET used_points=used_points+%s WHERE userid=%d LIMIT 1",
217                                  array(bigintval($points), bigintval($_CONFIG['bonus_uid'])), __FILE__, __LINE__);
218
219                                 // Update mediadata as well
220                                 if (GET_EXT_VERSION("mediadata") >= "0.0.4")
221                                 {
222                                         // Update database
223                                         MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
224                                 }
225                         }
226                 }
227                 break;
228
229         case "UID": // ... userid's account
230                 // Check his amount first
231                 $TOTAL = GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_points", "points") - GET_TOTAL_DATA($_CONFIG['bonus_uid'], "user_data", "used_points");
232                 if ($TOTAL >= $points)
233                 {
234                         // Subtract points from userid's account
235                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET used_points=used_points+%s WHERE userid=%d LIMIT 1",
236                          array(bigintval($points), bigintval($_CONFIG['bonus_uid'])),  __FILE__, __LINE__);
237
238                         // Update mediadata as well
239                         if (GET_EXT_VERSION("mediadata") >= "0.0.4")
240                         {
241                                 // Update database
242                                 MEDIA_UPDATE_ENTRY(array("total_points"), "sub", $points);
243                         }
244                 }
245                  else
246                 {
247                         // Try to subtract from jackpot
248                         $dummy = SUB_JACKPOT($points);
249                 }
250                 break;
251         }
252 }
253 //
254 function BONUS_PURGE_EXPIRED_TURBO_BONUS()
255 {
256         global $_CONFIG;
257         // Remove entries
258         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_bonus_turbo WHERE timemark < ".(time() - $_CONFIG['bonus_timeout']), __FILE__, __LINE__);
259         $DELETED = SQL_AFFECTEDROWS($result);
260         if ($DELETED > 0)
261         {
262                 // Send out email to admin
263                 if (GET_EXT_VERSION("admins") >= "0.4.1")
264                 {
265                         SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_TURBO_SUBJECT, "admin_autopurge_turbo", $DELETED, "");
266                 }
267                  else
268                 {
269                         $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_turbo", $DELETED, "");
270                         SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_TURBO_SUBJECT, $msg);
271                 }
272         }
273 }
274 //
275 ?>