Security line in all includes changed
[mailer.git] / inc / autopurge / purge-inact.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 09/14/2008 *
4  * ===============                              Last change: 09/14/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : purge-inactive.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Purge of inactive users                          *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auto-Loeschung von inaktiven Mitgliedern         *
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 } // END - if
39
40 // Abort if autopurge is not active or disabled by admin
41 if ((!EXT_IS_ACTIVE("autopurge")) || ($_CONFIG['auto_purge_active'] == "N")) {
42         // Abort here
43         return false;
44 } // END - if
45
46 // Shall I look for inactive accounts and autopurge inactive accounts?
47 if ($_CONFIG['ap_inactive'] == "Y") {
48         // Ok, let's have a look...
49         $since = bigintval($_CONFIG['ap_in_since']);
50         $EXCLUDE_LIST = " AND d.userid != c.def_refid";
51
52         // Check for more extensions
53         if (EXT_IS_ACTIVE("beg"))     $EXCLUDE_LIST .= " AND d.userid != c.beg_uid";
54         if (EXT_IS_ACTIVE("bonus"))   $EXCLUDE_LIST .= " AND d.userid != c.bonus_uid";
55         if (EXT_IS_ACTIVE("doubler")) $EXCLUDE_LIST .= " AND d.userid != c.doubler_uid";
56
57         // Check for new holiday extension
58         if (GET_EXT_VERSION("holiday") >= "0.1.3") {
59                 // Include only users with no active holiday
60                 $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";
61         } // END - if
62
63         // Check for all accounts
64         $result_inactive = SQL_QUERY_ESC("SELECT DISTINCT d.userid, d.email, d.last_online
65 FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
66 WHERE d.status='CONFIRMED' AND d.joined < (UNIX_TIMESTAMP() - %s) AND d.last_online < (UNIX_TIMESTAMP() - %s) AND d.ap_notified < (UNIX_TIMESTAMP() - %s)
67 ".$EXCLUDE_LIST."
68 ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
69
70         if (SQL_NUMROWS($result_inactive) > 0) {
71                 // Prepare variables and constants...
72                 $UIDs = "";
73                 define('__INACTIVE_SINCE', ($_CONFIG['ap_in_since'] / 60 / 60));
74                 define('__INACTIVE_TIME' , ($_CONFIG['ap_in_time']  / 60 / 60));
75
76                 // Mark found accounts as inactive and send an email
77                 while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive)) {
78                         // Remember userids for the admin
79                         $UIDs .= $uid.", ";
80
81                         // Get date/time from timestamp
82                         $last = MAKE_DATETIME($last, "0");
83
84                         // Load mail template
85                         $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));
86                         SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);
87
88                         // Update this account
89                         $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET ap_notified=UNIX_TIMESTAMP() WHERE userid=%s LIMIT 1",
90                          array(bigintval($uid)), __FILE__, __LINE__);
91                 } // END - while
92
93                 // Remove last comma
94                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
95
96                 // Send mail notification to admin
97                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");
98         } // END - if
99
100         // Free memory
101         SQL_FREERESULT($result_inactive);
102
103         // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
104         // here for e.g. excluding holiday users
105         $time = bigintval($_CONFIG['ap_in_time']);
106         $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
107 FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
108 WHERE status='CONFIRMED' AND joined < (UNIX_TIMESTAMP() - %s) AND last_online < (UNIX_TIMESTAMP() - %s) AND ap_notified < (UNIX_TIMESTAMP() - %s)
109 ".$EXCLUDE_LIST."
110 ORDER BY userid",
111          array($since, $since, $time), __FILE__, __LINE__);
112         if (SQL_NUMROWS($result_inactive) > 0) {
113                 // Prepare variable...
114                 $UIDs = "";
115
116                 // Delete inactive accounts
117                 while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive)) {
118                         // Remember userids for the admin
119                         $UIDs .= $uid.", ";
120
121                         // Get date/time from timestamp
122                         $last = MAKE_DATETIME($last, "0");
123
124                         // Finnaly delete this inactive account
125                         DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));
126                 } // END - while
127
128                 // Remove last comma
129                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
130
131                 // Send mail notification to admin
132                 if ($_CONFIG['ap_un_mail'] == "Y") {
133                         SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");
134                 } // END - if
135         } // END - if
136
137         // Free memory
138         SQL_FREERESULT($result_inactive);
139 } // END - if
140
141 //
142 ?>