Jump to content

User:Amgine/BrowserOsDetect 0.2 alpha/Source

From Meta, a Wikimedia project coordination wiki

Notes

[edit]

the script

[edit]

<?php
/**
 * BrowserOsDetect.php
 * ***
 * Purpose: to detect the operating system as reported by the browser via the
 * service.
 * *** 
 * Notes: To use this extension, add the following two lines to your 
 * 'Localsettings.php' file:
 
# Basic browser detection extension
include ("$IP/includes/browserdetect.php");

 *
 * BrowserOsDetect is largely based on PHPSniffer by Roger Raymond, available
 * at http://phpsniff.sourceforge.net/. It is a small part of the Wikimedia
 * plugin project 
 * (http://meta.wikimedia.org/wiki/Wikimedia_media_plugin#VLC_download_page).
 * BrowserOsDetect is released under the GFDL as an extension for Mediawiki
 *
 * @package Mediawiki extensions
 * @version $Alpha 0.2.1$
 * @author Amgine
 * *** */

/**
 * Get_ua
 * ***
 * Purpose: retrieve a user agent string from the server
 * ***
 * @return string $ua User agent string or FALSE
 * *** */
function get_ua() {
      if(isset($HTTP_SERVER_VARS['HTTP_USER_AGENT'])) {
            $ua = $HTTP_SERVER_VARS['HTTP_USER_AGENT'];
      } elseif(isset($_SERVER['HTTP_USER_AGENT'])) {
            $ua = $_SERVER['HTTP_USER_AGENT'];
      } else {
            // try to use the getenv function as a last resort
            $ua = getenv('HTTP_USER_AGENT');
      }
     
      if (!$ua)
            $ua = FALSE;

      return $ua;
}

/**
 * Get_os
 * ***
 * Purpose: to retrieve the Operating system from the user agent string provided
 * by the server.
 * ***
 * @param string $ua User Agent string from the server.
 * @return array $$browseros ['platform'], ['os']
 * *** */
 function get_os ( $ua ) {
         // Initialize $browseros
         $browseros['platform'] = '';
         $browseros['os'] = '';
         
         // regexes to use
        $regex_windows  = '/([^dar]win[dows]*)[\s]?([0-9a-z]*)[\w\s]?([a-z0-9.]*)/i';
        $regex_mac      = '/(68[k0]{1,3})|(ppc mac os x)|([p\S]{1,5}pc)|(darwin)/i';
        $regex_os2      = '/os\/2|ibm-webexplorer/i';
        $regex_sunos    = '/(sun|i86)[os\s]*([0-9]*)/i';
        $regex_irix     = '/(irix)[\s]*([0-9]*)/i';
        $regex_hpux     = '/(hp-ux)[\s]*([0-9]*)/i';
        $regex_aix      = '/aix([0-9]*)/i';
        $regex_dec      = '/dec|osfl|alphaserver|ultrix|alphastation/i';
        $regex_vms      = '/vax|openvms/i';
        $regex_sco      = '/sco|unix_sv/i';
        $regex_linux    = '/x11|inux/i';
        $regex_bsd      = '/(free)?(bsd)/i';
        $regex_amiga    = '/amiga[os]?/i';
        $regex_txtbrwsr = '/(lynx|dillo)?/i';

        // look for Windows Box
        if(preg_match_all($regex_windows, $ua, $match)) {
                //$match[1][count($match[0])-1];
                $v  = $match[2][count($match[0])-1];
                $v2 = $match[3][count($match[0])-1];
                // Establish NT 5.1 as Windows XP
                if(stristr($v,'NT') && $v2 == 5.1) $v = 'xp';
                // Establish NT 5.0 and Windows 2000 as win2k
                elseif($v == '2000') $v = '2k';
                elseif(stristr($v,'NT') && $v2 == 5.0) $v = '2k';
                // Establish 9x 4.90 as Windows 98
                elseif(stristr($v,'9x') && $v2 == 4.9) $v = '98';
                // See if we're running windows 3.1
                elseif($v.$v2 == '16bit') $v = '31';
                // otherwise display as is (31,95,98,NT,ME,XP)
                else $v .= $v2;
                // update browser info container array
                if(empty($v)) $v = 'win';
                $browseros['os'] = strtolower($v);
                $browseros['platform'] = 'win';
        }
        //  look for amiga OS
        elseif(preg_match($regex_amiga, $ua, $match)) {
                $browseros['platform'] = 'amiga';
                if(stristr($ua, 'morphos')) {
                        // checking for MorphOS
                        $browseros['os'] = 'morphos';
                } elseif(stristr($ua, 'mc680x0')) {
                        // checking for MC680x0
                        $browseros['os'] = 'mc680x0';
                } elseif(stristr($ua, 'ppc')) {
                        // checking for PPC
                        $browseros['os'] = 'ppc';
                } elseif(preg_match('/(AmigaOS [\.1-9]?)/i', $ua, $match)) {
                        // checking for AmigaOS version string
                        $browseros['os'] = $match[1];
                }
        }
        // look for OS2
        elseif( preg_match($regex_os2, $ua)) {
                $browseros['os'] = 'os2';
                $browseros['platform'] = 'os2';
        }
        //  look for *nix boxes
        //  sunos sets: platform = *nix ; os = sun|sun4|sun5|suni86
        elseif(preg_match($regex_sunos, $ua, $match)) {
                $browseros['platform'] = '*nix';
                if(!stristr('sun',$match[1])) $match[1] = 'sun'.$match[1];
                $browseros['os'] = $match[1].$match[2];
        }
        // look for mac
        // sets: platform = mac ; os = 68k or ppc
        elseif( preg_match($regex_mac, $ua, $match) ) {
                $browseros['platform'] = 'mac';
                $browseros['os'] = !empty($match[1]) ? '68k' : '';
                $browseros['os'] = !empty($match[2]) ? 'osx' : $browseros['os'];
                $browseros['os'] = !empty($match[3]) ? 'ppc' : $browseros['os'];
                $browseros['os'] = !empty($match[4]) ? 'osx' : $browseros['os'];
        }
        //  irix sets: platform = *nix ; os = irix|irix5|irix6|...
        elseif(preg_match($regex_irix, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = $match[1].$match[2];
        }
        //  hp-ux sets: platform = *nix ; os = hpux9|hpux10|...
        elseif(preg_match($regex_hpux, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $match[1] = str_replace('-','',$match[1]);
                $match[2] = (int) $match[2];
                $browseros['os'] = $match[1].$match[2];
        }
        //  aix sets: platform = *nix ; os = aix|aix1|aix2|aix3|...
        elseif(preg_match($regex_aix, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'aix'.$match[1];
        }
        //  dec sets: platform = *nix ; os = dec
        elseif(preg_match($regex_dec, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'dec';
        }
        //  vms sets: platform = *nix ; os = vms
        elseif(preg_match($regex_vms, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'vms';
        }
        //  sco sets: platform = *nix ; os = sco
        elseif(preg_match($regex_sco, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'sco';
        }
        //  unixware sets: platform = *nix ; os = unixware
        elseif(stristr($ua, 'unix_system_v')) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'unixware';
        }
        //  mpras sets: platform = *nix ; os = mpras
        elseif(stristr($ua, 'ncr')) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'mpras';
        }
        //  reliant sets: platform = *nix ; os = reliant
        elseif(stristr($ua, 'reliantunix')) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'reliant';
        }
        //  sinix sets: platform = *nix ; os = sinix
        elseif(stristr($ua, 'sinix')) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'sinix';
        }
        //  bsd sets: platform = *nix ; os = bsd|freebsd
        elseif(preg_match($regex_bsd, $ua, $match)){
                $browseros['platform'] = '*nix';
                $browseros['os'] = $match[1].$match[2];
        }
        //  last one to look for
        //  linux sets: platform = *nix ; os = linux
        elseif(preg_match($regex_linux, $ua, $match)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = 'linux';
        }
        // adding lynx browser as *nix, + dillo
        elseif(preg_match($regex_txtbrwsr, $ua)) {
                $browseros['platform'] = '*nix';
                $browseros['os'] = '*nix';
        } else {
        // nothing found
                $browseros['platform'] = 'other';
                $browseros['os'] = 'unknown';
        }
        
        return $browseros;
 }
  
function wfBrowserOsDetect() {
    global $wgParser, $wgMessageCache;
   
    $wgMessageCache->addMessages( array(
                                        'browserosdetect' => 'Error'
                                        )
                                  );

    $wgParser->setHook( "BrowserOsDetect", "BrowserOsDetect" );
}

function BrowserOsDetect () {

// disable cache
global $wgTitle;
$dbw =& wfGetDB( DB_MASTER );
$dbw->update( 'cur', array( 'cur_touched' => $dbw->timestamp( time() + 120 ) ), 
    array( 
        'cur_namespace' => $wgTitle->getNamespace(), 
        'cur_title' => $wgTitle->getDBkey() 
    ), 'BrowserOsDetect' 
);

        // Get the user agent string
        $ua = get_ua();
        // Get the OS from the user agent string
        $brwseros = get_os( $ua );
        // Create and populate table
        $output = '<div style="border: 1px solid gray;">
<table>
<tr><td style="text-align: right;">Your platform:</td><td>'.$brwseros['platform'].'</td></tr>
<tr><td style="text-align: right;">Your OS:</td><td>'.$brwseros['os'].'</td></tr>
</table>
'.$ua."<br />\n";

        return $output;
}

$wgExtensionFunctions[] = "wfBrowserOsDetect";

?>