<?php
include_once("inc_apikey"); //format - $api_key = "key";
$base "http://www.flickr.com/services/rest/?api_key=$api_key&";

$size $_REQUEST["picsize"];
$string $_REQUEST["string"];
$nsid $_REQUEST["nsid"];

$string preg_replace("/[^A-Za-z0-9 \?\.!\-,&\*]/"""$string);
if (!
preg_match("/^[st]$/"$size)) 
    
$size "s";
/* Functions to parse xml - not ideal */
/* Function to handle <(HERE)>.</tag> */
function startTag($parser$name$attrs) {
    global 
$tags$doc;
    
$name strtolower($name);
    if (
$name == "rsp") {
        
$doc "";
        
$tags = Array();
    }
    
array_push($tags$name);
    
$attrs array_change_key_case($attrs);
    
$tt join("\"][\""$tags);
    eval(
"\$doc[\"$tt\"][]=\$attrs;");
}
/* Function to handle <tag>.</(HERE)> */
function endTag($parser$name) {
    global 
$tags;
    
array_pop($tags);
}
/* Function to handle <tag>(HERE)</tag> */
function cdata($parser$cdata) {
    global 
$tags$doc;
    
$tt join("\"][\""$tags);
    if (
trim($cdata))
        eval(
"\$doc[\"$tt\"][count(\$doc[\"$tt\"])-1]['cdata']=\$cdata;");
}
/* / xml */

/* Send headers */
$date gmdate("D, d M Y H:i:s");
header("Content-type: text/xml");
header("Last-Modified: $date GMT");
echo <<<HTML
<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="spellwithflickr/kastner" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

<channel>
    <title>Spell with Flickr -- $string</title>
    <link>http://metaatem.net/words/$string</link>
    <description>Spell with Flickr - fun with letters</description>
    <pubDate>$date +0000</pubDate>
    <generator>http://metaatem.net/words/</generator>

    <language>en</language>
    <copyright>(C) 2006 Kastner @ metaatem.net, Images (c) their owners</copyright>
HTML;

foreach (
preg_split("//"strtolower(preg_replace("/\/words\//"""$string))) as $letter) {
    
$img "";
    if (
strlen($letter) == 1) {     // if ($letter) fails on "0"
        
$id_spot++;
        
$group_id "";
        switch (
$letter) {
            case 
"a":
                
$tag "aa";
                
$group_id='27034531@N00';           // Letter group
                
break;
            case 
"i":
                
$tag "ii";
                
$group_id='27034531@N00';           // Letter Group
                
break;
            case (
preg_match("/[b-z]/"$letter) ? $letter: !$letter):
                
$tag $letter;
                
$group_id='27034531@N00';           // Letter Group
                
break;
            case 
"?":
                
$tag "Question Mark";
                
$tag "questionmark";              // Not sure which to use
                
$group_id='34231816@N00';           // Puncuation group
                
break;
            case 
"+":
                
$tag "plus";
                
$group_id='34231816@N00';           // Puncuation group
                
break;
            case 
"&":
                
$tag "ampersand";
                
$group_id='34231816@N00';           // Puncuation group
                
break;
            case 
"!":
                
$tag "exclamation";
                
$group_id='34231816@N00';           // Puncuation group
                
break;
            case 
".":
                
$tag "period";
                
$group_id='34231816@N00';           // Puncuation group
                
break;
            case 
"0":
                
$tag "00";
                
$group_id='54718308@N00';           // Number Group
                
break;
            case (
preg_match("/[1-9]/"$letter) ? $letter : !$letter):
                
$tag $letter;
                
$group_id='54718308@N00';           // Number Group
                
break;
            default:
                
$group_id "";
                break;
        }
        if (
$group_id) {
            if (!
$letters[$tag]) {

                
$method "flickr.groups.pools.getPhotos";
                
$limit "&per_page=100";
                
$extra "&group_id=$group_id";
                
$extra .= "&tags=$tag";

                
$method "method=$method";

                
$tags = Array();
                
/* Check the filesystem first - rudimentary caching */
                
$get_new 1;
                
$cache_name "./cache/{$method}_" str_replace("@""-"$group_id) . "_{$tag}.xml";
                
$cache_name str_replace(".."""$cache_name);
                
$cache_name preg_replace("/;\@\*/"""$cache_name);
                if (
file_exists($cache_name)) {
                    if (
time() - (filemtime($cache_name)) <= (* (60 60))) {         //hours to cache for
                        
$xml_conts file_get_contents($cache_name);
                        
$get_new 0;
                    }
                }
                
/* request the xml */
                
if ($get_new) {
                    
#echo "SKIPPED CACHE!!! - $letter<br />";    #hopefuly to figure out why I slammed the api :/
                    
$fi fopen("cache-misses.txt""a") or die("can't pen cache-misses.txt");
                    
fwrite($fi"JS Cache miss at " date("D M j G:i:s T Y") . " with letter $letter and string $string\n");
                    
fclose($fi);
                    
$xml_conts file_get_contents("$base$method$limit$email$extra");
                    if (
preg_match("/stat=\"fail/"$xml_conts) || strlen($xml_conts) == 0) {
                        
$fi fopen("cache-misses.txt""a") or die("can't pen cache-misses.txt");
                        
fwrite($fi"    JS PROBLEM-+-+ Failure\n");
                        
fclose($fi);
                        continue;
                    }
                    else {
                        
$hand fopen($cache_name'w');
                        
fwrite($hand$xml_conts);
                        
fclose($hand);
                    }
                }

                
/* for some reason, I must re-initialze each time :( */
                
$xml_parser xml_parser_create();
                
xml_set_element_handler($xml_parser"startTag""endTag");
                
xml_set_character_data_handler($xml_parser"cdata");
                
xml_parse($xml_parser$xml_contstrue) or die(sprintf("XML error: %s at line %d\n\n%s"xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser), $xml_conts));
                
xml_parser_free($xml_parser);
                
/* done parsing xml */
                
$letters[$tag] = $doc;      //Cache letters
            
}

            if (
$letters[$tag]["rsp"][0]["stat"] == "ok") {
                
$photo "";
                
$i 0;
                
/* make sure we fill it with something, this was for an early bug, it might not be an issue any longer */
                
while (!$photo["id"] and $i++<10) {
                    
$photo $letters[$tag]["rsp"]["photos"]["photo"][rand(1,count($letters[$tag]["rsp"]["photos"]["photo"]))];
                }
                
$title addslashes($photo["title"]);
                
$link "http://www.flickr.com/photos/$photo[owner]/$photo[id]";
                
$image "http://static.flickr.com/$photo[server]/$photo[id]_$photo[secret]_$size.jpg";
                
$height $photo["height"];
                
$width $photo["width"];
                
$height 75;
                
$width 75;
                
#document.write('<a href="http://www.flickr.com/photos/$photo[owner]/$photo[id]/"><img src="http://photos$photo[server].flickr.com/$photo[id]_$photo[secret]_$size.jpg" class="flickrImg" title="$title"></a>');
                
echo <<<HTML
        <item>
            <title>$title</title>
            <link>$link</link>
            <description>&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/$photo
[owner]/&quot;&gt;$photo[ownername]&lt;/a&gt; posted a photo:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;$link&quot; title=&quot;$title&quot;&gt;&lt;img src=&quot;$image&quot; width=&quot;$width&quot; height=&quot;$height&quot; alt=&quot;$title&quot; style=&quot;border: 1px solid #ddd;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

</description>
            <author>nobody@flickr.com ($photo
[ownername])</author>
            <guid isPermaLink="false">tag:flickr.com,2004:/photo/$photo
[id]</guid>
            <media:content url="$image"
                       type="image/jpeg"
                       height="$height"
                       width="$width"/>
            <media:title>$title</media:title>
            <media:text type="html">&lt;p&gt;&lt;a href=&quot;http://www.flickr.com/people/$photo
[owner]/&quot;&gt;$photo[ownername]&lt;/a&gt; posted a photo:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;$link&quot; title=&quot;$title&quot;&gt;&lt;img src=&quot;$image&quot; width=&quot;$width&quot; height=&quot;$height&quot; alt=&quot;$title&quot; style=&quot;border: 1px solid #ddd;&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

</media:text>
            <media:thumbnail url="$image" height="75" width="75" />
            <media:credit role="photographer">$photo
[ownername]</media:credit>

        </item>
HTML;

            }
            else {
                
#echo "Error - {$letters[$tag][rsp][err][0][code]}: {$letters[$tag][rsp][err][0][msg]}<br />";
            
}
        }
    }
}
?>
</channel>
</rss>