a0896be2c32a52c8911a8af81318a8ada6c813bd
[mailer.git] / inc / autopurge.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 05/29/2004 *
4  * ===============                              Last change: 11/26/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : autopurge.php                                    *
8  * -------------------------------------------------------------------- *
9  * Short description : Automatical purging of outdated mail links       *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auto-Loeschung von veralteten Mail-Links         *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 if (($_CONFIG['auto_purge_active'] == "Y") && ($_CONFIG['auto_purge'] > 0))
42 {
43         // First calculate the timestamp
44         if (function_exists('CREATE_TIME_SELECTIONS'))
45         {
46                 $PURGE = time() - $_CONFIG['auto_purge'];
47         }
48          else
49         {
50                 $PURGE = time() - $_CONFIG['auto_purge'] * 24 * 60 * 60;
51         }
52
53         // Init variables
54         $admin_points = 0;
55
56         // Then check for outdated mail order. We don't delete them just the confirmation links will be deleted.
57         $result = SQL_QUERY_ESC("SELECT s.id, s.userid, s.pool_id, t.price
58 FROM "._MYSQL_PREFIX."_user_stats AS s
59 LEFT JOIN "._MYSQL_PREFIX."_payments AS t
60 ON s.payment_id=t.id
61 WHERE s.timestamp_ordered <= %s ORDER BY s.userid",
62          array(bigintval($PURGE)), __FILE__, __LINE__);
63         if (SQL_NUMROWS($result) > 0)
64         {
65                 // Start deleting procedure
66                 $uid = 0; $points = 0;
67                 while(list($mid, $sender, $pool, $price) = SQL_FETCHROW($result))
68                 {
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                         {
74                                 // Free memory
75                                 SQL_FREERESULT($result_links);
76
77                                 // At least one link was found, enougth to pay back the points
78                                 if (($uid != $sender) && ($uid > 0) && ($points > 0))
79                                 {
80                                         // Directly add points back to senders account
81                                         AUTOPURGE_ADD_POINTS($uid, $points);
82                                         $points = 0;
83                                 }
84                                 // Add points
85                                 $uid = $sender; $points += $price; $admin_points += $price;
86
87                                 // Remove confirmation links from queue
88                                 $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE stats_id=%s",
89                                  array(bigintval($mid)), __FILE__, __LINE__);
90
91                                 // Update status of order
92                                 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='DELETED' WHERE id=%s LIMIT 1",
93                                  array(bigintval($pool)), __FILE__, __LINE__);
94                         }
95                 }
96
97                 // Add last points to last user account
98                 if ($points > 0) AUTOPURGE_ADD_POINTS($uid, $points);
99         }
100
101         // Free memory
102         SQL_FREERESULT($result);
103
104         // Is the 'bonus' extension installed and activated?
105         if (EXT_IS_ACTIVE("bonus", true))
106         {
107                 // Check for bonus campaigns
108                 $result = SQL_QUERY_ESC("SELECT id, points FROM "._MYSQL_PREFIX."_bonus WHERE data_type='SEND' AND timestamp <= %s ORDER BY id",
109                  array(bigintval($PURGE)), __FILE__, __LINE__);
110                 if (SQL_NUMROWS($result) > 0)
111                 {
112                         // Start deleting procedure
113                         $points = 0;
114                         while (list($bid, $price) = SQL_FETCHROW($result))
115                         {
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($bid)), __FILE__, __LINE__);
119                                 if (SQL_NUMROWS($result_links) > 0)
120                                 {
121                                         // At least one link was found, enougth to pay back the points
122                                         $points += $price * SQL_NUMROWS($result_links);
123
124                                         // Free memory
125                                         SQL_FREERESULT($result_links);
126
127                                         // Remove confirmation links from queue
128                                         $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE bonus_id=%s",
129                                          array(bigintval($bid)), __FILE__, __LINE__);
130
131                                         // Update status of order
132                                         $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='DELETED' WHERE id=%s LIMIT 1",
133                                          array(bigintval($bid)), __FILE__, __LINE__);
134                                 }
135                         }
136
137                         // Add points to jackpot
138                         ADD_JACKPOT($points);
139
140                         // Add points for the admin
141                         $admin_points += $points;
142                 }
143
144                 // Free memory
145                 SQL_FREERESULT($result);
146         }
147
148         // Add points from deleted accounts to jackpot, but here just add to notify mail
149         if ($admin_points > 0)
150         {
151                 // Send mail to admin
152                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");
153         }
154 }
155
156 // Shall I look for inactive accounts and autopurge inactive accounts?
157 if ($_CONFIG['ap_inactive'] == "Y")
158 {
159         // Ok, let's have a look...
160         $since = bigintval(time() - $_CONFIG['ap_in_since']);
161         $EXCLUDE_LIST = " AND d.userid != c.def_refid";
162
163         // Check for more extensions
164         if (EXT_IS_ACTIVE("beg"))     $EXCLUDE_LIST .= " AND d.userid != c.beg_uid";
165         if (EXT_IS_ACTIVE("bonus"))   $EXCLUDE_LIST .= " AND d.userid != c.bonus_uid";
166         if (EXT_IS_ACTIVE("doubler")) $EXCLUDE_LIST .= " AND d.userid != c.doubler_uid";
167
168         // Check for new holiday extension
169         if (GET_EXT_VERSION("holiday") >= "0.1.3")
170         {
171                 $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";
172         }
173
174         // Check for all accounts
175         $result_inactive = SQL_QUERY_ESC("SELECT DISTINCT d.userid, d.email, d.last_online
176 FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
177 WHERE d.status='CONFIRMED' AND d.joined < %s AND d.last_online < %s AND d.ap_notified < %s
178 ".$EXCLUDE_LIST."
179 ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
180
181         if (SQL_NUMROWS($result_inactive) > 0)
182         {
183                 // Prepare variables and constants...
184                 $UIDs = "";
185                 define('__INACTIVE_SINCE', ($_CONFIG['ap_in_since'] / 60 / 60));
186                 define('__INACTIVE_TIME' , ($_CONFIG['ap_in_time']  / 60 / 60));
187
188                 // Mark found accounts as inactive and send an email
189                 while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
190                 {
191                         // Remember userids for the admin
192                         $UIDs .= $uid.", ";
193
194                         // Get date/time from timestamp
195                         $last = MAKE_DATETIME($last, "0");
196
197                         // Load mail template
198                         $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));
199                         SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);
200
201                         // Update this account
202                         $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET ap_notified=UNIX_TIMESTAMP() WHERE userid=%s LIMIT 1",
203                          array(bigintval($uid)), __FILE__, __LINE__);
204                 }
205
206                 // Remove last comma
207                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
208
209                 // Send mail notification to admin
210                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");
211         }
212
213         // Free memory
214         SQL_FREERESULT($result_inactive);
215
216         // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
217         // here for e.g. excluding holiday users
218         $time = bigintval(time() - $_CONFIG['ap_in_time']);
219         $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
220 FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
221 WHERE status='CONFIRMED' AND joined < %s AND last_online< %s AND ap_notified < %s
222 ".$EXCLUDE_LIST."
223 ORDER BY userid",
224          array($since, $since, $time), __FILE__, __LINE__);
225         if (SQL_NUMROWS($result_inactive) > 0)
226         {
227                 // Prepare variable...
228                 $UIDs = "";
229
230                 // Delete inactive accounts
231                 while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
232                 {
233                         // Remember userids for the admin
234                         $UIDs .= $uid.", ";
235
236                         // Get date/time from timestamp
237                         $last = MAKE_DATETIME($last, "0");
238
239                         // Finnaly delete this inactive account
240                         DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));
241                 }
242
243                 // Remove last comma
244                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
245
246                 // Send mail notification to admin
247                 if ($_CONFIG['ap_un_mail'] == "Y")
248                 {
249                         SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");
250                 }
251         }
252
253         // Free memory
254         SQL_FREERESULT($result_inactive);
255 }
256
257 // Shall I auto-purge unconfirmed accounts?
258 if ($_CONFIG['ap_unconfirmed'] == "Y")
259 {
260         // Init variables and find unconfirmed accounts which I shall auto-purge
261         $time = bigintval(time() - $_CONFIG['ap_un_time']);
262         $result_uncon = SQL_QUERY_ESC("SELECT userid, email, joined FROM "._MYSQL_PREFIX."_user_data WHERE status='UNCONFIRMED' AND joined < %s ORDER BY userid",
263          array($time), __FILE__, __LINE__);
264         if (SQL_NUMROWS($result_uncon) > 0)
265         {
266                 // Prepare variable...
267                 $UIDs = "";
268                 define('__UNCONFIRMED_TIME' , ($_CONFIG['ap_un_time']  / 60 / 60));
269
270                 // Delete inactive accounts
271                 while (list($uid, $email, $joined) = SQL_FETCHROW($result_uncon))
272                 {
273                         // Remember userids for the admin
274                         $UIDs .= $uid.", ";
275
276                         // Get date/time from timestamp
277                         $joined = MAKE_DATETIME($joined, "0");
278
279                         // Finnaly delete this inactive account
280                         DELETE_USER_ACCOUNT($uid, LOAD_EMAIL_TEMPLATE("member_autopurge_unconfirmed", $joined, ""));
281                 }
282
283                 // Remove last comma
284                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
285
286                 // Send mail notification to admin
287                 if ($_CONFIG['ap_un_mail'] == "Y")
288                 {
289                         SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, "admin_autopurge_unconfirmed", $UIDs, "");
290                 }
291         }
292
293         // Free memory
294         SQL_FREERESULT($result_uncon);
295 }
296
297 // Check version (must be > 0.0)
298 if ((GET_EXT_VERSION("task") > "0.0") && ($_CONFIG['ap_tasks'] == "Y"))
299 {
300         // Purge deleted tasks (no notification to admin)
301         $since = bigintval(time() - $_CONFIG['ap_tasks_time']);
302         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE status='DELETED' AND task_created <= %s",
303          array($since), __FILE__, __LINE__);
304         $DELETED = SQL_AFFECTEDROWS();
305
306         if (($DELETED > 0) && ($_CONFIG['ap_tasks_mail'] == "Y"))
307         {
308                 // Send out email to admin
309                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_TASKS_SUBJECT, "admin_autopurge_tsks", $DELETED, "");
310         }
311 }
312
313 // Search for mails from deleted members?
314 if ($_CONFIG['ap_del_mails']) {
315         // Okay, let's check for them...
316         $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
317         $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
318          array($since), __FILE__, __LINE__);
319
320         // Reset counter...
321         $DELETED = 0;
322
323         // Do we have "purged" mails?
324         if (SQL_NUMROWS($result_mails) > 0) {
325                 // Okay, check for their sender's
326                 while(list($sender) = SQL_FETCHROW($result_mails)) {
327                         // Check now...
328                         $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
329                         if ($found == 0) {
330                                 // Okay we found some mails!
331                                 $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_pool WHERE sender=%s",
332                                  array(bigintval($sender)), __FILE__, __LINE__);
333                                 $DELETED += SQL_AFFECTEDROWS();
334
335                                 // Reset query (to prevent possible errors) ...
336                                 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
337                                 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
338                                  array($since), __FILE__, __LINE__);
339                         }
340                 }
341         }
342
343         // Free memory
344         SQL_FREERESULT($result_mails);
345
346         // Now let's check for stats entries as well
347         $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
348         $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
349          array($since), __FILE__, __LINE__);
350
351         // Do we have "purged" mails?
352         if (SQL_NUMROWS($result_mails) > 0) {
353                 // Okay, check for their sender's
354                 while(list($sender) = SQL_FETCHROW($result_mails)) {
355                         // Check now...
356                         $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
357                         if ($found == 0) {
358                                 // Okay we found some mails!
359                                 $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_stats WHERE sender=%s", array(bigintval($sender)), __FILE__, __LINE__);
360                                 $DELETED += SQL_AFFECTEDROWS();
361
362                                 // Reset query (to prevent possible errors) ...
363                                 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
364                                 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_user_stats WHERE data_type='DELETED' AND timestamp_send <= %s ORDER BY sender",
365                                  array($since), __FILE__, __LINE__);
366                         }
367                 }
368         }
369
370         // Free memory
371         SQL_FREERESULT($result_mails);
372
373         // Do we have deleted mails and the admin want's to receive a notification
374         if (($DELETED > 0) && ($_CONFIG['ap_dm_notify'] == "Y")) {
375                 // Send out email to admin
376                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");
377         }
378 }
379
380 if (EXT_IS_ACTIVE("rallye")) {
381         // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
382         require_once(PATH."inc/libs/rallye_functions.php");
383         RALLYE_DELETE_EXPIRED_RALLYES();
384 }
385
386 if (EXT_IS_ACTIVE("bonus")) {
387         // Check for expired turbo bonus lines
388         require_once(PATH."inc/libs/bonus_functions.php");
389         BONUS_PURGE_EXPIRED_TURBO_BONUS();
390 }
391
392 //
393 ?>