<?php
require '../inc/zzz_admin.php';
check_admin('die');
$act = safe_word(getform('act', 'get', 'nul'));
$id = safe_key(getform('id', 'both'));
$table = safe_word(getform('table', 'post'));
$type = safe_word(getform('type', 'both'));
$limit = safe_word(getform('limit', 'post'));
$offset = safe_word(getform('offset', 'post'));
$sort = safe_key(getform('sort', 'post'));
$order = safe_word(getform('order', 'post'));
$GLOBALS['search'] = $search = getform('search', 'post');
$page = $offset > 0 ?  $offset / $limit + 1 : 0;
switch ($act) {
	case 'aboutlist':
		echo aboutlist();
		break;
	case 'adlist':
		echo adlist();
		break;
	case 'brandlist':
		echo brandlist();
		break;
	case 'contentlist':
		echo contentlist();
		break;
	case 'customlist':
		echo customlist();
		break;
	case 'linkslist':
		echo linkslist();
		break;
	case 'gbooklist':
		echo gbooklist();
		break;
	case 'taglist':
		echo taglist();
		break;
	case 'sortlist':
		echo sortlist($id);
		break;
	case 'recylist':
		echo recylist();
		break;
	case 'slidelist':
		echo slidelist();
		break;
	case 'modellist':
		echo modellist();
		break;
	case 'labelslist':
		echo labelslist();
		break;
	case 'loglist':
		echo loglist();
		break;
	case 'userlist':
		echo userlist();
		break;
	case 'usergroup':
		echo usergroup();
		break;
	case 'adminlist':
		echo adminlist();
		break;
	case 'admingroup':
		echo admingroup();
		break;
	case 'regionlist':
		echo regionlist();
		break;
	case 'imagelist':
		echo imagelist();
		break;
	case 'downlist':
		echo downlist();
		break;
	case 'templatelist':
		echo filelist('template');
		break;
	case 'uploadlist':
		echo filelist('upload');
		break;
	case 'htmllist':
		echo filelist('html');
		break;
	case 'cachelist':
		echo filelist('cache');
		break;
	case 'databackup':
		echo filelist('backup');
		break;
	case 'dbbacklist':
		echo filelist('data');
		break;
	case 'loginlist':
		echo filelist('login');
		break;
	case 'dbsqllist':
		echo filelist('log');
		break;
	case 'errlist':
		echo filelist('error');
		break;
	case 'selectsort':
		echo select_sort_content($type, $id);
		break;
	case 'selectcustom':
		echo select_custom($type);
		break;
	case 'pinyin':
		echo pinyin($sort, '-');
		break;
	case 'checkname':
		echo db_count('content', "c_pagename='" . $sort . "'");
		break;
	case 'gettag':
		echo (tojson(db_load('tag', array('t_onoff' => 1), 't_name')));
		break;
}
function select_custom($type)
{
	$r = "<option value='c_addtime'>创建时间</option>
		<option value='c_title'>内容标题</option>
		<option value='c_title2'>短标题</option>
		<option value='c_content'>内容</option>
		<option value='c_onoff'>状态(0禁用,1启用)</option>
		<option value='istop'>置顶(0取消,1启用)</option>
		<option value='isgood'>推荐(0取消,1启用)</option>
		<option value='c_visits'>访问量</option>
		<option value='c_tag'>标签</option>
		<option value='c_gid'>会员组(0,4-8)</option>
		<option value='c_brand'>品牌</option>
		<option value='c_pagekey'>关键词</option>
		<option value='c_pagedesc'>描述</option>";
	$data = db_load('content_custom', ['customonoff' => 1, 'customtype' => ['LIKE' => $type]]);
	foreach ($data as $v) {
		$r .= "<option value='" . $v['custom'] . "'>" . $v['customname'] . "</option>";
	}
	return $r;
}
function aboutlist()
{
	$arr = array(); global $ai_conf;
	$where = '';
	$i = 0;
	$sid = safe_key(getform('sid', 'get'));
	if ($sid)  $where = 'where a_sid=' . $sid;
	$data = db_load_sql("select aid,a_name,a_sid,a_enname,a_visits,a_order,a_onoff,a_addtime,a_edittime,(select s_name from [dbpre]sort where sid=a_sid) as sname,(select count(*) from [dbpre]sort where sid=a_sid and s_type='about') as num from [dbpre]about " . $where . " order by a_order asc, aid asc");
	foreach ($data as $value) {
		$i = $value['aid'];
		if ($value['num'] > 0) {
			$ai_edit= $ai_conf['enable_ai']=="1" ?"<button type='button' class='btn btn-success dim' onClick=\"opennew('?act=ai&type=polish&table=about&id=".$i."')\"> <i class='fa fa-paint-brush'></i> AI 润色 </button>":"";
			array_push($arr, array(
				'id' => $i,
				'sid' => $value['a_sid'],
				'sname' => $value['sname'] . '<a href=' . getsortlink('about', $value['a_sid'], '') . ' target="_blank"><i class="fa fa-external-link"></i></a>',
				'a_name' => '<input type="text" class="title" id="a_name-' . $i . '" placeholder="' . $value['a_name'] . '" value="' . $value['a_name'] . '"> ',
				'a_enname' => '<input type="text" class="title" id="a_enname-' . $i . '" placeholder="' . $value['a_enname'] . '" value="' . $value['a_enname'] . '">',
				'a_order' => '<input type="number" class="order" id="a_order-' . $i . '" placeholder="' . $value['a_order'] . '" value="' . $value['a_order'] . '">',
				'a_onoff' => $value['a_onoff'] == 1 ? 'ON' : 'OFF',
				'edit' => '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=about&aid=' .$i. '\')"><i class="fa fa-pencil"></i> 编辑</button> ' . $ai_edit,
			));
		}
	}
	return tojson($arr);
}

function sortlist($pid){
	if(!$pid) $pid=0;
	$arr = array();
	$count	=	db_count("sort");
	$data = db_load_sql("select t.*,model_type,model_name,model_table,(select count(*) from [dbpre]sort where s_pid=t.sid and (s_onoff=0 or s_onoff=1)) as c from [dbpre]model as a,[dbpre]sort t where s_type=model_type and (s_onoff=0 or s_onoff=1) and s_pid=$pid order by s_order asc , sid asc");
	foreach ($data as $value) {
		$value = array_change_key_case($value);
		if ($count > conf('sortsize') && $value['c'] > 0) {
			$open = '<a class="open" data-id="' . $value['sid'] . '"  data-level="' . $value['s_level'] . '" id="tid' . $value['sid'] . '"><i class="S_Level Level1"></i></a>';
		} elseif ($value['c'] > 0) {
			$open = '<i class="S_Level onLevel' . $value['s_level'] . '" title="' . $value['s_level'] . '级分类"></i>';
		} else {
			$open = '<i class="S_Level Level' . $value['s_level'] . '" title="' . $value['s_level'] . '级分类"></i>';
		}
		$i = $value['sid'];
		$stype = $value['s_type'];
		$sortlink = ' <a href=' . getsortlink($value['s_type'], $i, $value['s_filename'], $value['s_url']) . ' target="_blank"><i class="fa fa-external-link"></i></a>';
		if ($value['s_onoff'] == 1) {
			$edit = '<button type="button" class="btn btn-success dim" onclick=opennew(\'?act=sort&pid=' . $value['sid'] . '\')><i class="fa fa-plus"></i></button> <button type="button" class="btn btn-info dim" onclick="opennew(\'?act=sort&pid=' . $value['s_pid'] . '&sid=' . $value['sid'] . '\')"><i class="fa fa-pencil"></i></button> ';
		} else {
			$edit = '';
		}
		if ($value['s_type'] == 'links') {
			$surl = '<input type="text" class="title" id="s_url-' . $i . '" placeholder="' . $value['s_url'] . '" value="' . $value['s_url'] . '">';
		} else {
			$surl = '<input type="text" class="title" id="s_filename-' . $i . '" placeholder="' . $value['s_filename'] . '" value="' . $value['s_filename'] . '">';
		}
		$del = conf("isdel") == 0 ? '<button type="button" class="btn btn-warning dim" onclick="removesort(' . $i . ')"><i class="fa fa-trash"></i></button>' : '<button type="button" class="btn btn-danger dim" onclick="delsort(' . $i . ',\'sort\')"><i class="fa fa-times"></i></button> ';
		array_push($arr, array(
			'id' => $value['sid'],
			'num' => $open,
			's_name' => '<input type="text" class="title" id="s_name-' . $i . '" placeholder="' . $value['s_name'] . '" value="' . $value['s_name'] . '">',
			's_enname' => '<input type="text" class="title" id="s_enname-' . $i . '" placeholder="' . $value['s_enname'] . '" value="' . $value['s_enname'] . '">',
			'model_name' => $value['model_name'] . $sortlink,
			's_url' => $surl,
			's_order' => '<input type="number" class="order" id="s_order-' . $i . '" placeholder="' . $value['s_order'] . '" value="' . $value['s_order'] . '">',
			's_onoff' => '<button type="button" class="btn btn-' . $value['s_onoff'] . ' dim" onclick=setcol("s_onoff","' . abs($value['s_onoff'] - 1) . '","' . $i . '") id="' . $i . 's_onoff"  ><i class="fa fa-toggle-on"></i></button>',
			'edit' => $edit . $del
		));

		if ($count < conf('sortsize') && $value['c'] > 0) $arr = subsort($arr, $value['sid']);
	}
	//echop($arr);
	return tojson($arr);
}
function subsort($arr, $pid)
{
	$data = db_load_sql("select t.*,model_type,model_name,model_table,(select count(*) from [dbpre]sort where s_pid=t.sid  and (s_onoff=0 or s_onoff=1) ) as c from [dbpre]model as a,[dbpre]sort t where s_type=model_type  and (s_onoff=0 or s_onoff=1) and s_pid=$pid order by s_order asc , sid asc");
	foreach ($data as $key => $value) {
		$value = array_change_key_case($value);
		if ($value['c'] > 0) {
			$open = '<i class="S_Level onLevel' . $value['s_level'] . '" title="' . $value['s_level'] . '级分类">Parent1</i></a>';
		} else {
			if ($key == (count($data) - 1)) {
				$open = '<i class="S_Level Levelend Level' . $value['s_level'] . '" title="' . $value['s_level'] . '级分类">Parent1</i>';
			} else {
				$open = '<i class="S_Level Level' . $value['s_level'] . '" title="' . $value['s_level'] . '级分类">Parent1</i>';
			}
		}
		$i = $value['sid'];
		$stype = $value['s_type'];
		$sortlink = ' <a href=' . getsortlink($value['s_type'], $i, $value['s_filename'], $value['s_url']) . ' target="_blank"><i class="fa fa-external-link"></i></a>';
		if ($value['s_onoff'] == 1) {
			$edit = '<button type="button" class="btn btn-success dim" onclick=opennew(\'?act=sort&pid=' . $value['sid'] . '&sid=0\')><i class="fa fa-plus"></i></button> 
        <button type="button" class="btn btn-info dim" onclick="opennew(\'?act=sort&pid=' . $value['s_pid'] . '&sid=' . $value['sid'] . '\')"><i class="fa fa-pencil"></i></button> ';
		} else {
			$edit = '';
		}
		if ($value['s_type'] == 'links') {
			$surl = '<input type="text" class="title" id="s_url-' . $i . '" placeholder="' . $value['s_url'] . '" value="' . $value['s_url'] . '">';
		} else {
			$surl = '<input type="text" class="title" id="s_filename-' . $i . '" placeholder="' . $value['s_filename'] . '" value="' . $value['s_filename'] . '">';
		}
		array_push($arr, array(
			'id' => $value['sid'],
			'num' => $open,
			's_name' => '<input type="text" class="title" id="s_name-' . $i . '" placeholder="' . $value['s_name'] . '" value="' . $value['s_name'] . '">',
			's_enname' => '<input type="text" class="title" id="s_enname-' . $i . '" placeholder="' . $value['s_enname'] . '" value="' . $value['s_enname'] . '">',
			'model_name' => $value['model_name'] . $sortlink,
			's_url' => $surl,
			's_order' => '<input type="number" class="order" id="s_order-' . $i . '" placeholder="' . $value['s_order'] . '" value="' . $value['s_order'] . '">',
			's_onoff' => '<button type="button" class="btn btn-' . $value['s_onoff'] . ' dim" onclick=setcol("s_onoff","' . abs($value['s_onoff'] - 1) . '","' . $i . '") id="' . $i . 's_onoff" ><i class="fa fa-toggle-on"></i></button>',
			'edit' => conf("isdel") == 0 ? $edit . '<button type="button" class="btn btn-warning dim" onclick="removesort(' . $i . ')"><i class="fa fa-trash"></i></button>' : $edit . '<button type="button" class="btn btn-danger dim" onclick="delsort(' . $i . ',\'sort\')"><i class="fa fa-times"></i></button> '
		));
		if ($value['c'] > 0)  $arr = subsort($arr, $value['sid']);
	}
	return ($arr);
}

function adlist()
{
	$arr = array();
	$where = '';
	$i = 0;
	$data = db_load("ad", $where, "adid,adname,adclass,adlink,adetime,adonoff,adstyle");
	foreach ($data as $value) {
		$i = $value['adid'];
		array_push($arr, array(
			'id' => $value['adid'],
			'ad' => "{zzz:ad" . $value['adid'] . "}",
			'adname' => '<input type="text" class="title" id="adname-' . $i . '" placeholder="' . $value['adname'] . '" value="' . $value['adname'] . '">',
			'adclass' => ad_name($value['adclass']),
			'adlink' => '<input type="text" class="title" id="adlink-' . $i . '" placeholder="' . $value['adlink'] . '" value="' . $value['adlink'] . '">',
			'adstime' => $value['adstime'],
			'adetime' => $value['adetime'],
			'adonoff' => "<button type='button'  onclick=setcol('adonoff'," . abs($value['adonoff'] - 1) . ",'" . $i . "')  id='" . $i . "adonoff' class='btn btn-" . $value["adonoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=ad&id=' . $i . '\')"><i class="fa fa-pencil"></i></button> <button type="button" class="btn btn-danger dim" onclick="delid(' . $i . ',\'ad\')"><i class="fa fa-times"></i></button>'
		));
	}
	return tojson($arr);
}

function brandlist()
{
	$arr = array(); global $ai_conf;
	$i = 0;
	$data = db_load("brand", '', 'bid,b_name,b_enname,b_type,b_filename,b_visits,b_order,b_onoff,b_addtime,b_edittime');
	foreach ($data as $value) {
		$i = $value['bid'];
		$ai_edit= $ai_conf['enable_ai']=="1" ?"<button type='button' class='btn btn-success dim' onClick=\"opennew('?act=ai&type=polish&table=brand&id=".$i."')\"> <i class='fa fa-paint-brush'></i> AI 润色 </button>":"";
		array_push($arr, array(
			'id' => $value['bid'],
			'b_name' => '<input type="text" class="title" id="b_name-' . $i . '" placeholder="' . $value['b_name'] . '" value="' . $value['b_name'] . '">',
			'b_enname' => '<input type="text" class="title" id="b_enname-' . $i . '" placeholder="' . $value['b_enname'] . '" value="' . $value['b_enname'] . '">',
			'b_type' => '<input type="text" class="title" id="b_type-' . $i . '" placeholder="' . $value['b_type'] . '" value="' . $value['b_type'] . '">',
			'b_filename' => '<input type="text" class="name" id="b_filename-' . $i . '" placeholder="' . $value['b_filename'] . '" value="' . $value['b_filename'] . '">
			<a href=' . getbrandlink('', $value['bid'], $value['b_filename']) . ' target="_blank"><i class="fa fa-external-link"></i></a>',
			'b_order' => '<input type="number" class="order" id="b_order-' . $i . '" placeholder="' . $value['b_order'] . '" value="' . $value['b_order'] . '">',
			'b_onoff' => "<button type='button'  onclick=setcol('b_onoff'," . abs($value['b_onoff'] - 1) . ",'" . $i . "')  id='" . $i . "b_onoff' class='btn btn-" . $value["b_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=brand&id=' . $i . '\')"><i class="fa fa-pencil"></i> 编辑</button> <button type="button" class="btn btn-danger dim" onclick="delid(' . $i . ',\'brand\')"><i class="fa fa-times"></i> 删除</button> ' . $ai_edit,
		));
	}
	return tojson($arr);
}

function contentlist()
{
	$arr = array();
	$i = 0;
	$where = array();
	$sort = G('sort');
	$search = G('search');
	$limit = G('limit');
	$page = G('page');
	$order = G('order');
	$sid = G('id');
	$type = G('type');
	switch ($sort) {
		case 'id':
			$orderby = array('cid' => $order);
			break;
		case '':
			$orderby = array('cid' => 'desc');
			break;
		default:
			$orderby = array($sort => $order, 'cid' => 'desc');
	}
	if (!$sort)		$orderby = array('istop' => 1, 'isgood' => 1, 'c_order' => 'asc', 'c_addtime' => 'desc', 'cid' => 'desc');
	if ($type)		$where = array('c_onoff' => array(0, 1), 'c_type' => $type);
	if ($sid)		$where = array('c_onoff' => array(0, 1), 'c_sid' => db_subsort($sid));
	if ($search)	$where = array('c_onoff' => array(0, 1), 'c_title' => array('LIKE' => $search));
	if (!$where)	return false;
	$count	=	db_count("content", $where);
	arr_add($where, 'sid', array('=' => 'c_sid'));
	$data	=	db_load("content c,sort s", $where, 'c.*,s.s_filename,s.s_url,s.s_name,s.s_enname', $limit, $orderby, $page);
	foreach ($data as $value) {
		$value = array_change_key_case($value);
		$value['ispic'] == 1 ? $picstr = ' <a onclick="$.fancybox([\'' . repkong(str_replace(",", "','", $value['c_picsurl'])) . '\']);" class="lightbox"><i class="fa fa-photo"></i></a>' : $picstr = '';
		$contentlink = ' <a href="' . getcontentlink($value['cid'], $value['c_pagename'], $value['c_type'], $value['c_link']) . '" target="_blank"><i class="fa fa-external-link"></i></a>';
		$i = $value['cid'];
		$price = isset($value['zprice']) ? $value['zprice'] : 0;
		$edit = '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=content&cid=' . $i . '\')"><i class="fa fa-pencil"></i></button>';
		array_push($arr, array(
			'id' => $i,
			'link' => $picstr . $contentlink,
			'c_title' => '<input type="text" class="title" id="c_title-' . $i . '" placeholder="' . $value['c_title'] . '" value="' . $value['c_title'] . '">',
			'c_sid' => '<span id="sid' . $value['c_sid'] . '">' . $value['s_name'] . '</span>',
			'c_addtime' => '<a onclick="setjeDate(this,' . $i . ');" title="双击可修改">' . $value['c_addtime'] . '</a>',
			'zprice' => '<input type="number" class="order" id="zprice-' . $i . '" placeholder="' . $price . '" value="' . $price . '">',
			'c_order' => '<input type="number" class="order" id="c_order-' . $i . '" placeholder="' . $value['c_order'] . '" value="' . $value['c_order'] . '">',
			'c_onoff' => '<button type="button" onclick=setcol("c_onoff",' . abs($value['c_onoff'] - 1) . ',"' . $i . '") id="' . $i . 'c_onoff" class="btn btn-' . $value['c_onoff'] . '  dim"><i class="fa fa-toggle-on"></i></button>',
			'istop' =>
			'<button type="button"  onclick=setcol("istop",' . abs($value['istop'] - 1) . ',"' . $i . '") id="' . $i . 'istop" class="btn btn-' . $value['istop'] . ' dim"><i class="fa fa-thumb-tack"></i></button>' .
				'<button type="button"  onclick=setcol("isgood",' . abs($value['isgood'] - 1) . ',"' . $i . '") id="' . $i . 'isgood" class="btn btn-' . $value['isgood'] . ' dim"><i class="fa fa-thumbs-up"></i></button>'.
				'<button type="button"  onclick=setcol("isoffer",' . abs($value['isoffer'] - 1) . ',"' . $i . '") id="' . $i . 'isoffer" class="btn btn-' . $value['isoffer'] . ' dim"><i class="fa fa-tag"></i></button>',
			'edit' => conf("isdel") == 0 ? $edit . '<button type="button" class="btn btn-warning dim" onclick="remove(' . $i . ',\'content\')"><i class="fa fa-trash"></i></button>' : $edit . '<button type="button" class="btn btn-danger dim" onclick="delid(' . $i . ',\'content\')"><i class="fa fa-times"></i></button>'
		));
	}
	return '{"total":' . $count . ',"rows":' . tojson($arr) . '}';
}

function recylist()
{
	$arr = array();
	$i = 0;
	$type = G('type');
	if ($type == 'content') {
		$data	=	db_load($type, array('c_onoff' => 2), 'cid,c_title,c_type,c_addtime');
	} elseif ($type == 'sort') {
		$data	=	db_load($type, array('s_onoff' => 2), 'sid,s_name,s_type,s_addtime');
	}
	if (isset($data)) {
		$count = count($data);
		foreach (array_values($data) as $value) {
			$value = array_values($value);
			array_push($arr, array(
				'id' => $value[0],
				'title' => $value[1],
				'type' => $value[2],
				'time' => $value[3],
				'edit' => '<button type="button" class="btn btn-primary dim" onclick="recovery(' . $value[0] . ',\'' . $type . '\')"><i class="fa fa-undo"></i></button> <button type="button" class="btn btn-danger dim" onclick="delid(' . $value[0] . ',\'' . $type . '\')"><i class="fa fa-remove"></i></button> '
			));
		}
		return tojson($arr);
	}
}

function linkslist()
{
	$arr = array();
	$i = 0;
	$data = db_load("links", '', 'lid,l_name,l_cid,l_type,l_desc,l_pic,l_url,l_order,l_onoff');
	foreach ($data as $value) {
		$i = $value['lid'];
		array_push($arr, array(
			'id' => $value['lid'],
			'l_name' => '<input type="text" class="title" id="l_name-' . $i . '" placeholder="' . $value['l_name'] . '" value="' . $value['l_name'] . '">',
			'l_cid' => '<input type="text" class="title" id="l_cid-' . $i . '" placeholder="' . $value['l_cid'] . '" value="' . $value['l_cid'] . '">',
			'l_type' => '<input type="text" class="title" id="l_type-' . $i . '" placeholder="' . $value['l_type'] . '" value="' . $value['l_type'] . '">',
			'l_pic' => '<input type="text" class="title" id="l_pic-' . $i . '" placeholder="' . $value['l_pic'] . '" value="' . $value['l_pic'] . '">',
			'l_url' => '<input type="text" class="title" id="l_url-' . $i . '" placeholder="' . $value['l_url'] . '" value="' . $value['l_url'] . '">',
			'l_order' => '<input type="number" class="order" id="l_order-' . $i . '" placeholder="' . $value['l_order'] . '" value="' . $value['l_order'] . '">',
			'l_onoff' => '<button type="button"  onclick=setcol("l_onoff",' . abs($value['l_onoff'] - 1) . ',' . $i . ')  id="' . $i . '.l_onoff"  class="btn btn-' . $value["l_onoff"] . ' dim"><i class="fa fa-toggle-on"></i></button>',
			'edit' => '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=links&id=' . $value['lid'] . '\')"><i class="fa fa-pencil"></i></button> <button type="button" class="btn btn-danger dim" onclick="delid(' . $value['lid'] . ',\'links\')"><i class="fa fa-times"></i></button>'
		));
	}
	return tojson($arr);
}

function taglist()
{
	$arr = array();
	$i = 0;
	$data = db_load_sql("select tid,t_name,t_enname,t_type,t_visits,t_onoff,t_order,t_addtime,t_edittime from [dbpre]tag");
	foreach ($data as $value) {
		$i = $value['tid'];
		array_push($arr, array(
			'id' => $value['tid'],
			't_name' => $value['t_name'],
			't_enname' => '<input type="text" class="title" id="t_enname-' . $i . '" placeholder="' . $value['t_enname'] . '" value="' . $value['t_enname'] . '">',
			't_count' => db_count('content', array('c_tag' => array('LIKE' => $value['t_name']))),
			't_visits' => '<input type="text" class="title" id="t_visits-' . $i . '" placeholder="' . $value['t_visits'] . '" value="' . $value['t_visits'] . '">',
			't_order' => '<input type="number" class="order" id="t_order-' . $i . '" placeholder="' . $value['t_order'] . '" value="' . $value['t_order'] . '">',
			't_onoff' => "<button type='button'  onclick=setcol('t_onoff'," . abs($value['t_onoff'] - 1) . ",'" . $i . "') id='" . $i . "t_onoff' class='btn btn-" . $value["t_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=tag&id=' . $value['tid'] . '\')"><i class="fa fa-pencil"></i></button> <button type="button" class="btn btn-danger dim" onclick="delid(' . $value['tid'] . ',\'tag\')"><i class="fa fa-times"></i></button>'
		));
	}
	return tojson($arr);
}

function labelslist(){
	$arr = array(); global $ai_conf;
	$data = db_load('labels', '', 'labelid,label_title,label_entitle,label_name,label_onoff,label_order,label_pic');
	foreach ($data as $value) {
		$i = $value['labelid'];		
		$ai_edit= $ai_conf['enable_ai']=="1" ?"<button type='button' class='btn btn-success dim' onClick=\"opennew('?act=ai&type=polish&table=labels&id=".$i."')\"> <i class='fa fa-paint-brush'></i> AI 润色 </button>":"";
		array_push($arr, array(
			'id' => $i,
			'label_title' => "<input type='text' class='title' id='label_title-" . $i . "' placeholder='" . $value["label_title"] . "'  value='" . $value["label_title"] . "'>",
			'label_entitle' => "<input type='text' class='title' id='label_entitle-" . $i . "' placeholder='" . $value["label_entitle"] . "'  value='" . $value["label_entitle"] . "'>",
			'label_name' => "<input type='text' class='title' id='label_name-" . $i . "' placeholder='" . $value["label_name"] . "'  value='" . $value["label_name"] . "'>",
			'label_pic' => "<input type='text' class='title'  id='label_pic-" . $i . "' placeholder='" . $value["label_pic"] . "'  value='" . $value["label_pic"] . "'>",
			'label_order' => "<input type='number' class='order' id='label_order-" . $i . "' placeholder='" . $value["label_order"] . "'  value='" . $value["label_order"] . "'>",
			'label_onoff' => "<button type='button'  onclick=setcol('label_onoff'," . abs($value['label_onoff'] - 1) . ",'" . $i . "') id='" . $i . "label_onoff' class='btn btn-" . $value["label_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('?act=labels&id=" . $i . "')><i class='fa fa-pencil'></i> 编辑</button> <button type='button'  onclick=delid('" . $i . "','labels')  class='btn btn-danger dim'><i class='fa fa-times'></i> 删除</button> ".$ai_edit,

		));
	}
	return tojson($arr);
}

function regionlist()
{
	$arr = array();
	$i = 0;
	$where = '';
	$sort = G('sort');
	$search = G('search');
	$limit = G('limit');
	$page = G('page');
	$order = G('order');
	$type = G('type');
	$pid = safe_key(getform('pid', 'get'));
	if ($sort) {
		$orderby = array($sort => $order);
	} else {
		$orderby = ['id' => 'asc'];
	}
	if ($pid > 1000) {
		$sub = '';
		$where = ['pid' => $pid];
	} else if ($pid) {
		$sub = '下级区县';
		$where = ['pid' => $pid];
	} else {
		$sub = '下级市';
		$where = ['pid' => 0];
	}

	if ($search) {
		$where .= " and (title like '%" . $search . "%' or zipcode= '" . $search . "' or telcode= '" . $search . "' or zonecode= '" . $search . "')";
	}
	$data = db_load('region', $where, '*', $limit, $orderby, $page);
	$count = db_count('region', $where);
	foreach ($data as $value) {
		$i = $value['id'];
		array_push($arr, array(
			'id' => $i,
			'title' => $value["title"],
			'entitle' => $value["entitle"],
			'zipcode' => $value["zipcode"],
			'telcode' => $value["telcode"],
			'zonecode' => $value["zonecode"],

			'order' => "<input type='number' class='order' id='r_order-" . $i . "' placeholder='" . $value["r_order"] . "'  value='" . $value["r_order"] . "'>",
			'onoff' => "<button type='button'  onclick=setcol('onoff'," . abs($value['onoff'] - 1) . ",'" . $i . "')  id='" . $i . "onoff' class='btn btn-" . $value["onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => "<a href='?act=regionlist&pid=$value[rid]'>$sub</a> <a  onclick=opennew('?act=region&id=" . $i . "')>编辑</a> "

		));
	}
	return '{"total":' . $count . ',"rows":' . tojson($arr) . '}';
}

function loglist()
{
	$uid = getform('uid', 'get');
	$arr = array();
	$i = 0;
	$sort = G('sort');
	$search = G('search');
	$limit = G('limit');
	$page = G('page');
	$order = G('order');
	$sid = G('id');
	$type = G('type');
	if (get_session('adminmark') > 10000) {
		$where = $uid ? "userid=$uid" : 'lid=1';
	} else {
		$where = "userid=" . get_session('adminid');
	}
	if ($sort) {
		$orderby = array($sort => $order, 'logid' => 'desc');
	} else {
		$orderby = ['logid' => 'desc'];
	}

	if ($search) {
		$where .= " and  or ldesc like '%" . $search . "%'";
	}
	$data = db_load('log_dbsql', $where, '*', $limit, $orderby, $page);
	$count = db_count('log_dbsql', $where);
	foreach ($data as $value) {
		$i = $value['logid'];
		$edit = get_session('adminmark' > 10000) ? "<button type='button'  onclick=delid('" . $i . "','log_dbsql')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>" : '';
		array_push($arr, array(
			'id' => $i,
			'ip' => $value["ip"],
			'username' => $value["username"],
			'ldesc' => "<input type='text' class='title' id='ldesc-" . $i . "' placeholder='" . $value["ldesc"] . "'  value='" . $value["ldesc"] . "'>",
			'content' => "<input type='text' class='title' id='content-" . $i . "' placeholder='" . $value["content"] . "'  value='" . $value["content"] . "'>",
			'addtime' => $value["addtime"],
			'edit' => $edit
		));
	}
	return '{"total":' . $count . ',"rows":' . tojson($arr) . '}';
}

function userlist()
{

	$arr = array();
	$i = 0;
	$where = 'u_gid=4';
	$sort = G('sort');
	$search = G('search');
	$limit = G('limit');
	$page = G('page');
	$order = G('order');
	$sid = G('id');
	$type = G('type');

	if ($sort) {
		$orderby = array($sort => $order, 'uid' => 'desc');
	} else {
		$orderby = ['balance' => 'desc', 'uid' => 'asc'];
	}

	if ($search) {
		$where .= " and (truename = '" . $search . "' or mobile= '" . $search . "')";
	}
	$data = db_load('user', $where, '*', $limit, $orderby, $page);
	$count = db_count('user', ['u_gid' => 4]);
	foreach ($data as $value) {
		$i = $value['uid'];
		array_push($arr, array(
			'id' => $i,
			'username' => $value["username"],
			'truename' => "<input type='text' class='title' id='truename-" . $i . "' placeholder='" . $value["truename"] . "'  value='" . $value["truename"] . "'>",
			'mobile' => "<input type='text' class='title' id='mobile-" . $i . "' placeholder='" . $value["mobile"] . "'  value='" . $value["mobile"] . "'>",
			'email' => "<input type='text' class='title'  id='email-" . $i . "' placeholder='" . $value["email"] . "'  value='" . $value["email"] . "'>",
			'logincount' => $value["logincount"],
			'lastlogintime' => $value["lastlogintime"],
			'u_order' => "<input type='number' class='order' id='u_order-" . $i . "' placeholder='" . $value["u_order"] . "'  value='" . $value["u_order"] . "'>",
			'u_onoff' => "<button type='button'  onclick=setcol('u_onoff'," . abs($value['u_onoff'] - 1) . ",'" . $i . "')  id='" . $i . "u_onoff' class='btn btn-" . $value["u_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('?act=user&id=" . $i . "')><i class='fa fa-pencil'></i></button> <button type='button'  onclick=delid('" . $i . "','user')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>"

		));
	}
	return '{"total":' . $count . ',"rows":' . tojson($arr) . '}';
}

function adminlist()
{
	$arr = array();
	$data = db_load_sql('select * from [dbpre]user,[dbpre]user_group where U_GID=GID and IsAdmin=1  and g_mark<=' . get_session("adminmark") . ' order by U_order asc, UID desc');
	foreach ($data as $value) {
		$value = array_change_key_case($value);
		$i = $value['uid'];
		if ($i == get_session('adminid')) {
			$onoff = "<button type='button'  class='btn dim'><i class='fa fa-toggle-on'></i></button>";
			$del = "";
			$log = "<a href='?act=loglist&uid=" . $i . "'  class='btn btn-default dim'><i class='fa fa-history'></i></a>";
		} else if (get_session('adminmark') > $value["g_mark"] || get_session('adminmark') > 9999) {
			$onoff = "<button type='button'  onclick=setcol('u_onoff'," . abs($value['u_onoff'] - 1) . ",'" . $i . "') id='" . $i . "u_onoff' class='btn btn-" . $value["u_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>";
			$del = "<button type='button'  onclick=delid('" . $i . "','user')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>";
			$log = "<a href='?act=loglist&uid=" . $i . "'  class='btn btn-default dim'><i class='fa fa-history'></i></a>";
		} else {
			$onoff = '';
			$del = '';
			$log = '';
		}
		$face = lenstr($value["face"]) > 11 ?  $value["face"] : "../plugins/face/" . $value['face'];
		array_push($arr, array(
			'id' => $i,
			'username' => $value["username"],
			'userface' => "<img src='" . $face . "' height='25'>",
			'usergroup' => $value["g_name"],
			'truename' => "<input type='text' class='title' id='truename-" . $i . "' placeholder='" . $value["truename"] . "'  value='" . $value["truename"] . "'>",
			'lastlogintime' => $value["lastlogintime"],
			'u_order' => "<input type='number' class='order' id='u_order-" . $i . "' placeholder='" . $value["u_order"] . "'  value='" . $value["u_order"] . "'>",
			'u_onoff' => $onoff,
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('?act=admin&uid=" . $i . "')><i class='fa fa-pencil'></i></button> " . $del . $log
		));
	}
	return tojson($arr);
}

function admingroup()
{
	$arr = array();
	$data = db_load_sql("select * from [dbpre]user_group  where isadmin=1 and g_mark<=" . get_session("adminmark") . " order by g_order asc, gid desc");
	foreach ($data as $value) {
		$value = array_change_key_case($value);
		$i = $value['gid'];
		if ($i != get_session('admingid')) {
			$onoff = "<button type='button'  onclick=setcol('g_onoff'," . abs($value['g_onoff'] - 1) . ",'" . $i . "') id='" . $i . "g_onoff' class='btn btn-" . $value["g_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>";
			$del = "<button type='button'  onclick=delid('" . $i . "','user_group')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>";
		} else {
			$onoff = "<button type='button'  class='btn btn-" . $value["g_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>";
			$del = "";
		}
		array_push($arr, array(
			'id' => $i,
			'g_name' => "<input type='text' class='title' id='g_name-" . $i . "' placeholder='" . $value["g_name"] . "'  value='" . $value["g_name"] . "'>",
			'g_mark' => "<input type='text' class='title' id='g_mark-" . $i . "' placeholder='" . $value["g_mark"] . "'  value='" . $value["g_mark"] . "'>",
			'g_desc' => "<input type='text' class='title' id='g_desc-" . $i . "' placeholder='" . $value["g_desc"] . "'  value='" . $value["g_desc"] . "'>",
			'g_order' => "<input type='number' class='order' id='g_order-" . $i . "' placeholder='" . $value["g_order"] . "'  value='" . $value["g_order"] . "'>",
			'g_onoff' => $onoff,
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('?act=admingroup&gid=" . $i . "')><i class='fa fa-pencil'></i></button> " . $del
		));
	}
	return tojson($arr);
}
function usergroup()
{
	$arr = array();
	$data = db_load_sql("select * from [dbpre]user_group  where isadmin=0 order by g_order asc, gid desc");
	foreach ($data as $value) {
		$value = array_change_key_case($value);
		$i = $value['gid'];
		array_push($arr, array(
			'id' => $i,
			'g_name' => "<input type='text' class='title' id='g_name-" . $i . "' placeholder='" . $value["g_name"] . "'  value='" . $value["g_name"] . "'>",
			'g_mark' => "<input type='text' class='type' id='g_mark-" . $i . "' placeholder='" . $value["g_mark"] . "'  value='" . $value["g_mark"] . "'>",
			'g_desc' => "<input type='text' class='title' id='g_desc-" . $i . "' placeholder='" . $value["g_desc"] . "'  value='" . $value["g_desc"] . "'>",
			'g_order' => "<input type='number' class='order' id='g_order-" . $i . "' placeholder='" . $value["g_order"] . "'  value='" . $value["g_order"] . "'>",
			'g_onoff' => "<button type='button'  onclick=setcol('g_onoff'," . abs($value['g_onoff'] - 1) . ",'" . $i . "') id='" . $i . "g_onoff' class='btn btn-" . $value["g_onoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('?act=usergroup&gid=" . $i . "')><i class='fa fa-pencil'></i></button> <button type='button'  onclick=delid('" . $i . "','user_group')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>"
		));
	}
	return tojson($arr);
}


function slidelist()
{
	$arr = array();
	$data = db_load('slide');
	foreach ($data as $value) {
		$i = $value['slideid'];
		if(!isset($value['slideend'])){
			db_add('slide',['slidestart'=>'int']);	
			db_add('slide',['slideend'=>'int']);
			$start=$value["slidestart"]=0;
			$end=$value["slideend"]=0;
		}else{
			$start=$value["slidestart"] ? date("Y-m-d H:i:s",$value["slidestart"]) : "0";
			$end=$value["slideend"] ? date("Y-m-d H:i:s",$value["slideend"]) : "0";
		}
		array_push($arr, array(
			'id' => $i,
			'slidename' => "<input type='text' class='title' id='slidename-" . $i . "' placeholder='" . $value["slidename"] . "'  value='" . $value["slidename"] . "'>",
			'slideclass' => "<input type='text' class='order' id='slideclass-" . $i . "' placeholder='" . $value["slideclass"] . "'  value='" . $value["slideclass"] . "'>",
			'slideimg' => "<input type='text' class='title' id='slideimg-" . $i . "' placeholder='" . $value["slideimg"] . "'  value='" . $value["slideimg"] . "'>",
			'slidestart' => "<input type='text' class='title' id='slidestart-" . $i . "' placeholder='" .$value["slidestart"] . "'  value='" . $start . "'>",
			'slideend' => "<input type='text' class='title' id='slideend-" . $i . "' placeholder='" . $value["slideend"] . "'  value='" . $end . "'>",	
			'slidelink' => "<input type='text' class='title'  id='slidelink-" . $i . "' placeholder='" . $value["slidelink"] . "'  value='" . $value["slidelink"] . "'>",
			'slideorder' => "<input type='number' class='order' id='slideorder-" . $i . "' placeholder='" . $value["slideorder"] . "'  value='" . $value["slideorder"] . "'>",
			'slideonoff' => "<button type='button'  onclick=setcol('slideonoff'," . abs($value['slideonoff'] - 1) . ",'" . $i . "')  id='" . $i . "slideonoff' class='btn btn-" . $value["slideonoff"] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('?act=slide&id=" . $i . "')><i class='fa fa-pencil'></i></button> <button type='button'  onclick=delid('" . $i . "','slide')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>"
		));
	}
	return tojson($arr);
}

function modellist()
{
	$arr = array();
	$data = db_load('model', '', 'model_id,model_name,model_type,model_onoff,model_order,model_list_tp,model_list_fd,model_content_tp,model_content_fd,model_list_name,model_content_name,model_table');
	foreach ($data as $value) {
		$i = $value['model_id'];
		array_push($arr, array(
			'id' => $i,
			'model_name' => "<input type='text' id='model_name-" . $i . "' placeholder='" . $value['model_name'] . "' value='" . $value['model_name'] . "'>",
			'model_type' => $value['model_type'],
			'model_list_tp' => "<input type='text' class='title' id='model_list_tp-" . $i . "' placeholder='" . $value['model_list_tp'] . "' value='" . $value['model_list_tp'] . "'>",
			'model_content_tp' => "<input type='text' class='title' id='model_content_tp-" . $i . "' placeholder='" . $value['model_content_tp'] . "' value='" . $value['model_content_tp'] . "'>",
			'model_onoff' => "<button type='button'  onclick=setcol('model_onoff'," . abs($value['model_onoff'] - 1) . ",'" . $i . "')  id='" . $i . "model_onoff' class='btn btn-" . $value['model_onoff'] . "  dim'><i class='fa fa-toggle-on'></i></button>",
			'model_order' => "<input type='number' class='order'  min='0' max='99' id='model_order" . $i . "' placeholder='" . $value['model_order'] . "'  value=" . $value['model_order'] . ">",

		));
	}
	return tojson($arr);
}

function customlist()
{
	$type = safe_key(getform('type', 'get'));
	$arr = array();
	$i = 0;
	$sort = G('sort');
	$search = G('search');
	$limit = 500;
	$page = G('page');
	$order	=	empty($sort)	?	'customid desc' 		:	$sort . ' ' . G('order');
	$where	=	empty($search)	?	''		:	"`CustomName` like '%" . $search . "%'";
	$where	=	empty($type)	?	''		:	"`Customtype` like '%" . $type . "%'";
	$data	=	db_load("content_custom", $where, 'customid,custom,customname,lid,customtype,customclass,customoptions,customvalues,customplace,customorder,customonoff,customdesc', $limit, $order, $page);
	foreach ($data as $value) {
		$i = $value['customid'];
		$typelink = '';
		$customtype = splits($value['customtype'], ',');
		foreach ($customtype as $val) {
			$typelink .= "<a href='?act=customlist&type=" . $val . "'>" . $val . "</a> ";
		}
		$editlink = $value['customtype'] == 'gbook' ? '?act=gbookcustom&customid=' . $i : '?act=custom&customid=' . $i;
		array_push($arr, array(
			'id' => $i,
			'customname' => "<input type='text'  class='title' id='customname-" . $i . "' placeholder='" . $value['customname'] . "' value='" . $value['customname'] . "'>",
			'custom' => $value['custom'],
			'customclass' => get_custom_name($value['customclass']),
			'customtype' => $typelink,
			'customoptions' => "<input type='text' class='title' id='customoptions-" . $i . "' placeholder='" . $value['customoptions'] . "' value='" . $value['customoptions'] . "'>",
			'customvalues' => "<input type='text' class='title' id='customvalues-" . $i . "' placeholder='" . $value['customvalues'] . "'  value='" . $value['customvalues'] . "'>",
			'customorder' => "<input type='number' class='order' id='customorder-" . $i . "' placeholder='" . $value['customorder'] . "'  value='" . $value['customorder'] . "'>",
			'customonoff' => "<button type='button' class='btn btn-" . $value["customonoff"] . " dim'  onclick=setcol('customonoff'," . abs($value['customonoff'] - 1) . ",'" . $i . "')><i class='fa fa-toggle-on'></i></button>",
			'edit' => "<button type='button' class='btn btn-info dim' onclick=opennew('" . $editlink . "')><i class='fa fa-pencil'></i></button> 
		<button type='button'  onclick=delcustom('" . $value['custom'] . "')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>"
		));
	}
	return tojson($arr);
}

function dbbacklist()
{
	$arr = array();
	$i = 0;;
	$data	=	db_load("log_dbbackup");
	foreach ($data as $value) {
		array_push($arr, array(
			'id' => $value['id'],
			'dbname' => "<input type='text'  class='title' value='" . $value['dbname'] . "'>",
			'addtime' => $value['addtime'],
			'username' => $value['username'],
			'ip' => $value['ip'],
			'edit' => "<button type='button'  onclick=delid('" . $i . "','log_dbbackup')  class='btn btn-danger dim'><i class='fa fa-times'></i></button>"
		));
	}
	return tojson($arr);
}

function gbooklist()
{
	$arr = array();
	$i = 0;
	$sort = G('sort');
	$search = G('search');
	$limit = G('limit');
	$page = G('page');
	empty($sort)			?	$order = 'gid desc' 		:			$order = $sort . ' ' . G('order');
	empty($search)			?	$where = '' 				:			$where = "`g_title` like '%" . $search . "%'";
	$count	=	db_count("gbook", $where);
	$data	=	db_load("gbook", $where, 'gid,g_title,lid,g_uid,g_name,g_tel,g_email,g_content,g_ip,g_time,g_onoff,g_order,r_name,r_content,r_time,r_onoff', $limit, $order, $page);
	foreach ($data as $value) {
		$i = $value['gid'];
		array_push($arr, array(
			'id' => $i,
			'g_name' => '<input type="text"  id="g_name-' . $i . '" placeholder="' . $value['g_name'] . '" value="' . $value['g_name'] . '">',
			'g_title' => '<input type="text" class="title" id="g_title-' . $i . '" placeholder="' . $value['g_title'] . '" value="' . $value['g_title'] . '">',
			'g_time' => '<input type="text"id="g_time-' . $i . '" placeholder="' . $value['g_time'] . '" value="' . $value['g_time'] . '">',
			'g_tel' => '<input type="text"  id="g_tel-' . $i . '" placeholder="' . $value['g_tel'] . '" value="' . $value['g_tel'] . '">',
			'g_email' => '<input type="text" id="g_email-' . $i . '" placeholder="' . $value['g_email'] . '" value="' . $value['g_email'] . '">',
			'g_order' => '<input type="number" class="order" id="g_order-' . $i . '" placeholder="' . $value['g_order'] . '" value="' . $value['g_order'] . '">',
			'g_content' => txt_html($value['g_content']),
			'g_onoff' => '<button type="button" class="btn btn-' . $value['g_onoff'] . ' dim" onclick=setcol("g_onoff",' . abs($value['g_onoff'] - 1) . ',"' . $i . '") id="' . $i . 'g_onoff"><i class="fa fa-toggle-on"></i></button>',
			'edit' => '<button type="button" class="btn btn-info dim" onclick="opennew(\'?act=gbook&id=' . $i . '\')"><i class="fa fa-pencil"></i></button> <button type="button" class="btn btn-danger dim" onclick="delid(' . $i . ',\'gbook\')"><i class="fa fa-times"></i></button>'
		));
	}
	return '{"total":' . $count . ',"rows":' . tojson($arr) . '}';
}
function imagelist()
{
	$upfolder = safe_key(getform("upfolder", "post"));
	$uporder = safe_word(getform("uporder", "post"));
	$path = UPLOAD_DIR . $upfolder . '/';
	$allowFiles = str_replace(",", "|", conf('imageext'));
	$files = getfiles($path, $allowFiles);
	foreach ($files as $k => $v) {
		$size[$k] = $v['size'];
		$time[$k] = $v['mtime'];
		$name[$k] = $v['name'];
	}
	switch ($uporder) {
		case 'size1':
			array_multisort($size, SORT_DESC, SORT_STRING, $files);
			break;
		case 'size2':
			array_multisort($size, SORT_ASC, SORT_STRING, $files);
			break;
		case 'name1':
			array_multisort($name, SORT_DESC, SORT_STRING, $files);
			break;
		case 'name2':
			array_multisort($name, SORT_ASC, SORT_STRING, $files);
			break;
		case 'mtime2':
			array_multisort($time, SORT_ASC, SORT_STRING, $files);
			break;
		default:
			array_multisort($time, SORT_DESC, SORT_STRING, $files);
			break;
	}
	echo tojson($files);
}

function downlist()
{
	$upfolder = safe_key(getform("upfolder", "get"));
	$path = UPLOAD_DIR . $upfolder . '/';
	$allowFiles = str_replace(",", "|", conf('fileext'));
	$files = getfiles($path, $allowFiles);
	echo tojson($files);
}
function filelist($str)
{
	$arr = array();
	$folder = safe_key(getform("folder", "get"));
	$type = safe_word(getform("type", "get"));
	$folders = empty($folder) ? '' : str_replace('//', '/', $folder . '/');
	$conf = _SERVER('conf');
	$default_path = '';
	switch ($str) {
		case 'template':
			$type = $type == 'wap' ? 'wap' : 'pc';
			$folders = str_replace('/' . $str . '/' . $type . '/', '', $folders);
			$path = $type == 'wap' ? SITE_DIR . 'template/wap/' . $folders : SITE_DIR . 'template/pc/' . $folders;
			$files = path_list($path, "template");
			break;
		case 'upload':
			$folders = str_replace('/' . $str . '/', '', $folders);
			$path = UPLOAD_DIR . $folders;
			$files = path_list($path, "");
			break;
		case 'html':
			$type == 'wap' ?: 'pc';
			$folders = str_replace('/' . $str . '/' . $type . '/', '', $folders);
			$path = $type == 'wap' ? SITE_DIR . 'wap/' . conf('htmldir') : SITE_DIR . conf('htmldir');
			$files = getfiles($path, 'html', 'no');
			$about = getfiles($path . 'about/', 'html', 'no');
			is_array($about) and $files = array_merge($files, $about);
			$brand = getfiles($path . 'brand/', 'html', 'no');
			is_array($brand) and $files = array_merge($files, $brand);
			$brandlist = getfiles($path . 'brandlist/', 'html', 'no');
			is_array($brandlist) and $files = array_merge($files, $brandlist);
			$list = getfiles($path . 'list/', 'html', 'no');
			is_array($list) and $files = array_merge($files, $list);
			$taglist = getfiles($path . 'taglist/', 'html', 'no');
			foreach (load_model() as $v) {
				$content = getfiles($path . $v . '/', 'html', 'no');
				is_array($content) and $files = array_merge($files, $content);
			}
			break;
		case 'cache':
			$path = $type == 'web' ? RUN_DIR . 'cache/' . conf('adminpath') : RUN_DIR . 'cache/html/';
			$files = getfiles($path, 'tpl', 'all');
			break;
		case 'log':
		case 'error':
		case 'data':
			$path = RUN_DIR . $str . '/';
			$files = getfiles($path, 'zzz');
			break;
		case 'login':
			$default_path = SITE_PATH . 'runtime' . ADMIN_PATH . 'login/';
			if ($folders) {
				$path = $folders;
			} else {
				$path = $default_path;
			}
			$path = str_replace('//', '/', SITE_DIR . $path);
			$files = path_list($path, "");
			break;
		case 'backup':
			$path = ADMIN_DIR . 'backup';
			$file1 = getfiles($path, 'zip');
			switch ($conf['db']['type']) {
				case 'sqlite':
					$path = SITE_DIR . $conf['db']['sqlitepath'] . 'backup';
					$file2 = getfiles($path, 'lite');
					break;
				case 'mysql':
					$path = ADMIN_DIR . 'backup';
					$file2 = getfiles($path, 'bak');
					break;
			}
			$files = array_merge($file1, $file2);
			break;
	}

	if (!is_array($files)) {
		return false;
	} else {
		$data = arr_asc($files, 'ext');
		$count	= count($data);
		foreach ($data as $value) {
			$edit = ' <a href="javascript:void(0)"  class="btn btn-danger dim"  onclick=delfile(\'' . $value['url'] . '\')><i class="fa fa-times"></i></a> ';
			$url = $value['url'];
			if (lenstr($url) < lenstr($default_path)) continue;
			$size = formatnum($value['size'], 'size');
			switch ($value['ext']) {
				case '':
					if ($value['title'] == '返回') {
						$edit = '<a href="?act=' . $str . 'list&type=' . $type . '&folder=' . $url . '" class="btn btn-default dim"><i class="fa fa-folder-open"></i></a>';
						$name = '<a href="?act=' . $str . 'list&type=' . $type . '&folder=' . $url . '"><i class="fa fa-folder"></i>&nbsp;' . $value['title'] . '(' . $value['count'] . ')</a>';
					} else {
						$edit = '<a href="?act=' . $str . 'list&type=' . $type . '&folder=' . $url . '" class="btn btn-default dim"><i class="fa fa-folder"></i></a>';
						$name = '<a href="?act=' . $str . 'list&type=' . $type . '&folder=' . $url . '"><i class="fa fa-folder"></i>&nbsp;' . $value['title'] . '(' . $value['count'] . ')</a>';
					}
					$ext = 'Folder';
					$size = '';
					break;
				case 'jpg':
				case 'jpeg':
				case 'png':
				case 'gif':
				case 'bmp':
					$name = '<a href="' . $value['url'] . '" class="fancybox"  rel="group"><i class="fa fa-image"></i>&nbsp;' . $value['title'] . '</a>';
					$ext = 'Image';
					break;
				case 'txt':
				case 'xml':
				case 'htm':
				case 'js':
				case 'css':
				case 'zzz':
					$edit = '<a href="' . $value['url'] . '" class="btn btn-default dim" target="_blank"><i class="fa fa-external-link"></i></a> <a href="javascript:void(0)" class="btn btn-success dim" onclick=opennew(\'?act=codeedit&folder=' . $value['url'] . '\')><i class="fa fa-pencil"></i></a>' . $edit;
					$name = '<a href="javascript:void(0)" onclick=opennew(\'?act=codeedit&folder=' . $value['url'] . '\')><i class="fa fa-edit"></i>' . $value['title'] . '</a>';
					$ext = 'Text';
					break;
				case 'html':
				case 'tpl':
					$edit = '<a href="?act=templateedit&folder=' . $value['url'] . '" class="btn btn-success dim"><i class="fa fa-pencil"></i></a>' . $edit;
					$name =  '<a href="?act=codeedit&folder=' . $value['url'] . '"><i class="fa fa-edit"></i>&nbsp;' . $value['title'] . '</a>';
					$ext = 'Html';
					break;
				case 'acc':
				case 'lite':
				case 'bak':
					if (ifstrin($value['url'], 'backup')) {
						$edit = '<a href="javascript:void(0)" class="btn btn-warning dim" onclick=restore(\'' . $value['url'] . '\',\'' . $value['ext'] . '\')><i class="fa fa-recycle"></i></a>' . $edit;
					} else {
						$edit = '<a href="' . $value['url'] . '" class="btn btn-info dim")><i class="fa fa-down"></i></a>' . $edit;
					}
					$name =  $value['title'];
					$ext = 'Data';
					break;
				case 'tar':
				case 'rar':
				case '7z':
				case 'zip':
					if (ifstrin($value['url'], 'backup')) {
						$edit = '<a href="javascript:void(0)" class="btn btn-warning dim" onclick=restore(\'' . $value['url'] . '\',\'' . $value['ext'] . '\')><i class="fa fa-recycle"></i></a>' . $edit;
					} else {
						$edit = '<a href="' . $value['url'] . '" class="btn btn-info dim")><i class="fa fa-down"></i></a>' . $edit;
					}
					$name =  $value['title'];
					$ext = 'zip';
					break;
				default:
					$ext = $value['ext'];
					$name = '<a href="' . $value['url'] . '" download="' . $value['title'] . '"><i class="fa fa-' . $ext . '"></i> ' . $value['title'] . '</a> ';
					$edit = '<a href="' . $value['url'] . '" download="' . $value['title'] . '" class="btn btn-default dim"><i class="fa fa-external-link"></i></a> ' . $edit;
			}
			array_push($arr, array(
				'name' => $name,
				'url' => '<input class="title" value="' . $url . '" readonly="readonly">',
				'mtime' => $value['mtime'],
				'size' => $size,
				'ext' => $ext,
				'edit' => $edit
			));
		}
		return tojson($arr);
		//return '{"total":'.$count.',"rows":'.tojson($arr).'}';
	}
}
