#=======================================================================================
# サブルーチン User_Agent
#	プラットフォーム、ブラウザーを判別する
#	futomi's CGI Cafe http://www.futomi.com/
#	使い方
#	($platform, $platform_v, $browser, $browser_v) = &User_Agent($ENV{'HTTP_USER_AGENT'}); 
#=======================================================================================
sub User_Agent {
	my($user_agent, $remote_host) = @_;

	if($user_agent =~ /DoCoMo/i) {
		$platform = 'DoCoMo';
		@agetPart = split(/\//, $user_agent);
		$browser = 'DoCoMo';
		$browser_v = $agetPart[1];
		$platform_v = $agetPart[2];
		if($platform_v eq '') {
			$platform_v = '';
		}
	} elsif($user_agent =~ /Cuam Ver([0-9\.]+)/i) {
		$platform = 'Windows';
		$platform_v = '';
		$browser = 'Cuam';
		$browser_v = $1;
	} elsif($user_agent =~ /^JustView\/([0-9\.]+)/) {
		$platform = 'Windows';
		$platform_v = '';
		$browser = 'JustView';
		$browser_v = $1;
	} elsif($user_agent =~ /^sharp pda browser\/([0-9\.]+).*\((.+)\//) {
		$platform = 'ZAURUS';
		$platform_v = $2;
		$browser = 'sharp_pda_browser';
		$browser_v = $1;
	} elsif($user_agent =~ /^Mozilla\/.+ \(DreamPassport\/([0-9\.])/) {
		$platform = 'Dreamcast';
		$platform_v = '';
		$browser = 'DreamPassport';
		$browser_v = $1;
	} elsif($user_agent =~ /^Sonybrowser2 \(.+\/PlayStation2 .+\)/) {
		$platform = 'PlayStation2';
		$platform_v = '';
		$browser = 'Sonybrowser2';
		$browser_v = '';
	} elsif($user_agent =~ /UP\.Browser/i) {
		$browser = 'UP.Browser';
		@agentPart = split(/ /, $user_agent);
		@agentPart2 = split(/\//, $agentPart[0]);
		($browser_v, $platform_v) = split(/\-/, $agentPart2[1]);
		my(%devid_list) = (
			'SN12'=>'au,C404S',
			'ST11'=>'au,C403ST',
			'DN11'=>'au,C402DE',
			'SY12'=>'au,C401SA',
			'CA12'=>'au,C311CA',
			'TS12'=>'au,C310T',
			'HI12'=>'au,C309H',
			'MA11'=>'au,C308P',
			'MA12'=>'au,C308P',
			'KC11'=>'au,C307K',
			'SN11'=>'au,C305S',
			'SY11'=>'au,C304A',
			'CA11'=>'au,C303CA',
			'HI11'=>'au,C302H',
			'TS11'=>'au,C301T',
			'DN01'=>'au,C202DE',
			'HI01'=>'au,C201H',
			'SNI1'=>'au,705G',
			'MAI1'=>'au,704G',
			'MAI2'=>'au,704G',
			'KCI1'=>'au,703G',
			'KCI2'=>'au,702G',
			'TSI1'=>'au,701G',
			'SNC1'=>'au,D306S',
			'MAC1'=>'au,D305P',
			'MAC2'=>'au,D305P',
			'KCC2'=>'au,D303K',
			'TS11'=>'au,D302T',
			'SYC1'=>'au,D301SA',
			'KCT6'=>'TU-KA,TK05',
			'TST3'=>'TU-KA,TT03',
			'KCT5'=>'TU-KA,TK04',
			'KCT4'=>'TU-KA,TK03',
			'SYT2'=>'TU-KA,TS02',
			'MAT1'=>'TU-KA,TP01',
			'MAT2'=>'TU-KA,TP01',
			'TST1'=>'TU-KA,TT02',
			'KCT1'=>'TU-KA,TK02',
			'KCT2'=>'TU-KA,TK01',
			'SYT1'=>'TU-KA,TS01',
			'TST1'=>'TU-KA,TT01',
			'D2'=>'DoCoMo,D2',
			'P-PAT'=>'DoCoMo,P-PAT'
		);
		if($devid_list{$platform_v} eq '') {
			$platform = '';
			$platform_v = '';
		} else {
			($platform, $platform_v) = split(/,/, $devid_list{$platform_v});
		}
	} elsif($user_agent =~ /^J-PHONE\/(.+)\/(.+)/) {
		$platform = 'J-PHONE';
		$browser = 'J-PHONE';
		$browser_v = $1;
		$platform_v = $2;
	} elsif($user_agent =~ /^ASTEL\/(.+)\/(.+)\/(.+)\//) {
		$platform = 'ASTEL';
		$browser = 'ASTEL';
		$browser_v = '';
		$platform_v = substr($2, 0, 4);
	} elsif($user_agent =~ /^Mozilla\/.+ AVE-Front\/(.+) \(.+\;Product=(.+)\;.+\)/) {
		$browser = 'NetFront';
		$browser_v = $1;
		$platform = $2;
		$platform_v = '';
	} elsif($user_agent =~ /^Mozilla\/.+ Foliage-iBrowser\/([0-9\.]+) \(WinCE\)/) {
		$platform = 'Windows';
		$platform_v = 'CE';
		$browser = 'Foliage-iBrowser';
		$browser_v = $1;		
	} elsif($user_agent =~ /^Mozilla\/.+\(compatible\; MSPIE ([0-9\.]+)\; Windows CE/) {
		$platform = 'Windows';
		$platform_v = 'CE';
		$browser = 'PocketIE';
		$browser_v = $1;		
	} elsif($user_agent =~ /^Mozilla\/.+\(compatible\; MSIE .+\;.+\)/) {
		if($user_agent =~ /NetCaptor ([0-9\.]+)/) {
			$browser = 'NetCaptor';
			$browser_v = $1;
		} elsif($user_agent =~ /Opera ([0-9\.]+)/) {
			$browser = 'Opera';
			$browser_v = $1;
		} else {
			$browser = 'InternetExplorer';
			$user_agent2 = $user_agent;
			$user_agent2 =~ s/ //g;
			@buff = split(/\;/, $user_agent2);
			@version_buff = grep(/MSIE/i, @buff);
			$browser_v = $version_buff[0];
			$browser_v =~ s/MSIE//g;
			if($browser_v =~ /^([0-9]+)\.([0-9]+)/) {
        			$browser_v = "$1\.$2";
			}
		}

		if($user_agent =~ /Windows 3\.1/i) {
			$platform = 'Windows';
			$platform_v = '3.1';
		} elsif($user_agent =~ /Win32/i) {
			$platform = 'Windows';
			$platform_v = '32';
		} elsif($user_agent =~ /Windows 95/i) {
			$platform = 'Windows';
			$platform_v = '95';
		} elsif($user_agent =~ /Windows 98/i) {
			$platform = 'Windows';
			if($user_agent =~ /Win 9x 4\.90/) {
				$platform_v = 'Me';
			} else {
				$platform_v = '98';
			}
		} elsif($user_agent =~ /Windows NT 5\.1/i) {
			$platform = 'Windows';
			$platform_v = 'XP';
		} elsif($user_agent =~ /Windows NT 5\.0/i) {
			$platform = 'Windows';
			$platform_v = '2000';
		} elsif($user_agent =~ /Windows NT/i 
				&& $user_agent !~ /Windows NT 5\.0/i) {
			$platform = 'Windows';
			$platform_v = 'NT';
		} elsif($user_agent =~ /Windows CE/i) {
			$platform = 'Windows';
			$platform_v = 'CE';
		} elsif($user_agent =~ /Mac/i) {
			$platform = 'MacOS';
			if($user_agent =~ /Mac_68000/i) {
				$platform_v = '68K';
			} elsif($user_agent =~ /Mac_PowerPC/i) {
				$platform_v = 'PowerPC';
			}
		} elsif($user_agent =~ /WebTV/i) {
			$platform = 'WebTV';
			@buff2 = split(/ /, $user_agent);
			@buff3 = split(/\//, $buff2[1]);
			$platform_v = $buff3[1];
		} else {
			$platform = '';
			$platform_v = '';
		}
	} elsif($user_agent =~ /^Mozilla\/([0-9\.]+).*\(.+\; [NIU]/) {
		$browser = 'NetscapeNavigator';
		$browser_v = $1;

		if($user_agent =~ /Win95/) {
			$platform = 'Windows';
			$platform_v = '95';
		} elsif($user_agent =~ /Win 9x 4\.90/i) {
			$platform = 'Windows';
			$platform_v = 'Me';
		} elsif($user_agent =~ /Win98/i) {
			$platform = 'Windows';
			$platform_v = '98';
		} elsif($user_agent =~ /WinNT/i) {
			$platform = 'Windows';
			$platform_v = 'NT';
		} elsif($user_agent =~ /Windows NT 5\.1/i) {
			$platform = 'Windows';
			$platform_v = 'XP';
		} elsif($user_agent =~ /Windows NT 5\.0/i) {
			$platform = 'Windows';
			$platform_v = '2000';
		} elsif($user_agent =~ /Macintosh/i) {
			$platform = 'MacOS';
			if($user_agent =~ /68K/i) {
				$platform_v = '68K';
			} elsif($user_agent =~ /PPC/i) {
				$platform_v = 'PowerPC';
			}
		} elsif($user_agent =~ /SunOS/i) {
			$platform = 'SunOS';
			@buff2 = split(/\;/, $user_agent);
			@buff3 = split(/ /, $buff2[2]);
			$platform_v = $buff3[2];
		} elsif($user_agent =~ /Linux/i) {
			$platform = 'Linux';
			@buff2 = split(/ /, $user_agent);
			$flag = 0;
			for $key (@buff2) {
				if($flag) {
					$platform_v = $key;
					last;
				}
				$flag = 1 if($key =~ /^Linux$/i);
			}
		} elsif($user_agent =~ /FreeBSD/i) {
			$platform = 'FreeBSD';
			@buff2 = split(/ /, $user_agent);
			$flag = 0;
			for $key (@buff2) {
				if($flag) {
					$platform_v = $key;
					last;
				}
				$flag = 1 if($key =~ /^FreeBSD$/i);
			}
		} elsif($user_agent =~ /NetBSD/i) {
			$platform = 'NetBSD';
			$platform_v = '';
		} elsif($user_agent =~ /AIX/i) {
			$platform = 'AIX';
			$platform_v = '';
		} elsif($user_agent =~ /IRIX/i) {
			$platform = 'IRIX';
			@buff2 = split(/ /, $user_agent);
			$flag = 0;
			for $key (@buff2) {
				if($flag) {
					$platform_v = $key;
					last;
				}
				$flag = 1 if($key =~ /^IRIX$/i);
			}
		} elsif($user_agent =~ /HP-UX/i) {
			$platform = 'HP-UX';
			$platform_v = '';
		} elsif($user_agent =~ /OSF1/i) {
			$platform = 'OSF1';
			@buff2 = split(/ /, $user_agent);
			$flag = 0;
			for $key (@buff2) {
				if($flag) {
					$platform_v = $key;
					last;
				}
				$flag = 1 if($key =~ /^OSF1$/i);
			}
		} else {
			$platform = '';
			$platform_v = '';
		}
	} else {
		$platform = '';
		$platform_v = '';
		$browser = '';
		$browser_v = '';
	}

	return ($platform, $platform_v, $browser, $browser_v);

}
#=======================================================================================


return 1;
