<?
if (!defined("_GNUBOARD_")) exit;


// 获取skin路径
function get_skin_dir($skin, $len='')
{
    global $g4;

    $result_array = array();

    $dirname = "$g4[path]/skin/$skin/";
    $handle = opendir($dirname);
    while ($file = readdir($handle)) 
    {
        if($file == "."||$file == "..") continue;

        if (is_dir($dirname.$file)) $result_array[] = $file;
    }
    closedir($handle);
    sort($result_array);

    return $result_array;
}

// 删除会员
function member_delete($mb_id)
{
    global $config;
    global $g4;

    $sql = " select mb_name, mb_nick, mb_ip, mb_recommend, mb_memo, mb_level from $g4[member_table] where mb_id= '$mb_id' ";
    $mb = sql_fetch($sql);
    if ($mb[mb_recommend]) {
        $row = sql_fetch(" select count(*) as cnt from $g4[member_table] where mb_id = '".addslashes($mb[mb_recommend])."' ");
        if ($row[cnt])
            insert_point($mb[mb_recommend], $config[cf_recommend_point] * (-1), "{$mb_id}因被删除帐户返还积分", '@member', $mb[mb_recommend], "{$mb_id} 推荐人删除");
    }

    // 删除会员资料后保存id名称，防止段时间内被新用户注册: 061025
    if ($mb[mb_level] > 1) {
        $sql = " update $g4[member_table] 
                    set mb_jumin = '',
                        mb_password = '',
                        mb_level = '1',
                        mb_email = '',
                        mb_homepage = '',
                        mb_password_q = '',
                        mb_password_a = '',
                        mb_tel = '',
                        mb_hp = '',
                        mb_zip1 = '',
                        mb_zip2 = '',
                        mb_addr1 = '',
                        mb_addr2 = '',
                        mb_birth = '',
                        mb_sex = '',
                        mb_signature = '',
                        mb_memo = '".date("Ymd",$g4['server_time'])." 已删帐户\n\n$mb[mb_memo]',
                        mb_leave_date = '".date("Ymd",$g4['server_time'])."' 
                  where mb_id = '$mb_id' ";
        //echo $sql; exit;
        sql_query($sql);
    }
    
    /*
    // 会员资料删除
    sql_query(" delete from $g4[member_table] where mb_id = '$mb_id' ");

    // 再一次删除后可进行彻底删除
    if ($mb[mb_nick] != '[彻底删除]')
    {
        // 防止其他人占用已被删除id
        // 因为论坛数据中将会保留用户数据
        sql_query(" insert into $g4[member_table] set mb_id = '$mb_id', mb_name='$mb[mb_name]', mb_nick='[已删帐户]', mb_ip='$mb[mb_ip]', mb_datetime = '$g4[time_ymdhis]' ");
    }
    
    // 删除积分数据
    sql_query(" delete from $g4[point_table] where mb_id = '$mb_id' ");
    
    // 删除版块权限
    sql_query(" delete from $g4[group_member_table] where mb_id = '$mb_id' ");
    
    // 删除短信数据
    sql_query(" delete from $g4[memo_table] where me_recv_mb_id = '$mb_id' or me_send_mb_id = '$mb_id' ");
    
    // 删除收藏数据
    sql_query(" delete from $g4[scrap_table] where mb_id = '$mb_id' ");
    
    // 删除管理权限
    sql_query(" delete from $g4[auth_table] where mb_id = '$mb_id' ");

    // 如果是版块管理员则将管理员留空
    sql_query(" update $g4[group_table] set gr_admin = '' where gr_admin = '$mb_id' ");

    // 如果是论坛管理员则将论坛管理员留空
    sql_query(" update $g4[board_table] set bo_admin = '' where bo_admin = '$mb_id' ");

    // 删除头像
    @unlink("$g4[path]/data/member/".substr($mb_id,0,2)."/$mb_id.gif");
    */
}


// SELECT方式获取会员数据
function get_member_level_select($name, $start_id=0, $end_id=10, $selected='', $event='')
{
    global $g4;

    $str = "<select name='$name' $event>";
    for ($i=$start_id; $i<=$end_id; $i++)
    {
        $str .= "<option value='$i'";
        if ($i == $selected) 
            $str .= " selected";
        $str .= ">$i</option>";
    }
    $str .= "</select>";
    return $str;
}


// select方式获取会员权限
function get_member_id_select($name, $level, $selected='', $event='')
{
    global $g4;

    $sql = " select mb_id from $g4[member_table] where mb_level >= '$level' ";
    $result = sql_query($sql);
    $str = "<select name='$name' $event><option value=''>请选择";
    for ($i=0; $row=sql_fetch_array($result); $i++) 
    {
        $str .= "<option value='$row[mb_id]'";
        if ($row[mb_id] == $selected) $str .= " selected";
        $str .= ">$row[mb_id]</option>";
    }
    $str .= "</select>";
    return $str;
}

// 权限检查
function auth_check($auth, $attr)
{
    global $is_admin;

    if ($is_admin == "super") return;

    if (!trim($auth))
        alert("您没有此分类管理权限！\\n\\n请向网站管理员咨询");

    $attr = strtolower($attr);

    if (!strstr($auth, $attr)) {
        if ($attr == "r")
            alert("没有浏览权限");
        else if ($attr == "w")
            alert("没有编辑，添加，创建权限.");
        else if ($attr == "d")
            alert("没有删除权限");
        else 
            alert("属性设置错误");
    }
}


// text编辑窗口调整
function textarea_size($fld) 
{
    global $g4;

    $size = 10;
    $s  = "<table cellpadding=2 cellspacing=0 border=0 width=100%><tr><td align=right>";
    $s .= "<span onclick=\"javascript:textarea_size(document.getElementById('$fld'), {$size})\"><img src='$g4[admin_path]/img/btn_up.gif' border=0 align=absmiddle></span> ";
    $s .= "<span onclick=\"javascript:textarea_size(document.getElementById('$fld'), ".$size*(-1).")\"><img src='$g4[admin_path]/img/btn_down.gif' border=0 align=absmiddle></span>";
    $s .= "&nbsp;&nbsp;</td></tr></table>";
    return $s;
}


// 显示操作栏目
function icon($act, $link="", $target="_parent")
{
    global $g4;

    $img = array("新建"=>"insert", "添加"=>"insert", "创建"=>"insert", "编辑"=>"modify", "删除"=>"delete", "移动"=>"move", "版块"=>"move", "浏览"=>"view", "预览"=>"view", "复制"=>"copy");
    $icon = "<img src='{$g4[admin_path]}/img/icon_{$img[$act]}.gif' border=0 align=absmiddle title='$act' width=22 height=21>";
    if ($link)
        //$s = "<a href=\"$link\" target=\"$target\">$icon</a>";
        $s = "<a href=\"$link\">$icon</a>";
    else
        $s = $icon;
    return $s;
}


// rm -rf 选项 : exec(), system() 不能使用时，特别针对 windows系统
// www.php.net 参考: pal at degerstrom dot com
function rm_rf($file) 
{
    if (file_exists($file)) {
        @chmod($file,0777);
        if (is_dir($file)) {
            $handle = opendir($file); 
            while($filename = readdir($handle)) {
                if ($filename != "." && $filename != "..") 
                    rm_rf("$file/$filename");
            }
            closedir($handle);
            rmdir($file);
        } else 
            unlink($file);
    }
}

function help($help="", $left=0, $top=0)
{
    global $g4;
    static $idx = 0;

    $idx++;

    $help = preg_replace("/\n/", "<br>", $help);
    
    $str  = "<img src='$g4[admin_path]/img/icon_help.gif' border=0 width=15 height=15 align=absmiddle onclick=\"help('help$idx', $left, $top);\" style='cursor:hand;'>";
    $str .= "<div id='help$idx' style='position:absolute; display:none; z-index:9999;'>";
    $str .= "<div id='csshelp1'><div id='csshelp2'><div id='csshelp3'>$help</div></div></div>";
    $str .= "</div>";

    return $str;
}

function subtitle($title, $more="") 
{
    global $g4;

    $s = "<table width=100% cellpadding=0 cellspacing=0><tr><td width=80% align=left><table border='0' cellpadding='0' cellspacing='1'><tr><td height='24'><img src='$g4[admin_path]/img/icon_title.gif' width=20 height=9> <font color='#525252'><b>$title</b></font> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table><table width=100% cellpadding=0 cellspacing=0><tr><td height=1></td></tr></table></td><td width=20% align=right>";
    if ($more)
        $s .= "<a href='$more'><img src='$g4[admin_path]/img/icon_more.gif' width='43' height='11' border=0 align=absmiddle></a>";
    $s .= "</td></tr></table>\n";
    
    return $s;
}

// 显示顺序
function order_select($fld, $sel="") 
{
    $s = "<select name='$fld'>";
    for ($i=1; $i<=100; $i++) {
        $s .= "<option value='$i' ";
        if ($sel) {
            if ($i == $sel) {
                $s .= "selected";
            }
        } else {
            if ($i == 50) {
                $s .= "selected";
            }
        }
        $s .= ">$i</option>";
    }
    $s .= "</select>\n";

    return $s;
}

// 访问权限检查
if (!$member['mb_id'])
{
    //alert("请您登录后进行操作！", "$g4[bbs_path]/login.php?url=" . urlencode("$_SERVER[PHP_SELF]?w=$w&mb_id=$mb_id"));
    alert("请您登录后进行操作！", "$g4[bbs_path]/login.php?url=" . urlencode("$_SERVER[PHP_SELF]?$_SERVER[QUERY_STRING]"));
}
else if ($is_admin != "super") 
{
    $auth = array();
    $sql = " select au_menu, au_auth from $g4[auth_table] where mb_id = '$member[mb_id]' ";
    $result = sql_query($sql);
    for($i=0; $row=sql_fetch_array($result); $i++) 
    {
        $auth[$row[au_menu]] = $row[au_auth];
    }

    if (!$i)
    {
        alert("您没有相应管理权限！", $g4[path]);
    }
}

// 如果管理员id与浏览器ip地址冲突，则向管理员发出通知信息
$admin_key = md5($member[mb_datetime] . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
if (get_session("ss_mb_key") !== $admin_key) {

    session_destroy();

    include_once("$g4[path]/lib/mailer.lib.php");
    // 邮件通知
    mailer($member['mb_nick'], $member['mb_email'], $member['mb_email'], "XSS 攻击预警", "{$_SERVER['REMOTE_ADDR']} 此IP地址正在进行 XSS 攻击\n\n有可能进行管理权限提权操作\n\n请查封此ip地址，并进行整站安全检查，特别留意论坛是否有可以附件上传\n\n$g4[url]", 0);

    alert_close("访问错误！请通过正常方式登录！");
}

@ksort($auth);

// 栏目
unset($auth_menu);
unset($menu);
unset($amenu);
$tmp = dir($g4['admin_path']);
while ($entry = $tmp->read()) 
{
    //if (!preg_match("/^admin.menu([0-9]{3}).php/", $entry, $m)) 
    //if (!preg_match("/^admin.menu([0-9]{3}).*\.php/", $entry, $m)) 
    if (!preg_match("/^admin.menu([0-9]{3}).*\.php$/", $entry, $m)) 
        continue;  // 文件名不通过 menu 开始就忽视 

    $amenu[$m[1]] = $entry;
    include_once($g4['admin_path']."/".$entry);
}
@ksort($amenu);

$qstr = "";
if (isset($sst)) $qstr .= "&sst=$sst";
if (isset($sod)) $qstr .= "&sod=$sod";
if (isset($sfl)) $qstr .= "&sfl=$sfl";
if (isset($stx)) $qstr .= "&stx=$stx";
if (isset($page)) $qstr .= "&page=$page";
//$qstr = "sst=$sst&sod=$sod&sfl=$sfl&stx=$stx&page=$page";
?>