/**
 * This functions remove a plugin from the OCS webconsole and database.
 * Delete all created menu entries and all plugin related code
 * 
 * @param integer $pluginid : Plugin id in DB
 */
function delete_plugin($pluginid)
{
    global $l;
    $conn = new PDO('mysql:host=' . SERVER_WRITE . ';dbname=' . DB_NAME . '', COMPTE_BASE, PSWD_BASE);
    $query = $conn->query("SELECT * FROM `plugins` WHERE id = '" . $pluginid . "'");
    $anwser = $query->fetch();
    if (!class_exists('plugins')) {
        require 'plugins.class.php';
    }
    if (!function_exists('exec_plugin_soap_client')) {
        require 'functions_webservices.php';
    }
    if ($anwser['name'] != "" and $anwser['name'] != null) {
        require MAIN_SECTIONS_DIR . "ms_" . $anwser['name'] . "/install.php";
        $fonc = "plugin_delete_" . $anwser['name'];
        $fonc();
    }
    rrmdir(MAIN_SECTIONS_DIR . "ms_" . $anwser['name']);
    rrmdir(PLUGINS_DIR . "computer_detail/cd_" . $anwser['name']);
    if (file_exists(PLUGINS_SRV_SIDE . $anwser['name'] . ".zip")) {
        unlink(PLUGINS_SRV_SIDE . $anwser['name'] . ".zip");
        exec_plugin_soap_client($anwser['name'], 0);
    }
    $conn->query("DELETE FROM `" . DB_NAME . "`.`plugins` WHERE `plugins`.`id` = " . $pluginid . " ");
}
} else {
    echo "<center>\t\n    <div id='my-alert-top_msg_alert' class='alert alert-warning fade in' role='alert'>\t\n\t\t<div onmouseout='hidden_me();' onmouseover='show_me('Put your plugins into the download dir in the OCSreports.');'>\n\t\t\tThere is currently no plugin available for installation.</div>\n    </div>\n\t</center>";
}
echo close_form();
echo "</th></table>";
if (isset($protectedPost['plugin'])) {
    $pluginArchive = $protectedPost['plugin'];
    $bool = install($pluginArchive);
    if ($bool) {
        $pluginame = explode(".", $pluginArchive);
        $plugintab = array("name" => $pluginame[0]);
        $isok = check($plugintab);
        mv_computer_detail($pluginame[0]);
        $result = mv_server_side($pluginame[0]);
        if ($result) {
            exec_plugin_soap_client($pluginame[0], 1);
        }
        if ($isok) {
            echo "<center>\n\t\t\t\t<div id='my-alert-' class='alert alert-success fade in' role='alert'>Plugin " . $pluginame[0] . " installed</div>\n\t\t\t</center>";
        } else {
            echo "<center>\n\t\t\t<div id='my-alert-top_msg_alert' class='alert alert-danger fade in' role='alert'>Error : " . $pluginame[0] . " is an invalid plugin, check your sources.\n\t\t\t\t\t<br> Installation aborted ! </div>\n\t\t\t</center>";
        }
    } else {
        echo "<center>\t\n    <div id='my-alert-top_msg_alert' class='alert alert-danger fade in' role='alert'>\t\n\t\t<div onmouseout='hidden_me();' onmouseover='show_me('Put your plugins into the download dir in the OCSreports.');'>\n\t\t\tThis plugin has been already installed.</div>\n    </div>\n\t</center>";
    }
}
// Plugins Tab
printEnTete("Installed Plugins");
$form_name = "show_all_plugins";
$table_name = $form_name;
$tab_options = $protectedPost;