2016-01-17 00:15:26 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @Author: printempw
|
|
|
|
* @Date: 2016-01-16 23:01:33
|
|
|
|
* @Last Modified by: prpr
|
2016-01-17 12:14:19 +08:00
|
|
|
* @Last Modified time: 2016-01-17 12:10:44
|
2016-01-17 00:15:26 +08:00
|
|
|
*
|
|
|
|
* All ajax requests will be handled here
|
|
|
|
*/
|
|
|
|
|
2016-01-17 10:53:10 +08:00
|
|
|
header('Access-Control-Allow-Origin: *');
|
2016-01-17 00:15:26 +08:00
|
|
|
session_start();
|
|
|
|
|
|
|
|
function __autoload($classname) {
|
|
|
|
$filename = "./includes/". $classname .".class.php";
|
|
|
|
include_once($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = new user($_POST['uname']);
|
|
|
|
$action = $_GET['action'];
|
|
|
|
$json = null;
|
|
|
|
|
|
|
|
if ($action == "login") {
|
2016-01-17 12:14:19 +08:00
|
|
|
if (checkInput()) {
|
2016-01-17 00:15:26 +08:00
|
|
|
if (!$user -> is_registered) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Non-existent user.";
|
|
|
|
} else {
|
|
|
|
if ($user -> checkPasswd($_POST['passwd'])) {
|
|
|
|
$json['errno'] = 0;
|
|
|
|
$json['msg'] = 'Logging in succeed!';
|
|
|
|
$json['token'] = $user -> getToken();
|
2016-01-17 10:53:10 +08:00
|
|
|
$_SESSION['token'] = $user -> getToken();
|
2016-01-17 00:15:26 +08:00
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Incorrect usename or password.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($action == "register") {
|
2016-01-17 12:14:19 +08:00
|
|
|
if (checkInput()) {
|
2016-01-17 10:53:10 +08:00
|
|
|
if (!$user -> is_registered) {
|
|
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
|
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
|
|
} else {
|
|
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
}
|
2016-01-17 00:15:26 +08:00
|
|
|
|
2016-01-17 10:53:10 +08:00
|
|
|
if (!utils::select('ip', $ip)) {
|
|
|
|
// use once md5 to encrypt password
|
|
|
|
if ($user -> register(md5($_POST['passwd']), $ip)) {
|
|
|
|
$json['errno'] = 0;
|
|
|
|
$json['msg'] = "Registered successfully.";
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Uncaught error.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "It seems that you have already register a account with this IP address.";
|
|
|
|
}
|
2016-01-17 00:15:26 +08:00
|
|
|
|
2016-01-17 10:53:10 +08:00
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "User already existed.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ($action == "upload") {
|
|
|
|
if ($_SESSION['token'] == $user -> getToken()) {
|
2016-01-17 12:14:19 +08:00
|
|
|
if (checkFile()) {
|
2016-01-17 10:53:10 +08:00
|
|
|
if ($file = $_FILES['skin_file']) {
|
|
|
|
if ($user -> setTexture('skin', $file)) {
|
|
|
|
$json[0]['errno'] = 0;
|
|
|
|
$json[0]['msg'] = "Skin uploaded successfully.";
|
|
|
|
} else {
|
|
|
|
$json[0]['errno'] = 1;
|
|
|
|
$json[0]['msg'] = "Uncaught error.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($file = $_FILES['cape_file']) {
|
|
|
|
if ($user -> setTexture('cape', $file)) {
|
|
|
|
$json[1]['errno'] = 0;
|
|
|
|
$json[1]['msg'] = "Cape uploaded successfully.";
|
|
|
|
} else {
|
|
|
|
$json[1]['errno'] = 1;
|
|
|
|
$json[1]['msg'] = "Uncaught error.";
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 12:14:19 +08:00
|
|
|
} else {
|
|
|
|
echo "shit";
|
2016-01-17 10:53:10 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Invalid token.";
|
|
|
|
}
|
2016-01-17 00:15:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:14:19 +08:00
|
|
|
function checkInput() {
|
|
|
|
global $json;
|
|
|
|
if (!$_POST['uname']) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = 'Empty username!';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!$_POST['passwd']) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Empty password!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkFile() {
|
|
|
|
global $json;
|
|
|
|
if (!$_POST['uname']) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = 'Empty username!';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($_FILES['skin_file'] || $_FILES['cape_file'])) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "No input file selected.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check for skin_file
|
|
|
|
*/
|
|
|
|
if (($_FILES["skin_file"]["type"] == "image/png") || ($_FILES["skin_file"]["type"] == "image/x-png")) {
|
|
|
|
// if error occured while uploading file
|
|
|
|
if ($_FILES["skin_file"]["error"] > 0) {
|
|
|
|
$json[0]['errno'] = 1;
|
|
|
|
$json[0]['msg'] = $_FILES["skin_file"]["error"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json[0]['errno'] = 1;
|
|
|
|
$json[0]['msg'] = 'Skin file type error.';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for cape_file
|
|
|
|
*/
|
|
|
|
if (($_FILES["cape_file"]["type"] == "image/png") || ($_FILES["cape_file"]["type"] == "image/x-png")) {
|
|
|
|
// if error occured while uploading file
|
|
|
|
if ($_FILES["cape_file"]["error"] > 0) {
|
|
|
|
$json[0]['errno'] = 1;
|
|
|
|
$json[0]['msg'] = $_FILES["cape_file"]["error"];
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json[0]['errno'] = 1;
|
|
|
|
$json[0]['msg'] = 'Cape file type error.';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-17 00:15:26 +08:00
|
|
|
echo json_encode($json);
|