reply.mod.php 6.3 KB

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