0e5a9520eaf3bebe754a823c1afb130224eca009
[mailer.git] / inc / autopurge / 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  * Needs to be in all Files and every File needs "svn propset           *
19  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
20  * -------------------------------------------------------------------- *
21  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
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 }
44
45 // Abort if autopurge is not active or disabled by admin
46 if ((!isExtensionActive('autopurge')) || (getConfig('auto_purge_active') != 'Y')) {
47         // Abort here
48         return false;
49 }
50
51 if ((getConfig('auto_purge_active') == 'Y') && (getConfig('auto_purge') > 0)) {
52         // Init SQLs
53         initSqls();
54
55         // First calculate the timestamp
56         $PURGE = getConfig('auto_purge');
57
58         // Init variables
59         $admin_points = '0';
60
61         // Then check for outdated mail order. We don't delete them just the confirmation links will be deleted.
62         $result = SQL_QUERY_ESC("SELECT
63         s.id, s.userid, s.pool_id, t.price
64 FROM
65         `{?_MYSQL_PREFIX?}_user_stats` AS s
66 LEFT JOIN
67         `{?_MYSQL_PREFIX?}_payments` AS t
68 ON
69         s.payment_id=t.id
70 WHERE
71         s.timestamp_ordered <= (UNIX_TIMESTAMP() - %s)
72 ORDER BY
73         s.userid ASC",
74                 array(bigintval($PURGE)), __FILE__, __LINE__);
75         if (SQL_NUMROWS($result) > 0) {
76                 // Start deleting procedure
77                 $userid = '0'; $points = '0';
78                 while ($content = SQL_FETCHARRAY($result)) {
79                         // Check if confirmation links are purged or not
80                         $result_links = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s LIMIT 1",
81                                 array(bigintval($content['id'])), __FILE__, __LINE__);
82                         if (SQL_NUMROWS($result_links) == 1) {
83                                 // Free memory
84                                 SQL_FREERESULT($result_links);
85
86                                 // At least one link was found, enougth to pay back the points
87                                 if (($userid != $content['userid']) && ($userid > 0) && ($points > 0)) {
88                                         // Directly add points back to senders account
89                                         addPointsAutoPurge($userid, $points);
90                                         $points = '0';
91                                 } // END - if
92
93                                 // Add points
94                                 $userid = $content['userid']; $points += $content['price']; $admin_points += $content['price'];
95
96                                 // Remove confirmation links from queue
97                                 addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s",
98                                         array(bigintval($content['id'])), __FILE__, __LINE__, false));
99
100                                 // Update status of order
101                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_pool` SET `data_type`='DELETED' WHERE `id`=%s LIMIT 1",
102                                         array(bigintval($content['pool_id'])), __FILE__, __LINE__, false));
103                         } // END - if
104                 } // END - while
105
106                 // Add last points to last user account
107                 if ($points > 0) addPointsAutoPurge($userid, $points);
108         } // END - if
109
110         // Free memory
111         SQL_FREERESULT($result);
112
113         // Is the 'bonus' extension installed and activated?
114         if (isExtensionActive('bonus', true)) {
115                 // Check for bonus campaigns
116                 $result = SQL_QUERY_ESC("SELECT `id`, `points` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='SEND' AND timestamp <= %s ORDER BY `id`",
117                         array(bigintval($PURGE)), __FILE__, __LINE__);
118                 if (SQL_NUMROWS($result) > 0) {
119                         // Start deleting procedure
120                         $points = '0';
121                         while ($content = SQL_FETCHARRAY($result)) {
122                                 // Check if confirmation links are purged or not
123                                 $result_links = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s",
124                                 array(bigintval($content['id'])), __FILE__, __LINE__);
125                                 if (SQL_NUMROWS($result_links) > 0) {
126                                         // At least one link was found, enougth to pay back the points
127                                         $points += $content['points'] * SQL_NUMROWS($result_links);
128
129                                         // Free memory
130                                         SQL_FREERESULT($result_links);
131
132                                         // Remove confirmation links from queue
133                                         addSql(SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s",
134                                                 array(bigintval($content['id'])), __FILE__, __LINE__, false));
135
136                                         // Update status of order
137                                         addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='DELETED' WHERE `id`=%s LIMIT 1",
138                                                 array(bigintval($content['id'])), __FILE__, __LINE__, false));
139                                 } // END - if
140                         } // END - while
141
142                         // Add points to jackpot
143                         if (isExtensionActive('jackpot')) addPointsToJackpot($points);
144
145                         // Add points for the admin
146                         $admin_points += $points;
147                 } // END - if
148
149                 // Free memory
150                 SQL_FREERESULT($result);
151         } // END - if
152
153         // Add points from deleted accounts to jackpot, but here just add to notify mail
154         if ($admin_points > 0) {
155                 // Send mail to admin
156                 sendAdminNotification(getMessage('AUTOPURGE_ADMIN_SUBJECT'), "admin_autopurge_points", translateComma($points), 0);
157         } // END - if
158
159         // Run all SQLs here
160         runFilterChain('run_sqls');
161 } // END - if
162
163 //
164 ?>