<?php
/////////////////////////////////////////////////////////////////////////////
// 这个文件是 shopnc单用户 项目的一部分
//
// Copyright (c) 2007 - 2008 www.shopnc.net
//
// 要查看完整的版权信息和许可信息，请查看源代码中附带的 COPYRIGHT 文件，
// 或者访问 http://www.shopnc.net/ 获得详细信息。
/////////////////////////////////////////////////////////////////////////////

/**
* FILE_NAME : admin_system.php D:\binzi\shopnc6\admin\admin_system.php
* 后台网店管理
*
* @copyright Copyright (c) 2007 - 2007 www.shopnc.net 
* @author 网城创想单用户商城开发团队 php_netproject@yahoo.com.cn
* @package 
* @subpackage 
* @version Mon Jul 07 15:43:48 CST 2008
*/
require ("../global.inc.php");
class AdminSystem extends SystemFrameWork {
	/**
	 * 网店管理对象
	 *
	 * @var obj
	 */
	private $obj_admin_system;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	private $obj_validate;
	/**
	 * 日志对象
	 *
	 * @var obj
	 */
	private $obj_log;

	function main(){
		/**
		 * 创建商品对象
		 */
		if (!is_object($this->obj_system)) {
			require_once("system.class.php");
			$this->obj_admin_system = new SystemClass();
		}
		/**
		 * 创建日志对象
		 */
		if (!is_object($this->obj_log)){
			require_once("toolLog.class.php");
			$this->obj_log = new ToolLogClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 设置模板路径
		 */
		$this->setsubtemplates("admin/templates");
		/**
		 * 语言包
		 */
		$this->getlang("admin_option,admin_menu");
		/**
		 * 执行操作
		 */
		switch($this->_input['action']){
			case 'base_site':		//基本设置
			$this->systemBaseSite();
			break;
			case 'view_site':		//显示设置
			$this->systemShowSite();
			break;
			case 'templates_site':	//模板设置
			$this->systemTemplatesSite();
			break;
			case 'change_temp':		//变换模板
			$this->saveChangeTemp();
			break;
			case 'email_site':		//邮件设置
			$this->systemEmailSite();
			break;
			case 'save_mail':		//保存邮件设置
			$this->saveEmailSite();
			break;
			case 'view_mail_temp':	//查看邮件模板
			$this->viewMailTemplate();
			break;
			case 'save_email_temp':	//保存邮件模板
			$this->saveEmailTemplates();
			break;
			case 'system_save':		//保存系统设置
			$this->saveSystemFile();
			break;
			case 'system_info':		//系统信息
			$this->systemInfoList();
			break;
			case 'edit_system_info'://编辑系统信息
			$this->editSystemInfo();
			break;
			case 'system_info_save'://信息保存
			$this->saveSystemInfo();
			break;
			case 'system_image':	//图片设置
			$this->imageSite();
			break;
			case 'image_save':		//图片设置保存
			$this->imageSiteSave();
			break;
		}
	}
	/**
	 * 基本设置页面
	 * 
	 */
	private function systemBaseSite() {
		/*权限检查*/
		$this->checkAdmin('base_site');

		$array	= $this->obj_admin_system->getSystemConfig(array('config_type'=>'base'));
		$this->output('config_str',implode(',',$array['config_str']));
		$this->output('config_array',$array['config_array']);

		/*语言包选择*/
		require_once("moduleLanguage.class.php");
		$module_language = new ModuleLanguageClass();
		$conditon_array = array('language_state'=>1);
		$language_array	= $module_language->getLanguageList($conditon_array);
		$this->output('language_array',$language_array);
		/*货币选择*/
		require_once("moduleCurrencies.class.php");
		$module_currencies = new ModuleCurrenciesClass();
		$conditon_array = array();
		$currencies_array = $module_currencies->getCurrenciesList($conditon_array);
		$this->output('currency_array',$currencies_array);
		/*网店状态*/
		$shop_state_select	= array(intval($array['config_array']['shop_state']));
		$this->output('shop_state',Common::Input('shop_state','radio',array(1=>$this->_lang['admin_system_basic_open'],0=>$this->_lang['admin_system_basic_close']),$shop_state_select));

		$this->showpage('option_basic');
	}
	/**
	 * 显示设置页面
	 * 
	 */
	private function systemShowSite() {
		/*权限检查*/
		$this->checkAdmin('view_site');

		/*得到系统配置信息的数组*/
		$array = $this->obj_admin_system->getSystemConfig(array('config_type'=>'view'));
		$this->output('config_str',implode(',',$array['config_str']));
		
		/*得到单选框和复选框的数组*/
		if(is_array($array['config_array']) and count($array['config_array'])) {
			foreach ($array['config_array'] as $k => $v) {
				if(strstr($k,'other_')) {
					$radio_array[$k]	= Common::Input($k,'radio',array(0=>''.$this->_lang['admin_system_display_single'].'（'.$this->_lang['admin_system_display_in_cate'].'）',1=>$this->_lang['admin_system_display_united']),array($v));
				} else {
					$radio_array[$k]	= Common::Input($k,'radio',array(1=>''.$this->_lang['admin_system_display_yes'].'',0=>''.$this->_lang['admin_system_display_no'].''),array($v));
				}
				$input_array[$k]	= Common::Input($k.'_num','text',$array['other_array'][$k],'','size=5');
			}
		}
		$this->output('radio_array',$radio_array);
		$this->output('input_array',$input_array);

		$this->showpage('option_view');
	}
	/**
	 * 模板选取页面
	 * 
	 */
	private function systemTemplatesSite() {
		/*权限检查*/
		$this->checkAdmin('templates_site');

		$array	= $this->obj_admin_system->getSystemConfig(array('config_type'=>'base'));
		$dir = "../templates";
		$dh  = opendir($dir);
		while (false !== ($filename = readdir($dh))) {
			$files[] = $filename;
		}
		$templates = array();
		foreach ($files as $k=>$v){
			$pos = strpos($v, ".");
			if ($pos === false) {
				$templates[] = $v;
			}
		}

		$vi = 0;
		foreach ($templates as $k =>$v) {
			if (trim($v)!=$array['config_array']['templatesname']){
				$temp_array[$vi]['name'] = $v;
				$temp_array[$vi]['image'] = $dir.'/'.$v.'/'.$v.'.jpg';
				$vi++;
			} else {
				$temp_default['name']	= $v;
				$temp_default['image']	= $dir.'/'.$v.'/'.$v.'.jpg';
			}
		}

		$this->output('template',$temp_array);
		$this->output('template_default',$temp_default);
		$this->showpage('option_themes');
	}
	/**
	 * 模板变换保存
	 *
	 */
	private function saveChangeTemp() {
		/*权限检查*/
		$this->checkAdmin('templates_site');

		$array	= $this->obj_admin_system->getSystemConfig(array('config_type'=>'base'));
		foreach ($array['config_array'] as $k => $v) {
			if($k == 'templatesname') {
				$input_param[$k]	= trim($this->_input['temp_name']);
			} else {
				$input_param[$k]	= $v;
			}
		}
		$error = $this->obj_admin_system->writeSystemFile($input_param,'INFO','config');
		if($error) {
			/*日志保存*/
			$log_array			= array();
			$log_array['log_info']		= $this->_lang['admin_system_theme_apply_ok'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_system_theme_apply_ok'],$this->_configinfo['websit']['site_url']."/admin/admin_system.php?action=templates_site",1,2000);
		}
	}
	/**
	 * 邮件设置页面
	 * 
	 */
	private function systemEmailSite() {
		/*权限检查*/
		$this->checkAdmin('email_site');

		$this->showpage('option_mail');
	}
	/**
	 * 保存邮件设置
	 *
	 */
	private function saveEmailSite() {
		/*权限检查*/
		$this->checkAdmin('email_site');

		$input_param['mailtype']		= trim($this->_input['setmail']);	//邮件类型
		$input_param['smtpserver']		= trim($this->_input['smtpserver']);//smtp邮件服务器地址
		$input_param['smtpemail']		= trim($this->_input['smtpemail']);	//smtp邮件帐户
		$input_param['smtpprot']		= trim($this->_input['smtpprot']);	//smtp端口
		$input_param['mailto']			= trim($this->_input['mailto']);	//显示邮件
		$input_param['smtppass']		= trim($this->_input['smtppass']);	//smtp账户密码

		$input_param['new_user_mail']	= $this->_input['new_user_mail'];	//新会员（邮件是否发送）
		$input_param['buy_goods_mail']	= $this->_input['buy_goods_mail'];	//购买商品（邮件是否发送）
		$input_param['del_goods_mail']	= $this->_input['del_goods_mail'];	//取消购买（邮件是否发送）
		$input_param['pay_mail']		= $this->_input['pay_mail'];		//付款（邮件是否发送）
		$input_param['send_goods_mail']	= $this->_input['send_goods_mail'];	//发货（邮件是否发送）
		$input_param['end_goods_mail']	= $this->_input['end_goods_mail'];	//购买结束（邮件是否发送）

		$error	= $this->obj_admin_system->siteEmail(array_filter($input_param,'htmlspecialchars'));
		if($error) {
			/*日志保存*/
			$log_array				= array();
			$log_array['log_info']	= $this->_lang['admin_system_email_site_mail'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_system_email_site_mail'],$this->_configinfo['websit']['site_url']."/admin/admin_system.php?action=email_site",1,2000);
		}
	}
	/**
	 * 查看邮件模板
	 *
	 */
	private function viewMailTemplate() {
		/*权限检查*/
		$this->checkAdmin('email_site');

		$temp_array		= array('new_user_mail'		=>$this->_lang['admin_system_email_new_user'],
		'buy_goods_mail'	=>$this->_lang['admin_system_email_buy_goods'],
		'del_goods_mail'	=>$this->_lang['admin_system_email_del_goods'],
		'pay_mail'			=>$this->_lang['admin_system_email_pay'],
		'send_goods_mail'	=>$this->_lang['admin_system_email_goods_send'],
		'end_goods_mail'	=>$this->_lang['admin_system_email_end']);

		/*得到邮件模板信息*/
		$email_temp_array	= $this->obj_admin_system->getEmailTemplate(array('mail_template_name'=>"'".trim($this->_input['temp_name'])."'"));
		$this->output('temp_array',$email_temp_array);

		/*fck编辑器*/
		require_once("../classes/resource/fckeditor/fckeditor.php");
		$oFCKeditor = new FCKeditor('template_body') ;
		$sBasePath =  "../classes/resource/fckeditor/";
		$oFCKeditor->Width		= '550' ;
		$oFCKeditor->Height		= '330' ;
		$oFCKeditor->ToolbarSet	= 'Simple' ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Value		= $email_temp_array['mail_template_body'];
		$this->output('txt_template_body',$oFCKeditor->CreateHtml());

		$this->output('template_select',Common::Select('tempalate_name',$temp_array,'',$this->_input['temp_name']));
		$this->showpage('mail_template');
	}
	/**
	 * 保存邮件模板
	 *
	 */
	private function saveEmailTemplates() {
		/*权限检查*/
		$this->checkAdmin('email_site');

		$input_param['mail_template_id']= intval($this->_input['mail_template_id']);
		$input_param['template_body']	= trim($this->_input['template_body']);

		$error	= $this->obj_admin_system->saveTemplateInfo($input_param,$input_param['mail_template_id']);
		if($error) {
			/*日志保存*/
			$log_array				= array();
			$log_array['log_info']	= $this->_lang['admin_email_template_save_ok'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_email_template_save_ok'],$this->refer_url,1,2000);
		} else {
			/*日志保存*/
			$log_array				= array();
			$log_array['log_info']	= $this->_lang['admin_email_template_save_false'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_email_template_save_false'],$this->refer_url,1,2000);
		}
	}
	/**
	 * 系统设置保存
	 * 
	 */
	private function saveSystemFile() {

		$config_type = trim($this->_input['config_type']);
		if(!in_array($config_type,array('base','view'))) return false;
		$config_array	= explode(',',trim($this->_input['config_str']));
		if(is_array($config_array) and count($config_array)>0) {
			foreach ($config_array as $v) {
				if($config_type == 'base') {
					if($v == 'shop_logo_file' and $_FILES['shop_logo_file']['name'] != '') {
						/*上传logo*/
						require_once("uploadfile.class.php");
						$upload = new UploadFile();
						$upload->allow_type = explode(",",$upload->_fileconfig['allowuploadimagetype']);
						$upload->path		= "../data/";
						//$upload->filename	= "logo.jpg";
						$filename = $upload->upfile("shop_logo_file");
						$this->_input[$v] = str_replace('../','',$filename["filename"]);
						if ($this->_input['shop_old_logo_file'] != "") {
							$upload->unfile($this->_input['shop_old_logo_file']);
						}
						unset($upload);
					}
					/*当存在上传logo时，没有新的logo上传，则用之前的logo*/
					if($v == 'shop_logo_file' and $_FILES['shop_logo_file']['name'] == '' and $this->_input['shop_old_logo_file'] != '') {
						$this->_input[$v]	= $this->_input['shop_old_logo_file'];
					}
					
					$input_param[$v]	= $this->_input[$v];
					$array_name		= 'INFO';
					$file_name		= 'config';
				}
				if($config_type == 'view') {
					$input_param[$v]	= $this->_input[$v];
					$input_param[$v."_num"]	= intval($this->_input[$v."_num"]) == 0 ? 0 : trim($this->_input[$v."_num"]);
					$array_name		= $config_type.'_config';
					$file_name		= $config_type.'config';
				}
			}
		}
		
		
		$error = $this->obj_admin_system->writeSystemFile($input_param,$array_name,$file_name);
		if($error) {
			$message	= $config_type == 'base' ? $this->_lang['admin_system_basic_save_ok'] : $this->_lang['admin_system_display_save_ok'];
			/*日志保存*/
			$log_array					= array();
			$log_array['log_info']		= $message;
			$this->obj_log->inLog($log_array);

			$this->adminMessage($message,$this->_configinfo['websit']['site_url']."/admin/admin_system.php?action=".$config_type."_site",1,2000);
		}
	}
	/**
	 * 系统信息列表
	 *
	 */
	private function systemInfoList() {
		/*权限检查*/
		$this->checkAdmin('system_info');

		$info_array	= $this->obj_admin_system->getSystemList();
		$this->output('info_array',$info_array);

		$this->showpage("option_info_list");
	}
	/**
	 * 编辑系统信息
	 *
	 */
	private function editSystemInfo() {
		/*权限检查*/
		$this->checkAdmin('system_info');

		if(intval($this->_input['id']) == 0) {
			return false;
		}
		$info_array	= $this->obj_admin_system->getSystemInfo(array('info_id'=>intval($this->_input['id'])));
		$this->output('info_array',$info_array);

		/*fck编辑器*/
		require_once("../classes/resource/fckeditor/fckeditor.php");
		$oFCKeditor = new FCKeditor('txt_text') ;
		$sBasePath =  "../classes/resource/fckeditor/";
		$oFCKeditor->Width		= '550' ;
		$oFCKeditor->Height		= '330' ;
		$oFCKeditor->ToolbarSet	= 'Simple' ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Value		= $info_array['info_text'];
		$this->output('txt_fck',$oFCKeditor->CreateHtml());

		$this->showpage('option_info_edit');
	}
	/**
	 * 保存系统信息
	 *
	 */
	private function saveSystemInfo() {
		/*权限检查*/
		$this->checkAdmin('system_info');

		$input_param['txt_info_title']	= $this->_input['txt_info_title'];	//信息标题
		$input_param['txt_info_url']	= $this->_input['txt_info_url'];	//信息外联
		$input_param['txt_text']			= $this->_input['txt_text'];	//信息内容

		$error	= $this->obj_admin_system->modifySystemInfo($input_param,intval($this->_input['txt_info_id']));
		if($error) {
			/*日志保存*/
			$log_array				= array();
			$log_array['log_info']	= $this->_lang['admin_system_info_save_true'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_system_info_save_true'],$this->_configinfo['websit']['site_url']."/admin/admin_system.php?action=system_info",1,2000);
		} else {
			/*日志保存*/
			$log_array				= array();
			$log_array['log_info']	= $this->_lang['admin_system_info_save_false'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_system_info_save_false'],$this->_configinfo['websit']['site_url']."/admin/admin_system.php?action=system_info",1,2000);
		}

	}
	/**
	 * 图片设置
	 *
	 */
	private function imageSite() {
		/*权限检查*/
		$this->checkAdmin('image_set');

		$this->output($this->_configinfo['productinfo']);
		$this->showpage('option_image');
	}
	/**
	 * 商品水印设置保存
	 *
	 */
	private function imageSiteSave() {
		/*权限检查*/
		$this->checkAdmin('image_set');

		$watermark_array	= explode(',',$this->_input['watermark_str']);
		/*水印图片上传*/
		if(isset($_FILES['watermark_file']['name']) and $_FILES['watermark_file']['name'] != ''){
			require_once("uploadfile.class.php");
			$upload = new UploadFile();
			$upload->allow_type = explode(",",$upload->_fileconfig['allowuploadimagetype']);
			$upload->path		= "../data/";
			$upload->renamed	= false;
			$filename = $upload->upfile("watermark_file");
			$image_array['watermark_file'] = str_replace('../','',$filename["filename"]);
			if ($this->_input['old_watermark_file'] != "") {
				$upload->unfile($this->_input['old_watermark_file']);
			}
			unset($upload);
		}
		else{
			$image_array['watermark_file'] = $this->_input['old_watermark_file'];
		}

		$error	 = $this->obj_admin_system->siteWatermark($watermark_array,$image_array);
		if($error) {
			/*日志保存*/
			$log_array				= array();
			$log_array['log_info']	= $this->_lang['admin_system_wm_site_ok'];
			$this->obj_log->inLog($log_array);

			$this->adminMessage($this->_lang['admin_system_wm_site_ok'],$this->_configinfo['websit']['site_url']."/admin/admin_system.php?action=system_image",1,1000);
		}
	}
}
$admin_system = new AdminSystem();
$admin_system->main();
unset($admin_system);
?>