diff --git a/application/core/Files/AsynchronousFileReader.php b/application/core/Files/AsynchronousFileReader.php index 98e8f9b6..117ce600 100644 --- a/application/core/Files/AsynchronousFileReader.php +++ b/application/core/Files/AsynchronousFileReader.php @@ -55,7 +55,7 @@ class AsynchronousFileReader { * @param string $contentType * @return bool */ - public function loadFile($url, $function, $contentType = 'UTF-8') { + public function loadFile($url, $function, $contentType = 'UTF-8', $keepAlive = 0) { if (!is_callable($function)) { $this->maniaControl->log("Function is not callable"); return false; @@ -66,6 +66,12 @@ class AsynchronousFileReader { return false; } + if($keepAlive){ + $header = array("Content-Type: " . $contentType, "Keep-Alive: " . $keepAlive, "Connection: Keep-Alive"); + }else{ + $header = array("Content-Type: " . $contentType); + } + $request = new Request($url); $request->getOptions()->set(CURLOPT_TIMEOUT, 5) // @@ -73,7 +79,7 @@ class AsynchronousFileReader { ->set(CURLOPT_CRLF, true) //linux linefeed ->set(CURLOPT_ENCODING, "")//accept encoding ->set(CURLOPT_AUTOREFERER, true)//accept link reference - ->set(CURLOPT_HTTPHEADER, array("Content-Type: " . $contentType)) // + ->set(CURLOPT_HTTPHEADER, $header) // ->set(CURLOPT_USERAGENT, 'ManiaControl v' . ManiaControl::VERSION) // ->set(CURLOPT_RETURNTRANSFER, true); diff --git a/application/plugins/KarmaTest.php b/application/plugins/KarmaTest.php index 49e5a202..a8f48caa 100644 --- a/application/plugins/KarmaTest.php +++ b/application/plugins/KarmaTest.php @@ -9,6 +9,11 @@ use ManiaControl\Plugins\Plugin; * Time: 14:03 */ class KarmaTest implements Plugin { + const MX_KARMA_URL = 'http://karma.mania-exchange.com/api2/'; + const MX_KARMA_STARTSESSION = 'startSession'; + const MX_KARMA_ACTIVATESESSION = 'activateSession'; + + /** @var ManiaControl $maniaControl */ private $maniaControl = null; /** @@ -29,9 +34,64 @@ class KarmaTest implements Plugin { */ public function load(ManiaControl $maniaControl) { $this->maniaControl = $maniaControl; + return; + $this->openSession(); + } - $serverLogin = $this->maniaControl->server->login; + private function openSession() { + //$serverLogin = $this->maniaControl->server->login; + $serverLogin = ".escsiege"; + $applicationIdentifier = 'ManiaControl v' . ManiaControl::VERSION; + $testMode = 'true'; + $query = self::MX_KARMA_URL . self::MX_KARMA_STARTSESSION; + $query .= '?serverLogin=' . $serverLogin; + $query .= '&applicationIdentifier=' . urlencode($applicationIdentifier); + $query .= '&game=sm'; + $query .= '&testMode=' . $testMode; + + + $this->maniaControl->fileReader->loadFile($query, function ($data, $error) { + var_dump($error); + if (!$error) { + $data = json_decode($data); + if ($data->success) { + $this->activateSession($data->data->sessionKey, $data->data->sessionSeed); + } else { + $this->maniaControl->log("Error while authenticating on Mania-Exchange Karma"); + } + } else { + $this->maniaControl->log($error); + } + }, "application/json", 1000); + } + + private function activateSession($sessionKey, $sessionSeed) { + $hash = $this->buildActivationHash($sessionSeed, "XNTOWRNVQCKEUBBKXNJSCPYOAA"); + + $query = self::MX_KARMA_URL . self::MX_KARMA_ACTIVATESESSION; + $query .= '?sessionKey=' . urlencode($sessionKey); + $query .= '&activationHash=' . urlencode($hash); + + var_dump($query); + + $this->maniaControl->fileReader->loadFile($query, function ($data, $error) { + if (!$error) { + $data = json_decode($data); + if (!$data->activated) { + $this->maniaControl->log("Error while authenticating on Mania-Exchange Karma"); + } else { + $this->maniaControl->log("Successfully authenticated on Mania-Exchange Karma"); + } + } else { + $this->maniaControl->log($error); + } + }, "application/json", 1000); + + } + + private function buildActivationHash($sessionSeed, $mxKey) { + return hash('sha512', $mxKey . $sessionSeed); } /**