<?php
# MetInfo Enterprise Content Management System 
# Copyright (C) MetInfo Co.,Ltd (http://www.metinfo.cn). All rights reserved.

defined('IN_MET') or exit('No permission');

load::sys_class('curl');

class install{
	function dosql(){
		global $_M;	
		$dir = dirname(__FILE__);
		require_once  $dir.'/set.class.php';
		$in = new set();
		$re = $in->dosql();
		$sql = $re['sql'];
		$no = $re['no'];
		$devices = $re['devices'];
		
		$query = "SELECT * FROM {$_M['table']['skin_table']} WHERE skin_name='{$no}'";
		$tem = DB::get_one($query);
		if(!$tem){
			$query = "INSERT INTO {$_M['table']['skin_table']} set skin_name='{$no}',skin_file='{$no}',skin_info='',devices='{$devices}'";
			DB::query($query);
		}
		
		$curl = new curl();
		$curl->set('file', 'index.php?n=platform&c=temcheck&a=doauth');
		$post = array('type'=>'tem', 'no'=>$no, 'cmsver'=>$_M['config']['metcms_v'], 'user_key'=>$_M['config']['met_secret_key']);
		$data = $curl->curl_post($post);
		list($suc, $file, $code, $foot) = explode('|', $data);
		$file = PATH_WEB.'templates/'.$no.'/'.$file;
		if ($suc == 'suc') {
			$str = file_get_contents($file);
			$str = str_replace('$metinfi_auth_code;', $code, $str);
			$str = str_replace('$metinfi_auth_code', $code, $str);
			file_put_contents($file, $str);
		}

		$curl->set('file', 'index.php?n=platform&c=authcheck&a=dois_auth');
		$post = array('type'=>'tem', 'no'=>$no, 'cmsver'=>$_M['config']['metcms_v'], 'user_key'=>$_M['config']['met_secret_key']);
		$data = $curl->curl_post($post);
		$file = PATH_WEB.'templates/'.$no.'/'.'foot.php';
		$str = file_get_contents($file);
		if($data == 'suc'){
			$str = str_replace('$metinfi_auth_footcode;', '', $str);
			$str = str_replace('$metinfi_auth_footcode', '', $str);
		}else{
			$str = str_replace('$metinfi_auth_footcode;', $foot, $str);
			$str = str_replace('$metinfi_auth_footcode', $foot, $str);
		}
		file_put_contents($file, $str);
		
		$query = "DELETE FROM {$_M['table']['templates']} WHERE no='{$no}'";
		DB::query($query);
		foreach ($_M['langlist']['web'] as $key=>$val) {
			foreach ($sql as $ksql=>$vsql) {
				$query = "INSERT INTO {$_M['table']['templates']} SET no='{$no}',lang='{$key}',{$vsql}";
				DB::query($query);
			}
		}
		
		return 'complete';	
	}
}

# This program is an open source system, commercial use, please consciously to purchase commercial license.
# Copyright (C) MetInfo Co., Ltd. (http://www.metinfo.cn). All rights reserved.
?>