sms.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin','admin',0);
  4. pc_base::load_sys_class('form','',0);
  5. pc_base::load_sys_class('format','',0);
  6. pc_base::load_app_func('global','sms');
  7. class sms extends admin {
  8. function __construct() {
  9. parent::__construct();
  10. $this->log_db = pc_base::load_model('sms_report_model');
  11. $this->module_db = pc_base::load_model('module_model');
  12. $this->member_db = pc_base::load_model('member_model');
  13. //获取短信平台配置信息
  14. $siteid = get_siteid();
  15. $this->sms_setting_arr = getcache('sms');
  16. if(!empty($this->sms_setting_arr[$siteid])) {
  17. $this->sms_setting = $this->sms_setting_arr[$siteid];
  18. } else {
  19. $this->sms_setting = array('userid'=>'', 'productid'=>'', 'sms_key'=>'');
  20. }
  21. //初始化smsapi
  22. pc_base::load_app_class('smsapi', '', 0);
  23. $this->smsapi = new smsapi($this->sms_setting['userid'], $this->sms_setting['productid'], $this->sms_setting['sms_key']);
  24. }
  25. public function init() {
  26. $show_pc_hash = 1;
  27. //短信套餐列表
  28. $smsprice_arr = $this->smsapi->get_price();
  29. //字符转换
  30. if(CHARSET != 'utf-8') {
  31. if(is_array($smsprice_arr)) {
  32. foreach ($smsprice_arr as $k=>$v) {
  33. $smsprice_arr[$k]['name'] = iconv('utf-8', CHARSET, $smsprice_arr[$k]['name']);
  34. $smsprice_arr[$k]['description'] = iconv('utf-8', CHARSET, $smsprice_arr[$k]['description']);
  35. }
  36. }
  37. }
  38. //短信剩余条数
  39. $smsinfo_arr = $this->smsapi->get_smsinfo();
  40. include $this->admin_tpl('index');
  41. }
  42. /**
  43. *
  44. * 短信充值记录
  45. */
  46. public function sms_buy_history() {
  47. $payinfo_arr = $this->smsapi->get_buyhistory();
  48. //字符转换
  49. if(CHARSET != 'utf-8') {
  50. if(is_array($payinfo_arr)) foreach ($payinfo_arr as $k=>$v) {
  51. $payinfo_arr[$k]['name'] = iconv('utf-8', CHARSET, $payinfo_arr[$k]['name']);
  52. $payinfo_arr[$k]['description'] = iconv('utf-8', CHARSET, $payinfo_arr[$k]['description']);
  53. }
  54. }
  55. include $this->admin_tpl('sms_buy_history');
  56. }
  57. /**
  58. *
  59. * 短信消费记录
  60. */
  61. public function sms_pay_history() {
  62. $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
  63. $paylist_apiarr = $this->smsapi->get_payhistory($page);
  64. $paylist_arr = !empty($paylist_apiarr['datas']) ? $paylist_apiarr['datas'] : array();
  65. $payhistoryinfo_total = !empty($paylist_apiarr['total']) ? $paylist_apiarr['total'] : 0;
  66. $pages = pages($payhistoryinfo_total, $page, 20);
  67. include $this->admin_tpl('sms_pay_history');
  68. }
  69. public function sms_setting() {
  70. $siteid = get_siteid();
  71. if(isset($_POST['dosubmit'])) {
  72. $this->sms_setting_arr[$siteid] = $_POST['setting'];
  73. $setting = array2string($this->sms_setting);
  74. setcache('sms', $this->sms_setting_arr);
  75. $this->module_db->update(array('setting'=>$setting),array('module'=>'sms'));
  76. showmessage(L('operation_success'),HTTP_REFERER);
  77. } else {
  78. $show_pc_hash = '';
  79. include $this->admin_tpl('sms_setting');
  80. }
  81. }
  82. public function sms_api() {
  83. include $this->admin_tpl('sms_api');
  84. }
  85. public function sms_sent() {
  86. if(!$this->sms_setting['sms_enable']) showmessage(L('please_open_sms_platform_status'));
  87. if(empty($this->smsapi->userid)) {
  88. showmessage(L('need_band'), 'index.php?m=sms&c=sms&a=sms_setting&menuid='.$_GET[menuid].'&pc_hash='.$_SESSION['pc_hash']);
  89. }
  90. //检查短信余额
  91. if($this->sms_setting['sms_key']) {
  92. $smsinfo = $this->smsapi->get_smsinfo();
  93. }
  94. if(isset($_POST['dosubmit'])) {
  95. //组合短信参数内容
  96. $content = '';
  97. if(is_array($_POST['msg']) && !empty($_POST['msg'])){
  98. foreach ($_POST['msg'] as $val) {
  99. $val = stripcslashes($val);
  100. if($content==''){
  101. $content.="$val";
  102. }else{
  103. $content.="||$val";
  104. }
  105. }
  106. }
  107. $mobile = explode("\r\n", trim($_POST['mobile'],"\r\n"));
  108. $mobile = array_unique($mobile);
  109. $tplid = intval($_POST['tplid']);
  110. //过滤非手机号码
  111. foreach ($mobile as $k=>$v) {
  112. if(!preg_match('/^1([0-9]{9})/',$v)) {
  113. unset($mobile[$k]);
  114. }
  115. }
  116. //短信余额不足
  117. if($smsinfo['surplus'] < count($mobile)) {
  118. showmessage(L('need_more_surplus'));
  119. }
  120. //发送短信
  121. $return = $this->smsapi->send_sms($mobile, $content, $_POST['sendtime'], CHARSET,'',$tplid);
  122. showmessage($return, HTTP_REFERER,6000);
  123. } else {
  124. $smsinfo_arr = $this->smsapi->get_smsinfo();
  125. if(!empty($smsinfo_arr['allow_send_ip']) &&!in_array($_SERVER["SERVER_ADDR"],$smsinfo_arr['allow_send_ip'])) {
  126. showmessage(L('this_server_does_not_allow_send_sms'));
  127. }
  128. $start_time = date('Y-m-d', SYS_TIME-date('t', SYS_TIME)*86400);
  129. $end_time = date('Y-m-d', SYS_TIME);
  130. $grouplist = getcache('grouplist', 'member');
  131. foreach($grouplist as $k=>$v) {
  132. $grouplist[$k] = $v['name'];
  133. }
  134. //会员所属模型
  135. $modellistarr = getcache('member_model', 'commons');
  136. foreach ($modellistarr as $k=>$v) {
  137. $modellist[$k] = $v['name'];
  138. }
  139. //显示群发
  140. $show_qf_url = $this->smsapi->show_qf_url();
  141. include $this->admin_tpl('sms_sent');
  142. }
  143. }
  144. /*
  145. * 获取短信模版,ajax处理
  146. */
  147. public function public_get_tpl(){
  148. $sceneid = intval($_GET['sceneid']);
  149. if(!$sceneid){exit(0);}
  150. //获取服务器场景,组成表单单选项
  151. $tpl_arr = $this->smsapi->get_tpl($sceneid);
  152. if(!empty($tpl_arr)){
  153. exit($tpl_arr);
  154. }else{
  155. exit(0);
  156. }
  157. }
  158. /*
  159. * 显示短信内容,ajax处理
  160. */
  161. public function public_show_tpl(){
  162. $tplid = intval($_GET['tplid']);
  163. if(!$tplid){exit(0);}
  164. //获取模版内容
  165. $tpl_arr = $this->smsapi->show_tpl($tplid);
  166. if(!empty($tpl_arr)){
  167. exit($tpl_arr);
  168. }else{
  169. exit(0);
  170. }
  171. }
  172. public function exportmobile() {
  173. $start_time = isset($_POST['start_time']) ? $_POST['start_time'] : '';
  174. $end_time = isset($_POST['end_time']) ? $_POST['end_time'] : date('Y-m-d', SYS_TIME);
  175. $where_start_time = strtotime($start_time) ? strtotime($start_time) : 0;
  176. $where_end_time = strtotime($end_time) + 86400;
  177. $groupid = isset($_POST['groupid']) && !empty($_POST['groupid']) ? $_POST['groupid'] : '';
  178. $modelid = isset($_POST['modelid']) && !empty($_POST['modelid']) ? $_POST['modelid'] : 10;
  179. //开始时间大于结束时间,置换变量
  180. if($where_start_time > $where_end_time) {
  181. $tmp = $where_start_time;
  182. $where_start_time = $where_end_time;
  183. $where_end_time = $tmp;
  184. $tmptime = $start_time;
  185. $start_time = $end_time;
  186. $end_time = $tmptime;
  187. unset($tmp, $tmptime);
  188. }
  189. $where = '';
  190. if($groupid) {
  191. $where .= "`groupid` = '$groupid' AND ";
  192. }
  193. if($modelid) {
  194. $where .= "`modelid` = '$modelid' AND ";
  195. }
  196. $where .= "`regdate` BETWEEN '$where_start_time' AND '$where_end_time'";
  197. //根据条件读取会员主表
  198. $total = $this->member_db->count($where);
  199. $str = '';
  200. $perpage = 10;
  201. for($i=0;$i<=floor($total/$perpage);$i++) {
  202. $start = $i*$perpage;
  203. $data = $this->member_db->select($where, 'userid', "$start, $perpage");
  204. $userid_arr = array();
  205. foreach ($data as $v) {
  206. $userid_arr[] = $v['userid'];
  207. }
  208. $uids = to_sqls($userid_arr, '', 'userid');
  209. //读取模型表中手机号码字段
  210. $this->member_db->set_model($modelid);
  211. $data = $this->member_db->select($uids);
  212. foreach ($data as $v) {
  213. if(!empty($v['mobile'])) {
  214. $str .= $v['mobile']."\r\n";
  215. }
  216. }
  217. $this->member_db->set_model();
  218. }
  219. header("Content-type:application/octet-stream");
  220. header("Content-Disposition: attachment; filename=mobile.txt");
  221. echo $str;
  222. }
  223. }