]> git.mxchange.org Git - mailer.git/blob - inc/purge/purge-inact.php
Some fixes:
[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 - 2013 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 (isFilledArray($excludedUserids)) {
58                 // Exclude all
59                 $excludeSql = ' AND `userid` NOT IN (' . implode(', ', $excludedUserids) . ')';
60         } // END - if
61
62         // Check for all accounts
63         $result_inactive = sqlQuery("SELECT
64         `userid`,
65         `email`,
66         `last_online`
67 FROM
68         `{?_MYSQL_PREFIX?}_user_data`
69 WHERE
70         `status`='CONFIRMED' AND
71         (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
72         (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
73         (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_since?}
74         " . runFilterChain('user_exclusion_sql', ' ' . $excludeSql) . "
75 ORDER BY
76         `userid` ASC", __FILE__, __LINE__);
77
78         if (!ifSqlHasZeroNums($result_inactive)) {
79                 // Prepare variables and constants...
80                 $userids = array();
81                 $content['since'] = (getApInactiveSince() / 60 / 60);
82                 $content['time']  = (getApInactiveTime()  / 60 / 60);
83
84                 // Mark found accounts as inactive and send an email
85                 while ($row = sqlFetchArray($result_inactive)) {
86                         // Merge both arrays
87                         $content = merge_array($content, $row);
88
89                         // Remember userids for the admin
90                         array_push($userids, $content['userid']);
91
92                         // Get date/time from timestamp
93                         $content['last_online'] = generateDateTime($content['last_online'], 0);
94
95                         // Load mail template
96                         $message = loadEmailTemplate('member_autopurge_inactive', $content, bigintval($content['userid']));
97                         sendEmail($content['userid'], '{--MEMBER_AUTOPURGE_INACTIVE_SUBJECT--}', $message);
98
99                         // Update this account
100                         addSql(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `ap_notified`=UNIX_TIMESTAMP() WHERE `userid`=%s LIMIT 1",
101                                 array(bigintval($content['userid'])), __FILE__, __LINE__, FALSE));
102                 } // END - while
103
104                 // Remove last comma
105                 $content['userids'] = implode(PHP_EOL, $userids);
106
107                 // Send mail notification to admin
108                 sendAdminNotification('{--ADMIN_AUTOPURGE_INACTIVE_SUBJECT--}', 'admin_purge_inactive', $content);
109         } // END - if
110
111         // Free memory
112         sqlFreeResult($result_inactive);
113
114         /*
115          * Now let's have a look for inactive accounts for deletion. Use the newly
116          * added exclude list here for e.g. excluding holiday users, test users et
117          * cetera.
118          */
119         $result_inactive = sqlQuery("SELECT
120         `userid`,
121         `email`,
122         `last_online`
123 FROM
124         `{?_MYSQL_PREFIX?}_user_data`
125 WHERE
126         `status`='CONFIRMED'
127         " . runFilterChain('user_exclusion_sql', ' ' . $excludeSql) . " AND
128         (UNIX_TIMESTAMP() - `joined`) >= {?ap_inactive_since?} AND
129         (UNIX_TIMESTAMP() - `last_online`) >= {?ap_inactive_since?} AND
130         (UNIX_TIMESTAMP() - `ap_notified`) >= {?ap_inactive_time?} AND
131         `ap_notified` > 0
132 ORDER BY
133         `userid` ASC", __FILE__, __LINE__);
134
135         if (!ifSqlHasZeroNums($result_inactive)) {
136                 // Prepare userid array for admin...
137                 $userids = array();
138
139                 // Delete inactive accounts
140                 while ($content = sqlFetchArray($result_inactive)) {
141                         // Remember userids for the admin
142                         array_push($userids, $content['userid']);
143
144                         // Get date/time from timestamp
145                         $content['last_online'] = generateDateTime($content['last_online'], 0);
146
147                         // Finnaly delete this inactive account
148                         deleteUserAccount($content['userid'], loadEmailTemplate('member_autopurge_delete', $content['last_online'], ''));
149                 } // END - while
150
151                 // Display all userids
152                 $useridsContent = implode(PHP_EOL, $userids);
153
154                 // Send mail notification to admin
155                 if (getConfig('ap_in_notify') == 'Y') {
156                         sendAdminNotification('{--ADMIN_AUTOPURGE_DELETE_SUBJECT--}', 'admin_purge_delete', $useridsContent);
157                 } // END - if
158         } // END - if
159
160         // Free memory
161         sqlFreeResult($result_inactive);
162
163         // Run all SQLs
164         runFilterChain('run_sqls');
165 } // END - if
166
167 // [EOF]
168 ?>