Using switches and added upload
This commit is contained in:
parent
4bb747c11c
commit
639dd58d7a
125
jensmemes.php
125
jensmemes.php
|
@ -5,12 +5,15 @@ global $jmurl;
|
|||
$obj = new stdClass();
|
||||
$obj->status = 404;
|
||||
$req = $_SERVER["PATH_INFO"];
|
||||
$endpoint = explode("/", $req);
|
||||
if ($req == "" or $req == "/") {
|
||||
$method = $_SERVER['REQUEST_METHOD'];
|
||||
if ($method == "GET") {
|
||||
switch ($req) {
|
||||
case "":
|
||||
case "/":
|
||||
$obj->status = 200;
|
||||
$obj->endpoints = endpoints();
|
||||
}
|
||||
elseif ($req == "/all") {
|
||||
break;
|
||||
case "/all":
|
||||
$obj->status = 200;
|
||||
//Memes
|
||||
$q_memes = "SELECT * FROM images";
|
||||
|
@ -28,10 +31,13 @@ elseif ($req == "/all") {
|
|||
}
|
||||
mysqli_free_result($res_users);
|
||||
$obj->users = $users;
|
||||
} elseif ($req == "/base" or $req == "/baseurl") {
|
||||
break;
|
||||
case "/baseurl":
|
||||
case "/base":
|
||||
$obj->status = 200;
|
||||
$obj->baseurl = $jmurl;
|
||||
} elseif ($req == "/memes") {
|
||||
break;
|
||||
case "/memes":
|
||||
$obj->status = 200;
|
||||
$query = "SELECT * FROM images";
|
||||
if (isset($_GET["category"])) {
|
||||
|
@ -44,7 +50,8 @@ elseif ($req == "/all") {
|
|||
$query = addCondition('path LIKE "%' . $_GET["search"] . '%"', $query);
|
||||
}
|
||||
$obj->memes = memesArray($query);
|
||||
} elseif ($req == "/meme") {
|
||||
break;
|
||||
case "/meme":
|
||||
if (isset($_GET["id"])) {
|
||||
$q = 'SELECT * FROM images WHERE id=' . $_GET["id"];
|
||||
$res = mysqli_query($jmcon, $q);
|
||||
|
@ -59,7 +66,8 @@ elseif ($req == "/all") {
|
|||
$obj->category = $row["cat"];
|
||||
}
|
||||
}
|
||||
} elseif ($req == "/random") {
|
||||
break;
|
||||
case "/random":
|
||||
$query = "SELECT * FROM images";
|
||||
if (isset($_GET["category"])) {
|
||||
$query = addCondition('cat="' . $_GET["category"] . '"', $query);
|
||||
|
@ -76,10 +84,12 @@ elseif ($req == "/all") {
|
|||
$obj->category = $meme->category;
|
||||
$obj->user = $meme->user;
|
||||
}
|
||||
} elseif ($req == "/categories") {
|
||||
break;
|
||||
case "/categories":
|
||||
$obj->status = 200;
|
||||
$obj->categories = categoryArray("SELECT * FROM cats");
|
||||
} elseif ($req == "/category") {
|
||||
break;
|
||||
case "/category":
|
||||
if (isset($_GET["id"])) {
|
||||
$q = 'SELECT * FROM cats WHERE id="' . $_GET["id"] . '"';
|
||||
$res = mysqli_query($jmcon, $q);
|
||||
|
@ -91,7 +101,8 @@ elseif ($req == "/all") {
|
|||
$obj->name = $row["name"];
|
||||
}
|
||||
}
|
||||
} elseif ($req == "/users") {
|
||||
break;
|
||||
case "/users":
|
||||
$users = array();
|
||||
$q_users = "SELECT * FROM token";
|
||||
$res_users = mysqli_query($jmcon, $q_users);
|
||||
|
@ -106,22 +117,37 @@ elseif ($req == "/all") {
|
|||
mysqli_free_result($res_users);
|
||||
$obj->users = $users;
|
||||
$obj->status = 200;
|
||||
} elseif ($req == "/token/random") {
|
||||
break;
|
||||
case "/token/random":
|
||||
if (isset($_GET["user"])) {
|
||||
$obj->status = 200;
|
||||
$obj->token = genToken($_GET["user"]);
|
||||
} else {
|
||||
$obj->error = "Need to set a user with ?user";
|
||||
}
|
||||
}
|
||||
else {
|
||||
break;
|
||||
default:
|
||||
$obj->endpoints = endpoints();
|
||||
break;
|
||||
}
|
||||
} else if ($method == "POST") {
|
||||
switch ($req) {
|
||||
case "/upload":
|
||||
upload();
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
header('Content-Type: application/json');
|
||||
echo stripslashes(json_encode($obj, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
function endpoints() {
|
||||
return array("/all", "/baseurl", "/memes", "/meme", "/random", "/categories", "/category");
|
||||
return array("/all", "/baseurl", "/memes", "/meme", "/random", "/categories", "/category", "/users");
|
||||
}
|
||||
|
||||
function postendpoints() {
|
||||
return array("/admin", "/upload");
|
||||
}
|
||||
|
||||
function checksql($res) {
|
||||
|
@ -184,3 +210,72 @@ function genToken($discord) {
|
|||
$prehash = $random . md5(time()) . $discord;
|
||||
return md5($prehash);
|
||||
}
|
||||
|
||||
function upload() {
|
||||
global $jmcon;
|
||||
global $obj;
|
||||
global $jmimagepath;
|
||||
$token = $_POST["token"];
|
||||
$cat = $_POST["category"];
|
||||
$obj->token = $token;
|
||||
if (isset($token)) {
|
||||
if (isset($cat)) {
|
||||
$query = "SELECT * FROM token WHERE token='$token'";
|
||||
$res = mysqli_query($jmcon, $query);
|
||||
checksql($res);
|
||||
$row = mysqli_fetch_array($res, MYSQLI_ASSOC);
|
||||
if ($row) {
|
||||
$uploads = $row["uploadsLast24H"];
|
||||
$homedir = $row["userdir"];
|
||||
$user = $row["name"];
|
||||
$countfiles = count($_FILES['file']['name']);
|
||||
if ($countfiles == 0) {
|
||||
$obj->status = 400;
|
||||
$obj->error = "no files to upload send";
|
||||
}
|
||||
else if ($uploads + $countfiles <= 20) {
|
||||
$uploads += $countfiles;
|
||||
$sqlMaxUpl = "UPDATE token SET uploadsLast24H='$uploads' WHERE token='$token'";
|
||||
mysqli_query($jmcon, $sqlMaxUpl);
|
||||
if ($countfiles == 1) {
|
||||
$filename = $_FILES['file']['name'];
|
||||
if (isset($filename)) {
|
||||
$obj->file = $filename;
|
||||
move_uploaded_file($_FILES['file']['tmp_name'], $jmimagepath . $homedir . "/" . $filename);
|
||||
$path = "images/" . $homedir . "/" . $filename;
|
||||
$clientIP = $_SERVER['REMOTE_ADDR'];;
|
||||
$sqlType = "INSERT INTO images (user, path, cat, ip) VALUES ('$user', '$path', '$cat', '$clientIP')";
|
||||
$res = mysqli_query($jmcon, $sqlType);
|
||||
checksql($res);
|
||||
}
|
||||
} else {
|
||||
for ($i = 0; $i < $countfiles; $i++) {
|
||||
$filename = $_FILES['file']['name'][$i];
|
||||
if (isset($filename)) {
|
||||
$obj->file = $filename;
|
||||
move_uploaded_file($_FILES['file']['tmp_name'][$i], $jmimagepath . $homedir . "/" . $filename);
|
||||
$path = "images/" . $homedir . "/" . $filename;
|
||||
$clientIP = $_SERVER['REMOTE_ADDR'];;
|
||||
$sqlType = "INSERT INTO images (user, path, cat, ip) VALUES ('$user', '$path', '$cat', '$clientIP')";
|
||||
$res = mysqli_query($jmcon, $sqlType);
|
||||
checksql($res);
|
||||
}
|
||||
}
|
||||
}
|
||||
$obj->status = 201;
|
||||
} else {
|
||||
$obj->status = 403;
|
||||
$obj->error = "upload limit reached";
|
||||
}
|
||||
} else {
|
||||
$obj->status = 403;
|
||||
$obj->error = "token not existing";
|
||||
}
|
||||
} else {
|
||||
$obj->status = 400;
|
||||
$obj->error = "missing category";
|
||||
}
|
||||
} else {
|
||||
$obj->status = 401;
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue