0adcd3e18726614b0fb2f66d3d89f74b1d50657e
[mailer.git] / inc / autopurge / purge-mails.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 (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 } // END - if
39
40 // Abort if autopurge is not active or disabled by admin
41 if ((!EXT_IS_ACTIVE("autopurge")) || (getConfig('auto_purge_active') == "N")) {
42         // Abort here
43         return false;
44 } // END - if
45
46 // Search for mails from deleted members?
47 if (getConfig('ap_del_mails') == "Y") {
48         // Okay, let's check for them...
49         $since = bigintval(getConfig('ap_dm_timeout'));
50         $result_mails = SQL_QUERY_ESC("SELECT sender\
51 FROM `{!MYSQL_PREFIX!}_pool`
52 WHERE data_type='DELETED' AND timestamp <= (UNIX_TIMESTAMP() - %s)
53 ORDER BY sender ASC",
54          array($since), __FILE__, __LINE__);
55
56         // Reset counter...
57         $DELETED = 0;
58
59         // Do we have "purged" mails?
60         if (SQL_NUMROWS($result_mails) > 0) {
61                 // Okay, check for their sender's
62                 while(list($sender) = SQL_FETCHROW($result_mails)) {
63                         // Check now...
64                         $fount = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM `{!MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1",
65                                 array(bigintval($sender)), __FILE__, __LINE__));
66                         if ($found == 0) {
67                                 // Okay we found some mails!
68                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!MYSQL_PREFIX!}_pool` WHERE sender=%s",
69                                         array(bigintval($sender)), __FILE__, __LINE__);
70                                 $DELETED += SQL_AFFECTEDROWS();
71
72                                 // Reset query (to prevent possible errors) ...
73                                 $since = bigintval(getConfig('ap_dm_timeout'));
74                                 $result_mails = SQL_QUERY_ESC("SELECT sender
75 FROM `{!MYSQL_PREFIX!}_pool`
76 WHERE data_type='DELETED' AND timestamp <= (UNIX_TIMESTAMP() - %s)
77 ORDER BY sender ASC",
78                                         array($since), __FILE__, __LINE__);
79                         }
80                 }
81         }
82
83         // Free memory
84         SQL_FREERESULT($result_mails);
85
86         // Now let's check for stats entries as well
87         $since = bigintval(getConfig('ap_dm_timeout'));
88         $result_mails = SQL_QUERY_ESC("SELECT sender
89 FROM `{!MYSQL_PREFIX!}_user_stats`
90 WHERE data_type='DELETED' AND timestamp_send <= (UNIX_TIMESTAMP() - %s)
91 ORDER BY sender ASC",
92                 array($since), __FILE__, __LINE__);
93
94         // Do we have "purged" mails?
95         if (SQL_NUMROWS($result_mails) > 0) {
96                 // Okay, check for their sender's
97                 while(list($sender) = SQL_FETCHROW($result_mails)) {
98                         // Check now...
99                         $found = SQL_NUMROWS(SQL_QUERY_ESC("SELECT userid FROM `{!MYSQL_PREFIX!}_user_data` WHERE userid=%s LIMIT 1", array(bigintval($sender)), __FILE__, __LINE__));
100                         if ($found == 0) {
101                                 // Okay we found some mails!
102                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!MYSQL_PREFIX!}_user_stats` WHERE sender=%s", array(bigintval($sender)), __FILE__, __LINE__);
103                                 $DELETED += SQL_AFFECTEDROWS();
104
105                                 // Reset query (to prevent possible errors) ...
106                                 $since = bigintval(getConfig('ap_dm_timeout'));
107                                 $result_mails = SQL_QUERY_ESC("SELECT sender
108 FROM `{!MYSQL_PREFIX!}_user_stats`
109 WHERE data_type='DELETED' AND timestamp_send <= (UNIX_TIMESTAMP() - %s)
110 ORDER BY sender ASC",
111                                         array($since), __FILE__, __LINE__);
112                         }
113                 }
114         }
115
116         // Free memory
117         SQL_FREERESULT($result_mails);
118
119         // Do we have deleted mails and the admin want's to receive a notification
120         if (($DELETED > 0) && (getConfig('ap_dm_notify') == "Y")) {
121                 // Send out email to admin
122                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT, "admin_autopurge_del_mails", $DELETED, "");
123         }
124 }
125
126 //
127 ?>