人人商城

service.ctrl.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. if(empty($_W['isfounder'])) {
  8. $dos = array('switch');
  9. } else {
  10. $dos = array('switch', 'display', 'delete', 'post', 'import');
  11. }
  12. $do = in_array($do, $dos) ? $do : 'switch';
  13. load()->model('extension');
  14. load()->model('reply');
  15. load()->model('module');
  16. $predefines = s_predefines();
  17. if($do == 'switch') {
  18. global $_W, $_GPC;
  19. $m = module_fetch('userapi');
  20. $cfg = $m['config'];
  21. if($_W['ispost']) {
  22. $rids = explode(',', $_GPC['rids']);
  23. if(is_array($rids)) {
  24. $cfg = array();
  25. foreach($rids as $rid) {
  26. $cfg[intval($rid)] = true;
  27. }
  28. $this->saveSettings($cfg);
  29. }
  30. exit();
  31. }
  32. load()->model('reply');
  33. $rs = reply_search("weid = 0 AND module = 'userapi' AND `status`=1");
  34. $ds = array();
  35. foreach($rs as $row) {
  36. $reply = pdo_fetch('SELECT * FROM ' . tablename('userapi_reply') . ' WHERE `rid`=:rid', array(':rid' => $row['id']));
  37. $r = array();
  38. $r['title'] = $row['name'];
  39. $r['rid'] = $row['id'];
  40. $r['description'] = $reply['description'];
  41. $r['switch'] = $cfg[$r['rid']] ? ' checked="checked"' : '';
  42. $ds[] = $r;
  43. }
  44. template('extension/switch');
  45. }
  46. if($do == 'display') {
  47. $_W['page']['title'] = '管理服务 - 常用服务 - 扩展';
  48. load()->model('reply');
  49. $pindex = max(1, intval($_GPC['page']));
  50. $psize = 20;
  51. $types = array('', '等价', '包含', '正则表达式匹配');
  52. $condition = 'uniacid = 0 AND module = \'userapi\'';
  53. $params = array();
  54. if (isset($_GPC['status'])) {
  55. $condition .= " AND status = :status";
  56. $params[':status'] = intval($_GPC['status']);
  57. }
  58. if(isset($_GPC['keyword'])) {
  59. $condition .= ' AND `name` LIKE :keyword';
  60. $params[':keyword'] = "%{$_GPC['keyword']}%";
  61. }
  62. $ds = reply_search($condition, $params, $pindex, $psize, $total);
  63. $pager = pagination($total, $pindex, $psize);
  64. if (!empty($ds)) {
  65. foreach($ds as &$item) {
  66. $reply = pdo_fetch('SELECT * FROM ' . tablename('userapi_reply') . ' WHERE `rid`=:rid', array(':rid' => $item['id']));
  67. $item['description'] = $reply['description'];
  68. }
  69. }
  70. $import = false;
  71. $apis = implode('\',\'', array_keys($predefines));
  72. $apis = "'{$apis}'";
  73. $sql = 'SELECT DISTINCT `apiurl` FROM ' . tablename('userapi_reply') . ' AS `e` LEFT JOIN ' . tablename('rule') . " AS `r` ON (`e`.`rid`=`r`.`id`) WHERE `r`.`uniacid`='0' AND `apiurl` IN ({$apis})";
  74. $apiurls = pdo_fetchall($sql);
  75. if(count($apiurls) != count($predefines)) {
  76. $import = true;
  77. }
  78. template('extension/service');
  79. }
  80. if($do == 'import') {
  81. $apis = implode('\',\'', array_keys($predefines));
  82. $apis = "'{$apis}'";
  83. $sql = 'SELECT DISTINCT `apiurl` FROM ' . tablename('userapi_reply') . ' AS `e` LEFT JOIN ' . tablename('rule') . " AS `r` ON (`e`.`rid`=`r`.`id`) WHERE `r`.`uniacid`='0' AND `apiurl` IN ({$apis})";
  84. $apiurls = pdo_fetchall($sql);
  85. $as = array();
  86. foreach($apiurls as $url) {
  87. $as[] = $url['apiurl'];
  88. }
  89. foreach($predefines as $key => $v) {
  90. if(!in_array($key, $as)) {
  91. $rule = array(
  92. 'uniacid' => 0,
  93. 'name' => $v['title'],
  94. 'module' => 'userapi',
  95. 'displayorder' => 255,
  96. 'status' => 1,
  97. );
  98. pdo_insert('rule', $rule);
  99. $rid = pdo_insertid();
  100. if(!empty($rid)) {
  101. foreach($v['keywords'] as $row) {
  102. $data = array(
  103. 'content' => $row[1],
  104. 'type' => $row[0],
  105. 'rid' => $rid,
  106. 'uniacid' => 0,
  107. 'module' => 'userapi',
  108. 'status' => $rule['status'],
  109. 'displayorder' => $rule['displayorder'],
  110. );
  111. pdo_insert('rule_keyword', $data);
  112. }
  113. $reply = array(
  114. 'rid' => $rid,
  115. 'description' => htmlspecialchars($v['description']),
  116. 'apiurl' => $key,
  117. 'token' => '',
  118. 'default_text' => '',
  119. 'cachetime' => 0
  120. );
  121. pdo_insert('userapi_reply', $reply);
  122. }
  123. }
  124. }
  125. message('成功导入.', referer());
  126. }
  127. if($do == 'delete') {
  128. $rid = intval($_GPC['rid']);
  129. $sql = 'DELETE FROM ' . tablename('rule') . " WHERE `uniacid`=0 AND `module`='userapi' AND `id`={$rid}";
  130. pdo_query($sql);
  131. $sql = 'DELETE FROM ' . tablename('rule_keyword') . " WHERE `uniacid`=0 AND `module`='userapi' AND `rid`={$rid}";
  132. pdo_query($sql);
  133. $sql = 'DELETE FROM ' . tablename('userapi_reply') . " WHERE `rid`={$rid}";
  134. pdo_query($sql);
  135. message('成功删除.', referer());
  136. }
  137. if($do == 'post') {
  138. $rid = intval($_GPC['rid']);
  139. $_W['page']['title'] = $rid ? '编辑常用服务 - 常用服务 - 扩展' : '添加常用服务 - 常用服务 - 扩展';
  140. $m = 'userapi';
  141. if(!empty($rid)) {
  142. $reply = reply_single($rid);
  143. $reply['description'] = pdo_fetchcolumn('SELECT description FROM ' . tablename('userapi_reply') . ' WHERE rid = :rid', array(':rid' => $rid));
  144. if(empty($reply)) {
  145. message('抱歉,您操作的服务不在存或是已经被删除!', url('extension/service', array('m' => $m)), 'error');
  146. }
  147. foreach($reply['keywords'] as &$kw) {
  148. $kw = array_elements(array('type', 'content'), $kw);
  149. }
  150. }
  151. if(checksubmit('submit')) {
  152. if(empty($_GPC['name'])) {
  153. message('必须填写服务名称.');
  154. }
  155. $keywords = @json_decode(htmlspecialchars_decode($_GPC['keywords']), true);
  156. if(empty($keywords)) {
  157. message('必须填写有效的触发关键字.');
  158. }
  159. $rule = array(
  160. 'uniacid' => 0,
  161. 'name' => $_GPC['name'],
  162. 'module' => $m,
  163. 'status' => intval($_GPC['status']),
  164. );
  165. if(!empty($_GPC['istop']) && $_GPC['istop'] == 'true') {
  166. $rule['displayorder'] = 255;
  167. } else {
  168. $rule['displayorder'] = range_limit($_GPC['displayorder'], 0, 254);
  169. }
  170. $module = WeUtility::createModule($m);
  171. if(empty($module)) {
  172. message('抱歉,模块不存在请重新其它模块!');
  173. }
  174. $msg = $module->fieldsFormValidate();
  175. if(is_string($msg) && trim($msg) != '') {
  176. message($msg);
  177. }
  178. if (!empty($rid)) {
  179. $result = pdo_update('rule', $rule, array('id' => $rid));
  180. } else {
  181. $result = pdo_insert('rule', $rule);
  182. $rid = pdo_insertid();
  183. }
  184. if (!empty($rid)) {
  185. $sql = 'DELETE FROM '. tablename('rule_keyword') . ' WHERE `rid`=:rid AND `uniacid`=:uniacid';
  186. $pars = array();
  187. $pars[':rid'] = $rid;
  188. $pars[':uniacid'] = 0;
  189. pdo_query($sql, $pars);
  190. $rowtpl = array(
  191. 'rid' => $rid,
  192. 'uniacid' => 0,
  193. 'module' => $rule['module'],
  194. 'status' => $rule['status'],
  195. 'displayorder' => $rule['displayorder'],
  196. );
  197. foreach($keywords as $kw) {
  198. $krow = $rowtpl;
  199. $krow['type'] = range_limit($kw['type'], 1, 4);
  200. $krow['content'] = $kw['content'];
  201. pdo_insert('rule_keyword', $krow);
  202. }
  203. $module->fieldsFormSubmit($rid);
  204. message('服务保存成功!', url('extension/service/post', array('m' => $m, 'rid' => $rid)));
  205. } else {
  206. message('服务保存失败, 请联系网站管理员!');
  207. }
  208. }
  209. template('extension/service-post');
  210. }
  211. function s_predefines() {
  212. $predefines = array(
  213. 'weather.php' => array(
  214. 'title' => '城市天气',
  215. 'description' => '"城市名+天气", 如: "北京天气"',
  216. 'keywords' => array(
  217. array('3', '^.+天气$')
  218. )
  219. ),
  220. 'baike.php' => array(
  221. 'title' => '百度百科',
  222. 'description' => '"百科+查询内容" 或 "定义+查询内容", 如: "百科姚明", "定义自行车"',
  223. 'keywords' => array(
  224. array('3', '^百科.+$'),
  225. array('3', '^定义.+$'),
  226. )
  227. ),
  228. 'translate.php' => array(
  229. 'title' => '即时翻译',
  230. 'description' => '"@查询内容(中文或英文)"',
  231. 'keywords' => array(
  232. array('3', '^@.+$'),
  233. )
  234. ),
  235. 'calendar.php' => array(
  236. 'title' => '今日老黄历',
  237. 'description' => '"日历", "万年历", "黄历"或"几号"',
  238. 'keywords' => array(
  239. array('1', '日历'),
  240. array('1', '万年历'),
  241. array('1', '黄历'),
  242. array('1', '几号'),
  243. )
  244. ),
  245. 'news.php' => array(
  246. 'title' => '看新闻',
  247. 'description' => '"新闻"',
  248. 'keywords' => array(
  249. array('1', '新闻'),
  250. )
  251. ),
  252. 'express.php' => array(
  253. 'title' => '快递查询',
  254. 'description' => '"快递+单号", 如: "申通1200041125"',
  255. 'keywords' => array(
  256. array('3', '^(申通|圆通|中通|汇通|韵达|顺丰|EMS) *[a-z0-9]{1,}$')
  257. )
  258. ),
  259. );
  260. return $predefines;
  261. }