mdserver-web/plugins/phpmyadmin/index.py

461 lines
11 KiB
Python
Raw Normal View History

2019-01-07 06:56:53 -05:00
# coding:utf-8
import sys
import io
import os
import time
import re
import json
2019-01-07 06:56:53 -05:00
sys.path.append(os.getcwd() + "/class/core")
2020-07-10 03:57:25 -04:00
import mw
2019-01-07 06:56:53 -05:00
import site_api
app_debug = False
2020-07-10 03:57:25 -04:00
if mw.isAppleSystem():
2019-01-07 06:56:53 -05:00
app_debug = True
def getPluginName():
return 'phpmyadmin'
def getPluginDir():
2020-07-10 03:57:25 -04:00
return mw.getPluginDir() + '/' + getPluginName()
2019-01-07 06:56:53 -05:00
def getServerDir():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/' + getPluginName()
2019-01-07 06:56:53 -05:00
def getArgs():
args = sys.argv[2:]
tmp = {}
args_len = len(args)
if args_len == 1:
t = args[0].strip('{').strip('}')
t = t.split(':')
tmp[t[0]] = t[1]
elif args_len > 1:
for i in range(len(args)):
t = args[i].split(':')
tmp[t[0]] = t[1]
return tmp
def checkArgs(data, ck=[]):
for i in range(len(ck)):
if not ck[i] in data:
return (False, mw.returnJson(False, '参数:(' + ck[i] + ')没有!'))
return (True, mw.returnJson(True, 'ok'))
2019-01-07 06:56:53 -05:00
def getConf():
2020-07-10 03:57:25 -04:00
return mw.getServerDir() + '/web_conf/nginx/vhost/phpmyadmin.conf'
2019-01-07 06:56:53 -05:00
2019-01-07 22:35:36 -05:00
2022-06-24 23:04:58 -04:00
def getConfInc():
2022-10-29 08:12:01 -04:00
return getServerDir() + "/" + getCfg()['path'] + '/config.inc.php'
2022-06-24 23:04:58 -04:00
2019-01-07 22:35:36 -05:00
def getPort():
file = getConf()
2020-07-10 03:57:25 -04:00
content = mw.readFile(file)
2019-01-07 22:35:36 -05:00
rep = 'listen\s*(.*);'
tmp = re.search(rep, content)
return tmp.groups()[0].strip()
def getHomePage():
2019-01-08 01:51:45 -05:00
try:
port = getPort()
ip = '127.0.0.1'
2020-07-10 03:57:25 -04:00
if not mw.isAppleSystem():
ip = mw.getLocalIp()
2022-10-29 08:12:01 -04:00
url = 'http://' + ip + ':' + port + \
'/' + getCfg()['path'] + '/index.php'
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', url)
2019-01-08 01:51:45 -05:00
except Exception as e:
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '插件未启动!')
2019-01-07 06:56:53 -05:00
2022-11-01 02:09:49 -04:00
def getPhpVer(expect=55):
2019-01-07 06:56:53 -05:00
v = site_api.site_api().getPhpVersion()
2022-11-01 02:09:49 -04:00
is_find = False
2019-01-07 06:56:53 -05:00
for i in range(len(v)):
t = int(v[i]['version'])
2022-11-01 01:55:50 -04:00
if (t == expect):
2022-11-01 02:09:49 -04:00
is_find = True
2019-01-07 06:56:53 -05:00
return str(t)
2022-11-01 02:09:49 -04:00
if not is_find:
2022-11-01 02:29:31 -04:00
if len(v) > 1:
return v[1]['version']
return v[0]['version']
2019-01-07 06:56:53 -05:00
return str(expect)
def getCachePhpVer():
cacheFile = getServerDir() + '/php.pl'
v = ''
if os.path.exists(cacheFile):
2020-07-10 03:57:25 -04:00
v = mw.readFile(cacheFile)
2019-01-07 06:56:53 -05:00
else:
v = getPhpVer()
2020-07-10 03:57:25 -04:00
mw.writeFile(cacheFile, v)
2019-01-07 06:56:53 -05:00
return v
def contentReplace(content):
2020-07-10 03:57:25 -04:00
service_path = mw.getServerDir()
2019-01-07 06:56:53 -05:00
php_ver = getCachePhpVer()
2022-07-09 01:20:22 -04:00
tmp = mw.execShell(
'cat /dev/urandom | head -n 32 | md5sum | head -c 16')
blowfish_secret = tmp[0].strip()
2019-01-07 06:56:53 -05:00
# print php_ver
2022-06-28 12:39:10 -04:00
php_conf_dir = mw.getServerDir() + '/web_conf/php/conf'
2020-07-10 03:57:25 -04:00
content = content.replace('{$ROOT_PATH}', mw.getRootDir())
2019-01-07 06:56:53 -05:00
content = content.replace('{$SERVER_PATH}', service_path)
2022-06-28 12:39:10 -04:00
content = content.replace('{$PHP_CONF_PATH}', php_conf_dir)
2019-01-07 06:56:53 -05:00
content = content.replace('{$PHP_VER}', php_ver)
2022-07-09 01:20:22 -04:00
content = content.replace('{$BLOWFISH_SECRET}', blowfish_secret)
cfg = getCfg()
2022-11-02 06:22:21 -04:00
2022-11-06 22:50:32 -05:00
if cfg['choose'] == "mysql":
content = content.replace('{$CHOOSE_DB}', 'mysql')
content = content.replace('{$CHOOSE_DB_DIR}', 'mysql')
2022-11-06 22:50:32 -05:00
elif cfg['choose'] == "mysql-apt":
2022-11-06 22:50:13 -05:00
content = content.replace('{$CHOOSE_DB}', 'mysql')
content = content.replace('{$CHOOSE_DB_DIR}', 'mysql-apt')
2022-11-06 22:50:32 -05:00
elif cfg['choose'] == "mysql-yum":
2022-11-06 22:50:13 -05:00
content = content.replace('{$CHOOSE_DB}', 'mysql')
content = content.replace('{$CHOOSE_DB_DIR}', 'mysql-yum')
else:
content = content.replace('{$CHOOSE_DB}', 'MariaDB')
content = content.replace('{$CHOOSE_DB_DIR}', 'mariadb')
2022-10-29 08:12:01 -04:00
content = content.replace('{$PMA_PATH}', cfg['path'])
port = cfg["port"]
rep = 'listen\s*(.*);'
content = re.sub(rep, "listen " + port + ';', content)
2019-01-07 06:56:53 -05:00
return content
def initCfg():
cfg = getServerDir() + "/cfg.json"
if not os.path.exists(cfg):
data = {}
data['port'] = '888'
2022-11-04 11:18:32 -04:00
data['choose'] = 'mysql'
2022-10-29 08:12:01 -04:00
data['path'] = ''
data['username'] = 'admin'
data['password'] = 'admin'
mw.writeFile(cfg, json.dumps(data))
def setCfg(key, val):
cfg = getServerDir() + "/cfg.json"
data = mw.readFile(cfg)
data = json.loads(data)
data[key] = val
mw.writeFile(cfg, json.dumps(data))
def getCfg():
cfg = getServerDir() + "/cfg.json"
data = mw.readFile(cfg)
data = json.loads(data)
return data
2022-08-13 07:14:55 -04:00
def returnCfg():
cfg = getServerDir() + "/cfg.json"
data = mw.readFile(cfg)
return data
2019-01-07 06:56:53 -05:00
def status():
conf = getConf()
2022-10-29 08:12:01 -04:00
conf_inc = getServerDir() + "/" + getCfg()["path"] + '/config.inc.php'
2022-06-29 07:51:09 -04:00
# 两个文件都在,才算启动成功
if os.path.exists(conf) and os.path.exists(conf_inc):
2019-01-07 06:56:53 -05:00
return 'start'
return 'stop'
def start():
initCfg()
2022-10-29 08:12:01 -04:00
pma_dir = getServerDir() + "/phpmyadmin"
if os.path.exists(pma_dir):
rand_str = mw.getRandomString(6)
rand_str = rand_str.lower()
pma_dir_dst = pma_dir + "_" + rand_str
mw.execShell("mv " + pma_dir + " " + pma_dir_dst)
setCfg('path', 'phpmyadmin_' + rand_str)
2019-01-07 06:56:53 -05:00
file_tpl = getPluginDir() + '/conf/phpmyadmin.conf'
2019-01-09 00:51:14 -05:00
file_run = getConf()
if not os.path.exists(file_run):
2020-07-10 03:57:25 -04:00
centent = mw.readFile(file_tpl)
2019-01-08 04:44:30 -05:00
centent = contentReplace(centent)
2020-07-10 03:57:25 -04:00
mw.writeFile(file_run, centent)
2019-01-09 00:51:14 -05:00
pma_path = getServerDir() + '/pma.pass'
if not os.path.exists(pma_path):
username = mw.getRandomString(10)
pass_cmd = username + ':' + mw.hasPwd(username)
setCfg('username', username)
setCfg('password', username)
mw.writeFile(pma_path, pass_cmd)
2022-10-29 08:12:01 -04:00
tmp = getServerDir() + "/" + getCfg()["path"] + '/tmp'
2022-06-24 22:54:24 -04:00
if not os.path.exists(tmp):
os.mkdir(tmp)
2022-10-29 08:12:01 -04:00
mw.execShell("chown -R www:www " + tmp)
2022-06-24 22:54:24 -04:00
2022-10-29 08:12:01 -04:00
conf_run = getServerDir() + "/" + getCfg()["path"] + '/config.inc.php'
2019-01-09 00:51:14 -05:00
if not os.path.exists(conf_run):
conf_tpl = getPluginDir() + '/conf/config.inc.php'
2020-07-10 03:57:25 -04:00
centent = mw.readFile(conf_tpl)
2022-06-19 00:41:40 -04:00
centent = contentReplace(centent)
mw.writeFile(conf_run, centent)
2019-01-08 04:44:30 -05:00
2022-08-05 23:47:13 -04:00
log_a = accessLog()
log_e = errorLog()
for i in [log_a, log_e]:
if os.path.exists(i):
cmd = "echo '' > " + i
mw.execShell(cmd)
2020-07-10 03:57:25 -04:00
mw.restartWeb()
2019-01-07 06:56:53 -05:00
return 'ok'
def stop():
conf = getConf()
if os.path.exists(conf):
os.remove(conf)
2020-07-10 03:57:25 -04:00
mw.restartWeb()
2019-01-07 06:56:53 -05:00
return 'ok'
def restart():
return start()
def reload():
2022-10-29 08:12:01 -04:00
file_tpl = getPluginDir() + '/conf/phpmyadmin.conf'
file_run = getConf()
if os.path.exists(file_run):
centent = mw.readFile(file_tpl)
centent = contentReplace(centent)
mw.writeFile(file_run, centent)
2019-01-07 06:56:53 -05:00
return start()
2019-01-08 04:23:05 -05:00
def setPhpVer():
args = getArgs()
if not 'phpver' in args:
return 'phpver missing'
cacheFile = getServerDir() + '/php.pl'
2020-07-10 03:57:25 -04:00
mw.writeFile(cacheFile, args['phpver'])
2020-07-12 11:54:01 -04:00
file_tpl = getPluginDir() + '/conf/phpmyadmin.conf'
file_run = getConf()
content = mw.readFile(file_tpl)
content = contentReplace(content)
mw.writeFile(file_run, content)
2020-07-12 11:54:01 -04:00
mw.restartWeb()
2019-01-08 04:23:05 -05:00
return 'ok'
def getSetPhpVer():
cacheFile = getServerDir() + '/php.pl'
if os.path.exists(cacheFile):
2020-07-10 03:57:25 -04:00
return mw.readFile(cacheFile).strip()
2019-01-08 04:23:05 -05:00
return ''
def getPmaOption():
data = getCfg()
return mw.returnJson(True, 'ok', data)
2019-01-08 04:23:05 -05:00
def getPmaPort():
try:
port = getPort()
2020-07-10 03:57:25 -04:00
return mw.returnJson(True, 'OK', port)
2019-01-08 04:23:05 -05:00
except Exception as e:
2021-05-09 12:02:05 -04:00
# print(e)
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '插件未启动!')
2019-01-08 04:23:05 -05:00
2019-01-08 04:44:30 -05:00
def setPmaPort():
args = getArgs()
data = checkArgs(args, ['port'])
if not data[0]:
return data[1]
2019-01-08 04:44:30 -05:00
port = args['port']
if port == '80':
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '80端不能使用!')
2019-01-08 04:44:30 -05:00
file = getConf()
if not os.path.exists(file):
2020-07-10 03:57:25 -04:00
return mw.returnJson(False, '插件未启动!')
content = mw.readFile(file)
2019-01-08 04:44:30 -05:00
rep = 'listen\s*(.*);'
content = re.sub(rep, "listen " + port + ';', content)
2020-07-10 03:57:25 -04:00
mw.writeFile(file, content)
setCfg("port", port)
2020-07-10 03:57:25 -04:00
mw.restartWeb()
return mw.returnJson(True, '修改成功!')
2019-01-08 04:44:30 -05:00
def setPmaChoose():
args = getArgs()
data = checkArgs(args, ['choose'])
if not data[0]:
return data[1]
choose = args['choose']
setCfg('choose', choose)
2022-11-02 06:22:21 -04:00
pma_path = getCfg()['path']
conf_run = getServerDir() + "/" + pma_path + '/config.inc.php'
conf_tpl = getPluginDir() + '/conf/config.inc.php'
2022-11-02 06:22:21 -04:00
content = mw.readFile(conf_tpl)
content = contentReplace(content)
mw.writeFile(conf_run, content)
mw.restartWeb()
return mw.returnJson(True, '修改成功!')
def setPmaUsername():
args = getArgs()
data = checkArgs(args, ['username'])
if not data[0]:
return data[1]
username = args['username']
setCfg('username', username)
cfg = getCfg()
pma_path = getServerDir() + '/pma.pass'
username = mw.getRandomString(10)
2022-08-13 03:16:16 -04:00
pass_cmd = cfg['username'] + ':' + mw.hasPwd(cfg['password'])
mw.writeFile(pma_path, pass_cmd)
mw.restartWeb()
return mw.returnJson(True, '修改成功!')
def setPmaPassword():
args = getArgs()
data = checkArgs(args, ['password'])
if not data[0]:
return data[1]
password = args['password']
setCfg('password', password)
cfg = getCfg()
pma_path = getServerDir() + '/pma.pass'
username = mw.getRandomString(10)
2022-08-13 03:16:16 -04:00
pass_cmd = cfg['username'] + ':' + mw.hasPwd(cfg['password'])
mw.writeFile(pma_path, pass_cmd)
mw.restartWeb()
return mw.returnJson(True, '修改成功!')
2022-10-29 08:12:01 -04:00
def setPmaPath():
args = getArgs()
data = checkArgs(args, ['path'])
if not data[0]:
return data[1]
path = args['path']
2022-10-29 08:36:37 -04:00
if len(path) < 5:
return mw.returnJson(False, '不能小于5位!')
2022-10-29 08:12:01 -04:00
old_path = getServerDir() + "/" + getCfg()['path']
new_path = getServerDir() + "/" + path
mw.execShell("mv " + old_path + " " + new_path)
setCfg('path', path)
return mw.returnJson(True, '修改成功!')
2022-06-19 00:40:21 -04:00
def accessLog():
2022-08-05 23:47:13 -04:00
return getServerDir() + '/access.log'
2022-06-19 00:40:21 -04:00
def errorLog():
2022-08-05 23:47:13 -04:00
return getServerDir() + '/error.log'
2022-06-19 00:40:21 -04:00
def Version():
return mw.readFile(getServerDir() + '/version.pl')
2019-01-07 06:56:53 -05:00
if __name__ == "__main__":
func = sys.argv[1]
if func == 'status':
2021-05-08 13:11:18 -04:00
print(status())
2019-01-07 06:56:53 -05:00
elif func == 'start':
2021-05-08 13:11:18 -04:00
print(start())
2019-01-07 06:56:53 -05:00
elif func == 'stop':
2021-05-08 13:11:18 -04:00
print(stop())
2019-01-07 06:56:53 -05:00
elif func == 'restart':
2021-05-08 13:11:18 -04:00
print(restart())
2019-01-07 06:56:53 -05:00
elif func == 'reload':
2021-05-08 13:11:18 -04:00
print(reload())
2019-01-07 06:56:53 -05:00
elif func == 'conf':
2021-05-08 13:11:18 -04:00
print(getConf())
elif func == 'version':
print(Version())
2022-08-13 07:14:55 -04:00
elif func == 'get_cfg':
print(returnCfg())
2022-06-24 23:04:58 -04:00
elif func == 'config_inc':
print(getConfInc())
2019-01-07 22:35:36 -05:00
elif func == 'get_home_page':
2021-05-08 13:11:18 -04:00
print(getHomePage())
2019-01-08 04:23:05 -05:00
elif func == 'set_php_ver':
2021-05-08 13:11:18 -04:00
print(setPhpVer())
2019-01-08 04:23:05 -05:00
elif func == 'get_set_php_ver':
2021-05-08 13:11:18 -04:00
print(getSetPhpVer())
2019-01-08 04:23:05 -05:00
elif func == 'get_pma_port':
2021-05-08 13:11:18 -04:00
print(getPmaPort())
2019-01-08 04:44:30 -05:00
elif func == 'set_pma_port':
2021-05-08 13:11:18 -04:00
print(setPmaPort())
elif func == 'get_pma_option':
print(getPmaOption())
elif func == 'set_pma_choose':
print(setPmaChoose())
elif func == 'set_pma_username':
print(setPmaUsername())
elif func == 'set_pma_password':
2022-08-13 03:16:16 -04:00
print(setPmaPassword())
2022-10-29 08:12:01 -04:00
elif func == 'set_pma_path':
print(setPmaPath())
2022-06-19 00:40:21 -04:00
elif func == 'access_log':
print(accessLog())
elif func == 'error_log':
print(errorLog())
2019-01-07 06:56:53 -05:00
else:
2021-05-08 13:11:18 -04:00
print('error')