Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
horsley committed Jan 29, 2014
0 parents commit 81c9b7a
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .data/config.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"realm": "Private Area",
"user": [
{
"username": "horsley",
"password": "123456"
}
]
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.data/*
!.data/*.sample
4 changes: 4 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^\..* - [F] #保护.开头的目录,例如.git
</IfModule>
11 changes: 11 additions & 0 deletions asset/pure-min.css

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions asset/tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<title><?php echo config()->title ?></title>
<link rel="stylesheet" href="<?php echo get_baseurl()?>/asset/pure-min.css">
<style>
html, body {height: 100%; padding: 0; }
html {width: 100%; overflow: hidden; margin: 0; }
body {position: relative; }
#scrollContainer {overflow: auto; position:absolute; top: 0; bottom: 2em; width: 100%; }

body{background: #111;color: #999;margin: 0;font: 16px/1.3 Menlo, Monaco, Consolas, "Courier New", monospace}
#ctrl_bar{position: fixed;width: 100%;bottom: 0;background: #000;}
#ctrl_bar input{height: 2em;color: #fff;background: #000;font: 16px/1.8 Menlo, Monaco, Consolas, "Courier New", monospace}
#line_recv{margin: 0;border: 0;display: block;width: 100%;outline: none}
p.error{color: #800000}
#content p {margin: 0;}
#content {padding: 0.3em}
@media screen and (max-width: 480px) {
body{font: 12px/1.3}
}
</style>
</head>
<body>
<div id="scrollContainer">
<div id="content">
<?php if(!empty($logs)) foreach($logs as $l): ?>
<p><?php echo nl2br($l) ?></p>
<?php endforeach; ?>
</div>
<div id="bottom"></div>
</div>
<div id="ctrl_bar">
<form id="write_log">
<input type="text" id="line_recv" placeholder="Input Here~" name="log_line">
</form>
</div>
<script type="text/javascript" src="<?php echo get_baseurl() ?>/asset/zepto.min.js"></script>
<script type="text/javascript">
$(function() {
//滚到最低
var bottom = $('#bottom');
bottom.offsetParent().scrollTop(bottom.offset().top - 50);

$('#write_log').submit(function(){
$.post('<?php echo get_baseurl() ?>/index.php?m=save',
$('#write_log').serialize(),
function(resp){
if(resp.ok) {
$('#content').append('<p>'+resp.info+'</p>');
$('#line_recv').val('');
} else {
$('#content').append('<p class="error">'+resp.info+'</p>');
}
}, 'json');
return false;
});

$('#line_recv').keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
$("form").submit();
}
});
});
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions asset/zepto.min.js

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions func.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* 函数库
* @author: horsley
* @version: 2014-01-28
*/

/**
* 获取本系统存放的目录 对应的url
* 当本系统部署在非站点根目录的时候 需要使用本函数获取系统根目录对应url
* 其后没有斜杠
* @access public
* @return string
*/
function get_baseurl() {
static $baseURL;
if (!empty($baseURL)) return $baseURL;
$baseURL = (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if (!$host = $_SERVER['HTTP_HOST']) {
if (!$host = $_SERVER['SERVER_NAME']) {
$host = !empty($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '';
}
}
$baseURL .= $host. (preg_match('/(?:.*?):\d+/', $host) ? '': //如果host里面没有端口号,才考虑拼合
($_SERVER["SERVER_PORT"] == "80" ? '' : ':'.$_SERVER["SERVER_PORT"]));
$baseURL .= get_basedir(); //去掉root目录和末尾的/index.php
return $baseURL;
}

/**
* 获取本系统存放的目录
* 相对于站点根目录的相对目录
* 只能是通过统一入口进入的调用
* 返回的目录路径前面有杠,后面没杠
* 如果部署在站点根目录,返回空文本
* @access public
* @return string
*/
function get_basedir() {
$base_dir = substr(dirname($_SERVER['SCRIPT_FILENAME']), strlen($_SERVER['DOCUMENT_ROOT']));
//这里windows和linux环境可能存在差异导致丢失前面的斜杠,下面做兼容性处理
if (!empty($base_dir) && $base_dir{0} != '/') {
$base_dir = '/'.$base_dir;
}
return $base_dir;
}

/**
* 向浏览器输出错误信息,并停止解析
* @param $ErrMsg
*/
function err($ErrMsg = 'Access denied!') {
header('HTTP/1.1 405 Method Not Allowed');
header('Content-Type:text/plain; charset=utf-8');
echo $ErrMsg;
exit;
}

/**
* 简单身份验证
* @param array $available_user 用户密码数组array('username' => 'pass)
* @param string $realm 可选
*/
function simple_auth($available_user, $realm= 'My Realm') {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="'.$realm.'"');
header('HTTP/1.0 401 Unauthorized');
die('Restricted Area!');
} else {
if (!isset($available_user[$_SERVER['PHP_AUTH_USER']])
|| $available_user[$_SERVER['PHP_AUTH_USER']] != $_SERVER['PHP_AUTH_PW']
) {
die('Authentication Failed.');
}
}
}

/**
* 全局配置对象获取
* @return mixed
*/
function config() {
static $_obj;
if (empty($_obj)) { //单例
$_obj = json_decode(file_get_contents(CONFIG_FILE));
}
return $_obj;
}


/**
* 简单模板渲染
* @param $tmpl_file 文件中
* @param array $data
*/
function tmpl_render($tmpl_file, $data = null) {
if(is_array($data)) {
extract($data);
}
if (file_exists($tmpl_file)) {
include($tmpl_file);
} else {
die('tmpl file not found!');
}
}

/**
* 判断ajax请求
* @return bool
*/
function is_ajax() {
return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

/**
* 判断post请求
*/
function is_post() {
return $_SERVER['REQUEST_METHOD'] == 'POST';
}
77 changes: 77 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* 一句话日志系统
* @author: horsley
* @version: 2014-01-28
*/

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');

require APP_ROOT.'/func.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'
))) {
$module = 'index'; //默认路由
} else {
$module = $_GET['m'];
}

$func_name = 'm_'.$module;
if (function_exists($func_name) && is_callable($func_name)) {
call_user_func($func_name);
} else {
die('405 Method Not Allowed');
}
/////////////////// 路由分发结束,下面是各个模块 /////////////////////

/**
* 入口页
*/
function m_index() {
tmpl_render(ASSET_DIR.'/tmpl.html',array(
'logs' => file_exists(DATA_FILE) ? file(DATA_FILE) : array(),
));
}

/**
* 日志写入
*/
function m_save() {
if (is_ajax() && is_post()) {
$_POST['log_line'] = isset($_POST['log_line']) ? trim($_POST['log_line']) : '';
if (!empty($_POST['log_line'])) {
$time = date('[Y/m/d H:i:s] ');
$log_line = "{$time}{$_POST['log_line']}\n";

if(file_put_contents(DATA_FILE, $log_line, FILE_APPEND|LOCK_EX)) {
die(json_encode(array('ok' => true, 'info' => $log_line)));
} else {
die(json_encode(array('ok' => false, 'info' => $time.'Write File Error!')));
}
}
} else {
die('Invalid Request');
}
}




0 comments on commit 81c9b7a

Please sign in to comment.