From 2156cdffef5d2f8176441e5009c8e868f2416c60 Mon Sep 17 00:00:00 2001 From: Masco Skray Date: Fri, 12 Jul 2019 14:30:00 +0800 Subject: [PATCH] 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. --- web/app/.default-config.php | 2 +- web/app/controllers/subdomain/blog/route.php | 28 +++++++++++++---- web/app/models/HTML.php | 33 +++++++++++++++++--- web/app/route.php | 2 +- web/app/views/blog-nav.php | 1 + web/app/views/main-nav.php | 15 +++++---- web/js/uoj.js | 2 +- 7 files changed, 63 insertions(+), 20 deletions(-) diff --git a/web/app/.default-config.php b/web/app/.default-config.php index 232a0b8..82c2adc 100644 --- a/web/app/.default-config.php +++ b/web/app/.default-config.php @@ -52,6 +52,6 @@ return [ ], 'switch' => [ 'web-analytics' => false, - 'blog-use-subdomain' => false + 'blog-domain-mode' => 3 ] ]; diff --git a/web/app/controllers/subdomain/blog/route.php b/web/app/controllers/subdomain/blog/route.php index 0ad3539..82ace32 100644 --- a/web/app/controllers/subdomain/blog/route.php +++ b/web/app/controllers/subdomain/blog/route.php @@ -2,16 +2,32 @@ 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}'); - if (UOJConfig::$data['switch']['blog-use-subdomain']) { - $domain = '{blog_username}.'.UOJConfig::$data['web']['blog']['host']; - $prefix = ''; - } else { - $domain = UOJConfig::$data['web']['blog']['host']; - $prefix = '/blog/{blog_username}'; + switch (UOJConfig::$data['switch']['blog-domain-mode']) { + case 1: + $domain = '{blog_username}.'.UOJConfig::$data['web']['blog']['host']; + $prefix = ''; + break; + 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([ 'domain' => $domain, 'onload' => function() { diff --git a/web/app/models/HTML.php b/web/app/models/HTML.php index 783117e..848c245 100644 --- a/web/app/models/HTML.php +++ b/web/app/models/HTML.php @@ -43,15 +43,38 @@ class HTML { } public static function blog_url($username, $uri) { - $port = UOJConfig::$data['web']['blog']['port'] == 80 ? '' : (':'.UOJConfig::$data['web']['blog']['port']); - if (UOJConfig::$data['switch']['blog-use-subdomain']) - $url = UOJConfig::$data['web']['blog']['protocol'].'://'.blog_name_encode($username).'.'.UOJConfig::$data['web']['blog']['host'].$port; - else - $url = UOJConfig::$data['web']['blog']['protocol'].'://'.UOJConfig::$data['web']['blog']['host'].$port.'/blog/'.blog_name_encode($username); + switch (UOJConfig::$data['switch']['blog-domain-mode']) { + case 1: + $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'].'://'.blog_name_encode($username).'.'.UOJConfig::$data['web']['blog']['host'].$port; + 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 = rtrim($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()) { $config = array_merge(array( diff --git a/web/app/route.php b/web/app/route.php index d23d371..87372a7 100644 --- a/web/app/route.php +++ b/web/app/route.php @@ -39,7 +39,7 @@ Route::group([ Route::any('/hack/{id}', '/hack.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('/blogs/{id}', '/blog_show.php'); diff --git a/web/app/views/blog-nav.php b/web/app/views/blog-nav.php index 758dd8b..4a5a610 100644 --- a/web/app/views/blog-nav.php +++ b/web/app/views/blog-nav.php @@ -20,4 +20,5 @@ \ No newline at end of file diff --git a/web/app/views/main-nav.php b/web/app/views/main-nav.php index da09c05..110458c 100644 --- a/web/app/views/main-nav.php +++ b/web/app/views/main-nav.php @@ -10,13 +10,16 @@ + \ No newline at end of file diff --git a/web/js/uoj.js b/web/js/uoj.js index f4d1276..37a6437 100644 --- a/web/js/uoj.js +++ b/web/js/uoj.js @@ -295,7 +295,7 @@ $.fn.uoj_blog_tag = function() { function click_zan(zan_id, zan_type, zan_delta, node) { var loading_node = $('
loading...
'); $(node).replaceWith(loading_node); - $.post('/click-zan', { + $.post(zan_link + '/click-zan', { id : zan_id, delta : zan_delta, type : zan_type