home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Net / UserAgent / Detect / APC.php
Encoding:
PHP Script  |  2008-07-02  |  4.6 KB  |  115 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4.2                                                      |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Lucas Nealan <lucas@facebook.com>                           |
  16. // +----------------------------------------------------------------------+
  17.  
  18. // $Id: APC.php,v 1.1 2007/09/19 21:35:22 jrust Exp $
  19.  
  20. require_once 'Net_UserAgent/detect.php';
  21.  
  22. class Net_UserAgent_Detect_APC extends Net_UserAgent_Detect
  23. {
  24.     var $key = '';
  25.  
  26.     function Net_UserAgent_Detect_APC($in_userAgent = null, $in_detect = null, $ua_cache_window = 600)
  27.     {
  28.         $data = '';
  29.         $restored = false;
  30.         $ua_cache_timeout = apc_fetch('useragent:cache_timeout');               // don't cache after time period
  31.  
  32.         if ($ua_cache_window > 0) {
  33.             if (!$ua_cache_timeout) {
  34.                 // check apc uptime and disable after x mins
  35.                 $apc_data = apc_cache_info('file', true);
  36.  
  37.                 if (isset($apc_data['start_time'])) {
  38.                     $uptime = $apc_data['start_time'];
  39.  
  40.                     if (time() - $uptime > $ua_cache_window) { // timeout and disable after 10 minutes of uptime
  41.                         apc_store('useragent:cache_timeout', true);
  42.                         $ua_cache_timeout = true; // don't cache this one either
  43.                     }
  44.                 }
  45.             }
  46.  
  47.             if (!$this->key) {
  48.                 $key_flags = '';
  49.                 if ($in_detect !== null) {
  50.                     $key_flags = implode('-', $in_detect);
  51.                 }
  52.                 $this->key = 'useragent:'.md5($in_userAgent.$key_flags);
  53.             }
  54.  
  55.             if ($data = apc_fetch($this->key)) {
  56.                 $success = null;
  57.                 $data = unserialize($data);
  58.                 if ($data) {
  59.                     $restored = $this->cache_restore($data);
  60.                 }
  61.             }
  62.         }
  63.  
  64.         if (!$data) {
  65.             $this->detect($in_userAgent, $in_detect);
  66.  
  67.             if ($ua_cache_window > 0 && !$ua_cache_timeout) {
  68.                 $this->cache_save();
  69.             }
  70.         }
  71.     }
  72.  
  73.     function &singleton($in_userAgent = null, $in_detect = null) 
  74.     {
  75.         static $instance;
  76.  
  77.         if (!isset($instance)) {
  78.             $instance = new Net_UserAgent_Detect_APC($in_userAgent, $in_detect);
  79.         }
  80.  
  81.         return $instance;
  82.     }
  83.  
  84.     function cache_restore($cache) 
  85.     {
  86.         if (is_array($cache)) {
  87.             foreach($cache as $prop => $value) {
  88.                 $ptr = Net_UserAgent_Detect::_getStaticProperty($prop);
  89.                 $ptr = $value;
  90.             }
  91.             return true;
  92.         }
  93.         return false;
  94.     }
  95.  
  96.     function cache_save() 
  97.     {
  98.         if ($this->key) {
  99.             $data = array('browser'           => Net_UserAgent_Detect::_getStaticProperty('browser'),
  100.                           'features'          => Net_UserAgent_Detect::_getStaticProperty('features'),
  101.                           'leadingIdentifier' => Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier'),
  102.                           'majorVersion'      => Net_UserAgent_Detect::_getStaticProperty('majorVersion'),
  103.                           'options'           => Net_UserAgent_Detect::_getStaticProperty('options'),
  104.                           'os'                => Net_UserAgent_Detect::_getStaticProperty('os'),
  105.                           'quirks'            => Net_UserAgent_Detect::_getStaticProperty('quirks'),
  106.                           'subVersion'        => Net_UserAgent_Detect::_getStaticProperty('subVersion'),
  107.                           'userAgent'         => Net_UserAgent_Detect::_getStaticProperty('userAgent'),
  108.                           'version'           => Net_UserAgent_Detect::_getStaticProperty('version'),
  109.                          );
  110.             apc_store($this->key, serialize($data));
  111.         }
  112.     }
  113. }
  114. ?>
  115.