<?php
require_once('../wp-includes/wp-l10n.php');

$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die ("Could not connect");

@mysql_select_db(DB_NAME, $link);


$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 (__('Sorry, can&#8217;t edit files with ".." in the name. If you are trying to edit a file in your WordPress home directory, you can just type the name of the file in.'));
	
	if (':' == substr($file,1,1))
		die (__('Sorry, can&#8217;t call files with their real 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(__('<p>You have do not have sufficient permissions to edit templates for this blog.</p>'));
	}


	$template_number = $_POST['template_number'];
        $r=mysql_query("update blogusers set template='$template_number' where username='".mysql_escape_string($blog_user_name)."'",$link);

	$newcontent = stripslashes($_POST['newcontent']);
	$file = $_POST['file'];
    $file = validate_file($file);
	$real_file = '../users_css/' . $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(__('<p>You have do not have sufficient permissions to edit templates for this blog.</p>'));
	}

	if ('' == $file) {
		if ('' != get_settings('blogfilename')) {
			$file = get_settings('blogfilename');
		} else {
			$file = $blog_user_name.'.css';
		}
	}

    $file = validate_file($file);	
//	$real_file = '../' . $file;

	$real_file = '../users_css/'.$blog_user_name.'.css';

//        echo "$real_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 = __(' &#8212; this is a WordPress file, be careful when editing it!');
	
	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><?php _e('File edited successfully.') ?></p></div>
<?php endif; ?>
 <div class="wrap"> 
  <?php
	echo "<p>" . sprintf(__('Editing <strong>%s</strong>'), $file) . " $warning</p>";
	
if (!$error) {

// BLOG CODE!!!
	?> 
  <form name="template" action="templates.php" method="post">
     Select your template: <br><select name="template_number">
     <?php
       $arr=explode("_",$table_prefix);
       $blog_username = $arr[1];

       $r=mysql_query("select name from blog_templates");
       for ($i=1;$i<=mysql_num_rows($r);$i++) {
         list($name)=mysql_fetch_row($r);
         $template_name[$i]=$name;
       }


       $r=mysql_query("select template from blogusers where username='$blog_username'",$link);
       list($template_num)=mysql_fetch_row($r);
       for ($i=0;$i<=10;$i++) {
         if ($template_num==$i) { $selected="selected"; } else { $selected=""; }
         if (0 == $i) { $disp_text="my own template"; } else { $disp_text=$template_name[$i]; }
         echo "<option value=$i $selected >"."$disp_text";
       }
     ?>
     </select>
     <br><br>Change code for <?php echo "<b>".$blog_user_name.".css</b>"; ?>:<br> 
     <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 "$blog_user_name".".css"; ?>" /> 
     <p class="submit">
     <?php
		if (is_writeable($real_file)) {
			echo "<input type='submit' name='submit' value='Update Template &raquo;' tabindex='2' />";
		} else {
			echo "<input type='button' name='oops' value='" . __('(You cannot update that file/template: must make it writable, e.g. CHMOD 666)') ."' tabindex='2' />";
		}
		?> 
</p>
   </form> 
  <?php
	} else {
		echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
	}
	?> 
</div>

<?php

break;
}

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