人人商城

reply.ctrl.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->model('reply');
  8. load()->model('module');
  9. $dos = array('display', 'post', 'delete', 'change_status', 'change_keyword_status');
  10. $do = in_array($do, $dos) ? $do : 'display';
  11. $m = empty($_GPC['m']) ? 'keyword' : trim($_GPC['m']);
  12. if (in_array($m, array('keyword', 'special', 'welcome', 'default', 'apply', 'service', 'userapi'))) {
  13. permission_check_account_user('platform_reply_' . $m);
  14. } else {
  15. permission_check_account_user('', true, 'reply');
  16. $modules = uni_modules();
  17. $_W['current_module'] = $modules[$m];
  18. define('IN_MODULE', $m);
  19. }
  20. $_W['page']['title'] = '自动回复';
  21. if (empty($m)) {
  22. itoast('错误访问.', '', '');
  23. }
  24. if ($m == 'special') {
  25. $mtypes = array(
  26. 'image' => '图片消息',
  27. 'voice' => '语音消息',
  28. 'video' => '视频消息',
  29. 'shortvideo' => '小视频消息',
  30. 'location' => '位置消息',
  31. 'trace' => '上报地理位置',
  32. 'link' => '链接消息',
  33. 'merchant_order' => '微小店消息',
  34. 'ShakearoundUserShake' => '摇一摇:开始摇一摇消息',
  35. 'ShakearoundLotteryBind' => '摇一摇:摇到了红包消息',
  36. 'WifiConnected' => 'Wifi连接成功消息',
  37. 'qr' => '二维码',
  38. );
  39. if (in_array($_W['account']['type'], array(ACCOUNT_TYPE_XZAPP_NORMAL, ACCOUNT_TYPE_XZAPP_AUTH))) {
  40. $mtypes = array(
  41. 'image' => '图片消息',
  42. 'voice' => '语音消息',
  43. 'video' => '视频消息',
  44. );
  45. }
  46. }
  47. $sysmods = module_system();
  48. if (in_array($m, array('custom'))) {
  49. $site = WeUtility::createModuleSite('reply');
  50. $site_urls = $site->getTabUrls();
  51. }
  52. if ($do == 'display') {
  53. if ($m == 'keyword' || !in_array($m, $sysmods)) {
  54. $pindex = max(1, intval($_GPC['page']));
  55. $psize = 8;
  56. $cids = $parentcates = $list = array();
  57. $condition = "uniacid = :uniacid AND module != 'cover' AND module != 'userapi'";
  58. $params = array();
  59. $params[':uniacid'] = $_W['uniacid'];
  60. if (isset($_GPC['type']) && !empty($_GPC['type'])) {
  61. $type = trim($_GPC['type']);
  62. if ($type == 'apply') {
  63. $condition .= " AND module NOT IN ('basic', 'news', 'images', 'voice', 'video', 'music', 'wxcard', 'reply')";
  64. } else {
  65. if (!in_array($type, array('basic', 'news', 'images', 'voice', 'video', 'music', 'wxcard'))) {
  66. itoast('非法语句!', referer(), 'error');
  67. }
  68. $condition .= " AND (FIND_IN_SET('" . $type . "', `containtype`) OR module = :type)";
  69. $params[':type'] = $type;
  70. }
  71. }
  72. if (!in_array($m, $sysmods)) {
  73. $condition .= " AND `module` = :type";
  74. $params[':type'] = $m;
  75. }
  76. if (!empty($_GPC['keyword'])) {
  77. if ($_GPC['search_type'] == 'keyword') {
  78. $rule_keyword_rid_list = pdo_getall('rule_keyword',array('content LIKE' => "%{$_GPC['keyword']}%"),array('rid'),'rid',array('id DESC'));
  79. if (!empty($rule_keyword_rid_list)) {
  80. $condition .= " AND id IN (" . implode(",", array_keys($rule_keyword_rid_list)) . ")";
  81. }
  82. } else {
  83. $condition .= " AND `name` LIKE :keyword";
  84. $params[':keyword'] = "%{$_GPC['keyword']}%";
  85. }
  86. }
  87. if (!empty($_GPC['keyword']) && $_GPC['search_type'] == 'keyword' && empty($rule_keyword_rid_list)) {
  88. $replies = array();
  89. $pager = '';
  90. } else {
  91. $replies = reply_search($condition, $params, $pindex, $psize, $total);
  92. $pager = pagination($total, $pindex, $psize);
  93. if (!empty($replies)) {
  94. foreach ($replies as &$item) {
  95. $condition = '`rid`=:rid';
  96. $params = array();
  97. $params[':rid'] = $item['id'];
  98. $item['keywords'] = reply_keywords_search($condition, $params);
  99. $item['allreply'] = reply_contnet_search($item['id']);
  100. $entries = module_entries($item['module'], array('rule'),$item['id']);
  101. if (!empty($entries)) {
  102. $item['options'] = $entries['rule'];
  103. }
  104. if (!in_array($item['module'], array("basic", "news", "images", "voice", "video", "music", "wxcard", "reply"))) {
  105. $item['module_info'] = module_fetch($item['module']);
  106. }
  107. }
  108. unset($item);
  109. }
  110. }
  111. $entries = module_entries($m, array('rule'));
  112. }
  113. if ($m == 'special') {
  114. $setting = uni_setting_load('default_message', $_W['uniacid']);
  115. $setting = $setting['default_message'] ? $setting['default_message'] : array();
  116. $module = uni_modules();
  117. }
  118. if ($m == 'default' || $m == 'welcome') {
  119. $setting = uni_setting($_W['uniacid'], array($m));
  120. if (!empty($setting[$m])) {
  121. $rule_keyword_id = pdo_getcolumn('rule_keyword', array('uniacid' => $_W['uniacid'], 'content' => $setting[$m]), 'rid');
  122. $setting_keyword = $setting[$m];
  123. }
  124. }
  125. if ($m == 'service') {
  126. $service_list = reply_getall_common_service();
  127. }
  128. if ($m == 'userapi') {
  129. $pindex = max(1, intval($_GPC['page']));
  130. $psize = 8;
  131. $condition = "uniacid = :uniacid AND `module`=:module";
  132. $params = array();
  133. $params[':uniacid'] = $_W['uniacid'];
  134. $params[':module'] = 'userapi';
  135. if (!empty($_GPC['keyword'])) {
  136. if ($_GPC['search_type'] == 'keyword') {
  137. $rule_keyword_rid_list = pdo_getall('rule_keyword',array('content LIKE' => "%{$_GPC['keyword']}%"),array('rid'),'rid',array('id DESC'));
  138. if (!empty($rule_keyword_rid_list)) {
  139. $condition .= " AND id IN (" . implode(",", array_keys($rule_keyword_rid_list)) . ")";
  140. }
  141. } else {
  142. $condition .= " AND `name` LIKE :keyword" ;
  143. $params[':keyword'] = "%{$_GPC['keyword']}%";
  144. }
  145. }
  146. if (!empty($_GPC['keyword']) && $_GPC['search_type'] == 'keyword' && empty($rule_keyword_rid_list)) {
  147. $replies = array();
  148. $pager = '';
  149. } else {
  150. $replies = reply_search($condition, $params, $pindex, $psize, $total);
  151. $pager = pagination($total, $pindex, $psize);
  152. if (!empty($replies)) {
  153. foreach ($replies as &$item) {
  154. $condition = '`rid`=:rid';
  155. $params = array();
  156. $params[':rid'] = $item['id'];
  157. $item['keywords'] = reply_keywords_search($condition, $params);
  158. }
  159. unset($item);
  160. }
  161. }
  162. }
  163. template('platform/reply');
  164. }
  165. if ($do == 'post') {
  166. if ($m == 'keyword' || $m == 'userapi' || !in_array($m, $sysmods)) {
  167. $module['title'] = '关键字自动回复';
  168. if ($_W['isajax'] && $_W['ispost']) {
  169. $keyword = safe_gpc_string($_GPC['keyword']);
  170. $sensitive_word = detect_sensitive_word($keyword);
  171. if (!empty($sensitive_word)) {
  172. iajax(-2, '含有敏感词:' . $sensitive_word);
  173. }
  174. $keyword = preg_replace('/,/', ',', $keyword);
  175. $keyword_arr = explode(',', $keyword);
  176. $result = pdo_getall('rule_keyword', array('uniacid' => $_W['uniacid'], 'content IN' => $keyword_arr, 'status !=' => 1), array('rid'));
  177. if (!empty($result)) {
  178. $keywords = array();
  179. foreach ($result as $reply) {
  180. $keywords[] = $reply['rid'];
  181. }
  182. $rids = implode($keywords, ',');
  183. $sql = "SELECT `id`, `name` FROM " . tablename('rule') . " WHERE `id` IN ($rids)";
  184. $rules = pdo_fetchall($sql);
  185. iajax(-1, $rules, '');
  186. }
  187. iajax(0, '');
  188. }
  189. $rid = intval($_GPC['rid']);
  190. if (!empty($rid)) {
  191. $reply = reply_single($rid);
  192. if (empty($reply) || $reply['uniacid'] != $_W['uniacid']) {
  193. itoast('抱歉,您操作的规则不在存或是已经被删除!', url('platform/reply', array('m' => $m)), 'error');
  194. }
  195. if (!empty($reply['keywords'])) {
  196. foreach ($reply['keywords'] as &$keyword) {
  197. $keyword = array_elements(array('type', 'content'), $keyword);
  198. }
  199. unset($keyword);
  200. }
  201. }
  202. if (checksubmit('submit')) {
  203. $keywords = @json_decode(htmlspecialchars_decode($_GPC['keywords']), true);
  204. if (empty($keywords)) {
  205. itoast('必须填写有效的触发关键字.');
  206. }
  207. $rulename = trim($_GPC['rulename']);
  208. $containtype = '';
  209. $_GPC['reply'] = (array)$_GPC['reply'];
  210. foreach ($_GPC['reply'] as $replykey => $replyval) {
  211. if (!empty($replyval)) {
  212. $type = substr($replykey, 6);
  213. $containtype .= $type == 'image' ? 'images' : $type .',';
  214. }
  215. }
  216. if (empty($containtype) && in_array($m, $sysmods) && $m != 'userapi') {
  217. itoast('必须填写有效的回复内容!');
  218. }
  219. $rule = array(
  220. 'uniacid' => $_W['uniacid'],
  221. 'name' => $rulename,
  222. 'module' => $m == 'keyword' ? 'reply' : $m,
  223. 'containtype' => $containtype,
  224. 'status' => $_GPC['status'] == 'true' ? 1 : 0,
  225. 'displayorder' => intval($_GPC['displayorder_rule']),
  226. );
  227. if ($_GPC['istop'] == 1) {
  228. $rule['displayorder'] = 255;
  229. } else {
  230. $rule['displayorder'] = range_limit($rule['displayorder'], 0, 254);
  231. }
  232. if ($m == 'userapi') {
  233. $module = WeUtility::createModule('userapi');
  234. } else {
  235. $module = WeUtility::createModule('core');
  236. }
  237. $msg = $module->fieldsFormValidate();
  238. $module_info = module_fetch($m);
  239. if (!empty($module_info) && empty($module_info['issystem'])) {
  240. $user_module = WeUtility::createModule($m);
  241. if (empty($user_module)) {
  242. itoast('抱歉,模块不存在请重新选择其它模块!', '', '');
  243. }
  244. $user_module_error_msg = $user_module->fieldsFormValidate();
  245. }
  246. if ((is_string($msg) && trim($msg) != '') || (is_string($user_module_error_msg) && trim($user_module_error_msg) != '')) {
  247. itoast($msg.$user_module_error_msg, '', '');
  248. }
  249. if (!empty($rid)) {
  250. $result = pdo_update('rule', $rule, array('id' => $rid));
  251. } else {
  252. $result = pdo_insert('rule', $rule);
  253. $rid = pdo_insertid();
  254. }
  255. if (!empty($rid)) {
  256. pdo_delete('rule_keyword', array('rid' => $rid, 'uniacid' => $_W['uniacid']));
  257. $rowtpl = array(
  258. 'rid' => $rid,
  259. 'uniacid' => $_W['uniacid'],
  260. 'module' => $m == 'keyword' ? 'reply' : $m,
  261. 'status' => $rule['status'],
  262. 'displayorder' => $rule['displayorder'],
  263. );
  264. foreach ($keywords as $kw) {
  265. $krow = $rowtpl;
  266. $krow['type'] = range_limit($kw['type'], 1, 4);
  267. $krow['content'] = htmlspecialchars($kw['content']);
  268. pdo_insert('rule_keyword', $krow);
  269. }
  270. $kid = pdo_insertid();
  271. $module->fieldsFormSubmit($rid);
  272. if (!empty($module_info) && empty($module_info['issystem'])) {
  273. $user_module->fieldsFormSubmit($rid);
  274. }
  275. itoast('回复规则保存成功!', url('platform/reply', array('m' => $m)), 'success');
  276. } else {
  277. itoast('回复规则保存失败, 请联系网站管理员!', url('platform/reply', array('m' => $m)), 'error');
  278. }
  279. }
  280. template('platform/reply-post');
  281. }
  282. if ($m == 'special') {
  283. $type = trim($_GPC['type']);
  284. $setting = uni_setting_load('default_message', $_W['uniacid']);
  285. $setting = $setting['default_message'] ? $setting['default_message'] : array();
  286. if (checksubmit('submit')) {
  287. $rule_id = intval(trim(htmlspecialchars_decode($_GPC['reply']['reply_keyword']), "\""));
  288. $module = trim(htmlspecialchars_decode($_GPC['reply']['reply_module']), "\"");
  289. if ((empty($rule_id) && empty($module)) || $_GPC['status'] === '0') {
  290. $setting[$type] = array('type' => '', 'module' => $module, 'keyword' => $rule_id);
  291. uni_setting_save('default_message', $setting);
  292. itoast('关闭成功', url('platform/reply', array('m' => 'special')), 'success');
  293. }
  294. $reply_type = empty($rule_id) ? 'module' : 'keyword';
  295. $reply_module = WeUtility::createModule('core');
  296. $result = $reply_module->fieldsFormValidate();
  297. if (is_error($result)) {
  298. itoast($result['message'], '', 'info');
  299. }
  300. if ($reply_type == 'module') {
  301. $setting[$type] = array('type' => 'module', 'module' => $module);
  302. } else {
  303. $rule = pdo_get('rule_keyword', array('id' => $rule_id, 'uniacid' => $_W['uniacid']));
  304. $setting[$type] = array('type' => 'keyword', 'keyword' => $rule['content']);
  305. }
  306. uni_setting_save('default_message', $setting);
  307. itoast('发布成功', url('platform/reply', array('m' => 'special')), 'success');
  308. }
  309. if ($setting[$type]['type'] == 'module') {
  310. $rule_id = $setting[$type]['module'];
  311. } else {
  312. $rule_id = pdo_getcolumn('rule_keyword', array('uniacid' => $_W['uniacid'], 'content' => $setting[$type]['keyword']), 'rid');
  313. $setting_keyword = $setting[$type]['keyword'];
  314. }
  315. template('platform/specialreply-post');
  316. }
  317. if ($m == 'default' || $m == 'welcome') {
  318. if (checksubmit('submit')) {
  319. $rule_keyword_id = intval(trim(htmlspecialchars_decode($_GPC['reply']['reply_keyword']), "\""));
  320. if (!empty($rule_keyword_id)) {
  321. $rule = pdo_get('rule_keyword', array('id' => $rule_keyword_id, 'uniacid' => $_W['uniacid']));
  322. $settings = array(
  323. $m => $rule['content']
  324. );
  325. } else {
  326. $settings = array($m => '');
  327. }
  328. $item = pdo_fetch("SELECT uniacid FROM " . tablename('uni_settings') . " WHERE uniacid=:uniacid", array(':uniacid' => $_W['uniacid']));
  329. if (!empty($item)){
  330. pdo_update('uni_settings', $settings, array('uniacid' => $_W['uniacid']));
  331. } else {
  332. $settings['uniacid'] = $_W['uniacid'];
  333. pdo_insert('uni_settings', $settings);
  334. }
  335. cache_delete(cache_system_key('unisetting', array('uniacid' => $_W['uniacid'])));
  336. cache_delete(cache_system_key('keyword', array('content' => md5($rule['content']), 'uniacid' => $_W['uniacid'])));
  337. itoast('系统回复更新成功!', url('platform/reply', array('m' => $m)), 'success');
  338. }
  339. }
  340. if ($m == 'apply') {
  341. $module['title'] = '应用关键字';
  342. $installedmodulelist = uni_modules();
  343. foreach ($installedmodulelist as $key => &$value) {
  344. if ($value['type'] == 'system' || in_array($value['name'], $sysmods)) {
  345. unset($installedmodulelist[$key]);
  346. continue;
  347. }
  348. $value['official'] = empty($value['issystem']) && (strexists($value['author'], 'WeEngine Team') || strexists($value['author'], ''));
  349. }
  350. unset($value);
  351. foreach ($installedmodulelist as $name => $module) {
  352. if (empty($module['isrulefields']) && $name != "core") {
  353. continue;
  354. }
  355. $module['title_first_pinyin'] = get_first_pinyin($module['title']);
  356. if ($module['issystem']) {
  357. $path = '../framework/builtin/' . $module['name'];
  358. } else {
  359. $path = '../addons/' . $module['name'];
  360. }
  361. $cion = $path . '/icon-custom.jpg';
  362. if (!file_exists($cion)) {
  363. $cion = $path . '/icon.jpg';
  364. if (!file_exists($cion)) {
  365. $cion = './resource/images/nopic-small.jpg';
  366. }
  367. }
  368. $module['icon'] = $cion;
  369. if ($module['enabled'] == 1) {
  370. $enable_modules[$name] = $module;
  371. } else {
  372. $unenable_modules[$name] = $module;
  373. }
  374. }
  375. $current_user_permissions = pdo_getall('users_permission', array('uid' => $_W['user']['uid'], 'uniacid' => $_W['uniacid']), array(), 'type');
  376. if (!empty($current_user_permissions)) {
  377. $current_user_permission_types = array_keys($current_user_permissions);
  378. }
  379. $moudles = true;
  380. template('platform/reply-post');
  381. }
  382. }
  383. if ($do == 'delete') {
  384. $rids = $_GPC['rid'];
  385. if (!is_array($rids)) {
  386. $rids = array($rids);
  387. }
  388. if (empty($rids)) {
  389. itoast('非法访问.', '', '');
  390. }
  391. foreach ($rids as $rid) {
  392. $rid = intval($rid);
  393. $reply = reply_single($rid);
  394. if (empty($reply) || $reply['uniacid'] != $_W['uniacid']) {
  395. itoast('抱歉,您操作的规则不在存或是已经被删除!', url('platform/reply', array('m' => $m)), 'error');
  396. }
  397. if (pdo_delete('rule', array('id' => $rid))) {
  398. pdo_delete('rule_keyword', array('rid' => $rid));
  399. if (!in_array($m, $sysmods)) {
  400. $reply_module = $m;
  401. } else {
  402. if ($m == 'userapi') {
  403. $reply_module = 'userapi';
  404. } else {
  405. $reply_module = 'reply';
  406. }
  407. }
  408. $module = WeUtility::createModule($reply_module);
  409. if (method_exists($module, 'ruleDeleted')) {
  410. $module->ruleDeleted($rid);
  411. }
  412. }
  413. }
  414. itoast('规则操作成功!', referer(), 'success');
  415. }
  416. if ($do == 'change_status') {
  417. $m = $_GPC['m'];
  418. if ($m == 'service') {
  419. $rid = intval($_GPC['rid']);
  420. $file = trim($_GPC['file']);
  421. if ($rid == 0) {
  422. $rid = reply_insert_without_service($file);
  423. if (empty($rid)) {
  424. iajax(1, '参数错误');
  425. }
  426. }
  427. $userapi_module = module_fetch('userapi');
  428. $config = $userapi_module['config'];
  429. $config[$rid] = isset($config[$rid]) && $config[$rid] ? false : true;
  430. $module_api = WeUtility::createModule('userapi');
  431. $module_api->saveSettings($config);
  432. iajax(0, '');
  433. } else {
  434. $type = trim($_GPC['type']);
  435. $setting = uni_setting_load('default_message', $_W['uniacid']);
  436. $setting = $setting['default_message'] ? $setting['default_message'] : array();
  437. if (empty($setting[$type]['type'])) {
  438. if (!empty($setting[$type]['keyword'])) {
  439. $setting[$type]['type'] = 'keyword';
  440. }
  441. if (!empty($setting[$type]['module'])) {
  442. $setting[$type]['type'] = 'module';
  443. }
  444. if (empty($setting[$type]['type'])) {
  445. iajax(1, '请先设置回复内容', '');
  446. }
  447. } else {
  448. $setting[$type]['type'] = '';
  449. }
  450. $result = uni_setting_save('default_message', $setting);
  451. if ($result) {
  452. iajax(0, '更新成功!');
  453. }
  454. }
  455. }
  456. if ($do == 'change_keyword_status') {
  457. $id = intval($_GPC['id']);
  458. $result = pdo_get('rule', array('id' => $id), array('status'));
  459. if (!empty($result)) {
  460. $rule = $rule_keyword = false;
  461. if ($result['status'] == 1) {
  462. $rule = pdo_update('rule', array('status' => 0), array('id' => $id));
  463. $rule_keyword = pdo_update('rule_keyword', array('status' => 0), array('uniacid' => $_W['uniacid'], 'rid' => $id));
  464. } else {
  465. $rule = pdo_update('rule', array('status' => 1), array('id' => $id));
  466. $rule_keyword = pdo_update('rule_keyword', array('status' => 1), array('uniacid' => $_W['uniacid'], 'rid' => $id));
  467. }
  468. if ($rule && $rule_keyword) {
  469. iajax(0, '更新成功!', '');
  470. } else {
  471. iajax(-1, '更新失败!', '');
  472. }
  473. }
  474. iajax(-1, '更新失败!', '');
  475. }