Rewrites to use filters instead of mass if() blocks
[mailer.git] / inc / purge / 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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Abort if autopurge is not active or disabled by admin
44 if ((!isExtensionActive('autopurge')) || (!isAutoPurgingActive())) {
45         // Abort here
46         return FALSE;
47 } // END - if
48
49 // Shall I look for inactive accounts and autopurge inactive accounts?
50 if (isAutopurgeInactiveEnabled()) {
51         // Init SQLs
52         initSqls();
53
54         // Init userid exclusion
55         $excludeSql = '';
56         $excludedUserids = runFilterChain('config_userid_exclusion_sql', array());
57         if (count($excludedUserids) > 0) {
58                 // Exclude all
59                 $excludeSql = ' AND `userid` NOT IN (' . implode(', ', $excludedUserids) . ')';
60         } // END - if
61
62         // Check for new holiday extension
63         if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
64                 // Include only users with no active holiday
65                 $excludeSql .= " AND `holiday_active`='N'";
66         } // END - if
67
68         // Check for all accounts
69         $result_inactive = SQL_QUERY("SELECT
70         `userid`,
71         `email`,
72         `last_online`
73 FROM
74         `{?_MYSQL_PREFIX?}_user_data`
75 WHERE
76         `status`='CONFIRMED' AND
77         (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
78         (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
79         (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_since?} AND
80         " . runFilterChain('user_exclusion_sql', $excludeSql) . "
81 ORDER BY
82         `userid` ASC", __FILE__, __LINE__);
83
84         if (!SQL_HASZERONUMS($result_inactive)) {
85                 // Prepare variables and constants...
86                 $userids = array();
87                 $content['since'] = (getApInactiveSince() / 60 / 60);
88                 $content['time']  = (getApInactiveTime()  / 60 / 60);
89
90                 // Mark found accounts as inactive and send an email
91                 while ($row = SQL_FETCHARRAY($result_inactive)) {
92                         // Merge both arrays
93                         $content = merge_array($content, $row);
94
95                         // Remember userids for the admin
96                         array_push($userids, $content['userid']);
97
98                         // Get date/time from timestamp
99                         $content['last_online'] = generateDateTime($content['last_online'], 0);
100
101                         // Load mail template
102                         $message = loadEmailTemplate('member_autopurge_inactive', $content, bigintval($content['userid']));
103                         sendEmail($content['userid'], '{--MEMBER_AUTOPURGE_INACTIVE_SUBJECT--}', $message);
104
105                         // Update this account
106                         addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ap_notified`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
107                                 array(bigintval($content['userid'])), __FILE__, __LINE__, FALSE));
108                 } // END - while
109
110                 // Remove last comma
111                 $content['userids'] = implode(PHP_EOL, $userids);
112
113                 // Send mail notification to admin
114                 sendAdminNotification('{--ADMIN_AUTOPURGE_INACTIVE_SUBJECT--}', 'admin_purge_inactive', $content);
115         } // END - if
116
117         // Free memory
118         SQL_FREERESULT($result_inactive);
119
120         /*
121          * Now let's have a look for inactive accounts for deletion. Use the newly
122          * added exclude list here for e.g. excluding holiday users, test users et
123          * cetera.
124          */
125         $result_inactive = SQL_QUERY("SELECT
126         `userid`,
127         `email`,
128         `last_online`
129 FROM
130         `{?_MYSQL_PREFIX?}_user_data`
131 WHERE
132         `status`='CONFIRMED'
133         " . runFilterChain('user_exclusion_sql', $excludeSql) . " AND
134         (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
135         (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
136         (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_time?} AND
137         `ap_notified` > 0
138 ORDER BY
139         `userid` ASC", __FILE__, __LINE__);
140
141         if (!SQL_HASZERONUMS($result_inactive)) {
142                 // Prepare userid array for admin...
143                 $userids = array();
144
145                 // Delete inactive accounts
146                 while ($content = SQL_FETCHARRAY($result_inactive)) {
147                         // Remember userids for the admin
148                         array_push($userids, $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                 // Display all userids
158                 $useridsContent = implode(PHP_EOL, $userids);
159
160                 // Send mail notification to admin
161                 if (getConfig('ap_in_notify') == 'Y') {
162                         sendAdminNotification('{--ADMIN_AUTOPURGE_DELETE_SUBJECT--}', 'admin_purge_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 ?>