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

//	$dzz_announce = DB::result_first("SELECT svalue FROM ".DB::table('common_setting')." WHERE skey='dzz_announce'");
	$dzz_manyouconfig = dstripslashes(unserialize(stripslashes($_G['setting']['dzz_manyouconfig'])));
	$sysconfig = dstripslashes(unserialize(stripslashes($_G['setting']['dzz_sysconfig'])));
	
if($operation=='update'){
	
	dzz_shownav($lang['menu_manyou_config'],$lang['manyou_update']);
	@set_time_limit(0);
	
	$output_buffering=intval(@ini_get('output_buffering'))?intval(@ini_get('output_buffering')):4096;
	ob_end_flush();//关闭缓存
	echo str_repeat(" ",256); //ie下 需要先发送256个字节
	flush();
	ob_flush();
	echo str_repeat(" ",$output_buffering); 
	flush();
	ob_flush();
	$my_prefix = 'http://uchome.manyou.com';
	$my_suffix = '/app/list?sort=time';
	$my_extra ='';
	
	$delimiter = strrpos($my_suffix, '?') ? '&' : '?';
	
	$myUrl = $my_prefix.urldecode($my_suffix.$delimiter.'my_extra='.$my_extra);
	
	$uchUrl = $_G['siteurl'].'userapp.php?mod=manage';
	//$uchUrl = getsiteurl().'userapp.php?mod=manage';
	$hash = $_G['setting']['my_siteid'].'|'.$_G['uid'].'|'.$_G['setting']['my_sitekey'].'|'.$_G['timestamp'];
	$hash = md5($hash);
	$delimiter = strrpos($myUrl, '?') ? '&' : '?';
	
	$url = $myUrl.$delimiter.'s_id='.$_G['setting']['my_siteid'].'&uch_id='.$_G['uid'].'&uch_url='.urlencode($uchUrl).'&my_suffix='.urlencode($my_suffix).'&timestamp='.$_G['timestamp'].'&my_sign='.$hash;
	//$url='http://uchome.manyou.com/app/list?sort=time&my_extra=&s_id=5116351&uch_id=2786&uch_url=http%3A%2F%2Fdzz.cc%2Fuserapp.php%3Fmod%3Dmanage&my_suffix=%2Fapp%2Flist%3Fsort%3Dtime&timestamp=1314933940&my_sign=bf85ceea1c7a170b4e4d17a1615092d8';
	
	$content=dzz_file_get_contents($url);
	if(!$content) cpmsg($lang['network_busy'],$refer,'error');
	//获取页数
		preg_match("/<div class=\"pager\">(.+?)<\/div>/ims",$content,$pagenum);
		preg_match_all("/<a(.+?)>(.+?)<\/a>/ims",$pagenum[1],$pages);
		$page=1;
		foreach($pages[2] as $value){
			if(intval($value)>$page) $page=intval($value);
		}

	//采集数据
	
	$num=0;
	$total=0;
	$i=0;

	for($i=$page;$i>0;$i--){
		$link=$url.'&page='.$i;
		$content=dzz_file_get_contents($link);
		if(preg_match_all("/<!--app-->(.+?)<!--app over-->/ims",$content,$content1)){
			foreach($content1[1] as $value){
				$total++;
				$data=array();
				if(preg_match("/<img src=\"http:\/\/appicon.manyou.com\/logos\/(\d+)\"/ims",$value,$oid)){
					$data['oid']=$oid[1];
				}else continue;
				if(preg_match("/<div class=\"app_name\"><a(.+?)>(.+?)<\/a><\/div>/ims",$value,$appname)){
					$data['appname']=diconv($appname[2],'UTF-8');
				}else continue;
				if(preg_match("/<div class=\"app_lei\"><a(.+?)>(.+?)<\/a><\/div>/ims",$value,$appclass)){
					$data['classname']=diconv($appclass[2],'UTF-8');
				}else continue;
				if(preg_match("/<div class=\"app_description\">(.+?)<\/div>/ims",$value,$appdesc)){
					$data['appdesc']=diconv($appdesc[1],'UTF-8');
				}else continue;
				//数据入库
				if($data['oid']){
					$data['appurl']='userapp.php?mod=app&id='.$data['oid'];
					$data['appico']='http://appicon.manyou.com/logos/'.$data['oid'];
				}else continue;
								
				if(manyou_app_to_market($data)){
					$num++;
					echo '<span style="float:left;font-size:14px;color:black;width:250px;"><span style="color:green;padding:5px;">'.$data['appname'].'</span>'.$lang['manyou_app_update_success'].'</span>';
					flush();
					ob_flush();
				}
			}
		}
	}
	$dzz_manyouconfig['updateinfo']='<span style="padding:5px;">'.dgmdate($_G['timestamp']).'</span><span style="padding:5px 5px 5px 20px;">'.$lang['manyou_update_sum'].'</span><span style="color:green">'.$num.'</span><span style="padding:5px 5px 5px 20px;">'.$lang['manyou_total_sum'].'</span><span style="color:green">'.$total.'</span>';
	DB::insert('common_setting', array(
			'skey' => 'dzz_manyouconfig',
			'svalue' => addslashes(serialize(dstripslashes($dzz_manyouconfig))),
		), false, true);
		updatecache(array('setting', 'dzz_manyouconfig'));
	cpmsg($lang['manyou_app_update_success'],$refer,'succeed');
		
}else{
		dzz_shownav($lang['menu_manyou_config']);
		if(submitcheck('manyousubmit')) {
			$dzz_manyouconfig['manyou_open']=intval($_POST['manyou_open']);
			DB::insert('common_setting', array(
				'skey' => 'dzz_manyouconfig',
				'svalue' => addslashes(serialize(dstripslashes($dzz_manyouconfig))),
			), false, true);
			updatecache(array('setting', 'dzz_manyouconfig'));

			cpmsg($lang['do_success'], $refer, 'succeed');

		}
}
	
include dzztemplate('admin/dzz_manyou');
dzzfooter();
?>