<?php

// Wiki
// ````

require_once "settings.php";
require_once "markdown.php";
require_once "db.php";
date_default_timezone_set(S_TIMEZONE);

// Determine mode
// ``````````````

$content = '';
if (! isset($_GET['do'])) {
	redirect('?do=view&p='.urlencode(S_DEFAULTPAGE));
} else switch($_GET['do']) {

	// Viewing page
	// ````````````
	case 'view':
		if (! isset($_GET['p']))
			redirect('?do=view&p='.urlencode(S_DEFAULTPAGE));
		
		$article = DB\GetArticle($_GET['p']);
		$content = Markdown($article['body']);
	break;
	
	// Random page
	// ```````````
	case 'random':
		$article = DB\GetRandomArticle();
		redirect('?do=view&p='.urlencode($article['title']));
	break;
	
	// Recent changes
	// ``````````````
	case 'recent':
		$articles = DB\GetRecentArticles(NUM_RECENTCHANGES);
		$content  = '<h3>Recent Changes</h3>';
		$content .= '<table border="1" cellpadding="3" cellspacing="0">';
		foreach($articles as $article) {
			$content .= '
				<tr>
					<td>
						<a href="?do=view&p='.urlencode($article['title']).'">
							'.htmlspecialchars($article['title']).'
						</a>
					</td>
					<td>'.date('Y M jS, g:i a', $article['modified']).'</td>
				</tr>
			';
		}
		$content .= '</table>';
	break;

	// Edit or create page
	// ```````````````````
	case 'modify':
		$title = 'Untitled-'.strtoupper(substr(md5(rand()), 0, 6));
		if (isset($_GET['p'])) $title = $_GET['p'];
		$article = DB\GetArticle($title);
		if (isset($_GET['p'])) {
			$cheader = 'Editing article "'.htmlspecialchars($_GET['p']).'"';
		} else {
			$cheader = "Creating new article";
		}
		
		$content = '
			This wiki uses
			<a href="http://en.wikipedia.org/wiki/Markdown">Markdown</a> syntax
			for formatting.<br/>To link to another page, use the syntax
			<tt>[description](?do=view&p=pagename)</tt>.
			
			<br/><br/>
			
			<fieldset>
				<legend>'.$cheader.'</legend>
				<form method="POST" action="?do=save">
					<textarea name="content">'.$article['body'].'</textarea>
					<br/><br/>
					Save as:
					<input type="text" name="pagename" value="'.htmlspecialchars($article['title']).'"/>
					<input type="submit" value="Save &gt; &gt;">
				</form>
			</fieldset>
		';
	break;
	
	// Save changes
	// ````````````
	case 'save':
		if (! isset($_POST['pagename']) || ! isset($_POST['content'])) {
			die('Invalid request!');
		}
		DB\SaveArticle($_POST['pagename'], $_POST['content']);
		redirect('?do=view&p='.urlencode($_POST['pagename']));		
	break;
	
	// Invalid
	// ```````
	default:
		$content = '<em>Invalid mode.</em>';
	break;
}


// Helper functions
// ````````````````

function redirect($target) {
	header('Location: '.$target);
	die();
}

// Template
// ````````
?>
<!DOCTYPE html>
<html>
	<head>
		<title><?php echo S_PAGETITLE; ?></title>
		<link rel="stylesheet" type="text/css" href="style.css" />
	</head>
	<body>
		<div class="header">
			<a href="?do=view&p=<?php echo urlencode(S_DEFAULTPAGE); ?>">Home</a>
			<a href="?do=random">Random Page</a>
			<a href="?do=recent">Recent Changes</a>
			<a href="?do=modify">New Page</a>
<?php if ($_GET['do'] == 'view') { ?>
			<a href="?do=modify&p=<?php echo urlencode($_GET['p']); ?>">Modify</a>
<?php } ?>
		</div>
		
		<div class="content">
			<?php echo $content; ?>
		</div>
	</body>
</html>
