skip to Main Content

I am having some trouble with a PHP script. I am trying to do two things:

  1. Create an XML file in /usr/local/ezreplay/data/XML/ directory and add contents to it using inputs passed to it from a HTML form;

  2. Upload a PCAP file which is included in the submitted HTML form.

Here is my PHP (apologies it is a little long but I believe all of it is relevant here):

<?php

// Check if the 'expirydate' input is set
if (isset($_POST['expirydate'])) {

  // Convert the input string to a timestamp using 'strtotime'
  $timestamp = strtotime($_POST['expirydate']);

  // Format the timestamp as a 'mm/dd/yyyy' string using 'date'
  $expirydate = date('m/d/Y', $timestamp);
}

// Check if all required POST variables are set
if ( isset($_POST['destinationip']) && isset($_POST['destinationport']) && isset($expirydate) && isset($_POST['multiplier']) && isset($_POST['pcap']) ) {

    // Set the path for the XML file
    $path = '/usr/local/ezreplay/data/XML/' . trim($_POST['destinationip']) . ':' . trim($_POST['destinationport']) . ':' . $expirydate . ':' . trim($_POST['multiplier']) . ':' . trim($_POST['pcap']) . '.xml';

    // Initialize the contents of the XML file
    $contents = "";

    // Open the XML file in append mode
    if ( $fh = fopen($path,"a+") ) {

        // Add the opening 'config' tag to the XML file
        $contents .= '<config>';

        // If the 'destinationip' and 'destinationport' POST variables are not empty, add a 'destination' tag to the XML file
        if ( trim( $_POST['destinationip'] ) != "" && trim( $_POST['destinationport'] ) != "" ) {
            $contents .= "n" . '<destination>' . $_POST['destinationip'] . ':' . $_POST['destinationport'] . '</destination>';
        }

        // If the 'multiplier' POST variable is not empty, add a 'multiplier' tag to the XML file
        if ( trim( $_POST['multiplier'] ) != "" ) {
            $contents .= "n" . '<multiplier>' . $_POST['multiplier'] . '</multiplier>';
        }

        // If the 'pcap' POST variable is not empty, add a 'pcap' tag to the XML file
        if ( trim( $_POST['pcap'] ) != "" ) {
            $contents .= "n" . '<pcap>/usr/local/ezreplay/data/PCAP/' . $_POST['pcap'] . '</pcap>';

            // Add default tags to XML config file to ensure the pcap does not fail and loops continuously until expiration date hits
            $contents .= "n" . '<loop>0</loop>';
            $contents .= "n" . '<nofail>true</nofail>';
        }

        // Add the closing 'config' tag to the XML file
        $contents .= "n" . '</config>';

        // Write the contents to the file
        if ( fwrite( $fh, $contents ) ) {
            // Success
        } else {
            echo "The XML config could not be created";
        }

        // Close the file
        fclose($fh);
    }
}

// Set the target directory and file name
$target_dir = "/usr/local/ezreplay/data/PCAP/";
$basename = basename($_FILES["pcap"]["name"]);
$target_file = $target_dir . $basename;

// Check if the file has a pcap extension
$allowedExtensions = array('pcap');
$basenameWithoutExt = null;
foreach ($allowedExtensions as $allowedExtension) {
    if (preg_match('#\.' . $allowedExtension . '$#',$basename)) {
        $basenameWithoutExt = substr($basename,0,-1 - strlen($allowedExtension));
        break;
    }
}

// Accept only .pcap files
if (is_null($basenameWithoutExt)) {
    echo "Sorry, only .pcap files are allowed. Please try creating your Packet Replay again using a .pcap file.";
    exit;
}

// Check if the file already exists
if (file_exists($target_file)) {
    echo "The Packet Replay could not be started, the PCAP is already running.";
    exit;
}

// Try to upload the file
if (move_uploaded_file($_FILES["pcap"]["tmp_name"], $target_file)) {
    // Success
} else {
    echo "Sorry, there was an error uploading your file.";
    exit;
}

// Start the Packet Replay
$command = '/usr/local/ezreplay/bin/startreplay.sh ' . $path;
system($command);

echo "The Packet Replay has been started.";

?>

Now the file upload is working and I can see the final echo message being returned in my browser however the XML file is never created. I have changed the directory ownership to the apache user and even chmod 777 to eliminate any permissions issues but it still doesn’t create the file.

Any ideas why this is not working? The PHP and apache error logs don’t show any issues and as I mentioned the script seems to be working to a degree as the file upload takes place perfectly.

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    I managed to fix this with the following edits.

    <?php
    
    // Set the target directory and file name
    $target_dir = "/usr/local/ezreplay/data/PCAP/";
    $basename = basename($_FILES["pcap"]["name"]);
    $target_file = $target_dir . $basename;
    
    // Check if the file has a pcap extension
    $allowedExtensions = array('pcap');
    $basenameWithoutExt = null;
    foreach ($allowedExtensions as $allowedExtension) {
        if (preg_match('#\.' . $allowedExtension . '$#',$basename)) {
            $basenameWithoutExt = substr($basename,0,-1 - strlen($allowedExtension));
            break;
        }
    }
    
    // Accept only .pcap files
    if (is_null($basenameWithoutExt)) {
        echo "Sorry, only .pcap files are allowed. Please try creating your Packet Replay again using a .pcap file.";
        exit;
    }
    
    // Check if the file already exists
    if (file_exists($target_file)) {
        echo "The Packet Replay could not be started, the PCAP is already running.";
        exit;
    }
    
    // Try to upload the file
    if (move_uploaded_file($_FILES["pcap"]["tmp_name"], $target_file)) {
        //Success
    } else {
        echo "Sorry, there was an error uploading your file.";
        exit;
    }
    
    // Check if the 'expirydate' input is set
    if (isset($_POST['expirydate'])) {
      // Convert the input string to a timestamp using 'strtotime'
      $timestamp = strtotime($_POST['expirydate']);
      // Format the timestamp as a 'mm-dd-yyyy' string using 'date'
      $expirydate = date('m-d-Y', $timestamp);
    }
    
    // Check if 'destinationip', 'destinationport', 'multiplier' and 'pcap' required POST variables are set
    if (isset($_POST['destinationip']) && isset($_POST['destinationport']) && isset($_POST['multiplier'])) {
    
      // Set the filename and path for the XML file
      $file = '/usr/local/ezreplay/data/XML/' . trim($_POST['destinationip']) . ':' . trim($_POST['destinationport']) . ':' . trim($_POST['multiplier'])  . ':' . $expirydate . ':' . $_FILES["pcap"]["name"] . '.xml';
    
      // Initialize the contents of the XML file
      $contents = "";
    
      // Add the opening 'config' tag to the XML file
      $contents .= '<config>';
    
      // If the 'destinationip' and 'destinationport' POST variables are not empty, add a 'destination' tag to the XML file
      if (trim($_POST['destinationip']) != "" && trim($_POST['destinationport']) != "") {
        $contents .= "n" . '<destination>' . $_POST['destinationip'] . ':' . $_POST['destinationport'] . '</destination>';
      }
    
      // If the 'multiplier' POST variable is not empty, add a 'multiplier' tag to the XML file
      if (trim($_POST['multiplier']) != "") {
        $contents .= "n" . '<multiplier>' . $_POST['multiplier'] . '</multiplier>';
      }
    
      // If the 'pcap' POST variable is not empty, add a 'pcap' tag to the XML file
      if (trim($_FILES["pcap"]["name"]) != "") {
        $contents .= "n" . '<pcap>/usr/local/ezreplay/data/PCAP/' . $_FILES["pcap"]["name"] . '</pcap>';
      }
    
      // Add default tags to XML config file to ensure the pcap does not fail and loops continuously until expiration date hits
      $contents .= "n" . '<loop>0</loop>';
      $contents .= "n" . '<nofail>true</nofail>';
    
      // Add the closing 'config' tag to the XML file
      $contents .= "n" . '</config>';
    
      // Write the contents to the file
      if (file_put_contents($file, $contents)) {
        // Success
      } else {
        echo "The XML config could not be created";
      }
    }
    
    // Start the Packet Replay
    $command = '/usr/local/ezreplay/bin/startreplay.sh ' . $path;
    system($command);
    
    echo "The Packet Replay has been started.";
    
    ?>
    

  2. I think the file is not being created due to "/" in the filename. As mentioned at Allowed characters in filename

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search