a11fc14b333f8b9a65dbe635420900081dd6eaf6
[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') == "N")) {
43         // Abort here
44         return false;
45 }
46
47 if ((getConfig('auto_purge_active') == "Y") && (getConfig('auto_purge') > 0)) {
48         // First calculate the timestamp
49         $PURGE = getConfig('auto_purge');
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_ESC("SELECT s.id, s.userid, s.pool_id, t.price
56 FROM `{!_MYSQL_PREFIX!}_user_stats` AS s
57 LEFT JOIN `{!_MYSQL_PREFIX!}_payments` AS t
58 ON s.payment_id=t.id
59 WHERE s.timestamp_ordered <= (UNIX_TIMESTAMP() - %s) ORDER BY s.userid",
60          array(bigintval($PURGE)), __FILE__, __LINE__);
61         if (SQL_NUMROWS($result) > 0) {
62                 // Start deleting procedure
63                 $uid = 0; $points = 0;
64                 while (list($mid, $sender, $pool, $price) = SQL_FETCHROW($result)) {
65                         // Check if confirmation links are purged or not
66                         $result_links = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_links` WHERE stats_id=%s LIMIT 1",
67                          array(bigintval($mid)), __FILE__, __LINE__);
68                         if (SQL_NUMROWS($result_links) == 1) {
69                                 // Free memory
70                                 SQL_FREERESULT($result_links);
71
72                                 // At least one link was found, enougth to pay back the points
73                                 if (($uid != $sender) && ($uid > 0) && ($points > 0)) {
74                                         // Directly add points back to senders account
75                                         AUTOPURGE_ADD_POINTS($uid, $points);
76                                         $points = 0;
77                                 } // END - if
78
79                                 // Add points
80                                 $uid = $sender; $points += $price; $admin_points += $price;
81
82                                 // Remove confirmation links from queue
83                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_links` WHERE stats_id=%s",
84                                         array(bigintval($mid)), __FILE__, __LINE__);
85
86                                 // Update status of order
87                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_pool` SET data_type='DELETED' WHERE id=%s LIMIT 1",
88                                         array(bigintval($pool)), __FILE__, __LINE__);
89                         } // END - if
90                 } // END - while
91
92                 // Add last points to last user account
93                 if ($points > 0) AUTOPURGE_ADD_POINTS($uid, $points);
94         } // END - if
95
96         // Free memory
97         SQL_FREERESULT($result);
98
99         // Is the 'bonus' extension installed and activated?
100         if (EXT_IS_ACTIVE("bonus", true)) {
101                 // Check for bonus campaigns
102                 $result = SQL_QUERY_ESC("SELECT id, points FROM `{!_MYSQL_PREFIX!}_bonus` WHERE data_type='SEND' AND timestamp <= %s ORDER BY `id`",
103                         array(bigintval($PURGE)), __FILE__, __LINE__);
104                 if (SQL_NUMROWS($result) > 0) {
105                         // Start deleting procedure
106                         $points = 0;
107                         while (list($bid, $price) = SQL_FETCHROW($result)) {
108                                 // Check if confirmation links are purged or not
109                                 $result_links = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_user_links` WHERE bonus_id=%s",
110                                         array(bigintval($bid)), __FILE__, __LINE__);
111                                 if (SQL_NUMROWS($result_links) > 0) {
112                                         // At least one link was found, enougth to pay back the points
113                                         $points += $price * SQL_NUMROWS($result_links);
114
115                                         // Free memory
116                                         SQL_FREERESULT($result_links);
117
118                                         // Remove confirmation links from queue
119                                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_user_links` WHERE bonus_id=%s",
120                                                 array(bigintval($bid)), __FILE__, __LINE__);
121
122                                         // Update status of order
123                                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_bonus` SET data_type='DELETED' WHERE id=%s LIMIT 1",
124                                                 array(bigintval($bid)), __FILE__, __LINE__);
125                                 } // END - if
126                         } // END - while
127
128                         // Add points to jackpot
129                         ADD_JACKPOT($points);
130
131                         // Add points for the admin
132                         $admin_points += $points;
133                 } // END - if
134
135                 // Free memory
136                 SQL_FREERESULT($result);
137         } // END - if
138
139         // Add points from deleted accounts to jackpot, but here just add to notify mail
140         if ($admin_points > 0) {
141                 // Send mail to admin
142                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");
143         } // END - if
144 } // END - if
145
146 //
147 ?>