<?php
/**
* 插件管理
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id plugins.inc.php 1 2008-3-18 11:13 $
*/
(!defined('IN_ADMIN') || !defined('IN_MUDDER')) && exit('Access Denied');

if(!check_submit('pluginssubmit')) {

    if($action == 'modoer') {

        $query = $db->query("SELECT * FROM {$dbpre}plugins ORDER BY orders");
        $total = $db->num_rows($query);
        $HTML_list = '';
        if($total > 0) {
            while($row = $db->fetch_array($query)) {
                $id = $row['plugin_id'];
                $HTML_list .= "<tr></tr>\r\n";
                $HTML_list .= "\t<td><input type=\"checkbox\" name=\"ids[]\" value=\"$id\" /><input type=\"hidden\" name=\"pluginlist[$id][t]\" value=\"yes\" /></td>\r\n";
                $HTML_list .= "\t<td>{$row['name']}</td>\r\n";
                $HTML_list .= "\t<td>{$row['module']}</td>\r\n";
                $HTML_list .= "\t<td>".trimmed_title($row['description'], 20)."</td>\r\n";
                $HTML_list .= "\t<td>{$row['directory']}</td>\r\n";
                $HTML_list .= "\t<td><input type=\"checkbox\" name=\"pluginlist[$id][available]\" value=\"1\"".($row['available']?'checked="checked"':'')."/></td>\r\n";
                //<a href=\"admincp.php?action=$action&job=detail&id=$id\">查看</a>
                $HTML_list .= "\t<td>{$row['author']}</td>\r\n";
                $HTML_list .= "</tr>\r\n";
            }
        } else {
            $HTML_list .= "<tr><td colspan=\"7\">没有可用的插件。</td></tr>";
        }

        $exist = true;
        include cptpl('plugins');

    }

} else {

    if($action == 'modoer') {

        if($pluginlist || is_array($pluginlist)) {
            foreach($pluginlist as $id => $val) {
                $id = intval($id);
                if($ids && in_array($id, $ids)) {
                    $db->delete_new("{$dbpre}plugins", "plugin_id='$id'");
                } else {

                    $list['available'] = $val['available'] = intval(trim($val['available'])) ? 1 : 0;
                    $db->update_new("{$dbpre}plugins", "plugin_id='$id'", $list);
                }
            }
        }
        if($newplugin && $newplugin['name'] && $newplugin['directory']) {
            $newplugin['name'] = html_special_chars_ex($newplugin['name']);
            $newplugin['description'] = html_special_chars_ex($newplugin['description']);
            $newplugin['directory'] .= (substr(html_special_chars_ex($newplugin['directory']), -1) != '/') ? '/' : '';
            $pluginfile = MUDDER_PLUGINDIR.$newplugin['directory'].'plugin.inc.php';
            if(@!file_exists($pluginfile)) {
                cpmsg('无法查找插件（'.$newplugin['name'].'）的配置文件，请返回确认该插件的完整性。');
            }
            include $pluginfile;
            if(!$newplugin['module']) cpmsg('无知插件标识，请返回确认该插件的完整性。');
            $exist = $db->select_count("{$dbpre}plugins", "module='{$newplugin['module']}'") >= 1;
            $exist && cpmsg('该插件已存在，无法添加。');
            if(!$newplugin['menu'] || !is_array($newplugin['menu'])) {
                cpmsg('未配置插件管理菜单(menu)，请返回。');
            } else {
                $newplugin['module'] = html_special_chars_ex(addslashes(stripslashes($newplugin['module'])));
                $newplugin['directory'] = addslashes(stripslashes($newplugin['directory']));
                $newplugin['author'] = html_special_chars_ex(addslashes(stripslashes($newplugin['author'])));
                $newplugin['copyright'] = html_special_chars_ex(addslashes(stripslashes($newplugin['copyright'])));
                $newplugin['version'] = html_special_chars_ex(addslashes(stripslashes($newplugin['version'])));
                $newplugin['available'] = 1;
                $newplugin['actionlist'] = addslashes(serialize(html_special_chars_ex($newplugin['actionlist'])));
                $newplugin['menu'] = addslashes(serialize(html_special_chars_ex($newplugin['menu'])));
                $newplugin['config'] = addslashes(serialize(html_special_chars_ex($newplugin['config'])));

                $db->insert_new("{$dbpre}plugins", $newplugin);
            }
        }
        cache_plugins(); //更新插件
        cpmsg('插件更新完毕！');
    }

}


if(!$exist) {
    foreach($arr_plugins as $plugin_action) {
        if(empty($plugin_action)) continue;
        foreach($plugin_action as $k => $val) {
            if($k === 'title') continue;
            if($k === 'module') continue;
            list($m_action,,,,) = explode("|", $val);
            if($m_action==$action) {
                $plugin_directory = $_plugins[$plugin_action['module']]['directory']; 
                $exist = true;
                $file = MUDDER_PLUGINDIR.$plugin_directory.'admin/'.$action.'.inc.php';
                if(@file_exists($file)) {
                    include $file;
                    unset($plugin_directory, $file);
                } else {
                    cpmsg('插件管理文件不存在。', 'stop');
                }
                break;
            }
        }
    }
    unset($m_action);
}

if(!$exist) cpmsg('未知操作。', 'stop');

?>
