Do we still need this query reset?
[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  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
20  * For more information visit: http://www.mxchange.org                  *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Abort if autopurge is not active or disabled by admin
44 if ((!isExtensionActive('autopurge')) || (!isAutoPurgingActive()) || (getConfig('ap_del_mails') != 'Y')) {
45         // Abort here
46         return false;
47 } // END - if
48
49 // Okay, let's check for deleted mails
50 $result_mails = SQL_QUERY("SELECT
51         `sender`
52 FROM
53         `{?_MYSQL_PREFIX?}_pool`
54 WHERE
55         `data_type`='DELETED' AND
56         `timestamp` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
57 ORDER BY
58         `sender` ASC", __FILE__, __LINE__);
59
60 // Reset counter...
61 $deletedStats = '0';
62
63 // Do we have "purged" mails?
64 if (!SQL_HASZERONUMS($result_mails)) {
65         // Okay, check for their sender's
66         while ($content = SQL_FETCHARRAY($result_mails)) {
67                 // Check now...
68                 if (!fetchUserData($content['sender'])) {
69                         // Okay we found some mails!
70                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_pool` WHERE `sender`=%s",
71                                 array(bigintval($content['sender'])), __FILE__, __LINE__);
72
73                         // Get all affected (deleted) rows
74                         $deletedStats += SQL_AFFECTEDROWS();
75
76                         // Reset query (to prevent possible errors) ...;
77                         $result_mails = SQL_QUERY("SELECT
78         `sender`
79 FROM
80         `{?_MYSQL_PREFIX?}_pool`
81 WHERE
82         `data_type`='DELETED' AND
83         `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         `userid` ASC", __FILE__, __LINE__);
102
103 // Do we have "purged" mails?
104 if (!SQL_HASZERONUMS($result_mails)) {
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 `userid`=%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         `userid` 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('{--ADMIN_AUTOPURGE_DELETE_MAILS_SUBJECT--}', 'admin_autopurge_del_mails', $deletedStats);
136 } // END - if
137
138 // [EOF]
139 ?>