<?php
$title = "Template &amp; file editing";

function add_magic_quotes($array) {
	foreach ($array as $k => $v) {
		if (is_array($v)) {
			$array[$k] = add_magic_quotes($v);
		} else {
			$array[$k] = addslashes($v);
		}
	}
	return $array;
} 

function validate_file($file) {
	if ('..' == substr($file,0,2))
		die (_LANG_WAT_SORRY_EDIT);
	
	if (':' == substr($file,1,1))
		die (_LANG_WAT_SORRY_PATH);

	if ('/' == substr($file,0,1))
		$file = '.' . $file;
	
	$file = stripslashes($file);
	$file = str_replace('../', '', $file);

    return $file;
}

if (!get_magic_quotes_gpc()) {
	$_GET    = add_magic_quotes($_GET);
	$_POST   = add_magic_quotes($_POST);
	$_COOKIE = add_magic_quotes($_COOKIE);
}

$wpvarstoreset = array('action','standalone','redirect','profile','error','warning','a','file');
for ($i=0; $i<count($wpvarstoreset); $i += 1) {
	$wpvar = $wpvarstoreset[$i];
	if (!isset($$wpvar)) {
		if (empty($_POST["$wpvar"])) {
			if (empty($_GET["$wpvar"])) {
				$$wpvar = '';
			} else {
				$$wpvar = $_GET["$wpvar"];
			}
		} else {
			$$wpvar = $_POST["$wpvar"];
		}
	}
}

switch($action) {

case 'update':

	$standalone = 1;
	require_once("admin-header.php");

	if ($user_level < 5) {
		die(_LANG_WAT_LEVEL_ERR);
	}

	$newcontent = stripslashes($_POST['newcontent']);
	$file = $_POST['file'];
    $file = validate_file($file);
	$real_file = '../' . $file;
    if (is_writeable($real_file)) {
        $f = fopen($real_file, 'w+');
        fwrite($f, $newcontent);
        fclose($f);
        header("Location: templates.php?file=$file&a=te");
    } else {
        header("Location: templates.php?file=$file");
    }

	exit();

break;

default:

	require_once('admin-header.php');

	if ($user_level <= 5) {
		die(_LANG_WAT_LEVEL_ERR);
	}

	if ('' == $file) {
		if ('' != get_settings('blogfilename')) {
			$file = get_settings('blogfilename');
		} else {
			$file = 'index.php';
		}
	}

    $file = validate_file($file);	
	$real_file = '../' . $file;
	
	if (!is_file($real_file))
		$error = 1;

	if ((substr($file,0,2) == 'wp') and (substr($file,-4,4) == '.php') and ($file != 'wp.php'))
		$warning = _LANG_WAT_CAREFUL_EDIT;
	
	if (!$error) {
		$f = fopen($real_file, 'r');
		$content = fread($f, filesize($real_file));
		$content = htmlspecialchars($content);
//		$content = str_replace("</textarea","&lt;/textarea",$content);
	}

	?>
<?php if (isset($_GET['a'])) : ?>
 <div class="updated"><p>_LANG_WAT_EDITED_SUCCESS</p></div>
<?php endif; ?>
 <div class="wrap"> 
  <?php
	echo "<p>" . sprintf(_LANG_WAT_EDIT_ING, $file) . " $warning</p>";
	
	if (!$error) {
	?> 
  <form name="template" action="templates.php" method="post"> 
     <textarea cols="80" rows="21" style="width:98%; font-family: 'Courier New', Courier, monopace; font-size:small;" name="newcontent" tabindex="1"><?php echo $content ?></textarea> 
     <input type="hidden" name="action" value="update" /> 
     <input type="hidden" name="file" value="<?php echo $file ?>" /> 
     <p class="submit">
     <?php
		if (is_writeable($real_file)) {
			echo "<input type='submit' name='submit' value='" . _LANG_WAT_UPTEXT_TEMP . "' tabindex='2' />";
		} else {
			echo "<input type='button' name='oops' value='" . _LANG_WAT_FILE_CHMOD . "' tabindex='2' />";
		}
		?> 
</p>
   </form> 
  <?php
	} else {
		echo '<div class="error">' . _LANG_WAT_OOPS_EXISTS . '</div>';
	}
	?> 
</div> 
<div class="wrap">
  <p><?php echo _LANG_WAT_OTHER_FILE; ?></p> 
  <form name="file" action="templates.php" method="get"> 
    <input type="text" name="file" /> 
    <input type="submit" name="submit"  value="<?php echo _LANG_WAT_EDTEXT_TEMP; ?>" /> 
  </form> 
  <p><?php echo _LANG_WAT_COMMON_FILES; ?></p>
  <ul>
    <li><a href="templates.php?file=index.php">index.php</a></li>
    <li><a href="templates.php?file=wp-comments.php">wp-comments.php</a></li>
    <li><a href="templates.php?file=wp-comments-popup.php">wp-comments-popup.php</a></li>
    <li><a href="templates.php?file=.htaccess">.htaccess</a> (<?php echo _LANG_WAT_REWRITE_RULES; ?>)</li>
    <li><a href="templates.php?file=my-hacks.php">my-hacks.php</a> (<?php echo _LANG_WAT_LEGACY_HACKS; ?>)</li>
    </ul>
<?php
$plugins_dir = @ dir(ABSPATH . 'wp-content/plugins');
if ($plugins_dir) {
	while(($file = $plugins_dir->read()) !== false) {
	  if ( !preg_match('|^\.+$|', $file) && preg_match('|\.php$|', $file) ) 
		$plugin_files[] = $file;
	}
}
if ($plugins_dir || $plugin_files) :
?>
  <p><?php echo _LANG_WAT_PLUGIN_FILES; ?></p>
  <ul>
<?php foreach($plugin_files as $plugin_file) : ?>
	<li><a href="templates.php?file=wp-content/plugins/<?php echo $plugin_file; ?>"><?php echo $plugin_file; ?></a></li>
<?php endforeach; ?>
  </ul>
<?php endif; ?>
  <p><?php echo _LANG_WAT_FTP_CLIENT; ?></p>
</div> 
<?php

break;
}

include("admin-footer.php") ?> 
