<?php
/****************优客365网址导航系统 商业版********************/
/*                                                            */
/*  Youke365.com (C)2017 Youke365 Inc.                        */
/*  This is NOT a freeware, use is subject to license terms   */
/*  优客365网址导航系统是商业软件,使用于商业用途请购买授权    */
/*  V1.0 2017                                                 */
/*  技术支持：1513773524@qq.com                               */
/*  官方网址：http://www.youke365.com                         */
/*  官方论坛：http://bbs.youke365.com                         */                           
/**************************************************************/

require('common.php');

$fileurl = 'option.php';
$tempfile = 'option.html';
$table = $DB->table('options');

$option = I($_GET['opt'],$_POST['opt']);
$action = I($_POST['act']);


if (empty($option)) $option = 'basic';
$fileurl .= '?opt='.$option;

if (in_array($option, array('basic','reward','comment','misc', 'user', 'link', 'qq', 'mail'))) {
	switch ($option) {
		case 'basic' :
			$pagetitle = '站点信息';
			break;
		case 'misc' :
			$pagetitle = '网站配置';
			break;
		case 'user' :
			$pagetitle = '注册设置';
			break;
		case 'qq' :
			$pagetitle = 'QQ登陆';
			break;
		case 'comment' :
			$pagetitle = '评论设置';
		break;
		case 'link' :
			$pagetitle = '链接设置';
			break;
		case 'mail' :
			$pagetitle = '邮件设置';
			break;
		case 'reward' :
			$pagetitle = '打赏设置';
			break;

		default :
			$pagetitle = '站点信息';
			break;
	}
	
	$configs = stripslashes_deep($options);
	$configs['site_root'] = str_replace('\\', '/', dirname($site_root));
	
	$smarty->assign('pagetitle', $pagetitle);
	$smarty->assign('option', $option);
	$smarty->assign('cfg', $configs);
	unset($configs);
	 
	if ($action == 'update') {
		foreach ($_POST['cfg'] as $cname => $cval) {
			//if ($cname == 'site_url' && !empty($cval)) $cval .= (substr($cval, -1) != '/') ? '/' : '';
			if ($cname == 'data_update_cycle' && $cval <= 0) $cval = 3;
			if ($cname == 'filter_words') {
				$cval = str_replace('，', ',', $cval);
				$cval = str_replace(',,', ',', $cval);
				if (substr($cval, -1) == ',') {
					$cval = substr($cval, 0, strlen($cval) - 1);
				}


			 }

		if(!empty($_FILES['site_logo']['name'])){
		  //上传logo
		     
			 $Upload = new FileUpload;
			 $Upload->set('maxsize','1000000');
			 $Upload->set('israndname',false);		
			 
			 $Upload->set('path',ROOT_PATH.'public/images/');

			 $Upload->upload('site_logo');
			 if(!$Upload->getFileName()){
		         msgbox($Upload->getErrorMsg()); 
			 }else{
			         $site_logo = '/public/images/'.$Upload->getFileName();
			   		 rename(ROOT_PATH.$site_logo,ROOT_PATH.'/public/images/logo.png');
                     $site_logo = '/public/images/logo.png';
			        if($Upload->getFileName()){
				     $DB->fetch_one("SELECT option_name FROM $table WHERE option_name = 'site_logo'") ? 
					 $DB->update($table, array('option_value' => $site_logo),
					 	array('option_name' => 'site_logo')) : $DB->insert($table,array('option_name' => 'site_logo', 'option_value' => $site_logo));
		           }
			 }
         }


         if(!empty($_FILES['qcode_alipay']['name'])){
		  //上传支付宝二维码
		     
			 $Upload2 = new FileUpload;
			 $Upload2->set('maxsize','1000000');
			 $Upload2->set('israndname',false);		
			 
			 $Upload2->set('path',ROOT_PATH.'public/images/');

			 $Upload2->upload('qcode_alipay');
			 if(!$Upload2->getFileName()){
		         msgbox($Upload2->getErrorMsg()); 
			 }else{
			         $qcode_alipay = '/public/images/'.$Upload2->getFileName();
			   		 rename(ROOT_PATH.$qcode_alipay,ROOT_PATH.'/public/images/qcode_alipay.png');
                     $qcode_alipay = '/public/images/qcode_alipay.png';
			        if($Upload2->getFileName()){
				     $DB->fetch_one("SELECT option_name FROM $table WHERE option_name = 'qcode_alipay'") ? 
					 $DB->update($table, array('option_value' => $qcode_alipay),
					 	array('option_name' => 'qcode_alipay')) : 
					 $DB->insert($table,array('option_name' =>'qcode_alipay', 'option_value' => $qcode_alipay));
		           }
			 }

         }

         if(!empty($_FILES['qcode_weixin']['name'])){
		  //上传微信二维码
		     
			 $Upload3 = new FileUpload;
			 $Upload3->set('maxsize','1000000');
			 $Upload3->set('israndname',false);		
			 
			 $Upload3->set('path',ROOT_PATH.'public/images/');

			 $Upload3->upload('qcode_weixin');
			 if(!$Upload3->getFileName()){
		         msgbox($Upload->getErrorMsg()); 
			 }else{
			         $qcode_weixin = '/public/images/'.$Upload3->getFileName();
			   		 rename(ROOT_PATH.$qcode_weixin,ROOT_PATH.'/public/images/qcode_weixin.png');
                     $qcode_weixin = '/public/images/qcode_weixin.png';
			        if($Upload3->getFileName()){
				     $DB->fetch_one("SELECT option_name FROM $table WHERE option_name = 'qcode_weixin'") ? 
					 $DB->update($table, array('option_value' => $qcode_weixin),
					 	array('option_name' => 'qcode_weixin')) : $DB->insert($table,array('option_name' =>'qcode_weixin', 'option_value' =>$qcode_weixin));
		           }
			 }

		

         }

			  if (!get_magic_quotes_gpc()) { 
	
				$cval = addslashes($cval); 
		      }
			$udata = array('option_value' => $cval);
			$uwhere = array('option_name' => $cname);
			$idata = array('option_name' => $cname, 'option_value' => $cval);
			
			$DB->fetch_one("SELECT option_name FROM $table WHERE option_name = '$cname'") ? 
			$DB->update($table, $udata, $uwhere) : $DB->insert($table, $idata);
		}
		update_cache('options');
		
		// msgbox('更新系统配置成功！', $fileurl);
	}
}



smarty_output($tempfile);
?>