83584677776085fa5f208aafb22e5160666e9d7b
[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')) || (!isAutoPurgingActive()) || (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
58         `timestamp` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
59 ORDER BY
60         `sender` ASC", __FILE__, __LINE__);
61
62 // Reset counter...
63 $deletedStats = '0';
64
65 // Do we have "purged" mails?
66 if (!SQL_HASZERONUMS($result_mails)) {
67         // Okay, check for their sender's
68         while ($content = SQL_FETCHARRAY($result_mails)) {
69                 // Check now...
70                 if (!fetchUserData($content['sender'])) {
71                         // Okay we found some mails!
72                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_pool` WHERE `sender`=%s",
73                                 array(bigintval($content['sender'])), __FILE__, __LINE__);
74
75                         // Get all affected (deleted) rows
76                         $deletedStats += SQL_AFFECTEDROWS();
77
78                         // Reset query (to prevent possible errors) ...;
79                         $result_mails = SQL_QUERY("SELECT
80         `sender`
81 FROM
82         `{?_MYSQL_PREFIX?}_pool`
83 WHERE
84         `data_type`='DELETED' AND
85         `timestamp` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
86 ORDER BY
87         `sender` ASC", __FILE__, __LINE__);
88                 } // END - if
89         } // END - while
90 } // END - if
91
92 // Free memory
93 SQL_FREERESULT($result_mails);
94
95 // Now let's check for stats entries as well;
96 $result_mails = SQL_QUERY("SELECT
97         `userid` AS sender
98 FROM
99         `{?_MYSQL_PREFIX?}_user_stats`
100 WHERE
101         `timestamp_send` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
102 ORDER BY
103         `userid` ASC", __FILE__, __LINE__);
104
105 // Do we have "purged" mails?
106 if (!SQL_HASZERONUMS($result_mails)) {
107         // Okay, check for their sender's
108         while ($content = SQL_FETCHARRAY($result_mails)) {
109                 // Check now...
110                 if (!fetchUserData($content['sender'])) {
111                         // Okay we found some mails!
112                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_user_stats` WHERE `userid`=%s",
113                                 array(bigintval($content['userid'])), __FILE__, __LINE__);
114
115                         // Get all affected (deleted) rows
116                         $deletedStats += SQL_AFFECTEDROWS();
117
118                         // Reset query (to prevent possible errors) ...
119                         $result_mails = SQL_QUERY("SELECT
120         `userid`
121 FROM
122         `{?_MYSQL_PREFIX?}_user_stats`
123 WHERE
124         `timestamp_send` <= (UNIX_TIMESTAMP() - {?ap_dm_timeout?})
125 ORDER BY
126         `userid` ASC", __FILE__, __LINE__);
127                 } // END - if
128         } // END - while
129 } // END - if
130
131 // Free memory
132 SQL_FREERESULT($result_mails);
133
134 // Do we have deleted mails and the admin want's to receive a notification
135 if (($deletedStats > 0) && (getConfig('ap_dm_notify') == 'Y')) {
136         // Send out email to admin
137         sendAdminNotification('{--ADMIN_AUTOPURGE_DELETE_MAILS_SUBJECT--}', 'admin_autopurge_del_mails', $deletedStats);
138 } // END - if
139
140 //
141 ?>