Ticket #4336: mobile.2.diff

File mobile.2.diff, 3.9 KB (added by jmorganson, 16 years ago)

This is a more extensive modification of the file, it adds the background for the getMobileUserAgent function

  • includes/mobile.php

    old new  
    3333/**/
    3434
    3535
     36
    3637/**
    3738 * Return true if the user agent is mobile device. Otherwise return false.
    3839 * The user agent is determined based on the return value of getScreenSize()
     
    4142 * @return true or false
    4243 */
    4344function isMobileUser() {
    44   return (getScreenSize() === false) ? false : true;
     45  return (getMobileUserAgent() === null) ? false : true;
    4546}
    4647
     48function getMobiles() {
    4749
    48 /**
    49  * Return the screen size of the mobile user agent. This can be used to do very
    50  * detailed modifications to the web site's layout for the mobile user agents.
    51  * If the user is not regocnized to be a mobile device then return false. If the
    52  * user is a known mobile device but the screen size is unknown then return
    53  * an empty array. Mobile users are detected based on $_SERVER['HTTP_USER_AGENT']
    54  * and a list of known mobile user agent identification strings.
    55  *
    56  * @return array(width, height) or false
    57  */
    58 function getScreenSize() {
    59   static $isChecked = false;
    60   static $screen = false;
    61 
    62   if (!$isChecked) {
    6350    /*
    6451     * The $mobiles array contains a list of known mobile phone user agent
    6552     * identification strings. Each key of the array is a substring of the
     
    7562     * an empty array or approximate dimensions.
    7663     */
    7764    $mobiles = array(
     65                /* Apple's iPhone */
     66                    'iPhone'     => array('width' => 320, 'height' => 480),
    7867                /* Phones using the Series 60 platform, e.g. Nokia 3650 and 6600. */
    7968                    'Series 60'  => array('width' => 176, 'height' => 208),
    8069                    'Series60'   => array('width' => 176, 'height' => 208),
     
    141130                     //'Opera' => array('width' => 176, 'height' => 208)
    142131             );
    143132
     133    return $mobiles;
     134}
     135
     136
     137/**
     138 * Return the mobile user agent. If the user agent is not regocnized to be
     139 * a known mobile device then return null. Mobile users are detected based
     140 * on $_SERVER['HTTP_USER_AGENT'] and a list of known mobile user agent
     141 * identification strings.
     142 *
     143 * @return array(width, height) if known or null
     144 */
     145function getMobileUserAgent() {
     146  static $isChecked = false;
     147  static $userAgent = null;
     148
     149  $mobiles = getMobiles();
     150
     151  if (!$isChecked) {
     152
    144153    /* Scan through $mobiles and try to find matching user agent. */
    145154    foreach (array_keys($mobiles) as $needle) {
    146155      /* Check if user agent matches this screen size key. */
    147156      if (strpos($_SERVER['HTTP_USER_AGENT'], $needle) !== false) {
    148         $screen = $mobiles[$needle];
     157        $userAgent = $needle;
    149158        break;
    150159      }
    151160    }
    152161
    153162    /* If the user agent was not in our list, check if the terminal accepts WML. */
    154     if ($screen === false) {
    155       if (browserAcceptsMediaType(array('WAP', 'WML'))) {
    156         $screen = array();
    157       }
     163    if ($userAgent === null) {
     164
     165      if (browserAcceptsMediaType(array('text/html', '\*/\*')))
     166        if (browserAcceptsMediaType(array('WAP')))
     167            $userAgent = 'wap';
     168
     169      else if (browserAcceptsMediaType(array('WML')))
     170        $userAgent = 'wml';
    158171    }
    159172
    160173    $isChecked = true;
    161174  }
    162   return $screen;
     175
     176  return $userAgent;
    163177}
    164178
    165179
     
    182196
    183197
    184198/**
     199 * Return the screen size of the mobile user agent. This can be used to do very
     200 * detailed modifications to the web site's layout for the mobile user agents.
     201 * Meant to be called if isMobileUser() returns true.
     202 *
     203 * @return array(width, height) if known or null
     204 */
     205function getScreenSize() {
     206
     207  $screen = null;
     208  $mobiles = getMobiles();
     209  $userAgent = getMobileUserAgent();
     210
     211  $screen = $mobiles[$userAgent];
     212
     213  return $screen;
     214}
     215
     216
     217/**
    185218 * Return the width of the screen in pixels. Meant to be called if isMobileUser()
    186219 * returns true. See also getScreensize().
    187220 *