Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
200 lines (168 sloc) 4.15 KB
<?php
/**
*@file lib.php
*
* Utility functions
*
*/
require_once(dirname(__FILE__) . '/config.inc.php');
//--------------------------------------------------------------------------------------------------
/**
* @brief Format JSON nicely
*
* From umbrae at gmail dot com posted 10-Jan-2008 06:21 to http://uk3.php.net/json_encode
*
* @param json Original JSON
*
* @result Formatted JSON
*/
function json_format($json)
{
$tab = " ";
$new_json = "";
$indent_level = 0;
$in_string = false;
/* $json_obj = json_decode($json);
if($json_obj === false)
return false;
$json = json_encode($json_obj); */
$len = strlen($json);
for($c = 0; $c < $len; $c++)
{
$char = $json[$c];
switch($char)
{
case '{':
case '[':
if(!$in_string)
{
$new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
$indent_level++;
}
else
{
$new_json .= $char;
}
break;
case '}':
case ']':
if(!$in_string)
{
$indent_level--;
$new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
}
else
{
$new_json .= $char;
}
break;
case ',':
if(!$in_string)
{
$new_json .= ",\n" . str_repeat($tab, $indent_level);
}
else
{
$new_json .= $char;
}
break;
case ':':
if(!$in_string)
{
$new_json .= ": ";
}
else
{
$new_json .= $char;
}
break;
case '"':
if($c > 0 && $json[$c-1] != '\\')
{
$in_string = !$in_string;
}
default:
$new_json .= $char;
break;
}
}
return $new_json;
}
//--------------------------------------------------------------------------
/**
* @brief Test whether HTTP code is valid
*
* HTTP codes 200 and 302 are OK.
*
* For JSTOR we also accept 403
*
* @param HTTP code
*
* @result True if HTTP code is valid
*/
function HttpCodeValid($http_code)
{
if ( ($http_code == '200') || ($http_code == '302') || ($http_code == '403'))
{
return true;
}
else{
return false;
}
}
//--------------------------------------------------------------------------
/**
* @brief GET a resource
*
* Make the HTTP GET call to retrieve the record pointed to by the URL.
*
* @param url URL of resource
*
* @result Contents of resource
*/
function get($url, $userAgent = '', $content_type = '')
{
global $config;
$data = '';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_HEADER, 1);
// timeout (seconds)
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
if ($userAgent != '')
{
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
}
if ($config['proxy_name'] != '')
{
curl_setopt ($ch, CURLOPT_PROXY, $config['proxy_name'] . ':' . $config['proxy_port']);
}
if ($content_type != '')
{
curl_setopt ($ch, CURLOPT_HTTPHEADER, array ("Accept: " . $content_type));
}
$curl_result = curl_exec ($ch);
if (curl_errno ($ch) != 0 )
{
echo "CURL error: ", curl_errno ($ch), " ", curl_error($ch);
}
else
{
$info = curl_getinfo($ch);
//print_r($info);
$header = substr($curl_result, 0, $info['header_size']);
//echo $header;
$http_code = $info['http_code'];
//echo "HTTP code=$http_code\n";
if (HttpCodeValid ($http_code))
{
$data = substr($curl_result, $info['header_size']);
//$data = $curl_result;
}
}
return $data;
}
?>