This repository has been archived on 2022-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
gemcapi/index.php

196 lines
7.7 KiB
PHP
Raw Normal View History

2020-08-09 14:08:44 +00:00
<?php
2020-08-25 14:40:22 +00:00
error_reporting(0);//抑制部分无关紧要的报错
/*---------------------
6.0.0正在开发,请不要使用
6.0.0正在开发,请不要使用
6.0.0正在开发,请不要使用
---------------------*/
ini_set('user_agent', 'GeMCAPI/6.0.0 (GeMC API Sync 6.0.0;Release)');//设置默认UA
$server_id=1;//服务器的ID
$active=array('mojang/launchermeta/mc/game/version_manifest.json','forge/last','optifine/versionlist','liteloader/dl/versions/versions.json','forge/minecraft');//需要保持最新的文件
$type=array('.com','.net','.com','.moe','.net');//分配域名
//[0]:mojang,[1]:fabricmc,[2]:liteloader,[3]:authlib-injector,[4]:minecraft
function is_url_exists($url){
2020-08-09 14:08:44 +00:00
$opts = array(
'http'=>array(
'timeout'=>3,
)
);
$context = stream_context_create($opts);
2020-08-25 14:40:22 +00:00
return @file_get_contents($url, false, $context);//尝试访问文件,无法访问则不存在
2020-08-09 14:08:44 +00:00
}
2020-08-25 14:40:22 +00:00
function download_file($url,$file,$type,$type_sub){
global $link;
ob_start();
$result_download=system('python download.py --url "'.$url.'" --file "'.$file.'" --type "'.$type.'" --type_sub "'.$type_sub.'"');
ob_clean();
//echo $result_download;
//echo 'python download.py --url "'.$url.'" --file "'.$file.'" --type "'.$type.'" --type_sub "'.$type_sub.'"';
mysqli_query($link,$result_download);
//使用Python下载避开了PHP下载文件的不稳定劣势
2020-08-11 07:39:44 +00:00
}
2020-08-09 14:08:44 +00:00
function get_main_domain($file){
2020-08-25 14:40:22 +00:00
//使用正则表达式筛选信息
2020-08-09 14:08:44 +00:00
$str=preg_replace('/^[^\/]*\//is','',$file);
$str2=str_replace('/'.$str,'',$file);
return $str2;
}
function get_sub_domain($file){
2020-08-25 14:40:22 +00:00
//使用正则表达式筛选信息
2020-08-09 14:08:44 +00:00
$str=preg_replace('/^[^\/]*\//is','',$file);
$str2=preg_replace('/^[^\/]*\//is','',$str);
$str3=str_replace('/'.$str2,'',$str);
return $str3;
}
function get_file_path($file){
2020-08-25 14:40:22 +00:00
//使用正则表达式筛选信息
2020-08-09 14:08:44 +00:00
$str=preg_replace('/^[^\/]*\//is','',$file);
$str2=preg_replace('/^[^\/]*\//is','',$str);
return $str2;
}
2020-08-25 14:40:22 +00:00
function get_online_url($file){
//字符串拼接用函数
2020-08-09 14:08:44 +00:00
global $type;
2020-08-25 14:40:22 +00:00
global $id;
if(strstr($file,"maven")){
$str="https://bmclapi2.bangbang93.com/".$file;
}elseif(strstr($file,"forge/")){
$str="https://bmclapi2.bangbang93.com/".$file;
}elseif(strstr($file,"optifine/")){
$str="https://bmclapi2.bangbang93.com/".$file;
}elseif(strstr($file,"liteloader/list")){
$str="https://bmclapi2.bangbang93.com/".str_replace('list/','list?',$file);
}else{
$str='https://'.get_sub_domain($file).'.'.get_main_domain($file).$type[$id].'/'.get_file_path($file);
}
2020-08-09 14:08:44 +00:00
return $str;
}
2020-08-25 14:40:22 +00:00
function get_online_domain($file){
//字符串拼接用函数
global $type;
2020-08-25 14:40:22 +00:00
global $id;
$str='https://'.get_sub_domain($file).'.'.get_main_domain($file).$type[$id].'/';
return $str;
}
2020-08-25 14:40:22 +00:00
function output_file($array){
header('Content-type: '.$array['header']);
if($array['header']=="application/json"){
//是JSON文件不强制下载
2020-08-09 14:08:44 +00:00
}else{
2020-08-25 14:40:22 +00:00
//非JSON文件直接强制下载
header('Content-Disposition: attachment; filename="'.basename($array['file']).'"');
2020-08-09 14:08:44 +00:00
}
2020-08-25 14:40:22 +00:00
echo file_get_contents($array['file']);
2020-08-09 14:08:44 +00:00
}
2020-08-25 14:40:22 +00:00
function check_file_update($file,$url,$time){
global $result;
if(is_file('file/'.$file)){//判断本地文件是否存在
$time=time()-@filemtime('file/'.$file);
if($time<$time){//检查文件是否超出过期期限
echo file_get_contents('file/'.$file);
}else{
2020-08-25 14:40:22 +00:00
if(is_url_exists($url)){//防止源无法访问而损坏文件
if(file_get_contents($url)==@file_get_contents('file/'.$file)){
echo file_get_contents('file/'.$file);
}else{
ob_start();
system('python download.py --url "'.$url.'" --file "'.$file.'" --type "pass" --type_sub "pass"');
ob_clean();
echo file_get_contents('file/'.$file);
}
2020-08-11 09:01:44 +00:00
}else{
2020-08-25 14:40:22 +00:00
echo file_get_contents('file/'.$file);
2020-08-11 09:01:44 +00:00
}
}
2020-08-10 03:32:26 +00:00
}else{
2020-08-25 14:40:22 +00:00
ob_start();
system('python download.py --url "'.$url.'" --file "'.$file.'" --type "pass" --type_sub "pass"');
ob_clean();
echo file_get_contents('file/'.$file);
2020-08-10 03:32:26 +00:00
}
}
2020-08-25 14:40:22 +00:00
function is_server_alive($file){
global $type;
global $id;
$url='https://'.get_sub_domain($file).'.'.get_main_domain($file).$type[$id];
$opts = array(
'http'=>array(
'timeout'=>3,
)
);
$context = stream_context_create($opts);
return @file_get_contents($url, false, $context);//尝试访问文件,无法访问则不存在
2020-08-09 14:08:44 +00:00
}
2020-08-25 14:40:22 +00:00
if(strstr($_GET['path'],'?')!=False){//判断有无特殊字符
$get_path=str_replace('?','/',$_GET['path']);
}else{
$get_path=$_GET['path'];
}
if(get_main_domain($get_path)=='mojang'){//分配顶级域名尾
$id=0;
2020-08-25 14:40:22 +00:00
}elseif(get_main_domain($get_path)=='fabricmc'){
$id=1;
2020-08-25 14:40:22 +00:00
}elseif(get_main_domain($get_path)=='liteloader'){
2020-08-11 07:39:44 +00:00
$id=2;
2020-08-25 14:40:22 +00:00
}elseif(get_main_domain($get_path)=='yushi'){
2020-08-11 07:39:44 +00:00
$id=3;
2020-08-25 14:40:22 +00:00
}elseif(get_main_domain($get_path)=='minecraft'){
$id=4;
}
2020-08-09 14:08:44 +00:00
if($_GET['error']!=""){
2020-08-25 14:40:22 +00:00
header('Content-type: application/json');
echo '{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":'.$_GET['error'].',"server_time":'.time().',"server_id":'.$server_id.'}';
}elseif($get_path==""){
header('Content-type: application/json');
echo '{"wikis":"https://gitee.com/ghink/gemcapi/wikis","code":200,"server_time":'.time().',"server_id":'.$server_id.'}';
2020-08-09 14:08:44 +00:00
}else{
2020-08-25 14:40:22 +00:00
$link=mysqli_connect("localhost","gemcapi","ghink2014","gemcapi");
$result=mysqli_query($link,"SELECT * FROM `common` WHERE `path` = '".$get_path."';");
$result=mysqli_fetch_array($result);
if(in_array($get_path,$active)){
//需要保持最新的文件,不进行数据库操作
if(strstr($get_path,'mojang/')!=False){//目录分组
if(strstr($get_path,'mojang/launchermeta/')!=False){
header('Content-type: application/json');
check_file_update('mojang/launchermeta/mc/game/version_manifest.json','https://launchermeta.mojang.com/mc/game/version_manifest.json','http://launchermeta.mojang.com',86400);
2020-08-09 14:08:44 +00:00
}
2020-08-25 14:40:22 +00:00
}elseif(strstr($get_path,'forge/')!=False){//目录分组
if(strstr($get_path,'forge/last')!=False){
header('Content-type: application/json');
check_file_update('forge/last/index.json','https://download.mcbbs.net/forge/last','https://download.mcbbs.net/maven/net/minecraftforge/forge/1.7.10-10.13.0.1151/forge-1.7.10-10.13.0.1151-installer.jar',86400);
}elseif(strstr($get_path,'forge/minecraft')!=False){
header('Content-type: application/json');
check_file_update('forge/minecraft/index.json','https://download.mcbbs.net/forge/minecraft','https://download.mcbbs.net/maven/net/minecraftforge/forge/1.7.10-10.13.0.1151/forge-1.7.10-10.13.0.1151-installer.jar',86400);
2020-08-11 07:39:44 +00:00
}
2020-08-25 14:40:22 +00:00
}elseif(strstr($get_path,'optifine/')!=False){//目录分组
if(strstr($get_path,'optifine/versionlist')!=False){
header('Content-type: application/json');
check_file_update('optifine/versionlist/index.json','https://download.mcbbs.net/optifine/versionlist','https://download.mcbbs.net/maven/net/minecraftforge/forge/1.7.10-10.13.0.1151/forge-1.7.10-10.13.0.1151-installer.jar',86400);
2020-08-11 07:39:44 +00:00
}
2020-08-25 14:40:22 +00:00
}elseif(strstr($get_path,'liteloader/')!=False){//目录分组
if(strstr($get_path,'liteloader/dl/versions')!=False){
header('Content-type: application/json');
check_file_update('liteloader/dl/versions/versions.json','http://dl.liteloader.com/versions/versions.json','http://dl.liteloader.com/versions/versions.json',86400);
2020-08-09 14:08:44 +00:00
}
2020-08-25 14:40:22 +00:00
}
}else{
if(strstr($get_path,'forge/list')!=False){//其他特殊同步文件
check_file_update($get_path.'/index.json','https://bmclapi2.bangbang93.com/'.$get_path,'https://bmclapi2.bangbang93.com/forge/list/1',86400);
}elseif(empty($result)){//普通文件
//数据库中没有记录该文件
download_file(get_online_url($get_path),$get_path,get_main_domain($get_path),'common');
header("refresh: 0");
2020-08-09 14:08:44 +00:00
}else{
2020-08-25 14:40:22 +00:00
//数据库中记录了该文件
if(is_file($result['file'])){//判断本地文件是否存在
//文件存在
output_file($result);
2020-08-09 14:08:44 +00:00
}else{
2020-08-25 14:40:22 +00:00
//文件不存在
mysqli_query($link,"DELETE FROM `common` WHERE `path`='".$get_path."'");
header("refresh: 0");
2020-08-09 14:08:44 +00:00
}
}
}
2020-08-25 14:40:22 +00:00
}