Category: Coding


Ok well I’ve not posted on here for a while, mainly due to the fact that life has been so hecktic and I’ve been getting myself far too involved in too many different things..

So thought I’d better update you all with some info etc.

Well the last week I’ve managed to do a whole load more coding to the main framework of ZPanelX, I’ve now implemented the modular hook system, this enables module developers to ‘hook’ into core parts of the framework and execute their own code from inside their module/controller.ext.php file.

As well as the hook system, the daemon is now fully complete and I’ve spent the last few days implementing XMWS which stands fro ZPanelX Modular Web Services. XMWS is an XML based web service layer for ZPanelX which enables module developers to rapidly develop web service components within their module.

Luckily I have about 8 days off work in total over Christmas and the new year so I managed to get a lot of coding on ZPanelX done and dusted :) – Hopefully not much will come up between now and spring and ZPanel X will be released on time :)

Well I haven’t blog’d for a month so thought I’d better update you all with what’s been going on recently…

Well I decided to give GitHub a try out (I’ve had an account registered on the site and I reserved the ZPanel project name) but haven’t yet given it much of a try-out but as I released the latest version of Zantastico I decided that I would use Github to host the source and maybe I’ll now make the complete jump from SVN to Git for all my source control needs :) – I must say, Github is very cool indeed (once you get your head around how the permissions work and adding your machine keys etc.). The web interface offers some lovely features that allows you to edit code from the browser and will automatically revision it too :)

Also over the last month, I decided that I needed to upgrade and combine my home server’s, I went and bought a Netgear ReadyNAS Duo off Ebuyer… It was knocked down in price a little and thought ‘well why not…’ – It been great, it has some lovely in built features of which not all I am using but comes with DLNA media streaming services, Built in torrent client, APF shares compatible with Apple TimeMachine as well as all the standard NAS protocols.. SMB, CIFS, FTP, RSYNC etc. – I’m really pleased with it, I currently have a single 250GB drive with all my data on it but will be upgrading it 2x 2TB drives which will create a RAID1 mirror. :)

After a long time of meaning to install Rustus’s BIND module for ZPanel 6.1.1 on my CentOS VPS (used for hosting this site and many of my friends personal sites too) I was really impressed.. Rustus is one of the best module developers that I have seen and has produced several third-party modules for ZPanel. I am especially impressed with the DNS module due to how it handles the the ZONE files and how much effort has gone into creating this module. It really is a must for any persons running ZPanel 6.x.x as it is such a time saver… No more logging into UKReg (my domain registrar) to manage my DNS records now… :)

Lastly, not that you are interested… This weekend I went to go and try on a suit for my mums wedding, she is to be married on the 1st of October 2011 and my brother and myself will be giving her away as my beloved grandfather passed away in 2006. Here is a photograph of the awesome suit with me in it (it has ‘tails’ too but the photo isn’t all that good) :)

Until next time… :)

Well I should really be asshamed of myself (not have posted a blog post since Novemeber last year!!!) but to be honest with you all I have been very busy with many different things and haven’t really had anytime over the last month or two to do very much coding at all :(

But anyway, I thought I’d make a post for you PHP proceedural programmers out there who want to get to grips with OOP (Object Orientated Programming) and PHP Classes; Many new developers find this fairly daunting moving from Proceedural to Object Orientated (I did too!) so I thought I’d give a basic outline of what a Class is and then write a very basic class so you can apply what you learn here with what you already knew and then hopefully…. it’ll make much more sense and you’ll be writing your own PHP Classes in no time at all! :)

So in simple terms what is a PHP Class?

Well, really a Class is little more than a container that holds variables and functions but they can be very useful for building small components – almost miniture programs.  The difference is that you dont need to shell out to them or anything, and they can be plugged into most scripts with ease – Just a require or include statement at the top. A class describes an ‘object’. An object is a collection of smaller objects, just like in a class. Say for instance, we want to describe a simple door lock. A lock class might contain:

variable $Bolt
variable $Position
function TurnKey( $Direction, $Distance )
function CheckLock( )

TurnKey() would accept a direction value that would in turn determine if the bolt should be locked or unlocked. Once the key is turned far enough in either direction ($Distance) the Bolt will either set or clear. The $Bolt variable shows the current state of the door lock, and $Position is used to track how far the key has been turned in any direction. Now, obviously this could all be done with regular variables and functions, and if you only need to use one lock in the code, it would probably work just fine. But what if you want more than one lock? One if you wanted more details about the lock, or even something else? Maybe you want a door in the code, too – And the lock would just be a subset of the door. Maybe you have a whole house. You might have more than one door, or you might not. But each door certainly needs a lock. You can see how complex object-related problems can get. 

Class Structures

For lack of a better example right now, i’m going to show you how to create a simple page object. The page object encompasses a few of the basic page necessities in building an HTML page. Now, there is a lot more you could do with this class, but i’m going to keep it simple for the time being. You guys can expand on it if want later. Okay, lets start by looking at what the basic elements of an HTML page are. First, you have the open and closing tags, <HTML> and </HTML>. Those will need to be included in the output. Also, you have the page Title, Keywords for the Metatag field, and naturally the main content, or body. Now that we know what we need, lets start building our class. 

First thing we need is the class itself: 

<?php
 class Page {

 }
?>

This is a basic class. No variables, no functions, nothing – Completely empty. All class structures look the same here with the exception of the ‘Page’ name. Every class/object is assigned a name for reference. You’ll need to know this to create new copies of the object, so pick something straightforward and sensible. 

Next, we need to put in our variables. We need a title for the page, keywords, and a content body, like so: 

<?php
class Page {
   var $Title;
   var $Keywords;
   var $Content;
 }
?>

Now, we could actually start using the class here. We have an object. But it isn’t done – We still want to add some functions, to make it easier to work with. 

What functions do we need? Well, we need something to build the output HTML. Lets call that ‘Display’. Lets create simple functions for displaying the Title and Keywords, also. Lets make a function for setting the content as well. 

The final class code is:

<?php
class Page {
   var $Title;
   var $Keywords;
   var $Content;

   function Display( ) {
     echo "<HTML>\n<HEAD>\n";
     $this->DisplayTitle( );
     $this->DisplayKeywords( );
     echo "\n</HEAD>\n<BODY>\n";
     echo $this->Content;
     echo "\n</BODY>\n</HTML>\n";
   }

   function DisplayTitle( ) {
     echo "<TITLE>" $this->Title "</TITLE>\n";
   }

   function DisplayKeywords( ) {
     echo '<META NAME="keywords" CONTENT="' $this->Keywords '">';
   }

   function SetContent$Data ) {
     $this->Content $Data;
   }
 }
?>

Now, at first glance this looks pretty simple. And you know what? It is. Its just basic PHP code wrapped into a class. Let me point a few things out before we go any farther though.

VAR - All variables declared in the class must be placed at the top of the class structure, and preceeded with the VAR statement.�
$THIS - $this is a variable that incidates the current object. That way, the object knows how to find itself while running functions contained within it.�
 For instance, $this->Keywords gets the data from the $Keywords variable in the object. You’ll also notice that when using variables contained�
 within a class, you can’t use the $ to reference them – But you have to use it to reference the object itself. 

Lets save this class file out before we continue. If your following along, save it out as page.class for now. You’ll need it for the example coming up.  

Using the Class

 

Now that we have a class created, lets try using it in a normal script. 

<?php
 include "page.class";

 $Sample = new Page;

 $Content "<P>This page was generated by the Page Class example.</P>";

 $Sample->Title "Using Classes in PHP";
 $Sample->Keywords "PHP, Classes";
 $Sample->SetContent$Content );

 $Sample->Display( );

?>
 

Doesn’t get much simpler than that, does it? We use the include statement to bring in the class from its external file. We use the ‘new’ statement to create a new copy of the object so we can work with it. The new copy is stored into a variable called $Sample. Then we just set some variables – The Title, Keywords and Content – And use the Display function to output it. Easy or what?

Hi all,

Another feature that I needed for my project over at zpanel.co.uk was an SVN server so that other members of my team can download the source code, make changes and upload it back to the master server.

So this quick tutorial will show you the basics of how to setup an SVN server with public read access but authenticated write access.

Ok so first of all you’ll need Ubuntu Server 10.04 installed on your server…

Now we’ll install SVN, Apache and the SVN library for Apache…

apt-get install apache2 subversion libapache2-svn

and now we’ll create our repository like so..

svnadmin create /svn

In the above example I have called the repository ‘svn’ but obviously you can substitute ‘/svn’ for ‘/{yourprojectname}’ if you’d like.

Now what we’ll do is edit the configuration file for subversion…

nano /etc/apache2/mods-enabled/dav_svn.conf

The Location element in the configuration file dictates the root directory where subversion will be acessible from, for instance: http://www.server.com/svn

<Location /svn>

The DAV line needs to be uncommented to enable the dav module

# Uncomment this to enable the repository,
DAV svn

The SVNPath line should be set to the same place your created the repository with the svnadmin command.

# Set this to the path to your repository
SVNPath /svn

The next section will let you turn on authentication. This is just basic authentication, so don’t consider it extremely secure. The password file will be located where the AuthUserFile setting sets it to…  probably best to leave it at the default.

# Uncomment the following 3 lines to enable Basic Authentication
AuthType Basic
AuthName “Subversion Repository”
AuthUserFile /etc/apache2/dav_svn.passwd

To create a user on the repository use, the following command:

sudo htpasswd -cm /etc/apache2/dav_svn.passwd <username>

Note that you should only use the -c option the FIRST time that you create a user. After that you will only want to use the -m option, which specifies MD5 encryption of the password, but doesn’t recreate the file.

Example:

sudo htpasswd -cm /etc/apache2/dav_svn.passwd ballen
New password:
Re-type new password:
Adding password for user ballen

Restart apache by running the following command:

/etc/init.d/apache2 restart

Now if you go in your browser to http://www.server.com/svn, you should see that the repository is enabled for anonymous read access, but commit access will require a username.

If you want to force all users to authenticate even for read access, add the following line right below the AuthUserFile line from above. Restart apache after changing this line.

Require valid-user

Now if you refresh your browser, you’ll be prompted for your credentials:

So that is now it, you now have a working Linux Subversion server!

You can also create additonal respositories with different access rights by simply duplicating and renaming this file /etc/apache2/mods-enabled/dav_svn.conf. – Just use your head, you’ll figure it out!

Remember if you make changes to SVN you MUST restart apache for the changes to become active!

Happy coding!

Its highly likely that soon I will need to write a large database
application for handlering customer contact and calls.

Basically this enables you to write an aplication in PHP and then have
the ability to later migrate the database from say MySQL to Oracle,
Firebird, Postgres etc… (You get the idea) – This ultimately will
save you time not having to re-write huge chunks of code or SQL :)

This does however require the application server to have PEAR::DB
installed and registered with PHP – Which is very easy and if your not
sure how to achieve this, please try a search on the ‘oh-so-powerful’
Google! :)

I found this great tutorial with regards to PEAR::DB; if your
interested in this please go and check it out here:
http://www.evolt.org/article/Abstract_PHP_s_database_code_with_PEAR_DB/17/21927/index.html

I hope you found this as interesting as myself :)

I had a little hunt around online this evening for a graphical user interface for MySQL on my Linux workstation which is running Ubuntu 10.04 – In the past I’ve always just used the terminal and raw MySQL commands but to be honest this can be some what time consuming so I thought I’d share with you how I managed to install the MySQL graphical client….

So it was really simple….
sudo apt-get install mysql-admin mysql-navigator

Then that’ll not only install it for you (saves you having to compile for source) but will also mean you’ll get notification of application updates and if you decide, will also automatically update the software for you too!

Anyways, I hope this is useful for some people :)

A screenshot here too :)

Now a few of my projects are getting much bigger and the need for having dedicated Database servers are now more appealing than ever.

With MySQL when setting it up I noticed that when trying to access a remote database from an application server, it would takes ages to respond/connect to it.

After searching around the web, I have found what appears to be the solution…

It appears to be that the MySQL server lookups hostnames for authentication EVEN IF you are using for example: remoteuser@231.23.65.23 so to speed up remote connections to your remote database servers try adding the following line…

skip-name-resolve

To your MySQL configuration file (my.ini/my.cnf) and then be sure to reload (restart) the MySQL database server.  The ‘[mysqld]‘ section of the my.cnf file now looks like this:

[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-locking
skip-name-resolve

From Microsoft Windows this can be done from the ‘Services’ window in ‘Administrative tools’ or in Debian/Ubuntu entering this command:

/etc/init.d/mysql restart

The above addition will ONLY work if you are using IP Address or ‘%’ for the Database User accounts and will ofcourse disable the ability to use user accounts with the hostname eg. remoteuser@server2.example.com.