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