<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: veryide_welcome.php,v2 19:47 2008-2-18 Lay $
*/

require '../../include/loader.php';
html_start("版本验证 - VeryIDE");
?>

<!--body start-->
    
	<script type="text/javascript">
		//if( parent.parent.Mo.store.model == "modern" ){
			Mo("#wrapper").attr({"class":"snugly"});
		//}
	</script>
    
	<?php
	
	//验证地址
	$URL =  $_G['project']['home'].'client/service.php?action=activation';
	
	//安装地址
	$install = VI_HOST;
	
	$action = getgpc('action');
	
	switch($action){
		
		case "activation":
		
			//当前域名
			$post_data['domain'] = $_G['project']["domain"];
			
			//安装地址
			$post_data['install'] = $install;
			
			//安装时间
			$post_data['vistart'] = VI_START;
			
			$referrer="";
			
			// parsing the given URL
			$URL_Info=parse_url($URL);
			
			// Building referrer
			if($referrer=="") // if not given use this script as referrer
			$referrer=$_SERVER["SCRIPT_URI"];
			 
			// making string from $data
			foreach($post_data as $key=>$value)
			$values[]="$key=".urlencode($value);
			 
			$data_string=implode("&",$values);
			
			// Find out which port is needed - if not given use standard (=80)
			if(!isset($URL_Info["port"]))
			$URL_Info["port"]=80;
			
			// building POST-header:
			$header.="POST ".$URL_Info["path"].'?'.$URL_Info["query"]." HTTP/1.1\n";
			$header.="Host: ".$URL_Info["host"]."\n";
			$header.="Referer: $referrer\n";
			$header.="Content-type: application/x-www-form-urlencoded\n";
			$header.="Content-length: ".strlen($data_string)."\n";
			$header.="Connection: close\n";
			$header.="\n";
			$header.=$data_string."\n";
			
			$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
			
			fputs($fp, $header);
			$inheader = 1; 
			
			$result = '';
			
			while(!feof($fp)) {
				$line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据 
				
				if ($inheader && ($line == "\n" || $line == "\r\n")) { 
					$inheader = 0; 
				} 
				if ( $inheader == 0 && strpos($line,"{") !== false && strpos($line,"}") !== false ) { 
					//echo $line; 
					$result.=trim($line);
				} 
				//$result .= fgets($fp, 128);
			}
			fclose($line);
			
			$file = VI_ROOT."config/licence.php";
			
			switch( $result ){
				
				case "{ERR}":
					echo '<div id="state" class="failure">抱歉！VeryIDE 服务器暂时不能正常服务，请稍后再试。</div>';
				break;
				
				case "{NOT}":				
					echo '<div id="state" class="failure">您当前使用的是非商业授权的免费版。</div>';
					
					create_file($file,'<?php $_G[\'licence\'] = array ("domain" => "localhost", "type" => "free");?>');
				break;
				
				default:
				
					$hash = unserialize($result);
					
					if( !$hash ){
						echo '<div id="state" class="failure">抱歉！与 VeryIDE 服务器数据传输出错，请稍后再试。</div>';						
					}else{
						echo '<div id="state">恭喜！版本验证成功，当前使用的是 <strong class="text-key">'.$_G['version'][$hash['type']]['name'].'</strong></div>';
										
						create_file($file,'<?php $_G[\'licence\'] = '.var_export($hash, TRUE).';?>');
					}
					
				
				break;
			
			}
			
			//重新包含文件
			include($file);
			
		break;
		
		
	}
	
	//echo $URL;
	
	/*
	if( !$action ){
		echo '<div id="state">您目前使用的是 <strong class="text-key">'.$_G['version'][$_G['licence']['type']]['name'].'</strong></div>';	
	}	
	*/
	
	?>
    
    <div id="box">

        <form action="?" method="get">
       		<input type="hidden" name="action" value="activation" />
        
            <table cellpadding="0" cellspacing="0" class="form">
    
                    <tr><td class="section"><strong>版本验证</strong></td></tr>
    
                    <tr>
                    	<td class="block">
                            <p class="text-key">
                                将收集以下信息发送至 VeryIDE ：
                            </p>
                            <p>
                                当前域名：<span><?php echo $_G['project']["domain"];?></span>
                            </p>
                            <p>
                                安装地址：<span><?php echo $install;?></span>
                            </p>
                            <p>
                                安装时间：<span><?php echo date("Y-m-d H:i:s",VI_START);?></span>
                            </p>
                        </td>
                    </tr>
                    
                    <tr>
                    	<td>
                       		<p><input type="submit" class="submit" value="立即验证" /></p>
                        </td>                
                    </tr>
                    
            </table>
        
        </form>
        
        <table cellpadding="0" cellspacing="0" class="form">

                <tr><td colspan="<?php echo count($_G['version']);?>" class="section"><strong>版本对比</strong></td></tr>

                <tr>
					<?php
                    
                    foreach( $_G['version'] as $key => $val ){                    
                        echo '<td align="center">';
						
							if( $_G['licence']['type'] == $key ){
								echo '<span style="float:right" class="text-yes"><img src="'.VI_BASE.'image/icon/tick.png" /> 当前</span>';
							}
						
                        	echo '<a href="http://www.veryide.com/buy.php" target="_blank"><img src="'.VI_BASE.'image/model/'.$key.'.gif" /></a><br />';
							echo  $val['desc'];
                        echo '</td>';
                    
                    }
                    
                    ?>                    
                </tr>
        </table>

    </div>	
    
    
<!--body end-->

<?php html_close();?>