usrxprd

pstbn

Aug 8th, 2021 (edited)
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.55 KB | None | 0 0
  1. <?php
  2.  
  3. /*pstbn.php version 1.0.1*/
  4.  
  5. function msg(string $msg): void{    //small message to notify user of an error or whatever else
  6.     echo("<html><head><link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\"></head><body><code>$msg</code></body></html>");
  7. }
  8.  
  9. function validatePaste(): bool{
  10.     $paste_limit = 8192;
  11.     $desc_limit = 256;
  12.    
  13.     if($_POST["paste"]){    //check for content
  14.         $paste_len = strlen($_POST["paste"]);   //get length of paste
  15.         $desc_len = ($_POST["desc"]) ? strlen($_POST["desc"]) : 0;  //get length of description
  16.        
  17.         if($paste_len <= $paste_limit){ //validate paste length
  18.             if($desc_len <= $desc_limit){   //validate description length
  19.                 return true;
  20.             }
  21.             else{
  22.                 msg("ERROR: Description limit is $desc_limit chars. Your description if $desc_len chars.");
  23.             }
  24.         }
  25.         else{
  26.             msg("ERROR: Paste limit is $paste_limit chars. Your paste is $paste_len chars.");
  27.         }
  28.     }
  29.     else{
  30.         msg("ERROR: No content found.");
  31.     }
  32.     return false;
  33. }
  34.  
  35. function createPaste(string $paste, string $desc): ?int{
  36.     $max_pastes = 128;
  37.     $lastID_file = "p/lastID.dat";
  38.    
  39.     //encode into json
  40.     if($list = json_encode(["paste" => $paste, "size" => strlen($paste), "description" => $desc, "timestamp" => time()], true)){
  41.         if($fp = fopen($lastID_file, "r+")){    //open lastID file
  42.             $pasteID = (int)(file_get_contents($lastID_file) + 1);  //get pasteID
  43.            
  44.             if($pasteID > $max_pastes){ //check paste-count limit
  45.                 msg("Paste limit of $max_pastes pastes has been reached.");
  46.                 fclose($fp);
  47.                 return null;
  48.             }
  49.            
  50.             fwrite($fp, (string)$pasteID);  //update lastID
  51.             fclose($fp);
  52.            
  53.             $paste_file = "p/" . $pasteID . ".json";
  54.            
  55.             if($fp = fopen($paste_file, "w")){  //create paste file
  56.                 fwrite($fp, $list); //write paste and meta-data to file
  57.                 fclose($fp);
  58.                
  59.                 return $pasteID;
  60.             }
  61.             else{
  62.                 msg("ERROR: Failed to open paste file ($paste_file).");
  63.             }
  64.         }
  65.         else{
  66.             msg("ERROR: Failed to open lastID file ($lastID_file).");
  67.         }
  68.     }
  69.     else{
  70.         msg("ERROR: JSON parsing error.");
  71.     }
  72.     return null;
  73. }
  74.  
  75. function viewPaste(int $pasteID): bool{
  76.     $paste_file = "p/" . $pasteID . ".json";
  77.    
  78.     if(file_exists($paste_file)){   //check if paste exists
  79.         if($paste_data = json_decode(file_get_contents($paste_file), true)){    //obtain paste data
  80.             $paste = htmlspecialchars($paste_data["paste"]);    //parse paste
  81.             $description = htmlspecialchars($paste_data["description"]);    //parse description
  82.            
  83.             //send web page
  84.             echo("
  85.                 <html>
  86.                     <head>
  87.                         <link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\">
  88.                     </head>
  89.                     <body>
  90.                         <h2>description</h2>
  91.                         <p>" . $description . "<p>
  92.                         <h2>paste</h2>
  93.                         <p><b>timestamp:</b>&nbsp;" . $paste_data["timestamp"] . "&nbsp;|
  94.                         <b>size:</b>&nbsp;" . $paste_data["size"] . "&nbsp;(bytes)&nbsp;|
  95.                         <a href=\"\">permalink</a></p>
  96.                         <pre>" . $paste . "</pre>
  97.                     </body>
  98.                 </html>
  99.             ");
  100.             return true;
  101.         }
  102.         else{
  103.             msg("ERROR: JSON parsing error.");
  104.         }
  105.     }
  106.     else{
  107.         msg("ERROR: Failed to open paste file ($paste_file). Paste likely doesn't exist.");
  108.     }
  109.     return false;
  110. }
  111.  
  112. /*==========Main==========*/
  113.  
  114. if($_POST){ //check for POST
  115.     if(validatePaste()){    //validate
  116.         if($pasteID = createPaste($_POST["paste"], (($_POST["desc"]) ? $_POST["desc"] : ""))){  //create
  117.             msg("Paste created.<br><a href=\"pstbn.php?" . $pasteID . "\">View paste</a>");
  118.         }
  119.     }
  120. }
  121. else if($_SERVER["QUERY_STRING"]){  //check for query string
  122.     viewPaste((int)$_SERVER["QUERY_STRING"]);   //display paste
  123. }
  124. else{
  125.     msg("ERROR: No action specified.");
  126. }
  127.  
  128. ?>
Advertisement
Add Comment
Please, Sign In to add comment