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.

126 lines
4.9 KiB

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