Net_UserAgent_Mobile
[ class tree: Net_UserAgent_Mobile ] [ index: Net_UserAgent_Mobile ] [ all elements ]

Source for file Mobile.php

Documentation is available at Mobile.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3.  
  4. /**
  5.  * PHP versions 4 and 5
  6.  *
  7.  * Copyright (c) 2003-2009 KUBO Atsuhiro <kubo@iteman.jp>,
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *     * Redistributions of source code must retain the above copyright
  14.  *       notice, this list of conditions and the following disclaimer.
  15.  *     * Redistributions in binary form must reproduce the above copyright
  16.  *       notice, this list of conditions and the following disclaimer in the
  17.  *       documentation and/or other materials provided with the distribution.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29.  * POSSIBILITY OF SUCH DAMAGE.
  30.  *
  31.  * @category   Networking
  32.  * @package    Net_UserAgent_Mobile
  33.  * @author     KUBO Atsuhiro <kubo@iteman.jp>
  34.  * @copyright  2003-2009 KUBO Atsuhiro <kubo@iteman.jp>
  35.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  36.  * @version    CVS: $Id: Mobile.php,v 1.1 2009/05/26 08:48:16 kuboa Exp $
  37.  * @since      File available since Release 0.1
  38.  */
  39.  
  40. require_once 'Net/UserAgent/Mobile/Error.php';
  41. require_once 'PEAR.php';
  42.  
  43. // {{{ GLOBALS
  44.  
  45. /**
  46.  * globals for fallback on no match
  47.  *
  48.  * @global boolean $GLOBALS['NET_USERAGENT_MOBILE_FallbackOnNomatch'] 
  49.  */
  50. $GLOBALS['NET_USERAGENT_MOBILE_FallbackOnNomatch'= false;
  51.  
  52. // }}}
  53. // {{{ Net_UserAgent_Mobile
  54.  
  55. /**
  56.  * HTTP mobile user agent string parser
  57.  *
  58.  * Net_UserAgent_Mobile parses HTTP_USER_AGENT strings of (mainly Japanese) mobile
  59.  * HTTP user agents. It'll be useful in page dispatching by user agents.
  60.  * This package was ported from Perl's HTTP::MobileAgent.
  61.  * See {@link http://search.cpan.org/search?mode=module&query=HTTP-MobileAgent}
  62.  *
  63.  * SYNOPSIS:
  64.  * <code>
  65.  * require_once 'Net/UserAgent/Mobile.php';
  66.  *
  67.  * $agent = &Net_UserAgent_Mobile::factory($agent_string);
  68.  * // or $agent = &Net_UserAgent_Mobile::factory(); // to get from $_SERVER
  69.  *
  70.  * if ($agent->isDoCoMo()) {
  71.  *     // or if ($agent->getName() == 'DoCoMo')
  72.  *     // or if (strtolower(get_class($agent)) == 'http_mobileagent_docomo')
  73.  *     // it's NTT DoCoMo i-mode
  74.  *     // see what's available in Net_UserAgent_Mobile_DoCoMo
  75.  * } elseif ($agent->isSoftBank()) {
  76.  *     // it's SoftBank
  77.  *     // see what's available in Net_UserAgent_Mobile_SoftBank
  78.  * } elseif ($agent->isEZweb()) {
  79.  *     // it's KDDI/EZWeb
  80.  *     // see what's available in Net_UserAgent_Mobile_EZweb
  81.  * } else {
  82.  *     // may be PC
  83.  *     // $agent is Net_UserAgent_Mobile_NonMobile
  84.  * }
  85.  *
  86.  * $display = $agent->getDisplay();    // Net_UserAgent_Mobile_Display
  87.  * if ($display->isColor()) {
  88.  *    ...
  89.  * }
  90.  * </code>
  91.  *
  92.  * @category   Networking
  93.  * @package    Net_UserAgent_Mobile
  94.  * @author     KUBO Atsuhiro <kubo@iteman.jp>
  95.  * @copyright  2003-2009 KUBO Atsuhiro <kubo@iteman.jp>
  96.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  97.  * @version    Release: 1.0.0
  98.  * @since      Class available since Release 0.1
  99.  */
  100. {
  101.  
  102.     // {{{ properties
  103.  
  104.     /**#@+
  105.      * @access public
  106.      */
  107.  
  108.     /**#@-*/
  109.  
  110.     /**#@+
  111.      * @access private
  112.      */
  113.  
  114.     /**#@-*/
  115.  
  116.     /**#@+
  117.      * @access public
  118.      * @static
  119.      */
  120.  
  121.     // }}}
  122.     // {{{ factory()
  123.  
  124.     /**
  125.      * create a new {@link Net_UserAgent_Mobile_Common} subclass instance
  126.      *
  127.      * parses HTTP headers and constructs {@link Net_UserAgent_Mobile_Common}
  128.      * subclass instance.
  129.      * If no argument is supplied, $_SERVER{'HTTP_*'} is used.
  130.      *
  131.      * @param string $userAgent User-Agent string
  132.      * @return Net_UserAgent_Mobile_Common a newly created or an existing
  133.      *      Net_UserAgent_Mobile_Common object
  134.      * @throws Net_UserAgent_Mobile_Error
  135.      */
  136.     function &factory($userAgent = null)
  137.     {
  138.         if (is_null($userAgent)) {
  139.             $userAgent @$_SERVER['HTTP_USER_AGENT'];
  140.         }
  141.  
  142.         // parse User-Agent string
  143.         if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) {
  144.             $driver 'DoCoMo';
  145.         elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) {
  146.             $driver 'EZweb';
  147.         elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) {
  148.             $driver 'SoftBank';
  149.         elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) {
  150.             $driver 'Willcom';
  151.         else {
  152.             $driver 'NonMobile';
  153.         }
  154.  
  155.         $class = "Net_UserAgent_Mobile_$driver";
  156.  
  157.         if (!class_exists($class)) {
  158.             $file str_replace('_''/'$class'.php';
  159.             if (!include_once $file{
  160.                 return PEAR::raiseError(null,
  161.                                         NET_USERAGENT_MOBILE_ERROR_NOT_FOUND,
  162.                                         nullnull,
  163.                                         "Unable to include the $file file",
  164.                                         'Net_UserAgent_Mobile_Error'true
  165.                                         );
  166.             }
  167.         }
  168.  
  169.         PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
  170.         $instance = new $class($userAgent);
  171.         PEAR::staticPopErrorHandling();
  172.         $error &$instance->getError();
  173.         if (Net_UserAgent_Mobile::isError($error)) {
  174.             if ($GLOBALS['NET_USERAGENT_MOBILE_FallbackOnNomatch']
  175.                 && $error->getCode(== NET_USERAGENT_MOBILE_ERROR_NOMATCH
  176.                 {
  177.                 $instance &Net_UserAgent_Mobile::factory('Net_UserAgent_Mobile_Fallback_On_NoMatch');
  178.                 return $instance;
  179.             }
  180.  
  181.             return PEAR::raiseError($error);
  182.         }
  183.  
  184.         return $instance;
  185.     }
  186.  
  187.     // }}}
  188.     // {{{ singleton()
  189.  
  190.     /**
  191.      * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or returns
  192.      * a instance from existent ones
  193.      *
  194.      * @param string $userAgent User-Agent string
  195.      * @return Net_UserAgent_Mobile_Common a newly created or an existing
  196.      *      Net_UserAgent_Mobile_Common object
  197.      * @throws Net_UserAgent_Mobile_Error
  198.      */
  199.     function &singleton($userAgent = null)
  200.     {
  201.         static $instances;
  202.  
  203.         if (!isset($instances)) {
  204.             $instances = array();
  205.         }
  206.  
  207.         if (is_null($userAgent)) {
  208.             $userAgent @$_SERVER['HTTP_USER_AGENT'];
  209.         }
  210.  
  211.         if (!array_key_exists($userAgent$instances)) {
  212.             $instances[$userAgentNet_UserAgent_Mobile::factory($userAgent);
  213.         }
  214.  
  215.         return $instances[$userAgent];
  216.     }
  217.  
  218.     // }}}
  219.     // {{{ isError()
  220.  
  221.     /**
  222.      * tell whether a result code from a Net_UserAgent_Mobile method is an error
  223.      *
  224.      * @param integer $value result code
  225.      * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error}
  226.      */
  227.     function isError($value)
  228.     {
  229.         return is_object($value)
  230.             && (strtolower(get_class($value)) == strtolower('Net_UserAgent_Mobile_Error')
  231.                 || is_subclass_of($value'Net_UserAgent_Mobile_Error'));
  232.     }
  233.  
  234.     // }}}
  235.     // {{{ errorMessage()
  236.  
  237.     /**
  238.      * return a textual error message for a Net_UserAgent_Mobile error code
  239.      *
  240.      * @param integer $value error code
  241.      * @return string error message, or null if the error code was not recognized
  242.      */
  243.     function errorMessage($value)
  244.     {
  245.         static $errorMessages;
  246.         if (!isset($errorMessages)) {
  247.             $errorMessages = array(
  248.                                    NET_USERAGENT_MOBILE_ERROR           => 'unknown error',
  249.                                    NET_USERAGENT_MOBILE_ERROR_NOMATCH   => 'no match',
  250.                                    NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found',
  251.                                    NET_USERAGENT_MOBILE_OK              => 'no error'
  252.                                    );
  253.         }
  254.  
  255.         if (Net_UserAgent_Mobile::isError($value)) {
  256.             $value $value->getCode();
  257.         }
  258.  
  259.         return isset($errorMessages[$value]?
  260.             $errorMessages[$value:
  261.             $errorMessages[NET_USERAGENT_MOBILE_ERROR];
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ isMobile()
  266.  
  267.     /**
  268.      * Checks whether or not the user agent is mobile by a given user agent string.
  269.      *
  270.      * @param string $userAgent 
  271.      * @return boolean 
  272.      * @since Method available since Release 0.31.0
  273.      */
  274.     function isMobile($userAgent = null)
  275.     {
  276.         if (Net_UserAgent_Mobile::isDoCoMo($userAgent)) {
  277.             return true;
  278.         elseif (Net_UserAgent_Mobile::isEZweb($userAgent)) {
  279.             return true;
  280.         elseif (Net_UserAgent_Mobile::isSoftBank($userAgent)) {
  281.             return true;
  282.         elseif (Net_UserAgent_Mobile::isWillcom($userAgent)) {
  283.             return true;
  284.         }
  285.  
  286.         return false;
  287.     }
  288.  
  289.     // }}}
  290.     // {{{ isDoCoMo()
  291.  
  292.     /**
  293.      * Checks whether or not the user agent is DoCoMo by a given user agent string.
  294.      *
  295.      * @param string $userAgent 
  296.      * @return boolean 
  297.      * @since Method available since Release 0.31.0
  298.      */
  299.     function isDoCoMo($userAgent = null)
  300.     {
  301.         if (is_null($userAgent)) {
  302.             $userAgent @$_SERVER['HTTP_USER_AGENT'];
  303.         }
  304.  
  305.         if (preg_match('!^DoCoMo!'$userAgent)) {
  306.             return true;
  307.         }
  308.  
  309.         return false;
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ isEZweb()
  314.  
  315.     /**
  316.      * Checks whether or not the user agent is EZweb by a given user agent string.
  317.      *
  318.      * @param string $userAgent 
  319.      * @return boolean 
  320.      * @since Method available since Release 0.31.0
  321.      */
  322.     function isEZweb($userAgent = null)
  323.     {
  324.         if (is_null($userAgent)) {
  325.             $userAgent @$_SERVER['HTTP_USER_AGENT'];
  326.         }
  327.  
  328.         if (preg_match('!^KDDI-!'$userAgent)) {
  329.             return true;
  330.         elseif (preg_match('!^UP\.Browser!'$userAgent)) {
  331.             return true;
  332.         }
  333.  
  334.         return false;
  335.     }
  336.  
  337.     // }}}
  338.     // {{{ isSoftBank()
  339.  
  340.     /**
  341.      * Checks whether or not the user agent is SoftBank by a given user agent string.
  342.      *
  343.      * @param string $userAgent 
  344.      * @return boolean 
  345.      * @since Method available since Release 0.31.0
  346.      */
  347.     function isSoftBank($userAgent = null)
  348.     {
  349.         if (is_null($userAgent)) {
  350.             $userAgent @$_SERVER['HTTP_USER_AGENT'];
  351.         }
  352.  
  353.         if (preg_match('!^SoftBank!'$userAgent)) {
  354.             return true;
  355.         elseif (preg_match('!^Semulator!'$userAgent)) {
  356.             return true;
  357.         elseif (preg_match('!^Vodafone!'$userAgent)) {
  358.             return true;
  359.         elseif (preg_match('!^Vemulator!'$userAgent)) {
  360.             return true;
  361.         elseif (preg_match('!^MOT-!'$userAgent)) {
  362.             return true;
  363.         elseif (preg_match('!^MOTEMULATOR!'$userAgent)) {
  364.             return true;
  365.         elseif (preg_match('!^J-PHONE!'$userAgent)) {
  366.             return true;
  367.         elseif (preg_match('!^J-EMULATOR!'$userAgent)) {
  368.             return true;
  369.         }
  370.  
  371.         return false;
  372.     }
  373.  
  374.     // }}}
  375.     // {{{ isWillcom()
  376.  
  377.     /**
  378.      * Checks whether or not the user agent is Willcom by a given user agent string.
  379.      *
  380.      * @param string $userAgent 
  381.      * @return boolean 
  382.      * @since Method available since Release 0.31.0
  383.      */
  384.     function isWillcom($userAgent = null)
  385.     {
  386.         if (is_null($userAgent)) {
  387.             $userAgent @$_SERVER['HTTP_USER_AGENT'];
  388.         }
  389.  
  390.         if (preg_match('!^Mozilla/3\.0\((?:DDIPOCKET|WILLCOM);!'$userAgent)) {
  391.             return true;
  392.         }
  393.  
  394.         return false;
  395.     }
  396.  
  397.     /**#@-*/
  398.  
  399.     /**#@+
  400.      * @access private
  401.      */
  402.  
  403.     /**#@-*/
  404.  
  405.     // }}}
  406. }
  407.  
  408. // }}}
  409.  
  410. /*
  411.  * Local Variables:
  412.  * mode: php
  413.  * coding: iso-8859-1
  414.  * tab-width: 4
  415.  * c-basic-offset: 4
  416.  * c-hanging-comment-ender-p: nil
  417.  * indent-tabs-mode: nil
  418.  * End:
  419.  */

Documentation generated on Tue, 23 Jun 2009 10:00:05 +0000 by phpDocumentor 1.4.2. PEAR Logo Copyright © PHP Group 2004.