ae1d8907b5bcc9bc898b9959f12e79cf6fb9c4b3
[mailer.git] / inc / autopurge / purge-inact.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Abort if autopurge is not active or disabled by admin
46 if ((!isExtensionActive('autopurge')) || (getConfig('auto_purge_active') != 'Y')) {
47         // Abort here
48         return false;
49 } // END - if
50
51 // Shall I look for inactive accounts and autopurge inactive accounts?
52 if (getConfig('autopurge_inactive') == 'Y') {
53         // Init SQLs
54         initSqls();
55
56         // Init exclusion list
57         // @TODO Rewrite these if() blocks to a filter
58         $EXCLUDE_LIST = '';
59         if (getConfig('def_refid') > 0) {
60                 $EXCLUDE_LIST = " AND d.userid != ".getConfig('def_refid');
61         } // END - if
62
63         // Check for more extensions
64         if (isExtensionActive('beg'))     $EXCLUDE_LIST .= " AND d.userid != ".getConfig('beg_userid')."";
65         if (isExtensionActive('bonus'))   $EXCLUDE_LIST .= " AND d.userid != ".getConfig('bonus_userid')."";
66         if (isExtensionActive('doubler')) $EXCLUDE_LIST .= " AND d.userid != ".getConfig('doubler_userid')."";
67
68         // Check for new holiday extension
69         if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
70                 // Include only users with no active holiday
71                 $EXCLUDE_LIST .= " AND d.`holiday_active`='N'";
72         } // END - if
73
74         // Check for all accounts
75         $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
76 FROM
77         `{?_MYSQL_PREFIX?}_user_data` AS d
78 WHERE
79         d.status='CONFIRMED' AND
80         d.joined < (UNIX_TIMESTAMP() - %s) AND
81         d.last_online < (UNIX_TIMESTAMP() - %s) AND
82         d.ap_notified < (UNIX_TIMESTAMP() - %s)
83         ".$EXCLUDE_LIST."
84 ORDER BY
85         d.userid ASC",
86                 array(
87                         getConfig('ap_inactive_since'),
88                         getConfig('ap_inactive_since'),
89                         getConfig('ap_inactive_since')
90                 ), __FILE__, __LINE__);
91
92         if (SQL_NUMROWS($result_inactive) > 0) {
93                 // Prepare variables and constants...
94                 $useridsContent = '';
95                 $content['since'] = (getConfig('ap_inactive_since') / 60 / 60);
96                 $content['time']  = (getConfig('ap_inactive_time')  / 60 / 60);
97
98                 // Mark found accounts as inactive and send an email
99                 while ($row = SQL_FETCHARRAY($result_inactive)) {
100                         // Merge both arrays
101                         $content = merge_array($content, $row);
102
103                         // Remember userids for the admin
104                         $useridsContent .= $content['userid'].", ";
105
106                         // Get date/time from timestamp
107                         $content['last_online'] = generateDateTime($content['last_online'], 0);
108
109                         // Load mail template
110                         $message = loadEmailTemplate('member_autopurge_inactive', $content, bigintval($content['userid']));
111                         sendEmail($content['email'], getMessage('AUTOPURGE_MEMBER_INACTIVE_SUBJECT'), $message);
112
113                         // Update this account
114                         addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ap_notified`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
115                                 array(bigintval($content['userid'])), __FILE__, __LINE__, false));
116                 } // END - while
117
118                 // Remove last comma
119                 $useridsContent = str_replace(', ', "\n", substr($useridsContent, 0, -2));
120
121                 // Send mail notification to admin
122                 sendAdminNotification(getMessage('AUTOPURGE_ADMIN_INACTIVE_SUBJECT'), 'admin_autopurge_inactive', $useridsContent);
123         } // END - if
124
125         // Free memory
126         SQL_FREERESULT($result_inactive);
127
128         // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
129         // here for e.g. excluding holiday users
130         $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
131 FROM `{?_MYSQL_PREFIX?}_user_data` AS d
132 WHERE `status`='CONFIRMED' AND `joined` < (UNIX_TIMESTAMP() - %s) AND `last_online` < (UNIX_TIMESTAMP() - %s) AND `ap_notified` < (UNIX_TIMESTAMP() - %s)
133 ".$EXCLUDE_LIST."
134 ORDER BY `userid` ASC",
135                 array(
136                         getConfig('ap_inactive_since'),
137                         getConfig('ap_inactive_since'),
138                         getConfig('ap_inactive_time')
139                 ), __FILE__, __LINE__);
140
141         if (SQL_NUMROWS($result_inactive) > 0) {
142                 // Prepare variable...
143                 $useridsContent = '';
144
145                 // Delete inactive accounts
146                 while ($content = SQL_FETCHARRAY($result_inactive)) {
147                         // Remember userids for the admin
148                         $useridsContent .= $content['userid'] . ', ';
149
150                         // Get date/time from timestamp
151                         $content['last_online'] = generateDateTime($content['last_online'], 0);
152
153                         // Finnaly delete this inactive account
154                         deleteUserAccount($content['userid'], loadEmailTemplate('member_autopurge_delete', $content['last_online'], ''));
155                 } // END - while
156
157                 // Remove last comma
158                 $useridsContent = str_replace(', ', "\n", substr($useridsContent, 0, -2));
159
160                 // Send mail notification to admin
161                 if (getConfig('ap_in_notify') == 'Y') {
162                         sendAdminNotification(getMessage('AUTOPURGE_ADMIN_DELETE_SUBJECT'), 'admin_autopurge_delete', $useridsContent);
163                 } // END - if
164         } // END - if
165
166         // Free memory
167         SQL_FREERESULT($result_inactive);
168
169         // Run all SQLs
170         runFilterChain('run_sqls');
171 } // END - if
172
173 // [EOF]
174 ?>