Archive

Posts Tagged ‘ubuntu’

PHP Script To Select A Person To Make The Tea

April 6th, 2009 1 comment

In any office there can be arguments about who will make the next round of tea. The following script will allow you to randomly pick a person who is going to make the tea. Rather than have a script that did this once and threw away the information I thought it would be a good idea to use cookies to save the form data for the next time you want to pick a person to make the tea. This is a good exercise if you are trying to understand how cookies work.

First, we will need to variables, the first is an array of people and the second is the number of people in the office.

$people = array();
$number = 10;

We can now build the form that will contain all of our names. Here we just cycle through a simple for loop and if an array item exists in the $people array for the value of $i then we use this in our form.

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
for ( $i=0 ; $i<$number ; $i++ ) {
    if ( isset($people[$i]) ) {
        echo '<input type="text" name="people[]" value="'.$people[$i].'" /><br />';        
    } else {
        echo '<input type="text" name="people[]" value="" /><br />';    
    }
}
?>
<input type="submit" value="Save &amp; Run" name="submit" />
</form>

Next we need to grab the variables coming from the form and store them in a array, because we used a name with square brackets we can access the people GET variable as if it were an array.

if ( isset($_GET['people']) ) {
  $people = $_GET['people'];
}

We can now select who will be making the tea. Here I use a combination of array_filter() to remove all blank entries in the array and array_rand() to randomly select a key from the array. The array_rand() function returns NULL if it is given a blank array so we need to account for people first visiting the page.

if ( $who = array_rand(array_filter($people)) ) {
    $teaMaker = $people[$who];
    echo '<p>'.$teaMaker.' will be making the tea!</p>';
}

Enabling cookies with this script is quite easy and involves only a small amount of code addition. First we need to create a cookie if the people variable exists. Because this is an array and the setcookie() function requires a string we need to use the serialize() function to convert the $people array into a string value.

if ( isset($_GET['people']) ) {
    $people = $_GET['people'];
    setcookie("teaCookie", serialize($people));
}

Now that we have created our cookie we need to retrieve it again when the page reloads. All cookies are kept by reference in the $_COOKIE superglobal array. Once we are sure that the cookie exists we can user unserialize() to convert our serialized string into an array again.

if ( isset($_COOKIE['teaCookie']) ) {
    $people = unserialize($_COOKIE["teaCookie"]);
}

We now have the $people array in its original state.

Finally, rather than continuously setting and unsetting the cookie I have added a link that will allow you to pick another person from the list, without running the form. We first need to make sure that our cookie exists before allowing this link as it will do nothing.

<?php if ( isset($_COOKIE['teaCookie']) ) { ?>
<p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Pick Again!</a></p>
<?php }?>

Of course this script isn’t all that useful if you either don’t drink tea or work alone, but it can easily be adapted to other functions. You can host this script on your Intranet or website for internal usage if you wish, but I have uploaded a version of the tea picker for you to play with, along with the source code.

Installing SVN With Web Access Through Apache On Ubuntu

April 1st, 2009 2 comments

Getting started with SVN on Ubuntu takes only a few minutes, and enabling web access to the repository is also very straightforward.

First (in order to actually serve the files) you need to install Apache, open up a terminal window and run the following command. This will ensure that Apache is installed if you unselected it for some reason during the install.

sudo apt-get install apache2

Note the use of the sudo command. This will run the command you give it as a super user as normal users will not generally have access to install software like this. When you use sudo you will be prompted for the super user password. Next, use the following command to install SVN.

sudo apt-get install subversion libapache2-svn

You can now create your subversion repository, it is best to keep all of our repositories under the same directory so that things don’t get confusing in the long run. To create the SVN directory run the following.

mkdir svn

The following will create the repository in the directory /svn/myproject.

sudo svnadmin create /svn/myproject

Now we will need to edit some of the settings in our Apache SVN module. Use the following command to edit the correct file.

sudo gedit /etc/apache2/mods-enabled/dav_svn.conf

When you first open this file it is all commented out. You first need to uncomment the Location. This is the directory where subversion will be available from on the Apache side. Here I am setting this to svn so that when I navigate to http://127.0.0.1/svn I will see the correct output.

<Location /svn>

Note that you must also uncomment the closing location tag at the bottom of the document!

Uncomment the DAV line to enable the repository, this enables the DAV module.

DAV svn

If you have a single repository then then enable the SVNPath setting. However, if you have multiple repositories then enable the SVNParentPath setting. this will point to the main directory where your repository (or repositories) is stored.

SVNParentPath /svn

That’s about it really, but as a further step lets enable some sort of authentication. You will need to uncomment the following lines (which are grouped together).

AuthType Basic
AuthName "Subversion Repository"
AuthUserFile /etc/apache2/dav_svn.passwd

And the following line.

Require valid-user

Now, before we restart our Apache server and enable everything you have done we need to setup a username and password for the authentication to work. Use the following command to create a user and assign a password.

sudo htpasswd2 -cm /etc/apache2/dav_svn.passwd user

If this doesn’t work then try using htpasswd instead of htpasswd2. You should be prompted to enter your password twice and then receive a message telling you that the user has been created. Note that you should only use the -c option when you create a user for the first time because it creates the password file even if it is already there. If you use this option again in the future you will destroy any other users you have created. Instead, simply drop the -c and use -m on its own which causes the password to be MD5 encrypted, but will not recreate the file.

You can now restart the server by entering the following command.

sudo /etc/init.d/apache2 restart

You might initially try a short cut and do something like the following:

sudo apache2 restart

However, this will not work as the script in /etc/init.d/apache2 explicitly reads some environment variables that are important when starting the server.

You can now go to your subversion directory and see the following:

SVN Web Access

SVN Web Access

You will also see an authentication window appear, just use the username and password you set up before.

You now have a fully working subversion server with web access.

Categories: Apache, Unix/Linux Tags: , , , , , ,

Virtualization With VirtualBox

March 25th, 2009 2 comments

Virtualization is basically a term used to describe the creation of a computer in software. The main benefits of which are that if you want to try out an operating system or test client server communications you don’t have to get multiple computers. You can simply create a few computers virtually, which will act just like the real thing.

There are quite a few virtualization products available, some are free and some cost quite a bit of money. After messing about with quite a few different virtualisation products other the past few weeks I have uncovered a great bit of software called VirtualBox from Sun Microsystems.

VirtualBox

After installing VirtualBox it took me only a few minutes to set up a new virtual machine. I decided that I wanted to create a virtual machine with Ubuntu installed on it, so I selected the options that optimise VirtualBox for this system. Note that this doesn’t set up a virtual machine with Ubuntu already installed, you have to go and download it yourself! Here is a screenshot of the main interface, which is very easy to understand.

VirtualBox interface

VirtualBox interface

Using virtual machines in the past I have found that to install anything can take a very long time. Installing Ubuntu took not more than 30 minutes, at which point I was presented with the bootup screen.

VirtualBox Ubuntu Boot

VirtualBox Ubuntu Boot

Here is another shot of the operating system fully loaded.

VirtualBox Ubuntu Loaded

VirtualBox Ubuntu Loaded

VirtualBox also supports the creation of snapshots, so if you have a fresh install of an operating system that you want to mess about with (or break) then you can create a snapshot of the virtual machine before you begin. This way, after you trash the system, you can revert back to a snapshot of the system before you started messing about.

The only trouble I had was figuring out how to interface the virtual machine properly with my network, but after reading the documentation this all became very clear.

Overall VirtualBox is a great bit of software, with a detailed and comprehensive manual. I just can’t believe that this software is free!