a7c002584bd1d0921c9f2bd34cf7898766043fd8
[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 (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } elseif (!EXT_IS_ACTIVE("bonus")) {
39         return;
40 }
41
42 // Do not execute when script is in CSS mode or no daily reset
43 if (($CSS == 1) || (!defined('__DAILY_RESET'))) return;
44
45 // Get current month (2 digits)
46 $curr = date("m", time());
47
48 if (($curr != $_CONFIG['last_month']) && ($_CONFIG['bonus_ranks'] > 0) && ($CSS != 1))
49 {
50         // Extension "autopurge" is inactive or purging of inactive accounts is deactivated
51         $whereStatement1 = "WHERE status='CONFIRMED'";
52         $whereStatement2 = bigintval($_CONFIG['bonus_ranks']);
53
54         // Shall I keep inactive members away from here? (mostly wanted in an "active-rallye" ...)
55         if (EXT_IS_ACTIVE("autopurge"))
56         {
57                 // Use last online stamp only when autopurge for inactive members is activated
58                 if ($_CONFIG['ap_inactive_since'] > 0)
59                 {
60                         // Okay, include last online timestamp
61                         $whereStatement1 = sprintf("WHERE status='CONFIRMED' AND last_online >= (UNIX_TIMESTAMP() - %s)", $_CONFIG['ap_inactive_since']);
62                         $whereStatement2 = bigintval($_CONFIG['bonus_ranks']);
63                 }
64         }
65
66         // Add more bonus points here
67         $ADD = "";
68         if ($_CONFIG['bonus_click_yn'] == "Y") $ADD .= " + turbo_bonus";
69         if ($_CONFIG['bonus_login_yn'] == "Y") $ADD .= " + login_bonus";
70         if ($_CONFIG['bonus_order_yn'] == "Y") $ADD .= " + bonus_order";
71         if ($_CONFIG['bonus_stats_yn'] == "Y") $ADD .= " + bonus_stats";
72         if ($_CONFIG['bonus_ref_yn']   == "Y") $ADD .= " + bonus_ref";
73
74         if (!empty($ADD)) {
75                 $ADD .= " AND (0".$ADD.") > 0";
76         } // END - if
77
78         // SQL string to check for accounts
79         $result_main = SQL_QUERY_ESC("SELECT userid, email, (turbo_bonus + login_bonus + bonus_order + bonus_stats + bonus_ref) AS active_bonus
80 FROM "._MYSQL_PREFIX."_user_data
81 ".$whereStatement1."".$ADD."
82 ORDER BY active_bonus DESC, userid LIMIT %s",
83  array($whereStatement2), __FILE__, __LINE__);
84
85         if (SQL_NUMROWS($result_main) > 0)
86         {
87                 // Load our winners...
88                 $UIDs = "";
89                 while(list($uid, $email, $points) = SQL_FETCHROW($result_main))
90                 {
91                         // Add userids in a row
92                         $UIDs .= ",'".$uid."'";
93
94                         // Add points to user's account directly
95                         ADD_POINTS_REFSYSTEM($uid, $points, false, "0", false, "direct");
96
97                         // Load email template and email it away
98                         $msg = LOAD_EMAIL_TEMPLATE("member_bonus", $points, bigintval($uid));
99                         SEND_EMAIL($email, BONUS_MONTHLY_ONLINE_BONUS, $msg);
100                 }
101
102                 // Remove first commata
103                 $UIDs = substr($UIDs, 1);
104
105                 // Get current month
106                 $curr = date("m", time());
107                 if (strlen($curr) == 1) $curr = "0".$curr;
108                 if ($curr == "00") $curr = "12";
109
110                 // Reset accounts
111                 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_user_data
112 SET turbo_bonus=0, login_bonus=0, bonus_order=0, bonus_stats=0, bonus_ref=0", __FILE__, __LINE__);
113         }
114
115         // Free memory
116         SQL_FREERESULT($result_main);
117 }
118
119 //
120 ?>