Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.
2 changes: 1 addition & 1 deletion src/main/java/com/tale/controller/BaseController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tale.controller;

import com.blade.mvc.WebContext;
import com.blade.mvc.http.Request;
import com.tale.model.entity.Users;
import com.tale.utils.MapCache;
Expand All @@ -14,7 +15,6 @@ public abstract class BaseController {

protected MapCache cache = MapCache.single();


public String render(String viewName) {
return THEME + "/" + viewName;
}
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/com/tale/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ public String page(@PathParam String cid, Request request) {
@GetRoute(value = {"page/:page", "page/:page.html"})
public String index(Request request, @PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
// Page<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).page(page, limit, "created desc");
// request.attribute("articles", articles);
if (page > 1) {
this.title(request, "第" + page + "页");
}
Expand Down Expand Up @@ -191,7 +189,7 @@ public String archives(Request request) {
*
* @return
*/
@GetRoute(value = {"feed", "feed.xml", "atom.xml", "sitemap.xml"})
@GetRoute(value = {"feed", "feed.xml", "atom.xml"})
public void feed(Response response) {

List<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH)
Expand All @@ -203,7 +201,27 @@ public void feed(Response response) {
response.contentType("text/xml; charset=utf-8");
response.body(xml);
} catch (Exception e) {
log.error("生成RSS失败", e);
log.error("生成 rss 失败", e);
}
}

/**
* sitemap 站点地图
*
* @return
*/
@GetRoute(value = {"sitemap", "sitemap.xml"})
public void sitemap(Response response) {
List<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH)
.and("allow_feed", true)
.findAll(OrderBy.desc("created"));

try {
String xml = TaleUtils.getSitemapXml(articles);
response.contentType("text/xml; charset=utf-8");
response.body(xml);
} catch (Exception e) {
log.error("生成 sitemap 失败", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public String index(Request request) {
List<Contents> contents = siteService.getContens(Types.RECENT_ARTICLE, 5);
Statistics statistics = siteService.getStatistics();
// 取最新的20条日志
Page<Logs> logsPage = new Logs().page(1, 20);
Page<Logs> logsPage = new Logs().page(1, 20, "id desc");
List<Logs> logs = logsPage.getRows();

request.attribute("comments", comments);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/tale/extension/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public static String show_thumb(Contents contents) {
*/
public static Contents article_next() {
Contents cur = current_article();
return null != cur ? siteService.getNhContent(Types.NEXT, cur.getCid()) : null;
return null != cur ? siteService.getNhContent(Types.NEXT, cur.getCreated()) : null;
}

/**
Expand All @@ -354,7 +354,7 @@ public static Contents article_next() {
*/
public static Contents article_prev() {
Contents cur = current_article();
return null != cur ? siteService.getNhContent(Types.PREV, cur.getCid()) : null;
return null != cur ? siteService.getNhContent(Types.PREV, cur.getCreated()) : null;
}

/**
Expand Down
41 changes: 23 additions & 18 deletions src/main/java/com/tale/hooks/BaseWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,37 @@ public boolean before(Signature signature) {
log.info("UserAgent: {}", request.userAgent());
log.info("用户访问地址: {}, 来路地址: {}", uri, ip);

if (uri.startsWith("/static")) {
if (uri.startsWith(TaleConst.STATIC_URI)) {
return true;
}

if (!TaleConst.INSTALL && !uri.startsWith("/install")) {
response.redirect("/install");
if (!TaleConst.INSTALLED && !uri.startsWith(TaleConst.INSTALL_URI)) {
response.redirect(TaleConst.INSTALL_URI);
return false;
}

if (TaleConst.INSTALL) {
Users user = TaleUtils.getLoginUser();
if (null == user) {
Integer uid = TaleUtils.getCookieUid(request);
if (null != uid) {
user = new Users().find(uid);
request.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
}
}
if (TaleConst.INSTALLED) {
return isRedirect(request, response);
}
return true;
}

if (uri.startsWith("/admin") && !uri.startsWith("/admin/login")) {
if (null == user) {
response.redirect("/admin/login");
return false;
}
request.attribute("PLUGIN_MENUS", TaleConst.PLUGIN_MENUS);
private boolean isRedirect(Request request, Response response) {
Users user = TaleUtils.getLoginUser();
String uri = request.uri();
if (null == user) {
Integer uid = TaleUtils.getCookieUid(request);
if (null != uid) {
user = new Users().find(uid);
request.session().attribute(TaleConst.LOGIN_SESSION_KEY, user);
}
}
if (uri.startsWith(TaleConst.ADMIN_URI) && !uri.startsWith(TaleConst.LOGIN_URI)) {
if (null == user) {
response.redirect(TaleConst.LOGIN_URI);
return false;
}
request.attribute(TaleConst.PLUGINS_MENU_NAME, TaleConst.PLUGIN_MENUS);
}
return true;
}
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/tale/init/TaleConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TaleConst {
public static String AES_SALT = "0123456789abcdef";
public static String LOGIN_SESSION_KEY = "login_user";
public static Environment OPTIONS = Environment.of(new HashMap<>());
public static Boolean INSTALL = false;
public static Boolean INSTALLED = false;
public static Boolean ENABLED_CDN = true;
public static Environment BCONF = null;

Expand Down Expand Up @@ -56,4 +56,29 @@ public class TaleConst {
*/
public static final Set<String> BLOCK_IPS = new HashSet<>(16);

/**
* 静态资源URI
*/
public static final String STATIC_URI = "/static";

/**
* 安装页面URI
*/
public static final String INSTALL_URI = "/install";

/**
* 后台URI前缀
*/
public static final String ADMIN_URI = "/admin";

/**
* 后台登录地址
*/
public static final String LOGIN_URI = "/admin/login";

/**
* 插件菜单 Attribute Name
*/
public static final String PLUGINS_MENU_NAME = "plugin_menus";

}
2 changes: 1 addition & 1 deletion src/main/java/com/tale/init/WebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void processor(Blade blade) {
TaleConst.BLOCK_IPS.addAll(Arrays.asList(ips.split(",")));
}
if (Files.exists(Paths.get(AttachController.CLASSPATH + "install.lock"))) {
TaleConst.INSTALL = Boolean.TRUE;
TaleConst.INSTALLED = Boolean.TRUE;
}

BaseController.THEME = "themes/" + Commons.site_option("site_theme");
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/tale/service/SiteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void initSite(Users users) {
String cp = SiteService.class.getClassLoader().getResource("").getPath();
File lock = new File(cp + "install.lock");
lock.createNewFile();
TaleConst.INSTALL = Boolean.TRUE;
TaleConst.INSTALLED = Boolean.TRUE;
new Logs(LogActions.INIT_SITE, null, "", uid.intValue()).save();
} catch (Exception e) {
throw new TipException("初始化站点失败");
Expand Down Expand Up @@ -274,16 +274,17 @@ public List<Metas> getMetas(String searchType, String type, int limit) {
* 获取相邻的文章
*
* @param type 上一篇:prev | 下一篇:next
* @param cid 当前文章id
* @param created 当前文章创建时间
*/
public Contents getNhContent(String type, Integer cid) {
public Contents getNhContent(String type, Integer created) {
Contents contents = null;
if (Types.NEXT.equals(type)) {
return new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).and("cid", ">", cid).find();
contents = new Contents().query("SELECT * FROM t_contents WHERE type = ? AND status = ? AND created > ? ORDER BY created ASC LIMIT 1", Types.ARTICLE, Types.PUBLISH, created);
}
if (Types.PREV.equals(type)) {
return new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).and("cid", "<", cid).find();
contents = new Contents().query("SELECT * FROM t_contents WHERE type = ? AND status = ? AND created < ? ORDER BY created DESC LIMIT 1", Types.ARTICLE, Types.PUBLISH, created);
}
return null;
return contents;
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/tale/utils/TaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Tale工具类
Expand Down Expand Up @@ -278,6 +279,43 @@ public static String getRssXml(java.util.List<Contents> articles) throws FeedExc
return out.outputString(channel);
}

private static final String SITEMAP_HEAD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\" xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">";

static class Url {
String loc;
String lastmod;

public Url(String loc) {
this.loc = loc;
}
}

public static String getSitemapXml(List<Contents> articles) {
List<Url> urls = articles.stream()
.map(TaleUtils::parse)
.collect(Collectors.toList());
urls.add(new Url(Commons.site_url() + "/archives"));

String urlBody = urls.stream()
.map(url -> {
String s = "<url><loc>" + url.loc + "</loc>";
if (null != url.lastmod) {
s += "<lastmod>" + url.lastmod + "</lastmod>";
}
return s + "</url>";
})
.collect(Collectors.joining("\n"));

return SITEMAP_HEAD + urlBody + "</urlset>";
}

private static Url parse(Contents contents) {
Url url = new Url(Commons.site_url() + "/article/" + contents.getCid());
url.lastmod = DateKit.toString(contents.getModified(), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
return url;
}

/**
* 替换HTML脚本
*
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/static/admin/js/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ function allow_comment(obj) {
var on = this_.attr('on');
if (on == 'true') {
this_.attr('on', 'false');
$('#allow_comment').val('false');
$('#allowComment').val('false');
} else {
this_.attr('on', 'true');
$('#allow_comment').val('true');
$('#allowComment').val('true');
}
}

Expand All @@ -261,10 +261,10 @@ function allow_ping(obj) {
var on = this_.attr('on');
if (on == 'true') {
this_.attr('on', 'false');
$('#allow_ping').val('false');
$('#allowPing').val('false');
} else {
this_.attr('on', 'true');
$('#allow_ping').val('true');
$('#allowPing').val('true');
}
}

Expand All @@ -274,10 +274,10 @@ function allow_feed(obj) {
var on = this_.attr('on');
if (on == 'true') {
this_.attr('on', 'false');
$('#allow_feed').val('false');
$('#allowFeed').val('false');
} else {
this_.attr('on', 'true');
$('#allow_feed').val('true');
$('#allowFeed').val('true');
}
}

Expand Down