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

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