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