Initial YakPanel commit

This commit is contained in:
Niranjan
2026-04-07 02:04:22 +05:30
commit 2826d3e7f3
5359 changed files with 1390724 additions and 0 deletions

View File

185
class_v2/adminer/api.py Normal file
View File

@@ -0,0 +1,185 @@
# coding: utf-8
# -------------------------------------------------------------------
# YakPanel
# -------------------------------------------------------------------
# Copyright (c) 2014-2099 YakPanel(www.yakpanel.com) All rights reserved.
# -------------------------------------------------------------------
# Author: yakpanel
# -------------------------------------------------------------------
# ------------------------------
# Adminer Api
# ------------------------------
import os
import time
import public
from public.exceptions import HintException
from public.validate import Param
from .config import *
from .manager import AdminerManager
class AdminerApi(AdminerManager):
def support_versions(self, get=None):
"""Adminer支持的版本列表"""
return public.success_v2(self.get_support_versions)
def uninstall(self, get=None):
"""卸载"""
if not self.is_install:
return public.success_v2(public.lang("Adminer is not installed!"))
public.ExecShell(f"rm -rf {DEFAULT_DIR}/")
public.ExecShell(f"rm -f {NGX_CONF_PATH}")
public.ExecShell(f"rm -f {APC_CONF_PATH}")
public.ExecShell(f"rm -f {OLS_CONF_PATH}")
public.ExecShell(f"rm -f {NGX_CONF_PATH}.bak")
public.ExecShell(f"rm -f {APC_CONF_PATH}.bak")
public.ExecShell(f"rm -f {OLS_CONF_PATH}.bak")
self.service_reload()
return public.success_v2(public.lang("Uninstall Successfully!"))
def get_status(self, get=None):
"""获取服务的综合状态信息"""
if not self.is_install:
return public.success_v2({
"php_version": "",
"install": 0,
"version": "",
"port": 0,
})
return public.success_v2({
"php_version": self.adminer_php_version,
"install": 1,
"version": self.adminer_version,
"port": self.adminer_port,
})
@AdminerManager.require_env
def install(self, get):
"""安装"""
try:
get.validate([
Param("version").Regexp(r"^\d+(?:\.\d+)+$").Require(),
], [
public.validate.trim_filter(),
])
version = str(get.version)
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.fail_v2(str(ex))
done = False
# 清空目录, 清空配置 (即便开启多服务)
self.uninstall()
dst = f"{DEFAULT_DIR}/adminer_{public.GetRandomString(16)}"
try:
msg = ""
count = 0
while count <= 2:
try:
file = self.download_official_file(ver=version, dst=dst)
if os.path.exists(file):
break
except Exception as ex:
msg = ex
time.sleep(1)
count += 1
if msg != "":
raise HintException(msg or "Download Adminer failed, please try again!")
public.ExecShell(f"chown -R root:root {DEFAULT_DIR}")
public.ExecShell(f"chmod -R 755 {dst}")
public.writeFile(VERSION_PL, version)
public.writeFile(PORT_PL, str(DEFAULT_PORT))
self.all_generate_conf(force=True)
self.service_reload()
done = True
return public.success_v2(public.lang("Install Successfully!"))
except Exception as e:
import traceback
public.print_log(traceback.format_exc())
public.print_log('Download Adminer Error {}'.format(e))
raise HintException(e)
finally:
if done is False:
public.ExecShell(f"rm -rf {dst}")
@AdminerManager.require_env
def repair(self, get):
"""重新安装, 或重新安装指定版本"""
try:
get.validate([
Param("version").Regexp(r"^\d+(?:\.\d+)+$").Require(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.fail_v2(str(ex))
try:
self.install(get)
return public.success_v2(public.lang("Install Successfully!"))
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.fail_v2(str(ex))
@AdminerManager.require_env
def switch_port(self, get):
"""更新端口"""
try:
get.validate([
Param("port").Integer().Require(),
], [
public.validate.trim_filter(),
])
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.fail_v2(str(ex))
if not self.is_install:
return public.fail_v2(public.lang("Adminer is not installed!"))
port = int(get.port)
if port == self.adminer_port:
return public.success_v2(public.lang("Set Adminer port successfully!"))
if not public.checkPort(port):
return public.fail_v2(public.lang(f"Port {port} is already in use or invalid!"))
try:
public.writeFile(PORT_PL, str(port))
self.all_generate_conf(force=True)
self.service_reload()
return public.success_v2(public.lang("Set successfully!"))
except Exception as e:
public.print_log('Set Adminer Port Error {}'.format(e))
raise HintException("Set failed!")
@AdminerManager.require_env
def switch_php(self, get):
try:
get.validate([
Param("php_version").Regexp("\d+").Require(),
], [
public.validate.trim_filter(),
])
php_version = str(get.php_version).replace(".", "")
except Exception as ex:
public.print_log("error info: {}".format(ex))
return public.fail_v2(str(ex))
if not self.is_install:
return public.fail_v2(public.lang("Adminer is not installed!"))
if php_version == self.adminer_php_version:
return public.success_v2(public.lang("Set successfully!"))
try:
self.all_generate_conf(force=True, php_ver=php_version)
self.service_reload()
return public.success_v2(public.lang("Set Adminer php version successfully!"))
except HintException as e:
raise e
except Exception as ex:
return public.fail_v2(f"Set Adminer PHP Version Error {ex}")

173
class_v2/adminer/config.py Normal file
View File

@@ -0,0 +1,173 @@
# coding: utf-8
# -------------------------------------------------------------------
# YakPanel
# -------------------------------------------------------------------
# Copyright (c) 2014-2099 YakPanel(www.yakpanel.com) All rights reserved.
# -------------------------------------------------------------------
# Author: yakpanel
# -------------------------------------------------------------------
# ------------------------------
# Adminer config
# ------------------------------
__version__ = "5.4.0"
SERVER_PATH = "/www/server"
DEFAULT_VER = "5.4.0"
DEFAULT_PORT = 999
DEFAULT_DIR = f"{SERVER_PATH}/adminer"
VERSION_PL = f"{DEFAULT_DIR}/version.pl"
PORT_PL = f"{DEFAULT_DIR}/port.pl"
NGX_CONF_PATH = f"{SERVER_PATH}/panel/vhost/nginx/0.adminer.conf"
APC_CONF_PATH = f"{SERVER_PATH}/panel/vhost/apache/0.adminer.conf"
OLS_CONF_PATH = f"{SERVER_PATH}/panel/vhost/openlitespeed/0.adminer.conf"
class WebConfig:
nginx = NGX_CONF_PATH
apache = APC_CONF_PATH
openlitespeed = OLS_CONF_PATH
all_web = [
"nginx", "apache", "openlitespeed"
]
all_conf = [
NGX_CONF_PATH, APC_CONF_PATH, OLS_CONF_PATH
]
@classmethod
def get(cls, key, default=None):
return getattr(cls, key, default if default else "")
VER_SHA_MAP = {
"5.4.0": "1a330e8c197b8bbb57a5c07e245c9424aa1007b3e1ccec17dd39e709d3983c17"
}
NG_CONF = r"""server
{{
listen 127.0.0.1:{port};
server_name _;
index index.html index.htm index.php;
root {root_dir};
location ~ /tmp/ {{
return 403;
}}
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{{
expires 30d;
}}
location ~ .*\.(js|css)?$
{{
expires 12h;
}}
location ~ /\.
{{
deny all;
}}
access_log /www/wwwlogs/access.log;
}}
"""
APACHE_CONF = r"""Listen {port}
<VirtualHost 127.0.0.1:{port}>
ServerAdmin webmaster@example.com
DocumentRoot "{root_dir}"
ServerName adminer.localhost
#PHP
<FilesMatch \.php$>
SetHandler "{proxy_pass}"
</FilesMatch>
#DENY FILES
<Files ~ (\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)$>
Order allow,deny
Deny from all
</Files>
#PATH
<Directory "{root_dir}">
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>
</VirtualHost>
"""
OLS_CONF = r"""virtualhost adminer {{
vhRoot {root_dir}
allowSymbolLink 1
enableScript 1
restrained 1
setUIDMode 0
docRoot $VH_ROOT
vhDomain $VH_NAME
adminEmails admin@localhost
index {{
useServer 0
indexFiles index.php, index.html
}}
scripthandler {{
add lsapi:adminer-lsphp php
}}
extprocessor adminer-lsphp {{
type lsapi
address UDS://tmp/lshttpd/adminer.sock
maxConns 10
env LSAPI_CHILDREN=10
initTimeout 600
retryTimeout 0
persistConn 1
respBuffer 0
autoStart 1
path /usr/local/lsws/lsphp{php_version}/bin/lsphp
extUser www
extGroup www
}}
phpIniOverride {{
php_admin_value open_basedir "/tmp:$VH_ROOT"
}}
}}
listener adminer-listener {{
address 127.0.0.1:{port}
secure 0
map adminer *
}}
"""
__all__ = [
"__version__",
"DEFAULT_VER",
"DEFAULT_PORT",
"DEFAULT_DIR",
"VERSION_PL",
"PORT_PL",
"NGX_CONF_PATH",
"APC_CONF_PATH",
"OLS_CONF_PATH",
"WebConfig",
"VER_SHA_MAP",
"NG_CONF",
"APACHE_CONF",
"OLS_CONF",
]

323
class_v2/adminer/manager.py Normal file
View File

@@ -0,0 +1,323 @@
# coding: utf-8
# -------------------------------------------------------------------
# YakPanel
# -------------------------------------------------------------------
# Copyright (c) 2014-2099 YakPanel(www.yakpanel.com) All rights reserved.
# -------------------------------------------------------------------
# Author: yakpanel
# -------------------------------------------------------------------
# ------------------------------
# Adminer Manager
# ------------------------------
import hashlib
import os
import re
import time
from typing import Tuple
import public
from YakPanel import cache
from panel_plugin_v2 import panelPlugin
from public.exceptions import HintException
from .config import *
www_server = public.get_setup_path()
def get_global_php_versions() -> list:
from panel_site_v2 import panelSite
res = panelSite().GetPHPVersion(None, False)
return [x.get("version", "00") for x in res] if res else []
def find_cgi(file_path: str) -> str:
try:
if os.path.exists(file_path):
content = public.readFile(file_path)
if content:
m = re.search(r"php-cgi-(\d+)", content)
if m: # ng, ap
return f"{m.group(1)[0]}{m.group(1)[1]}"
ols = re.search(r"lsphp(\d+)", content)
if ols: # ols
return f"{ols.group(1)[0]}{ols.group(1)[1]}"
except Exception as e:
public.print_log('Get Adminer PHP Version Error {}'.format(e))
pass
return ""
class AdminerManager:
INFO_CACHE_KEY = "adminer_port_path"
@property
def get_support_versions(self) -> list:
"""支持的版本列表"""
return list(VER_SHA_MAP.keys())
@property
def adminer_dir(self) -> str:
"""目录, 不是绝对路径"""
dir_path = ""
if os.path.exists(DEFAULT_DIR):
for dir_name in os.listdir(DEFAULT_DIR):
if os.path.isdir(os.path.join(DEFAULT_DIR, dir_name)) and dir_name.startswith("adminer_"):
dir_path = dir_name
break
if not dir_path:
return ""
return dir_path
@property
def adminer_port(self) -> int:
port = DEFAULT_PORT
try:
p = public.readFile(PORT_PL)
if p and p.strip().isdigit():
port = int(p.strip())
except Exception as e:
public.print_log('Get Adminer PORT PL Error {}'.format(e))
return int(port)
@property
def adminer_dir_port(self) -> Tuple[str, int]:
c = cache.get(self.INFO_CACHE_KEY)
if c:
return c
path = self.adminer_dir
port = self.adminer_port
if not path or not port:
raise HintException(
"Adminer is not installed, please go to the [Database] page to install it!"
)
info = (path, port)
cache.set(self.INFO_CACHE_KEY, list(info), 10)
return info
@property
def adminer_version(self) -> str:
"""版本号"""
try:
d, p = self.adminer_dir_port
except HintException:
return ""
if not d or not p:
return ""
version = DEFAULT_VER
try:
if os.path.exists(VERSION_PL):
with open(VERSION_PL, "r") as f:
version = f.read().strip()
except Exception as e:
public.print_log('Get Adminer Version Error {}'.format(e))
return version
@property
def adminer_php_version(self) -> str:
"""
获取adminer使用的PHP版本, 并尝试修复自身
:return: final_ver = "00" | "74" | "80" | "81" | "82" | "83"...
"00" 表示没php环境
"""
def _alive_php(php_vers: list) -> str:
for v in php_vers:
if v != "00" and os.path.exists(f"{www_server}/php/{v}/bin/php"):
return v
return "00"
php_versions = get_global_php_versions()
if not php_versions: # 没可用的php版本
return "00"
web_server = public.GetWebServer()
if web_server == "nginx":
# Nginx 从 enable-php.conf 中找
enable_cfg = f"{www_server}/nginx/conf/enable-php.conf"
if os.path.exists(enable_cfg):
final_ver = find_cgi(enable_cfg)
if final_ver not in php_versions:
# enable-php.conf 里有版本, 但版本不可用时, 进行替换
final_ver = _alive_php(php_versions)
php_ver_cgi = f"{www_server}/nginx/conf/enable-php-{final_ver}.conf"
public.writeFile(enable_cfg, public.readFile(php_ver_cgi))
else:
# enable-php.conf 不存在时, 进行覆盖
final_ver = _alive_php(php_versions)
php_ver_cgi = f"{www_server}/nginx/conf/enable-php-{final_ver}.conf"
public.writeFile(enable_cfg, public.readFile(php_ver_cgi))
else: # Apache 和 OLS 从各自配置文件中找
conf = WebConfig.get(web_server)
if conf and os.path.exists(conf):
final_ver = find_cgi(conf)
if final_ver not in php_versions:
# 配置里有版本, 但版本不可用时, 仅返回版本号
final_ver = _alive_php(php_versions)
else:
final_ver = _alive_php(php_versions)
# 兜底
if final_ver == "00" or final_ver not in php_versions:
final_ver = _alive_php(php_versions)
return final_ver
@staticmethod
def require_env(func):
"""环境wrapper"""
def wrapper(*args, **kwargs):
# has web server
if not public.GetWebServer() or not panelPlugin().check_dependent("nginx|apache|openlitespeed"):
raise HintException("Web service is not installed, please install web service first!")
# has php version
for ver in public.get_php_versions():
if os.path.exists(f"{www_server}/php/{ver}/bin/php"):
return func(*args, **kwargs)
raise HintException("PHP environment is not installed, please install PHP first!")
return wrapper
@staticmethod
def download_official_file(ver: str, dst: str) -> None | str:
"""下载源码"""
ver_sha256 = VER_SHA_MAP.get(ver, "")
if not ver_sha256:
raise Exception(f"Version {ver} not support or wrong, please try again!")
tmp_dir = "/tmp/adminer_tmp"
if os.path.exists(tmp_dir):
public.ExecShell(f"rm -rf {tmp_dir}")
os.makedirs(tmp_dir, exist_ok=True)
download_file = f"adminer-{ver}.zip"
tmp_zip_path = f"{tmp_dir}/{download_file}"
try:
download_url = f"{public.get_url()}/src/{download_file}"
public.downloadFile(download_url, tmp_zip_path)
if not os.path.exists(tmp_zip_path):
raise Exception(f"Download {download_file} File not exists, please try again!")
with open(tmp_zip_path, "rb") as f:
tmp_sha256 = hashlib.sha256(f.read()).hexdigest()
public.print_log(f"tmp_sha256= {tmp_sha256}")
if ver_sha256 != tmp_sha256:
raise Exception("Downlaod File sha256 check failed, please try again!")
if not os.path.exists(dst):
os.makedirs(dst, 0o755, exist_ok=True)
public.ExecShell(f"unzip -o {tmp_zip_path} -d {dst}")
file_abs = os.path.join(dst, "index.php")
if not os.path.exists(file_abs):
raise Exception("Download official file file failed, please try again!")
return file_abs
except Exception as e:
public.print_log('Download Adminer Error {}'.format(e))
raise Exception(e)
finally:
if os.path.exists(tmp_dir):
public.ExecShell(f"rm -rf {tmp_dir}")
@staticmethod
def generate_conf(web_server: str, force: bool = False, php_ver: str = None) -> None:
"""
动态生成配置, 覆盖
依据当前的 port, php_ver, (root_dir强制默认)
"""
web_conf = WebConfig.get(web_server)
if not web_conf:
raise HintException("Web service not support! WEB CONF MAP not found!")
if public.get_multi_webservice_status(): # 当前是否开启多服务
web_confs = [web_conf] if web_server == "nginx" else [f"{web_conf}.bak"]
if web_server != "nginx": # 非nginx的配置.conf都不应存在
public.ExecShell(f"rm -f {WebConfig.apache} && rm -f {WebConfig.openlitespeed}")
else: # 非多服务
for w in WebConfig.all_conf: # .bak不应存在
if os.path.exists(f"{w}.bak"):
public.ExecShell(f"rm -f {w}.bak")
web_confs = [web_conf]
# conf | conf.bak
for conf in web_confs:
if os.path.exists(conf) and force is False:
return
port = DEFAULT_PORT
port_pl = public.readFile(PORT_PL)
if port_pl and port_pl.strip().isdigit():
port = int(port_pl.strip())
if php_ver is None:
# nginx 从 enable-php.conf 中找, 其他从自己的配置文件中找
php_version = AdminerManager().adminer_php_version
else:
php_version = php_ver
# 修改ng的 enable-php.conf
enable_cfg = f"{www_server}/nginx/conf/enable-php.conf"
php_ver_cgi = f"{www_server}/nginx/conf/enable-php-{php_version}.conf"
public.writeFile(enable_cfg, public.readFile(php_ver_cgi))
if not php_version or php_version == "00":
raise HintException("PHP environment is not installed, please install PHP first!")
content = ""
root_dir = DEFAULT_DIR
# 模板动态生成配置
try:
if web_server == "nginx":
content = NG_CONF.format(port=port, root_dir=root_dir)
elif web_server == "apache":
proxy_pass = f"proxy:{public.get_php_proxy(php_version, "apache")}"
content = APACHE_CONF.format(port=port, root_dir=root_dir, proxy_pass=proxy_pass)
elif web_server == "openlitespeed":
content = OLS_CONF.format(port=port, root_dir=root_dir, php_version=php_version)
except Exception as e:
public.print_log('Generate Adminer Conf Error {}'.format(e))
pass
if content:
public.writeFile(conf, content)
@staticmethod
def all_generate_conf(force: bool = False, php_ver: str = None) -> None:
if php_ver is not None and php_ver not in get_global_php_versions():
raise HintException(public.lang("PHP version does not install!"))
for web in WebConfig.all_web:
AdminerManager.generate_conf(web_server=web, force=force, php_ver=php_ver)
@property
def is_install(self) -> bool:
"""是否安装, 只检测入口文件"""
adminer_dir = self.adminer_dir
if not adminer_dir:
return False
if not os.path.exists(f"{DEFAULT_DIR}/{adminer_dir}/index.php"):
return False
mulit = public.get_multi_webservice_status()
if mulit:
if os.path.exists(NGX_CONF_PATH):
return True
else:
conf = WebConfig.get(public.GetWebServer())
if not conf or not os.path.exists(conf):
return False
return True
def service_reload(self):
public.serviceReload()
cache.delete(self.INFO_CACHE_KEY)
time.sleep(0.5)