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