Hotfix
[mailer.git] / inc / stats-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/25/2009 *
4  * ===================                          Last change: 10/25/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : stats-functions.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 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://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 // Init stats system
44 function initStatsSystem () {
45         // Is stats cache loaded?
46         if ((!isset($GLOBALS['stats_loaded'])) && (ifInternalStatsEnabled())) {
47                 // Init statistics array
48                 $GLOBALS['stats'] = array();
49
50                 // Load statistics entry from temporary table
51                 loadStatsTable();
52
53                 // Stats are loaded!
54                 $GLOBALS['stats_loaded'] = true;
55         } // END - if
56 }
57
58 // Checks if we have a statistics entry
59 function isStatsEntrySet ($entry) {
60         // Do we have the entry?
61         return (isset($GLOBALS['stats'][$entry]));
62 }
63
64 // Increments a statistics entry
65 function incrementStatsEntry ($entry, $amount=1) {
66         // Do we have stats enabled?
67         if (!ifInternalStatsEnabled()) {
68                 return;
69         } // END - if
70
71         // Is it there?
72         if (isStatsEntrySet($entry)) {
73                 // Then increment it
74                 $GLOBALS['stats'][$entry] += $amount;
75         } else {
76                 // Write it
77                 $GLOBALS['stats'][$entry] = $amount;
78         }
79 }
80
81 // Getter for stats
82 function getStatsEntry ($entry) {
83         // Default is zero
84         $stats = '0';
85
86         // Is it there?
87         if (isStatsEntrySet($entry)) {
88                 // Then use it
89                 $stats = $GLOBALS['stats'][$entry];
90         } // END - if
91
92         // Return the value
93         return $stats;
94 }
95
96 // Setter for stats
97 function setStatsEntry ($entry, $value) {
98         $GLOBALS['stats'][$entry] = $value;
99 }
100
101 // Loads stats table
102 function loadStatsTable () {
103         // Check if the link is up
104         if (!SQL_IS_LINK_UP()) return false;
105
106         // Do we have it there for today?
107         if (!isStatsTableCreated()) {
108                 // Then create a default one
109                 createStatsTable();
110         } // END - if
111
112         // Load it from database
113         $result = SQL_QUERY_ESC("SELECT `stats_entry`,`stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
114                 array(
115                         generateDateTime(time(), '6')
116                 ), __FUNCTION__, __LINE__);
117
118         // Read all rows
119         while ($row = SQL_FETCHARRAY($result)) {
120                 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
121         } // END - while
122
123         // Free the result
124         SQL_FREERESULT($result);
125 }
126
127 // Checks if the the statistics table is created
128 function isStatsTableCreated () {
129         // Check if the link is up
130         if (!SQL_IS_LINK_UP()) return false;
131
132         // Ask for it
133         $result = SQL_QUERY_ESC("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
134                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
135
136         // Do we have a row?
137         return SQL_NUMROWS($result);
138 }
139
140 // Create the dummy table
141 function createStatsTable () {
142         // Check if the link is up
143         if (!SQL_IS_LINK_UP()) return false;
144
145         // Create it here
146         $result = SQL_QUERY_ESC("CREATE TEMPORARY TABLE IF NOT EXISTS `{?_MYSQL_PREFIX?}_stats_%s` (
147 `stats_entry` VARCHAR(100) NOT NULL DEFAULT '',
148 `stats_value` BIGINT(20) NOT NULL DEFAULT 0,
149 PRIMARY KEY (`stats_entry`)
150 ) ENGINE = HEAP COMMENT = 'Temporary statistics table'",
151                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
152 }
153
154 // Write all entries to the table
155 function writeStatsTable () {
156         // Check if the link is up
157         if (!SQL_IS_LINK_UP()) return false;
158
159         // Empty the table first
160         SQL_QUERY_ESC("TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`",
161                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
162
163         // Begin the SQL command
164         $sql = sprintf("REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`,`stats_value`) VALUES ",
165                 generateDateTime(time(), '6'));
166
167         // Add all entries to the final query
168         foreach ($GLOBALS['stats'] as $entry => $value) {
169                 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
170         } // END - foreach
171
172         // Finalize it and run it
173         SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
174 }
175
176 // Filter for flushing statistics
177 function FILTER_FLUSH_STATS () {
178         // Now do we have stats?
179         if ((isset($GLOBALS['stats'])) && (!isInstallationPhase()) && (ifInternalStatsEnabled())) {
180                 // Write statistics to temporary table
181                 writeStatsTable();
182         } // END - if
183 }
184
185 // [EOF]
186 ?>