<?php

/*

Imode_User_Agent.php A class for identifying i-mode handset
Copyright (C) 2000,2001 Mika Tuupola

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

Contact: tuupola@appelsiini.net

$Id: Imode_User_Agent.php,v 1.9 2001/08/27 18:17:16 tuupola Exp $

*/

define('IMODE_COLOUR_BW',        0);
define('IMODE_COLOUR_GREYSCALE'1);
define('IMODE_COLOUR_256',       2);
define('IMODE_COLOUR_4096',      3);
define('IMODE_COLOUR_65536',     4);

define('IMODE_DEFAULT_CACHE'5);

define('IMODE_UNKNOWN_USER_AGENT'1);

class 
Imode_User_Agent {

  
// internal data. 
  // this probably should be in separate file...
  // http://www.nttdocomo.co.jp/i/tag/s5.html#5_1
  //
  
var $_data = array ( 
        
"D209i" => array (
           
"imagewidth"  => 96"imageheight" => 90
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"F209i" => array (
           
"imagewidth"  => 96"imageheight" => 91
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"N209i" => array (
           
"imagewidth"  => 108"imageheight" => 82
           
"textwidth"   => 9,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"P209i" => array (
           
"imagewidth"  => 96"imageheight" => 87
           
"textwidth"   => 8,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"P209is" => array ( 
           
"imagewidth"  => 96"imageheight" => 87
           
"textwidth"   => 8,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"R209i" => array ( 
           
"imagewidth"  => 96"imageheight" => 72
           
"textwidth"   => 8,  "textheight" => 6
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"ER209i" => array ( 
           
"imagewidth"  => 120"imageheight" => 72
           
"textwidth"   => 10,  "textheight" => 6
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"KO209i" => array ( 
           
"imagewidth"  => 96"imageheight" => 96
           
"textwidth"   => 8,  "textheight" => 8
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"D210i" => array ( 
           
"imagewidth"  => 96"imageheight" => 91
           
"textwidth"   => 8,  "textheight" => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"F210i" => array ( 
           
"imagewidth"  => 96"imageheight" => 113
           
"textwidth"   => 8,  "textheight" => 8
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"N210i" => array ( 
           
"imagewidth"  => 118"imageheight" => 113
           
"textwidth"   => 10,  "textheight" => 8
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"P210i" => array ( 
           
"imagewidth"  => 96"imageheight" => 91
           
"textwidth"   => 8,  "textheight" => 6
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"KO210i" => array ( 
           
"imagewidth"  => 96"imageheight" => 96
           
"textwidth"   => 8,  "textheight" => 8
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"SO210i" => array ( 
           
"imagewidth"  => 120"imageheight" => 113
           
"textwidth"   => 8,  "textheight" => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"D501i" => array (
           
"imagewidth"  => 96"imageheight" => 72
           
"textwidth"   => 8,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_BW,
           
"imageformats"=> array("gif")
        ),
        
"F501i" => array (
           
"imagewidth"  => 112"imageheight" => 84
           
"textwidth"   => 8,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_BW,
           
"imageformats"=> array("gif")
        ),
        
"N501i" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight"   =>10
           
"colour" => IMODE_COLOUR_BW,
           
"imageformats"=> array("gif")
        ),
        
"P501i" => array (
           
"imagewidth"  => 96"imageheight" => 120
           
"textwidth"   => 8,  "textheight"   => 8
           
"colour" => IMODE_COLOUR_BW,
           
"imageformats"=> array("gif")
        ),

        
"D502i" => array (
           
"imagewidth"  => 96"imageheight" => 90
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"F502i" => array (
           
"imagewidth"  => 96"imageheight" => 91
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"F502it" => array (
           
"imagewidth"  => 96"imageheight" => 91
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"N502i" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight"   => 10
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"N502it" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight"  => 10
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"P502i" => array (
           
"imagewidth"  => 96"imageheighth" => 117
           
"textwidth"   => 8,  "textheight"   => 8
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"NM502i" => array (
//           "imagewidth"  => 111, "imageheight" => 106, 
           
"imagewidth"  => 111"imageheight" => 77
           
"textwidth"   => 8,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_BW,
           
"imageformats"=> array("gif")
        ),
        
"SO502i" => array (
           
"imagewidth"  => 120"imageheight" => 120
           
"textwidth"   => 8,  "textheight"   => 8
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"SO502iwm" => array (
           
"imagewidth"  => 120"imageheight" => 113
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),

        
"F503i" => array (
           
"imagewidth"  => 120"imageheight" => 130
           
"textwidth"   => 10,  "textheight"  => 10
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"F503iS" => array (
           
"imagewidth"  => 120"imageheight" => 130
           
"textwidth"   => 12,  "textheight"  => 12
           
"colour" => IMODE_COLOUR_4096,
           
"imageformats"=> array("gif")
        ),
        
"P503i" => array (
           
"imagewidth"  => 120"imageheight" => 130
           
"textwidth"   => 12,  "textheight"   => 10
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"P503iS" => array (
           
"imagewidth"  => 120"imageheight" => 130,
           
"textwidth"   => 12,  "textheight"  => 10,
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"SO503i" => array (
           
"imagewidth"  => 120"imageheight" => 113
           
"textwidth"   => 8.5,  "textheight" => 7
           
"colour" => IMODE_COLOUR_65536,
           
"imageformats"=> array("gif")
        ),
        
"D503i" => array (
           
"imagewidth"  => 132"imageheight" => 126
           
"textwidth"   => 8,  "textheight"   => 7
           
"colour" => IMODE_COLOUR_4096,
           
"imageformats"=> array("gif")
        ),
        
"N503i" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight"  => 10
           
"colour" => IMODE_COLOUR_4096,
           
"imageformats"=> array("gif""jpg")
        ),
        
"N503iS" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight"  => 10
           
"colour" => IMODE_COLOUR_4096,
           
"imageformats"=> array("gif""jpg")
        ),
        
"N691i" => array (
           
"imagewidth"  => 96"imageheight" => 72
           
"textwidth"   => 8,  "textheight"  => 6
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"SH821i" => array (
           
"imagewidth"  => 96"imageheight" => 78
           
"textwidth"   => 8,  "textheight"   => 6
           
"colour" => IMODE_COLOUR_256,
           
"imageformats"=> array("gif")
        ),
        
"N821i" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight"   => 10
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"P821i" => array (
           
"imagewidth"  => 118"imageheight" => 128
           
"textwidth"   => 10,  "textheight" => 10
           
"colour" => IMODE_COLOUR_GREYSCALE,
           
"imageformats"=> array("gif")
        ),
        
"safe" => array (
           
"imagewidth"  => 94"imageheight" => 72
           
"textwidth"   => 8,  "textheight" => 6
           
"colour" => IMODE_COLOUR_BW,
           
"imageformats"=> array("gif")
        )
      );

  var 
$_manufacturerlist = array (
        
"D"  => "Mitsubishi",
        
"P"  => "Panasonic (Matsushita)",
        
"NM" => "Nokia",
        
"SO" => "Sony",
        
"F"  => "Fujitsu",
        
"N"  => "Nec",
        
"SH" => "Sharp",
        
"ER" => "Ericsson",
        
"R"  => "Japan Radio",
        
"KO" => "Kokusai (Hitachi)"
  
);

  var 
$_extra = array (
        
"t"  => "Transport layer",
        
"e"  => "English language",
        
"s"  => "Second version"
  
);

  
// properties. meant be private. 
  //
  
var $_user_agent;    
  var 
$_model;           
  var 
$_manufacturer;     
  var 
$_httpversion;  
  var 
$_cache;
  var 
$_extra;
  var 
$_error;

  
// Constructor
  //   This gets called when new object is initialized. Does not
  //   handle bogus user_agents or most of the other error situation
  //   properly yet.
  //
  // Parameters:
  //   String describing the user_agent.
  //
  // Returns:
  //   Object
  //
  // Example usage:
  //   $ua = new Imode_User_Agent($HTTP_USER_AGENT);
  //
  
function Imode_User_Agent($input) {

    
//DoCoMo/1.0/SO502i
    //DoCoMo/1.0/N502it/c10

    
$_error 0;
    
$temp explode("/"$input);    

    
$this->_user_agent  $input;
    
$this->_httpversion $temp[1];
    
$this->_model       $temp[2];
    if (
$temp[3]) {
      
$this->_cache      substr($temp[3], 1);;
    } else {
      
$this->_cache      IMODE_DEFAULT_CACHE;
    }

    
preg_match("/(^[a-zA-Z]+)([0-9]+i)(.*)\/?(.*)/"$this->_model$matches);
 
    
// TODO: Fix situation of unknown manufacturer. Implement
    // extrainfo properly
    //
    
$this->_manufacturer $this->_manufacturerlist[$matches[1]];
    
$this->_extra        $matches[3]; 

    if (!(
$this->_data[$this->_model])) {
      
$_error IMODE_UNKNOWN_USER_AGENT;
    }

  } 

  
// Method
  //
  // Returns:
  //   Array containing maximum imagewidth and imageheight
  //   to fit on the handset screen without scrolling.
  //
  // Example usage:
  //  $imagedim    = $ua->getImageDimensions();
  //  $imagewidth  = $imagedim[0];
  //  $imageheight = $imagedim[1];
  //
  
function getImageDimensions() {
    
$data   $this->_data["$this->_model"];
    
$width   $data["imagewidth"];
    
$height  $data["imageheight"];
    
$retval = array($width$height);
    return(
$retval);
  }

  
// Method
  //
  // Returns:
  //   Array containing maximum textwidth and textheight
  //   to fit on the handset screen without scrolling.
  //
  // Example usage:
  //  $textdim    = $ua->getTextDimensions();
  //  $textwidth  = $textdim[0];
  //  $textheight = $textdim[1];
  //
  
function getTextDimensions() {
    
$data   $this->_data[$this->_model];
    
$width  $data[textwidth];
    
$height $data[textheight];
    
$retval = array($width$height);
    return(
$retval);
  }

  
// Method
  //
  // Returns:
  //   Integer containing the amount of handset cache in
  //   kilobytes.
  //
  // Example usage:
  //   $cache = $ua->getCache();
  //
  
function getCache() {
    return((int)
$this->_cache);
  }

  function 
getManufacturer() {
    return(
$this->_manufacturer);
  }

  function 
getExtra() {
    return(
$this->_extra);
  }

  function 
getImageFormats() {
    
$data   $this->_data[$this->_model];
    
$retval $data[imageformats];
    return(
$retval);
  }

  
// Method
  //
  // Returns:
  //   Integer describing what colour model the handset supports.
  //   Values have the following meaning:
  //     0 -> black and white
  //     1 -> 4 tone greyscale
  //     2 -> 256 colour
  //
  // Example usage:
  //   $ua->getColour()
  //
  
function getColour() {
     
$data   $this->_data[$this->_model];
     
$retval $data[colour];
     return(
$retval);
  }

  function 
getHTTPVersion() {
    return(
$this->_httpversion);
  }

  function 
isColour() {
    
$data   $this->_data[$this->_model];
    
$colour $data[colour];
    
$retval 0;
    if (
$colour IMODE_COLOUR_256) { 
      
$retval 1;
    };
    return(
$retval);
  }

  function 
isGreyScale() {
    
$data   $this->_data[$this->_model];
    
$colour $data[colour];
    
$retval 0;
    if (
$colour IMODE_COLOUR_GREYSCALE) { 
      
$retval 1;
    };
    return(
$retval);
  }

  function 
isBlackAndWhite() {
    
$data   $this->_data[$this->_model];
    
$colour $data[colour];
    
$retval 0;
    if (
$colour IMODE_COLOUR_BW) { 
      
$retval 1;
    };
    return(
$retval);
  }

  function 
supportsGIF() {

  }

  function 
supportsJPG() {

  }

  function 
supportsPNG() {

  }
 
  function 
getAllInfo() {

  }

}

?>