Monday, 10 February 2014

Cleaning up PHP short-open-tags with Eclipse

One major problem of old/legacy or unmaintained PHP scripts is, that they usually use short-open-tags to enclose PHP code:

<? echo "foobar"; ?>

This used to be common in the old days, but trying to run those scripts on a modern LAMP stack will most likely result in 500-code errors.

Sure you could just enable short_open_tag in the php.ini and it would solve the problem. But even though there are no official hints that it might get deprecated or removed, it is still a bad practice. The Basic Coding Standard by the PHP Framework Interop Group clearly states:

Files MUST use only <?php and <?= tags.

So why should you hold on to bad practices, if you can just easily mass replace them to normal open-tags? Eclipse offers a feature where you can do a project based search & replace, and even use Regular Expression with group matching. Follow the steps below to refactor your (or someones else) code.

Steps to fix under Eclipse (PDT):

1) Select via the "File Navigator" your root project folder

Eclipse File Navigator

2) Go to the menu: "Search" -> "File..."

3) Configure as seen in the screenshot below, type into "Containing text:" the following: <\?($|\s+)

Eclipse Search & Replace in Files

4) Click on the button "Replace..."

5) Again, configure as seen in the screenshot. Type into "With:" the following: <?php$1

Eclipse Regular Expresion Text Matches

6) Click on "OK" -> Done! :)

Basically with that regular expression we just used, you are searching for all "<?" occurrences that are followed by either a newline or white space (tabs, spaces). It will then replace it by "<?php" including the same amount of white spaces (or newline) after. This will ensure we are not breaking the code or changing anything else except the PHP short-open tag.

Wednesday, 29 May 2013

Compile tinyproxy as anonymous proxy

Tinyproxy is a small light-weight proxy daemon for Linux environments. Espescially usefull if you have some spare dedicated/virtual servers running with multiple IPs. In this tutorial I will show you how to compile it from source on a Debian server - as the current official .deb package does not allow running it in complete anonymous mode.

Additionally if you don't want to install asciidoc (1GB!!!) you can use the modified patch from my compile steps (original). My version of the patch works with the current stable (1.8.3) version of tinyproxy. Not using the patch might give you following error:

checking for a2x... no
configure: error: Test for asciidoc failed. See the file 'INSTALL' for help.



# cd /usr/src
# wget --no-check-certificate
# wget --no-check-certificate
# tar xvfj tinyproxy-1.8.3.tar.bz2
# cd tinyproxy-1.8.3/
# patch -p1 < ../tinyproxy-1.8.3-no-asciidoc.patch

# ./configure \
--prefix=/usr \
--sysconfdir=/etc \
--localstatedir=/var \
--disable-xtinyproxy \
--disable-filter \
--disable-upstream \
--disable-reverse \

# make && make install


# nano /etc/tinyproxy.conf
User nobody
Group nogroup

Port 8888
BindSame yes

Timeout 600

DefaultErrorFile "/usr/share/tinyproxy/default.html"
StatFile "/usr/share/tinyproxy/stats.html"
Syslog On
LogLevel Error
PidFile "/var/run/tinyproxy/"

MaxClients 100
MinSpareServers 5
MaxSpareServers 20
StartServers 10
MaxRequestsPerChild 10000


DisableViaHeader Yes

ConnectPort 443
ConnectPort 563


# mkdir /var/run/tinyproxy
# chown nobody:nogroup /var/run/tinyproxy/
# tinyproxy

Sunday, 26 May 2013

Nandoo's Banned Commercial

Hilarious commercial was banned by Mugabe and Supporters (even here in South Africa!!) - what a shame Youtube shares my humor.