Skip to content

Commit

Permalink
Merge pull request otale#3 from otale/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dongm2ez authored Mar 3, 2017
2 parents 95999ab + d629ed7 commit ac8171d
Show file tree
Hide file tree
Showing 21 changed files with 246 additions and 202 deletions.
2 changes: 1 addition & 1 deletion assembly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<format>dir</format>
</formats>

<includeBaseDirectory>true</includeBaseDirectory>
<includeBaseDirectory>false</includeBaseDirectory>

<fileSets>
<fileSet>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.junicorn</groupId>
<artifactId>tale</artifactId>
<version>1.02</version>
<version>1.03</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/tale/controller/InstallController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class InstallController extends BaseController {
@Inject
private OptionsService optionsService;

private boolean dbConn = false;

/**
* 安装页
*
Expand All @@ -46,7 +48,10 @@ public class InstallController extends BaseController {
public String index(Request request) {
String webRoot = AttachController.CLASSPATH;
boolean existInstall = FileKit.exist(webRoot + "install.lock");
if (existInstall) {
int isInstall = TaleConst.OPTIONS.getInt("site_is_install", 0);
// 已经安装过
if(existInstall || isInstall == 1){
// 如果设置允许重新安装
if("1".equals(TaleConst.OPTIONS.get("allow_install", "0"))){
request.attribute("is_install", false);
} else {
Expand Down Expand Up @@ -89,6 +94,10 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
return RestResponse.fail("邮箱格式不正确");
}

if(!dbConn){
return RestResponse.fail("请检查数据库连接");
}

TaleJdbc.injection(Blade.$().ioc());

Users users = new Users();
Expand All @@ -108,6 +117,7 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
}
optionsService.saveOption("site_title", site_title);
optionsService.saveOption("site_url", site_url);
optionsService.saveOption("site_is_install", "1");

Config config = new Config();
config.addAll(optionsService.getOptions());
Expand Down Expand Up @@ -143,6 +153,7 @@ public RestResponse conn_test(@QueryParam String db_host, @QueryParam String db_
TaleJdbc.put("password", db_pass);
try {
TaleJdbc.testConn();
dbConn = true;
} catch (Exception e) {
String msg = "数据库连接失败, 请检查数据库配置";
if (e instanceof TipException) {
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/tale/controller/admin/AuthController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tale.controller.admin;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.DateKit;
import com.blade.kit.StringKit;
import com.blade.mvc.annotation.Controller;
import com.blade.mvc.annotation.JSON;
Expand Down Expand Up @@ -38,7 +39,11 @@ public class AuthController extends BaseController {
private LogService logService;

@Route(value = "login", method = HttpMethod.GET)
public String login() {
public String login(Response response) {
if(null != this.user()){
response.go("/admin/index");
return null;
}
return "admin/login";
}

Expand All @@ -57,6 +62,10 @@ public RestResponse doLogin(@QueryParam String username,
if (StringKit.isNotBlank(remeber_me)) {
TaleUtils.setCookie(response, user.getUid());
}
Users temp = new Users();
temp.setUid(user.getUid());
temp.setLogged(DateKit.getCurrentUnixTime());
usersService.update(temp);
logService.save(LogActions.LOGIN, null, request.address(), user.getUid());
} catch (Exception e) {
error_count = null == error_count ? 1 : error_count + 1;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/tale/controller/admin/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -213,4 +214,41 @@ public RestResponse backup(@QueryParam String bk_type, @QueryParam String bk_pat
}
}

/**
* 后台高级选项页面
*
* @return
*/
@Route(value = "advanced", method = HttpMethod.GET)
public String advanced(Request request){
Map<String, String> options = optionsService.getOptions();
request.attribute("options", options);
return "admin/advanced";
}

/**
* 保存高级选项设置
* @return
*/
@Route(value = "advanced", method = HttpMethod.POST)
public String doAdvanced(@QueryParam String cache_key, @QueryParam String block_ips){
// 清除缓存
if(StringKit.isNotBlank(cache_key)){
if(cache_key.equals("*")){
cache.clean();
} else {
cache.del(cache_key);
}
}
// 要过过滤的黑名单列表
if(StringKit.isNotBlank(block_ips)){
optionsService.saveOption(Types.BLOCK_IPS, block_ips);
TaleConst.BLOCK_IPS.addAll(Arrays.asList(StringKit.split(block_ips, ",")));
} else {
optionsService.saveOption(Types.BLOCK_IPS, "");
TaleConst.BLOCK_IPS.clear();
}
return "admin/advanced";
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/tale/dto/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public interface Types {
String NEXT = "next";
String PREV = "prev";

/**
* 附件存放的URL,默认为网站地址,如集成第三方则为第三方CDN域名
*/
String ATTACH_URL = "attach_url";

/**
* 网站要过滤,禁止访问的ip列表
*/
String BLOCK_IPS = "site_block_ips";
}
8 changes: 8 additions & 0 deletions src/main/java/com/tale/init/TaleConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.blade.kit.base.Config;
import com.tale.dto.PluginMenu;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Tale 常量存储
Expand Down Expand Up @@ -45,4 +47,10 @@ public class TaleConst {
* 上传文件最大20M
*/
public static Integer MAX_FILE_SIZE = 204800;

/**
* 要过滤的ip列表
*/
public static final Set<String> BLOCK_IPS = new HashSet<>(16);

}
42 changes: 22 additions & 20 deletions src/main/java/com/tale/init/TaleLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.blade.config.BConfig;
import com.blade.kit.FileKit;
import com.tale.controller.admin.AttachController;
import com.tale.utils.ExtClasspathLoader;

import java.io.File;
import java.lang.reflect.Method;
Expand All @@ -19,37 +18,37 @@
*/
public final class TaleLoader {

private TaleLoader(){
private TaleLoader() {
}

public static void init(){
// loadPlugins();
ExtClasspathLoader.loadClasspath();
loadThemes();
public static void init() {
BConfig bConfig = $().bConfig();
loadPlugins(bConfig);
loadThemes(bConfig);
}

public static void loadThemes(){
BConfig bConfig = $().bConfig();
public static void loadThemes(BConfig bConfig) {

String themeDir = AttachController.CLASSPATH + "templates/themes";
try {
themeDir = new URI(themeDir).getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
themeDir = new URI(themeDir).getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
File[] dir = new File(themeDir).listFiles();
for(File f : dir){
if(f.isDirectory() && FileKit.isDirectory(f.getPath() + "/static")){
bConfig.addStatic(new String[]{"/templates/themes/"+ f.getName() +"/static"});
for (File f : dir) {
if (f.isDirectory() && FileKit.isDirectory(f.getPath() + "/static")) {
bConfig.addStatic(new String[]{"/templates/themes/" + f.getName() + "/static"});
}
}
}

public static void loadPlugins() {
public static void loadPlugins(BConfig bConfig) {
File pluginDir = new File(AttachController.CLASSPATH + "plugins");
if (pluginDir.exists() && pluginDir.isDirectory()) {
File[] plugins = pluginDir.listFiles();
for (File plugin : plugins) {
loadPlugin(plugin);
loadPlugin(bConfig, plugin);
}
}
}
Expand All @@ -59,13 +58,16 @@ public static void loadPlugins() {
*
* @param pluginFile 插件文件
*/
public static void loadPlugin(File pluginFile) {
public static void loadPlugin(BConfig bConfig, File pluginFile) {
try {
if(pluginFile.isFile() && pluginFile.getName().endsWith(".jar")){
if (pluginFile.isFile() && pluginFile.getName().endsWith(".jar")) {
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method add = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
add.setAccessible(true);
add.invoke(classLoader, pluginFile.toURI().toURL());

String pluginName = pluginFile.getName().substring(6);
bConfig.addStatic(new String[]{"/templates/plugins/" + pluginName + "/static"});
}
} catch (Exception e) {
throw new RuntimeException("插件 [" + pluginFile.getName() + "] 加载失败");
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/com/tale/init/WebContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import com.blade.ioc.Ioc;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.FileKit;
import com.blade.kit.StringKit;
import com.blade.mvc.view.ViewSettings;
import com.blade.mvc.view.template.JetbrickTemplateEngine;
import com.tale.controller.BaseController;
import com.tale.controller.admin.AttachController;
import com.tale.dto.Types;
import com.tale.ext.AdminCommons;
import com.tale.ext.Commons;
import com.tale.ext.JetTag;
Expand All @@ -24,6 +26,7 @@
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

/**
* Tale初始化进程
Expand All @@ -43,14 +46,14 @@ public void init(BConfig bConfig, ServletContext sec) {
templateEngine.addConfig("jetx.import.macros", "/comm/macros.html");
// 扫描主题下面的所有自定义宏
String themeDir = AttachController.CLASSPATH + "templates/themes";
try {
themeDir = new URI(themeDir).getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
themeDir = new URI(themeDir).getPath();
} catch (URISyntaxException e) {
e.printStackTrace();
}
File[] dir = new File(themeDir).listFiles();
for(File f : dir){
if(f.isDirectory() && FileKit.exist(f.getPath() + "/macros.html")){
for (File f : dir) {
if (f.isDirectory() && FileKit.exist(f.getPath() + "/macros.html")) {
templateEngine.addConfig("jetx.import.macros", "/themes/" + f.getName() + "/macros.html");
}
}
Expand All @@ -68,10 +71,19 @@ public void init(BConfig bConfig, ServletContext sec) {
ViewSettings.$().templateEngine(templateEngine);
if (dbIsOk) {
TaleConst.OPTIONS.addAll(optionsService.getOptions());
TaleConst.INSTALL = true;
TaleConst.INSTALL = TaleConst.OPTIONS.getInt("site_is_install", 0) == 1;
BaseController.THEME = "themes/" + Commons.site_option("site_theme");

String ips = TaleConst.OPTIONS.get(Types.BLOCK_IPS, "");
if(StringKit.isNotBlank(ips)){
TaleConst.BLOCK_IPS.addAll(Arrays.asList(StringKit.split(ips, ",")));
}

Commons.setSiteService(Blade.$().ioc().getBean(SiteService.class));
}
if (FileKit.exist(AttachController.CLASSPATH + "install.lock")) {
TaleConst.INSTALL = true;
}
TaleConst.BCONF = bConfig.config();
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/tale/interceptor/BaseInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public class BaseInterceptor implements Interceptor {
public boolean before(Request request, Response response) {

String uri = request.uri();
String ip = IPKit.getIpAddrByRequest(request.raw());

// 禁止该ip访问
if(TaleConst.BLOCK_IPS.contains(ip)){
response.text("You have been banned, brother");
return false;
}

LOGGE.info("UserAgent: {}", request.userAgent());
LOGGE.info("用户访问地址: {}, 来路地址: {}", uri, IPKit.getIpAddrByRequest(request.raw()));
LOGGE.info("用户访问地址: {}, 来路地址: {}", uri, ip);

if (!TaleConst.INSTALL && !uri.startsWith("/install")) {
response.go("/install");
Expand Down
Loading

0 comments on commit ac8171d

Please sign in to comment.