<?php
$title = 'Permalink Options';
$parent_file = 'options-general.php';

$wpvarstoreset = array('action','standalone', 'option_group_id');
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"];
		}
	}
}

require_once('./optionhandler.php');

if (isset($_POST['submit'])) {
	update_option('permalink_structure', $_POST['permalink_structure']);
	$permalink_structure = $_POST['permalink_structure'];

	update_option('category_base', $_POST['category_base']);
	$category_base = $_POST['category_base'];
} else {
	$permalink_structure = get_settings('permalink_structure');
	$category_base = get_settings('category_base');
}



	require_once('admin-header.php');
	if ($user_level <= 6) {
		die("You have do not have sufficient permissions to edit the options for this blog.");
	}
	require('./options-head.php');
?>
<?php if (isset($_POST['submit'])) : ?>
<div class="updated"><p><?php echo _LANG_WPL_EDIT_UPDATED; ?></p></div>
<?php endif; ?>
<div class="wrap"> 
  <h2><?php echo _LANG_WPL_EDIT_STRUCT; ?></h2> 
  <p><?php echo _LANG_WPL_CREATE_CUSTOM; ?></p>

<dl>
	<dt><code>%year%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_YEAR; ?>
	</dd>
	<dt><code>%monthnum%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_MONTH; ?>
	</dd>
	<dt><code>%day%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_DAY; ?>
	</dd>
	<dt><code>%hour%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_HOUR; ?>
	</dd>
	<dt><code>%minute%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_MINUTE; ?>
	</dd>
	<dt><code>%second%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_SECOND; ?>
	</dd>
	<dt><code>%postname%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_POSTNAME; ?>
	</dd>
	<dt><code>%post_id%</code></dt>
	<dd>
		<?php echo _LANG_WPL_CODE_POSTID; ?>
	</dd>
</dl>
<?php echo _LANG_WPL_USE_EXAMPLE; ?>
  <form name="form" action="options-permalink.php" method="post"> 
    <p><?php echo _LANG_WPL_USE_TEMPTEXT; ?></p>
    <p> 
      <input name="permalink_structure" type="text" style="width: 98%;" value="<?php echo $permalink_structure; ?>" /> 
    </p> 
	<p><?php echo _LANG_WPL_USE_BLANK; ?></p>
	<p> 
  <input name="category_base" type="text" style="width: 98%;" value="<?php echo $category_base; ?>" /> 
     </p> 
    <p class="submit"> 
      <input type="submit" name="submit" value="<?php echo _LANG_WPL_SUBMIT_UPDATE; ?>"> 
    </p> 
  </form> 
<?php
 if ($permalink_structure) {
?>
  <p><?php printf(_LANG_WPL_USE_HTACCESS, $permalink_structure) ?></p>
  <?php
$site_root = str_replace('http://', '', trim(get_settings('siteurl')));
$site_root = preg_replace('|([^/]*)(.*)|i', '$2', $site_root);
if ('/' != substr($site_root, -1)) $site_root = $site_root . '/';

$home_root = str_replace('http://', '', trim(get_settings('home')));
$home_root = preg_replace('|([^/]*)(.*)|i', '$2', $home_root);
if ('/' != substr($home_root, -1)) $home_root = $home_root . '/';

?> 
<form action="">
    <p>
    	<textarea rows="5" style="width: 98%;"><?php echo _LANG_WPL_ENGINE_ON; ?> <?php echo $home_root; ?> 
<?php
$rewrite = rewrite_rules('', $permalink_structure);
foreach ($rewrite as $match => $query) {
	if (strstr($query, 'index.php')) echo 'RewriteRule ^' . $match . ' ' . $home_root . $query . " [QSA]\n";
    else echo 'RewriteRule ^' . $match . ' ' . $site_root . $query . " [QSA]\n";
}
?>
    </textarea>
    </p>
    <?php printf(_LANG_WPL_EDIT_TEMPLATE, 'templates.php?file=.htaccess') ?>
</form>
</div> 
<?php
} else {
?>
<p>
<?php echo _LANG_WPL_MOD_REWRITE; ?>
</p>
<?php } ?>
</div>

<?php
require('./admin-footer.php');
?>
