人人商城

site.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. class UserApiModuleSite extends WeModuleSite {
  8. public $tablename = 'userapi_reply';
  9. private $predefines = array(
  10. 'weather.php' => array(
  11. 'title' => '城市天气',
  12. 'description' => '"城市名+天气", 如: "北京天气"',
  13. 'keywords' => array(
  14. array('3', '^.+天气$')
  15. )
  16. ),
  17. 'baike.php' => array(
  18. 'title' => '百度百科',
  19. 'description' => '"百科+查询内容" 或 "定义+查询内容", 如: "百科姚明", "定义自行车"',
  20. 'keywords' => array(
  21. array('3', '^百科.+$'),
  22. array('3', '^定义.+$'),
  23. )
  24. ),
  25. 'translate.php' => array(
  26. 'title' => '即时翻译',
  27. 'description' => '"@查询内容(中文或英文)"',
  28. 'keywords' => array(
  29. array('3', '^@.+$'),
  30. )
  31. ),
  32. 'calendar.php' => array(
  33. 'title' => '今日老黄历',
  34. 'description' => '"日历", "万年历", "黄历"或"几号"',
  35. 'keywords' => array(
  36. array('1', '日历'),
  37. array('1', '万年历'),
  38. array('1', '黄历'),
  39. array('1', '几号'),
  40. )
  41. ),
  42. 'news.php' => array(
  43. 'title' => '看新闻',
  44. 'description' => '"新闻"',
  45. 'keywords' => array(
  46. array('1', '新闻'),
  47. )
  48. ),
  49. 'express.php' => array(
  50. 'title' => '快递查询',
  51. 'description' => '"快递+单号", 如: "申通1200041125"',
  52. 'keywords' => array(
  53. array('3', '^(申通|圆通|中通|汇通|韵达|顺丰|EMS) *[a-z0-9]{1,}$')
  54. )
  55. ),
  56. );
  57. public function doWebManage() {
  58. global $_W, $_GPC;
  59. $foo = !empty($_GPC['foo']) ? $_GPC['foo'] : 'display';
  60. if($foo == 'display') {
  61. require model('rule');
  62. $pindex = max(1, intval($_GPC['page']));
  63. $psize = 20;
  64. $types = array('', '等价', '包含', '正则表达式匹配');
  65. $condition = 'weid = 0 AND module = \'userapi\'';
  66. $params = array();
  67. if (isset($_GPC['status'])) {
  68. $condition .= " AND status = :status";
  69. $params[':status'] = intval($_GPC['status']);
  70. }
  71. if(isset($_GPC['keyword'])) {
  72. $condition .= ' AND `name` LIKE :keyword';
  73. $params[':keyword'] = "%{$_GPC['keyword']}%";
  74. }
  75. $ds = rule_search($condition, $params, $pindex, $psize, $total);
  76. $pager = pagination($total, $pindex, $psize);
  77. if (!empty($ds)) {
  78. foreach($ds as &$item) {
  79. $reply = pdo_fetch('SELECT * FROM ' . tablename($this->tablename) . ' WHERE `rid`=:rid', array(':rid' => $item['id']));
  80. $item['description'] = $reply['description'];
  81. }
  82. }
  83. $import = false;
  84. $apis = implode('\',\'', array_keys($this->predefines));
  85. $apis = "'{$apis}'";
  86. $sql = 'SELECT DISTINCT `apiurl` FROM ' . tablename($this->tablename) . ' AS `e` LEFT JOIN ' . tablename('rule') . " AS `r` ON (`e`.`rid`=`r`.`id`) WHERE `r`.`weid`='0' AND `apiurl` IN ({$apis})";
  87. $apiurls = pdo_fetchall($sql);
  88. if(count($apiurls) != count($this->predefines)) {
  89. $import = true;
  90. }
  91. include $this->template('display');
  92. }
  93. if($foo == 'import') {
  94. $apis = implode('\',\'', array_keys($this->predefines));
  95. $apis = "'{$apis}'";
  96. $sql = 'SELECT DISTINCT `apiurl` FROM ' . tablename($this->tablename) . ' AS `e` LEFT JOIN ' . tablename('rule') . " AS `r` ON (`e`.`rid`=`r`.`id`) WHERE `r`.`weid`='0' AND `apiurl` IN ({$apis})";
  97. $apiurls = pdo_fetchall($sql);
  98. $as = array();
  99. foreach($apiurls as $url) {
  100. $as[] = $url['apiurl'];
  101. }
  102. foreach($this->predefines as $key => $v) {
  103. if(!in_array($key, $as)) {
  104. $rule = array(
  105. 'weid' => 0,
  106. 'cid' => 0,
  107. 'name' => $v['title'],
  108. 'module' => 'userapi',
  109. 'displayorder' => 255,
  110. 'status' => 1,
  111. );
  112. pdo_insert('rule', $rule);
  113. $rid = pdo_insertid();
  114. if(!empty($rid)) {
  115. foreach($v['keywords'] as $row) {
  116. $data = array(
  117. 'content' => $row[1],
  118. 'type' => $row[0],
  119. 'rid' => $rid,
  120. 'weid' => 0,
  121. 'module' => 'userapi',
  122. 'status' => $rule['status'],
  123. 'displayorder' => $rule['displayorder'],
  124. );
  125. pdo_insert('rule_keyword', $data);
  126. }
  127. $reply = array(
  128. 'rid' => $rid,
  129. 'description' => $v['description'],
  130. 'apiurl' => $key,
  131. 'token' => '',
  132. 'default_text' => '',
  133. 'cachetime' => 0
  134. );
  135. pdo_insert($this->tablename, $reply);
  136. }
  137. }
  138. }
  139. itoast('成功导入.', referer(), 'success');
  140. }
  141. if($foo == 'delete') {
  142. $rid = intval($_GPC['rid']);
  143. pdo_delete('rule', array('module' => 'userapi', 'id' => $rid));
  144. pdo_delete('rule_keyword', array('module' => 'userapi', 'rid' => $rid));
  145. pdo_delete($this->tablename, array('rid' => $rid));
  146. itoast('成功删除.', referer(), 'success');
  147. }
  148. if($foo == 'post') {
  149. $rid = intval($_GPC['id']);
  150. require model('rule');
  151. if(checksubmit()) {
  152. if (empty($_GPC['name'])) {
  153. itoast('抱歉,规则名称为必填项,请选回修改!', '', '');
  154. }
  155. if (($_GPC['type'] && empty($_GPC['apiurl'])) || (empty($_GPC['type']) && empty($_GPC['apilocal']))) {
  156. itoast('请填写接口地址!', '', '');
  157. }
  158. if ($_GPC['type'] && empty($_GPC['token'])) {
  159. itoast('请填写Token值!', '', '');
  160. }
  161. $rule = array(
  162. 'weid' => 0,
  163. 'cid' => 0,
  164. 'name' => $_GPC['service'],
  165. 'module' => 'userapi',
  166. 'displayorder' => 255,
  167. 'status' => intval($_GPC['status']),
  168. );
  169. if($rid) {
  170. pdo_update('rule', $rule, array('id' => $rid));
  171. } else {
  172. pdo_insert('rule', $rule);
  173. $rid = pdo_insertid();
  174. }
  175. if(empty($rid)) {
  176. itoast('增加服务失败, 请稍后重试. ', '', 'error');
  177. }
  178. pdo_delete('rule_keyword', array('rid' => $rid));
  179. $rows = array();
  180. $rowtpl = array(
  181. 'rid' => $rid,
  182. 'weid' => 0,
  183. 'module' => $rule['module'],
  184. 'status' => $rule['status'],
  185. 'displayorder' => $rule['displayorder'],
  186. );
  187. if (!empty($_GPC['keyword-name'])) {
  188. foreach ($_GPC['keyword-name'] as $id => $row) {
  189. if (empty($row) && strlen($row) == 0 && intval($_GPC['keyword-type'][$id]) != 4) {
  190. continue;
  191. }
  192. $rowtpl['content'] = $row;
  193. $rowtpl['type'] = intval($_GPC['keyword-type'][$id]);
  194. if($rowtpl['type'] == 4) {
  195. $rowtpl['content'] = '';
  196. }
  197. $rows[md5($rowtpl['type'] . $rowtpl['content'])] = $rowtpl;
  198. }
  199. }
  200. if (!empty($_GPC['keyword-name-new'])) {
  201. foreach ($_GPC['keyword-name-new'] as $id => $row) {
  202. if (empty($row) && strlen($row) == 0 && intval($_GPC['keyword-type-new'][$id]) != 4) {
  203. continue;
  204. }
  205. $rowtpl['content'] = $row;
  206. $rowtpl['type'] = intval($_GPC['keyword-type-new'][$id]);
  207. if($rowtpl['type'] == 4) {
  208. $rowtpl['content'] = '';
  209. }
  210. $rows[md5($rowtpl['type'] . $rowtpl['content'])] = $rowtpl;
  211. }
  212. }
  213. if(!empty($_GPC['keywords'])) {
  214. $kwds = explode(',', trim($_GPC['keywords']));
  215. foreach($kwds as $kwd) {
  216. $kwd = trim($kwd);
  217. if(empty($kwd)) {
  218. continue;
  219. }
  220. $rowtpl['content'] = $kwd;
  221. $rowtpl['type'] = 1;
  222. $rows[md5($rowtpl['type'] . $rowtpl['content'])] = $rowtpl;
  223. }
  224. }
  225. foreach($rows as $krow) {
  226. $result = pdo_insert('rule_keyword', $krow);
  227. }
  228. $reply = array(
  229. 'rid' => $rid,
  230. 'description' => htmlspecialchars_decode($_GPC['description']),
  231. 'apiurl' => empty($_GPC['type']) ? $_GPC['apilocal'] : $_GPC['apiurl'],
  232. 'token' => $_GPC['wetoken'],
  233. 'default_text' => $_GPC['default-text'],
  234. 'cachetime' => intval($_GPC['cachetime']),
  235. );
  236. if($_GPC['id']) {
  237. if(pdo_update($this->tablename, $reply, array('rid' => $rid)) !== false) {
  238. itoast('编辑服务成功. ', 'refresh', 'success');
  239. } else {
  240. itoast('编辑服务失败, 请稍后重试. ', '', 'error');
  241. }
  242. } else {
  243. if(pdo_insert($this->tablename, $reply)) {
  244. itoast('增加服务成功. ', 'refresh', 'success');
  245. } else {
  246. itoast('增加服务失败, 请稍后重试. ', '', 'error');
  247. }
  248. }
  249. }
  250. $rid = intval($_GPC['id']);
  251. if (!empty($rid)) {
  252. $rule = rule_single($rid);
  253. $rule['kwd-adv'] = false;
  254. $rule['keywords'] = array();
  255. foreach($rule['keyword'] as $kwd) {
  256. if($kwd['type'] != '1') {
  257. $rule['kwd-adv'] = true;
  258. } else {
  259. $rule['keywords'][] = $kwd['content'];
  260. }
  261. }
  262. $rule['keywords'] = implode(',', $rule['keywords']);
  263. $row = pdo_fetch("SELECT * FROM ".tablename($this->tablename)." WHERE rid = :rid ORDER BY `id` DESC", array(':rid' => $rid));
  264. if (!strexists($row['apiurl'], 'http://') && !strexists($row['apiurl'], 'https://')) {
  265. $row['apilocal'] = $row['apiurl'];
  266. $row['apiurl'] = '';
  267. }
  268. } else {
  269. $row = array(
  270. 'cachetime' => 0,
  271. );
  272. }
  273. $types = array(
  274. 2 => array(
  275. 'name' => '包含关键字',
  276. 'description' => '用户进行交谈时,对话中包含上述关键字就执行这条规则。',
  277. ),
  278. 3 => array(
  279. 'name' => '正则表达式匹配',
  280. 'description' => "用户进行交谈时,对话内容符合述关键字中定义的模式才会执行这条规则。<br/><strong>注意:如果你不明白正则表达式的工作方式,请不要使用正则匹配</strong> <br/><strong>注意:正则匹配使用MySQL的匹配引擎,请使用MySQL的正则语法</strong> <br /><br /><strong>示例: </strong><br/><b>^微擎</b>匹配以“微擎”开头的语句<br /><b>微擎$</b>匹配以“微擎”结尾的语句<br /><b>^微擎$</b>匹配等同“微擎”的语句<br /><b>微擎</b>匹配包含“微擎”的语句<br /><b>[0-9\.\-]</b>匹配所有的数字,句号和减号<br /><b>^[a-zA-Z_]$</b>所有的字母和下划线<br /><b>^[[:alpha:]]{3}$</b>所有的3个字母的单词<br /><b>^a{4}$</b>aaaa<br /><b>^a{2,4}$</b>aa,aaa或aaaa<br /><b>^a{2,}$</b>匹配多于两个a的字符串",
  281. ),
  282. 4 => array(
  283. 'name' => '直接接管',
  284. 'description' => "如果没有比这条回复优先级更高的回复被触发,那么直接使用这条回复。<br/><strong>注意:如果你不明白这个机制的工作方式,请不要使用直接接管</strong>",
  285. )
  286. );
  287. $path = IA_ROOT . '/source/modules/userapi/api';
  288. if (is_dir($path)) {
  289. $apis = array();
  290. if ($handle = opendir($path)) {
  291. while (false !== ($file = readdir($handle))) {
  292. if ($file != "." && $file != "..") {
  293. $apis[] = $file;
  294. }
  295. }
  296. }
  297. }
  298. include $this->template('post');
  299. }
  300. }
  301. }