2016-03-28 14:45:00 +08:00
|
|
|
|
<?php
|
2017-07-15 14:31:28 +08:00
|
|
|
|
require_once __DIR__ . '/Access_Bootstrap.php';
|
|
|
|
|
|
2018-01-26 17:20:26 +08:00
|
|
|
|
class Access_Action extends Typecho_Widget implements Widget_Interface_Do
|
2016-03-28 14:45:00 +08:00
|
|
|
|
{
|
2016-11-20 16:57:01 +08:00
|
|
|
|
private $access;
|
2016-03-28 14:45:00 +08:00
|
|
|
|
|
2018-01-26 17:20:26 +08:00
|
|
|
|
public function __construct($request, $response, $params = null)
|
2016-03-28 14:45:00 +08:00
|
|
|
|
{
|
2018-01-26 17:20:26 +08:00
|
|
|
|
parent::__construct($request, $response, $params);
|
|
|
|
|
|
2016-11-20 16:57:01 +08:00
|
|
|
|
$this->access = new Access_Core();
|
2016-03-28 14:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute()
|
2018-01-25 18:55:06 +08:00
|
|
|
|
{}
|
2016-03-28 14:45:00 +08:00
|
|
|
|
|
|
|
|
|
public function action()
|
2018-01-25 18:55:06 +08:00
|
|
|
|
{}
|
2016-03-28 14:45:00 +08:00
|
|
|
|
|
2016-12-11 18:55:12 +08:00
|
|
|
|
public function writeLogs()
|
|
|
|
|
{
|
2017-07-14 11:09:10 +08:00
|
|
|
|
$image = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAQUAP8ALAAAAAABAAEAAAICRAEAOw==');
|
|
|
|
|
$this->response->setContentType('image/gif');
|
2017-07-15 14:31:28 +08:00
|
|
|
|
if ($this->access->config->writeType == 1) {
|
|
|
|
|
$this->access->writeLogs(null, $this->request->u, $this->request->cid, $this->request->mid);
|
|
|
|
|
}
|
2017-07-14 11:09:10 +08:00
|
|
|
|
echo $image;
|
2016-12-11 18:55:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-01 15:00:23 +08:00
|
|
|
|
public function ip()
|
2016-03-28 14:45:00 +08:00
|
|
|
|
{
|
2017-05-15 17:36:27 +08:00
|
|
|
|
$ip = $this->request->get('ip');
|
2016-11-08 13:27:19 +08:00
|
|
|
|
try {
|
|
|
|
|
$this->checkAuth();
|
2016-12-11 18:55:12 +08:00
|
|
|
|
$response = Access_Ip::find($ip);
|
|
|
|
|
if (is_array($response)) {
|
|
|
|
|
$response = array(
|
|
|
|
|
'code' => 0,
|
|
|
|
|
'data' => implode(' ', $response),
|
|
|
|
|
);
|
|
|
|
|
} else {
|
2017-05-15 17:36:27 +08:00
|
|
|
|
throw new Exception('解析ip失败');
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
try {
|
|
|
|
|
$http = Typecho_Http_Client::get();
|
|
|
|
|
$result = $http->send('https://tools.keycdn.com/geo.json?host=' . $ip);
|
|
|
|
|
$result = Json::decode($result, true);
|
|
|
|
|
if ($result['status'] == 'success') {
|
|
|
|
|
$response = array(
|
|
|
|
|
'code' => 0,
|
|
|
|
|
'data' => $result['data']['geo']['country_name'] . ' ' . $result['data']['geo']['city'],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception $e) {
|
2016-12-11 18:55:12 +08:00
|
|
|
|
$response = array(
|
|
|
|
|
'code' => 100,
|
2017-05-15 17:36:27 +08:00
|
|
|
|
'data' => '很抱歉,ipip.net查询无结果,同时你的服务器无法连接fallback接口(tools.keycdn.com)',
|
2016-12-11 18:55:12 +08:00
|
|
|
|
);
|
2016-11-08 13:27:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-26 17:20:26 +08:00
|
|
|
|
$this->response->throwJson($response);
|
2016-11-08 13:27:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function deleteLogs()
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->checkAuth();
|
|
|
|
|
$data = @file_get_contents('php://input');
|
|
|
|
|
$data = Json::decode($data, true);
|
|
|
|
|
if (!is_array($data)) {
|
|
|
|
|
throw new Exception('params invalid');
|
|
|
|
|
}
|
2016-11-20 16:57:01 +08:00
|
|
|
|
$this->access->deleteLogs($data);
|
2017-05-15 17:36:27 +08:00
|
|
|
|
$response = array(
|
2016-11-08 13:27:19 +08:00
|
|
|
|
'code' => 0,
|
2017-05-15 17:36:27 +08:00
|
|
|
|
);
|
2016-11-08 13:27:19 +08:00
|
|
|
|
|
|
|
|
|
} catch (Exception $e) {
|
2017-05-15 17:36:27 +08:00
|
|
|
|
$response = array(
|
2016-11-08 13:27:19 +08:00
|
|
|
|
'code' => 100,
|
2017-05-15 17:36:27 +08:00
|
|
|
|
'data' => $e->getMessage(),
|
|
|
|
|
);
|
2016-11-08 13:27:19 +08:00
|
|
|
|
}
|
2017-05-15 17:36:27 +08:00
|
|
|
|
|
2018-01-25 18:55:06 +08:00
|
|
|
|
$this->response->throwJson($response);
|
2016-11-08 13:27:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function checkAuth()
|
|
|
|
|
{
|
2016-11-20 16:57:01 +08:00
|
|
|
|
if (!$this->access->isAdmin()) {
|
2016-11-08 13:27:19 +08:00
|
|
|
|
throw new Exception('Access Denied');
|
|
|
|
|
}
|
2016-03-28 14:45:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|