Simple Linux Panel
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.
mdserver-web/plugins/php/index.py

454 lines
14 KiB

7 years ago
# coding:utf-8
import sys
import io
import os
import time
6 years ago
import re
6 years ago
import json
import shutil
6 years ago
reload(sys)
sys.setdefaultencoding('utf8')
7 years ago
sys.path.append(os.getcwd() + "/class/core")
import public
7 years ago
app_debug = False
6 years ago
if public.isAppleSystem():
7 years ago
app_debug = True
def getPluginName():
7 years ago
return 'php'
7 years ago
def getPluginDir():
return public.getPluginDir() + '/' + getPluginName()
def getServerDir():
return public.getServerDir() + '/' + getPluginName()
def getInitDFile():
if app_debug:
return '/tmp/' + getPluginName()
return '/etc/init.d/' + getPluginName()
7 years ago
7 years ago
def getArgs():
6 years ago
args = sys.argv[3:]
7 years ago
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 getConf(version):
6 years ago
path = getServerDir() + '/' + version + '/etc/php.ini'
7 years ago
return path
7 years ago
def status(version):
7 years ago
cmd = "ps -ef|grep 'php/" + version + \
"' |grep -v grep | grep -v python | awk '{print $2}'"
data = public.execShell(cmd)
7 years ago
if data[0] == '':
return 'stop'
return 'start'
7 years ago
def contentReplace(content, version):
7 years ago
service_path = public.getServerDir()
7 years ago
content = content.replace('{$SERVER_PATH}', service_path)
content = content.replace('{$PHP_VERSION}', version)
6 years ago
if public.isAppleSystem():
6 years ago
# user = public.execShell(
# "who | sed -n '2, 1p' |awk '{print $1}'")[0].strip()
content = content.replace('{$PHP_USER}', 'nobody')
content = content.replace('{$PHP_GROUP}', 'nobody')
rep = 'user\s*=\s*(.+)\r?\n'
val = ';user = nobody\n'
content = re.sub(rep, val, content)
rep = 'group\s*=\s*(.+)\r?\n'
val = ';group = nobody\n'
content = re.sub(rep, val, content)
6 years ago
else:
content = content.replace('{$PHP_USER}', 'www')
content = content.replace('{$PHP_GROUP}', 'www')
7 years ago
return content
6 years ago
def makeOpenrestyConf():
if public.isInstalledWeb():
d_pathinfo = public.getServerDir() + '/openresty/nginx/conf/pathinfo.conf'
if not os.path.exists(d_pathinfo):
6 years ago
s_pathinfo = getPluginDir() + '/conf/pathinfo.conf'
6 years ago
shutil.copyfile(s_pathinfo, d_pathinfo)
info = getPluginDir() + '/info.json'
content = public.readFile(info)
content = json.loads(content)
versions = content['versions']
tpl = getPluginDir() + '/conf/enable-php.conf'
tpl_content = public.readFile(tpl)
for x in range(len(versions)):
desc_file = public.getServerDir() + '/openresty/nginx/conf/enable-php-' + \
versions[x] + '.conf'
if not os.path.exists(desc_file):
6 years ago
w_content = contentReplace(tpl_content, versions[x])
6 years ago
public.writeFile(desc_file, w_content)
7 years ago
def phpFpmReplace(version):
6 years ago
desc_php_fpm = getServerDir() + '/' + version + '/etc/php-fpm.conf'
if not os.path.exists(desc_php_fpm):
tpl_php_fpm = getPluginDir() + '/conf/php-fpm.conf'
content = public.readFile(tpl_php_fpm)
content = contentReplace(content, version)
public.writeFile(desc_php_fpm, content)
7 years ago
def phpFpmWwwReplace(version):
7 years ago
service_php_fpm_dir = getServerDir() + '/' + version + '/etc/php-fpm.d/'
6 years ago
7 years ago
if not os.path.exists(service_php_fpm_dir):
os.mkdir(service_php_fpm_dir)
6 years ago
service_php_fpmwww = service_php_fpm_dir + '/www.conf'
if not os.path.exists(service_php_fpmwww):
6 years ago
tpl_php_fpmwww = getPluginDir() + '/conf/www.conf'
content = public.readFile(tpl_php_fpmwww)
content = contentReplace(content, version)
public.writeFile(service_php_fpmwww, content)
7 years ago
6 years ago
def makePhpIni(version):
d_ini = public.getServerDir() + '/php/' + version + '/etc/php.ini'
if not os.path.exists(d_ini):
6 years ago
s_ini = getPluginDir() + '/conf/php' + version + '.ini'
6 years ago
shutil.copyfile(s_ini, d_ini)
6 years ago
def initReplace(version):
makeOpenrestyConf()
6 years ago
makePhpIni(version)
7 years ago
7 years ago
initD_path = getServerDir() + '/init.d'
6 years ago
file_bin = initD_path + '/php' + version
7 years ago
if not os.path.exists(initD_path):
os.mkdir(initD_path)
6 years ago
file_tpl = getPluginDir() + '/init.d/php.tpl'
7 years ago
6 years ago
content = public.readFile(file_tpl)
content = contentReplace(content, version)
7 years ago
6 years ago
public.writeFile(file_bin, content)
public.execShell('chmod +x ' + file_bin)
7 years ago
7 years ago
phpFpmWwwReplace(version)
7 years ago
phpFpmReplace(version)
7 years ago
7 years ago
return file_bin
def phpOp(version, method):
6 years ago
file = initReplace(version)
7 years ago
data = public.execShell(file + ' ' + method)
if data[1] == '':
7 years ago
return 'ok'
6 years ago
return data[1]
7 years ago
7 years ago
def start(version):
return phpOp(version, 'start')
def stop(version):
return phpOp(version, 'stop')
7 years ago
7 years ago
def restart(version):
return phpOp(version, 'restart')
7 years ago
7 years ago
def reload(version):
return phpOp(version, 'reload')
7 years ago
7 years ago
6 years ago
def fpmLog(version):
return getServerDir() + '/' + version + '/var/log/php-fpm.log'
def fpmSlowLog(version):
return getServerDir() + '/' + version + '/var/log/php-fpm-slow.log'
6 years ago
def getPhpConf(version):
gets = [
{'name': 'short_open_tag', 'type': 1, 'ps': '短标签支持'},
{'name': 'asp_tags', 'type': 1, 'ps': 'ASP标签支持'},
{'name': 'max_execution_time', 'type': 2, 'ps': '最大脚本运行时间'},
{'name': 'max_input_time', 'type': 2, 'ps': '最大输入时间'},
{'name': 'memory_limit', 'type': 2, 'ps': '脚本内存限制'},
{'name': 'post_max_size', 'type': 2, 'ps': 'POST数据最大尺寸'},
{'name': 'file_uploads', 'type': 1, 'ps': '是否允许上传文件'},
{'name': 'upload_max_filesize', 'type': 2, 'ps': '允许上传文件的最大尺寸'},
{'name': 'max_file_uploads', 'type': 2, 'ps': '允许同时上传文件的最大数量'},
{'name': 'default_socket_timeout', 'type': 2, 'ps': 'Socket超时时间'},
{'name': 'error_reporting', 'type': 3, 'ps': '错误级别'},
{'name': 'display_errors', 'type': 1, 'ps': '是否输出详细错误信息'},
{'name': 'cgi.fix_pathinfo', 'type': 0, 'ps': '是否开启pathinfo'},
{'name': 'date.timezone', 'type': 3, 'ps': '时区'}
7 years ago
]
6 years ago
phpini = public.readFile(getServerDir() + '/' + version + '/etc/php.ini')
result = []
for g in gets:
rep = g['name'] + '\s*=\s*([0-9A-Za-z_& ~]+)(\s*;?|\r?\n)'
tmp = re.search(rep, phpini)
if not tmp:
7 years ago
continue
6 years ago
g['value'] = tmp.groups()[0]
result.append(g)
7 years ago
return public.getJson(result)
6 years ago
def submitPhpConf(version):
gets = ['display_errors', 'cgi.fix_pathinfo', 'date.timezone', 'short_open_tag',
'asp_tags', 'max_execution_time', 'max_input_time', 'memory_limit',
'post_max_size', 'file_uploads', 'upload_max_filesize', 'max_file_uploads',
'default_socket_timeout', 'error_reporting']
args = getArgs()
filename = getServerDir() + '/' + version + '/etc/php.ini'
phpini = public.readFile(filename)
for g in gets:
if g in args:
rep = g + '\s*=\s*(.+)\r?\n'
val = g + ' = ' + args[g] + '\n'
phpini = re.sub(rep, val, phpini)
public.writeFile(filename, phpini)
public.execShell(getServerDir() + '/init.d/php' + version + ' reload')
return public.returnJson(True, '设置成功')
def getLimitConf(version):
fileini = getServerDir() + "/" + version + "/etc/php.ini"
phpini = public.readFile(fileini)
filefpm = getServerDir() + "/" + version + "/etc/php-fpm.conf"
phpfpm = public.readFile(filefpm)
# print fileini, filefpm
data = {}
try:
rep = "upload_max_filesize\s*=\s*([0-9]+)M"
tmp = re.search(rep, phpini).groups()
data['max'] = tmp[0]
except:
data['max'] = '50'
try:
rep = "request_terminate_timeout\s*=\s*([0-9]+)\n"
tmp = re.search(rep, phpfpm).groups()
data['maxTime'] = tmp[0]
except:
data['maxTime'] = 0
try:
rep = r"\n;*\s*cgi\.fix_pathinfo\s*=\s*([0-9]+)\s*\n"
tmp = re.search(rep, phpini).groups()
if tmp[0] == '1':
data['pathinfo'] = True
else:
data['pathinfo'] = False
except:
data['pathinfo'] = False
return public.getJson(data)
def setMaxTime(version):
args = getArgs()
if not 'time' in args:
return 'missing time args!'
time = args['time']
if int(time) < 30 or int(time) > 86400:
return public.returnJson(False, '请填写30-86400间的值!')
filefpm = getServerDir() + "/" + version + "/etc/php-fpm.conf"
conf = public.readFile(filefpm)
rep = "request_terminate_timeout\s*=\s*([0-9]+)\n"
conf = re.sub(rep, "request_terminate_timeout = " + time + "\n", conf)
public.writeFile(filefpm, conf)
fileini = getServerDir() + "/" + version + "/etc/php.ini"
phpini = public.readFile(fileini)
rep = "max_execution_time\s*=\s*([0-9]+)\r?\n"
phpini = re.sub(rep, "max_execution_time = " + time + "\n", phpini)
rep = "max_input_time\s*=\s*([0-9]+)\r?\n"
phpini = re.sub(rep, "max_input_time = " + time + "\n", phpini)
public.writeFile(fileini, phpini)
return public.returnJson(True, '设置成功!')
def setMaxSize(version):
args = getArgs()
if not 'max' in args:
return 'missing time args!'
max = args['max']
if int(max) < 2:
return public.returnJson(False, '上传大小限制不能小于2MB!')
path = getServerDir() + '/' + version + '/etc/php.ini'
conf = public.readFile(path)
rep = u"\nupload_max_filesize\s*=\s*[0-9]+M"
conf = re.sub(rep, u'\nupload_max_filesize = ' + max + 'M', conf)
rep = u"\npost_max_size\s*=\s*[0-9]+M"
conf = re.sub(rep, u'\npost_max_size = ' + max + 'M', conf)
public.writeFile(path, conf)
# if public.get_webserver() == 'nginx':
# path = web.ctx.session.setupPath + '/nginx/conf/nginx.conf'
# conf = public.readFile(path)
# rep = "client_max_body_size\s+([0-9]+)m"
# tmp = re.search(rep, conf).groups()
# if int(tmp[0]) < int(max):
# conf = re.sub(rep, 'client_max_body_size ' + max + 'm', conf)
# public.writeFile(path, conf)
public.writeLog("TYPE_PHP", "PHP_UPLOAD_MAX", (version, max))
return public.returnJson(True, '设置成功!')
6 years ago
def getFpmConfig(version):
filefpm = getServerDir() + '/' + version + '/etc/php-fpm.d/www.conf'
conf = public.readFile(filefpm)
data = {}
rep = "\s*pm.max_children\s*=\s*([0-9]+)\s*"
tmp = re.search(rep, conf).groups()
data['max_children'] = tmp[0]
rep = "\s*pm.start_servers\s*=\s*([0-9]+)\s*"
tmp = re.search(rep, conf).groups()
data['start_servers'] = tmp[0]
rep = "\s*pm.min_spare_servers\s*=\s*([0-9]+)\s*"
tmp = re.search(rep, conf).groups()
data['min_spare_servers'] = tmp[0]
rep = "\s*pm.max_spare_servers \s*=\s*([0-9]+)\s*"
tmp = re.search(rep, conf).groups()
data['max_spare_servers'] = tmp[0]
rep = "\s*pm\s*=\s*(\w+)\s*"
tmp = re.search(rep, conf).groups()
data['pm'] = tmp[0]
return public.getJson(data)
def setFpmConfig(version):
args = getArgs()
# if not 'max' in args:
# return 'missing time args!'
version = args['version']
max_children = args['max_children']
start_servers = args['start_servers']
min_spare_servers = args['min_spare_servers']
max_spare_servers = args['max_spare_servers']
pm = args['pm']
file = getServerDir() + '/' + version + '/etc/php-fpm.d/www.conf'
conf = public.readFile(file)
rep = "\s*pm.max_children\s*=\s*([0-9]+)\s*"
conf = re.sub(rep, "\npm.max_children = " + max_children, conf)
rep = "\s*pm.start_servers\s*=\s*([0-9]+)\s*"
conf = re.sub(rep, "\npm.start_servers = " + start_servers, conf)
rep = "\s*pm.min_spare_servers\s*=\s*([0-9]+)\s*"
conf = re.sub(rep, "\npm.min_spare_servers = " +
min_spare_servers, conf)
rep = "\s*pm.max_spare_servers \s*=\s*([0-9]+)\s*"
conf = re.sub(rep, "\npm.max_spare_servers = " +
max_spare_servers + "\n", conf)
rep = "\s*pm\s*=\s*(\w+)\s*"
conf = re.sub(rep, "\npm = " + pm + "\n", conf)
public.writeFile(file, conf)
# public.phpReload(version)
public.writeLog("TYPE_PHP", 'PHP_CHILDREN', (version, max_children,
start_servers, min_spare_servers, max_spare_servers))
return public.returnJson(True, '设置成功')
6 years ago
def getFpmStatus(version):
result = public.httpGet(
'http://127.0.0.1/phpfpm_status_' + version + '?json')
tmp = json.loads(result)
fTime = time.localtime(int(tmp['start time']))
tmp['start time'] = time.strftime('%Y-%m-%d %H:%M:%S', fTime)
return public.getJson(tmp)
7 years ago
if __name__ == "__main__":
6 years ago
if len(sys.argv) < 3:
print 'missing parameters'
exit(0)
7 years ago
func = sys.argv[1]
7 years ago
version = sys.argv[2]
if func == 'status':
print status(version)
7 years ago
elif func == 'start':
7 years ago
print start(version)
7 years ago
elif func == 'stop':
7 years ago
print stop(version)
7 years ago
elif func == 'restart':
7 years ago
print restart(version)
7 years ago
elif func == 'reload':
7 years ago
print reload(version)
6 years ago
elif func == 'fpm_log':
print fpmLog(version)
elif func == 'fpm_slow_log':
print fpmSlowLog(version)
7 years ago
elif func == 'conf':
7 years ago
print getConf(version)
6 years ago
elif func == 'get_php_conf':
print getPhpConf(version)
elif func == 'submit_php_conf':
print submitPhpConf(version)
elif func == 'get_limit_conf':
print getLimitConf(version)
elif func == 'set_max_time':
print setMaxTime(version)
elif func == 'set_max_size':
print setMaxSize(version)
6 years ago
elif func == 'get_fpm_conf':
print getFpmConfig(version)
elif func == 'set_fpm_conf':
print setFpmConfig(version)
6 years ago
elif func == 'get_fpm_status':
print getFpmStatus(version)
7 years ago
else:
print "fail"