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