Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Net_UserAgent_Detect_APC.php
4
 *
5
 * PHP version 4.2
6
 *
7
 * Copyright (c) 1997-2003 The PHP Group
8
 *
9
 * This source file is subject to version 2.0 of the PHP license,
10
 * that is bundled with this package in the file LICENSE, and is
11
 * available at through the world-wide-web at
12
 * http://www.php.net/license/2_02.txt.
13
 * If you did not receive a copy of the PHP license and are unable to
14
 * obtain it through the world-wide-web, please send a note to
15
 * license@php.net so we can mail you a copy immediately.
16
 *
17
 * @category Net
18
 * @package  Net_UserAgent
19
 * @author   Lucas Nealan <lucas@facebook.com>
20
 * @license  http://www.php.net/license/2_02.txt PHP 2.0 Licence
21
 * @version  CVS: $Id: APC.php 281797 2009-06-08 04:49:23Z clockwerx $
22
 * @link     http://pear.php.net/package/Net_UserAgent_Detect
23
 */
24
 
25
require_once 'Net/UserAgent/Detect.php';
26
 
27
/**
28
 * Net_UserAgent_Detect_APC
29
 *
30
 * PHP version 4.2
31
 *
32
 * Copyright (c) 1997-2003 The PHP Group
33
 *
34
 * This source file is subject to version 2.0 of the PHP license,
35
 * that is bundled with this package in the file LICENSE, and is
36
 * available at through the world-wide-web at
37
 * http://www.php.net/license/2_02.txt.
38
 * If you did not receive a copy of the PHP license and are unable to
39
 * obtain it through the world-wide-web, please send a note to
40
 * license@php.net so we can mail you a copy immediately.
41
 *
42
 * @category Net
43
 * @package  Net_UserAgent
44
 * @author   Lucas Nealan <lucas@facebook.com>
45
 * @license  http://www.php.net/license/2_02.txt PHP 2.0 Licence
46
 * @link     http://pear.php.net/package/Net_UserAgent_Detect
47
 */
48
class Net_UserAgent_Detect_APC extends Net_UserAgent_Detect
49
{
50
    var $key = '';
51
 
52
    /**
53
     * Class constructor
54
     *
55
     * @param string $in_userAgent    (optional) User agent override.
56
     * @param mixed  $in_detect       (optional) The level of checking to do.
57
     * @param mixed  $ua_cache_window Unknown
58
     */
59
    function Net_UserAgent_Detect_APC($in_userAgent = null, $in_detect = null,
60
                                      $ua_cache_window = 600)
61
    {
62
        $data     = '';
63
        $restored = false;
64
 
65
        // don't cache after time period
66
        $ua_cache_timeout = apc_fetch('useragent:cache_timeout');
67
 
68
        if ($ua_cache_window > 0) {
69
            if (!$ua_cache_timeout) {
70
                // check apc uptime and disable after x mins
71
                $apc_data = apc_cache_info('file', true);
72
 
73
                if (isset($apc_data['start_time'])) {
74
                    $uptime = $apc_data['start_time'];
75
 
76
                    // timeout and disable after 10 minutes of uptime
77
                    if (time() - $uptime > $ua_cache_window) {
78
                        apc_store('useragent:cache_timeout', true);
79
                        $ua_cache_timeout = true; // don't cache this one either
80
                    }
81
                }
82
            }
83
 
84
            if (!$this->key) {
85
                $key_flags = '';
86
                if ($in_detect !== null) {
87
                    $key_flags = implode('-', $in_detect);
88
                }
89
                $this->key = 'useragent:'.md5($in_userAgent.$key_flags);
90
            }
91
 
92
            if ($data = apc_fetch($this->key)) {
93
                $success = null;
94
                $data    = unserialize($data);
95
 
96
                if ($data) {
97
                    $restored = $this->cacheRestore($data);
98
                }
99
            }
100
        }
101
 
102
        if (!$data) {
103
            $this->detect($in_userAgent, $in_detect);
104
 
105
            if ($ua_cache_window > 0 && !$ua_cache_timeout) {
106
                $this->cacheSave();
107
            }
108
        }
109
    }
110
 
111
    /**
112
     * To be used in place of the contructor to return only open instance.
113
     *
114
     * @param string $in_userAgent (optional) User agent override.
115
     * @param mixed  $in_detect    (optional) The level of checking to do.
116
     *
117
     * @access public
118
     * @return object Net_UserAgent_Detect instance
119
     */
120
    function &singleton($in_userAgent = null, $in_detect = null)
121
    {
122
        static $instance;
123
 
124
        if (!isset($instance)) {
125
            $instance = new Net_UserAgent_Detect_APC($in_userAgent, $in_detect);
126
        }
127
 
128
        return $instance;
129
    }
130
 
131
    /**
132
     * Restore cached items
133
     *
134
     * @param mixed[] $cache An array of items to restore
135
     *
136
     * @return bool
137
     */
138
    function cacheRestore($cache)
139
    {
140
        if (is_array($cache)) {
141
            foreach ($cache as $prop => $value) {
142
                $ptr = Net_UserAgent_Detect::_getStaticProperty($prop);
143
                $ptr = $value;
144
            }
145
 
146
            return true;
147
        }
148
 
149
        return false;
150
    }
151
 
152
    /**
153
     * Store items in APC
154
     *
155
     * @return void
156
     */
157
    function cacheSave()
158
    {
159
        if ($this->key) {
160
            $items = array('browser',
161
                          'features',
162
                          'leadingIdentifier',
163
                          'majorVersion',
164
                          'options',
165
                          'os',
166
                          'quirks',
167
                          'subVersion',
168
                          'userAgent',
169
                          'version');
170
 
171
            $data = array();
172
            foreach ($items as $item) {
173
                $data[$item] = Net_UserAgent_Detect::_getStaticProperty($item);
174
            }
175
 
176
            apc_store($this->key, serialize($data));
177
        }
178
    }
179
}
180
?>