diff --git a/get.php b/get.php index 0bc3fbb1..be389294 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-02 21:33:12 + * @Last Modified time: 2016-02-03 13:26:30 */ $dir = dirname(__FILE__); @@ -12,13 +12,16 @@ require "$dir/config.php"; if (isset($_GET['type']) && isset($_GET['uname'])) { $user = new user($_GET['uname']); - if (!$user->is_registered) utils::raise(1, 'Non-existent user.'); - if ($_GET['type'] == "skin") { - echo $user->getBinaryTexture('skin'); - } else if ($_GET['type'] == "cape") { - echo $user->getBinaryTexture('cape'); + $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : null; + + if ($_GET['type'] == "skin" || $_GET['type'] == "cape") { + if ($if_modified_since >= $user->getLastModified()) { + header('HTTP/1.0 304 Not Modified'); + } else { + echo $user->getBinaryTexture($_GET['type']); + } } else if ($_GET['type'] == "json") { echo $user->getJsonProfile(); } else { diff --git a/includes/database.class.php b/includes/database.class.php index 57f0aea5..a26b7c45 100644 --- a/includes/database.class.php +++ b/includes/database.class.php @@ -3,7 +3,7 @@ * @Author: printempw * @Date: 2016-02-02 21:59:06 * @Last Modified by: prpr - * @Last Modified time: 2016-02-03 00:10:17 + * @Last Modified time: 2016-02-03 12:12:16 */ class database @@ -39,7 +39,7 @@ class database if (!$this->connection->error) { return $result; } - utils::raise(-1, "Database query error: ", $this->connection->error); + utils::raise(-1, "Database query error: ".$this->connection->error); } public function fetchArray($sql) { @@ -69,7 +69,7 @@ class database } public function update($uname, $key, $value) { - return $this->query("UPDATE users SET $key='$value' WHERE username='$uname'"); + return $this->query("UPDATE users SET `$key`='$value' WHERE username='$uname'"); } public function delete($uname) { diff --git a/includes/user.class.php b/includes/user.class.php index ecbb0277..8678d44c 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-02 23:41:37 + * @Last Modified time: 2016-02-03 13:25:55 */ class user @@ -73,6 +73,7 @@ class user $filename = "./textures/".$this->getTexture($type); if (file_exists($filename)) { header('Content-Type: image/png'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->getLastModified()).' GMT'); $data = fread(fopen($filename, 'r'), filesize($filename)); return $data; } else { @@ -87,10 +88,12 @@ class user // 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); } return false; @@ -107,10 +110,9 @@ class user 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['model'] = $preference; + $json['skin'] = $this->getTexture('skin'); $json['cape'] = $this->getTexture('cape'); } else { $json['errno'] = 1; @@ -119,4 +121,17 @@ class user return json_encode($json); } + public function updateLastModified() { + // 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")); + } + + /** + * Get last modified time + * @return timestamp + */ + public function getLastModified() { + return strtotime($this->db->select('username', $this->uname)['last_modified']); + } + } diff --git a/textures/aed8c3fc67aae4906b72fa74c27e15866c89752f0838f6b2a1c44bb4d59cec1e b/textures/aed8c3fc67aae4906b72fa74c27e15866c89752f0838f6b2a1c44bb4d59cec1e deleted file mode 100644 index ba732216..00000000 Binary files a/textures/aed8c3fc67aae4906b72fa74c27e15866c89752f0838f6b2a1c44bb4d59cec1e and /dev/null differ