2 /************************************************************************
3 * MXChange v0.2.1 Start: 05/29/2004 *
4 * =============== Last change: 11/26/2004 *
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 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
32 ************************************************************************/
34 // Some security stuff...
35 if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
37 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 if (($_CONFIG['auto_purge_active'] == "Y") && ($_CONFIG['auto_purge'] > 0))
43 // First calculate the timestamp
44 if (function_exists('CREATE_TIME_SELECTIONS'))
46 $PURGE = time() - $_CONFIG['auto_purge'];
50 $PURGE = time() - $_CONFIG['auto_purge'] * 24 * 60 * 60;
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
61 WHERE s.timestamp_ordered <= %s ORDER BY s.userid",
62 array(bigintval($PURGE)), __FILE__, __LINE__);
63 if (SQL_NUMROWS($result) > 0)
65 // Start deleting procedure
66 $uid = "0"; $points = "0";
67 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=%d LIMIT 1",
71 array(bigintval($mid)), __FILE__, __LINE__);
72 if (SQL_NUMROWS($result_links) == 1)
75 SQL_FREERESULT($result_links);
77 // At least one link was found, enougth to pay back the points
78 if (($uid != $sender) && ($uid > 0) && ($points > 0))
80 // Directly add points back to senders account
81 AUTOPURGE_ADD_POINTS($uid, $points);
85 $uid = $sender; $points += $price; $admin_points += $price;
87 // Remove confirmation links from queue
88 $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE stats_id=%d",
89 array(bigintval($mid)), __FILE__, __LINE__);
91 // Update status of order
92 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='DELETED' WHERE id=%d LIMIT 1",
93 array(bigintval($pool)), __FILE__, __LINE__);
97 // Add last points to last user account
98 if ($points > 0) AUTOPURGE_ADD_POINTS($uid, $points);
102 SQL_FREERESULT($result);
104 // Is the 'bonus' extension installed and activated?
105 if (EXT_IS_ACTIVE("bonus", true))
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)
112 // Start deleting procedure
114 while (list($bid, $price) = SQL_FETCHROW($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=%d",
118 array(bigintval($bid)), __FILE__, __LINE__);
119 if (SQL_NUMROWS($result_links) > 0)
121 // At least one link was found, enougth to pay back the points
122 $points += $price * SQL_NUMROWS($result_links);
125 SQL_FREERESULT($result_links);
127 // Remove confirmation links from queue
128 $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE bonus_id=%d",
129 array(bigintval($bid)), __FILE__, __LINE__);
131 // Update status of order
132 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='DELETED' WHERE id=%d LIMIT 1",
133 array(bigintval($bid)), __FILE__, __LINE__);
137 // Add points to jackpot
138 ADD_JACKPOT($points);
140 // Add points for the admin
141 $admin_points += $points;
145 SQL_FREERESULT($result);
148 // Add points from deleted accounts to jackpot, but here just add to notify mail
149 if ($admin_points > 0)
151 // Send mail to admin
152 if (GET_EXT_VERSION("admins") >= "0.4.1")
154 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");
158 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_points", TRANSLATE_COMMA($points), "0");
159 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_SUBJECT, $msg);
164 // Shall I look for inactive accounts and autopurge inactive accounts?
165 if ($_CONFIG['ap_inactive'] == "Y")
167 // Ok, let's have a look...
168 $since = bigintval(time() - $_CONFIG['ap_in_since']);
169 $EXCLUDE_LIST = " AND d.userid != c.def_refid";
171 // Check for more extensions
172 if (EXT_IS_ACTIVE("beg")) $EXCLUDE_LIST .= " AND d.userid != c.beg_uid";
173 if (EXT_IS_ACTIVE("bonus")) $EXCLUDE_LIST .= " AND d.userid != c.bonus_uid";
174 if (EXT_IS_ACTIVE("doubler")) $EXCLUDE_LIST .= " AND d.userid != c.doubler_uid";
176 // Check for new holiday extension
177 if (GET_EXT_VERSION("holiday") >= "0.1.3")
179 $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";
182 // Check for all accounts
183 $result_inactive = SQL_QUERY_ESC("SELECT DISTINCT d.userid, d.email, d.last_online
184 FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
185 WHERE d.status='CONFIRMED' AND d.joined < %s AND d.last_online < %s AND d.ap_notified < %s
187 ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
189 if (SQL_NUMROWS($result_inactive) > 0)
191 // Prepare variables and constants...
193 define('__INACTIVE_SINCE', ($_CONFIG['ap_in_since'] / 60 / 60));
194 define('__INACTIVE_TIME' , ($_CONFIG['ap_in_time'] / 60 / 60));
196 // Mark found accounts as inactive and send an email
197 while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
199 // Remember userids for the admin
202 // Get date/time from timestamp
203 $last = MAKE_DATETIME($last, "0");
205 // Load mail template
206 $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));
207 SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);
209 // Update this account
210 $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET ap_notified=UNIX_TIMESTAMP() WHERE userid=%d LIMIT 1",
211 array(bigintval($uid)), __FILE__, __LINE__);
215 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
217 // Send mail notification to admin
218 if (GET_EXT_VERSION("admins") >= "0.4.1")
220 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");
224 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_inactive", $UIDs, "");
225 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, $msg);
230 SQL_FREERESULT($result_inactive);
232 // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
233 // here for e.g. excluding holiday users
234 $time = bigintval(time() - $_CONFIG['ap_in_time']);
235 $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
236 FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
237 WHERE status='CONFIRMED' AND joined < %s AND last_online< %s AND ap_notified < %s
240 array($since, $since, $time), __FILE__, __LINE__);
241 if (SQL_NUMROWS($result_inactive) > 0)
243 // Prepare variable...
246 // Delete inactive accounts
247 while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
249 // Remember userids for the admin
252 // Get date/time from timestamp
253 $last = MAKE_DATETIME($last, "0");
255 // Finnaly delete this inactive account
256 DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));
260 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
262 // Send mail notification to admin
263 if ($_CONFIG['ap_un_mail'] == "Y")
265 if (GET_EXT_VERSION("admins") >= "0.4.1")
267 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");
271 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_delete", $UIDs, "");
272 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DELETE_SUBJECT, $msg);
278 SQL_FREERESULT($result_inactive);
281 // Shall I auto-purge unconfirmed accounts?
282 if ($_CONFIG['ap_unconfirmed'] == "Y")
284 // Init variables and find unconfirmed accounts which I shall auto-purge
285 $time = bigintval(time() - $_CONFIG['ap_un_time']);
286 $result_uncon = SQL_QUERY_ESC("SELECT userid, email, joined FROM "._MYSQL_PREFIX."_user_data WHERE status='UNCONFIRMED' AND joined < %s ORDER BY userid",
287 array($time), __FILE__, __LINE__);
288 if (SQL_NUMROWS($result_uncon) > 0)
290 // Prepare variable...
292 define('__UNCONFIRMED_TIME' , ($_CONFIG['ap_un_time'] / 60 / 60));
294 // Delete inactive accounts
295 while (list($uid, $email, $joined) = SQL_FETCHROW($result_uncon))
297 // Remember userids for the admin
300 // Get date/time from timestamp
301 $joined = MAKE_DATETIME($joined, "0");
303 // Finnaly delete this inactive account
304 DELETE_USER_ACCOUNT($uid, LOAD_EMAIL_TEMPLATE("member_autopurge_unconfirmed", $joined, ""));
308 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
310 // Send mail notification to admin
311 if ($_CONFIG['ap_un_mail'] == "Y")
313 if (GET_EXT_VERSION("admins") >= "0.4.1")
315 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, "admin_autopurge_unconfirmed", $UIDs, "");
319 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_unconfirmed", $UIDs, "");
320 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, $msg);
326 SQL_FREERESULT($result_uncon);
329 // Check version (must be > 0.0)
330 if ((GET_EXT_VERSION("task") > "0.0") && ($_CONFIG['ap_tasks'] == "Y"))
332 // Purge deleted tasks (no notification to admin)
333 $since = bigintval(time() - $_CONFIG['ap_tasks_time']);
334 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE status='DELETED' AND task_created <= %s",
335 array($since), __FILE__, __LINE__);
336 $DELETED = SQL_AFFECTEDROWS();
338 if (($DELETED > 0) && ($_CONFIG['ap_tasks_mail'] == "Y"))
340 // Send out email to admin
341 if (GET_EXT_VERSION("admins") >= "0.4.1")
343 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_TASKS_SUBJECT, "admin_autopurge_tsks", $DELETED, "");
347 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_tsks", $DELETED, "");
348 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_TASKS_SUBJECT, $msg);
353 // Search for mails from deleted members?
354 if ($_CONFIG['ap_del_mails'])
356 // Okay, let's check for them...
357 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
358 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
359 array($since), __FILE__, __LINE__);
364 // Do we have "purged" mails?
365 if (SQL_NUMROWS($result_mails) > 0)
367 // Okay, check for their sender's
368 while(list($sender) = SQL_FETCHROW($result_mails))
371 $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
374 // Okay we found some mails!
375 $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_pool WHERE sender=%d",
376 array(bigintval($sender)), __FILE__, __LINE__);
377 $DELETED += SQL_AFFECTEDROWS();
379 // Reset query (to prevent possible errors) ...
380 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
381 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
382 array($since), __FILE__, __LINE__);
388 SQL_FREERESULT($result_mails);
390 // Now let's check for stats entries as well
391 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
392 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
393 array($since), __FILE__, __LINE__);
395 // Do we have "purged" mails?
396 if (SQL_NUMROWS($result_mails) > 0)
398 // Okay, check for their sender's
399 while(list($sender) = SQL_FETCHROW($result_mails))
402 $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
405 // Okay we found some mails!
406 $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_stats WHERE sender=%d", array(bigintval($sender)), __FILE__, __LINE__);
407 $DELETED += SQL_AFFECTEDROWS();
409 // Reset query (to prevent possible errors) ...
410 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
411 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_user_stats WHERE data_type='DELETED' AND timestamp_send <= %s ORDER BY sender",
412 array($since), __FILE__, __LINE__);
418 SQL_FREERESULT($result_mails);
420 // Do we have deleted mails and the admin want's to receive a notification
421 if (($DELETED > 0) && ($_CONFIG['ap_dm_notify'] == "Y"))
423 // Send out email to admin
424 if (GET_EXT_VERSION("admins") >= "0.4.1")
426 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");
430 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_del_mails", $DELETED, "");
431 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, $msg);
436 if (EXT_IS_ACTIVE("rallye"))
438 // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
439 require_once(PATH."inc/libs/rallye_functions.php");
440 RALLYE_DELETE_EXPIRED_RALLYES();
443 if (EXT_IS_ACTIVE("bonus"))
445 // Check for expired turbo bonus lines
446 require_once(PATH."inc/libs/bonus_functions.php");
447 BONUS_PURGE_EXPIRED_TURBO_BONUS();