Skip to content

Commit

Permalink
Added composer
Browse files Browse the repository at this point in the history
Optimized MCPremium
Added MCPremiumResponse
  • Loading branch information
Lukasss93 committed Sep 29, 2018
1 parent b0d6240 commit 03f7043
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 106 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
vendor
/composer.lock
106 changes: 0 additions & 106 deletions MCPremium.php

This file was deleted.

22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "lukasss93/minecraft-premium-account-checker",
"description": "PHP library to check if a Minecraft account is Premium",
"license": "MIT",
"type": "project",
"authors": [
{
"name": "Luca Patera",
"email": "[email protected]",
"homepage": "https://www.lucapatera.it/"
}
],
"require": {
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.3"
},
"autoload": {
"psr-4": {
"MCPremium\\": "src/"
}
}
}
104 changes: 104 additions & 0 deletions src/MCPremium.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace MCPremium;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Psr7\Response;

/**
* PHP class to check if a Minecraft account is Premium
* Class MCPremium
* @package MCPremium
*/
class MCPremium {
const game = "Minecraft";
const version = 12;
const api = "https://authserver.mojang.com/authenticate";

private function __construct() {}

/**
* Check if an account is premium
* @param $username
* @param $password
* @return \MCPremium\MCPremiumResponse
*/
public static function check($username, $password) {

//build parameters
$parameters = [
'agent' => [
'name' => self::game,
'version' => self::version
],
'username' => (string)$username,
'password' => (string)$password
];

//initialize client
$client = new Client();
$response = new Response();

//initialize output
$data = null;

try {
//send request
$response = $client->post(self::api, [
'json' => $parameters
]);
}
catch(ClientException $e) {
if($e->hasResponse()) {
$response = $e->getResponse();
}
}

//get content
$content = $response->getBody()->getContents();

//decode json content
$json = \GuzzleHttp\json_decode($content, true);

//initialize mcpremium response class
$output = new MCPremiumResponse();

//set original content to raw property
$output->raw = $json;

//get values
if(array_key_exists('errorMessage', $json)) {
$output->error = $json['errorMessage'];
}
else if(!array_key_exists('selectedProfile', $json)) {
$output->error = "Your account is not premium.";
}
else {
$selectedProfile = $json['selectedProfile'];

//username
$output->username=$username;

//correct_username
$output->correct_username = array_key_exists('name', $selectedProfile) ? $selectedProfile['name'] : null;

//uuid
$output->uuid = array_key_exists('id', $selectedProfile) ? $selectedProfile['id'] : null;

//created_at
$output->created_at= array_key_exists('createdAt', $selectedProfile) ? $selectedProfile['createdAt'] : null;

//check premium
if($output->uuid == null && $output->correct_username == null) {
$output->error = "Your account is not premium.";
$output->premium = false;
}
else {
$output->premium = true;
}
}

return $output;
}
}
43 changes: 43 additions & 0 deletions src/MCPremiumResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace MCPremium;

/**
* Response class used by MCPremium class
* Class MCPremiumResponse
* @package MCPremium
*/
class MCPremiumResponse {

/** @var bool $premium Returns true if the account is premium, otherwise false. */
public $premium = false;

/** @var string Error message. */
public $error = null;

/** @var string $username Entered username. */
public $username = null;

/** @var string Correct username if the account is migrated. */
public $correct_username = null;

/** @var string Universally unique identifier of the account. */
public $uuid = null;

/** @var int $created_at Creation timestamp account */
public $created_at=null;

/** @var array $raw Json response from API */
public $raw = null;

/**
* Returns all class properties as an array.
* @return array
*/
public function toArray() {
$properties = get_object_vars($this);
unset($properties['raw']);
return $properties;
}

}

0 comments on commit 03f7043

Please sign in to comment.