uname = utils::convertString($uname); $this->db = new database(); if ($this->db->checkRecordExist('username', $this->uname)) { $this->passwd = $this->db->select('username', $this->uname)['password']; $this->token = md5($this->uname . $this->passwd.SALT); $this->is_registered = true; if ($this->db->select('username', $this->uname)['uid'] == 1) { $this->is_admin = true; } } } public function checkPasswd($raw_passwd) { if (md5($raw_passwd) == $this->passwd) { return true; } else { return false; } } public function getToken() { return $this->token; } public function register($passwd, $ip) { if ($this->db->insert(array( "uname" => $this->uname, "passwd" => $passwd, "ip" => $ip ))) { return true; } else { return false; } } public function unRegister() { utils::remove("./textures/".$this->getTexture('skin')); utils::remove("./textures/".$this->getTexture('cape')); return utils::delete($this->uname); } 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']; } return false; } public function getBinaryTexture($type) { $filename = "./textures/".$this->getTexture($type); if (file_exists($filename)) { header('Content-Type: image/png'); $data = fread(fopen($filename, 'r'), filesize($filename)); return $data; } else { header('Content-type: application/json'); utils::raise(-1, 'Texture no longer exists.'); } } 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')); return $this->db->update($this->uname, 'skin_hash', $hash); } else if ($type == "cape") { if ($this->getTexture('cape') != "") utils::remove("./textures/".$this->getTexture('cape')); return $this->db->update($this->uname, 'cape_hash', $hash); } return false; } public function setPreference($type) { return $this->db->update($this->uname, 'preference', $type); } public function getPreference() { return $this->db->select('username', $this->uname)['preference']; } public function getJsonProfile() { header('Content-type: application/json'); if ($this->is_registered) { $json['player_name'] = $this->uname; $preference = $this->getPreference(); $json['model_preference'] = [$preference]; $json['skins'][$preference] = $this->getTexture('skin'); $json['cape'] = $this->getTexture('cape'); } else { $json['errno'] = 1; $json['msg'] = "Non-existent user."; } return json_encode($json); } }