· 6 years ago · Dec 17, 2019, 07:40 PM
1<?php
2/*
3php?upload[]=<log url>&upload[]=<log url>&title=<log title>&map=<map name>&api=<api key>
4*/
5header("Access-Control-Allow-Origin: *");
6//error_reporting(0);
7function getIP()
8{
9 $ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
10 foreach ($ip_keys as $key) {
11 if (array_key_exists($key, $_SERVER) === true) {
12 foreach (explode(',', $_SERVER[$key]) as $ip) {
13 // trim for safety measures
14 $ip = trim($ip);
15 // attempt to validate IP
16 if (validate_ip($ip)) {
17 return $ip;
18 }
19 }
20 }
21 }
22 return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : false;
23}
24function validate_ip($ip) {
25 if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
26 return false;
27 }
28 return true;
29}
30
31/* ODCZYTANIE JSON */
32$JSON_POST = json_decode(file_get_contents('php://input'), true);
33
34$USER_IP = getIP();
35if (!isset($JSON_POST['api'])) {
36 exit('{"error": "Missing api field.", "success": false}');
37}
38if (!isset($JSON_POST['title'])) {
39 exit('{"error": "Missing title field.", "success": false}');
40}
41if (!isset($JSON_POST['map'])) {
42 exit('{"error": "Missing map field.", "success": false}');
43}
44if (!isset($JSON_POST['ids'])) {
45 exit('{"error": "Missing ids[] field.", "success": false}');
46}
47if (count($JSON_POST['ids']) <= 0) {
48 exit('{"error": "ids[] field must contain an array of valid ids.", "success": false}');
49}
50/* BOT WYSYŁA OD RAZU ARRAYLISTE Z LISTĄ ID */
51$log_ids = $JSON_POST['ids'];
52
53/* STARE POZYSKIWANIE ID Z LISTY URL
54$log_ids = array();
55foreach ($upload_urls as $log_url) {
56 $upload_url_parts = explode('/', $log_url);
57 $upload_url_id_array = explode('#', end($upload_url_parts));
58 $upload_url_id = $upload_url_id_array[0];
59 array_push($log_ids, $upload_url_id);
60}*/
61$storage_dir = str_replace(array(
62 '.',
63 ':'
64), '-', $USER_IP) . '/';
65$log_files = array();
66
67echo 'StorageDir: '.$storage_dir;
68
69if (!file_exists(substr($storage_dir, 0, -1))) {
70 echo '<br>mkdir: '.substr($storage_dir, 0, -1);
71 mkdir(substr($storage_dir, 0, -1));
72}
73foreach ($log_ids as $id) {
74 $log_zip_dir = $storage_dir . $id . '_log.zip';
75 if (!$chk = fopen('http://logs.tf/logs/log_' . $id . '.log.zip', 'r')) {
76 exit('{"error": "Invalid log url submitted.", "success": false}');
77 }
78 else {
79 file_put_contents($log_zip_dir, fopen('http://logs.tf/logs/log_' . $id . '.log.zip', 'r'));
80 //EXAMPLE: ./255-255-255-0/1234567_log.zip
81 $log_zip = new ZipArchive;
82 $log_zip->open($log_zip_dir);
83 $log_zip->extractTo($storage_dir);
84 $log_zip->close();
85 array_push($log_files, $storage_dir . 'log_' . $id . '.log');
86 }
87}
88//array for log file directories is stored in $log_files
89$final_log_dir = $storage_dir . 'LOG_FINAL-' . microtime() . '.log';
90$final_log = fopen($final_log_dir, 'a+');
91foreach ($log_files as $f) {
92 $l = file_get_contents($f) . "\n";
93 fwrite($final_log, $l);
94}
95fclose($final_log);
96$UPLOAD_URL = 'http://logs.tf/upload';
97$post = array(
98 'title' => $JSON_POST['title'],
99 'map' => $JSON_POST['map'],
100 'key' => $JSON_POST['api'],
101 'logfile' => curl_file_create($final_log_dir),
102 'uploader' => "Sharky's Logify v1.3"
103);
104$ch = curl_init($UPLOAD_URL);
105$ch_set = array(
106 CURLOPT_POST => 1,
107 CURLOPT_POSTFIELDS => $post,
108 CURLOPT_FOLLOWLOCATION => 1,
109 CURLOPT_HEADER => 0,
110 CURLOPT_RETURNTRANSFER => 1
111);
112curl_setopt_array($ch, $ch_set);
113$response = curl_exec($ch);
114echo ($response);
115$ffiles = glob($storage_dir . '*');
116foreach ($ffiles as $ffile) {
117 unlink($ffile);
118}
119?>