<?php

/*
 *     Copyright (C) 2005, Erik Borra
 *
 *      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
 *
 *      More info can be found at http://www.gnu.org/copyleft/gpl.html
 *
 *    See 'What is this page all about?' at the bottom of this script to see ... what it is all about :-)
*/

include_once('./counter.php'); // this is just to get an idea of how many people actually visit this site ;)
error_reporting(1); // comment out if you want to see that flickr is not valid xhtml + other errors and warnings

// get a random page with a smile
$frontUrl 'http://www.flickr.com/photos/tags/smile/?page='.rand(0,100); 
$frontpage implode('',file($frontUrl)); 
// load the page into a DOM tree
$dom = new DOMDocument;
$dom->loadHtml($frontpage);

// get a random smile
$xpath = new DOMXpath($dom);
$query "//div[1]/p/span/a/img"
$smiles $xpath->query($query);
while(! 
$randomSmile $smiles->item(rand(0,20)) ) { // catch a bug in flickr
 // sometimes a picture has been deleted or something like that
 // anyway, it wouldn't work, so ...
 // do nothing in this while loop but get another pic
}
$smileUrl "http://www.flickr.com".$randomSmile->parentNode->getAttribute('href');

if(
strstr($smileUrl,'drkoontz')) { // i don't know what this guy has with christmas puppets and barbies, but they are certainly not smiling: skip this guy!
      // if we got a page with only pictures from drkoontz see we don't get in an infite loop.  Go back to start without getting paid!
         
$output "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
         
$output .= "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>";
         
$output .= "<head>";
     if(isset(
$_GET['refresh'])) { // screensaver functionality
        
$time $_GET['refresh']; // get the time in seconds before refreshing
        
if($time<5$time=5// let's give the Flickr servers a break
        
$output .= "<meta http-equiv='Refresh' content='0; URL=index.php?refresh=$time' />";
     } else {
        
$output .= "<meta http-equiv='Refresh' content='0; URL=index.php' />";
     }
         
$output .= "</head><body></body></html>";
     print 
$output;
     die();
}

// fetch the source of the random smile and load it into a DOM tree
$page implode('',file($smileUrl)); 
$dom = new DOMDocument;
$dom->loadHTML($page);

// look for the image on the page
$xpath = new DOMXpath($dom);
$query "//div/img";
$smiles $xpath->query($query);
foreach(
$smiles as $smile) {
  if(
strstr($smile->getAttribute('src'),"static.flickr.com")) {
    
$src $smile->getAttribute('src');
    continue;
  }
}

// output
$output "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
$output .= "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>";
$output .= "<head>";
$output .= "<title>Smile :-)</title>";
$output .= "<style type='text/css'>";
$output .= "html { height: 100% }";
$output .= "body { height: 100% }";
$output .= "#top { position: relative; min-height: 99% }";
$output .= "html #top { height: 99% }";
$output .= "#footer { position: relative; margin-top: -2em }";
$output .= "</style>";
$output .= "<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1' />";
if(isset(
$_GET['refresh'])) { // screensaver functionality
  
$time $_GET['refresh']; // get the time in seconds before refreshing
  
if($time<5$time=5// let's give the Flickr servers a break
  
$output .= "<meta http-equiv='Refresh' content='$time; URL=index.php?refresh=$time' />";
}
$output .= "</head>";
$output .= "<body>";
$output .= "<div id='top'>";
$output .= "<center>";
//$output .= "<a href=$frontUrl>frontUrl $frontUrl</a><br />"; //4 debug: show which url was generated for the frontpage 
//$output .= "<a href=$smileUrl>smileUrl $smileUrl</a><br />"; //4 debug: show which url the smile has
$output .= "<h3>Smile</h3>"
$output .= "<table border='0'><tr><td>";
$output .= "<a href='$smileUrl'><img src='$src' border='0' alt='smile'/></a>";
$output .= "<br />";
$output .= "</td><td>";
$output .= "Smiling is contagious,<br />"
$output .= "you catch it like the flu,<br />"
$output .= "When someone smiled at<br />"
$output .= "me today, I started smiling too.<br />"
$output .= "I passed around the corner,<br />"
$output .= "and someone saw my grin -<br />"
$output .= "When he smiled I<br />"
$output .= "realized, I'd passed it on to him.<br />"
$output .= "<br />";
$output .= "I thought about that smile,<br />"
$output .= "then I realized its worth,<br />"
$output .= "A single smile, just like mine,<br />"
$output .= "could travel round the earth.<br />"
$output .= "So, if you feel a smile begin,<br />"
$output .= "don't leave it undetected -<br />"
$output .= "Let's start an epidemic quick<br />"
$output .= "and get the world infected!!!<br />"
$output .= "</td></tr></table>";
$output .= "<br />";
$output .= "</center>";
$output .= "</div>";
$output .= "<div id='footer'>";
$output .= "<center>";
$output .= "<a href='http://smile.justlol.net/'>Reload</a> this page to see another smile :-) <br />";
$output .= "<hr />";
$output .= "</center>";
$output .= "<b>What is this page all about?</b><br />";
$output .= "In the multimedia show / lecture series '<a href='http://www.heteindevannederland.net/'>Het einde van Nederland in 12 stappen</a>' van 'Rob en de rebellen', <a href='http://www.lottemeijer.com'>Lotte Meijer</a> had a talk about someone committing suicide because he didn't receive enough smiles.  Rob and Lotte asked everyone to smile more, also at strangers.  That really moved me.  I generally do smile a lot, also at strangers, and then I see them lighting up.  I remembered an old poem I read - the one you see next to the picture, and came up with the idea of making a script that would fetch a random smile from <a href='http://www.flickr.com'>flickr.com</a>.  The original idea was to make a mailinglist that would send out a fresh smile each morning.  This way you would be reminded to smile today and spread some positive vibrations throughout the world.  If there is a demand for it I will implement that feature. Or you can set it as the startpage in your browser ;-)<br /> <br />";  
$output .= "<b> How do you do it?</b><br />";
$output .= "With PHP :-)  Have a look at the <a href='./source.php'>source code</a>, if you like. <br /><br />";
$output .= "<b>Why is there not always a smile on the picture?</b><br />";
$output .= "I search Flickr for pictures with the tag smile.  Tags are userdefined labels attached to the pictures.  If the label is wrong there won't be any smile on the picture.<br /><br />";
$output .= "<b>This is neat!  Give me more!</b><br />";
$output .= "Use it as a screensaver: <a href='index.php?refresh=10'>On</a> / <a href='index.php'>Off</a>. Tip: make your browser full screen. <br/>";
$output .= "Check out <a href='./opposites.php'>opposites</a>, more random tagness<br /><br />";
$output .= "<b>Who are you?</b><br />";
$output .= "I am <a href='http://www.erikborra.net'>Erik Borra</a>, currently doing research in New Media at the University of Amsterdam, and a freelance consultant and programmer.  <a href='mailto:smile[didlidoo]justlol.net'>Email</a><br /><br />";
$output .= "<b>Legal stuff</b><br />";
$output .= "This site nor the programmer of this site are affiliated with <a href='http://www.flickr.com'>Flickr</a> in any way.  Neither is the programmer of this site affiliated with (almost) any of the persons you will see on the pictures.  This site does follow the terms of use of Flickr and provides a backlink to Flickr from each photo to its photo page on Flickr (just click on the picture).  All pictures belong to their respective owners.  If you want to use a picture follow the link by clicking on the picture and see what kind of copyright the owner uses. <br /><br />";
$output .= "<br /><br />";
$output .= "<center>Smile! You're living in a wonderful world!</center><br />"
$output .= "<br />";
$output .= "</div>";
$output .= "</body>";
$output .= "</html>";

print 
$output;

?>