<?
/*
*  flyr - search photos for geotagged data
*
*  $Header: /usr/local/cvsrep/flyr/flyr.php,v 1.21 2005/09/22 12:28:22 pauldowney Exp $
*
*  written by Paul Downey (paul.downey@whatfettle.com)
*  released under GNU General Public License (http://www.gnu.org/copyleft/gpl.html)
*/

/*
*  The quite natty phpFlickr.php API from:
*  http://www.phpflickr.com/
*/
require_once("phpFlickr.php");

/*
*  a secret php file with:
*
*    $flickr_api_key="my flickr api key";
*    $gm_api_key="my Google Maps api key";
*    $flickr_cache="mysql://username:password@mysql-hostname/mysql_database";
*/
require_once("api_keys.php");

/*
*  test for blank field
*/
function isblank($v)
{
    
trim($v);
    return
$v == "";
}

/*
*  decode flickr title, descriptions
*/
function fdecode($s)
{
    
$s = preg_replace('/\n/', '<br/>', $s);
    
$s = html_entity_decode($s);
    
$s = addslashes($s);
    return
$s;
}


class
flyr
{
    var
$date;
    var
$user;
    var
$folder = 'flyr';
    var
$search = array('tag_mode'=>'all', 'per_page'=>20, 'page'=>1);
    var
$photos = array();
    var
$ids;
    var
$flickr;
    var
$text;
    var
$tags;

    function
flyr()
    {
        global
$flickr_api_key;
        global
$flickr_cache;

        
$this->flickr = new phpFlickr($flickr_api_key);
        
$this->flickr->enableCache("db", $flickr_cache, 900);
    }

    function
parseForm($form)
    {
        
$this->date = gmdate("D, d M Y H:i:s") . " GMT";

        
$this->ids = $form['ids'];

        if (!
isblank($form['folder']))
        {
            
$this->folder = $form['folder'];
        }

        if (!
isblank($form['user']))
        {
            
$this->user = $form['user'];
            
$this->search['user_id'] = $this->flickr->people_findByUsername($form['user']);
        }

        if (!
isblank($form['per_page']))
        {
            
$this->search['per_page'] = $form['per_page'];
        }

        if (!
isblank($form['page']))
        {
            
$this->search['page'] = $form['page'];
        }

        
/*
         *  search by text or by tags
         */
        
if (!isblank($form['text']))
        {
            
$this->text = $form['text'];
            
$this->search['text'] = "geotagged " . $form['text'];
        }
        elseif (!
isblank($form['tags']))
        {
            
$this->tags = $form['tags'];
            
$this->search['tags'] = "geotagged," . $form['tags'];
        }
        else
        {
            
$this->search['tags'] = "geotagged";
        }
    }

    function
search()
    {
        
$this->photos = array();
        
$f = $this->flickr;

        if (
isblank($this->ids))
        {
            
$photos = $f->photos_search($this->search);
            
$id = $photo['id'];

            
$sep = "";

            foreach (
$photos['photo'] as $photo)
            {
                
$this->ids .= $sep . $photo['id'];
                
$sep = ",";
            }
        }

        foreach (
split("[,]", $this->ids) as $id)
        {
            
$photo = $f->photos_getInfo($id);

            
$lon = $lat = "";

            foreach (
$photo['tags']['tag'] as $tag)
            {
                
$rtag = $tag['raw'];

                if (
preg_match('/^geo:lon/', $rtag))
                {
                    
$lon = preg_replace('/^geo:long?=(.*)/', '$1', $rtag);
                }
                elseif (
preg_match('/^geo:lat/', $rtag))
                {
                    
$lat = preg_replace('/^geo:lat=(.*)/', '$1', $rtag);
                }
            }

            if (!
isblank($lon) && !isblank($lat))
            {
                
$nsid = $photo['owner']['nsid'];
                
$photo['page'] = "http://www.flickr.com/photos/" . $nsid . "/" . $photo['id'];
                
$photo['square'] = $f->buildPhotoURL($photo, "square");
                
$photo['small'] = $f->buildPhotoURL($photo, "small");

                
$photo['title'] = fdecode($photo['title']);

                
$photo['desc'] =
                    
"<a href='" . $photo['page'] . "'><img height='75' width='75'"
                    
. " src='" . $photo['square'] . "'/></a>"
                    
. "<br/><a href='" . $photo['page'] . "'>" . $photo['title'] . "</a>";

                
$photo['lon'] = $lon;
                
$photo['lat'] = $lat;

                
$photo['glink'] = "At <a href='http://maps.google.com/maps?q=" .
                    
$lat . "+" . $lon . "&t=h'>" . $lat . " " . $lon . '</a>';

                
$photo['description'] = fdecode($photo['description']);

                
$photo['from'] = "From <a href='http://www.flickr.com/photos/"
                        
. $nsid . "'>"
                        
. $photo['owner']['username']
                        .
"</a>";
                
                
$this->photos[] = $photo;
            }
        }

        return
$this->photos;
    }

    function
printTitle()
    {
        print <<<EOF
        <p>
        <a href="http://flyr.whatfettle.com/" style="text-decoration: none;"><span class="fly">fly<span class="r">r</span></span></a>
            <span class="byline">- search <a href="http://www.flickr.com">flickr</a>
            for <a href="http://www.flickr.com/groups/geotagging">geotagged</a> photos</span>
        </p>

EOF;
    }

    function
printComment()
    {
        print
        
"<!--"
        
. "\n"
        
. "\n    Generated by http://flyr.whatfettle.com"
        
. "\n    date:     " . $this->date
        
. "\n    folder:   " . $this->folder
        
. "\n    user:     " . $this->user
        
. "\n    text:     " . $this->search['text']
        .
"\n    tags:     " . $this->search['tags']
        .
"\n    user_id:  " . $this->search['user_id']
        .
"\n    per_page: " . $this->search['per_page']
        .
"\n    page:     " . $this->search['page']
        .
"\n    ids:      " . $this->ids
        
. "\n"
        
. "\n-->\n";
    }

    function
printForm()
    {
        
$text = $this->text;
        
$tags = $this->tags;
        
$user = $this->user;
        
$folder = $this->folder;
        
$per_page = $this->search['per_page'];
        
$page = $this->search['page'];

        print <<<EOF
        <form action="/flyr.kml" method="get">
          <p>
            <br/> search the text in titles, tags and descriptions:
            <br/> <input name="text" type="text" size="25" value="$text">
            <br/> <i>"Netherlands", "castle" or "balloon"</i>
            <br/>
            <br/> or <a href="http://www.flickr.com/photos/tags/">tags</a>, separated by commas:
            <br/> <input name="tags" type="text" size="25" value="$tags">
            <br/> <i>"kite", "San Francisco" or "geocaching, uk"</i>
            <br/>
            <br/> within this user's photos:
            <br/> <input name="user" type="text" size="25" value="$user">
            <br/> <i>"psd", "Norm Walsh", "danbri" or leave blank for all users</i>
            <br/>
            <br/> give the search a name:
            <br/> <input name="folder" type="text" size="25" value="$folder">
            <br/> <i>used when saving <a href="http://earth.google.com">kml</a> and <a href="http://www.topografix.com/gpx.asp">gpx</a> files</i>
            <br/>

            <input name="per_page" type="hidden" value="$per_page">
            <input name="page" type="hidden" value="$page">
            <input name="ids" type="hidden" value="">

            <br/>
            <br/>
            <input type="submit" onClick="this.form.action='photos'" value="Photos">
            <input type="submit" onClick="this.form.action='maps'" value="Google Maps">
            <input type="submit" onClick="this.form.action='flyr.kml'" value="Google Earth">
          </p>
        </form>

EOF;
    }

    function
printMore($prompt, $action, $addids, $per_pageo=0, $pageo=0)
    {
        
$text = htmlentities($this->text);
        
$tags = htmlentities($this->tags);
        
$user = htmlentities($this->user);
        
$folder = htmlentities($this->folder);
        
$per_page = (int)$this->search['per_page'] + $per_pageo;
        
$next = (int)$this->search['page'] + $pageo;

        if (
$addids) {
            
$ids = $this->ids;
        }

        print
"\n<a href='" . $action
            
. "?text=$text&tags=$tags&user=$user&folder=$folder&per_page=$per_page&page=$next&ids=$ids'>"
            
. "$prompt</a>\n";
    }

    function
printNav($name)
    {
        print
"\n<div id='nav'>\n<p>\n";

        
$sep = " | ";

        
$this->printMore('Search', 'search', 0);
        print
$sep;

        
$this->printMore('Photos', 'photos', 1);
        print
$sep;

        
$this->printMore('Google Maps', 'maps', 1);
        print
$sep;

        
$this->printMore('Google Earth', 'flyr.kml', 1);
        print
$sep;

        
$this->printMore('GPX', 'flyr.gpx', 1);
        print
$sep;

        print
"\n<a href='abouts'>About</a>\n";
        print
$sep;

        
$this->printMore('More', $name, 0, 20, 0);
        print
$sep;

        
$this->printMore('Next', $name, 0, 0, 1);

        print
"\n</p>\n</div>\n";
    }

    function
printFooter()
    {
        print
"\n";
    }

}

?>