You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
4.8 KiB

1 year ago
  1. #coding: utf-8
  2. import public,os,sys,json
  3. #获取插件列表(0/1)
  4. def get_plugin_list(force = 0):
  5. api_root_url = 'https://api.bt.cn'
  6. api_url = api_root_url+ '/wpanel/get_plugin_list'
  7. cache_file = 'data/plugin_list.json'
  8. if force==0 and os.path.exists(cache_file):
  9. jsonData = public.readFile(cache_file)
  10. softList = json.loads(jsonData)
  11. else:
  12. try:
  13. jsonData = public.HttpGet(api_url)
  14. except Exception as ex:
  15. raise public.error_conn_cloud(str(ex))
  16. softList = json.loads(jsonData)
  17. if type(softList)!=dict or 'list' not in softList: raise Exception('云端插件列表获取失败')
  18. public.writeFile(cache_file, jsonData)
  19. return softList
  20. #获取授权状态() 返回:0.免费版 1.专业版 2.企业版 -1.获取失败
  21. def get_auth_state():
  22. try:
  23. softList = get_plugin_list()
  24. if softList['ltd'] > -1:
  25. return 2
  26. elif softList['pro'] > -1:
  27. return 1
  28. else:
  29. return 0
  30. except:
  31. return -1
  32. #执行插件方法(插件名,方法名,参数)
  33. def plugin_run(plugin_name, def_name, args):
  34. if not plugin_name or not def_name: return public.returnMsg(False,'插件名称和插件方法名称不能为空!')
  35. if not path_check(plugin_name) or not path_check(def_name): return public.returnMsg(False,'插件名或方法名不能包含特殊符号!')
  36. p_path = public.get_plugin_path(plugin_name)
  37. if not os.path.exists(p_path + '/index.php') and not os.path.exists(p_path + '/%s_main.py' % plugin_name): return public.returnMsg(False,'插件不存在!')
  38. is_php = os.path.exists(p_path + '/index.php')
  39. if not is_php:
  40. sys.path.append(p_path)
  41. plugin_main = __import__(plugin_name + '_main')
  42. try:
  43. if sys.version_info[0] == 2:
  44. reload(plugin_main)
  45. else:
  46. from imp import reload
  47. reload(plugin_main)
  48. except:
  49. pass
  50. plu = eval('plugin_main.' + plugin_name + '_main()')
  51. if not hasattr(plu, def_name):
  52. return public.returnMsg(False,'在[%s]插件中找不到[%s]方法' % (plugin_name,def_name))
  53. if 'plugin_get_object' in args and args.plugin_get_object == 1:
  54. if not is_php:
  55. return getattr(plu, def_name)
  56. else:
  57. return None
  58. else:
  59. if not is_php:
  60. data = eval('plu.' + def_name + '(args)')
  61. else:
  62. import panelPHP
  63. args.s = def_name
  64. args.name = plugin_name
  65. data = panelPHP.panelPHP(plugin_name).exec_php_script(args)
  66. return data
  67. #执行模块方法(模块名,方法名,参数)
  68. def module_run(mod_name, def_name, args):
  69. if not mod_name or not def_name: return public.returnMsg(False,'模块名称和模块方法名称不能为空!')
  70. if not path_check(mod_name) or not path_check(def_name): return public.returnMsg(False,'模块名或方法名不能包含特殊符号!')
  71. if 'model_index' in args:
  72. if args.model_index:
  73. mod_file = "{}/{}Model/{}Model.py".format(public.get_class_path(),args.model_index,mod_name)
  74. else:
  75. mod_file = "{}/projectModel/{}Model.py".format(public.get_class_path(),mod_name)
  76. else:
  77. module_list = get_module_list()
  78. for module_dir in module_list:
  79. mod_file = "{}/{}/{}Model.py".format(public.get_class_path(),module_dir,mod_name)
  80. if os.path.exists(mod_file): break
  81. if not os.path.exists(mod_file):
  82. return public.returnMsg(False,'模块[%s]不存在' % mod_name)
  83. def_object = public.get_script_object(mod_file)
  84. if not def_object: return public.returnMsg(False,'模块[%s]不存在!' % mod_name)
  85. try:
  86. run_object = getattr(def_object.main(),def_name,None)
  87. except:
  88. return public.returnMsg(False,'模块入口实例化失败' % mod_name)
  89. if not run_object: return public.returnMsg(False,'在[%s]模块中找不到[%s]方法' % (mod_name,def_name))
  90. if 'module_get_object' in args and args.module_get_object == 1:
  91. return run_object
  92. result = run_object(args)
  93. return result
  94. #获取模块文件夹列表
  95. def get_module_list():
  96. list = []
  97. class_path = public.get_class_path()
  98. f_list = os.listdir(class_path)
  99. for fname in f_list:
  100. f_path = class_path+'/'+fname
  101. if os.path.isdir(f_path) and len(fname) > 6 and fname.find('.') == -1 and fname.find('Model') != -1:
  102. list.append(fname)
  103. return list
  104. #检查路径是否合法
  105. def path_check(path):
  106. list = ["./","..",",",";",":","?","'","\"","<",">","|","\\","\n","\r","\t","\b","\a","\f","\v","*","%","&","$","#","@","!","~","`","^","(",")","+","=","{","}","[","]"]
  107. for i in path:
  108. if i in list:
  109. return False
  110. return True