c944d040425933ba4b24c50725fa21d4d96aef2b
[mailer.git] / inc / monthly / monthly_beg.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/22/2005 *
4  * ===============                              Last change: 10/22/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : monthly_beg.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Monthly begging rallye                           *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Monatliche Bettelrallye                          *
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("beg")) && (!IS_ADMIN()))
41 {
42         ADD_FATAL(EXTENSION_PROBLEM_EXT_INACTIVE, "beg");
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 // Check if month is done
53 if (($curr != $CONFIG['beg_month']) && ($CONFIG['beg_month'] > 0) && ($CONFIG['beg_ranks'] > 0) && ($CSS != 1))
54 {
55         // Extension "autopurge" is inactive or purging of inactive accounts is deactivated
56         $WHERE1 = "";
57         $WHERE2 = 0;
58         $WHERE3 = bigintval($CONFIG['beg_ranks']);
59
60         // Let's check if there are some points left we can "pay"...
61         if (EXT_IS_ACTIVE("autopurge"))
62         {
63                 // Use last online stamp only when autopurge for inactive members is activated
64                 if (($CONFIG['ap_in_since'] > 0) && ($CONFIG['beg_active'] == 'Y'))
65                 {
66                         // Okay, include last online timestamp
67                         $WHERE1 = "AND last_online >=";
68                         $WHERE2 = bigintval(time() - $CONFIG['ap_in_since']);
69                         $WHERE3 = bigintval($CONFIG['beg_ranks']);
70                 }
71         }
72
73         // SQL string to check for accounts
74         $result_main = SQL_QUERY_ESC("SELECT userid, email, beg_points
75 FROM "._MYSQL_PREFIX."_user_data WHERE status='CONFIRMED' ".$WHERE1." %s AND beg_points > 0 ORDER BY beg_points DESC, userid LIMIT %s",
76  array($WHERE2, $WHERE3), __FILE__, __LINE__);
77
78         if (SQL_NUMROWS($result_main) > 0)
79         {
80                 // Load our winners...
81                 $UIDs = "";
82                 while(list($uid, $email, $points) = SQL_FETCHROW($result_main))
83                 {
84                         // Add points to user's account directly
85                         $result_data = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_points
86 SET points=points+%s WHERE ref_depth='0' AND userid=%d LIMIT 1",
87  array($points, bigintval($uid)), __FILE__, __LINE__);
88
89                         // Update mediadata as well
90                         if (GET_EXT_VERSION("mediadata") >= "0.0.4")
91                         {
92                                 // Update database
93                                 MEDIA_UPDATE_ENTRY(array("total_points"), "add", $points);
94                         }
95
96                         // Load email template and email it away
97                         $msg = LOAD_EMAIL_TEMPLATE("member_beg", $points, bigintval($uid));
98                         SEND_EMAIL($email, BEG_MONTHLY_RALLYE, $msg);
99                 }
100
101                 // Remove first commata
102                 $UIDs = substr($UIDs, 1);
103
104                 // Get current month
105                 $curr = date("m", time());
106                 if (strlen($curr) == 1) $curr = "0".$curr;
107                 if ($curr == "00") $curr = "12";
108
109                 // Reset accounts
110                 $result = SQL_QUERY("UPDATE "._MYSQL_PREFIX."_user_data SET beg_points=0.00000 WHERE beg_points > 0",
111                  __FILE__, __LINE__);
112         }
113
114         // Free memory
115         SQL_FREERESULT($result_main);
116
117         // Finally update database and config array
118         $CONFIG['beg_month'] = $curr;
119         $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_config SET beg_month='%s' WHERE config='0' LIMIT 1",
120          array($curr), __FILE__, __LINE__);
121
122         // Destroy cache
123 }
124 //
125 ?>