<?php
// PukiWiki - Yet another WikiWikiWeb clone.
// $Id: unfreeze.inc.php 1074 2008-10-11 10:49:19Z fishbone $
//
// Unfreeze(Unlock) plugin

// Show edit form when unfreezed
define('PLUGIN_UNFREEZE_EDIT', TRUE);

function plugin_unfreeze_action()
{
	// modified for Magic3 by naoki on 2008/10/10
	//global $script, $vars, $function_freeze;
	global $script, $function_freeze;
	global $_title_isunfreezed, $_title_unfreezed, $_title_unfreeze;
	global $_msg_invalidpass, $_msg_unfreezing, $_btn_unfreeze;

	//$page = isset($vars['page']) ? $vars['page'] : '';
	$page = WikiParam::getPage();
	if (! $function_freeze || ! is_page($page))
		return array('msg' => '', 'body' => '');

	//$pass = isset($vars['pass']) ? $vars['pass'] : NULL;
	$pass = WikiParam::getVar('pass');
	$msg = $body = '';
	if (! is_freeze($page)) {
		// Unfreezed already
		$msg  = $_title_isunfreezed;
		$body = str_replace('$1', htmlspecialchars(strip_bracket($page)),
			$_title_isunfreezed);

	//} else if ($pass !== NULL && pkwk_login($pass)) {
	} else if ($pass != '' && pkwk_login($pass)) {
		// Unfreeze
		//$postdata = get_source($page);
		//array_shift($postdata);
		//$postdata = join('', $postdata);
		//file_write(DATA_DIR, $page, $postdata, TRUE);
		// ページをロックを解除
		WikiPage::lockPage($page, false);

		// Update 
		is_freeze($page, TRUE);
		if (PLUGIN_UNFREEZE_EDIT) {
			//$vars['cmd'] = 'read'; // To show 'Freeze' link
			WikiParam::setCmd('read');
			$msg  = $_title_unfreezed;
			//$body = edit_form($page, $postdata);
			$body = edit_form($page, get_source($page, true));
		} else {
			//$vars['cmd'] = 'read';
			WikiParam::setCmd('read');
			$msg  = $_title_unfreezed;
			$body = '';
		}
	} else {
		// Show unfreeze form
		$msg    = $_title_unfreeze;
		$s_page = htmlspecialchars($page);
		//$body   = ($pass === NULL) ? '' : "<p><strong>$_msg_invalidpass</strong></p>\n";
		$body   = ($pass == '') ? '' : "<p><strong>$_msg_invalidpass</strong></p>\n";
		// modified for Magic3 by naoki on 2008/10/10
		/*
		$body  .= <<<EOD
<p>$_msg_unfreezing</p>
<form action="$script" method="post">
 <div>
  <input type="hidden"   name="cmd"  value="unfreeze" />
  <input type="hidden"   name="page" value="$s_page" />
  <input type="password" name="pass" size="12" />
  <input type="submit"   name="ok"   value="$_btn_unfreeze" />
 </div>
</form>
EOD;*/
		$postScript = $script . WikiParam::convQuery("?");
		$body  .= <<<EOD
<p>$_msg_unfreezing</p>
<form action="$postScript" method="post">
 <div>
  <input type="hidden"   name="wcmd"  value="unfreeze" />
  <input type="hidden"   name="page" value="$s_page" />
  <input type="hidden"   name="pass" />
  <input type="password" name="password" size="12" />
  <input type="submit"   name="ok"   value="$_btn_unfreeze" onclick="this.form.pass.value = hex_md5(this.form.password.value);" />
 </div>
</form>
EOD;
	}

	return array('msg'=>$msg, 'body'=>$body);
}
?>
