<?php
   //+-------------下载系统主程序./index.php---------
   //+-------------@access public--------------------
   //+-------------@auther coldwind------------------
   ob_start();
   require"data/config.php";
   require"include/functions.php";
   $myfile=new IWFile();
   require"include/$language.php";
   if(file_exists("data/sort/list.php")){
      require"data/sort/list.php";
	  }
    //-----
   ///////////////////定义初始量///////////
   if(empty($page)){$page="1";}
   $ip=getip();
   /////////////////////////////////////
   if(!isset($mainarr)||empty($mainarr)||sizeof($mainarr)<=1){
       $mainarr=$myfile->arrayfromfile("data/main.php","\n");
       setcookie("mainarr","$mainarr",time()+100);  
   }
   if(!isset($hitarr)||empty($hitarr)||sizeof($hitarr)<=1){
       $hitarr=$myfile->arrayfromfile("data/hit.php","\n");
       setcookie("hitarr","$hitarr",time()+100);  
   }
     if(!isset($ratingarr)||empty($ratingarr)||sizeof($ratingarr)<=1){
       $ratingarr=$myfile->arrayfromfile("data/rating.php","\n");
       setcookie("ratingarr","$ratingarr",time()+100);  
   }
   if(!isset($sortarr)||empty($sortarr)||sizeof($sortarr)<=1){
       $sortarr=$myfile->arrayfromfile("data/sort.php","\n");
       setcookie("sortarr","$sortarr",time()+100);  
   }
   if(!isset($catearr)||empty($catearr)||sizeof($catearr)<=1){
       $catearr=$myfile->arrayfromfile("data/sort/sort.php","\n");
       setcookie("catearr","$catearr",time()+100);  
   }
   if(!isset($topicarr)||empty($topicarr)||sizeof($topicarr)<=1){
       $topicarr=$myfile->arrayfromfile("data/topic.php","\n");
       setcookie("topicarr","$topicarr",time()+100);  
   }
   $viewarr=$myfile->arrayfromfile("data/view.php","\n");
   //备份数组
   $bk_hitarr=$hitarr;
   $bk_ratingarr=$ratingarr;
   //点击排序
   arsort($hitarr);
   $keys=array_keys($hitarr);
   //评分排序
   arsort($ratingarr);
   $keys1=array_keys($ratingarr);
   require"template/header.html";
   //+----------首页面显示--------------------------------
   if(empty($action)){
       $today=date("d",time(0));
	   //+----------
	   $annarr=$myfile->arrayfromfile("data/ann.php","\n");
	   $total=sizeof($annarr)-2;
	   list($date,$title)=explode("|%|",$annarr[$total]);
	   if($title==""){
	     $title="暂时没有公告";
	   }
	   else{
	     $title="<a href=?action=ann>$title</a>";
	   }
	   
       $total_hit=@$myfile->readfromfile("data/total.php");
	   $day_hit=@$myfile->readfromfile("data/$today.php");
       $total_item=sizeof($mainarr)-1;
       $total_cate=sizeof($catearr)-1;
	   $total_online=OnLine();
	   ////////////////////更新ip记录///////
	   	  if(!file_exists("data/ip.php")){
	     $myfile->writetofile("data/ip.php","|$ip|\n","a+");
	   }
	   else{
	     $ipdata=$myfile->readfromfile("data/ip.php");
	     if(!ereg("\|$ip\|",$ipdata)){
		     //$myfile->writetofile("data/ip.php","|$ip|\n","a+");
		 }
	   }
	   $iparray=$myfile->arrayfromfile("data/ip.php","\n");
	   $total_ip=sizeof($iparray)-1;
	   /////////////////////
       require"template/body.html";
   }
   //+------------显示大类信息--------------
   else if($action=="cata"){
          $sortdata=$catearr[$cata];
		  $sort_id=explode("|",$sortdata);
		  $_sortarr=array_intersect($sortarr,$sort_id);
		  $total_sort_item=sizeof($_sortarr);
         //分页信息
	      $pagenum=ceil($total_sort_item/$adminpage);
	     $pagestart=$total_sort_item-1-$adminpage*($page-1);
         $pageend=$total_sort_item-$adminpage*$page;
	    //--------------
	     $keys_sort=array_keys($_sortarr);
	     $_keys=array_intersect($keys,$keys_sort);
	     $_keys1=array_intersect($keys1,$keys_sort);
		 //+--------
         require"template/cata.html";
         require"template/list.html";
   }
   //+-----------------------显示分类信息---------------
   else if($action=="sort"){
      $_sortarr=ArrSectString($sortarr,$sort_id);
	  $topic_arr=$myfile->arrayfromfile("data/sort/topic.php");
	  $total_sort_item=sizeof($_sortarr);
      //分页信息
	   $pagenum=ceil($total_sort_item/$adminpage);
	   $pagestart=$total_sort_item-1-$adminpage*($page-1);
       $pageend=$total_sort_item-$adminpage*$page;
	  //--------------
	  $keys_sort=array_keys($_sortarr);
	  $_keys=array_intersect($keys,$keys_sort);
	  $_keys1=array_intersect($keys1,$keys_sort);
      require"template/sort.html";
      require"template/list.html";
   }
 //+--------------显示条目信息-------------------------
  else if($action=="info"){
     if(!isset($replyarr)||empty($replyarr)||sizeof($replyarr)<=1){
       $replyarr=$myfile->arrayfromfile("data/reply.php","\n");
       setcookie("replyarr","$replyarr",time()+100);  
      }
     $maininfo=$myfile->readfromfile("data/main/$pid.php");
     list($name,$env,$mod,$size,$date,$link,$user,$p_addr,$s_addr,$brief,$time)=explode("|%|",$maininfo);
	 $hit="$bk_hitarr[$id]";
	 $view=$viewarr[$id];
	 //更新浏览次数
	 $view+=1;
	 $myfile->editfile("data/view.php",$id,"\n",$view);
	 //总点击
	 if(file_exists("data/total.php")){
	   $total_hit=$myfile->readfromfile("data/total.php");
	   $total_hit+=1;
	 }
	 else{
	   $total_hit=1;
	 }
	 $myfile->writetofile("data/total.php",$total_hit,"w+");
	 //已有评论
     $reply_arr=explode("|!|",$replyarr[$id]);
	 //记录日访问数
	 $yesterday=GetYesterday();
	 if(file_exists("data/$yesterday.php")){
	   unlink("data/$yesterday.php");
	 }
	 $today=date("d",time(0));
	 if(file_exists("data/$today.php")){
	      $day_hit=$myfile->readfromfile("data/$today.php");
		  $day_hit+=1;
	 }
	 else{
	      $day_hit=1;
	 }
	 $myfile->writetofile("data/$today.php","$day_hit","w+");
	 //---------------------------------
     $rating=$bk_ratingarr[$id];
	 $rating=ceil($rating);
     require"template/info.html";
  }
  
  //+-----------------------打分动作--------------------------
  else if($action=="rating"){
     if(!ereg("^[1-5]$",$rating)){
	     goback("-1","打分不合法");
		 exit;
	 }
	 $replyarr=$myfile->arrayfromfile("data/reply.php","\n");
	 $oldrating=$bk_ratingarr[$id];
	 $newrating=($oldrating+$rating)/2;
	 $newrating=number_format($newrating,1);
	 $myfile->editfile("data/rating.php",$id,"\n",$newrating);
	 if(trim($saying)!=""){
	   	 $saying=ubb($saying);
		 $date=date("Y-m-d H",time(0));
         $replyarr[$id].="$saying|%|$date|!|";
		 $myfile->editfile("data/reply.php","$id","\n",$replyarr[$id]);
	 }
	 goback("-1","感谢！请返回");
  }
//+--------------------系统搜索动作--------------------
  else if($action=="search"){
    $keyword=urlencode($keyword);
	$urlword=urldecode($keyword);
    $keyword1=RegEncode($urlword);
	if($keyword1==""){
	  goback("-1","关键字不能为空");
	  exit;
	}
	$search_res=array();
	if(empty($select)){
      foreach($mainarr as $key=>$value){
	     list(,,,,$pid)=explode("|%|",$value);
	     $info=$myfile->readfromfile("data/main/$pid.php");
	     if(eregi($keyword1,$info)){
	         $search_res[$key]=$value;
	     }
	  }
	}
	if($select=="1"){
	     foreach($mainarr as $key=>$value){
	     list(,,,,$pid)=explode("|%|",$value);
	     $info=$myfile->readfromfile("data/main/$pid.php");
	     if(eregi($keyword1,$info)||eregi($keyword1,$replyarr[$key])){
	         $search_res[$key]=$value;
	     }
	  }
	}
	$keys_sort=array_keys($search_res);
	$_keys=array_intersect($keys,$keys_sort);
	$_keys1=array_intersect($keys1,$keys_sort);
	$total=sizeof($search_res);
	//分页信息
	$pagenum=ceil($total/$adminpage);
	$pagestart=$total-1-$adminpage*($page-1);
    $pageend=$total+1-$adminpage*$page;
	require"template/search_res.html";
  }
  //+-------------显示所有软件列表-------------------
  else if($action=="list"){
		 $_sortarr=$mainarr;
		 $total_sort_item=sizeof($_sortarr)-1;
         //分页信息
	     $pagenum=ceil($total_sort_item/$adminpage);
	     $pagestart=$total_sort_item-1-$adminpage*($page-1);
         $pageend=$total_sort_item-$adminpage*$page;
	    //--------------
	     $keys_sort=array_keys($_sortarr);
	     $_keys=array_intersect($keys,$keys_sort);
	     $_keys1=array_intersect($keys1,$keys_sort);
         require"template/main.html";
         require"template/list.html";
  }
  //+---------------------------公告列表------------------
  else if($action=="ann"){
    $arr=$myfile->arrayfromfile("data/ann.php","\n");
	require"template/ann.html";
  }
  //+------------------------------专题显示-----------
  else if($action=="topic"){
    	 $_sortarr=ArrSectString($topicarr,"$topic_id");
		 $total_sort_item=sizeof($_sortarr);
         //分页信息
	     $pagenum=ceil($total_sort_item/$adminpage);
	     $pagestart=$total_sort_item-1-$adminpage*($page-1);
         $pageend=$total_sort_item-$adminpage*$page;
	    //--------------
	     $keys_sort=array_keys($_sortarr);
	     $_keys=array_intersect($keys,$keys_sort);
	     $_keys1=array_intersect($keys1,$keys_sort);
         require"template/topic.html";
         require"template/list.html";
  }
  //+-------下载软件------------
  else if($action=="down"){
     $hit=$bk_hitarr[$id];
	 $hit+=1;
	 $myfile->editfile("data/hit.php","$id","\n",$hit);
     $maininfo=$myfile->readfromfile("data/main/$pid.php");
     list(,,,,,,,,$s_addr)=explode("|%|",$maininfo);
	 $newarr=explode("|",$s_addr);
	 header("location:$newarr[$k]");
  }
 require"template/footer.html";
 ob_end_flush();
 //+------------结束-------------------------
?>