Ticket #793 (closed New feature: fixed)
New Code for sendHttpRequest for allow_url_fopen = 0
| Reported by: | Uli | Owned by: | vipsoft |
|---|---|---|---|
| Priority: | normal | Milestone: | Piwik 0.4.3 |
| Component: | Core | Keywords: | CoreUpdater |
| Cc: | Sensitive: | no |
Description
I've changed the sendHttpRequest to allow checking for new versions on servers where allow_url_fopen equals zero. My code is tested on my server, and works as expected.
Open core/Piwik.php and replace sendHttpRequest by the following code:
static public function sendHttpRequest($url, $timeout)
{
// Modified by Uli <m [AT] il [DOT] wolf-u [DOT] li>
$response = false;
if (ini_get('allow_url_fopen') == 0) {
if(function_exists(curl_init)) {
$ch = @curl_init();
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
@curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$response = @curl_exec ($ch);
@curl_close ($ch);
unset($ch);
} else {
$fsockurl = @parse_url($url);
if(empty($fsockurl['port'])) { $fsockurl['port'] = 80; }
$fsock = @fsockopen($fsockurl['host'], $fsockurl['port'], $errno, $errstr, $timeout);
@fputs($fsock, "GET " . $fsockurl['path'] . " HTTP/1.0\r\n");
@fputs($fsock, "HOST: " . $fsockurl['host'] . "\r\n");
@fputs($fsock, "Connection: close\r\n\r\n");
while (!@feof($fsock)) {
if ($get_info) {
$response = @fread($fsock, 1024);
} else {
if (@fgets($fsock, 1024) == "\r\n") {
$get_info = true;
}
}
}
@fclose($fsock);
unset($fsockurl, $fsock, $get_info);
}
} else {
// we make sure the request takes less than a few seconds to fail
// we also set the socket_timeout (for php < 5.2.1)
$default_socket_timeout = @ini_get('default_socket_timeout');
@ini_set('default_socket_timeout', $timeout);
// we create a stream_context (works in php >= 5.2.1)
$ctx = null;
if(function_exists('stream_context_create')) {
$ctx = stream_context_create(array('http' => array( 'timeout' => $timeout)));
}
$response = trim(@file_get_contents($url, 0, $ctx));
// restore the socket_timeout value
if(!empty($default_socket_timeout))
{
@ini_set('default_socket_timeout', $default_socket_timeout);
}
}
return $response;
}
Checks by curl if allow_url_fopen = 0. If curl is not available, it gets checked by a socket.
I hope this (or something similar) gets included into one of the next releases ;)
Attachments
Change History
Note: See
TracTickets for help on using
tickets.

