<?php
$table_prefix = $GLOBALS['_MAX']['CONF']['table']['prefix'];

$_query = OA_Dal_Delivery_query("select * from {$table_prefix}terawurfl") ;

$_tera_path = '';


	if(OA_Dal_Delivery_numRows($_query)) {

		$_row = OA_Dal_Delivery_fetchAssoc($_query);

		$_tera_path = $_row['terawurfl_path'];

	} 


$_tera_path .= "/webservice.php";

define("M_WURFL", $_tera_path);



	global $keycode, $mobileSize, $errorM;

	$ua = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'SonyEricssonK700i/R2AC SEMC-Browser/4.0.2 Profile/MIDP-2.0 Configuration/CLDC-1.1' ;

	$all_values = getCapabilityValues(M_WURFL."?search=is_wireless_device|ux_full_desktop|is_tablet|device_os|brand_name|model_name&ua=".urlencode($ua));
	
	$common = $all_values->device;
	$all = $common->capability;
	$json            = json_encode($common);
    $capability      = json_decode($json, TRUE);
    $all_capability  = $capability['capability'];
    $mobileparamters = array();
     foreach ($all_capability as $key => $value)
    {
        foreach ($value as $paramname => $paramvalue)
        {
            if ($paramvalue['name'] == 'brand_name')
            {
                $mobileparamters['device_brand'] = $paramvalue['value'];
            }
            else if ($paramvalue['name'] == 'device_os')
            {
                $mobileparamters['device_os'] = $paramvalue['value'];
            }
            else if ($paramvalue['name'] == 'model_name')
            {
                $mobileparamters['device_model'] = $paramvalue['value'];
            }
             else if ($paramvalue['name'] == 'is_wireless_device'  && $paramvalue['value'] == "true")
            {
                $mobileparamters['device_type'] = "Phone";
            }
             else if ($paramvalue['name'] == 'ux_full_desktop'  && $paramvalue['value'] == "true")
            {
                $mobileparamters['device_type'] = "Computer";
            }
             else if ($paramvalue['name'] == 'is_tablet'  &&  $paramvalue['value'] == "true")
            {
				
                $mobileparamters['device_type'] = "Tablet";
            }
		}
	}
	
	
	if(isset($_REQUEST['device_width']) && $_REQUEST['device_width'])
	{
		$mobileparamters['device_width'] = $_REQUEST['device_width'];	
	}
	else
	{
		$mobileparamters['device_width'] = 0 ;	
	} 


	$GLOBALS['_MAX']['device']=$mobileparamters;
	





function getCapabilityValues($url){
	//echo($url);
	$rfd = fopen($url, 'r');
	//print_r($rfd);
	stream_set_blocking($rfd,true);
	stream_set_timeout($rfd, 20);  // 20-second timeout


	$data = stream_get_contents($rfd);
	$status = stream_get_meta_data($rfd);
	
	fclose($rfd);
	
	if($status['timed_out']){
			$xml = simplexml_load_file($url);
		}else{
			$xml = simplexml_load_string($data);
		}
		
return $xml;

}


function getVirtualCapabilityValues($url){
	//echo($url);die("askk");
	$rfd = fopen($url, 'r');
	//print_r($rfd);
	stream_set_blocking($rfd,true);
	stream_set_timeout($rfd, 20);  // 20-second timeout


	$data = stream_get_contents($rfd);//print_r($data);die("as..l,23,2hh");
	$status = stream_get_meta_data($rfd);
	
	fclose($rfd);
	
	if($status['timed_out']){
			$xml = simplexml_load_file($url);
		}else{
			$xml = simplexml_load_string($data);
		}
return $xml;

}
?>
