UpgradeNoticeBehavior.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace Behavior;
  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. class UpgradeNoticeBehavior {
  35. protected $header_ = '';
  36. protected $httpCode_;
  37. protected $httpDesc_;
  38. protected $accesskey_;
  39. protected $secretkey_;
  40. public function run(&$params) {
  41. if (C('UPGRADE_NOTICE_ON') && (!S('think_upgrade_interval') || C('UPGRADE_NOTICE_DEBUG'))) {
  42. if(IS_SAE && C('UPGRADE_NOTICE_QUEUE') && !isset($_POST['think_upgrade_queque'])){
  43. $queue=new SaeTaskQueue(C('UPGRADE_NOTICE_QUEUE'));
  44. $queue->addTask('http://'.$_SERVER['HTTP_HOST'].__APP__,'think_upgrade_queque=1');
  45. if(!$queue->push()){
  46. trace('升级提醒队列执行失败,错误原因:'.$queue->errmsg(), '升级通知出错', 'NOTIC', true);
  47. }
  48. return ;
  49. }
  50. $akey = C('UPGRADE_NOTICE_AKEY',null,'');
  51. $skey = C('UPGRADE_NOTICE_SKEY',null,'');
  52. $this->accesskey_ = $akey ? $akey : (defined('SAE_ACCESSKEY') ? SAE_ACCESSKEY : '');
  53. $this->secretkey_ = $skey ? $skey : (defined('SAE_SECRETKEY') ? SAE_SECRETKEY : '');
  54. $current_version = C('UPGRADE_CURRENT_VERSION',null,0);
  55. //读取接口
  56. $info = $this->send('http://sinaclouds.sinaapp.com/thinkapi/upgrade.php?v=' . $current_version);
  57. if ($info['version'] != $current_version) {
  58. if($this->send_sms($info['msg'])) trace($info['msg'], '升级通知成功', 'NOTIC', true); //发送升级短信
  59. }
  60. S('think_upgrade_interval', true, C('UPGRADE_NOTICE_CHECK_INTERVAL',null,604800));
  61. }
  62. }
  63. private function send_sms($msg) {
  64. $timestamp=time();
  65. $url = 'http://inno.smsinter.sina.com.cn/sae_sms_service/sendsms.php'; //发送短信的接口地址
  66. $content = "FetchUrl" . $url . "TimeStamp" . $timestamp . "AccessKey" . $this->accesskey_;
  67. $signature = (base64_encode(hash_hmac('sha256', $content, $this->secretkey_, true)));
  68. $headers = array(
  69. "FetchUrl: $url",
  70. "AccessKey: ".$this->accesskey_,
  71. "TimeStamp: " . $timestamp,
  72. "Signature: $signature"
  73. );
  74. $data = array(
  75. 'mobile' => C('UPGRADE_NOTICE_MOBILE',null,'') ,
  76. 'msg' => $msg,
  77. 'encoding' => 'UTF-8'
  78. );
  79. if(!$ret = $this->send('http://g.apibus.io', $data, $headers)){
  80. return false;
  81. }
  82. if (isset($ret['ApiBusError'])) {
  83. trace('errno:' . $ret['ApiBusError']['errcode'] . ',errmsg:' . $ret['ApiBusError']['errdesc'], '升级通知出错', 'NOTIC', true);
  84. return false;
  85. }
  86. return true;
  87. }
  88. private function send($url, $params = array() , $headers = array()) {
  89. $ch = curl_init();
  90. curl_setopt($ch, CURLOPT_URL, $url);
  91. if (!empty($params)) {
  92. curl_setopt($ch, CURLOPT_POST, true);
  93. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  94. }
  95. if (!empty($headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  97. $txt = curl_exec($ch);
  98. if (curl_errno($ch)) {
  99. trace(curl_error($ch) , '升级通知出错', 'NOTIC', true);
  100. return false;
  101. }
  102. curl_close($ch);
  103. $ret = json_decode($txt, true);
  104. if (!$ret) {
  105. trace('接口[' . $url . ']返回格式不正确', '升级通知出错', 'NOTIC', true);
  106. return false;
  107. }
  108. return $ret;
  109. }
  110. }