<?php
require_once("../data/admin.php");
require_once("../data/config.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 encrypt($content)
{
	$content=substr(md5($content),4);
	return $content;
	}
	

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


function check_login(){
	global $config;
	if($_POST['username']==$config['username']&&encrypt($_POST['password'])==$config['password'])
	{
		$_SESSION['logintime']=mktime();
		$_SESSION['usershell']=encrypt('administrator');
		header("Location:../page/g.php");
		}
	else
	{
		echo "<script type=\"text/javascript\">alert(\"用户或密码错误！请重试！\");window.location.href=\"../page/g_login.php\";</script>";
		}
}

function check_loginstatu($outtime){
if($_SESSION['usershell']==encrypt('administrator')){
	$now=mktime();
	if($now-$_SESSION['logintime']>$outtime)
	{
		session_destroy();
		header("Location:../page/g_login.php");
		}
	else
	{
		$_SESSION['logintime']=mktime();
		}
	}
else
	{
		session_destroy();
		header("Location:../page/g_login.php");
		}
}

function loginout(){
		session_destroy();
		header("Location:../page/g_login.php");
	}


function site_update(){
	unset($_POST['site_update']);
	$result=$_POST;
	@ $fp=fopen("../data/config.php",'w');
	fwrite($fp,"<?php \n//网站配置信息 \n");
	foreach($result as $key=> $value){
		$content='$config[\''.$key.'\']=\''.addslashes($value)."';\n";
		fwrite($fp,$content,strlen($content));
		}
	fwrite($fp,"\n?>");
	fclose($fp);
	}
	
 
function manager_update(){
	unset($_POST['manager_update']);
	unset($_POST['password_again']);
	$_POST['password']=encrypt($_POST['password']);
	$result=$_POST;
	@ $fp=fopen("../data/admin.php",'w');
	fwrite($fp,"<?php \n//网站管理员信息 \n");
	foreach($result as $key=> $value){
		$content='$config[\''.$key.'\']=\''.addslashes($value)."';\n";
		fwrite($fp,$content,strlen($content));
		}
	fwrite($fp,"\n?>");
	fclose($fp);
	}


function cut_str($sourcestr,$cutlength) 
{ 
   $returnstr=''; 
   $i=0; 
   $n=0; 
   $str_length=strlen($sourcestr);
   while (($n<$cutlength) and ($i<=$str_length)) 
    { 
      $temp_str=substr($sourcestr,$i,1); 
      $ascnum=Ord($temp_str);
      if ($ascnum>=224)    
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,3);          
         $i=$i+3;          
         $n++;            
      }
       elseif ($ascnum>=192) 
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,2);  
         $i=$i+2;            
         $n++;            
      }
       elseif ($ascnum>=65 && $ascnum<=90) 
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,1); 
         $i=$i+1;            
         $n++;            
      }
       else               
      { 
         $returnstr=$returnstr.substr($sourcestr,$i,1); 
         $i=$i+1;            
         $n=$n+0.5;       
      } 
    } 
          if ($str_length>$cutlength){
          $returnstr = $returnstr;
      }
     return $returnstr; 

}

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

function pages($mpurl,$table) { 	
	global $config;
	global $fromlimit;
	global $curr_page;
	global $perpage;
	$conn=conn();
	$sql_count="select `id` from `$table`";
	$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\">首页</a> "; 
	for($i = $from; $i <= $to; $i++) { 
	if($i != $curr_page) { 
	$multipage .= "<a href=\"$mpurl?page=$i\">$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 files(){
			global $fromlimit;
			global $perpage;
			$conn=conn();
			$sql="select id.id ,id.name,id.pro,info.size,info.type,info.up,info.dt,info.des,info.down from id,info where id.id=info.id order by up desc limit $fromlimit,$perpage";
			$result=mysql_query($sql,$conn);
			mysql_close($conn);
			return $result;			
	} 

 function file_view($viewid){
	 $conn=conn();
	 $sql="select id.id ,id.name,id.pro,id.po,id.pass,info.type,info.size,info.up,info.dt,info.des,info.down from id,info where id.id='$viewid' and info.id='$viewid' ";
	 $result=mysql_query($sql,$conn);
	 mysql_close($conn);
	 return $result;
	 
	 }
	 
 
function files_update(){
	 $conn=conn();
	 $sql_id="update id set pass='$_POST[pass]',pro='$_POST[pro]',po='$_POST[po]',name='$_POST[name]' where id='$_POST[id]'";
	 $result_id=mysql_query($sql_id,$conn);
	 $sql_info="update info set des='$_POST[des]' where id='$_POST[id]'";
	 $result_info=mysql_query($sql_info,$conn);
	 mysql_close($conn);

	}



function files_del(){
	$select=$_POST['select'];
	$conn=conn();
	if($select!="")
		{
			$del_num=count($select);
			for($i=0;$i<$del_num;$i++)
				{
					$sql_info="delete from info where id='$select[$i]'";
					$result_info=mysql_query($sql_info,$conn);
					$sql_id="delete from id where id='$select[$i]'";
					$result_id=mysql_query($sql_id,$conn);
					$filepath=mk_path($select[$i]);
					@unlink("$filepath");
				}
		}
	mysql_close($conn);
	}		

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 search($searchword){
	
	$conn=conn();
	$sql="select id.id ,id.name,id.pro,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 ";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;
	}


function singlepage(){
	$conn=conn();
	$sql = "select * from singlepage order by id desc";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;
	}
	

function addsinglepage(){
	$conn=conn();
	$time=time();
	$sql = "insert into singlepage (id,pagename,content,pubdate) values ('$time','$_POST[pagename]','$_POST[content]','$time')";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	}



function singlepage_del(){
	$select=$_POST['select'];
	$conn=conn();
	if($select!="")
		{
			$del_num=count($select);
			for($i=0;$i<$del_num;$i++)
				{
					$sql="delete from singlepage where id='$select[$i]'";
					$result=mysql_query($sql,$conn);
				}
		}
	mysql_close($conn);
	}



function singlepage_view(){
	$id=$_GET['id'];
	$conn=conn();	
	$sql = "select * from singlepage where id = '$id'";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;	
	}



function singlepage_update(){
	$id=$_POST['id'];
	$time=time();
	$conn=conn();
	$sql = "update singlepage set pagename='$_POST[pagename]',content='$_POST[content]',pubdate='$time' where id = '$id'";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;	
	}
	
	
function cleardate_list(){
	global $fromlimit;
	global $perpage;
	$limitdate=trim($_GET['limitdate'])*3600*24;
	$time=time();
	$limitdate=$time-$limitdate;
	$conn=conn();
	$sql="select id.id ,id.name,id.pro,info.size,info.type,info.up,info.dt,info.des,info.down from id,info where id.id=info.id and info.dt<='$limitdate' order by up desc limit $fromlimit,$perpage";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;
	}

function clearpages($mpurl,$table) { 	
	global $config;
	global $fromlimit;
	global $curr_page;
	global $perpage;
	$limitdate=$_GET['limitdate'];
	$conn=conn();
	$sql_count="select `id` from `$table`";
	$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 .= "<a href=\"$mpurl?page=1&limitdate=$limitdate\">首页</a> "; 
	for($i = $from; $i <= $to; $i++) { 
	if($i != $curr_page) { 
	$multipage .= "<a href=\"$mpurl?page=$i&limitdate=$limitdate\">$i</a> "; 
	} else { 
	$multipage .= '<strong>'.$i.'</strong> '; 
	} 
	} 
	$multipage .= $pages > $page ? " … <a href=\"$mpurl?page=$pages&limitdate=$limitdate\">尾页</a>" : " <a href=\"$mpurl?page=$pages&limitdate=$limitdate\">尾页</a>"; 
	} 
	return $multipage; 
	}


function report(){
			global $fromlimit;
			global $perpage;
			$conn=conn();
			$sql="select id.name,id.locked,info.id,info.size,info.type,info.up,info.dt,info.des,info.down,report.reason,report.date from info,report,id where id.id=info.id and report.id=info.id and info.status='1' order by date desc limit $fromlimit,$perpage";
			$result=mysql_query($sql,$conn);
			mysql_close($conn);
			return $result;			
	} 

function report_del(){
	$select=$_POST['select'];
	$conn=conn();
	if($select!="")
		{
			$del_num=count($select);
			for($i=0;$i<$del_num;$i++)
				{
					$sql_report="delete from `report` where id='$select[$i]'";
					$result_report=mysql_query($sql_report,$conn);
					$sql_info="update `info` set `status`='0' where id='$select[$i]'";
					$result_info=mysql_query($sql_info,$conn);
					$sql_free="update `id` set `locked`= '0' where id='$select[$i]'";
					$result_free=mysql_query($sql_free,$conn);
				}
		}
	mysql_close($conn);
	}	

function report_view(){
	$id=$_GET['id'];
	$conn=conn();
	$sql = "select id.id,id.name,report.reason from id,report where report.id ='$id' limit 1";
	$result=mysql_query($sql,$conn);
	mysql_close($conn);
	return $result;
	}
function report_lock(){
	$select=$_POST['select'];
	$conn=conn();
	if($select!="")
		{
			$del_num=count($select);
			for($i=0;$i<$del_num;$i++)
				{
					$sql="update `id` set `locked`= '1' where id='$select[$i]'";
					$result=mysql_query($sql,$conn);
				}
		}
	mysql_close($conn);
	
	}

function report_lock_free(){
	$select=$_POST['select'];
	$conn=conn();
	if($select!="")
		{
			$del_num=count($select);
			for($i=0;$i<$del_num;$i++)
				{
					$sql="update `id` set `locked`= '0' where id='$select[$i]'";
					$result=mysql_query($sql,$conn);
				}
		}
	mysql_close($conn);
	
	}

?>