<?

  include_once '../../config.inc.php';
  if (!include '../autorization.php')
  {
    echo 'Access denied!';
    exit ();
  }

  if (!($result_proper = mysql_query ('select * from properties')))
  {
    exit ('<b>Error:</b>' . mysql_error ());
    ;
  }

  $proper = mysql_fetch_array ($result_proper);
  include '../lang/' . $proper['admin_language'];
  echo '
<HTML><BODY STYLE="color: Navy;"><LINK HREF="../style.css" REL="stylesheet" TYPE="text/css"><TABLE WIDTH="600" BORDER=0 CELLSPACING=0 CELLPADDING=1><TR><TD style="background-color:#505090;"><TABLE WIDTH="100%" BORDER=0 CELLSPACING=0 CELLPADDING=5 BGCOLOR="#EFEFF8">
<TR><TD VALIGN=top BGCOLOR="#BFBFDF" STYLE="border-bottom: 1px solid #505090;"><FONT COLOR="navy"><B>';
  echo $admin_Manage_Categories;
  echo '</B></FONT></TD></TR><TR><TD VALIGN=top>

';
  echo '<S';
  echo 'CRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function confirmSubmit(record) 
{
if (confirm("Are you sure you wish to delete category \\"" + record + "\\"?"))
	return true ;
else
	return false ;
}
//-->
</SCRIPT>




';
  if ($_POST['edit'])
  {
    $_POST['modified'] = trim ($_POST['modified']);
    if (!(mysql_query ('' . 'UPDATE categories SET name=\'' . $_POST['modified'] . '\',protect=\'' . $_POST['protect'] . '\' WHERE id=' . $_POST['id'])))
    {
      exit ('Query failed');
      ;
    }

    if ($_FILES['image']['name'])
    {
      if (($_POST['Old_Image_Name'] AND file_exists ($documentroot . ('' . '/upload/cat_images/' . $_POST['Old_Image_Name']))))
      {
        unlink ($documentroot . ('' . '/upload/cat_images/' . $_POST['Old_Image_Name']));
      }

      if (copy ($_FILES['image']['tmp_name'], $documentroot . ('' . '/upload/cat_images/' . $_POST['modified'] . '.') . substr ($_FILES['image']['type'], 6)))
      {
        unlink ($_FILES['image']['tmp_name']);
      }
    }
  }

  echo '

';
  if ($_GET['delete'])
  {
    if (!(mysql_query ('' . 'DELETE FROM categories WHERE id=' . $_GET['delete'])))
    {
      exit ('Query failed');
      ;
    }

    if (!(mysql_query ('' . 'DELETE FROM sub_categories WHERE parent=\'' . $_GET['delete'] . '\'')))
    {
      exit ('Query failed00');
      ;
    }

    if (!($result0 = mysql_query ('select * from categories')))
    {
      exit ('Query failed');
      ;
    }

    unset ($ALLOWED_CAT);
    while ($row0 = mysql_fetch_array ($result0))
    {
      $ALLOWED_CAT = $ALLOWED_CAT . $row0['id'] . '
';
    }

    $ALLOWED_CAT = trim ($ALLOWED_CAT);
    if (!(mysql_query ('' . 'UPDATE users SET allowed=\'' . $ALLOWED_CAT . '\' WHERE root=\'0\'')))
    {
      exit ('Query failed');
      ;
    }
  }

  if ($_GET['order'])
  {
    if (!($result = mysql_query ('SELECT * FROM categories')))
    {
      exit ('Query failed4');
      ;
    }

    $i = 0;
    while ($row = mysql_fetch_array ($result))
    {
      if ($_GET['id'] == $row['id'])
      {
        $AR_MATCH = $i;
        $AR_MATCH_NAME = $row['name'];
        $AR_MATCH_PROTECT = $row['protect'];
      }

      $AR_ID[$i] = $row['id'];
      $AR_NAME[$i] = $row['name'];
      $AR_PROTECT[$i++] = $row['protect'];
    }

    if ($_GET['order'] == 'up')
    {
      if (0 < $AR_MATCH)
      {
        $AR_ID[$AR_MATCH] = $AR_ID[$AR_MATCH - 1];
        $AR_ID[$AR_MATCH - 1] = $_GET['id'];
        $AR_NAME[$AR_MATCH] = $AR_NAME[$AR_MATCH - 1];
        $AR_NAME[$AR_MATCH - 1] = $AR_MATCH_NAME;
        $AR_PROTECT[$AR_MATCH] = $AR_PROTECT[$AR_MATCH - 1];
        $AR_PROTECT[$AR_MATCH - 1] = $AR_MATCH_PROTECT;
      }
    }
    else
    {
      if ($AR_MATCH < sizeof ($AR_ID) - 1)
      {
        $AR_ID[$AR_MATCH] = $AR_ID[$AR_MATCH + 1];
        $AR_ID[$AR_MATCH + 1] = $_GET['id'];
        $AR_NAME[$AR_MATCH] = $AR_NAME[$AR_MATCH + 1];
        $AR_NAME[$AR_MATCH + 1] = $AR_MATCH_NAME;
        $AR_PROTECT[$AR_MATCH] = $AR_PROTECT[$AR_MATCH + 1];
        $AR_PROTECT[$AR_MATCH + 1] = $AR_MATCH_PROTECT;
      }
    }

    if (!(mysql_query ('DELETE FROM categories')))
    {
      exit ('Query failed UP');
      ;
    }

    for ($i = 0; $i < sizeof ($AR_ID); ++$i)
    {
      $SQL = '' . 'INSERT INTO categories VALUES(' . $AR_ID[$i] . ',\'' . addslashes ($AR_NAME[$i]) . ('' . '\',\'' . $AR_PROTECT[$i] . '\')');
      if (!(mysql_query ($SQL)))
      {
        exit ('Query failed');
        ;
      }
    }

    if (!($result0 = mysql_query ('select * from categories')))
    {
      exit ('Query failed');
      ;
    }

    unset ($ALLOWED_CAT);
    while ($row0 = mysql_fetch_array ($result0))
    {
      $ALLOWED_CAT = $ALLOWED_CAT . $row0['id'] . '
';
    }

    $ALLOWED_CAT = trim ($ALLOWED_CAT);
    if (!(mysql_query ('' . 'UPDATE users SET allowed=\'' . $ALLOWED_CAT . '\' WHERE root=\'0\'')))
    {
      exit ('Query failed');
      ;
    }
  }

  if ($_GET['order'] == 'down')
  {
  }

  echo '




<form action="';
  echo $php_self;
  echo '" method="post" enctype="multipart/form-data">
';
  echo $admin_Name;
  echo '<BR>
<INPUT TYPE=text NAME="category" SIZE=40><BR><br>
';
  echo $admin_Lock;
  echo ' <INPUT TYPE=radio NAME="protect" VALUE="on">';
  echo $admin_On;
  echo '<INPUT TYPE=radio NAME="protect" VALUE="off" CHECKED>';
  echo $admin_Off;
  echo '<BR><br>
';
  echo $admin_Picture;
  echo ' <input type="file" name="image" size="31"><br><br>
<INPUT TYPE=submit NAME="add" VALUE="';
  echo $admin_Add_Category;
  echo '">
</FORM>


';
  if (($_POST['add'] AND $_POST['category'] != ''))
  {
    if (!($result0 = mysql_query ('select max(id) as id from categories')))
    {
      exit ('Query failed');
      ;
    }

    $row0 = mysql_fetch_array ($result0);
    if (!($result = mysql_query ('select * from categories')))
    {
      exit ('Query failed');
      ;
    }

    $row = mysql_fetch_array ($result);
    if ($row)
    {
      $CATEGORIES = join ('
', $row);
    }

    if (!strstr ($CATEGORIES, $_POST['category']))
    {
      $SQL = 'INSERT INTO categories VALUES(' . ($row0[0] + 1) . ('' . ',\'' . $_POST['category'] . '\',\'' . $_POST['protect'] . '\')');
      if (!(mysql_query ($SQL)))
      {
        exit ('Query failed');
        ;
      }
    }
    else
    {
      echo '<S';
      echo 'CRIPT LANGUAGE="JavaScript" TYPE="text/javascript">alert("Category \\"';
      echo $_POST['category'];
      echo '\\" already exist!")</SCRIPT>';
    }

    if ($_FILES['image']['name'])
    {
      if (strstr ($_FILES['image']['type'], 'flash'))
      {
        if (copy ($_FILES['image']['tmp_name'], $documentroot . ('' . '/upload/cat_images/' . $_POST['category'] . '.swf')))
        {
          unlink ($_FILES['image']['tmp_name']);
        }
      }
      else
      {
        if (copy ($_FILES['image']['tmp_name'], $documentroot . ('' . '/upload/cat_images/' . $_POST['category'] . '.') . substr ($_FILES['image']['type'], 6)))
        {
          unlink ($_FILES['image']['tmp_name']);
        }
      }
    }

    if (!($result0 = mysql_query ('select * from categories')))
    {
      exit ('Query failed');
      ;
    }

    unset ($ALLOWED_CAT);
    while ($row0 = mysql_fetch_array ($result0))
    {
      $ALLOWED_CAT = $ALLOWED_CAT . $row0['id'] . '
';
    }

    $ALLOWED_CAT = trim ($ALLOWED_CAT);
    if (!(mysql_query ('' . 'UPDATE users SET allowed=\'' . $ALLOWED_CAT . '\' WHERE root=\'0\'')))
    {
      exit ('Query failed');
      ;
    }
  }

  echo '

<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=3>
<TR BGCOLOR="#505090">
<TD><FONT COLOR="#FFFFFF">';
  echo $admin_table1[0];
  echo '</FONT></TD>
<TD><FONT COLOR="#FFFFFF">';
  echo $admin_table1[1];
  echo '</FONT></TD>
<TD><FONT COLOR="#FFFFFF">';
  echo $admin_table1[2];
  echo '</FONT></TD>
<TD><FONT COLOR="#FFFFFF">';
  echo $admin_table1[3];
  echo '</FONT></TD>
<td><FONT COLOR="#FFFFFF">';
  echo $admin_table1[4];
  echo '</FONT></td>
</tr>

';
  if (!($result = mysql_query ('SELECT * FROM categories')))
  {
    exit ('Query failed');
    ;
  }

  $i = 0;
  while ($row = mysql_fetch_array ($result))
  {
    echo '					<TR bgcolor="';
    if ($i++ % 2)
    {
      echo '#BFBFDF';
    }
    else
    {
      echo '#DCDCED';
    }

    echo '">

					<TD ALIGN=center>
						<A HREF="categories.php?id=';
    echo $row['id'];
    echo '&order=down"><IMG SRC="../images/down.gif" ALT="Down" BORDER=0></A>
						<A HREF="categories.php?id=';
    echo $row['id'];
    echo '&order=up"><IMG SRC="../images/up.gif" ALT="Up" BORDER=0></A>
					</TD>

					<TD width=233>';
    echo $row['name'];
    echo '</TD>
					<td align="center">
					';
    if ($dir = @opendir ($documentroot . '/upload/cat_images'))
    {
      while ($file = readdir ($dir) !== false)
      {
        if (strstr ($file, $row['name']))
        {
          echo '						<img src="../images/camera.gif" alt="" border="0" align="middle">
						';
          continue;
        }
      }

      closedir ($dir);
    }

    echo '					</td>
					<TD ALIGN=center>';
    if ($row['protect'] == 'on')
    {
      echo '<FONT COLOR="#FF0000">';
      echo $admin_On;
      echo '</FONT>';
    }
    else
    {
      echo $admin_Off;
    }

    echo '</TD>					

					<TD ALIGN=center>
						<A HREF="cat_edit.php?id=';
    echo $row['id'];
    echo '" STYLE="color:blue; font-size:11"><IMG SRC="../images/edit.gif" ALT="Edit" BORDER=0></A>
						<A onclick="return confirmSubmit(\'';
    echo urlencode ($row['name']);
    echo '\')" HREF="categories.php?delete=';
    echo $row['id'];
    echo '" STYLE="color:red; font-size:11"><IMG SRC="../images/delete.gif" ALT="Delete" BORDER=0></A>
						<A HREF="sub.php?id=';
    echo $row['id'];
    echo '"><IMG SRC="../images/sub.gif" ALT="Sub Categories" BORDER=0></A></TD>
					</TR>
			';
  }

  echo '</TABLE>




</TD></TR></TABLE></TD></TR></TABLE></BODY></HTML>';
?>
