<?php

/*
 * Copyright (C) xgcms.com
 */

!defined('FRAMEWORK_PATH') && exit('FRAMEWORK_PATH not defined.');

include POST_PATH.'core/control/common_control.class.php';

class scraw_control extends common_control {
	
	function __construct(&$conf) {
		parent::__construct($conf);
		$this->_checked['bbs'] = ' class="checked"';
		
		// 加载积分策略
		// $this->conf += $this->kv->xget('conf_ext');
		
		// 检查IP 屏蔽
		$this->check_ip();
	}

	public function on_upload(){
		header("Content-Type:text/html;charset=utf-8");
        error_reporting( E_ERROR | E_WARNING );
	    $fid = intval(core::gpc('fid'));
		$pid = intval(core::gpc('pid'));
		$user = $this->_user;
		$this->check_forbidden_group();
		$uid=$user['uid'];
		$forum = $this->forum->read($fid);
		$uid>0 && $user = $this->member->read($uid);
		$group = $this->_group;
        $action =core::gpc('action','G');
		if(empty($forum)||empty($group['allowscraw'])||$user['groupid'] == 7){
			 if ( $action == "tmpImg" ) { 
			      echo "<script>parent.ue_callback('','你所在用户组没有权限玩涂鸦！')</script>";
				  exit;
			 }
			echo "{'url':'','title':'','original':'','state':'没有权限涂鸦'}";
			exit;
		}
		
        include POST_PATH."view/js/ueditor/php/Uploader.class.php";
		
		$uploadpath = 'upload/attach/scraw/';
		
        //上传配置
        $config = array(
            "savePath" => $uploadpath,//"upload/" ,             //存储文件夹
            "maxSize" => 1000 ,                   //允许的文件最大尺寸，单位KB
            "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
        );
       //临时文件目录
       $tmpPath = "upload/attach/tmp/";

       //获取当前上传的类型
	   
       if ( $action == "tmpImg" ) { // 背景上传
			//背景保存在临时目录中
			$config[ "savePath" ] = $tmpPath;
			$up = new Uploader( "upfile" , $config );
			$info = $up->getFileInfo();
			/**
			 * 返回数据，调用父页面的ue_callback回调
			 */
			$info[ "url" ]=str_replace('upload/attach/','',$info['url']);
			echo "<script>parent.ue_callback('" . $info[ "url" ] . "','" . $info[ "state" ] . "')</script>";
		} else {
			
			
			//涂鸦上传，上传方式采用了base64编码模式，所以第三个参数设置为true
			$up = new Uploader( "content" , $config , true );
			
			
			$info = $up->getFileInfo();
			$info[ "url" ]=str_replace('upload/attach/','',$info['url']);

			if($info['state']=='SUCCESS'){
				$arr = array (
					'fid'=>$fid,
					'tid'=>0,
					'pid'=>$pid,
					'filesize'=>$info['size'],
					'width'=>0,
					'height'=>0,
					'filename'=>$info['url'],
					'orgfilename'=>'涂鸦图片',
					'filetype'=>'image',
					'dateline'=>$_SERVER['time'],
					'comment'=>'',
					'downloads'=>0,
					'isimage'=>1,
					'golds'=>0,
					'uid'=>$this->_user['uid'],
				);
				$aid = $this->attachment->create($arr);
				$this->attachment->save_aid_to_tmp($fid, $aid, $uid);
			}
			
			//上传成功后删除临时目录
			if(file_exists($tmpPath)){
				$this->delDir($tmpPath);
			}
			
			echo "{'url':'" . $info[ "url" ] . "',state:'" . $info[ "state" ] . "'}";
		}
		
		
		
		
	}
	private function delDir( $dir )
    {
        //先删除目录下的所有文件：
        $dh = opendir( $dir );
        while ( $file = readdir( $dh ) ) {
            if ( $file != "." && $file != ".." ) {
                $fullpath = $dir . "/" . $file;
                if ( !is_dir( $fullpath ) ) {
                    unlink( $fullpath );
                } else {
                    self::delDir( $fullpath );
                }
            }
        }
        closedir( $dh );
        //删除当前文件夹：
        return rmdir( $dir );
    }
	
	
}




?>