Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Client Informations - browser_detection_functions.inc.php

browser_detection_functions.inc.php

Caricato da: Antometal
Scarica il programma completo

  1. <?php
  2. /*
  3. Script Name: Full Featured PHP Browser/OS detection
  4. Author: Harald Hope, Website: http://techpatterns.com/
  5. Script Source URI: http://techpatterns.com/downloads/php_browser_detection.php
  6. Version 4.10.2
  7. Copyright (C) 26 December 2008
  8.  
  9. Special thanks to alanjstr for cleaning up the code, especially on function browser_version(), which he improved
  10. greatly. Also to Tapio Markula, for his initial inspiration of creating a useable php browser detector.
  11.  
  12. This program is free software; you can redistribute it and/or modify it under
  13. the terms of the GNU General Public License as published by the Free Software
  14. Foundation; either version 3 of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful, but WITHOUT
  17. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  18. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  19.  
  20. Get the full text of the GPL here: http://www.gnu.org/licenses/gpl.txt
  21.  
  22. Coding conventions:
  23. http://cvs.sourceforge.net/viewcvs.py/phpbb/phpBB2/docs/codingstandards.htm?rev=1.3
  24. */
  25.  
  26. /******************************************
  27. this is currently set to accept 11 parameters, although you can add as many as you want:
  28. 1. safe - returns true/false, you can determine what makes the browser be safe lower down,
  29.         currently it's set for ns4 and pre version 1 mozillas not being safe, plus all older browsers
  30. 2. ie_version - tests to see what general IE it is, ie5x-6, ie4, or ieMac, returns these values.
  31. 3. moz_version - returns array of moz version, version number (includes full version, + etc), rv number (for math
  32.         comparison), rv number (for full rv, including alpha and beta versions), and release date
  33. 4. dom - returns true/false if it is a basic dom browser, ie >= 5, opera >= 5, all new mozillas, safaris, konquerors
  34. 5. os - returns which os is being used
  35. 6. os_number - returns windows versions, 95, 98, me, nt 4, nt 5 [windows 2000], nt 5.1 [windows xp],
  36.         Just added: os x detection[crude] otherwise returns false
  37. 7. browser - returns the browser name, in shorthand: ie, ie4, ie5x, op, moz, konq, saf, ns4
  38. 8. number - returns the browser version number, if available, otherwise returns '' [not available]
  39. 9. full - returns this array: $browser_name, $version_number, $ie_version, $dom_browser,
  40.         $safe_browser, $os, $os_number, $s_browser [the browser search string from the browser array], $type
  41. 10. type - returns whether it's a bot or a browser
  42. 11. math_number - returns basic version number, for math comparison, ie. 1.2rel2a becomes 1.2
  43. *******************************************/
  44.  
  45. // main script, uses two other functions, which_os() and browser_version() as needed
  46. function browser_detection( $which_test ) {
  47.         /*
  48.         uncomment the global variable declaration if you want the variables to be available on a global level
  49.         throughout your php page, make sure that php is configured to support the use of globals first!
  50.         Use of globals should be avoided however, and they are not necessary with this script
  51.         */
  52.  
  53.         /*global $dom_browser, $safe_browser, $browser_user_agent, $os, $browser_name, $s_browser, $ie_version,
  54.         $version_number, $os_number, $b_repeat, $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release;*/
  55.  
  56.         static $dom_browser, $safe_browser, $browser_user_agent, $os, $browser_name, $s_browser, $ie_version,
  57.         $version_number, $os_number, $b_repeat, $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release,
  58.         $type, $math_version_number;
  59.  
  60.         /*
  61.         this makes the test only run once no matter how many times you call it
  62.         since all the variables are filled on the first run through, it's only a matter of returning the
  63.         the right ones
  64.         */
  65.         if ( !$b_repeat )
  66.         {
  67.                 //initialize all variables with default values to prevent error
  68.                 $dom_browser = false;
  69.                 $type = 'bot';// default to bot since you never know with bots
  70.                 $safe_browser = false;
  71.                 $os = '';
  72.                 $os_number = '';
  73.                 $a_os_data = '';
  74.                 $browser_name = '';
  75.                 $version_number = '';
  76.                 $math_version_number = '';
  77.                 $ie_version = '';
  78.                 $moz_version = '';
  79.                 $moz_version_number = '';
  80.                 $moz_rv = '';
  81.                 $moz_rv_full = '';
  82.                 $moz_release = '';
  83.                 $webkit_types = '';
  84.                 $webkit_version = '';
  85.                 $b_success = false;// boolean for if browser found in main test
  86.  
  87.                 //make navigator user agent string lower case to make sure all versions get caught
  88.                 // isset protects against blank user agent failure
  89.                 $browser_user_agent = ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) ? strtolower( $_SERVER['HTTP_USER_AGENT'] ) : '';
  90.  
  91.                 /*
  92.                 pack the browser type array, in this order
  93.                 the order is important, because opera must be tested first, then omniweb [which has safari data in string],
  94.                 same for konqueror, then safari, then gecko, since safari navigator user agent id's with 'gecko' in string.
  95.                 note that $dom_browser is set for all  modern dom browsers, this gives you a default to use.
  96.  
  97.                 array[0] = id string for useragent, array[1] is if dom capable, array[2] is working name for browser,
  98.                 array[3] identifies navigator useragent type
  99.  
  100.                 Note: all browser strings are in lower case to match the strtolower output, this avoids possible detection
  101.                 errors
  102.  
  103.                 Note: There are currently 5 navigator user agent types:
  104.                 bro - modern, css supporting browser.
  105.                 bbro - basic browser, text only, table only, defective css implementation
  106.                 bot - search type spider
  107.                 dow - known download agent
  108.                 lib - standard http libraries
  109.                 */
  110.                 // known browsers, list will be updated routinely, check back now and then
  111.                 $a_browser_types[] = array( 'opera', true, 'op', 'bro' );
  112.                 $a_browser_types[] = array( 'msie', true, 'ie', 'bro' );
  113.                 $a_browser_types[] = array( 'konqueror', true, 'konq', 'bro' );
  114.                 $a_browser_types[] = array( 'webkit', true, 'webkit', 'bro' );
  115.                 // covers Netscape 6-7, K-Meleon, Most linux versions, uses moz array below
  116.                 $a_browser_types[] = array( 'gecko', true, 'moz', 'bro' );
  117.                 $a_browser_types[] = array( 'netpositive', false, 'netp', 'bbro' );// beos browser
  118.                 $a_browser_types[] = array( 'lynx', false, 'lynx', 'bbro' ); // command line browser
  119.                 $a_browser_types[] = array( 'elinks ', false, 'elinks', 'bbro' ); // new version of links
  120.                 $a_browser_types[] = array( 'elinks', false, 'elinks', 'bbro' ); // alternate id for it
  121.                 $a_browser_types[] = array( 'links ', false, 'links', 'bbro' ); // old name for links
  122.                 $a_browser_types[] = array( 'links', false, 'links', 'bbro' ); // alternate id for it
  123.                 $a_browser_types[] = array( 'w3m', false, 'w3m', 'bbro' ); // open source browser, more features than lynx/links
  124.                 $a_browser_types[] = array( 'webtv', false, 'webtv', 'bbro' );// junk ms webtv
  125.                 $a_browser_types[] = array( 'amaya', false, 'amaya', 'bbro' );// w3c browser
  126.                 $a_browser_types[] = array( 'dillo', false, 'dillo', 'bbro' );// linux browser, basic table support
  127.                 $a_browser_types[] = array( 'ibrowse', false, 'ibrowse', 'bbro' );// amiga browser
  128.                 $a_browser_types[] = array( 'icab', false, 'icab', 'bro' );// mac browser
  129.                 $a_browser_types[] = array( 'crazy browser', true, 'ie', 'bro' );// uses ie rendering engine
  130.                 $a_browser_types[] = array( 'sonyericssonp800', false, 'sonyericssonp800', 'bbro' );// sony ericsson handheld
  131.  
  132.                 // search engine spider bots:
  133.                 $a_browser_types[] = array( 'googlebot', false, 'google', 'bot' );// google
  134.                 $a_browser_types[] = array( 'mediapartners-google', false, 'adsense', 'bot' );// google adsense
  135.                 $a_browser_types[] = array( 'yahoo-verticalcrawler', false, 'yahoo', 'bot' );// old yahoo bot
  136.                 $a_browser_types[] = array( 'yahoo! slurp', false, 'yahoo', 'bot' ); // new yahoo bot
  137.                 $a_browser_types[] = array( 'yahoo-mm', false, 'yahoomm', 'bot' ); // gets Yahoo-MMCrawler and Yahoo-MMAudVid bots
  138.                 $a_browser_types[] = array( 'inktomi', false, 'inktomi', 'bot' ); // inktomi bot
  139.                 $a_browser_types[] = array( 'slurp', false, 'inktomi', 'bot' ); // inktomi bot
  140.                 $a_browser_types[] = array( 'fast-webcrawler', false, 'fast', 'bot' );// Fast AllTheWeb
  141.                 $a_browser_types[] = array( 'msnbot', false, 'msn', 'bot' );// msn search
  142.                 $a_browser_types[] = array( 'ask jeeves', false, 'ask', 'bot' ); //jeeves/teoma
  143.                 $a_browser_types[] = array( 'teoma', false, 'ask', 'bot' );//jeeves teoma
  144.                 $a_browser_types[] = array( 'scooter', false, 'scooter', 'bot' );// altavista
  145.                 $a_browser_types[] = array( 'openbot', false, 'openbot', 'bot' );// openbot, from taiwan
  146.                 $a_browser_types[] = array( 'ia_archiver', false, 'ia_archiver', 'bot' );// ia archiver
  147.                 $a_browser_types[] = array( 'zyborg', false, 'looksmart', 'bot' );// looksmart
  148.                 $a_browser_types[] = array( 'almaden', false, 'ibm', 'bot' );// ibm almaden web crawler
  149.                 $a_browser_types[] = array( 'baiduspider', false, 'baidu', 'bot' );// Baiduspider asian search spider
  150.                 $a_browser_types[] = array( 'psbot', false, 'psbot', 'bot' );// psbot image crawler
  151.                 $a_browser_types[] = array( 'gigabot', false, 'gigabot', 'bot' );// gigabot crawler
  152.                 $a_browser_types[] = array( 'naverbot', false, 'naverbot', 'bot' );// naverbot crawler, bad bot, block
  153.                 $a_browser_types[] = array( 'surveybot', false, 'surveybot', 'bot' );//
  154.                 $a_browser_types[] = array( 'boitho.com-dc', false, 'boitho', 'bot' );//norwegian search engine
  155.                 $a_browser_types[] = array( 'objectssearch', false, 'objectsearch', 'bot' );// open source search engine
  156.                 $a_browser_types[] = array( 'answerbus', false, 'answerbus', 'bot' );// http://www.answerbus.com/, web questions
  157.                 $a_browser_types[] = array( 'sohu-search', false, 'sohu', 'bot' );// chinese media company, search component
  158.                 $a_browser_types[] = array( 'iltrovatore-setaccio', false, 'il-set', 'bot' );
  159.  
  160.                 // various http utility libaries
  161.                 $a_browser_types[] = array( 'w3c_validator', false, 'w3c', 'lib' ); // uses libperl, make first
  162.                 $a_browser_types[] = array( 'wdg_validator', false, 'wdg', 'lib' ); //
  163.                 $a_browser_types[] = array( 'libwww-perl', false, 'libwww-perl', 'lib' );
  164.                 $a_browser_types[] = array( 'jakarta commons-httpclient', false, 'jakarta', 'lib' );
  165.                 $a_browser_types[] = array( 'python-urllib', false, 'python-urllib', 'lib' );
  166.  
  167.                 // download apps
  168.                 $a_browser_types[] = array( 'getright', false, 'getright', 'dow' );
  169.                 $a_browser_types[] = array( 'wget', false, 'wget', 'dow' );// open source downloader, obeys robots.txt
  170.  
  171.                 // netscape 4 and earlier tests, put last so spiders don't get caught
  172.                 $a_browser_types[] = array( 'mozilla/4.', false, 'ns', 'bbro' );
  173.                 $a_browser_types[] = array( 'mozilla/3.', false, 'ns', 'bbro' );
  174.                 $a_browser_types[] = array( 'mozilla/2.', false, 'ns', 'bbro' );
  175.  
  176.                 //$a_browser_types[] = array( '', false ); // browser array template
  177.  
  178.                 /*
  179.                 moz types array
  180.                 note the order, netscape6 must come before netscape, which  is how netscape 7 id's itself.
  181.                 rv comes last in case it is plain old mozilla
  182.                 */
  183.                 $moz_types = array( 'firebird', 'flock', 'seamonkey', 'phoenix', 'iceweasel', 'firefox', 'galeon', 'k-meleon', 'camino', 'epiphany', 'netscape6', 'netscape', 'multizilla', 'swiftfox', 'rv' );
  184.  
  185.                 /*
  186.                 webkit types, this is going to expand over time as webkit browsers spread
  187.                 konqueror is probably going to move to webkit, so this is preparing for that
  188.                 It wil now default to khtml. gtklauncher is the temp id for epiphany, might
  189.                 change. Defaults to applewebkit, and will all show the webkit number.
  190.                 */
  191.                 $webkit_types = array( 'arora', 'midori', 'chrome', 'epiphany', 'gtklauncher', 'iphone', 'omniweb', 'konqueror', 'safari', 'applewebkit' );
  192.  
  193.                 /*
  194.                 run through the browser_types array, break if you hit a match, if no match, assume old browser
  195.                 or non dom browser, assigns false value to $b_success.
  196.                 */
  197.                 $i_count = count($a_browser_types);
  198.                 for ($i = 0; $i < $i_count; $i++)
  199.                 {
  200.                         //unpacks browser array, assigns to variables
  201.                         $s_browser = $a_browser_types[$i][0];// text string to id browser from array
  202.  
  203.                         if (stristr($browser_user_agent, $s_browser))
  204.                         {
  205.                                 // it defaults to true, will become false below if needed
  206.                                 // this keeps it easier to keep track of what is safe, only
  207.                                 //explicit false assignment will make it false.
  208.                                 $safe_browser = true;
  209.  
  210.                                 // assign values based on match of user agent string
  211.                                 $dom_browser = $a_browser_types[$i][1];// hardcoded dom support from array
  212.                                 $browser_name = $a_browser_types[$i][2];// working name for browser
  213.                                 $type = $a_browser_types[$i][3];// sets whether bot or browser
  214.  
  215.                                 switch ( $browser_name )
  216.                                 {
  217.                                         // this is modified quite a bit, now will return proper netscape version number
  218.                                         // check your implementation to make sure it works
  219.                                         case 'ns':
  220.                                                 $safe_browser = false;
  221.                                                 $version_number = browser_version( $browser_user_agent, 'mozilla' );
  222.                                                 break;
  223.                                         case 'moz':
  224.                                                 /*
  225.                                                 note: The 'rv' test is not absolute since the rv number is very different on
  226.                                                 different versions, for example Galean doesn't use the same rv version as Mozilla,
  227.                                                 neither do later Netscapes, like 7.x. For more on this, read the full mozilla numbering
  228.                                                 conventions here:
  229.                                                 http://www.mozilla.org/releases/cvstags.html
  230.                                                 */
  231.  
  232.                                                 // this will return alpha and beta version numbers, if present
  233.                                                 $moz_rv_full = browser_version( $browser_user_agent, 'rv' );
  234.                                                 // this slices them back off for math comparisons
  235.                                                 $moz_rv = substr( $moz_rv_full, 0, 3 );
  236.  
  237.                                                 // this is to pull out specific mozilla versions, firebird, netscape etc..
  238.                                                 $i_count = count( $moz_types );
  239.                                                 for ( $i = 0; $i < $i_count; $i++ )
  240.                                                 {
  241.                                                         if ( stristr( $browser_user_agent, $moz_types[$i] ) )
  242.                                                         {
  243.                                                                 $moz_version = $moz_types[$i];
  244.                                                                 $moz_version_number = browser_version( $browser_user_agent, $moz_version );
  245.                                                                 break;
  246.                                                         }
  247.                                                 }
  248.                                                 // this is necesary to protect against false id'ed moz'es and new moz'es.
  249.                                                 // this corrects for galeon, or any other moz browser without an rv number
  250.                                                 if ( !$moz_rv )
  251.                                                 {
  252.                                                         $moz_rv = substr( $moz_version_number, 0, 3 );
  253.                                                         $moz_rv_full = $moz_version_number;
  254.                                                         /*
  255.                                                         // you can use this instead if you are running php >= 4.2
  256.                                                         $moz_rv = floatval( $moz_version_number );
  257.                                                         $moz_rv_full = $moz_version_number;
  258.                                                         */
  259.                                                 }
  260.                                                 // this corrects the version name in case it went to the default 'rv' for the test
  261.                                                 if ( $moz_version == 'rv' )
  262.                                                 {
  263.                                                         $moz_version = 'mozilla';
  264.                                                 }
  265.  
  266.                                                 //the moz version will be taken from the rv number, see notes above for rv problems
  267.                                                 $version_number = $moz_rv;
  268.                                                 // gets the actual release date, necessary if you need to do functionality tests
  269.                                                 $moz_release = browser_version( $browser_user_agent, 'gecko/' );
  270.                                                 /*
  271.                                                 Test for mozilla 0.9.x / netscape 6.x
  272.                                                 test your javascript/CSS to see if it works in these mozilla releases, if it does, just default it to:
  273.                                                 $safe_browser = true;
  274.                                                 */
  275.                                                 if ( ( $moz_release < 20020400 ) || ( $moz_rv < 1 ) )
  276.                                                 {
  277.                                                         $safe_browser = false;
  278.                                                 }
  279.                                                 break;
  280.                                         case 'ie':
  281.                                                 $version_number = browser_version( $browser_user_agent, $s_browser );
  282.                                                 // first test for IE 5x mac, that's the most problematic IE out there
  283.                                                 if ( stristr( $browser_user_agent, 'mac') )
  284.                                                 {
  285.                                                         $ie_version = 'ieMac';
  286.                                                 }
  287.                                                 // this assigns a general ie id to the $ie_version variable
  288.                                                 elseif ( $version_number >= 5 )
  289.                                                 {
  290.                                                         $ie_version = 'ie5x';
  291.                                                 }
  292.                                                 elseif ( ( $version_number > 3 ) && ( $version_number < 5 ) )
  293.                                                 {
  294.                                                         $dom_browser = false;
  295.                                                         $ie_version = 'ie4';
  296.                                                         // this depends on what you're using the script for, make sure this fits your needs
  297.                                                         $safe_browser = true;
  298.                                                 }
  299.                                                 else
  300.                                                 {
  301.                                                         $ie_version = 'old';
  302.                                                         $dom_browser = false;
  303.                                                         $safe_browser = false;
  304.                                                 }
  305.                                                 break;
  306.                                         case 'op':
  307.                                                 $version_number = browser_version( $browser_user_agent, $s_browser );
  308.                                                 if ( $version_number < 5 )// opera 4 wasn't very useable.
  309.                                                 {
  310.                                                         $safe_browser = false;
  311.                                                 }
  312.                                                 break;
  313.                                         // note: webkit returns always the webkit version number, not the specific user
  314.                                         // agent version, ie, webkit 583, not chrome 0.3
  315.                                         case 'webkit':
  316.                                                 $version_number = browser_version( $browser_user_agent, $s_browser );
  317.                                                 // this is to pull out specific mozilla versions, firebird, netscape etc..
  318.                                                 $i_count = count( $webkit_types );
  319.                                                 for ( $i = 0; $i < $i_count; $i++ )
  320.                                                 {
  321.                                                         if ( stristr( $browser_user_agent, $webkit_types[$i] ) )
  322.                                                         {
  323.                                                                 $webkit_version = $webkit_types[$i];
  324.                                                                 $webkit_version_number = browser_version( $browser_user_agent, $webkit_version );
  325.                                                                 // epiphany hack
  326.                                                                 if ( $webkit_types[$i] == 'gtklauncher' )
  327.                                                                 {
  328.                                                                         $s_browser = 'Epiphany';
  329.                                                                 }
  330.                                                                 else
  331.                                                                 {
  332.                                                                         $s_browser = $webkit_types[$i];
  333.                                                                 }
  334.                                                                 break;
  335.                                                         }
  336.                                                 }
  337.                                                 break;
  338.                                         default:
  339.                                                 $version_number = browser_version( $browser_user_agent, $s_browser );
  340.                                                 break;
  341.                                 }
  342.                                 // the browser was id'ed
  343.                                 $b_success = true;
  344.                                 break;
  345.                         }
  346.                 }
  347.  
  348.                 //assigns defaults if the browser was not found in the loop test
  349.                 if ( !$b_success )
  350.                 {
  351.                         /*
  352.                                 this will return the first part of the browser string if the above id's failed
  353.                                 usually the first part of the browser string has the navigator useragent name/version in it.
  354.                                 This will usually correctly id the browser and the browser number if it didn't get
  355.                                 caught by the above routine.
  356.                                 If you want a '' to do a if browser == '' type test, just comment out all lines below
  357.                                 except for the last line, and uncomment the last line. If you want undefined values,
  358.                                 the browser_name is '', you can always test for that
  359.                         */
  360.                         // delete this part if you want an unknown browser returned
  361.                         $s_browser = substr( $browser_user_agent, 0, strcspn( $browser_user_agent , '();') );
  362.                         // this extracts just the browser name from the string
  363.                         ereg('[^0-9][a-z]*-*\ *[a-z]*\ *[a-z]*', $s_browser, $r );
  364.                         $s_browser = $r[0];
  365.                         $version_number = browser_version( $browser_user_agent, $s_browser );
  366.  
  367.                         // then uncomment this part
  368.                         //$s_browser = '';//deletes the last array item in case the browser was not a match
  369.                 }
  370.                 // get os data, mac os x test requires browser/version information, this is a change from older scripts
  371.                 $a_os_data = which_os( $browser_user_agent, $browser_name, $version_number );
  372.                 $os = $a_os_data[0];// os name, abbreviated
  373.                 $os_number = $a_os_data[1];// os number or version if available
  374.  
  375.                 // this ends the run through once if clause, set the boolean
  376.                 //to true so the function won't retest everything
  377.                 $b_repeat = true;
  378.  
  379.                 // pulls out primary version number from more complex string, like 7.5a,
  380.                 // use this for numeric version comparison
  381.                 $m = array();
  382.                 if ( ereg('[0-9]*\.*[0-9]*', $version_number, $m ) )
  383.                 {
  384.                         $math_version_number = $m[0];
  385.                         //print_r($m);
  386.                 }
  387.  
  388.         }
  389.         //$version_number = $_SERVER["REMOTE_ADDR"];
  390.         /*
  391.         This is where you return values based on what parameter you used to call the function
  392.         $which_test is the passed parameter in the initial browser_detection('os') for example call
  393.         */
  394.         switch ( $which_test )
  395.         {
  396.                 case 'safe':// returns true/false if your tests determine it's a safe browser
  397.                         // you can change the tests to determine what is a safeBrowser for your scripts
  398.                         // in this case sub rv 1 Mozillas and Netscape 4x's trigger the unsafe condition
  399.                         return $safe_browser;
  400.                         break;
  401.                 case 'ie_version': // returns ieMac or ie5x
  402.                         return $ie_version;
  403.                         break;
  404.                 case 'moz_version':// returns array of all relevant moz information
  405.                         $moz_array = array( $moz_version, $moz_version_number, $moz_rv, $moz_rv_full, $moz_release );
  406.                         return $moz_array;
  407.                         break;
  408.                 case 'dom':// returns true/fale if a DOM capable browser
  409.                         return $dom_browser;
  410.                         break;
  411.                 case 'os':// returns os name
  412.                         return $os;
  413.                         break;
  414.                 case 'os_number':// returns os number if windows
  415.                         return $os_number;
  416.                         break;
  417.                 case 'browser':// returns browser name
  418.                         return $browser_name;
  419.                         break;
  420.                 case 'number':// returns browser number
  421.                         return $version_number;
  422.                         break;
  423.                 case 'full':// returns all relevant browser information in an array
  424.                         $full_array = array( $browser_name, $version_number, $ie_version, $dom_browser, $safe_browser, $os, $os_number, $s_browser, $type, $math_version_number );
  425.                         return $full_array;
  426.                         break;
  427.                 case 'type':// returns what type, bot, browser, maybe downloader in future
  428.                         return $type;
  429.                         break;
  430.                 case 'math_number':// returns numerical version number, for number comparisons
  431.                         return $math_version_number;
  432.                         break;
  433.                 default:
  434.                         break;
  435.         }
  436. }
  437.  
  438. // gets which os from the browser string
  439. function which_os ( $browser_string, $browser_name, $version_number  )
  440. {
  441.         // initialize variables
  442.         $os = '';
  443.         $os_version = '';
  444.         /*
  445.         packs the os array
  446.         use this order since some navigator user agents will put 'macintosh' in the navigator user agent string
  447.         which would make the nt test register true
  448.         */
  449.         $a_mac = array( 'mac68k', 'macppc' );// this is not used currently
  450.         // same logic, check in order to catch the os's in order, last is always default item
  451.         $a_unix = array( 'freebsd', 'openbsd', 'netbsd', 'bsd', 'unixware', 'solaris', 'sunos', 'sun4', 'sun5', 'suni86', 'sun', 'irix5', 'irix6', 'irix', 'hpux9', 'hpux10', 'hpux11', 'hpux', 'hp-ux', 'aix1', 'aix2', 'aix3', 'aix4', 'aix5', 'aix', 'sco', 'unixware', 'mpras', 'reliant', 'dec', 'sinix', 'unix' );
  452.         // only sometimes will you get a linux distro to id itself...
  453.         $a_linux = array( 'ubuntu', 'kubuntu', 'xubuntu', 'mepis', 'xandros', 'linspire', 'winspire', 'sidux', 'kanotix', 'debian', 'opensuse', 'suse', 'fedora', 'redhat', 'slackware', 'slax', 'mandrake', 'mandriva', 'gentoo', 'sabayon', 'linux' );
  454.         $a_linux_process = array ( 'i386', 'i586', 'i686' );// not use currently
  455.         // note, order of os very important in os array, you will get failed ids if changed
  456.         $a_os = array( 'beos', 'os2', 'amiga', 'webtv', 'mac', 'nt', 'win', $a_unix, $a_linux );
  457.  
  458.         //os tester
  459.         $i_count = count( $a_os );
  460.         for ( $i = 0; $i < $i_count; $i++ )
  461.         {
  462.                 // unpacks os array, assigns to variable
  463.                 $s_os = $a_os[$i];
  464.  
  465.                 // assign os to global os variable, os flag true on success
  466.                 // !stristr($browser_string, "linux" ) corrects a linux detection bug
  467.                 if ( !is_array( $s_os ) && stristr( $browser_string, $s_os ) && !stristr( $browser_string, "linux" ) )
  468.                 {
  469.                         $os = $s_os;
  470.  
  471.                         switch ( $os )
  472.                         {
  473.                                 case 'win':
  474.                                         if ( strstr( $browser_string, '95' ) )
  475.                                         {
  476.                                                 $os_version = '95';
  477.                                         }
  478.                                         elseif ( ( strstr( $browser_string, '9x 4.9' ) ) || ( strstr( $browser_string, 'me' ) ) )
  479.                                         {
  480.                                                 $os_version = 'me';
  481.                                         }
  482.                                         elseif ( strstr( $browser_string, '98' ) )
  483.                                         {
  484.                                                 $os_version = '98';
  485.                                         }
  486.                                         elseif ( strstr( $browser_string, '2000' ) )// windows 2000, for opera ID
  487.                                         {
  488.                                                 $os_version = 5.0;
  489.                                                 $os = 'nt';
  490.                                         }
  491.                                         elseif ( strstr( $browser_string, 'xp' ) )// windows 2000, for opera ID
  492.                                         {
  493.                                                 $os_version = 5.1;
  494.                                                 $os = 'nt';
  495.                                         }
  496.                                         elseif ( strstr( $browser_string, '2003' ) )// windows server 2003, for opera ID
  497.                                         {
  498.                                                 $os_version = 5.2;
  499.                                                 $os = 'nt';
  500.                                         }
  501.                                         elseif ( strstr( $browser_string, 'vista' ) )// windows vista, for opera ID
  502.                                         {
  503.                                                 $os_version = 6.0;
  504.                                                 $os = 'nt';
  505.                                         }
  506.                                         elseif ( strstr( $browser_string, 'ce' ) )// windows CE
  507.                                         {
  508.                                                 $os_version = 'ce';
  509.                                         }
  510.                                         break;
  511.                                 case 'nt':
  512.                                         if ( strstr( $browser_string, 'nt 6.1' ) )// windows 7
  513.                                         {
  514.                                                 $os_version = 6.1;
  515.                                                 $os = 'nt';
  516.                                         }
  517.                                         elseif ( strstr( $browser_string, 'nt 6.0' ) )// windows vista/server 2008
  518.                                         {
  519.                                                 $os_version = 6.0;
  520.                                                 $os = 'nt';
  521.                                         }
  522.                                         elseif ( strstr( $browser_string, 'nt 5.2' ) )// windows server 2003
  523.                                         {
  524.                                                 $os_version = 5.2;
  525.                                                 $os = 'nt';
  526.                                         }
  527.                                         elseif ( strstr( $browser_string, 'nt 5.1' ) || strstr( $browser_string, 'xp' ) )// windows xp
  528.                                         {
  529.                                                 $os_version = 5.1;//
  530.                                         }
  531.                                         elseif ( strstr( $browser_string, 'nt 5' ) || strstr( $browser_string, '2000' ) )// windows 2000
  532.                                         {
  533.                                                 $os_version = 5.0;
  534.                                         }
  535.                                         elseif ( strstr( $browser_string, 'nt 4' ) )// nt 4
  536.                                         {
  537.                                                 $os_version = 4;
  538.                                         }
  539.                                         elseif ( strstr( $browser_string, 'nt 3' ) )// nt 4
  540.                                         {
  541.                                                 $os_version = 3;
  542.                                         }
  543.                                         break;
  544.                                 case 'mac':
  545.                                         if ( strstr( $browser_string, 'os x' ) )
  546.                                         {
  547.                                                 $os_version = 10;
  548.                                         }
  549.                                         //this is a crude test for os x, since safari, camino, ie 5.2, & moz >= rv 1.3
  550.                                         //are only made for os x
  551.                                         elseif ( ( $browser_name == 'saf' ) || ( $browser_name == 'cam' ) ||
  552.                                                 ( ( $browser_name == 'moz' ) && ( $version_number >= 1.3 ) ) ||
  553.                                                 ( ( $browser_name == 'ie' ) && ( $version_number >= 5.2 ) ) )
  554.                                         {
  555.                                                 $os_version = 10;
  556.                                         }
  557.                                         break;
  558.                                 default:
  559.                                         break;
  560.                         }
  561.                         break;
  562.                 }
  563.                 // check that it's an array, check it's the second to last item
  564.                 //in the main os array, the unix one that is
  565.                 elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 2 ) ) )
  566.                 {
  567.                         $i_count = count($s_os);
  568.                         for ($j = 0; $j < $i_count; $j++)
  569.                         {
  570.                                 if ( stristr( $browser_string, $s_os[$j] ) )
  571.                                 {
  572.                                         $os = 'unix'; //if the os is in the unix array, it's unix, obviously...
  573.                                         $os_version = ( $s_os[$j] != 'unix' ) ? $s_os[$j] : '';// assign sub unix version from the unix array
  574.                                         break;
  575.                                 }
  576.                         }
  577.                 }
  578.                 // check that it's an array, check it's the last item
  579.                 //in the main os array, the linux one that is
  580.                 elseif ( is_array( $s_os ) && ( $i == ( count( $a_os ) - 1 ) ) )
  581.                 {
  582.                         $i_count = count($s_os);
  583.                         for ($j = 0; $j < $i_count; $j++)
  584.                         {
  585.                                 if ( stristr( $browser_string, $s_os[$j] ) )
  586.                                 {
  587.                                         $os = 'lin';
  588.                                         // assign linux distro from the linux array, there's a default
  589.                                         //search for 'lin', if it's that, set version to ''
  590.                                         $os_version = ( $s_os[$j] != 'linux' ) ? $s_os[$j] : '';
  591.                                         break;
  592.                                 }
  593.                         }
  594.                 }
  595.         }
  596.  
  597.         // pack the os data array for return to main function
  598.         $os_data = array( $os, $os_version );
  599.         return $os_data;
  600. }
  601.  
  602. // function returns browser number, gecko rv number, or gecko release date
  603. //function browser_version( $browser_user_agent, $search_string, $substring_length )
  604. function browser_version( $browser_user_agent, $search_string )
  605. {
  606.         // 12 is the longest that will be required, handles release dates: 20020323; 0.8.0+
  607.         $substring_length = 12;
  608.         //initialize browser number, will return '' if not found
  609.         $browser_number = '';
  610.  
  611.         // use the passed parameter for $search_string
  612.         // start the substring slice right after these moz search strings
  613.         // there are some cases of double msie id's, first in string and then with then number
  614.         $start_pos = 0;
  615.         /* this test covers you for multiple occurrences of string, only with ie though
  616.          with for example google bot you want the first occurance returned, since that's where the
  617.         numbering happens */
  618.  
  619.         for ( $i = 0; $i < 4; $i++ )
  620.         {
  621.                 //start the search after the first string occurrence
  622.                 if ( strpos( $browser_user_agent, $search_string, $start_pos ) !== false )
  623.                 {
  624.                         //update start position if position found
  625.                         $start_pos = strpos( $browser_user_agent, $search_string, $start_pos ) + strlen( $search_string );
  626.                         if ( $search_string != 'msie' )
  627.                         {
  628.                                 break;
  629.                         }
  630.                 }
  631.                 else
  632.                 {
  633.                         break;
  634.                 }
  635.         }
  636.  
  637.         // this is just to get the release date, not other moz information
  638.         // also corrects for the omniweb 'v'
  639.         if ( $search_string != 'gecko/' )
  640.         {
  641.                 if ( $search_string == 'omniweb' )
  642.                 {
  643.                         $start_pos += 2;// handles the v in 'omniweb/v532.xx
  644.                 }
  645.                 else
  646.                 {
  647.                         $start_pos++;
  648.                 }
  649.         }
  650.  
  651.         // Initial trimming
  652.         $browser_number = substr( $browser_user_agent, $start_pos, $substring_length );
  653.  
  654.         // Find the space, ;, or parentheses that ends the number
  655.         $browser_number = substr( $browser_number, 0, strcspn($browser_number, ' );') );
  656.  
  657.         //make sure the returned value is actually the id number and not a string
  658.         // otherwise return ''
  659.         if ( !is_numeric( substr( $browser_number, 0, 1 ) ) )
  660.         {
  661.                 $browser_number = '';
  662.         }
  663.         //$browser_number = strrpos( $browser_user_agent, $search_string );
  664.         return $browser_number;
  665. }
  666.  
  667. /*
  668. Here are some typical navigator.userAgent strings so you can see where the data comes from
  669. Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5) Gecko/20031007 Firebird/0.7
  670. Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1
  671. */
  672. ?>