Archive for January, 2006


How to: Easily parse XML with PHP

Sunday, January 29th, 2006

There are so many packages and scripts online to help a person parse XML. Unless your parsing some insanely complex files, there is no need. Its as easy as this:

Code:
$batch = new DOMDocument();
$batch->load($this->webPath);
$emails = $batch->getElementsByTagName("email");
foreach( $emails as $email ) {
  $to = $email->getElementsByTagName("to")->item(0)->nodeValue;
  $cc = $email->getElementsByTagName("cc")->item(0)->nodeValue;
  $bcc = $email->getElementsByTagName("bcc")->item(0)->nodeValue;
  $from = $email->getElementsByTagName("from")->item(0)->nodeValue;
  $subject = $email->getElementsByTagName("subject")->item(0)->nodeValue;
  $body = $email->getElementsByTagName("body")->item(0)->nodeValue;
}

How to: Embed HTML within XML

Sunday, January 29th, 2006

I spent some time today trying to figure out how to embed HTML within XML. This would obviously be an issue. When the XML would be parsed, the parser would easily mistaken the HTML tags as child XML tags.

I thought using urencode() or perhaps replacing all ‘<' and '>‘ with < and >. Then it hit me, the XML standard should have thought about this. So I checked the XML 1.0 standard. They did have a solution (remove spaces):

CDATA sections begin with the string ” < ! [C D A T A [ " and end with the string " ] ] >“

Using XML to store/send emails

Thursday, January 26th, 2006

This has been a rough week, 4 assignments, an MIS, etc. I made that phpPostFix script, its very simple, but I won’t be able to use it. My main server with GoDaddy is so terrible. Its running an old version of PHP (4.2). This version did not have file_get_contents as a function, so to make up for it, a while ago, I set up the PEAR Compat replacements. To make a long story short, there are limitations in PHP 4.2 that for some reason, file_get_contents (the PEAR version used in PHP 4.2) can not accept URLs with more than 1 parameter.

I know what your thinking: “Just upgrade your version of PHP”. You obviously have never used a GoDaddy product. This is not an easy task, especially since their servers are married to Plesk and all the crap that come with it. Above all that the main server is fully deployed, I can’t risk upgrading a shady setup. Anyways, here is the script that I wrote. It does the job as explained in the previous post.

Using the script is as easy as the following:

Code:
$to = "[email protected]";
$subject = "Test email";
$query_string = 'to='.urlencode($to).'&subject='.urlencode($subject);
file_get_contents("http://www.someserver.com/phpPostFix.php?".htmlentities($query_string));

I still need to be able to send email from development server. So I spent some time thinking about it earlier this week. I’m going to write the emails which need to be sent to an XML file (send.xml). The schema will be as follows:

Code:
<email>
<to></to>
<cc></cc>
<bcc></bcc>
<subject></subject>
<body></body>
</email>

Every 5 minutes the main server will read the XML file, parse it, and send the emails based on the results. Once the emails are read, it will clear the original XML file (or append it to another backup.xml file). At this point any new emails will be written to a fresh send.xml. This method makes intuitive sense (sending in batch), especially since the development server could be sending hundreds of emails a day. Lets hope this goes without a problem.

My solution to port 25 being blocked

Thursday, January 19th, 2006

So I’ve spent the last 2 days straight trying to get a full scale mail server running on my development Ubuntu server. I had heard it wasn’t going to be easy, but it needed to happen. The plan was to go all out (Postfix + Courier IMAP + Amavisd-new + SpamAssassin + ClamAV + SASL + TLS + SquirrelMail + Postgrey). At around 7:00pm today, when I was completely defeated as to why it wasn’t working it hit me: I bet my ISP has port 25 blocked. A quick search on Google proved me right.

Depressed and annoyed I turned off my laptop, went to the library and then to the gym. While reading Data Mining slides it hit me, I have my main dedicated server at GoDaddy (not to say they haven’t done me wrong in the past). I was considering using some relay host, but I thought of something better: I’ll write a PHP script, which will reside on the main server, and will send mail the standard way (sendmail). The PHP script (lets call it phpPostFix) will be accessed as a URL, and its parameters will be simply:
- To:
- From:
- Subject:
- Body:

On the development server any time I have:

Code:
<?php

file_get_contents("http://www.xxxxxx.com/phpPostFix.php?to=[email protected]&from=.....");

?>

Then the email will be sent. Simple as that. I’ll post the script when its done.

How to: Static IP from command line in Ubuntu-Server

Sunday, January 15th, 2006

In Ubuntu-Server everything is from the command line, and most likely your going to need to set a static ip to your server. All network interface settings are in the /etc/network/interfaces file. Here is what to do:

1. Backup /etc/network/interfaces
2. edit the file with vi. Here you can set the static setting as below. In my case I set the static router ip to be 192.168.1.100

Code:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# This is a list of hotpluggable network interfaces.
# They will be activated automatically by the hotplug subsystem.
mapping hotplug
        script grep
        map eth0

# The primary network interface
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1

Ubuntu-Server quick comments

Saturday, January 14th, 2006

If your going to be administering an Ubuntu server remotely, be sure to leave the install cd in the cdrom drive. While I was installing SSH, for some reason the Ubuntu-Server cd was requested. Even typing this now I’m doubting what I saw.. but anyways, no harm in leaving the cd inside.

Though the following makes sense looking back, when I first realized it I was pleasantly surprised: To turn the server on, all that has to be done is turn the computer on. Once Ubuntu-Server is at the login screen (assuming everything is setup properly) then the web server is running. Of course this makes sense, it would be the ultimate security risk leaving the web server logged in, unattended.

I’m blown away how simple it is to get Apache/PHP/MySQL up and going. In Windows I wrote a fairly extensive tutorial on getting started. Using the Debian apt-get command in Ubuntu, I literally had the server running in less than 5 minutes.

Next steps: Setup SendMail, DNS binds and configure the Apache config file to handle the beating the computer is going take.

Ubuntu-Server 5.10 first look

Friday, January 13th, 2006

I just finished installing Ubuntu-Server 5.10 on a server that I built from scratch. At first glance, it seems like normal Ubuntu just stripped down. The install is exactly the same as normal Ubuntu and everything is from the command line. This flavour of Ubuntu just came out in December; I expect some good things to come soon.

I’m excited to get going with this; the server is nothing crazy, the specs below:

- Intel Celeron 2.93Ghz (over clocked to 3.3Ghz) on Soyo motherboard
- 512MB DDR 400MHz (1 stick)
- 3 fans (1 CPU, 2 on the case)
- 20 gig Maxtor (for now)

Next is to get SSH and FTP set up — then I’ll administer the server from my laptop.

The reality of Canadian politics

Thursday, January 12th, 2006

A couple days ago, I was told that when a news paper shows a photo of a politician, their mouth will be open or closed, based on who the news paper is rooting for. If the news paper likes the politician (political party) they will show a nice (mouth closed) picture. So yesterday, when I saw the cover of the Hamilton Spectator, I couldn’t help but notice….

Hamilton Spectator

I always knew that Canadian news papers had a political slant, but I never thought to the extent of showing (purposely) ridiculous looking photos.

What to do if you accidentally run shuffle through the wash..

Wednesday, January 11th, 2006

…. run it through the dryer. Amazingly it worked! Strangely not right away, but a couple days later, its perfectly fine… except for the fact that only one headphone works (The headphones broke, not the iPod).

In any case, check EVERY pocket before putting your clothing in the wash…

Compiling VPN client on Linux

Tuesday, January 10th, 2006

Just in case anyone in the future has a similar problem — The installation requirements for compiling the Cisco VPN Linux Client says to download the Kernel Source. This won’t work, download the Kernel Source Headers.

Cygwin to Ubuntu

Monday, January 9th, 2006

I finally made the move from Cygwin (Windows) to Linux. I’m working on getting a home-built server running, and the first step was getting a mirror image (of the future server) running on my laptop. My distro of choice is Ubuntu. The installation was easy, but I had a few problems getting the video card and my wireless card going. Everything is running smoothly now.. Next step: install Apache, PHP, MySQL, etc.. on my laptop, exactly as I would have it on the server.

My laptop is 4 years old, a Dell Inspiron 1100, if anyone has the same problem getting their video card to work, just do the following:

1. type ‘gedit /etc/X11/xorg.conf’
2. Add the following 2 lines (inside of the #)

Code:
Section "Monitor"
Identifier "Generic Monitor"
Option "DPMS"
# add
HorizSync 31.5-48.5
VertRefresh 59-75
# end add
EndSection

Once I have the server done, I’ll be installing Ubuntu-Server on the server. I’ll talk about that when it happens.