feat(web): add a new way to access blog

We add a new way to access blog page. Now there are 3.
The switch now uses number to choose the way.
1 is for subdomain and 3 is for subfolder.
2 is for separated blog domain with its subfolder.
For example, mike's link is blog.xxx.xxx/mike.
Blog list page link on navbar will change by domain mode.
This commit is contained in:
Masco Skray 2019-07-12 14:30:00 +08:00
parent 0854940fe0
commit 2156cdffef
7 changed files with 63 additions and 20 deletions

View File

@ -52,6 +52,6 @@ return [
], ],
'switch' => [ 'switch' => [
'web-analytics' => false, 'web-analytics' => false,
'blog-use-subdomain' => false 'blog-domain-mode' => 3
] ]
]; ];

View File

@ -2,16 +2,32 @@
call_user_func(function() { // to prevent variable scope leak call_user_func(function() { // to prevent variable scope leak
Route::pattern('id', '[1-9][0-9]{0,9}');
Route::pattern('blog_username', '[a-zA-Z0-9_\-]{1,20}'); Route::pattern('blog_username', '[a-zA-Z0-9_\-]{1,20}');
if (UOJConfig::$data['switch']['blog-use-subdomain']) { switch (UOJConfig::$data['switch']['blog-domain-mode']) {
$domain = '{blog_username}.'.UOJConfig::$data['web']['blog']['host']; case 1:
$prefix = ''; $domain = '{blog_username}.'.UOJConfig::$data['web']['blog']['host'];
} else { $prefix = '';
$domain = UOJConfig::$data['web']['blog']['host']; break;
$prefix = '/blog/{blog_username}'; case 2:
$domain = UOJConfig::$data['web']['blog']['host'];
$prefix = '/{blog_username}';
break;
case 3:
$domain = UOJConfig::$data['web']['main']['host'];
$prefix = '/blog/{blog_username}';
break;
} }
Route::group([
'domain' => UOJConfig::$data['web']['blog']['host']
], function() {
Route::any("/", '/blogs.php');
Route::any("/blogs/{id}", '/blog_show.php');
Route::any("/post/{id}", '/blog_show.php');
}
);
Route::group([ Route::group([
'domain' => $domain, 'domain' => $domain,
'onload' => function() { 'onload' => function() {

View File

@ -43,15 +43,38 @@ class HTML {
} }
public static function blog_url($username, $uri) { public static function blog_url($username, $uri) {
$port = UOJConfig::$data['web']['blog']['port'] == 80 ? '' : (':'.UOJConfig::$data['web']['blog']['port']); switch (UOJConfig::$data['switch']['blog-domain-mode']) {
if (UOJConfig::$data['switch']['blog-use-subdomain']) case 1:
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.blog_name_encode($username).'.'.UOJConfig::$data['web']['blog']['host'].$port; $port = ((UOJConfig::$data['web']['blog']['protocol'] === "http" && UOJConfig::$data['web']['blog']['port'] == 80) || (UOJConfig::$data['web']['blog']['protocol'] === "https" && UOJConfig::$data['web']['blog']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['blog']['port']);
else $url = UOJConfig::$data['web']['blog']['protocol'].'://'.blog_name_encode($username).'.'.UOJConfig::$data['web']['blog']['host'].$port;
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.UOJConfig::$data['web']['blog']['host'].$port.'/blog/'.blog_name_encode($username); break;
case 2:
$port = ((UOJConfig::$data['web']['blog']['protocol'] === "http" && UOJConfig::$data['web']['blog']['port'] == 80) || (UOJConfig::$data['web']['blog']['protocol'] === "https" && UOJConfig::$data['web']['blog']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['blog']['port']);
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.UOJConfig::$data['web']['blog']['host'].$port.'/'.blog_name_encode($username);
break;
case 3:
$port = ((UOJConfig::$data['web']['main']['protocol'] === "http" && UOJConfig::$data['web']['main']['port'] == 80) || (UOJConfig::$data['web']['main']['protocol'] === "https" && UOJConfig::$data['web']['main']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['main']['port']);
$url = UOJConfig::$data['web']['main']['protocol'].'://'.UOJConfig::$data['web']['main']['host'].$port.'/blog/'.blog_name_encode($username);
break;
}
$url .= $uri; $url .= $uri;
$url = rtrim($url, '/'); $url = rtrim($url, '/');
return HTML::escape($url); return HTML::escape($url);
} }
public static function blog_list_url() {
switch (UOJConfig::$data['switch']['blog-domain-mode']) {
case 1:
case 2:
$port = ((UOJConfig::$data['web']['blog']['protocol'] === "http" && UOJConfig::$data['web']['blog']['port'] == 80) || (UOJConfig::$data['web']['blog']['protocol'] === "https" && UOJConfig::$data['web']['blog']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['blog']['port']);
$url = UOJConfig::$data['web']['blog']['protocol'].'://'.UOJConfig::$data['web']['blog']['host'].$port;
break;
case 3:
$port = ((UOJConfig::$data['web']['main']['protocol'] === "http" && UOJConfig::$data['web']['main']['port'] == 80) || (UOJConfig::$data['web']['main']['protocol'] === "https" && UOJConfig::$data['web']['main']['port'] == 443)) ? '' : (':'.UOJConfig::$data['web']['main']['port']);
$url = UOJConfig::$data['web']['main']['protocol'].'://'.UOJConfig::$data['web']['main']['host'].$port.'/blogs';
break;
}
return HTML::escape(rtrim($url, '/'));
}
public static function url($uri, $config = array()) { public static function url($uri, $config = array()) {
$config = array_merge(array( $config = array_merge(array(

View File

@ -39,7 +39,7 @@ Route::group([
Route::any('/hack/{id}', '/hack.php'); Route::any('/hack/{id}', '/hack.php');
Route::any('/blogs', '/blogs.php'); Route::any('/blogs', '/blogs.php');
if (UOJConfig::$data['switch']['blog-use-subdomain']) { if (UOJConfig::$data['switch']['blog-domain-mode'] != 3) {
Route::any('/blog/{id}', '/blog_show.php'); Route::any('/blog/{id}', '/blog_show.php');
} }
Route::any('/blogs/{id}', '/blog_show.php'); Route::any('/blogs/{id}', '/blog_show.php');

View File

@ -20,4 +20,5 @@
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
var uojBlogUrl = '<?= HTML::blog_url(UOJContext::userid(), '')?>'; var uojBlogUrl = '<?= HTML::blog_url(UOJContext::userid(), '')?>';
var zan_link = uojBlogUrl;
</script> </script>

View File

@ -10,13 +10,16 @@
</div> </div>
<div class="navbar-collapse collapse"> <div class="navbar-collapse collapse">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
<li><a href="/contests"><?= UOJLocale::get('contests') ?></a></li> <li><a href="<?= HTML::url('/contests') ?>"><?= UOJLocale::get('contests') ?></a></li>
<li><a href="/problems"><?= UOJLocale::get('problems') ?></a></li> <li><a href="<?= HTML::url('/problems') ?>"><?= UOJLocale::get('problems') ?></a></li>
<li><a href="/submissions"><?= UOJLocale::get('submissions') ?></a></li> <li><a href="<?= HTML::url('/submissions') ?>"><?= UOJLocale::get('submissions') ?></a></li>
<li><a href="/hacks"><?= UOJLocale::get('hacks') ?></a></li> <li><a href="<?= HTML::url('/hacks') ?>"><?= UOJLocale::get('hacks') ?></a></li>
<li><a href="/blogs"><?= UOJLocale::get('blogs') ?></a></li> <li><a href="<?= HTML::blog_list_url() ?>"><?= UOJLocale::get('blogs') ?></a></li>
<li><a href="/faq"><?= UOJLocale::get('help') ?></a></li> <li><a href="<?= HTML::url('/faq') ?>"><?= UOJLocale::get('help') ?></a></li>
</ul> </ul>
</div><!--/.nav-collapse --> </div><!--/.nav-collapse -->
</div> </div>
</div> </div>
<script type="text/javascript">
var zan_link = '';
</script>

View File

@ -295,7 +295,7 @@ $.fn.uoj_blog_tag = function() {
function click_zan(zan_id, zan_type, zan_delta, node) { function click_zan(zan_id, zan_type, zan_delta, node) {
var loading_node = $('<div class="text-muted">loading...</div>'); var loading_node = $('<div class="text-muted">loading...</div>');
$(node).replaceWith(loading_node); $(node).replaceWith(loading_node);
$.post('/click-zan', { $.post(zan_link + '/click-zan', {
id : zan_id, id : zan_id,
delta : zan_delta, delta : zan_delta,
type : zan_type type : zan_type