<?php

/************************
* 
* txtStaticPages
* v 0.5-a
* last update: 25.12.07
* (c) 2006-2008 by camel
* marcin(at)killbox(dot)pl
* 
* **********************/

// folder z plikami
$path = 'static/';

// rozszerzenie plików
$ex = '.txt';

// ----------------------

function msg($msg_text, $msg_back=0)
{
	$msg = ( $msg_back !=0 ) ? '' : '&laquo <a href="read.php">Powrót do listy</a><br /><br />';
	$msg .= $msg_text;
	$msg .= '<br />';
	
	echo $msg;
	foother();
	exit;
}

function foother()
{
	echo '<br /><hr />camel &copy 2006-2008 | Powered by <b>txtStaticPages</b> v 0.5a';
	echo '</body></html>';
}

function form($fTitle = false, $fName = false, $fContent = false, $fButton = 'Utwórz stronę', $fAction = 'read.php?s=add')
{
	echo
'<form name="addpage" method="post" action="'.$fAction.'">
  <b>Tytuł strony:</b>
  <br />
  <input name="title" type="text" size="40" value="'.$fTitle.'" />
  <br /><br />
  <b>Nazwa linku</b> (read.php?s=<i>"nazwa"</i>). <i>Tylko małe litery, cyfry i "_", bez spacji i polskich znaków</i>:
  <br />
  <input name="link_name" type="text" size="30" value="'.$fName.'" />
  <br /><br />
  <b>Treść strony</b> (można używać html):
  <br />
  <textarea name="content" cols="80" rows="25">'.$fContent.'</textarea>
  <br /><br />
  <input type="submit" value="'.$fButton.'" style="font-weight:bold;" />
  <input type="reset" value="Wyczyść" />
  <input type="hidden" name="save" value="1" />
</form>';
}

$new_title = $_POST['title'];
$new_link_name = $_POST['link_name'];
$new_content = $_POST['content'];

echo '<html><head><title>txtStaticPages</title></head><body>';
echo '<h2>txtStaticPages - <i>by camel</i></h2>';

if ($_GET['del'])
{	
	$delFile = $path.$_GET['del'].$ex;
	
	if ( file_exists($delFile) )
	{
		unlink($delFile);
		
		msg('Strona zostala skasowana');
	}
	else
	{
		msg('Plik <i>'.$_GET['del'].$ex.'</i> nie istnieje');
	}
	
	exit;
}

if(!$_GET['s'])
{
	echo '&raquo; <a href="read.php?s=add">Dodaj nowa strone</a><br />';
	
	@$dir = opendir($path);
	if (!$dir)
	{
		msg('Nie moge otworzyc katalogu', 1);
	}
	while ( $files = readdir($dir) )
	{
  		$fileEx = strtolower(strrchr($files, "."));
  		if($fileEx == $ex) 
		{
			$fileslist[] = $files;
		}
	}
	
	echo '<ul>';
	for ($f=0; $f < count($fileslist); $f++)
	{
    	$fileslist[$f] = explode('.', $fileslist[$f]);
		$link = '<a href="read.php?s='. $fileslist[$f][0] .'"><b>'. $fileslist[$f][0] .'</b></a>'; 
		$edit = '<a href="read.php?s='.$fileslist[$f][0].'&amp;mode=edit">edytuj</a>';
		$delete = '<a href="read.php?del='.$fileslist[$f][0].'" onclick="return confirm(\'Czy napewno chcesz to skasowac?\')">usun</a>';
		
		echo '<li>'.$link.' [ '.$edit.' ] [ '.$delete.' ]</li>';
	}
	echo '</ul>';
	
}

elseif ($_GET['s'] == 'add')
{
	echo '&laquo <a href="read.php">Powrót do listy</a><br /><br />';
	
	if (isset($new_title) OR isset($new_link_name) OR isset($new_content))
	{
		if ($new_title == '' OR $new_link_name == '' OR $new_content == '')
		{
			echo '&laquo; <a href="read.php?s=add">Dodaj nowa strone</a><br /><br />';
			msg ('Nie wypelniles wszystkich pól', 1);
		}
		elseif ( file_exists($path.$new_link_name.$ex) )
		{
			echo '&laquo; <a href="read.php?s=add">Dodaj nowa strone</a><br /><br />';
			msg ('Strona o takiej nazwie juz instnieje', 1);
		}
		else
		{
			//$tmpfname = tempnam($_ENV["DOCUMENT_ROOT"]."/$path", "FOO"); DEL 3.06.06
			$tmpfname = tempnam("$path", "FOO");
			$handle = fopen($tmpfname, "w+");
			
			$writing = $new_title."||".$new_content;
			
			fwrite($handle, $writing);
			fclose($handle);

			///echo($_ENV["DOCUMENT_ROOT"]);
			copy($tmpfname, $path.$new_link_name.$ex);
			chmod($path.$new_link_name.$ex, 0755);
			
			if (file_exists($path.$new_link_name.$ex))
			{
				@unlink($tmpfname);
				msg ('Strona zostala zapisana', 1);
			}
			else
			{
				msg ('Wystapil blad podczas dodawania', 1);
			}
		}
	}
	else
	{
		echo '<b>Dodaj strone:</b><br /><br />';
		
		form();
	}
}

else 
{
	if (file_exists($path.$_GET['s'].$ex))
	{
		$open = fopen($path.$_GET['s'].$ex, "r+");
		$dane = fread($open, filesize($path.$_GET['s'].$ex));
	}
	else
	{
		msg('Taka strona nie istnieje');
	}
	
	echo '&laquo <a href="read.php">Powrót do listy</a><br /><br />';

	$title = explode( "||", $dane );
	$filename = $_GET['s'];
	//$text = nl2br($title[1]);
	$text = $title[1];
	
	fclose($open);
	
	if(isset($_GET['mode']) AND $_GET['mode'] == 'edit')
	{
		if ( !isset($_POST['save']) )
		{
			form(stripslashes($title[0]), $filename, $text, 'Zapisz', '');
		}
		else
		{
			//$modOpen = fopen($path.$_GET['s'].$ex, "r+");
			$open = fopen($path.$filename.$ex, "w+");
			$writing = $new_title."||".$new_content;
			if (fwrite($open, $writing) === FALSE) {
       			msg ('Nie mozna bylo zmodyfikowac strony [1]', 1);
        		exit;
     		} 
			else fclose($open);
			//fwrite($modOpen, $writing); DEL 3.06.06
			//fclose($modOpen);
			if ($new_link_name != $filename) {
				if (file_exists($path.$filename.$ex)) {
					copy($path.$filename.$ex, $path.$new_link_name.$ex);
					unlink($path.$filename.$ex);
				}
				else msg ('Nie mozna bylo zmodyfikowac strony [2]', 1);
			}
			
			msg ('Strona zostala zmodyfikowana', 1);
		}
	}
	else
	{
		echo 'Tytuł: <b>'.stripslashes($title[0]).'</b><br />';
		echo 'Nazwa pliku: <b>'.$filename.$ex.'</b><br /><br />';
		echo '<fieldset style="width: 85%;"><legend>Tresc strony</legend>'.nl2br($text).'</fieldset>';
	}
	
}

foother();

?>