Thursday, April 30, 2009

osCommerce & Register Globals

What happens when your web host finally get's around to upgrading to php5 and MySQL 5? Here is what happened to me in order:

1. Rejoice because finally your website is hosted according to industry standards.
2. Panic because you are unprepared.
3. Worked through the night to get the website updated.

In order to migrate an osCommerce website from php4 to php5 there is really only one thing you need to worry about and that's a little "feature" in php called, Register Globals.

Register Globals makes every variable contained in the URL, POST, SESSION and ENVIRONMENT directly available in every script. What this means is that with register globals turned on, you can write a webform that posts a variable to a script and that script can refer to the variable in the POST by name. For example:


<?php
/*
** Example script to show what happens when register globals is turned on.
**
** A form will submit POST data to this website as follows:
** String $_POST['name'];
** Int $_POST['age'];
*/
echo "<h2>Thank you for telling me your name and age.</h2>";
echo "With register globals turned on we can do this:<br/>";
echo "Your name is: " . $name . "</br>";
echo "You are " . $age . " years old.";
?>


Now with register globals turned off, you need to do it this way.

<?php
/*
** Example script to show what happens when register globals is turned on.
**
** A form will submit POST data to this website as follows:
** String $_POST['name'];
** Int $_POST['age'];
*/
echo "<h2>Thank you for telling me your name and age.</h2>";
echo "With register globals turned on we can do this:<br/>";
echo "Your name is: " . $_POST['name'] . "</br>";
echo "You are " . $_POST['age'] . " years old.";
?>


PHP does have an neat function called, "extract" which will take all the variables in and associative array. It works like this:

<?php
/*
** As before we have a post that looks like this:
** String $_POST['name'];
** Int $_POST['age'];
**
** We want to create variables out of the $_POST array keys and make the values the values
** if you catch my drift.
**
*/
echo "<h2>Thank you for telling me your name and age.</h2>";
echo "With register globals turned off we can do this:<br/>";
if( isset ( $_POST ) && !empty( $_POST ) ) {
extract($_POST, EXTR_SKIP);
echo "Your name is: " . $name . "</br>";
echo "You are " . $age . " years old.";
} else {
echo "You did not post any data.";
}
?>


So you can see where I am going with this. The first thing I needed to do was to make sure that all the "registered global" variables were first caught and made available in the above way.

osCommerce Session handling
It quickly became apparent that I was not going to get anywhere until I had the sessions stuff sorted. At this stage I gave up working this out for myself and searched for help. I found an excellent resource called: Magic SEO URL for osCommerce (osCMax)

There were a couple of other pitfalls:

1. root/admin/includes/classes/upload.php has a line that sets "$this = null;" PHP 5 objects to this ( excuse the pun ) so the correction is fairly simple. Just comment it out. I did play with unset( $this ) but found that to cause some other difficulties.

2. The variable $PHP_SELF is referred to many times in osCommerce code. As this is a registered global it will not be available to your code after the upgrade. You must set this variable in application_top.php like this:

$PHP_SELF = $_SERVER['PHP_SELF'];


Other things that did not happen to me but might happen to you.
MySQL upgrade breaks all your queries. This is quite a common problem that I have encountered before but not recently. MySQL 5 requires the correct bracketing of clauses in the from statement of a query. Especially with regards to left joins. Search for this in the contributions section of osCommerce.

References:
http://www.magic-seo-url.com/oscommerce/tips/running-oscommerce-with-register-globals-off.html
http://www.oscommerce.com/community/contributions,2097/category,all/search,globals
http://www.oscommerce.com/community/contributions,4654

Sunday, April 26, 2009

Recycling old computers

I have an old PIII at home with 256Mb Ram and a 20Gb hard disk. Graphics is nothing to write home about and it will not support Vista or KDE 4.2.

I know there are many, many blog posts on the internet that discuss this so I will simply add to the general wash.

Ubuntu Server:
  • File server with samba
  • Apache webserver with dyndns.com ( free dynamic IP to Domain Name resolution ) - This can be useful if you are a freelance website designer / programmer and you need to publish draft websites for your clients prior to going live.
Xbuntu Desktop:
  • Install Childsplay - a very basic suite of games that are designed to develop mouse and keyboard familiarisation in a fun and easy to understand environment. - In fedora this was just as simple as, "yum install childsplay"
  • Install the Mame Emulator and download roms for some of those neat and safe for kids arcade games that you played in the 80's at your local cornershop or arcade.
  • If you have an XBOX controller - install the drivers for the XBOX and purchase an adaptor for the controller to plug into your USB. Then those mame arcade games start to look more appealing.
Windows XP ( If you have it ):
  • Childsplay is available for both Windows and Linux so get the windows version up and running.
  • Get Mame32 installed and the XBox controller drivers. My XBOX Gamepad to USB adapter came with a disk with drivers to install for XP.
  • Head down to noel leeming and buy some kids games. They don't need anything more than a 75mhz PC and 64mb ram. They are still fun to play and will keep your kids occupied.
  • Windows XP can also be used for accounting software if you have a small business - Cashbook Complete for example.

I am sure there are folks around who have loads of other suggestions but these are the ones I have tried / thought about.

I know this has always been a linux related blog, and this small bit on Windows does seem out of charicter. The reality of it is that even though I use linux exclusively at home, my family do not...

Friday, April 10, 2009

Write .NET applications on Mono and use them in Windows

I am no professional .NET developer but I thought I would have a crack at a simple application written on my Fedora 10 in the Monodevelop IDE and then run the same exe it compiles in a Windows XP. It took a while but I got there in the end and in retrospect it was not too hard. Just a few dependancies, a short forum-search for a solution to a small problem.

Here is what I did:

1. Install mono, monodevelop and gtk-sharp ( GTK# ):
The monodevelop IDE provides a gui designer for gtk#. So I thought I would try this out and see how I could get that working in windows too. In Fedora I just ran the following:

# yum install monodevelop gtk-sharp*

As usual I let yum decide dependancies for me.

2. Start monodevelop and create a new solution:

Select the C# templateand then pick Gtk# 2.0 Project
Under Solution name: type "tutorial" and under Name: type "myGuiApp" to create a Gtk# GUI application under the folder {$HOME}/Projects/tutorial.

Click Forward and select your Target GTK environment and click Finish.


There will be two files in your solution: Main.cs and MainWindow.cs. You will only need to work on MainWindow.cs

3. Design your application in Monodevelop:

This will be a very simple application. Only a little more advanced than Hello, World.
Double click on MainWindow.cs to start editing the Window. Monodevelop has already provided you with a basic GUI shell to work with.
Beneath the source code window on MainWindow.cs you will see two buttons: Source Code and Designer. Click the Designer button to start dragging and dropping your tools onto your palette.

Seeing as though you are using Gtk# to create this GUI there is something that you should know before continuing. It requires the use of containers to handle the positioning of widgets. For the purposes of this tutorial I will just use the Fixed Container.

Drag a Fixed container onto your pallet first. Then drag the following widgets onto the pallet. Position them something like the next screen shot.

3 Labels
2 buttons
1 entry box


Resize your pallet to suit.


Above the designer pallet you will see a dropdown list that can be used to select each element on your pallet starting with the window itself. Select each of these and then adjust their properties as below:

MainWindow: Window Properties: Window Title = "My Gui App"
label1: Label Properties: Label = "Welcome to My GUI APP"
label2: Label Properties: Label = "Your name please:"
label3: Label Properties: Label = "Hi there, "
button1: Button Properties: Label = "Greet!"
button2: Button Properties: Label = "Close"


You may have to rearrange things a bit to get everything fitting on your screen. Now it should look something like this:


4. Add signal handlers to the buttons

Click button1 ( now labelled, "Greet!" ) and under the properties pallet select the Signals tab. Next under the Button Signals double click on the Clicked signal. Monodevelop will automatically create a handler called OnButton1Clicked for you.

Click button2 ( now labelled, "Close" ) and do the same as you did for button1.

5. Time to start editing some code:

Click the Source Code button to view the source code for MainWindow.cs. You will find two new functions called:

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
{
}

protected virtual void OnButton2Clicked (object sender, System.EventArgs e)
{
}


Edit them to look like this:

protected virtual void OnButton1Clicked (object sender, System.EventArgs e)
{
label3.Text = label3.Text + entry1.Text;
}

protected virtual void OnButton2Clicked (object sender, System.EventArgs e)
{
Application.Quit();
}


6. Compile:

Click the build button and try your application out. It should compile without any difficulties at all. Should being the operative word. If your app did not compile do not proceed. Get it working first!

7. Getting Windows XP ready:

Getting Windows ready was as simple as installing the Mono package for windows. It includes a command prompt mode. Also you can create shortcuts to your mono apps that execute the mono launcher to launch your code. A bit like the java stuff.

I have yet to get it to work effectively on both linux and windows without having to first install the mono environment on both. I had thought that mono applications written on Linux could be migrated over to Windows without the mono libraries present.

I have yet to test how a windows app written for .NET v2 will port over to Linux. Again I think that without winforms it will not work. It might work for a command prompt application. But that's about it.

Anway here it is working just fine in my qemu windows xp vm.

Thursday, April 9, 2009

Blogger Templates

I have changed the template only slightly. It seems that this default denim template also has a "wide" option. The colours and widgets remain the same. The only difference is that source code can now be read more easily as the layout will stretch with the browser.

Wednesday, April 8, 2009

CSS Floating Box Model

A simple floating box model example showing how to "float" div elements within another div element. This example includes boxes that float left and boxes that float right. It is easy to imagine how one could beef up the aesthetic quality of these floating boxes with rounded corners or varied background colours or patterns. The boxes used here are for demonstration purposes only.


<html>
<head>
<title>Floating Right and Wrapping</title>
<style>
#maincontent {
display:block;
width: 600px;
margin: 0px auto;
}

.wrapcontainer {
float:left;
width:600px;
padding: 5px;
border: 1px solid #777777;
margin: 5px;
display: block;
font-family: arial,helvetica,sans-serif;
}

.sidebar-right {
float: right;
border: 1px solid #777777;
padding: 5px;
margin: 5px;
display: block;
width: 210px;
font-family: arial,helvetica,sans-serif;
}

.sidebar-left {
float: left;
border: 1px solid #777777;
padding: 5px;
margin: 5px;
display: block;
width: 210px;
font-family: arial,helvetica,sans-serif;
}

h3 {
font-family: arial,helvetica,sans-serif;
color: #333333;
}

p {
font-family: arial,helvetica,sans-serif;
color: #333333;
}

</style>
</head>
<body>
<div id='maincontent'>
<h3>Wrap text around object floating right.</h3>
<p style='width:600px; display:block;'>
Below you can see three block level elements. They are #maincontent on the outside
#wrapcontainer which inherits it's width from #maincontent and floats to the left
of #maincontent. The last block level element you can see is called .sidebar-left|right. It
floats to the right of #maincontent.
</p>
<div class='wrapcontainer'>
<div class='sidebar-right'>
This text will appear inside the sidebar and will be constrained by the
width of the sidebar.
This text will appear inside the sidebar and will be constrained by the
width of the sidebar.
This text will appear inside the sidebar and will be constrained by the
width of the sidebar.
</div>
Te, vel dolor, dignissim volutpat, exerci wisi laoreet euismod et enim nulla. Vel ut eum et lobortis nisl consectetuer luptatum ad blandit, veniam dolore consequat veniam. In dolor iriure eros in odio ex, ea hendrerit, molestie, et zzril. Et facilisis, eu tation tation iriure minim et velit praesent dolore autem esse dolore dignissim, dolore blandit duis ex nisl praesent in exerci in nibh.
Facilisi veniam feugait dignissim zzril ea esse nostrud dolore veniam sit iriure te tincidunt feugait sit ut ipsum ullamcorper, facilisi minim qui, et facilisis vulputate. Ex illum consequat vulputate et velit dolore vel, consequat dolor laoreet dolore autem aliquip, suscipit, facilisi tation ullamcorper praesent minim, te eros dignissim consectetuer. Blandit, ut hendrerit exerci vel odio, iriure, lobortis vel eros enim, eu tation laoreet volutpat at aliquam. Wisi facilisis autem consectetuer vel, duis, accumsan delenit augue, aliquip qui vero hendrerit molestie euismod.
<div class='sidebar-right'>
And another<br/>
This text will appear inside the sidebar and will be constrained by the
width of the sidebar.
</div>
Feugait veniam ullamcorper iriure diam in duis zzril, in in, enim accumsan ut, iusto elit dolore feugiat odio luptatum ad tincidunt adipiscing, nulla. Commodo vulputate velit et, dolor minim blandit nonummy ex at blandit augue nostrud magna nonummy facilisis eu consequat. Lobortis odio in et ullamcorper molestie consequat wisi esse feugait dignissim feugiat ut et at, nisl nisl in, molestie, qui. Ad dolore, lorem minim vel consequat luptatum vero illum odio et ex augue. Vulputate euismod zzril commodo nulla amet exerci feugiat eum duis, autem nulla nostrud minim, te te, illum dolore nisl feugait consequat elit blandit. Hendrerit hendrerit ex dolor commodo, suscipit exerci ipsum ipsum dolore, volutpat.
Wisi volutpat at aliquam, hendrerit facilisis autem consectetuer vel, duis. Suscipit delenit augue, aliquip qui vero hendrerit molestie euismod velit, feugait nibh zzril ea delenit molestie dolore veniam quis, odio ut delenit facilisis nostrud iriure. Feugiat illum iriure in velit veniam sit tation commodo nulla facilisi consequat veniam nibh facilisis eu. At adipiscing eu lorem ipsum consequat aliquip vero blandit praesent.
<div class='sidebar-left'>
And another<br/>
This text will appear inside the sidebar and will be constrained by the
width of the sidebar.
</div>
Et in et ullamcorper molestie consequat wisi esse feugait dignissim feugiat ut et at, nisl nisl in. Nisl, qui, ut dolore, lorem minim vel consequat luptatum vero illum odio et ex augue esse euismod zzril commodo nulla amet exerci feugiat. Dolore duis, autem nulla nostrud minim, te te, illum dolore nisl feugait consequat elit. Ut erat hendrerit ex dolor commodo, suscipit exerci ipsum ipsum dolore, volutpat ea, wisi eum vulputate zzril.
Feugait veniam ullamcorper iriure diam in duis zzril, in in, enim accumsan ut, iusto elit dolore feugiat odio luptatum ad tincidunt adipiscing, nulla. Commodo vulputate velit et, dolor minim blandit nonummy ex at blandit augue nostrud magna nonummy facilisis eu consequat. Lobortis odio in et ullamcorper molestie consequat wisi esse feugait dignissim feugiat ut et at, nisl nisl in, molestie, qui. Ad dolore, lorem minim vel consequat luptatum vero illum odio et ex augue. Vulputate euismod zzril commodo nulla amet exerci feugiat eum duis, autem nulla nostrud minim, te te, illum dolore nisl feugait consequat elit blandit. Hendrerit hendrerit ex dolor commodo, suscipit exerci ipsum ipsum dolore, volutpat.
Wisi volutpat at aliquam, hendrerit facilisis autem consectetuer vel, duis. Suscipit delenit augue, aliquip qui vero hendrerit molestie euismod velit, feugait nibh zzril ea delenit molestie dolore veniam quis, odio ut delenit facilisis nostrud iriure. Feugiat illum iriure in velit veniam sit tation commodo nulla facilisi consequat veniam nibh facilisis eu. At adipiscing eu lorem ipsum consequat aliquip vero blandit praesent.
</div>
</div>
</body>
</html>