Ignore:
Timestamp:
2008/03/12 21:08:31 (18 years ago)
Author:
adachi
Message:

merge r17127:17138 (update Net_UserAgent_Mobile)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/comu-ver2/data/module/Net/UserAgent/Mobile.php

    r15532 r17140  
    1414 * @package    Net_UserAgent_Mobile 
    1515 * @author     KUBO Atsuhiro <[email protected]> 
    16  * @copyright  2003-2007 KUBO Atsuhiro <[email protected]> 
     16 * @copyright  2003-2008 KUBO Atsuhiro <[email protected]> 
    1717 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 
    1818 * @version    CVS: $Id$ 
     
    2121 
    2222require_once dirname(__FILE__) . '/../../PEAR.php'; 
    23 require_once dirname(__FILE__) . '/Mobile/Request.php'; 
    2423 
    2524// {{{ constants 
     
    8988 * @package    Net_UserAgent_Mobile 
    9089 * @author     KUBO Atsuhiro <[email protected]> 
    91  * @copyright  2003-2007 KUBO Atsuhiro <[email protected]> 
     90 * @copyright  2003-2008 KUBO Atsuhiro <[email protected]> 
    9291 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 
    93  * @version    Release: 0.30.0 
     92 * @version    Release: 0.31.0 
    9493 * @since      Class available since Release 0.1 
    9594 */ 
     
    126125     * If no argument is supplied, $_SERVER{'HTTP_*'} is used. 
    127126     * 
    128      * @param mixed $stuff User-Agent string or object that works with 
    129      *     HTTP_Request (not implemented) 
     127     * @param string $userAgent User-Agent string 
    130128     * @return mixed a newly created Net_UserAgent_Mobile object, or a PEAR 
    131129     *     error object on error 
    132      * @see Net_UserAgent_Mobile_Request::factory() 
    133      */ 
    134     function &factory($stuff = null) 
    135     { 
    136         static $mobileRegex; 
    137         if (!isset($mobileRegex)) { 
    138             $docomoRegex    = '^DoCoMo/\d\.\d[ /]'; 
    139             $vodafoneRegex  = '^(?:(?:SoftBank|Vodafone|J-PHONE|Vemulator|J-EMULATOR)/\d\.\d|(?:MOT|MOTEMULATOR)-)'; 
    140             $ezwebRegex     = '^(?:KDDI-[A-Z]+\d+[A-Z]? )?UP\.Browser\/'; 
    141             $airhphoneRegex = '^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);'; 
    142             $mobileRegex = 
    143                 "(?:($docomoRegex)|($vodafoneRegex)|($ezwebRegex)|($airhphoneRegex))"; 
    144         } 
    145  
    146         $request = &Net_UserAgent_Mobile_Request::factory($stuff); 
     130     */ 
     131    function &factory($userAgent = null) 
     132    { 
     133        if (is_null($userAgent)) { 
     134            $userAgent = $_SERVER['HTTP_USER_AGENT']; 
     135        } 
    147136 
    148137        // parse User-Agent string 
    149         $ua = $request->get('User-Agent'); 
    150         $sub = 'NonMobile'; 
    151         if (preg_match("!$mobileRegex!", $ua, $matches)) { 
    152             $sub = @$matches[1] ? 'DoCoMo' : 
    153                 (@$matches[2] ? 'Vodafone' : 
    154                  (@$matches[3] ? 'EZweb' : 'AirHPhone')); 
    155         } 
    156         $className = "Net_UserAgent_Mobile_{$sub}"; 
    157  
    158         if (!class_exists($className)) { 
    159             $file = dirname(__FILE__) . "/Mobile/{$sub}.php"; 
     138        if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) { 
     139            $driver = 'DoCoMo'; 
     140        } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) { 
     141            $driver = 'EZweb'; 
     142        } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) { 
     143            $driver = 'SoftBank'; 
     144        } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) { 
     145            $driver = 'Willcom'; 
     146        } else { 
     147            $driver = 'NonMobile'; 
     148        } 
     149 
     150        $class = "Net_UserAgent_Mobile_$driver"; 
     151 
     152        if (!class_exists($class)) { 
     153             
     154            $file = dirname(__FILE__) . "/Mobile/{$driver}.php"; 
    160155            if (!include_once $file) { 
    161156                return PEAR::raiseError(null, 
     
    168163        } 
    169164 
    170         $instance = &new $className($request); 
     165        $instance = &new $class($userAgent); 
    171166        $error = &$instance->isError(); 
    172167        if (Net_UserAgent_Mobile::isError($error)) { 
     
    191186     * returns a instance from existent ones 
    192187     * 
    193      * @param mixed $stuff User-Agent string or object that works with 
    194      *     HTTP_Request (not implemented) 
     188     * @param string $userAgent User-Agent string 
    195189     * @return mixed a newly created or a existent Net_UserAgent_Mobile 
    196190     *     object, or a PEAR error object on error 
    197191     * @see Net_UserAgent_Mobile::factory() 
    198192     */ 
    199      function &singleton($stuff = null) 
    200      { 
    201          static $instance; 
    202          if (!isset($instance)) { 
    203              $instance = Net_UserAgent_Mobile::factory($stuff); 
    204          } 
    205  
    206          return $instance; 
    207      } 
     193    function &singleton($userAgent = null) 
     194    { 
     195        static $instances; 
     196 
     197        if (!isset($instances)) { 
     198            $instances = array(); 
     199        } 
     200 
     201        if (is_null($userAgent)) { 
     202            $userAgent = $_SERVER['HTTP_USER_AGENT']; 
     203        } 
     204 
     205        if (!array_key_exists($userAgent, $instances)) { 
     206            $instances[$userAgent] = Net_UserAgent_Mobile::factory($userAgent); 
     207        } 
     208 
     209        return $instances[$userAgent]; 
     210    } 
    208211 
    209212    // }}} 
     
    253256    } 
    254257 
     258    // }}} 
     259    // {{{ isMobile() 
     260 
     261    /** 
     262     * Checks whether or not the user agent is mobile by a given user agent 
     263     * string. 
     264     * 
     265     * @param string $userAgent 
     266     * @return boolean 
     267     * @since Method available since Release 0.31.0 
     268     */ 
     269    function isMobile($userAgent = null) 
     270    { 
     271        if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) { 
     272            return true; 
     273        } elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) { 
     274            return true; 
     275        } elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) { 
     276            return true; 
     277        } elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) { 
     278            return true; 
     279        } 
     280 
     281        return false; 
     282    } 
     283 
     284    // }}} 
     285    // {{{ isDoCoMo() 
     286 
     287    /** 
     288     * Checks whether or not the user agent is DoCoMo by a given user agent 
     289     * string. 
     290     * 
     291     * @param string $userAgent 
     292     * @return boolean 
     293     * @since Method available since Release 0.31.0 
     294     */ 
     295    function isDoCoMo($userAgent = null) 
     296    { 
     297        if (is_null($userAgent)) { 
     298            $userAgent = $_SERVER['HTTP_USER_AGENT']; 
     299        } 
     300 
     301        if (preg_match('!^DoCoMo!', $userAgent)) { 
     302            return true; 
     303        } 
     304 
     305        return false; 
     306    } 
     307 
     308    // }}} 
     309    // {{{ isEZweb() 
     310 
     311    /** 
     312     * Checks whether or not the user agent is EZweb by a given user agent 
     313     * string. 
     314     * 
     315     * @param string $userAgent 
     316     * @return boolean 
     317     * @since Method available since Release 0.31.0 
     318     */ 
     319    function isEZweb($userAgent = null) 
     320    { 
     321        if (is_null($userAgent)) { 
     322            $userAgent = $_SERVER['HTTP_USER_AGENT']; 
     323        } 
     324 
     325        if (preg_match('!^KDDI-!', $userAgent)) { 
     326            return true; 
     327        } elseif (preg_match('!^UP\.Browser!', $userAgent)) { 
     328            return true; 
     329        } 
     330 
     331        return false; 
     332    } 
     333 
     334    // }}} 
     335    // {{{ isSoftBank() 
     336 
     337    /** 
     338     * Checks whether or not the user agent is SoftBank by a given user agent 
     339     * string. 
     340     * 
     341     * @param string $userAgent 
     342     * @return boolean 
     343     * @since Method available since Release 0.31.0 
     344     */ 
     345    function isSoftBank($userAgent = null) 
     346    { 
     347        if (is_null($userAgent)) { 
     348            $userAgent = $_SERVER['HTTP_USER_AGENT']; 
     349        } 
     350 
     351        if (preg_match('!^SoftBank!', $userAgent)) { 
     352            return true; 
     353        } elseif (preg_match('!^Semulator!', $userAgent)) { 
     354            return true; 
     355        } elseif (preg_match('!^Vodafone!', $userAgent)) { 
     356            return true; 
     357        } elseif (preg_match('!^Vemulator!', $userAgent)) { 
     358            return true; 
     359        } elseif (preg_match('!^MOT-!', $userAgent)) { 
     360            return true; 
     361        } elseif (preg_match('!^MOTEMULATOR!', $userAgent)) { 
     362            return true; 
     363        } elseif (preg_match('!^J-PHONE!', $userAgent)) { 
     364            return true; 
     365        } elseif (preg_match('!^J-EMULATOR!', $userAgent)) { 
     366            return true; 
     367        } 
     368 
     369        return false; 
     370    } 
     371 
     372    // }}} 
     373    // {{{ isWillcom() 
     374 
     375    /** 
     376     * Checks whether or not the user agent is Willcom by a given user agent 
     377     * string. 
     378     * 
     379     * @param string $userAgent 
     380     * @return boolean 
     381     * @since Method available since Release 0.31.0 
     382     */ 
     383    function isWillcom($userAgent = null) 
     384    { 
     385        if (is_null($userAgent)) { 
     386            $userAgent = $_SERVER['HTTP_USER_AGENT']; 
     387        } 
     388 
     389        if (preg_match('!^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);!', $userAgent)) { 
     390            return true; 
     391        } 
     392 
     393        return false; 
     394    } 
     395 
    255396    /**#@-*/ 
    256397 
     
    276417 * @copyright  2003-2007 KUBO Atsuhiro <[email protected]> 
    277418 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0 
    278  * @version    Release: 0.30.0 
     419 * @version    Release: 0.31.0 
    279420 * @since      Class available since Release 0.1 
    280421 */ 
     
    356497 * End: 
    357498 */ 
    358 ?> 
Note: See TracChangeset for help on using the changeset viewer.