e425c8f1ac13216546df38cbb03bcb52fee7c6e9
[mailer.git] / inc / monthly / monthly_bonus.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 11/14/2004 *
4  * ===============                              Last change: 11/19/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : monthly_bonus.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : Monthly bonus from click-bonus                   *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Monatlicher Bonus von Klick-Verguetung           *
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  elseif ((!EXT_IS_ACTIVE("bonus")) && (!IS_ADMIN()))
41 {
42         ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "bonus");
43         return;
44 }
45
46 // Do not execute when script is in CSS mode or no daily reset
47 if (($CSS == 1) || (!defined('__DAILY_RESET'))) return;
48
49 // Get current month (2 digits)
50 $curr = date("m", time());
51
52 if (($curr != $_CONFIG['bonus_month']) && ($_CONFIG['bonus_ranks'] > 0) && ($CSS != 1))
53 {
54         // Extension "autopurge" is inactive or purging of inactive accounts is deactivated
55         $whereStatement1 = "WHERE status='CONFIRMED'";
56         $whereStatement2 = 0;
57         $whereStatement3 = bigintval($_CONFIG['bonus_ranks']);
58
59         // Shall I keep inactive members away from here? (mostly wanted in an "active-rallye" ...)
60         if (EXT_IS_ACTIVE("autopurge"))
61         {
62                 // Use last online stamp only when autopurge for inactive members is activated
63                 if ($_CONFIG['ap_in_since'] > 0)
64                 {
65                         // Okay, include last online timestamp
66                         $whereStatement1 = "WHERE status='CONFIRMED' AND last_online >=";
67                         $whereStatement2 = bigintval(time() - $_CONFIG['ap_in_since']);
68                         $whereStatement3 = bigintval($_CONFIG['bonus_ranks']);
69                 }
70         }
71
72         // Add more bonus points here
73         $ADD = " AND (0";
74         if ($_CONFIG['bonus_click_yn'] == 'Y') $ADD .= " + turbo_bonus";
75         if ($_CONFIG['bonus_login_yn'] == 'Y') $ADD .= " + login_bonus";
76         if ($_CONFIG['bonus_order_yn'] == 'Y') $ADD .= " + bonus_order";
77         if ($_CONFIG['bonus_stats_yn'] == 'Y') $ADD .= " + bonus_stats";
78         if ($_CONFIG['bonus_ref_yn']   == 'Y') $ADD .= " + bonus_ref";
79         $ADD .= ") > 0";
80
81         // SQL string to check for accounts
82         $result_main = SQL_QUERY_ESC("SELECT userid, email, (turbo_bonus + login_bonus + bonus_order + bonus_stats + bonus_ref) AS active_bonus
83 FROM "._MYSQL_PREFIX."_user_data
84 ".$whereStatement1." %s".$ADD."
85 ORDER BY active_bonus DESC, userid LIMIT %s",
86  array($whereStatement2, $whereStatement3), __FILE__, __LINE__);
87
88         if (SQL_NUMROWS($result_main) > 0)
89         {
90                 // Load our winners...
91                 $UIDs = "";
92                 while(list($uid, $email, $points) = SQL_FETCHROW($result_main))
93                 {
94                         // Add userids in a row
95                         $UIDs .= ",'".$uid."'";
96
97                         // Add points to user's account directly
98                         $result_data = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points
99 SET points=points+%s WHERE ref_depth=0 AND userid=%d LIMIT 1",
100  array($points, bigintval($uid)), __FILE__, __LINE__);
101
102                         // Update mediadata as well
103                         if (GET_EXT_VERSION("mediadata") >= "0.0.4")
104                         {
105                                 // Update database
106                                 MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);
107                         }
108
109                         // Load email template and email it away
110                         $msg = LOAD_EMAIL_TEMPLATE("member_bonus", $points, bigintval($uid));
111                         SEND_EMAIL($email, BONUS_MONTHLY_ONLINE_BONUS, $msg);
112                 }
113                 // Remove first commata
114                 $UIDs = substr($UIDs, 1);
115
116                 // Get current month
117                 $curr = date("m", time());
118                 if (strlen($curr) == 1) $curr = "0".$curr;
119                 if ($curr == "00") $curr = "12";
120
121                 // Reset accounts
122                 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_user_data
123 SET turbo_bonus=0, login_bonus=0, bonus_order=0, bonus_stats=0, bonus_ref=0", __FILE__, __LINE__);
124
125         }
126
127         // Free memory
128         SQL_FREERESULT($result_main);
129
130         // Finally update database and config array
131         $_CONFIG['bonus_month'] = $curr;
132         $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET last_bonus_month='%s' WHERE config=0 LIMIT 1",
133          array($curr), __FILE__, __LINE__);
134
135         // Destroy cache
136 }
137 //
138 ?>