mirror of
https://github.com/bs-community/blessing-skin-server.git
synced 2025-01-30 14:09:58 +08:00
Encapsulate 2 classes to clarify code logic
This commit is contained in:
parent
51443b0a0a
commit
c3fe4cff36
@ -3,11 +3,11 @@ $(document).ready(function(){
|
||||
if (docCookies.hasItem("uname") && docCookies.hasItem("token") && $("#login-reg").html() == 'Register') {
|
||||
checkToken(docCookies.getItem("token"),function(json) {
|
||||
if (json.success == 1) {
|
||||
showMsg("alert-success", json.msg);
|
||||
window.location = "./user.php";
|
||||
} else {
|
||||
showMsg("alert-danger", json.msg);
|
||||
}
|
||||
showMsg("alert-success", json.msg);
|
||||
window.location = "./user.php";
|
||||
} else {
|
||||
showMsg("alert-danger", json.msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -15,34 +15,34 @@ $(document).ready(function(){
|
||||
$('body').css('height', document.documentElement.clientHeight);
|
||||
|
||||
function showMsg(type, msg) {
|
||||
$("#msg").removeClass().addClass("alert").addClass(type).html(msg);
|
||||
$("#msg").removeClass().addClass("alert").addClass(type).html(msg);
|
||||
}
|
||||
|
||||
$("body").on("click", "#login-reg", function(){
|
||||
if ($("#login-reg").html() == 'Register') {
|
||||
$(".login-container").fadeOut(500);
|
||||
window.setTimeout("$('#login-reg').html('Login');changeForm(1)", 500);
|
||||
$(".login-container").fadeIn(500);
|
||||
} else {
|
||||
$(".login-container").fadeOut(500);
|
||||
window.setTimeout("$('#login-reg').html('Register');changeForm(0)", 500);
|
||||
$(".login-container").fadeIn(500);
|
||||
}
|
||||
if ($("#login-reg").html() == 'Register') {
|
||||
$(".login-container").fadeOut(500);
|
||||
window.setTimeout("$('#login-reg').html('Login');changeForm(1)", 500);
|
||||
$(".login-container").fadeIn(500);
|
||||
} else {
|
||||
$(".login-container").fadeOut(500);
|
||||
window.setTimeout("$('#login-reg').html('Register');changeForm(0)", 500);
|
||||
$(".login-container").fadeIn(500);
|
||||
}
|
||||
});
|
||||
|
||||
function changeForm(code){
|
||||
$("#msg").addClass("hide");
|
||||
if (code == 1) {
|
||||
$(".login-title").html('Register');
|
||||
$("#confirm-passwd").show();
|
||||
$(".login-group").html('<button id="register" type="button" class="btn btn-default">Register</button>');
|
||||
window.history.pushState(null, null, "./index.php?action=register");
|
||||
document.title = "Register - Blessing Skin Server 0.1";
|
||||
} else {
|
||||
$(".login-title").html('Login');
|
||||
$("#confirm-passwd").hide();
|
||||
$(".login-group").html('<div class="checkbox-wrapper"><input id="keep" type="checkbox" class="checkbox"><label for="keep" class="checkbox-label"></label><span> Remember me</span></div><button id="login" type="button" class="btn btn-default">Log in</button>');
|
||||
window.history.pushState(null, null, "./index.php?action=login");
|
||||
document.title = "Login - Blessing Skin Server 0.1";
|
||||
}
|
||||
if (code == 1) {
|
||||
$(".login-title").html('Register');
|
||||
$("#confirm-passwd").show();
|
||||
$(".login-group").html('<button id="register" type="button" class="btn btn-default">Register</button>');
|
||||
window.history.pushState(null, null, "./index.php?action=register");
|
||||
document.title = "Register - Blessing Skin Server 0.1";
|
||||
} else {
|
||||
$(".login-title").html('Login');
|
||||
$("#confirm-passwd").hide();
|
||||
$(".login-group").html('<div class="checkbox-wrapper"><input id="keep" type="checkbox" class="checkbox"><label for="keep" class="checkbox-label"></label><span> Remember me</span></div><button id="login" type="button" class="btn btn-default">Log in</button>');
|
||||
window.history.pushState(null, null, "./index.php?action=login");
|
||||
document.title = "Login - Blessing Skin Server 0.1";
|
||||
}
|
||||
}
|
||||
|
63
includes/user.class.php
Normal file
63
includes/user.class.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
class user {
|
||||
private $uname = "";
|
||||
private $passwd = "";
|
||||
private $token = "";
|
||||
|
||||
public $is_registered = false;
|
||||
public $is_admin = false;
|
||||
|
||||
function __construct($uname) {
|
||||
$this -> $uname = $uname;
|
||||
if (utils::select('username', $this -> $uname)['uid'] == 1) {
|
||||
$this -> $is_admin = true;
|
||||
}
|
||||
if (utils::select('username', $this -> $uname)['password'] !== "") {
|
||||
$this -> $password = utils::select('username', $this -> $uname)['password'];
|
||||
$this -> $is_registered = true;
|
||||
$this -> $token = md5($this -> $uname.$this -> $password.SALT);
|
||||
}
|
||||
}
|
||||
|
||||
public function checkPasswd($raw_passwd) {
|
||||
if ($raw_passwd == $this -> $password) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getToken() {
|
||||
return $this -> $token;
|
||||
}
|
||||
|
||||
public function register($passwd, $ip) {
|
||||
if (utils::insert([$this -> $uname, $passwd, $ip])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function getTexture($type) {
|
||||
if ($type == "skin") {
|
||||
return utils::select('username', $this -> $uname)['skin_hash'];
|
||||
} else if ($type == "cape") {
|
||||
return utils::select('username', $this -> $uname)['cape_hash'];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setTexture($type, $file) {
|
||||
$hash = utils::upload($file);
|
||||
if ($type == "skin") {
|
||||
return utils::update($this -> $uname, 'skin_hash', $hash);
|
||||
} else if ($type == "cape") {
|
||||
return utils::update($this -> $uname, 'cape_hash', $hash);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
56
includes/utils.class.php
Normal file
56
includes/utils.class.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
require "../config.php";
|
||||
|
||||
class utils {
|
||||
private static $connection = null;
|
||||
|
||||
public static function connect() {
|
||||
if (!self::$connection) {
|
||||
if ($con = mysql_connect(DB_HOST, DB_USER, DB_PASSWD)) {
|
||||
self::$connection = $con;
|
||||
mysql_select_db(DB_NAME, self::$connection);
|
||||
} else {
|
||||
$msg = "Can not connect to mysql, check if database info correct in config.php. ".mysql_error();
|
||||
self::raise(-1, $msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// use static function to replace raising a exception
|
||||
public static function raise($errno = -1, $msg = "Error occured.") {
|
||||
$exception['errno'] = $errno;
|
||||
$exception['msg'] = $msg;
|
||||
die(json_encode($exception));
|
||||
}
|
||||
|
||||
public static function select($key, $value) {
|
||||
self::connect();
|
||||
$query = mysql_query("SELECT * FROM users WHERE '$key'='$value'", self::$connection);
|
||||
$row = mysql_fetch_array($query);
|
||||
return $row;
|
||||
}
|
||||
|
||||
// @param $array[uname, passwd, ip]
|
||||
public static function insert($array) {
|
||||
$uname = $array[0];
|
||||
$passwd = $array[1];
|
||||
$ip = $array[2];
|
||||
self::connect();
|
||||
$query = mysql_query("INSERT INTO users (username, password, ip) VALUES ('$uname', '$passwd', '$ip')", self::$connection);
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function update($uname, $key, $value) {
|
||||
self::connect();
|
||||
$query = mysql_query("UPDATE users SET $key='$value' WHERE username='$uname'", self::$connection);
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function upload($file) {
|
||||
move_uploaded_file($file["tmp_name"],"../textures/tmp.png");
|
||||
$hash = hash_file('sha256', "../textures/tmp.png");
|
||||
rename("../textures/tmp.png", $hash);
|
||||
return $hash;
|
||||
}
|
||||
}
|
||||
?>
|
Binary file not shown.
Before Width: | Height: | Size: 114 KiB |
Binary file not shown.
Before Width: | Height: | Size: 3.6 KiB |
Binary file not shown.
Before Width: | Height: | Size: 2.0 KiB |
Loading…
Reference in New Issue
Block a user