<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: portal_list.php 17431 2010-10-19 03:35:51Z zhangguosheng $
 */


if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
$do=$_GET['do'];
$url=(trim($_GET['url'],'/'));

if($do=='geturlinfo'){
	set_time_limit(60);
	$imgencode=md5($url).'.png';
	$subdir=substr($imgencode,0,2);
	$target=$_G['setting']['attachdir'].'appico/'.$subdir.'/'.$imgencode;
	$target1=$_G['setting']['attachurl'].'appico/'.$subdir.'/'.$imgencode;
	$arr=array();
	
		$chararr=array('GBK','GB2312','UTF-8','BIG5','iso-8859-1');
		$str = dzz_file_get_contents($url);
		$arr=array();
		if(	preg_match("/charset=(.*?)\"/i",$str,$matches2)){
			$arr['charset']=$matches2[1];
		}elseif(function_exists('mb_convert_encoding')) {
			if($char=mb_detect_encoding($str,$chararr)){
				$arr['charset']=$char;
			}else{
				$arr['charset']='';
			}
		}
		
		if(preg_match("/<title>(.*?)<\/title>/ims",$str, $matches1)){
			$arr['appname']=getstr($matches1[1],80);
			if($arr['charset']) $arr['appname']=diconv($arr['appname'],$arr['charset'],'UTF-8');
		}
		if(preg_match("/<meta\s+name=\"*description\"*\s+content=\"(.*?)\"/i",$str,$desc)){
			$arr['appdesc']=$desc[1];
			if($arr['charset']) $arr['appdesc']=diconv($arr['appdesc'],$arr['charset'],'UTF-8');
		}
		
		//判断图片是否存在
		//echo $target.$target1;
		//echo is_file($target);
		if(is_file($target) && $imginfo = @getimagesize($target)){
			if($imginfo && $imginfo[0]<30 && $imginfo[1]<30){
				$arr['isico']=max($imginfo[0],$imginfo[1]);
				$arr['appico']=$target1;
			}else{
				$arr['isico']=0;
				$arr['appico']=$target1;
			}
		}
		if(!$arr['appico']){
			$hostarr=parse_url ($url);
			if(substr($hostarr['host'],0,4)=='www.') $hostarr['host_not_www']=substr($hostarr['host'],4);
			else $hostarr['host_not_www']=$hostarr['host'];
			$ico=$hostarr['scheme'].'://'.$hostarr['host'].'/favicon.ico';
			$ico_not_www=$hostarr['scheme'].'://www.'.$hostarr['host_not_www'].'/favicon.ico';
			$arr['isico']=1;
			if(check_remote_file_exists($ico)) $arr['appico']=$ico;
			elseif(check_remote_file_exists($ico_not_www)) $arr['appico']=$ico_not_www;
			else $arr['isico']=0;
		}
		if(!$arr['appico']){
			 $arr['isico']=0;
			 $arr['appico']='dzz/images/default/icodefault.png';
		}
	echo json_encode_gbk($arr);
	exit;
}else{
	if(submitcheck('addappsubmit')) {
		$msg='';
		$setarr=array(
						'appname'=>getstr($_POST['appname'],80,1,0,0,-1),
						'appurl'=>(trim($_POST['appurl'],'/')),
						'classid'=>intval($_POST['classid']),
						'appdesc'=>getstr(trim($_POST['appdesc']),0,1,0,0,0),
						'open'=>intval($_POST['open']),
						'uid'=>$_G['uid'],
						'username'=>$_G['username'],
						'dateline'=>$_G['timestamp']
						);
		if(!$setarr['appname']) $msg=$lang['msg_no_appname'].'<br>';
		if(!$setarr['appurl'] || $setarr['appurl']=='http://') $msg.=$lang['msg_no_appurl'].'<br>';
		if(!$setarr['uid']) $msg.=$lang['msg_no_login']."<br>";
		if($msg){
			showmessage($msg,DZZSCRIPT,array('success'=>0,'message'=>$msg));
		}else{
			$imgencode=md5($setarr['appurl']).'.png';
			$subdir=substr($imgencode,0,2);
			$target=$_G['setting']['attachdir'].'appico/'.$subdir.'/'.$imgencode;
			$target1=$_G['setting']['attachurl'].'appico/'.$subdir.'/'.$imgencode;
			$source='';
			if(!is_file($target) || !$imginfo = @getimagesize($target)){
				$hostarr=parse_url($setarr['appurl']);
				if($hostarr['scheme']){
					if(substr($hostarr['host'],0,4)=='www.') $hostarr['host_not_www']=substr($hostarr['host'],4);
					else $hostarr['host_not_www']=$hostarr['host'];
					$ext=strtolower(substr(strrchr($link, '.'), 1, 10));
					$ico=$hostarr['scheme'].'://'.$hostarr['host'].'/favicon.ico';
					$ico_not_www=$hostarr['scheme'].'://www.'.$hostarr['host_not_www'].'/favicon.ico';
				}
				if($ico && check_remote_file_exists($ico)) $source=$ico;
				elseif($ico_not_www && check_remote_file_exists($ico_not_www)) $source=$ico_not_www;
				if($source){
					$targetpath = dirname($target);
					dmkdir($targetpath);
					ico_png($source,$target);
				}
			}
			if(is_file($target)) $setarr['appico']='appico/'.$subdir.'/'.$imgencode;
			if($setarr['appid']=DB::insert('dzz_apps',$setarr,1)){
				 $setarr['success']=1;
			}
			if(is_file($target)){
				if($imginfo = @getimagesize($target)){
					if($imginfo[0]<30 && $imginfo[1]<30){
						$setarr['isico']=max($imginfo[0],$imginfo[1]);
					}else{
						$setarr['isico']=0;
					}
				}
				$setarr['appico']=$target1;
			}else{
				$setarr['appico']='dzz/images/default/icodefault.png';
				$setarr['isico']=0;
			}
			showmessage('do_sucess',DZZSCRIPT,$setarr);
		}
		
	}else{
		
		//获取分类
		$appclass=array();
		$query=DB::query("select * from ".DB::table('dzz_appclass')." where fupid=0 order by disp");
		while($value=DB::fetch($query)){
			$appclass[$value['classid']]=$value;
		}
		include template('market_addapp',0,'./dzz/apps/'.CURMODULE.'/template');	
	}
}
//获取应用信息


?>