123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace components\components\messageQueue;
- use yii\base\Component;
- use yii\base\InvalidParamException;
- /**
- * Signature
- * 签名
- * ---------
- * @author Verdient。
- * @version 1.0.0
- */
- class Signature extends Component {
- /**
- * @var public String $key
- * 签名密钥
- * -----------------------
- * @author Verdient。
- */
- public $key;
- /**
- * @var public String $verb
- * 访问方式
- * ------------------------
- * @author Verdient。
- */
- public $verb;
- /**
- * @var public String $contentType
- * 消息体类型
- * -------------------------------
- * @author Verdient。
- */
- public $contentType = 'application/json';
- /**
- * @var public String $signatureMethod
- * 签名方法
- * -----------------------------------
- * @author Verdient。
- */
- public $signatureMethod = 'MD5';
- /**
- * @var public String $signatureVersion
- * 签名版本
- * ------------------------------------
- * @author Verdient。
- */
- public $signatureVersion = '1.0';
- /**
- * @var public String $_content
- * 消息体
- * ----------------------------
- * @author Verdient。
- */
- public $_content = [];
- /**
- * @var protected String $_contentMd5
- * 消息体MD5值
- * ----------------------------------
- * @author Verdient。
- */
- protected $_contentMd5 = null;
- /**
- * @var protected String $_date
- * 日期
- * ----------------------------
- * @author Verdient。
- */
- protected $_date;
- /**
- * @var protected String $_signature
- * 签名
- * ---------------------------------
- * @author Verdient。
- */
- protected $_signature;
- /**
- * setContent(Array $content)
- * 设置消息体
- * --------------------------
- * @param Array $content 消息体
- * ----------------------------
- * @author Verdient。
- */
- public function setContent(Array $content){
- static::_prepareContent($content);
- $this->_content = $content;
- }
- /**
- * getContent()
- * 获取消息体
- * ------------
- * @return Array
- * @author Verdient。
- */
- public function getContent(){
- return $this->_content;
- }
- /**
- * setDate(Mixed $date)
- * 设置日期
- * --------------------
- * @param Mixed $date 日期
- * -----------------------
- * @author Verdient。
- */
- public function setDate($date){
- if(is_numeric($date) && ctype_digit($str) && $str <= 2147483647){
- $this->_date = gmdate('D, d M Y H:i:s \G\M\T', $date);
- }else if(is_string($date)){
- $this->_date = gmdate('D, d M Y H:i:s \G\M\T', strtotime($date));
- }else{
- throw new InvalidParamException('Data must be a timestamp or date string');
- }
- }
- /**
- * getDate()
- * 获取日期
- * ---------
- * @return String
- * @author Verdient。
- */
- public function getDate(){
- if(!$this->_date){
- $this->_date = gmdate('D, d M Y H:i:s \G\M\T');
- }
- return $this->_date;
- }
- /**
- * getContentMd5()
- * 获取消息体MD5值
- * ---------------
- * @return String
- * @author Verdient。
- */
- public function getContentMd5(){
- if(!$this->_contentMd5 !== null){
- if(!is_array($this->content)){
- throw new InvalidParamException('Content must be an Array');
- }
- $this->_contentMd5 = empty($this->content) ? '' : md5(json_encode($this->content));
- }
- return $this->_contentMd5;
- }
- /**
- * getSignature()
- * 获取签名
- * --------------
- * @return String
- * @author Verdient。
- */
- public function getSignature(){
- if(!$this->_signature){
- if(!$this->key){
- throw new InvalidParamException('Signature key must be set');
- }
- $this->_signature = md5(base64_encode(hash_hmac("sha1", $this->_buildSignatureString(), $this->key . '&', true)));
- }
- return $this->_signature;
- }
- /**
- * _buildSignatureString()
- * 构建待签名的字符串
- * -----------------------
- * @return String
- * @author Verdient。
- */
- protected function _buildSignatureString(){
- return strtoupper($this->verb) .
- "\n\n" .
- $this->contentMd5 .
- "\n" .
- $this->contentType .
- "\n" .
- $this->date.
- "\n" .
- $this->signatureMethod .
- "\n" .
- $this->signatureVersion .
- "\n\n" .
- $this->key .
- "\n";
- }
- /**
- * _prepareContent(Array &$content)
- * 准备消息体
- * --------------------------------
- * @param Array &$content 消息体
- * ----------------------------
- * @author Verdient。
- */
- protected static function _prepareContent(&$content){
- ksort($content);
- foreach($content as $key => $value){
- if(empty($value)){
- unset($content[$key]);
- }else if(is_array($value)){
- static::_prepareContent($content[$key]);
- }
- }
- }
- }
|