<?php
class extendMod extends commonMod
{
    public function __construct()
    {
        parent::__construct();
        $this->auth(26);
    }
    /**
     * 插件管理器
     */
    public function index()
    {
        
        $tpl_list=glob(__ROOTDIR__.'/plugins/*/setting.php');
        foreach($tpl_list as $key){
        $tpl_arry[]=require($key);
        }
        $this->assign('list', $tpl_arry);
        $this->display();
    }

    public function info()
    {
        
        $condition['PLUS_NAME']=$_GET[0];
        $info=$this->model->table('extend')->where($condition)->find();
        $this->assign('info', $info);
        $this->display();
    }

    public function open()
    {
        
        $condition['PLUS_NAME']=in($_POST['name']);
        $data['OPEN']=0;
        $this->model->table('extend')->data($data)->where($condition)->update(); 
    }
    
    public function close()
    {
        
        $condition['PLUS_NAME']=in($_POST['name']);
        $data['OPEN']=1;
        $this->model->table('extend')->data($data)->where($condition)->update(); 
    }

    

    public function install()
    {
        
        //通过数据库文件方式去安装
        $name=in($_POST['name']);
        if(empty($name)){
            $this->jserror('数据传输错误!');
        }

        $db_file = __ROOTDIR__ .'/plugins/'.$name.'/install.sql';

        if (is_file($db_file))
        {
            $this->_runSql(Install::mysql($db_file));
        }

        $setting = empty($setting) ? require (__ROOTDIR__ .'/plugins/'.$name.'/setting.php') :$setting;


        //安装到插件库
        $mod_data['NAME']=$setting['NAME'];
        $mod_data['PLUS_NAME']=$setting['PLUS_NAME'];
        $mod_data['VER']=$setting['VER'];
        $mod_data['AUTHOR']=$setting['AUTHOR'];
        $mod_data['help']=$setting['HELP'];
        $mod_data['config']=$setting['CONFIG']; //配置文件为序列化后的
        $this->model->table('extend')->data($mod_data)->insert();
        //安装到插件库结束
        
        //写入权限数据
        $id=$this->extend_auth_Install($setting['NAME'],$setting['PLUS_NAME']);
        $this->plussetting($name,'RID',$id); //写入权限ID
        //权限写入结束
        
        //修改配置
        $this->jssuccess('插件安装成功！');
    }

    //卸载插件
    public function uninstall()
    {
        
        $name=in($_POST['name']);
        if(empty($name)){
            $this->jserror('数据传输错误');
        }

        $setting = empty($setting) ? require (__ROOTDIR__ .'/plugins/'.$name.'/setting.php') :$setting;

        //插件卸载sql
        $db_file = __ROOTDIR__ .'/plugins/'.$name.'/uninstall.sql';

        if (is_file($db_file))
        {
            $this->_runSql(Install::mysql($db_file));
        }
        //结束

        $this->extend_auth_unInstall($this->plussetting($name,'RID')); //删除插件权限

        //删除插件库
        $condition['id']=$this->plussetting($name,'id');
        $this->model->table('extend')->where($condition)->delete(); 
        //结束

        @del_dir(__ROOTDIR__ .'/plugins/'.$name); //删除本插件目录及文件
        $this->jssuccess('插件卸载成功！');
    }

    //插件权限安装
    public function extend_auth_Install($name,$file)
    {
       $data = array();
       $data['pid']=25;
       $data['name']=$name;
       $data['module']=$file;
       $id=$this->model->table('resource')->data($data)->insert();
       
       $config=$this->config; //加载配置
        if ($_SESSION[$config['spot'] . 'admin_uid'] == 1) {
            $group_info = $this->model->table('admin_group')->where('id=1')->find(); //查询权限列表
            $group_data = array();
            $group_data['power_value'] = $group_info['power_value'] . ',' . $id;
            $this->model->table('admin_group')->data($group_data)->where('id=1')->update(); //增加超级管理组权限
        } else {
            $group_condition['id'] = $_SESSION[$config['spot'] . 'admin_uid'];
            $group_info = $this->model->table('admin_group')->where($group_condition)->find(); //查询权限列表
            $group_data = array();
            $group_data['power_value'] = $group_info['power_value'] . ',' . $id;
            $this->model->table('admin_group')->data($group_data)->where($group_condition)->update(); //增加当前管理组权限

            $admin_group_info = $this->model->table('admin_group')->where('id=1')->find(); //查询权限列表
            $admin_group_data = array();
            $admin_group_data['power_value'] = $admin_group_info['power_value'] . ',' . $id;
            $this->model->table('admin_group')->data($admin_group_data)->where('id=1')->update(); //增加超级管理组权限
        }
        return $id;
    }
    
    //插件权限删除
    public function extend_auth_unInstall($id)
    {
       $condition = array();
       $condition['id']=$id;
       $this->model->table('resource')->where($condition)->delete(); //删除权限栏目
    }

}

?>