ClarkeRubber

My Blog's Mainframe

Dec 23rd, 2011
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.28 KB | None | 0 0
  1. <?php
  2. function display_content($admin){
  3.     //open the current count
  4.     //This will begin to display content to the user.
  5.     $fh[2] = fopen("resources/count/entry.txt",'r');
  6.     $entry_count = intval(fgets($fh[2]));
  7.     fclose($fh[2]);
  8.     include("resources/layouts/header.html");
  9.     include("resources/layouts/main_container.html");
  10.     include("resources/layouts/banner.html");
  11.         if($admin){
  12.         //include("resources/layouts/create_entry.html");
  13.         //Also include the ability to see items in the recycle bin
  14.         /*I'm considering writing this script in the create_entry.php file
  15.         as it would make it easier to display the information
  16.         although this would destroy the centralised paradigm of my website.
  17.        
  18.         ...Screw it, the create_entry.php will be incorporated into this main .php file
  19.         */
  20.         echo <<<END
  21. <div class="container_comments_banner">
  22. Admin Control Panel
  23. </div>
  24. <div class="container_admin_controls">
  25. <div class="container_create_content">
  26. <span class="entry_title">Create Entry</span><br>
  27. <form enctype="multipart/form-data" method="post" action="index.php">
  28. <input name="action" type="hidden" value="post">
  29. Title:
  30. <input type="text" name="title" id="title" />
  31. </br>
  32. Body (HTML scripting enabled):
  33. </br>
  34. <textarea name="body" id="body" cols="45" rows="5"></textarea>
  35. </br>
  36. File:
  37. <input type="hidden" name="MAX_FILE_SIZE" value="300000000000" />
  38. <input name="uploadedimage" type="file" />
  39. </br>
  40. <input type="submit" name="button" id="button" value="Submit" />
  41. </form>
  42. </br>
  43. </div>
  44. END;
  45.         //Div is not closed yet
  46.         //open the recycle bin directory
  47.         $dir = "resources/entries/recycle/";
  48.         if(is_dir($dir)){
  49.             if($dh = opendir($dir)){
  50.                 $c = 0;
  51.                 while(($file[$c] = readdir($dh)) !== false){
  52.                     $c++;
  53.                     //that's a funny looking piece of code, this was taken mainly from php.net, but altered for my needs.
  54.                 }
  55.             closedir($dh);
  56.             }
  57.         }
  58.         if(isset($file)){
  59.             echo <<<END
  60. <div class="container_recycle_bin">
  61. <span class="entry_title">Recycle Bin</span>
  62. END;
  63.             $flag = true;
  64.             foreach($file as $key => $value){
  65.                 //I could do this as a for loop, but foreach is easier
  66.                 //I need to think about how this is going to be formatted visually.
  67.                 if(file_exists("resources/entries/recycle/$value")){
  68.                     $fh[$key] = fopen("resources/entries/recycle/$value", 'r');
  69.                     //Get the entries name, and image
  70.                     $title = trim(fgets($fh[$key]));
  71.                     $date = trim(fgets($fh[$key]));
  72.                     $image = trim(fgets($fh[$key]));
  73.                     $entry = explode(".", $value);
  74.                     $entry = $entry[0];
  75.                     if($image != "none" && $image != ""){
  76.                         $flag = false;
  77.                         echo <<<END
  78. <div class="container_recycleBinOptions">
  79. <img class="small_image" src="resources/entries/images/$image">
  80. <span class="small_title">$title</span>
  81. <span class="small_date">$date</span>
  82. <div class="container_restore_entry"><a href="index.php?entry=$entry&action=restore">restore</a></div>
  83. <div class="container_delete_entry"><a href="index.php?entry=$entry&action=deleteentry">delete permanently</a></div>
  84. </div>
  85. END;
  86.                     }elseif($image != ""){
  87.                         $flag = false;
  88.                         echo <<<END
  89. <div class="container_recycleBinOptions">
  90. <span class="small_title">$title</span>
  91. <span class="small_date">$date</span>
  92. <div class="container_restore_entry"><a href="index.php?entry=$entry&action=restore">restore</a></div>
  93. <div class="container_delete_entry"><a href="index.php?entry=$entry&action=deleteentry">delete permanently</a></div>
  94. </div>
  95. END;
  96.                     }
  97.                     fclose($fh[$key]);
  98.                 }
  99.             }
  100.             if($flag){
  101.                 echo "<div class=\"container_bin_is_empty\">Recycle Bin Is Empty</div>";
  102.             }
  103.         }
  104.         echo <<<END
  105. </div>
  106. </div>
  107. <div class="container_comments_banner">
  108. Entries
  109. </div>
  110. END;
  111.     }
  112.     //display entries
  113.     $styles = array(1 => "entry_title", 2 => "entry_date", 4 => "entry_body");
  114.     //display 10 items per page
  115.     if($_GET['page']){
  116.         $page = $_GET['page'];
  117.     }else{
  118.         $page = 1;
  119.     }
  120.     for($i = $entry_count-(($page-1)*10); $i >= 0 && $i > $entry_count-$page*10; $i--){
  121.         if(file_exists("resources/entries/$i.txt")){
  122.             $fh[3] = fopen("resources/entries/$i.txt", 'r');
  123.             echo "<div onclick=\"location.href='index.php?entry=$i';\" class=\"container_home_entry\">";
  124.             $title = trim(fgets($fh[3]));
  125.             $date = trim(fgets($fh[3]));
  126.             $image = trim(fgets($fh[3]));
  127.             echo <<<END
  128. <span class="entry_title">$title</span>
  129. <span class="entry_date">$date</span>
  130. <p class="entry_body">
  131. END;
  132.             $file = "resources/entries/images/$image";
  133.             if($image != "none" && file_exists($file)){
  134.                 echo "<a href=\"$file\" target=\"_new\"><img src=\"". $file ."\" border=0 class=\"entry_image\"></a>\n";
  135.             }
  136.             while(!feof($fh[3])){
  137.                 echo trim(nl2br(fgets($fh[3])))."\n";
  138.             }
  139.             echo <<<END
  140. <a href="index.php?entry=$i">more...</a>
  141. </p></div>
  142. END;
  143.             fclose($fh[3]);
  144.         }
  145.     }
  146.     echo "<br>\n";
  147.     if($page > 1){
  148.         $out = $page-1;
  149.         echo "<a href=\"index.php?page=$out\"><img src=\"resources/images/newer.png\" border=0></a>";
  150.     }
  151.     if($entry_count-$page*10 > 0){
  152.         $out = $page+1;
  153.         echo "<a href=\"index.php?page=$out\"><img src=\"resources/images/older.png\" border=0></a>";
  154.     }
  155. }
  156.  
  157. function display_entry($entry, $admin, $user_comments){
  158.     //must remember, if admin is true, they have the ability to delete this post.
  159.     include("resources/layouts/header.html");
  160.     include("resources/layouts/main_container.html");
  161.     include("resources/layouts/banner.html");
  162.     if(file_exists("resources/entries/$entry.txt")){
  163.         //Display the entry
  164.         $fh[3] = fopen("resources/entries/$entry.txt", 'r');
  165.         include("resources/layouts/home_entry_top.html");
  166.         $title = trim(fgets($fh[3]));
  167.         $date = trim(fgets($fh[3]));
  168.         $image = trim(fgets($fh[3]));
  169.         if($admin){
  170.             echo "<div class=\"container_delete_comment\"><a href=\"index.php?entry=$entry&action=recycle\">delete</a></div>\n";
  171.         }
  172.         echo <<<END
  173. <span class="entry_title">$title</span>
  174. <span class="entry_date">$date</span>
  175. <p class="entry_body">
  176. END;
  177.         $file = "resources/entries/images/$image";
  178.         if($image != "none" && file_exists($file)){
  179.             echo "<a href=\"$file\" target=\"_new\"><img src=\"". $file ."\" border=0 class=\"entry_image\"></a>\n";
  180.         }
  181.         while(!feof($fh[3])){
  182.             echo trim(nl2br(fgets($fh[3])))."\n";
  183.         }
  184.         echo "</p></div>";
  185.         fclose($fh[3]);
  186.         //display the comments
  187.         //comments are contained in "resources/entries/comments/$entry/#.txt"
  188.         //Comments count is contained in "resources/entries/comments/$entry/count.txt"
  189.         $fh[5] = fopen("resources/entries/comments/$entry/count.txt", 'r'); //The amount of comments
  190.         $count = intval(trim(fgets($fh[5])));
  191.         include("resources/layouts/comments_banner.html");
  192.         for($i = $count; $i >= 0; $i--){
  193.             $flag = 0;
  194.             $char_count = 0;
  195.             if(file_exists("resources/entries/comments/$entry/$i.txt")){
  196.                 $fh[6] = fopen("resources/entries/comments/$entry/$i.txt", 'r');
  197.                 //file layout: title \n author \n date \n content
  198.                 //Content has a character limit of 50, 50, none, 750 - respectively
  199.                 $title = substr(trim(fgets($fh[6])), 0, 50);
  200.                 $author = substr(trim(fgets($fh[6])), 0, 50);
  201.                 $date = trim(fgets($fh[6]));
  202.                 include("resources/layouts/comment_container.html");
  203.                 if(isset($user_comments[$i]) || $admin == 1){
  204.                     //That means that this is the users comment!! :D
  205.                     //Display the ability to delete this post
  206.                     echo "<div class=\"container_delete_comment\"><a href=\"index.php?entry=$entry&action=delete&comment=$i\">delete</a></div>\n";
  207.                 }
  208.                 echo "<span class=\"entry_title\">$title - $author</span>\n";
  209.                 echo "<span class=\"entry_date\">$date</span><br>\n";
  210.                 while(!feof($fh[6]) && $char_count <= 750){
  211.                     if($flag == 0){
  212.                         echo "<p class=\"entry_body\">";
  213.                     }
  214.                     $new_line = trim(fgets($fh[6]));
  215.                     $char_count += strlen($new_line);
  216.                     $print_len = 750-$char_count;
  217.                     if((750-$char_count)== 0){
  218.                         $print_len =  0;
  219.                     }
  220.                     echo substr($new_line, 0, $print_len);
  221.                     $flag = 1;
  222.                 }
  223.                 echo "</p></div>\n";
  224.                 fclose($fh[6]);
  225.             }
  226.         }
  227.         include("resources/layouts/create_comment.php");
  228.     }else{
  229.         include("resources/layouts/home_entry_top.html");
  230.         echo "<span class=\"entry_title\">ENTRY DOES NOT EXIST</span>\n</div>\n";
  231.     }
  232. }
  233. putenv("TZ=Australia/Sydney");
  234. //------ DETERMINE THE USER
  235. $user = $_SERVER['REMOTE_ADDR'];
  236. //open the users file
  237. $fh[1] = fopen("users/admin.txt", 'r');
  238. $admin = fgets($fh[1], 4096);
  239. fclose($fh[1]);
  240. $enc_user = md5(sha1($user));
  241. //Determine who the user is
  242. if($enc_user == $admin){
  243.     if(!file_exists("users/$enc_user.txt")){
  244.         //create new user file
  245.         //This file is where a record of the users posts are kept
  246.         //this is so they can later be deleted.
  247.         $fh[14] = fopen("users/$enc_user.txt", 'x');
  248.         fclose($fh[14]);
  249.     }
  250.     //determine what the admin wants to do
  251.     if($_POST['action']){
  252.         $action = $_POST['action'];
  253.         if($action == "post"){
  254.             //if the admin wants to create a new entry
  255.             //update the entry count file
  256.             $fh[4] = fopen("resources/count/entry.txt", 'r');
  257.             $entry_count = intval(fgets($fh[4]));
  258.             fclose($fh[4]);
  259.             unlink("resources/count/entry.txt");
  260.             $fh[4] = fopen("resources/count/entry.txt", 'x');
  261.             fwrite($fh[4], 1+intval($entry_count));
  262.             fclose($fh[4]);
  263.             //create new entry
  264.             $title = $_POST['title'];
  265.             $date = date('l, j/n/Y g:ia');
  266.             $extension = end(explode('.', $_FILES['uploadedimage']['name']));
  267.             $file_location = "resources/entries/images/$entry_count.$extension";
  268.             if(move_uploaded_file($_FILES['uploadedimage']['tmp_name'], $file_location)){
  269.                 //echo "succesful";
  270.                 $image = $entry_count.".".$extension;
  271.             }else{
  272.                 //echo "unsuccesful";
  273.                 //echo $_FILES['uploadedimage']['error'];
  274.                 $image = "none";
  275.             }
  276.             $body = $_POST['body'];
  277.             $fh[5] = fopen("resources/entries/$entry_count.txt", 'w');
  278.             fwrite($fh[5], "$title\n$date\n$image\n$body");
  279.             fclose($fh[5]);
  280.             mkdir("resources/entries/comments/$entry_count", 0777);
  281.             $fh[11] = fopen("resources/entries/comments/$entry_count/history.txt", 'x');
  282.             fclose($fh[11]);
  283.             $fh[7] = fopen("resources/entries/comments/$entry_count/count.txt", 'x');
  284.             fwrite($fh[7], "0");
  285.             fclose($fh[7]);
  286.         }
  287.         if($action == "comment"){
  288.             $target = htmlspecialchars(trim($_POST['target']));
  289.             $body =  htmlspecialchars(trim($_POST['body']));
  290.             $body_md5 = md5($body);
  291.             //make sure that the post does not already exist in this thread.
  292.             $fh[12] = fopen("resources/entries/comments/$target/history.txt", 'r');
  293.             $j = 0;
  294.             while(!feof($fh[12])){
  295.                 $history[$j] = trim(fgets($fh[12]));
  296.                 $j++;
  297.             }
  298.             fclose($fh[12]);
  299.             if(!in_array($body_md5, $history)){
  300.                 $title =  htmlspecialchars(trim($_POST['title']));
  301.                 $author = htmlspecialchars(trim($_POST['author']));
  302.                 $date = date('l, j/n/Y g:ia');
  303.                 if(file_exists("resources/entries/comments/$target/count.txt")){
  304.                     $fh[8] = fopen("resources/entries/comments/$target/count.txt", 'r');
  305.                     $count = 1+intval(trim(fgets($fh[8])));
  306.                     fclose($fh[8]);
  307.                     unlink("resources/entries/comments/$target/count.txt");
  308.                     $fh[9] = fopen("resources/entries/comments/$target/count.txt", 'x');
  309.                     fwrite($fh[9], $count);
  310.                     fclose($fh[9]);
  311.                     $fh[10] = fopen("resources/entries/comments/$target/$count.txt", 'w');
  312.                     $string = "$title\n$author\n$date\n$body";
  313.                     fwrite($fh[10], $string);
  314.                     fclose($fh[10]);
  315.                     //write the comment to the history file
  316.                     $fh[13] = fopen("resources/entries/comments/$target/history.txt",'a');
  317.                     fwrite($fh[13], $body_md5."\n");
  318.                     //Update the users 'user' file
  319.                     $fh[16] = fopen("users/$enc_user.txt", 'a');
  320.                     $string = $target . "_" . $count . "\n";
  321.                     fwrite($fh[16], $string);
  322.                     fclose($fh[16]);
  323.                 }else{
  324.                     $comment_fail = 1;
  325.                 }
  326.             }else{
  327.                 $comment_fail = 1;
  328.             }
  329.             header("Location: http://vrysrs.me/index.php?entry=$target");
  330.         }
  331.     }
  332.     if($_GET['action']){
  333.         $action = $_GET['action'];
  334.         //if the user wants to delete their comment
  335.         if($action == "delete"){
  336.             $entry = $_GET['entry'];
  337.             $comment = $_GET['comment'];
  338.             if(file_exists("resources/entries/comments/$entry/$comment.txt")){
  339.                 unlink("resources/entries/comments/$entry/$comment.txt");
  340.             }
  341.             header("Location: http://vrysrs.me/index.php?entry=$entry");
  342.         }
  343.         if($action == "recycle"){
  344.             //Basically move the latter mentioned entry to the recycle bin, where it can later be removed permanently
  345.             $entry = $_GET['entry'];
  346.             if(file_exists("resources/entries/$entry.txt")){
  347.                 rename("resources/entries/$entry.txt", "resources/entries/recycle/$entry.txt");
  348.             }
  349.             header("Location: http://vrysrs.me");
  350.             //That's basically it, the rest will be handled else-where
  351.             //That works, now to write the bit which allows the entry to be restored
  352.         }
  353.         if($action == "restore"){
  354.             $entry = $_GET['entry'];
  355.             if(file_exists("resources/entries/recycle/$entry.txt")){
  356.                 rename("resources/entries/recycle/$entry.txt", "resources/entries/$entry.txt");
  357.             }
  358.             header("Location: http://vrysrs.me/index.php?entry=$entry");
  359.             //Again this is very simple because that's all it needs to be
  360.             //Actually deleting is a larger process as it needs to be completed thoroughly
  361.         }
  362.         if($action == "deleteentry"){
  363.             $entry = $_GET['entry'];
  364.             if(file_exists("resources/entries/recycle/$entry.txt")){
  365.                 //Proceed to delete entry
  366.                 /*Btw, if you're wandering, not much checking has to go on here.
  367.                 The user has already been signed on as being the admin.*/
  368.                 if(($fh[18] = fopen("resources/entries/recycle/$entry.txt", 'r')) !== false){
  369.                     //move to the third line in the file where the image name is stored
  370.                     for($i = 0; $i < 3; $i++){
  371.                         //for loop because Marin would want me to
  372.                         $image = trim(fgets($fh[18]));
  373.                     }
  374.                     if($image !== "none"){
  375.                         unlink("resources/entries/images/$image");
  376.                         //No confirmation here because 'whatcha gunna do?'
  377.                     }
  378.                 }
  379.                 fclose($fh[18]);
  380.                 unlink("resources/entries/recycle/$entry.txt");
  381.                 if(file_exists("resources/entries/comments/$entry/count.txt")){
  382.                     //begin deleting comments
  383.                     $fh[19] = fopen("resources/entries/comments/$entry/count.txt", 'r');
  384.                     $count = trim(fgets($fh[19]));
  385.                     fclose($fh[19]);
  386.                     unlink("resources/entries/comments/$entry/count.txt");
  387.                     for($i = 1; $i <= $count; $i++){
  388.                         if(file_exists("resources/entries/comments/$entry/$i.txt")){
  389.                             unlink("resources/entries/comments/$entry/$i.txt");
  390.                         }
  391.                         //it's nice to be able to write code which wont generate any E_level warnings
  392.                     }
  393.                 }
  394.                 if(file_exists("resources/entries/comments/$entry/history.txt")){
  395.                     unlink("resources/entries/comments/$entry/history.txt");
  396.                 }
  397.                 rmdir("resources/entries/comments/$entry/");
  398.             }
  399.             header('location: http://vrysrs.me');
  400.         }
  401.     }
  402.     if($_GET['entry'] && $action !== "deleteentry"){
  403.         $entry = $_GET['entry'];
  404.         display_entry($entry, 1, 0);
  405.     }else{
  406.         display_content(1);
  407.     }
  408. }else{
  409.     //if not admin
  410.     //Determine if the user exists in our database
  411.     if(!file_exists("users/$enc_user.txt")){
  412.         //create new user file
  413.         //This file is where a record of the users posts are kept
  414.         //this is so they can later be deleted.
  415.         $fh[14] = fopen("users/$enc_user.txt", 'x');
  416.         fclose($fh[14]);
  417.     }
  418.     //welcome, you are now a user of the the system.
  419.     //Load a record of the users comments... even if they just joined the system, it's easier that way
  420.     //array structure: $user_comments[<entry>][<comment number>]
  421.     $fh[15] = fopen("users/$enc_user.txt", 'r');
  422.     while(!feof($fh[15])){
  423.         $line = explode("_", trim(fgets($fh[15])));
  424.         $user_comments[$line[0]][$line[1]] = 1;
  425.     }
  426.     if($_POST['action']){
  427.         $action = $_POST['action'];
  428.         if($action == "comment"){
  429.             $target = htmlspecialchars(trim($_POST['target']));
  430.             $body =  htmlspecialchars(trim($_POST['body']));
  431.             $body_md5 = md5($body);
  432.             //make sure that the post does not already exist in this thread.
  433.             $fh[12] = fopen("resources/entries/comments/$target/history.txt", 'r');
  434.             $j = 0;
  435.             while(!feof($fh[12])){
  436.                 $history[$j] = trim(fgets($fh[12]));
  437.                 $j++;
  438.             }
  439.             fclose($fh[12]);
  440.             if(!in_array($body_md5, $history)){
  441.                 $title =  htmlspecialchars(trim($_POST['title']));
  442.                 $author = htmlspecialchars(trim($_POST['author']));
  443.                 $date = date('l, j/n/Y g:ia');
  444.                 if(file_exists("resources/entries/comments/$target/count.txt")){
  445.                     $fh[8] = fopen("resources/entries/comments/$target/count.txt", 'r');
  446.                     $count = 1+intval(trim(fgets($fh[8])));
  447.                     fclose($fh[8]);
  448.                     unlink("resources/entries/comments/$target/count.txt");
  449.                     $fh[9] = fopen("resources/entries/comments/$target/count.txt", 'x');
  450.                     fwrite($fh[9], $count);
  451.                     fclose($fh[9]);
  452.                     $fh[10] = fopen("resources/entries/comments/$target/$count.txt", 'w');
  453.                     $string = "$title\n$author\n$date\n$body";
  454.                     fwrite($fh[10], $string);
  455.                     fclose($fh[10]);
  456.                     //write the comment to the history file
  457.                     $fh[13] = fopen("resources/entries/comments/$target/history.txt",'a');
  458.                     fwrite($fh[13], $body_md5."\n");
  459.                     //Update the users 'user' file
  460.                     $fh[16] = fopen("users/$enc_user.txt", 'w');
  461.                     $string = $target . "_" . $count . "\n";
  462.                     fwrite($fh[16], $string);
  463.                     fclose($fh[16]);
  464.                 }else{
  465.                     $comment_fail = 1;
  466.                 }
  467.             }else{
  468.                 $comment_fail = 1;
  469.                 //Yea, nothing really happens with that variable... :S
  470.             }
  471.             header("Location: http://vrysrs.me/index.php?entry=$target");
  472.         }
  473.     }
  474.     if($_GET['action']){
  475.         $action = $_GET['action'];
  476.         //if the user wants to delete their comment
  477.         if($action == "delete"){
  478.             //Determine what the user wants to delete
  479.             $entry = $_GET['entry'];
  480.             $comment = $_GET['comment'];
  481.             //Determine who the user is and what they have permission to delete
  482.             $fh[17] = fopen("users/$enc_user.txt", 'r');
  483.             while(!feof($fh[17])){
  484.                 $line = explode("_", trim(fgets($fh[17])));
  485.                 $user_comments[$line[0]][$line[1]] = 1;
  486.             }
  487.             fclose($fh[17]);
  488.             if($user_comments[$entry][$comment] == 1){
  489.                 //if the user has permission to delete the file, delete it.
  490.                 if(file_exists("resources/entries/comments/$entry/$comment.txt")){
  491.                     unlink("resources/entries/comments/$entry/$comment.txt");
  492.                 }
  493.             }
  494.         }
  495.     }
  496.     if($_GET['entry']){
  497.         //Display the main content
  498.         $entry = $_GET['entry'];
  499.         display_entry($entry, 0, $user_comments[$entry]);
  500.     }else{
  501.         //By default, display the homepage.
  502.         display_content(0);
  503.     }
  504.     //echo "Your IP is: $user <br> $enc_user";
  505. }
  506. echo "<br><span class=\"footer\">&copy; James Clarke ". date('Y') ."<br><br></span>";
  507. echo "</div>";
  508. include("resources/layouts/footer.html");
  509. ?>
Advertisement
Add Comment
Please, Sign In to add comment