<?php
require_once("data/config_mysql.php");

//数据库链接
function conn(){
	global $config;
	$conn = mysql_connect("$config[dbhost]","$config[dbuser]","$config[dbpwd]");
	if (!$conn)
		  {
		  die('无法连接数据库: ' . mysql_error());
		  }
	mysql_select_db($config['dbname'],$conn);
	mysql_query("set names utf8",$conn);
	return $conn;
	}

function save_to_db($arr)
{
	$id   = $arr['id'];
	$name = $arr['name'];
	$size = $arr['size'];
	$type = $arr['type'];
	$up   = $arr['up'];
	$dt   = $arr['dt'];
	$down = $arr['down'];
	$pro  = $arr['pro'];
	$po   = $arr['po'];
	$des  = $arr['des'];
	$pass = $arr['pass'];

	$conn=conn();
	mysql_query("INSERT INTO id (id,pass,pro,po,name) VALUES ('$id','$pass','$pro','$po','$name');",$conn);
	mysql_query("INSERT INTO info (id,type,size,up,dt,down,des) VALUES ('$id','$type','$size','$up','$dt','$down','$des');",$conn);
	mysql_close($conn);
	return true;
}

function id_info($id)
{

	$conn=conn();
	$result = mysql_query("select * from id where id='$id'",$conn);
	
	
    if($result != false)
    {
		while($res=mysql_fetch_array($result))
		{
		$re['id'] = $res['id'];
		$re['pass'] = $res['pass'];
		$re['pro'] = $res['pro'];
		$re['po'] = $res['po'];
		$re['name'] = $res['name'];
		$re['locked'] = $res['locked'];
		}
	}
	else
	{$re = false;}
	return $re;
	mysql_close($conn);
}

function file_info($id)
{
	$conn=conn();	
	$result = mysql_query("select * from info where id='$id'",$conn);
	while($res=mysql_fetch_array($result))
	{
	$re['id']=$res['id'];
	$re['type']=$res['type'];
	$re['size']=$res['size'];
	$re['up']=$res['up'];
	$re['dt']=$res['dt'];
	$re['down']=$res['down'];
	$re['des']=$res['des'];
	$re['status']=$res['status'];
	}
	return $re;
	mysql_close($conn);
}

function update_down($id)
{
	$conn=conn();
	$dt=time();
	$res = mysql_query("update info set down=down+1,dt='$dt' where id='$id' ",$conn);
	mysql_close($conn);

}

function update_info($arr)
{
	$id   = $arr['id'];
	$name = $arr['name'];
	$pro  = $arr['pro'];
	$po   = $arr['po'];
	$des  = $arr['des'];
	$pass = $arr['pass'];

	$conn=conn();
	mysql_query("update id set name='$name',pro='$pro',po='$po' ,pass='$pass' where id='$id'",$conn);
	mysql_query("update info set des='$des' where id='$id'",$conn);
	mysql_close($conn);
	return true;
    echo "<script type=\"text/javascript\">alert('修改成功！');</script>";
}



function singlepage($id){
	$conn=conn();
	$sql = "select content from singlepage where id ='$id'";
	$result=mysql_query($sql,$conn);
	while($row=mysql_fetch_array($result))
	{
		$singlepage['content']=$row['content'];
		}
	mysql_close($conn);
	return $singlepage;
	}


function size_info($fileSize01) 
{     
   $size01 = sprintf("%u", $fileSize01);     
   if($size01 == 0) { return("0 Bytes");}
   $sizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); 
   return round($size01/pow(1024, ($i = floor(log($size01, 1024)))), 2) . $sizename[$i]; 
}



function mk_path($id)
{
	$time = substr($id,2,12);
	$path1= date('ym',$time);
	$path = "file/$path1/$id.data";
	return $path;
}


function files_del(){
			$href=$_GET['t'];
			$now =time();
			$p = $now - $href;
			if($href > 300){
			$id=$_GET['id'];
			$conn=conn();
			$sql_info="delete from info where id='$id'";
			$result_info=mysql_query($sql_info,$conn);
			$sql_id="delete from id where id='$id'";
			$result_id=mysql_query($sql_id,$conn);
			$filepath=mk_path($id);
			@unlink("$filepath");
			echo "<script type=\"text/javascript\">alert('文件成功删除，现在返回首页');window.location.href='index.php';</script>";	
			}
	}

function report(){
	$id=$_POST['report_id'];
	$date=time();
	$resocn=addslashes($_POST['resocn']);
	$conn=conn();
	$sql="update `info` set `status`='1' where `id` = '$id'";
	$result=mysql_query($sql,$conn);
	$sql_report="insert into `report` (`id`,`reason`,`date`) values ('$id','$resocn','$date')";
	$result_report=mysql_query($sql_report);
	mysql_close($conn);
	echo "感谢你的举报，我们会尽快处理！<a href='$_SERVER[HTTP_REFERER]'>点击返回</a>";
	}


@ ($_GET['page'] ? $curr_page=$_GET['page'] : $curr_page=1); 
$perpage=20; 
$fromlimit=($curr_page-1)*$perpage;   

function pages($mpurl,$table,$searchword) { 	
	global $fromlimit;
	global $curr_page;
	global $perpage;
	$conn=conn();
	$sql_count="select `id` from `$table` where `name` like '%$searchword%'";
	$result_count=mysql_query($sql_count,$conn);
	mysql_close($conn);
	$counts=mysql_num_rows($result_count);  
	$num=$counts;
	
	$multipage = ''; 
	if($num > $perpage) { 
	$page = 10; 
	$offset = 2; 
	
	$pages = ceil($num / $perpage); 
	$from = $curr_page - $offset; 
	$to = $curr_page + $page - $offset - 1; 
	if($page > $pages) { 
	$from = 1; 
	$to = $pages; 
	} else { 
	if($from < 1) { 
	$to = $curr_page + 1 - $from; 
	$from = 1; 
	if(($to - $from) < $page && ($to - $from) < $pages) { 
	$to = $page; 
	} 
	} elseif($to > $pages) { 
	$from = $curr_page - $pages + $to; 
	$to = $pages; 
	if(($to - $from) < $page && ($to - $from) < $pages) { 
	$from = $pages - $page + 1; 
	} 
	} 
	} 
    $multipage .= ' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当前 '.$curr_page.'/'.$pages.' 页 &nbsp;&nbsp;&nbsp;共'.$num.'条结果'; 
	$multipage .= "<a href=\"$mpurl?page=1&searchword=$searchword\">首页</a> "; 
	for($i = $from; $i <= $to; $i++) { 
	if($i != $curr_page) { 
	$multipage .= "<a href=\"$mpurl?page=$i&searchword=$searchword\">$i</a> "; 
	} else { 
	$multipage .= '<strong>'.$i.'</strong> '; 
	} 
	} 
	$multipage .= $pages > $from+$page-1 ? " <strong>…</strong> <a href=\"$mpurl?page=$pages&searchword=$searchword\">尾页</a>" : " <a href=\"$mpurl?page=$pages&searchword=$searchword\">尾页</a>"; 	
	} 
	return $multipage; 
	} 

function search($searchword){
	global $fromlimit;
	global $perpage;
	$conn=conn();
	$sql="select id.id ,id.name,info.size,info.type,info.up,info.dt,info.des,info.down from id,info where id.name like '%$searchword%' and id.id=info.id order by up desc limit $fromlimit,$perpage";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;
	}

function CreateForsearch(){
	$conn=conn();
	global $config;	
	$taskpath='task.php';	
     if(!file_exists($taskpath)){
		 file_put_contents($taskpath,mktime());
		 };
	@ $time=file_get_contents($taskpath);
	$now=mktime();
	if($now-$time>3600*24){
	$conn=conn();
	global $config;	
	$sql="select id.id ,id.name,info.size,info.type,info.up as time,info.down from id,info group by id";	
	$result=mysql_query($sql,$conn);
	while($row=mysql_fetch_array($result)){
			unset($row[0]);
			unset($row[1]);
			unset($row[2]);
			unset($row[3]);
			unset($row[4]);
			unset($row[5]);
			$row['type']=strtolower($row['type']);
			$row['url']=$config['url'].'/t.php/'.$row['id'].'.html';
			$a[]=$row;
		}	
		$c=json_encode($a);
		file_put_contents($config['forsearchfile'].'.txt',$c);
		}
	}
CreateForsearch();
?>