Move busui to seperate repository
[bus.git] / browscap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
 
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2006 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
 
//require_once(OWA_BASE_DIR.DIRECTORY_SEPARATOR.'owa_base.php');
require_once(OWA_BASE_DIR.DIRECTORY_SEPARATOR.'ini_db.php');
 
/**
 * Browscap Class
 * 
 * Used to load and lookup user agents in a local Browscap file
 * 
 * @author      Peter Adams <peter@openwebanalytics.com>
 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
 * @category    owa
 * @package     owa
 * @version             $Revision$            
 * @since               owa 1.0.0
 */
 
class owa_browscap extends owa_base {
        
        
        /**
         * main browscap_db maintained by Gary Keith's 
         * Browser Capabilities project.
         *
         * @var array
         */
        var $browscap_db;
        
        /**
         * Browscap Record for current User agent
         *
         * @var unknown_type
         */
        var $browser;
        
        /**
         * Current user Agent
         *
         * @var string
         */
        var $ua;
        var $cache;
        var $cacheExpiration;
        
        function __construct($ua = '') {
                
                parent::__construct();
                // set user agent
                $this->ua = $ua;
                
                // init cache
                $this->cache = &owa_coreAPI::cacheSingleton(); 
                $this->cacheExpiration = owa_coreAPI::getSetting('base', 'default_cache_expiration_period');
                $this->cache->setCollectionExpirationPeriod('browscap', $this->cacheExpiration);
                //lookup robot in main browscap db
                $this->browser = $this->lookup($this->ua);
                $this->e->debug('Browser Name : '. $this->browser->Browser);
                
        }
        
        function robotCheck() {
                // must use == due to wacky type issues with phpBrowsecap ini file
                if ($this->browser->Crawler == "true" || $this->browser->Crawler == "1") {
                        return true;
                } elseif ($this->browser->Browser === "Default Browser") {
                        return $this->robotRegexCheck();
                }
                
                return false;
        }
        
        function lookup($user_agent) {
                
                if (owa_coreAPI::getSetting('base','cache_objects') === true) {
                        owa_coreAPI::profile($this, __FUNCTION__, __LINE__);
                        $cache_obj = $this->cache->get('browscap', $this->ua);
                }
                
                if (!empty($cache_obj)) {
                        owa_coreAPI::profile($this, __FUNCTION__, __LINE__);
                        return $cache_obj;
                                        
                } else {
                        owa_coreAPI::profile($this, __FUNCTION__, __LINE__);
                                                
                        // Load browscap file into memory
                        $user_browscap_file = OWA_DATA_DIR.'browscap/php_browscap.ini';
                        // check to see if a user downloaded version of the file exists
                        if ( file_exists( $user_browscap_file ) ) {
                                $this->browscap_db = $this->load( $user_browscap_file );        
                        } else {
                                $this->browscap_db = $this->load( $this->config['browscap.ini'] );
                        }
                
                        $cap = null;
                        
                        foreach ($this->browscap_db as $key=>$value) {
                                  if (($key!='*')&&(!array_key_exists('Parent',$value))) continue;
                                  $keyEreg='^'.str_replace(
                                   array('\\','.','?','*','^','$','[',']','|','(',')','+','{','}','%'),
                                   array('\\\\','\\.','.','.*','\\^','\\$','\\[','\\]','\\|','\\(','\\)','\\+','\\{','\\}','\\%'),
                                   $key).'$';
                                  if (preg_match('%'.$keyEreg.'%i',$user_agent))
                                  {
                                   $cap=array('browser_name_regex'=>strtolower($keyEreg),'browser_name_pattern'=>$key)+$value;
                                   $maxDeep=8;
                                   while (array_key_exists('Parent',$value)&&(--$maxDeep>0))
                                        $cap += ($value = $this->browscap_db[$value['Parent']]);
                                   break;
                                  }
                         }
                         
                        if ( ! empty( $cap ) ) {
                                
                                if ( $this->config['cache_objects'] == true ) {
                                        if ( $cap['Browser'] != 'Default Browser' ) {
                                                $this->cache->set( 'browscap', $this->ua, (object)$cap, $this->cacheExpiration );
                                        }
                                }
                        }
 
                        return ( (object)$cap );
                }
        }
        
        function load($file) {
        
                if(defined('INI_SCANNER_RAW')) {
                return parse_ini_file($file, true, INI_SCANNER_RAW);
        } else {
                return parse_ini_file($file, true);
        }
 
        }
        
        function robotRegexCheck() {
                
                $db = new ini_db(OWA_CONF_DIR.'robots.ini');
                owa_coreAPI::debug('Checking for robot strings...');
                $match = $db->contains($this->ua);
                
                if (!empty($match)):
                        owa_coreAPI::debug('Robot detect string found.');
                        $this->browser->Crawler = true;
                        return true;
                else:
                        return false;
                endif;
        
        }
        
        function get($name) {
        
                return $this->browser->$name;
        }
        
}
 
?>