From d41f2d88aa1336cda92fb405883c2bf941aaee2c Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 11:08:45 +0800 Subject: [PATCH 1/7] better responsive design --- assets/js/user.utils.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/assets/js/user.utils.js b/assets/js/user.utils.js index d673f147..91e3e337 100644 --- a/assets/js/user.utils.js +++ b/assets/js/user.utils.js @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-01-21 13:56:40 * @Last Modified by: prpr -* @Last Modified time: 2016-02-04 23:38:39 +* @Last Modified time: 2016-02-05 11:08:02 */ 'use strict'; @@ -43,23 +43,18 @@ var handleFiles = function (files, type) { } }; -if ($(window).width() < 600) { - var canvas = MSP.get3dSkinCanvas($('#skinpreview').width(), $('#skinpreview').width()); - $("#skinpreview").append($(canvas).prop("id", "canvas3d")); -} else { - var canvas = MSP.get3dSkinCanvas(400, 400); - $("#skinpreview").append($(canvas).prop("id", "canvas3d")); -} - -$(window).resize(function(){ - if ($(document).height() <= $(window).height() || $(window).width() < 600) { +function initCanvas() { + if ($(window).width() < 800) { var canvas = MSP.get3dSkinCanvas($('#skinpreview').width(), $('#skinpreview').width()); $("#skinpreview").append($(canvas).prop("id", "canvas3d")); } else { var canvas = MSP.get3dSkinCanvas(400, 400); $("#skinpreview").append($(canvas).prop("id", "canvas3d")); } -}); +} + +initCanvas(); +$(window).resize(function(){ initCanvas(); }); $("[title='Movements']").click(function(){ MSP.setStatus("movements", !MSP.getStatus("movements")); From 68283049bc0e0ba7f0f7b926534441d1a3c68231 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 11:22:35 +0800 Subject: [PATCH 2/7] better uploading --- assets/css/user.style.css | 4 ++-- assets/js/user.utils.js | 12 +++--------- user/index.php | 4 ++-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/assets/css/user.style.css b/assets/css/user.style.css index 627c9f81..33e498b2 100644 --- a/assets/css/user.style.css +++ b/assets/css/user.style.css @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-01-21 19:12:06 * @Last Modified by: prpr -* @Last Modified time: 2016-02-04 22:57:13 +* @Last Modified time: 2016-02-05 11:16:53 */ .home-menu-blur { @@ -84,7 +84,7 @@ span { display: inline !important; } .alert { - margin: 20px 0 0; + margin: 20px 0 10px; } input[type=radio] { margin: 20px 0 0; diff --git a/assets/js/user.utils.js b/assets/js/user.utils.js index 91e3e337..1e88b1f6 100644 --- a/assets/js/user.utils.js +++ b/assets/js/user.utils.js @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-01-21 13:56:40 * @Last Modified by: prpr -* @Last Modified time: 2016-02-05 11:08:02 +* @Last Modified time: 2016-02-05 11:16:43 */ 'use strict'; @@ -71,15 +71,9 @@ $("[title='Rotation']").click(function(){ $("#upload").click(function(){ var skin_file = $("#skininput").get(0).files[0]; var cape_file = $("#capeinput").get(0).files[0]; - var form_data = new FormData(); - if (skin_file) { - form_data.append('skin_file', skin_file); - } - if (cape_file) { - form_data.append('cape_file', cape_file); - } - form_data.append('uname', docCookies.getItem('uname')); + if (skin_file) form_data.append('skin_file', skin_file); + if (cape_file) form_data.append('cape_file', cape_file); if (skin_file || cape_file) { $.ajax({ type: 'POST', diff --git a/user/index.php b/user/index.php index 7c75ac75..0f089885 100644 --- a/user/index.php +++ b/user/index.php @@ -76,7 +76,7 @@ if (isset($_SESSION['uname'])) { My skin fits on the new Alex player model.

- Preview + 2D Preview @@ -84,7 +84,7 @@ if (isset($_SESSION['uname'])) {
-
Preview +
3D Preview
From dfa70528b1ef116fe3b67a240db4fa870f777612 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 11:25:00 +0800 Subject: [PATCH 3/7] adjust style --- assets/css/index.style.css | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/css/index.style.css b/assets/css/index.style.css index 0f9cf8bc..275e3ceb 100644 --- a/assets/css/index.style.css +++ b/assets/css/index.style.css @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-01-21 20:07:47 * @Last Modified by: prpr -* @Last Modified time: 2016-02-03 22:06:53 +* @Last Modified time: 2016-02-05 11:23:43 */ .home-menu-blur { position: absolute; @@ -109,7 +109,7 @@ } #msg { width: 50%; - margin: 3.5em auto 0; + margin: 4.3em auto 0; } .alert { padding: 15px 0; From bdb6d11ced8d6afc31d47bfbd507c713c4d60d99 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 11:41:37 +0800 Subject: [PATCH 4/7] show 2d preview --- assets/js/user.utils.js | 19 ++++++++++++++----- user/index.php | 2 +- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/assets/js/user.utils.js b/assets/js/user.utils.js index 1e88b1f6..d76bb66b 100644 --- a/assets/js/user.utils.js +++ b/assets/js/user.utils.js @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-01-21 13:56:40 * @Last Modified by: prpr -* @Last Modified time: 2016-02-05 11:16:43 +* @Last Modified time: 2016-02-05 11:38:39 */ 'use strict'; @@ -17,7 +17,7 @@ $("body").on("change", "#capeinput", function(){ handleFiles(files, "cape"); }); -var handleFiles = function (files, type) { +function handleFiles(files, type) { if(files.length > 0) { var file = files[0]; if(file.type === 'image/png') { @@ -43,7 +43,7 @@ var handleFiles = function (files, type) { } }; -function initCanvas() { +function init3dCanvas() { if ($(window).width() < 800) { var canvas = MSP.get3dSkinCanvas($('#skinpreview').width(), $('#skinpreview').width()); $("#skinpreview").append($(canvas).prop("id", "canvas3d")); @@ -52,9 +52,13 @@ function initCanvas() { $("#skinpreview").append($(canvas).prop("id", "canvas3d")); } } +$(document).ready(function(){ + init3dCanvas(); +}); -initCanvas(); -$(window).resize(function(){ initCanvas(); }); +$(window).resize(function(){ + init3dCanvas(); +}); $("[title='Movements']").click(function(){ MSP.setStatus("movements", !MSP.getStatus("movements")); @@ -68,6 +72,11 @@ $("[title='Rotation']").click(function(){ MSP.setStatus("rotation", !MSP.getStatus("rotation")); }); +function show2dPreview() { + $('#canvas3d').remove(); + $("#skinpreview").html($('

Skin for Steve model:

').append($('').css('float', 'right').attr('src', '../skin/admin.png'))); +} + $("#upload").click(function(){ var skin_file = $("#skininput").get(0).files[0]; var cape_file = $("#capeinput").get(0).files[0]; diff --git a/user/index.php b/user/index.php index 0f089885..00906d2f 100644 --- a/user/index.php +++ b/user/index.php @@ -84,7 +84,7 @@ if (isset($_SESSION['uname'])) {
-
3D Preview +
Preview
From 6c8241ee8e70ba5630a6c949a7e78828428f95c3 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 12:38:49 +0800 Subject: [PATCH 5/7] adjusted data table structure --- admin/install.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/admin/install.php b/admin/install.php index 5183e50f..d361065c 100644 --- a/admin/install.php +++ b/admin/install.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 18:42:51 + * @Last Modified time: 2016-02-05 12:36:34 * * Create tables automatically */ @@ -32,8 +32,9 @@ if (!file_exists("./install.lock")) { `password` varchar(32) NOT NULL, `ip` varchar(32) NOT NULL, `preference` varchar(10) NOT NULL, - `skin_hash` varchar(64) NOT NULL, - `cape_hash` varchar(64) NOT NULL, + `hash_steve` varchar(64) NOT NULL, + `hash_alex` varchar(64) NOT NULL, + `hash_cape` varchar(64) NOT NULL, `last_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`uid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15;"; @@ -52,11 +53,11 @@ if (!file_exists("./install.lock")) { echo "
-+-----------------------------------------------------------------------------------+-----------------+
-|  uid  |  username  |  password  |  ip  |  preference  |  skin_hash  |  cape_hash  |  last_modofied  |
-+-----------------------------------------------------------------------------------+-----------------+
-|   1   |    admin   |   123456   |   *  |    default   |      *      |      *      |        *        |
-+-----------------------------------------------------------------------------------+-----------------+
++-------------------------------------------------------------------------------------------------+-----------------+
+|  uid  |  username  |  password  |  ip  |  preference  |  hash_steve |  hash_alex  |  hash_cape  |  last_modofied  |
++-------------------------------------------------------------------------------------------------+-----------------+
+|   1   |    admin   |   123456   |   *  |    default   |      *      |      *      |      *      |        *        |
++-------------------------------------------------------------------------------------------------+-----------------+
 
"; From 212c0d676791245689b5716b10135664f0983a2e Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 15:56:17 +0800 Subject: [PATCH 6/7] supported skin storage for both steve and alex model --- admin/index.php | 11 ++- ajax.php | 35 +++++-- assets/css/user.style.css | 5 +- assets/js/admin.utils.js | 16 +++- assets/js/user.utils.js | 187 +++++++++++++++++++++----------------- get.php | 9 +- includes/user.class.php | 80 +++++++++------- includes/utils.class.php | 12 ++- user/index.php | 16 +++- user/preview.php | 2 +- 10 files changed, 229 insertions(+), 144 deletions(-) diff --git a/admin/index.php b/admin/index.php index d1d0ede8..25d92e8b 100644 --- a/admin/index.php +++ b/admin/index.php @@ -3,7 +3,7 @@ * @Author: prpr * @Date: 2016-02-03 14:39:50 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 23:45:06 + * @Last Modified time: 2016-02-05 15:52:39 */ session_start(); @@ -83,13 +83,14 @@ if (isset($_SESSION['uname'])) { - '; ?> - '; ?> + '; ?> + '; ?> + '; ?> - Skin + Skin Cape - Model + Model () diff --git a/ajax.php b/ajax.php index b1da2cea..bcc4f497 100644 --- a/ajax.php +++ b/ajax.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 18:48:30 + * @Last Modified time: 2016-02-05 15:35:31 * * - login, register, logout * - upload, change, delete @@ -20,7 +20,12 @@ require "$dir/includes/autoload.inc.php"; database::checkConfig(); if (isset($_POST['uname'])) { - $user = new user($_POST['uname']); + $uname = $_POST['uname']; + if (user::checkValidUname($uname)) { + $user = new user($_POST['uname']); + } else { + utils::raise(1, 'Invalid username. Only letters, numbers and _ is allowed.'); + } } else { utils::raise('1', 'Empty username.'); } @@ -29,7 +34,6 @@ $json = null; /** * Handle requests from index.php - * @var [type] */ if ($action == "login") { if (checkPost()) { @@ -59,7 +63,7 @@ if ($action == "login") { } else { $ip = $_SERVER['REMOTE_ADDR']; } - // If amout of registered accounts of IP is more than allowed mounts, + // If amount 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 @@ -99,7 +103,8 @@ if ($action == "upload") { if (utils::getValue('token', $_SESSION) == $user->getToken()) { if (checkFile()) { if ($file = utils::getValue('skin_file', $_FILES)) { - if ($user->setTexture('skin', $file)) { + $model = (isset($_GET['model']) && $_GET['model'] == "steve") ? "steve" : "alex"; + if ($user->setTexture($model, $file)) { $json['skin']['errno'] = 0; $json['skin']['msg'] = "Skin uploaded successfully."; } else { @@ -121,14 +126,15 @@ if ($action == "upload") { $json['errno'] = 1; $json['msg'] = "Invalid token."; } -} else if ($action == "logout") { - if (utils::getValue('token', $_SESSION)) { - session_destroy(); +} else if ($action == "model") { + if (utils::getValue('token', $_SESSION) == $user->getToken()) { + $new_model = ($user->getPreference() == "default") ? "slim" : "default"; + $user->setPreference($new_model); $json['errno'] = 0; - $json['msg'] = 'Session destroyed.'; + $json['msg'] = "Preferred model successfully changed to ".$user->getPreference()."."; } else { $json['errno'] = 1; - $json['msg'] = 'No available session.'; + $json['msg'] = "Invalid token."; } } @@ -221,6 +227,15 @@ if ($action == "change") { $json['errno'] = 1; $json['msg'] = "Invalid token."; } +} else if ($action == "logout") { + if (utils::getValue('token', $_SESSION)) { + session_destroy(); + $json['errno'] = 0; + $json['msg'] = 'Session destroyed.'; + } else { + $json['errno'] = 1; + $json['msg'] = 'No available session.'; + } } if (!$action) { diff --git a/assets/css/user.style.css b/assets/css/user.style.css index 33e498b2..69b86e89 100644 --- a/assets/css/user.style.css +++ b/assets/css/user.style.css @@ -2,7 +2,7 @@ * @Author: prpr * @Date: 2016-01-21 19:12:06 * @Last Modified by: prpr -* @Last Modified time: 2016-02-05 11:16:53 +* @Last Modified time: 2016-02-05 15:13:00 */ .home-menu-blur { @@ -100,3 +100,6 @@ input[type=radio] { .container { position: initial !important; } +#skinpreview > p { + margin: 20px 0; +} diff --git a/assets/js/admin.utils.js b/assets/js/admin.utils.js index 1116f79e..386bd619 100644 --- a/assets/js/admin.utils.js +++ b/assets/js/admin.utils.js @@ -2,11 +2,23 @@ * @Author: prpr * @Date: 2016-02-04 16:48:42 * @Last Modified by: prpr -* @Last Modified time: 2016-02-04 18:27:44 +* @Last Modified time: 2016-02-05 15:52:32 */ 'use strict'; +function uploadSkin(uname) { + Ply.dialog("confirm", { + text: "Which model do you want to change?", + ok: "Steve", + cancel: "Alex" + }).done(function(){ + uploadTexture(uname, 'steve'); + }).fail(function(){ + uploadTexture(uname, 'alex'); + }); +} + function uploadTexture(uname, type) { var ply = new Ply({ el: '

Upload new '+type+':

'+ @@ -31,7 +43,7 @@ function uploadTexture(uname, type) { location.reload(); }); } else { - showAlert("Error when uploading cape:\n" + json.msg); + showAlert("Error when uploading texture:\n" + json.msg); } } }); diff --git a/assets/js/user.utils.js b/assets/js/user.utils.js index d76bb66b..fcbfebbb 100644 --- a/assets/js/user.utils.js +++ b/assets/js/user.utils.js @@ -2,115 +2,134 @@ * @Author: prpr * @Date: 2016-01-21 13:56:40 * @Last Modified by: prpr -* @Last Modified time: 2016-02-05 11:38:39 +* @Last Modified time: 2016-02-05 15:35:02 */ 'use strict'; -$("body").on("change", "#skininput", function(){ - var files = $("#skininput").prop("files"); - handleFiles(files, "skin"); +$('body').on('change', '#skininput', function(){ + var files = $('#skininput').prop('files'); + handleFiles(files, 'skin'); }); -$("body").on("change", "#capeinput", function(){ - var files = $("#capeinput").prop("files"); - handleFiles(files, "cape"); +$('body').on('change', '#capeinput', function(){ + var files = $('#capeinput').prop('files'); + handleFiles(files, 'cape'); }); +// Real-time preview function handleFiles(files, type) { - if(files.length > 0) { - var file = files[0]; - if(file.type === 'image/png') { - var fr = new FileReader(); - fr.onload = function (e) { - var img = new Image(); - img.onload = function () { - if (type == "skin") { - MSP.changeSkin(img.src); - } else { - MSP.changeCape(img.src); - } - }; - img.onerror = function () { - showMsg("alert-danger", "Error: Not an image or unknown file format"); - }; - img.src = this.result; - }; - fr.readAsDataURL(file); - } else { - showMsg("alert-danger", "Error: This is not a PNG image!"); - } - } + if(files.length > 0) { + var file = files[0]; + if(file.type === "image/png") { + var fr = new FileReader(); + fr.onload = function (e) { + var img = new Image(); + img.onload = function () { + if (type == "skin") { + MSP.changeSkin(img.src); + } else { + MSP.changeCape(img.src); + } + }; + img.onerror = function () { + showMsg("alert-danger", "Error: Not an image or unknown file format"); + }; + img.src = this.result; + }; + fr.readAsDataURL(file); + } else { + showMsg("alert-danger", "Error: This is not a PNG image!"); + } + } }; function init3dCanvas() { - if ($(window).width() < 800) { - var canvas = MSP.get3dSkinCanvas($('#skinpreview').width(), $('#skinpreview').width()); - $("#skinpreview").append($(canvas).prop("id", "canvas3d")); - } else { - var canvas = MSP.get3dSkinCanvas(400, 400); - $("#skinpreview").append($(canvas).prop("id", "canvas3d")); - } + if ($(window).width() < 800) { + var canvas = MSP.get3dSkinCanvas($('#skinpreview').width(), $('#skinpreview').width()); + $("#skinpreview").append($(canvas).prop("id", "canvas3d")); + } else { + var canvas = MSP.get3dSkinCanvas(400, 400); + $("#skinpreview").append($(canvas).prop("id", "canvas3d")); + } } -$(document).ready(function(){ - init3dCanvas(); -}); - -$(window).resize(function(){ - init3dCanvas(); -}); +$(document).ready(init3dCanvas); +// Auto resize canvas to fit responsive design +$(window).resize(init3dCanvas); +// Change 3D preview status $("[title='Movements']").click(function(){ - MSP.setStatus("movements", !MSP.getStatus("movements")); + MSP.setStatus("movements", !MSP.getStatus("movements")); }); - $("[title='Running']").click(function(){ - MSP.setStatus("running", !MSP.getStatus("running")); + MSP.setStatus("running", !MSP.getStatus("running")); }); - $("[title='Rotation']").click(function(){ - MSP.setStatus("rotation", !MSP.getStatus("rotation")); + MSP.setStatus("rotation", !MSP.getStatus("rotation")); }); function show2dPreview() { - $('#canvas3d').remove(); - $("#skinpreview").html($('

Skin for Steve model:

').append($('').css('float', 'right').attr('src', '../skin/admin.png'))); + $('#canvas3d').remove(); + $("#skinpreview").html($('

Skin for Steve model:

').append($('').css('float', 'right').attr('src', '../skin/'+docCookies.getItem('uname')+'-steve.png'))); + $("#skinpreview").append($('

Skin for Alex model:

').append($('').css('float', 'right').attr('src', '../skin/'+docCookies.getItem('uname')+'-alex.png'))); + $("#skinpreview").append($('

Cape:

').append($('').css('float', 'right').attr('src', '../cape/'+docCookies.getItem('uname')+'.png'))); } $("#upload").click(function(){ - var skin_file = $("#skininput").get(0).files[0]; - var cape_file = $("#capeinput").get(0).files[0]; - var form_data = new FormData(); - if (skin_file) form_data.append('skin_file', skin_file); - if (cape_file) form_data.append('cape_file', cape_file); - if (skin_file || cape_file) { - $.ajax({ - type: 'POST', - url: '../ajax.php?action=upload', - contentType: false, - dataType: "json", - data: form_data, - processData: false, - beforeSend: function() { - showMsg("alert-info", "Uploading..."); - }, - success: function(json) { - console.log(json); - if (json.skin.errno == 0 && json.cape.errno == 0) { - showMsg("alert-success", "Successfully uploaded."); - } - if (json.skin.errno != 0) { - showMsg("alert-danger", "Error when uploading skin:\n"+json.skin.msg); - } - if (json.cape.errno != 0) { - showMsg("alert-danger", "Error when uploading cape:\n"+json.cape.msg); - } - } - }); - } else { - showMsg("alert-warning", "No input file selected"); - } + var model = $('#model-steve').prop('checked') ? "steve" : "alex"; + var skin_file = $('#skininput').get(0).files[0]; + var cape_file = $('#capeinput').get(0).files[0]; + var form_data = new FormData(); + if (skin_file) form_data.append('skin_file', skin_file); + if (cape_file) form_data.append('cape_file', cape_file); + form_data.append('uname', docCookies.getItem('uname')); + // Ajax file upload + if (skin_file || cape_file) { + $.ajax({ + type: 'POST', + url: '../ajax.php?action=upload&model='+model, + contentType: false, + dataType: "json", + data: form_data, + processData: false, + beforeSend: function() { + showMsg('alert-info', 'Uploading...'); + }, + success: function(json) { + console.log(json); + if (json.skin.errno == 0 && json.cape.errno == 0) { + showMsg('alert-success', 'Successfully uploaded.'); + } + if (json.skin.errno != 0) { + showMsg('alert-danger', 'Error when uploading skin:\n'+json.skin.msg); + } + if (json.cape.errno != 0) { + showMsg('alert-danger', 'Error when uploading cape:\n'+json.cape.msg); + } + } + }); + } else { + showMsg('alert-warning', 'No input file selected'); + } }); - +function changeModel(uname) { + showAlert("Sure to change?", function(){ + $.ajax({ + type: "POST", + url: "../ajax.php?action=model", + data: { "uname": docCookies.getItem('uname') }, + dataType: "json", + success: function(json) { + if (json.errno == 0) { + showAlert(json.msg, function(){ + location.reload(); + }); + } else { + showAlert(json.msg); + } + } + }); + }); +} diff --git a/get.php b/get.php index 38bbbad4..f2f7c4ed 100644 --- a/get.php +++ b/get.php @@ -3,7 +3,7 @@ * @Author: prpr * @Date: 2016-02-02 20:56:42 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 22:06:20 + * @Last Modified time: 2016-02-05 14:51:26 * * All textures requests of legacy link will be handle here. */ @@ -18,10 +18,15 @@ if (isset($_GET['type']) && isset($_GET['uname'])) { $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : null; if ($_GET['type'] == "skin" || $_GET['type'] == "cape") { + $model = (isset($_GET['model']) && $_GET['model'] == "steve") ? "steve" : "alex"; if ($if_modified_since >= $user->getLastModified()) { header('HTTP/1.0 304 Not Modified'); } else { - echo $user->getBinaryTexture($_GET['type']); + if ($_GET['type'] == "cape") { + echo $user->getBinaryTexture('cape'); + } else { + echo $user->getBinaryTexture($model); + } } } else if ($_GET['type'] == "json") { if (isset($_GET['api'])) { diff --git a/includes/user.class.php b/includes/user.class.php index f25bb602..a078e13d 100644 --- a/includes/user.class.php +++ b/includes/user.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 20:38:42 + * @Last Modified time: 2016-02-05 15:11:35 */ class user @@ -37,6 +37,10 @@ class user } } + public static function checkValidUname($uname) { + return preg_match("([A-Za-z0-9_\-]+)", $uname); + } + public static function checkValidPwd($passwd) { if (strlen($passwd) > 16 || strlen($passwd) < 5) { utils::raise(1, 'Illegal password. Password length should be in 5~16.'); @@ -55,32 +59,33 @@ class user } public function register($passwd, $ip) { - if ($this->db->insert(array( - "uname" => $this->uname, - "passwd" => $passwd, - "ip" => $ip - ))) - { - return true; - } else { - return false; - } + return $this->db->insert(array( + "uname" => $this->uname, + "passwd" => $passwd, + "ip" => $ip + )); } public function unRegister() { - if ($this->getTexture('skin') != "") - utils::remove("./textures/".$this->getTexture('skin')); - if ($this->getTexture('skin') != "") + if ($this->getTexture('steve') != "") + utils::remove("./textures/".$this->getTexture('steve')); + if ($this->getTexture('alex') != "") + utils::remove("./textures/".$this->getTexture('alex')); + if ($this->getTexture('cape') != "") utils::remove("./textures/".$this->getTexture('cape')); return $this->db->delete($this->uname); } + /** + * Get textures of user + * @param string $type steve|alex|cape + * @return string sha256-hash of texture file + */ public function getTexture($type) { - if ($type == "skin") { - return $this->db->select('username', $this->uname)['skin_hash']; - } else if ($type == "cape") { - return $this->db->select('username', $this->uname)['cape_hash']; - } + if ($type == "skin") + $type = ($this->getPreference() == "default") ? "steve" : "alex"; + if ($type == "steve" | $type == "alex" | $type == "cape") + return $this->db->select('username', $this->uname)['hash_'.$type]; return false; } @@ -88,9 +93,9 @@ class user $filename = "./textures/".$this->getTexture($type); if (file_exists($filename)) { header('Content-Type: image/png'); + // Cache friendly header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT'); - $data = fread(fopen($filename, 'r'), filesize($filename)); - return $data; + return utils::fread($filename); } else { utils::raise(-1, 'Texture no longer exists.'); } @@ -98,21 +103,19 @@ class user public function setTexture($type, $file) { $hash = utils::upload($file); - if ($type == "skin") { - // remove the original texture first - if ($this->getTexture('skin') != "") - utils::remove("./textures/".$this->getTexture('skin')); - $this->updateLastModified(); - return $this->db->update($this->uname, 'skin_hash', $hash); - } else if ($type == "cape") { - if ($this->getTexture('cape') != "") - utils::remove("./textures/".$this->getTexture('cape')); - $this->updateLastModified(); - return $this->db->update($this->uname, 'cape_hash', $hash); - } + // Remove the original texture first + if ($this->getTexture($type) != "") + utils::remove("./textures/".$this->getTexture($type)); + $this->updateLastModified(); + if ($type == "steve" | $type == "alex" | $type == "cape") + return $this->db->update($this->uname, 'hash_'.$type, $hash); return false; } + /** + * Set preferred model + * @param string $type, 'slim' or 'default' + */ public function setPreference($type) { return $this->db->update($this->uname, 'preference', $type); } @@ -121,9 +124,15 @@ class user return $this->db->select('username', $this->uname)['preference']; } + /** + * Get JSON profile + * @param int $api_type, which API to use, 0 for CustomSkinAPI, 1 for UniSkinAPI + * @return string, user profile in json format + */ public function getJsonProfile($api_type) { header('Content-type: application/json'); if ($this->is_registered) { + // Support both CustomSkinLoader API & UniSkinAPI if ($api_type == 0 || $api_type == 1) { $json[($api_type == 0) ? 'username' : 'player_name'] = $this->uname; $model = $this->getPreference(); @@ -132,8 +141,9 @@ class user $json['last_update'] = $this->getLastModified(); $json['model_preference'] = [$model, $sec_model]; } - $json['skins'][$model] = $this->getTexture('skin'); - $json['skins'][$sec_model] = $this->getTexture('skin'); + // Skins dict order by preference model + $json['skins'][$model] = $this->getTexture($model == "default" ? "steve" : "alex"); + $json['skins'][$sec_model] = $this->getTexture($sec_model == "default" ? "steve" : "alex"); $json['cape'] = $this->getTexture('cape'); } else { utils::raise(-1, 'Configuration error. Non-supported API_TYPE.'); diff --git a/includes/utils.class.php b/includes/utils.class.php index b416ae7f..a842508c 100644 --- a/includes/utils.class.php +++ b/includes/utils.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 23:43:07 + * @Last Modified time: 2016-02-05 13:27:43 */ class utils @@ -36,6 +36,16 @@ class utils return $hash; } + /** + * Read a file and return bin data + * + * @param string $filename + * @return resource, binary data + */ + public static function fread($filename) { + return fread(fopen($filename, 'r'), filesize($filename)); + } + /** * Remove a file * diff --git a/user/index.php b/user/index.php index 00906d2f..1cf2695e 100644 --- a/user/index.php +++ b/user/index.php @@ -71,9 +71,9 @@ if (isset($_SESSION['uname'])) {

Select a cape:


- My skin fits on the classic Steve player model. +
- My skin fits on the new Alex player model. +

2D Preview @@ -81,6 +81,12 @@ if (isset($_SESSION['uname'])) {
+
+
Change Preferred Model
+
+

Your preference model is getPreference(); ?>. Change?

+
+
@@ -104,5 +110,9 @@ if (isset($_SESSION['uname'])) { - +getTexture('alex') && ($user->getTexture('steve') == "")) {?> + + diff --git a/user/preview.php b/user/preview.php index e27af970..c6e2cd48 100644 --- a/user/preview.php +++ b/user/preview.php @@ -3,7 +3,7 @@ * @Author: prpr * @Date: 2016-02-04 19:37:21 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 22:43:15 + * @Last Modified time: 2016-02-05 15:02:52 */ ?> From c61ae2b395e370331a9ac7b9b17cfcac41071567 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 5 Feb 2016 16:08:37 +0800 Subject: [PATCH 7/7] easily customize site title --- admin/index.php | 6 +++--- config.php | 3 +++ index.php | 8 ++++---- user/index.php | 4 ++-- user/profile.php | 6 +++--- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/admin/index.php b/admin/index.php index 25d92e8b..2b62a500 100644 --- a/admin/index.php +++ b/admin/index.php @@ -3,7 +3,7 @@ * @Author: prpr * @Date: 2016-02-03 14:39:50 * @Last Modified by: prpr - * @Last Modified time: 2016-02-05 15:52:39 + * @Last Modified time: 2016-02-05 16:05:42 */ session_start(); @@ -32,7 +32,7 @@ if (isset($_SESSION['uname'])) { - Console - Blessing Skin Server + Console - <?php echo SITE_TITLE; ?> @@ -45,7 +45,7 @@ if (isset($_SESSION['uname'])) {
- Blessing Skin Server +
  • Profile diff --git a/config.php b/config.php index 847ec57e..6c4978b7 100644 --- a/config.php +++ b/config.php @@ -19,3 +19,6 @@ define('REGS_PER_IP', 2); /* Which API to use, 0 for CustomSkinLoader API, 1 for UniSkinAPI */ define('API_TYPE', 0); + +/* Letters only is recommended */ +define('SITE_TITLE', 'Blessing Skin Server'); diff --git a/index.php b/index.php index 95a20a34..e50b59a1 100755 --- a/index.php +++ b/index.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-17 13:55:20 * @Last Modified by: prpr - * @Last Modified time: 2016-02-05 00:01:22 + * @Last Modified time: 2016-02-05 16:04:20 */ session_start(); $dir = dirname(__FILE__); @@ -24,7 +24,7 @@ if (isset($_COOKIE['uname']) && isset($_COOKIE['token'])) { - Blessing Skin Server + <?php echo SITE_TITLE; ?> @@ -38,7 +38,7 @@ if (isset($_COOKIE['uname']) && isset($_COOKIE['token'])) {
    - Blessing Skin Server +
    • @@ -58,7 +58,7 @@ if (isset($_COOKIE['uname']) && isset($_COOKIE['token'])) {
      -

      Blessing Skin Server

      +

      Just a simple open-source Minecraft skin server

      diff --git a/user/index.php b/user/index.php index 1cf2695e..3dffdd19 100644 --- a/user/index.php +++ b/user/index.php @@ -30,7 +30,7 @@ if (isset($_SESSION['uname'])) { - Upload - Blessing Skin Server + Upload - <?php echo SITE_TITLE; ?> @@ -42,7 +42,7 @@ if (isset($_SESSION['uname'])) {
      - Blessing Skin Server +
      • Profile diff --git a/user/profile.php b/user/profile.php index be6a21cb..0874a17f 100644 --- a/user/profile.php +++ b/user/profile.php @@ -3,7 +3,7 @@ * @Author: prpr * @Date: 2016-02-03 16:12:45 * @Last Modified by: prpr - * @Last Modified time: 2016-02-04 23:11:11 + * @Last Modified time: 2016-02-05 16:05:18 */ session_start(); @@ -29,7 +29,7 @@ if (isset($_SESSION['uname'])) { - Profile - Blessing Skin Server + Profile - <?php echo SITE_TITLE; ?> @@ -40,7 +40,7 @@ if (isset($_SESSION['uname'])) {