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=%s 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=%s",
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=%s 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=%s",
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=%s",
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=%s 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 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");
156 // Shall I look for inactive accounts and autopurge inactive accounts?
157 if ($_CONFIG['ap_inactive'] == "Y")
159 // Ok, let's have a look...
160 $since = bigintval(time() - $_CONFIG['ap_in_since']);
161 $EXCLUDE_LIST = " AND d.userid != c.def_refid";
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";
168 // Check for new holiday extension
169 if (GET_EXT_VERSION("holiday") >= "0.1.3")
171 $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";
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
179 ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
181 if (SQL_NUMROWS($result_inactive) > 0)
183 // Prepare variables and constants...
185 define('__INACTIVE_SINCE', ($_CONFIG['ap_in_since'] / 60 / 60));
186 define('__INACTIVE_TIME' , ($_CONFIG['ap_in_time'] / 60 / 60));
188 // Mark found accounts as inactive and send an email
189 while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
191 // Remember userids for the admin
194 // Get date/time from timestamp
195 $last = MAKE_DATETIME($last, "0");
197 // Load mail template
198 $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));
199 SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);
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__);
207 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
209 // Send mail notification to admin
210 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");
214 SQL_FREERESULT($result_inactive);
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
224 array($since, $since, $time), __FILE__, __LINE__);
225 if (SQL_NUMROWS($result_inactive) > 0)
227 // Prepare variable...
230 // Delete inactive accounts
231 while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
233 // Remember userids for the admin
236 // Get date/time from timestamp
237 $last = MAKE_DATETIME($last, "0");
239 // Finnaly delete this inactive account
240 DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));
244 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
246 // Send mail notification to admin
247 if ($_CONFIG['ap_un_mail'] == "Y")
249 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");
254 SQL_FREERESULT($result_inactive);
257 // Shall I auto-purge unconfirmed accounts?
258 if ($_CONFIG['ap_unconfirmed'] == "Y")
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)
266 // Prepare variable...
268 define('__UNCONFIRMED_TIME' , ($_CONFIG['ap_un_time'] / 60 / 60));
270 // Delete inactive accounts
271 while (list($uid, $email, $joined) = SQL_FETCHROW($result_uncon))
273 // Remember userids for the admin
276 // Get date/time from timestamp
277 $joined = MAKE_DATETIME($joined, "0");
279 // Finnaly delete this inactive account
280 DELETE_USER_ACCOUNT($uid, LOAD_EMAIL_TEMPLATE("member_autopurge_unconfirmed", $joined, ""));
284 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
286 // Send mail notification to admin
287 if ($_CONFIG['ap_un_mail'] == "Y")
289 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, "admin_autopurge_unconfirmed", $UIDs, "");
294 SQL_FREERESULT($result_uncon);
297 // Check version (must be > 0.0)
298 if ((GET_EXT_VERSION("task") > "0.0") && ($_CONFIG['ap_tasks'] == "Y"))
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();
306 if (($DELETED > 0) && ($_CONFIG['ap_tasks_mail'] == "Y"))
308 // Send out email to admin
309 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_TASKS_SUBJECT, "admin_autopurge_tsks", $DELETED, "");
313 // Search for mails from deleted members?
314 if ($_CONFIG['ap_del_mails'])
316 // Okay, let's check for them...
317 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
318 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
319 array($since), __FILE__, __LINE__);
324 // Do we have "purged" mails?
325 if (SQL_NUMROWS($result_mails) > 0)
327 // Okay, check for their sender's
328 while(list($sender) = SQL_FETCHROW($result_mails))
331 $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
334 // Okay we found some mails!
335 $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_pool WHERE sender=%s",
336 array(bigintval($sender)), __FILE__, __LINE__);
337 $DELETED += SQL_AFFECTEDROWS();
339 // Reset query (to prevent possible errors) ...
340 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
341 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
342 array($since), __FILE__, __LINE__);
348 SQL_FREERESULT($result_mails);
350 // Now let's check for stats entries as well
351 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
352 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
353 array($since), __FILE__, __LINE__);
355 // Do we have "purged" mails?
356 if (SQL_NUMROWS($result_mails) > 0)
358 // Okay, check for their sender's
359 while(list($sender) = SQL_FETCHROW($result_mails))
362 $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%s LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
365 // Okay we found some mails!
366 $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_stats WHERE sender=%s", array(bigintval($sender)), __FILE__, __LINE__);
367 $DELETED += SQL_AFFECTEDROWS();
369 // Reset query (to prevent possible errors) ...
370 $since = bigintval(time() - $_CONFIG['ap_dm_timeout']);
371 $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_user_stats WHERE data_type='DELETED' AND timestamp_send <= %s ORDER BY sender",
372 array($since), __FILE__, __LINE__);
378 SQL_FREERESULT($result_mails);
380 // Do we have deleted mails and the admin want's to receive a notification
381 if (($DELETED > 0) && ($_CONFIG['ap_dm_notify'] == "Y"))
383 // Send out email to admin
384 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");
388 if (EXT_IS_ACTIVE("rallye"))
390 // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
391 require_once(PATH."inc/libs/rallye_functions.php");
392 RALLYE_DELETE_EXPIRED_RALLYES();
395 if (EXT_IS_ACTIVE("bonus"))
397 // Check for expired turbo bonus lines
398 require_once(PATH."inc/libs/bonus_functions.php");
399 BONUS_PURGE_EXPIRED_TURBO_BONUS();