9126d81632e0c35aa403d159849d64c2a211cc99
[mailer.git] / inc / autopurge / purge-mails.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Abort if autopurge is not active or disabled by admin
46 if ((!isExtensionActive('autopurge')) || (getConfig('auto_purge_active') != 'Y') || (getConfig('ap_del_mails') != 'Y')) {
47         // Abort here
48         return false;
49 } // END - if
50
51 // Okay, let's check for deleted mails
52 $result_mails = SQL_QUERY("SELECT
53         `sender`
54 FROM
55         `{?_MYSQL_PREFIX?}_pool`
56 WHERE
57         `data_type`='DELETED' AND `timestamp` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
58 ORDER BY
59         `sender` ASC", __FILE__, __LINE__);
60
61 // Reset counter...
62 $deletedStats = '0';
63
64 // Do we have "purged" mails?
65 if (SQL_NUMROWS($result_mails) > 0) {
66         // Okay, check for their sender's
67         while ($content = SQL_FETCHARRAY($result_mails)) {
68                 // Check now...
69                 if (!fetchUserData($content['sender'])) {
70                         // Okay we found some mails!
71                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_pool` WHERE `sender`=%s",
72                                 array(bigintval($content['sender'])), __FILE__, __LINE__);
73
74                         // Get all affected (deleted) rows
75                         $deletedStats += SQL_AFFECTEDROWS();
76
77                         // Reset query (to prevent possible errors) ...;
78                         $result_mails = SQL_QUERY("SELECT
79         `sender`
80 FROM
81         `{?_MYSQL_PREFIX?}_pool`
82 WHERE
83         `data_type`='DELETED' AND timestamp <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
84 ORDER BY
85         `sender` ASC", __FILE__, __LINE__);
86                 } // END - if
87         } // END - while
88 } // END - if
89
90 // Free memory
91 SQL_FREERESULT($result_mails);
92
93 // Now let's check for stats entries as well;
94 $result_mails = SQL_QUERY("SELECT
95         `userid` AS sender
96 FROM
97         `{?_MYSQL_PREFIX?}_user_stats`
98 WHERE
99         `timestamp_send` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
100 ORDER BY
101         `sender` ASC", __FILE__, __LINE__);
102
103 // Do we have "purged" mails?
104 if (SQL_NUMROWS($result_mails) > 0) {
105         // Okay, check for their sender's
106         while ($content = SQL_FETCHARRAY($result_mails)) {
107                 // Check now...
108                 if (!fetchUserData($content['sender'])) {
109                         // Okay we found some mails!
110                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `sender`=%s",
111                                 array(bigintval($content['sender'])), __FILE__, __LINE__);
112
113                         // Get all affected (deleted) rows
114                         $deletedStats += SQL_AFFECTEDROWS();
115
116                         // Reset query (to prevent possible errors) ...
117                         $result_mails = SQL_QUERY("SELECT
118         `userid` AS sender
119 FROM
120         `{?_MYSQL_PREFIX?}_user_stats`
121 WHERE
122         `timestamp_send` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
123 ORDER BY
124         `sender` ASC", __FILE__, __LINE__);
125                 } // END - if
126         } // END - while
127 } // END - if
128
129 // Free memory
130 SQL_FREERESULT($result_mails);
131
132 // Do we have deleted mails and the admin want's to receive a notification
133 if (($deletedStats > 0) && (getConfig('ap_dm_notify') == 'Y')) {
134         // Send out email to admin
135         sendAdminNotification(getMessage('AUTOPURGE_ADMIN_DEL_MAILS_SUBJECT'), 'admin_autopurge_del_mails', $deletedStats);
136 } // END - if
137
138 //
139 ?>