<?


  include_once '../../config.inc.php';
  include_once 'translate.php';
  include '../FCKeditor/fckeditor.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="100%" BORDER=0 CELLSPACING=0 CELLPADDING=5 BGCOLOR="#EFEFF8"><TR><TD VALIGN=top BGCOLOR="#BFBFDF" STYLE="border: 1px solid #505090;">

';
  if ($_POST['save'])
  {
    $handle = fopen ($documentroot . ('' . '/upload/templates/' . $_POST['old_tpl_name']), 'w+');
    $buffer = fputs ($handle, stripslashes (txt_2_src ($_POST['FCKeditor1'])));
    fclose ($handle);
  }

  if ($_POST['new_tpl'])
  {
    $handle = fopen ($documentroot . ('' . '/upload/templates/' . $_POST['new_tpl'] . '.tpl'), 'w+');
    $buffer = fputs ($handle, stripslashes (txt_2_src ($_POST['FCKeditor1'])));
    fclose ($handle);
    if (!($result = mysql_query ('SELECT * FROM styles WHERE active=\'yes\'')))
    {
      exit ('<b>Error:</b>' . mysql_error ());
      ;
    }

    $row = mysql_fetch_row ($result);
    unset ($SQL);
    for ($i = 0; $i < sizeof ($row); ++$i)
    {
      $SQL = $SQL . '\'' . $row[$i] . '\',';
    }

    $SQL = str_replace ($_POST['old_tpl_name'], $_POST['new_tpl'] . '.tpl', $SQL);
    $SQL = 'INSERT INTO styles VALUES(' . substr ($SQL, 0, -1) . ')';
    if (!(mysql_query ('UPDATE styles SET active=\'no\'')))
    {
      exit ('<b>Error:</b>' . mysql_error ());
      ;
    }

    if (!(mysql_query ($SQL)))
    {
      exit ('<b>Error:</b>' . mysql_error ());
      ;
    }
  }

  echo '
';
  if (((($_POST['skin'] AND !$_POST['save']) AND !$_POST['save_as']) AND !$_POST['new_tpl']))
  {
    $SQL = 'UPDATE styles SET active=\'no\'';
    if (!(mysql_query ($SQL)))
    {
      exit ('<b>Error:</b>' . mysql_error ());
      ;
    }

    $SQL = '' . 'UPDATE styles SET active=\'yes\' WHERE skin=\'' . $_POST['skin'] . '\'';
    if (!(mysql_query ($SQL)))
    {
      exit ('<b>Error:</b>' . mysql_error ());
      ;
    }
  }

  if (!($result = mysql_query ('SELECT * FROM styles WHERE active=\'yes\'')))
  {
    exit ('Invalid query3');
    ;
  }

  $row = mysql_fetch_array ($result);
  echo '
<FORM name="form" ACTION="';
  echo $php_self;
  echo '" METHOD=post>
<input type="hidden" name="old_tpl_name" value="';
  echo $row['skin'];
  echo '">



';
  echo $admin_Templates;
  echo '	';
  echo '<S';
  echo 'ELECT NAME="skin" onchange="javascript:submit()">
	';
  if ($dir = @opendir ($documentroot . '/upload/templates'))
  {
    while ($file = readdir ($dir) !== false)
    {
      if (($file != '.' AND $file != '..'))
      {
        echo '					<OPTION VALUE="';
        echo $file;
        echo '" ';
        if ($row['skin'] == $file)
        {
          echo 'selected';
        }

        echo '>';
        echo $file;
        echo '</OPTION>
			';
        continue;
      }
    }
  }

  closedir ($dir);
  echo '	</SELECT>
<input type="submit" name="save" value="';
  echo $admin_save;
  echo '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<font color="red">';
  echo $admin_Save_current_template_as;
  echo '</font>
<input type="text" name="new_tpl">

<input type="submit" name="save_as" value="';
  echo $admin_save_as;
  echo '">
<hr>
';
  $oFCKeditor = new FCKeditor ('FCKeditor1');
  $oFCKeditor->BasePath = $sBasePath;
  if (file_exists ($documentroot . ('' . '/upload/templates/' . $row['skin'])))
  {
    $SRC = join ('', file ($documentroot . ('' . '/upload/templates/' . $row['skin'])));
  }

  if ($SRC != '')
  {
    $SRC = src_2_txt ($SRC);
    $oFCKeditor->Value = str_replace ('
', '', $SRC);
  }
  else
  {
    $oFCKeditor->Value = '' . 'Template <b>' . $row['skin'] . '</b> not found!';
  }

  $oFCKeditor->Width = '80%';
  $oFCKeditor->Height = '700';
  $oFCKeditor->Config['DefaultLanguage'] = 'en';
  $oFCKeditor->Create ();
  echo '</form>
<br></TD></TR></TABLE></BODY></HTML>';
?>
