<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');
//Save data if given
$saved=false;
//File that blogroll links are in
$blogRoll='../'.BLOGROLL_PATH;
//The path to the theme html
if(isset($_POST['links'])) {
	$blogRollFile=@fopen($blogRoll,'w+');

if($blogRollFile===false)
	die('Could not open "'.$blogRoll.'" for writing, please check the file permissions.');
	
	//Format links
	$rawlinks=fetch::unescapePostVar($_POST['links']);
	$links=explode("\n",$rawlinks);
	foreach($links as $index=>$link)
		$links[$index]=trim(str_replace("\n",'',$link));
	$links=array_unique($links);
	$blank=array_search('',$links);
	if($blank!==false)
		unset($links[$blank]);
	if(count($links)>0) {
		$linkstring=implode("\n",$links);
	}
	else {
		$linkstring='';
	}
	
	//Write
	fwrite($blogRollFile,$linkstring);
	fclose($blogRollFile);
	$saved=true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Manage Blogroll</title>
<link rel="stylesheet" href="admin.css" />
</head>

<body>
<div id="wrapper">
	<div id="header">
    	<?php echo('<a href="../" class="breadcrumb">'.SITE_TITLE.'</a> &raquo; <a href="dashboard.php" class="breadcrumb">Dashboard</a> &raquo; <a href="blogroll.php" class="breadcrumb">Manage Blogroll</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
		<h2>Manage Blogroll</h2>
		<tt>http://mylink.com->My Site</tt><br />
		<form action="blogroll.php" method="post">
			<textarea name="links" class="full" cols="60" rows="30"><?php
				if(file_exists($blogRoll))
					$blogrollcontents=file_get_contents($blogRoll);
				else
					$blogrollcontents='';
				echo(htmlentities($blogrollcontents,ENT_QUOTES,"UTF-8"));
				?></textarea>
			<br />
			<input type="submit" value="Save"/>
		</form>
		<?php
		if($saved)
			echo('Blogroll saved.');
		?>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>