forked from jay2503/Poster
/
poster.php
97 lines (72 loc) · 2.52 KB
/
poster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
require_once 'config.php';
$_SESSION['url']=$_REQUEST['url'];
addURL($_REQUEST['url']);
$_SESSION['method']=$_REQUEST['method'];
$parseURL = parse_url($_REQUEST['url']);
$keyValue = array();
foreach ($_REQUEST['key'] as $k=>$key){
if (isset($key) && $key != ""){
$keyValue[$key]=$_REQUEST['value'][$k];
addKey($key,$parseURL['host']);
}
}
$_SESSION['key_value']=$keyValue;
$headerKeyValue = array();
$isContentTypeSet = FALSE;
foreach ($_REQUEST['header_value'] as $k=>$value){
if (isset($value) && $value != ""){
if ($_REQUEST['header_key'][$k] == "Content-Type"){
$isContentTypeSet = TRUE;
}
$headerKeyValue[]=$_REQUEST['header_key'][$k].": ".$value;
}
}
$_SESSION['header_key_value']=$headerKeyValue;
if(!$isContentTypeSet){
$headerKeyValue[]='Content-Type: multipart/form-data';
}
$headerKeyValue[]='Expect:';
$ch = curl_init();
$url = ($_REQUEST['method'] == "POST") ? $_REQUEST['url'] : $_REQUEST['url'].(strpos($_REQUEST['url'], "?")===false?"?":"&").http_build_query($keyValue);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if(($_REQUEST['method'] == "POST")){
curl_setopt($ch, CURLOPT_POST, ($_REQUEST['method'] == "POST"));
}
curl_setopt($ch, CURLOPT_HTTPGET, ($_REQUEST['method'] == "GET"));
if(in_array("Content-Type: application/json", $headerKeyValue)){
$keyValue = json_encode($keyValue);
}
if(($_REQUEST['method'] == "POST")){
curl_setopt($ch, CURLOPT_POSTFIELDS, ($_REQUEST['method'] == "POST") ? $keyValue : null);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerKeyValue);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, false);
$result = curl_exec ($ch);
if ($result === false){
$result = "<div style='color:red;'>".curl_error($ch)."</div>";
}
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$insertData = array(
'url' => $_REQUEST['url'],
'params' => serialize(json_decode($keyValue, true)),
'method' => $_REQUEST['method'],
'response' => $result,
'time' => time(),
);
$history_id = has_in_history($insertData);
if(!$history_id){
$id = re_db_insert("history", $insertData);
}else{
$id = $history_id;
re_db_update("history", $insertData, "id=".$history_id);
}
$history = re_db_select("history", array("*"),"id = $id");
$his = $history[0];
$return = array("id"=>$id,"history"=>getHistoryAcc($his));
echo json_encode($return);
exit;
?>