<?php
//Set up the system
require('config.include.php');
//Auth stuff
require('auth.include.php');
?>
<!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>Write A Page</title>
<link rel="stylesheet" href="admin.css" />
<script type="text/javascript" src="editor.js"></script>
</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="page.php" class="breadcrumb">Write A Page</a>'); ?>
    </div>
    <div id="sidebar">
<?php
	require('nav.include.php');
?>
    </div>
    <div id="content">
        <div class="contentblock">
				<h2>Write A Page</h2>
			<?php
				$printform=true;
				//Do the right thing
				if(isset($_POST['pagetitle'])) {
					//Fix page data
					$_POST['pagetitle']=fetch::unescapePostVar($_POST['pagetitle']);
					$_POST['pagecontent']=fetch::unescapePostVar($_POST['pagecontent']);
					if($_POST['pagetitle']==='') {
						echo('<h3 class="red">Error</h3>
						Please enter a title.<br />');
					}
					else {
						//Publishing or drafting or attaching
						if(isset($_POST['publish'])||isset($_POST['draft'])||isset($_POST['attach'])) {
							$comments=$_POST['comments']==1?true:false;
							echo('
							<h3>Page Saved</h3>');
							if(isset($_POST['publish'])) {
								$id=$_p->make_page($_POST['pagetitle'],$_POST['pagecontent']);
								echo('<a href="../index.php?page='.$id.'">View Page</a>');
							}
							else {
								$id=$_p->make_page($_POST['pagetitle'],$_POST['pagecontent'], false);
								echo('<a href="managepages.php?p='.$id.'&amp;edit">Continue Editing</a>');
							}
							echo(' | <a href="dashboard.php">Return to dashboard</a>');
							//Attachment system
							if(isset($_POST['attach'])) {
								echo('<br />Attaching...');
								//Add and check result
								if(($attachedFile=$_p->attach(basename($_FILES['file_upload']['name']),$_FILES['file_upload']['tmp_name'],'../'.UPLOAD_PATH))!==false)
									echo('<br />Attachment "'.$attachedFile.'" added.');
								else
									echo('<br />Attachment failed to be added.');
							}
							$printform=false;
						}
						//Preview
						else {
							echo('
							<h3>Previewing Page</h3>'.
							$_p->parsePost($_POST['pagecontent'],-1,USE_GESHI_PAGES));
						}
					}
				}
				
				if($printform) {
				echo('
					<form enctype="multipart/form-data" action="page.php" method="post">
						<h3>Title</h3>
						<input tabindex="1" type="text" class="full" name="pagetitle" value="'.htmlentities($_POST['pagetitle'],ENT_QUOTES,"UTF-8").'"/>
						<h3>Content</h3>
						<div class="toolbar">
						<a href="#" title="Bold" onclick="addtag(\'pagecontent\', \'b\')"><b>B</b></a>
						<a href="#" title="Italics" onclick="addtag(\'pagecontent\', \'i\')"><i>I</i></a>
						<a href="#" title="Underline" onclick="addtag(\'pagecontent\', \'u\')"><u>U</u></a>
						<a href="#" class="deform" title="Code" onclick="addtag(\'pagecontent\', \'code\')">code</a>
						<a href="#" class="deform" title="Code" onclick="addtag(\'pagecontent\', \'quote\')">quote</a>
						<a href="#" class="deform" title="Html" onclick="addtag(\'pagecontent\', \'html\')">html</a>
						<a href="#" class="deform" title="Create A Link" onclick="addurl(\'pagecontent\')">url</a>
						<a href="#" class="deform" title="Insert an Image" onclick="addtag(\'pagecontent\', \'img\')">img</a>
						<a href="#" class="deform" title="Add Link to a Post" onclick="linkpost(\'pagecontent\')">+post</a>
						<a href="#" class="deform" title="Add Link to a Page" onclick="linkpage(\'pagecontent\')">+page</a>
						</div>
						<textarea tabindex="2" class="full" rows="30" cols="60" name="pagecontent" id="pagecontent">'.htmlentities($_POST['pagecontent'],ENT_QUOTES,"UTF-8").'</textarea>
						<h3>Insert Attachments</h3>
						<input type="text" id="attachment_search_text" name="attachment_search_text" value="'.htmlentities($_POST['attachment_search_text'],ENT_QUOTES,'UTF-8').'" onkeyup="javascript:searchattachments(true,false);" />
						<a href="javascript:searchattachments(\'\',false);">Show all attachments</a> | 
						<a href="javascript:searchattachments(false,false);">Show recent attachments</a>
						<div id="attachment_search_report">');
						//If we were sent a request for a search
						if(isset($_POST['attachsearch'])) {
							$searchResults=$_p->search_attachments($_POST['attachment_search_text'],'../'.UPLOAD_PATH);
							echo('<strong>Search results for "'.htmlentities($_POST['attachment_search_text'],ENT_QUOTES,'UTF-8').'":</strong><br />'.
							$_p->render_attachment_search(true,$searchResults));
						}
						//If not, display the recent attachments
						else {
							echo('
							Search results will appear here as you type.<br />
							(Requires Javascript, or else hit the Search button)<br /><br />
							<strong>Recent attachments:</strong><br />');
							echo($_p->render_attachment_search(true,$_p->recent_attachments('../'.UPLOAD_PATH)));
						}
				echo('</div>
						<br /><br />
						<input type="submit" name="attachsearch" value="Search" />
						<input type="hidden" id="userid" value="'.$_SESSION['auth_id'].'" />
						<input type="hidden" id="userkey" value="'.$_p->tb_users->getFieldValue($_SESSION['auth_id'],'hash').'" />
						<h3>Upload Attachments</h3>
						<input type="file" name="file_upload" />
						<input type="hidden" name="MAX_FILE_SIZE" value="'.MAX_UPLOAD_BYTES.'" /><br />
						Maximum upload size: '.MAX_UPLOAD_BYTES.' bytes<br />
						<br />
						<input type="submit" name="attach" value="Attach and draft" />
						<h3>Action</h3>
						<input tabindex="3" type="submit" name="publish" value="Publish" />
						<input tabindex="4" type="submit" name="draft" value="Save as draft" />
						<input tabindex="5" type="submit" name="preview" value="Preview" />
					</form>
				');
				}
			?>
		</div>
    </div>
    <div id="footer">
    </div>
</div>
</body>
</html>