-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
80 lines (67 loc) · 1.8 KB
/
Copy pathindex.php
File metadata and controls
80 lines (67 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Created by PhpStorm.
* User: felix
* Date: 9/25/15
* Time: 10:37 AM
*/
require 'vendor/autoload.php';
function startsWith($haystack, $needle)
{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
$app = new \Slim\Slim([
"debug" => false
]);
$app->contentType("application/json");
$app->error(function (Exception $e) use($app) {
$output = array(
"error" => "invalid operation"
);
echo json_encode($output);
});
$app->get('/pages/', function() {
require_once 'data/config.php';
global $config;
$output = array();
foreach ($config["pages"] as $key => $value) {
$page = array();
$page["id"] = $key;
$page["title"] = $value["title"];
$output[] = $page;
}
echo json_encode($output);
});
$app->get('/title/:lang/:page_id', function($lang, $page_id) {
require_once 'data/config.php';
global $config;
$output = $config["pages"][$page_id]["title"][$lang];
echo json_encode($output);
});
$app->get('/content/:lang/:page_id', function($lang, $page_id) {
require_once 'data/config.php';
global $config;
$path = $config["pages"][$page_id]["content"][$lang];
$Parsedown = new Parsedown();
$output = array();
$output["format"] = array();
if(endsWith($path, '.md')) {
$output["content"] = $Parsedown->text(file_get_contents($path));
$output["format"]["source"] = "md";
} else {
$output["content"] = file_get_contents($path);
$output["format"]["source"] = "html";
}
$output["format"]["output"] = "html";
echo json_encode($output);
});
$app->run();