Referal levels for surfbar added (unfinished)
[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=%d 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=%d",
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=%d 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=%d",
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=%d",
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=%d 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                 if (GET_EXT_VERSION("admins") >= "0.4.1")
153                 {
154                         SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");
155                 }
156                  else
157                 {
158                         $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_points", TRANSLATE_COMMA($points), "0");
159                         SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_SUBJECT, $msg);
160                 }
161         }
162 }
163
164 // Shall I look for inactive accounts and autopurge inactive accounts?
165 if ($_CONFIG['ap_inactive'] == "Y")
166 {
167         // Ok, let's have a look...
168         $since = bigintval(time() - $_CONFIG['ap_in_since']);
169         $EXCLUDE_LIST = " AND d.userid != c.def_refid";
170
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";
175
176         // Check for new holiday extension
177         if (GET_EXT_VERSION("holiday") >= "0.1.3")
178         {
179                 $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";
180         }
181
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
186 ".$EXCLUDE_LIST."
187 ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
188
189         if (SQL_NUMROWS($result_inactive) > 0)
190         {
191                 // Prepare variables and constants...
192                 $UIDs = "";
193                 define('__INACTIVE_SINCE', ($_CONFIG['ap_in_since'] / 60 / 60));
194                 define('__INACTIVE_TIME' , ($_CONFIG['ap_in_time']  / 60 / 60));
195
196                 // Mark found accounts as inactive and send an email
197                 while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
198                 {
199                         // Remember userids for the admin
200                         $UIDs .= $uid.", ";
201
202                         // Get date/time from timestamp
203                         $last = MAKE_DATETIME($last, "0");
204
205                         // Load mail template
206                         $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));
207                         SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);
208
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__);
212                 }
213
214                 // Remove last comma
215                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
216
217                 // Send mail notification to admin
218                 if (GET_EXT_VERSION("admins") >= "0.4.1")
219                 {
220                         SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");
221                 }
222                  else
223                 {
224                         $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_inactive", $UIDs, "");
225                         SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, $msg);
226                 }
227         }
228
229         // Free memory
230         SQL_FREERESULT($result_inactive);
231
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
238 ".$EXCLUDE_LIST."
239 ORDER BY userid",
240          array($since, $since, $time), __FILE__, __LINE__);
241         if (SQL_NUMROWS($result_inactive) > 0)
242         {
243                 // Prepare variable...
244                 $UIDs = "";
245
246                 // Delete inactive accounts
247                 while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
248                 {
249                         // Remember userids for the admin
250                         $UIDs .= $uid.", ";
251
252                         // Get date/time from timestamp
253                         $last = MAKE_DATETIME($last, "0");
254
255                         // Finnaly delete this inactive account
256                         DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));
257                 }
258
259                 // Remove last comma
260                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
261
262                 // Send mail notification to admin
263                 if ($_CONFIG['ap_un_mail'] == "Y")
264                 {
265                         if (GET_EXT_VERSION("admins") >= "0.4.1")
266                         {
267                                 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");
268                         }
269                          else
270                         {
271                                 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_delete", $UIDs, "");
272                                 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DELETE_SUBJECT, $msg);
273                         }
274                 }
275         }
276
277         // Free memory
278         SQL_FREERESULT($result_inactive);
279 }
280
281 // Shall I auto-purge unconfirmed accounts?
282 if ($_CONFIG['ap_unconfirmed'] == "Y")
283 {
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)
289         {
290                 // Prepare variable...
291                 $UIDs = "";
292                 define('__UNCONFIRMED_TIME' , ($_CONFIG['ap_un_time']  / 60 / 60));
293
294                 // Delete inactive accounts
295                 while (list($uid, $email, $joined) = SQL_FETCHROW($result_uncon))
296                 {
297                         // Remember userids for the admin
298                         $UIDs .= $uid.", ";
299
300                         // Get date/time from timestamp
301                         $joined = MAKE_DATETIME($joined, "0");
302
303                         // Finnaly delete this inactive account
304                         DELETE_USER_ACCOUNT($uid, LOAD_EMAIL_TEMPLATE("member_autopurge_unconfirmed", $joined, ""));
305                 }
306
307                 // Remove last comma
308                 $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
309
310                 // Send mail notification to admin
311                 if ($_CONFIG['ap_un_mail'] == "Y")
312                 {
313                         if (GET_EXT_VERSION("admins") >= "0.4.1")
314                         {
315                                 SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, "admin_autopurge_unconfirmed", $UIDs, "");
316                         }
317                          else
318                         {
319                                 $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_unconfirmed", $UIDs, "");
320                                 SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, $msg);
321                         }
322                 }
323         }
324
325         // Free memory
326         SQL_FREERESULT($result_uncon);
327 }
328
329 // Check version (must be > 0.0)
330 if ((GET_EXT_VERSION("task") > "0.0") && ($_CONFIG['ap_tasks'] == "Y"))
331 {
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();
337
338         if (($DELETED > 0) && ($_CONFIG['ap_tasks_mail'] == "Y"))
339         {
340                 // Send out email to admin
341                 if (GET_EXT_VERSION("admins") >= "0.4.1")
342                 {
343                         SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_TASKS_SUBJECT, "admin_autopurge_tsks", $DELETED, "");
344                 }
345                  else
346                 {
347                         $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_tsks", $DELETED, "");
348                         SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_TASKS_SUBJECT, $msg);
349                 }
350         }
351 }
352
353 // Search for mails from deleted members?
354 if ($_CONFIG['ap_del_mails'])
355 {
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__);
360
361         // Reset counter...
362         $DELETED = 0;
363
364         // Do we have "purged" mails?
365         if (SQL_NUMROWS($result_mails) > 0)
366         {
367                 // Okay, check for their sender's
368                 while(list($sender) = SQL_FETCHROW($result_mails))
369                 {
370                         // Check now...
371                         $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
372                         if ($found == 0)
373                         {
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();
378
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__);
383                         }
384                 }
385         }
386
387         // Free memory
388         SQL_FREERESULT($result_mails);
389
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__);
394
395         // Do we have "purged" mails?
396         if (SQL_NUMROWS($result_mails) > 0)
397         {
398                 // Okay, check for their sender's
399                 while(list($sender) = SQL_FETCHROW($result_mails))
400                 {
401                         // Check now...
402                         $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
403                         if ($found == 0)
404                         {
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();
408
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__);
413                         }
414                 }
415         }
416
417         // Free memory
418         SQL_FREERESULT($result_mails);
419
420         // Do we have deleted mails and the admin want's to receive a notification
421         if (($DELETED > 0) && ($_CONFIG['ap_dm_notify'] == "Y"))
422         {
423                 // Send out email to admin
424                 if (GET_EXT_VERSION("admins") >= "0.4.1")
425                 {
426                         SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");
427                 }
428                  else
429                 {
430                         $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_del_mails", $DELETED, "");
431                         SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, $msg);
432                 }
433         }
434 }
435
436 if (EXT_IS_ACTIVE("rallye"))
437 {
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();
441 }
442 //
443 if (EXT_IS_ACTIVE("bonus"))
444 {
445         // Check for expired turbo bonus lines
446         require_once(PATH."inc/libs/bonus_functions.php");
447         BONUS_PURGE_EXPIRED_TURBO_BONUS();
448 }
449 //
450 ?>