<?php
/*******************************************************************
 * 版权协议：2009-2019
 * 版权所有: PHP-北京金南曦网络技术有限公司旗下PHPWMS，PHP商城系统技术支持中心http://www.php188.net，并保留所有权利。 
* PHP商城系统网址: http://www.php188.com
 * PHPWMS系统网址：http://www.phpwms.com
 * 销售电话：010-87875088
 * 销售电话：010-87875188 
*------------------------------------------------------------------
 * 这不是一个完全免费软件！您只能在不用于商业目的的前提下对程序代码进
 * 行修改和使用；不允许对程序代码以任何形式任何目的的再发布。
 *------------------------------------------------------------------
 * 文件名称：admin_deal.php
 * 摘　　要：
 * 适用范围：PHPWMS 1.0
 * 版　　本：1.0
 *------------------------------------------------------------------
 * Script Written By phpwms项目组
 * 最后更新： 2009-09-24 phpwms 由PHP金南曦网络技术有限公司整理发布
 *******************************************************************/
if(!defined('IN_SYS'))exit('access die');
//require_once '../include/classes/cls.pinyin.php';
$type = trim($_REQUEST['type']);
$do	  = trim($_REQUEST['do']);
$data = $_REQUEST['data'];
$id = to_int($_REQUEST['id']);
//用户私有权限
$priv_config = $_POST['group'];
$member_priv_string = '';
if(!empty($priv_config) && is_array($priv_config)){
	foreach ($priv_config as &$p){
		$p = abs(intval(trim($p)));
	}
	$member_priv_string = join('|',$priv_config); 
}
$channel_type = intval($_REQUEST['channel_type']);
switch ($type)
{
	case "info";
		if(!$_POST['table'])
		{
			$tab_add=$_GET['table'];
		}else{
			$tab_add=$_POST['table'];
		}
		/* 删除 缓存 */
		if($tab_add == 'link'){
			php188StaticCache::getInstance()->remove('phpwms_' . $tab_add);
		}
		if($do=='add'){
			$msg = "操作成功！";
            
            if(!$data['post_time'])
            {
                $data['post_time'] = date("Y-m-d H:i:s");
			}
            
			if(!$data['category_id'])
			{
				echo "<script>
					alert('必须选择分类');
					javascript:history.back(-1);
					</script>";
					exit;
			}
			$formname=$_POST['upfile'];
			if($tab_add == 'picture'){
				if(empty($_FILES['file']['name'])){
					$sp->bgmsg('上传文件不能为空', 'admincp.php?action=article_add&channel_type=2&table=picture');
				}
			}elseif ($tab_add == 'download'){
				$data['level'] = intval($_POST['level']);
				require SYS_ROOT . '/include/classes/upload.class.php';
				$file_up = new Upload();
				$file_up->upload_save_path = 'files/upfiles/soft';
				$file_up->upload_maxsize = 1024 * 1024 * 2;
				$file_up->upload_type = array('rar');
				$upmsg = $file_up->up('down_url');
				if(!$upmsg){
					$sp->bgmsg($file_up->getMsg(), 'admincp.php?action=article_add&channel_type=3&table=download');
				}
				$data['down_url'] = $upmsg['upload_part'];
			}
			if ($_FILES['pic']['error'] == 0) {
				$up = $sp -> upFiles('pic',null,true,true);
				$data['pic'] = $up[0]['name'];
			}
			//saveRemoteFiles($text,$upload_dir)
			$data['content'] = saveRemoteFiles($data['content'],'r_pic');
		
			$sql = 'INSERT INTO `'.$tbprefix.$tab_add.'`'.$db ->sql_build_array('INSERT',$data);
	
			$re  = $db -> sql_query($sql);
           
            $id=$db -> sql_nextid();
             
            jd_path($tab_add,$id);//静态处理
         
			$db -> sql_freeresult($re);
             
			$db -> sql_close();
            
			$sp -> bgmsg($msg,$_SERVER["HTTP_REFERER"]);
			
		}elseif($do=='change'){
		
			$msg = "操作成功！";
			$sql ="UPDATE `".$tbprefix.$tab_add."` SET `state` = '".$_GET['state']."' WHERE `id` =".$_GET['id'];
			$re  = $db -> sql_query($sql);
			$db -> sql_freeresult($re);
			$db -> sql_close();
			$sp -> bgmsg($msg,$_SERVER["HTTP_REFERER"]);
			
		}elseif($do=='edit'){
			$msg = "操作成功！";
			$formname=$_POST['upfile'];
			
			for($l=0;$l<count($formname);$l++) {
				$form_name = $formname[$l];
				if ($_FILES[$form_name]['error'] == 0) {
					$up = $sp -> upFiles($form_name,null,true,true);
					$up[0]['name'] and $data[$form_name] = $up[0]['name'];
				}
			}
			$data['content'] = saveRemoteFiles($data['content'],'r_pic');
			$sql = 'UPDATE '.$tbprefix.$tab_add.' SET '.$db -> sql_build_array('UPDATE', $data).' WHERE id = '.$_GET['id'];
			$re  = $db -> sql_query($sql);
            
            jd_path($tab_add,$_GET['id']);//静态处理
            
			$db -> sql_freeresult($re);
            
			$db -> sql_close();
            
			$sp -> bgmsg($msg,$_SERVER["HTTP_REFERER"]);
            
		}elseif($do=='del'){
			$msg = "操作成功！";
			
			$sqls= 'SELECT * FROM '.$tbprefix.$tab_add.' WHERE id = '.$_GET['id'];
			$ree  = $db -> sql_query($sqls);
			$arrc = $db -> sql_fetchrowset($ree);
			
			foreach($table_config[$tab_add]['attribute'] as $key_ =>$value_)
			{
				if($value_==5)
				{
					utterly($arrc[0][$key_],true);
				}
			}
			$p=$arrc[0]['url'];
            utterly('/'.$p,true);
			$sql ="delete from `".$tbprefix.$tab_add."` WHERE `id` =".$_GET['id'];
			$re  = $db -> sql_query($sql);
			$db -> sql_freeresult($re);
			$db -> sql_close();
			$sp -> bgmsg($msg,$_SERVER["HTTP_REFERER"]);
		}
		break;
	case 'db':
		if(!class_exists('DbOperate'))require SYS_ROOT.'include/classes/cls.db_operate.php';
		$dbo = new DbOperate();
		if ($do == 'backup') {
			if (!getAdminPri('9001'))exit(NO_AUTH_LOGIN);
			$tb_arr   = $_REQUEST['tb_arr'];
			$tablesel = trim($_GET['tablesel']);
			$sizelimit	= isset($_POST['sizelimit'])?(int)$_POST['sizelimit']:(int)$_GET['sizelimit'];
			$start		= (int)$_GET['start'];
			$step		= (int)$_GET['step'];
			$tbid		= (int)$_GET['tbid'];
			$rnd		= $_GET['rnd'];
			$rows		= $_GET['rows'];

			$topstr = "# PHPWMS Backup SQL File\n# Version:".SYS_VERSION."\n# Time: ".date("Y-m-d H:i:s")."\n# 官方网站: http://www.phpwms.com\n# --------------------------------------------------------\n\n";
			$db -> sql_query("SET SQL_QUOTE_SHOW_CREATE = 0");
			!$tb_arr && !$tablesel && die($sp -> bgmsg('你没有选择任何数据表！',"admincp.php?action=".$type."&do=".$do));
			!$tb_arr && $tb_arr=explode("|",$tablesel);
			!$step && $sizelimit/=2;
			$bakupdata = $dbo -> bakupData($tb_arr,$start);
			if(!$step){
				!$tb_arr && die($sp -> bgmsg('你没有选择任何数据表！',"admincp.php?action=".$type."&do=".$do));
				$tablesel = implode("|",$tb_arr);
				$step  = 1;
				$rnd=random(9).date("s");
				$start = 0;
				$bakuptable = $dbo -> bakupTable($tb_arr);
			}
			
			!$step && $sizelimit/=2;

			$f_num = ceil($step/2);
			
			$filename = 'PHPWMS_'.date("md").'_'.$rnd.'_'.$f_num.'.sql';

			$step++;
			$writedata = $bakuptable ? $bakuptable.$bakupdata : $bakupdata;
			$writedata = $topstr.$writedata;

			$t_name	= $tb_arr[$tbid-1];
			$c_n	= $startfrom;

			if ($stop == 1) {
				$files = $step-1;
				trim($writedata) && writefile(SYS_ROOT.'data/db_backup/'.$filename,$writedata,'ab');
				$sp -> bgmsg("正在备份<font color='blue'>{$t_name}</font>表，共<font color='blue'>{$rows}</font>条记录，<br>已经备份至<font color='blue'>{$c_n}</font>条记录,已生成<font color='blue'>{$f_num}</font>个备份文件！","admincp.php?action=deal&type=".$type."&do=".$do."&start={$startfrom}&tbid={$tbid}&sizelimit={$sizelimit}&step={$step}&rnd={$rnd}&tablesel={$tablesel}&rows={$rows}",0);
			} else{
				trim($writedata) && writefile(SYS_ROOT.'data/db_backup/'.$filename,$writedata,'ab');		
				if($step>1){
					for($i=1;$i<=$f_num;$i++){
						$temp=substr($filename,0,19).$i.".sql";
						if(file_exists(SYS_ROOT.'data/db_backup/'.$temp)){
							$bakfile.='<a href="'.SYS_ROOT.'data/db_backup/'.$temp.'">'.$temp.'</a><br>';
						}
					}
				}
				$sp -> bgmsg("已全部备份完成,备份文件保存在data\db_backup目录下","admincp.php?action=".$type."&do=recover");
			}
			
		}

		if ($do == 'repair') {
			if (!getAdminPri('9003'))exit(NO_AUTH_LOGIN);
			$tb_arr = $_REQUEST['tb_arr'];
			!$tb_arr and die($sp -> bgmsg('你没有选择任何数据表！',"admincp.php?action=".$type."&do=".$do));
			$table = implode(',',$tb_arr);
			$res = $db -> sql_query("REPAIR TABLE $table "); //EXTENDED
			$arr = $db -> sql_fetchrowset($res);
			$msg = '';
			foreach ($arr as $k => $v) {
				$msg .= '<ul style="clear:both;width:100%;text-align:left;font-size:12px;"><li style="float:left;width:220px;">表：<font color=blue>'.$v['Table'].'</font></li> <li style="float:left;width:120px;">操作：<font color=blue>'.$v['Op'].'</font></li> <li style="float:left;width:120px;">状态：'.($v['Msg_text']=='OK'?'<font color=green>'.$v['Msg_text'].'</font>':'<font color=red>'.$v['Msg_text'].'</font>').'</li> </ul>';
			}
			$sp -> bgmsg($msg,"admincp.php?action=".$type."&do=".$do,10000);
		}

		if ($do == 'optimize') {
			if (!getAdminPri('9003'))exit(NO_AUTH_LOGIN);
			$tb_arr = $_REQUEST['tb_arr'];
			!$tb_arr and die($sp -> bgmsg('你没有选择任何数据表！',"admincp.php?action=".$type."&do=".$do));
			$table = implode(',',$tb_arr);
			$res = $db -> sql_query("OPTIMIZE TABLE $table "); //EXTENDED
			$arr = $db -> sql_fetchrowset($res);
			$msg = '';
			foreach ($arr as $k => $v) {
				$msg .= '<ul style="clear:both;width:100%;text-align:left;font-size:12px;"><li style="float:left;width:220px;">表：<font color=blue>'.$v['Table'].'</font></li> <li style="float:left;width:120px;">操作：<font color=blue>'.$v['Op'].'</font></li> <li style="float:left;width:60px;">状态：'.($v['Msg_text']=='OK'?'<font color=green>'.$v['Msg_text'].'</font>':'<font color=red>'.$v['Msg_text'].'</font>').'</li> </ul>';
			}
			$sp -> bgmsg($msg,"admincp.php?action=".$type."&do=".$do,10000);
		}

		if ($do == 'recover') {
			if (!getAdminPri('9002'))exit(NO_AUTH_LOGIN);
			$pre = trim($_GET['pre']);
			!$pre and die($sp -> bgmsg('参数错误，请勿手动更改参数！',"admincp.php?action=".$type."&do=recover"));
			$step  = $_GET['step'];
			!$step  and $step  = 1;
			$res = $dbo -> getSqlFile($pre);
			//!$res and die($sp -> bgmsg('备份文件丟失！',"admincp.php?action=".$type."&do=recover"));
			$count = intval(count($res));
			if ($step <= $count) {
				$dbo -> bakInData(SYS_ROOT.'data/db_backup/'.$res[$step-1]['name']);
				$step++;
				$sp -> bgmsg("正在导入第<font color='blue'>{$i}</font>卷备份文件，程序将自动导入余下备份文件...","admincp.php?action=deal&type=db&do=recover&pre=".$pre."step=".$step,0);				
			} else {
				die($sp -> bgmsg('导入备份数据成功！',"admincp.php?action=".$type."&do=recover"));
			}
			
		}

		if ($do == 'del') {
			$del = $_REQUEST['delete'];
			!$del and die($sp -> bgmsg('你没有选择任何数据文件！',"admincp.php?action=".$type."&do=recover"));
			$dbo -> delBackup($del);
			die($sp -> bgmsg('执行操作完成，如果文件未被删除，请检查目录权限！',"admincp.php?action=".$type."&do=recover"));
		}

		
	break;
}

?>
