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.13 2004/03/20 13:43:04 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->isJPhone()) {
  57.  *     // it's J-PHONE J-Sky
  58.  *     // see what's available in Net_UserAgent_Mobile_JPhone
  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.13 $
  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 $mobile_regex;
  105.         if (!isset($mobile_regex)) {
  106.             $docomo_regex    '^DoCoMo/\d\.\d[ /]';
  107.             $jphone_regex    '^J-PHONE/\d\.\d';
  108.             $ezweb_regex     '^(?:KDDI-[A-Z]+\d+ )?UP\.Browser\/';
  109.             $airhphone_regex '^Mozilla/3\.0\(DDIPOCKET;';
  110.             $mobile_regex =
  111.                 "(?:($docomo_regex)|($jphone_regex)|($ezweb_regex)|($airhphone_regex))";
  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("!$mobile_regex!"$ua$matches)) {
  120.             $sub @$matches[1'DoCoMo' :
  121.                 (@$matches[2'JPhone' :
  122.                  (@$matches[3'EZweb' 'AirHPhone'));
  123.         }
  124.         $class_name = "Net_UserAgent_Mobile_{$sub}";
  125.         $include    dirname(__FILE__. "/Mobile/{$sub}.php";
  126.         @include_once($include);
  127.  
  128.         if (!class_exists($class_name)) {
  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 $class_name($request);
  138.         return $instance;
  139.     }
  140.  
  141.     // }}}
  142.     // {{{ singleton()
  143.  
  144.     /**
  145.      * creates a new {@link Net_UserAgent_Mobile_Common} subclass instance or
  146.      * returns a instance from existent ones
  147.      *
  148.      * @param mixed $stuff User-Agent string or object that works with
  149.      *      HTTP_Request (not implemented)
  150.      * @return mixed a newly created or a existent Net_UserAgent_Mobile
  151.      *      object, or a PEAR error object on error
  152.      * @see Net_UserAgent_Mobile::factory()
  153.      */
  154.      function &singleton($stuff = null)
  155.      {
  156.          static $instance;
  157.          if (!isset($instance)) {
  158.              $instance Net_UserAgent_Mobile::factory($stuff);
  159.          }
  160.  
  161.          return $instance;
  162.      }
  163.  
  164.     // }}}
  165.     // {{{ isError()
  166.  
  167.     /**
  168.      * tell whether a result code from a Net_UserAgent_Mobile method
  169.      * is an error
  170.      *
  171.      * @param integer $value result code
  172.      * @return boolean whether $value is an {@link Net_UserAgent_Mobile_Error}
  173.      */
  174.     function isError($value)
  175.     {
  176.         return is_a($value'Net_UserAgent_Mobile_Error');
  177.     }
  178.  
  179.     // }}}
  180.     // {{{ errorMessage()
  181.  
  182.     /**
  183.      * return a textual error message for a Net_UserAgent_Mobile error code
  184.      *
  185.      * @param integer $value error code
  186.      * @return string error message, or false if the error code was not
  187.      *      recognized
  188.      */
  189.     function errorMessage($value)
  190.     {
  191.         static $errorMessages;
  192.         if (!isset($errorMessages)) {
  193.             $errorMessages = array(
  194.                                    NET_USERAGENT_MOBILE_ERROR           => 'unknown error',
  195.                                    NET_USERAGENT_MOBILE_ERROR_NOMATCH   => 'no match',
  196.                                    NET_USERAGENT_MOBILE_ERROR_NOT_FOUND => 'not found',
  197.                                    NET_USERAGENT_MOBILE_OK              => 'no error'
  198.                                    );
  199.         }
  200.  
  201.         if (Net_UserAgent_Mobile::isError($value)) {
  202.             $value $value->getCode();
  203.         }
  204.  
  205.         return isset($errorMessages[$value]?
  206.             $errorMessages[$value:
  207.             $errorMessages[NET_USERAGENT_MOBILE_ERROR];
  208.     }
  209.  
  210.     /**#@-*/
  211. }
  212.  
  213. /**
  214.  * Net_UserAgent_Mobile_Error implements a class for reporting user
  215.  * agent error messages
  216.  *
  217.  * @package  Net_UserAgent_Mobile
  218.  * @category Networking
  219.  * @author   KUBO Atsuhiro <kubo@isite.co.jp>
  220.  * @access   public
  221.  * @version  $Revision: 1.13 $
  222.  */
  223. class Net_UserAgent_Mobile_Error extends PEAR_Error
  224. {
  225.  
  226.     // }}}
  227.     // {{{ constructor
  228.  
  229.     /**
  230.      * constructor
  231.      *
  232.      * @param mixed   $code     Net_UserAgent_Mobile error code, or string
  233.      *      with error message.
  234.      * @param integer $mode     what 'error mode' to operate in
  235.      * @param integer $level    what error level to use for $mode and
  236.      *      PEAR_ERROR_TRIGGER
  237.      * @param mixed   $userinfo additional user/debug info
  238.      * @access public
  239.      */
  240.     function Net_UserAgent_Mobile_Error($code = NET_USERAGENT_MOBILE_ERROR,
  241.                                         $mode = PEAR_ERROR_RETURN,
  242.                                         $level = E_USER_NOTICE,
  243.                                         $userinfo = null
  244.                                         )
  245.     {
  246.         if (is_int($code)) {
  247.             $this->PEAR_Error('Net_UserAgent_Mobile Error: ' .
  248.                               Net_UserAgent_Mobile::errorMessage($code),
  249.                               $code$mode$level$userinfo
  250.                               );
  251.         else {
  252.             $this->PEAR_Error("Net_UserAgent_Mobile Error: $code",
  253.                               NET_USERAGENT_MOBILE_ERROR$mode$level,
  254.                               $userinfo
  255.                               );
  256.         }
  257.     }
  258. }
  259.  
  260. /*
  261.  * Local Variables:
  262.  * mode: php
  263.  * coding: iso-8859-1
  264.  * tab-width: 4
  265.  * c-basic-offset: 4
  266.  * c-hanging-comment-ender-p: nil
  267.  * indent-tabs-mode: nil
  268.  * End:
  269.  */
  270. ?>

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