<?
    
# Retrieve FoaF from URI, possibly with autodiscovery.
    # Returns actual location and content.

    # (c) 2002-2010 Morten Frederiksen
    # License: http://www.gnu.org/licenses/gpl

include_once('Throttle.php');

function
getFoaF($foaf)
{
    
$blacklist=array(
        
'http://www.ecademy.com/module.php?mod=network&op=foafrdf&uid=40645',
        
'http://www.sentient.co.uk/foaf/thomas_power_foaf.xml',
        
'http://www.sentient.co.uk/foaf/jonathan_greensted_foaf.xml',
        
'http://jibbering.com/foaf/santa.rdf');
    if (
preg_match('"^http://[^/]*?(ecademy\.com|livejournal\.com|tribe\.net|qdos\.com)/"',$foaf)
            ||
in_array($foaf,$blacklist)) {
        
header('HTTP/1.1 400 Bad Request');
        
header('Content-Type: text/plain');
        print(
"ETOOMANYFRIENDS: Sorry, that URI has been blacklisted, it's too heavy for this little service...");
        exit;
    }

    
# Some security...
    
if (!preg_match('|^http:|',$foaf))
    {
        
header('HTTP/1.1 400 Bad Request');
        print(
'<head><title>Error</title></head><body><h1>Error</h1><p>Non-HTTP request denied ('.htmlspecialchars($foaf).').</p></body>');
        exit;
    };

    
# Retrieve...
    
$foafdata=getFoaF_curl($foaf);

    
# Check for HTML link...
    
if ($foafdata
            
&& (preg_match('|<head.+?(<link\s+[^>]+type="application/rdf\+xml"[^>]*>).*?</head>|is',$foafdata,$link)
            &&
preg_match('|\s+title="?[^">]*?foaf|i',$link[1])
            &&
preg_match('|\s+href="?([^"\s>]+)"?[\s>]|i',$link[1],$M)
            ||
preg_match('|<body.+?(<a\s+[^>]+foaf[^>]*>).+?</body>|i',$foafdata,$link)
            &&
preg_match('|\s+href="?([^"\s>]+)"?[\s>]|i',$link[1],$M)))
    {
        
$link=$M[1];
        
# Try to handle relative URIs...
        
$htmluri=parse_url($foaf);
        if (!isset(
$htmluri['path'])) $htmluri['path']='/';
        
$foafuri=parse_url($link);
        
$foaf=(@$foafuri['scheme']!='')?$link:
                (
preg_match('|^/|',$foafuri['path'])?$htmluri['scheme'].'://'.$htmluri['host'].$link:
                (
preg_match('|^(/(.*/)?)[^/]*$|',$htmluri['path'],$M)?$htmluri['scheme'].'://'.$htmluri['host'].$M[1].$link:
                
$foaf));
        
$foafdata=getFoaF_curl($foaf);
    };
    return array(
$foaf,$foafdata);
};

function
getFoaF_curl($uri) {
    
$ch=curl_init();
    
curl_setopt($ch,CURLOPT_USERAGENT,'CURL; http://xml.mfd-consult.dk/foaf/');
    
curl_setopt($ch,CURLOPT_URL,str_replace(' ','%20',$uri));
    
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: application/rdf+xml, */*'));
    
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    
curl_setopt($ch,CURLOPT_TIMEOUT,10);
    
curl_setopt($ch,CURLOPT_HEADER,0);
    
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    
$data=curl_exec($ch);
    
curl_close($ch);
    return
$data;
}

?>