From c4339e37de7bc7575794d91eb575ae68f5a113be Mon Sep 17 00:00:00 2001 From: printempw Date: Sat, 26 Mar 2016 21:49:58 +0800 Subject: [PATCH] added inspection for occupied texture --- libraries/User.class.php | 33 ++++++++++++++------------------- libraries/Utils.class.php | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/libraries/User.class.php b/libraries/User.class.php index f6e5a41a..4557b00b 100644 --- a/libraries/User.class.php +++ b/libraries/User.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 20:38:36 + * @Last Modified time: 2016-03-26 21:41:40 */ use Database\Database; @@ -71,27 +71,22 @@ class User } public function unRegister() { - 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')); + $skin_type_map = ["steve", "alex", "cape"]; + for ($i = 0; $i <= 2; $i++) { + if ($this->getTexture($skin_type_map[$i]) != "" && !Utils::checkTextureOccupied($this->getTexture($skin_type_map[$i]))) + Utils::remove("./textures/".$this->getTexture($skin_type_map[$i])); + } return $this->db->delete($this->uname); } public function reset() { - for ($i = 1; $i <= 3; $i++) { - switch($i) { - case 1: $type = "steve"; break; - case 2: $type = "alex"; break; - case 3: $type = "cape"; break; - } - if ($this->getTexture($type) != "") - Utils::remove("./textures/".$this->getTexture($type)); - $this->db->update($this->uname, 'hash_'.$type, ''); + $skin_type_map = ["steve", "alex", "cape"]; + for ($i = 0; $i <= 2; $i++) { + if ($this->getTexture($skin_type_map[$i]) != "" && !Utils::checkTextureOccupied($this->getTexture($skin_type_map[$i]))) + Utils::remove("./textures/".$this->getTexture($skin_type_map[$i])); + $this->db->update($this->uname, 'hash_'.$skin_type_map[$i], ''); } - $this->db->update($this->uname, 'preference', 'default'); + return $this->db->update($this->uname, 'preference', 'default'); } /** @@ -149,7 +144,7 @@ class User public function setTexture($type, $file) { // Remove the original texture first - if ($this->getTexture($type) != "") + if ($this->getTexture($type) != "" && !Utils::checkTextureOccupied($this->getTexture($type))) Utils::remove("./textures/".$this->getTexture($type)); $this->updateLastModified(); $hash = Utils::upload($file); @@ -205,7 +200,7 @@ class User } public function updateLastModified() { - // http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql + // @see http://stackoverflow.com/questions/2215354/php-date-format-when-inserting-into-datetime-in-mysql return $this->db->update($this->uname, 'last_modified', date("Y-m-d H:i:s")); } diff --git a/libraries/Utils.class.php b/libraries/Utils.class.php index 5067cc51..4f630b52 100644 --- a/libraries/Utils.class.php +++ b/libraries/Utils.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-01-16 23:01:33 * @Last Modified by: printempw - * @Last Modified time: 2016-03-26 20:32:03 + * @Last Modified time: 2016-03-26 21:35:53 */ class Utils @@ -161,4 +161,23 @@ class Utils return $dest; } + /** + * Check if given texture is occupied + * + * @param string $hash + * @return bool + */ + public static function checkTextureOccupied($hash) { + $db = new Database\Database(); + if ($db->getNumRows('hash_steve', $hash) > 1) { + return true; + } elseif ($db->getNumRows('hash_alex', $hash) > 1) { + return true; + } elseif ($db->getNumRows('hash_cape', $hash) > 1) { + return true; + } + // finally if given texture is not used by anyone else + return false; + } + }