Skip to content

Commit e535379

Browse files
committed
add gitignore and jsonblob script
1 parent 72661dc commit e535379

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config.php
2+
settings.php

jsonblob.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
require_once("settings.php");
4+
5+
// This stops CORS issues
6+
header("access-control-allow-origin: *");
7+
header('Content-type: application/json');
8+
9+
$METHOD = $_SERVER['REQUEST_METHOD'];
10+
$REQUEST_BODY = file_get_contents("php://input");
11+
12+
function generateRandomString($length = 10) {
13+
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
14+
}
15+
16+
if ($METHOD=='GET') {
17+
if (empty($_GET['id'])) {
18+
http_response_code(400);
19+
print "Blob ID cannot be empty";
20+
die;
21+
}
22+
$id = $db->real_escape_string($_GET['id']);
23+
$result = $db->query("SELECT data FROM blobs WHERE id='$id'");
24+
if ($result->num_rows==0) {
25+
http_response_code(404);
26+
print "Not a valid blob ID";
27+
die;
28+
}
29+
http_response_code(200);
30+
print $result->fetch_row()[0];
31+
} else if ($METHOD=='POST') {
32+
$id = generateRandomString();
33+
$data = $db->real_escape_string($REQUEST_BODY);
34+
$sql = "INSERT INTO blobs SET id='$id', data='$data'";
35+
$db->query($sql);
36+
if (!empty($db->error)) {
37+
http_response_code(400);
38+
print $db->error;
39+
die;
40+
}
41+
http_response_code(201);
42+
$location = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . "?id=$id";
43+
header("Location: $location");
44+
} else if ($METHOD=='PUT') {
45+
if (empty($_GET['id'])) {
46+
http_response_code(400);
47+
print "Blob ID cannot be empty";
48+
die;
49+
}
50+
$id = $db->real_escape_string($_GET['id']);
51+
$data = $db->real_escape_string($REQUEST_BODY);
52+
$sql = "REPLACE INTO blobs SET id='$id', data='$data'";
53+
$db->query($sql);
54+
if (!empty($db->error)) {
55+
http_response_code(400);
56+
print $db->error;
57+
die;
58+
}
59+
http_response_code(201);
60+
$location = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
61+
header("Location: $location");
62+
} else if ($METHOD=='DELETE') {
63+
if (empty($_GET['id'])) {
64+
http_response_code(400);
65+
print "Blob ID cannot be empty";
66+
die;
67+
}
68+
$id = $db->real_escape_string($_GET['id']);
69+
$db->query("DELETE FROM blobs WHERE id='$id'");
70+
if ($db->affected_rows==0) {
71+
http_response_code(404);
72+
}
73+
} else {
74+
http_response_code(405);
75+
echo "METHOD NOT ALLOWED";
76+
}
77+
78+
?>

0 commit comments

Comments
 (0)