<?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'];
if($do=='updatesearch'){
	$keyword=urldecode(trim($_GET['keyword']));
	if(DB::result_first("select COUNT(*) from ".DB::table('dzz_apps')." where ismarket>0 and ischeck=3 and  appname LIKE '%".$keyword."%'")){
		if($srid=DB::result_first("select srid from ".DB::table('dzz_search')." where keyword='{$keyword}' LIMIT 1")){
			DB::query("update ".DB::table('dzz_search')." set sum=sum+1 ,dateline=".$_G['timestamp']." where srid='{$srid}'");
		}else{
			DB::insert('dzz_search',array('keyword'=>$keyword,'dateline'=>$_G['timestamp'],'sum'=>1));
		}
	}
}elseif($do=='comment'){
	$appid=intval($_GET['appid']);
	$app=getapp($appid);
}elseif($do=='updateopen'){
	$appid=intval($_GET['appid']);
	DB::query("update ".DB::table('dzz_apps')." set hot=hot+1,opennum=opennum+1 where appid='{$appid}'");
	if($_G['uid'])	DB::insert("dzz_userdo",array('type'=>'open','appid'=>$appid,'uid'=>$_G['uid'],'username'=>$_G['username'],'dateline'=>$_G['timestamp']));
}elseif($do=='updateview'){
	$appid=intval($_GET['appid']);
	if($_G['coockie']['view_app_'.$appid]){
		exit();
	}else{
		DB::query("update ".DB::table('dzz_apps')." set hot=hot+1,viewnum=viewnum+1 where appid='{$appid}'");
		dsetcookie('view_app_'.$appid,'1',86400);
	}
	//if($_G['uid'])	DB::insert("dzz_userdo",array('type'=>'view','appid'=>$appid,'uid'=>$_G['uid'],'username'=>$_G['username'],'dateline'=>$_G['timestamp']));
}elseif($do == 'imagelist') {
	$attachlist = x_getattach($_GET['pid'], intval($_GET['posttime']));
	$imagelist = $attachlist['imgattachs']['unused'];
	include template('common/header_ajax');
	include template('forum/dzz_ajax_imagelist');
	include template('common/footer_ajax');
	dexit();
}elseif($do=='addapp'){
	$arr=array();
	$container=$_GET['container'];
	$appid=$_GET['appid'];
	
	if(!$space['self'] ||(strpos($container,'icosContainer_body_sys_')!==false && $space['self']<2)){
		$arr['msg']=dzzlang('message','no_privilege');
		echo json_encode_gbk($arr);
		exit();
	}
	
	if($app=dstripslashes(DB::fetch_first("select * from ".DB::table('dzz_apps')." where appid='{$appid}'"))){
		if($app['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $app['appico'])){
			$app['appico']=$_G['setting']['attachurl'].$app['appico'];
		}
		$icoarr=array(
							'uid'=>$space['uid'],
							'username'=>$space['username'],
							'oid'=>$app['appid'],
							'name'=>$app['appname'],
							'url'=>$app['appurl'],
							'img'=>$app['appico'],
							'wwidth'=>$app['width'],
							'wheight'=>$app['height'],
							'open'=>$app['open'],
							'haveflash'=>$app['haveflash'],
							'idtype'=>$app['idtype'],
							'typeid'=>$app['typeid'],
							'havetask'=>$app['havetask'],
							'isshow'=>$app['isshow'],
							'titlebuttons'=>$app['titlebuttons'],
							'type'=>'app',
							'size'=>0,
							'ext'=>'',
							'friend'=>0,
							'dateline'=>$_G['timestamp'],
							
			);
		if($icoarr['icoid']=DB::insert('dzz_icos',daddslashes($icoarr),1)){
			$icoarr['container']=$container;
			$icoarr['msg']='do_success';
			
			if(addtoconfig($icoarr['icoid'],$container)){
				$icoarr['url']=replace_canshu($icoarr['url']);
				$icoarr['fsize']=formatsize($icoarr['size']);
				$icoarr['ftype']=getFileTypeName($icoarr['type'],$icoarr['ext']);
				$icoarr['fdateline']=dgmdate($icoarr['dateline']);
				echo json_encode_gbk($icoarr);
				DB::query("update ".DB::table('dzz_apps')." set hot=hot+3,setupnum=setupnum+1 where appid='{$appid}'");
				if($_G['uid'])	DB::insert("dzz_userdo",array('type'=>'setup','appid'=>$appid,'uid'=>$_G['uid'],'username'=>$_G['username'],'dateline'=>$_G['timestamp']));
			}else{
				dzz_delete_ico($icoarr['icoid']);
				$arr['msg']='target error!';
				echo json_encode_gbk($arr);
			}
			exit();
		}else{
			$arr['msg']='unkown_error';
			echo json_encode_gbk($arr);
			exit();
		}
	}else{
		$arr['msg']='app not exist';
			echo json_encode_gbk($arr);
			exit();
	}
	

}elseif($do=='search_region_rb'){ //热搜榜
	$list=array();
	$query=DB::query("select * from ".DB::table('dzz_search')." where 1 order by sum DESC LIMIT 10");
	while($value=dstripslashes(DB::fetch($query))){
		$list[]=$value;
	}
}elseif($do=='search_region_rt'){ //热搜榜
	$list=array();
	$query=DB::query("select * from ".DB::table('dzz_search')." where 1 order by dateline DESC LIMIT 10");
	while($value=dstripslashes(DB::fetch($query))){
		$list[]=$value;
	}
}elseif($do=='search_region_l'){ //搜索
$myicos=getmyappid();
	$keyword=urldecode(trim($_GET['keyword']));
	$page = empty($_GET['page'])?1:intval($_GET['page']);
	$perpage = 12;
	$start = ($page-1)*$perpage;
	$gets = array(
			'mod'=>'market',
			'op'=>'ajax',
			'do'=>'search_region_l',
			'keyword'=>$keyword
			);
	$theurl = DZZSCRIPT."?".url_implode($gets);
	$list=array();
	$wheresql="ismarket>0 and ischeck=3 and appname LIKE '%".$keyword."%'";
	$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_apps')." where $wheresql  ");
	if($count){
		$query=DB::query("SELECT * FROM ".DB::table('dzz_apps')." where $wheresql order by LOCATE('$keyword',appname)   LIMIT $start,$perpage" );
		while($values=dstripslashes(DB::fetch($query))){
			if($values['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $values['appico'])){
						$values['appico']=$_G['setting']['attachurl'].$values['appico'];
			}
			$values['sappname']= str_replace($keyword, '<span class="height_light1">'.$keyword.'</span>', $values['appname']);
			$values['appdesc_short']=getstr($values['appdesc'],60,0,0-1,-1);
			$list[]=$values;
		}
	}
	$realpages = @ceil($count / $perpage);
	$multi = multi($count, $perpage, $page, $theurl,0,6);
}elseif($do=='autosearch'){ //搜索自动完成

	$keyword=diconv(urldecode(trim($_GET['keyword'])),'UTF-8');
	$wheresql="ismarket>0 and ischeck=3 and appname LIKE '%".$keyword."%'";
	$list=array();
	$query=DB::query("select appname from ".DB::table('dzz_apps')." where $wheresql order by LOCATE('$keyword',appname) LIMIT 10");
	while($value=dstripslashes(DB::fetch($query))){
		$value['sappname']= str_replace($keyword, '<span class="height_light1">'.$keyword.'</span>', $value['appname']);
		$list[]=$value;
	}	
}elseif($do=='main_region_lt'){ //幻灯片
	$list=array();
	$query=DB::query("select r.*,a.appname,a.classid from ".DB::table('dzz_recomment')." r
						LEFT JOIN ".DB::table('dzz_apps')." a ON r.appid=a.appid 
						where r.available>0 and r.type=0 order by r.disp");
	while($value=dstripslashes(DB::fetch($query))){
		$list[]=$value;
	}	
}elseif($do=='main_region_lm'){	//最新应用top50
$myicos=getmyappid();
	$list=array();
	
	$query=DB::query("select p.*,c.classname from ".DB::table('dzz_apps')." p 
						LEFT JOIN ".DB::table('dzz_appclass')." c ON p.classid=c.classid 
						where p.ismarket>0 and ischeck=3 ORDER BY p.dateline DESC LIMIT 10");
						
	while($value=dstripslashes(DB::fetch($query))){
		$value['appdesc']=getstr($value['appdesc'],60,0,0-1,-1);
		if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
		$list[$value['appid']]=$value;
	}
	$maxwidth=count($list)*90;
}elseif($do=='main_region_lm2'){	//一周精选应用top10
$myicos=getmyappid();
	$list=array();
	$weektime=$_G['timestamp']-7*24*60*60;
	$appids=array();
	$query=DB::query("select appid from ".DB::table('dzz_userdo')." where type='setup' and dateline>$weektime");
	while($value=DB::fetch($query)){
		$appids[$value['appid']]=$value['appid'];
	}
	if($appids){
		$query=DB::query("select p.*,c.classname from ".DB::table('dzz_apps')." p 
							LEFT JOIN ".DB::table('dzz_appclass')." c ON p.classid=c.classid 
							where p.appid IN (".dimplode($appids).") ORDER BY p.setupnum DESC LIMIT 10");
		while($value=dstripslashes(DB::fetch($query))){
			$value['appdesc']=getstr($value['appdesc'],60,0,0-1,-1);
			if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
			$list[$value['appid']]=$value;
		}
	}
	$maxwidth=(ceil(count($list)/2))*60;
}elseif($do=='jinqizhuda'){	//每周排行盒子

	$list=array();
	$query=DB::query("select r.*,a.appname,a.classid from ".DB::table('dzz_recomment')." r
						LEFT JOIN ".DB::table('dzz_apps')." a ON r.appid=a.appid 
						where r.available>0 and r.type=1 order by r.disp LIMIT 10");
	while($value=dstripslashes(DB::fetch($query))){
		$list[]=$value;
	}
}elseif($do=='main_region_rm'){	//每周排行盒子

}elseif($do=='mainWeekRank_weekRankBox'){	//每周排行最多评分盒子
$myicos=getmyappid();
	$list=array();
	$page=empty($_GET['page'])?1:intval($_GET['page']);
	$perpage = 15;
	$start = ($page-1)*$perpage;
	
	$weektime=$_G['timestamp']-7*24*60*60;
	$appids=array();
	$query=DB::query("select appid from ".DB::table('dzz_userdo')." where dateline>$weektime");
	while($value=DB::fetch($query)){
		$appids[$value['appid']]=$value['appid'];
	}
	if($appids){
		$query=DB::query("select * from ".DB::table('dzz_apps')." where ismarket>0 and ischeck=3 and appid IN (".dimplode($appids).") ORDER BY starnum DESC LIMIT $start,$perpage");
		$i=0;
		while($value=dstripslashes(DB::fetch($query))){
			$i++;
			if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
			$value['index']=$i+($perpage*($page-1));
			$list[$value['appid']]=$value;
		}
	}
	$total=ceil(count($appids)/$perpage);
}elseif($do=='mainWeekRank_markRankBox'){	//每周排行最多评论盒子
$myicos=getmyappid();
	$list=array();
	$page=empty($_GET['page'])?1:intval($_GET['page']);
	$perpage = 15;
	$start = ($page-1)*$perpage;
	
	$weektime=$_G['timestamp']-7*24*60*60;
	$appids=array();
	$query=DB::query("select appid from ".DB::table('dzz_userdo')." where dateline>$weektime");
	while($value=DB::fetch($query)){
		$appids[$value['appid']]=$value['appid'];
	}
	if($appids){
		$query=DB::query("select * from ".DB::table('dzz_apps')." where ismarket>0 and ischeck=3 and appid IN (".dimplode($appids).") ORDER BY replynum DESC LIMIT $start,$perpage");
		$i=0;
		while($value=dstripslashes(DB::fetch($query))){
			$i++;
			if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
			$value['index']=$i+($perpage*($page-1));
			$list[$value['appid']]=$value;
		}
	}
	$total=ceil(count($appids)/$perpage);
}elseif($do=='allWeekRank_weekRankBox'){	//每周排行最多评分盒子
$myicos=getmyappid();
	$list=array();
	$page=empty($_GET['page'])?1:intval($_GET['page']);
	$perpage = 15;
	$start = ($page-1)*$perpage;
	
	$weektime=$_G['timestamp']-7*24*60*60;
	$appids=array();
	$query=DB::query("select appid from ".DB::table('dzz_userdo')." where dateline>$weektime");
	while($value=DB::fetch($query)){
		$appids[$value['appid']]=$value['appid'];
	}
	if($appids){
		$query=DB::query("select * from ".DB::table('dzz_apps')." where ismarket>0 and ischeck=3 and appid IN (".dimplode($appids).") ORDER BY starnum DESC LIMIT $start,$perpage");
		$i=0;
		while($value=dstripslashes(DB::fetch($query))){
			$i++;
			if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
			$value['index']=$i+($perpage*($page-1));
			$list[$value['appid']]=$value;
		}
	}
	$total=ceil(count($appids)/$perpage);
	
}elseif($do=='allWeekRank_markRankBox'){	//每周排行最多评论盒子
	$myicos=getmyappid();
	$list=array();
	$page=empty($_GET['page'])?1:intval($_GET['page']);
	$perpage = 15;
	$start = ($page-1)*$perpage;
	
	$weektime=$_G['timestamp']-7*24*60*60;
	$appids=array();
	$query=DB::query("select appid from ".DB::table('dzz_userdo')." where dateline>$weektime");
	while($value=DB::fetch($query)){
		$appids[$value['appid']]=$value['appid'];
	}
	if($appids){
		$query=DB::query("select * from ".DB::table('dzz_apps')." where ismarket>0 and ischeck=3 and appid IN (".dimplode($appids).") ORDER BY replynum DESC LIMIT $start,$perpage");
		$i=0;
		while($value=dstripslashes(DB::fetch($query))){
			$i++;
			if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
			$value['index']=$i+($perpage*($page-1));
			$list[$value['appid']]=$value;
		}
	}
	$total=ceil(count($appids)/$perpage);
}elseif($do=='appPanel_all_list_body'){
	$myicos=getmyappid();
	//获取所有分类
	$appclass=array();
	$query=DB::query("select * from ".DB::table('dzz_appclass')." where 1 order by disp");
	while($value=DB::fetch($query)){
		$appclass[$value['fupid']][]=$value;
	}

	$page = empty($_GET['page'])?1:intval($_GET['page']);
	$cid=intval($_GET['cid']);
	$sid=intval($_GET['sid']);
	$sort=intval($_GET['sort']);
	$perpage = 12;
	$start = ($page-1)*$perpage;
	$gets = array(
			'mod'=>'market',
			'op'=>'ajax',
			'do'=>'appPanel_all_list_body',
			'cid' => $cid,
			'sid'=>$sid,
			'sort'=>$sort,
			);
	
	$theurl = DZZSCRIPT."?".url_implode($gets);
	$wheresql='ismarket>0 and ischeck=3';
	if($cid>0) $wheresql.=" and classid='{$cid}'";
	if($sid){
		$appids=array(); 
		$query=DB::query("select appid from ".DB::table('dzz_relative')." where classid='{$sid}'");
		while($value=DB::fetch($query)){
			$appids[]=$value['appid'];
		}
		if($appids)	$wheresql.=" and appid IN (".dimplode($appids).")";
		else $wheresql.=" and appid IN ('0')";
	}
	if($sort==0) $ordersql=" ORDER BY `hot` DESC";
	elseif($sort==1) $ordersql=" ORDER BY `dateline` DESC";
	elseif($sort==2) $ordersql=" ORDER BY `star` DESC";
	$list=array();
	
	$count=DB::result_first("SELECT COUNT(*) FROM ".DB::table('dzz_apps')." where $wheresql  ");
	if($count){
		$query=DB::query("SELECT * FROM ".DB::table('dzz_apps')." where $wheresql $ordersql   LIMIT $start,$perpage" );
		while($values=dstripslashes(DB::fetch($query))){
			if($values['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $values['appico'])){
						$values['appico']=$_G['setting']['attachurl'].$values['appico'];
			}
			$values['appdesc_short']=getstr($values['appdesc'],60,0,0-1,-1);
			if($values['dateline']>$_G['timestamp']-60*60*24) $values['isnew']=1;
			$list[]=$values;
		}
	}
	$realpages = @ceil($count / $perpage);
	$multi = multi($count, $perpage, $page, $theurl,0,6);
}elseif($do=='introduce_region_rt'){//同类应用

	$list=array();
	$cid=intval($_GET['cid']);
	$appid=intval($_GET['appid']);
	$query=DB::query("select appname,appurl,appico,appid,hot,classid from ".DB::table('dzz_apps')." where ismarket>0  and ischeck=3 and classid='{$cid}' and appid!='{$appid}' ORDER BY dateline DESC LIMIT 10");
	while($value=dstripslashes(DB::fetch($query))){
		if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
		}
		$list[]=$value;
	}
}elseif($do=='introduce_region_rb'){//大家喜欢
$appids=array();
	$query=DB::query("select appid from ".DB::table('dzz_userdo')." where 1 order by dateline DESC LIMIT 30");
	while($value=DB::fetch($query)){
		$appids[$value['appid']]=$value['appid'];
	}
	if($appids){
		$list=array();
		$query=DB::query("select appname,appurl,appico,appid,setupnum,classid from ".DB::table('dzz_apps')." where appid IN (".dimplode($appids).")");
		while($value=dstripslashes(DB::fetch($query))){
			if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
			$list[]=$value;
		}
		$count=count($list);
	}
}elseif($do=='introduce_region_lt'){
	$myicos=getmyappid();
	$appid=intval($_GET['appid']);
	$appclass=array();
		$query=DB::query("select * from ".DB::table('dzz_appclass')." where 1");
		while($value=dstripslashes(DB::fetch($query))){
			$appclass[$value['classid']]=$value;
		}
	//获取应用信息
	$app=array();
	$app=dstripslashes(getapp($appid));
	if(!$app) showmessage($lang['app_not_exist']);
	if($value['appico']!='dzz/images/default/icodefault.png' && !preg_match("/^(http|ftp|https|mms)\:\/\/(.+?)/i", $value['appico'])){
						$value['appico']=$_G['setting']['attachurl'].$value['appico'];
			}
	$app['appurl']=addslashes($app['appurl']);
	
	$app['classname']=$appclass[$app['classid']]['classname'];
	if(!$pingjia=DB::fetch_first("SELECT * FROM ".DB::table('dzz_score')." where id='{$appid}' and idtype='appid'  and uid='{$_G[uid]}'  LIMIT 1" )){
		$pingjia=array('star'=>0);	
	}
	
	/*//获取我的应用
		
	$myapps=array();
	if($_G['uid'] && $setuplist=DB::result_first("select setuplist from ".DB::table('dzz_userconfig')." where uid='{$_G[uid]}'")){
		$myapps=explode(',',$setuplist);
	}else{
		$query=DB::query("select appid from ".DB::table("dzz_apps")." where `default`!=0");
		while($value=DB::fetch($query)){
			$myapps[]=$value['appid'];
		}
	}
	if(in_array($app['appid'],$myapps)) $app['isadd']=1;*/
	//获取所有分类
	 

		$images=array();
		$imageids=array();
		$wheresql = "id='{$app[appid]}'";
		
		//showmessage("SELECT * FROM ".DB::table('dzz_pic')." where id='{$app[appid]}' and `checked`>0 ORDER BY viewnum DESC ");
		$query=DB::query("SELECT * FROM ".DB::table('dzz_pic')." where $wheresql ORDER BY viewnum DESC " );
				while($value=DB::fetch($query)){
					$value['pic']=pic_get($value['filepath'], '', $value['thumb'], $value['remote'], 1,1);
					$value['filepath']=pic_get($value['filepath'], '', $value['thumb'], $value['remote'], 0,1);
					$images[]=$value;
					$imageids[]=$value['picid'];
					
				}
	 $imagesum=count($images);
}elseif($do=='deleteattach'){
	$count = 0;
	if($_GET['aids']) {
		foreach($_GET['aids'] as $aid) {
			$attach = DB::fetch_first("SELECT * FROM ".DB::table('dzz_attachment')." WHERE aid='$aid'");
			if($attach &&  $_G['uid'] == $attach['uid']) {
				DB::delete('dzz_attachment', "aid='$aid'");
				require_once libfile('function/forum');
				dunlink($attach);
				$count++;
			}
		}
	}
	include template('common/header_ajax');
	echo $count;
	include template('common/footer_ajax');
	dexit();
}
include dzztemplate('market/market_ajax');

?>