forked from 0xABADCAFE/aoproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.curl.php
184 lines (152 loc) · 4.81 KB
/
classes.curl.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
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
<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Amiga.org classic browser proxy project
//
// (c) Karl Churchill 2010
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Curl - wrapper class for php curl function library
// Generally a fluent interface.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Curl {
const
C_TIMEOUT = 30,
C_MAXREDIRECT = 4
;
public function __construct($followlocation=true, $timeOut=self::C_TIMEOUT, $maxRedirecs=self::C_MAXREDIRECT, $binaryTransfer=false, $noBody=false) {
$this->followlocation = $followlocation;
$this->timeout = $timeOut;
$this->maxRedirects = $maxRedirecs;
$this->noBody = $noBody;
$this->binaryTransfer = $binaryTransfer;
}
public final function useAuth($use) {
$this->authentication = $use ? 1:0; // seems boolean for this didn't work with curl
return $this;
}
public final function setName($name) {
$this->auth_name = $name;
return $this;
}
public final function setPass($pass) {
$this->auth_pass = $pass;
return $this;
}
public final function setBinary($binary) {
$this->binaryTransfer = $binary;
return $this;
}
public final function setReferer($referer) {
$this->referer = $referer;
return $this;
}
public final function setCookiFileLocation($path) {
$this->cookieFileLocation = $path;
return $this;
}
public final function setPostData($postFields) {
$this->post = true;
$this->postFields = $postFields;
return $this;
}
public final function setUserAgent($userAgent) {
$this->useragent = $userAgent;
return $this;
}
public function execute($url) {
$this->cookieFileLocation = tempnam(dirname(__FILE__).'/cookies/', 'cookie_');
if ($this->cookieData) {
file_put_contents($this->cookieFileLocation, $this->cookieData);
}
$curlHandle = curl_init();
curl_setopt_array(
$curlHandle,
array(
CURLOPT_URL,$url,
CURLOPT_HTTPHEADER => array('Expect:'),
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_MAXREDIRS => $this->maxRedirects,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEJAR => $this->cookieFileLocation,
CURLOPT_COOKIEFILE => $this->cookieFileLocation,
CURLOPT_ENCODING => '',
CURLOPT_USERAGENT => $this->useragent,
CURLOPT_REFERER => $this->referer,
CURLOPT_HEADER => false,
CURLOPT_HEADERFUNCTION => array(&$this, 'parseHeader'),
)
);
if ($this->authentication == 1) {
curl_setopt($curlHandle, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
}
if ($this->post) {
curl_setopt_array(
$curlHandle,
array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($this->postFields)
)
);
}
if ($this->noBody) {
curl_setopt($curlHandle,CURLOPT_NOBODY,true);
}
//if ($this->binaryTransfer) {
//curl_setopt($curlHandle,CURLOPT_BINARYTRANSFER,true);
//}
$this->webpage = curl_exec($curlHandle);
$this->status = curl_getinfo($curlHandle,CURLINFO_HTTP_CODE);
curl_close($curlHandle);
// ok this is a bit lame but I couldn't figure out how to reliably capture the cookie data
// directly, even using a callback
$this->cookieData = file_get_contents($this->cookieFileLocation);
unlink($this->cookieFileLocation);
}
public final function getHeaderData() {
return $this->headerData;
}
public final function getCookieData() {
return $this->cookieData;
}
public final function setCookieData($data) {
$this->cookieData = $data;
}
public final function getHttpStatus() {
return $this->status;
}
public final function getContent() {
return $this->webpage;
}
public function __tostring() {
return $this->webpage;
}
private function parseHeader($curlHandle, $string) {
$len = strlen($string);
$this->headerData[] = $string;
return $len;
}
private
$useragent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1',
$cookieFileLocation = './cookie.txt',
$referer = "http://www.google.com",
$cookieData = null,
$authentication = 0,
$auth_name = '',
$auth_pass = '',
$followlocation,
$timeout,
$maxRedirects,
$post,
$postFields,
$session,
$webpage,
$headerData,
$noBody,
$status,
$binaryTransfer
;
}