Advertisement
Guest User

Sample Sieve script validation via Web interface using PHP

a guest
Jun 14th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.70 KB | None | 0 0
  1. <?php
  2.  
  3. if (!empty($_POST)) {
  4.     $errors = array();
  5.    
  6.     $sieveScript = $_POST['script'];
  7.     $emailBody = $_POST['body'];
  8.    
  9.     if (empty($sieveScript)) {
  10.         $errors[] = 'The script contents cannot be empty.';
  11.     }
  12.    
  13.     if (empty($emailBody)) {
  14.         $errors[] = 'The email body cannot be empty.';
  15.     }
  16.    
  17.     if (count($errors) === 0) {
  18.         $tmpScript = tempnam(sys_get_temp_dir(), '');
  19.         $tmpBody = tempnam(sys_get_temp_dir(), '');
  20.        
  21.         $handle = fopen($tmpScript, 'w');
  22.         fwrite($handle, $sieveScript);
  23.         fclose($handle);
  24.        
  25.         $handle = fopen($tmpBody, 'w');
  26.         fwrite($handle, $emailBody);
  27.         fclose($handle);
  28.        
  29.         $cmd = 'sieve-test "' . $tmpScript . '" "' . $tmpBody . '" 2>&1 1> /dev/null';
  30.        
  31.         $output = shell_exec($cmd);
  32.        
  33.         unlink($tmpScript);
  34.         unlink($tmpBody);
  35.     }
  36. }
  37.  
  38. ?>
  39. <html>
  40.  
  41. <head>
  42.     <style type="text/css">
  43.         .fullWidth {
  44.             width:100%;
  45.         }
  46.         .quarterHeight {
  47.             height:25%;
  48.         }
  49.         .error {
  50.             color:red;
  51.         }
  52.     </style>
  53. </head>
  54.  
  55. <form method="post" name="form" action="">
  56.     <h2>Sieve Script</h2>
  57.     <div>
  58.     <textarea name="script" class="fullWidth quarterHeight"><?php if (isset($sieveScript)) { echo $sieveScript; } ?></textarea>
  59.     </div>
  60.     <h2>Email Body</h2>
  61.     </div>
  62.     <div>
  63.     <textarea name="body" class="fullWidth quarterHeight"><?php if (isset($emailBody)) { echo $emailBody; } ?></textarea>
  64.     </div>
  65.     <input type="submit" value="Test Script" />
  66.     <?php
  67.     echo '<h2>Output</h2>';
  68.    
  69.     if (isset($errors) && count($errors) > 0) {
  70.         foreach ($errors as $error) {
  71.             echo '<div class="error">' . $error . '</div>';
  72.         }
  73.     }
  74.    
  75.     if (isset($output)) {
  76.         echo '<pre>' . $output . '</pre>';
  77.     }
  78.     elseif (!isset($errors)) {
  79.         echo '[No input provided]';
  80.     }
  81.     echo '</div>';
  82.     ?>
  83. </form>
  84.  
  85. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement