| 4 | rodolico | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           | 17 | rodolico | 3 | /*
 | 
        
           |  |  | 4 |  * script: upload_sysinfo_report.php
 | 
        
           |  |  | 5 |  * author: Rod Rodolico
 | 
        
           |  |  | 6 |  * date:   2016-04-03
 | 
        
           |  |  | 7 |  * use:    Accept a CAMP sysinfo 3 report from client machine and
 | 
        
           |  |  | 8 |  *         place it in a file for later processing
 | 
        
           |  |  | 9 |  * Description:
 | 
        
           |  |  | 10 |  * Contents of report are upload to this script in the report GET variable
 | 
        
           |  |  | 11 |  * Other variables required are client, report date and hostname.
 | 
        
           |  |  | 12 |  * Serial number is not required.
 | 
        
           |  |  | 13 |  * A file name is built (an sanitized) from the report_date, client
 | 
        
           |  |  | 14 |  * hostname and serial number, and the four values are separated by an
 | 
        
           |  |  | 15 |  * underscore. The .yaml suffix is a constant in this version.
 | 
        
           |  |  | 16 |  * 
 | 
        
           |  |  | 17 |  * The file is then saved to STORAGE_PATH (must be writeable by web
 | 
        
           |  |  | 18 |  * server).
 | 
        
           |  |  | 19 |  * 
 | 
        
           |  |  | 20 |  * If it all succeeds, the original report content (ONLY, not the other
 | 
        
           |  |  | 21 |  * parameters) is returned. If it does not succeed, and error messsage
 | 
        
           |  |  | 22 |  * is returned.
 | 
        
           |  |  | 23 |  * 
 | 
        
           |  |  | 24 |  * NOTE: all values are required except for serial number
 | 
        
           |  |  | 25 |  * 
 | 
        
           |  |  | 26 |  * Example Usage:
 | 
        
           |  |  | 27 |  * 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
 | 
        
           |  |  | 28 |  * Obviously, all values must be URL encoded
 | 
        
           |  |  | 29 |  * 
 | 
        
           |  |  | 30 | */
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 | /*
 | 
        
           |  |  | 33 |  * You should create the path manually. For example, assuming you are 
 | 
        
           |  |  | 34 |  * storing it in ~/camp/sysinfo_reports, the following should be run
 | 
        
           |  |  | 35 |  * as root
 | 
        
           |  |  | 36 |  * mkdir -p ~camp/sysinfo_reports/http
 | 
        
           |  |  | 37 |  * chown camp:www-data ~camp/sysinfo_reports/http
 | 
        
           |  |  | 38 |  * chmod 775 ~camp/sysinfo_reports/http
 | 
        
           |  |  | 39 | */
 | 
        
           |  |  | 40 |   | 
        
           |  |  | 41 | define ( 'VERSION', '1.0' );
 | 
        
           |  |  | 42 | define ( 'BUILD_DATE', '2016-04-03' );
 | 
        
           |  |  | 43 | define ( 'STORAGE_PATH', '/home/camp/sysinfo_reports/http' ); 
 | 
        
           |  |  | 44 |   | 
        
           |  |  | 45 | function sanitize_filename ( $filename, $allowed = 'a-zA-Z0-9-', $special_chars = '_', $replace_char = '-', $removeDups = true ) {
 | 
        
           |  |  | 46 |    $filename = str_replace( $allowed . $special_chars,$replace_char, $filename );
 | 
        
           |  |  | 47 |    if ( $removeDups )
 | 
        
           |  |  | 48 |       $filename = preg_replace( "/($replace_char)+/", $replace_char, $filename );
 | 
        
           |  |  | 49 |    return $filename;
 | 
        
           |  |  | 50 | }
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 | $date = $_GET['report_date'];
 | 
        
           |  |  | 54 | $client = $_GET['client'];
 | 
        
           |  |  | 55 | $hostname = $_GET['hostname'];
 | 
        
           |  |  | 56 | $serial = $_GET['serialnumber'];
 | 
        
           | 4 | rodolico | 57 | $report = $_GET['report'];
 | 
        
           |  |  | 58 |   | 
        
           | 17 | rodolico | 59 | /* test data
 | 
        
           |  |  | 60 | $client = 'Roome Land Surveying';
 | 
        
           |  |  | 61 | $hostname = 'router.roome.local';
 | 
        
           |  |  | 62 | $date = '"2016-03-31 01:25:04"';
 | 
        
           |  |  | 63 | $serial = 'dd-app-040';
 | 
        
           |  |  | 64 | $report = 'A Report';
 | 
        
           |  |  | 65 | */
 | 
        
           | 4 | rodolico | 66 |   | 
        
           | 17 | rodolico | 67 | // some of the dates actually have quotes around them, so we remove those
 | 
        
           |  |  | 68 | // here.
 | 
        
           |  |  | 69 | $date = preg_replace( '/(")/', '', $date );
 | 
        
           |  |  | 70 | // make sure we can parse it
 | 
        
           |  |  | 71 | $date = strtotime( $date );
 | 
        
           |  |  | 72 | if ( empty( $date ) ) { // we couldn't parse the date
 | 
        
           |  |  | 73 |    // grab the original
 | 
        
           |  |  | 74 |    $date = $_GET['report_date'];
 | 
        
           |  |  | 75 |    print "Unable to parse date [$date]";
 | 
        
           |  |  | 76 |    // set it to empty so we won't do any work.
 | 
        
           |  |  | 77 |    $date = '';
 | 
        
           |  |  | 78 | }
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 | if ( empty( $date ) or empty( $client ) or empty( $hostname ) or empty( $report ) ) {
 | 
        
           |  |  | 81 |    print( "You must pass a date, client, hostname and report. The values read were\n" );
 | 
        
           |  |  | 82 |    print "Date=[$date]\nClient=[$client]\nHostname=[$hostname]\n";
 | 
        
           |  |  | 83 | } else { // we have the requireds
 | 
        
           |  |  | 84 |    if ( empty( $serial ) ) $serial = '';
 | 
        
           |  |  | 85 |    /* Figure out a file name for it */
 | 
        
           |  |  | 86 |    $filename = STORAGE_PATH . '/' . 
 | 
        
           |  |  | 87 |                sanitize_filename( 
 | 
        
           |  |  | 88 |                implode( '_', 
 | 
        
           |  |  | 89 |                   array( date('Y-m-d H:i:s', $date ),
 | 
        
           |  |  | 90 |                   $client,
 | 
        
           |  |  | 91 |                   $hostname,
 | 
        
           |  |  | 92 |                   $serial ) ) ) . 
 | 
        
           |  |  | 93 |                   '.yaml';
 | 
        
           |  |  | 94 |    if ( file_put_contents( $filename, $report ) ) {
 | 
        
           |  |  | 95 |       touch ( $filename, $date );
 | 
        
           |  |  | 96 |       print( $report );
 | 
        
           |  |  | 97 |    } else {
 | 
        
           |  |  | 98 |       print( "Could not save file [$filename]" );
 | 
        
           |  |  | 99 |    }
 | 
        
           |  |  | 100 | }
 | 
        
           |  |  | 101 |   | 
        
           |  |  | 102 |   | 
        
           | 4 | rodolico | 103 | ?>
 |