<?php
if(!defined('IN_ADMIN')) exit('Access Denied');
if($_SESSION["admin"]!="NewsTeng")showmessage('请登陆', SCRIPT_NAME.'?file=login');
require_once TM_ROOT.'include/admin.class.php';
$admin1 = new admin();
@set_time_limit(0);
if(!isset($action)) $action = 'list';
switch ($action){
	case 'add':
            if($dosubmit){
                if(empty($info)) showmessage('操作失败', 'goback'); 
            	if(!is_array($info)){	
            		showmessage('用户名不符合规范！', 'goback');					
				}    
            	$strlen = strlen($info['username']);
				if($admin1->is_badword($info['username']) || !preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $info['username'])){
					showmessage('用户名不符合规范！', 'goback');	
				}elseif ( 20 <= $strlen || $strlen < 2 ){
					showmessage('用户名不得少于3个字符超过20个字符！', 'goback');	
			
				}
            	if(!$admin1->username_exists($info['username'])){	
    				showmessage('用户名已经存在请更换一个！', 'goback');				
				} 
            	if(!$admin1->is_password($info['password'])){	
            		showmessage('密码不得少于3个字符超过20个字符', 'goback');						
				}       	
				if(!$admin1->register($info)) {
					showmessage('添加失败,请检查输入是否正确!','goback');
				}
				showmessage('添加成功！', 'goback');
            }else{     
                include admin_tpl('admin');
            }
	break;

    case 'list':
    	$page = max(intval($page), 1);
    	$page_size = 15;
    	$r = $db->get_one("SELECT count(*) as num FROM ".DB_PRE."admin");
    	$mpurl =SCRIPT_NAME.'?file=admin&action=list&';
    	$dispage =  page($r['num'],$page_size,$page,$mpurl);
      	$adm = $admin1->listinfo('', 'id DESC', $page, $page_size);
        include admin_tpl('admin');
	break;

    case 'edit':
        if($dosubmit){
        	if(!$admin1->is_password($password)){	
            	showmessage('密码不得少于3个字符超过20个字符', 'goback');						
			} 
        	if(!$admin1->updata_password(intval($id),$password)){
        		showmessage('请确认您的填写是否正确！', 'goback');
        	}
        	showmessage('保存成功！', 'goback');
        }else{
        	$adm = $admin1->get(intval($id)); 
            include admin_tpl('admin');
        }
	break;
	case 'del':
		$u = $admin1->del(intval($id));
	    if(!$u){
	    	showmessage('删除失败！', 'goback');
	    }
	    showmessage('删除成功！', 'goback');
	break;
}
?>