Example #1
0
 /**
  * download and parse the browscap file
  *
  * @return	array	array with parsed download info, or empty if the download is disabled of failed
  */
 protected static function parse_browscap()
 {
     // get the browscap.ini file
     switch (static::$config['browscap']['method']) {
         case 'local':
             if (!file_exists(static::$config['browscap']['file']) or filesize(static::$config['browscap']['file']) == 0) {
                 throw new \Exception('Agent class: could not open the local browscap.ini file.');
             }
             $data = @file_get_contents(static::$config['browscap']['file']);
             break;
             // socket connections are not implemented yet!
         // socket connections are not implemented yet!
         case 'sockets':
             $data = false;
             break;
         case 'curl':
             $curl = curl_init();
             curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
             curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
             curl_setopt($curl, CURLOPT_HEADER, 0);
             curl_setopt($curl, CURLOPT_USERAGENT, 'Fuel PHP framework - Agent class (http://fuelphp.com)');
             curl_setopt($curl, CURLOPT_URL, static::$config['browscap']['url']);
             $data = curl_exec($curl);
             curl_close($curl);
             break;
         case 'wrapper':
             ini_set('user_agent', 'Fuel PHP framework - Agent class (http://fuelphp.com)');
             $data = file_get_contents(static::$config['browscap']['url']);
         default:
             break;
     }
     if ($data === false) {
         logger(\Fuel::L_ERROR, 'Failed to download browscap.ini file.', 'Agent::parse_browscap');
     }
     // parse the downloaded data
     $browsers = @parse_ini_string($data, true, INI_SCANNER_RAW) or $browsers = array();
     // remove the version and timestamp entry
     array_shift($browsers);
     $result = array();
     // reverse sort on key string length
     uksort($browsers, function ($a, $b) {
         return strlen($a) < strlen($b) ? 1 : -1;
     });
     $index = array();
     $count = 0;
     // reduce the array keys
     foreach ($browsers as $browser => $properties) {
         $index[$browser] = $count++;
         // fix any type issues
         foreach ($properties as $var => $value) {
             if (is_numeric($value)) {
                 $properties[$var] = $value + 0;
             } elseif ($value == 'true') {
                 $properties[$var] = true;
             } elseif ($value == 'false') {
                 $properties[$var] = false;
             }
         }
         $result[$browser] = \Arr::replace_key($properties, static::$keys);
     }
     // reduce parent links to
     foreach ($result as $browser => &$properties) {
         if (array_key_exists('Parent', $properties)) {
             if ($properties['Parent'] == 'DefaultProperties') {
                 unset($properties['Parent']);
             } else {
                 if (array_key_exists($properties['Parent'], $index)) {
                     $properties['Parent'] = $index[$properties['Parent']];
                 } else {
                     unset($properties['Parent']);
                 }
             }
         }
     }
     // save the result to the cache
     if (!empty($result)) {
         $cache = \Cache::forge(static::$config['cache']['identifier'] . '.browscap');
         $cache->set($result, static::$config['cache']['expiry']);
     }
     return $result;
 }
 /**
  * Insert before a specific key.
  * 
  * @access public
  * @return void
  */
 public function insert_before($key, $title, $callback, $ukey = null)
 {
     $this->prepare_insert($callback, $ukey);
     \Arr::insert_before_key($this->properties, $title, $key);
     $this->properties = \Arr::replace_key($this->properties, array(0 => $ukey));
     return $this;
 }