<?PHP
//.......................................

$sql_error = "<h1>MySQL Query Error:</h1><br>";

//.......................................

$controller     = $_POST['controller'];
$pieces         = explode("~", $controller);
$table          = $pieces[0];
$qt             = $pieces[1];
$return         = $pieces[2];
$id             = $pieces[3];

if ($qt == insert)
        $qt = 'INSERT INTO';
if ($qt == update) {
        $qt = 'UPDATE';
        $end = "WHERE ID = '$id'";
}

$pre = array_keys( $_POST );

//.......................................

mysql_query ("CREATE TABLE IF NOT EXISTS `$table` (`ID` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY ( `id` ) )");

//.......................................

$count = count($pre);
$count = $count - 2;
$sql = "$qt $table SET";
for ($i=0; $i < $count; $i++) {
        $x=$i+1;
        $y = $_POST[$pre[$x]];
        $d = $y;

        mysql_query ("ALTER TABLE `$table` ADD `$pre[$x]` TEXT NOT NULL");
        $sql .= " `$pre[$x]` = '$d',";
}

$sql .= " ID = '$id' $end";
$query = mysql_query($sql) or die("$sql_error" . mysql_error());


//.......................................

$thistable = "$prefix" . "OBJECTS";
$thattable = "$prefix" . "CATEGORIES";
if ($table == $thistable) {
        $SQL = "SELECT * from $thattable ORDER BY TITLE ASC";
        $result = @mySQL_query( $SQL );
        while( $row = @mySQL_fetch_array( $result ) ) {
        $category = $row['ID'];

                $SQLbeta = "SELECT * from $thistable WHERE ID = '$id'";
                $resultbeta = mySQL_query( $SQLbeta );
                $rowbeta = mySQL_fetch_array( $resultbeta );
                if (!empty($rowbeta[$category]))
                $catstring = "-$_POST[$category]-$catstring";
        }

        $sql            = "UPDATE $table SET
        CATEGORY        = '$catstring'
        WHERE ID        = '$id'";
        $query          = mysql_query($sql);
}

//.......................................
$css = "AddType application/x-httpd-php .css";

$SQL = "SELECT * from $thattable";
$result = @mysql_query( $SQL ); while( $row = @mysql_fetch_array( $result ) ) {
        $row[TITLE] = EdURL($row[TITLE]);
        $new = "RewriteRule ^$row[TITLE]$ index.php?page=browse&id=$row[ID] [NC,L]";
        $pot = "$new\n$pot";
}

$SQL = "SELECT * from $thistable";
$result = @mysql_query( $SQL ); while( $row = @mysql_fetch_array( $result ) ) {
        $row[TITLE] = EdURL($row[TITLE]);
        $new = "RewriteRule ^$row[TITLE].html$ index.php?page=focus&id=$row[ID] [NC,L]";
        $pot = "$new\n$pot";
}

$somecontent = "RewriteEngine on
$css
$pot";

unlink("../.htaccess");
fopen("../.htaccess", "x+");
$filename = "../.htaccess";

if (is_writable($filename)) {
        if (!$handle = fopen($filename, 'w+')) {
                echo "Error ($filename)."; exit;
        }

        if (fwrite($handle, $somecontent) === FALSE) {
                echo "Error ($filename)."; exit;
        }
}

//.......................................

if (empty($_GET['return'])) { 

        $filearray = array();
        if ($fil = opendir("../admin/")) {
                while (($file = @readdir($fil)) !== false) {
                        if ($file != "." && $file != "..") {
                                $filearray[] = $file;
                                        if ($file == $return) {
                                                $pieces = explode(".", "$return");
                                                $rpage = $pieces[0];
                                                $_GET[page] = "$rpage";
                                                include "$return";
                                        }
                        }
                }
                @closedir($fil);
        }        

}

//.......................................
?>

<?PHP
if (!empty($_GET['return'])) { 
$pieces = explode("-", "$_GET[return]");
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>One moment please...</title>
<meta http-equiv="REFRESH" content="0;url=../index.php?page=<?php echo $pieces[0]; ?>&id=<?php echo $pieces[1]; ?>"></HEAD>
<BODY></BODY>
</HTML>

<?PHP } ?>

<?PHP
if (!empty($_GET['langreturn'])) {
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>One moment please...</title>
<meta http-equiv="REFRESH" content="0;url=index.php?page=<?php echo $_GET[langreturn]; ?>&id=<?php echo $_GET[id]; ?>"></HEAD>
<BODY></BODY>
</HTML>

<?PHP } ?>

