<?php
/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: market_ajax.php 1000 2011-6-31 15:41:43 by angelrain $
 */
 /**
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
*/
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
define('CURRENT_PATH','dzz/ueditor');
$do = empty($_GET['do'])?'':$_GET['do'];
//判断数据唯一
$refer=dreferer();

$file1=trim($_GET['file']);
$file=DISCUZ_ROOT.$file1;
if(!$space['self']){
		echo dzzlang('message','no_privilege');
		exit();
}
if($do=='save'){
	$newsave=intval($_POST['newsave']); 
	$code=trim($_POST['code']);
	$savecmd=trim($_POST['cmd']);
	$message=($_POST['editorValue']);
	$message =preg_replace("/<br.*?>/","",$message);
	if($code) $message=diconv($message,'UTF-8',$code);
	
	if(!$newsave){
		$icoid=intval($_POST['icoid']);
		
		 if($icoarr=textsaveattach($icoid,$message)){
			if($icoarr['error']){
				include dzztemplate('header_common');
				echo "<script type=\"text/javascript\">";
				echo "top.showDialog('".$icoarr['error']."');";
				echo "</script>";	
				include dzztemplate('footer');
				exit();
			}else{
				include dzztemplate('header_common');
				echo "<script type=\"text/javascript\">";	
				if($savecmd=='save'){
					echo "parent.editor_a.fileicoid='".$icoid."';";
					echo "parent.editor_a.fileencode='".$code."';";
					echo "parent.setWinParam({\"needsave\":0});";
					echo "parent.editor_a.needsave=0;";		
				}
				echo "</script>";	
				include dzztemplate('footer');
				exit();
			}
			 
		 }else{
			include dzztemplate('header_common');
			echo "<script type=\"text/javascript\">";	
			echo "top.showDialog('".$file.dzzlang('message','file_save_failure')."');";
			echo "</script>";	
			include dzztemplate('footer');
		 }
	}else{
		$position=trim($_POST['position']);
		$filename_title=trim($_POST['filename']);
		if(strpos($filename_title,'.')===false){
			$filename_title.='.dzzdoc';
		}
		$msg='';
		//判断位置 有无权限存储；
		list($prex,$fid,$fuid)=explode('-',$position);
		$container='';
		if($prex=='d'){
			$container='icosContainer_body_'.$fid;
		}elseif($prex=='f'){
			$container='icosContainer_folder_'.$fid;
		}
		if($fuid!=$space['uid']){
			include dzztemplate('header_common');
			echo "<script type=\"text/javascript\">";
			echo "top.showDialog('".dzzlang('message','no_privilege_not_self')."');";
			echo "</script>";	
			include dzztemplate('footer');
			exit();
		}
		if($icoarr=textsaveToattach($filename_title,$container,$message)){
			if($icoarr['error']){
				include dzztemplate('header_common');
				echo "<script type=\"text/javascript\">";	
				echo "top.showDialog('".$icoarr['error']."');";
				echo "</script>";	
				include dzztemplate('footer');
				exit();
			}else{
				include dzztemplate('header_common');
				echo "<script type=\"text/javascript\">";
				if($savecmd=='save'){
					echo "parent.editor_a.fileicoid='".$icoarr['icoid']."';";	
					echo "parent.editor_a.fileencode='".$code."';";	
					echo "parent.setWinParam({\"needsave\":0});";
					echo "parent.editor_a.needsave=0;";		
				}
				echo "top._ico.createIco(".json_encode($icoarr).");";
				echo "</script>";	
				include dzztemplate('footer');
				exit();
			}
		}else{
			include dzztemplate('header_common');
			echo "<script type=\"text/javascript\">";	
			echo "top.showDialog('".$icoarr['error']?$icoarr['error']:dzzlang('message','file_save_failure')."');";
			echo "</script>";	
			include dzztemplate('footer');
			exit();
		}
	}
}else{
	$txtexts=array('DZZDOC','HTM','HTML','SHTM','SHTML','HTA','HTC','XHTML','STM','SSI','JS','JSON','AS','ASC','ASR','XML','XSL','XSD','DTD','XSLT','RSS','RDF','LBI','DWT','ASP','ASA','ASPX','ASCX','ASMX','CONFIG','CS','CSS','CFM','CFML','CFC','TLD','TXT','PHP','PHP3','PHP4','PHP5','PHP-DIST','PHTML','JSP','WML','TPL','LASSO','JSF','VB','VBS','VTM','VTML','INC','SQL','JAVA','EDML','MASTER','INFO','INSTALL','THEME','CONFIG','MODULE','PROFILE','ENGINE');
	$icoid=intval($_GET['icoid']);
	$type=intval($_GET['type']);
	if(!$icoid){
		$str='&nbsp;';
	}else{
		if(!$icoarr=DB::fetch_first("select icoid,url,name,ext from ".DB::table('dzz_icos')." where icoid='{$icoid}' and type='attach'")){
				include dzztemplate('header_common');
				echo "<script type=\"text/javascript\">";	
				echo "top.showDialog('".dzzlang('message','file_not_exist')."');";
				echo "</script>";	
				include dzztemplate('footer');
				exit();
		}
		if(!in_array(strtoupper($icoarr['ext']),$txtexts)){
				include dzztemplate('header_common');
				echo "<script type=\"text/javascript\">";	
				echo "top.showDialog('".dzzlang('message','file_ext_not_support')."');";
				echo "</script>";	
				include dzztemplate('footer');
				exit();
		}
	
	
		$str=(file_get_contents(DISCUZ_ROOT.$icoarr['url']));
		require_once DISCUZ_ROOT.'./dzz/class/class_encode.php';
		$p=new Encode_Core();
		$code=$p->get_encoding($str);
	//echo 'wwwwwwwww='.$code;
	//$haha= mb_detect_encoding($str);echo 'vvvvvv='.$haha.'<br>';
		$str=preg_replace("/<script[^>]*>.*?<\/script>/ims",'',$str);
		//$str=preg_replace("/<!--\{.*?\}-->/i",'',$str);
		  $str = preg_replace("/>[\n\r\t]+([ ]{4})+/",'>',$str);
		  $str =preg_replace("/[\n\r\t]+([ ]{4})+</",'<',$str);
		  $str =preg_replace("/>[\n\r\t]+</",'><',$str);
		  $str =preg_replace("/\t/",'&nbsp;&nbsp;&nbsp;&nbsp;',$str);
		  $str =preg_replace("/\r\n/",'<br>',$str);
		 if($code) $str=diconv($str,$code, 'UTF-8'); 
	}
}


function textsaveattach($icoid,$message){
	global $_G,$space;
	if(!$icoarr=DB::fetch_first("select size,icoid,oid,type,uid,name from ".DB::table('dzz_icos')." where icoid='{$icoid}' and type='attach'")){
		return array('error' => dzzlang('message','file_not_exist'));
	}
	if(!$attach=getAttachByMd5($message,$icoarr['name'])){
		return array('error' => dzzlang('message','file_save_failure'));
	}
	//判断用户权限
	if(!$space['self'] ||($icoarr['uid']!=$_G['uid'] && $space['self']<2)){
		return array('error' => dzzlang('message','no_privilege'));
	}
	//计算用户新的空间大小
	$csize=$attach['filesize']-$icoarr['size'];
	//检查用户空间容量
	if($space['maxspacesize']>0 && ($csize+$space['usesize'])>$space['maxspacesize']){
		return array('error' => dzzlang('message','inadequate_capacity_space'));
	}
	//重新计算用户空间；
	if($csize){
		 DB::query("update ".DB::table('dzz_userconfig')." set usesize=usesize+$csize where uid='{$space[uid]}'");
	}
	//更新sourcedata
	  if($aid=DB::result_first("select aid from ".DB::table('dzz_attach')." where qid='{$icoarr[oid]}'")){
		 if($attachold=DB::fetch_first("select * from ".DB::table('dzz_attachment')." where aid='{$aid}'")){
			if($attachold['copys']<=1){
				@unlink($_G['setting']['attachdir'].$attachold['attachment']);
				DB::delete('dzz_attachment',"aid='{$aid}'");
			}else{
				DB::update('dzz_attachment',array('copys'=>$attachold['copys']-1),"aid='{$aid}'");
			}
		 }
	  }
		$sourcedata=array(
					'filesize'=>$attach['filesize'],
					'attachment'=>$attach['attachment'],
					'filetype'=>$attach['filetype'],
					'filename' =>$attach['filename'],
					'remote'=>$attach['remote'],
					'aid'=>$attach['aid']
			);
			
		DB::update('dzz_attach',daddslashes($sourcedata),"qid='{$icoarr[oid]}'");
		DB::query("update ".DB::table('dzz_attachment')." set copys=copys+1 where aid='{$attach[aid]}'");
	//更新icoarr
		$icoarr=array(
					'url'=>$_G['setting']['attachurl'].$attach['attachment'],
					'size'=>$attach['filesize'],
					'ext'=>$attach['filetype'],
				);
	
		DB::update('dzz_icos',daddslashes($icoarr)," icoid='{$icoid}'");
	return $icoarr;
}
		
include dzztemplate('ueditor/editor_txt');

?>