Sms.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace My;
  3. /**
  4. * 短信驱动
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class Sms
  11. {
  12. private $expire_time;
  13. private $key_code;
  14. private $sign;
  15. public $error;
  16. /**
  17. * [__construct 构造方法]
  18. * @author Devil
  19. * @blog http://gong.gg/
  20. * @version 0.0.1
  21. * @datetime 2017-03-07T14:03:02+0800
  22. * @param [int] $param['interval_time'] [间隔时间(默认30)单位(秒)]
  23. * @param [int] $param['expire_time'] [到期时间(默认30)单位(秒)]
  24. * @param [string] $param['key_prefix'] [验证码种存储前缀key(默认 空)]
  25. */
  26. public function __construct($param = array())
  27. {
  28. $this->sign = '【'.MyC('common_sms_sign').'】';
  29. $this->interval_time = isset($param['interval_time']) ? intval($param['interval_time']) : 30;
  30. $this->expire_time = isset($param['expire_time']) ? intval($param['expire_time']) : 30;
  31. $this->key_code = isset($param['key_prefix']) ? trim($param['key_prefix']).'_sms_code' : '_sms_code';
  32. }
  33. /**
  34. * [SendText 发送文字短信]
  35. * @author Devil
  36. * @blog http://gong.gg/
  37. * @version 0.0.1
  38. * @datetime 2017-03-07T14:16:26+0800
  39. * @param [string] $mobile [手机号码]
  40. * @param [string] $content [短信内容]
  41. * @param [string] $code [验证码(存在则种验证码)]
  42. */
  43. public function SendText($mobile, $content, $code = '')
  44. {
  45. // 是否频繁操作
  46. if(!$this->IntervalTimeCheck())
  47. {
  48. $this->error = L('common_prevent_harassment_error');
  49. return false;
  50. }
  51. // 验证码替换
  52. if(!empty($code))
  53. {
  54. $content = str_replace('#code#', $code, $content);
  55. }
  56. // 请求发送
  57. $param = array(
  58. 'apikey' => MyC('common_sms_apikey'),
  59. 'text' => $this->sign.$content,
  60. 'mobile' => $mobile,
  61. );
  62. $result = json_decode(Fsockopen_Post('https://sms.yunpian.com/v2/sms/single_send.json', $param), true);
  63. // 错误信息
  64. if(isset($result['detail']))
  65. {
  66. $this->error = $result['detail'];
  67. }
  68. // 是否发送成功
  69. if(isset($result['code']) && $result['code'] == 0)
  70. {
  71. // 是否存在验证码
  72. if(!empty($code))
  73. {
  74. $this->KindofSession($code);
  75. }
  76. return true;
  77. }
  78. return false;
  79. }
  80. /**
  81. * [KindofSession 种验证码session]
  82. * @author Devil
  83. * @blog http://gong.gg/
  84. * @version 0.0.1
  85. * @datetime 2017-03-07T14:59:13+0800
  86. * @param [string] $code [验证码]
  87. */
  88. private function KindofSession($code)
  89. {
  90. $_SESSION[$this->key_code] = array(
  91. 'code' => $code,
  92. 'time' => time(),
  93. );
  94. }
  95. /**
  96. * [CheckExpire 验证码是否过期]
  97. * @author Devil
  98. * @blog http://gong.gg/
  99. * @version 0.0.1
  100. * @datetime 2017-03-05T19:02:26+0800
  101. * @return [boolean] [有效true, 无效false]
  102. */
  103. public function CheckExpire()
  104. {
  105. if(isset($_SESSION[$this->key_code]))
  106. {
  107. $data = $_SESSION[$this->key_code];
  108. return (time() <= $data['time']+$this->expire_time);
  109. }
  110. return false;
  111. }
  112. /**
  113. * [CheckCorrect 验证码是否正确]
  114. * @author Devil
  115. * @blog http://gong.gg/
  116. * @version 0.0.1
  117. * @datetime 2017-03-05T16:55:00+0800
  118. * @param [string] $code [验证码(默认从post读取)]
  119. * @return [booolean] [正确true, 错误false]
  120. */
  121. public function CheckCorrect($code = '')
  122. {
  123. if(isset($_SESSION[$this->key_code]['code']))
  124. {
  125. if(empty($code) && isset($_POST['code']))
  126. {
  127. $code = trim($_POST['code']);
  128. }
  129. return ($_SESSION[$this->key_code]['code'] == $code);
  130. }
  131. return false;
  132. }
  133. /**
  134. * [Remove 验证码清除]
  135. * @author Devil
  136. * @blog http://gong.gg/
  137. * @version 0.0.1
  138. * @datetime 2017-03-08T10:18:20+0800
  139. * @return [other] [无返回值]
  140. */
  141. public function Remove()
  142. {
  143. if(isset($_SESSION[$this->key_code]))
  144. {
  145. unset($_SESSION[$this->key_code]);
  146. }
  147. }
  148. /**
  149. * [IntervalTimeCheck 是否已经超过控制的间隔时间]
  150. * @author Devil
  151. * @blog http://gong.gg/
  152. * @version 0.0.1
  153. * @datetime 2017-03-10T11:26:52+0800
  154. * @return [booolean] [已超过间隔时间true, 未超过间隔时间false]
  155. */
  156. private function IntervalTimeCheck()
  157. {
  158. if(isset($_SESSION[$this->key_code]))
  159. {
  160. $data = $_SESSION[$this->key_code];
  161. return (time() > $data['time']+$this->interval_time);
  162. }
  163. return true;
  164. }
  165. }
  166. ?>