<?php

/*
 * TODO
 *  Better logging instantiation/customization
 *  Config handling class
 *  Make it easier to set response code and output error
 *  Check user<->game permissions
 *  should login care about roles? or is that job for getUserInfo?
 */

require 'Outspark/3rdparty/Restafarian/restafarian_common.php';
require_once 'Outspark/Common/KeyMaster/KeyMaster.php';

require '../OSKSignedRestResource.inc';

// Set up logging
if (!class_exists('Log')) {
    include 'Log.php';
}
$GLOBALS['logger'] = Log::factory('file', '/tmp/osk_error.log', 'UserResource');
if (is_null($GLOBALS['logger'])) {
    exit("Unable to initiate Logging");
}
$GLOBALS['logger']->setMask(PEAR_LOG_ALL);

$GLOBALS['conf'] = parse_ini_file('/var/www/conf/restconf.ini', true);

class InstallerResource extends OSKRestResource
{
    public function dispatch ($req)
    {
        $next = $this;
        $segment = $this->getNextRawSegment();

        switch ($segment) {
            default:
                break;
        }

        return $next;
    }

    public function GET ($req, $resp)
    {
        /*
         * Anything not handled under /user is an error.
         */
        $resp->setStatusCode(REST_STATUS_BAD_REQUEST);
        $resp->makeError($this, $resp->getStatusCode(), "Bad Request");
    }
}

class CaptureMetricsResource extends OSKRestResource {

    public $event = '';
    private $dbh;
    public function __construct($event){
	parent::__construct();
	$this->event = $event;
    }

    public function GET ($req, $resp)
    {
        $conf = $GLOBALS['conf'];
	$dbpass = OSKKeyMaster::getInstance()->getKey('outspark.rest.osk_rest_rw');
                try {
                        $this->dbh = new PDO("mysql:host=$conf[write_dbhost];dbname=$conf[metrics_dbname]",$conf['write_dbuser'], $dbpass);
                        $this->dbh->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
                        $this->dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,
true);
                } catch (PDOException $e) {
                        $GLOBALS['logger']->crit($e->getMessage());
                        throw new Exception($e->getMessage());
                }
        

        $params = $req->getUri()->getQueryParams();

$result = json_encode($params->values);

        $user = $params->getFirst('user');
        $pass = $params->getFirst('password');
        $realm = $params->getFirst('realm');
        $version = $params->getFirst('version');
        $output = $params->getFirst('output');

	$query = "INSERT INTO data set
			game=:game, 
			version=:version,
			windowsVersion=:windowsVersion,
			servicePack=:servicePack,
			physicalMemory=:physhicalMemory,
			freeCDriveSpace=:freeCDriveSpace,
			DOTNET20VERSION=:DOTNET20VERSION,
			DOTNET30VERSION=:DOTNET30VERSION,
			DOTNET35VERSION=:DOTNET35VERSION,
			installed=:installed,
			time=NOW(),
			ip=:ip,
			hash='-',
			event=:event
	";
	
	$replacements = array();
	$replacements[':game']=$params->getFirst('game');
	$replacements[':version']=$params->getFirst('version');
	$replacements[':windowsVersion']=$params->getFirst('windowsVersion');
	$replacements[':servicePack']=$params->getFirst('servicePack');
	$replacements[':physhicalMemory']=$params->getFirst('physhicalMemory');
	$replacements[':freeCDriveSpace']=$params->getFirst('freeCDriveSpace');
	$replacements[':DOTNET20VERSION']=$params->getFirst('DOTNET20VERSION');
	$replacements[':DOTNET30VERSION']=$params->getFirst('DOTNET30VERSION');
	$replacements[':DOTNET35VERSION']=$params->getFirst('DOTNET35VERSION');
	$replacements[':installed']=$params->getFirst('installed');
	$replacements[':ip']=$_SERVER['REMOTE_ADDR'];
	$replacements[':event']=$this->event;
 	
	try{
		$stmt = $this->dbh->prepare($query);
		$stmt->execute($replacements);
	}catch(Exception $e){
		error_log($e->getMessage());
	}	
        $resp->setStatusCode(REST_STATUS_OK);
    }

}

?>
