<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id modules.inc.php 1 2008-08-22 23:23 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

$action = empty($action) ? 'modules' : $action;

$in_system = false;
foreach($arr_modules[0] as $key => $val) {
    if($key === 'title') continue;
    if($key === 'module') continue;
    list($m_action,,,,) = explode("|", $val);
    if($m_action == $action) {
        $in_system = true;
        break;
    }
}
if(!$in_system) {

    require_once MUDDER_ROOT.'./'.$action.'/admin/'.$action.'.inc.php';

} else {

    $op = empty($op) ? 'manage' : $op;
    switch($op) {
        case 'manage':
            if(!check_submit('dosubmit')) {
                $query = $db->query("SELECT * FROM {$dbpre}modules ORDER BY iscore DESC,moduleid");
                include cptpl('modules');
            } else {
                if(is_array($moduleflags)) {
                    foreach($moduleflags as $val) {
                        cache_module($val);
                    }
                    cpmsg("缓存更新完毕！","admincp.php?action=$action&file=$file&op=manage");
                } else {
                    cpmsg("未选择更新模块，请返回选择。");
                }
            }
            break;
        case 'info':
            $moduleinfo = $db->get_one("SELECT * FROM {$dbpre}modules WHERE moduleid='$moduleid'");
            unset($moduleinfo['config']);
            include cptpl('modules_info');
            break;
        case 'enable':
        case 'disable':
            $disable = $op=='enable' ? 0 : 1;
            $db->update("UPDATE {$dbpre}modules SET disable='$disable' WHERE flag='$flag'");
            cache('modules');
            cache_module($flag);
            cpmsg('模块状态设置完毕。',"admincp.php?action=$action&file=$file&op=manage");
            break;
        case 'versioncheck':
            $moduleinfo = $db->get_one("SELECT name,checkurl FROM {$dbpre}modules WHERE moduleid='$moduleid'");
            if(empty($moduleinfo['checkurl'])) {
                cpmsg($moduleinfo['name'].'模块未实现新版检测接口功能，请返回。');
            }
            $newver = @file_get_contents($moduleinfo['checkurl']);
            if(empty($newversion)) {
                cpmsg('检测失败，请返回。');
            }
            include cptpl('modules_checkup');
            break;
        case 'install':
            $step = empty($step) ? 1 : $step;
            switch($step) {
                case 2:
                    empty($directory) and cpmsg('未填写目录名称，请返回填写。');
                    !file_exists(MUDDER_ROOT.'./'.$directory) and cpmsg('目录不存在，请返回填写。');
                    $newmodule = array();
                    if(!@include(MUDDER_ROOT.'./'.$directory.'/install/info.php')) {
                        cpmsg('无法读取模块信息，请返回确认模块的完整性。');
                    }
                    if(empty($newmodule) || empty($newmodule['flag'])) {
                        cpmsg('空的模块信息，请返回确认模块的完整性。');
                    }
                    $modexist = $db->get_value("SELECT COUNT(*) FROM {$dbpre}modules WHERE flag='{$newmodule['flag']}'") >= 1;
                    $modexist and cpmsg('系统已安装了'.$newmodule['name'].'模块，请返回。');
                    $readonly = array('flag','reliant','author','siteurl','email','copyright');
                    break;
                case 3:
                    empty($newmodule['directory']) and cpmsg('未填写目录名称，请返回填写。');
                    $dir = MUDDER_ROOT.'./'.$newmodule['directory'];
                    // 安装数据表
                    if(file_exists($sqlfile = $dir.'/install/module_install.sql')) {
                        $fp = fopen($sqlfile, 'rb');
                        $modulesql = fread($fp, 2048000);
                        fclose($fp);
                    }
                    $modulesql && runQuery($modulesql);
                    // 调用模块安装文件
                    @include($dir.'/install/install.php');
                    // 写入模块表
                    $moduleconfig = array();
                    @include(MUDDER_ROOT.'./'.$directory.'/install/config.php');
                    $newmodule['config'] = empty($moduleconfig) ? '' : addslashes(serialize($moduleconfig));
                    $db->insert_new($dbpre.'modules', $newmodule);
                    // 缓存
                    cache('modules');
                    cache_module($newmodule['flag']);
                    cpmsg($newmodule['name'].'模块安装完成。','admincp.php?action=modoer&file=modules&op=manage');
            }
            include cptpl('modules_install');
            break;
        case 'unstall':
            $moduleinfo = $db->get_one("SELECT * FROM {$dbpre}modules WHERE moduleid='$moduleid'");
            if($moduleinfo['iscore']) cpmsg('本模块为核心模块，不允许删除。');
            $existreliant = $db->get_value("SELECT COUNT(*) FROM {$dbpre}modules WHERE reliant LIKE '%{$moduleinfo['flag']}%'") >= 1;
            if($existreliant) cpmsg('系统内存在依赖于本模块的其他子模块，请返回先删除依赖于本模块的子模块。');
            $dir = MUDDER_ROOT.'./'.$moduleinfo['directory'];
            // 执行卸载数据表SQL
            if(file_exists($sqlfile = $dir.'/install/module_uninstall.sql')) {
                $fp = fopen($sqlfile, 'rb');
                $modulesql = fread($fp, 2048000);
                fclose($fp);
            }
            $modulesql && runQuery($modulesql);
            // 调用模块卸载文件
            @include($dir.'/install/uninstall.php');
            // 删除模块文件夹下的所有文件
            if(strlen($dir) - strlen(MUDDER_ROOT) > 2) {
                @removeDir($dir);
            }
            // 从模块表删除
            $db->query("DELETE FROM {$dbpre}modules WHERE moduleid='$moduleid'");
            // 删除缓存配置文件
            @unlink(MUDDER_ROOT.'./data/cachefiles/cache_'.$moduleinfo['flag'].'_config.php');
            // 缓存
            cache('modules');
            cpmsg($moduleinfo['name'].'模块卸载完毕。','admincp.php?action=modoer&file=modules&op=manage');
            break;
        default:
            cpmsg('未定义的操作。','stop');
    }

}
?>