<?
/*

lj.php
yet another LiveJournal image getter thingy

by Ryland Sanders
Friday, November 24, 2006

This source code is released into the public domain, copy it and modify it as you
see fit. A link and credit would be nice, though.

This script gets the "latest images" XML stream from LiveJournal, parses it,
filters it, and displays the resulting list as clickable links, limited to
the number of images specified by the $limit parameter in the query string.

*/
?><html>
<head>
<title>yet another LiveJournal image getter thingy</title>
<style type="text/css">
BODY { font-family: sans-serif; }
P { font-size: 70%; }
H1 { margin: 0; }
</style>
</head>
<body><a name="topofpage"></a>
<h1>LiveJournal images</h2>
<p><a href="http://www.livejournal.com/stats/latest-img.bml">LiveJournal images XML feed</a> :: <a href="lj.phps">PHP source code for this page</a></p>

<?php

/*
put script file name of this page in a variable (just in case you change
the filename but forget to update the script with the new name)
*/
$thispage getenv("SCRIPT_NAME");

/*
get the 'limit' parameter from the query string. if the server is configured with
register_globals turned on, it does this for you automatically, but I like to
add zero to numerical values from the query string to make sure it's a number.
if there's no 'limit' paramter in the query string, set it
to the default, 100 images.
*/
$limit $_GET['limit'] + 0;
if (!
$limit) { $limit 100; }

?>

<p>Number of images: <? echo $limit?> - Limit to: <a href="<? echo $thispage?>?limit=25">25</a> :: <a href="<? echo $thispage?>?limit=50">50</a> :: <a href="<? echo $thispage?>?limit=100">100</a> :: <a href="<? echo $thispage?>?limit=250">250</a></p>

<p>&nbsp;</p>

<?

/*
this is a callback function used by array_filter() below to filter our
any elements that aren't tagged as 'RECENT-IMAGE' in LJ's xml stream
*/
function is_img($recent_image) {
    return 
$recent_image['tag'] == 'RECENT-IMAGE';
}

/*
open the stream and assign it to a file handle variable
*/
$handle fopen("http://www.livejournal.com/stats/latest-img.bml""r");

/*
dump the stream contents into a string variable
*/
$ljxml "";
while (!
feof($handle)) {
    
$ljxml .= fgets($handle);
}

/*
close the stream
*/
fclose($handle);

/*
create an XML parser object
*/
$p xml_parser_create();

/*
parse the stream data into an array $vals
*/
xml_parse_into_struct($p$ljxml$vals);

/*
free the parser object, since we're done with it
*/
xml_parser_free($p);

/*
filter the $vals array using the callback function above
*/
$imgs array_filter($vals"is_img");

/*
get a subset of the array limited to the number of images
in the $limit variable
*/
if ($limit 250) { $imgs array_slice($imgs0$limit); }

/*
loop through the array and print each array element as a clickable link
*/
$num_imgs sizeof($imgs);
$ct 1;
foreach (
$imgs as $recent_image) {
    echo 
"<p><a name=\"img$ct\"></a><a target=\"_blank\" href=\"" $recent_image['attributes']['URL'] . "\">LJ post URL</a> ::\n";
    echo 
"<a target=\"_blank\" href=\"" $recent_image['attributes']['IMG'] . "\">Image URL</a> ::\n";
    if (
$ct 1) {
        echo 
"<a href=\"#img" . ($ct 1) . "\">Previous image</a> ::\n";
    } else {
        echo 
"Previous image ::\n";
    }
    if (
$ct $num_imgs) {
        echo 
"<a href=\"#img" . ($ct 1) . "\">Next image</a> ::\n";
    } else {
        echo 
"Next image ::\n";
    }
    echo 
"<a href=\"#topofpage\">Top of page</a>\n";
    echo 
"<br>\n<img src=\"" $recent_image['attributes']['IMG'] . "\"></p>\n\n";
    
$ct++;
}

?>

</body>
</html>