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 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
44 function initStatsSystem () {
45 // Is stats cache loaded?
46 if ((!isset($GLOBALS['stats_loaded'])) && (ifStatsAreEnabled())) {
47 // Init statistics array
48 $GLOBALS['stats'] = array();
50 // Load statistics entry from temporary table
54 $GLOBALS['stats_loaded'] = true;
58 // Checks if we have a statistics entry
59 function isStatsEntrySet ($entry) {
60 // Do we have the entry?
61 return (isset($GLOBALS['stats'][$entry]));
64 // Increments a statistics entry
65 function incrementStatsEntry ($entry, $amount=1) {
66 // Do we have stats enabled?
67 if (!ifStatsAreEnabled()) return;
70 if (isStatsEntrySet($entry)) {
72 $GLOBALS['stats'][$entry] += $amount;
75 $GLOBALS['stats'][$entry] = $amount;
80 function getStatsEntry ($entry) {
85 if (isStatsEntrySet($entry)) {
87 $stats = $GLOBALS['stats'][$entry];
95 function setStatsEntry ($entry, $value) {
96 $GLOBALS['stats'][$entry] = $value;
100 function loadStatsTable () {
101 // Check if the link is up
102 if (!SQL_IS_LINK_UP()) return false;
104 // Do we have it there for today?
105 if (!isStatsTableCreated()) {
106 // Then create a default one
110 // Load it from database
111 $result = SQL_QUERY_ESC("SELECT `stats_entry`, `stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
113 generateDateTime(time(), '6')
114 ), __FUNCTION__, __LINE__);
117 while ($row = SQL_FETCHARRAY($result)) {
118 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
122 SQL_FREERESULT($result);
125 // Checks if the the statistics table is created
126 function isStatsTableCreated () {
127 // Check if the link is up
128 if (!SQL_IS_LINK_UP()) return false;
131 $result = SQL_QUERY_ESC("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
132 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
135 return SQL_NUMROWS($result);
138 // Create the dummy table
139 function createStatsTable () {
140 // Check if the link is up
141 if (!SQL_IS_LINK_UP()) return false;
144 $result = SQL_QUERY_ESC("CREATE TABLE IF NOT EXISTS `{?_MYSQL_PREFIX?}_stats_%s` (
145 `stats_entry` VARCHAR(100) NOT NULL DEFAULT '',
146 `stats_value` BIGINT(20) NOT NULL DEFAULT 0,
147 PRIMARY KEY (`stats_entry`)
148 ) TYPE=HEAP COMMENT='Temporary statistics table'",
149 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
152 // Write all entries to the table
153 function writeStatsTable () {
154 // Check if the link is up
155 if (!SQL_IS_LINK_UP()) return false;
157 // Empty the table first
158 SQL_QUERY_ESC("TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`",
159 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
161 // Begin the SQL command
162 $sql = sprintf("REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`,`stats_value`) VALUES ",
163 generateDateTime(time(), '6'));
165 // Add all entries to the final query
166 foreach ($GLOBALS['stats'] as $entry => $value) {
167 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
170 // Finalize it and run it
171 SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
174 // Filter for flushing statistics
175 function FILTER_FLUSH_STATS () {
176 // Now do we have stats?
177 if ((isset($GLOBALS['stats'])) && (!isInstallationPhase()) && (ifStatsAreEnabled())) {
178 // Write statistics to temporary table