<?php
require 'data/func.php';
session_start();
checkUser();
//include the configuration file and modules.php
require 'data/conf.php';
require 'data/modules.php';
//WORKSIMPLE CONTROL PANEL
//version 0.050 aug 12 2011
##
#I wouldn't recommend anyone looking at this code. It might kill you. Any feedback on it (ugh) would be nice!
#easton@geekness.eu
##
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title><?="$site_name control panel"; ?></title>
<link href="<?=$css_theme; ?>" title="compact" rel="stylesheet" type="text/css">
</meta>
</head>
<body>
<div id="frame">
	<div id="contentheader"><h1><a href="."><?=$site_name; ?></a>: <?=$tagline; ?></h1></div>
	<div id="contentleft">
<?php 
	require $lang;	
	if($lang == $lang):
		echo "<h1><a href='cp.php?do=logout'>$logout_n</a></h1>";
                
		//Opens the config file _just_ for reading
		$fh = fopen("data/conf.php", 'r') or die("Can't open 'conf.php' ");
		fclose($fh);

		$file = array_reverse(file("data/blog.txt"));

		?>
	</div>
	<div id="contentcenter">

		<h1><?="$control_panel $home"?></h1>
		<hr>
<?php	
	#CONTROL PANEL HOME#
                        if ($_GET == "" || $_GET == NULL) :
                            ?>
        	<p> 
                            <?php
                            $CSS_ch = explode("/", $css_theme);
                            echo "$current_theme_n:  $CSS_ch[1]   <br /><br />";
                            echo "$version_of_ws:   $ws_version <br /><br />";
                            echo "$number_of_posts: " . count($file);
                        endif;
                        ?>
    </p>

<?php
	#THEME CHANGER#
	if($_GET['do'] == "theme"):
		echo "<b>$available_themes</b><br /><br />$prefered_theme: <br /><br />";
		$wd = files_deep('themes/', '.css');
		//Using the functions
		echo '<form name="themeselect" method="post" action="cp.php?do=changethemenow"><p>';
		$zie = sizeof($wd, 0);
		for($x=0; $x!=$zie; $x++):
		$rar = explode(".",$wd[$x]);
			//take out .css 
			echo "<b>".$rar[0]."</b> -> "."<input type='radio' value='theme' name=theme[$wd[$x]] <br /><br/>";
		endfor;

		echo '<br />   <input type="submit" value="'.$done_n.'"></p></form>';
	endif;


	#THEME CHANGER#
	
	if($_GET['do'] == "lang"):
		echo "<b>$available_langs</b><br /><br />$select_your_prefered_lang: <br /><br />";
		$wd = files_deep('langs/', '.php');
		//Using the functions
		echo '<form name="themeselect" method="post" action="cp.php?do=langx">';
		$zie = sizeof($wd, 0);
		for($x=0; $x!=$zie; $x++):
			$rar = explode(".",$wd[$x]);
			//take out .css 
			echo "<b>".$rar[0]."</b> -> "."<input type='radio' value='theme' name=theme[$wd[$x]] <br /><br />";
		endfor;

		echo '<br />   <input type="submit" value="'.$done_n.'"></form>';
	endif;

	?>
<?php
	#CHANGING THEME NOW #
	if($_GET['do'] == "changethemenow"):
		$css_theme2 = $_POST['theme'];
		$key = array_search('theme', $css_theme2);
		echo "You chose: $key <br />";
		$search_replace = replace_in_file('data/conf.php', '$css_theme', $key);
		switch($search_replace):
			case 1:
				echo "$successfully_changed $theme_n";
				echo "<script>location.replace('cp.php')</script>";
				break;
			case 2:
				echo "$unable_to_find  ".'$css_theme'."   $in_n 'data/conf.php'";
				break;
			case 3:
				echo $unable_to_save;
				break;
	endswitch;

endif;

?>
<?php
	#CHANGING LANGUAGE NOW #
	
	if($_GET['do'] == "langx"):
		$css_theme2 = $_POST['theme'];
		$key = array_search('theme', $css_theme2);
		echo "You chose: $key <br />";
		$search_replace = replace_in_file('data/conf.php', '$lang', $key);
		switch($search_replace):
			case 1:
				echo "$successfully_changed $language_n";
				echo "<script>location.replace('cp.php')</script>";
				break;
			case 2:
				echo "$unable_to_find  $lang   $in_n 'data/conf.php'";
				break;
			case 3:
				echo $unable_to_save;
				break;
	endswitch;

endif;
?>	
<?php
#Write post
if($_GET['do'] == "write"):
?>
<table align="center" width="350"><tr><td>
<form action="cp.php?do=writepostx" method="post" enctype="multipart/form-data" name="content">

<?=$title_n; ?>: <input type="text" name="title" size="35" style="font-size: 11px" ><br /><br /><br />
<?=$content_n; ?>:<br /><textarea name="content" rows="8" cols="40" style="font-size: 11px"></textarea><br /><br />
<input type="submit" name="submit" value="Submit" >
</table>
<?php
endif;

if($_GET['do'] == "ttag"):
?>
<form action="cp.php?do=ttagx" method="post" enctype="multipart/form-data" name="content">
<?=$title_n; ?>: <input type="text" value="<?=$site_name; ?>" name="title" size="40" style="font-size: 11px" >
<br />
<br />
<?=$tagline_n; ?>:<input type="text" value="<?=$tagline; ?>" name="tag" size="40" style="font-size: 11px" >
<br />
<br />
<input type="submit" name="submit" value="Done" style="font-size: 11px;background: #FFFFFF; font-weight:bold">
</form>
<?php
endif;
?>
<?php
	#Logout
	if($_GET['do'] == "logout"):
		logoutUser();
		echo "<script>location.replace('index.php')</script>";
	endif;

	#Change title/tagline
	if($_GET['do'] == "ttagx"):
		$title = $_POST['title'];
		$tag = $_POST['tag'];
		//##Now do the replacing!
		$search_replace = replace_in_file('data/conf.php', '$site_name', $title);
		$search_replace_t = replace_in_file('data/conf.php', '$tagline', $tag);
		//Title
		switch($search_replace):
			case 1:
				echo "$successfully_changed $title_n";
				echo "<script>location.replace('cp.php')</script>";
				break;
			case 2:
				echo "$unable_to_find  ".'$site_name'."   $in_n 'data/conf.php'";
				break;
			case 3:
				echo $unable_to_save;
				break;
		endswitch;

	//Tagline   
	switch($search_replace_t):
		case 1:
			echo "$successfully_changed $title_n";
			echo "<script>location.replace('cp.php')</script>";
			break;
		case 2:
			echo "$unable_to_find  ".'$tagline'."   $in_n 'data/conf.php'";
			break;
		case 3:
			echo $unable_to_save;
			break;
	endswitch;

endif;

?>

                    <?php
  if($_GET['do'] == "posts"):
?>
<form action="cp.php?do=postsx" method="post" enctype="multipart/form-data" name="content">
<?=$posts_page_n; ?>: <input type="text" value="<?=$per_page; ?>" name="opi" size="20" style="font-size: 11px" >
<br />
<br />
<input type="submit" name="submit" value="<?=$done_n; ?>">
</form>

<?php
  endif;
	#edit posts
	if($_GET['do'] == "edit"):
        if(isset($_GET['post'])):
     $lines= file("data/blog.txt");
    $linenum = $_GET['post']-1; 
    $text = explode("#",$lines[$linenum]);                   
    ?>
                    <form action="cp.php?do=editx&post=<?= $_GET['post'] ?>" method="post" name="edit">
                                    <input type="text" value="<?= $text[2] ?>" name="title" size="20">
                                        <textarea name="content" rows="6" cols="36" style="font-size: 11px"><?= $text[3] ?></textarea>
                                        <br>
                                            <input type="checkbox" name="del">Delete
                                                <br />
                                                <input type="hidden" name="post" value="<?= $_GET['post'] ?>">
                                                    <input type="hidden" name="date" value="<?= $text[4] ?>">
                                                        <input class="text" type="submit" name="done" value="Done" />
                                                        </form>
                                                        <hr>
    <?php
        endif;

        #list posts
$file = array_reverse(file("data/blog.txt"));
for($x=(int)$_GET['start']; $x<sizeof($file) && $x<((int)$_GET['start']+5); $x++):   
$explode = explode("#",$file[$x]);
echo $explode[1].' <a href="cp.php?do=edit&post='.$explode[1].'">'.$explode[2].'</a><br><br>';
endfor;

endif;

?>
<?php
#Write post
if ($_GET['do'] == "writepostx") :
        $fileup = file("data/blog.txt");
        $id = sizeof($fileup, 0) + 1;
        $title = $_POST['title'];
        $title = str_replace("#", "", $title);
        $content = $_POST['content'];
        $content = str_replace("#", "", $content);
        $content = str_replace("\r\n", "<br />", $content);
        $title = stripslashes($title);
        $content = stripslashes($content);
        $datex = date("d.m.Y");
        $fp = fopen("data/blog.txt", "a+");
        $line = "#$id#$title#$content#$datex\n";
        fwrite($fp, $line);
        fclose($fp);
        echo "$done_n!";
        //Redirects home on default
        echo "<script>location.replace('cp.php')</script>";
    endif;

?>
<?php
#edit post execute
  if($_GET['do'] == "editx"):
  if($_POST['del']=="on"):
cutline('data/blog.txt', $_POST['post']);
echo "<script>location.replace('cp.php')</script>";
endif;

  $line = "#".$_POST['post']."#".$_POST['title']."#".$_POST['content']."#".$_POST['date'];
  replaceLineInTextFile("data/blog.txt", "#".$_POST['post']."#", $line); 


endif;

?>
                    <?php
	#Change post per pages

	if($_GET['do'] == "postsx"):
		$title = $_POST['opi'];
                if(is_numeric($title)):
		$search_replace = replace_in_file('data/conf.php', '$per_page', $title);
		switch($search_replace):
			case 1:
				echo "$successfully_changed $posts_per_page_n";
				echo "<script>location.replace('cp.php')</script>";
				break;
			case 2:
				echo "$unable_to_find  ".'$per_page'."   $in_n 'data/conf.php'";
				break;
			case 3:
				echo $unable_to_save;
				break;
		endswitch;

else:
    die('posts per page is not numeric!');
endif;
       endif;
?>
	<?php 
       # endif;
        else:
        die('error!');
        endif;
			 ?>
</div>
	<div id="contentright">	
		
			 	<div id="tabsJ">
                               <ul>
                                        <!-- CSS Tabs -->
<li id="current"><a href="cp.php"><span><?=$home; ?>        </span></a></li>
<li><a href="cp.php?do=write"><span><?=$write_post; ?></span></a></li>
<li><a href="cp.php?do=theme"><span><?=$change_theme; ?></span></a></li>
<li><a href="cp.php?do=ttag"><span><?=$change_title; ?></span></a></li>
<li><a href="cp.php?do=posts"><span><?=$change_posts_page; ?></span></a></li>
<li><a href="cp.php?do=lang"><span><?=$change_lang; ?></span></a></li>
<li><a href="cp.php?do=edit"><span><? if($editdel_post==""): echo "Edit/delete post"; else: echo $editdel_post; endif; ?></span></a></li>
                                </ul>              
           </div>
	</p>
	</div>
	<br clear="all" />
</div>
</body>
</html>


