-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
118 lines (105 loc) · 3.64 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* 一句话日志系统
* @author: horsley
* @version: 2014-01-28
*/
date_default_timezone_set('Asia/Shanghai');
define('APP_ROOT', dirname(__FILE__));
define('DATA_DIR', APP_ROOT . '/.data');
define('ASSET_DIR', APP_ROOT . '/asset');
define('CONFIG_FILE', DATA_DIR.'/config.json');
define('DATA_FILE', DATA_DIR.'/data.log');
define('DATA_INDEX', DATA_DIR.'/data.idx');
require APP_ROOT.'/func.php';
require APP_ROOT.'/log.php';
if (!file_exists(CONFIG_FILE) || !is_readable(CONFIG_FILE)) {
err('Config file not exist or permission denied');
}
//简单身份验证
//simple_auth(array(
// config()->user->username => config()->user->password
//), config()->realm);
//////////////////////////////////////////////////////////////////////
// 路由分发 //
//////////////////////////////////////////////////////////////////////
if (empty($_GET['m']) || !in_array($_GET['m'], array(
'save', 'load'
))) {
$module = 'index'; //默认路由
} else {
$module = $_GET['m'];
}
$func_name = 'm_'.$module;
if (function_exists($func_name) && is_callable($func_name)) {
session_start();
call_user_func($func_name);
} else {
die('405 Method Not Allowed');
}
/////////////////// 路由分发结束,下面是各个模块 /////////////////////
/**
* 入口页
*/
function m_index() {
session_commit();
$data = paged_read_lines(0, config()->page_size, $last_page);
tmpl_render(ASSET_DIR.'/tmpl.html',array(
'logs' => $data,
'last_page' => $last_page,
));
}
/**
* 日志写入
*/
function m_save() {
if (is_ajax() && is_post()) {
//这里做些简单的限制吧
//简单防相同内容刷屏(后面加点随机的东西就可以了)
if (isset($_SESSION['last_post']) && $_SESSION['last_post'] == $_POST['log_line']) {
die(json_encode(array('ok' => false, 'info' => '不要发重复内容撒!')));
} else if(isset($_SESSION['last_post_ts']) && $_SESSION['last_post_ts'] > time() - 1) {
die(json_encode(array('ok' => false, 'info' => '不要发太快撒!')));
} else {
$_SESSION['last_post'] = $_POST['log_line'];
$_SESSION['last_post_ts'] = time();
session_commit();
}
$_POST['log_line'] = isset($_POST['log_line']) ? trim($_POST['log_line']) : '';
$_POST['log_line'] = mb_substr($_POST['log_line'], 0, 300, 'UTF-8'); //限制单行长度
if (!empty($_POST['log_line'])) {
$time = date('[Y/m/d H:i:s] ');
$log_line = "{$time}{$_POST['log_line']}\n"; //追加日期格式
if(write_line($log_line)) {
//广播过程
$broadcast_url = "http://127.0.0.1:8421/pub?line=".urlencode(htmlspecialchars($log_line));
$ch = curl_init($broadcast_url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true
));
curl_exec($ch);
die(json_encode(array('ok' => true, 'info' => htmlspecialchars($log_line))));
} else {
die(json_encode(array('ok' => false, 'info' => $time.'Write File Error!')));
}
}
} else {
die('Invalid Request');
}
}
/**
* ajax加载前一页
*/
function m_load() {
if (is_ajax()) {
$page = intval($_GET['p']);
if (empty($page)) {
die('Invalid Page Number');
}
$data = paged_read_lines($page, config()->page_size, $last_page);
echo json_encode(array(
'data' => $data,
'last_page' => $last_page,
));
}
}