Fixes for missing user data in referal list
[mailer.git] / inc / stats-functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Init stats system
45 function initStatsSystem () {
46         // Is stats cache loaded?
47         if ((!isset($GLOBALS['stats_loaded'])) && (getConfig('STATS_ENABLED') == 'Y')) {
48                 // Init statistics array
49                 $GLOBALS['stats'] = array();
50
51                 // Load statistics entry from temporary table
52                 loadStatsTable();
53
54                 // Stats are loaded!
55                 $GLOBALS['stats_loaded'] = true;
56         } // END - if
57 }
58
59 // Checks if we have a statistics entry
60 function isStatsEntrySet ($entry) {
61         // Do we have the entry?
62         return (isset($GLOBALS['stats'][$entry]));
63 }
64
65 // Increments a statistics entry
66 function incrementStatsEntry ($entry, $amount=1) {
67         // Do we have stats enabled?
68         if (getConfig('STATS_ENABLED') != 'Y') return;
69
70         // Is it there?
71         if (isStatsEntrySet($entry)) {
72                 // Then increment it
73                 $GLOBALS['stats'][$entry] += $amount;
74         } else {
75                 // Write it
76                 $GLOBALS['stats'][$entry] = $amount;
77         }
78 }
79
80 // Getter for stats
81 function getStatsEntry ($entry) {
82         // Default is zero
83         $stats = 0;
84
85         // Is it there?
86         if (isStatsEntrySet($entry)) {
87                 // Then use it
88                 $stats = $GLOBALS['stats'][$entry];
89         } // END - if
90
91         // Return the value
92         return $stats;
93 }
94
95 // Setter for stats
96 function setStatsEntry ($entry, $value) {
97         $GLOBALS['stats'][$entry] = $value;
98 }
99
100 // Loads stats table
101 function loadStatsTable () {
102         // Do we have it there for today?
103         if (!isStatsTableCreated()) {
104                 // Then create a default one
105                 createStatsTable();
106         } // END - if
107
108         // Load it from database
109         $result = SQL_QUERY_ESC("SELECT `stats_entry`,`stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
110                 array(date('Ymd', time())), __FUNCTION__, __LINE__);
111
112         // Read all rows
113         while ($row = SQL_FETCHARRAY($result)) {
114                 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
115         } // END - while
116
117         // Free the result
118         SQL_FREERESULT($result);
119 }
120
121 // Checks if the the statistics table is created
122 function isStatsTableCreated () {
123         // Ask for it
124         $result = SQL_QUERY_ESC("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
125                 array(date('Ymd', time())), __FUNCTION__, __LINE__);
126
127         // Do we have a row?
128         return SQL_NUMROWS($result);
129 }
130
131 // Create the dummy table
132 function createStatsTable () {
133         // Create it here
134         $result = SQL_QUERY_ESC("CREATE TABLE IF NOT EXISTS `{?_MYSQL_PREFIX?}_stats_%s` (
135 `stats_entry` VARCHAR(100) NOT NULL DEFAULT '',
136 `stats_value` BIGINT(20) NOT NULL DEFAULT 0,
137 PRIMARY KEY (`stats_entry`)
138 ) TYPE=HEAP COMMENT='Temporary statistics table'",
139                 array(date('Ymd', time())), __FUNCTION__, __LINE__);
140 }
141
142 // Write all entries to the table
143 function writeStatsTable () {
144         // Empty the table first
145         SQL_QUERY_ESC("TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`",
146                 array(date('Ymd', time())), __FUNCTION__, __LINE__);
147
148         // Begin the SQL command
149         $sql = sprintf("INSERT INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`,`stats_value`) VALUES ",
150                 date('Ymd', time()));
151
152         // Add all entries to the final query
153         foreach ($GLOBALS['stats'] as $entry=>$value) {
154                 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
155         } // END - foreach
156
157         // Finalize it and run it
158         SQL_QUERY(substr($sql, 0, -1), __FUNCTION__, __LINE__);
159 }
160
161 // Filter for flushing statistics
162 function FILTER_FLUSH_STATS () {
163         // Now do we have stats?
164         if ((isset($GLOBALS['stats'])) && (getConfig('STATS_ENABLED') == 'Y')) {
165                 // Write statistics to temporary table
166                 writeStatsTable();
167         } // END - if
168 }
169
170 // [EOF]
171 ?>