UpgradeNoticeBehavior.class.php 5.3 KB

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