masssend_setting.inc.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id$
  7. */
  8. if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
  9. exit('Access Denied');
  10. }
  11. require_once DISCUZ_ROOT . './source/plugin/wechat/wechat.lib.class.php';
  12. require_once DISCUZ_ROOT . './source/plugin/wechat/setting.class.php';
  13. WeChatSetting::menu();
  14. define('PMODURL', 'action=plugins&operation&config&identifier=wechat&pmod=masssend_setting&ac=');
  15. $_G['wechat']['setting'] = unserialize($_G['setting']['mobilewechat']);
  16. $wechat_client = new WeChatClient($_G['wechat']['setting']['wechat_appId'], $_G['wechat']['setting']['wechat_appsecret']);
  17. if (!submitcheck('addsubmit') && !submitcheck('sendsubmit') && !submitcheck('delsubmit')) {
  18. $ac = !empty($_GET['ac']) ? $_GET['ac'] : '';
  19. if (!$ac) {
  20. showtips(lang('plugin/wechat', 'mass_main_tips'));
  21. echo '<a href="' . ADMINSCRIPT . '?' . PMODURL . 'add" class="addtr">' . lang('plugin/wechat', 'mass_create') . '</a>';
  22. $ppp = 10;
  23. $page = max(1, intval($_GET['page']));
  24. $start = ($page - 1) * $ppp;
  25. $count = C::t('#wechat#mobile_wechat_masssend')->count();
  26. $msg = C::t('#wechat#mobile_wechat_masssend')->range($start, $ppp, 'DESC');
  27. $multi = multi($count, $ppp, $page, ADMINSCRIPT . '?' . PMODURL);
  28. showformheader('plugins&operation=config&identifier=wechat&pmod=masssend_setting&ac=send', 'enctype');
  29. showtableheader(lang('plugin/wechat', 'mass_list'), '');
  30. showsubtitle(array(lang('plugin/wechat', 'mass_text_oper'), lang('plugin/wechat', 'mass_text_send'), lang('plugin/wechat', 'mass_type'), lang('plugin/wechat', 'mass_created_at'), 'MSG_ID', lang('plugin/wechat', 'mass_finish_at'), lang('plugin/wechat', 'mass_status'), lang('plugin/wechat', 'mass_totalcount'), lang('plugin/wechat', 'mass_filtercount'), lang('plugin/wechat', 'mass_sendcount'), lang('plugin/wechat', 'mass_errorcount')));
  31. foreach ($msg as $m) {
  32. showtablerow('', array(), array(
  33. '<a href="' . ADMINSCRIPT . '?' . PMODURL . 'del&id=' . $m['id'] . '">' . lang('plugin/wechat', 'mass_delete') . '</a> | <a href="' . ADMINSCRIPT . '?' . PMODURL . 'add&id=' . $m['id'] . '">' . lang('plugin/wechat', 'mass_edit') . '</a>',
  34. "<input type=\"radio\" name=\"massid\" value=\"$m[id]\" class=\"radio\">",
  35. $m['type'],
  36. dgmdate($m['created_at']),
  37. $m['msg_id'],
  38. $m['res_finish_at'] ? dgmdate($m['res_finish_at']) : '',
  39. $m['res_status'],
  40. $m['res_totalcount'],
  41. $m['res_filtercount'],
  42. $m['res_sentcount'],
  43. $m['res_errorcount'],
  44. ));
  45. }
  46. showtablefooter();
  47. echo '<br style="clear:both"><div class="right pg">' . $multi . '</div>';
  48. showsubmit('sendsubmit', lang('plugin/wechat', 'mass_send'));
  49. showformfooter();
  50. } else if ($ac == 'add') {
  51. $groups = $wechat_client->getAllGroups();
  52. if (!$groups) {
  53. cpmsg_error('wechat:mass_get_group_failed');
  54. }
  55. if (intval($_GET['id']) > 0) {
  56. $mass = C::t('#wechat#mobile_wechat_masssend')->fetch(intval($_GET['id']));
  57. }
  58. $massmessage = $mass['resource_id'] ? "[resource=$mass[resource_id]]" : $mass['text'];
  59. showtips(lang('plugin/wechat', 'mass_add_tips'));
  60. WeChatSetting::showResource();
  61. showformheader('plugins&operation=config&identifier=wechat&pmod=masssend_setting&ac=add', 'enctype');
  62. showtableheader();
  63. showtablerow('', '', array(lang('plugin/wechat', 'mass_type')));
  64. showtablerow('', array('', 'class="td23 td28"', '', 'class="td29"'), array(
  65. "<textarea class=\"tarea\" name=\"massmessage\" id=\"res_subscribe\" rows=\"5\" cols=\"40\">$massmessage</textarea>"
  66. . "<br /><a href=\"javascript:;\" id=\"rsel\" onclick=\"showResource('res_subscribe')\">" . lang('plugin/wechat', 'mass_select_media') . "</a>"
  67. ));
  68. $select = array('group_id', array());
  69. foreach ($groups as $g) {
  70. $select[1][] = array($g['id'], diconv($g['name'], 'UTF-8', CHARSET) . "[$g[count]]");
  71. }
  72. showsetting(lang('plugin/wechat', 'mass_group_id'), $select, '', 'select');
  73. showsubmit('addsubmit');
  74. showtablefooter();
  75. showformfooter();
  76. } else if ($ac == 'del') {
  77. $massid = intval($_GET['id']);
  78. $msg = C::t('#wechat#mobile_wechat_masssend')->fetch($massid);
  79. if (!$msg) {
  80. cpmsg_error('wechat:mass_not_exist');
  81. }
  82. if (!$msg['res_finish_at'] && $msg['msg_id']) {
  83. cpmsg('wechat:mass_del_tips_1', PMODURL . '&massid=' . $_GET['id'] . '&delsubmit=yes', 'form');
  84. }
  85. cpmsg('wechat:mass_del_tips_2', PMODURL . '&massid=' . $_GET['id'] . '&delsubmit=yes', 'form');
  86. }
  87. } elseif (submitcheck('addsubmit')) {
  88. $group_id = intval($_GET['group_id']);
  89. $massmessage = trim($_GET['massmessage']);
  90. if (empty($massmessage)) {
  91. cpmsg_error('wechat:mass_no_text');
  92. }
  93. if (preg_match("/^\[resource=(\d+)\]/", $massmessage, $r)) {
  94. $resource_id = $r[1];
  95. $res = C::t('#wechat#mobile_wechat_resource')->fetch($resource_id);
  96. if (!$res) {
  97. cpmsg('wechat:mass_no_found');
  98. }
  99. $news = array();
  100. if ($res['type'] == 0) {
  101. if ($res['data']['pic']) {
  102. $thumb_media_id = $wechat_client->upload('image', $_G['setting']['attachdir'] . 'common/' . $res['data']['local']);
  103. if (!$thumb_media_id) {
  104. cpmsg_error($wechat_client->error());
  105. }
  106. $res['data']['thumb_media_id'] = $thumb_media_id;
  107. $res['data']['author'] = '';
  108. } else {
  109. cpmsg('wechat:mass_no_pic');
  110. }
  111. array_push($news, $res['data']);
  112. } else if ($res['type'] == 1) {
  113. $news = array();
  114. foreach (array_keys($res['data']['mergeids']) as $resource_id) {
  115. $res = C::t('#wechat#mobile_wechat_resource')->fetch($resource_id);
  116. if (!$res) {
  117. cpmsg('wechat:mass_no_found');
  118. }
  119. if ($res['data']['pic']) {
  120. $thumb_media_id = $wechat_client->upload('image', $_G['setting']['attachdir'] . 'common/' . $res['data']['local']);
  121. if (!$thumb_media_id) {
  122. cpmsg_error($wechat_client->error());
  123. }
  124. $res['data']['thumb_media_id'] = $thumb_media_id;
  125. $res['data']['author'] = '';
  126. array_push($news, $res['data']);
  127. } else {
  128. cpmsg_error('wechat:mass_no_pic');
  129. }
  130. }
  131. }
  132. $newsRes = $wechat_client->uploadNews($news);
  133. if (!$newsRes) {
  134. cpmsg_error($wechat_client->error());
  135. }
  136. C::t('#wechat#mobile_wechat_masssend')->insert(array(
  137. 'type' => 'media',
  138. 'resource_id' => $res['id'],
  139. 'text' => '',
  140. 'group_id' => $group_id,
  141. 'media_id' => $newsRes['media_id'],
  142. 'created_at' => $newsRes['created_at']
  143. ));
  144. cpmsg('wechat:mass_created_succ', PMODURL, 'succeed');
  145. } else {
  146. $data = array(
  147. 'resource_id' => 0,
  148. 'type' => 'text',
  149. 'text' => $massmessage,
  150. 'group_id' => $group_id,
  151. 'created_at' => TIMESTAMP
  152. );
  153. C::t('#wechat#mobile_wechat_masssend')->insert($data);
  154. cpmsg('wechat:mass_created_succ', PMODURL, 'succeed');
  155. }
  156. } else if (submitcheck('sendsubmit')) {
  157. $massid = intval($_GET['massid']);
  158. if (!$massid) {
  159. cpmsg_error('wechat:mass_not_exist');
  160. }
  161. $msg = C::t('#wechat#mobile_wechat_masssend')->fetch($massid);
  162. if (!$msg) {
  163. cpmsg_error('wechat:mass_not_exist');
  164. }
  165. if ($msg['type'] == 'media' && ($msg['created_at'] + 86400 * 3) < TIMESTAMP) {
  166. cpmsg_error('wechat:mass_send_expire');
  167. }
  168. $res = $wechat_client->sendMassMsg($msg);
  169. if ($res) {
  170. C::t('#wechat#mobile_wechat_masssend')->update($massid, array('msg_id' => $res['msg_id'], 'sent_at' => TIMESTAMP));
  171. $updatedata = array('receiveEvent::masssendjobfinish' => array('plugin' => 'wechat', 'include' => 'response.class.php', 'class' => 'WSQResponse', 'method' => 'masssendFinish'));
  172. $responsehook = WeChatHook::updateResponse($updatedata);
  173. cpmsg('wechat:mass_sent_succ', PMODURL, 'succeed');
  174. } else {
  175. cpmsg_error($wechat_client->error());
  176. }
  177. } else if (submitcheck('delsubmit')) {
  178. $massid = intval($_GET['massid']);
  179. $msg = C::t('#wechat#mobile_wechat_masssend')->fetch($massid);
  180. if (!$msg) {
  181. cpmsg_error('wechat:mass_not_exist');
  182. }
  183. C::t('#wechat#mobile_wechat_masssend')->delete($massid);
  184. cpmsg('wechat:mass_oper_succ', PMODURL, 'succeed');
  185. }
  186. ?>