]> git.mxchange.org Git - mailer.git/blobdiff - 0.2.1/inc/autopurge.php
win32 to unix line delimiters changed
[mailer.git] / 0.2.1 / inc / autopurge.php
index b9d7e68adb0cdbbc3b31539047bd9d8d49c81949..52ac70472785d03c154d95ed708e9c406884f5ec 100644 (file)
-<?php\r
-/************************************************************************\r
- * MXChange v0.2.1                                    Start: 05/29/2004 *\r
- * ===============                              Last change: 11/26/2004 *\r
- *                                                                      *\r
- * -------------------------------------------------------------------- *\r
- * File              : autopurge.php                                    *\r
- * -------------------------------------------------------------------- *\r
- * Short description : Automatical purging of outdated mail links       *\r
- * -------------------------------------------------------------------- *\r
- * Kurzbeschreibung  : Auto-Loeschung von veralteten Mail-Links         *\r
- * -------------------------------------------------------------------- *\r
- *                                                                      *\r
- * -------------------------------------------------------------------- *\r
- * Copyright (c) 2003 - 2008 by Roland Haeder                           *\r
- * For more information visit: http://www.mxchange.org                  *\r
- *                                                                      *\r
- * This program is free software; you can redistribute it and/or modify *\r
- * it under the terms of the GNU General Public License as published by *\r
- * the Free Software Foundation; either version 2 of the License, or    *\r
- * (at your option) any later version.                                  *\r
- *                                                                      *\r
- * This program is distributed in the hope that it will be useful,      *\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of       *\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *\r
- * GNU General Public License for more details.                         *\r
- *                                                                      *\r
- * You should have received a copy of the GNU General Public License    *\r
- * along with this program; if not, write to the Free Software          *\r
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *\r
- * MA  02110-1301  USA                                                  *\r
- ************************************************************************/\r
-\r
-// Some security stuff...\r
-if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))\r
-{\r
-       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";\r
-       require($INC);\r
-}\r
-\r
-if (($CONFIG['auto_purge_active'] == "Y") && ($CONFIG['auto_purge'] > 0))\r
-{\r
-       // First calculate the timestamp\r
-       if (function_exists('CREATE_TIME_SELECTIONS'))\r
-       {\r
-               $PURGE = time() - $CONFIG['auto_purge'];\r
-       }\r
-        else\r
-       {\r
-               $PURGE = time() - $CONFIG['auto_purge'] * 24 * 60 * 60;\r
-       }\r
-\r
-       // Init variables\r
-       $admin_points = "0";\r
-\r
-       // Then check for outdated mail order. We don't delete them just the confirmation links will be deleted.\r
-       $result = SQL_QUERY_ESC("SELECT s.id, s.userid, s.pool_id, t.price\r
-FROM "._MYSQL_PREFIX."_user_stats AS s\r
-LEFT JOIN "._MYSQL_PREFIX."_payments AS t\r
-ON s.payment_id=t.id\r
-WHERE s.timestamp_ordered <= %s ORDER BY s.userid",\r
-        array(bigintval($PURGE)), __FILE__, __LINE__);\r
-       if (SQL_NUMROWS($result) > 0)\r
-       {\r
-               // Start deleting procedure\r
-               $uid = "0"; $points = "0";\r
-               while(list($mid, $sender, $pool, $price) = SQL_FETCHROW($result))\r
-               {\r
-                       // Check if confirmation links are purged or not\r
-                       $result_links = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_links WHERE stats_id=%d LIMIT 1",\r
-                        array(bigintval($mid)), __FILE__, __LINE__);\r
-                       if (SQL_NUMROWS($result_links) == 1)\r
-                       {\r
-                               // Free memory\r
-                               SQL_FREERESULT($result_links);\r
-\r
-                               // At least one link was found, enougth to pay back the points\r
-                               if (($uid != $sender) && ($uid > 0) && ($points > 0))\r
-                               {\r
-                                       // Directly add points back to senders account\r
-                                       AUTOPURGE_ADD_POINTS($uid, $points);\r
-                                       $points = "0";\r
-                               }\r
-                               // Add points\r
-                               $uid = $sender; $points += $price; $admin_points += $price;\r
-\r
-                               // Remove confirmation links from queue\r
-                               $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE stats_id=%d",\r
-                                array(bigintval($mid)), __FILE__, __LINE__);\r
-\r
-                               // Update status of order\r
-                               $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='DELETED' WHERE id=%d LIMIT 1",\r
-                                array(bigintval($pool)), __FILE__, __LINE__);\r
-                       }\r
-               }\r
-\r
-               // Add last points to last user account\r
-               if ($points > 0) AUTOPURGE_ADD_POINTS($uid, $points);\r
-       }\r
-\r
-       // Free memory\r
-       SQL_FREERESULT($result);\r
-\r
-       // Is the 'bonus' extension installed and activated?\r
-       if (EXT_IS_ACTIVE("bonus", true))\r
-       {\r
-               // Check for bonus campaigns\r
-               $result = SQL_QUERY_ESC("SELECT id, points FROM "._MYSQL_PREFIX."_bonus WHERE data_type='SEND' AND timestamp <= %s ORDER BY id",\r
-                array(bigintval($PURGE)), __FILE__, __LINE__);\r
-               if (SQL_NUMROWS($result) > 0)\r
-               {\r
-                       // Start deleting procedure\r
-                       $points = "0";\r
-                       while (list($bid, $price) = SQL_FETCHROW($result))\r
-                       {\r
-                               // Check if confirmation links are purged or not\r
-                               $result_links = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_links WHERE bonus_id=%d",\r
-                                array(bigintval($bid)), __FILE__, __LINE__);\r
-                               if (SQL_NUMROWS($result_links) > 0)\r
-                               {\r
-                                       // At least one link was found, enougth to pay back the points\r
-                                       $points += $price * SQL_NUMROWS($result_links);\r
-\r
-                                       // Free memory\r
-                                       SQL_FREERESULT($result_links);\r
-\r
-                                       // Remove confirmation links from queue\r
-                                       $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE bonus_id=%d",\r
-                                        array(bigintval($bid)), __FILE__, __LINE__);\r
-\r
-                                       // Update status of order\r
-                                       $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='DELETED' WHERE id=%d LIMIT 1",\r
-                                        array(bigintval($bid)), __FILE__, __LINE__);\r
-                               }\r
-                       }\r
-\r
-                       // Add points to jackpot\r
-                       ADD_JACKPOT($points);\r
-\r
-                       // Add points for the admin\r
-                       $admin_points += $points;\r
-               }\r
-\r
-               // Free memory\r
-               SQL_FREERESULT($result);\r
-       }\r
-\r
-       // Add points from deleted accounts to jackpot, but here just add to notify mail\r
-       if ($admin_points > 0)\r
-       {\r
-               // Send mail to admin\r
-               if (GET_EXT_VERSION("admins") >= "0.4.1")\r
-               {\r
-                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");\r
-               }\r
-                else\r
-               {\r
-                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_points", TRANSLATE_COMMA($points), "0");\r
-                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_SUBJECT, $msg);\r
-               }\r
-       }\r
-}\r
-\r
-// Shall I look for inactive accounts and autopurge inactive accounts?\r
-if ($CONFIG['ap_inactive'] == "Y")\r
-{\r
-       // Ok, let's have a look...\r
-       $since = bigintval(time() - $CONFIG['ap_in_since']);\r
-       $EXCLUDE_LIST = " AND d.userid != c.def_refid";\r
-\r
-       // Check for more extensions\r
-       if (EXT_IS_ACTIVE("beg"))     $EXCLUDE_LIST .= " AND d.userid != c.beg_uid";\r
-       if (EXT_IS_ACTIVE("bonus"))   $EXCLUDE_LIST .= " AND d.userid != c.bonus_uid";\r
-       if (EXT_IS_ACTIVE("doubler")) $EXCLUDE_LIST .= " AND d.userid != c.doubler_uid";\r
-\r
-       // Check for new holiday extension\r
-       if (GET_EXT_VERSION("holiday") >= "0.1.3")\r
-       {\r
-               $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";\r
-       }\r
-\r
-       // Check for all accounts\r
-       $result_inactive = SQL_QUERY_ESC("SELECT DISTINCT d.userid, d.email, d.last_online\r
-FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c\r
-WHERE d.status='CONFIRMED' AND d.joined < %s AND d.last_online < %s AND d.ap_notified < %s\r
-".$EXCLUDE_LIST."\r
-ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);\r
-\r
-       if (SQL_NUMROWS($result_inactive) > 0)\r
-       {\r
-               // Prepare variables and constants...\r
-               $UIDs = "";\r
-               define('__INACTIVE_SINCE', ($CONFIG['ap_in_since'] / 60 / 60));\r
-               define('__INACTIVE_TIME' , ($CONFIG['ap_in_time']  / 60 / 60));\r
-\r
-               // Mark found accounts as inactive and send an email\r
-               while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive))\r
-               {\r
-                       // Remember userids for the admin\r
-                       $UIDs .= $uid.", ";\r
-\r
-                       // Get date/time from timestamp\r
-                       $last = MAKE_DATETIME($last, "0");\r
-\r
-                       // Load mail template\r
-                       $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));\r
-                       SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);\r
-\r
-                       // Update this account\r
-                       $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET ap_notified=UNIX_TIMESTAMP() WHERE userid=%d LIMIT 1",\r
-                        array(bigintval($uid)), __FILE__, __LINE__);\r
-               }\r
-\r
-               // Remove last comma\r
-               $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));\r
-\r
-               // Send mail notification to admin\r
-               if (GET_EXT_VERSION("admins") >= "0.4.1")\r
-               {\r
-                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");\r
-               }\r
-                else\r
-               {\r
-                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_inactive", $UIDs, "");\r
-                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, $msg);\r
-               }\r
-       }\r
-\r
-       // Free memory\r
-       SQL_FREERESULT($result_inactive);\r
-\r
-       // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list\r
-       // here for e.g. excluding holiday users\r
-       $time = bigintval(time() - $CONFIG['ap_in_time']);\r
-       $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online\r
-FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c\r
-WHERE status='CONFIRMED' AND joined < %s AND last_online< %s AND ap_notified < %s\r
-".$EXCLUDE_LIST."\r
-ORDER BY userid",\r
-        array($since, $since, $time), __FILE__, __LINE__);\r
-       if (SQL_NUMROWS($result_inactive) > 0)\r
-       {\r
-               // Prepare variable...\r
-               $UIDs = "";\r
-\r
-               // Delete inactive accounts\r
-               while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive))\r
-               {\r
-                       // Remember userids for the admin\r
-                       $UIDs .= $uid.", ";\r
-\r
-                       // Get date/time from timestamp\r
-                       $last = MAKE_DATETIME($last, "0");\r
-\r
-                       // Finnaly delete this inactive account\r
-                       DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));\r
-               }\r
-\r
-               // Remove last comma\r
-               $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));\r
-\r
-               // Send mail notification to admin\r
-               if ($CONFIG['ap_un_mail'] == "Y")\r
-               {\r
-                       if (GET_EXT_VERSION("admins") >= "0.4.1")\r
-                       {\r
-                               SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");\r
-                       }\r
-                        else\r
-                       {\r
-                               $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_delete", $UIDs, "");\r
-                               SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DELETE_SUBJECT, $msg);\r
-                       }\r
-               }\r
-       }\r
-\r
-       // Free memory\r
-       SQL_FREERESULT($result_inactive);\r
-}\r
-\r
-// Shall I auto-purge unconfirmed accounts?\r
-if ($CONFIG['ap_unconfirmed'] == "Y")\r
-{\r
-       // Init variables and find unconfirmed accounts which I shall auto-purge\r
-       $time = bigintval(time() - $CONFIG['ap_un_time']);\r
-       $result_uncon = SQL_QUERY_ESC("SELECT userid, email, joined FROM "._MYSQL_PREFIX."_user_data WHERE status='UNCONFIRMED' AND joined < %s ORDER BY userid",\r
-        array($time), __FILE__, __LINE__);\r
-       if (SQL_NUMROWS($result_uncon) > 0)\r
-       {\r
-               // Prepare variable...\r
-               $UIDs = "";\r
-               define('__UNCONFIRMED_TIME' , ($CONFIG['ap_un_time']  / 60 / 60));\r
-\r
-               // Delete inactive accounts\r
-               while (list($uid, $email, $joined) = SQL_FETCHROW($result_uncon))\r
-               {\r
-                       // Remember userids for the admin\r
-                       $UIDs .= $uid.", ";\r
-\r
-                       // Get date/time from timestamp\r
-                       $joined = MAKE_DATETIME($joined, "0");\r
-\r
-                       // Finnaly delete this inactive account\r
-                       DELETE_USER_ACCOUNT($uid, LOAD_EMAIL_TEMPLATE("member_autopurge_unconfirmed", $joined, ""));\r
-               }\r
-\r
-               // Remove last comma\r
-               $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));\r
-\r
-               // Send mail notification to admin\r
-               if ($CONFIG['ap_un_mail'] == "Y")\r
-               {\r
-                       if (GET_EXT_VERSION("admins") >= "0.4.1")\r
-                       {\r
-                               SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, "admin_autopurge_unconfirmed", $UIDs, "");\r
-                       }\r
-                        else\r
-                       {\r
-                               $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_unconfirmed", $UIDs, "");\r
-                               SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, $msg);\r
-                       }\r
-               }\r
-       }\r
-\r
-       // Free memory\r
-       SQL_FREERESULT($result_uncon);\r
-}\r
-\r
-// Check version (must be > 0.0)\r
-if ((GET_EXT_VERSION("task") > "0.0") && ($CONFIG['ap_tasks'] == "Y"))\r
-{\r
-       // Purge deleted tasks (no notification to admin)\r
-       $since = bigintval(time() - $CONFIG['ap_tasks_time']);\r
-       $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE status='DELETED' AND task_created <= %s",\r
-        array($since), __FILE__, __LINE__);\r
-       $DELETED = SQL_AFFECTEDROWS();\r
-\r
-       if (($DELETED > 0) && ($CONFIG['ap_tasks_mail'] == "Y"))\r
-       {\r
-               // Send out email to admin\r
-               if (GET_EXT_VERSION("admins") >= "0.4.1")\r
-               {\r
-                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_TASKS_SUBJECT, "admin_autopurge_tsks", $DELETED, "");\r
-               }\r
-                else\r
-               {\r
-                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_tsks", $DELETED, "");\r
-                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_TASKS_SUBJECT, $msg);\r
-               }\r
-       }\r
-}\r
-\r
-// Search for mails from deleted members?\r
-if ($CONFIG['ap_del_mails'])\r
-{\r
-       // Okay, let's check for them...\r
-       $since = bigintval(time() - $CONFIG['ap_dm_timeout']);\r
-       $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",\r
-        array($since), __FILE__, __LINE__);\r
-\r
-       // Reset counter...\r
-       $DELETED = 0;\r
-\r
-       // Do we have "purged" mails?\r
-       if (SQL_NUMROWS($result_mails) > 0)\r
-       {\r
-               // Okay, check for their sender's\r
-               while(list($sender) = SQL_FETCHROW($result_mails))\r
-               {\r
-                       // Check now...\r
-                       $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));\r
-                       if ($found == 0)\r
-                       {\r
-                               // Okay we found some mails!\r
-                               $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_pool WHERE sender=%d",\r
-                                array(bigintval($sender)), __FILE__, __LINE__);\r
-                               $DELETED += SQL_AFFECTEDROWS();\r
-\r
-                               // Reset query (to prevent possible errors) ...\r
-                               $since = bigintval(time() - $CONFIG['ap_dm_timeout']);\r
-                               $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",\r
-                                array($since), __FILE__, __LINE__);\r
-                       }\r
-               }\r
-       }\r
-\r
-       // Free memory\r
-       SQL_FREERESULT($result_mails);\r
-\r
-       // Now let's check for stats entries as well\r
-       $since = bigintval(time() - $CONFIG['ap_dm_timeout']);\r
-       $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",\r
-        array($since), __FILE__, __LINE__);\r
-\r
-       // Do we have "purged" mails?\r
-       if (SQL_NUMROWS($result_mails) > 0)\r
-       {\r
-               // Okay, check for their sender's\r
-               while(list($sender) = SQL_FETCHROW($result_mails))\r
-               {\r
-                       // Check now...\r
-                       $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));\r
-                       if ($found == 0)\r
-                       {\r
-                               // Okay we found some mails!\r
-                               $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_stats WHERE sender=%d", array(bigintval($sender)), __FILE__, __LINE__);\r
-                               $DELETED += SQL_AFFECTEDROWS();\r
-\r
-                               // Reset query (to prevent possible errors) ...\r
-                               $since = bigintval(time() - $CONFIG['ap_dm_timeout']);\r
-                               $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_user_stats WHERE data_type='DELETED' AND timestamp_send <= %s ORDER BY sender",\r
-                                array($since), __FILE__, __LINE__);\r
-                       }\r
-               }\r
-       }\r
-\r
-       // Free memory\r
-       SQL_FREERESULT($result_mails);\r
-\r
-       // Do we have deleted mails and the admin want's to receive a notification\r
-       if (($DELETED > 0) && ($CONFIG['ap_dm_notify'] == "Y"))\r
-       {\r
-               // Send out email to admin\r
-               if (GET_EXT_VERSION("admins") >= "0.4.1")\r
-               {\r
-                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");\r
-               }\r
-                else\r
-               {\r
-                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_del_mails", $DELETED, "");\r
-                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, $msg);\r
-               }\r
-       }\r
-}\r
-\r
-if (EXT_IS_ACTIVE("rallye"))\r
-{\r
-       // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)\r
-       require_once(PATH."inc/libs/rallye_functions.php");\r
-       RALLYE_DELETE_EXPIRED_RALLYES();\r
-}\r
-//\r
-if (EXT_IS_ACTIVE("bonus"))\r
-{\r
-       // Check for expired turbo bonus lines\r
-       require_once(PATH."inc/libs/bonus_functions.php");\r
-       BONUS_PURGE_EXPIRED_TURBO_BONUS();\r
-}\r
-//\r
-?>\r
+<?php
+/************************************************************************
+ * MXChange v0.2.1                                    Start: 05/29/2004 *
+ * ===============                              Last change: 11/26/2004 *
+ *                                                                      *
+ * -------------------------------------------------------------------- *
+ * File              : autopurge.php                                    *
+ * -------------------------------------------------------------------- *
+ * Short description : Automatical purging of outdated mail links       *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung  : Auto-Loeschung von veralteten Mail-Links         *
+ * -------------------------------------------------------------------- *
+ *                                                                      *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2008 by Roland Haeder                           *
+ * For more information visit: http://www.mxchange.org                  *
+ *                                                                      *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or    *
+ * (at your option) any later version.                                  *
+ *                                                                      *
+ * This program is distributed in the hope that it will be useful,      *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
+ * GNU General Public License for more details.                         *
+ *                                                                      *
+ * You should have received a copy of the GNU General Public License    *
+ * along with this program; if not, write to the Free Software          *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
+ * MA  02110-1301  USA                                                  *
+ ************************************************************************/
+
+// Some security stuff...
+if (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
+{
+       $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
+       require($INC);
+}
+
+if (($CONFIG['auto_purge_active'] == "Y") && ($CONFIG['auto_purge'] > 0))
+{
+       // First calculate the timestamp
+       if (function_exists('CREATE_TIME_SELECTIONS'))
+       {
+               $PURGE = time() - $CONFIG['auto_purge'];
+       }
+        else
+       {
+               $PURGE = time() - $CONFIG['auto_purge'] * 24 * 60 * 60;
+       }
+
+       // Init variables
+       $admin_points = "0";
+
+       // Then check for outdated mail order. We don't delete them just the confirmation links will be deleted.
+       $result = SQL_QUERY_ESC("SELECT s.id, s.userid, s.pool_id, t.price
+FROM "._MYSQL_PREFIX."_user_stats AS s
+LEFT JOIN "._MYSQL_PREFIX."_payments AS t
+ON s.payment_id=t.id
+WHERE s.timestamp_ordered <= %s ORDER BY s.userid",
+        array(bigintval($PURGE)), __FILE__, __LINE__);
+       if (SQL_NUMROWS($result) > 0)
+       {
+               // Start deleting procedure
+               $uid = "0"; $points = "0";
+               while(list($mid, $sender, $pool, $price) = SQL_FETCHROW($result))
+               {
+                       // Check if confirmation links are purged or not
+                       $result_links = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_links WHERE stats_id=%d LIMIT 1",
+                        array(bigintval($mid)), __FILE__, __LINE__);
+                       if (SQL_NUMROWS($result_links) == 1)
+                       {
+                               // Free memory
+                               SQL_FREERESULT($result_links);
+
+                               // At least one link was found, enougth to pay back the points
+                               if (($uid != $sender) && ($uid > 0) && ($points > 0))
+                               {
+                                       // Directly add points back to senders account
+                                       AUTOPURGE_ADD_POINTS($uid, $points);
+                                       $points = "0";
+                               }
+                               // Add points
+                               $uid = $sender; $points += $price; $admin_points += $price;
+
+                               // Remove confirmation links from queue
+                               $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE stats_id=%d",
+                                array(bigintval($mid)), __FILE__, __LINE__);
+
+                               // Update status of order
+                               $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_pool SET data_type='DELETED' WHERE id=%d LIMIT 1",
+                                array(bigintval($pool)), __FILE__, __LINE__);
+                       }
+               }
+
+               // Add last points to last user account
+               if ($points > 0) AUTOPURGE_ADD_POINTS($uid, $points);
+       }
+
+       // Free memory
+       SQL_FREERESULT($result);
+
+       // Is the 'bonus' extension installed and activated?
+       if (EXT_IS_ACTIVE("bonus", true))
+       {
+               // Check for bonus campaigns
+               $result = SQL_QUERY_ESC("SELECT id, points FROM "._MYSQL_PREFIX."_bonus WHERE data_type='SEND' AND timestamp <= %s ORDER BY id",
+                array(bigintval($PURGE)), __FILE__, __LINE__);
+               if (SQL_NUMROWS($result) > 0)
+               {
+                       // Start deleting procedure
+                       $points = "0";
+                       while (list($bid, $price) = SQL_FETCHROW($result))
+                       {
+                               // Check if confirmation links are purged or not
+                               $result_links = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_user_links WHERE bonus_id=%d",
+                                array(bigintval($bid)), __FILE__, __LINE__);
+                               if (SQL_NUMROWS($result_links) > 0)
+                               {
+                                       // At least one link was found, enougth to pay back the points
+                                       $points += $price * SQL_NUMROWS($result_links);
+
+                                       // Free memory
+                                       SQL_FREERESULT($result_links);
+
+                                       // Remove confirmation links from queue
+                                       $result_del = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_links WHERE bonus_id=%d",
+                                        array(bigintval($bid)), __FILE__, __LINE__);
+
+                                       // Update status of order
+                                       $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_bonus SET data_type='DELETED' WHERE id=%d LIMIT 1",
+                                        array(bigintval($bid)), __FILE__, __LINE__);
+                               }
+                       }
+
+                       // Add points to jackpot
+                       ADD_JACKPOT($points);
+
+                       // Add points for the admin
+                       $admin_points += $points;
+               }
+
+               // Free memory
+               SQL_FREERESULT($result);
+       }
+
+       // Add points from deleted accounts to jackpot, but here just add to notify mail
+       if ($admin_points > 0)
+       {
+               // Send mail to admin
+               if (GET_EXT_VERSION("admins") >= "0.4.1")
+               {
+                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_SUBJECT, "admin_autopurge_points", TRANSLATE_COMMA($points), "0");
+               }
+                else
+               {
+                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_points", TRANSLATE_COMMA($points), "0");
+                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_SUBJECT, $msg);
+               }
+       }
+}
+
+// Shall I look for inactive accounts and autopurge inactive accounts?
+if ($CONFIG['ap_inactive'] == "Y")
+{
+       // Ok, let's have a look...
+       $since = bigintval(time() - $CONFIG['ap_in_since']);
+       $EXCLUDE_LIST = " AND d.userid != c.def_refid";
+
+       // Check for more extensions
+       if (EXT_IS_ACTIVE("beg"))     $EXCLUDE_LIST .= " AND d.userid != c.beg_uid";
+       if (EXT_IS_ACTIVE("bonus"))   $EXCLUDE_LIST .= " AND d.userid != c.bonus_uid";
+       if (EXT_IS_ACTIVE("doubler")) $EXCLUDE_LIST .= " AND d.userid != c.doubler_uid";
+
+       // Check for new holiday extension
+       if (GET_EXT_VERSION("holiday") >= "0.1.3")
+       {
+               $EXCLUDE_LIST .= " AND d.holiday_active = 'N'";
+       }
+
+       // Check for all accounts
+       $result_inactive = SQL_QUERY_ESC("SELECT DISTINCT d.userid, d.email, d.last_online
+FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
+WHERE d.status='CONFIRMED' AND d.joined < %s AND d.last_online < %s AND d.ap_notified < %s
+".$EXCLUDE_LIST."
+ORDER BY d.userid", array($since, $since, $since), __FILE__, __LINE__);
+
+       if (SQL_NUMROWS($result_inactive) > 0)
+       {
+               // Prepare variables and constants...
+               $UIDs = "";
+               define('__INACTIVE_SINCE', ($CONFIG['ap_in_since'] / 60 / 60));
+               define('__INACTIVE_TIME' , ($CONFIG['ap_in_time']  / 60 / 60));
+
+               // Mark found accounts as inactive and send an email
+               while(list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
+               {
+                       // Remember userids for the admin
+                       $UIDs .= $uid.", ";
+
+                       // Get date/time from timestamp
+                       $last = MAKE_DATETIME($last, "0");
+
+                       // Load mail template
+                       $msg = LOAD_EMAIL_TEMPLATE("member_autopurge_inactive", $last, bigintval($uid));
+                       SEND_EMAIL($email, AUTOPURGE_MEMBER_INACTIVE_SUBJECT, $msg);
+
+                       // Update this account
+                       $result_update = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_user_data SET ap_notified=UNIX_TIMESTAMP() WHERE userid=%d LIMIT 1",
+                        array(bigintval($uid)), __FILE__, __LINE__);
+               }
+
+               // Remove last comma
+               $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
+
+               // Send mail notification to admin
+               if (GET_EXT_VERSION("admins") >= "0.4.1")
+               {
+                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, "admin_autopurge_inactive", $UIDs, "");
+               }
+                else
+               {
+                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_inactive", $UIDs, "");
+                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_INACTIVE_SUBJECT, $msg);
+               }
+       }
+
+       // Free memory
+       SQL_FREERESULT($result_inactive);
+
+       // Now let's have a look for inactive accounts we want to delete we newly use the same exclude list
+       // here for e.g. excluding holiday users
+       $time = bigintval(time() - $CONFIG['ap_in_time']);
+       $result_inactive = SQL_QUERY_ESC("SELECT d.userid, d.email, d.last_online
+FROM "._MYSQL_PREFIX."_user_data AS d, "._MYSQL_PREFIX."_config AS c
+WHERE status='CONFIRMED' AND joined < %s AND last_online< %s AND ap_notified < %s
+".$EXCLUDE_LIST."
+ORDER BY userid",
+        array($since, $since, $time), __FILE__, __LINE__);
+       if (SQL_NUMROWS($result_inactive) > 0)
+       {
+               // Prepare variable...
+               $UIDs = "";
+
+               // Delete inactive accounts
+               while (list($uid, $email, $last) = SQL_FETCHROW($result_inactive))
+               {
+                       // Remember userids for the admin
+                       $UIDs .= $uid.", ";
+
+                       // Get date/time from timestamp
+                       $last = MAKE_DATETIME($last, "0");
+
+                       // Finnaly delete this inactive account
+                       DELETE_USER_ACCOUNT(bigintval($uid), LOAD_EMAIL_TEMPLATE("member_autopurge_delete", $last, ""));
+               }
+
+               // Remove last comma
+               $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
+
+               // Send mail notification to admin
+               if ($CONFIG['ap_un_mail'] == "Y")
+               {
+                       if (GET_EXT_VERSION("admins") >= "0.4.1")
+                       {
+                               SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DELETE_SUBJECT, "admin_autopurge_delete", $UIDs, "");
+                       }
+                        else
+                       {
+                               $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_delete", $UIDs, "");
+                               SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DELETE_SUBJECT, $msg);
+                       }
+               }
+       }
+
+       // Free memory
+       SQL_FREERESULT($result_inactive);
+}
+
+// Shall I auto-purge unconfirmed accounts?
+if ($CONFIG['ap_unconfirmed'] == "Y")
+{
+       // Init variables and find unconfirmed accounts which I shall auto-purge
+       $time = bigintval(time() - $CONFIG['ap_un_time']);
+       $result_uncon = SQL_QUERY_ESC("SELECT userid, email, joined FROM "._MYSQL_PREFIX."_user_data WHERE status='UNCONFIRMED' AND joined < %s ORDER BY userid",
+        array($time), __FILE__, __LINE__);
+       if (SQL_NUMROWS($result_uncon) > 0)
+       {
+               // Prepare variable...
+               $UIDs = "";
+               define('__UNCONFIRMED_TIME' , ($CONFIG['ap_un_time']  / 60 / 60));
+
+               // Delete inactive accounts
+               while (list($uid, $email, $joined) = SQL_FETCHROW($result_uncon))
+               {
+                       // Remember userids for the admin
+                       $UIDs .= $uid.", ";
+
+                       // Get date/time from timestamp
+                       $joined = MAKE_DATETIME($joined, "0");
+
+                       // Finnaly delete this inactive account
+                       DELETE_USER_ACCOUNT($uid, LOAD_EMAIL_TEMPLATE("member_autopurge_unconfirmed", $joined, ""));
+               }
+
+               // Remove last comma
+               $UIDs = str_replace(", ", "\n", substr($UIDs, 0, -2));
+
+               // Send mail notification to admin
+               if ($CONFIG['ap_un_mail'] == "Y")
+               {
+                       if (GET_EXT_VERSION("admins") >= "0.4.1")
+                       {
+                               SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, "admin_autopurge_unconfirmed", $UIDs, "");
+                       }
+                        else
+                       {
+                               $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_unconfirmed", $UIDs, "");
+                               SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_UNCONFIRMED_SUBJECT, $msg);
+                       }
+               }
+       }
+
+       // Free memory
+       SQL_FREERESULT($result_uncon);
+}
+
+// Check version (must be > 0.0)
+if ((GET_EXT_VERSION("task") > "0.0") && ($CONFIG['ap_tasks'] == "Y"))
+{
+       // Purge deleted tasks (no notification to admin)
+       $since = bigintval(time() - $CONFIG['ap_tasks_time']);
+       $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE status='DELETED' AND task_created <= %s",
+        array($since), __FILE__, __LINE__);
+       $DELETED = SQL_AFFECTEDROWS();
+
+       if (($DELETED > 0) && ($CONFIG['ap_tasks_mail'] == "Y"))
+       {
+               // Send out email to admin
+               if (GET_EXT_VERSION("admins") >= "0.4.1")
+               {
+                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_TASKS_SUBJECT, "admin_autopurge_tsks", $DELETED, "");
+               }
+                else
+               {
+                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_tsks", $DELETED, "");
+                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_TASKS_SUBJECT, $msg);
+               }
+       }
+}
+
+// Search for mails from deleted members?
+if ($CONFIG['ap_del_mails'])
+{
+       // Okay, let's check for them...
+       $since = bigintval(time() - $CONFIG['ap_dm_timeout']);
+       $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
+        array($since), __FILE__, __LINE__);
+
+       // Reset counter...
+       $DELETED = 0;
+
+       // Do we have "purged" mails?
+       if (SQL_NUMROWS($result_mails) > 0)
+       {
+               // Okay, check for their sender's
+               while(list($sender) = SQL_FETCHROW($result_mails))
+               {
+                       // Check now...
+                       $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
+                       if ($found == 0)
+                       {
+                               // Okay we found some mails!
+                               $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_pool WHERE sender=%d",
+                                array(bigintval($sender)), __FILE__, __LINE__);
+                               $DELETED += SQL_AFFECTEDROWS();
+
+                               // Reset query (to prevent possible errors) ...
+                               $since = bigintval(time() - $CONFIG['ap_dm_timeout']);
+                               $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
+                                array($since), __FILE__, __LINE__);
+                       }
+               }
+       }
+
+       // Free memory
+       SQL_FREERESULT($result_mails);
+
+       // Now let's check for stats entries as well
+       $since = bigintval(time() - $CONFIG['ap_dm_timeout']);
+       $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_pool WHERE data_type='DELETED' AND timestamp <= %s ORDER BY sender",
+        array($since), __FILE__, __LINE__);
+
+       // Do we have "purged" mails?
+       if (SQL_NUMROWS($result_mails) > 0)
+       {
+               // Okay, check for their sender's
+               while(list($sender) = SQL_FETCHROW($result_mails))
+               {
+                       // Check now...
+                       $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM "._MYSQL_PREFIX."_user_data WHERE userid=%d LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
+                       if ($found == 0)
+                       {
+                               // Okay we found some mails!
+                               $result_remove = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_user_stats WHERE sender=%d", array(bigintval($sender)), __FILE__, __LINE__);
+                               $DELETED += SQL_AFFECTEDROWS();
+
+                               // Reset query (to prevent possible errors) ...
+                               $since = bigintval(time() - $CONFIG['ap_dm_timeout']);
+                               $result_mails = SQL_QUERY_ESC("SELECT sender FROM "._MYSQL_PREFIX."_user_stats WHERE data_type='DELETED' AND timestamp_send <= %s ORDER BY sender",
+                                array($since), __FILE__, __LINE__);
+                       }
+               }
+       }
+
+       // Free memory
+       SQL_FREERESULT($result_mails);
+
+       // Do we have deleted mails and the admin want's to receive a notification
+       if (($DELETED > 0) && ($CONFIG['ap_dm_notify'] == "Y"))
+       {
+               // Send out email to admin
+               if (GET_EXT_VERSION("admins") >= "0.4.1")
+               {
+                       SEND_ADMIN_EMAILS_PRO(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");
+               }
+                else
+               {
+                       $msg = LOAD_EMAIL_TEMPLATE("admin_autopurge_del_mails", $DELETED, "");
+                       SEND_ADMIN_EMAILS(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, $msg);
+               }
+       }
+}
+
+if (EXT_IS_ACTIVE("rallye"))
+{
+       // Check expired rallyes (hard-coded 3 days limit for displaying expired rallyes!)
+       require_once(PATH."inc/libs/rallye_functions.php");
+       RALLYE_DELETE_EXPIRED_RALLYES();
+}
+//
+if (EXT_IS_ACTIVE("bonus"))
+{
+       // Check for expired turbo bonus lines
+       require_once(PATH."inc/libs/bonus_functions.php");
+       BONUS_PURGE_EXPIRED_TURBO_BONUS();
+}
+//
+?>