<?php
/**
 * Needed Functions
 *
 */

function login($key){
	$out = array();
	if ($key === 42){
		
		
		$out['login_secret_key'] = 42;
		$out['db_hostname'] = "localhost";
		$out['db_username'] = "rxpant5_admin";
		$out['db_password'] = "eventfull!!";
		$out['db_name'] = "rxpant5_eventfull";
		$out['success'] = true;
	} else {
	$out['success'] = false;
	}
	return $out;
} 
 
 function GUID()
{
    if (function_exists('com_create_guid') === true)
    {
        return trim(com_create_guid(), '{}');
    }

    return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}

function verifyDevice($deviceID, $ping){
	$login = login(42);
	//$db_hostname = $login['db_hostname'];
	//$db_username = $login['db_username'];
	//$db_password = $login['db_password'];
	//$db_name = $login['db_name'];
	$id = 0;
	try {
		$conn = new PDO('mysql:host=' . $login['db_hostname'] . ';dbname=' . $login['db_name'], $login['db_username'], $login['db_password']);
		$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		
		// check to see if the hardware id exists...
		$data = $conn->prepare('SELECT id FROM `devices` WHERE deviceID=:deviceID AND tombstone=0;');
		$data->bindParam(':deviceID', $deviceID, PDO::PARAM_STR);
		$data->execute();
		//$results = $data->fetchAll(PDO::FETCH_ASSOC);
		
		foreach($data as $row) {
		//We found a match, so
		$id = $row['id'];
		}
		
		if ($ping == true && $id>0){
			echo 'attempting ping++';
			$data = $conn->prepare('UPDATE `devices` SET pings = pings + 1 WHERE id=:id;');
			$data->bindParam(':id', $id, PDO::PARAM_STR);
			$data->execute();
		}
				
	} catch(PDOException $e) {
	    echo $e->getMessage();
}		
	return $id;
}
 
function array2xml($array, $xml = false){
    if($xml === false){
        $xml = new SimpleXMLExtended('<root/>');
    }
    foreach($array as $key => $value){
        if(is_array($value)){
            if(!is_numeric($key)){
            	array2xml($value, $xml->addChild($key));
            } else {
            	array2xml($value, $xml->addChild('item'));
            }
        }else{
            if(!is_numeric($key)){
            	$xml->addChild($key, $value);
            } else {
            	$xml->addChild('item', $value);
            }
        }
    }
    return $xml->asXML();
}