<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Files extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->load->model('CsdjAdmin');
		$this->load->model('CsdjSkins');
                $this->db->cache_off();
	        $this->CsdjAdmin->Admin_Login();
                $this->load->helper('file');
	}


	public function index()
	{
 	    $path = $this->input->get('path');
 	    $page = $this->input->get('page');
            if(empty($page)) $page=1;
            if(empty($path)) $path="/attachment/";
            if(empty($path)) $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，路径为空！</font>','javascript:history.back();');
            if(substr($path,0,11)!='/attachment' && substr($path,0,6)!='/skins'){
                    $this->CsdjAdmin->Admin_Url('<font color=red>参数错误，路径非法！</font>','javascript:history.back();');
            }

            $arrs=get_dir_file_info(".".$path, $top_level_only = TRUE);

            $data['pagesize']=20;
            $data['pagejs']=ceil((count($arrs)-1) / $data['pagesize']); //总页数
            $data['path']=$path;
            $data['page']=$page;
            $data['url']=site_url('admin/files/index/')."?path=".$path;
            $data['show']=array_slice($arrs,$data['pagesize']*(($page>$data['pagejs']?$data['pagejs']:$page)-1),$data['pagesize']);

            if($path=="/attachment/"){
                $data['uppage']=site_url('admin/files/index/');
            }else{
                $data['uppage']="javascript:history.back();";
            }
            $this->load->view('admin/files',$data);
        }

	public function del()
	{
            $this->CsdjAdmin->Admin_Qx('39');
 	    $path = $this->input->get('path');
            if(empty($path)) exit('no');
            if(substr($path,0,11)!='/attachment') exit('no');
  	    $path = ".".$path;
            if(!is_dir($path)){
	         if(file_exists($path)){
		     if(!unlink($path)){
                          echo 'no';
                     }else{
                          echo 'ok';
                     }
	         }
            }
        }

	public function delall()
	{
            $this->CsdjAdmin->Admin_Qx('39');
            $dirs=$this->input->post('dirs');
            $files=$this->input->post('files');

            if($dirs){
                foreach ($dirs as $value) {  //文件夹
                    if(substr($value,0,12)=='./attachment'){
                         $this->CsdjAdmin->deldir($value);
                    }
                }
            }
            if($files){
                 foreach ($files as $value) {  //文件
                    if(substr($value,0,12)=='./attachment'){
	               if(file_exists($value)){
		         @unlink($value);
	               }
                    }
                 }
            }
            $this->CsdjAdmin->Admin_Url('<font color=#0000ff>恭喜您，删除成功！</font>',$_SERVER['HTTP_REFERER'],'ok');
        }
}

