<?php

class uploadMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();
    }

    // 显示管理后台首页
    public function index()
    {
        header("content-type:text/html; charset=utf-8");

        if( class_exists('SaeStorage') ) { //sae判断
           $saetor = new SaeStorage();
           $imgdir='';
           $imgurl= substr(@$saetor->getUrl('public'), 0,-1);
        }else{
           $imgdir=__ROOTDIR__;
           $imgurl=__ROOTURL__;
        }

        $config = $this->config;
        $upload = new UploadFile();
        $upload->maxSize = 1024 * 1024 * $config['accessory_size']; //大小
        $upload->allowExts = explode(',', $config['accessory_type']); //格式
        $upload->savePath = $imgdir . '/upload/' . date('Y-m-d') . '/'; //保存路径
        //$upload->saveRule = date('ymdhis') . mt_rand(); //命名规范
        $upload->thumb = $config['thumbnail_switch']; //缩略图开关
        $upload->thumbMaxWidth = $config['thumbnail_maxwidth']; // 缩略图最大宽度
        $upload->thumbMaxHeight = $config['thumbnail_maxhight']; // 缩略图最大高度
        if(!$upload->upload()) //上传
        {
            echo $upload->getErrorMsg();
            return;
        }else{
           $info = $upload->getUploadFileInfo(); //返回信息 Array ( [0] => Array ( [name] => 未命名.jpg [type] => image/pjpeg [size] => 53241 [key] => Filedata [extension] => jpg [savepath] => ../../../upload/2011-12-17/ [savename] => 1112170727041127335395.jpg ) )

        }

        $time_dir=date('Y-m-d');  
        // 输出
        $extension = end(explode(".", $info[0]['savename']));
        if ($extension == 'jpg' || $extension == 'bmp' || $extension == 'gif' || $extension ==
            'png') {
            if ($config['watermark_switch']) {
                $Image = new Image();
                $tp = $info[0]['savepath'] . $info[0]['savename']; //原图
                $logo = $imgdir.'/public/watermark/' . $config['img_watermark_logo']; //水印图
                $Image->water($tp, $logo, $config['watermark_place']); //执行
            }
            if ($config['thumbnail_switch']) {
                echo $imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'] . '|' . $imgurl.'/upload/' .
                    $time_dir . '/thumb_' . $info[0]['savename'];

                $data['file']=$imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'];
                $data['time']=time();
                $this->model->table('upload')->data($data)->insert();  //上传入库
                $data2['file']=$imgurl.'/upload/' . $time_dir . '/thumb_' . $info[0]['savename'];
                $data2['time']=time();
                $this->model->table('upload')->data($data2)->insert();  //上传入库2
            } else {
                echo $imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'] . '|' .$imgurl. '/upload/' .
                    $time_dir. '/' . $info[0]['savename'];
                $data['file']=$imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'];
                $data['time']=time();
                $this->model->table('upload')->data($data)->insert();  //上传入库
            }

            exit;
        } else {
            if ($extension == 'htm' || $extension == 'html' || $extension == 'php' || $extension ==
                'asp' || $extension == 'jsp' || $extension == 'js') {
                echo '非法上传';
            } else {
                echo $imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'];
                $data['file']=$imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'];
                $data['time']=time();
                $this->model->table('upload')->data($data)->insert();  //上传入库
            }
        }

        
    }


    // 显示管理后台首页
    public function file()
    {
        header("content-type:text/html; charset=utf-8");
        if (empty($_POST['post_do'])) {
        $inputName = $_GET['inputName'];
        echo '<form action="' . __APP__ . '/upload/file" method="post" style=" margin:0; padding:0;"  enctype="multipart/form-data" name="form1" id="form1">';
        echo '<label for="fileField"></label>';
        echo '<input type="file" name="fileField" id="fileField" style=" width:180px;" size="17" />';
        echo '<input name="post_do" type="hidden" value="yes" />';
        echo '<input name="inputName" type="hidden" value="'.$inputName.'" />';
        echo '&nbsp;<input type="submit" value="上传" />';
        echo '</form>';
        return;
        }
		if (empty($_FILES['fileField']['name'])){
			$this->error('未选择文件');
		}

        if( class_exists('SaeStorage') ) { //sae判断
           $saetor = new SaeStorage();
           $imgdir='';
           $imgurl= substr(@$saetor->getUrl('public'), 0,-1);
        }else{
           $imgdir=__ROOTDIR__;
           $imgurl=__ROOTURL__;
        }

        $time_dir=date('Y-m-d');  
        $config = $this->config;
        $upload = new UploadFile();
        $upload->maxSize = 1024 * 1024 * $config['accessory_size']; //大小
        $upload->allowExts = explode(',', $config['accessory_type']); //格式
        $upload->savePath = $imgdir . '/upload/' . date('Y-m-d') . '/'; //保存路径
        //$upload->saveRule = date('ymdhis') . mt_rand(); //命名规范
        if(!$upload->upload()) //上传
        {
            echo $upload->getErrorMsg();
            return;
        }else{
           $info = $upload->getUploadFileInfo(); //返回信息 Array ( [0] => Array ( [name] => 未命名.jpg [type] => image/pjpeg [size] => 53241 [key] => Filedata [extension] => jpg [savepath] => ../../../upload/2011-12-17/ [savename] => 1112170727041127335395.jpg ) )
        }

        // 输出
        $extension = end(explode(".", $info[0]['savename']));
        if ($extension == 'htm' || $extension == 'html' || $extension == 'php' || $extension ==
            'asp' || $extension == 'jsp' || $extension == 'js') {
            echo '<a style="font-size:12px; color:#333" href="' . __APP__ . '/upload/file.html?inputName=' . $_POST['inputName'] .'">非法上传！点击重新上传！</a>';
        } else {
            echo '<a style="font-size:12px; color:#333" href="' . __APP__ . '/upload/file.html?inputName=' . $_POST['inputName'] .'">上传成功！点击重新上传！</a>';
            echo '<script>parent.$("#' . $_POST['inputName'] . '").val("' . $imgurl .
                '/upload/' . $time_dir . '/' . $info[0]['savename'] . '")</script>';
        }
        $data['file']=$imgurl.'/upload/' . $time_dir . '/' . $info[0]['savename'];
        $data['time']=time();
        $this->model->table('upload')->data($data)->insert();  //上传入库


    }


}

?>