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. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: KUBO Atsuhiro <kubo@isite.co.jp>                            |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Mobile.php,v 1.21 2005/07/27 08:15:27 kuboa Exp $
  20. //
  21.  
  22. require_once('PEAR.php');
  23. require_once(dirname(__FILE__'/Mobile/Request.php');
  24.  
  25. /**
  26.  * constants for error handling
  27.  */
  28. define('NET_USERAGENT_MOBILE_OK',               1);
  29. define('NET_USERAGENT_MOBILE_ERROR',           -1);
  30. define('NET_USERAGENT_MOBILE_ERROR_NOMATCH',   -2);
  31. define('NET_USERAGENT_MOBILE_ERROR_NOT_FOUND'-3);
  32.  
  33. /**
  34.  * globals for fallback on no match
  35.  * @name $_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH
  36.  * @global boolean $GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH'] 
  37.  */
  38. $GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH'= false;
  39.  
  40. /**
  41.  * HTTP mobile user agent string parser
  42.  *
  43.  * Net_UserAgent_Mobile parses HTTP_USER_AGENT strings of (mainly Japanese)
  44.  * mobile HTTP user agents. It'll be useful in page dispatching by user
  45.  * agents.
  46.  * This package was ported from Perl's HTTP::MobileAgent.
  47.  * See {@link http://search.cpan.org/search?mode=module&query=HTTP-MobileAgent}
  48.  * The author of the HTTP::MobileAgent module is Tatsuhiko Miyagawa
  49.  * <miyagawa@bulknews.net>
  50.  *
  51.  * SYNOPSIS:
  52.  * <code>
  53.  * require_once('Net/UserAgent/Mobile.php');
  54.  *
  55.  * $agent = &Net_UserAgent_Mobile::factory($agent_string);
  56.  * // or $agent = &Net_UserAgent_Mobile::factory(); // to get from $_SERVER
  57.  *
  58.  * if ($agent->isDoCoMo()) {
  59.  *     // or if ($agent->getName() == 'DoCoMo')
  60.  *     // or if (strtolower(get_class($agent)) == 'http_mobileagent_docomo')
  61.  *     // it's NTT DoCoMo i-mode
  62.  *     // see what's available in Net_UserAgent_Mobile_DoCoMo
  63.  * } elseif ($agent->isVodafone()) {
  64.  *     // it's Vodafone(J-PHONE)
  65.  *     // see what's available in Net_UserAgent_Mobile_Vodafone
  66.  * } elseif ($agent->isEZweb()) {
  67.  *     // it's KDDI/EZWeb
  68.  *     // see what's available in Net_UserAgent_Mobile_EZweb
  69.  * } else {
  70.  *     // may be PC
  71.  *     // $agent is Net_UserAgent_Mobile_NonMobile
  72.  * }
  73.  *
  74.  * $display = $agent->getDisplay();    // Net_UserAgent_Mobile_Display
  75.  * if ($display->isColor()) {
  76.  *    ...
  77.  * }
  78.  * </code>
  79.  *
  80.  * @package  Net_UserAgent_Mobile
  81.  * @category Networking
  82.  * @author   KUBO Atsuhiro <kubo@isite.co.jp>
  83.  * @access   public
  84.  * @version  $Revision: 1.21 $
  85.  */
  86. {
  87.  
  88.     /**#@+
  89.      * @access public
  90.      * @static
  91.      */
  92.  
  93.     // }}}
  94.     // {{{ factory()
  95.  
  96.     /**
  97.      * create a new {@link Net_UserAgent_Mobile_Common} subclass instance
  98.      *
  99.      * parses HTTP headers and constructs {@link Net_UserAgent_Mobile_Common}
  100.      * subclass instance.
  101.      * If no argument is supplied, $_SERVER{'HTTP_*'} is used.
  102.      *
  103.      * @param mixed $stuff User-Agent string or object that works with
  104.      *      HTTP_Request (not implemented)
  105.      * @return mixed a newly created Net_UserAgent_Mobile object, or a PEAR
  106.      *      error object on error
  107.      * @see Net_UserAgent_Mobile_Request::factory()
  108.      */
  109.     function &factory($stuff = null)
  110.     {
  111.         static $mobileRegex;
  112.         if (!isset($mobileRegex)) {
  113.             $docomoRegex    '^DoCoMo/\d\.\d[ /]';
  114.             $vodafoneRegex  '^(?:(?:Vodafone|J-PHONE)/\d\.\d|MOT-)';
  115.             $ezwebRegex     '^(?:KDDI-[A-Z]+\d+[A-Z]? )?UP\.Browser\/';
  116.             $airhphoneRegex '^Mozilla/3\.0\(DDIPOCKET;';
  117.             $mobileRegex =
  118.                 "(?:($docomoRegex)|($vodafoneRegex)|($ezwebRegex)|($airhphoneRegex))";
  119.         }
  120.  
  121.         $request &Net_UserAgent_Mobile_Request::factory($stuff);
  122.  
  123.         // parse User-Agent string
  124.         $ua $request->get('User-Agent');
  125.         $sub 'NonMobile';
  126.         if (preg_match("!$mobileRegex!"$ua$matches)) {
  127.             $sub @$matches[1'DoCoMo' :
  128.                 (@$matches[2'Vodafone' :
  129.                  (@$matches[3'EZweb' 'AirHPhone'));
  130.         }
  131.         $className = "Net_UserAgent_Mobile_{$sub}";
  132.         $include    dirname(__FILE__. "/Mobile/{$sub}.php";
  133.  
  134.         if (!class_exists($className)) {
  135.             if (!is_readable($include)) {
  136.                 return PEAR::raiseError(null,
  137.                                         NET_USERAGENT_MOBILE_ERROR_NOT_FOUND,
  138.                                         nullnull,
  139.                                         "Unable to read the $include file",
  140.                                         'Net_UserAgent_Mobile_Error'true
  141.                                         );
  142.             }
  143.             if (!include_once($include)) {
  144.                 return PEAR::raiseError(null,
  145.                                         NET_USERAGENT_MOBILE_ERROR_NOT_FOUND,
  146.                                         nullnull,
  147.                                         "Unable to include the $include file",
  148.                                         'Net_UserAgent_Mobile_Error'true
  149.                                         );
  150.             }
  151.         }
  152.  
  153.         $instance &new $className($request);
  154.         $error &$instance->isError();
  155.         if (Net_UserAgent_Mobile::isError($error)) {
  156.             if ($GLOBALS['_NET_USERAGENT_MOBILE_FALLBACK_ON_NOMATCH']
  157.                 && $error->getCode(== NET_USERAGENT_MOBILE_ERROR_NOMATCH
  158.                 {
  159.                 $instance &Net_UserAgent_Mobile::factory('Net_UserAgent_Mobile_Fallback_On_NoMatch');
  160.                 return $instance;
  161.             }
  162.  
  163.             $instance &$error;
  164.         }
  165.  
  166.         return $instance;
  167.     }
  168.  
  169.     // }}}
  170.     // {{{ singleton()
  171.  
  172.     /**
  173.      * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or
  174.      * returns a instance from existent ones
  175.      *
  176.      * @param mixed $stuff User-Agent string or object that works with
  177.      *      HTTP_Request (not implemented)
  178.      * @return mixed a newly created or a existent Net_UserAgent_Mobile
  179.      *      object, or a PEAR error object on error
  180.      * @see Net_UserAgent_Mobile::factory()
  181.      */
  182.      function &singleton($stuff = null)
  183.      {
  184.          static $instance;
  185.          if (!isset($instance)) {
  186.              $instance Net_UserAgent_Mobile::factory($stuff);
  187.          }
  188.  
  189.          return $instance;
  190.      }
  191.  
  192.     // }}}
  193.     // {{{ isError()
  194.  
  195.     /**
  196.      * tell whether a result code from a Net_UserAgent_Mobile method
  197.      * is an error
  198.      *
  199.      * @param integer $value result code
  200.      * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error}
  201.      */
  202.     function isError($value)
  203.     {
  204.         return is_a($value'Net_UserAgent_Mobile_Error');
  205.     }
  206.  
  207.     // }}}
  208.     // {{{ errorMessage()
  209.  
  210.     /**
  211.      * return a textual error message for a Net_UserAgent_Mobile error code
  212.      *
  213.      * @param integer $value error code
  214.      * @return string error message, or false if the error code was not
  215.      *      recognized
  216.      */
  217.     function errorMessage($value)
  218.     {
  219.         static $errorMessages;
  220.         if (!isset($errorMessages)) {
  221.             $errorMessages = array(
  222.                                    NET_USERAGENT_MOBILE_ERROR           => 'unknown error',
  223.                                    NET_USERAGENT_MOBILE_ERROR_NOMATCH   => 'no match',
  224.                                    NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found',
  225.                                    NET_USERAGENT_MOBILE_OK              => 'no error'
  226.                                    );
  227.         }
  228.  
  229.         if (Net_UserAgent_Mobile::isError($value)) {
  230.             $value $value->getCode();
  231.         }
  232.  
  233.         return isset($errorMessages[$value]?
  234.             $errorMessages[$value:
  235.             $errorMessages[NET_USERAGENT_MOBILE_ERROR];
  236.     }
  237.  
  238.     /**#@-*/
  239. }
  240.  
  241. /**
  242.  * Net_UserAgent_Mobile_Error implements a class for reporting user
  243.  * agent error messages
  244.  *
  245.  * @package  Net_UserAgent_Mobile
  246.  * @category Networking
  247.  * @author   KUBO Atsuhiro <kubo@isite.co.jp>
  248.  * @access   public
  249.  * @version  $Revision: 1.21 $
  250.  */
  251. class Net_UserAgent_Mobile_Error extends PEAR_Error
  252. {
  253.  
  254.     // }}}
  255.     // {{{ constructor
  256.  
  257.     /**
  258.      * constructor
  259.      *
  260.      * @param mixed   $code     Net_UserAgent_Mobile error code, or string
  261.      *      with error message.
  262.      * @param integer $mode     what 'error mode' to operate in
  263.      * @param integer $level    what error level to use for $mode and
  264.      *      PEAR_ERROR_TRIGGER
  265.      * @param mixed   $userinfo additional user/debug info
  266.      * @access public
  267.      */
  268.     function Net_UserAgent_Mobile_Error($code = NET_USERAGENT_MOBILE_ERROR,
  269.                                         $mode = PEAR_ERROR_RETURN,
  270.                                         $level = E_USER_NOTICE,
  271.                                         $userinfo = null
  272.                                         )
  273.     {
  274.         if (is_int($code)) {
  275.             $this->PEAR_Error('Net_UserAgent_Mobile Error: ' .
  276.                               Net_UserAgent_Mobile::errorMessage($code),
  277.                               $code$mode$level$userinfo
  278.                               );
  279.         else {
  280.             $this->PEAR_Error("Net_UserAgent_Mobile Error: $code",
  281.                               NET_USERAGENT_MOBILE_ERROR$mode$level,
  282.                               $userinfo
  283.                               );
  284.         }
  285.     }
  286. }
  287.  
  288. /*
  289.  * Local Variables:
  290.  * mode: php
  291.  * coding: iso-8859-1
  292.  * tab-width: 4
  293.  * c-basic-offset: 4
  294.  * c-hanging-comment-ender-p: nil
  295.  * indent-tabs-mode: nil
  296.  * End:
  297.  */
  298. ?>

Documentation generated on Mon, 11 Mar 2019 14:16:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.