Changeset 2131

Show
Ignore:
Timestamp:
02/22/2008 11:47:33 AM (9 months ago)
Author:
Shadowhand
Message:

Added Kohana::user_agent(), as a replacement for the User_agent library.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/system/core/Kohana.php

    r2094 r2131  
    876876 
    877877                // We return NULL, because it's less common than FALSE 
    878                 return; 
     878                return NULL; 
     879        } 
     880 
     881        /** 
     882         * Retrieves current user agent information: 
     883         * keys:        browser, version, platform, mobile, robot, referrer, languages, charsets 
     884         * tests:       is_browser, is_mobile, is_robot, accept_ 
     885         * 
     886         * @param   string   key or test name 
     887         * @param   string   used with "accept" tests: user_agent(accept_lang, en) 
     888         * @return  array    languages and charsets 
     889         * @return  string   all other keys 
     890         * @return  boolean  all tests 
     891         */ 
     892        public static function user_agent($key, $compare = NULL) 
     893        { 
     894                static $info; 
     895 
     896                // Return the raw string 
     897                if ($key === 'agent') 
     898                        return Kohana::$user_agent; 
     899 
     900                if ($info === NULL) 
     901                { 
     902                        // Parse the user agent and extract basic information 
     903                        foreach(Config::item('user_agents') as $type => $data) 
     904                        { 
     905                                foreach($data as $agent => $name) 
     906                                { 
     907                                        if (stripos(Kohana::$user_agent, $agent) !== FALSE) 
     908                                        { 
     909                                                if ($type === 'browser' AND preg_match('|'.preg_quote($agent).'[^0-9.]*([0-9.]+)|i', Kohana::$user_agent, $match)) 
     910                                                { 
     911                                                        // Set the browser version 
     912                                                        $info['version'] = $match[1]; 
     913                                                } 
     914 
     915                                                // Set the agent name 
     916                                                $info[$type] = $name; 
     917                                                break; 
     918                                        } 
     919                                } 
     920                        } 
     921                } 
     922 
     923                if (empty($info[$key])) 
     924                { 
     925                        switch ($key) 
     926                        { 
     927                                case 'is_robot': 
     928                                case 'is_browser': 
     929                                case 'is_mobile': 
     930                                        // A boolean result 
     931                                        $return = ! empty($info[substr($key, 3)]); 
     932                                break; 
     933                                case 'languages': 
     934                                        if ( ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) 
     935                                        { 
     936                                                if (preg_match_all('/[-a-z]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])), $matches)) 
     937                                                { 
     938                                                        // Found a result 
     939                                                        $return = $matches[0]; 
     940                                                } 
     941                                        } 
     942                                break; 
     943                                case 'charsets': 
     944                                        if ( ! empty($_SERVER['HTTP_ACCEPT_CHARSET'])) 
     945                                        { 
     946                                                if (preg_match_all('/[-a-z0-9]{2,}/', strtolower(trim($_SERVER['HTTP_ACCEPT_CHARSET'])), $matches)) 
     947                                                { 
     948                                                        // Found a result 
     949                                                        $return = $matches[0]; 
     950                                                } 
     951                                        } 
     952                                break; 
     953                                case 'referrer': 
     954                                        if ( ! empty($_SERVER['HTTP_REFERER'])) 
     955                                        { 
     956                                                // Found a result 
     957                                                $return = trim($_SERVER['HTTP_REFERER']); 
     958                                        } 
     959                                break; 
     960                        } 
     961 
     962                        // Cache the return value 
     963                        isset($return) and $info[$key] = $return; 
     964                } 
     965 
     966                if ( ! empty($compare)) 
     967                { 
     968                        // The comparison must always be lowercase 
     969                        $compare = strtolower($compare); 
     970 
     971                        switch ($key) 
     972                        { 
     973                                case 'accept_lang': 
     974                                        // Check if the lange is accepted 
     975                                        return in_array($compare, Kohana::user_agent('languages')); 
     976                                break; 
     977                                case 'accept_charset': 
     978                                        // Check if the charset is accepted 
     979                                        return in_array($compare, Kohana::user_agent('charsets')); 
     980                                break; 
     981                                default: 
     982                                        // Invalid comparison 
     983                                        return FALSE; 
     984                                break; 
     985                        } 
     986                } 
     987 
     988                // Return the key, if set 
     989                return isset($info[$key]) ? $info[$key] : NULL; 
    879990        } 
    880991