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