2016-01-17 00:15:26 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @Author: printempw
|
|
|
|
* @Date: 2016-01-16 23:01:33
|
|
|
|
* @Last Modified by: prpr
|
2016-02-03 21:15:29 +08:00
|
|
|
* @Last Modified time: 2016-02-03 21:10:24
|
2016-02-03 18:13:20 +08:00
|
|
|
*
|
|
|
|
* - login, register, logout
|
|
|
|
* - upload, change, delete
|
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-02-03 18:13:20 +08:00
|
|
|
header('Content-type: application/json');
|
|
|
|
|
2016-02-02 21:52:53 +08:00
|
|
|
$dir = dirname(__FILE__);
|
|
|
|
require "$dir/includes/autoload.inc.php";
|
2016-02-02 23:53:08 +08:00
|
|
|
require "$dir/config.php";
|
|
|
|
database::checkConfig();
|
2016-01-17 00:15:26 +08:00
|
|
|
|
2016-02-03 18:13:20 +08:00
|
|
|
session_start();
|
|
|
|
|
2016-02-02 21:52:53 +08:00
|
|
|
if (isset($_POST['uname'])) {
|
2016-02-02 23:53:08 +08:00
|
|
|
$user = new user($_POST['uname']);
|
2016-01-17 15:15:56 +08:00
|
|
|
} else {
|
|
|
|
utils::raise('1', 'Empty username.');
|
|
|
|
}
|
2016-02-03 18:13:20 +08:00
|
|
|
$action = isset($_GET['action']) ? $_GET['action'] : null;
|
2016-01-17 00:15:26 +08:00
|
|
|
$json = null;
|
|
|
|
|
2016-02-03 18:13:20 +08:00
|
|
|
/**
|
|
|
|
* Handle requests from index.php
|
|
|
|
* @var [type]
|
|
|
|
*/
|
2016-01-17 00:15:26 +08:00
|
|
|
if ($action == "login") {
|
2016-02-03 18:13:20 +08:00
|
|
|
if (checkPost()) {
|
2016-02-02 21:52:53 +08:00
|
|
|
if (!$user->is_registered) {
|
2016-01-17 00:15:26 +08:00
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Non-existent user.";
|
|
|
|
} else {
|
2016-02-02 21:52:53 +08:00
|
|
|
if ($user->checkPasswd($_POST['passwd'])) {
|
2016-01-17 00:15:26 +08:00
|
|
|
$json['errno'] = 0;
|
|
|
|
$json['msg'] = 'Logging in succeed!';
|
2016-02-02 21:52:53 +08:00
|
|
|
$json['token'] = $user->getToken();
|
|
|
|
$_SESSION['token'] = $user->getToken();
|
2016-01-17 00:15:26 +08:00
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Incorrect usename or password.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 15:15:56 +08:00
|
|
|
} else if ($action == "register") {
|
2016-02-03 21:15:29 +08:00
|
|
|
if (checkPost('register')) {
|
2016-02-02 21:52:53 +08:00
|
|
|
if (!$user->is_registered) {
|
2016-02-03 21:15:29 +08:00
|
|
|
if (user::checkValidPwd($_POST['passwd'])) {
|
|
|
|
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'];
|
|
|
|
}
|
|
|
|
// If amout of registered accounts of IP is more than allowed mounts,
|
|
|
|
// then reject the registration.
|
|
|
|
if ($user->db->getNumRows('ip', $ip) < REGS_PER_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.";
|
|
|
|
}
|
2016-01-17 10:53:10 +08:00
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
2016-02-03 21:15:29 +08:00
|
|
|
$json['msg'] = "You can't create more than ".REGS_PER_IP." accounts with this IP.";
|
2016-01-17 10:53:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
2016-02-03 18:13:20 +08:00
|
|
|
$json['msg'] = "User already registered.";
|
2016-01-17 10:53:10 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-03 18:13:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkPost() {
|
|
|
|
global $json;
|
|
|
|
if (!isset($_POST['passwd'])) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Empty password!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request from user/index.php
|
|
|
|
*/
|
|
|
|
if ($action == "upload") {
|
2016-02-03 10:27:15 +08:00
|
|
|
if (utils::getValue('token', $_SESSION) == $user->getToken()) {
|
2016-01-17 12:14:19 +08:00
|
|
|
if (checkFile()) {
|
2016-02-02 21:52:53 +08:00
|
|
|
if ($file = utils::getValue('skin_file', $_FILES)) {
|
|
|
|
if ($user->setTexture('skin', $file)) {
|
2016-02-03 10:32:48 +08:00
|
|
|
$json['skin']['errno'] = 0;
|
|
|
|
$json['skin']['msg'] = "Skin uploaded successfully.";
|
2016-01-17 10:53:10 +08:00
|
|
|
} else {
|
2016-02-03 10:32:48 +08:00
|
|
|
$json['skin']['errno'] = 1;
|
|
|
|
$json['skin']['msg'] = "Uncaught error.";
|
2016-01-17 10:53:10 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-02 21:52:53 +08:00
|
|
|
if ($file = utils::getValue('cape_file', $_FILES)) {
|
|
|
|
if ($user->setTexture('cape', $file)) {
|
2016-02-03 10:32:48 +08:00
|
|
|
$json['cape']['errno'] = 0;
|
|
|
|
$json['cape']['msg'] = "Cape uploaded successfully.";
|
2016-01-17 10:53:10 +08:00
|
|
|
} else {
|
2016-02-03 10:32:48 +08:00
|
|
|
$json['cape']['errno'] = 1;
|
|
|
|
$json['cape']['msg'] = "Uncaught error.";
|
2016-01-17 10:53:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Invalid token.";
|
|
|
|
}
|
2016-01-17 15:15:56 +08:00
|
|
|
} else if ($action == "logout") {
|
2016-02-02 21:52:53 +08:00
|
|
|
if (utils::getValue('token', $_SESSION)) {
|
2016-01-17 15:15:56 +08:00
|
|
|
session_destroy();
|
|
|
|
$json['errno'] = 0;
|
|
|
|
$json['msg'] = 'Session destroyed.';
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = 'No available session.';
|
|
|
|
}
|
2016-01-17 00:15:26 +08:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:14:19 +08:00
|
|
|
function checkFile() {
|
|
|
|
global $json;
|
|
|
|
|
2016-02-02 21:52:53 +08:00
|
|
|
if (!(utils::getValue('skin_file', $_FILES) || utils::getValue('cape_file', $_FILES))) {
|
2016-01-17 12:14:19 +08:00
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "No input file selected.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check for skin_file
|
|
|
|
*/
|
2016-02-02 21:52:53 +08:00
|
|
|
if ((utils::getValue('skin_file', $_FILES)["type"] == "image/png") || (utils::getValue('skin_file', $_FILES)["type"] == "image/x-png")) {
|
2016-01-17 12:14:19 +08:00
|
|
|
// if error occured while uploading file
|
2016-02-02 21:52:53 +08:00
|
|
|
if (utils::getValue('skin_file', $_FILES)["error"] > 0) {
|
2016-01-17 12:20:09 +08:00
|
|
|
$json['errno'] = 1;
|
2016-02-02 21:52:53 +08:00
|
|
|
$json['msg'] = utils::getValue('skin_file', $_FILES)["error"];
|
2016-01-17 12:14:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-02 21:52:53 +08:00
|
|
|
if (utils::getValue('skin_file', $_FILES)) {
|
2016-01-17 15:15:56 +08:00
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = 'Skin file type error.';
|
|
|
|
return false;
|
2016-01-17 15:56:36 +08:00
|
|
|
} else {
|
2016-02-03 10:32:48 +08:00
|
|
|
$json['skin']['errno'] = 0;
|
|
|
|
$json['skin']['msg'] = 'No skin file selected.';
|
2016-01-17 15:15:56 +08:00
|
|
|
}
|
2016-01-17 12:14:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for cape_file
|
|
|
|
*/
|
2016-02-02 21:52:53 +08:00
|
|
|
if ((utils::getValue('cape_file', $_FILES)["type"] == "image/png") || (utils::getValue('cape_file', $_FILES)["type"] == "image/x-png")) {
|
2016-01-17 12:14:19 +08:00
|
|
|
// if error occured while uploading file
|
2016-02-02 21:52:53 +08:00
|
|
|
if (utils::getValue('cape_file', $_FILES)["error"] > 0) {
|
2016-01-17 12:20:09 +08:00
|
|
|
$json['errno'] = 1;
|
2016-02-02 21:52:53 +08:00
|
|
|
$json['msg'] = utils::getValue('cape_file', $_FILES)["error"];
|
2016-01-17 12:14:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-02 21:52:53 +08:00
|
|
|
if (utils::getValue('cape_file', $_FILES)) {
|
2016-01-17 15:15:56 +08:00
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = 'Cape file type error.';
|
|
|
|
return false;
|
2016-01-17 15:56:36 +08:00
|
|
|
} else {
|
2016-02-03 10:32:48 +08:00
|
|
|
$json['cape']['errno'] = 0;
|
|
|
|
$json['cape']['msg'] = 'No cape file selected.';
|
2016-01-17 15:15:56 +08:00
|
|
|
}
|
2016-01-17 12:14:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:13:20 +08:00
|
|
|
/**
|
|
|
|
* Handle requests from user/profile.php
|
|
|
|
*/
|
|
|
|
if ($action == "change") {
|
|
|
|
if (checkPost()) {
|
|
|
|
if (isset($_POST['new_passwd'])) {
|
|
|
|
if ($user->checkPasswd($_POST['passwd'])) {
|
|
|
|
$user->changePasswd($_POST['new_passwd']);
|
|
|
|
$json['errno'] = 0;
|
|
|
|
$json['msg'] = "Password updated successfully.";
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Incorrect usename or password.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "New password required.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ($action == "delete") {
|
|
|
|
if (isset($_SESSION['token']) && $_SESSION['token'] == $user->getToken()) {
|
|
|
|
if (checkPost()) {
|
2016-02-03 20:28:02 +08:00
|
|
|
if ($user->checkPasswd($_POST['passwd'])) {
|
|
|
|
session_destroy();
|
|
|
|
$user->unRegister();
|
|
|
|
$json['errno'] = 0;
|
|
|
|
$json['msg'] = "Account successfully deleted.";
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Incorrect password.";
|
|
|
|
}
|
2016-02-03 18:13:20 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Invalid token.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$action) {
|
|
|
|
$json['errno'] = 1;
|
|
|
|
$json['msg'] = "Invalid parameters.";
|
|
|
|
}
|
|
|
|
|
2016-01-17 00:15:26 +08:00
|
|
|
echo json_encode($json);
|