20.8. How to display images in PHP-Nuke blocks

Blocks offer a quick way to incorporate content in PHP-Nuke. They are happily used to display images, since they can be placed anywhere in the three layout columns - and their position can be comfortably changed from the administration panel. A very common use of blocks in PHP-Nuke is to display dynamic images, i.e. images that change depending on some parameter of dynamic nature, most offen time: a daily quote, a daily weather forecast or a daily comic.

Most of the time you will not be the original author of the quote, forecast or comic. You will want to fetch this content from some popular source on the Internet.

Important Before you proceed: think on copyright!
 

We will describe the tecnology that allows you to use somebody else's work to make your site more beautiful, attractive or informative. This almost certainly touches that person's copyright. Please take the time to read the admonition on copyright in Section 8.2.3!

PHP enables you to read a remote URL, store its whole content in one (!) variable, then search the contents of the variable for any regular expression (for regular expressions, see Section 25.3). We will use this technique to find out the filename of the User Friendly daily comic strip, in order to display it in a PHP-Nuke block. This UserFriendly block reads the User Friendly daily cartoon page line by line, searches for the pattern

whatever<IMG ALT="Latest Strip"whateverSRC="imagefile">whatever

extracts the location of the image file (imagefile) from it and displays it in the block. The PHP code of the User Friendly block is a classic example of how you can do this "scrapping" in PHP (-Nuke):

<?php
if (eregi("block-User_Friendly.php",$PHP_SELF)) { (1)
    Header("Location: index.php");
    die();
}
$ufurl =  "http://www.userfriendly.org/static/"; (2)
$content = "<i>ufurl:</i> $ufurl<br>";
$imagefile = ""; (3)
if (!($textfile = fopen( "$ufurl",  "r"))) { (4)
    $content .= "$ufurl<br> could not be opened<br>"; (5)
    die();
}
while (!feof($textfile)) { (6)
    $line = fgets($textfile, 1500);          (7)
    if (eregi( ".*<IMG ALT=\"Latest Strip\".*SRC=\"(.*)\">.*", $line, $out)) { (8)
        $imagefile = $out[1]; (9)
        break;
    }
}
fclose($textfile);
$content = "<p align=center>";
$content .= "<A HREF=\"http://www.userfriendly.org\" target=\"_blank\">
<IMG SRC=\"$imagefile\" alt=\"User Friendly by Illiad\"></A>"; (10)
?>
(1)
This is the standard check at the start of every PHP-Nuke block: if the name of the script, as it was called by the user, contains the full name of the block, then the user must have called the block directly. In this case we redirect him to the home page. As with modules, a direct call of a block not only will not work, it will also bypass all security measures of PHP-Nuke (see for example Section 23.4.3).
(2)
The URL of the remote page containing the dynamic image.
(3)
Initialization of the image filename.
(4)
We open the remote page for reading ("r"). This operation returns a "file handle", a unique identifier for the opened file, which is stored in $textfile.
(5)
If the opening operation failed, we output an error message in the content variable of the block and quit.
(6)
If the opening operation succeeded, we start reading the content of the remote page in a loop, line-by-line, until the end-of-file (eof) character is encountered.
(7)
We only read 1500 characters at a time. This is more than enough for our purposes.
(8)
This is where all the magic takes place! We search the line we just read for occurences of a regular expression. The expression itself means
whatever<IMG ALT="Latest Strip"whateverSRC="imagefile">whatever

where "whatever" may be any content, including space. The regular expression contains a subexpression in the place of the image filename. This results in the file name being stored in the $out array, as its first element.

(9)
We copy the first element the $out array to the $imagefile variable. By construction of the regular expression above, $imagefile now contains the image filename of the daily comic strip. We only have to display it!
(10)
We echo an image anchor using as source the filename we just "scrapped" from our source page. This displays the daily comic strip without fetching the image to our web server (hot linking!).

A similar technique is used in news aggregators, like the MyHeadlines module (see Section 8.3.9), that "scrap" news sources for pieces of dynamic information that are not otherwise available (i.e. not available as RSS/RDF news feeds), see Template Based Scraping. For each source page, a regular expression is stored in a table. Its subexpressions will automatically store the dynamic information pieces that are of interest to us in the elements of some $out array. We would then only have to display those elements for each source.

Tip Quod medicina aliis, aliis est acre venenum[1]
 

If you are an artist whose images are being hot-linked with the above methods without your permission, you are certainly not excited. But don't despair! You can defend your work with a simple technique that requires only the Apache mod_rewrite module and a few lines in your .htaccess file. See Section 25.6.

Notes

[1]

One person's medicine is another's foul poison.


PHP-Nuke Documentation module made by Chris Karakas