Skip to content

Instantly share code, notes, and snippets.

@vittodevit
Created March 4, 2021 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vittodevit/d18b7556a4fea694ce61ce9e930b6f47 to your computer and use it in GitHub Desktop.
Save vittodevit/d18b7556a4fea694ce61ce9e930b6f47 to your computer and use it in GitHub Desktop.
logLoader
<?php
$enableDebug = false;
header('Content-Type: text/plain; charset=utf-8');
function indirizzoIpReale(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
$ip=$_SERVER['HTTP_CLIENT_IP'];
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$p_ip = indirizzoIpReale();
try {
if (
!isset($_FILES['upfile']['error']) ||
is_array($_FILES['upfile']['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
switch ($_FILES['upfile']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
if ($_FILES['upfile']['size'] > 1000000) {
throw new RuntimeException('Exceeded filesize limit.');
}
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['upfile']['tmp_name']),
array(
'txt' => 'text/plain',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
if ($c_useragent !== "libcurl-clang-agent/1.0"){
throw new RuntimeException('Invalid user agent.');
}
//0.0.0.0_15-04-2020_22-11-55.log
$filename_OK = $p_ip . "_" . date("d-m-Y_H-i-s");
$path_OK = sprintf('logserverFiles/%s.%s', $filename_OK, $ext);
if (!move_uploaded_file(
$_FILES['upfile']['tmp_name'],
$path_OK
)) {
throw new RuntimeException('Failed to move uploaded file.');
}
echo 'OK';
} catch (RuntimeException $e) {
if($enableDebug == true){
echo $e->getMessage();
echo "\n";
print_r($_FILES);
echo "\n";
echo $path_OK;
}else{
echo 'NO';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment