Advertisement
DumperJumper

printMessage.php

Jul 16th, 2018
605
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php
  2.  
  3.     define ( 'MYSQL_HOST',      '***' );
  4.     define ( 'MYSQL_BENUTZER',  '***' );
  5.     define ( 'MYSQL_KENNWORT',  '***' );
  6.     define ( 'MYSQL_DATENBANK', '***' );
  7.      
  8.     $db_link = mysqli_connect(MYSQL_HOST, MYSQL_BENUTZER, MYSQL_KENNWORT, MYSQL_DATENBANK);
  9.    
  10.     function e($x){echo "$x\n";}
  11.    
  12.     if (mysqli_connect_errno())
  13.     {
  14.         file_put_contents("allConErrors", date("Y-m-d H:i:s")."\n", FILE_APPEND);
  15.        
  16.         if(file_exists("lastConError"))
  17.         {
  18.             $lastErr = json_decode(file_get_contents("lastConError"));
  19.            
  20.             if(abs(time() - $lastErr[0]) > $lastErr[1]*60*10)
  21.             {
  22.                 if($handle = popen('/usr/bin/lp - 2>&1', 'w'))
  23.                 {
  24.                     fwrite($handle, date("Y-m-d H:i:s")."\nCould not connect to MySQL Server!\nStage ".$lastErr[1].", ERR#".mysqli_connect_errno());
  25.                     e(date("Y-m-d H:i:s").": Printed Connection Error");
  26.                     fclose($handle);
  27.                 }
  28.                 else
  29.                 {
  30.                     e(date("Y-m-d H:i:s").": Error: Unable to pOpen");
  31.                 }
  32.                
  33.                 file_put_contents("lastConError", json_encode([time(), $lastErr[1]+1]));
  34.                 e(date("Y-m-d H:i:s").": Moved to next Error Stage");
  35.             }
  36.         }
  37.         else
  38.         {
  39.             file_put_contents("lastConError", json_encode([time(), 1]));
  40.             e(date("Y-m-d H:i:s").": Started Error Tracking");
  41.         }
  42.         exit();
  43.     }
  44.     @unlink("lastConError");
  45.    
  46.     function printMessage($ID, $name, $email, $text)
  47.     {
  48.         if($handle = popen('/usr/bin/lp -o cpi=12 - 2>&1', 'w'))
  49.         {
  50.             $ESC="\x1b";
  51.             $GS="\x1d";
  52.             # Init Printer
  53.             fwrite($handle, $ESC);
  54.             fwrite($handle, "@");
  55.            
  56.             # Justify C
  57.             fwrite($handle, $ESC);
  58.             fwrite($handle, "a");
  59.             fwrite($handle, 1);
  60.            
  61.             # Inverted On
  62.             fwrite($handle, $GS);
  63.             fwrite($handle, "B");
  64.             fwrite($handle, 1);
  65.            
  66.             fwrite($handle, "        New Message #$ID        \n");
  67.            
  68.             # Inverted Off
  69.             fwrite($handle, $GS);
  70.             fwrite($handle, "B");
  71.             fwrite($handle, 0);
  72.            
  73.             fwrite($handle, date("Y-m-d H:i")."\n\n");
  74.            
  75.             # Justify L
  76.             fwrite($handle, $ESC);
  77.             fwrite($handle, "a");
  78.             fwrite($handle, 0);
  79.  
  80.             if($name){fwrite($handle, "'$name'\n");}
  81.             if($email){fwrite($handle, "<$email>\n");}
  82.            
  83.             fwrite($handle, "\n\r");
  84.             fwrite($handle, "$text\n\r\n\r\n\r");
  85.             e(date("Y-m-d H:i:s").": Printed Message #$ID");
  86.             fclose($handle);
  87.         }
  88.         else
  89.         {
  90.             e(date("Y-m-d H:i:s").": Error: Unable to pOpen");
  91.         }
  92.     }
  93.    
  94.     $query = "SELECT * FROM Messages WHERE Printed is NULL";
  95.     $res = mysqli_query($db_link, $query);
  96.     $rows = mysqli_num_rows($res);
  97.    
  98.     e(date("Y-m-d H:i:s").": Querried $rows new Messages");
  99.    
  100.     if($res)
  101.     {
  102.         while($row = mysqli_fetch_array($res, MYSQLI_ASSOC))
  103.         {
  104.             printMessage($row["ID"], $row["Name"], $row["Email"], $row["Message"]);
  105.            
  106.             $query = "UPDATE messages SET Printed=CURRENT_TIMESTAMP() WHERE ID = ?";
  107.             $stmt = mysqli_stmt_init($db_link);
  108.            
  109.             mysqli_stmt_prepare($stmt, $query);
  110.             mysqli_stmt_bind_param($stmt, 'i', $row["ID"]);
  111.            
  112.             mysqli_stmt_execute($stmt);
  113.             e(date("Y-m-d H:i:s").": Updated Message #".$row["ID"]);
  114.            
  115.             mysqli_stmt_close($stmt);
  116.         }
  117.     }
  118.     else
  119.     {
  120.         e(date("Y-m-d H:i:s").": MySQL Error #".mysqli_errno());
  121.     }
  122.    
  123.     mysqli_free_result($res);
  124.    
  125.     e(":EOF:");
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement