<?php
$mu = Session::get('mu');
if(!$mu->id){
	Redirect::to('/manage/login.html');
}
$fs = File::scandir(DOXCX_FRAME_PATH.'controller/weidogsadmin');
$menu_arr = array();
foreach ($fs as $f){
	if(file_exists(DOXCX_FRAME_PATH.'controller/weidogsadmin/'.$f.'/index.php')){
		include DOXCX_FRAME_PATH.'controller/weidogsadmin/'.$f.'/index.php';
		if(array_search(array('name'=>'系统更新','file'=>'update'),$p_sub) && $mu->isadmin!=1){
			unset($p_sub[array_search(array('name'=>'系统更新','file'=>'update'),$p_sub)]);
		}

		if(array_search(array('name'=>'管理员账号设置','file'=>'adminSet'),$p_sub) && $mu->isadmin!=1){
			unset($p_sub[array_search(array('name'=>'管理员账号设置','file'=>'adminSet'),$p_sub)]);
		}
		if(array_search(array('name'=>'账号余额设置','file'=>'balanceSet'),$p_sub) && $mu->isadmin!=1){
			unset($p_sub[array_search(array('name'=>'账号余额设置','file'=>'balanceSet'),$p_sub)]);
		}

		$menu_arr[$p_ind] = array('path'=>$f,'name'=>$p_name,'sub'=>$p_sub);
		if($p_name=='模板管理'&& $mu->isadmin!=1 && $mu->isyg!=1){
			unset($menu_arr[$p_ind]);
		}
		if($p_name=='代理管理'&& $mu->isadmin!=1 && $mu->isyg==1){
			unset($menu_arr[$p_ind]);
		}
        if($p_name=='应用中心'&& $mu->isadmin!=1 && $mu->isyg==1){
            unset($menu_arr[$p_ind]);
        }
		
    }
}
//插件使用方法方式加载避免 变量污染问题
if(!function_exists("getPlugArr")){
    function getPlugArr(){
        $mu = Session::get('mu');
        $pfs = File::scandir(DOXCX_FRAME_PATH.'Addons');
        $plug_arr = array();
        foreach($pfs as $f){
            if(file_exists(DOXCX_FRAME_PATH.'Addons/'.$f.'/index.php')){
                $arr=include(DOXCX_FRAME_PATH.'Addons/'.$f.'/index.php');
                if(isset($arr['p_ind'])&&($mu->isadmin==1 || !$arr['need_admin'])){
                    $plug_arr[$arr['p_ind']] = array('path'=>$f,'name'=>$arr['p_name'],'imgurl'=>$arr['p_img'],'sub'=>$arr['p_sub']);
                }

            }
        }
        return $plug_arr;
    }
}
$plug_arr= getPlugArr();

ksort($plug_arr);
//var_dump($plug_arr);

					
ksort($menu_arr);

//查找用户权限
$qxm = new Model('weidogsadmin_check');
$qxm->field('id,nopages')->find(array('typ'=>'3','typid'=>$mu->id));
if(!$qxm->has_id()){
	$qxm = new Model('weidogsadmin_check');	
	$tjwhere = array();
	if($mu->isyg){
		$tjwhere['typ'] = '1';
	}else{
		$tjwhere['typ'] = '2';
	}
	$tjwhere['typid'] = $mu->level;
	$qxm->field('id,nopages')->find($tjwhere);
}
$nopages = explode(',', trim($qxm->nopages));
if($mu->isyg && $mu->isadmin!=1){//是员工并且不是管理员
	//查找代理权限
	$daili=(new Model('user_agency'))->find($mu->pid);
	$dailiqxm=(new Model('weidogsadmin_check'))->find(array('typ'=>2,'typid'=>$daili->level));
	$dailiNopages=explode(',', trim($dailiqxm->nopages));
	$nopages=array_merge($dailiNopages,$nopages);
}
function check_page($p,$f){
	global $nopages,$mu;
	//echo $p.'@'.$f;
	if(!$mu->isadmin && in_array($p.'@'.$f, $nopages)){
		return false;
	}
	return true;
}
//去除插件的空的一级菜单
foreach ((array)$menu_arr as $k=>$m){
    foreach ((array)$m['sub'] as $kk=>$mm){
        if(!check_page($m['path'],$mm['file']))unset($m['sub'][$kk]);
    }
    if(empty($m['sub']))unset($menu_arr[$k]);
}
foreach ((array)$plug_arr as $k=>$m){
    foreach ((array)$m['sub'] as $kk=>$mm){
        if(!check_page($m['path'],$mm['file']))unset($m['sub'][$kk]);
    }
    if(empty($m['sub']))unset($plug_arr[$k]);
}

//获取短信余额并保存
$agency=new Model('user_agency');	
$agency->find($mu->id);
	$url="http://service.winic.org/webservice/public/remoney.asp?";
    $data = "uid=%s&pwd=%s";
    $uid = $agency->message_id;
    $pwd = $agency->message_pwd;
    $rdata = sprintf($data, $uid, $pwd);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$rdata);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $result = curl_exec($ch);
    curl_close($ch);
	$agency->sms_balance=$result;
	$agency->save();

