<?
class site extends action{
	function setting()
	{
		$setting_res		=		$this->db->getRecords(" SELECT * FROM {$this->db->db_pre}site_config");
		$setting_arr		=		array();
		foreach($setting_res as $val){
			if($val['image_size']){
				$size		=	explode('X',$val['image_size']);
				$setting_arr[$val['name']]['image_size']['width']	=	$size[0];
				$setting_arr[$val['name']]['image_size']['height']	=	$size[1];
			}
			if($val['name']=='site_logo'||$val['name']=='list_thumb'||$val['name']=='img_thumb'||$val['name']=='default_icon'){
				$setting_arr[$val['name']]['preview_img']		=		dir_to_url($val['value']);
			}
			if($val['name']=='site_external_link'&&$val['value']){
				$setting_arr[$val['name']]['list_val']		=		storage_dencode($val['value']);
			}
			if($val['name']=='book_mark'&&$val['value']){
				$setting_arr[$val['name']]['list_val']		=		storage_dencode($val['value']);
			}
			$setting_arr[$val['name']]['value']	=		$val['value'];
		}
		$this->init_tpl();
		$this->tpl->out['setting_arr']		=	$setting_arr;
		$this->tpl->display('site.setting.html');	
	}

	function save_seeting()
	{
		global $_ADMIN_LANG;
		if($_SERVER['REQUEST_METHOD'] != "POST"){
			$this->setting();	
			return;
		}
		$submit_arr		=	$this->check_submit_setting();
		foreach($submit_arr as $key=>$val){
			$cond			=	" name= '".$this->db->escape($key)."'";
			$sql_data	=	array(
					'value'			=>		$val['value'],
					'image_size'	=>		$val['image_size'],
					'update_time'	=>		time(),
			);
			if(!$this->db->updateRecord("{$this->db->db_pre}site_config",$cond,$sql_data)){
					$this->system_message($_ADMIN_LANG['site_settings_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_settings']=>'index.php?act=site.setting'));	
					exit;
			}
		}
		$this->system_message($_ADMIN_LANG['site_settings_modification_success'],'succeed',array($_ADMIN_LANG['back_to_the_settings']=>'index.php?act=site.setting'));	
		exit;
	}
	function check_submit_setting()
	{
		if(!$_POST['site_config']){
				return false;
		}
		global $_ADMIN_LANG;
		$submit_arr		=		array();
		require_once(ROOT_PATH.'includes/glo_images.php');
		$images_obj				=	new glo_images;	
		foreach($_POST['site_config'] as $key=>$val){
			switch($key){
				case 'site_logo':
					$img_width			=	intval($val['image_width']);
					$img_height			=	intval($val['image_height']);
					$submit_arr[$key]['image_size']	=	$img_width.'X'.$img_height;
					if($_FILES['site_logo_upload_file']['name']){
						$file_arr			=	$_FILES['site_logo_upload_file'];
						if($file_arr['error']==0){
							$submit_arr[$key]['value']			=	$images_obj->upload_img($file_arr,'logo','',$img_width,$img_height);
						}
					}else{
						$submit_arr[$key]['value']			=	$val['value'];
					}
					break;
				case 'admin_pass':
						if($val['new']){
							$admin_pass		=		$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_pass' AND value='".$this->db->escape(encrypt_admin_pass($val['old']))."' LIMIT 1");
							if(!$admin_pass){
								$this->system_message($_ADMIN_LANG['site_settings_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_settings']=>'index.php?act=site.setting'),$_ADMIN_LANG['admin_old_password_error']);	
								exit;
							}else{
								$submit_arr[$key]['value']	=	encrypt_admin_pass($val['new']);
							}
						}
					break;
				case 'img_thumb':
					$img_width								=	intval($val['img_width']);
					$img_height								=	intval($val['img_height']);
					$submit_arr[$key]['image_size']	=	$img_width.'X'.$img_height;
					if($_FILES['default_img_thumb']['name']){
						$file_arr			=	$_FILES['default_img_thumb'];
						if($file_arr['error']==0){
							$submit_arr[$key]['value']			=	$images_obj->upload_img($file_arr,'software_thumb_image','',$img_width,$img_height);
						}
					}else{
						$submit_arr[$key]['value']			=	$val['value'];
					}	
					break;
				case 'list_thumb':
					$img_width								=	intval($val['img_width']);
					$img_height								=	intval($val['img_height']);	
					$submit_arr[$key]['image_size']	=	$img_width.'X'.$img_height;
					if($_FILES['default_list_thumb']['name']){
						$file_arr			=	$_FILES['default_list_thumb'];
						if($file_arr['error']==0){
							$submit_arr[$key]['value']			=	$images_obj->upload_img($file_arr,'software_list_image','',$img_width,$img_height);
						}
					}else{
						$submit_arr[$key]['value']			=	$val['value'];
					}
					break;
				case 'default_icon':
					if($_FILES['default_icon']['name']){
						$file_arr			=	$_FILES['default_icon'];
						if($file_arr['error']==0){
							$submit_arr[$key]['value']			=	$images_obj->upload_img($file_arr,'software_icon','',32,32);
						}
					}else{
						$submit_arr[$key]['value']			=	$val['value'];
					}
					break;	
				case 'site_external_link':
						$v_arr		=		array();
						foreach($val['name'] as $k=>$v){
							if(!$v||!$val['link'][$k]){
								continue;
							}
							$v_arr[]	=	array(
								'name'	=>	$v,
								'link'	=>	$val['link'][$k],
							);
						}
						$submit_arr[$key]['value']	=	storage_encode($v_arr);
					break;
				case 'book_mark':
						$v_arr		=		array();
						foreach($val['name'] as $k=>$v){
							if(!$v||!$val['code'][$k]){
								continue;
							}
							$v_arr[]	=	array(
								'name'	=>	$v,
								'code'	=>	$val['code'][$k],
							);
						}
						$submit_arr[$key]['value']	=	storage_encode($v_arr);
					break;
				default:
						$submit_arr[$key]['value']		=		$val;
					break;
			}	
		}
		return $submit_arr;
	}


	function stat()
	{
		$this->init_tpl();
		require_once(ROOT_PATH.'includes/glo_stat.php');
		$stat_obj	=	new stat();
		global $_ADMIN_LANG;	
		if($end_date	=	$_GET['end_date']){
			$date_arr	=	explode('-',$end_date);
			$end_time	=	mktime(0,0,0,$date_arr[1],$date_arr[2]+1,$date_arr[0]);	
		}else{
			$end_time	=	time();
			$end_date	=	date('Y-m-d',$end_time);
			$_GET['end_date']	=	$end_date;
		}
		if($start_date	=	$_GET['start_date']){
			$date_arr	=	explode('-',$start_date);
			$start_time	=	mktime(0,0,0,$date_arr[1],$date_arr[2],$date_arr[0]);
		}else{
			$start_time	=	mktime(0,0,0,date('m'),date('d'),date('Y'))-86400 * 7;
			$start_date	=	date('Y-m-d',$start_time);	
			$_GET['start_date']	=	$start_date;
		}

			$where_sql	=	" access_time>=$start_time AND access_time<=$end_time ";

			$day_report		=	$this->db->getRecords(" SELECT COUNT(access_time) as count,FROM_UNIXTIME(access_time,'%Y-%m-%d')  as time FROM {$this->db->db_pre}stats WHERE {$where_sql} GROUP BY FROM_UNIXTIME(access_time,'%Y-%m-%d') ");
			if($day_report){
				foreach($day_report as $key=>$val){
					$day_report[$key]['chart_color']		=	chart_color($key);
				}
			}
			$this->tpl->out['day_report']	=	$day_report;

			$area_report	=	$this->db->getRecords(" SELECT COUNT(access_time) as count ,ip_address, area FROM {$this->db->db_pre}stats WHERE  {$where_sql} GROUP BY area");
			global $glo_lang;
			require_once(ROOT_PATH.'admin/lang/country_'.$glo_lang.'.php');
			if($area_report){
				foreach($area_report as $key=>$val){
					$ipdot	=	explode('.',$val['ip_address']);
					if ($ipdot[0] == 10 || $ipdot[0] == 127 || ($ipdot[0] == 192 && $ipdot[1] == 168) || ($ipdot[0] == 172 && ($ipdot[1] >= 16 && $ipdot[1] <= 31))){
    					$area_report[$key]['area']		=	'LAN';
					}else{
						$area_report[$key]['area']		=	trim($_COUNTRY[$area_report[$key]['area']]);
					}
				}
			}
			$this->tpl->out['area_report']	=	$area_report;

			$domin_report	=	$this->db->getRecords(" SELECT COUNT(access_time) as count ,referer_domain FROM {$this->db->db_pre}stats WHERE {$where_sql} GROUP BY referer_domain ");
			if($domin_report){
				foreach($domin_report as $key=>$val){
					if($val['referer_domain']==''||str_replace(array('http://','https://'),'',$val['referer_domain'])==$SERVER['HTTP_HOST']){
						$domin_report[$key]['referer_domain']	=	$_ADMIN_LANG['enter_the_website_address'];
					}
				}
			}
			$this->tpl->out['domin_report']	=	$domin_report;

			$end_date	=	date('Y-m-d',$end_time+86400);
			if($searchengine	=	$_GET['searchengine']){
				$searchengine_sql	=	" AND searchengine LIKE '%".$this->db->like_escape($searchengine)."%'";
			}else{
				$searchengine_sql	=	'';
			}
	
			$keyword_tmp_report	=	$this->db->getRecords(" SELECT count,keyword,searchengine,date FROM {$this->db->db_pre}keywords WHERE date>='".$this->db->escape($start_date)."' AND date<='".$this->db->escape($end_date)."' {$searchengine_sql}");
			$this->tpl->out['searchengine_type']	=	array(
				 'GOOGLE',
		       'BAIDU',
        		 'YAHOO',
		       'MSN',
				 'YODAO',
				 'IASK',
				 'SOGOU',
			);

			$keyword_report		=	array();
			$keyword_arr			=	array();
			if($keyword_tmp_report){
				foreach($keyword_tmp_report as $key=>$val){
						if($keyword_report[$val['date']][$val['keyword']]){
							$keyword_report[$val['date']][$val['keyword']]['count']	+=	$val['count'];
						}else{
							$keyword_report[$val['date']][$val['keyword']]				=	$val;
						}
					$keyword_arr[]	=		$val['keyword'];
				}
				$keyword_arr		=		array_unique($keyword_arr);
				$keyword_new_report	=	array();
				foreach($keyword_report as $key=>$val){
					foreach($keyword_arr as $k_val){
						if($val[$k_val]){
							$keyword_new_report[$key][]	=	$val[$k_val];	
						}else{
							$keyword_new_report[$key][]	=	array('count'=>0);
						}
					}
				
				}	
				$keyword_report	=	$keyword_new_report;
			}
			$this->tpl->out['keyword_arr']		=	$keyword_arr;
			$this->tpl->out['keyword_report']	=	$keyword_report;
		if($_GET['report_file']){
			$excel_data		=		'<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">';	
			$excel_data		.=		"<table   width='100%' cellpadding='4'>";
			
			$file_name		=	$_GET['start_date'].'_'.$_GET['end_date'];
    		header("Content-type: application/vnd.ms-excel; charset=UTF-8");
		   header("Content-Disposition: attachment; filename=$file_name.xls");
			
			$excel_data		.=		"<tr style='font-size:15px;font-weight:bold;background:#7d7b7b;'><td align=center >".$_ADMIN_LANG['calculated_by_date']."</td></tr>";
			$excel_data		.=		"<tr style='font-weight:bold;'><td align=center >".$_ADMIN_LANG['date']."</td><td align=center>".$_ADMIN_LANG['visitors']."</td></tr>";

			if($day_report){
				$excel_data		.=	"<tr>";
				foreach($day_report as $key=> $val){
					$excel_data	.="<td align=center >".$val['time']."</td><td align=center>".$val['count']."</td>";
				}
				$excel_data		.=	"</tr>";
			}
			$excel_data			.=	"<tr></tr>";
			
			$excel_data		.=		"<tr style='font-size:15px;font-weight:bold;background:#7d7b7b;'><td align=center >".$_ADMIN_LANG['calculated_by_area']."</td></tr>";
			$excel_data		.=		"<tr style='font-weight:bold;'><td align=center >".$_ADMIN_LANG['area']."</td><td align=center>".$_ADMIN_LANG['visitors']."</td></tr>";
			if($area_report){
				 $excel_data    .= "<tr>";
				foreach($area_report as $key=>$val){
					$excel_data	.="<td align=center >".$val['area']."</td><td align=center>".$val['count']."</td>";
				}
				$excel_data    .= "</tr>";
			}
			$excel_data       .= "<tr></tr>";
			
			$excel_data		.=		"<tr style='font-size:15px;font-weight:bold;background:#7d7b7b;'><td align=center >".$_ADMIN_LANG['calculated_by_source']."</td></tr>";
			$excel_data		.=		"<tr style='font-weight:bold;'><td align=center >".$_ADMIN_LANG['website_address']."</td><td align=center>".$_ADMIN_LANG['visitors']."</td></tr>";
			if($domin_report){
				$excel_data    .= "<tr>";
				foreach($domin_report as $key=>$val){
					$excel_data	.="<td align=center >".$val['referer_domain']."</td><td align=center>".$val['count']."</td>";
				}
				 $excel_data    .= "</tr>";
			}
			$excel_data       .= "<tr></tr>";

			$excel_data		.=		"<tr style='font-size:15px;font-weight:bold;background:#7d7b7b;'><td align=center >".$_ADMIN_LANG['calculated_by_keywords_source']."</td></tr>";
			$excel_data		.=		"<tr style='font-weight:bold;'><td align=center >".$_ADMIN_LANG['date']."</td><td align=center>".$_ADMIN_LANG['searchengine']."</td><td align=center>".$_ADMIN_LANG['keyword']."</td><td align=center>".$_ADMIN_LANG['times']."</td><tr>";
			
			if($keyword_report){
				foreach($keyword_report as $key=>$val){
					$excel_data    .= "<tr>";
					foreach($val as $c_val){
						$excel_data	.="<td align=center >".$c_val['date']."</td><td align=center>".$c_val['searchengine']."</td><td align=center>".$c_val['keyword']."</td><td align=center>".$c_val['count']."</td><tr>";
					}
					 $excel_data    .= "</tr>";
				}
			}
			$excel_data			.=	"</table>";
			echo $excel_data;
			exit;
		}else{
			$this->tpl->display("site.stat.html");
		}
	}

	function module()
	{
		$this->init_tpl();
		require_once(ROOT_PATH.'includes/glo_module_extension.php');
		$module_ex	=	new module_ex; 
		$this->tpl->out['module_message']	=	$module_ex->list_module();
		$this->tpl->display("site.module.html");
	}

	function module_install()
	{
		global $_ADMIN_LANG;
		require_once(ROOT_PATH.'includes/glo_module_extension.php');
		$module_ex	=	new module_ex; 
		$module_dir	=	$_GET['module_dir'];	
		$result		=	$module_ex->install_module($module_dir);
		if($result){
			$this->system_message($_ADMIN_LANG['install_extension_success'],'succeed',array($_ADMIN_LANG['return']=>'index.php?act=site.module'));	
		}else{
			$this->system_message($_ADMIN_LANG['install_extension_failure'],'error',array($_ADMIN_LANG['return']=>'index.php?act=site.module'));	
		}
	}

	function module_uninstall()
	{
		require_once(ROOT_PATH.'includes/glo_module_extension.php');
		global $_ADMIN_LANG;
		$module_ex	=	new module_ex; 
		$module_dir	=	$_GET['module_dir'];	
		$result		=	$module_ex->uninstall_module($module_dir);
		if($result){
			$this->system_message($_ADMIN_LANG['uninstall_extension_success'],'succeed',array($_ADMIN_LANG['return']=>'index.php?act=site.module'));	
		}else{
			$this->system_message($_ADMIN_LANG['uninstall_extension_failure'],'succeed',array($_ADMIN_LANG['return']=>'index.php?act=site.module'));	
		}

	}

	function setemail()
	{
		$this->init_tpl();
		$email_setting		=		$this->db->getString( " SELECT value FROM {$this->db->db_pre}site_config WHERE name='email_setting' LIMIT 1");
		$admin_email		=		$this->db->getString( "SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_email' LIMIT 1" );
		$this->tpl->out['admin_email']	=	$admin_email;
		$email_setting		=		storage_dencode($email_setting);
		if($email_setting['type']=='smtp'){
			$this->tpl->out['is_smtp']	=	' checked =true ';
		}else{
			$this->tpl->out['is_mail']	=	' checked =true ';
		}
		$this->tpl->out['email_setting']	=	$email_setting;

		$this->tpl->display('site.setemail.html');
	}
	
	function save_setemail()
	{
		global $_ADMIN_LANG;
		$mail_type	=	$_POST['mail_type']?$_POST['mail_type']:'mail';
		$port			=	$_POST['mail_port']?$_POST['mail_port']:25;
		if($mail_type=='smtp'){
			$smtp['host']			=	$_POST['smtp']['host'];
			$smtp['user']			=	$_POST['smtp']['user'];
			$smtp['pass']			=	$_POST['smtp']['pass'];
		}	
		if(!($reply					=	$_POST['mail_reply'])){
				$reply				=	$this->db->getString(" SELECT value FROM {$this->db->db_pre}site_config WHERE name='admin_email' LIMIT 1");
		}	
		$from_name					=	$_POST['from_name']?$_POST['from_name']:$reply;
		
		$mail_value_arr		=		array(
			'type'			=>	$mail_type,
			'smtp'			=>	$smtp,
			'reply'			=>	$reply,
			'port'			=>	$port,
			'from_name'		=>	$from_name,
		);	
		$mail_value_str	=	storage_encode($mail_value_arr);
		if($_POST['is_email_test']&&$_POST['email_test_addr']){
			//test email
			$email_test_addr				=	$_POST['email_test_addr'];
			if($mail_type=='smtp'){
				$smtp_arr	=	$smtp;
			}else{
				$smtp_arr	=	array();
			}
			$send_test_email_result		=	sendmail($email_test_addr,' '.$_ADMIN_LANG['system_name'].' '.$_ADMIN_LANG['sending_test_mail'], ' '.$_ADMIN_LANG['system_name'].' '.$_ADMIN_LANG['sending_test_mail'],$reply,$from_name,$port,$smtp_arr,$reply);
			if(!$send_test_email_result){
				$this->system_message($_ADMIN_LANG['mail_test_delivery_failed'],'error',array($_ADMIN_LANG['mail_test_delivery_failed']=>'javascript:history.go(-1);'));	
				exit;
			}
		}
		$result	=	$this->db->updateRecord("{$this->db->db_pre}site_config"," name='email_setting '",array('value'=>$mail_value_str));	
		if($result){
			$this->system_message($_ADMIN_LANG['mail_system_settings_success'],'succeed',array($_ADMIN_LANG['return']=>'index.php?act=site.setemail'));	
			exit;
		}
	}
	function setsite_type()
	{
		$this->init_tpl();
		$software_list		=		$this->db->getRecords("SELECT software_name,software_id FROM {$this->db->db_pre}software GROUP BY software_name");
		$site_type			=		$this->db->getString("SELECT value FROM {$this->db->db_pre}site_config WHERE name='site_type'  ");
		if(is_string($site_type)&&$site_type){
			$tmp_list		=		array();
			if($software_list){
				foreach($software_list as $val){
					$tmp_list[]	=	$val['software_name'];
				}
				if(!in_array($site_type,$tmp_list)){
					$site_type		=		current($tmp_list);
					$this->db->updateRecord("{$this->db->db_pre}site_config"," name='site_type' ",array('value'=>$site_type));
				}
			}
			$this->tpl->out['only']		=		true;
		}else{
			$this->tpl->out['more']		=		true;
		}
		$this->tpl->out['software_list']		=	$software_list;
		$this->tpl->out['site_type']	=	$site_type;
		$this->tpl->display("site.setsite_type.html");	
	}

	function savesetsite_type()
	{
		global $_ADMIN_LANG;
		$site_type	=	$_POST['site_type'];
		if($site_type=='only'){
			$value		=	$_POST['software_name'];
		}else{
			$value		=	'';
		}
		$result	=	$this->db->query(" INSERT INTO  {$this->db->db_pre}site_config SET value='".$this->db->escape($value)."', name='site_type' ON DUPLICATE KEY UPDATE value='".$this->db->escape($value)."'");
		if($result){
			$this->system_message($_ADMIN_LANG['site_pattern_settings_success'],'succeed',array($_ADMIN_LANG['back_to_site_pattern_settings']=>'index.php?act=site.setsite_type',$_ADMIN_LANG['select_template_again']=>'index.php?act=template_set.select'));	
			exit;
		}else{
			$this->system_message($_ADMIN_LANG['site_pattern_settings_failure'],'error',array($_ADMIN_LANG['return']=>'index.php?act=site.setsite_type'));	
			exit;
		}

	}

}
