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' );
|
30 |
rodolico |
43 |
define ( 'STORAGE_PATH', '/home/camp/sysinfo_reports/unprocessed' );
|
20 |
rodolico |
44 |
define ( 'TESTING', false );
|
17 |
rodolico |
45 |
|
|
|
46 |
function sanitize_filename ( $filename, $allowed = 'a-zA-Z0-9-', $special_chars = '_', $replace_char = '-', $removeDups = true ) {
|
|
|
47 |
$filename = str_replace( $allowed . $special_chars,$replace_char, $filename );
|
|
|
48 |
if ( $removeDups )
|
|
|
49 |
$filename = preg_replace( "/($replace_char)+/", $replace_char, $filename );
|
|
|
50 |
return $filename;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
|
18 |
rodolico |
54 |
$date = $_REQUEST['report_date'];
|
|
|
55 |
$client = $_REQUEST['client'];
|
|
|
56 |
$hostname = $_REQUEST['hostname'];
|
|
|
57 |
$serial = isset( $_REQUEST['serialnumber'] ) ? $_REQUEST['serialnumber'] : '';
|
|
|
58 |
$report = $_FILES['report'];
|
4 |
rodolico |
59 |
|
17 |
rodolico |
60 |
/* test data
|
|
|
61 |
$client = 'Roome Land Surveying';
|
|
|
62 |
$hostname = 'router.roome.local';
|
|
|
63 |
$date = '"2016-03-31 01:25:04"';
|
|
|
64 |
$serial = 'dd-app-040';
|
|
|
65 |
$report = 'A Report';
|
|
|
66 |
*/
|
4 |
rodolico |
67 |
|
17 |
rodolico |
68 |
// some of the dates actually have quotes around them, so we remove those
|
|
|
69 |
// here.
|
|
|
70 |
$date = preg_replace( '/(")/', '', $date );
|
|
|
71 |
// make sure we can parse it
|
|
|
72 |
$date = strtotime( $date );
|
|
|
73 |
if ( empty( $date ) ) { // we couldn't parse the date
|
|
|
74 |
// grab the original
|
18 |
rodolico |
75 |
$date = $_REQUEST['report_date'];
|
17 |
rodolico |
76 |
print "Unable to parse date [$date]";
|
|
|
77 |
// set it to empty so we won't do any work.
|
|
|
78 |
$date = '';
|
|
|
79 |
}
|
|
|
80 |
|
20 |
rodolico |
81 |
// file_put_contents( STORAGE_PATH . '/report.log', print_r( $report, true ) );
|
17 |
rodolico |
82 |
if ( empty( $date ) or empty( $client ) or empty( $hostname ) or empty( $report ) ) {
|
18 |
rodolico |
83 |
print( "You must pass a date, client, hostname and report. The values read were\n<br />" );
|
|
|
84 |
print "Date=[$date]\n<br />Client=[$client]\n<br />Hostname=[$hostname]\n<br />";
|
|
|
85 |
print "Report is\n<br /><pre>";
|
|
|
86 |
print_r( $report );
|
|
|
87 |
print "</pre>\n";
|
17 |
rodolico |
88 |
} else { // we have the requireds
|
|
|
89 |
if ( empty( $serial ) ) $serial = '';
|
|
|
90 |
/* Figure out a file name for it */
|
|
|
91 |
$filename = STORAGE_PATH . '/' .
|
|
|
92 |
sanitize_filename(
|
|
|
93 |
implode( '_',
|
|
|
94 |
array( date('Y-m-d H:i:s', $date ),
|
|
|
95 |
$client,
|
|
|
96 |
$hostname,
|
|
|
97 |
$serial ) ) ) .
|
|
|
98 |
'.yaml';
|
18 |
rodolico |
99 |
|
|
|
100 |
if ( move_uploaded_file( $report['tmp_name'], $filename ) ) {
|
17 |
rodolico |
101 |
touch ( $filename, $date );
|
18 |
rodolico |
102 |
print( $report['size'] );
|
17 |
rodolico |
103 |
} else {
|
|
|
104 |
print( "Could not save file [$filename]" );
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
4 |
rodolico |
109 |
?>
|