<?php

if(empty($_GET['u'])) exit;

$u = trim($_GET['u']);

//[style] => width: 231px; height: 321px; background-image: url(http://192.168.1.34/panels/images/ntouch/white.gif); background-position: initial initial; background-repeat: initial initial;

function panelCSSFromID($id) {

	$css = "";
	$jsonText =  @file_get_contents("/var/www/htdocs/panels/projects/".$id."/project.svsi");
	
	if(strlen($jsonText) > 100) {

		$json = json_decode($jsonText, true);
		$css =  $json['panels'][0]['core']['style'];
	}
	
	return $css;
	
}

function parseWidthHeightCSS($css) {
	$widthheight = array();
	$widthheight['width'] = 0;
	$widthheight['height'] = 0;
	$remove = array("width:", "height:", " ", "px", ";");
			
	
	$widthLocate = stripos($css, "width:");
	$widthLocateEnd = stripos($css, ";", $widthLocate);
	$heightLocate = stripos($css, "height:");
	$heightLocateEnd = stripos($css, ";", $heightLocate);
	$cssWidth = substr($css,$widthLocate, $widthLocateEnd-$widthLocate);
	$cssHeight = substr($css,$heightLocate, $heightLocateEnd-$heightLocate);
	
	$widthheight['width'] = trim(str_ireplace($remove, "", $cssWidth));
	$widthheight['height'] =  trim(str_ireplace($remove, "", $cssHeight));
	
	return $widthheight;	
}

if(stripos(($_SERVER['SERVER_SOFTWARE']), "Ubuntu") > 0) {
	include_once "/var/www/htdocs/php_includes/Common.php";
	include_once "/var/www/htdocs/php_includes/Connect.php";
	include_once "/var/www/htdocs/php_includes/CoreSelect.php";
	
	header("Content-Type:text/plain");
				
	$u_escape = pg_escape_string($u);
	
	$sql = "SELECT forcepanel FROM users WHERE username = '".$u_escape."' and forcepanel <> '-1'";
	//echo $sql;exit;
	$results = CoreSelect($sql);
	//echo pg_numrows($results);exit;
	if(pg_numrows($results))
	{
		
		$panelrow = pg_fetch_assoc($results);
		if($panelrow['forcepanel'] < -1) {
			$panelrow['forcepanel'] = $panelrow['forcepanel'] * -1;
			$css = panelCSSFromID($panelrow['forcepanel']);
			if(strlen($css) > 5) {

				$wh = parseWidthHeightCSS($css);
				$width = $wh['width'];
				$height = $wh['height'];
					echo "NAME:N-Cmd\rWIDTH:$width\rHEIGHT:$height\r";
				exit;
				//echo ($json['panels'][0]['core']['style']);
				//print_r($json);
				
				
			}
			
			if(empty($mobile_user)) {
				echo "NAME:N-Cmd\r";
				exit;
			}
		}

	} else {
		
		if(empty($mobile_user)) {
			echo "NAME:N-Cmd\r";
			exit;
		}
	}
	
} else {
	
	require_once "../wallfunctions.php";
	header("Content-Type:text/plain");
	$mobile_user = getUser($u);
	if(empty($mobile_user)) {
		echo "NAME:".$xml['name']."\r";
		exit;
	}
	$css = panelCSSFromID($mobile_user['panelproject']);
	$wh = parseWidthHeightCSS($css);
	$width = $wh['width'];
	$height = $wh['height'];
	echo "NAME:".$xml['name']."\rWIDTH:$width\rHEIGHT:$height\r";
}
