<?php
defined('CML_PATH') || exit();
class IndexController extends CmlController {		
	public function index(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
	    $IndexModel = new IndexModel();
	    $classlists=$IndexModel->gcmsclass();
	    CmlView::assign('classlists',$classlists);
	    $lists=$IndexModel->articlelist();
	    
$page = new page(100,2);
$pageer=$page->getPage(U('Site/Index/index',array('cate_id'=>1,'status'=>''),true,0));
	    CmlView::assign('pages',$pageer);
	    CmlView::display();
	}		
		
	//用户设置
	public function user(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
		CmlView::display();
	}
		
	//保存用户设置
	public function adminsave(){
		if (check_login() != true){
			$this->error('没有登陆','Index/user');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$siteadminname=getGp('siteadminname',2);
		$siteadminpass=getGp('siteadminpass',2);
		$sitenadminname=getGp('sitenadminname',2);
		$sitenadminpass=getGp('sitenadminpass',2);
		$siteradminpass=getGp('siteradminpass',2);
		if(empty($siteadminname) || empty($siteadminpass) || empty($sitenadminname) || empty($sitenadminpass) || empty($siteradminpass)){
				$this->error('不能为空','Index/user');
			}
		$siteadminpass=md5($siteadminpass);
		$sitenadminpass=md5($sitenadminpass);
		$siteradminpass=md5($siteradminpass);
		$UserModel = new UserModel();
		$userdatas=$UserModel->userck($siteadminname,$siteadminpass);
		
		if ($userdatas['0']['name']==$siteadminname && $userdatas['0']['pass']==$siteadminpass){
			
			if ($sitenadminpass!=$siteradminpass){
			$this->error('新密码与重复密码不一致！请重新输入。','Index/user');
			}else {
				$userok=$UserModel->edituser(1,$sitenadminname,$sitenadminpass);
	    if ($userok == true){
	    	$this->success('修改成功','Index/logout');
			} else {
				$this->error('修改失败','Index/user');;
			}
				
				}
			
			} else {
				$this->error('用户名或密码不正确','Index/user');
				}
				
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
	public function logout(){
	    logout();
			$this->success('注销成功','Index/login');
	}		
	public function loginck(){
		
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
			//检查验证码
			if(isset($_SESSION['captcha_keystring']) && $_SESSION['captcha_keystring'] == getGp('keystring',2)){
		//销毁验证码
		unset($_SESSION['captcha_keystring']);
		$username=getGp('username',2);
		$password=getGp('password',2);
		//检查用户名是否为空
		if (!empty($username)){
		//检查密码是否为空
		if (!empty($password)){
		$password=md5($password);
		$UserModel = new UserModel();
		$userdatas=$UserModel->userck($username,$password);
		if ($userdatas['0']['name']==$username && $userdatas['0']['pass']==$password){
			$_SESSION['gu'] = $userdatas['0']['name'];
			$this->success('登陆成功','Index/admin');
			} else {
				$this->error('登陆失败，用户名或密码不正确','Index/login');
				}
				} else {
				$this->error('没有输入密码','Index/login');
				}	
				} else {
				$this->error('没有输入用户名','Index/login');
				}	
				}else{
				$this->error('验证码不正确','Index/login');
				unset($_SESSION['captcha_keystring']);
					}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
	public function login(){
	    CmlView::display();
	}		
	public function admin(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
$sysos = $_SERVER["SERVER_SOFTWARE"];      //获取服务器标识的字串
$sysversion = PHP_VERSION;                   //获取PHP服务器版本
//以下两条代码连接MySQL数据库并获取MySQL数据库版本信息
$mydbtemp = C('DEFAULT_DB');
mysql_connect("localhost",$mydbtemp[MASTER][USERNAME],$mydbtemp[MASTER][PASSWORD]);
$mysqlinfo = mysql_get_server_info();
//从服务器中获取GD库的信息
if(function_exists("gd_info")){                  
$gd = gd_info();
$gdinfo = $gd['GD Version'];
}else {
$gdinfo = "未知";
}
//从PHP配置文件中获得是否可以远程文件获取
//$allowurl= ini_get("allow_url_fopen") ? "支持" : "不支持";
//从PHP配置文件中获得最大上传限制
$max_upload = ini_get("file_uploads") ? ini_get("upload_max_filesize") : "Disabled";
//从PHP配置文件中获得脚本的最大执行时间
$max_ex_time= ini_get("max_execution_time")."秒";
//以下两条获取服务器时间，中国大陆采用的是东八区的时间,设置时区写成Etc/GMT-8
date_default_timezone_set("Etc/GMT-8");
$systemtime = date("Y-m-d H:i:s",time());

//GB库扩展环境检测
if(extension_loaded('gd')){
	$gdchecker='支持';
	}else{
	$gdchecker='不支持';
		}
			
//pdo扩展环境检测
if(extension_loaded('pdo_sqlite')){
	$pdochecker='支持';
	}else{
	$pdochecker='不支持';
		}
	
//memcache库扩展环境检测
if(extension_loaded('memcache')){
	$memcchecker='支持';
	}else{
	$memcchecker='不支持';
		}	
		
	    CmlView::assign('sysos',$sysos);
	    CmlView::assign('sysversion',$sysversion);
	    CmlView::assign('mysqlinfo',$mysqlinfo);
	    CmlView::assign('gdinfo',$gdinfo);
	    //CmlView::assign('allowurl',$allowurl);
	    CmlView::assign('max_upload',$max_upload);
	    CmlView::assign('max_ex_time',$max_ex_time);
	    CmlView::assign('systemtime',$systemtime);
	    
	    CmlView::assign('pdochecker',$pdochecker);
	    CmlView::assign('gdchecker',$gdchecker);
	    CmlView::assign('memcchecker',$memcchecker);
	    CmlView::display();
	}		
	
	public function config(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
	    CmlView::display();
	}		
	public function configsave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){	
		$sitename=getGp('sitename',2);
		$sitekeywords=getGp('sitekeywords',2);
		$sitedescription=getGp('sitedescription',2);
		$urlradio=getGp('urlradio',2);
		$urlhtmlsuffix=getGp('urlhtmlsuffix',2);
		$sitepagesize=getGp('sitepagesize',2);
		$siteauthor=getGp('siteauthor',2);
// 配置文件变量
$cfg_filename = '_GCMS/Configs/online.inc.php';
// 获取配置文件
$cfg_content = file_get_contents($cfg_filename,'r');
$cfg_content = str_replace("'SITE_SITENAME' => '".C('SITE_SITENAME')."'", "'SITE_SITENAME' => '$sitename'", $cfg_content);
$cfg_content = str_replace("'SITE_SITEKEYWORDS' => '".C('SITE_SITEKEYWORDS')."'", "'SITE_SITEKEYWORDS' => '$sitekeywords'", $cfg_content);
$cfg_content = str_replace("'SITE_SITEDESCRIPTION' => '".C('SITE_SITEDESCRIPTION')."'", "'SITE_SITEDESCRIPTION' => '$sitedescription'", $cfg_content);
$cfg_content = str_replace("'URL_MODEL' => ".C('URL_MODEL'), "'URL_MODEL' => $urlradio", $cfg_content);
$cfg_content = str_replace("'URL_HTML_SUFFIX' => '".C('URL_HTML_SUFFIX')."'", "'URL_HTML_SUFFIX' => '$urlhtmlsuffix'", $cfg_content);
$cfg_content = str_replace("'SITE_PAGESIZE' => ".C('SITE_PAGESIZE'), "'SITE_PAGESIZE' => $sitepagesize", $cfg_content);
$cfg_content = str_replace("'SITE_AUTHOR' => '".C('SITE_AUTHOR')."'", "'SITE_AUTHOR' => '$siteauthor'", $cfg_content);
// 修改配置文件
file_put_contents($cfg_filename, $cfg_content, LOCK_EX);
switch ($urlradio) {
	case 0:
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='/index.php?m=Admin&c=Index&a=config'";
	echo "</script>";
	break;
	case 1:
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='/index.php/Admin/Index/config'";
	echo "</script>";
		break;
	case 2:
	$url = "/Admin/Index/config".$urlhtmlsuffix;
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
		break;
	case 3:
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='/index.php?s=/Admin/Index/config'";
	echo "</script>";
		break;
}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
	// 导航栏
	public function addnav(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
	    CmlView::display();
	}
	
	public function navsave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$linkname=getGp('linkname',2);
		$link=getGp('link',2);
		$linkclass=getGp('linkclass',2);
	    $SitenavModel = M('Sitenav');
	    $navok=$SitenavModel->addnav($link,$linkname,$linkclass);
	    if ($navok == true){
	    	$this->success('添加成功','Index/sitenav');
			} else {
				$this->error('添加失败','Index/addnav');;
			}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
	public function editnav(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$navid=getGp('navid',2);
		$navcid=getGp('navcid',2);
		$link=getGp('link',2);
		$linkname=getGp('linkname',2);
		$linkclass=getGp('linkclass',2);
	    $SitenavModel = M('Sitenav');
	    $navok=$SitenavModel->editnav($navid,$navcid,$link,$linkname,$linkclass);
	    if ($navok == true){
	    	$this->success('修改成功','Index/sitenav');
			} else {
				$this->error('修改失败','Index/sitenav');
			}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
	public function delnav(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$navid=getGp('id');
		$SitenavModel = M('Sitenav');
		$navok=$SitenavModel->delnav($navid);
		if ($navok == true){
			$this->success('删除成功','Index/sitenav');
		} else {
			$this->error('删除失败','Index/sitenav');;
		}
	}
	
	public function sitenav(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$IndexModel =  M('Sitenav');
		$navdata=$IndexModel->indexnav();
		CmlView::assign('navdata',$navdata);
		CmlView::display();
	}
	// 单页
	public function addpage(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
	  CmlView::assign('pagedata',$pagedata);
		CmlView::display();
	}
	
	public function addpagesave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){	
		$ptitle=getGp('ptitle',2);
		$pkeywords=getGp('pkeywords',2);
		$pdescription=getGp('pdescription',2);
		$pcontent=getGp('pcontent',2);
		$poname=getGp('poname',2);
	  $PagesModel = M('Pages');
	  $pageok=$PagesModel->addpage($ptitle,$pkeywords,$pdescription,$pcontent,$poname);
	    if ($pageok == true){
	    	$this->success('添加成功','Index/onepage');
			} else {
				$this->error('添加失败','Index/addpage');;
			}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}	
	
	public function editpage(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$pid=getGp('id');
	  $PagesModel = M('Pages');
		$pagedata=$PagesModel->editpage($pid);
	  CmlView::assign('pagedata',$pagedata);
		CmlView::display('addpage');
	}	
			
	public function editpagesave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$pid=getGp('id',2);
		$ptitle=getGp('ptitle',2);
		$pkeywords=getGp('pkeywords',2);
		$pdescription=getGp('pdescription',2);
		$pcontent=getGp('pcontent',2);
		$poname=getGp('poname',2);
	  $PagesModel = M('Pages');
	    $pageok=$PagesModel->editpagesave($pid,$ptitle,$pkeywords,$pdescription,$pcontent,$poname);
	    if ($pageok == true){
	    	$this->success('修改成功','Index/onepage');
			} else {
				$this->error('修改失败','Index/onepage');
			}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
		CmlView::display();
	}
	
	public function delpage(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$pid=getGp('id');
		$PagesModel = M('Pages');
		$pageok=$PagesModel->delpage($pid);
		if ($pageok == true){
			$this->success('删除成功','Index/onepage');
		} else {
			$this->error('删除失败','Index/onepage');;
		}
		CmlView::display();
	}
	
	public function onepage(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$PagesModel = M('Pages');
		$pagedata=$PagesModel->indexpage();
		CmlView::assign('pagedata',$pagedata);
		CmlView::display();
	}
	
	// 标签
	public function addtags(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		CmlView::display();
	}
	
	public function addtagssave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$tagname=getGp('tagname',2);
		$seotag=getGp('seotag',2);
	    $TagsModel = M('Tags');
	    $tagok=$TagsModel->addtags($tagname,$seotag);
	    if ($tagok == true){
	    	$this->success('添加成功','Index/sitetags');
			} else {
				$this->error('添加失败','Index/addtags');;
			}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
		public function edittags(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$id=getGp('id');
	  $TagsModel = M('Tags');
		$tagdata=$TagsModel->edittags($id);
	  CmlView::assign('tagdata',$tagdata);
		CmlView::display('addtags');
	  }	
			
	public function edittagssave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$id=getGp('id',2);
		$tagname=getGp('tagname',2);
		$seotag=getGp('seotag',2);
	    $TagsModel = M('Tags');
	    $tagok=$TagsModel->edittagssave($id,$tagname,$seotag);
	    if ($tagok == true){
	    	$this->success('修改成功','Index/sitetags');
			} else {
				$this->error('修改失败','Index/sitetags');
			}
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
	
	public function deltags(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$id=getGp('id');
		$TagsModel = M('Tags');
		$tagok=$TagsModel->deltags($id);
		if ($tagok == true){
			$this->success('删除成功','Index/sitetags');
		} else {
			$this->error('删除失败','Index/sitetags');;
		}
	}
	
	public function sitetags(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$TagsModel = M('Tags');
		$tagsdata=$TagsModel->indextags();
		CmlView::assign('tagsdata',$tagsdata);
		CmlView::display();
	}
	//幻灯设置
	public function slide(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		$TagsModel = M('Tags');
		$slidedata=$TagsModel->indexslide();
		CmlView::assign('slidedata',$slidedata);
		CmlView::display();
	}
	//幻灯设置
	public function slidesave(){
		
		if (check_login() != true){
			$this->error('没有登陆','Index/login');
			return;
			}
			
		if(isset($_POST['submit']) && CmlSecure::checkToken()==true){
		$id=getGp('id',2);
		$title=getGp('title',2);
		$imgurl=getGp('imgurl',2);
		$url=getGp('url',2);
		$info=getGp('info',2);
		
		$TagsModel = M('Tags');
		$slideok=$TagsModel->slidesave($id,$title,$imgurl,$url,$info);
		if ($slideok == true){
	    	$this->success('操作成功','Index/slide');
			} else {
				$this->error('操作失败','Index/slide');
			}
			
		}else
		{ 
				$this->error('非法提交','Index/logout');
		}
	}
}
