reply.mod.php 6.8 KB

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