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