Guest User

Untitled

a guest
May 29th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * TODO
  5.  *  Better logging instantiation/customization
  6.  *  Config handling class
  7.  *  Make it easier to set response code and output error
  8.  *  Check user<->game permissions
  9.  *  should login care about roles? or is that job for getUserInfo?
  10.  */
  11.  
  12. require 'Outspark/3rdparty/Restafarian/restafarian_common.php';
  13. require_once 'Outspark/Common/KeyMaster/KeyMaster.php';
  14.  
  15. require '../OSKSignedRestResource.inc';
  16.  
  17. // Set up logging
  18. if (!class_exists('Log')) {
  19.     include 'Log.php';
  20. }
  21. $GLOBALS['logger'] = Log::factory('file', '/tmp/osk_error.log', 'UserResource');
  22. if (is_null($GLOBALS['logger'])) {
  23.     exit("Unable to initiate Logging");
  24. }
  25. $GLOBALS['logger']->setMask(PEAR_LOG_ALL);
  26.  
  27. $GLOBALS['conf'] = parse_ini_file('/var/www/conf/restconf.ini', true);
  28.  
  29. class InstallerResource extends OSKRestResource
  30. {
  31.     public function dispatch ($req)
  32.     {
  33.         $next = $this;
  34.         $segment = $this->getNextRawSegment();
  35.  
  36.         switch ($segment) {
  37.             default:
  38.                 break;
  39.         }
  40.  
  41.         return $next;
  42.     }
  43.  
  44.     public function GET ($req, $resp)
  45.     {
  46.         /*
  47.          * Anything not handled under /user is an error.
  48.          */
  49.         $resp->setStatusCode(REST_STATUS_BAD_REQUEST);
  50.         $resp->makeError($this, $resp->getStatusCode(), "Bad Request");
  51.     }
  52. }
  53.  
  54. class CaptureMetricsResource extends OSKRestResource {
  55.  
  56.     public $event = '';
  57.     private $dbh;
  58.     public function __construct($event){
  59.     parent::__construct();
  60.     $this->event = $event;
  61.     }
  62.  
  63.     public function GET ($req, $resp)
  64.     {
  65.         $conf = $GLOBALS['conf'];
  66.     $dbpass = OSKKeyMaster::getInstance()->getKey('outspark.rest.osk_rest_rw');
  67.                 try {
  68.                         $this->dbh = new PDO("mysql:host=$conf[write_dbhost];dbname=$conf[metrics_dbname]",$conf['write_dbuser'], $dbpass);
  69.                         $this->dbh->setAttribute(PDO::ATTR_ERRMODE,
  70. PDO::ERRMODE_EXCEPTION);
  71.                         $this->dbh->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,
  72. true);
  73.                 } catch (PDOException $e) {
  74.                         $GLOBALS['logger']->crit($e->getMessage());
  75.                         throw new Exception($e->getMessage());
  76.                 }
  77.        
  78.  
  79.         $params = $req->getUri()->getQueryParams();
  80.  
  81. $result = json_encode($params->values);
  82.  
  83.         $user = $params->getFirst('user');
  84.         $pass = $params->getFirst('password');
  85.         $realm = $params->getFirst('realm');
  86.         $version = $params->getFirst('version');
  87.         $output = $params->getFirst('output');
  88.  
  89.     $query = "INSERT INTO data set
  90.             game=:game,
  91.             version=:version,
  92.             windowsVersion=:windowsVersion,
  93.             servicePack=:servicePack,
  94.             physicalMemory=:physhicalMemory,
  95.             freeCDriveSpace=:freeCDriveSpace,
  96.             DOTNET20VERSION=:DOTNET20VERSION,
  97.             DOTNET30VERSION=:DOTNET30VERSION,
  98.             DOTNET35VERSION=:DOTNET35VERSION,
  99.             installed=:installed,
  100.             time=NOW(),
  101.             ip=:ip,
  102.             hash='-',
  103.             event=:event
  104.     ";
  105.    
  106.     $replacements = array();
  107.     $replacements[':game']=$params->getFirst('game');
  108.     $replacements[':version']=$params->getFirst('version');
  109.     $replacements[':windowsVersion']=$params->getFirst('windowsVersion');
  110.     $replacements[':servicePack']=$params->getFirst('servicePack');
  111.     $replacements[':physhicalMemory']=$params->getFirst('physhicalMemory');
  112.     $replacements[':freeCDriveSpace']=$params->getFirst('freeCDriveSpace');
  113.     $replacements[':DOTNET20VERSION']=$params->getFirst('DOTNET20VERSION');
  114.     $replacements[':DOTNET30VERSION']=$params->getFirst('DOTNET30VERSION');
  115.     $replacements[':DOTNET35VERSION']=$params->getFirst('DOTNET35VERSION');
  116.     $replacements[':installed']=$params->getFirst('installed');
  117.     $replacements[':ip']=$_SERVER['REMOTE_ADDR'];
  118.     $replacements[':event']=$this->event;
  119.    
  120.     try{
  121.         $stmt = $this->dbh->prepare($query);
  122.         $stmt->execute($replacements);
  123.     }catch(Exception $e){
  124.         error_log($e->getMessage());
  125.     }  
  126.         $resp->setStatusCode(REST_STATUS_OK);
  127.     }
  128.  
  129. }
  130.  
  131. ?>
Add Comment
Please, Sign In to add comment