Enable VPN routing via Firestarter

I had some trouble getting my VPN to work with Firestarter, so I’ll post this simple solution I found after some googling.

In Firestarter:
Go to Preferences->Network Settings.
Choose Ethernet Device (eth0) (yours may be different) as Internet connected network device.
Choose Routed IP Tunnel (tun0) (yours may be different) as Local network connected device.
Place a check mark in Enable Internet Connection Sharing box.
Stop and restart Firestarter.

Originally found here.

JR syntax highlighting in gedit

There were no syntax highlighting for the programming language JR in gedit, so I wrote one myself.

This will work for Ubuntu 9.10, but it may work for others as well.

How to install:
1. Download jr.lang.
2. Place it in either

/usr/share/gtksourceview-2.0/language-specs

or

~/.local/share/gtksourceview-2.0/language-specs

3. Download jr.xml.
4. Place it in (create it if it doesn’t exist yet)

~/.local/share/mime/packages

5. Update the mine database:

cd ~/.local/share
update-mime-database mime

All done! You should now get JR syntax highlighting when opening .jr files in gedit.

Note: The directory paths are based on an Ubuntu system and might be different for other distributions.

Alternatively, you can just run this script to install it automatically:

wget -O jr.lang http://files.kerola.nu/?file=gedit_jr_syntax_hl/jr.lang
wget -O jr.xml http://files.kerola.nu/?file=gedit_jr_syntax_hl/jr.xml
mkdir -p ~/.local/share/gtksourceview-2.0/language-specs
mv jr.lang ~/.local/share/gtksourceview-2.0/language-specs/
mkdir -p ~/.local/share/mime/packages
mv jr.xml ~/.local/share/mime/packages/
update-mime-database ~/.local/share/mime

How to find out and change file encoding in Ubuntu

First, you can find a file’s encoding like this:

$ file oldfilename.txt 
oldfilename.txt: ISO-8859 English text

Then, if you want to convert the file oldfilename.txt from ISO-8859 to UTF-8 and call the new UTF-8 file newfilename.txt you do like this:

$ iconv -f ISO-8859-1 -t UTF-8 -o newfilename.txt oldfilename.txt

Note that the file command says ISO-8859 and that you have to use ISO-8859-1 in the iconv command.
For a long list of available iconv encodings, use

$ iconv -l

How to Connect to Chalmers VPN in Ubuntu Karmic (9.10)

This example is for Chalmers PPTP VPN.
Server: vpn-gw.chalmers.se

1. Install network-manager-pptp.

sudo apt-get install network-manager-pptp

2. Add a new VPN connection in Network Connections. Choose PPTP, create.
3. Add the following and only the following

Gateway: vpn-gw.chalmers.se
User name: NET\your-username-here
Password: your-password-here

Click Advanced...
In Authentication, select only
CHAP
MSCHAP
MSCHAPv2

In Security and Compression select all and
Security: All Available (Default).

4. Restart your computer. This is important, don’t miss out on this one.
5. When back in Ubuntu, connect to Chalmers VPN, then click Deny when it asks
about the keyring. Enter your password for your account and then select Always Allow
when it asks about the keyring the next time.
6. Done!

Why all this restarting, denying, re-entering passwords and stuff you ask? It seems like there’s a bug in network-manager-pptp which will hinder you from connecting if you don’t follow the above procedure. Don’t ask me why this bug isn’t fixed, but I don’t think doing the above steps are that gruesome in order to get the simple functionality of an otherwise well-working VPN GUI for Ubuntu.

Salmon skewer with bulgur

A delicious and yet simple dish.

Servings:
4
Preparation time:
10 minutes
Ingredients:
500g salmon
2 tbsp. cooking oil
1 tsp. salt
1 tsp. lemon-pepper
1 tbsp. sesame seeds
Bulgur:
4 dl bulgur
8 dl chicken stock
1 tbsp. neutral cooking oil
2 tbsp. chopped leek
1/2 zucchini
1/2 bell pepper
salt and grounded black pepper
Pesto sauce:
2 dl crème fraiche
2 tbsp. pesto

Pesto sauce:
Mix crème fraice and pesto. Let it cool down until serving.

Bulgur:
Trim and chop the leek finely. Cut the zucchini and bell pepper into cubes. Sizzle the leek and bulgur in oil in a deep, wide kettle. Pour 4 dl of stock and cook on low heat during constant stir. Add more stock as time goes by. When the groats are soft they are done. Finish off by adding zuccini and bell pepper along with salt and newly grounded black pepper.

Salmon Skewer:
Cut the fish into smaller bits and skewer them. Brush some oil on and season with salt and lemon-pepper. Add sesame seeds over it all. Grill the skewers until they get a nice color.

Installing Git on a shared webhost

This great article describes how to install git on a remote machine where you don’t have root access. It’s really useful if you want to use your web host’s server as a place to backup your code. You’ll need SSH access to the server though.

http://writepermission.com/2009/09/install-git-on-a-shared-webhost/

Just to make sure this information stays online, I’ve just made a copy of the article’s most useful part below:

This rpm needs to be installed, but you won’t be able to use the regular rpm installer because this requires root access. You can extract the rpm file with the command:

rpm2cpio git-1.6.x.x.rpm | cpio -imdv

This will create a usr/ directory in currect directory. You best move this directory to your home root:

mv usr ~/usr

Now we are almost there, we only need to add the directory to $PATH variable. Doing this will make it possible to execute the command git from anywhere. Open your ~/.bashrc file with your favorite editor (vim or pico) and add the following line:

export PATH=$PATH:$HOME/usr/bin:$HOME/usr/libexec/git-core

And that’s it. To activate this change, run source ~/.bashrc or log out and in again.

Get remote HTML with PHP

This piece of code retrieves the HTML code from a page, which you can then process and manipulate as you see fit. In the below example, the source code of my Last.fm page is retrieved.

<?php
// This variable will hold the code
$content = "";

// You have to divide host and the rest of the url,
// so in this example, the full url you wanted to get
// would be http://www.last.fm/user/CaseuS_
// As you can see, you don't have to specify the
// protocol used.
$host = "www.last.fm";
$url = "/user/CaseuS_";

// Open the connection on port 80
// $errno and $errstr can be used to
// get the error number and error message
// in case something went wrong.
// 30 stands for the timeout, in seconds,
// that we use for the connection attempt.
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp)
{
    echo "$errstr ($errno)<br />\n";
}
else
{
    $out = "GET $url HTTP/1.1\r\n";
    $out .= "Host: $host\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp))
    {
        $content .= fgets($fp, 128);
    }
    fclose($fp);
}

// Do something with $content, e.g. output the whole code:
echo $content;
?>

Google CSE Module URL Rewrite

I found a great tip regarding fully integrating the Google CSE module with the rest of your Drupal installation.

Basically, the tip I found to be really useful was the rewrite rules for the Google CSE search itself. Since Drupal comes with this built-in search module which is easily accessible at http://kerola.nu/search/your-terms-here, it’s really great to use Apache URL rewrite and redirect users from that Drupal search (which I think isn’t really that good) to the Google CSE result page. The only real change from the original page is the last line of the code, which sort of sums things up and enables the user to search directly from “search/your-terms-here”. Take a look at the code below!

In your .htaccess file, add the following lines:

RewriteRule ^search$ /search/google [R=301,NC,L]
RewriteRule ^search/node$ /search/google [R=301,NC,L]
RewriteRule ^search/node/(.*)$ /search/google?query=$1 [R=301,NC,L]
RewriteRule ^search/taxonomy_search$ /search/google [R=301,NC,L]
RewriteRule ^search/taxonomy_search/(.*)$ /search/google?query=$1 [R=301,NC,L]
RewriteRule ^search/((?!google).*)$ /search/google?query=$1 [R=301,NC,L]

Idea from http://www.mc-kenna.com/general/2008/12/tips-for-google-cse-plugin-for-d…
Thanks!

Fix endless redirection issue with Drupal Google CSE and Search 404 module

When using the Search 404 module and Google CSE (Custom Search Engine) together, the result is an endless redirection that will… well, you know, loop endlessly and stop you from doing more important things, like getting to your search results.

The problem occurs in at least Search 404 version 6.x-1.7.

This can be easily fixed by changing one line into two in the /sites/all/modules/search404/search404.module file.

On line 117, in function search404_page(), change

drupal_goto ('search/google/'. $keys);

into

$_REQUEST['destination'] = 'search/google/'. $keys;
drupal_goto();

Fix Spotify sound in Ubuntu Karmic (9.10)

When I first ran Spotify through Wine after just having had installed the new Ubuntu Karmic I immediately noticed that the sound from Spotify wasn’t the best, really. Actually, I’m being a bit too kind here. The sound was horrible! It sounded like the audio wasn’t decrypted or something like that since all I heard was a lot of noise.

Luckily, there is a solution to this.

  1. Make sure the package “wine1.2″ is installed:
    sudo apt-get install wine1.2
  2. Type winecfg in terminal, go into the Audio
    tab and change the following as below:
    Sound Drivers: Check both OSS and ALSA
    Hardware Acceleration: Full
    Default Sample Rate: 44100
    Default Bits Per Sample: 16

Shorten a text string

This piece of code shortens a text string to the specified length on a word basis and adds “…” after. Perfect for use in teasers of lengthy text pieces and so on.

<?php
/**
* Shortens a string to the specified number of characters on a word basis.
* Adds "..." to the end of the string if the string was longer than specified.
* Example 1
* $text = "The sky is not the limit, I know 'cause I feel higher today!";
* echo shortenText($text);
*
* Would print:
* The sky is not the limit, I know 'cause I feel higher today!
*
* Example 2
* $text = "The sky is not the limit, I know 'cause I feel higher today!";
* echo shortenText($text, 20);
*
* Would print:
* The sky is not the...
*/
function shortenText($text, $chars = 256)
{
    // Do not shorten if the text is already
    // shorter that the desired amount.
    if(strlen($text) <= $chars)
        return $text;

    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text."...";

    return $text;
}
?>

Prevent hot-linking

Hot-linking is the act of someone showing an image from your site on a site that ain’t yours, meaning that you get no visitors or any kind of recognition for having the image on your site, but you’ll still have to pay for it with your bandwidth. That sounds kind of unreasonable, don’t you think?

So let’s learn how to prevent it by adding a few rows in our .htaccess file in the root of our website.

Add the code in the box into your .htaccess file to make it work.

# Anti-hotlinking
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?kerola\.nu/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

The first row checks if the referer (i.e. the place on the Internet your visitor came from) isn’t your own domain.

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?kerola\.nu/ [NC]

The second row checks if the referer isn’t empty, i.e. the user typed in the URL to the image directly in her browser.

RewriteCond %{HTTP_REFERER} !^$

The third row generates a HTTP 403 Error (Forbidden) if the two above conditions were met and the requested file type was one of jpg, jpeg, gif, bmp or png.

RewriteRule .*\.(jpe?g|gif|bmp|png)$ - [F]

So in short, this code denies access to your image if you aren’t requesting it from kerola.nu or directly through the browser window. Exactly what we wanted! :)

Change Caps Lock Into Scroll Lock

The caps lock key is really useless and just a burden in everyday life. You accidentally hit it while typing and come out as a Rageaholic 9000 to the people around you.

So what can you do about this? Disable caps lock entirely, you say? Well, that is possible, but if you think about the whole situation for a second there, you’d soon realize that you’d be throwing a whole key away from your keyboard. While playing games (say) I sometimes wish I’d had an extra key right around the regular WASD area, so that I could bind it to something useful. If the whole Caps Lock key is disabled, that can’t be done. Therefore, let’s change the Caps Lock key into an equally useless key, but much more harmless key: Scroll Lock. Scroll Lock is also never being used, but nothing scary happens when you push it.

So let’s combine the Caps Lock’s dreamy position with the harmlessness of the Scroll Lock key shall we?

To do this, you need a mere simple registry hack.

Go to the following place:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout

Change the value of ScanCode Map to the value below, restart your computer and you’re done!

00000000 00000000 02000000 46003A00 00000000

If you’d like a bit more explanation of what this actually does then sure. Basically the ScanCode Map value is just a field wish tells Windows that we want to remap some key into another.

Let’s analyze those numbers a bit, shall we?

We start with

00000000 00000000

This is just here to waste space. Brilliant!

Then we have

 02000000

This number 2 in here stands for how many keys we are going to remap plus one. In this case, 1 + 1 = 2. Simple enough. The following zeros just waste space, again.

Then we get to the interesting section:

 46003A00

The first four numbers, 4600, stands for the scan code of the key we are going to map to. The last 4 numbers are the scan code of the key we are going to map from.
Luckily for us, 4600 is the scan code for Scroll Lock and 3A00 is the one for Caps Lock. So a little logic processing tells us that we are going to map the keyboard so that whenever you press Caps Lock, it will behave as if you’d pressed Scroll Lock. Just what we wanted! :)

The remaining

 00000000

just wastes some more space, nothing to worry about.

Don’t forget to restart your computer or it won’t work!

Nice urls with .htaccess

This code turns ugly urls full of parameters into user- and search-engine-friendly browser urls. And it’s totally automatic. This is a win-win-situation :)

Edit your .htaccess file like this if you want your urls to function like this:
http://domain.com/about -> http://domain.com/about.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
# http://domain.com/about -> http://domain.com/about.php

Or, if you want
http://domain.com/about -> http://domain.com/index.php?q=about

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
# http://domain.com/about -> http://domain.com/index.php?q=about

Hello world!

This page is just a place for me to dump random stuff that I need to get off my mind but still don’t want to forget completely. So be prepared for a ruthless mind dump!