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