A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[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")) || (getConfig('auto_purge_active') != "Y")) {
42         // Abort here
43         return false;
44 } // END - if
45
46 // Shall I look for inactive accounts and autopurge inactive accounts?
47 if (getConfig('autopurge_inactive') == "Y") {
48         // Init SQLs
49         INIT_SQLS();
50
51         // Ok, let's have a look...
52         $since = getConfig(('ap_inactive_since'));
53
54         // Init exclusion list
55         $EXCLUDE_LIST = "";
56         if (getConfig('def_refid') > 0) {
57                 $EXCLUDE_LIST = " AND d.userid != ".getConfig('def_refid');
58         } // END - if
59
60         // Check for more extensions
61         if (EXT_IS_ACTIVE("beg"))     $EXCLUDE_LIST .= " AND d.userid != ".getConfig('beg_uid')."";
62         if (EXT_IS_ACTIVE("bonus"))   $EXCLUDE_LIST .= " AND d.userid != ".getConfig('bonus_uid')."";
63         if (EXT_IS_ACTIVE("doubler")) $EXCLUDE_LIST .= " AND d.userid != ".getConfig('doubler_uid')."";
64
65         // Check for new holiday extension
66         if (GET_EXT_VERSION("holiday") >= "0.1.3") {
67                 // Include only users with no active holiday
68                 $EXCLUDE_LIST .= " AND d.holiday_active='N'";
69         } // END - if
70
71         // Check for all accounts
72         $result_inactive = SQL_QUERY_ESC("SELECT DISTINCT d.userid, d.email, d.last_online
73 FROM `{!_MYSQL_PREFIX!}_user_data` AS d
74 WHERE d.`status`='CONFIRMED' AND d.joined < (UNIX_TIMESTAMP() - %s) AND d.last_online < (UNIX_TIMESTAMP() - %s) AND d.ap_notified < (UNIX_TIMESTAMP() - %s)
75 ".$EXCLUDE_LIST."
76 ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
77
78         if (SQL_NUMROWS($result_inactive) > 0) {
79                 // Prepare variables and constants...
80                 $UIDs = "";
81                 define('__INACTIVE_SINCE', (getConfig('ap_inactive_since') / 60 / 60));
82                 define('__INACTIVE_TIME' , (getConfig('ap_in_time')  / 60 / 60));
83
84                 // Mark found accounts as inactive and send an email
85                 while ($content = SQL_FETCHARRAY($result_inactive)) {
86                         // Remember userids for the admin
87                         $UIDs .= $content['userid'].", ";
88
89                         // Get date/time from timestamp
90                         $content['last_online'] = MAKE_DATETIME($content['last_online'], "0");
91
92                         // Load mail template
93                         $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $content['last_online'], bigintval($content['userid']));
94                         SEND_EMAIL($content['email'], getMessage('AUTOPURGE_MEMBER_INACTIVE_SUBJECT'), $msg);
95
96                         // Update this account
97                         ADD_SQL(SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET ap_notified=UNIX_TIMESTAMP() WHERE userid=%s LIMIT 1",
98                                 array(bigintval($content['userid'])), __FILE__, __LINE__, false));
99                 } // END - while
100
101                 // Remove last comma
102                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
103
104                 // Send mail notification to admin
105                 SEND_ADMIN_NOTIFICATION(getMessage('AUTOPURGE_ADMIN_INACTIVE_SUBJECT'), "admin_autopurge_inactive", $UIDs, "");
106         } // END - if
107
108         // Free memory
109         SQL_FREERESULT($result_inactive);
110
111         // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
112         // here for e.g. excluding holiday users
113         $time = getConfig(('ap_in_time'));
114         $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
115 FROM `{!_MYSQL_PREFIX!}_user_data` AS d
116 WHERE `status`='CONFIRMED' AND joined < (UNIX_TIMESTAMP() - %s) AND last_online < (UNIX_TIMESTAMP() - %s) AND ap_notified < (UNIX_TIMESTAMP() - %s)
117 ".$EXCLUDE_LIST."
118 ORDER BY userid",
119                 array($since, $since, $time), __FILE__, __LINE__);
120         if (SQL_NUMROWS($result_inactive) > 0) {
121                 // Prepare variable...
122                 $UIDs = "";
123
124                 // Delete inactive accounts
125                 while ($content = SQL_FETCHARRAY($result_inactive)) {
126                         // Remember userids for the admin
127                         $UIDs .= $content['userid'].", ";
128
129                         // Get date/time from timestamp
130                         $content['last_online'] = MAKE_DATETIME($content['last_online'], "0");
131
132                         // Finnaly delete this inactive account
133                         DELETE_USER_ACCOUNT($content['userid'], LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $content['last_online'], ""));
134                 } // END - while
135
136                 // Remove last comma
137                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
138
139                 // Send mail notification to admin
140                 if (getConfig('ap_in_notify') == "Y") {
141                         SEND_ADMIN_NOTIFICATION(getMessage('AUTOPURGE_ADMIN_DELETE_SUBJECT'), "admin_autopurge_delete", $UIDs, "");
142                 } // END - if
143         } // END - if
144
145         // Free memory
146         SQL_FREERESULT($result_inactive);
147
148         // Run all SQLs
149         runFilterChain('run_sqls');
150 } // END - if
151
152 //
153 ?>