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.
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):
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.
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.
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. |
We now move to a more advanced example of a PHP-Nuke block: a block that displays random images. Suppose you have a collection of images in a directory of your webserver and would like to display a random image each time in a PHP-Nuke block. Using PHP-Nuke's own positioning capabilities for blocks (in the administration panel, from the “Blocks” link, see Section 7.1), you can achieve an almost arbitrary positioning of random images in your page - a functionality that could be used to display whatever visual content you like, ranging from a “daily babe” to a random skyscraper banner.
To create a random image block for PHP-Nuke, proceed as follows (see Random Picture Block):
Make a script called block-RandomPics.php and put it in the blocks folder. The code for block-RandomPics.php is:
<?php $content=""; if (eregi("block-RandomPics.php",$PHP_SELF)) { Header("Location: index.php"); die(); } // $whereimgs has to be in the Domain (like localhost area) // or put ../ in front of folder name $whereimgs = "../pictures"; mt_srand((double)microtime()*1000000); $imgs = dir($whereimgs); while ($file = $imgs->read()) { if (eregi("gif", $file) || eregi("jpg", $file)) { $imglist .= "$file "; } } closedir($imgs->handle); $imglist = explode(" ", $imglist); $a = sizeof($imglist)-2; $random = mt_rand(0, $a); $image = $imglist[$random]; $asin = explode(".", $image); $content = ""; $content .= "<center><a href=\"PicShow.php?show=".$whereimgs."/$image\">"; $content .= "<img src=\"".$whereimgs."/$image\" border=\"0\" width=\"120\" alt=\"\"><br><br>"; $content .= "Click for full picture.</a><BR></center>"; ?> |
The block-RandomPics.php block reads all .gif's and .jpg's from the $whereimgs directory into an array ($imglist), computes a random index between 0 and the maximum array index ($a) and uses the random index to select an image name from the array. It then writes some HTML code to display a thumbnail of 120 pixels width with a ling to the original picture. The link itself passes the show parameters on the URL as the concatenation of the $whereimgs directory and the random image name ($image) to a script we still have to write, PicShow.php.
The right images path | |
---|---|
Taking into account that block-RandomPics.php has to be located in the blocks folder, then, if your images folder is the folder “pictures” under the “PHP-Nuke root” directory (i.e. under the same directory where mainfile.php, index.php, config.php etc. are located), then you have to prepend a “../” to “pictures”, as we did in the code, assuming that you don't want to give an absolute, fully qualified URL there. |
For PicShow.php, which is the script that will show the full image, we have to write code that behaves just like a standard PHP-Nuke module (but without any administration functions), so you can see it as an example of a minimal module:
<?php if (!isset($mainfile)) { include("mainfile.php"); } $index = 0; include("header.php"); OpenTable(); $sl = $show; echo " <center><h3><a href=\"javascript:history.back()\">Go back!<br><br> <img src=$sl border=\"2\"></a></h3> </center>"; closetable(); include("footer.php"); ?> |
[1] | One person's medicine is another's foul poison. |