|
1
|
|
|
<?php |
|
2
|
|
|
/* Copyright (C) 2008-2013 Laurent Destailleur <[email protected]> |
|
3
|
|
|
* |
|
4
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU General Public License as published by |
|
6
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* This program is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16
|
|
|
* or see http://www.gnu.org/ |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* \file htdocs/core/lib/geturl.lib.php |
|
21
|
|
|
* \brief This file contains functions dedicated to get URL. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Function get content from an URL (use proxy if proxy defined) |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $url URL to call. |
|
28
|
|
|
* @param string $postorget 'POST', 'GET', 'HEAD', 'PUT', 'PUTALREADYFORMATED', 'DELETE' |
|
29
|
|
|
* @param string $param Parameters of URL (x=value1&y=value2) or may be a formated content with PUTALREADYFORMATED |
|
30
|
|
|
* @param integer $followlocation 1=Follow location, 0=Do not follow |
|
31
|
|
|
* @param string[] $addheaders Array of string to add into header. Example: ('Accept: application/xrds+xml', ....) |
|
32
|
|
|
* @return array Returns an associative array containing the response from the server array('content'=>response,'curl_error_no'=>errno,'curl_error_msg'=>errmsg...) |
|
33
|
|
|
*/ |
|
34
|
|
|
function getURLContent($url,$postorget='GET',$param='',$followlocation=1,$addheaders=array()) |
|
35
|
|
|
{ |
|
36
|
|
|
//declaring of global variables |
|
37
|
|
|
global $conf, $langs; |
|
38
|
|
|
$USE_PROXY=empty($conf->global->MAIN_PROXY_USE)?0:$conf->global->MAIN_PROXY_USE; |
|
39
|
|
|
$PROXY_HOST=empty($conf->global->MAIN_PROXY_HOST)?0:$conf->global->MAIN_PROXY_HOST; |
|
40
|
|
|
$PROXY_PORT=empty($conf->global->MAIN_PROXY_PORT)?0:$conf->global->MAIN_PROXY_PORT; |
|
41
|
|
|
$PROXY_USER=empty($conf->global->MAIN_PROXY_USER)?0:$conf->global->MAIN_PROXY_USER; |
|
42
|
|
|
$PROXY_PASS=empty($conf->global->MAIN_PROXY_PASS)?0:$conf->global->MAIN_PROXY_PASS; |
|
43
|
|
|
|
|
44
|
|
|
dol_syslog("getURLContent postorget=".$postorget." URL=".$url." param=".$param); |
|
45
|
|
|
|
|
46
|
|
|
//setting the curl parameters. |
|
47
|
|
|
$ch = curl_init(); |
|
48
|
|
|
|
|
49
|
|
|
/*print $API_Endpoint."-".$API_version."-".$PAYPAL_API_USER."-".$PAYPAL_API_PASSWORD."-".$PAYPAL_API_SIGNATURE."<br>"; |
|
50
|
|
|
print $USE_PROXY."-".$gv_ApiErrorURL."<br>"; |
|
51
|
|
|
print $nvpStr; |
|
52
|
|
|
exit;*/ |
|
53
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
54
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, 1); |
|
55
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Dolibarr geturl function'); |
|
56
|
|
|
|
|
57
|
|
|
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ($followlocation?true:false)); // We use @ here because this may return warning if safe mode is on or open_basedir is on |
|
58
|
|
|
|
|
59
|
|
|
if (count($addheaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders); |
|
60
|
|
|
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // To be able to retrieve request header and log it |
|
61
|
|
|
|
|
62
|
|
|
// By default use tls decied by PHP. |
|
63
|
|
|
// You can force, if supported a version like TLSv1 or TLSv1.2 |
|
64
|
|
|
if (! empty($conf->global->MAIN_CURL_SSLVERSION)) curl_setopt($ch, CURLOPT_SSLVERSION, $conf->global->MAIN_CURL_SSLVERSION); |
|
65
|
|
|
//curl_setopt($ch, CURLOPT_SSLVERSION, 6); for tls 1.2 |
|
66
|
|
|
|
|
67
|
|
|
//turning off the server and peer verification(TrustManager Concept). |
|
68
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
|
69
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
|
70
|
|
|
|
|
71
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)?5:$conf->global->MAIN_USE_CONNECT_TIMEOUT); |
|
72
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT)?30:$conf->global->MAIN_USE_RESPONSE_TIMEOUT); |
|
73
|
|
|
|
|
74
|
|
|
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // PHP 5.5 |
|
75
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // We want response |
|
76
|
|
|
if ($postorget == 'POST') |
|
77
|
|
|
{ |
|
78
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); // POST |
|
79
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // Setting param x=a&y=z as POST fields |
|
80
|
|
|
} |
|
81
|
|
|
else if ($postorget == 'PUT') |
|
82
|
|
|
{ |
|
83
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT' |
|
84
|
|
|
if (! is_array($param)) parse_str($param, $array_param); |
|
85
|
|
|
else |
|
86
|
|
|
{ |
|
87
|
|
|
dol_syslog("parameter param must be a string", LOG_WARNING); |
|
88
|
|
|
$array_param=$param; |
|
89
|
|
|
} |
|
90
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param)); // Setting param x=a&y=z as PUT fields |
|
91
|
|
|
} |
|
92
|
|
|
else if ($postorget == 'PUTALREADYFORMATED') |
|
93
|
|
|
{ |
|
94
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT' |
|
95
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string |
|
96
|
|
|
} |
|
97
|
|
|
else if ($postorget == 'HEAD') |
|
98
|
|
|
{ |
|
99
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD' |
|
100
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, true); |
|
101
|
|
|
} |
|
102
|
|
|
else if ($postorget == 'DELETE') |
|
103
|
|
|
{ |
|
104
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // POST |
|
105
|
|
|
} |
|
106
|
|
|
else |
|
107
|
|
|
{ |
|
108
|
|
|
curl_setopt($ch, CURLOPT_POST, 0); // GET |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
//if USE_PROXY constant set at begin of this method. |
|
112
|
|
|
if ($USE_PROXY) |
|
113
|
|
|
{ |
|
114
|
|
|
dol_syslog("getURLContent set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS); |
|
115
|
|
|
//curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10 |
|
116
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT); |
|
117
|
|
|
if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
//getting response from server |
|
121
|
|
|
$response = curl_exec($ch); |
|
122
|
|
|
|
|
123
|
|
|
$request = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request |
|
124
|
|
|
|
|
125
|
|
|
dol_syslog("getURLContent request=".$request); |
|
126
|
|
|
//dol_syslog("getURLContent response =".response); // This may contains binary data, so we dont output it |
|
127
|
|
|
dol_syslog("getURLContent response size=".strlen($response)); // This may contains binary data, so we dont output it |
|
128
|
|
|
|
|
129
|
|
|
$rep=array(); |
|
130
|
|
|
if (curl_errno($ch)) |
|
131
|
|
|
{ |
|
132
|
|
|
// Ad keys to $rep |
|
133
|
|
|
$rep['content']=$response; |
|
134
|
|
|
|
|
135
|
|
|
// moving to display page to display curl errors |
|
136
|
|
|
$rep['curl_error_no']=curl_errno($ch); |
|
137
|
|
|
$rep['curl_error_msg']=curl_error($ch); |
|
138
|
|
|
|
|
139
|
|
|
dol_syslog("getURLContent response array is ".join(',',$rep)); |
|
140
|
|
|
} |
|
141
|
|
|
else |
|
142
|
|
|
{ |
|
143
|
|
|
$info = curl_getinfo($ch); |
|
144
|
|
|
|
|
145
|
|
|
// Ad keys to $rep |
|
146
|
|
|
$rep = $info; |
|
147
|
|
|
//$rep['header_size']=$info['header_size']; |
|
148
|
|
|
//$rep['http_code']=$info['http_code']; |
|
149
|
|
|
dol_syslog("getURLContent http_code=".$rep['http_code']); |
|
150
|
|
|
|
|
151
|
|
|
// Add more keys to $rep |
|
152
|
|
|
$rep['content']=$response; |
|
153
|
|
|
$rep['curl_error_no']=''; |
|
154
|
|
|
$rep['curl_error_msg']=''; |
|
155
|
|
|
|
|
156
|
|
|
//closing the curl |
|
157
|
|
|
curl_close($ch); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return $rep; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Function get second level domain name. |
|
166
|
|
|
* For example: https://www.abc.mydomain.com/dir/page.html return 'mydomain' |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $url Full URL. |
|
169
|
|
|
* @return string Returns domaine name |
|
170
|
|
|
*/ |
|
171
|
|
|
function getDomainFromURL($url) |
|
172
|
|
|
{ |
|
173
|
|
|
$tmpdomain = preg_replace('/^https?:\/\//i', '', $url); // Remove http(s):// |
|
174
|
|
|
$tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain); // Remove part after domain |
|
175
|
|
|
$tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain); // Remove first level domain (.com, .net, ...) |
|
176
|
|
|
$tmpdomain = preg_replace('/^[^\.]+\./', '', $tmpdomain); // Remove part www. before domain name |
|
177
|
|
|
|
|
178
|
|
|
return $tmpdomain; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Function root url from a long url |
|
183
|
|
|
* For example: https://www.abc.mydomain.com/dir/page.html return 'https://www.abc.mydomain.com' |
|
184
|
|
|
* For example: http://www.abc.mydomain.com/ return 'https://www.abc.mydomain.com' |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $url Full URL. |
|
187
|
|
|
* @return string Returns root url |
|
188
|
|
|
*/ |
|
189
|
|
|
function getRootURLFromURL($url) |
|
190
|
|
|
{ |
|
191
|
|
|
$prefix=''; |
|
192
|
|
|
$tmpurl = $url; |
|
193
|
|
|
if (preg_match('/^(https?:\/\/)/i', $tmpurl, $reg)) $prefix = $reg[1]; |
|
194
|
|
|
$tmpurl = preg_replace('/^https?:\/\//i', '', $tmpurl); // Remove http(s):// |
|
195
|
|
|
$tmpurl = preg_replace('/\/.*$/i', '', $tmpurl); // Remove part after domain |
|
196
|
|
|
|
|
197
|
|
|
return $prefix.$tmpurl; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Function to remove comments into HTML content |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $content Text content |
|
204
|
|
|
* @return string Returns text without HTML comments |
|
205
|
|
|
*/ |
|
206
|
|
|
function removeHtmlComment($content) |
|
207
|
|
|
{ |
|
208
|
|
|
$content = preg_replace('/<!--[^\-]+-->/', '', $content); |
|
209
|
|
|
return $content; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
|