You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cf-sdk/ThinkPHP/Library/Behavior/UpgradeNoticeBehavior.class...

118 lines
5.2 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: luofei614<www.3g4k.com>
// +----------------------------------------------------------------------
namespace Behavior;
/**
* 升级短信通知, 如果有ThinkPHP新版升级或者重要的更新会发送短信通知你。
* 需要使用SAE的短信服务。请先找一个SAE的应用开通短信服务。
* 使用步骤如下:
* 1在项目的Conf目录下建立tags.php配置文件内容如下
* <code>
* <?php
* return array(
* 'app_init' => array('UpgradeNotice')
* );
* </code>
*
* 2将此文件放在应用的Lib/Behavior文件夹下。
*注在SAE上面使用时以上两步可以省略
* 3在config.php中配置
* 'UPGRADE_NOTICE_ON'=>true,//开启短信升级提醒功能
* 'UPGRADE_NOTICE_AKEY'=>'your akey',//SAE应用的AKEY如果在SAE上使用可以不填
* 'UPGRADE_NOTICE_SKEY'=>'your skey',//SAE应用的SKEY如果在SAE上使用可以不填
*'UPGRADE_NOTICE_MOBILE'=>'136456789',//接受短信的手机号
*'UPGRADE_NOTICE_CHECK_INTERVAL' => 604800,//检测频率,单位秒,默认是一周
*'UPGRADE_CURRENT_VERSION'=>'0',//升级后的版本号,会在短信中告诉你填写什么
*UPGRADE_NOTICE_DEBUG=>true, //调试默认如果为trueUPGRADE_NOTICE_CHECK_INTERVAL配置不起作用每次都会进行版本检查此时用于调试调试完毕后请设置次配置为false
*
*/
class UpgradeNoticeBehavior {
protected $header_ = '';
protected $httpCode_;
protected $httpDesc_;
protected $accesskey_;
protected $secretkey_;
public function run(&$params) {
if (C('UPGRADE_NOTICE_ON') && (!S('think_upgrade_interval') || C('UPGRADE_NOTICE_DEBUG'))) {
if(IS_SAE && C('UPGRADE_NOTICE_QUEUE') && !isset($_POST['think_upgrade_queque'])){
$queue=new SaeTaskQueue(C('UPGRADE_NOTICE_QUEUE'));
$queue->addTask('http://'.$_SERVER['HTTP_HOST'].__APP__,'think_upgrade_queque=1');
if(!$queue->push()){
trace('升级提醒队列执行失败,错误原因:'.$queue->errmsg(), '升级通知出错', 'NOTIC', true);
}
return ;
}
$akey = C('UPGRADE_NOTICE_AKEY',null,'');
$skey = C('UPGRADE_NOTICE_SKEY',null,'');
$this->accesskey_ = $akey ? $akey : (defined('SAE_ACCESSKEY') ? SAE_ACCESSKEY : '');
$this->secretkey_ = $skey ? $skey : (defined('SAE_SECRETKEY') ? SAE_SECRETKEY : '');
$current_version = C('UPGRADE_CURRENT_VERSION',null,0);
//读取接口
$info = $this->send('http://sinaclouds.sinaapp.com/thinkapi/upgrade.php?v=' . $current_version);
if ($info['version'] != $current_version) {
if($this->send_sms($info['msg'])) trace($info['msg'], '升级通知成功', 'NOTIC', true); //发送升级短信
}
S('think_upgrade_interval', true, C('UPGRADE_NOTICE_CHECK_INTERVAL',null,604800));
}
}
private function send_sms($msg) {
$timestamp=time();
$url = 'http://inno.smsinter.sina.com.cn/sae_sms_service/sendsms.php'; //发送短信的接口地址
$content = "FetchUrl" . $url . "TimeStamp" . $timestamp . "AccessKey" . $this->accesskey_;
$signature = (base64_encode(hash_hmac('sha256', $content, $this->secretkey_, true)));
$headers = array(
"FetchUrl: $url",
"AccessKey: ".$this->accesskey_,
"TimeStamp: " . $timestamp,
"Signature: $signature"
);
$data = array(
'mobile' => C('UPGRADE_NOTICE_MOBILE',null,'') ,
'msg' => $msg,
'encoding' => 'UTF-8'
);
if(!$ret = $this->send('http://g.apibus.io', $data, $headers)){
return false;
}
if (isset($ret['ApiBusError'])) {
trace('errno:' . $ret['ApiBusError']['errcode'] . ',errmsg:' . $ret['ApiBusError']['errdesc'], '升级通知出错', 'NOTIC', true);
return false;
}
return true;
}
private function send($url, $params = array() , $headers = array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if (!empty($params)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$txt = curl_exec($ch);
if (curl_errno($ch)) {
trace(curl_error($ch) , '升级通知出错', 'NOTIC', true);
return false;
}
curl_close($ch);
$ret = json_decode($txt, true);
if (!$ret) {
trace('接口[' . $url . ']返回格式不正确', '升级通知出错', 'NOTIC', true);
return false;
}
return $ret;
}
}