2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/25/2009 *
4 * =================== Last change: 10/25/2009 *
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 * Copyright (c) 2003 - 2009 by Roland Haeder *
14 * Copyright (c) 2009 - 2013 by Mailer Developer Team *
15 * For more information visit: http://mxchange.org *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
31 ************************************************************************/
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
39 function initStatsSystem () {
40 // Is stats cache loaded?
41 if ((!isset($GLOBALS['stats_loaded'])) && (ifInternalStatsEnabled())) {
42 // Init statistics array
43 $GLOBALS['stats'] = array();
45 // Load statistics entry from temporary table
49 $GLOBALS['stats_loaded'] = TRUE;
53 // Checks if a statistics entry has been set
54 function isStatsEntrySet ($entry) {
55 // Is there the entry?
56 return (isset($GLOBALS['stats'][$entry]));
59 // Increments a statistics entry
60 function incrementStatsEntry ($entry, $amount=1) {
61 // Is there stats enabled?
62 if ((!ifInternalStatsEnabled()) && ($entry != 'db_hits')) {
68 if (isStatsEntrySet($entry)) {
70 $GLOBALS['stats'][$entry] += $amount;
73 setStatsEntry($entry, $amount);
78 function getStatsEntry ($entry) {
83 if (isStatsEntrySet($entry)) {
85 $stats = $GLOBALS['stats'][$entry];
93 function setStatsEntry ($entry, $value) {
94 $GLOBALS['stats'][$entry] = $value;
98 function loadStatsTable () {
99 // Check if the link is up
100 if (!isSqlLinkUp()) return FALSE;
102 // Is it there for today?
103 if (!isStatsTableCreated()) {
104 // Then create a default one
108 // Load it from database
109 $result = sqlQueryEscaped("SELECT `stats_entry`, `stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
111 generateDateTime(time(), '6')
112 ), __FUNCTION__, __LINE__);
115 while ($row = sqlFetchArray($result)) {
116 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
120 sqlFreeResult($result);
123 // Checks if the the statistics table is created
124 function isStatsTableCreated () {
125 // Check if the link is up
126 if (!isSqlLinkUp()) return FALSE;
129 $result = sqlQueryEscaped("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
130 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
133 return sqlNumRows($result);
136 // Create the dummy table
137 function createStatsTable () {
138 // Check if the link is up
139 if (!isSqlLinkUp()) return FALSE;
142 $result = sqlQueryEscaped("CREATE TEMPORARY TABLE IF NOT EXISTS `{?_MYSQL_PREFIX?}_stats_%s` (
143 `stats_entry` VARCHAR(100) NOT NULL DEFAULT '',
144 `stats_value` BIGINT(20) NOT NULL DEFAULT 0,
145 PRIMARY KEY (`stats_entry`)
146 ) ENGINE = HEAP COMMENT = 'Temporary statistics table'",
147 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
150 // Write all entries to the table
151 function writeStatsTable () {
152 // Check if the link is up
153 if (!isSqlLinkUp()) return FALSE;
155 // Empty the table first
156 sqlQueryEscaped('TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`',
157 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
159 // Begin the SQL command
160 $sql = sprintf('REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`, `stats_value`) VALUES ',
161 generateDateTime(time(), '6'));
163 // Add all entries to the final query
164 foreach ($GLOBALS['stats'] as $entry => $value) {
165 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
168 // Finalize it and run it
169 sqlQuery(substr($sql, 0, -1), __FUNCTION__, __LINE__);
172 // Filter for flushing statistics
173 function FILTER_FLUSH_STATS () {
174 // Now do we have stats?
175 if ((isset($GLOBALS['stats'])) && (!isInstaller()) && (ifInternalStatsEnabled())) {
176 // Write statistics to temporary table
179 // Count db/cache hits in database
185 getStatsEntry('db_hits'),
186 getStatsEntry('cache_hits')