ClarkeRubber

Functions.php

Dec 24th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.15 KB | None | 0 0
  1. <?php
  2. function action_post(){
  3.     //update the entry count file
  4.     $fh[4] = fopen("resources/count/entry.txt", 'r');
  5.     $entry_count = intval(fgets($fh[4]));
  6.     fclose($fh[4]);
  7.     unlink("resources/count/entry.txt");
  8.     $fh[4] = fopen("resources/count/entry.txt", 'x');
  9.     fwrite($fh[4], 1+intval($entry_count));
  10.     fclose($fh[4]);
  11.     //create new entry
  12.     $title = $_POST['title'];
  13.     $date = date('l, j/n/Y g:ia');
  14.     $extension = end(explode('.', $_FILES['uploadedimage']['name']));
  15.     $file_location = "resources/entries/images/$entry_count.$extension";
  16.     if(move_uploaded_file($_FILES['uploadedimage']['tmp_name'], $file_location)){
  17.         //echo "succesful";
  18.         $image = $entry_count.".".$extension;
  19.     }else{
  20.         //echo "unsuccesful";
  21.         //echo $_FILES['uploadedimage']['error'];
  22.         $image = "none";
  23.     }
  24.     $body = $_POST['body'];
  25.     $fh[5] = fopen("resources/entries/$entry_count.txt", 'w');
  26.     fwrite($fh[5], "$title\n$date\n$image\n$body");
  27.     fclose($fh[5]);
  28.     mkdir("resources/entries/comments/$entry_count", 0777);
  29.     $fh[11] = fopen("resources/entries/comments/$entry_count/history.txt", 'x');
  30.     fclose($fh[11]);
  31.     $fh[7] = fopen("resources/entries/comments/$entry_count/count.txt", 'x');
  32.     fwrite($fh[7], "0");
  33.     fclose($fh[7]);
  34. }
  35. function create_user($enc_user){
  36.     if(!file_exists("users/$enc_user.txt")){
  37.         //create new user file
  38.         //This file is where a record of the users posts are kept
  39.         //this is so they can later be deleted.
  40.         $fh[14] = fopen("users/$enc_user.txt", 'x');
  41.         fclose($fh[14]);
  42.     }
  43. }
  44. function display_image(){
  45.     if($_GET['target'] && $_GET['type']){
  46.         $image = "resources/entries/images/".$_GET['target'].".".$_GET['type'];
  47.         if(file_exists($image)){
  48.             //this is basically a page in itself, so all of the includes must be done.
  49.             page_start();
  50.             if($_GET['source']){
  51.                 $source = $_GET['source'];
  52.                 echo <<<END
  53. <div onclick="location.href='index.php?entry=$source';" class="container_comments_banner">Back</div>
  54. <div class="container_no_background">
  55. <img class="full_image" src="$image">
  56. </div>
  57. END;
  58.             }else{
  59.                 echo <<<END
  60. <div class="container_no_background">
  61. <img class="full_image" src="$image">
  62. </div>
  63. END;
  64.             }
  65.         }else{
  66.             page_start();
  67.             echo <<<END
  68. <div class="container_home_entry">
  69. <span class="entry_title">FILE DOES NOT EXIST</span>
  70. </div>
  71. END;
  72.         }
  73.     }
  74. }
  75. function comment_entry(){
  76.     $target = htmlspecialchars(trim($_POST['target']));
  77.     $body =  trim(str_replace("\n", "<br>", htmlspecialchars(substr($_POST['body'], 0, 750))));
  78.     $body_md5 = md5($body);
  79.     //make sure that the post does not already exist in this thread.
  80.     $fh[12] = fopen("resources/entries/comments/$target/history.txt", 'r');
  81.     $j = 0;
  82.     while(!feof($fh[12])){
  83.         $history[$j] = trim(fgets($fh[12]));
  84.         $j++;
  85.     }
  86.     fclose($fh[12]);
  87.     if(!in_array($body_md5, $history)){
  88.         $title =  nl2br(htmlspecialchars(substr($_POST['title'], 0, 20)));
  89.         $author = nl2br(htmlspecialchars(substr($_POST['author'], 0, 20)));
  90.         $date = date('l, j/n/Y g:ia');
  91.         if(file_exists("resources/entries/comments/$target/count.txt")){
  92.             $fh[8] = fopen("resources/entries/comments/$target/count.txt", 'r');
  93.             $count = 1+intval(trim(fgets($fh[8])));
  94.             fclose($fh[8]);
  95.             unlink("resources/entries/comments/$target/count.txt");
  96.             $fh[9] = fopen("resources/entries/comments/$target/count.txt", 'x');
  97.             fwrite($fh[9], $count);
  98.             fclose($fh[9]);
  99.             $fh[10] = fopen("resources/entries/comments/$target/$count.txt", 'w');
  100.             $string = "$title\n$author\n$date\n$body";
  101.             fwrite($fh[10], $string);
  102.             fclose($fh[10]);
  103.             //write the comment to the history file
  104.             $fh[13] = fopen("resources/entries/comments/$target/history.txt",'a');
  105.             fwrite($fh[13], $body_md5."\n");
  106.             //Update the users 'user' file
  107.             $fh[16] = fopen("users/$enc_user.txt", 'a');
  108.             $string = $target . "_" . $count . "\n";
  109.             fwrite($fh[16], $string);
  110.             fclose($fh[16]);
  111.         }else{
  112.             $comment_fail = 1;
  113.         }
  114.     }else{
  115.         $comment_fail = 1;
  116.     }
  117.     header("Location: http://vrysrs.me/index.php?entry=$target");
  118. }
  119. function page_start(){
  120.     /* This will replace the
  121.     include("resources/layouts/header.html");
  122.     include("resources/layouts/main_container.html");
  123.     include("resources/layouts/banner.html");
  124.    
  125.     sequence
  126.     */
  127.     echo <<<END
  128. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  129. <html xmlns="http://www.w3.org/1999/xhtml">
  130. <head>
  131. <LINK href="resources/styles/entries.css" rel="stylesheet" type="text/css">
  132. <link rel="shortcut icon" href="/resources/images/favicon.ico">
  133. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  134. <title>RAWR</title>
  135. </head>
  136. <body link="#A40000">
  137. <div class="container_main">
  138. <div class="container_banner">
  139. <a href="http://vrysrs.me">
  140. <img class="banner_image" src="resources/images/banner.png" border=0>
  141. </a>
  142. </div>
  143. END;
  144. }
  145. function display_content($admin){
  146.     //open the current count
  147.     //This will begin to display content to the user.
  148.     $fh[2] = fopen("resources/count/entry.txt",'r');
  149.     $entry_count = intval(fgets($fh[2]));
  150.     fclose($fh[2]);
  151.     page_start();
  152.         if($admin){
  153.         //include("resources/layouts/create_entry.html");
  154.         //Also include the ability to see items in the recycle bin
  155.         /*I'm considering writing this script in the create_entry.php file
  156.         as it would make it easier to display the information
  157.         although this would destroy the centralised paradigm of my website.
  158.        
  159.         ...Screw it, the create_entry.php will be incorporated into this main .php file
  160.         */
  161.         echo <<<END
  162. <div class="container_comments_banner">
  163. Admin Control Panel
  164. </div>
  165. <div class="container_admin_controls">
  166. <div class="container_create_content">
  167. <span class="entry_title">Create Entry</span><br>
  168. <form enctype="multipart/form-data" method="post" action="index.php">
  169. <input name="action" type="hidden" value="post">
  170. Title:
  171. <input type="text" name="title" id="title" />
  172. </br>
  173. Body (HTML scripting enabled):
  174. </br>
  175. <textarea name="body" id="body" cols="45" rows="5"></textarea>
  176. </br>
  177. File:
  178. <input type="hidden" name="MAX_FILE_SIZE" value="300000000000" />
  179. <input name="uploadedimage" type="file" />
  180. </br>
  181. <input type="submit" name="button" id="button" value="Submit" />
  182. </form>
  183. </br>
  184. </div>
  185. END;
  186.         //Div is not closed yet
  187.         //open the recycle bin directory
  188.         $dir = "resources/entries/recycle/";
  189.         if(is_dir($dir)){
  190.             if($dh = opendir($dir)){
  191.                 $c = 0;
  192.                 while(($file[$c] = readdir($dh)) !== false){
  193.                     $c++;
  194.                     //that's a funny looking piece of code, this was taken mainly from php.net, but altered for my needs.
  195.                 }
  196.             closedir($dh);
  197.             }
  198.         }
  199.         if(isset($file)){
  200.             echo <<<END
  201. <div class="container_recycle_bin">
  202. <span class="entry_title">Recycle Bin</span>
  203. END;
  204.             $flag = true;
  205.             foreach($file as $key => $value){
  206.                 //I could do this as a for loop, but foreach is easier
  207.                 //I need to think about how this is going to be formatted visually.
  208.                 if(file_exists("resources/entries/recycle/$value")){
  209.                     $fh[$key] = fopen("resources/entries/recycle/$value", 'r');
  210.                     //Get the entries name, and image
  211.                     $title = trim(fgets($fh[$key]));
  212.                     $date = trim(fgets($fh[$key]));
  213.                     $image = trim(fgets($fh[$key]));
  214.                     $entry = explode(".", $value);
  215.                     $entry = $entry[0];
  216.                     if($image != "none" && $image != ""){
  217.                         $flag = false;
  218.                         echo <<<END
  219. <div class="container_recycleBinOptions">
  220. <img class="small_image" src="resources/entries/images/$image">
  221. <span class="small_title">$title</span>
  222. <span class="small_date">$date</span>
  223. <div class="container_restore_entry"><a href="index.php?entry=$entry&action=restore">restore</a></div>
  224. <div class="container_delete_entry"><a href="index.php?entry=$entry&action=deleteentry">delete permanently</a></div>
  225. </div>
  226. END;
  227.                     }elseif($image != ""){
  228.                         $flag = false;
  229.                         echo <<<END
  230. <div class="container_recycleBinOptions">
  231. <span class="small_title">$title</span>
  232. <span class="small_date">$date</span>
  233. <div class="container_restore_entry"><a href="index.php?entry=$entry&action=restore">restore</a></div>
  234. <div class="container_delete_entry"><a href="index.php?entry=$entry&action=deleteentry">delete permanently</a></div>
  235. </div>
  236. END;
  237.                     }
  238.                     fclose($fh[$key]);
  239.                 }
  240.             }
  241.             if($flag){
  242.                 echo "<div class=\"container_bin_is_empty\">Recycle Bin Is Empty</div>";
  243.             }
  244.         }
  245.         echo <<<END
  246. </div>
  247. </div>
  248. <div class="container_comments_banner">
  249. Entries
  250. </div>
  251. END;
  252.     }
  253.     //display entries
  254.     //display 10 items per page
  255.     if($_GET['page']){
  256.         $page = $_GET['page'];
  257.     }else{
  258.         $page = 1;
  259.     }
  260.     for($i = $entry_count-(($page-1)*10); $i >= 0 && $i > $entry_count-$page*10; $i--){
  261.         if(file_exists("resources/entries/$i.txt")){
  262.             $fh[3] = fopen("resources/entries/$i.txt", 'r');
  263.             echo "<div onclick=\"location.href='index.php?entry=$i';\" class=\"container_home_entry\">";
  264.             $title = trim(fgets($fh[3]));
  265.             $date = trim(fgets($fh[3]));
  266.             $image = trim(fgets($fh[3]));
  267.             echo <<<END
  268. <span class="entry_title">$title</span>
  269. <span class="entry_date">$date</span>
  270. <p class="entry_body">
  271. END;
  272.             $file = "resources/entries/images/$image";
  273.             if($image != "none" && file_exists($file)){
  274.                 echo "<img src=\"$file\" border=0 class=\"entry_image\">\n";
  275.             }
  276.             while(!feof($fh[3])){
  277.                 echo trim(nl2br(fgets($fh[3])))."\n";
  278.             }
  279.             echo <<<END
  280. <a href="index.php?entry=$i">more...</a>
  281. </p></div>
  282. END;
  283.             fclose($fh[3]);
  284.         }
  285.     }
  286.     echo "<br>\n";
  287.     if($page > 1){
  288.         $out = $page-1;
  289.         echo "<a href=\"index.php?page=$out\"><img src=\"resources/images/newer.png\" border=0></a>";
  290.     }
  291.     if($entry_count-$page*10 > 0){
  292.         $out = $page+1;
  293.         echo "<a href=\"index.php?page=$out\"><img src=\"resources/images/older.png\" border=0></a>";
  294.     }
  295. }
  296.  
  297. function display_entry($entry, $admin, $user_comments){
  298.     //must remember, if admin is true, they have the ability to delete this post.
  299.     page_start();
  300.     if(file_exists("resources/entries/$entry.txt")){
  301.         //Display the entry
  302.         $fh[3] = fopen("resources/entries/$entry.txt", 'r');
  303.         echo "<div class=\"container_home_entry\">";
  304.         $title = trim(fgets($fh[3]));
  305.         $date = trim(fgets($fh[3]));
  306.         $image = trim(fgets($fh[3]));
  307.         if($admin){
  308.             echo "<div class=\"container_delete_comment\"><a href=\"index.php?entry=$entry&action=recycle\">delete</a></div>\n";
  309.         }
  310.         echo <<<END
  311. <span class="entry_title">$title</span>
  312. <span class="entry_date">$date</span>
  313. <p class="entry_body">
  314. END;
  315.         $file = "resources/entries/images/$image";
  316.         if($image != "none" && file_exists($file)){
  317.             $explode = explode(".", $image);
  318.             $name = $explode[0];
  319.             $type = $explode[1];
  320.             echo "<a href=\"index.php?action=displayimage&target=$name&type=$type&source=$entry\" target=\"_new\"><img src=\"$file\" border=0 class=\"entry_image\"></a>\n";
  321.         }
  322.         while(!feof($fh[3])){
  323.             echo trim(nl2br(fgets($fh[3])))."\n";
  324.         }
  325.         echo "</p></div>";
  326.         fclose($fh[3]);
  327.         //display the comments
  328.         //comments are contained in "resources/entries/comments/$entry/#.txt"
  329.         //Comments count is contained in "resources/entries/comments/$entry/count.txt"
  330.         $fh[5] = fopen("resources/entries/comments/$entry/count.txt", 'r'); //The amount of comments
  331.         $count = intval(trim(fgets($fh[5])));
  332.         echo "<div class=\"container_comments_banner\">Comments</div>";
  333.         for($i = $count; $i >= 0; $i--){
  334.             $flag = 0;
  335.             $char_count = 0;
  336.             if(file_exists("resources/entries/comments/$entry/$i.txt")){
  337.                 $fh[6] = fopen("resources/entries/comments/$entry/$i.txt", 'r');
  338.                 //file layout: title \n author \n date \n content
  339.                 //Content has a character limit of 50, 50, none, 750 - respectively
  340.                 $title = substr(trim(fgets($fh[6])), 0, 50);
  341.                 $author = substr(trim(fgets($fh[6])), 0, 50);
  342.                 $date = trim(fgets($fh[6]));
  343.                 echo <<<END
  344. <div class="container_comment">
  345. END;
  346.                 if(isset($user_comments[$i]) || $admin == 1){
  347.                     //That means that this is the users comment!! :D
  348.                     //Display the ability to delete this post
  349.                     echo "<div class=\"container_delete_comment\"><a href=\"index.php?entry=$entry&action=delete&comment=$i\">delete</a></div>\n";
  350.                 }
  351.                 echo "<span class=\"entry_title\">$title - $author</span>\n";
  352.                 echo "<span class=\"entry_date\">$date</span><br>\n";
  353.                 while(!feof($fh[6]) && $char_count <= 750){
  354.                     if($flag == 0){
  355.                         echo "<p class=\"entry_body\">";
  356.                     }
  357.                     $new_line = trim(fgets($fh[6]));
  358.                     $char_count += strlen($new_line);
  359.                     $print_len = 750-$char_count;
  360.                     if((750-$char_count)== 0){
  361.                         $print_len =  0;
  362.                     }
  363.                     echo substr($new_line, 0, $print_len);
  364.                     $flag = 1;
  365.                 }
  366.                 echo "</p></div>\n";
  367.                 fclose($fh[6]);
  368.             }
  369.         }
  370.         $entry = $_GET['entry'];
  371.         echo <<<END
  372. <div class="container_create_comment">
  373. <form method="post" action="index.php?entry=$entry">
  374. <span class="entry_title">Write Comment</span>
  375. <br>
  376. <input name="action" type="hidden" value="comment">
  377. <input name="target" type="hidden" value="$entry">
  378. Title:
  379. <input type="text" name="title" id="title" />
  380. Author:
  381. <input type="text" name="author" id="author" value="Anonymous"/>
  382. <br>
  383. Body (750 character limit):
  384. <br>
  385. <textarea name="body" id="body" cols="95" rows="6"></textarea>
  386. <br>
  387.  
  388. <input type="submit" name="button" id="button" value="Submit" />
  389. </form>
  390. <br>
  391. </div>
  392. END;
  393.     }else{
  394.         echo <<<END
  395. <div class="container_home_entry">
  396. <span class="entry_title">ENTRY DOES NOT EXIST</span>
  397. </div>
  398. END;
  399.     }
  400. }
  401. ?>
Advertisement
Add Comment
Please, Sign In to add comment