<?
    
# A PHP/XSLT based web service for adding a new friend to a FoaF file.
    # - /?foaf=http://example.com/file.rdf
    #     Show updated FoaF file if cookie for own FoaF file is set.

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

    
include_once('../getFoaF.php');

    
$path=$_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
        
$path=preg_replace('|\?.+|','',$path);
    
$ifp='foaf-ifp.xsl';
    
$ifpname='foaf-ifp-name.xsl';
    
$new='new.xsl';
    
$metadata='new.rdf';
    
$xhtml='';
    
$mename='';

    
# Create XSLT parser.
    
$xslt=xslt_create();
    
xslt_set_error_handler($xslt,'xsltError');
    
$xslterror='';

    
# Handle cookie setting and removal.
    
$_GET=array_merge($_POST,$_GET);
    if ((
$me=@$_POST['me'])!='')
    {
        
# Check for valid FoaF...
        
list($me,$medata)=getFoaF($me);
        if (!
$medata)
            
# Flag non-retrievable RDF.
            
$xhtml.='<p>Unable to find or read your FoaF (<a href="'.$me.'">'.$me.'</a>).</p>';
        
# Set cookie.
        
if (!headers_sent()
                && @
setcookie('me',$me,time()+86400*365,'/'))
            
$_COOKIE['me']=$me;
    }
    elseif ((@
$_POST['remove'])!='')
    {
        
# Remove cookie.
        
if (!headers_sent()
                &&
setcookie('me',"",time()-86400,'/'))
            
$_COOKIE['me']='';
    };

    if ((
$foaf=@$_GET['foaf'])!='')
    {
        
# Retrieve FoaF RDF/XML for friend.
        
list($foaf,$foafdata)=getFoaF($foaf);
        if (!
$foafdata)
            
# Flag non-retrievable RDF.
            
$xhtml.='<p>Unable to find or read FoaF for friend (<a href="'.$foaf.'">'.$foaf.'</a>).</p>';
        elseif (!(
$foafIFP=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                array(
'/_xsl'=>join('',file($ifp)),'/_xml'=>$foafdata),
                array(
'ifp'=>@$_GET['ifp']))))
            
# Flag un-parsable RDF.
            
$xhtml.='<p>Unable to parse FoaF for friend (<a href="'.$foaf.'">'.$foaf.'</a>).</p><p>'.$xslterror.'</p>';
        elseif (
preg_match('/#404/',$foafIFP))
            
$xhtml.='<p>Could not find a "thing" in FoaF file (<a href="'.$foaf.'">'.$foaf.'</a>).</p><p>'.$xslterror.'</p>';
        elseif (!(
$foafname=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                array(
'/_xsl'=>join('',file($ifpname)),'/_xml'=>$foafdata,'/ifp'=>$foafIFP),
                array(
'foaf'=>$foaf))))
            
# Flag un-parsable RDF.
            
$xhtml.='<p>Unable to parse IFP FoaF for friend (<a href="'.$foaf.'">'.$foaf.'</a>).</p><p>'.$xslterror.'</p>';
        else
        {
            
$foafname=str_replace("\n",'',$foafname);
            
$foafname='<a href="../explorer/?foaf='.rawurlencode($foaf).'" title="FoaF Explorer: '.htmlspecialchars($foafname).'"><img style="vertical-align: middle" src="/images/foaf-explorer.16.png" alt=""/></a> '.$foafname;
        };
    };

    if ((
$me=@$_COOKIE['me'])!='')
    {
        
# Retrieve FoaF RDF/XML for me.
        
list($me,$medata)=getFoaF($me);
        if (!
$medata)
            
# Flag non-retrievable RDF.
            
$xhtml.='<p>Unable to find or read your FoaF (<a href="'.$me.'">'.$me.'</a>).</p>';
        elseif (!(
$meIFP=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                array(
'/_xsl'=>join('',file($ifp)),'/_xml'=>$medata))))
            
# Flag un-parsable RDF.
            
$xhtml.='<p>Unable to parse your FoaF (<a href="'.$me.'">'.$me.'</a>).</p><p>'.$xslterror.'</p>';
        elseif (
preg_match('/#404/',$meIFP))
            
$xhtml.='<p>Could not find a "thing" in your FoaF file (<a href="'.$me.'">'.$me.'</a>).</p><p>'.$xslterror.'</p>';
        elseif (!(
$mename=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                array(
'/_xsl'=>join('',file($ifpname)),'/_xml'=>$medata,'/ifp'=>$meIFP),
                array(
'foaf'=>$me))))
            
# Flag un-parsable RDF.
            
$xhtml.='<p>Unable to parse your IFP FoaF (<a href="'.$foaf.'">'.$foaf.'</a>).</p><p>'.$xslterror.'</p>';
        else
        {
            
$mename=str_replace("\n",'',$mename);
            
$mename='<a href="../explorer/?foaf='.rawurlencode($me).'" title="FoaF Explorer: '.htmlspecialchars($mename).'"><img style="vertical-align: middle" src="/images/foaf-explorer.16.png" alt=""/></a> '.$mename;
        };
    };

    if (
$foaf!='' and $me!='')
    {
        
# Generate new FoaF.
        
$relation=@$_GET['relation'];
        if (!(
$new=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
                array(
'/_xsl'=>join('',file($new)),'/_xml'=>$medata,'/ifp'=>$meIFP,'/foaf'=>$foafdata,'/foafifp'=>$foafIFP),
                array(
'now'=>(gmdate('Y-m-d').'T'.gmdate('H:i:s').'Z'),'foaf'=>$foaf,'me'=>$me,'relation'=>$relation))))
            
# Flag un-parsable RDF.
            
$xhtml.='<p>Unable to generate your new FoaF description.</p><p>'.$xslterror.'</p>';
    };

    if (
$xhtml!='')
    {
        
# Display error message(s).
        
$xhtml='<div class="service-error">'.$xhtml.'</div>';
        if (
$mename!='')
        {
            
$xhtml='
<div class="service-welcome">
<p>Welcome <strong>'
.$mename.'</strong>!</p>
</div>'
.$xhtml;
        };
    }
    elseif (
$foaf!='' and $me!='')
    {
        
# Show welcome messge and updated FoaF ($me+=$foaf).
        
$xhtml.='
<div class="service-welcome">
<p>Welcome <strong>'
.$mename.'</strong>!</p>';
        if (
$relation=='')
        {
            
$xhtml.='
<p>Below is your new FoaF description, indicating that you <em>know</em> '
.$foafname.'. You may however choose to qualify your relationship by choosing a more specific term:</p>
<form method="get" action="./">
    <fieldset style="padding: 0em">
        <label style="display: block; margin-top: 0.5em" for="i__138373832_316">Relationship:</label>
        <select id="i__138373832_316" name="relation">
            <option value="friendOf">'
.strip_tags($mename).' is a Friend of '.strip_tags($foafname).'</option>
            <option value="acquaintanceOf">'
.strip_tags($mename).' is an Acquaintance of '.strip_tags($foafname).'</option>
            <option value="siblingOf">'
.strip_tags($mename).' is a Sibling of '.strip_tags($foafname).'</option>
            <option value="spouseOf">'
.strip_tags($mename).' is a Spouse of '.strip_tags($foafname).'</option>
            <option value="parentOf">'
.strip_tags($mename).' is a Parent of '.strip_tags($foafname).'</option>
            <option value="childOf">'
.strip_tags($mename).' is a Child of '.strip_tags($foafname).'</option>
            <option value="grandchildOf">'
.strip_tags($mename).' is a Grandchild of '.strip_tags($foafname).'</option>
            <option value="grandparentOf">'
.strip_tags($mename).' is a Grandparent of '.strip_tags($foafname).'</option>
            <option value="ambivalentOf">'
.strip_tags($mename).' is Ambivalent of '.strip_tags($foafname).'</option>
            <option value="antagonistOf">'
.strip_tags($mename).' is an Antagonist of '.strip_tags($foafname).'</option>
            <option value="enemyOf">'
.strip_tags($mename).' is an Enemy of '.strip_tags($foafname).'</option>
        </select><br/>
        <input type="hidden" name="foaf" value="'
.htmlspecialchars($foaf).'" />
        <input style="display: block; margin-top: 0.5em" type="submit" value="Select" />
    </fieldset>
</form>'
;
        };
        
$xhtml.='
</div>'
;
        
$xhtml.='
<div class="service">
<form method="get" action="./">
    <fieldset style="padding: 0.2em">
        <legend style="margin: 0 0 1em 0; font-weight: bold; display: block">New FoaF description, with '
.$foafname.' added:</legend>
        <textarea cols="80" rows="25" name="foaf">'
.htmlspecialchars($new).'</textarea>
    </fieldset>
</form>
<p>Note: There might be some extraneous XML namespace declarations due to the XSLT processor, you may remove those if you wish.</p>
</div>'
;
    }
    elseif (
$foaf!='')
    {
        
# Show add message.
        
$xhtml.='
<div class="service">
<p>You are about to add <strong>'
.$foafname.'</strong> to your FoaF description.</p>
<p>Please start by setting a cookie to point to your own FoaF description. After doing that, you will be presented with an updated version of your FoaF description - use it to replace your existing FoaF file.</p>
<p>If you have yet to create your own FoaF file, you can use <a href="http://xml.mfd-consult.dk/foaf/explorer/?foaf=http://www.ldodds.com/ldodds.rdf" title="FoaF Explorer: Leigh Dodds"><img style="vertical-align: bottom" src="/images/foaf-explorer.16.png" alt="[FoaF]" /></a> <a href="http://www.ldodds.com">Leigh Dodds</a>\'s <a href="http://www.ldodds.com/foaf/foaf-a-matic">Foaf-a-Matic</a> to create one.</p>
</div>'
;
    }
    elseif (
$me!='')
    {
        
# Show welcome message and add form.
        
$xhtml.='
<div class="service-welcome">
<p>Welcome <strong>'
.$mename.'</strong>!</p>
<p>Enter the location of the FoaF file for a new friend to generate an updated version of your own FoaF file, or remove the cookie containg the location of your FoaF file.</p>
</div>'
;
        
$xhtml.='
<div class="service">
<form method="get" action="./">
    <fieldset style="padding: 0.2em">
        <legend style="font-weight: bold; display: block">Add friend of a friend</legend>
        <label style="display: block" for="i__138373832_316">Location:</label>
        <input type="text" size="60" id="i__138373832_316" name="foaf" value="" /><br />
        <input style="display: block; margin-top: 0.5em" type="submit" value="Add" />
    </fieldset>
</form>
</div>'
;
    };
    if (
$me=='')
    {
        
# Show request for cookie setting.
        
$xhtml.='
<div class="service">
<form method="post" action="./">
    <fieldset style="padding: 0.2em">
        <legend style="font-weight: bold; display: block">Set cookie with location of your FoaF description</legend>
        <label style="display: block" for="i__138373832_316">Location:</label>
        <input type="text" size="60" id="i__138373832_316" name="me" value="" /><br />
        <input type="hidden" name="foaf" value="'
.htmlspecialchars($foaf).'" />
        <input style="display: block; margin-top: 0.5em" type="submit" value="Set" />
    </fieldset>
</form>
</div>'
;
    }
    else
    {
        
# Show cookie removal button.
        
$xhtml.='
<div class="service">
<form method="post" action="./">
    <fieldset style="padding: 0.2em">
        <legend style="font-weight: bold; display: block">Remove cookie with location of your FoaF description</legend>
        <p>Location: <a href="'
.htmlspecialchars($me).'">'.htmlspecialchars($me).'</a></p>
        <input type="hidden" name="remove" value="yes" />
        <input style="display: block; margin-top: 0.5em" type="submit" value="Remove" />
    </fieldset>
</form>
</div>'
;
    };

    
# Transform metadata into header and footer.
    
if (!($metahead=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
            array(
'/_xsl'=>join('',@file('metahead.xsl',1)),'/_xml'=>join('',@file($metadata))))))
        
$metahead='<head><title>?</title></head>';
    if (!(
$metatop=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
            array(
'/_xsl'=>join('',@file('metatop.xsl',1)),'/_xml'=>join('',@file($metadata))),
            array(
'path'=>$path,'uri'=>''))))
        
$metatop='<p>Unable to parse metadata.</p><p>'.$xslterror.'</p>';
    if (!(
$metafoot=@xslt_process($xslt,'arg:/_xml','arg:/_xsl',NULL,
            array(
'/_xsl'=>join('',@file('metafoot.xsl',1)),'/_xml'=>join('',@file($metadata))))))
        
$metafoot='<p>Unable to parse metadata.</p><p>'.$xslterror.'</p>';

    
# Display page.
    
print('<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
'
.$metahead.'
    <style type="text/css">
div.service,div.service-welcome,div.service-error { margin: 1em 4em; padding: 0.5em 1em 1em; border: 1px solid #c0c0c0 }
div.service-welcome { margin-right: 300px }
div.service,div.service-error { margin: 1em 4em; clear: both }
div.service-error { background-color: #f0d0d0; color: #f00000; border: 1px solid #c00000 }
    </style>
</head>
<body style="margin: 0; padding: 0">
'
.$metatop.'
    <div style="padding: 1em">
        <a href="/images/foaf-new.svg">
            <img style="float: right" src="/images/foaf-new.240.png" alt=""/>
        </a>
        <div class="meta">
            <h1><a href="http://xml.mfd-consult.dk/foaf/new/">Add New Friend</a></h1>
            <p>A web service for adding a new friend to a <a href="http://rdfweb.org/foaf/">FoaF</a> description.</p>
        </div>
                <div style="margin: auto; border: none; text-align: center">
<script type="text/javascript"><!--
google_ad_client = "pub-2944687592158127";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text";
google_ad_channel ="";
google_color_border = "80D080";
google_color_bg = "D0F0D0";
google_color_link = "0000CC";
google_color_url = "00A000";
google_color_text = "6F6F6F";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
                </div>
'
.$xhtml.'
        <p>This service makes it easier for you to add new friends to your existing FoaF description - try <a href="http://xml.mfd-consult.dk/foaf/explorer/?foaf=http://www.ldodds.com/ldodds.rdf" title="FoaF Explorer: Leigh Dodds"><img style="vertical-align: bottom" src="/images/foaf-explorer.16.png" alt="[FoaF]" /></a> <a href="http://www.ldodds.com">Leigh Dodds</a>\'s <a href="http://www.ldodds.com/foaf/foaf-a-matic">Foaf-a-Matic</a> if you don\'t have one already.</p>
        <p>Simply point it to your own FoaF file, set a cookie, then direct it to a FoaF file of the friend you wish to add, and an updated version of your FoaF description will be generated, making it easy to copy &amp; paste.</p>
        <p>The next time you wish to update your FoaF description with a new friend, all you have to do is supply the location of the FoaF file for your friend - you can even find and add friends through the <a href="../explorer/">FoaF Explorer</a>, which has direct links to this service.</p>
    </div>
'
.$metafoot.'
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-62237-5";
urchinTracker();
</script>
</body>
</html>'
);

    function
xsltError($x,$e,$l,$f)
    {
        global
$xslterror;
        
$xslterror='Sablotron XSLT transformation error';
        
reset($f);
        while(list(
$key,$val)=each($f))
        {
            if (
$key=='line')
                
$xslterror.=' on line '.$val;
            if (
$key=='msg')
                
$xslterror.=': <strong>'.$val.'</strong>';
        };
        
$xslterror.='.';
    };

?>