MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / SyncGetDatabases

Method SyncGetDatabases

class/database.py:2199–2246  ·  view source on GitHub ↗
(self, get)

Source from the content-addressed store, hash-verified

2197
2198 # 从服务器获取数据库
2199 def SyncGetDatabases(self, get):
2200 sid = get.get('sid/d', 0)
2201 db_type = 0
2202 if sid: db_type = 2
2203 if not os.path.exists('/usr/bin/mysql'):
2204 public.install_mysql_client()
2205 mysql_obj = public.get_mysql_obj_by_sid(sid)
2206 if not mysql_obj: return public.returnMsg(False, '连接指定数据库失败')
2207 data = mysql_obj.query("show databases")
2208 isError = self.IsSqlError(data)
2209 if isError != None: return isError
2210 users = mysql_obj.query("select User,Host from mysql.user where User!='root' AND Host!='localhost' AND Host!=''")
2211
2212 if type(users) == str: return public.returnMsg(False, users)
2213 if type(users) != list: return public.returnMsg(False, public.GetMySQLError(users))
2214
2215 sql = public.M('databases')
2216 nameArr = ['information_schema', 'performance_schema', 'mysql', 'sys']
2217 n = 0
2218 for value in data:
2219 b = False
2220 for key in nameArr:
2221 if value[0] == key:
2222 b = True
2223 break
2224 if b: continue
2225 host = self.get_db_host_by_db_name(int(sid))
2226 if db_type == 0:
2227 if sql.where("name=? AND LOWER(type)=LOWER('mysql')", (value[0])).count(): continue
2228 else:
2229 if sql.where("name=? AND LOWER(type)=LOWER('mysql') AND accept=?", (value[0], host)).count(): continue
2230 # host = '127.0.0.1'
2231 # for user in users:
2232 # if value[0] == user[0]:
2233 # print(user)
2234 # host = user[1]
2235 # break
2236 ps = public.getMsg('INPUT_PS')
2237 if value[0] == 'test':
2238 ps = public.getMsg('DATABASE_TEST')
2239
2240 # XSS过虑
2241 if not re.match("^[\w+\.-]+$", value[0]): continue
2242
2243 addTime = time.strftime('%Y-%m-%d %X', time.localtime())
2244
2245 if public.M('databases').add('name,sid,db_type,username,password,accept,ps,addtime', (value[0], sid, db_type, value[0], '', host, ps, addTime)): n += 1
2246 return public.returnMsg(True, 'DATABASE_GET_SUCCESS', (str(n),))
2247
2248 # 获取数据库权限
2249 def GetDatabaseAccess(self, get):

Callers 1

backup_database_allMethod · 0.45

Calls 12

IsSqlErrorMethod · 0.95
returnMsgMethod · 0.80
GetMySQLErrorMethod · 0.80
getMethod · 0.45
existsMethod · 0.45
queryMethod · 0.45
MMethod · 0.45
countMethod · 0.45
whereMethod · 0.45
matchMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected