<?

include "config.php";
include "function.php";
include "getid3.php";

session_start();

unset ($body); // If someone tries to be funny...
if (!$conn) { $conn=connectdb(); }

// Is there a file that needs to be added?
if ($HTTP_SESSION_VARS["filetoadd"]) {
    $fileinfo=GetAllMP3info($filedir."/".$filetoadd);
    $result=mysql_query("insert into files (filename, title, artist, album, genreid, playtime, bitrate, chanelmode, frequency, year) values ('".addslashes($HTTP_SESSION_VARS["filetoadd"])."', '".addslashes(trim($songtitle))."', '".addslashes(trim($songartist))."', '".addslashes(trim($songalbum))."', '$songgenre', '".$fileinfo["playtime_seconds"]."', '".$fileinfo["bitrate"]."','".$fileinfo["channelmode"]."','".$fileinfo["frequency"]."','".$fileinfo["id3"]["id3v1"]["year"]."')", $conn);
	
	if (!$result) {
		$errormes.="Insert failed\n";
		$addthem=1;
	} else { 
		$noticemes.="Successfull";
		session_unregister("filetoadd");
		$addthem=1;
	}
}

// Check check
if (!$HTTP_SESSION_VARS["unregged_files"] && !$HTTP_SESSION_VARS["dead_db_entrys"]) {
    html_header();
	start_maintable();
	echo "Nothing to update (anymore).";
	end_maintable();
	html_footer();
    die;

// First delete DB entrys if nessecary.
} elseif ($HTTP_SESSION_VARS["dead_db_entrys"]) {
    // Confirm if user wants to delete
    if (!$deletethem && !$pleasedontdelete) {
        $body.="The following database entrys are not found in: $filedir:<br>\n";
        while (list ($key, $filename)=each($HTTP_SESSION_VARS["dead_db_entrys"])) {
            $body.="<li>$filename</li>\n";
        }
        $body.="Do you want to delete all the above database entry's?<br>\n";
        $body.="<a href=\"$PHP_SELF?pleasedontdelete=1\">Oh no! Please don't do it!</a> - <a href=\"$PHP_SELF?deletethem=1\">Yes, sure! Delete them!</a><br>\n";
    // Dont delete
    } elseif ($pleasedontdelete) {
        session_unregister("dead_db_entrys");
        $body.="Left the database as it was.<br>\n".
            "<a href=\"$PHP_SELF?continue=1\">Continue &gt;&gt;</a>";
    // Delete them
    } elseif ($deletethem) {
        while (list ($key, $filename)=each($HTTP_SESSION_VARS["dead_db_entrys"])) {
            $result=mysql_query("delete from files where filename='$filename'", $conn);
            if (!$result) { $errormes.="Error while deleting $filename from the database.\n"; }
            else { $deletecount++; }
        }
        $body.="Processing complete, total deleted database entry's: $deletecount<br>\n".
            "<a href=\"$PHP_SELF?continue=1\">Continue &gt;&gt;</a>";
        session_unregister("dead_db_entrys");
    }

// Second, add files to DB if nessecary.
} elseif ($HTTP_SESSION_VARS["unregged_files"]) {
    // Confirm if user wants to add
    if (!$addthem && !$pleasedontadd) {
        $body.="The following files are not found in the database:<br><br>\n";
        while (list ($key, $filename)=each($HTTP_SESSION_VARS["unregged_files"])) {
            $body.="<li>$filename</li>\n";
        }
        $body.="<br><br>\nDo you want to add all the above files into the database?<br>\n";
        $body.="<a href=\"$PHP_SELF?pleasedontadd=1\">Oh no! Please don't do it!</a> - <a href=\"$PHP_SELF?addthem=1\">Yes, sure! Add them!</a><br>\n";
    // Dont add
    } elseif ($pleasedontadd) {
        session_unregister("unregged_files");
        $body.="Left the database as it was.<br>\n".
            "<a href=\"$PHP_SELF?continue=1\">Continue &gt;&gt;</a>";
    // Add them
    } elseif ($addthem) {
        if (is_array($HTTP_SESSION_VARS["unregged_files"])) {
            $filetoadd=array_pop($HTTP_SESSION_VARS["unregged_files"]);
            session_register("filetoadd");
            $fileinfo=GetAllMP3info($filedir."/".$filetoadd);
            $songtitle=$fileinfo['title'];
            $songartist=$fileinfo['artist'];
            $songalbum=$fileinfo['album'];
            $result=mysql_query("select id from genres where description='".strtolower($fileinfo['genre'])."'", $conn);
            $rowgenre=mysql_fetch_row($result);
			$genreidtje=$fileinfo["id3"]["id3v1"]["genreid"];
			$genreidtje++;
            $body.="<form method=\"post\" action=\"$PHP_SELF\">\n".
                "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\">\n".
                "<tr><td>Current file:</td><td>$filetoadd</td></tr>\n".
				"<tr><td colspan=\"2\"><a href=\"playsongquick.php?file=".urlencode($filetoadd)."\" target=\"_blank\">Play this song</a></td></tr>".
                "<tr><td>Artist:</td><td><input type=\"text\" name=\"songartist\" value=\"".$fileinfo["id3"]["id3v1"]["artist"]."\"></td></tr>\n".
                "<tr><td>Song title:</td><td><input type=\"text\" name=\"songtitle\" value=\"".$fileinfo["id3"]["id3v1"]["title"]."\"></td></tr>\n".
                "<tr><td>Album:</td><td><input type=\"text\" name=\"songalbum\" value=\"".$fileinfo["id3"]["id3v1"]["album"]."\"></td></tr>\n".
                "<tr><td>Genre:</td><td>".get_genre('formselect', $genreidtje)."</td></tr>\n". //<input type=\"text\" name=\"songgenre\" value=\"".$rowgenre[0]."\">
				"<tr><td>&nbsp;</td><td><input type=\"submit\" name=\"submitsong\" value=\"Use this information\"></td></tr>\n".
                "</table>\n".
                "</form>\n";
        } else {
            session_unregister("unregged_files");
            $body.="All files added to DB.";
        }
    }
}

// Show output
html_header();
start_maintable();
echo $body;
end_maintable();
html_footer();
?>