Skip to content

Commit 0a7a4e2

Browse files
committed
Added tiny blog-like API for testing/demo
1 parent b760b52 commit 0a7a4e2

File tree

8 files changed

+296
-0
lines changed

8 files changed

+296
-0
lines changed

blog/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
password.php
2+
comments
3+
posts

blog/config.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
session_start();
3+
4+
define('POST_DIR', "posts/");
5+
define('COMMENT_DIR', "comments/");
6+
define('POSTS_PER_PAGE', 5);
7+
define('ADMINISTRATOR_FLAG', 'demo_blog_administrator');
8+
9+
$isAdministrator = isset($_SESSION[ADMINISTRATOR_FLAG]) && $_SESSION[ADMINISTRATOR_FLAG];
10+
?>

blog/index.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
require_once('config.php');
3+
4+
if ($_SERVER['REQUEST_METHOD'] == "POST" && $isAdministrator) {
5+
$postData = json_decode(file_get_contents("php://input"));
6+
if ($postData != null) {
7+
$newPostData = array(
8+
"author" => $postData->author,
9+
"title" => $postData->title,
10+
"content" => $postData->content,
11+
"date" => date(DATE_ISO8601)
12+
);
13+
$safeAuthor = rawurlencode(substr($newPostData['author'], 0, 10));
14+
$newPostFilename = POST_DIR.$newPostData['date'].$safeAuthor.".json";
15+
if (!file_put_contents($newPostFilename, json_encode($newPostData))) {
16+
die('{"error": "Error saving post"}');
17+
}
18+
}
19+
}
20+
21+
$page = 0;
22+
if (isset($_GET['page'])) {
23+
$page = (int)$_GET['page'];
24+
if ($page < 0) {
25+
$page = 0;
26+
}
27+
}
28+
29+
$jsonData = array(
30+
"title" => "The Jsonary Blog",
31+
"page" => $page
32+
);
33+
if ($page > 0) {
34+
$jsonData['prev'] = "?page=".($page - 1);
35+
}
36+
37+
$posts = array();
38+
$filenames = scandir(POST_DIR);
39+
sort($filenames);
40+
$filenames = array_reverse($filenames);
41+
42+
$remainingToSkip = POSTS_PER_PAGE*$page;
43+
$remainingToShow = POSTS_PER_PAGE;
44+
foreach ($filenames as $filename) {
45+
if ($filename[0] == "." || is_dir($filename)) {
46+
continue;
47+
}
48+
if ($remainingToSkip-- > 0) {
49+
continue;
50+
}
51+
if ($remainingToShow-- <= 0) {
52+
break;
53+
}
54+
$postData = json_decode(file_get_contents(POST_DIR.$filename));
55+
$posts[] = array(
56+
"postId" => substr($filename, 0, strlen($filename) - 5),
57+
"title" => $postData->title,
58+
"author" => $postData->author,
59+
"date" => $postData->date
60+
);
61+
}
62+
$jsonData['posts'] = $posts;
63+
64+
if (count($posts) == POSTS_PER_PAGE) {
65+
$jsonData["next"] = "?page=".($page + 1);
66+
}
67+
68+
if ($isAdministrator) {
69+
header("Content-Type: application/json; profile=schemas/index-administrator.json");
70+
} else {
71+
header("Content-Type: application/json; profile=schemas/index.json");
72+
}
73+
echo (json_encode($jsonData));
74+
?>

blog/login.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
require_once('config.php');
3+
require_once('password.php');
4+
5+
if ($_SERVER['REQUEST_METHOD'] == "POST") {
6+
$jsonData = json_decode(file_get_contents("php://input"));
7+
if ($jsonData == PASSWORD) {
8+
$_SESSION[ADMINISTRATOR_FLAG] = TRUE;
9+
header("Content-Type: application/json; profile=?schema");
10+
die('{"home": "./", "logout": "?logout"}');
11+
}
12+
}
13+
14+
if (isset($_GET["logout"])) {
15+
$_SESSION[ADMINISTRATOR_FLAG] = FALSE;
16+
}
17+
18+
if (isset($_GET['schema'])) {
19+
?>
20+
{
21+
"title": "Login page",
22+
"links": [
23+
{"rel": "login", "href": "{+login}", "method": "POST", "schema":{"type": "string"}},
24+
{"rel": "logout", "href": "{+logout}"},
25+
{"rel": "home", "href": "{+home}"}
26+
]
27+
}
28+
<?php } else {
29+
if ($isAdministrator) {
30+
header("Content-Type: application/json; profile=?schema");
31+
die('{"home": "./", "logout": "?logout"}');
32+
}
33+
header("Content-Type: application/json; profile=?schema");
34+
?>
35+
{
36+
"login": "?login"
37+
}
38+
<?php } ?>

blog/post.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
require_once('config.php');
3+
4+
$postId = $_GET['postId'];
5+
$postId = str_replace("/", "_", $postId);
6+
7+
$postData = json_decode(file_get_contents(POST_DIR.$postId.".json"));
8+
$postData->postId = $postId;
9+
10+
$commentDir = COMMENT_DIR.$postId."/";
11+
if (!file_exists($commentDir)) {
12+
mkdir($commentDir, 0777, TRUE);
13+
}
14+
15+
if ($_SERVER['REQUEST_METHOD'] == "POST") {
16+
$submittedData = json_decode(file_get_contents("php://input"));
17+
if ($submittedData != null) {
18+
$author = $submittedData->author;
19+
$message = $submittedData->message;
20+
if (!is_string($author) || !is_string($message) || strlen($author) > 25 || strlen($message) > 200 || strlen($author) == 0 || strlen($message) == 0) {
21+
die('{"error": "Invalid parameters"}');
22+
} else {
23+
$newComment = array(
24+
"author" => $author,
25+
"message" => $message,
26+
"date" => date(DATE_ISO8601)
27+
);
28+
$safeAuthor = rawurlencode(substr($author, 0, 10));
29+
$newCommentFilename = $commentDir.$newComment['date'].$safeAuthor.".json";
30+
if (!file_put_contents($newCommentFilename, json_encode($newComment))) {
31+
die('{"error": "Error saving comment"}');
32+
}
33+
}
34+
}
35+
}
36+
37+
$comments = array();
38+
$commentFilenames = scandir($commentDir);
39+
sort($commentFilenames);
40+
foreach ($commentFilenames as $filename) {
41+
if ($filename[0] == "." || is_dir($filename)) {
42+
continue;
43+
}
44+
$commentData = json_decode(file_get_contents($commentDir.$filename));
45+
$comments[] = $commentData;
46+
}
47+
$postData->comments = $comments;
48+
49+
header("Content-Type: application/json; profile=schemas/post.json");
50+
echo (json_encode($postData));
51+
?>

blog/schemas/index-administrator.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"title": "Administrator",
3+
"allOf": [{"$ref": "index.json"}],
4+
"links": [
5+
{
6+
"rel": "create",
7+
"href": "?",
8+
"method": "POST",
9+
"schema": {"$ref": "post.json#/definitions/submitPost"}
10+
}
11+
]
12+
}

blog/schemas/index.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"title": "Blog",
3+
"type": "object",
4+
"properties": {
5+
"posts": {
6+
"type": "array",
7+
"items": {"$ref": "post.json#/definitions/reference"}
8+
},
9+
"page": {
10+
"type": "integer"
11+
},
12+
"next": {
13+
"type": "string",
14+
"format": "uri"
15+
},
16+
"prev": {
17+
"type": "string",
18+
"format": "uri"
19+
}
20+
},
21+
"links": [
22+
{
23+
"rel": "prev",
24+
"href": "{+prev}"
25+
},
26+
{
27+
"rel": "next",
28+
"href": "{+next}"
29+
}
30+
]
31+
}

blog/schemas/post.json

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"title": "Blog post",
3+
"type": "object",
4+
"properties": {
5+
"title": {
6+
"type": "string"
7+
},
8+
"author": {
9+
"type": "string"
10+
},
11+
"date": {
12+
"title": "Posted on",
13+
"type": "string",
14+
"format": "date-time"
15+
},
16+
"content": {
17+
"type": "array",
18+
"items": {"$ref": "#/definitions/contentEntry"}
19+
},
20+
"comments": {
21+
"type": "array",
22+
"items": {"$ref": "#/definitions/comment"}
23+
}
24+
},
25+
"definitions": {
26+
"reference": {
27+
"links": [
28+
{"rel": "full", "href": "post.php{?postId*}"}
29+
]
30+
},
31+
"submitPost": {
32+
"allOf": [{"$ref": "#"}],
33+
"properties": {
34+
"author": {},
35+
"title": {},
36+
"content": {}
37+
},
38+
"required": ["author", "title", "content"],
39+
"additionalProperties": false
40+
},
41+
"comment": {
42+
"title": "Comment",
43+
"type": "object",
44+
"properties": {
45+
"author": {"type": "string"},
46+
"message": {"type": "string"},
47+
"date": {"type": "string", "format": "date-time"}
48+
}
49+
},
50+
"submitComment": {
51+
"title": "Comment",
52+
"type": "object",
53+
"properties": {
54+
"author": {"type": "string", "maxLength": 25},
55+
"message": {"type": "string", "maxLength": 200}
56+
},
57+
"required": ["author", "message"],
58+
"additionalProperties": false
59+
},
60+
"contentEntry": {
61+
"oneOf": [
62+
{
63+
"title": "Paragraph",
64+
"type": "string"
65+
}
66+
]
67+
}
68+
},
69+
"links": [
70+
{
71+
"rel": "comment",
72+
"href": "{?postId*}",
73+
"method": "POST",
74+
"schema": {"$ref": "#/definitions/submitComment"}
75+
}
76+
]
77+
}

0 commit comments

Comments
 (0)