!empty($attach) and attach_big_insert($attach);
$attachlist = $imagelist = $filelist = '';
$images = 0;
$files = 0;
// 处理不在 message 中的图片,删除掉没有插入的附件
if ($message) {
// 只有评论会传pid
list($attachlist, $imagelist, $filelist) = $pid ? well_attach_find_by_pid($pid) : well_attach_find_by_tid($tid);
if (!empty($imagelist)) {
$aids = array();
foreach ($imagelist as $key => $attach) {
$image_url = $conf['upload_url'] . 'website_attach/' . $attach['filename'];
if (FALSE === strpos($message, $image_url)) {
unset($attachlist[$attach['aid']], $imagelist[$attach['aid']]);
$aids[] = $attach['aid'];
$path = $conf['upload_path'] . 'website_attach/' . $attach['filename'];
is_file($path) and unlink($path);
// 删除云储存文件
}
}
!empty($aids) and well_attach__delete($aids);
}
$images = count($imagelist);
$files = count($filelist);
}
$return += array('tid' => $tid, 'pid' => $pid, 'icon' => $icon, 'message' => $message, 'images' => $images, 'files' => $files);
return $return;
}
// 关联内容中的文件,逐渐放弃使用该函数,使用 well_attach_assoc_handle()
function well_attach_assoc_file($arr = array())
{
global $conf, $time;
$uid = array_value($arr, 'uid', 0);
$tid = array_value($arr, 'tid', 0);
$post_create = array_value($arr, 'post_create', 0); // 创建回复
$pid = array_value($arr, 'pid', 0);
$images = array_value($arr, 'images', 0);
$files = array_value($arr, 'files', 0);
$message = array_value($arr, 'message');
if (!$tid && !$pid) return $message;
$attach_dir_save_rule = array_value($conf, 'attach_dir_save_rule', 'Ym');
$day = date($attach_dir_save_rule, $time);
$path = $conf['upload_path'] . 'website_attach/' . $day;
$url = $conf['upload_url'] . 'website_attach/' . $day;
is_dir($path) || mkdir($path, 0777, TRUE);
if (!empty($arr['sess_tmp_files'])) {
$message = urldecode($message);
preg_match_all('#
]+src="(.*?)"#i', $message, $match);
$localurlarr = array(
'http://' . $_SERVER['SERVER_NAME'] . '/',
'https://' . $_SERVER['SERVER_NAME'] . '/',
);
// 跳过云储存
$conf['cloud_url'] and $localurlarr[] = $conf['cloud_url'];
/*$match[1]
Array
(
[0] => https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2021/1109/d3321a58j00r29xq20013c000hk00h4g.jpg&thumbnail=650x2147483647&quality=80&type=jpg
[1] => https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2021/1109/e4974b87j00r29xq30013c000fi00i3g.jpg&thumbnail=650x2147483647&quality=80&type=jpg
[2] => upload/tmp/1_KAUXKFMJHFTUMNS.jpg
[3] => https://nimg.ws.126.net/?url=http://dingyue.ws.126.net/2021/1109/5a7be552j00r29xq3001qc000hs00hvg.jpg&thumbnail=650x2147483647&quality=80&type=jpg
)*/
$upload_arr = array();
$http_arr = array();
if (!empty($match[1])) {
foreach ($match[1] as $_url) {
foreach ($localurlarr as $localurl) {
if ($localurl == substr($_url, 0, strlen($localurl))) continue 2;
}
if (substr($_url, 0, 7) == 'http://' || substr($_url, 0, 8) == 'https://') {
$http_arr[] = $_url;
} elseif (substr($_url, 0, 11) == 'upload/tmp/' || substr($_url, 0, 12) == '/upload/tmp/' || substr($_url, 0, 14) == '../upload/tmp/') {
$upload_arr[] = $_url;
}
}
}
$attach = array();
foreach ($arr['sess_tmp_files'] as $file) {
// 过滤非内容图,不包括附件
if (!in_array($file['url'], $upload_arr) && 0 != $file['isimage']) {
unlink($file['path']);
continue;
}
// 后台提交的内容需要替换掉../
$file['url'] = $file['backstage'] ? str_replace('../upload/', 'upload/', $file['url']) : str_replace('/upload/', 'upload/', $file['url']);
// 内容附件 将文件移动到 upload/website_attach 目录
$filename = file_name($file['url']);
// 绝对路径
$destfile = $path . '/' . $filename;
// 相对路径
$desturl = $url . '/' . $filename;
// 复制
xn_copy($file['path'], $destfile) || xn_log("xn_copy($file[path]), $destfile) failed, tid:$tid, pid:$pid", 'php_error');
if (is_file($destfile) && filesize($destfile) == filesize($file['path'])) unlink($file['path']);
// 按照$destfile文件路径,上传至云储存或图床,返回数据.附件分离,最优方案是redis队列,单独写上传云储存php文件,nohup后台运行,将队列数据上传云储存,然后根据aid更新附件表attach_on、image_url自动,根据tid更新主题表attach_on。关联附件上传云储存,有可能导致超时。
$attacharr = array(
/*'tid' => $tid,
'pid' => $pid,*/
'uid' => $uid,
'filesize' => $file['filesize'],
'width' => $file['width'],
'height' => $file['height'],
'filename' => $day . '/' . $filename,
'orgfilename' => $file['orgfilename'],
//'image_url' => '', // 图床文件完整网址
'filetype' => $file['filetype'],
'create_date' => $time,
'isimage' => $file['isimage']
);
$tid and $attacharr += $pid ? array('pid' => $pid) : array('tid' => $tid);
$attach[] = $attacharr;
// 关联内容再入库
//$aid = well_attach_create($attach);
$file['backstage'] and $message = str_replace('../upload/', 'upload/', $message);
$message = str_replace($file['url'], $desturl, $message);
}
!empty($attach) and attach_big_insert($attach);
// 清空 session
$_SESSION['tmp_website_files'] = array();
}
// 更新附件数
$update = array();
$_images = 0;
$_files = 0;
// 处理不在 message 中的图片,删除掉没有插入的图片附件
if ($message) {
// 只有评论会传pid
list($attachlist, $imagelist, $filelist) = $pid ? well_attach_find_by_pid($pid) : well_attach_find_by_tid($tid);
if (!empty($imagelist)) {
$aids = array();
foreach ($imagelist as $key => $attach) {
$url = $conf['upload_url'] . 'website_attach/' . $attach['filename'];
if (FALSE === strpos($message, $url)) {
unset($attachlist[$attach['aid']], $imagelist[$attach['aid']]);
$aids[] = $attach['aid'];
$path = $conf['upload_path'] . 'website_attach/' . $attach['filename'];
is_file($path) and unlink($path);
// 删除云储存文件
}
}
!empty($aids) and well_attach__delete($aids);
}
$_images = count($imagelist);
$images != $_images and $update['images'] = $_images;
$_files = count($filelist);
$files != $_files and $update['files'] = $_files;
}
if (empty($update)) return $pid ? array($message, $_images, $_files) : $message;
if ($pid) {
if ($post_create) {
$update['message'] = $message;
comment__update($pid, $update);
} else {
// 编辑回复返回的数据
return array($message, $_images, $_files);
}
} else {
well_thread_update($tid, $update);
}
return $message;
}
// thumbnail:主题主图 post:内容图片或附件
function well_attach_assoc_type($type)
{
switch ($type) {
case 'thumbnail':
$k = 'tmp_thumbnail';
break;
case 'post':
$k = 'tmp_website_files';
break;
default:
return NULL;
break;
}
$sess_tmp_files = _SESSION($k);
// 如果session中没有,从数据库中获取储存的session
//if (empty($sess_tmp_files) && preg_match('#' . $k . '\|(a\:1\:\{.*\})#', _SESSION('data'), $matches)) $sess_tmp_files = unserialize(str_replace(array('+', '='), array('_', '.'), $matches['1']));
return $sess_tmp_files;
}
// Create thumbnail
function well_attach_create_thumbnail($arr)
{
global $conf, $time, $forumlist, $config;
$uid = array_value($arr, 'uid', 0);
$tid = array_value($arr, 'tid', 0);
$fid = array_value($arr, 'fid', 0);
$forum = array_value($forumlist, $fid);
$picture = $config['picture_size'];
$picture = isset($forum['thumbnail']) ? $forum['thumbnail'] : $picture['picture_size'];
$pic_width = $picture['width'];
$pic_height = $picture['height'];
$attachlist = well_attach_assoc_type('post');
if (empty($attachlist)) return;
$attach_dir_save_rule = array_value($conf, 'attach_dir_save_rule', 'Ym');
$day = date($attach_dir_save_rule, $time);
$path = $conf['upload_path'] . 'thumbnail/' . $day;
is_dir($path) || mkdir($path, 0777, TRUE);
$tmp_file = $conf['upload_path'] . 'tmp/' . $uid . '_' . $tid . '_' . $time . '.jpeg';
$i = 0;
foreach ($attachlist as $val) {
++$i;
if (1 == $val['isimage'] && 1 == $i) {
'clip' == array_value($conf, 'upload_resize', 'clip') ? well_image_clip_thumb($val['path'], $tmp_file, $pic_width, $pic_height) : well_image_thumb($val['path'], $tmp_file, $pic_width, $pic_height);
break;
}
}
$destfile = $path . '/' . $uid . '_' . $tid . '_' . $time . '.jpeg';
xn_copy($tmp_file, $destfile) || xn_log("xn_copy($tmp_file), $destfile) failed, tid:$tid", 'php_error');
}
function well_save_remote_image($arr)
{
global $conf, $time, $forumlist, $config;
$message = array_value($arr, 'message');
$tid = array_value($arr, 'tid', 0);
$fid = array_value($arr, 'fid', 0);
$uid = array_value($arr, 'uid', 0);
$thumbnail = array_value($arr, 'thumbnail', 0);
$save_image = array_value($arr, 'save_image', 0);
$attach_dir_save_rule = array_value($conf, 'attach_dir_save_rule', 'Ym');
$day = date($attach_dir_save_rule, $time);
$attach_dir = $conf['upload_path'] . 'website_attach/' . $day . '/';
$attach_url = $conf['upload_url'] . 'website_attach/' . $day . '/';
is_dir($attach_dir) || mkdir($attach_dir, 0777, TRUE);
if ($thumbnail) {
$picture = $config['picture_size'];
$forum = array_value($forumlist, $fid);
$picture = isset($forum['thumbnail']) ? $forum['thumbnail'] : $picture['picture_size'];
$pic_width = $picture['width'];
$pic_height = $picture['height'];
$thumbnail_path = $conf['upload_path'] . 'thumbnail/' . $day . '/';
is_dir($thumbnail_path) || mkdir($thumbnail_path, 0777, TRUE);
$tmp_file = $thumbnail_path . $uid . '_' . $tid . '_' . $time . '.jpeg';
}
$localurlarr = array(
'http://' . $_SERVER['SERVER_NAME'] . '/',
'https://' . $_SERVER['SERVER_NAME'] . '/',
);
// 跳过云储存
$conf['cloud_url'] and $localurlarr[] = $conf['cloud_url'];
//$save_image_quality = array_value($conf, 'save_image_quality', 0);
$save_image_quality = 0;
$message = urldecode($message);
//$message = str_replace('&', '&', $message);
//$message = htmlspecialchars_decode($message);
preg_match_all('#
]+src="(http.*?)"#i', $message, $match);
if (!empty($match[1])) {
$n = 0;
$i = 0;
foreach ($match[1] as $url) {
foreach ($localurlarr as $localurl) {
if ($localurl == substr($url, 0, strlen($localurl))) continue 2;
}
$full_url = htmlspecialchars_decode($url);
$message = str_replace($url, $full_url, $message);
$getimgsize = getimagesize($full_url);
if (FALSE === $getimgsize) continue; // 非图片跳出
$filename = $uid . '_' . xn_rand(16);
if (1 == $getimgsize[2]) {
$filename .= '.gif';
$destpath = $attach_dir . $filename;
} elseif (in_array($getimgsize[2], array(2, 3, 15, 18))) {
$filename .= '.jpeg';
$destpath = $attach_dir . $filename;
} else {
continue; // 非常见图片格式跳出
}
$desturl = $attach_url . $filename;
$_message = str_replace($full_url, $desturl, $message);
if ($message != $_message) {
if ($save_image) {
if (0 == $save_image_quality) {
$imgdata = https_request($full_url);
//$destpath = $attach_dir . $filename;
file_put_contents_try($destpath, $imgdata);
} else {
// 图片压缩 GD 库效率低下 ImageMagick 需要额外安装扩展
switch ($getimgsize[2]) {
case 1: // GIF
$imgdata = imagecreatefromgif($full_url);
break;
case 2: // JPG
$imgdata = imagecreatefromjpeg($full_url);
break;
case 3: // PNG
$imgdata = imagecreatefrompng($full_url);
break;
case 15: // WBMP
$imgdata = imagecreatefromwbmp($full_url);
break;
case 18: // WEBP
$imgdata = imagecreatefromwebp($full_url);
break;
}
imagejpeg($imgdata, $destpath, $save_image_quality);
imagedestroy($imgdata);
}
}
// 创建缩略图
if ($thumbnail) {
if (1 == ++$i) {
if (empty($save_image)) {
$imgdata = https_request($full_url);
file_put_contents_try($destpath, $imgdata);
}
// 裁切保存到缩略图目录
'clip' == array_value($conf, 'upload_resize', 'clip') ? well_image_clip_thumb($destpath, $tmp_file, $pic_width, $pic_height, $getimgsize) : well_image_thumb($destpath, $tmp_file, $pic_width, $pic_height, $getimgsize);
well_thread_update($tid, array('icon' => $time));
}
if (empty($save_image)) {
is_file($destpath) and unlink($destpath);
continue;
}
}
$filesize = strlen($imgdata);
$attach = array('tid' => $tid, 'uid' => $uid, 'filesize' => $filesize, 'width' => $getimgsize[0], 'height' => $getimgsize[1], 'filename' => "$day/$filename", 'orgfilename' => $filename, 'filetype' => 'image', 'create_date' => $time, 'downloads' => 0, 'isimage' => 1);
$aid = well_attach_create($attach);
$n++;
}
$message = preg_replace('#( $n));
}
return $message;
}
function well_get_image_url($url)
{
if ($n = strpos($url, '.jpg')) {
$_n = $n + 4;
} elseif ($n = strpos($url, '.jpeg')) {
$_n = $n + 5;
} elseif ($n = strpos($url, '.png')) {
$_n = $n + 4;
} elseif ($n = strpos($url, '.gif')) {
$_n = $n + 4;
} elseif ($n = strpos($url, '.bmp')) {
$_n = $n + 4;
}
$url = $n ? mb_substr($url, 0, $_n, 'UTF-8') : NULL;
return $url;
}
?>是我代码中的 color FF1493你的爱让我心动不已