<?

/**
 * 
 * Version 0.2
 * 
 *   v0.2 16/8/06 - First Public Version
 * 
 * This file copyright (C) 2006 Barry Hunter (nearby@barryhunter.co.uk)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#######################

$geographApiKey "insert-key-here";
$geographDomain "www.geograph.org.uk";
$numberToDisplay 5;

// code assume that $lat&$long (ONLY decimal degrees understoond), $postcode or $gridref is set

#######################
    
if ($lat && $long) {
    
$lookup "$lat,$long";
} elseif (
$postcode) {
    
$lookup $postcode;
} elseif (
$gridref) {
    
$lookup $gridref;


if (
$lookup) {
    
    
//connect
    
$url "http://$geographDomain/syndicator.php?format=GeoPhotoRSS&q=$lookup&key=$geographApiKey";
    
$file file_get_contents($url);
    

    
//build up array
    
$inside 0;
    
$photos = array();
    foreach (
explode("\n",$file) as $line) {
        
#print "<P>".htmlentities($line);
        
if ($inside) {
            if (
strpos ($line,'</item>') !== FALSE) {
                
$inside 0;
            } elseif (
preg_match('/<([\w:]+)>(.*)<\/([\w:]+)>/',$line,$m)) {
                
$photos[$inside][$m[1]] = $m[2];
            }
        } elseif (
preg_match('/<item rdf:about=".*\/photo\/(\d+)">/',$line,$m)) {
            
$inside $m[1];
            
$photos[$inside] = array();
        } elseif (
preg_match('/<([\w:]+)>(.*)<\/([\w:]+)>/',$line,$m)) {
            
$feed[$m[1]] = $m[2];
        }
    }
    
    
//set styles
    
?><style type="text/css">
        .geograph_container {
            padding:10px;
            background:#000066;
            color:white;
        }
        .geograph_container A {
            color:white;
        }

        .geograph_photo {
            float:left;
            width=150px;
            height=150px;
            font-size:12px;
            text-align:center;
            background:#000066;
        }
    </style>
    <div class="geograph_container">
    <?
    
    
//title
    
print "<h2>Images from <a href=\"{$feed['link']}\" 
    target=\"_blank\">
{$feed['title']}</a> <small>(<a 
    href=\"
{$feed['link']}/submit.php\" target=\"_blank\">Submit your 
    Own</a>!)</small></h2>"
;

    
//display each photo
    
$c 1;
    foreach(
$photos as $id => $photo) {
        
?>
        <div class="geograph_photo">
    
        <a href="<?= $photo['link'?>" title="click to view: <?= $photo['title'?> at <?= $feed['title'?>" target="_blank">
        <img 
        src="<?= $photo['photo:thumbnail'?>
        border="0" alt="<?= $photo['title'?> by <?= $photo['dc:creator'?>"/></a><br/>
        by <a href="<?= $photo['dc:source'?>" title="click to 
        view more photos by <?= $photo['dc:creator'?>" target="_blank"><?= $photo['dc:creator'?></a><br/>
        &copy;<? echo substr($photo['dc:date'],0,4?> (<a 
        href="http://creativecommons.org/licenses/by-sa/2.0/" rel="licence" 
        target="_blank">licence</a>)
        
        </div>
        <?

        $c
++;
        if (
$c $numberToDisplay)
            break;
    }
    
    print 
"<br style=\"clear:both\"/>";
    print 
"</div>";
    
}

#######################

?>