Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[mailer.git] / inc / purge / 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 - 2015 by Mailer Developer Team                   *
21  * For more information visit: http://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 = sqlQuery('SELECT
56         `id`,
57         `userid`,
58         `pool_id`,
59         `payment_id`
60 FROM
61         `{?_MYSQL_PREFIX?}_user_stats`
62 WHERE
63         (UNIX_TIMESTAMP() - `timestamp_ordered`) >= {?auto_purge?}
64 ORDER BY
65         `userid` ASC', __FILE__, __LINE__);
66         if (!ifSqlHasZeroNumRows($result)) {
67                 // Start deleting procedure
68                 $userid = NULL;
69                 $points = '0';
70                 while ($content = sqlFetchArray($result)) {
71                         // Check if confirmation links are purged or not
72                         $result_links = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s LIMIT 1",
73                                 array(bigintval($content['id'])), __FILE__, __LINE__);
74
75                         // Is there one entry?
76                         if (sqlNumRows($result_links) == 1) {
77                                 // At least one link was found, enougth to pay back the points
78                                 if (($userid != $content['userid']) && (isValidId($userid)) && ($points > 0)) {
79                                         // Directly add points back to senders account
80                                         addPointsAutoPurge($userid, $points);
81                                         $points = '0';
82                                 } // END - if
83
84                                 // Add points
85                                 $userid = $content['userid'];
86                                 $points += getPaymentPrice($content['payment_id']);
87                                 $admin_points += $content['price'];
88
89                                 // Remove confirmation links from queue
90                                 addSql(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `stats_id`=%s",
91                                         array(bigintval($content['id'])), __FILE__, __LINE__, FALSE));
92
93                                 // Update status of order
94                                 updatePoolDataById($content['pool_id'], 'data_type', 'DELETED');
95                         } // END - if
96
97                         // Free memory
98                         sqlFreeResult($result_links);
99                 } // END - while
100
101                 // Are some points collected?
102                 if (($points > 0) && (isValidId($userid))) {
103                         // Add last points to last user account
104                         addPointsAutoPurge($userid, $points);
105                 } // END - if
106         } // END - if
107
108         // Free memory
109         sqlFreeResult($result);
110
111         // Is the 'bonus' extension installed and activated?
112         if (isExtensionActive('bonus', TRUE)) {
113                 // Check for bonus campaigns
114                 $result = sqlQuery("SELECT `id`, `points` FROM `{?_MYSQL_PREFIX?}_bonus` WHERE `data_type`='SEND' AND `timestamp` <= {?auto_purge?} ORDER BY `id` ASC", __FILE__, __LINE__);
115                 if (!ifSqlHasZeroNumRows($result)) {
116                         // Start deleting procedure
117                         $points = '0';
118                         while ($content = sqlFetchArray($result)) {
119                                 // Check if confirmation links are purged or not
120                                 $result_links = sqlQueryEscaped("SELECT `id` FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s",
121                                         array(bigintval($content['id'])), __FILE__, __LINE__);
122                                 if (!ifSqlHasZeroNumRows($result_links)) {
123                                         // At least one link was found, enougth to pay back the points
124                                         $points += $content['points'] * sqlNumRows($result_links);
125
126                                         // Free memory
127                                         sqlFreeResult($result_links);
128
129                                         // Remove confirmation links from queue
130                                         addSql(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_links` WHERE `bonus_id`=%s",
131                                                 array(bigintval($content['id'])), __FILE__, __LINE__, FALSE));
132
133                                         // Update status of order
134                                         addSql(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_bonus` SET `data_type`='DELETED' WHERE `id`=%s LIMIT 1",
135                                                 array(bigintval($content['id'])), __FILE__, __LINE__, FALSE));
136                                 } // END - if
137                         } // END - while
138
139                         // Is ext-jackpot installed?
140                         if (isExtensionActive('jackpot')) {
141                                 // Add points to jackpot
142                                 addPointsToJackpot($points);
143                         } // END - if
144
145                         // Add points for the admin
146                         $admin_points += $points;
147                 } // END - if
148
149                 // Free memory
150                 sqlFreeResult($result);
151         } // END - if
152
153         // Add points from deleted accounts to jackpot, but here just add to notify mail
154         if ($admin_points > 0) {
155                 // Send mail to admin
156                 sendAdminNotification('{--ADMIN_AUTOPURGE_SUBJECT--}', 'admin_purge_points', $admin_points);
157         } // END - if
158
159         // Run all SQLs here
160         runFilterChain('run_sqls');
161 } // END - if
162
163 // [EOF]
164 ?>