Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function display_content($admin){
- //open the current count
- //This will begin to display content to the user.
- $fh[2] = fopen("resources/count/entry.txt",'r');
- $entry_count = intval(fgets($fh[2]));
- fclose($fh[2]);
- include("resources/layouts/header.html");
- include("resources/layouts/main_container.html");
- include("resources/layouts/banner.html");
- if($admin){
- //include("resources/layouts/create_entry.html");
- //Also include the ability to see items in the recycle bin
- /*I'm considering writing this script in the create_entry.php file
- as it would make it easier to display the information
- although this would destroy the centralised paradigm of my website.
- ...Screw it, the create_entry.php will be incorporated into this main .php file
- */
- echo <<<END
- <div class="container_comments_banner">
- Admin Control Panel
- </div>
- <div class="container_admin_controls">
- <div class="container_create_content">
- <span class="entry_title">Create Entry</span><br>
- <form enctype="multipart/form-data" method="post" action="index.php">
- <input name="action" type="hidden" value="post">
- Title:
- <input type="text" name="title" id="title" />
- </br>
- Body (HTML scripting enabled):
- </br>
- <textarea name="body" id="body" cols="45" rows="5"></textarea>
- </br>
- File:
- <input type="hidden" name="MAX_FILE_SIZE" value="300000000000" />
- <input name="uploadedimage" type="file" />
- </br>
- <input type="submit" name="button" id="button" value="Submit" />
- </form>
- </br>
- </div>
- END;
- //Div is not closed yet
- //open the recycle bin directory
- $dir = "resources/entries/recycle/";
- if(is_dir($dir)){
- if($dh = opendir($dir)){
- $c = 0;
- while(($file[$c] = readdir($dh)) !== false){
- $c++;
- //that's a funny looking piece of code, this was taken mainly from php.net, but altered for my needs.
- }
- closedir($dh);
- }
- }
- if(isset($file)){
- echo <<<END
- <div class="container_recycle_bin">
- <span class="entry_title">Recycle Bin</span>
- END;
- $flag = true;
- foreach($file as $key => $value){
- //I could do this as a for loop, but foreach is easier
- //I need to think about how this is going to be formatted visually.
- if(file_exists("resources/entries/recycle/$value")){
- $fh[$key] = fopen("resources/entries/recycle/$value", 'r');
- //Get the entries name, and image
- $title = trim(fgets($fh[$key]));
- $date = trim(fgets($fh[$key]));
- $image = trim(fgets($fh[$key]));
- $entry = explode(".", $value);
- $entry = $entry[0];
- if($image != "none" && $image != ""){
- $flag = false;
- echo <<<END
- <div class="container_recycleBinOptions">
- <img class="small_image" src="resources/entries/images/$image">
- <span class="small_title">$title</span>
- <span class="small_date">$date</span>
- <div class="container_restore_entry"><a href="index.php?entry=$entry&action=restore">restore</a></div>
- <div class="container_delete_entry"><a href="index.php?entry=$entry&action=deleteentry">delete permanently</a></div>
- </div>
- END;
- }elseif($image != ""){
- $flag = false;
- echo <<<END
- <div class="container_recycleBinOptions">
- <span class="small_title">$title</span>
- <span class="small_date">$date</span>
- <div class="container_restore_entry"><a href="index.php?entry=$entry&action=restore">restore</a></div>
- <div class="container_delete_entry"><a href="index.php?entry=$entry&action=deleteentry">delete permanently</a></div>
- </div>
- END;
- }
- fclose($fh[$key]);
- }
- }
- if($flag){
- echo "<div class=\"container_bin_is_empty\">Recycle Bin Is Empty</div>";
- }
- }
- echo <<<END
- </div>
- </div>
- <div class="container_comments_banner">
- Entries
- </div>
- END;
- }
- //display entries
- $styles = array(1 => "entry_title", 2 => "entry_date", 4 => "entry_body");
- //display 10 items per page
- if($_GET['page']){
- $page = $_GET['page'];
- }else{
- $page = 1;
- }
- for($i = $entry_count-(($page-1)*10); $i >= 0 && $i > $entry_count-$page*10; $i--){
- if(file_exists("resources/entries/$i.txt")){
- $fh[3] = fopen("resources/entries/$i.txt", 'r');
- echo "<div onclick=\"location.href='index.php?entry=$i';\" class=\"container_home_entry\">";
- $title = trim(fgets($fh[3]));
- $date = trim(fgets($fh[3]));
- $image = trim(fgets($fh[3]));
- echo <<<END
- <span class="entry_title">$title</span>
- <span class="entry_date">$date</span>
- <p class="entry_body">
- END;
- $file = "resources/entries/images/$image";
- if($image != "none" && file_exists($file)){
- echo "<a href=\"$file\" target=\"_new\"><img src=\"". $file ."\" border=0 class=\"entry_image\"></a>\n";
- }
- while(!feof($fh[3])){
- echo trim(nl2br(fgets($fh[3])))."\n";
- }
- echo <<<END
- <a href="index.php?entry=$i">more...</a>
- </p></div>
- END;
- fclose($fh[3]);
- }
- }
- echo "<br>\n";
- if($page > 1){
- $out = $page-1;
- echo "<a href=\"index.php?page=$out\"><img src=\"resources/images/newer.png\" border=0></a>";
- }
- if($entry_count-$page*10 > 0){
- $out = $page+1;
- echo "<a href=\"index.php?page=$out\"><img src=\"resources/images/older.png\" border=0></a>";
- }
- }
- function display_entry($entry, $admin, $user_comments){
- //must remember, if admin is true, they have the ability to delete this post.
- include("resources/layouts/header.html");
- include("resources/layouts/main_container.html");
- include("resources/layouts/banner.html");
- if(file_exists("resources/entries/$entry.txt")){
- //Display the entry
- $fh[3] = fopen("resources/entries/$entry.txt", 'r');
- include("resources/layouts/home_entry_top.html");
- $title = trim(fgets($fh[3]));
- $date = trim(fgets($fh[3]));
- $image = trim(fgets($fh[3]));
- if($admin){
- echo "<div class=\"container_delete_comment\"><a href=\"index.php?entry=$entry&action=recycle\">delete</a></div>\n";
- }
- echo <<<END
- <span class="entry_title">$title</span>
- <span class="entry_date">$date</span>
- <p class="entry_body">
- END;
- $file = "resources/entries/images/$image";
- if($image != "none" && file_exists($file)){
- echo "<a href=\"$file\" target=\"_new\"><img src=\"". $file ."\" border=0 class=\"entry_image\"></a>\n";
- }
- while(!feof($fh[3])){
- echo trim(nl2br(fgets($fh[3])))."\n";
- }
- echo "</p></div>";
- fclose($fh[3]);
- //display the comments
- //comments are contained in "resources/entries/comments/$entry/#.txt"
- //Comments count is contained in "resources/entries/comments/$entry/count.txt"
- $fh[5] = fopen("resources/entries/comments/$entry/count.txt", 'r'); //The amount of comments
- $count = intval(trim(fgets($fh[5])));
- include("resources/layouts/comments_banner.html");
- for($i = $count; $i >= 0; $i--){
- $flag = 0;
- $char_count = 0;
- if(file_exists("resources/entries/comments/$entry/$i.txt")){
- $fh[6] = fopen("resources/entries/comments/$entry/$i.txt", 'r');
- //file layout: title \n author \n date \n content
- //Content has a character limit of 50, 50, none, 750 - respectively
- $title = substr(trim(fgets($fh[6])), 0, 50);
- $author = substr(trim(fgets($fh[6])), 0, 50);
- $date = trim(fgets($fh[6]));
- include("resources/layouts/comment_container.html");
- if(isset($user_comments[$i]) || $admin == 1){
- //That means that this is the users comment!! :D
- //Display the ability to delete this post
- echo "<div class=\"container_delete_comment\"><a href=\"index.php?entry=$entry&action=delete&comment=$i\">delete</a></div>\n";
- }
- echo "<span class=\"entry_title\">$title - $author</span>\n";
- echo "<span class=\"entry_date\">$date</span><br>\n";
- while(!feof($fh[6]) && $char_count <= 750){
- if($flag == 0){
- echo "<p class=\"entry_body\">";
- }
- $new_line = trim(fgets($fh[6]));
- $char_count += strlen($new_line);
- $print_len = 750-$char_count;
- if((750-$char_count)== 0){
- $print_len = 0;
- }
- echo substr($new_line, 0, $print_len);
- $flag = 1;
- }
- echo "</p></div>\n";
- fclose($fh[6]);
- }
- }
- include("resources/layouts/create_comment.php");
- }else{
- include("resources/layouts/home_entry_top.html");
- echo "<span class=\"entry_title\">ENTRY DOES NOT EXIST</span>\n</div>\n";
- }
- }
- putenv("TZ=Australia/Sydney");
- //------ DETERMINE THE USER
- $user = $_SERVER['REMOTE_ADDR'];
- //open the users file
- $fh[1] = fopen("users/admin.txt", 'r');
- $admin = fgets($fh[1], 4096);
- fclose($fh[1]);
- $enc_user = md5(sha1($user));
- //Determine who the user is
- if($enc_user == $admin){
- if(!file_exists("users/$enc_user.txt")){
- //create new user file
- //This file is where a record of the users posts are kept
- //this is so they can later be deleted.
- $fh[14] = fopen("users/$enc_user.txt", 'x');
- fclose($fh[14]);
- }
- //determine what the admin wants to do
- if($_POST['action']){
- $action = $_POST['action'];
- if($action == "post"){
- //if the admin wants to create a new entry
- //update the entry count file
- $fh[4] = fopen("resources/count/entry.txt", 'r');
- $entry_count = intval(fgets($fh[4]));
- fclose($fh[4]);
- unlink("resources/count/entry.txt");
- $fh[4] = fopen("resources/count/entry.txt", 'x');
- fwrite($fh[4], 1+intval($entry_count));
- fclose($fh[4]);
- //create new entry
- $title = $_POST['title'];
- $date = date('l, j/n/Y g:ia');
- $extension = end(explode('.', $_FILES['uploadedimage']['name']));
- $file_location = "resources/entries/images/$entry_count.$extension";
- if(move_uploaded_file($_FILES['uploadedimage']['tmp_name'], $file_location)){
- //echo "succesful";
- $image = $entry_count.".".$extension;
- }else{
- //echo "unsuccesful";
- //echo $_FILES['uploadedimage']['error'];
- $image = "none";
- }
- $body = $_POST['body'];
- $fh[5] = fopen("resources/entries/$entry_count.txt", 'w');
- fwrite($fh[5], "$title\n$date\n$image\n$body");
- fclose($fh[5]);
- mkdir("resources/entries/comments/$entry_count", 0777);
- $fh[11] = fopen("resources/entries/comments/$entry_count/history.txt", 'x');
- fclose($fh[11]);
- $fh[7] = fopen("resources/entries/comments/$entry_count/count.txt", 'x');
- fwrite($fh[7], "0");
- fclose($fh[7]);
- }
- if($action == "comment"){
- $target = htmlspecialchars(trim($_POST['target']));
- $body = htmlspecialchars(trim($_POST['body']));
- $body_md5 = md5($body);
- //make sure that the post does not already exist in this thread.
- $fh[12] = fopen("resources/entries/comments/$target/history.txt", 'r');
- $j = 0;
- while(!feof($fh[12])){
- $history[$j] = trim(fgets($fh[12]));
- $j++;
- }
- fclose($fh[12]);
- if(!in_array($body_md5, $history)){
- $title = htmlspecialchars(trim($_POST['title']));
- $author = htmlspecialchars(trim($_POST['author']));
- $date = date('l, j/n/Y g:ia');
- if(file_exists("resources/entries/comments/$target/count.txt")){
- $fh[8] = fopen("resources/entries/comments/$target/count.txt", 'r');
- $count = 1+intval(trim(fgets($fh[8])));
- fclose($fh[8]);
- unlink("resources/entries/comments/$target/count.txt");
- $fh[9] = fopen("resources/entries/comments/$target/count.txt", 'x');
- fwrite($fh[9], $count);
- fclose($fh[9]);
- $fh[10] = fopen("resources/entries/comments/$target/$count.txt", 'w');
- $string = "$title\n$author\n$date\n$body";
- fwrite($fh[10], $string);
- fclose($fh[10]);
- //write the comment to the history file
- $fh[13] = fopen("resources/entries/comments/$target/history.txt",'a');
- fwrite($fh[13], $body_md5."\n");
- //Update the users 'user' file
- $fh[16] = fopen("users/$enc_user.txt", 'a');
- $string = $target . "_" . $count . "\n";
- fwrite($fh[16], $string);
- fclose($fh[16]);
- }else{
- $comment_fail = 1;
- }
- }else{
- $comment_fail = 1;
- }
- header("Location: http://vrysrs.me/index.php?entry=$target");
- }
- }
- if($_GET['action']){
- $action = $_GET['action'];
- //if the user wants to delete their comment
- if($action == "delete"){
- $entry = $_GET['entry'];
- $comment = $_GET['comment'];
- if(file_exists("resources/entries/comments/$entry/$comment.txt")){
- unlink("resources/entries/comments/$entry/$comment.txt");
- }
- header("Location: http://vrysrs.me/index.php?entry=$entry");
- }
- if($action == "recycle"){
- //Basically move the latter mentioned entry to the recycle bin, where it can later be removed permanently
- $entry = $_GET['entry'];
- if(file_exists("resources/entries/$entry.txt")){
- rename("resources/entries/$entry.txt", "resources/entries/recycle/$entry.txt");
- }
- header("Location: http://vrysrs.me");
- //That's basically it, the rest will be handled else-where
- //That works, now to write the bit which allows the entry to be restored
- }
- if($action == "restore"){
- $entry = $_GET['entry'];
- if(file_exists("resources/entries/recycle/$entry.txt")){
- rename("resources/entries/recycle/$entry.txt", "resources/entries/$entry.txt");
- }
- header("Location: http://vrysrs.me/index.php?entry=$entry");
- //Again this is very simple because that's all it needs to be
- //Actually deleting is a larger process as it needs to be completed thoroughly
- }
- if($action == "deleteentry"){
- $entry = $_GET['entry'];
- if(file_exists("resources/entries/recycle/$entry.txt")){
- //Proceed to delete entry
- /*Btw, if you're wandering, not much checking has to go on here.
- The user has already been signed on as being the admin.*/
- if(($fh[18] = fopen("resources/entries/recycle/$entry.txt", 'r')) !== false){
- //move to the third line in the file where the image name is stored
- for($i = 0; $i < 3; $i++){
- //for loop because Marin would want me to
- $image = trim(fgets($fh[18]));
- }
- if($image !== "none"){
- unlink("resources/entries/images/$image");
- //No confirmation here because 'whatcha gunna do?'
- }
- }
- fclose($fh[18]);
- unlink("resources/entries/recycle/$entry.txt");
- if(file_exists("resources/entries/comments/$entry/count.txt")){
- //begin deleting comments
- $fh[19] = fopen("resources/entries/comments/$entry/count.txt", 'r');
- $count = trim(fgets($fh[19]));
- fclose($fh[19]);
- unlink("resources/entries/comments/$entry/count.txt");
- for($i = 1; $i <= $count; $i++){
- if(file_exists("resources/entries/comments/$entry/$i.txt")){
- unlink("resources/entries/comments/$entry/$i.txt");
- }
- //it's nice to be able to write code which wont generate any E_level warnings
- }
- }
- if(file_exists("resources/entries/comments/$entry/history.txt")){
- unlink("resources/entries/comments/$entry/history.txt");
- }
- rmdir("resources/entries/comments/$entry/");
- }
- header('location: http://vrysrs.me');
- }
- }
- if($_GET['entry'] && $action !== "deleteentry"){
- $entry = $_GET['entry'];
- display_entry($entry, 1, 0);
- }else{
- display_content(1);
- }
- }else{
- //if not admin
- //Determine if the user exists in our database
- if(!file_exists("users/$enc_user.txt")){
- //create new user file
- //This file is where a record of the users posts are kept
- //this is so they can later be deleted.
- $fh[14] = fopen("users/$enc_user.txt", 'x');
- fclose($fh[14]);
- }
- //welcome, you are now a user of the the system.
- //Load a record of the users comments... even if they just joined the system, it's easier that way
- //array structure: $user_comments[<entry>][<comment number>]
- $fh[15] = fopen("users/$enc_user.txt", 'r');
- while(!feof($fh[15])){
- $line = explode("_", trim(fgets($fh[15])));
- $user_comments[$line[0]][$line[1]] = 1;
- }
- if($_POST['action']){
- $action = $_POST['action'];
- if($action == "comment"){
- $target = htmlspecialchars(trim($_POST['target']));
- $body = htmlspecialchars(trim($_POST['body']));
- $body_md5 = md5($body);
- //make sure that the post does not already exist in this thread.
- $fh[12] = fopen("resources/entries/comments/$target/history.txt", 'r');
- $j = 0;
- while(!feof($fh[12])){
- $history[$j] = trim(fgets($fh[12]));
- $j++;
- }
- fclose($fh[12]);
- if(!in_array($body_md5, $history)){
- $title = htmlspecialchars(trim($_POST['title']));
- $author = htmlspecialchars(trim($_POST['author']));
- $date = date('l, j/n/Y g:ia');
- if(file_exists("resources/entries/comments/$target/count.txt")){
- $fh[8] = fopen("resources/entries/comments/$target/count.txt", 'r');
- $count = 1+intval(trim(fgets($fh[8])));
- fclose($fh[8]);
- unlink("resources/entries/comments/$target/count.txt");
- $fh[9] = fopen("resources/entries/comments/$target/count.txt", 'x');
- fwrite($fh[9], $count);
- fclose($fh[9]);
- $fh[10] = fopen("resources/entries/comments/$target/$count.txt", 'w');
- $string = "$title\n$author\n$date\n$body";
- fwrite($fh[10], $string);
- fclose($fh[10]);
- //write the comment to the history file
- $fh[13] = fopen("resources/entries/comments/$target/history.txt",'a');
- fwrite($fh[13], $body_md5."\n");
- //Update the users 'user' file
- $fh[16] = fopen("users/$enc_user.txt", 'w');
- $string = $target . "_" . $count . "\n";
- fwrite($fh[16], $string);
- fclose($fh[16]);
- }else{
- $comment_fail = 1;
- }
- }else{
- $comment_fail = 1;
- //Yea, nothing really happens with that variable... :S
- }
- header("Location: http://vrysrs.me/index.php?entry=$target");
- }
- }
- if($_GET['action']){
- $action = $_GET['action'];
- //if the user wants to delete their comment
- if($action == "delete"){
- //Determine what the user wants to delete
- $entry = $_GET['entry'];
- $comment = $_GET['comment'];
- //Determine who the user is and what they have permission to delete
- $fh[17] = fopen("users/$enc_user.txt", 'r');
- while(!feof($fh[17])){
- $line = explode("_", trim(fgets($fh[17])));
- $user_comments[$line[0]][$line[1]] = 1;
- }
- fclose($fh[17]);
- if($user_comments[$entry][$comment] == 1){
- //if the user has permission to delete the file, delete it.
- if(file_exists("resources/entries/comments/$entry/$comment.txt")){
- unlink("resources/entries/comments/$entry/$comment.txt");
- }
- }
- }
- }
- if($_GET['entry']){
- //Display the main content
- $entry = $_GET['entry'];
- display_entry($entry, 0, $user_comments[$entry]);
- }else{
- //By default, display the homepage.
- display_content(0);
- }
- //echo "Your IP is: $user <br> $enc_user";
- }
- echo "<br><span class=\"footer\">© James Clarke ". date('Y') ."<br><br></span>";
- echo "</div>";
- include("resources/layouts/footer.html");
- ?>
Advertisement
Add Comment
Please, Sign In to add comment