-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJustSendClient.php
More file actions
233 lines (204 loc) · 6.96 KB
/
JustSendClient.php
File metadata and controls
233 lines (204 loc) · 6.96 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/*
JustSend.pl curl class
*/
class JustSendClient
{
protected $Curl;
protected $Port = 0;
protected $Url = "localhost";
protected $Method = "GET";
protected $Timeout = 60;
protected $ConnectionTimeout = 10;
protected $FollowLocation = true;
protected $VerifySsl = true;
protected $Json = false;
protected $Headers = array();
protected $Files = array();
protected $Data = array();
protected $Token = "";
protected $InputFileName = "files";
protected $Params = '';
protected $Gzip = true;
protected $Http2 = true;
protected $Session = true;
protected $ShowHeader = false;
// Proxy
protected $ProxyHost = "";
protected $ProxyPort = "";
protected $ProxyUser = "";
protected $ProxyPass = "";
function __construct(){
$this->Curl = curl_init();
}
function SetDisableSession(){
$this->Session = false;
}
function AddPort($port = 0){
if($port > 0){
$this->Port = $port;
}
}
function AddUrl($url = "localhost"){
$this->Url = $url;
}
function AddToken($token = ""){
$this->Token = $token;
}
function AddHeader($str){
$this->Headers[] = $str;
}
function AddData($name, $value){
if(!empty($name)){
$this->Data[$name] = $value;
}
}
function AddFile($path){
if(file_exists($path)){
$this->Files[] = $path;
}
}
function SetMethod($name = "GET"){
if($name == "POST" || $name == "PUT" || $name == "DELETE"){
$this->Method = $name;
}else{
$this->Method = "GET";
}
}
function SetJson(){
$this->Json = true;
}
function SetHttp1(){
$this->Http2 = false;
}
function SetDisableGzip(){
$this->Gzip = false;
}
function SetShowHeader(){
$this->ShowHeader = true;
}
function SetAllowSelfsigned(){
$this->VerifySsl = false;
}
function SetInputFileName($name){
if(!empty($name) && strlen($name) > 0){
$this->InputFileName = $name;
}
}
function SetProxy($host = "", $port = "", $user = "", $pass = "")
{
$this->ProxyHost = $host;
$this->ProxyPort = $port;
$this->ProxyUser = $user;
$this->ProxyPass = $pass;
}
function Send(){
// Port
if($this->Port > 0){
curl_setopt($this->Curl, CURLOPT_PORT, $this->Port);
}
// GET url params
if($this->Method == "GET"){
foreach($this->Data as $k => $v){
$this->Params .= $k.'='.$v.'&';
}
$this->Params = trim($this->Params, '&');
curl_setopt($this->Curl, CURLOPT_URL, $this->Url.'?'.$this->Params);
}else{
curl_setopt($this->Curl, CURLOPT_URL, $this->Url);
}
// Gzip encoding
if($this->Gzip == true){
curl_setopt($this->Curl, CURLOPT_ENCODING, 'gzip');
}
// Http2
if($this->Http2 == true){
curl_setopt($this->Curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
}
// Show header
if($this->ShowHeader == true){
curl_setopt($this->Curl, CURLOPT_HEADER, true);
}
curl_setopt($this->Curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->Curl, CURLOPT_CUSTOMREQUEST, $this->Method);
curl_setopt($this->Curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->Curl, CURLOPT_FOLLOWLOCATION, $this->FollowLocation);
curl_setopt($this->Curl, CURLOPT_CONNECTTIMEOUT, $this->ConnectionTimeout);
curl_setopt($this->Curl, CURLOPT_TIMEOUT, $this->Timeout);
curl_setopt($this->Curl, CURLOPT_SSLVERSION, 6);
curl_setopt($this->Curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)");
// Proxy
if (!empty($this->ProxyHost) && !empty($this->ProxyPort)) {
curl_setopt($this->Curl, CURLOPT_PROXY, $this->ProxyHost.':'.$this->ProxyPort);
if (!empty($this->ProxyUser) && !empty($this->ProxyPass)) {
curl_setopt($this->Curl, CURLOPT_PROXYUSERPWD, $this->ProxyUser.':'.$this->ProxyPass);
}
}
// Add files
if($this->Method == "POST" && $this->Json == false){
// Add files
foreach ($this->Files as $k => $v) {
$f = realpath($v);
if(file_exists($f)){
$fc = new CurlFile($f, mime_content_type($f), basename($f));
// For -> $_FILES["files"];
$this->Data[$this->InputFileName."[".$k."]"] = $fc;
}
}
}
// Not GET
if($this->Method != "GET"){
curl_setopt($this->Curl, CURLOPT_POST, 1);
// Json headers
if($this->Json == true){
curl_setopt($this->Curl, CURLOPT_POSTFIELDS, json_encode($this->Data));
$this->Headers[] = 'Content-Type: application/json';
$this->Headers[] = 'Accept: application/json';
$this->Headers[] = 'Content-Length: ' . strlen(json_encode($this->Data));
}else{
if(count($this->Files) > 0){
curl_setopt($this->Curl, CURLOPT_POSTFIELDS, $this->Data);
}else{
curl_setopt($this->Curl, CURLOPT_POSTFIELDS, http_build_query($this->Data));
$this->Headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
}
}
// Set token
if(!empty($this->Token)){
$this->Headers[] = 'App-Key: '.$this->Token;
$this->Headers[] = 'Authorization: Bearer '.$this->Token;
}
// Add headers
if(!empty($this->Headers)){
curl_setopt($this->Curl, CURLOPT_HTTPHEADER, $this->Headers);
}
// Ssl/Tls
if($this->VerifySsl == true){
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYPEER, 1);
}else{
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYPEER, 0);
}
// Session
if($this->Session){
curl_setopt($this->Curl, CURLOPT_COOKIESESSION, true );
curl_setopt($this->Curl, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($this->Curl, CURLOPT_COOKIEFILE, 'cookies.txt');
}
// Execute
$this->Result = curl_exec($this->Curl);
// Error code
$this->StatusCode = curl_getinfo($this->Curl, CURLINFO_HTTP_CODE);
// Error message
if (curl_errno($this->Curl)) {
$this->Error = curl_error($this->Curl);
throw new Exception('CURL_ERROR '.$this->Error, $this->StatusCode);
}
curl_close($this->Curl);
// Response
return $this->Result;
}
}
?>