$filename"); } } else { $in_fh = fopen('php://input', 'r'); $tmp_path = tempnam(UPLOAD_DIR, '.up-'); $out_fh = fopen($tmp_path, 'w'); if (FALSE === $out_fh) { die_error("Couldn't open temporary file $tmp_path"); } $hash = hash_init("sha512"); $size = 0; while (!feof($in_fh) && FALSE !== ($data = fread($in_fh, 1024*1024))) { hash_update($hash, $data); $fwrite_status = fwrite($out_fh, $data); if (FALSE === $fwrite_status || $fwrite_status < strlen($data)) { die_error("Outputting to temporary file $tmp_path failed"); } $size += $fwrite_status; } fclose($in_fh); if (FALSE === fclose($out_fh)) { die_error("Closing temporary file $tmp_path failed"); } $filename = hash_final($hash) . get_extension($_SERVER['PATH_INFO']); $filepath = UPLOAD_DIR . '/' . $filename; if (file_exists($filepath)) { unlink($tmp_path); } else { if (FALSE === rename($tmp_path, $filepath)) { die_error("Renaming temporary file $tmp_path to $filepath failed"); } } echo UPLOAD_URL . $filename . "\n"; send_notification("[hostfil.es] $_SERVER[REMOTE_ADDR] uploaded $_SERVER[PATH_INFO] $c_t -> $filename"); } function send_notification($note) { $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_connect($sock, gethostbyname(NOTIFICATION_ADDRESS), NOTIFICATION_PORT); socket_write($sock, $note, strlen($note)); socket_close($sock); } function die_error($s) { global $tmp_path, $filepath, $size; header('Status: 500 Server Is 💩'); echo $s; error_log($s); @$debug = json_encode(array('s'=>$_SERVER, 'f'=>$_FILES, 't'=>$tmp_path, 'p'=>$filepath, 'size' => $size)); send_notification("[hostfil.es] $_SERVER[REMOTE_ADDR] ! $s\n$debug"); exit; } function get_extension($file) { $ext = strrchr($file, '.'); if (FALSE === $ext) { return ''; } return $ext; } function remap_files() { $files = array(); foreach (array_keys($_FILES) as $input_name) { $files[$input_name] = array(); foreach (array_keys($_FILES[$input_name]) as $file_info_key) { if (is_array($_FILES[$input_name][$file_info_key])) { foreach ($_FILES[$input_name][$file_info_key] as $i => $v) { if (!isset($files[$input_name][$i])) $files[$input_name][$i] = array(); $files[$input_name][$i][$file_info_key] = $v; } } else { if (!isset($files[$input_name][0])) $files[$input_name][0] = array(); $files[$input_name][0][$file_info_key] = $_FILES[$input_name][$file_info_key]; } } } return $files; }