www.gusucode.com > ShopEx481 & PHPWind 整合版码程序 > bbs/admin/attachrenew.php

    <?php
!function_exists('adminmsg') && exit('Forbidden');

$basename="$admin_file?adminjob=attachrenew";
($attach_url || $db_ifftp) && adminmsg('attachrenew_forbidden');

if(empty($_POST['action'])){
	InitGP(array('S_start','orderway'));
	$rs = $db->get_one("SELECT COUNT(*) AS count FROM pw_attachs");
	$A_count  = $rs['count'];
	$threaddb = array();
	if($A_count!=0){
		$P_N = 1000;
		$A_C = ceil($A_count/$P_N);
		$L_N = $P_N-$A_C*$P_N+$A_count;
		$S_end = $L_N;
		for($i=1;$i<=$A_C;$i++){
			$num = ($i-1)*$P_N;
			$E_num = $i==$A_C ? $num+$L_N : $num+$P_N;
			$S_num = "$num To $E_num";
			$A_option.="<option value=$i>$S_num</option>";
		}
		!$S_start && $S_start=1;
		$S_start==$A_C ? $S_end=$L_N : $S_end=$P_N;
		$start_limit=($S_start-1)*$P_N;
		$orderway!='size' && $orderway='aid';
		$query=$db->query("SELECT aid,fid,name,size,attachurl FROM pw_attachs ORDER BY $orderway LIMIT $start_limit,$S_end");
		
		while($attach=$db->fetch_array($query)){
			if(!file_exists("$attachdir/$attach[attachurl]")){
				$thread['aid']=$attach['aid'];
				$thread['size']=$attach['size'];
				$thread['url']=$attach['attachurl'];
				$thread['name']=$attach['name'];
				$thread['where']="thread.php?fid=$fid";
				$threaddb[]=$thread;
			}
		}
	}
	include PrintEot('attachrenew');exit;
} else{
	$count = 0;
	$ttable_a = array('pw_tmsgs');
	if($db_tlist){
		$tlistdb = unserialize($db_tlist);
		foreach($tlistdb as $key=>$val){
			$ttable_a[] = 'pw_tmsgs'.$key;
		}
	}
	foreach($ttable_a as $pw_tmsgs){
		$query=$db->query("SELECT t.fid,t.tid,t.authorid,aid FROM $pw_tmsgs tm LEFT JOIN pw_threads t ON t.tid=tm.tid  WHERE aid<>''");
		while($aids=$db->fetch_array($query)){
			$attachs= unserialize(stripslashes($aids['aid']));
			if(is_array($attachs)){
				$update=0;
				foreach($attachs as $key=>$attach){
					if($attach['attachurl'] && file_exists($attachdir.'/'.$attach['attachurl'])){
						$check=$db->get_one("SELECT aid FROM pw_attachs WHERE aid='$attach[aid]'");
						if(!$check){
							$uploadtime=filemtime($attachdir.'/'.$attach['attachurl']);
							$count++;
							$attach['name']=addslashes($attach['name']);
							$db->update("INSERT INTO pw_attachs(aid,fid,uid,tid,pid,name,type,size,attachurl,hits,needrvrc,uploadtime,descrip) VALUES('$attach[aid]','$aids[fid]','$aids[authorid]','$aids[tid]','0','$attach[name]','$attach[type]','$attach[size]','$attach[attachurl]','$attach[hits]','$attach[needrvrc]','$uploadtime','$attach[desc]')");
						}
					} else{
						$count++;
						$check=$db->get_one("SELECT aid FROM pw_attachs WHERE aid='$attach[aid]'");
						if($check){
							$db->update("DELETE FROM pw_attachs WHERE aid='$attach[aid]'");
						}
						$update=1;
						unset($attachs[$key]);
					}
				}
				if($update){
					$attachs=$attachs ? addslashes(serialize($attachs)):'';
					$db->update("UPDATE $pw_tmsgs SET aid='$attachs' WHERE tid='$aids[tid]'");
				}
			} else{
				$count++;
				$db->update("UPDATE $pw_tmsgs SET aid='' WHERE tid='$aids[tid]'");
			}
		}
	}
	$ptable_a = array('pw_posts');
	if($db_plist){
		$p_list=explode(',',$db_plist);
		foreach($p_list as $val){
			$ptable_a[] = 'pw_posts'.$val;
		}
	}
	foreach($ptable_a as $pw_posts){
		$query=$db->query("SELECT fid,tid,authorid,aid FROM $pw_posts WHERE aid<>''");
		while($aids=$db->fetch_array($query)){
			$attachs= unserialize(stripslashes($aids['aid']));
			if(is_array($attachs)){
				$update=0;
				foreach($attachs as $key=>$attach){
					if($attach['attachurl'] && file_exists($attachdir.'/'.$attach['attachurl'])){
						$check=$db->get_one("SELECT aid FROM pw_attachs WHERE aid='$attach[aid]'");
						if(!$check){
							$uploadtime=filemtime($attachdir.'/'.$attach['attachurl']);
							$count++;
							$attach['name']=addslashes($attach['name']);
							$db->update("INSERT INTO pw_attachs (aid,fid,uid,tid,pid,name,type,size,attachurl,hits,needrvrc,uploadtime,descrip) VALUES ('$attach[aid]','$aids[fid]','$aids[authorid]','$aids[tid]','0','$attach[name]','$attach[type]','$attach[size]','$attach[attachurl]','$attach[hits]','$attach[needrvrc]','$uploadtime','$attach[desc]')");
						}
					} else{
						$count++;
						$check=$db->get_one("SELECT aid FROM pw_attachs WHERE aid='$attach[aid]'");
						if($check){
							$db->update("DELETE FROM pw_attachs WHERE aid='$attach[aid]'");
						}
						$update=1;
						unset($attachs[$key]);
					}
				}
				if($update){
					$attachs=$attachs ?  addslashes(serialize($attachs)):'';
					$db->update("UPDATE $pw_posts SET aid='$attachs' WHERE tid='$aids[tid]'");
				}
			} else{
				$count++;
				$db->update("UPDATE $pw_posts SET aid='' WHERE tid='$aids[tid]'");
			}
		}
	}
	adminmsg('attach_renew');
}
?>