erik_keresztes

Post processing script

Mar 17th, 2020
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php
  2.  
  3. //       ____           _                                       _                             _       _  
  4. //      |  _ \ ___  ___| |_   _ __  _ __ ___   ___ ___  ___ ___(_)_ __   __ _   ___  ___ _ __(_)_ __ | |_
  5. //      | |_) / _ \/ __| __| | '_ \| '__/ _ \ / __/ _ \/ __/ __| | '_ \ / _` | / __|/ __| '__| | '_ \| __|
  6. //      |  __/ (_) \__ \ |_  | |_) | | | (_) | (_|  __/\__ \__ \ | | | | (_| | \__ \ (__| |  | | |_) | |_
  7. //      |_|   \___/|___/\__| | .__/|_|  \___/ \___\___||___/___/_|_| |_|\__, | |___/\___|_|  |_| .__/ \__|
  8. //                           |_|        by Erik Keresztes               |___/                  |_|        
  9. //                                        (fiverr.com/erik_keresztes)
  10.  
  11. require_once('config.php');
  12.  
  13. if($_SERVER['REQUEST_METHOD'] == 'POST')
  14. {
  15.     // get captcha response
  16.     $captcharesponse = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".recaptchaKeys['secret']."&response={$_POST['g-recaptcha-response']}"));
  17.    
  18.     // if user did not complete captcha
  19.     if($captcharesponse->success === false)
  20.     {
  21.         // redirect to error page
  22.         redirect('../error?m=captcha');
  23.         die();
  24.     }
  25.  
  26.     // init variables
  27.     $replyTo = 0;
  28.     $fileID = 0;
  29.     $title = '';
  30.     $message = '';
  31.  
  32.     // sanitize & store user input
  33.     $replyTo = mysqli_real_escape_string($conn, htmlspecialchars($_POST['replyto']));
  34.     $title = mysqli_real_escape_string($conn, htmlspecialchars($_POST['title']));
  35.     $message = mysqli_real_escape_string($conn, htmlspecialchars($_POST['message']));
  36.    
  37.     // if message is longer than 3 characters or a file was uploaded
  38.     if(strlen($message) > 3 || file_exists($_FILES['uploadedfile']['tmp_name']) || is_uploaded_file($_FILES['uploadedfile']['tmp_name']))
  39.     {  
  40.         // if file was uploaded
  41.         if(file_exists($_FILES['uploadedfile']['tmp_name']) || is_uploaded_file($_FILES['uploadedfile']['tmp_name']))
  42.         {
  43.             // process file
  44.             include('processFile.php');
  45.         }
  46.  
  47.         // get IP of user
  48.         $ip = $_SERVER['REMOTE_ADDR'];
  49.  
  50.         if($replyTo == 0) // if post is parent
  51.         {
  52.             // insert post in database
  53.             mysqli_query($conn, "INSERT INTO posts (replyToThreadNr, title, message, fileID, ip, id) VALUES (0, '{$title}', '{$message}', {$fileID}, '{$ip}', '')");
  54.            
  55.             // get thread number from database
  56.             $threadNr = end(mysqli_fetch_array(mysqli_query($conn, "SELECT nr FROM posts WHERE ip = '{$ip}' ORDER BY date DESC LIMIT 1")));
  57.            
  58.             // generate ID of user based on IP and thread number (this script is for a bulletin board with no authentication)
  59.             $id = strtoupper(substr(md5($ip . $threadNr), 0, 10));
  60.  
  61.             // insert ID into database
  62.             mysqli_query($conn, "UPDATE posts SET id = '{$id}' WHERE nr = {$threadNr}");
  63.         }
  64.         else // if post is reply
  65.         {
  66.             // generate ID based on IP and the thread user is replying to
  67.             $id = strtoupper(substr(md5($ip . $replyTo), 0, 10));
  68.  
  69.             // insert post in database
  70.             mysqli_query($conn, "INSERT INTO posts (replyToThreadNr, title, message, fileID, ip, id) VALUES ({$replyTo}, '{$title}', '{$message}', {$fileID}, '{$ip}', '{$id}')");
  71.         }
  72.         // get post number from database
  73.         $postNr = end(mysqli_fetch_array(mysqli_query($conn, "SELECT nr FROM posts WHERE ip = '{$ip}' ORDER BY date DESC LIMIT 1")));
  74.         redirect('../post?nr='.$postNr); // redirect user to the post
  75.     }
  76.     else
  77.     {
  78.         redirect('../error?m=msgshortornoimg');
  79.     }
  80. }
  81.  
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment