<?PHP
if(stristr($_SERVER['SERVER_SOFTWARE'],'microsoft')){
         error_reporting("E_ALL");
}
include '../library/session_save_path.php';
session_start();

$chars = "abcdefghijklmnopqrstuvwxyz";
$len = strlen($chars);
mt_srand((double)microtime() * 1000000);
for ($i = 0; $i < 15; $i++)
{
    $randomize = mt_rand(0, $len);
    $imagename .= $chars{$randomize};
}

?>

<?PHP
//---------------------------------------------------------
@include '../include/mysql.php';
@include '../include/global.php';

$authcommands = 'AND STATUS = \'1\'';
$login_form = 'authorize_form_admin.php';
@include '../library/authorize.php';

@include 'gd.php';

$sql_error = '<h1>I\'m givin\' her all she\'s got, Captain, but we need more power!</h1><b><i>This error said :</i></b><br>';
//---------------------------------------------------------

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

//----------------
$sql_active_accounts = "SELECT count(*) FROM FEATURES";
$res_active_accounts = @mysql_query($sql_active_accounts);
$row_active_accounts = @mysql_fetch_row($res_active_accounts);
        $num_features = $row_active_accounts[0];

$i=1;
while($i<=$num_features) {
        $string = "FEATURE_$i";
        if ($_POST[$string] != $nil) {
                $feature_prep = $_POST[$string];
                $feature_query = "$feature_query$feature_prep,";
        }


        $i++;
}

//-------------------------

        if ($_GET['addphoto'] == '1' && $table != SETTINGS) {
        $SQL         = "SELECT * from LISTINGS WHERE ID = '$id'";
        $result = @mysql_query( $SQL );
        $row         = @mysql_fetch_array( $result );
        $original = $row[IMAGEPOT];
        }

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` ) )");
        //--
        mysql_query ("ALTER TABLE `LISTINGS` CHANGE `PRICE` PRICE INT(20) NOT NULL");
        mysql_query ("ALTER TABLE `LISTINGS` CHANGE `BEDROOMS` BEDROOMS INT(20) NOT NULL");
        mysql_query ("ALTER TABLE `LISTINGS` CHANGE `BATHROOMS` BATHROOMS INT(20) NOT NULL");
        //--

//---------------------------------------------------------

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

$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 ($q = 1);

if ($q == 1) {

        $_POST['JBPOT'] = addslashes($_POST['JBPOT']);

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

        $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;

                $d = htmlentities($d, ENT_QUOTES);

        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(error66);
}

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

//--------------------------------------------------------- [ TABLE TYPE HACK ]
if (empty($hack)) { } else {
$pieces = explode("/", $hack);
$h0 = $pieces[0];
$h1 = $pieces[1];
$h2 = $pieces[2];
$h3 = $pieces[3];
$h4 = $pieces[4];
$h5 = $pieces[5];


mysql_query ("ALTER TABLE `$table` $h0 $h1 $h2 $h3 $h4 $h5");
$query = mysql_query($sql) or die("$sql_error" . mysql_error());
}

//---------------------------------------------------------
if (empty($HTTP_POST_FILES['1'])) $HTTP_POST_FILES['1'] = $_FILES['1'];
$file  = $HTTP_POST_FILES['1']['tmp_name'];
if ($file != $nil && $HTTP_POST_FILES['1'][type] == 'image/jpeg' || $HTTP_POST_FILES['1'][type] == 'image/pjpeg' || $HTTP_POST_FILES['1'][type] == 'image/png') {

if ($HTTP_POST_FILES['1'][type] == 'image/jpeg' || $HTTP_POST_FILES['1'][type] == 'image/pjpeg')
{ $ext = jpg; 
@unlink ("$svr_rootscript/product_images/$_POST[IMAGENAME].png");
}

if ($HTTP_POST_FILES['1'][type] == 'image/png')
{ $ext = png;
@unlink ("$svr_rootscript/product_images/$_POST[IMAGENAME].jpg");
}

        move_uploaded_file($HTTP_POST_FILES[1]['tmp_name'], "$svr_rootscript/product_images/$_POST[IMAGENAME].$ext");

}
?>


<?PHP

function EdURL($uttl)
{
$uttl=str_replace("$","","$uttl");
$uttl=str_replace("&","","$uttl");
$uttl=str_replace("=","","$uttl");
$uttl=str_replace("?","","$uttl");
$uttl=str_replace("`","","$uttl");
$uttl=str_replace(":","","$uttl");
$uttl=str_replace("<","","$uttl");
$uttl=str_replace(">","","$uttl");
$uttl=str_replace("[","","$uttl");
$uttl=str_replace("]","","$uttl");
$uttl=str_replace("{","","$uttl");
$uttl=str_replace("}","","$uttl");
$uttl=str_replace("\"","","$uttl");
$uttl=str_replace("+","","$uttl");
$uttl=str_replace("%","","$uttl");
$uttl=str_replace("@","","$uttl");
$uttl=str_replace("/","","$uttl");
$uttl=str_replace(";","","$uttl");
$uttl=str_replace("\\","","$uttl");
$uttl=str_replace("^","","$uttl");
$uttl=str_replace("|","","$uttl");
$uttl=str_replace("~","","$uttl");
$uttl=str_replace("'","","$uttl");
$uttl=str_replace(",","","$uttl");
$uttl=str_replace("#","","$uttl");
$uttl=str_replace("(","","$uttl");
$uttl=str_replace("_","","$uttl");
$uttl=str_replace("!","","$uttl");
$uttl=str_replace(".","","$uttl");
$uttl=str_replace("  "," ","$uttl");
$uttl=str_replace(" ","-","$uttl");
$uttl=urlencode($uttl);
return $uttl;
}

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

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

$somecontent = "
RewriteEngine on
$pot";
unlink("$svr_rootscript/.htaccess");
fopen("$svr_rootscript/.htaccess", "x+");
$filename = "$svr_rootscript/.htaccess";

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

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

?>

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