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