Merge branch 'csl', added support of last-modified

This commit is contained in:
printempw 2016-02-03 13:30:12 +08:00
commit fc0b7c51ed
4 changed files with 31 additions and 13 deletions

15
get.php
View File

@ -3,7 +3,7 @@
* @Author: prpr * @Author: prpr
* @Date: 2016-02-02 20:56:42 * @Date: 2016-02-02 20:56:42
* @Last Modified by: prpr * @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__); $dir = dirname(__FILE__);
@ -12,13 +12,16 @@ require "$dir/config.php";
if (isset($_GET['type']) && isset($_GET['uname'])) { if (isset($_GET['type']) && isset($_GET['uname'])) {
$user = new user($_GET['uname']); $user = new user($_GET['uname']);
if (!$user->is_registered) utils::raise(1, 'Non-existent user.'); if (!$user->is_registered) utils::raise(1, 'Non-existent user.');
if ($_GET['type'] == "skin") { $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) : null;
echo $user->getBinaryTexture('skin');
} else if ($_GET['type'] == "cape") { if ($_GET['type'] == "skin" || $_GET['type'] == "cape") {
echo $user->getBinaryTexture('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") { } else if ($_GET['type'] == "json") {
echo $user->getJsonProfile(); echo $user->getJsonProfile();
} else { } else {

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-02-02 21:59:06 * @Date: 2016-02-02 21:59:06
* @Last Modified by: prpr * @Last Modified by: prpr
* @Last Modified time: 2016-02-03 00:10:17 * @Last Modified time: 2016-02-03 12:12:16
*/ */
class database class database
@ -39,7 +39,7 @@ class database
if (!$this->connection->error) { if (!$this->connection->error) {
return $result; return $result;
} }
utils::raise(-1, "Database query error: ", $this->connection->error); utils::raise(-1, "Database query error: ".$this->connection->error);
} }
public function fetchArray($sql) { public function fetchArray($sql) {
@ -69,7 +69,7 @@ class database
} }
public function update($uname, $key, $value) { 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) { public function delete($uname) {

View File

@ -3,7 +3,7 @@
* @Author: printempw * @Author: printempw
* @Date: 2016-01-16 23:01:33 * @Date: 2016-01-16 23:01:33
* @Last Modified by: prpr * @Last Modified by: prpr
* @Last Modified time: 2016-02-02 23:41:37 * @Last Modified time: 2016-02-03 13:25:55
*/ */
class user class user
@ -73,6 +73,7 @@ class user
$filename = "./textures/".$this->getTexture($type); $filename = "./textures/".$this->getTexture($type);
if (file_exists($filename)) { if (file_exists($filename)) {
header('Content-Type: image/png'); 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)); $data = fread(fopen($filename, 'r'), filesize($filename));
return $data; return $data;
} else { } else {
@ -87,10 +88,12 @@ class user
// remove the original texture first // remove the original texture first
if ($this->getTexture('skin') != "") if ($this->getTexture('skin') != "")
utils::remove("./textures/".$this->getTexture('skin')); utils::remove("./textures/".$this->getTexture('skin'));
$this->updateLastModified();
return $this->db->update($this->uname, 'skin_hash', $hash); return $this->db->update($this->uname, 'skin_hash', $hash);
} else if ($type == "cape") { } else if ($type == "cape") {
if ($this->getTexture('cape') != "") if ($this->getTexture('cape') != "")
utils::remove("./textures/".$this->getTexture('cape')); utils::remove("./textures/".$this->getTexture('cape'));
$this->updateLastModified();
return $this->db->update($this->uname, 'cape_hash', $hash); return $this->db->update($this->uname, 'cape_hash', $hash);
} }
return false; return false;
@ -107,10 +110,9 @@ class user
public function getJsonProfile() { public function getJsonProfile() {
header('Content-type: application/json'); header('Content-type: application/json');
if ($this->is_registered) { if ($this->is_registered) {
$json['player_name'] = $this->uname;
$preference = $this->getPreference(); $preference = $this->getPreference();
$json['model_preference'] = [$preference]; $json['model'] = $preference;
$json['skins'][$preference] = $this->getTexture('skin'); $json['skin'] = $this->getTexture('skin');
$json['cape'] = $this->getTexture('cape'); $json['cape'] = $this->getTexture('cape');
} else { } else {
$json['errno'] = 1; $json['errno'] = 1;
@ -119,4 +121,17 @@ class user
return json_encode($json); 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']);
}
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 114 KiB