Subversion Repositories computer_asset_manager_v1

Rev

Rev 30 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

/*
 * script: upload_sysinfo_report.php
 * author: Rod Rodolico
 * date:   2016-04-03
 * use:    Accept a CAMP sysinfo 3 report from client machine and
 *         place it in a file for later processing
 * Description:
 * Contents of report are upload to this script in the report GET variable
 * Other variables required are client, report date and hostname.
 * Serial number is not required.
 * A file name is built (an sanitized) from the report_date, client
 * hostname and serial number, and the four values are separated by an
 * underscore. The .yaml suffix is a constant in this version.
 * 
 * The file is then saved to STORAGE_PATH (must be writeable by web
 * server).
 * 
 * If it all succeeds, the original report content (ONLY, not the other
 * parameters) is returned. If it does not succeed, and error messsage
 * is returned.
 * 
 * NOTE: all values are required except for serial number
 * 
 * Example Usage:
 * http://url?report_date=2016-04-01 13:11:04&client=My Client Name&hostname=server.example.com&serialnumber=dd-app-005&report=now is the time for all good men
 * Obviously, all values must be URL encoded
 * 
*/

/*
 * You should create the path manually. For example, assuming you are 
 * storing it in ~/camp/sysinfo_reports, the following should be run
 * as root
 * mkdir -p ~camp/sysinfo_reports/http
 * chown camp:www-data ~camp/sysinfo_reports/http
 * chmod 775 ~camp/sysinfo_reports/http
*/

define ( 'VERSION', '1.0' );
define ( 'BUILD_DATE', '2016-04-03' );
define ( 'STORAGE_PATH', '/home/camp/sysinfo_reports/unprocessed' ); 
define ( 'TESTING', false );

function sanitize_filename ( $filename, $allowed = 'a-zA-Z0-9-', $special_chars = '_', $replace_char = '-', $removeDups = true ) {
   $filename = str_replace( $allowed . $special_chars,$replace_char, $filename );
   if ( $removeDups )
      $filename = preg_replace( "/($replace_char)+/", $replace_char, $filename );
   return $filename;
}


$date = $_REQUEST['report_date'];
$client = $_REQUEST['client'];
$hostname = $_REQUEST['hostname'];
$serial = isset( $_REQUEST['serialnumber'] ) ? $_REQUEST['serialnumber'] : '';
$report = $_FILES['report'];

/* test data
$client = 'Roome Land Surveying';
$hostname = 'router.roome.local';
$date = '"2016-03-31 01:25:04"';
$serial = 'dd-app-040';
$report = 'A Report';
*/

// some of the dates actually have quotes around them, so we remove those
// here.
$date = preg_replace( '/(")/', '', $date );
// make sure we can parse it
$date = strtotime( $date );
if ( empty( $date ) ) { // we couldn't parse the date
   // grab the original
   $date = $_REQUEST['report_date'];
   print "Unable to parse date [$date]";
   // set it to empty so we won't do any work.
   $date = '';
}

// file_put_contents( STORAGE_PATH . '/report.log', print_r( $report, true ) );
if ( empty( $date ) or empty( $client ) or empty( $hostname ) or empty( $report ) ) {
   print( "You must pass a date, client, hostname and report. The values read were\n<br />" );
   print "Date=[$date]\n<br />Client=[$client]\n<br />Hostname=[$hostname]\n<br />";
   print "Report is\n<br /><pre>";
   print_r( $report );
   print "</pre>\n";
} else { // we have the requireds
   if ( empty( $serial ) ) $serial = '';
   /* Figure out a file name for it */
   $filename = STORAGE_PATH . '/' . 
               sanitize_filename( 
               implode( '_', 
                  array( date('Y-m-d H:i:s', $date ),
                  $client,
                  $hostname,
                  $serial ) ) ) . 
                  '.yaml';
                  
   if ( move_uploaded_file( $report['tmp_name'], $filename ) ) {
      touch ( $filename, $date );
      chmod ($filename, 0666 );
      print( $report['size'] );
   } else {
      print( "Could not save file [$filename]" );
   }
}


?>