|
1 |
| -<?php |
2 |
| -// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] |
3 |
| -// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved. |
4 |
| -// +---------------------------------------------------------------------- |
5 |
| -// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
6 |
| -// +---------------------------------------------------------------------- |
7 |
| -// | Author: luofei614<www.3g4k.com> |
8 |
| -// +---------------------------------------------------------------------- |
9 |
| -defined('THINK_PATH') or exit(); |
10 |
| -/** |
11 |
| - * 升级短信通知, 如果有ThinkPHP新版升级,或者重要的更新,会发送短信通知你。 |
12 |
| - * 需要使用SAE的短信服务。请先找一个SAE的应用开通短信服务。 |
13 |
| - * 使用步骤如下: |
14 |
| - * 1,在项目的Conf目录下建立tags.php配置文件,内容如下: |
15 |
| - * <code> |
16 |
| - * <?php |
17 |
| - * return array( |
18 |
| - * 'app_init' => array('UpgradeNotice') |
19 |
| - * ); |
20 |
| - * </code> |
21 |
| - * |
22 |
| - * 2,将此文件放在项目的Lib/Behavior文件夹下。 |
23 |
| - *注:在SAE上面使用时,以上两步可以省略 |
24 |
| - * 3,在config.php中配置: |
25 |
| - * 'UPGRADE_NOTICE_ON'=>true,//开启短信升级提醒功能 |
26 |
| - * 'UPGRADE_NOTICE_AKEY'=>'your akey',//SAE应用的AKEY,如果在SAE上使用可以不填 |
27 |
| - * 'UPGRADE_NOTICE_SKEY'=>'your skey',//SAE应用的SKEY,如果在SAE上使用可以不填 |
28 |
| - *'UPGRADE_NOTICE_MOBILE'=>'136456789',//接受短信的手机号 |
29 |
| - *'UPGRADE_NOTICE_CHECK_INTERVAL' => 604800,//检测频率,单位秒,默认是一周 |
30 |
| - *'UPGRADE_CURRENT_VERSION'=>'0',//升级后的版本号,会在短信中告诉你填写什么 |
31 |
| - *UPGRADE_NOTICE_DEBUG=>true, //调试默认,如果为true,UPGRADE_NOTICE_CHECK_INTERVAL配置不起作用,每次都会进行版本检查,此时用于调试,调试完毕后请设置次配置为false |
32 |
| - * |
33 |
| - */ |
34 |
| - |
35 |
| -class UpgradeNoticeBehavior extends Behavior { |
36 |
| - // 行为参数定义(默认值) 可在项目配置中覆盖 |
37 |
| - protected $options = array( |
38 |
| - 'UPGRADE_NOTICE_ON' => false, // 是否开启升级提醒 |
39 |
| - 'UPGRADE_NOTICE_DEBUG'=>false, |
40 |
| - 'UPGRADE_NOTICE_AKEY' => '', //SAE应用的AKEY |
41 |
| - 'UPGRADE_NOTICE_SKEY' => '', //SAE应用的SKEY |
42 |
| - 'UPGRADE_NOTICE_MOBILE' => '', //接受短信的手机号 |
43 |
| - 'UPGRADE_CURRENT_VERSION'=>'0', |
44 |
| - 'UPGRADE_NOTICE_CHECK_INTERVAL' => 604800, //检测频率,单位秒,默认是一周 |
45 |
| - ); |
46 |
| - protected $header_ = ''; |
47 |
| - protected $httpCode_; |
48 |
| - protected $httpDesc_; |
49 |
| - protected $accesskey_; |
50 |
| - protected $secretkey_; |
51 |
| - public function run(&$params) { |
52 |
| - if (C('UPGRADE_NOTICE_ON') && (!S('think_upgrade_interval') || C('UPGRADE_NOTICE_DEBUG'))) { |
53 |
| - $akey = C('UPGRADE_NOTICE_AKEY'); |
54 |
| - $skey = C('UPGRADE_NOTICE_SKEY'); |
55 |
| - $this->accesskey_ = $akey ? $akey : (defined('SAE_ACCESSKEY') ? SAE_ACCESSKEY : ''); |
56 |
| - $this->secretkey_ = $skey ? $skey : (defined('SAE_SECRETKEY') ? SAE_SECRETKEY : ''); |
57 |
| - $current_version = C('UPGRADE_CURRENT_VERSION'); |
58 |
| - //读取接口 |
59 |
| - $info = $this->send('http://sinaclouds.sinaapp.com/thinkapi/upgrade.php?v=' . $current_version); |
60 |
| - if ($info['version'] != $current_version) { |
61 |
| - if($this->send_sms($info['msg'])) trace($info['msg'], '升级通知成功', 'DEBUG', true); //发送升级短信 |
62 |
| - } |
63 |
| - S('think_upgrade_interval', true, C('UPGRADE_NOTICE_CHECK_INTERVAL')); |
64 |
| - } |
65 |
| - } |
66 |
| - private function send_sms($msg) { |
67 |
| - $timestamp=time(); |
68 |
| - $url = 'http://inno.smsinter.sina.com.cn/sae_sms_service/sendsms.php'; //发送短信的接口地址 |
69 |
| - $content = "FetchUrl" . $url . "TimeStamp" . $timestamp . "AccessKey" . $this->accesskey_; |
70 |
| - $signature = (base64_encode(hash_hmac('sha256', $content, $this->secretkey_, true))); |
71 |
| - $headers = array( |
72 |
| - "FetchUrl: $url", |
73 |
| - "AccessKey: ".$this->accesskey_, |
74 |
| - "TimeStamp: " . $timestamp, |
75 |
| - "Signature: $signature" |
76 |
| - ); |
77 |
| - $data = array( |
78 |
| - 'mobile' => C('UPGRADE_NOTICE_MOBILE') , |
79 |
| - 'msg' => $msg, |
80 |
| - 'encoding' => 'UTF-8' |
81 |
| - ); |
82 |
| - if(!$ret = $this->send('http://g.apibus.io', $data, $headers)){ |
83 |
| - return false; |
84 |
| - } |
85 |
| - if (isset($ret['ApiBusError'])) { |
86 |
| - trace('errno:' . $ret['ApiBusError']['errcode'] . ',errmsg:' . $ret['ApiBusError']['errdesc'], '升级通知出错', 'DEBUG', true); |
87 |
| - |
88 |
| - return false; |
89 |
| - } |
90 |
| - |
91 |
| - return true; |
92 |
| - } |
93 |
| - private function send($url, $params = array() , $headers = array()) { |
94 |
| - $ch = curl_init(); |
95 |
| - curl_setopt($ch, CURLOPT_URL, $url); |
96 |
| - if (!empty($params)) { |
97 |
| - curl_setopt($ch, CURLOPT_POST, true); |
98 |
| - curl_setopt($ch, CURLOPT_POSTFIELDS, $params); |
99 |
| - } |
100 |
| - if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
101 |
| - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
102 |
| - $txt = curl_exec($ch); |
103 |
| - if (curl_errno($ch)) { |
104 |
| - trace(curl_error($ch) , '升级通知出错', 'DEBUG', true); |
105 |
| - |
106 |
| - return false; |
107 |
| - } |
108 |
| - curl_close($ch); |
109 |
| - $ret = json_decode($txt, true); |
110 |
| - if (!$ret) { |
111 |
| - trace('接口[' . $url . ']返回格式不正确', '升级通知出错', 'DEBUG', true); |
112 |
| - |
113 |
| - return false; |
114 |
| - } |
115 |
| - |
116 |
| - return $ret; |
117 |
| - } |
118 |
| -} |
| 1 | +<?php |
| 2 | +// | ThinkPHP [ WE CAN DO IT JUST THINK IT ] |
| 3 | +// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved. |
| 4 | +// +---------------------------------------------------------------------- |
| 5 | +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
| 6 | +// +---------------------------------------------------------------------- |
| 7 | +// | Author: luofei614<www.3g4k.com> |
| 8 | +// +---------------------------------------------------------------------- |
| 9 | +defined('THINK_PATH') or exit(); |
| 10 | +/** |
| 11 | + * 升级短信通知, 如果有ThinkPHP新版升级,或者重要的更新,会发送短信通知你。 |
| 12 | + * 需要使用SAE的短信服务。请先找一个SAE的应用开通短信服务。 |
| 13 | + * 使用步骤如下: |
| 14 | + * 1,在项目的Conf目录下建立tags.php配置文件,内容如下: |
| 15 | + * <code> |
| 16 | + * <?php |
| 17 | + * return array( |
| 18 | + * 'app_init' => array('UpgradeNotice') |
| 19 | + * ); |
| 20 | + * </code> |
| 21 | + * |
| 22 | + * 2,将此文件放在项目的Lib/Behavior文件夹下。 |
| 23 | + *注:在SAE上面使用时,以上两步可以省略 |
| 24 | + * 3,在config.php中配置: |
| 25 | + * 'UPGRADE_NOTICE_ON'=>true,//开启短信升级提醒功能 |
| 26 | + * 'UPGRADE_NOTICE_AKEY'=>'your akey',//SAE应用的AKEY,如果在SAE上使用可以不填 |
| 27 | + * 'UPGRADE_NOTICE_SKEY'=>'your skey',//SAE应用的SKEY,如果在SAE上使用可以不填 |
| 28 | + *'UPGRADE_NOTICE_MOBILE'=>'136456789',//接受短信的手机号 |
| 29 | + *'UPGRADE_NOTICE_CHECK_INTERVAL' => 604800,//检测频率,单位秒,默认是一周 |
| 30 | + *'UPGRADE_CURRENT_VERSION'=>'0',//升级后的版本号,会在短信中告诉你填写什么 |
| 31 | + *UPGRADE_NOTICE_DEBUG=>true, //调试默认,如果为true,UPGRADE_NOTICE_CHECK_INTERVAL配置不起作用,每次都会进行版本检查,此时用于调试,调试完毕后请设置次配置为false |
| 32 | + * |
| 33 | + */ |
| 34 | + |
| 35 | +class UpgradeNoticeBehavior extends Behavior { |
| 36 | + // 行为参数定义(默认值) 可在项目配置中覆盖 |
| 37 | + protected $options = array( |
| 38 | + 'UPGRADE_NOTICE_ON' => false, // 是否开启升级提醒 |
| 39 | + 'UPGRADE_NOTICE_DEBUG'=>false, |
| 40 | + 'UPGRADE_NOTICE_QUEUE'=>'',//队列名称, 在SAE平台上设置 |
| 41 | + 'UPGRADE_NOTICE_AKEY' => '', //SAE应用的AKEY |
| 42 | + 'UPGRADE_NOTICE_SKEY' => '', //SAE应用的SKEY |
| 43 | + 'UPGRADE_NOTICE_MOBILE' => '', //接受短信的手机号 |
| 44 | + 'UPGRADE_CURRENT_VERSION'=>'0', |
| 45 | + 'UPGRADE_NOTICE_CHECK_INTERVAL' => 604800, //检测频率,单位秒,默认是一周 |
| 46 | + ); |
| 47 | + protected $header_ = ''; |
| 48 | + protected $httpCode_; |
| 49 | + protected $httpDesc_; |
| 50 | + protected $accesskey_; |
| 51 | + protected $secretkey_; |
| 52 | + public function run(&$params) { |
| 53 | + if (C('UPGRADE_NOTICE_ON') && (!S('think_upgrade_interval') || C('UPGRADE_NOTICE_DEBUG'))) { |
| 54 | + if(IS_SAE && C('UPGRADE_NOTICE_QUEUE') && !isset($_POST['think_upgrade_queque'])){ |
| 55 | + $queue=new SaeTaskQueue(C('UPGRADE_NOTICE_QUEUE')); |
| 56 | + $queue->addTask('http://'.$_SERVER['HTTP_HOST'].__APP__,'think_upgrade_queque=1'); |
| 57 | + if(!$queue->push()){ |
| 58 | + trace('升级提醒队列执行失败,错误原因:'.$queue->errmsg(), '升级通知出错', 'NOTIC', true); |
| 59 | + } |
| 60 | + return ; |
| 61 | + } |
| 62 | + $akey = C('UPGRADE_NOTICE_AKEY'); |
| 63 | + $skey = C('UPGRADE_NOTICE_SKEY'); |
| 64 | + $this->accesskey_ = $akey ? $akey : (defined('SAE_ACCESSKEY') ? SAE_ACCESSKEY : ''); |
| 65 | + $this->secretkey_ = $skey ? $skey : (defined('SAE_SECRETKEY') ? SAE_SECRETKEY : ''); |
| 66 | + $current_version = C('UPGRADE_CURRENT_VERSION'); |
| 67 | + //读取接口 |
| 68 | + $info = $this->send('http://sinaclouds.sinaapp.com/thinkapi/upgrade.php?v=' . $current_version); |
| 69 | + if ($info['version'] != $current_version) { |
| 70 | + if($this->send_sms($info['msg'])) trace($info['msg'], '升级通知成功', 'NOTIC', true); //发送升级短信 |
| 71 | + } |
| 72 | + S('think_upgrade_interval', true, C('UPGRADE_NOTICE_CHECK_INTERVAL')); |
| 73 | + } |
| 74 | + } |
| 75 | + private function send_sms($msg) { |
| 76 | + $timestamp=time(); |
| 77 | + $url = 'http://inno.smsinter.sina.com.cn/sae_sms_service/sendsms.php'; //发送短信的接口地址 |
| 78 | + $content = "FetchUrl" . $url . "TimeStamp" . $timestamp . "AccessKey" . $this->accesskey_; |
| 79 | + $signature = (base64_encode(hash_hmac('sha256', $content, $this->secretkey_, true))); |
| 80 | + $headers = array( |
| 81 | + "FetchUrl: $url", |
| 82 | + "AccessKey: ".$this->accesskey_, |
| 83 | + "TimeStamp: " . $timestamp, |
| 84 | + "Signature: $signature" |
| 85 | + ); |
| 86 | + $data = array( |
| 87 | + 'mobile' => C('UPGRADE_NOTICE_MOBILE') , |
| 88 | + 'msg' => $msg, |
| 89 | + 'encoding' => 'UTF-8' |
| 90 | + ); |
| 91 | + if(!$ret = $this->send('http://g.apibus.io', $data, $headers)){ |
| 92 | + return false; |
| 93 | + } |
| 94 | + if (isset($ret['ApiBusError'])) { |
| 95 | + trace('errno:' . $ret['ApiBusError']['errcode'] . ',errmsg:' . $ret['ApiBusError']['errdesc'], '升级通知出错', 'NOTIC', true); |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + private function send($url, $params = array() , $headers = array()) { |
| 103 | + $ch = curl_init(); |
| 104 | + curl_setopt($ch, CURLOPT_URL, $url); |
| 105 | + if (!empty($params)) { |
| 106 | + curl_setopt($ch, CURLOPT_POST, true); |
| 107 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); |
| 108 | + } |
| 109 | + if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 110 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 111 | + $txt = curl_exec($ch); |
| 112 | + if (curl_errno($ch)) { |
| 113 | + trace(curl_error($ch) , '升级通知出错', 'NOTIC', true); |
| 114 | + |
| 115 | + return false; |
| 116 | + } |
| 117 | + curl_close($ch); |
| 118 | + $ret = json_decode($txt, true); |
| 119 | + if (!$ret) { |
| 120 | + trace('接口[' . $url . ']返回格式不正确', '升级通知出错', 'NOTIC', true); |
| 121 | + |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return $ret; |
| 126 | + } |
| 127 | +} |
0 commit comments