优化网页抓取和增加收录页图片上传 v1.4

This commit is contained in:
六零 2022-12-01 04:06:41 +08:00
parent b1c763abea
commit d3fe15bb6a
3 changed files with 418 additions and 194 deletions

View File

@ -2,11 +2,13 @@
include("../include/common.php");
$grouplists =$DB->query("SELECT * FROM `lylme_groups`");
if(!empty($url = isset($_GET['url']) ? $_GET['url'] : null)) {
header('Content-Type:application/json');
//获取网站信息
$head = get_head($_GET['url']);
exit('{"title": "'.$head['title'].'", "icon": "'.$head['icon'].'","charset": "'.$head['charset'].'"}');
}
else if(isset($_GET['submit']) == 'post') {
$head = json_encode($head,JSON_UNESCAPED_UNICODE); //将合并后的数组转换为json
exit($head); //输出json
} else if(isset($_GET['submit']) == 'post') {
if(isset($_REQUEST['authcode'])) {
session_start();
if(strtolower($_REQUEST['authcode'])== $_SESSION['authcode']) {
@ -15,8 +17,7 @@ else if(isset($_GET['submit']) == 'post') {
exit('{"code": "400", "msg": "网站已关闭收录"}');
}
exit(apply($_POST['name'], $_POST['url'], $_POST['icon'], $_POST['group_id'], $status));
}
else {
} else {
exit('{"code": "-6", "msg": "验证码错误"}');
}
}
@ -28,36 +29,24 @@ else if(isset($_GET['submit']) == 'post') {
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<title>申请收录 - <?php echo explode("-", $conf['title'])[0];?></title>
<title>申请收录 - <?php echo explode("-", $conf['title'])[0];
?></title>
<link rel="icon" href="<?php echo get_urlpath($conf['logo'],siteurl().'/apply');?>" type="image/ico">
<link href="https://cdn.lylme.com/admin/lyear/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.lylme.com/admin/lyear/css/style.min.css" rel="stylesheet">
<style>
#loading{position:absolute;left:0;top:0;height:100vh;width:100vw;z-index:100;display:none;align-items:center;justify-content:center;color:#bbb;font-size:16px}
#loading>img{height:18px;width:18px}
.lylme-wrapper{position:relative}
.lylme-form{display:flex !important;min-height:100vh;align-items:center !important;justify-content:center !important}
.lylme-form:after{content:'';min-height:inherit;font-size:0}
.lylme-center{background:#fff;min-width:29.25rem;padding:30px;border-radius:20px;margin:2.85714em}
.lylme-header{margin-bottom:1.5rem !important}
.lylme-center .has-feedback.feedback-left .form-control-feedback{left:0;right:auto;width:38px;height:38px;line-height:38px;z-index:4;color:#dcdcdc}
.lylme-center .has-feedback.feedback-left.row .form-control-feedback{left:15px}
.code{height:38px}
.apply_gg{margin:20px 0;font-size:15px;line-height:2}
.home{text-decoration: none;color: #bbb;line-height: 2;}
li {list-style-type: none;}
ol, ul {padding-left: 10px;}
</style>
<style>#loading{position:absolute;left:0;top:0;height:100vh;width:100vw;z-index:100;display:none;align-items:center;justify-content:center;color:#bbb;font-size:16px}#loading>img{height:18px;width:18px}.lylme-wrapper{position:relative}.lylme-form{display:flex !important;min-height:100vh;align-items:center !important;justify-content:center !important}.lylme-form:after{content:'';min-height:inherit;font-size:0}.lylme-center{background:#fff;min-width:29.25rem;padding:30px;border-radius:20px;margin:2.85714em}.lylme-header{margin-bottom:1.5rem !important}.lylme-center .has-feedback.feedback-left .form-control-feedback{left:0;right:auto;width:38px;height:38px;line-height:38px;z-index:4;color:#dcdcdc}.lylme-center .has-feedback.feedback-left.row .form-control-feedback{left:15px}.code{height:38px}.apply_gg{margin:20px 0;font-size:15px;line-height:2}.home{text-decoration:none;color:#bbb;line-height:2}li{list-style-type:none}ol,ul{padding-left:10px}</style>
</head>
<body>
<div id="loading"><img src="https://cdn.lylme.com/admin/lyear/img/loading.gif"/> &nbsp;正在获取....</div>
<div id="loading"><img src="https://cdn.lylme.com/admin/lyear/img/loading.gif"/> &nbsp;
正在获取....</div>
<?php
if(!empty($background = background())) {
$background = str_replace('./','../',$background);
echo '<div class="row lylme-wrapper" style="background-image: url('.$background.');background-size: cover;">';}
else{ echo '<div class="row lylme-wrapper">';}
echo '<div class="row lylme-wrapper" style="background-image: url('.$background.');background-size: cover;">';
} else {
echo '<div class="row lylme-wrapper">';
}
?>
<div class="lylme-form">
<div class="lylme-center">
<?php if($conf["apply"]==2) {
@ -73,7 +62,7 @@ if(!empty($background = background())){
<div class="input-group">
<input type="text" class="form-control" name="url" placeholder="完整链接或域名" value="" onchange="gurl()" required >
<span class="input-group-btn">
<button class="btn btn-default" onclick="geturl()" type="button">自动获取</button>
<button class="btn btn-default" onclick="get_url()" type="button">自动获取</button>
</span>
</div></div>
<div class="form-group has-feedback feedback-left row">
@ -99,17 +88,24 @@ if(!empty($background = background())){
<small class="help-block">填写网站名称</small>
</div>
</div>
<div class="form-group has-feedback feedback-left row">
<div class="col-xs-12">
<div class="form-group">
<label>网站图标:</label>
<textarea type="text" id="icon" class="form-control" name="icon" placeholder="填写图标的URL地址"></textarea>
<div class="input-group">
<!-- 用于展示上传文件名的表单 -->
<input type="text" id="icon" class="form-control" name="icon" placeholder="填写图标的URL地址">
<!-- 点击触发按钮 -->
<span class="input-group-btn">
<input type="file" id="file" onchange="uploadimg()" accept="image/png, image/jpeg,image/gif,image/x-icon" style="display: none" />
<button class="btn btn-default" id="uploadImage" onclick="$('#file').click();" type="button">选择</button>
</span>
</div>
<img id="review" src="" width="100px" height="100px" class="center-block" style="display: none;"/>
<span class="mdi mdi-emoticon form-control-feedback" aria-hidden="true"></span>
<small class="help-block">填写图标的<code>URL</code>地址,如:<code>http://www.xxx.com/logo.png</code><br>
部分网站无法自动获取,请手动填写</small>
</div>
</div>
<label>* 验证码:</label>
<div class="form-group has-feedback feedback-left row">
<label>* 验证码:</label>
<div class="col-xs-8">
<input type="text" name="authcode" class="form-control" placeholder="验证码" required>
<span class="mdi mdi-check form-control-feedback" aria-hidden="true"></span>
@ -121,9 +117,7 @@ if(!empty($background = background())){
<div class="form-group">
<button class="btn btn-primary btn-block" onclick="submit()">提交</button>
</div>
<center><p><a href="../" class="home">返回首页</a></p><?php echo $conf['copyright']?></center>
</div>
</div>
</body>

205
include/file.php Normal file
View File

@ -0,0 +1,205 @@
<?php
header('Content-Type:application/json');
include("common.php");
define('SAVE_PATH','files/');//保存路径
define('IMG_NAME',date ("Ymdhis").rand(100,999));//文件名
/**
* 通过curl下载
* @param string $url网上资源图片的url
* @return string
*/
function download_img($url,$referer="") {
$maxsize = pow(1024,2)*5; //文件大小5M
$size = remote_filesize($url); //文件大小
if($size > $maxsize){
exit('{"code": "-1","msg":"抓取的图片超过'.$maxsize/pow(1024,2).'M当前为'.round($size/pow(1024,2),2).'M"}');
}
// if($size == 0){
// exit('{"code": "-1","msg":"抓取图片失败"}');
// }
$img_ext = pathinfo($url, PATHINFO_EXTENSION);
//文件后缀名
$file_type = array("jpg","gif","png","ico");
//允许的后缀名
if (!in_array($img_ext,$file_type)) {
exit('{"code": "-1","msg":"抓取的图片类型不支持"}');
}
$img_name = IMG_NAME.'.'.$img_ext;
//文件名
$dir = ROOT.SAVE_PATH.'download/';
$save_to = $dir.$img_name;
if(!is_dir($dir)) {
mkdir($dir,0755,true);
//创建路径
}
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_POST, 0);
if(!empty($referer)){curl_setopt($ch, CURLOPT_REFERER,$referer);}
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//超过10秒不处理
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//执行之后信息以文件流的形式返回
$data = curl_exec($ch);
curl_close($ch);
$downloaded_file = fopen($save_to, 'w');
fwrite($downloaded_file, $data);
fclose($downloaded_file);
$url = siteurl().'/'.SAVE_PATH.'download/'.$img_name;
echo('{"code": "200","msg":"抓取图片成功","url":"'.$url.'","size":"'.round($size/1024,2).'KB"}');
return $save_to;
}
// 获取远程文件大小
function remote_filesize($url)
{
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$ok = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
return isset($matches[1]) ? $matches[1] : "0";
}
/**
* PHP上传图片
* @param file 生成的文件
* @return string
*/
function upload_img($upfile) {
$maxsize = pow(1024,2)*5;
//文件大小5M
$dir = ROOT.SAVE_PATH.'upload/';
if(!is_dir($dir)) {
mkdir($dir,0755,true);
//创建路径
}
$type = $upfile ["type"];
$size = $upfile ["size"];
$tmp_name = $upfile ["tmp_name"];
switch ($type) {
case 'image/jpeg' :
case 'image/jpg' :
$extend = ".jpg";
break;
case 'image/gif' :
$extend = ".gif";
break;
case 'image/png' :
$extend = ".png";
break;
case 'image/x-icon':
$extend = ".ico";
break;
}
if (empty( $extend )) {
exit('{"code": "-1","msg":"上传的图片类型不支持"}');
}
if ($size > $maxsize) {
exit('{"code": "-1","msg":"图片不能超过'.$maxsize/pow(1024,2).'M"}');
}
$img_name = IMG_NAME.$extend;
//文件名
$save_to = $dir.$img_name;
$url = siteurl().'/'.SAVE_PATH.'upload/'.$img_name;
if (move_uploaded_file ( $tmp_name, $dir . $img_name )) {
echo('{"code": "200","msg":"上传成功","url":"'.$url.'"}');
return $dir . $img_name;
}
}
/**
* 图像裁剪
* @param $title string 原图路径
* @param $content string 需要裁剪的宽
* @param $encode string 需要裁剪的高
*/
function imagecropper($source_path, $target_width, $target_height) {
if(filesize($source_path)<10000){return false;}
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
// 源图过高
if ($source_ratio > $target_ratio) {
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
}
// 源图过宽
elseif ($source_ratio < $target_ratio) {
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
}
// 源图适中
else {
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
switch ($source_mime) {
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
case 'image/x-icon':
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
break;
}
imagesavealpha($source_image, true);
// 保留源图片透明度
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
imagealphablending($target_image, false);
// 不合并图片颜色
imagealphablending($cropped_image, false);
// 不合并图片颜色
imagesavealpha($target_image, true);
// 保留目标图片透明
imagesavealpha($cropped_image, true);
// 保留目标图片透明
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 裁剪
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
// 缩放
imagepng($target_image,$source_path);
imagedestroy($target_image);
return true;
}
if(empty($_POST["url"]) &&!empty($_FILES["file"])) {
$filename = upload_img($_FILES["file"]);
//上传图片
} elseif(!empty($_POST["url"])) {
$filename = download_img($_POST["url"],$_POST["referer"]);
//下载图片
} else {
exit('{"code": "0","msg":"error"}');
}
imagecropper($filename,480,480)
?>

View File

@ -43,7 +43,6 @@ function is_spider(){
}
return false;
}
function daddslashes($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
@ -132,6 +131,9 @@ function saveSetting($k, $v) {
}
//获取相对路径
function get_urlpath($srcurl,$baseurl) {
if(substr($srcurl,0,2)=="//"){
return parse_url($baseurl)['scheme'].':'.$srcurl;
}
if(empty($srcurl))return '';
$srcinfo = parse_url($srcurl);
if(isset($srcinfo['scheme'])) {
@ -192,7 +194,8 @@ function get_real_ip() {
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
function yan() {
$filename = ROOT.'/assets/data/data.dat'; //随机一言文件路径
$filename = ROOT.'/assets/data/data.dat';
//随机一言文件路径
if (file_exists($filename)) {
$data = explode(PHP_EOL, file_get_contents($filename));
$result = str_replace(array(
@ -220,36 +223,64 @@ function rearr($data,$arr){
$arr = str_replace('{link_icon}', $icon,$arr);
return $arr;
}
//获取head
function get_head($url) {
ini_set("user_agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39 Lylme/11.24");
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>4
)
);
$contents = @file_get_contents("compress.zlib://".$url, false, stream_context_create($opts));
preg_match('/<title>(.*?)<\/title>/is',$contents,$title); // 获取网站标题
preg_match('/<link rel=".*?icon" * href="(.*?)".*?>/is', $contents,$icon); // 获取网站icon
preg_match('/<meta.+?charset=[^\w]?([-\w]+)/i', $contents,$charset); //获取网站编码
$get_heads = array();
$get_heads['charset']=$charset[1];
$get_heads['title'] = str_replace("'","\"",preg_replace("/\s/","",$title[1]));
$get_heads['icon'] = get_urlpath(preg_replace("/\s/","",$icon[1]),$url);
if(strtolower($get_heads['charset'])!="uft-8"){
// 将非UTF-8编码转换
$get_heads['title'] = iconv($get_heads['charset'], "UTF-8",$get_heads['title']);
$get_heads['icon'] = iconv($get_heads['charset'], "UTF-8",$get_heads['icon']);
header("Content-type:text/html;charset=utf-8");
$data = get_curl($url);
//获取网站title
preg_match('/<title.*?>(?<title>.*?)<\/title>/sim', $data, $title);
$encode = mb_detect_encoding($title['title'], array('GB2312','GBK','UTF-8', 'CP936'));
//得到字符串编码
$file_charset = iconv_get_encoding()['internal_encoding'];
//当前文件编码
if ( $encode != 'CP936' && $encode != $file_charset) {
$title = iconv($encode, $file_charset, $title['title']);
$data = iconv($encode, $file_charset, $data);
} else {
$title = $title['title'];
}
return $get_heads;
if(empty($get_heads['title'])&&empty($get_heads['icon']))exit('Unable to access');
// 获取网站icon
preg_match('/<link rel=".*?icon" * href="(.*?)".*?>/is', $data,$icon);
preg_match('/<meta +name *=["\']?description["\']? *content=["\']?([^<>"]+)["\']?/i', $data, $description);
preg_match('/<meta +name *=["\']?keywords["\']? *content=["\']?([^<>"]+)["\']?/i', $data, $keywords);
$icon = $icon[1];
if(!empty($icon)){
$icon = get_urlpath($icon,$url);
}else{
$parse = parse_url($url);
$port = $parse['port']==80||$parse['port']=="" ? '': ":".$parse['port'];
$iconurl = $parse['scheme'].'://'.$parse['host'].$port.'/favicon.ico';
if(get_curl($iconurl)!=404) {
$icon = $iconurl;
}
}
$get_heads=array("title" =>$title,"charset"=> $encode,"icon" => $icon,"description"=>$description[1],"keywords"=>$keywords[1],"url"=>$url);
return $get_heads;
}
//模拟GET请求
function get_curl($url) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39 Lylme/11.24'
),
));
$contents = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if($httpCode==404) {
return $httpCode;
}
return $contents;
}
//长度判断
function strlens($str) {
if(strlen($str) > 255) {
@ -269,14 +300,11 @@ function apply($name, $url, $icon, $group_id, $status){
if(empty($name) || empty($url) || empty($group_id)) {
//|| empty($icon)
return('{"code": "-1", "msg": "必填项不能为空"}');
}
else if(!preg_match('/^http*/i', $url)) {
} else if(!preg_match('/^http*/i', $url)) {
return('{"code": "-2", "msg": "链接不符合要求"}');
}
else if(strlens($name)||strlens($url)||strlens($icon)||strlens($group_id)||strlens($userip)) {
} else if(strlens($name)||strlens($url)||strlens($icon)||strlens($group_id)||strlens($userip)) {
return('{"code": "500", "msg": "非法参数"}');
}
else {
} else {
global $DB;
if($DB->num_rows($DB->query("SELECT * FROM `lylme_apply` WHERE `apply_url` LIKE '".$url."';"))>0) {
return('{"code": "-3", "msg": "链接已存在,请勿重复提交"}');
@ -290,14 +318,12 @@ function apply($name, $url, $icon, $group_id, $status){
case 1:
if(ins_link($name, $url, $icon, $group_id, $status,$userip)) {
return('{"code": "200", "msg": "网站已收录"}');
}
else{
} else {
return('{"code": "-5", "msg": "请联系网站管理员"}');
}
break;
}
}
else {
} else {
return('{"code": "-4", "msg": "未知错误,请联系网站管理员"}');
}
}
@ -323,8 +349,7 @@ function theme_file($file){
$theme = ROOT.'template/'.$conf['template'].'/'.$file;
if(file_exists($theme)) {
return $theme;
}
else{
} else {
return 'template/'.$file;
}
}