Skip to content

Instantly share code, notes, and snippets.

@horsley
Created January 4, 2014 06:14
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save horsley/8252295 to your computer and use it in GitHub Desktop.
Save horsley/8252295 to your computer and use it in GitHub Desktop.
sms + tasker转发服务的脚本
<?php
define('APP_PATH', dirname(__FILE__));
define('DATA_FILE', APP_PATH. '/sms.htdata');
if ($_SERVER['REQUEST_METHOD'] == 'POST') { //tasker提交
if (!isset($_POST['c'])) {
die('Invalid Request!');
} else {
$data = '';
if (file_exists(DATA_FILE)) { //读入已有数据
$data = json_decode(file_get_contents(DATA_FILE), true);
}
$data[] = array('c' => $_POST['c'], 'd' => $_POST['d'], 'n' => $_POST['n'], 'f' => $_POST['f'], 't' => $_POST['t']);
file_put_contents(DATA_FILE, json_encode($data)); //保存数据
//转发响应
if (isset($_POST['c']) && preg_match('/(校验码|验证码|快递|快件|亚马逊|腾讯)/', $_POST['c'])) {
echo "HTTP/1.1 202 Please Forward";
} else {
echo "HTTP/1.1 201 Do Not Forward";
}
}
} else { //用户访问
simple_auth(array('horsley' => '123456'));
$data = '';
if (file_exists(DATA_FILE)) {
$data = json_decode(file_get_contents(DATA_FILE), true);
}
$data = array_reverse($data); //数组反转,新的在前
foreach($data as &$d) {
$d['t'] = str_replace('.', ':', $d['t']);
$d['d'] = $d['d'] . ' ' . $d['t']; //@todo: 时间显示的优化
}
tmpl_render(APP_PATH.'/tmpl.html', array('sms' => $data));
}
/**
* 简单身份验证
* @param array $available_user 用户密码数组array('username' => 'pass)
*/
function simple_auth($available_user) {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My 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.');
}
}
}
/**
* 简单模板渲染
* @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!');
}
}
<!doctype html>
<html>
<head>
<title>SMS</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
<style>
body{background: #f2f2f2;color: #7e7e7e;margin: 0;padding: 0}
.container{padding: 0.8em}
.sms_card{background: #fff;padding: 0.8em;border: 1px solid #e7e7e7}
.sms_card + .sms_card {margin-top: -1px}
.sms_date{float: right}
.sms_from{font-weight: bold;color: #000}
.sms_content p {margin: 0.2em 0}
</style>
</head>
<body>
<div class="container">
<?php if(empty($sms)): ?>
<p style="text-align:center">尚未有任何数据</p>
<?php else:?>
<?php foreach($sms as $s): ?>
<div class="sms_card">
<div class="sms_meta">
<span class="sms_date">
<?php if(!empty($s['d'])) echo $s['d']?>
</span>
<span class="sms_from">
<?php if(!empty($s['f'])) echo $s['f']?>
</span>
</div>
<div class="sms_content">
<p><?php if(!empty($s['c'])) echo $s['c']?></p>
</div>
</div>
<?php endforeach;?>
<?php endif;?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment