Subversion Repositories sysadmin_scripts

Rev

Rev 123 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 123 Rev 125
Line 46... Line 46...
46
 * chmod 775 ~camp/sysinfo_reports/http
46
 * chmod 775 ~camp/sysinfo_reports/http
47
*/
47
*/
48
 
48
 
49
define ( 'VERSION', '1.0' );
49
define ( 'VERSION', '1.0' );
50
define ( 'BUILD_DATE', '2016-04-03' );
50
define ( 'BUILD_DATE', '2016-04-03' );
51
define ( 'STORAGE_PATH', '/home/camp/sysinfo_reports/unprocessed' ); 
51
define ( 'STORAGE_PATH', '/srv/camp/reports' ); 
52
define ( 'TESTING', false );
-
 
53
 
52
 
54
function sanitize_filename ( $filename, $allowed = 'a-zA-Z0-9-', $special_chars = '_', $replace_char = '-', $removeDups = true ) {
53
function sanitize_filename ( $filename, $allowed = 'a-zA-Z0-9-', $special_chars = '_', $replace_char = '-', $removeDups = true ) {
55
   $filename = str_replace( $allowed . $special_chars,$replace_char, $filename );
54
   $filename = str_replace( $allowed . $special_chars,$replace_char, $filename );
56
   if ( $removeDups )
55
   if ( $removeDups )
57
      $filename = preg_replace( "/($replace_char)+/", $replace_char, $filename );
56
      $filename = preg_replace( "/($replace_char)+/", $replace_char, $filename );
58
   return $filename;
57
   return $filename;
59
}
58
}
60
 
59
 
61
 
60
 
62
$date = $_REQUEST['report_date'];
-
 
63
$client = $_REQUEST['client'];
-
 
64
$hostname = $_REQUEST['hostname'];
-
 
65
$serial = isset( $_REQUEST['serialnumber'] ) ? $_REQUEST['serialnumber'] : '';
-
 
66
$report = $_FILES['report'];
-
 
67
 
-
 
68
/* test data
-
 
69
$client = 'Roome Land Surveying';
-
 
70
$hostname = 'router.roome.local';
-
 
71
$date = '"2016-03-31 01:25:04"';
-
 
72
$serial = 'dd-app-040';
-
 
73
$report = 'A Report';
-
 
74
*/
-
 
75
 
-
 
76
 
-
 
77
function saveFile ( $filesHash, $targetPath, $filename, $info ) {
61
function saveFile ( $filesHash, $targetPath, $filename, $info ) {
78
   if ( $filesHash['error'] )
62
   if ( $filesHash['error'] )
79
      return false;
63
      return "Error in filesHash " . $filesHash[error];
80
   // first, let's get a file name we can use
64
   // first, let's get a file name we can use
81
   if ( ! $filename ) {
65
   if ( ! $filename ) {
82
      $filename = sanitize_filename( isset( $filesHash['name'] ? $filesHash['name'] : $filesHash['tmp_name']  );
66
      $filename = sanitize_filename( isset( $filesHash['name'] ) ? $filesHash['name'] : $filesHash['tmp_name']  );
83
      if ( ! $filename ) {
67
      if ( ! $filename ) {
84
         $filename = uniqid( rand(), true )
68
         $filename = uniqid( rand(), true );
-
 
69
      }
85
   }
70
   }
86
   $targetPath .= '/' . $filename;
71
   $targetPath .= '/' . $filename;
87
   while ( file_exists( $targetPath ) ) {
72
   while ( file_exists( $targetPath ) ) {
88
      $targetPath .= '.1';
73
      $targetPath .= '.1';
89
   }
74
   }
90
   # Now, move the uploaded file
75
   # Now, move the uploaded file
91
   move_uploaded_file( $filesHash['tmp_name'], $targetPath );
76
   if ( move_uploaded_file( $filesHash['tmp_name'], $targetPath ) ) {
92
   $targetPath .= '.info';
77
      $targetPath .= '.info';
93
   file_put_contents( $targetPath, $info );
78
      file_put_contents( $targetPath, $info );
-
 
79
   } else {
-
 
80
      return "could not save file $targetPath";
-
 
81
   }
94
   return true;
82
   return 'Ok';
95
}
83
}
96
 
84
 
97
 
85
 
98
// file_put_contents( STORAGE_PATH . '/report.log', print_r( $report, true ) );
86
// file_put_contents( STORAGE_PATH . '/report.log', print_r( $report, true ) );
99
 
87
 
-
 
88
 
-
 
89
$path = STORAGE_PATH;
-
 
90
if ( isset( $_REQUEST['upload_type'] ) ) {
100
$path = STORAGE_PATH . isset( $_REQUEST['upload_type'] : '/' . sanitize_filename($_REQUEST['upload_type'])  );
91
   $path .= '/' . sanitize_filename( $_REQUEST['upload_type'] );
-
 
92
}
-
 
93
 
-
 
94
if ( ! is_dir( $path ) ) {
-
 
95
   mkdir( $path, 0777, true );
-
 
96
}
-
 
97
 
101
$text = array;
98
$text = array();
102
foreach ( $_REQUEST as $key => $value ) {
99
foreach ( $_REQUEST as $key => $value ) {
103
   $text[] = $key . ':' $value;
100
   $text[] = $key . ':' . $value;
104
}
101
}
105
$text = implode( "\n", $text );
102
$text = implode( "\n", $text );
106
 
-
 
107
foreach ( $_FILES as $key => $value ) {
103
foreach ( $_FILES as $key => $value ) {
108
   saveFile( 
104
   print saveFile( 
109
      $value, 
105
      $value, 
110
      $path,
106
      $path,
111
      isset( $_REQUEST['filename'] ) ? sanitize_filename( $_REQUEST['filename'] : '' ),
107
      isset( $_REQUEST['filename'] ) ? sanitize_filename( $_REQUEST['filename'] ) : '',
112
      $text
108
      $text
113
   );
109
   );
114
}
110
}
115
 
111
 
116
 
112