session_(un)register are deprecated as of 5.3.1
[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  * 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 // Init stats system
46 function initStatsSystem () {
47         // Is stats cache loaded?
48         if ((!isset($GLOBALS['stats_loaded'])) && (getConfig('STATS_ENABLED') == 'Y')) {
49                 // Init statistics array
50                 $GLOBALS['stats'] = array();
51
52                 // Load statistics entry from temporary table
53                 loadStatsTable();
54
55                 // Stats are loaded!
56                 $GLOBALS['stats_loaded'] = true;
57         } // END - if
58 }
59
60 // Checks if we have a statistics entry
61 function isStatsEntrySet ($entry) {
62         // Do we have the entry?
63         return (isset($GLOBALS['stats'][$entry]));
64 }
65
66 // Increments a statistics entry
67 function incrementStatsEntry ($entry, $amount=1) {
68         // Do we have stats enabled?
69         if (getConfig('STATS_ENABLED') != 'Y') return;
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(date('Ymd', time())), __FUNCTION__, __LINE__);
115
116         // Read all rows
117         while ($row = SQL_FETCHARRAY($result)) {
118                 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
119         } // END - while
120
121         // Free the result
122         SQL_FREERESULT($result);
123 }
124
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;
129
130         // Ask for it
131         $result = SQL_QUERY_ESC("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
132                 array(date('Ymd', time())), __FUNCTION__, __LINE__);
133
134         // Do we have a row?
135         return SQL_NUMROWS($result);
136 }
137
138 // Create the dummy table
139 function createStatsTable () {
140         // Check if the link is up
141         if (!SQL_IS_LINK_UP()) return false;
142
143         // Create it here
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(date('Ymd', time())), __FUNCTION__, __LINE__);
150 }
151
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;
156
157         // Empty the table first
158         SQL_QUERY_ESC("TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`",
159                 array(date('Ymd', time())), __FUNCTION__, __LINE__);
160
161         // Begin the SQL command
162         $sql = sprintf("REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`,`stats_value`) VALUES ",
163                 date('Ymd', time()));
164
165         // Add all entries to the final query
166         foreach ($GLOBALS['stats'] as $entry => $value) {
167                 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
168         } // END - foreach
169
170         // Finalize it and run it
171         SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
172 }
173
174 // Filter for flushing statistics
175 function FILTER_FLUSH_STATS () {
176         // Now do we have stats?
177         if ((isset($GLOBALS['stats'])) && (!isInstallationPhase()) && (getConfig('STATS_ENABLED') == 'Y')) {
178                 // Write statistics to temporary table
179                 writeStatsTable();
180         } // END - if
181 }
182
183 // [EOF]
184 ?>