def setFileAccessApi(self): if public.isAppleSystem(): return public.returnJson(True, '开发机不设置!') filename = request.form.get('filename', '').encode('utf-8') user = request.form.get('user', '').encode('utf-8') access = request.form.get('access', '755') sall = '-R' try: if not self.checkDir(filename): return public.returnJson(False, '请不要花样作死') if not os.path.exists(filename): return public.returnJson(False, '指定文件不存在!') os.system('chmod ' + sall + ' ' + access + " '" + filename + "'") os.system('chown ' + sall + ' ' + user + ':' + user + " '" + filename + "'") msg = public.getInfo( '设置[{1}]权限为[{2}]所有者为[{3}]', (filename, access, user,)) public.writeLog('文件管理', msg) return public.returnJson(True, '设置成功!') except: return public.returnJson(False, '设置失败!')
def closeLogsApi(self): logPath = public.getLogsDir() os.system('rm -f ' + logPath + '/*') os.system('kill -USR1 `cat ' + public.getServerDir() + 'openresty/nginx/logs/nginx.pid`') public.writeLog('文件管理', '网站日志已被清空!') tmp = self.getDirSize(logPath) return public.returnJson(True, tmp[0].split()[0])
def modifyCrondApi(self): sid = request.form.get('id', '') iname = request.form.get('name', '') field_type = request.form.get('type', '') week = request.form.get('week', '') where1 = request.form.get('where1', '') hour = request.form.get('hour', '') minute = request.form.get('minute', '') save = request.form.get('save', '') backup_to = request.form.get('backup_to', '') stype = request.form.get('stype', '') sname = request.form.get('sname', '') sbody = request.form.get('sbody', '') urladdress = request.form.get('urladdress', '') if len(iname) < 1: return public.returnJson(False, '任务名称不能为空!') params = { 'name': iname, 'type': field_type, 'week': week, 'where1': where1, 'hour': hour, 'minute': minute, 'save': save, 'backup_to': backup_to, 'stype': stype, 'sname': sname, 'sbody': sbody, 'urladdress': urladdress, } cuonConfig, get, name = self.getCrondCycle(params) cronInfo = public.M('crontab').where('id=?', (sid, )).field(self.field).find() del (cronInfo['id']) del (cronInfo['addtime']) cronInfo['name'] = get['name'] cronInfo['type'] = get['type'] cronInfo['where1'] = get['where1'] cronInfo['where_hour'] = get['hour'] cronInfo['where_minute'] = get['minute'] cronInfo['save'] = get['save'] cronInfo['backup_to'] = get['backup_to'] cronInfo['sbody'] = get['sbody'] cronInfo['urladdress'] = get['urladdress'] addData = public.M('crontab').where('id=?', (sid, )).save( 'name,type,where1,where_hour,where_minute,save,backup_to,sbody,urladdress', (get['name'], field_type, get['where1'], get['hour'], get['minute'], get['save'], get['backup_to'], get['sbody'], get['urladdress'])) self.removeForCrond(cronInfo['echo']) self.syncToCrond(cronInfo) public.writeLog('计划任务', '修改计划任务[' + cronInfo['name'] + ']成功') return public.returnJson(True, '修改成功')
def batchPasteApi(self): path = request.form.get('path', '').encode('utf-8') stype = request.form.get('type', '').encode('utf-8') # filename = request.form.get('filename', '').encode('utf-8') import shutil if not self.checkDir(path): return public.returnJson(False, '请不要花样作死!') i = 0 myfiles = json.loads(session['selected']['data']) l = len(myfiles) if stype == '1': for key in myfiles: i += 1 public.writeSpeed(key, i, l) try: sfile = session['selected'][ 'path'] + '/' + key.encode('utf-8') dfile = path + '/' + key.encode('utf-8') if os.path.isdir(sfile): shutil.copytree(sfile, dfile) else: shutil.copyfile(sfile, dfile) stat = os.stat(sfile) os.chown(dfile, stat.st_uid, stat.st_gid) except: continue msg = public.getInfo('从[{1}]批量复制到[{2}]成功', (session['selected']['path'], path,)) public.writeLog('文件管理', msg) else: for key in myfiles: try: i += 1 public.writeSpeed(key, i, l) sfile = session['selected'][ 'path'] + '/' + key.encode('utf-8') dfile = path + '/' + key.encode('utf-8') shutil.move(sfile, dfile) except: continue msg = public.getInfo('从[{1}]批量移动到[{2}]成功', (session['selected']['path'], path,)) public.writeLog('文件管理', msg) public.writeSpeed(None, 0, 0) errorCount = len(myfiles) - i del(session['selected']) msg = public.getInfo('批量操作成功[{1}],失败[{2}]', (str(i), str(errorCount))) return public.returnJson(True, msg)
def recycleBinApi(self): c = 'data/recycle_bin.pl' db = request.form.get('db', '').encode('utf-8') if db != '': c = 'data/recycle_bin_db.pl' if os.path.exists(c): os.remove(c) public.writeLog('文件管理', '已关闭回收站功能!') return public.returnJson(True, '已关闭回收站功能!') else: public.writeFile(c, 'True') public.writeLog('文件管理', '已开启回收站功能!') return public.returnJson(True, '已开启回收站功能!')
def createDirApi(self): path = request.form.get('path', '').encode('utf-8') try: if not self.checkFileName(path): return public.returnJson(False, '目录名中不能包含特殊字符!') if os.path.exists(path): return public.returnJson(False, '指定目录已存在!') os.makedirs(path) self.setFileAccept(path) msg = public.getInfo('创建目录[{1}]成功!', (path,)) public.writeLog('文件管理', msg) return public.returnJson(True, '目录创建成功!') except Exception as e: return public.returnJson(False, '目录创建失败!')
def reRecycleBinApi(self): rPath = self.rPath path = request.form.get('path', '').encode('utf-8') dFile = path.replace('_mw_', '/').split('_t_')[0] try: import shutil shutil.move(rPath + path, dFile) msg = public.getInfo('移动文件[{1}]到回收站成功!', (dFile,)) public.writeLog('文件管理', msg) return public.returnJson(True, '恢复成功!') except Exception as e: msg = public.getInfo('从回收站恢复[{1}]失败!', (dFile,)) public.writeLog('文件管理', msg) return public.returnJson(False, '恢复失败!')
def mvRecycleBin(self, path): rPath = self.rPath if not os.path.exists(rPath): os.system('mkdir -p ' + rPath) rFile = rPath + path.replace('/', '_mw_') + '_t_' + str(time.time()) try: import shutil shutil.move(path, rFile) public.writeLog('文件管理', public.getInfo( '移动文件[{1}]到回收站成功!', (path))) return True except: public.writeLog('文件管理', public.getInfo( '移动文件[{1}]到回收站失败!', (path))) return False
def deleteDirApi(self): path = request.form.get('path', '').encode('utf-8') if not os.path.exists(path): return public.returnJson(False, '指定文件不存在!') # 检查是否为.user.ini if path.find('.user.ini'): os.system("chattr -i '" + path + "'") try: if os.path.exists('data/recycle_bin.pl'): if self.mvRecycleBin(path): return public.returnJson(True, '已将文件移动到回收站!') public.execShell('rm -rf ' + path) public.writeLog('文件管理', '删除文件成功!', (path,)) return public.returnJson(True, '删除文件成功!') except: return public.returnJson(False, '删除文件失败!')
def closeRecycleBinApi(self): rPath = self.rPath os.system('which chattr && chattr -R -i ' + rPath) rlist = os.listdir(rPath) i = 0 l = len(rlist) for name in rlist: i += 1 path = rPath + name public.writeSpeed(name, i, l) if os.path.isdir(path): shutil.rmtree(path) else: os.remove(path) public.writeSpeed(None, 0, 0) public.writeLog('文件管理', '已清空回收站!') return public.returnJson(True, '已清空回收站!')
def copyDir(self, sfile, dfile): if not os.path.exists(sfile): return public.returnJson(False, '指定目录不存在!') if os.path.exists(dfile): return public.returnJson(False, '指定目录已存在!') import shutil try: shutil.copytree(sfile, dfile) stat = os.stat(sfile) os.chown(dfile, stat.st_uid, stat.st_gid) msg = public.getInfo('复制目录[{1}]到[{2}]成功!', (sfile, dfile)) public.writeLog('文件管理', msg) return public.returnJson(True, '目录复制成功!') except: return public.returnJson(False, '目录复制失败!')
def createFileApi(self): file = request.form.get('path', '').encode('utf-8') try: if not self.checkFileName(file): return public.returnJson(False, '文件名中不能包含特殊字符!') if os.path.exists(file): return public.returnJson(False, '指定文件已存在!') _path = os.path.dirname(file) if not os.path.exists(_path): os.makedirs(_path) open(file, 'w+').close() self.setFileAccept(file) msg = public.getInfo('创建文件[{1}]成功!', (file,)) public.writeLog('文件管理', msg) return public.returnJson(True, '文件创建成功!') except Exception as e: # print str(e) return public.returnJson(True, '文件创建失败!')
def delete(self, path): if not os.path.exists(path): return public.returnJson(False, '指定文件不存在!') # 检查是否为.user.ini if path.find('.user.ini') >= 0: os.system("chattr -i '" + path + "'") try: if os.path.exists('data/recycle_bin.pl'): if self.mvRecycleBin(path): return public.returnJson(True, '已将文件移动到回收站!') os.remove(path) public.writeLog('文件管理', public.getInfo( '删除文件[{1}]成功!', (path))) return public.returnJson(True, '删除文件成功!') except: return public.returnJson(False, '删除文件失败!')
def mvFileApi(self): sfile = request.form.get('sfile', '').encode('utf-8') dfile = request.form.get('dfile', '').encode('utf-8') if not self.checkFileName(dfile): return public.returnJson(False, '文件名中不能包含特殊字符!') if not os.path.exists(sfile): return public.returnJson(False, '指定文件不存在!') if not self.checkDir(sfile): return public.returnJson(False, 'FILE_DANGER') import shutil try: shutil.move(sfile, dfile) msg = public.getInfo('移动文件或目录[{1}]到[{2}]成功!', (sfile, dfile,)) public.writeLog('文件管理', msg) return public.returnJson(True, '移动文件或目录成功!') except: return public.returnJson(False, '移动文件或目录失败!')
def delRecycleBinApi(self): rPath = self.rPath path = request.form.get('path', '').encode('utf-8') empty = request.form.get('empty', '').encode('utf-8') dFile = path.split('_t_')[0] if not self.checkDir(path): return public.returnJson(False, '敏感目录,请不要花样作死!') os.system('which chattr && chattr -R -i ' + rPath + path) if os.path.isdir(rPath + path): import shutil shutil.rmtree(rPath + path) else: os.remove(rPath + path) tfile = path.replace('_mw_', '/').split('_t_')[0] msg = public.getInfo('已彻底从回收站删除{1}!', (tfile,)) public.writeLog('文件管理', msg) return public.returnJson(True, msg)
def saveBody(self, path, data, encoding='utf-8'): if not os.path.exists(path): return public.returnJson(False, '文件不存在') try: if encoding == 'ascii': encoding = 'utf-8' if sys.version_info[0] == 2: data = data.encode(encoding, errors='ignore') fp = open(path, 'w+') else: data = data.encode( encoding, errors='ignore').decode(encoding) fp = open(path, 'w+', encoding=encoding) fp.write(data) fp.close() public.writeLog('文件管理', '文件保存成功', (path,)) return public.returnJson(True, '文件保存成功') except Exception as ex: return public.returnJson(False, 'FILE_SAVE_ERR:' + str(ex))
def copyFileApi(self): sfile = request.form.get('sfile', '').encode('utf-8') dfile = request.form.get('dfile', '').encode('utf-8') if not os.path.exists(sfile): return public.returnJson(False, '指定文件不存在!') if os.path.isdir(sfile): return self.copyDir(sfile, dfile) import shutil try: shutil.copyfile(sfile, dfile) msg = public.getInfo('复制文件[{1}]到[{2}]成功!', (sfile, dfile,)) public.writeLog('文件管理', msg) stat = os.stat(sfile) os.chown(dfile, stat.st_uid, stat.st_gid) return public.returnJson(True, '文件复制成功!') except: return public.returnJson(False, '文件复制失败!')
def delApi(self): sid = request.form.get('id', '') try: find = public.M('crontab').where( "id=?", (sid, )).field('name,echo').find() if not self.removeForCrond(find['echo']): return public.returnJson(False, '无法写入文件,请检查是否开启了系统加固功能!') cronPath = public.getServerDir() + '/cron' sfile = cronPath + '/' + find['echo'] if os.path.exists(sfile): os.remove(sfile) sfile = cronPath + '/' + find['echo'] + '.log' if os.path.exists(sfile): os.remove(sfile) public.M('crontab').where("id=?", (sid, )).delete() public.writeLog('计划任务', public.getInfo('删除计划任务[{1}]成功!', (find['name'], ))) return public.returnJson(True, '删除成功') except Exception as e: return public.returnJson(False, '删除失败:' + str(e))
def uploadFileApi(self): from werkzeug.utils import secure_filename from flask import request path = request.args.get('path', '').encode('utf-8') if not os.path.exists(path): os.makedirs(path) f = request.files['zunfile'] filename = os.path.join(path, f.filename) if sys.version_info[0] == 2: filename = filename.encode('utf-8') s_path = path if os.path.exists(filename): s_path = filename p_stat = os.stat(s_path) f.save(filename) os.chown(filename, p_stat.st_uid, p_stat.st_gid) os.chmod(filename, p_stat.st_mode) msg = public.getInfo('上传文件[{1}] 到 [{2}]成功!', (filename, path)) public.writeLog('文件管理', msg) return public.returnMsg(True, '上传成功!')
def zip(self, sfile, dfile, stype, path): if sfile.find(',') == -1: if not os.path.exists(path + '/' + sfile): return public.returnMsg(False, '指定文件不存在!') try: tmps = public.getRunDir() + '/tmp/panelExec.log' if stype == 'zip': os.system("cd '" + path + "' && zip '" + dfile + "' -r '" + sfile + "' > " + tmps + " 2>&1") else: sfiles = '' for sfile in sfile.split(','): if not sfile: continue sfiles += " '" + sfile + "'" os.system("cd '" + path + "' && tar -zcvf '" + dfile + "' " + sfiles + " > " + tmps + " 2>&1") self.setFileAccept(dfile) public.writeLog("文件管理", '文件压缩成功!', (sfile, dfile)) return public.returnJson(True, '文件压缩成功!') except: return public.returnJson(False, '文件压缩失败!')
def setBatchDataApi(self): path = request.form.get('path', '').encode('utf-8') stype = request.form.get('type', '').encode('utf-8') access = request.form.get('access', '').encode('utf-8') user = request.form.get('user', '').encode('utf-8') data = request.form.get('data') if stype == '1' or stype == '2': session['selected'] = { 'path': path, 'type': stype, 'access': access, 'user': user, 'data': data } return public.returnJson(True, '标记成功,请在目标目录点击粘贴所有按钮!') elif stype == '3': for key in json.loads(data): try: key = key.encode('utf-8') filename = path + '/' + key if not self.checkDir(filename): return public.returnJson(False, 'FILE_DANGER') os.system('chmod -R ' + access + " '" + filename + "'") os.system('chown -R ' + user + ':' + user + " '" + filename + "'") except: continue public.writeLog('文件管理', '批量设置权限成功!') return public.returnJson(True, '批量设置权限成功!') else: import shutil isRecyle = os.path.exists('data/recycle_bin.pl') data = json.loads(data) l = len(data) i = 0 for key in data: try: filename = path + '/' + key.encode('utf-8') topath = filename if not os.path.exists(filename): continue i += 1 public.writeSpeed(key, i, l) if os.path.isdir(filename): if not self.checkDir(filename): return public.returnJson(False, '请不要花样作死!') if isRecyle: self.mvRecycleBin(topath) else: shutil.rmtree(filename) else: if key == '.user.ini': os.system('which chattr && chattr -i ' + filename) if isRecyle: self.mvRecycleBin(topath) else: os.remove(filename) except: continue public.writeSpeed(None, 0, 0) public.writeLog('文件管理', '批量删除成功!') return public.returnJson(True, '批量删除成功!')