Skip to content

Instantly share code, notes, and snippets.

@Matthww
Created May 3, 2017 13:36
Show Gist options
  • Select an option

  • Save Matthww/69f4eb159bafc81bf8ece58c4d3ccb43 to your computer and use it in GitHub Desktop.

Select an option

Save Matthww/69f4eb159bafc81bf8ece58c4d3ccb43 to your computer and use it in GitHub Desktop.
EPICMC API
<?php
header("Content-type: application/json;charset=utf-8");
$tokens = array(
"670f9d349c7e8fdb5e75e04ad23ad45c"
);
$reservedUsernames = array(
"admin",
"administrator",
"moderator",
"mod",
"vip",
"verified",
"partner",
"yt",
"youtube",
"epicmc",
"epic mc",
"epicmcgames",
"epicmc games",
"epicmcus",
"epicmc us",
"epicmclink",
"epicmc link",
"redstone",
"redst one",
"d",
"de",
"dmcpe",
"daltonedwards",
"daltonedwards97",
"dalton edwards",
"daltontastic",
"daltontasticmc",
"imagicalgamer",
"pewdiepie"
);
function is_positive_integer($str) {
return (is_numeric($str) && $str > 0 && $str == round($str));
}
?>
<?php
require_once("../wp-load.php");
require_once("_header.php");
$token = $_POST["token"];
$username = sanitize_user($_POST["username"]);
$action = strtolower($_POST["action"]);
$actions = array(
"add",
"subtract"
);
$amount = $_GET["amount"];
if (empty($token)) {
$error[] = "empty token";
}
if (!in_array($token, $tokens) && !empty($token)) {
$error[] = "invalid token";
}
if (!empty($token) && in_array($token, $tokens)) {
if (empty($username)) {
$error[] = "empty username";
}
if (!username_exists($username) && !empty($username)) {
$error[] = "invalid username";
}
if (username_exists($username) && !empty($username)) {
$currentUser = get_user_by("login", $username);
$balance = get_user_meta($currentUser->ID, "coins", true);
if (empty($balance)) {
update_user_meta($currentUser->ID, "coins", 0);
$balance = get_user_meta($currentUser->ID, "coins", true);
}
}
if (empty($action)) {
$error[] = "empty action";
}
if (!in_array($action, $actions) && !empty($action)) {
$error[] = "invalid action";
}
if (empty($amount)) {
$error[] = "empty amount";
}
if (!is_positive_integer($amount) && !empty($amount)) {
$error[] = "invalid amount";
}
if (count($error) === 0) {
if ($action === "subtract" && $amount > $balance) {
$error[] = "insufficient coins";
} else {
if ($action === "add") {
$newBalance = $balance + $amount;
update_user_meta($currentUser->ID, "coins", $newBalance);
} else if ($action === "subtract") {
$newBalance = $balance - $amount;
update_user_meta($currentUser->ID, "coins", $newBalance);
} else {
//
}
}
}
}
if (empty($error)) {
echo json_encode(array(
"error" => false,
"coins" => get_user_meta($currentUser->ID, "coins", true)
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else if (count($error) === 1) {
echo json_encode(array(
"error" => $error[0]
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
echo json_encode(array(
"error" => $error
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
?>
<?php
require_once("../wp-load.php");
require_once("_header.php");
$username = sanitize_user($_POST["username"]);
if (empty($username)) {
$error = "empty username";
} else {
$error = false;
}
if (!empty($username)) {
if (username_exists($username) && strtolower($username) !== "epicmc") {
$registered = true;
} else {
$registered = false;
}
if (in_array(strtolower($username), $reservedUsernames)) {
$reserved = true;
} else {
$reserved = false;
}
if (strlen($username) > 15) {
$long = true;
} else {
$long = false;
}
}
if ($error) {
echo json_encode(array(
"error" => $error
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else if ($registered === true) {
$user = get_user_by("login", $username);
$sliced = array_slice($user->roles, 1, count($user->roles) - 1, true);
if (count($sliced) > 0) {
$roles = implode(",", $sliced);
} else {
$roles = null;
}
$coinsBalance = get_user_meta($user->ID, "coins", true);
if (empty($coinsBalance)) {
update_user_meta($user->ID, "coins", 0);
$coinsBalance = 0;
}
$spinsBalance = get_user_meta($user->ID, "spins", true);
if (empty($spinsBalance)) {
update_user_meta($user->ID, "spins", 0);
$spinsBalance = 0;
}
echo json_encode(array(
"error" => $error,
"registered" => $registered,
"id" => $user->ID,
"username" => $user->user_login,
"roles" => $roles,
"coins" => $coinsBalance,
"spins" => $spinsBalance
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
echo json_encode(array(
"error" => $error,
"registered" => $registered,
"reserved" => $reserved,
"long" => $long
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
?>
<?php
require_once("../wp-load.php");
require_once("_header.php");
$token = $_POST["token"];
$username = sanitize_user($_POST["username"]);
$password = trim($_POST["password"]);
if (empty($token)) {
$error[] = "empty token";
}
if (!in_array($token, $tokens) && !empty($token)) {
$error[] = "invalid token";
}
if (!empty($token) && in_array($token, $tokens)) {
if (empty($username)) {
$error[] = "empty username";
}
if (empty($password)) {
$error[] = "empty password";
}
if (count($error) === 0) {
$auth = wp_authenticate_username_password(null, $username, $password);
if (is_wp_error($auth) || strtolower($username) === "epicmc") {
$error[] = "invalid login";
}
}
}
if (empty($error)) {
$error[] = false;
}
if (count($error) === 1) {
echo json_encode(array(
"error" => $error[0]
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
echo json_encode(array(
"error" => $error
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
?>
<?php
require_once("../wp-load.php");
require_once("_header.php");
$token = $_POST["token"];
$username = sanitize_user($_POST["username"]);
$password = trim($_POST["password"]);
$email = sanitize_email($_POST["email"]);
if (empty($token)) {
$error[] = "empty token";
}
if (!in_array($token, $tokens) && !empty($token)) {
$error[] = "invalid token";
}
if (!empty($token) && in_array($token, $tokens)) {
if (empty($username)) {
$error[] = "empty username";
}
if (!empty($username)) {
if (strlen($username) > 16) {
$error[] = "long username";
}
if (in_array(strtolower($username), $reservedUsernames)) {
$error[] = "reserved username";
}
if (username_exists($username) && strtolower($username) !== "epicmc") {
$error[] = "username exists";
}
}
if (empty($password)) {
$error[] = "empty password";
}
if (empty($email)) {
$error[] = "empty email";
}
if (!empty($email)) {
if (!is_email($email)) {
$error[] = "invalid email";
}
if (email_exists($email)) {
$error[] = "email exists";
}
}
if (count($error) === 0) {
$auth = wp_create_user($username, $password, $email);
if (is_wp_error($auth)) {
$error[] = "invalid registration";
}
}
}
if (empty($error)) {
$error[] = false;
}
if (count($error) === 1) {
echo json_encode(array(
"error" => $error[0]
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
echo json_encode(array(
"error" => $error
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
?>
<?php
require_once("../wp-load.php");
require_once("_header.php");
$token = $_POST["token"];
$username = sanitize_user($_POST["username"]);
if (empty($token)) {
$error[] = "empty token";
}
if (!in_array($token, $tokens) && !empty($token)) {
$error[] = "invalid token";
}
if (!empty($token) && in_array($token, $tokens)) {
if (empty($username)) {
$error[] = "empty username";
}
if (!username_exists($username) && !empty($username)) {
$error[] = "invalid username";
}
if (username_exists($username) && !empty($username)) {
$currentUser = get_user_by("login", $username);
$balance = get_user_meta($currentUser->ID, "spins", true);
if (empty($balance)) {
update_user_meta($currentUser->ID, "spins", 0);
$balance = get_user_meta($currentUser->ID, "spins", true);
}
if (count($error) === 0 && $balance == 0) {
$error[] = "insufficient spins";
}
}
if (count($error) === 0) {
$spinsRemaining = $balance - 1;
update_user_meta($currentUser->ID, "spins", $spinsRemaining);
$error = false;
$prizes = array(
"nothing",
"coins",
"particle pack",
"spin"
);
shuffle($prizes);
$prize = $prizes[array_rand($prizes)];
if ($prize === "nothing") {
$won = false;
$message = "you won nothing";
} else if ($prize === "coins") {
$won = true;
$coins = get_user_meta($currentUser->ID, "coins", true);
$coinsPrize = mt_rand(1, 500);
$coinsTotal = $coins + $coinsPrize;
update_user_meta($currentUser->ID, "coins", $coinsTotal);
if ($coinsPrize === 1) {
$message = "you won $coinsPrize coin";
} else {
$message = "you won $coinsPrize coins";
}
} else if ($prize === "particle pack") {
$won = true;
$savedParticlePacks = get_user_meta($currentUser->ID, "particle_packs", true);
if (empty($savedParticlePacks)) {
update_user_meta($currentUser->ID, "particle_packs", "");
$savedParticlePacks = get_user_meta($currentUser->ID, "particle_packs", true);
}
$ownedParticlePacks = explode(",", $savedParticlePacks);
$particlePackPrizes = array(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20
);
shuffle($particlePackPrizes);
$particlePackPrize = $particlePackPrizes[array_rand($particlePackPrizes)];
if (in_array($particlePackPrize, $ownedParticlePacks)) {
$won = true;
$message = "you won ID:$particlePackPrize, but you already own it";
} else {
$won = true;
array_push($ownedParticlePacks, $particlePackPrize);
asort($ownedParticlePacks);
update_user_meta($currentUser->ID, "particle_packs", implode(",", array_filter($ownedParticlePacks)));
$message = "you won ID:$particlePackPrize";
}
} else if ($prize === "spin") {
$won = true;
$balance = get_user_meta($currentUser->ID, "spins", true);
$spinsTotal = $balance + 1;
update_user_meta($currentUser->ID, "spins", $spinsTotal);
$message = "you won another spin";
} else {
//
}
}
}
if ($error === false) {
echo json_encode(array(
"error" => false,
"won" => $won,
"type" => $prize,
"message" => $message,
"coins" => get_user_meta($currentUser->ID, "coins", true),
"spins" => get_user_meta($currentUser->ID, "spins", true)
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else if (count($error) === 1) {
echo json_encode(array(
"error" => $error[0]
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
echo json_encode(array(
"error" => $error
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
?>
<?php
require_once("../wp-load.php");
require_once("_header.php");
$token = $_POST["token"];
$username = sanitize_user($_POST["username"]);
$action = strtolower($_POST["action"]);
$actions = array(
"add",
"subtract"
);
$amount = $_GET["amount"];
if (empty($token)) {
$error[] = "empty token";
}
if (!in_array($token, $tokens) && !empty($token)) {
$error[] = "invalid token";
}
if (!empty($token) && in_array($token, $tokens)) {
if (empty($username)) {
$error[] = "empty username";
}
if (!username_exists($username) && !empty($username)) {
$error[] = "invalid username";
}
if (username_exists($username) && !empty($username)) {
$currentUser = get_user_by("login", $username);
$balance = get_user_meta($currentUser->ID, "spins", true);
if (empty($balance)) {
update_user_meta($currentUser->ID, "spins", 0);
$balance = get_user_meta($currentUser->ID, "spins", true);
}
}
if (empty($action)) {
$error[] = "empty action";
}
if (!in_array($action, $actions) && !empty($action)) {
$error[] = "invalid action";
}
if (empty($amount)) {
$error[] = "empty amount";
}
if (!is_positive_integer($amount) && !empty($amount)) {
$error[] = "invalid amount";
}
if (count($error) === 0) {
if ($action === "subtract" && $amount > $balance) {
$error[] = "insufficient spins";
} else {
if ($action === "add") {
$newBalance = $balance + $amount;
update_user_meta($currentUser->ID, "spins", $newBalance);
} else if ($action === "subtract") {
$newBalance = $balance - $amount;
update_user_meta($currentUser->ID, "spins", $newBalance);
} else {
//
}
}
}
}
if (empty($error)) {
echo json_encode(array(
"error" => false,
"spins" => get_user_meta($currentUser->ID, "spins", true)
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else if (count($error) === 1) {
echo json_encode(array(
"error" => $error[0]
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
} else {
echo json_encode(array(
"error" => $error
), JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment