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