Filters for configurable userid exclusion added:
[mailer.git] / inc / purge / purge-general.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-general.php                                *
8  * -------------------------------------------------------------------- *
9  * Short description : General autopurging, nothing extension-specific  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Allgemeine, nicht erweiterunsabhaengige Auto-    *
12  *                     Loeschung                                        *
13  * -------------------------------------------------------------------- *
14  * $Revision::                                                        $ *
15  * $Date::                                                            $ *
16  * $Tag:: 0.2.1-FINAL                                                 $ *
17  * $Author::                                                          $ *
18  * -------------------------------------------------------------------- *
19  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
20  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
21  * For more information visit: http://mxchange.org                      *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } elseif ((!isExtensionActive('autopurge')) || (!isAutoPurgingActive())) {
43         // Abort here
44         return FALSE;
45 }
46
47 if (getAutoPurge() > 0) {
48         // Init SQLs
49         initSqls();
50
51         // Init variables
52         $admin_points = '0';
53
54         // Then check for outdated mail order. We don't delete them just the confirmation links will be deleted.
55         $result = SQL_QUERY('SELECT
56         `s`.`id`,
57         `s`.`userid`,
58         `s`.`pool_id`,
59         `t`.`price`
60 FROM
61         `{?_MYSQL_PREFIX?}_user_stats` AS `s`
62 LEFT JOIN
63         `{?_MYSQL_PREFIX?}_payments` AS `t`
64 ON
65         `s`.`payment_id`=`t`.`id`
66 WHERE
67         (UNIX_TIMESTAMP() - `s`.`timestamp_ordered`) >= {?auto_purge?}
68 ORDER BY
69         `s`.`userid` ASC', __FILE__, __LINE__);
70         if (!SQL_HASZERONUMS($result)) {
71                 // Start deleting procedure
72                 $userid = NULL;
73                 $points = '0';
74                 while ($content = SQL_FETCHARRAY($result)) {
75                         // Check if confirmation links are purged or not
76                         $result_links = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s LIMIT 1",
77                                 array(bigintval($content['id'])), __FILE__, __LINE__);
78                         if (SQL_NUMROWS($result_links) == 1) {
79                                 // Free memory
80                                 SQL_FREERESULT($result_links);
81
82                                 // At least one link was found, enougth to pay back the points
83                                 if (($userid != $content['userid']) && (isValidId($userid)) && ($points > 0)) {
84                                         // Directly add points back to senders account
85                                         addPointsAutoPurge($userid, $points);
86                                         $points = '0';
87                                 } // END - if
88
89                                 // Add points
90                                 $userid = $content['userid']; $points += $content['price']; $admin_points += $content['price'];
91
92                                 // Remove confirmation links from queue
93                                 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s",
94                                         array(bigintval($content['id'])), __FILE__, __LINE__, FALSE));
95
96                                 // Update status of order
97                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='DELETED' WHERE `id`=%s LIMIT 1",
98                                         array(bigintval($content['pool_id'])), __FILE__, __LINE__, FALSE));
99                         } // END - if
100                 } // END - while
101
102                 // Add last points to last user account
103                 if ($points > 0) addPointsAutoPurge($userid, $points);
104         } // END - if
105
106         // Free memory
107         SQL_FREERESULT($result);
108
109         // Is the 'bonus' extension installed and activated?
110         if (isExtensionActive('bonus', TRUE)) {
111                 // Check for bonus campaigns
112                 $result = SQL_QUERY("SELECT `id`, `points` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='SEND' AND `timestamp` <= {?auto_purge?} ORDER BY `id` ASC", __FILE__, __LINE__);
113                 if (!SQL_HASZERONUMS($result)) {
114                         // Start deleting procedure
115                         $points = '0';
116                         while ($content = SQL_FETCHARRAY($result)) {
117                                 // Check if confirmation links are purged or not
118                                 $result_links = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s",
119                                         array(bigintval($content['id'])), __FILE__, __LINE__);
120                                 if (!SQL_HASZERONUMS($result_links)) {
121                                         // At least one link was found, enougth to pay back the points
122                                         $points += $content['points'] * SQL_NUMROWS($result_links);
123
124                                         // Free memory
125                                         SQL_FREERESULT($result_links);
126
127                                         // Remove confirmation links from queue
128                                         addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s",
129                                                 array(bigintval($content['id'])), __FILE__, __LINE__, FALSE));
130
131                                         // Update status of order
132                                         addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='DELETED' WHERE `id`=%s LIMIT 1",
133                                                 array(bigintval($content['id'])), __FILE__, __LINE__, FALSE));
134                                 } // END - if
135                         } // END - while
136
137                         // Add points to jackpot
138                         if (isExtensionActive('jackpot')) {
139                                 addPointsToJackpot($points);
140                         } // END - if
141
142                         // Add points for the admin
143                         $admin_points += $points;
144                 } // END - if
145
146                 // Free memory
147                 SQL_FREERESULT($result);
148         } // END - if
149
150         // Add points from deleted accounts to jackpot, but here just add to notify mail
151         if ($admin_points > 0) {
152                 // Send mail to admin
153                 sendAdminNotification('{--ADMIN_AUTOPURGE_SUBJECT--}', 'admin_autopurge_points', $admin_points);
154         } // END - if
155
156         // Run all SQLs here
157         runFilterChain('run_sqls');
158 } // END - if
159
160 // [EOF]
161 ?>