<?php class FileFlag_Api { private $flag='fileflag';//唯一標識 private $method='file_request';//方法名稱 private $type='json';//數據格式 private $charset='utf-8';//數據編碼 private $ver=1;//版本 private $token='tjiePjfjvnyGUuyMFEXVPmsewokCauuZ';//密鑰 private $post_data=array();//全部數據 private $sign=null;//簽名 private $timestamp=null;//時間戳 private $data='';//實際數據 //返回信息數組0是返回出錯的信息,1返回處理成功的信息 private $result=array( 0=>array('code'=>'','info'=>'FAILED','msg'=>'','data'=>''), 1=>array('code'=>'','info'=>'SUCCESS','msg'=>'','data'=>'') ); //返回信息數組$this->result的鍵名 private $key=0;//$this->result (key) //構造函數 public function __construct() { } //設置簽名 private function set_sign($sign=array()) { $return_sign=''; if(!is_array($sign)) { return null; } else { ksort($sign,SORT_STRING); foreach($sign as $key=>$value) { if( $value != null && strtolower($key) != 'sign' ) { if( get_magic_quotes_gpc() ) { $value=stripslashes($value); $return_sign.=$key.(is_array($value)?$this->set_sign($value):$value); } else { $return_sign.=$key.(is_array($value)?$this->set_sign($value):$value); } //$return_sign.=$key.(is_array($value)?$this->set_sign($value):$value); } else { } } return $return_sign; } } //獲取簽名 private function get_sign($sign) { if($sign) { return strtoupper(md5(strtoupper(md5($sign)).$this->token)); } else { return null; } } //檢查字段是否符號要求 private function check_sign($sign=array()) { $sign_str=$this->set_sign($sign); $signs=$this->get_sign($sign_str); if($this->sign==$signs) { return true; } else { return false; } } //檢查時間戳 private function check_timestamp() { if($this->timestamp) { if(($this->timestamp+60)<time()) { return true; } else { return false; } } } //檢查簽名等必須字段是否符號要求 private function check_post($post) { if(!empty($post)) { $type=$post['type']; $flags=$post['flag']; $timestamp=$post['timestamp']; $this->data=$post['data']; $sign=$post['sign']; $method=$post['method']; $ver=$post['ver']; $flag=true; //var_dump($this->data); //exit; if($type!=$this->type) { $flag=false; $this->key=0; $this->result[$this->key]['code']='type'; $this->result[$this->key]['msg']='Response:type field Invalid!'; } elseif($flags!=$this->flag) { $this->key=0; $flag=false; $this->result[$this->key]['code']='flag'; $this->result[$this->key]['msg']='Response:flag field Invalid!'; } elseif($sign!=$this->sign) { $this->key=0; $flag=false; $this->result[$this->key]['code']='sign'; $this->result[$this->key]['msg']='Response:sign field Invalid!'; } elseif($method!=$this->method) { $this->key=0; $flag=false; $this->result[$this->key]['code']='method'; $this->result[$this->key]['msg']='Response:method field Invalid!'; } elseif($ver!=$this->ver) { $this->key=0; $flag=false; $this->result[$this->key]['code']='ver'; $this->result[$this->key]['msg']='Response:ver field Invalid!'; } else { $this->key=1; } /* else { $this->key=1; } */ return $flag; } else { return false; } } //檢查轉送過來的三維數組是否符合要求 private function data_check() { $field_array=array('order_sn','code','is_upfile','fileinfo'); if(empty($this->post_data)) { $this->key=0; $this->result[$this->key]['code']='data'; $this->result[$this->key]['msg']='Response:data is empty!'; return false; } elseif(!empty($this->post_data) && is_array($this->post_data)) { foreach($this->post_data as $key => $value) { if(in_array(strtolower($key),$field_array)) { if(is_array($value) && !empty($value)) { foreach($value as $k => $v) { if(is_array($v) && !empty($v)) { foreach($v as $ky => $val) { if(in_array(strtolower($ky),$field_array)) { if(empty($val)) { /* $this->key=0; $this->result[$this->key]['code']='data'; $this->result[$this->key]['msg']='Response:'.$ky.' is empty!'; return false; */ } } } } } } elseif(empty($value) && !is_array($value)) { $this->key=0; $this->result[$this->key]['code']='data'; $this->result[$this->key]['msg']='Response:'.$key.' is empty!'; return false; } } } } else { return true; } } //過濾轉送過來的數據 private function PostFilter($post) { $return_post=array(); if(is_array($post)) { foreach( $post as $key => $value ) { if(is_array($value) && !empty($value)) { $value=$this->PostFilter($value); } else { if(!get_magic_quotes_gpc()) { $value=addslashes($value); } $value=strip_tags($value); } $return_post[$key]=$value; } } /* else { if(!get_magic_quotes_gpc()) { $return_post[]=addslashes($post); } $post[]=strip_tags($post); } */ return $return_post; } //$post是三維數組 public function DoResponse($post=array()) { $sign=$this->set_sign($post); $this->sign=$this->get_sign($sign); $check=$this->check_post($post); if($check) { $this->data=json_decode(trim(stripslashes(urldecode($this->data)),chr(239).chr(187).chr(191)),true); $post=$this->PostFilter($this->data); $this->post_data=$post; if(!$this->data_check()) { return false; } else { return true; } } else { return false; } } //輸出結果信息 public function display() { $this->result[$this->key]['data']=$this->data; $json=json_encode($this->result[$this->key]); $rs=new stdClass(); foreach($this->result[$this->key] as $k => $v) { $rs->$k=$v;//$this->result[$this->key][$k]; } $rs=json_encode($rs); echo $rs; //echo $json; } //獲取轉送過來的處理過的數據 public function get_request_data() { return $this->post_data; } } $fileflag=new FileFlag_Api(); $response=$fileflag->DoResponse($post); $fileflag->display(); ?>
對外開發接口類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.