<?php
/*
\* ===========================================
\* 项目: CTB文本论坛
\* 版本: 1.3
\* 团队: 实易数码
\* 小组: CTB文本论坛小组
\* 作者: windnets，16hot，险情
\* 功能: 上传文件
\* ===========================================
\* Copyright (c) 2002 
\* 团队主页: http://www.16hot.com
\* 团队信箱: --------------------
\* 创建日期: 2002.6
\* 修改日期: 2003-4-10
\* 修改说明: 代码整理/优化
\* 版权声明: 这个程序是免费软件.
\* ===========================================
*/


class Module extends CommonClass
{
    
    /**
     *构造函数
     *
     */
    function execute()
    {
        if (!$this->sess['sess_uid'] || !$this->sess['sess_uname'] || !$this->sess['sess_upass']) {
            $this->showMsg("注册用户才可以使用此功能，请先登陆/注册!","./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber");
        }
        switch ($this->input['action']) {
            case "uploadfile":
                $this->uploadFile();
                break;
            case "downloadfile":
                $this->downloadFile();
                break;
            case "deletefile":
                $this->deleteFile();
                break;
            case "douploadfile":
            	$this->doUploadFile();
            	break;

            case "uploaduserface":
            	$this->uploadUserFace();
            	break;

        }
    } // end func

	
	/**
	 *显示上传文件界面
	 *2002-4-26
	 */
	function uploadFile()
	{
		$this->file = $this->set[dataPath]."/forums.php";
        $this->forum = $this->select_line("2",$this->input['forumid']);
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $postTitles = $this->get();
        $this->BAN = " &gt; <a href=\"index.php?mods=forumdisplay&forumid={$this->input['forumid']}\">".$this->forum[3]."</a> &gt; <a href=\"index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}\">".$postTitles[2]."</a> &gt; 上传附件";
		$header = $this->header("上传附件");
		$allUploadFileType = explode("|", $this->set[uploadFileType]);
		$showAllType = "<select name='allType'>";
		for ($i=0; $i<count($allUploadFileType); $i++){
            $showAllType .= "<option value=".$allUploadFileType[$i].">|-".$allUploadFileType[$i]."</option>";
		}
        $showAllType .= "</select>";
		//<<
		eval("\$this->outPut(\"".$this->getTemplate("uploadFile")."\");");
	} // end func
	
	
	/**
	 *上传文件具体操作
	 *2002-4-26
	 */
	function doUploadFile()
	{
		$uploadFile 	 = $this->input[uploadFile][tmp_name];
		$uploadFile_name = $this->input[uploadFile][name];
		$uploadFile_size = $this->input[uploadFile][size];
		$check = "0";
        $this->file = $this->set[dataPath]."/forums.php";
        $forum = $this->select_line("2",$this->input['forumid']);
        if ($forum[8] != "") {
            $moderators = explode("|",$forum[8]);
            foreach ($moderators as $modId) {
                $modInfo = explode(",",$modId);
                $mod = $modInfo[0];
                if ($mod == $this->sess['sess_uid'] && session_is_registered('sess_ugrade')) {
                    $check  = "1";
                }
            }
        }
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $userInfo = $this->get();
        $userPosts = $userInfo[13];
        $userRates = $userInfo[18];
		$this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
		if (($userPosts < $this->set[uploadFileLimit] or $this->set[allowUploadFile] == "N" or $line[9] == "0") and $this->sess['sess_ugrade'] != "1" and $check != "1"){
           $this->showMsg("抱歉，您还没有达到上传附件的要求。或管理员已经禁止上传附件。");			
		}
		$newName = explode(".", $uploadFile_name);
		$newNameCount=count($newName)-1;
		$allUploadFileType = explode("|", $this->set[uploadFileType]);
		if (!in_array ($newName[$newNameCount], $allUploadFileType) or ereg("%",$uploadFile_name) or ereg("%",$this->sess['sess_uname'])) {
           $this->showMsg("抱歉，您所上传的附件类型为禁止类型。");
		}
        if ($uploadFile_size >= $this->set[uploadFileSize]){
           $this->showMsg("抱歉，您所上传的附件太大。");
		}
		$getUploadTime = time();
        $getNewUploadName = $this->input['forumid']."_".$this->input['postid']."_".$this->sess['sess_uid']."_".$getUploadTime.".".$newName[$newNameCount];


		copy($uploadFile,$this->set[dataPath]."/upfile/".$getNewUploadName);
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
        $line[11] = $line[11].$uploadFile_name."%".$this->sess['sess_uname']."%".$getUploadTime."%".$getNewUploadName."%".$uploadFile_size."|";
        $line[12] = $line[12]."0|";
        $line[9] = "1";
		$line[10] = "1";
        $line[8] = $uploadFile_name;
        $this->overwrite($line);
        if ($this->input['insertPost']=="1" and ($newName[$newNameCount]=="jpg" or  $newName[$newNameCount]=="gif" or $newName[$newNameCount]=="JPG" or $newName[$newNameCount]=="GIF")) {
            $this->file =$this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
            $addLine = $this->get();
			$pothoInfo = "<br><br>\[img\]{$this->set[dataPath]}/upfile/$getNewUploadName\[/img\]";
            $addLine[3] = $addLine[3].$pothoInfo;
            $this->update(1,"1",$addLine);
		}
        if ($this->input['insertPost']=="1" and $newName[$newNameCount]=="swf") {
            $this->file =$this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
            $addLine = $this->get();
			$pothoInfo = "<br><br>\[swf\]{$this->set[dataPath]}/upfile/$getNewUploadName\[/swf\]";
            $addLine[3] = $addLine[3].$pothoInfo;
            $this->update(1,"1",$addLine);
		}
		//<<
    	$this->showMsg("恭喜您，您的附件已经成功上传。","./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber");
	} // end func


	/**
	 *下载文件操作
	 *2002-4-26
	 */
	function downloadFile()
	{
		$check = "0";
        $this->file = $this->set[dataPath]."/forums.php";
        $forum = $this->select_line("2",$this->input['forumid']);
        if ($forum[8] != "") {
            $moderators = explode("|",$forum[8]);
            foreach ($moderators as $modId) {
                $modInfo = explode(",",$modId);
                $mod = $modInfo[0];
                if ($mod == $this->sess['sess_uid'] && session_is_registered('sess_ugrade')) {
                    $check  = "1";
                }
            }
        }       
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $userInfo = $this->get();
        $userPosts = $userInfo[13];
        $userRates = $userInfo[18];
		$this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
        if (($userPosts < $this->set[downloadFileLimit] or $line[10] == "0") and $this->sess['sess_ugrade'] != "1" and $check != "1"){
        $this->showMsg("抱歉，您没有下载权限或本附件被管理员禁止下载","./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber");
		}
        
        $downloadNum = explode("|",$line[12]);
		$downloadFile = explode("|",$line[11]);
        for ($i=0; $i<count($downloadNum); $i++){
            if ($this->input['downloadid'] == $i) {
				$downloadNum[$i]++;
                $downloadFileOne = explode("%",$downloadFile[$i]);
			}
		}
        $line[12] = implode("|",$downloadNum);
        $this->overwrite($line);
		$downloadFilePath = $this->set[dataPath]."/upfile/".$downloadFileOne[3];
        header("location:$downloadFilePath");  

	} // end func


	/**
	 *删除附件操作
	 *2002-4-26
	 */
	function deleteFile()
	{
		$check = "0";
        $this->file = $this->set[dataPath]."/forums.php";
        $forum = $this->select_line("2",$this->input['forumid']);
        if ($forum[8] != "") {
            $moderators = explode("|",$forum[8]);
            foreach ($moderators as $modId) {
                $modInfo = explode(",",$modId);
                $mod = $modInfo[0];
                if ($mod == $this->sess['sess_uid'] && session_is_registered('sess_ugrade')) {
                    $check  = "1";
                }
            }
        }       
		$this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
		$downloadFile = explode("|",$line[11]);
        for ($i=0; $i<count($downloadFile); $i++){
            if ($this->input['downloadid'] == $i) {
                $downloadFileOne = explode("%",$downloadFile[$i]);
				$getDownloadFileName = $downloadFileOne[3];
				if ($downloadFileOne[1] == $this->sess['sess_uname']){
					$checkUser = "1";
				}
			}
		}
        if ($checkUser == "1" or $this->sess['sess_ugrade'] == "1" or $check == "1"){
		$this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
		$downloadFile = explode("|",$line[11]);
        for ($j=0; $j<count($downloadFile); $j++){
            if ($this->input['downloadid'] != $j) {
				$downloadFileNew[] = $downloadFile[$j];
			}
		}
		if (count($downloadFileNew)<2) $line[8] = ""; //判断是否没有附件了
        $line[11] = implode("|",$downloadFileNew);
        $this->overwrite($line);

           @unlink ($this->set[dataPath]."/upfile/".$getDownloadFileName);
        $this->showMsg("恭喜，您已经成功删除了此附件","./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber");
		}
		else {
			$this->showMsg("抱歉，您没有删除此附件的权限","./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber");
		}

	}//end func


	/**
	 *用户上传投降
	 *2002-5-7
	 *create by 险情
	 */
	function uploadUserFace()
	{
		$uploadFile 	 = $this->input[uploadFile][tmp_name];
		$uploadFile_name = $this->input[uploadFile][name];
		$uploadFile_size = $this->input[uploadFile][size];
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $userInfo = $this->get();
        $userPosts = $userInfo[13];
        $userRates = $userInfo[18];
		if (($userPosts < $this->set['uploadPhotoLimit'] or $this->set['allowUploadPhoto'] == "N" or $line[9] == "0") and $this->sess['sess_ugrade'] != "1"){
		    $userFacePath = "<font color=red>抱歉，您还没有达到上传头像的要求。或管理员已经禁止上传头像。</font>";
            eval("\$this->outPut(\"".$this->getTemplate("uploadUserFace")."\");");
			exit();
		}
		$newName = explode(".", $uploadFile_name);
		$newNameCount=count($newName)-1;
		$allUploadFileType = explode("|", $this->set['uploadPhotoType']);
		if (!in_array ($newName[$newNameCount], $allUploadFileType) or ereg("%",$uploadFile_name)) {
            $userFacePath = "<font color=red>抱歉，您的头像不符合上传类型。</font>";
            eval("\$this->outPut(\"".$this->getTemplate("uploadUserFace")."\");");
			exit();
		}
        if ($uploadFile_size >= $this->set[uploadPhotoSize]){
            $userFacePath = "<font color=red>抱歉，您的头像文件太大。</font>";
            eval("\$this->outPut(\"".$this->getTemplate("uploadUserFace")."\");");
			exit();
		}
		$faceSize = getimagesize($uploadFile);
		list($foo,$faceWidth,$bar,$faceHeight) = explode("\"",$faceSize[3]);
		if ($faceWidth > $this->set[uploadPhotoWidth]){
            $userFacePath = "<font color=red>抱歉，您的头像太宽了。</font>";
            eval("\$this->outPut(\"".$this->getTemplate("uploadUserFace")."\");");
			exit();
		}
		if ($faceHeight > $this->set[uploadPhotoHeight]){
            $userFacePath = "<font color=red>抱歉，您的头像太高了。</font>";
            eval("\$this->outPut(\"".$this->getTemplate("uploadUserFace")."\");");
			exit();
		}
		$getUploadTime = time();
        $getNewUploadName = $this->sess['sess_uid']."_".$getUploadTime.".".$newName[$newNameCount];
		copy($uploadFile,$this->set[dataPath]."/upface/".$getNewUploadName);

		$this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
		$userInfo = $this->get();
		$userFaceInfo = explode("|",$userInfo[14]);
			@unlink ($this->set[dataPath]."/upface/".$userFaceInfo[1]);
            $userFaceInfo[0] = $userFaceInfo[0];
            $userFaceInfo[1] = $getNewUploadName;
            $userFaceInfo[2] = "1";

		$userInfo[14] = implode("|",$userFaceInfo);

		$this->overwrite($userInfo);
        $userFacePath = "您的头像已经成功上传,点击<a href=\"{$this->set[dataPath]}/upface/$getNewUploadName\" target=\"blank\">此处查看</a>。";
        eval("\$this->outPut(\"".$this->getTemplate("uploadUserFace")."\");");

	}


} // end class

?>