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