root/branches/user_agent/stats.php

Revision 72, 4.6 KB (checked in by daris, 3 years ago)

user_agent: releasing 1.1

Line 
1<?php
2
3if (!defined('FORUM_ROOT'))
4        define('FORUM_ROOT', '../../');
5require FORUM_ROOT.'include/common.php';
6
7if (!defined('EXT_ROOT'))
8        define('EXT_ROOT', './');
9require EXT_ROOT.'functions.php';
10
11
12define('FORUM_PAGE', 'useragent-stats');
13require FORUM_ROOT.'header.php';
14
15$user_agent_cache = array();
16
17// START SUBST - <!-- forum_main -->
18ob_start();
19
20?>
21<style type="text/css">
22
23.ua-stats div.ua-box {
24        clear: both;
25}
26
27.ua-stats div img {
28        float: left;
29        margin-right: 5px;
30}
31
32.ua-stats span.ua-left {
33        width: 5%;
34        float: left;
35}
36
37.ua-stats span.ua-right {
38        display: block;
39        padding-top: 3px;
40}
41
42</style>
43<?php
44
45$useragent_stats = array();
46
47if (file_exists(FORUM_CACHE_DIR.'cache_useragent_stats.php'))
48        require FORUM_CACHE_DIR.'cache_useragent_stats.php';
49
50// generate cache if it is older than 3 hours
51if (!isset($useragent_stats['timestamp']) || isset($useragent_stats['timestamp']) && time() - $useragent_stats['timestamp'] > 3600 * 3)
52{       
53        // Fetch some info about the topic
54        $query = array(
55                'SELECT'        => 'p.user_agent',
56                'FROM'          => 'posts AS p',
57                'WHERE'         => 'p.user_agent IS NOT NULL',
58                'ORDER BY'      => 'p.user_agent ASC',
59        );
60
61        ($hook = get_hook('vt_qr_get_topic_info')) ? eval($hook) : null;
62        $result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
63
64        while ($cur_post = $forum_db->fetch_assoc($result))
65        {
66                $agent = $cur_post['user_agent'];
67                if (!$agent)
68                        continue;
69               
70                $system = $browser = '';
71               
72                // Load from cache
73                if (isset($user_agent_cache[$agent]))
74                {
75                        $agent_names = $user_agent_cache[$agent];
76                }
77                else
78                {
79                        $agent_names = get_useragent_names($agent);
80                        $user_agent_cache[$agent] = $agent_names;
81                }
82               
83                if ($agent_names['system'])
84                {
85                        if (isset($system_count[$agent_names['system']]))
86                                $system_count[$agent_names['system']]++;
87                        else
88                                $system_count[$agent_names['system']] = 1;
89                               
90                        $system_count_all++;
91                }
92
93                if ($agent_names['browser_img'])
94                {
95                        if (isset($browser_count[$agent_names['browser_name']]))
96                                $browser_count[$agent_names['browser_name']]++;
97                        else
98                                $browser_count[$agent_names['browser_name']] = 1;
99                }
100        }
101
102        arsort($system_count);
103        arsort($browser_count);
104       
105        $useragent_stats = array(
106                'system'                => $system_count,
107                'browser'               => $browser_count,
108                'timestamp'             => time(),
109        );
110
111        // Output config as PHP code
112        $fh = @fopen(FORUM_CACHE_DIR.'cache_useragent_stats.php', 'wb');
113        if (!$fh)
114                error('Unable to write configuration cache file to cache directory. Please make sure PHP has write access to the directory \'cache\'.', __FILE__, __LINE__);
115
116        fwrite($fh, '<?php'."\n\n".'$useragent_stats = '.var_export($useragent_stats, true).';'."\n\n".'?>');
117
118        fclose($fh);
119}
120
121$system_count = array_sum($useragent_stats['system']);
122$browser_count = array_sum($useragent_stats['browser']);
123
124$i = 0;
125foreach ($useragent_stats['system'] as $system => $count)
126{
127        if ($i > 20)
128                break;
129       
130        $forum_page['system_list'][] = '<span class="ua-left">'.$count.'</span>'.ua_chart($count, $system_count).'<img src="'.ua_get_filename($system, 'system').'" /><span class="ua-right">'.$system.'</span>';
131       
132        $i++;
133}
134
135$i = 0;
136
137foreach ($useragent_stats['browser'] as $browser => $count)
138{
139        if ($i > 30)
140                break;
141       
142        if (ua_str_contains($browser, 'Internet Explorer'))
143        {
144                $browser_name = $browser;
145                $ie_version = substr($browser, strlen($browser) - 3);
146                $ie_version = substr($ie_version, 0, 1);
147                if (intval($ie_version) > 6)
148                        $browser_name = 'Internet Explorer 7';
149                else
150                        $browser_name = 'Internet Explorer';
151        }
152        else
153                $browser_name = substr($browser, 0, strrpos($browser, ' '));
154
155
156        $forum_page['browser_list'][] = '<span class="ua-left">'.$count.'</span>'.ua_chart($count, $browser_count).'<img src="'.ua_get_filename($browser_name, 'browser').'" /><span class="ua-right">'.$browser.'</span>';
157       
158        $i++;
159}
160
161
162?>
163        <div class="main-head">
164                <h2 class="hn"><span>Stats</span></h2>
165        </div>
166
167        <div class="main-content main-frm ua-stats">
168
169<?php
170
171
172?>             
173                        <div class="ct-box">
174                                <h2 class="hn">Systems</h2>
175                                <div class="ua-box"><?php echo implode('</div><div class="ua-box">', $forum_page['system_list']) ?></div>
176                        </div>
177
178                        <div class="ct-box">
179                                <h2 class="hn">Browsers</h2>
180                                <div class="ua-box"><?php echo implode('</div><div class="ua-box">', $forum_page['browser_list']) ?></div>
181                        </div>
182<?php
183
184
185?>
186        </div>
187<?php
188
189
190$tpl_temp = forum_trim(ob_get_contents());
191$tpl_main = str_replace('<!-- forum_main -->', $tpl_temp, $tpl_main);
192ob_end_clean();
193// END SUBST - <!-- forum_main -->
194
195require FORUM_ROOT.'footer.php';
Note: See TracBrowser for help on using the browser.