123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php namespace Maze\Security;
- use Maze\Http\Input;
- use Maze\Http\Output;
- use Maze\Config\Load as Config;
- use Maze\Config\Project;
- use Maze\Routing\Load;
- class Api
- {
- /**
- * prefix
- *
- * @var string
- */
- const PREFIX = 'api_';
-
- /**
- * time
- *
- * @var int
- */
- const TIME = 300;
- /**
- * path 定义api目录
- *
- * @var string
- */
- const PATH = 'api/';
-
- /**
- * default token
- *
- * @var string
- */
- static private $token = 'maze_api';
-
- /**
- * log
- *
- * @var string
- */
- static private $log;
-
- /**
- * state
- *
- * @var string
- */
- static public $state = false;
- /**
- * init
- * @param string $key
- * @param string $path
- *
- * @return string
- */
- static public function init($path = false)
- {
- if(!$path)
- {
- $path = MAZE_PROJECT_PATH;
- }
- $file = $path . self::PATH . 'main.php';
- if(is_file($file))
- {
- return include($file);
- }
- return array();
- }
-
- /**
- * check
- * @param string $key
- *
- * @return string
- */
- static public function check($key)
- {
- $config = self::init();
- if($config && isset($config[$key]))
- {
- self::$log = array();
- self::$log['add_site'] = $key;
-
- self::$state = true;
- # 验证当前api有效性
- $request = Input::prefix(self::PREFIX);
-
- if($request)
- {
- # 增加测试效果
- if(Input::get('maze_api_test') == 'test_yes')
- {
- print_r(self::get($request));die;
- }
-
- //self::result($request);
-
- foreach($request as $k => $v)
- {
- $k = str_replace(self::PREFIX, '', $k);
-
- if(!isset($config[$key]['request'][$k]))
- {
- Output::abert('api_param_exists', $k);
- }
-
- Input::set($k, $v);
-
- self::$log['add_request'][0][$k] = $v;
- }
-
- Config::$global['base']['api'] = true;
- }
- else
- {
- //Output::abert('api_param_exists', 'request');
- }
- }
- }
-
- /**
- * 得到数据结果
- *
- * @return mixed
- */
- static public function out($data)
- {
- if(self::$state == true)
- {
- if(!$data)
- {
- Output::abert('api_param_exists', 'response');
- }
-
- # 记录日志 暂时不做记录
- if(Project::load('manage'))
- {
- self::$log['add_response'][0] = $data;
- //Load::get('manage/api_log-insert', $param);
- }
-
- $result['msg'] = 'success';
- $result['status'] = 1;
- $result['data'] = $data;
- Output::result($result);
- }
- }
-
- /**
- * 得到解密结果
- *
- * @return mixed
- */
- static public function result($info)
- {
- $time = Input::get('time');
- # 验证时间是否超时,默认为5分钟
- if(time() - $time > self::TIME)
- {
- Output::abert('api_signature_exists');
- }
- $nonce = Input::get('nonce');
- $key = Input::get('signature');
- $signature = self::signature($time, $nonce, $info);
-
- if(Input::get('signature') != $signature)
- {
- Output::abert('api_signature_exists');
- }
- }
-
- /**
- * 得到签名及其数据
- *
- * @return mixed
- */
- static public function get($info)
- {
- $time = time();
- $nonce = self::nonce();
- $signature = self::signature($time, $nonce, $info);
-
- $info += array
- (
- 'time' => $time,
- 'nonce' => $nonce,
- 'signature' => $signature,
- //'token' => self::token(),
- //'status' => 1,
- //'msg' => 'success',
- );
-
- return $info;
- }
-
- /**
- * 获取signature
- *
- * @return mixed
- */
- static public function signature($time, $nonce, $info = array())
- {
- $info['token'] = self::token();
- $info['time'] = $time;
- $info['nonce'] = $nonce;
- ksort($info);
- $signature_string = '';
- foreach($info as $k => $v)
- {
- $signature_string .= $k . '=' . $v . '&';
- }
- $signature_string = substr($signature_string, 0, -1);
- return sha1($signature_string);
- }
-
- /**
- * 获取token
- *
- * @return mixed
- */
- static public function token()
- {
- self::$token = isset(Config::$global['base']['token']) ? Config::$global['base']['token'] : self::$token;
- return md5(self::$token);
- }
-
- /**
- * 获取nonce
- *
- * @return mixed
- */
- static public function nonce()
- {
- return substr(md5(microtime()), rand(10, 15));
- }
- }
|