Removed deprecated 'hidden' column from mod_reg table.
[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  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2013 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
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.                                  *
21  *                                                                      *
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.                         *
26  *                                                                      *
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,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // Init stats system
39 function initStatsSystem () {
40         // Is stats cache loaded?
41         if ((!isset($GLOBALS['stats_loaded'])) && (ifInternalStatsEnabled())) {
42                 // Init statistics array
43                 $GLOBALS['stats'] = array();
44
45                 // Load statistics entry from temporary table
46                 loadStatsTable();
47
48                 // Stats are loaded!
49                 $GLOBALS['stats_loaded'] = TRUE;
50         } // END - if
51 }
52
53 // Checks if a statistics entry has been set
54 function isStatsEntrySet ($entry) {
55         // Is there the entry?
56         return (isset($GLOBALS['stats'][$entry]));
57 }
58
59 // Increments a statistics entry
60 function incrementStatsEntry ($entry, $amount=1) {
61         // Is there stats enabled?
62         if ((!ifInternalStatsEnabled()) && ($entry != 'db_hits')) {
63                 // Abort here
64                 return;
65         } // END - if
66
67         // Is it there?
68         if (isStatsEntrySet($entry)) {
69                 // Then increment it
70                 $GLOBALS['stats'][$entry] += $amount;
71         } else {
72                 // Write it
73                 setStatsEntry($entry, $amount);
74         }
75 }
76
77 // Getter for stats
78 function getStatsEntry ($entry) {
79         // Default is zero
80         $stats = '0';
81
82         // Is it there?
83         if (isStatsEntrySet($entry)) {
84                 // Then use it
85                 $stats = $GLOBALS['stats'][$entry];
86         } // END - if
87
88         // Return the value
89         return $stats;
90 }
91
92 // Setter for stats
93 function setStatsEntry ($entry, $value) {
94         $GLOBALS['stats'][$entry] = $value;
95 }
96
97 // Loads stats table
98 function loadStatsTable () {
99         // Check if the link is up
100         if (!isSqlLinkUp()) return FALSE;
101
102         // Is 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 = sqlQueryEscaped("SELECT `stats_entry`, `stats_value` FROM `{?_MYSQL_PREFIX?}_stats_%s` ORDER BY `stats_entry` ASC",
110                 array(
111                         generateDateTime(time(), '6')
112                 ), __FUNCTION__, __LINE__);
113
114         // Read all rows
115         while ($row = sqlFetchArray($result)) {
116                 $GLOBALS['stats'][$row['stats_entry']] = $row['stats_value'];
117         } // END - while
118
119         // Free the result
120         sqlFreeResult($result);
121 }
122
123 // Checks if the the statistics table is created
124 function isStatsTableCreated () {
125         // Check if the link is up
126         if (!isSqlLinkUp()) return FALSE;
127
128         // Ask for it
129         $result = sqlQueryEscaped("SHOW TABLES LIKE '{?_MYSQL_PREFIX?}_stats_%s'",
130                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
131
132         // Is there a row?
133         return sqlNumRows($result);
134 }
135
136 // Create the dummy table
137 function createStatsTable () {
138         // Check if the link is up
139         if (!isSqlLinkUp()) return FALSE;
140
141         // Create it here
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__);
148 }
149
150 // Write all entries to the table
151 function writeStatsTable () {
152         // Check if the link is up
153         if (!isSqlLinkUp()) return FALSE;
154
155         // Empty the table first
156         sqlQueryEscaped('TRUNCATE `{?_MYSQL_PREFIX?}_stats_%s`',
157                 array(generateDateTime(time(), '6')), __FUNCTION__, __LINE__);
158
159         // Begin the SQL command
160         $sql = sprintf('REPLACE INTO `{?_MYSQL_PREFIX?}_stats_%s` (`stats_entry`, `stats_value`) VALUES ',
161                 generateDateTime(time(), '6'));
162
163         // Add all entries to the final query
164         foreach ($GLOBALS['stats'] as $entry => $value) {
165                 $sql .= sprintf("('%s', %s),", $entry, bigintval($value));
166         } // END - foreach
167
168         // Finalize it and run it
169         sqlQuery(substr($sql, 0, -1), __FUNCTION__, __LINE__);
170 }
171
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
177                 writeStatsTable();
178
179                 // Count db/cache hits in database
180                 updateConfiguration(
181                         array(
182                                 'db_hits',
183                                 'cache_hits'
184                         ), array(
185                                 getStatsEntry('db_hits'),
186                                 getStatsEntry('cache_hits')
187                         ),
188                         '+'
189                 );
190         } // END - if
191 }
192
193 // [EOF]
194 ?>