Code style changed, ext-user continued:
[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 exclusion list
55         // @TODO Rewrite these if() blocks to a filter
56         $excludeUserids = '';
57         if (isValidUserId(getDefRefid())) $excludeUserids .= ' AND `userid` != {?def_refid?}';
58
59         // Check for more extensions
60         if (isExtensionActive('beg'))     $excludeUserids .= ' AND `userid` != {?beg_userid?}';
61         if (isExtensionActive('bonus'))   $excludeUserids .= ' AND `userid` != {?bonus_userid?}';
62         if (isExtensionActive('doubler')) $excludeUserids .= ' AND `userid` != {?doubler_userid?}';
63
64         // Check for new holiday extension
65         if (isExtensionInstalledAndNewer('holiday', '0.1.3')) {
66                 // Include only users with no active holiday
67                 $excludeUserids .= " AND `holiday_active`='N'";
68         } // END - if
69
70         // Check for all accounts
71         $result_inactive = SQL_QUERY("SELECT
72         `userid`,
73         `email`,
74         `last_online`
75 FROM
76         `{?_MYSQL_PREFIX?}_user_data`
77 WHERE
78         `status`='CONFIRMED'
79         " . runFilterChain('user_exclusion_sql', ' ') . " AND
80         (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
81         (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
82         (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_since?}
83         " . $excludeUserids . "
84 ORDER BY
85         `userid` ASC", __FILE__, __LINE__);
86
87         if (!SQL_HASZERONUMS($result_inactive)) {
88                 // Prepare variables and constants...
89                 $userids = array();
90                 $content['since'] = (getApInactiveSince() / 60 / 60);
91                 $content['time']  = (getApInactiveTime()  / 60 / 60);
92
93                 // Mark found accounts as inactive and send an email
94                 while ($row = SQL_FETCHARRAY($result_inactive)) {
95                         // Merge both arrays
96                         $content = merge_array($content, $row);
97
98                         // Remember userids for the admin
99                         array_push($userids, $content['userid']);
100
101                         // Get date/time from timestamp
102                         $content['last_online'] = generateDateTime($content['last_online'], 0);
103
104                         // Load mail template
105                         $message = loadEmailTemplate('member_autopurge_inactive', $content, bigintval($content['userid']));
106                         sendEmail($content['userid'], '{--MEMBER_AUTOPURGE_INACTIVE_SUBJECT--}', $message);
107
108                         // Update this account
109                         addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ap_notified`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
110                                 array(bigintval($content['userid'])), __FILE__, __LINE__, FALSE));
111                 } // END - while
112
113                 // Remove last comma
114                 $useridsContent = implode(chr(10), $userids);
115
116                 // Send mail notification to admin
117                 sendAdminNotification('{--ADMIN_AUTOPURGE_INACTIVE_SUBJECT--}', 'admin_autopurge_inactive', $useridsContent);
118         } // END - if
119
120         // Free memory
121         SQL_FREERESULT($result_inactive);
122
123         // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
124         // here for e.g. excluding holiday users
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', ' ') . " 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 " . $excludeUserids . "
139 ORDER BY
140         `userid` ASC", __FILE__, __LINE__);
141
142         if (!SQL_HASZERONUMS($result_inactive)) {
143                 // Prepare userid array for admin...
144                 $userids = array();
145
146                 // Delete inactive accounts
147                 while ($content = SQL_FETCHARRAY($result_inactive)) {
148                         // Remember userids for the admin
149                         array_push($userids, $content['userid']);
150
151                         // Get date/time from timestamp
152                         $content['last_online'] = generateDateTime($content['last_online'], 0);
153
154                         // Finnaly delete this inactive account
155                         deleteUserAccount($content['userid'], loadEmailTemplate('member_autopurge_delete', $content['last_online'], ''));
156                 } // END - while
157
158                 // Display all userids
159                 $useridsContent = implode(chr(10), $userids);
160
161                 // Send mail notification to admin
162                 if (getConfig('ap_in_notify') == 'Y') {
163                         sendAdminNotification('{--ADMIN_AUTOPURGE_DELETE_SUBJECT--}', 'admin_autopurge_delete', $useridsContent);
164                 } // END - if
165         } // END - if
166
167         // Free memory
168         SQL_FREERESULT($result_inactive);
169
170         // Run all SQLs
171         runFilterChain('run_sqls');
172 } // END - if
173
174 // [EOF]
175 ?>