Converting from Mac to Unix line endings

Posted: March 10th, 2010 | Author: Rob Searles | Filed under: Linux, Tutorials | Comments

This is more a post for me than anyone else, but I thought I’d share anyway.

In our team we have a mixture of Linux and Mac users, and we are constantly editing the same files. One of the most irritating things is when a file is saved with Mac line endings instead of Unix line endings (displaying as ^M in Emacs). This happens very rarely, but on the occasion it does I can quickly convert back using the following commands on my Linux box:

$ tr '\r' '\n' < file-in-question.txt > tmp.file
$ mv tmp.file file-in-question.txt

Works perfectly

If you want to find more about the tr (Translate) program, visit the man page.


Understanding Bitwise Operators (hopefully)

Posted: December 4th, 2009 | Author: Rob Searles | Filed under: PHP, Tutorials | Comments

After the trouble I had with bitwise operators yesterday I found some time to really sit down and get my head properly around them. Let’s dive straight in.

We need to initially define our flags:

define('BASE', 0); // binary 00000000
define('F1', 1);   // binary 00000001
define('F2', 2);   // binary 00000010
define('F3', 4);   // binary 00000100

To start with we have no flags set, so if we set F1 using the following:

$f1_set = BASE + F1; // $f1_set = 1
echo "F1 set = $f1_set\n";

All is well and good, $f1_set = 1 as expected.

However, what if we set F1 again?

$f1_set_twice = $f1_set + F1; // $f1_set_twice = 2 !!! wrong !!!
echo "F1 set twice = $f1_set_twice\n";

As you can see, if we set F1 twice, it effectively “unsets” F1 and sets F2. Not what we were after.

So why is this? Well, it’s kind of obvious and I was being a bit of a muppet for not spotting it yesterday. The reason is pretty simple: 1 + 1 = 2. (I told you it was obvious!)

Clearly this is not what we want, but how can we solve this? By using the bitwise OR. If we change the statements slightly as follows:

$f1_or_set = BASE | F1; // $f1_or_set = 1
echo "F1 OR set = $f1_or_set\n";
 
$f1_or_set_twice = $f1_or_set | F1; // $f1_or_set_twice = 1 - huzzah!
echo "F1 OR set twice = $f1_or_set_twice\n";

As far as “unsetting” the flags if we use my original method we fall (again) into trouble.

$f1_and_f3 = BASE | F1 | F3; // $f1_and_f3 = 5;
$unset_f3 = $f1_and_f3 - F3; // $unset_f3 = 1
echo "Unset F3 = $unset_f3\n";
 
$unset_f1 = $f1_and_f3 - F1; // $unset_f1 = 4;
echo "Unset F1 = $unset_f1\n";

Now, if we try to “unset” F1 twice, we arrive at the problem.

$unset_f1 = $f1_and_f3 - F1; // $unset_f1 = 3;
echo "Unset F1 = $unset_f1\n";

Unsetting F1 twice here effectively turns off F3 and sets F1 and F2 – completely wrong!

Instead, if we use the &~ binary operator mentioned in Jesper’s comment all works as expected. (note: I can’t find mention of this operator in the PHP docs, please someone help me out)

$f1_and_f3 = BASE | F1 | F3; // $f1_and_f3 = 5;
$unset_f3 = $f1_and_f3 &amp;~ F3; // $unset_f3 = 1
echo "Unset F3 = $unset_f3\n";
 
$unset_f1 = $f1_and_f3 &amp;~ F1; // $unset_f1 = 4;
echo "Unset F1 = $unset_f1\n";

Even if we try to “unset” a flag twice, it still has the same results:

$unset_f1 = $f1_and_f3 &amp;~ F1; // $unset_f1 = 4;
echo "Unset F1 = $unset_f1\n";
 
$unset_f1_twice = $unset_f1 &amp;~ F1; // $unset_f1_twice = 4;
echo "Unset F1 twice = $unset_f1_twice\n";

Also in Jesper’s comment and original post was the use of the left shift operator: <<. After playing around with this it seems very simple to use, as follows:

$f1 = 1;    // 00000001
$f2 = 1<<1; // 00000010
$f3 = 1<<2; // 00000100
$f4 = 1<<3; // 00001000

Or to put it another way:

$f1 = 1;      // 00000001
$f2 = $f1<<1; // 00000010
$f3 = $f2<<1; // 00000100
$f4 = $f3<<1; // 00001000

After all this I think I am a little closer to understanding Bitwise operations, hopefully! Tomorrow I’ll have a crack at testing to see if a flag is turned on or not. Until then…


Bitwise Operators used for Flagging Items

Posted: December 2nd, 2009 | Author: Rob Searles | Filed under: PHP, Tutorials | Comments

Update: Thanks to Jesper Noehr of BitBucket fame for pointing out gaping flaws in my post below (see his comment). I strongly advise you disregard all I have said below, because it will get you into a mess, in much the same way it has me. I’m going to sit down when I have a spare 1/2 hour and try to work out exactly what is going on! Many thanks and big kudos to Jesper, I really appreciate the time you took to correct me.


I have always wondered what the point of Bitwise Operators were,to me they seem to belong to a distant past. However, after reading a couple of great blog posts I have at last an understanding of how they can be put to use, and have started playing around with them a bit (ba dum!).

Jesper Noehr has written about using bitwise operators for a flexible permissions scheme within Python  and Jonathan Snook has taken the bitwise concept further creating a great calendar app in Javascript. After reading these I thought I better dive in, and an opportunity came along yesterday when I had to code a flagging system within PHP.

Read the rest of this entry »


Emacs CSS-Mode Fix (adding to the chain!)

Posted: October 29th, 2009 | Author: Rob Searles | Filed under: Open Source, Tutorials | Comments

After my week with Emacs I’m still struggling along. One of my main annoyances is the default way some of the modes indent the code. I think the worst offender of this is the default CSS-mode. After frustration when, yet again, I hit the tab key and the cursor rockets about 80 places to the right I decided to Google for a fix.

The internet is great! Clearly it wasn’t just me having a problem, Guido Stevens was also suffering and posted a fix, which he himself found on another blog post by Chris Miller, which Chris in turn found on StokeBloke.com which had been found originally on a now dead post.

Not bad! Four people all having problems with the same thing and posting a fix for others to share. So I thought I’d join the chain, and write this blog post.

Oh yes, and the fix itself:

;; fix css mode
(require 'css-mode)
(setq cssm-indent-level 2)
(setq cssm-newline-before-closing-bracket t)
(setq cssm-indent-function #'cssm-c-style-indenter)
(setq cssm-mirror-mode t)

VMWare Server: Connecting to Guest via SSH

Posted: October 10th, 2009 | Author: Rob Searles | Filed under: Linux, Tutorials | Comments

I have recently been playing around with an old laptop and it’s give me the taste for delving deeper into the world of Linux. For this I normally use VirtualBox, but a friend of mine (who is an avid Mac fan) was recently raving about VMWare’s Fusion. I decided to see if they had anything similar for linux and give it a whirl. I toddled along to the VMWare site, couldn’t find anything, so Googled VMWare Server and went straight to the relevant page!

I downloaded, installed and connected to the VMWare Server admin via Firefox. All well and good. So now I wanted to actually use it. I decided to test it out using Debian as the Guest. I chose Debian simply because I know it, and I know it has a net install, meaning I could test out a minimum system.

I downloaded the ISO, and then set about creating my new Debian Guest. First problem, I couldn’t find the Debian ISO! After reading the docs (which always takes second place to proding and poking around) it turns out that you need to add a Data Store, using the Commands menu on the right hand side of the VMWare admin home.

Once I had sorted this out I then created the new Guest specifying NAT networking, attached the Debian ISO and started it up.I selected NAT networking because Host Only will not let me get access to the outside world from the guest – obviously a pain as I was using the Debian net install – and I have always found Bridge Networking to be as flaky as a delicious if not slightly dry Cadbury’s chocolate bar.

Hmm, VMWare is clearly very different to VirtualBox because it didnt open a window where I could see the new guest booting up. How do I communicate with the guest. Again, after some poking I found a tab for the Console, which at first asked me to install a Firefox plugin, but once that was installed I could see the Guest.

I then installed Debian and restarted the Guest server.

Once rebooting I then tried to install OpenSSH. Slight problem, I couldn’t get an outside line! I couldn’t even ping Google. Hmm. After some searching I found out that the problem was DHCP was not starting up. I ran the command (as root)

$ dhclient eth0

This allowed me to connect to the outside world, and to ensure it would always start up I followed the commands from this post within Debian Help:

Add the line

auto eth0

to /etc/network/interfaces

After this I managed to successfully install Open SSH server, by running the following command as root:

apt-get install openssh-server

So now it was already to I tried to connect via SSH from my Host Ubuntu system to my Guest Debian system. But how? Again, after some searching I found out that you had to forward some ports. On the host, edit /etc/vmware/vmnet8/nat/nat.conf. Uncomment the line under the SSH section that says:

# 8889 = 172.16.81.128:22

the restart the VMWare networking services:

sudo /usr/lib/vmware/net-services.sh restart

You can see from the nat.conf file that you can also forward other ports such as Web and FTP

Finally, you can now connect to your Guest from your Host with the command:

ssh -p 8889 root@localhost

It took some effort, but I can now successfully connect to any number of Guests from my Host.


Installing PHP5.3 on Ubuntu Jaunty

Posted: October 4th, 2009 | Author: Rob Searles | Filed under: PHP, Tutorials | Comments

I have recently dusted off an old laptop to play around with and do some “fun” development. One of the things I have done to this laptop is to install a LAMP stack using PHP 5.3 so I can play around and see what’s new and exciting.

I had a quick Google on how to install PHP 5.3 on Ubuntu and found Brandon Savage’s post on the very subject! Whilst he has a great write up it was one or two of his comments mentioning the DotDeb.Org package repository.

This was a breeze to use and in about 5 minutes I have PHP 5.3 running with Apache and MySQL on my clean Jaunty system.

The first thing to do is add the DotDeb.org repositories to your sources list:

$ sudo nano /etc/apt/sources.list
# php5.3
deb http://php53.dotdeb.org stable all
deb-src http://php53.dotdeb.org stable all

Now update and upgrade Ubuntu

$ sudo apt-get update
$ sudo apt-get upgrade

Now simply install apache mysql and php as usual

$ sudo aptitude install apache2 apache2-mpm-prefork mysql-client-5.1 mysql-server-5.1 php5 php5-cli php5-mysql libapache2-mod-php5

You will get a warning about untrusted packages being installed but this simply means that the PHP5.3 stuff is not signed, so I typed in “yes” and carried on. Obviously you can cater this line to your specific needs, but this is the basic stack up and running

When it was all complete I tested out my system:

$ php -v
PHP 5.3.0-0.dotdeb.8 with Suhosin-Patch 0.9.7 (cli) (built: Aug 12 2009 18:11:27)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

and tested out my apache installation using a phpinfo() script:

$ sudo nano /var/www/info.php

then fired up http://localhost/info.php
<?PHP
phpinfo();
?>

and saw as the main header of the page:
PHP Version 5.3.0-0.dotdeb.8

All done. Simple!


Simple tools produce powerful results

Posted: September 14th, 2009 | Author: Rob Searles | Filed under: Linux, Open Source, Tutorials | Comments

Tools

Sometimes I really love the Linux world. Sometimes it all fits so neatly together and lets you just get stuff done. Sure it might take a bit of digging around, but when it all comes together it really is beautiful!

I’m waxing lyrical today because I have just managed to fit all the pieces together for a certain aspect of a large project we’re working here at ibrow towers. This particular problem was a three parter:

  1. Users can FTP into a server into their own directory.
  2. These users should be created automatically (i.e. sign up on site, get details, ftp) and have the same login credentials as the rest of the site
  3. Uploads to the FTP server should be monitored and each file processed

To solve this three part problem, I utilised the following:

  • VSFTPD FTP Server using MySQL as the credentials database
  • incron as the directory monitoring
  • bash as the glue

Read the rest of this entry »


Ubuntu Jaunty command line email: Sup, OfflineIMAP and MSMTP

Posted: August 30th, 2009 | Author: Rob Searles | Filed under: Linux, Tutorials | Comments

Recently I’ve been getting increasingly frustrated with both Thunderbird. Don’t get me wrong, it is an excellent email solution, but lately I’ve been finding that it is slowing down due to the amount of emails I have. I do have a webmail package attached to it, which I use of Thunderbird has slowed to a grinding halt, but sometimes, and as hard as this may be to believe, I’m not connected to the internets!

Also, using Thunderbird is just not geeky enough.

So I was looking for something better, something quicker that allowed me to download emails, but keep in sync my IMAP. And don’t forget, something truly dweeby. For me, this meant the command line. I find the more I use Linux the more I am drifting towards the command line as a way of getting things done.

After a bit of searching around, I found a Rails package called Sup. This looked like the ideal solution from my point of view. The authors seemed to be coming from the same head space I am regarding email, so I thought I’d give it a try.

After several hours hacking about, I now have it fully working, and even sending email, and I’ll outline the steps I took to get it up and running. I’m running Xubuntu Jaunty, but hopefully this will apply to most Ubuntu and Debian versions.

Read the rest of this entry »


Wordpress: Multiple Content Columns

Posted: July 5th, 2009 | Author: Rob Searles | Filed under: Open Source, PHP, Tutorials | Tags: , , | Comments

I’ve been working on a Wordpress site for a client with one of our developers Tom Hartnell. The design of this particular site was that some pages would need content displayed over multiple columns. This content had to be updatable by the client, as such it could not be hard coded into the template files.

We had a look around and we found a potential solution on this blog post.

We played around with this for a while but found that, whilst it was a good solution, it wasn’t quite suitable for our needs. What we needed was a solution which allowed us to have any number of columns and for each column to contain completely different content. The solution in the post demanded that each column start with the same HTML tag (H2 in the example). We decided to rewrite the function described above, based on the same principals, but updated and improved so it would fit our needs.

To see what the end result is, you can see a demo page.

Read the rest of this entry »


Securing CentOS/cPanel with Sudo

Posted: June 30th, 2009 | Author: Rob Searles | Filed under: Tutorials | Comments

I’m primarily a Ubuntu and Debian user, however just bought a new VPS server to hold my company’s clients’ website. For ease of use I chose a cPanel powered VPS which runs on CentOS 5.2

I wanted to secure the setup as much as I could, and one way is to disabled login via root and use the system I’m more familiar with: Sudo. This was slightly more tricky than I thought, but is still relatively painless once the problems have been worked out.

The steps below show how to add a new user (rob) remove any unneeded (for this user) directories, add them into the sudoers list, fix sudo if it is causing you problems, disable root from login in via SSH.

Read the rest of this entry »