Passed
Branch develop (a7390e)
by
unknown
25:38
created

getURLContent()   F

Complexity

Conditions 21
Paths 6144

Size

Total Lines 133
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 67
c 0
b 0
f 0
nc 6144
nop 5
dl 0
loc 133
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 to get a 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', 'POSTALREADYFORMATED', '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;
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
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for curl_setopt(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

57
	/** @scrutinizer ignore-unhandled */ @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

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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
    elseif ($postorget == 'POSTALREADYFORMATED')
82
    {
83
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // HTTP request is 'POST' but param string is taken as it is
84
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);	// param = content of post, like a xml string
85
    }
86
    elseif ($postorget == 'PUT')
87
    {
88
        $array_param=null;
89
    	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
90
    	if (! is_array($param)) parse_str($param, $array_param);
0 ignored issues
show
introduced by
The condition is_array($param) is always false.
Loading history...
91
    	else
92
    	{
93
    	    dol_syslog("parameter param must be a string", LOG_WARNING);
94
    	    $array_param=$param;
95
    	}
96
    	curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param));	// Setting param x=a&y=z as PUT fields
97
    }
98
    elseif ($postorget == 'PUTALREADYFORMATED')
99
    {
100
    	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT'
101
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $param);	// param = content of post, like a xml string
102
    }
103
    elseif ($postorget == 'HEAD')
104
    {
105
    	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'
106
    	curl_setopt($ch, CURLOPT_NOBODY, true);
107
    }
108
    elseif ($postorget == 'DELETE')
109
    {
110
    	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');	// POST
111
    }
112
    else
113
    {
114
    	curl_setopt($ch, CURLOPT_POST, 0);			// GET
115
    }
116
117
    //if USE_PROXY constant set at begin of this method.
118
    if ($USE_PROXY)
119
    {
120
        dol_syslog("getURLContent set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS);
121
        //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10
122
        curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
123
        if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS);
124
    }
125
126
    //getting response from server
127
    $response = curl_exec($ch);
128
129
    $request = curl_getinfo($ch, CURLINFO_HEADER_OUT);	// Reading of request must be done after sending request
130
131
    dol_syslog("getURLContent request=".$request);
132
    //dol_syslog("getURLContent response =".response);	// This may contains binary data, so we dont output it
133
    dol_syslog("getURLContent response size=".strlen($response));	// This may contains binary data, so we dont output it
134
135
    $rep=array();
136
    if (curl_errno($ch))
137
    {
138
        // Ad keys to $rep
139
        $rep['content']=$response;
140
141
        // moving to display page to display curl errors
142
		$rep['curl_error_no']=curl_errno($ch);
143
        $rep['curl_error_msg']=curl_error($ch);
144
145
		dol_syslog("getURLContent response array is ".join(',', $rep));
146
    }
147
    else
148
    {
149
    	$info = curl_getinfo($ch);
150
151
    	// Ad keys to $rep
152
    	$rep = $info;
153
    	//$rep['header_size']=$info['header_size'];
154
    	//$rep['http_code']=$info['http_code'];
155
    	dol_syslog("getURLContent http_code=".$rep['http_code']);
156
157
        // Add more keys to $rep
158
        $rep['content']=$response;
159
    	$rep['curl_error_no']='';
160
    	$rep['curl_error_msg']='';
161
162
    	//closing the curl
163
        curl_close($ch);
164
    }
165
166
    return $rep;
167
}
168
169
170
/**
171
 * Function get second level domain name.
172
 * For example: https://www.abc.mydomain.com/dir/page.html return 'mydomain'
173
 *
174
 * @param	string	  $url 				    Full URL.
175
 * @param	int	 	  $mode					0=return 'mydomain', 1=return 'mydomain.com', 2=return 'abc.mydomain.com'
176
 * @return	string						    Returns domaine name
177
 */
178
function getDomainFromURL($url, $mode = 0)
179
{
180
	$tmpdomain = preg_replace('/^https?:\/\//i', '', $url);				// Remove http(s)://
181
	$tmpdomain = preg_replace('/\/.*$/i', '', $tmpdomain);				// Remove part after domain
182
	if ($mode == 2)
183
	{
184
		$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)\.([^\.]+)$/', '\1.\2.\3', $tmpdomain);	// Remove part 'www.' before 'abc.mydomain.com'
185
	}
186
	else
187
	{
188
		$tmpdomain = preg_replace('/^.*\.([^\.]+)\.([^\.]+)$/', '\1.\2', $tmpdomain);				// Remove part 'www.abc.' before 'mydomain.com'
189
	}
190
	if (empty($mode))
191
	{
192
		$tmpdomain = preg_replace('/\.[^\.]+$/', '', $tmpdomain);			// Remove first level domain (.com, .net, ...)
193
	}
194
195
	return $tmpdomain;
196
}
197
198
/**
199
 * Function root url from a long url
200
 * For example: https://www.abc.mydomain.com/dir/page.html return 'https://www.abc.mydomain.com'
201
 * For example: http://www.abc.mydomain.com/ return 'https://www.abc.mydomain.com'
202
 *
203
 * @param	string	  $url 				    Full URL.
204
 * @return	string						    Returns root url
205
 */
206
function getRootURLFromURL($url)
207
{
208
	$prefix='';
209
	$tmpurl = $url;
210
	$reg = null;
211
	if (preg_match('/^(https?:\/\/)/i', $tmpurl, $reg)) $prefix = $reg[1];
212
	$tmpurl = preg_replace('/^https?:\/\//i', '', $tmpurl);				// Remove http(s)://
213
	$tmpurl = preg_replace('/\/.*$/i', '', $tmpurl);					// Remove part after domain
214
215
	return $prefix.$tmpurl;
216
}
217
218
/**
219
 * Function to remove comments into HTML content
220
 *
221
 * @param	string	  $content 				Text content
222
 * @return	string						    Returns text without HTML comments
223
 */
224
function removeHtmlComment($content)
225
{
226
	$content = preg_replace('/<!--[^\-]+-->/', '', $content);
227
	return $content;
228
}
229