reply.mod.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. function reply_search($condition = '', $params = array(), $pindex = 0, $psize = 10, &$total = 0) {
  8. if (!empty($condition)) {
  9. $where = " WHERE {$condition}";
  10. }
  11. $sql = "SELECT * FROM " . tablename('rule') . $where . " ORDER BY status DESC, displayorder DESC, id DESC";
  12. $pindex = intval($pindex);
  13. $psize = intval($psize);
  14. if ($pindex > 0) {
  15. $start = ($pindex - 1) * $psize;
  16. $sql .= " LIMIT {$start},{$psize}";
  17. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('rule') . $where, $params);
  18. }
  19. return pdo_fetchall($sql, $params);
  20. }
  21. function reply_single($id) {
  22. $id = intval($id);
  23. $result = table('rule')->getById($id);
  24. if (empty($result)) {
  25. return $result;
  26. }
  27. $result['keywords'] = table('rule_keyword')->whereRid($id)->getall();
  28. return $result;
  29. }
  30. function reply_keywords_search($condition = '', $params = array(), $pindex = 0, $psize = 10, &$total = 0) {
  31. global $_W;
  32. if (!empty($condition)) {
  33. $where = " WHERE {$condition} ";
  34. }
  35. $sql = 'SELECT * FROM ' . tablename('rule_keyword') . $where . ' ORDER BY displayorder DESC, `type` ASC, id DESC';
  36. $pindex = intval($pindex);
  37. $psize = intval($psize);
  38. if ($pindex > 0) {
  39. $start = ($pindex - 1) * $psize;
  40. $sql .= " LIMIT {$start},{$psize}";
  41. $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('rule_keyword') . $where, $params);
  42. }
  43. $result = pdo_fetchall($sql, $params);
  44. if (!empty($result)) {
  45. $rule_setting_select = table('uni_account_modules')->getByUniacidAndModule('userapi', $_W['uniacid']);
  46. foreach ($result as $key => $val) {
  47. if ($val['module'] == 'userapi' && empty($val['uniacid'])) {
  48. if (empty($rule_setting_select['settings'][$val['rid']])) {
  49. unset($result[$key]);
  50. continue;
  51. }
  52. }
  53. $containtypes = pdo_get('rule', array('id' => $val['rid']), array('containtype'));
  54. if (!empty($containtypes)) {
  55. $containtype = explode(',', $containtypes['containtype']);
  56. $containtype = array_filter($containtype);
  57. } else {
  58. $containtype = array();
  59. }
  60. $result[$key]['reply_type'] = $containtype;
  61. }
  62. } else {
  63. $result = array();
  64. }
  65. return $result;
  66. }
  67. function reply_content_search($rid = 0) {
  68. $result = array();
  69. $rid = intval($rid);
  70. if (empty($rid)) {
  71. return $result;
  72. }
  73. $modules = array('basic', 'images', 'news', 'music', 'voice', 'video', 'wxapp');
  74. $params = array(':rid' => $rid);
  75. $result['sum'] = 0;
  76. foreach ($modules as $key => $module) {
  77. $result[$module] = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($module.'_reply') . ' WHERE `rid` = :rid', $params);
  78. $result['sum'] += $result[$module];
  79. }
  80. return $result;
  81. }
  82. function reply_predefined_service() {
  83. $predefined_service = array(
  84. 'weather.php' => array(
  85. 'title' => '城市天气',
  86. 'description' => '"城市名+天气", 如: "北京天气"',
  87. 'keywords' => array(
  88. array('3', '^.+天气$')
  89. )
  90. ),
  91. 'baike.php' => array(
  92. 'title' => '百度百科',
  93. 'description' => '"百科+查询内容" 或 "定义+查询内容", 如: "百科姚明", "定义自行车"',
  94. 'keywords' => array(
  95. array('3', '^百科.+$'),
  96. array('3', '^定义.+$'),
  97. )
  98. ),
  99. 'translate.php' => array(
  100. 'title' => '即时翻译',
  101. 'description' => '"@查询内容(中文或英文)"',
  102. 'keywords' => array(
  103. array('3', '^@.+$'),
  104. )
  105. ),
  106. 'calendar.php' => array(
  107. 'title' => '今日老黄历',
  108. 'description' => '"日历", "万年历", "黄历"或"几号"',
  109. 'keywords' => array(
  110. array('1', '日历'),
  111. array('1', '万年历'),
  112. array('1', '黄历'),
  113. array('1', '几号'),
  114. )
  115. ),
  116. 'news.php' => array(
  117. 'title' => '看新闻',
  118. 'description' => '"新闻"',
  119. 'keywords' => array(
  120. array('1', '新闻'),
  121. )
  122. ),
  123. 'express.php' => array(
  124. 'title' => '快递查询',
  125. 'description' => '"快递+单号", 如: "申通1200041125"',
  126. 'keywords' => array(
  127. array('3', '^(申通|圆通|中通|汇通|韵达|顺丰|EMS) *[a-z0-9]{1,}$')
  128. )
  129. ),
  130. );
  131. return $predefined_service;
  132. }
  133. function reply_getall_common_service() {
  134. global $_W;
  135. $rule_setting_select = table('uni_account_modules')->getByUniacidAndModule('userapi', $_W['uniacid']);
  136. $rule_setting_select = empty($rule_setting_select['settings']) ? array() : (array)$rule_setting_select['settings'];
  137. $exists_rule = table('rule')->where(array('uniacid' => 0, 'module' => 'userapi', 'status' => 1))->getall();
  138. $service_list = array();
  139. $rule_ids = array();
  140. $api_url = array();
  141. if (!empty($exists_rule)) {
  142. foreach ($exists_rule as $rule_detail) {
  143. $rule_ids[] = $rule_detail['id'];
  144. $service_list[$rule_detail['id']] = $rule_detail;
  145. }
  146. $all_description = table('userapi_reply')->where('rid IN', $rule_ids)->getall();
  147. if (!empty($all_description)) {
  148. foreach ($all_description as $description) {
  149. $service_list[$description['rid']]['description'] = $description['description'];
  150. $service_list[$description['rid']]['switch'] = isset($rule_setting_select[$description['rid']]) && $rule_setting_select[$description['rid']] ? 'checked' : '';
  151. $api_url[] = $description['apiurl'];
  152. }
  153. }
  154. }
  155. $all_service = reply_predefined_service();
  156. $all_url = array_keys($all_service);
  157. $diff_url = array_diff($all_url, $api_url);
  158. if (!empty($diff_url)) {
  159. foreach ($diff_url as $url) {
  160. $userapi_reply_info = table('userapi_reply')->getByApiurl($url);
  161. $userapi_reply_info['rid'] = empty($userapi_reply_info['rid']) ? 0 : $userapi_reply_info['rid'];
  162. $service_list[$userapi_reply_info['rid']]['url'] = empty($userapi_reply_info['apiurl']) ? '' : $userapi_reply_info['apiurl'];
  163. $service_list[$userapi_reply_info['rid']]['rid'] = $userapi_reply_info['rid'];
  164. $service_list[$userapi_reply_info['rid']]['id'] = empty($userapi_reply_info['id']) ? 0 : $userapi_reply_info['id'];
  165. $service_list[$userapi_reply_info['rid']]['name'] = empty($all_service[$url]['title']) ? '' : $all_service[$url]['title'];
  166. $service_list[$userapi_reply_info['rid']]['description'] = empty($all_service[$url]['description']) ? '' : $all_service[$url]['description'];
  167. $service_list[$userapi_reply_info['rid']]['switch'] = isset($rule_setting_select[$userapi_reply_info['rid']]) && $rule_setting_select[$userapi_reply_info['rid']] ? 'checked' : '';
  168. }
  169. }
  170. return $service_list;
  171. }
  172. function reply_insert_without_service($file) {
  173. $all_service = reply_predefined_service();
  174. $all_url = array_keys($all_service);
  175. if (!in_array($file, $all_url)) {
  176. return false;
  177. }
  178. $userapi_reply_info = table('userapi_reply')->getByApiurl($file);
  179. if (!empty($userapi_reply_info) && !empty($userapi_reply_info['rid'])) {
  180. return $userapi_reply_info['rid'];
  181. }
  182. $rule_info = array('uniacid' => 0, 'name' => $all_service[$file]['title'], 'module' => 'userapi', 'displayorder' => 255, 'status' => 1);
  183. table('rule')->fill($rule_info)->save();
  184. $rule_id = pdo_insertid();
  185. $rule_keyword_info = array('rid' => $rule_id, 'uniacid' => 0, 'module' => 'userapi', 'displayorder' => $rule_info['displayorder'], 'status' => $rule_info['status']);
  186. if (!empty($all_service[$file]['keywords'])) {
  187. foreach ($all_service[$file]['keywords'] as $keyword_info) {
  188. $rule_keyword_info['content'] = $keyword_info[1];
  189. $rule_keyword_info['type'] = $keyword_info[0];
  190. table('rule_keyword')->fill($rule_keyword_info)->save();
  191. }
  192. }
  193. $userapi_reply = array('rid' => $rule_id, 'description' => htmlspecialchars($all_service[$file]['description']), 'apiurl' => $file);
  194. table('userapi_reply')->fill($userapi_reply)->save();
  195. return $rule_id;
  196. }
  197. function reply_check_uni_default_keyword($uniacid = 0) {
  198. global $_W;
  199. $uniacid = empty($uniacid) ? $_W['uniacid'] : $uniacid;
  200. $default = uni_setting_load('default', $uniacid);
  201. if (!empty($default['default'])) {
  202. $rule = table('rule_keyword')->getByUniacidAndContent($uniacid, $default['default']);
  203. if (empty($rule)) {
  204. uni_setting_save('default', '');
  205. cache_delete(cache_system_key('unisetting', array('uniacid' => $uniacid)));
  206. }
  207. }
  208. return true;
  209. }