diff --git a/.travis.yml b/.travis.yml old mode 100755 new mode 100644 diff --git a/LICENSE b/LICENSE old mode 100755 new mode 100644 diff --git a/addons/alisms/Alisms.php b/addons/alisms/Alisms.php new file mode 100644 index 00000000..44dd9601 --- /dev/null +++ b/addons/alisms/Alisms.php @@ -0,0 +1,72 @@ +mobile($params->mobile) + ->template($config['template'][$params->event]) + ->param(['code' => $params->code]) + ->send(); + return $result; + } + + /** + * 短信发送通知 + * @param array $params + * @return boolean + */ + public function smsNotice(&$params) + { + $alisms = library\Alisms::instance(); + $result = $alisms->mobile($params['mobile']) + ->template($params['template']) + ->param($params) + ->send(); + return $result; + } + + /** + * 检测验证是否正确 + * @param Sms $params + * @return boolean + */ + public function smsCheck(&$params) + { + return TRUE; + } + +} diff --git a/addons/alisms/config.php b/addons/alisms/config.php new file mode 100644 index 00000000..6e69ecd9 --- /dev/null +++ b/addons/alisms/config.php @@ -0,0 +1,70 @@ + + array ( + 'name' => 'key', + 'title' => '应用key', + 'type' => 'string', + 'content' => + array ( + ), + 'value' => 'LTAIrWkYK52SWqlA', + 'rule' => 'required', + 'msg' => '', + 'tip' => '', + 'ok' => '', + 'extend' => '', + ), + 1 => + array ( + 'name' => 'secret', + 'title' => '密钥secret', + 'type' => 'string', + 'content' => + array ( + ), + 'value' => 'mAoToIZsRvuzM1n1Z66uj5cQctawCX', + 'rule' => 'required', + 'msg' => '', + 'tip' => '', + 'ok' => '', + 'extend' => '', + ), + 2 => + array ( + 'name' => 'sign', + 'title' => '签名', + 'type' => 'string', + 'content' => + array ( + ), + 'value' => '吉泰隆', + 'rule' => 'required', + 'msg' => '', + 'tip' => '', + 'ok' => '', + 'extend' => '', + ), + 3 => + array ( + 'name' => 'template', + 'title' => '短信模板', + 'type' => 'array', + 'content' => + array ( + ), + 'value' => + array ( + 'register' => 'SMS_114000000', + 'resetpwd' => 'SMS_114000000', + 'changepwd' => 'SMS_114000000', + '短信验证码' => 'SMS_119078242', + ), + 'rule' => 'required', + 'msg' => '', + 'tip' => '', + 'ok' => '', + 'extend' => '', + ), +); diff --git a/addons/alisms/controller/Index.php b/addons/alisms/controller/Index.php new file mode 100644 index 00000000..46d6ea2a --- /dev/null +++ b/addons/alisms/controller/Index.php @@ -0,0 +1,49 @@ +view->fetch(); + } + + public function send() + { + $mobile = $this->request->post('mobile'); + $template = $this->request->post('template'); + $sign = $this->request->post('sign'); + $param = (array) json_decode($this->request->post('param')); + $alisms = new \addons\alisms\library\Alisms(); + $ret = $alisms->mobile($mobile) + ->template($template) + ->sign($sign) + ->param($param) + ->send(); + if ($ret) + { + $this->success("发送成功"); + } + else + { + $this->error("发送失败!失败原因:" . $alisms->getError()); + } + } + +} diff --git a/addons/alisms/info.ini b/addons/alisms/info.ini new file mode 100644 index 00000000..3b8e23b5 --- /dev/null +++ b/addons/alisms/info.ini @@ -0,0 +1,7 @@ +name = alisms +title = 阿里短信发送 +intro = 阿里短信发送插件 +author = Karson +website = http://www.fastadmin.net +version = 1.0.1 +state = 1 diff --git a/addons/alisms/install.sql b/addons/alisms/install.sql new file mode 100644 index 00000000..c99efa08 --- /dev/null +++ b/addons/alisms/install.sql @@ -0,0 +1,10 @@ + +CREATE TABLE IF NOT EXISTS `__PREFIX__mobile_code` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', + `type` varchar(30) NOT NULL DEFAULT '' COMMENT '类型', + `mobile` varchar(20) NOT NULL DEFAULT '' COMMENT '手机号', + `code` varchar(10) DEFAULT '' COMMENT '验证码', + `times` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '验证次数', + `createtime` int(10) unsigned DEFAULT '0' COMMENT '创建时间', + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='短信验证码表'; \ No newline at end of file diff --git a/addons/alisms/library/Alisms.php b/addons/alisms/library/Alisms.php new file mode 100644 index 00000000..1bfae92e --- /dev/null +++ b/addons/alisms/library/Alisms.php @@ -0,0 +1,178 @@ +config = array_merge($this->config, $config); + } + $this->config = array_merge($this->config, is_array($options) ? $options : []); + } + + /** + * 单例 + * @param array $options 参数 + * @return Alisms + */ + public static function instance($options = []) + { + if (is_null(self::$instance)) + { + self::$instance = new static($options); + } + + return self::$instance; + } + + /** + * 设置签名 + * @param string $sign + * @return Alisms + */ + public function sign($sign = '') + { + $this->_params['SignName'] = $sign; + return $this; + } + + /** + * 设置参数 + * @param array $param + * @return Alisms + */ + public function param(array $param = []) + { + foreach ($param as $k => &$v) + { + $v = (string) $v; + } + unset($v); + $this->_params['TemplateParam'] = json_encode($param); + return $this; + } + + /** + * 设置模板 + * @param string $code 短信模板 + * @return Alisms + */ + public function template($code = '') + { + $this->_params['TemplateCode'] = $code; + return $this; + } + + /** + * 接收手机 + * @param string $mobile 手机号码 + * @return Alisms + */ + public function mobile($mobile = '') + { + $this->_params['PhoneNumbers'] = $mobile; + return $this; + } + + /** + * 立即发送 + * @return boolean + */ + public function send() + { + $this->error = ''; + $params = $this->_params(); + $params['Signature'] = $this->_signed($params); + $response = $this->_curl($params); + if ($response !== FALSE) + { + $res = (array) json_decode($response, TRUE); + if (isset($res['Code']) && $res['Code'] == 'OK') + return TRUE; + $this->error = isset($res['Message']) ? $res['Message'] : 'InvalidResult'; + } + else + { + $this->error = 'InvalidResult'; + } + return FALSE; + } + + /** + * 获取错误信息 + * @return array + */ + public function getError() + { + return $this->error; + } + + private function _params() + { + return array_merge([ + 'AccessKeyId' => $this->config['key'], + 'SignName' => isset($this->config['sign']) ? $this->config['sign'] : '', + 'Action' => 'SendSms', + 'Format' => 'JSON', + 'Version' => '2017-05-25', + 'SignatureVersion' => '1.0', + 'SignatureMethod' => 'HMAC-SHA1', + 'SignatureNonce' => uniqid(), + 'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'), + ], $this->_params); + } + + private function percentEncode($string) + { + $string = urlencode($string); + $string = preg_replace('/\+/', '%20', $string); + $string = preg_replace('/\*/', '%2A', $string); + $string = preg_replace('/%7E/', '~', $string); + return $string; + } + + private function _signed($params) + { + $sign = $this->config['secret']; + ksort($params); + $canonicalizedQueryString = ''; + foreach ($params as $key => $value) + { + $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); + } + $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); + $signature = base64_encode(hash_hmac('sha1', $stringToSign, $sign . '&', true)); + return $signature; + } + + private function _curl($params) + { + $uri = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($ch, CURLOPT_URL, $uri); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36"); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + $reponse = curl_exec($ch); + curl_close($ch); + return $reponse; + } + +} diff --git a/addons/alisms/view/index/index.html b/addons/alisms/view/index/index.html new file mode 100644 index 00000000..8e3d7253 --- /dev/null +++ b/addons/alisms/view/index/index.html @@ -0,0 +1,63 @@ + + +
+ +