<?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');
}
$do = empty($_GET['do'])?'':$_GET['do'];
//判断数据唯一

$msg='';
	if(!$space['self']) showmessage(dzzlang('message','no_privilege'),DZZSCRIPT,'',array('showdialog'=>1, 'showmsg' => true, 'closetime' => 3));
	$ukey=trim($_GET['ukey']);
	if($ukey!=DB::result_first("select `ukey` from ".DB::table('dzz_userconfig')." where uid='{$uid}'")){
		showmessage(dzzlang('message','refresh'),DZZSCRIPT,'',array('showdialog'=>1, 'showmsg' => true, 'closetime' => 3));
	}
$refer=dreferer();	
if($do=='upload'){
	// list of valid extensions, ex. array("jpeg", "xml", "bmp")
	require_once dzz_libfile('class/fileupload');
	

	$allowedExtensions = $space['attachextensions']?explode(',',$space['attachextensions']):array();
	// max file size in bytes
	$sizeLimit =intval($space['maxattachsize']);
	
	$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
	$size = $uploader->getFileSize()	;
	if($space['maxspacesize']>0 && ($size+$space['usesize'])>$space['maxspacesize']){
		$result=array('error' => dzzlang('message','inadequate_capacity_space'));
		echo htmlspecialchars(json_encode_gbk($result), ENT_NOQUOTES);
		exit();
	}
	$path=$uploader->getPath();
	
	$result = $uploader->handleUpload($path);
	// to pass data through iframe you will need to encode all html tags
	echo htmlspecialchars(json_encode_gbk($result), ENT_NOQUOTES);
	exit();
}elseif($do=='widget_delete'){
	if(submitcheck('deletesubmit')){
		$gid=intval($_POST['gid']);
		$msg=dzz_delete_widget($gid);
		if($msg=='success'){
			showmessage('do_success',$refer,array('gid'=>$gid),array());
		}else{
			showmessage($msg);
		}
	}
}elseif($do=='widget'){
	if(submitcheck('imagesubmit')){
		$setarr=array('url'=>trim($_POST['url']),
					  'width'=>intval($_POST['width']),
					  'height'=>intval($_POST['height']),
					  'classname'=>trim($_POST['classname']),
					  'href'=>trim($_POST['href']),
					  'open'=>intval($_POST['open']),
					  'type'=>trim($_POST['type']),
					  'uid'=>intval($space['uid']),
					  'username'=>$space['username'],
					  'dateline'=>$_G['timestamp']
					  );
	}elseif(submitcheck('linksubmit')){
		
		$setarr=array('url'=>trim($_POST['url']),
					  'width'=>intval($_POST['width']),
					  'height'=>intval($_POST['height']),
					  'classname'=>trim($_POST['classname']),
					  'href'=>trim($_POST['href']),
					  'open'=>intval($_POST['open']),
					  'type'=>trim($_POST['type']),
					  'uid'=>intval($space['uid']),
					  'username'=>$space['username'],
					  'dateline'=>$_G['timestamp']
					  );
		//通过url查找plugin
		if(preg_match("/mod=plugin:(\w+)&op=/i",$setarr['url'],$matches)){
			$identifier=$matches[1];
			if($plugin=DB::fetch_first("select * from ".DB::table('dzz_plugin')." where identifier='{$identifier}'")){
				$setarr['idtype']='pluginid';
				$setarr['typeid']=$plugin['pluginid'];
			}
		}
	}elseif(submitcheck('flashsubmit')){
		$setarr=array('url'=>trim($_POST['url']),
					  'width'=>intval($_POST['width']),
					  'height'=>intval($_POST['height']),
					  'classname'=>trim($_POST['classname']),
					  'href'=>trim($_POST['href']),
					  'open'=>intval($_POST['open']),
					  'type'=>trim($_POST['type']),
					  'uid'=>intval($space['uid']),
					  'username'=>$space['username'],
					  'dateline'=>$_G['timestamp']
					  );
	}
	$gid=intval($_POST['gid']);
	if($gid){
		if(DB::update('dzz_widget',daddslashes($setarr),"gid='{$gid}'")){
			$setarr['gid']=$gid;
		}
	}else{
		if($gid=DB::insert('dzz_widget',daddslashes($setarr),1)){
			$setarr['gid']=$gid;
		}
	}
	$setarr['url']=replace_canshu($setarr['url']);
	
	showmessage('do_success',$_POST['referer'],($setarr),array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
					
}elseif(submitcheck('newlinksubmit')){
	$link=trim($_GET['link']);
	//$link = dhtmlspecialchars(trim($_POST['link']));

	//检查网址合法性
	if(!preg_match("/^(http|ftp|https|mms)\:\/\/.{4,300}$/i", $link)) showmessage(dzzlang('message','href_illegal'));

	$ext=strtolower(substr(strrchr($link, '.'), 1, 10));
	static $imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
	static $musicext  = array('mp3','wma','ram','ra','mid');
	static $videoext  = array('swf', 'flv');
	static $videohost  = array('tudou.com', 'youku.com','56.com','ku6.com');
	$isimage= in_array($ext, $imgext) ? 1 : 0;
	$ismusic= in_array($ext, $musicext) ? 1 : 0;
	$container=trim($_GET['container']);

	//是图片时处理
	if($isimage){
		if($data=linktoimage($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']) showmessage($data['error']);
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}
	}elseif($ismusic){
		if($data=linktomusic($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']) showmessage($data['error']);
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}
	}else{
		//试图作为视频处理
		
		if($data=linktovideo($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']) showmessage($data['error']);
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}
		//作为网址处理

		if($data=linktourl($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']) showmessage($data['error']);
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}else{
			showmessage(dzzlang('message','network_error'));
		}
	}
}elseif($do=='newlink'){
	$link=trim($_GET['link']);
	//$link = dhtmlspecialchars(trim($_POST['link']));

	//检查网址合法性
	if(!preg_match("/^(http|ftp|https|mms)\:\/\/.{4,300}$/i", $link)){
		     include template('common/header_ajax');
				echo "<script type=\"text/javascript\" reload=\"1\">";
				echo "showPrompt(null,'','".dzzlang('message','href_illegal')."',1000);";;
				echo "</script>";
			include template('common/footer_ajax');
			exit();
	}

	$ext=strtolower(substr(strrchr($link, '.'), 1, 10));
	static $imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
	static $musicext  = array('mp3', 'wma','mid');
	static $videoext  = array('swf', 'flv');
	static $videohost  = array('tudou.com', 'youku.com','56.com','ku6.com');
	$isimage= in_array($ext, $imgext) ? 1 : 0;
	$ismusic= in_array($ext, $musicext) ? 1 : 0;
	$container=trim($_GET['container']);

	//是图片时处理
	if($isimage){
		if($data=linktoimage($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']){
				 include template('common/header_ajax');
					echo "<script type=\"text/javascript\">";
					echo "showPrompt(null,'','".$data['error']."',1000);";;
					echo "</script>";
				include template('common/footer_ajax');
			}else{
				include template('common/header_ajax');
					$data=json_encode_gbk($data);
					echo "<script type=\"text/javascript\">";
					echo "_ico.createIco(".$data.");";
					echo "</script>";
				include template('common/footer_ajax');
			}
			exit();
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}
	}elseif($ismusic){
		if($data=linktomusic($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']){
				 include template('common/header_ajax');
					$data=json_encode_gbk($data);
					echo "<script type=\"text/javascript\">";
					echo "showPrompt(null,'','".$data['error']."',1000);";
					echo "</script>";
				include template('common/footer_ajax');
			}else{
				include template('common/header_ajax');
					$data=json_encode_gbk($data);
					echo "<script type=\"text/javascript\">";
					echo "_ico.createIco(".$data.");";
					echo "</script>";
				include template('common/footer_ajax');
			}
			exit();
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}
	}else{
		//试图作为视频处理
		
		if($data=linktovideo($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']){
				 include template('common/header_ajax');
					echo "<script type=\"text/javascript\">";
					echo "showPrompt(null,'','".$data['error']."',1000);";
					echo "</script>";
				include template('common/footer_ajax');
			}else{
				include template('common/header_ajax');
					$data=json_encode_gbk($data);
					echo "<script type=\"text/javascript\">";
					echo "_ico.createIco(".$data.");";
					echo "</script>";
				include template('common/footer_ajax');
			}
			exit();
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}
		//作为网址处理

		if($data=linktourl($link,$uid,intval($_GET['friend']),$container)){
			if($data['error']){
				 include template('common/header_ajax');
					echo "<script type=\"text/javascript\">";
					echo "showPrompt(null,'','".$data['error']."',1000);";
					echo "</script>";
				include template('common/footer_ajax');
			}else{
				include template('common/header_ajax');
					$data=json_encode_gbk($data);
					echo "<script type=\"text/javascript\">";
					echo "_ico.createIco(".$data.");";
					echo "</script>";
				include template('common/footer_ajax');
			}
			exit();
			
			showmessage('do_success',$refer.'',$data,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
		}else{
			include template('common/header_ajax');
				$data=json_encode_gbk($data);
				echo "<script type=\"text/javascript\" reload=\"1\">";
				echo "showPrompt(null,'','".dzzlang('message','network_error')."',1000);";
				echo "</script>";
			include template('common/footer_ajax');
			exit();
			
		}
	}
	
}elseif(submitcheck('editsubmit')){
	$type=$_GET['type'];
	$icoid=intval($_GET['icoid']);
	switch($type){
		case 'folder':
			$icoarr=DB::fetch_first("select * from ".DB::table('dzz_icos')." where icoid='{$icoid}'");
			$folder=array(
						'fname'=>getstr(($_POST['name']), 80),
						'ficon'=>$_GET['img'],
						'friend'=>intval($_GET['friend']),
						);
			DB::update('dzz_folder',daddslashes($folder)," fid='{$icoarr[oid]}'");
				$setarr=array(
							'name'=>$folder['fname'],
							'img'=>$_GET['img'],
							'friend'=>intval($_GET['friend']),
							);
				
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$setarr['name'];
					$icoarr['img']=$setarr['img'];
					$icoarr['friend']=$setarr['friend'];
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
			
			break;
		case 'image':
			$icoarr=DB::fetch_first("select * from ".DB::table('dzz_icos')." where icoid='{$icoid}'");
			$image=array(
						'title'=>getstr(($_POST['name']), 80),
						'friend'=>intval($_GET['friend']),
						);
			DB::update('dzz_image',daddslashes($image)," picid='{$icoarr[oid]}'");
				$setarr=array(
							'name'=>$image['title'],
							'friend'=>intval($_GET['friend']),
							);
				
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$setarr['name'];
					$icoarr['friend']=intval($_GET['friend']);
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
			
			
			break;
		case 'video':
			$icoarr=DB::fetch_first("select * from ".DB::table('dzz_icos')." where icoid='{$icoid}'");
			$video=array(
						'title'=>getstr(($_POST['name']), 80),
						'friend'=>intval($_GET['friend']),
						);
			DB::update('dzz_video',daddslashes($video)," vid='{$icoarr[oid]}'");
				$setarr=array(
							'name'=>$video['title'],
							'friend'=>intval($_GET['friend']),
							);
				
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$setarr['name'];
					$icoarr['friend']=intval($_GET['friend']);
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
			
			
			break;
		case 'music':
			$icoarr=DB::fetch_first("select * from ".DB::table('dzz_icos')." where icoid='{$icoid}'");
			$music=array(
						'title'=>getstr(($_POST['name']), 80),
						'friend'=>intval($_GET['friend']),
						);
			DB::update('dzz_music',daddslashes($music)," mid='{$icoarr[oid]}'");
				$setarr=array(
							'name'=>$music['title'],
							'friend'=>intval($_GET['friend']),
							);
				
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$setarr['name'];
					$icoarr['friend']=intval($_GET['friend']);
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
						
			break;
		case 'attach':
			$icoarr=DB::fetch_first("select * from ".DB::table('dzz_icos')." where icoid='{$icoid}'");
			$attach=array(
						'filename'=>getstr(($_POST['name']), 80),
						'friend'=>intval($_GET['friend']),
						);
			DB::update('dzz_attach',daddslashes($attach)," qid='{$icoarr[oid]}'");
				$setarr=array(
							'name'=>$attach['filename'],
							'friend'=>intval($_GET['friend']),
							);
				
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$setarr['name'];
					$icoarr['friend']=intval($_GET['friend']);
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
			
			break;
		case 'app':
			$icoarr=DB::fetch_first("select * from ".DB::table('dzz_icos')." where icoid='{$icoid}'");
			$ismyapp=intval($_GET['ismyapp']);
			
			if($ismyapp && $apparr=DB::fetch_first("select appid,appico,uid from ".DB::table('dzz_apps')." where appid='{$icoarr[oid]}' and uid='{$icoarr[uid]}'")){
					if($apparr['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $apparr['appico'])){
						$apparr['appico1']=$_G['setting']['attachurl'].$apparr['appico'];
					}else{
						$apparr['appico1']=$apparr['appico'];
					}
				$app=array(
						'appname'=>getstr($_POST['name'], 80),
						'appdesc'=>getstr(trim($_POST['appdesc'])),
						'classid'=>intval($_POST['classid']),
						'appurl'=>trim($_POST['url']),
						'open'=>intval($_POST['open']),
						'width'=>intval($_POST['width']),
						'height'=>intval($_POST['height']),
						);
				//设置应用图标
				if($apparr['appico1']!=$_POST['img']){
					$appico=imagetolocal($_POST['img'], 'appimg',$apparr['appico']);
				}
				if($appico) $app['appico']=$appico;
				
				DB::update('dzz_apps',daddslashes($app),"appid='{$apparr[appid]}' and uid='{$appico[uid]}'");
			}
				$setarr=array(
							'name'=>getstr($_POST['name'], 80),
							'img'=>$_GET['img'],
							'friend'=>intval($_GET['friend']),
							'open'=>intval($_POST['open']),
							'wwidth'=>intval($_POST['width']),
							'wheight'=>intval($_POST['height']),
							);
				if($ismyapp) {
					$setarr['url']=trim($_POST['url']);
					$icoarr['url']=$setarr['url'];
				}
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$_GET['name'];
					$icoarr['img']=$_GET['img'];
					$icoarr['friend']=intval($_GET['friend']);
					$icoarr['wwidth']=intval($_GET['width']);
					$icoarr['wheight']=intval($_GET['height']);
					$icoarr['open']=intval($_GET['open']);
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
			break;
		case 'link':
			$icoarr=DB::fetch_first("select ico.*,link.did from ".DB::table('dzz_icos')." ico
													  LEFT JOIN ".DB::table('dzz_link')." link ON ico.oid=link.lid  
			 						where icoid='{$icoid}'");
			$parseurl=parse_url($icoarr['url']);
			$host=$parseurl['host'];
			$host=preg_replace("/^www./",'',$host);
			//设置用户特征库
			$relationship=intval($_POST['relationship']);
			if($relationship && $icoarr['img']!==$_POST['img']){ //用户更换了图标且 设置了图标网址关联
				$usericon=array(
								 'uid'=>$space['uid'],
								 'domain'=>$host,
								 'pic'=>$_POST['img'],
								 'dateline'=>$_G['timestamp'],
								 'pdid'=>intval($_POST['did']),
								 );
				$did=DB::result(DB::query("SELECT did FROM ".DB::table('dzz_usericon')." where domain='{$host}' AND uid='{$space[uid]}'"));
				if($did){
					DB::update('dzz_usericon',daddslashes($usericon),"did='{$did}'");
				}else{
					DB::insert('dzz_usericon',daddslashes($usericon));
				}
			}
			$link=array(
						'title'=>getstr(($_POST['name']), 80),
						'img'=>$_POST['img'],
						'friend'=>intval($_POST['friend']),
						'did'=>intval($_POST['did']),
						);
			DB::update('dzz_link',daddslashes($link)," lid='{$icoarr[oid]}'");
				$setarr=array(
					 'img'=>$_POST['img'],
					 'wwidth'=>intval($_POST['width']),
					 'wheight'=>intval($_POST['height']),
					 'open'=>intval($_POST['open']),
					 'name'=>$link['title'],
					 'friend'=>intval($_POST['friend'])
				);
				if(DB::update('dzz_icos',daddslashes($setarr)," icoid='{$icoid}'")){
					$icoarr['name']=$link['title'];
					$icoarr['img']=$link['img'];
					$icoarr['friend']=$link['friend'];
					$icoarr['wwidth']=intval($_POST['width']);
					$icoarr['wheight']=intval($_POST['height']);
					$icoarr['open']=intval($_POST['open']);
					
					//更新特征库使用量
					if($icoarr['did'] && $icoarr['did']!=$link['did']){
							if($icon=DB::fetch_first("select * from ".DB::table('dzz_icon')." where did='{$icoarr[did]}'")){
								if($icon['check']<2 && $icon['copys']<2){
									@unlink(DISCUZ_ROOT.'./'.$icon['pic']);
									DB::delete('dzz_icon',"did='{$icoarr[did]}'");
								}else{
									DB::update('dzz_icon',array('copys'=>$icon['copys']-1),"did='{$icoarr[did]}'");
								}
							}
					}
					if($link['did'] && $icoarr['did']!=$link['did']) DB::query("update ".DB::table('dzz_icon')." SET copys=copys+1 where did='{$link[did]}'");
					showmessage('do_success',$refer.'',$icoarr,array('showdialog'=>1, 'showmsg' => true, 'closetime' => 1));
				}
		
			break;
	}
	include template('common/header_ajax');
		echo "<script type=\"text/javascript\" reload=\"1\">";
		echo "hideWindow('".$_GET['handlekey']."');";
		echo "</script>";
	include template('common/footer_ajax');
	exit();
}

?>