Skip to content

Commit 43b96b7

Browse files
committed
Add docker files from SimplyCode repo.
1 parent 260266f commit 43b96b7

11 files changed

Lines changed: 952 additions & 0 deletions

File tree

html/.htaccess

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Files settings.json>
2+
<Limit GET>
3+
Order Allow,Deny
4+
Deny from all
5+
</Limit>
6+
</Files>
7+
<Files data.json>
8+
Header set Cache-Control "max-age=0, public, must-revalidate"
9+
</Files>
10+
RewriteEngine on
11+
#<Limit POST PUT DELETE>
12+
# RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
13+
#
14+
# RewriteCond %{REQUEST_METHOD} PUT
15+
# RewriteRule ^(.*)$ simply-edit/store.php [L]
16+
#
17+
# RewriteCond %{REQUEST_METHOD} DELETE
18+
# RewriteRule ^(.*)$ simply-edit/store.php [L]
19+
#
20+
# RewriteCond %{QUERY_STRING} _method=(PUT|DELETE)
21+
# RewriteRule ^(.*)$ simply-edit/store.php [L]
22+
#</Limit>
23+
<Limit GET POST>
24+
RewriteRule ^logout$ simply-edit/logout.php [L]
25+
#RewriteRule ^login$ simply-edit/login.php [L]
26+
</Limit>
27+
<Limit GET>
28+
RewriteCond %{HTTP_USER_AGENT} Lynx|w3m|googlebot|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|Validator [NC,OR]
29+
RewriteCond %{QUERY_STRING} _escaped_fragment_
30+
31+
# Only proxy the request to Prerender if it's a request for HTML
32+
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) simply-edit/prerender.php [L]
33+
</Limit>
34+
35+
Options +Indexes
36+
37+
RewriteCond %{REQUEST_FILENAME} !-f
38+
RewriteCond %{REQUEST_FILENAME} !-d
39+
RewriteRule .* simply-edit/router.php [L]

html/api/.htaccess

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
RewriteEngine on
2+
<Limit POST PUT DELETE>
3+
RewriteCond %{QUERY_STRING} _method=(GET|PUT|DELETE)
4+
RewriteRule ^(.*)$ index.php [L]
5+
</Limit>
6+
7+
RewriteCond %{REQUEST_FILENAME} !-f
8+
RewriteCond %{REQUEST_FILENAME} !-d
9+
RewriteRule .* index.php [L]

html/api/index.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
ini_set('display_errors', 1);
3+
ini_set('display_startup_errors', 1);
4+
error_reporting(E_ALL);
5+
require_once('../../lib/config.php');
6+
7+
$request = http::request();
8+
$requestTarget = $request['target'];
9+
$requestTarget = preg_replace("/^\/api/", "", $requestTarget);
10+
$path = filesystem::path($requestTarget);
11+
$dirname = dirname($path);
12+
$filename = basename($path);
13+
14+
function guid() {
15+
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
16+
}
17+
function tagId() {
18+
return sprintf('%03d-%03d', mt_rand(0, 999), mt_rand(0, 999));
19+
}
20+
21+
function readRecursive($path) {
22+
$dirname = dirname($path);
23+
$filename = basename($path);
24+
if (filesystem::is_dir($path)) {
25+
$files = filesystem::scandir($path);
26+
$files = array_filter($files, function($file) {
27+
if ($file == '.' || $file == '..') {
28+
return false;
29+
}
30+
return true;
31+
});
32+
$result = [];
33+
foreach ($files as $filename) {
34+
if (filesystem::exists($path . '/' . $filename . '/meta')) {
35+
$data = json_decode(filesystem::read($path . '/' . $filename, "meta"), true);
36+
}
37+
if (filesystem::exists($path . '/' . $filename . '/meta.json')) {
38+
$data = json_decode(filesystem::read($path . '/' . $filename, "meta.json"), true);
39+
}
40+
41+
$data['id'] = $filename;
42+
$data['ctime'] = filesystem::ctime($path, $filename);
43+
$data['ctime-human'] = date("c", $data['ctime']);
44+
$data['contents'] = readRecursive($path . "/" . $filename);
45+
$result[] = $data;
46+
}
47+
return $result;
48+
} else {
49+
return filesystem::read($dirname, $filename);
50+
}
51+
}
52+
53+
function deleteRecursive($path) {
54+
$dirname = dirname($path);
55+
$filename = basename($path);
56+
if (filesystem::is_dir($path)) {
57+
$files = filesystem::scandir($path);
58+
$files = array_filter($files, function($file) {
59+
if ($file == '.' || $file == '..') {
60+
return false;
61+
}
62+
return true;
63+
});
64+
foreach ($files as $file) {
65+
deleteRecursive($path . "/" . $file);
66+
}
67+
filesystem::delete($dirname, $filename);
68+
} else {
69+
filesystem::delete($dirname, $filename);
70+
}
71+
}
72+
73+
try {
74+
switch($request['method']) {
75+
case 'OPTIONS':
76+
output('ok',200);
77+
break;
78+
case 'GET':
79+
if (filesystem::exists($dirname . "/" . $filename)) {
80+
$result = readRecursive($path);
81+
output($result, 200);
82+
} else {
83+
filenotfound($path);
84+
}
85+
break;
86+
case 'POST':
87+
if (filesystem::exists($path)) {
88+
if (filesystem::is_dir($path)) {
89+
$exists = true;
90+
while ($exists) {
91+
$newId = tagId();
92+
$newFilename = $newId;
93+
$exists = filesystem::exists($path . $newFilename);
94+
}
95+
96+
// at this point, we are sure that the generated filename does not yet exists, so we can write it;
97+
filesystem::put($path, $newFilename);
98+
output($newId, 200);
99+
}
100+
}
101+
break;
102+
case 'PUT':
103+
if (filesystem::put($dirname, $filename)) {
104+
output('ok',200);
105+
} else {
106+
error(new \Exception("unexpected result from write",501));
107+
}
108+
break;
109+
case 'DELETE':
110+
if (filesystem::exists($path) || filesystem::exists($dirname . "/" . $filename)) {
111+
deleteRecursive($path);
112+
output("deleted", 200);
113+
} else {
114+
filenotfound($path);
115+
}
116+
break;
117+
}
118+
} catch( \Exception $e) {
119+
error($e);
120+
}
121+
122+
function output($data, $status) {
123+
http::response($status, $data);
124+
}
125+
126+
function error($e) {
127+
output(["error" => $e->getCode(), "message" => $e->getMessage()], 501);
128+
}
129+
130+
function filenotfound($path) {
131+
output(["error" => 404, "message" => "File not found"], 404);
132+
}

html/data/data.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

lib/config.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
ini_set('display_errors',1);
3+
4+
require_once('http.php');
5+
require_once('filesystem.php');
6+
7+
filesystem::basedir(dirname(__DIR__).'/www/api/data');
8+
9+
filesystem::allow('/','.*');
10+
/*
11+
filesystem::check('put','/', function($filename, $realfile) {
12+
$contents = file_get_contents($realfile);
13+
$result = json_decode($contents);
14+
if ($result === null) {
15+
throw new \Exception('Invalid JSON',1);
16+
}
17+
return true;
18+
});
19+
*/
20+
/*
21+
filesystem::check('put','/',function($filename, $realfile) {
22+
if (preg_match('/^\{?[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}\}?$/', $filename)) {
23+
return true;
24+
}
25+
throw new \Exception('Invalid ID');
26+
});
27+
*/

0 commit comments

Comments
 (0)