<?php


  class files{
	  var $extend = array();
	  function __construct(){
	      $this->extend = array('txt','tpl','html','htm','php','js','csv');
	  }
      function dirbox($url){
	      $handler = opendir($url);
          $left=array();
          $right=array();
          while( ($filename = readdir($handler)) !== false ) 
          {
              if($filename != "." && $filename != ".." && $filename != "Thumbs.db" && $filename != $this->name($_SERVER['PHP_SELF']))
              {    
				   if($this->type($url."/".$filename)=='dir'){
                       $left[]  = iconv("gb2312","UTF-8",$filename);
				   }else{
				       $right[] = iconv("gb2312","UTF-8",$filename);
				   }
              }
          }
          closedir($handler);
		  $content = @array_merge($left,$right);
		  return $content;
	  }
	  function showfile($url){
	      return @file_get_contents(iconv('utf-8','gb2312',$url));
	  }
	  function wfile($file,$content){
	      return file_put_contents(iconv('utf-8','gb2312',$file),$content);
	  }
	  function type($file){
	      return filetype(iconv('utf-8','gb2312',$file));
	  }
	  function ftime($file){
	      return date("Y-m-d H:i:s",filemtime($file));
	  }
	  function isarray($array){
	      if(is_array($array)){
		      return implode($array);
		  }else{
		      return $array;
		  }
	  }
	  function name($url){
		  $url1 = explode("/",$url);
	      return array_pop($url1);
	  }
	  function back($url){
          return "";
		  $url1 = explode("/",$url);
		  if(eregi("/",$url)){
		      $back = preg_replace("/\/".$url1[(count($url1)-1)]."$/i","",$url);		  
	          return "<a href='?dir=".$back."'>返回上一级目录</a>";
		  }else{
		      return "";
		  }
	  }
	  function del(){
	      if(isset($_GET['del']) && !empty($_GET['del'])){
			  if(file_exists($_GET['del'])){
				  if($this->type($_GET['del'])=='file'){
		              @unlink(iconv('utf-8','gb2312',$_GET['del']));
				  }else{
				      @rmdir(iconv('utf-8','gb2312',$_GET['del']));
				  }
			  }
		  }
	  }
	  function post(){
	      if(isset($_POST['submit'])){
		      $url     = $_POST['url'];
			  $content = str_replace("&lt;?php", "<?php", $_POST['content']);
              $content = str_replace("?&gt;", "?>", $content);
			  $this->wfile($url,$content);
			  echo "<script>alert('编缉成功！');location.href='".$_SERVER['HTTP_REFERER']."'</script>";
		  }
	  }
	  function fdir($url){
	      if($this->type($url)=='file'){
			  $tend = pathinfo($url);
			  if(in_array($tend["extension"],$this->extend)){
		          return "<a href='?edit=".urlencode($url)."'>编缉</a>";
			  }else{
			      return "<a href='".urlencode($url)."' target='_blank'>查看</a>";
			  }
		  }else{
		      return "<a href='?dir=".urlencode($url)."'>打开</a>";
		  }
	  }
	  function table($res,$url=''){
	      return '<table><thead>
                    <tr>
	                    <th>序号</th>
	                    <th>文件名称</th>
	                    <th>修改时间</th>
	                    <th>操作</ht>
                    </tr></thead>
                    <tbody>
					  '.$res.'
                    </tbody>
                  </table>';
	  }
	  function edit($content,$url=''){
          $html = '<FORM METHOD=POST ACTION="">'.$this->back($url).'<INPUT TYPE="hidden" NAME="url" value="'.$url.'"><TEXTAREA id="content36524" NAME="content" rows="20" cols="100" style="width:95%;height:400px;">'.$content.'</TEXTAREA><br /><INPUT TYPE="submit" name="submit" value="修改">
              		<input type="button" name="button" value="加载编辑器" onclick="javascript:KE.create(\'content36524\');" />
                    <input type="button" name="button" value="卸载编辑器" onclick="javascript:KE.remove(\'content36524\');" />
                    </FORM>';
	      
          return '<FORM METHOD=POST ACTION="">'.$this->back($url).'<INPUT TYPE="hidden" NAME="url" value="'.$url.'"><TEXTAREA NAME="content" style="width:100%;height:500px;">'.$content.'</TEXTAREA><br /><INPUT TYPE="submit" name="submit" value="修改"></FORM>';
	  }
	  function show($array,$url){
	      for($i=0;$i<count($array);$i++){
              $one = $array[$i];
		      $imp[] = '<tr id="file_'.($i+1).'" onmouseover="cart_item_over(\'file_'.($i+1).'\')" onmouseout="cart_item_out(\'file_'.($i+1).'\')">
	                    <td>'.($i+1).'</td>
	                    <td>'.$one.'</td>
	                    <td>'.$this->ftime($url."/".$one).'</td>
	                    <td>'.$this->fdir($url."/".$one).'｜<a href="?del='.urlencode($url."/".$one).'" onclick="if(!confirm(\'确定要删除吗？删除后不可恢复！\'))return false;">删除</a></td>
	                    </tr>';
		  }
		  return $this->isarray($imp);
	  }
	  function content($url){
          $_GET['edit'] = urldecode($_GET['edit']);
		  $this->del();
		  $this->post();
          if(isset($_GET['dir']) && !empty($_GET['dir'])){
		      $file = $this->dirbox($_GET['dir']);
			  $content = $this->table($this->show($file,$_GET['dir']),$_GET['dir']);
		  }elseif(isset($_GET['edit']) && !empty($_GET['edit'])){
		      $content = $this->edit($this->showfile($_GET['edit']),$_GET['edit']);
		  }else{
		      $file = $this->dirbox($url);
			  $content = $this->table($this->show($file,$url));
		  }	  
		  print $content;
	  }
  }
?>
