Web development articles

If you are looking for an alternative to base64_decode() in PHP then this page might help you. To jump straight to the code click here.

You might have come across an issue where your hosting company has disabled the function and as a result you are getting an error about base64_decode() being disabled.

The reason the hosting company has disabled base64_decode() in PHP is that infected websites will have code injected that uses the function. While disabling base64_decode() in PHP will effectively disable the infection, it will unfortunately also break any other code that uses the function legitimately.

So if you cannot convince the hosting company to re-enable the function and you do not wish to move your website to a different host, then you can always use the code below as a drop in replacement to base64_decode() in PHP.

Please note the disclaimer! We do not take any responsibility that the function below works as intended. It worked for us with very limited testing. Also performance is likely to be poor but in our situation performance was never an issue. This is a straight port of the implementation found here.

As always, your feedback and comments are much appreciated!

To see the code click here.

WP Pedigree Builder (Beta)

This is the homepage for the WP Pedigree Builder plugin for WordPress.

The WP Pedigree Builder plugin is a plugin for Wordpress that enables you to create an animal pedigree chart. Perfect for horse breeders, dog breeders, or any other animal breeder. Each animal has their own post within a "Breeds" category. In the breeds posts you can specify birth dates, mother, father, etc. A complete pedigree tree can be displayed on any page with the [pedigree-chart] shortcode.

Download

Click the following link to download the Wordpress Pedigree Builder

Installation

1. Copy the pedgree builder plugin directory to the '/wp-content/plugins/' directory.
2. Activate the plugin through the 'Plugins' menu in WordPress.
3. Create a page and put a [pedigree-chart] shortcode on it; (this is the pedigree chart page).
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Optional
4. Create a page and name it Breeds
5. Create a page and parent that page to the breeds page, put [pedigree-list category='breed'] short code on it; (this is the breed list page)
6. Replace breed with the name of your chosen animal breed. (You can create as many pages for breeds as long as they are parented to the breeds page).
--------------------------------------------------------------------------------------------------------------------------------------------------------------
7. Visit the pedigree builder admin panel and set the link option; (Link to page with pedigree chart)
8. Add a post category with the name "Pedigree"
9. For each pedigree member, add a post specifying the custom fields available at the bottom of edit posts page, making the posts belong to the "Pedigree" and name of breed category.

Note: You can put the post name/title of the animal you wish to be displayed at the root of the pedigree chart as a parameter in the shortcode: [pedigree-chart root='']

Screenshots

1. Pedigree chart as drawn on a page using the short code [pedigree-chart]

alt

2. Here are the custom fields that are added to the posts

alt
Note: Reg no, Order and Allbreed link have been custom added to this custom field

3. Pedigree list entry with links to each individual's blog post page

alt

Tutorial of the Wordpress Family Tree plugin


Family Tree Plugin

The family tree plugin for WordPress allows you to create a family tree layout for either a personal or distant family for use on your website.

Upload using FileZilla

To upload the plug-in, locate the family tree plugin that you have downloaded on the local side of FileZilla. Once located simply click and drag the whole folder to WordPress wp-content/plugins section of the server side.

Once uploaded click on your website and access the configurations of the website.

Activating Family Tree Plugin

Click on the plugins tab on the left of the screen and then click on installed plugins. You should see WP Family Tree in the list, click activate under the plugin to allow it to be used.

Using Family Tree Plugin

Make a new page and in the description add [family-tree] and select publish. Visit the newly created page and you will receive an error message. This is because the plugin is not configured correctly at the moment.

Make a copy of the URL of the page then go back into the website configuration page. This time select settings at the bottom of all the tabs and you should hopefully see WP Family Tree located somewhere at the bottom. In here is all the options that you can use to tweak the plugin to your liking. Paste the URL into the second box down which will link the page to the plugin. Scroll down to the bottom to save.

Before going back to the website click on the post tab and select categorise. In here type the name as 'Family' and select add new category. This will allow you to add people to the tree.

Now you are ready to start creating your family tree.

This image gallery is implemented in PHP and uses javascript and CSS to display the image under the mouse pointer. The benefit of this rollover effect is that the site visitor can vey quickly browse the images in the gallery. The downside is that all images need to be loaded first. You can see a variant of this gallery in action at http://www.orcabags.co.uk/bags.php

Please feel free to use this code in any way you want. If you use it on your website then please let me know. Feedback is always welcome.

The images to display in the gallery are specified in a simple text file that defines the image filename and the caption to use for each image. In this example this file is called gallery1.txt and contains the following lines.

img1.jpg;Image caption 1
img2.jpg;Image caption 2
img3.jpg;Image caption 3
img4.jpg;Image caption 4

The gallery script assumes that you have two folders 'images' and 'thumbs' in the same place as the gallery1.txt file where all the images are stored. The thumbnails should have the same filenames as their full-size equivalents that are stored in the 'images' folder.

In order to implement this gallery on your page you need to insert the javascript block that you get from calling gallery_script() from gallery.php (gallery.php is listed further down on this page). The resulting string should be placed inside the html header.

<?php
// This is done in the page header...
echo gallery_script('galleries/', 'galleries/gallery1.txt');
?>

Then, in the body of the page, you need to call the function gallery() at the place where you want the gallery to be displayed.

<?php
gallery('galleries/', 'galleries/gallery1.txt');
?>

The gallery is implemented in the gallery.php file and needs to be included at the top of your page.

<?php
require_once('galleries/gallery.php');
?>

The following code is the 'gallery.php' script.

<?php
define('GALLERY_TNWIDTH', '80');
define('GALLERY_TNHEIGHT', '80');
define('GALLERY_NUMCOLS', '2');
define('GALLERY_TNBORDER', '1');

function gallery_script($folder, $gallery, $stickyimage=true) {

$str = "<script language=\"JavaScript\" type=\"text/JavaScript\">\n";
$str .= "<!--\n";
$str .= "var preloadedimages   = new Array();\n";
$str .= "var images   = new Array();\n";
$str .= "var captions = new Array();\n\n";
$str .= "var blankimage;\n";
$str .= "blankimage = new Image();\n";
$str .= "blankimage.src = \"/img/blank.gif\";\n";
$num = 0;

if (($handle = @fopen($gallery, "r")) !== false) {
// Parse the "images.txt" file and display gallery...
while (!feof($handle)) {
$line = fgets($handle);
$params = explode(";", $line);
$str .= "images[$num] = \"".trim($params[0])."\";\n";
$str .= "captions[$num] = \"".trim($params[1])."\";\n";
$str .= "preloadedimages[$num] = new Image();\n";
$str .= "preloadedimages[$num].src = \"".$folder."images/".trim($params[0])."\";\n";
$num++;
}
fclose($handle);
}

$str .= "\nfunction showimage(id) { \n";
$str .= "    var str = \"".$folder."images/\"+images[id];\n";
$str .= "    document.getElementById(\"galleryimage\").src = str;\n";
$str .= "    document.getElementById(\"caption\").childNodes[0].nodeValue = captions[id];\n";
$str .= "}\n";
$str .= "\nfunction hideimage(id) { \n";
if (!$stickyimage) {
$str .= "    var str = \"".$folder."images/\"+images[id];\n";
$str .= "    document.getElementById(\"galleryimage\").src = \"/img/blank.gif\";\n"; //str;\n";
$str .= "    document.getElementById(\"caption\").childNodes[0].nodeValue = \"\";\n"; //captions[id];\n";
}
$str .= "}\n";
$str .= "-->\n";
$str .= "</script>\n";

return $str;
}

function gallery($folder, $gallery, $stickyimage=true) {

echo '<div style="border:0px dashed blue;padding:0px;margin-top:10px;height:1000px;position:relative;">';
$num = 0; $rows = 0;
if (($handle = @fopen($gallery, "r")) !== false) {
// Parse the "images.txt" file and display gallery...
while (!feof($handle)) {
$line = fgets($handle);
$params = explode(";", $line);
if ($num%GALLERY_NUMCOLS == 0) { /*echo "<br>";*/ $rows++; }
// draw thumbnail...
echo "<a style=\"padding:4px\" href=\"javascript:void(0);\" onmouseover=\"showimage($num);\" onmouseout=\"hideimage($num);\"><img src=\"".$folder."thumbs/".$params[0]."\" width=\"".GALLERY_TNWIDTH."\" height=\"".GALLERY_TNHEIGHT."\" style=\"padding:0px;border:solid ".GALLERY_TNBORDER."px black\"></a>";
if ($num == 0) {
$firstimage = $params[0];
$firstcaption = $params[1];
}
$num++;
}
fclose($handle);
}

$top = 95;
$left = 4;
if (!$stickyimage) {
echo '<br><br><br><br><div style="position:absolute;top:'.$top.'px;left:'.$left.'px;"><img id="galleryimage" border="0" src="/img/blank.gif">';
echo '<br><span id="caption"></span></div>';
} else {
echo '<br><br><br><br><div style="position:absolute;top:'.$top.'px;left:'.$left.'px;"><img id="galleryimage" border="0" src="'.$folder.'images/'.$firstimage.'">';
echo '<br><span id="caption">'.$firstcaption.'</span></div>';
}
echo '</div>';
}
?>

If you are running a Debian Linux webserver you may want to create users that can upload and download files from specific directories via SFTP. This post will explain how to set this up. First you need to create a user. If you want to create a user with the name 'webdev' then login as root and use the following commands at the prompt.

useradd webdev

Now you want to set the password for the new user 'webdev'

passwd webdev

Now we can create a home directory with the following commands.

mkdir /home/webdev
chown webdev:users /home/webdev

Now we only want this user to be able to login to the server via SFTP. We can do this by changing the users shell to sftp-server.

usermod -s /usr/lib/sftp-server webdev

Next we need to make the sftp-server a valid shell by adding the line "/usr/lib/stfp-server" at the end of the file shells in the etc directory.

By default, a new user will be assigned to a new group with the same name as the new user. So in our case the user 'webdev' will belong to the group 'webdev'. In order to make our new user the owner of any other directory including sub-directories (such as the website document root) we can execute the following command.

chown -R webdev:webdev /var/www

So now the new user webdev will be able to login va SFTP and have owner permissions to their home directory as well the webserver document root directory.

References: The following page has more useful information about managing groups and users in Linux.

Further Restrictions

In the example above we made the directory that holds the website belong to the group "webdev". If you have other users on the system that you do not want to have access to the website data then it could be wise to assign the www directory to the group "www-data" and remove read access for other system users to the directory www.

This is in order to limit access from users except the web server that runs under the user www-data.

For example:

usermod -a -G www-data webdev
usermod -g www-data webdev
chown -R :www-data /var/www
chmod -R o-r /var/www

By doing the above, we have added the user webdev to the group www-data (the same user that Apache normally runs as). Then we make www-data the default group for the user webdev, whereafter we make the www directory belong to the www-data group and set the permissions such that the files in that directory are group-readable but not world-readable.