module.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 UserapiModule extends WeModule {
  8. public $tablename = 'userapi_reply';
  9. public function fieldsFormDisplay($rid = 0) {
  10. global $_W;
  11. if (!empty($rid)) {
  12. $row = table($this->tablename)->where(array('rid' => $rid))->orderby(array('id' => 'DESC'))->get();
  13. $row['type'] = 1;
  14. if (!strexists($row['apiurl'], 'http://') && !strexists($row['apiurl'], 'https://')) {
  15. $row['apilocal'] = $row['apiurl'];
  16. $row['type'] = 0;
  17. $row['apiurl'] = '';
  18. }
  19. } else {
  20. $row = array(
  21. 'cachetime' => 0,
  22. 'type' => 1,
  23. );
  24. }
  25. $path = IA_ROOT . '/framework/builtin/userapi/api/';
  26. if (is_dir($path)) {
  27. $apis = array();
  28. if ($handle = opendir($path)) {
  29. while (false !== ($file = readdir($handle))) {
  30. if ('.' != $file && '..' != $file) {
  31. $apis[] = $file;
  32. }
  33. }
  34. }
  35. }
  36. include $this->template('form');
  37. }
  38. public function fieldsFormValidate($rid = 0) {
  39. global $_GPC;
  40. if (($_GPC['type'] && empty($_GPC['apiurl'])) || (empty($_GPC['type']) && empty($_GPC['apilocal']))) {
  41. itoast('请填写接口地址!', '', '');
  42. }
  43. if ($_GPC['type'] && empty($_GPC['token'])) {
  44. itoast('请填写Token值!', '', '');
  45. }
  46. return '';
  47. }
  48. public function fieldsFormSubmit($rid = 0) {
  49. global $_GPC, $_W;
  50. permission_check_account_user('platform_reply_userapi');
  51. $rid = intval($rid);
  52. $reply = array(
  53. 'rid' => $rid,
  54. 'description' => safe_gpc_string($_GPC['description']),
  55. 'apiurl' => empty($_GPC['type']) ? safe_gpc_string($_GPC['apilocal']) : safe_gpc_url($_GPC['apiurl'], false),
  56. 'token' => safe_gpc_string($_GPC['wetoken']),
  57. 'default_text' => safe_gpc_string($_GPC['default-text']),
  58. 'cachetime' => intval($_GPC['cachetime']),
  59. );
  60. $rule_exists = table('rule')->getById($rid, $_W['uniacid']);
  61. if (empty($rule_exists)) {
  62. return false;
  63. }
  64. $is_exists = table($this->tablename)->where(array('rid' => $rid))->getcolumn('id');
  65. if (!empty($is_exists)) {
  66. if (false !== table($this->tablename)->where(array('rid' => $rid))->fill($reply)->save()) {
  67. return true;
  68. }
  69. } else {
  70. if (table($this->tablename)->fill($reply)->save()) {
  71. return true;
  72. }
  73. }
  74. return false;
  75. }
  76. public function ruleDeleted($rid = 0) {
  77. global $_W;
  78. $rid = intval($rid);
  79. permission_check_account_user('platform_reply_userapi');
  80. $rule_exists = table('rule')->getById($rid, $_W['uniacid']);
  81. if (empty($rule_exists)) {
  82. return false;
  83. }
  84. $result = table($this->tablename)->where(array('rid' => $rid))->delete();
  85. return $result;
  86. }
  87. public function doSwitch() {
  88. global $_W, $_GPC;
  89. $m = array_merge($_W['modules']['userapi'], $_W['account']['modules']['userapi']);
  90. $cfg = $m['config'];
  91. if ($_W['ispost']) {
  92. $rids = explode(',', safe_gpc_string($_GPC['rids']));
  93. if (is_array($rids)) {
  94. $cfg = array();
  95. foreach ($rids as $rid) {
  96. $cfg[intval($rid)] = true;
  97. }
  98. $this->saveSettings($cfg);
  99. }
  100. exit();
  101. }
  102. load()->model('reply');
  103. $rs = reply_search("uniacid = 0 AND module = 'userapi' AND `status`=1");
  104. $ds = array();
  105. foreach ($rs as $row) {
  106. $reply = table($this->tablename)->where(array('rid' => $row['id']))->get();
  107. $r = array();
  108. $r['title'] = $row['name'];
  109. $r['rid'] = $row['id'];
  110. $r['description'] = $reply['description'];
  111. $r['switch'] = $cfg[$r['rid']] ? ' checked="checked"' : '';
  112. $ds[] = $r;
  113. }
  114. include $this->template('switch');
  115. }
  116. }