$fdir = opendir($apcms['path'] . "/plugins");
while ($thisplugin = readdir($fdir)) {
    if (ereg("^apcms", $thisplugin)) {
        require_once $PATH . "/plugins/" . $thisplugin . "/" . $thisplugin . "." . $SUFFIX;
        $plugobj[$thisplugin] = new $thisplugin(true);
        $LOCPLUGINS['name'][] = trim($thisplugin);
        $LOCPLUGINS['plugin'][] = apcms_Strip($plugobj[$thisplugin]->plugin);
        $LOCPLUGINS['version'][] = apcms_Strip($plugobj[$thisplugin]->version);
        $LOCPLUGINS['description'][] = apcms_Strip($plugobj[$thisplugin]->description);
        $LOCPLUGINS['author_name'][] = apcms_Strip($plugobj[$thisplugin]->author_name);
        $LOCPLUGINS['author_email'][] = apcms_Strip($plugobj[$thisplugin]->author_email);
        $LOCPLUGINS['homepage'][] = apcms_Strip($plugobj[$thisplugin]->author_homepage);
    }
}
$ALLPLUGINS = array();
$return = apcms_GetHTML('http://apcms.php-programs.de/download/plugins/plugin.lst');
$pluginlist = preg_replace("`^.*\\s*\n\\s*\n\\s*`isU", "", $return['html']);
$plistarray = explode("\n", trim($pluginlist));
for ($a = 0; $a < count($plistarray); $a++) {
    $linear = explode("\t", trim($plistarray[$a]));
    if (in_array(trim($linear[0]), $LOCPLUGINS['name'])) {
        $pversion = apcms_Strip($plugobj[trim($linear[0])]->version);
        if (version_compare($pversion, apcms_Strip($linear[2]), "<")) {
            $ALLPLUGINS['name'][] = trim($linear[0]);
            $ALLPLUGINS['plugin'][] = apcms_Strip($linear[1]);
            $ALLPLUGINS['version'][] = apcms_Strip($linear[2]);
            $ALLPLUGINS['description'][] = apcms_Strip($linear[6]);
            $ALLPLUGINS['author_name'][] = apcms_Strip($linear[3]);
            $ALLPLUGINS['author_email'][] = apcms_Strip($linear[4]);
            $ALLPLUGINS['homepage'][] = apcms_Strip($linear[5]);
        } elseif (version_compare($pversion, apcms_Strip($linear[2]), ">")) {
$AOUT .= "\t<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">\n";
$AOUT .= "\t\t<tr class=\"adminmain2\">\n";
$AOUT .= "\t\t\t<td>\n";
$AOUT .= "\t\t\t\t" . $apcms['LANGUAGE']['ADMIN_WELCOMEMSG1'] . "\n";
$AOUT .= "\t\t\t</td>\n";
$AOUT .= "\t\t</tr>\n";
$AOUT .= "\t</table>\n";
$AOUT .= "</div><br />\n";
$AOUT .= "\n<div id=\"adminmain1\">\n";
$AOUT .= "\t<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"3\">\n";
$AOUT .= "\t\t<tr class=\"adminmain2\">\n";
$AOUT .= "\t\t\t<td colspan=\"2\">\n";
$AOUT .= "\t\t\t\t<b>" . $apcms['LANGUAGE']['ADMIN_SYSINFO'] . "</b>\n";
$AOUT .= "\t\t\t</td>\n";
$AOUT .= "\t\t</tr>\n";
$return = apcms_GetHTML('http://www.php-programs.de/versions_backend.php?what=apcms');
$available_version = ereg_replace(".*\\<version\\>([^\\<]+)\\<\\/version\\>.*", "\\1", apcms_Strip($return['html'], 1));
$available_version = apcms_Strip($available_version);
if (version_compare($apcms['version'], $available_version, "<")) {
    // update available!
    $class_local = "red";
    $class_official = "green";
} elseif (version_compare($apcms['version'], $available_version, ">")) {
    // local version is newer than official available version
    $class_local = "green";
    $class_official = "";
} else {
    // Local version is the most actual version
    $class_local = "green";
    $class_official = "green";
}
     $newplugin = new $plugin_name(true);
     $newplugin->update($oldversion);
     $config_items = $newplugin->config_items;
     $config = array();
     foreach ($config_items as $key => $val) {
         if (!in_array($key, $oldconf)) {
             $config[$key] = $config_items[$key]['default'];
         } else {
             $config[$key] = $oldconf[$key];
         }
     }
     $query = "INSERT INTO `" . $apcms['table']['global']['plugins'] . "` \n\t\t\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t\t\t`name`,\n\t\t\t\t\t\t\t\t\t\t`md5`,\n\t\t\t\t\t\t\t\t\t\t`config`, \n\t\t\t\t\t\t\t\t\t\t`version` \n\t\t\t\t\t\t) VALUES \t(\n\t\t\t\t\t\t\t\t\t\t'" . apcms_ESC($plugin_name) . "',\n\t\t\t\t\t\t\t\t\t\t'" . md5(apcms_ESC($plugin_name)) . "',\n\t\t\t\t\t\t\t\t\t\t'" . addslashes(serialize($config)) . "', \n\t\t\t\t\t\t\t\t\t\t'" . apcms_ESC($newplugin->version) . "'\n\t\t\t\t\t\t\t\t\t)";
     $db->unbuffered_query($query);
 } else {
     $url = "http://apcms.php-programs.de/download/plugins/" . $plugin_name . ".tar.gz";
     $return = apcms_GetHTML($url);
     $pplugin = preg_replace("`^.*\\s*\n\\s*\n\\s*`isU", "", $return['html']);
     $fp = fopen($apcms['path'] . "/plugins/" . $plugin_name . ".tar.gz", "w");
     fwrite($fp, $pplugin);
     fclose($fp);
     if (file_exists($apcms['path'] . "/plugins/" . $plugin_name . "/" . $plugin_name . "." . $SUFFIX)) {
         apcms_DeleteDirectory($apcms['path'] . "/plugins/" . $plugin_name);
     }
     require_once "Archive/Tar.php";
     $tar = new Archive_Tar($apcms['path'] . "/plugins/" . $plugin_name . ".tar.gz");
     $tar->extract($apcms['path'] . "/plugins/");
     @unlink($apcms['path'] . "/plugins/" . $plugin_name . ".tar.gz");
     apcms_ChmodDirectory($apcms['path'] . "/plugins/" . $plugin_name, 777);
     require_once $PATH . "/plugins/" . $plugin_name . "/" . $plugin_name . "." . $SUFFIX;
     $thisplugin = new $plugin_name(true);
     $thisplugin->install();