Skip to content

Commit 1690b86

Browse files
committed
server
1 parent e7a9246 commit 1690b86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+14690
-19
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.idea
22
/.vscode
33
webman
4-
runtime
54
*Thumbs.db*
5+
server/composer.lock
6+
server/vendor

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Manual for webman
33

44
# Start
5-
`./webman.phar start` or `php ./webman.phar start`
5+
`php server/start.php start`
66

77
# Visit
88
http://127.0.0.1:8282/doc/en

script/translate/ar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/bn.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/de.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/en.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/es.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/fr.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/hi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/it.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/jp.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/ko.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/pt.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/ru.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/th.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/tr.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/vi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/zh-hk.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

script/translate/zh-tw.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require __DIR__ . '/../vendor/autoload.php';
2+
require __DIR__ . '/../../server/vendor/autoload.php';
33
require __DIR__ . '/filters.php';
44

55
ini_set('display_errors', 'on');

server/app/controller/Doc.php

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
namespace app\controller;
3+
4+
use GuzzleHttp\Psr7\Response;
5+
use support\Request;
6+
7+
class Doc
8+
{
9+
/*public function index(Request $request)
10+
{
11+
return view('doc/index', [
12+
'nav' => 'doc',
13+
'html_title' => 'webman',
14+
]);
15+
}*/
16+
17+
public function view(Request $request, $lang)
18+
{
19+
$uri = $request->uri();
20+
if (strpos($uri, '../') !== false) {
21+
return response('<h1>400 Bad Request</h1>', 400);
22+
}
23+
24+
if ($uri === "/doc/$lang") {
25+
return redirect("/doc/$lang/");
26+
}
27+
28+
if ($uri === "/doc/$lang/README.html") {
29+
return redirect("/doc/$lang/");
30+
}
31+
32+
if ($uri === "/doc/$lang/") {
33+
$uri = "/doc/$lang/README.md";
34+
$index_title = "$lang";
35+
} else {
36+
if (pathinfo($uri, PATHINFO_EXTENSION) !== 'html') {
37+
return notfound();
38+
}
39+
$uri = strstr($uri, '.html', true).'.md';
40+
}
41+
42+
$file = resource_path() . "/$uri";
43+
44+
if (!is_file($file)) {
45+
return response('<h1>404 Not Found</h1>', 404);
46+
}
47+
48+
[$title, $sidebar] = $this->sidebar( resource_path() . "/doc/{$lang}/SUMMARY.md", $uri);
49+
50+
$content = $this->format($file);
51+
52+
$info = $this->getInfo($lang);
53+
54+
$langs = $this->getLangs();
55+
56+
return view('doc/view', [
57+
'html_title' => $index_title??($title ? "$title-{$info['name']}": $info['name']),
58+
'repo' => $info['repo'],
59+
'name' => $info['name'],
60+
'sidebar' => $sidebar,
61+
'content' => $content,
62+
'current_lang' => $langs[$lang],
63+
'langs' => $langs
64+
]);
65+
}
66+
67+
protected function sidebar($sidebar_file, $uri)
68+
{
69+
$sidebar_content = file_get_contents($sidebar_file);
70+
$title = '';
71+
$relative_uri_arr = explode('/', $uri);
72+
if (empty($relative_uri_arr[0])) {
73+
unset($relative_uri_arr[0], $relative_uri_arr[1], $relative_uri_arr[2]);
74+
} else {
75+
unset($relative_uri_arr[0], $relative_uri_arr[1]);
76+
}
77+
78+
$relative_uri = implode('/', $relative_uri_arr);
79+
if (preg_match('/\[(.*?)\]\('.preg_quote($relative_uri, '/').'\)/', $sidebar_content, $match)) {
80+
$title = $match[1];
81+
}
82+
$path = '';
83+
$count = substr_count($uri, '/');
84+
if ($count > 3) {
85+
for ($i=0; $i<$count-3;$i++) {
86+
$path .= '../';
87+
}
88+
}
89+
return [$title, $this->formatContent($sidebar_content, $path)];
90+
}
91+
92+
protected function format($file, $path = '') {
93+
if (!is_file($file)) {
94+
return '';
95+
}
96+
return $this->formatContent(file_get_contents($file, $path));
97+
}
98+
99+
protected function formatContent($content, $path = '') {
100+
if ($path) {
101+
$content = preg_replace('/\[(.*?)\]\((.*?\.md)\)/', "[$1]($path$2)", $content);
102+
}
103+
return markdown(str_replace('.md', '.html', $content), false);
104+
}
105+
106+
public function file(Request $request)
107+
{
108+
$path = $request->path();
109+
if (strpos($path, '/..') !== false) {
110+
return notfound();
111+
}
112+
$file = resource_path()."/$path";
113+
if (!is_file($file)) {
114+
return notfound();
115+
}
116+
return \response()->withFile($file);
117+
}
118+
119+
protected function getLangs()
120+
{
121+
$langs = [];
122+
foreach (glob(resource_path() . '/doc/*.json') as $file) {
123+
$lang = basename($file, '.json');
124+
$data = json_decode(file_get_contents($file), true);
125+
$langs[$lang] = $data['lang'];
126+
}
127+
return $langs;
128+
}
129+
130+
protected function getInfo($lang)
131+
{
132+
return json_decode(file_get_contents(resource_path() . "/doc/$lang.json"), true);
133+
}
134+
135+
}

server/app/functions.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
use app\support\Markdown;
3+
4+
function resource_path()
5+
{
6+
return config('app.resource_path');
7+
}
8+
9+
function notfound()
10+
{
11+
return response(file_get_contents(public_path() . '/404.html', 404));
12+
}
13+
14+
function markdown($content, $output_safe_content = true)
15+
{
16+
static $markdown;
17+
if (!$markdown) $markdown = Markdown::instance()->setBreaksEnabled(true);
18+
$content = htmlspecialchars_decode($content);
19+
return $markdown->setMarkupEscaped($output_safe_content)->text($content);
20+
}

server/app/middleware/StaticFile.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* This file is part of webman.
4+
*
5+
* Licensed under The MIT License
6+
* For full copyright and license information, please see the MIT-LICENSE.txt
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @author walkor<[email protected]>
10+
* @copyright walkor<[email protected]>
11+
* @link http://www.workerman.net/
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
15+
namespace app\middleware;
16+
17+
use Webman\MiddlewareInterface;
18+
use Webman\Http\Response;
19+
use Webman\Http\Request;
20+
21+
/**
22+
* Class StaticFile
23+
* @package app\middleware
24+
*/
25+
class StaticFile implements MiddlewareInterface
26+
{
27+
public function process(Request $request, callable $next) : Response
28+
{
29+
// Access to files beginning with. Is prohibited
30+
if (strpos($request->path(), '/.') !== false) {
31+
return response('<h1>403 forbidden</h1>', 403);
32+
}
33+
/** @var Response $response */
34+
$response = $next($request);
35+
// Add cross domain HTTP header
36+
/*$response->withHeaders([
37+
'Access-Control-Allow-Origin' => '*',
38+
'Access-Control-Allow-Credentials' => 'true',
39+
]);*/
40+
return $response;
41+
}
42+
}

server/app/support/Markdown.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace app\support;
3+
4+
class Markdown extends \Parsedown
5+
{
6+
protected function inlineUrl($Excerpt)
7+
{
8+
return $this->myParseUrl(parent::inlineUrl($Excerpt));
9+
}
10+
11+
protected function inlineLink($Excerpt)
12+
{
13+
return $this->myParseUrl(parent::inlineLink($Excerpt));
14+
}
15+
16+
protected function myParseUrl($result)
17+
{
18+
if (!is_array($result)) {
19+
return $result;
20+
}
21+
$href = $result['element']['attributes']['href'];
22+
$host = parse_url((string)$href, PHP_URL_HOST);
23+
if ($host && (strpos($host, 'workerman.net') === false && strpos($host, 'popoim') === false && strpos($host, '99kf') === false)) {
24+
$result['element']['attributes']['rel'] = 'nofollow';
25+
$result['element']['attributes']['target'] = '_blank';
26+
}
27+
return $result;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)