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