Autarchy of the Private Cave

Tiny bits of bioinformatics, [web-]programming etc

    • Archives

    • Recent comments

    fail2ban and Google translate: how to easily cut your WP blog traffic

    14th November 2009

    translate_logofail2ban has a php-url-fopen rule.

    WordPress has a Global Translator plugin, which – among others – uses Google Translate service.

    If someone uses Google Translate (e.g. using Global Translate’s mini-language-flags), and goes back to your blog – that someone might get banned by fail2ban (especially if you have set maxretry to 1), as the referrer will contain the php-URL-fopen attack signature. The bad thing is that you will not realize that until after you check one or several translations yourself, as a random site visitor experiencing the problem is highly unlikely to bother reporting this problem – especially when your blog’s Contact page is also inaccessible.

    Clearly, Google Translate is not the only legitimate service which will trigger that rule.

    Solution: The only solution I have found is to specify the whitelist regex for the php-URL-fopen rule.

    Share

    Posted in *nix, Software, Web, WP PlugIns | No Comments »

    Debugging PHP: Eclipse PDT + XDebug + XDebug helper

    8th June 2009

    Stimulated by a bug in a complex and unfamiliar web PHP application with heaps of custom tweaks by other programmers, I decided to try a more professional approach to PHP programming and debugging than the standard var_dump() and family.

    As a result, I’m now using Eclipse PDT with Xdebug and Xdebug Helper (Firefox extension). Now I don’t understand how I used to debug my PHP programs before!

    After proper configuration (I’m using local Apache, but it is also possible to debug remotely), my work flow is rather simple:

    • use my web-app as usual, e.g. tweaking and testing here and there
    • if something server-side goes wrong: click the XDebug helper icon in Firefox, and perform some server-request action (e.g. load a page)
    • debugging is started in Eclipse PDT, where I can step through the code, set breakpoints, and examine all variables
    • as soon as the problem is fixed – click the XDebug helper icon again to continue using the site normally (w/o invoking the debugger)

    It takes some time to get used to, but then it’s a breeze.

    Some advice:

    • don’t use apt-get/aptitude to install Eclipse; it will be much easier both in the short and long run to use some all-in-one package from the Eclipse PDT site; all you need to do – download, extract, run!
    • before actually starting to do anything, tweak the eclipse.ini file by increasing heap size from 40 MiB (default) to some larger value (I used 128MiB). If you don’t do this, then at some point your debugging will become painfully sloooow, and then you’ll start getting tons of “out of heap memory” errors, each one suggesting that you quit Eclipse immediately
    • install XDebug with apt-get/aptitude – worked perfectly, and there’s /etc/php5/conf.d/xdebug.ini not to mess with php.ini
    • do read XDebug guide for PDT 2.x (I’m assuming you got the 2.x version); it should be the only document you will really need to configure everything

    I only wish Eclipse was faster – that is, written not in Java but e.g. C or C++.

    Share

    Posted in Links, PHP, Programming, Software | 3 Comments »

    Less than an hour of GoDaddy MySQL5 database downtime today

    26th March 2008

    Must have been some maintenance, as I didn’t notice any changes in PHP/MySQL versions since the 7th of March.

    Update: it seems as though since that short MySQL outage everything is faster at GoDaddy shared hosting. Did they upgrade database server(s)? I have no idea, but I like the change.

    Share

    Posted in Misc | 1 Comment »

    Convert MySQL database from one encoding/collation into another

    8th February 2008

    Most frequent use: convert database from latin1_swedish to utf8_general_ci.
    Original script found at: MySQL and UTF-8.

    Update: the original script had an error, it would generate queries likes this one (note the bold part):

    ALTER TABLE `links` CHANGE `link_rel` `link_rel` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT NULL;

    This is clearly wrong syntax (and logic). I fixed this by making comparison to NULL strict (with three equal signs instead of two):

    // Does the field default to null, a string, or nothing?
    if ($row['Default'] === NULL)

    Update 2: based on comment by banesto, I modified the script; now it does not require specifying the from_collation, it’s sufficient to specify to_collation (which will be used for all the fields and tables). The modified code is:

    if ($row['Collation'] == ” || $row['Collation'] == $convert_to)
    continue;

    Update 3: the long-lasting, re-appearing NOT NULL DEFAULT NULL problem is finally fixed.

    Update 4: incorporated Russ’s fix to skip numeric fields (in order to leave autoincrement values intact).

    Here’s the script itself: (to copy-paste: first click the “Plain text” header)
    Read the rest of this entry »

    Share

    Posted in Links, Notepad, PHP, Programming, Web | 60 Comments »

    HTTP caching: universal approach and sample code

    9th December 2006

    As described in my previous post, there are some rather simple mechanisms to enable visitor’s browser to cache content, and avoid unnecessary load on your servers. In this post I’ll take a look at some parts of the practical implementation of the caching mechanism on the server, using PHP.
    Read the rest of this entry »

    Share

    Posted in PHP, Programming, Web | 1 Comment »

    Using PEAR HTTP_Client or HTTP_Request with HTTP proxy

    6th September 2006

    If you happen to write PHP script, which uses either HTTP_Client or its dependancy HTTP_Request from PEAR, and the script is supposed to work through the HTTP proxy – here are the sample settings you need:

    1. $params['proxy_user'] = 'proxy_username';
    2. $params['proxy_pass'] = 'proxy_password';
    3. $params['proxy_host'] = 'proxy_hostname_or_ip';
    4. $params['proxy_port'] = 8080; // 3128, ...
    5.  
    6. // I assume $headers were set somewhere else
    7. $req = &new HTTP_Client($params, $headers);

    If your proxy does not need authorization – just drop the proxy_user and proxy_pass parameters.

    Share

    Posted in how-to, PHP, Programming | 1 Comment »

    Simple substring counting script in Python

    21st June 2006

    Approximately a month ago I endeavoured to use Python as my main shell-scripting language. At that moment, I was already aware of multiple benefits you get when you use Python for scripting:

    • source-level cross-platform scripting: your script will run anywhere, where Python compiles; expanding this statement – your script will run anywhere, where there is a C compiler (needed to build Python itself)
    • high-level language: you can iterate all the lines in a text file with as little as one ‘for’-statement, for example (see the actual example below)
    • simple/minimalist syntax: no curly braces around blocks of statements, no semicolons after each and every line of code, etc. Python at a glance looks much more understandable, than, for example, Perl.
    • the power of C in a language-interpreting system
    • it is interpreted! This gives easyness of debugging: modify, execute, see the trouble – with no compile/link stages
    • and, despite being interpreted, it is fast!

    For the comparison (in speed, memory use, program size) with other computer programming languages, please see the “Computer Language Shootout Benchmarks”. Here I provide the link only to the comparison of Python with Perl and comparison of Python with PHP (which can also be used as shell-scripting language, albeit after some tinkering with settings and stuff)

    Below is an example of the 2-minute script in Python, which counts the number of occurrences of some string in a file.

    1. """Read FILE and count number of occurences of SUBSTR."""
    2. version = 0.01
    3.  
    4. import sys
    5.  
    6. def main():
    7.   from optparse import OptionParser
    8.   opts = OptionParser(usage="%prog [options] FILE SUBSTR",
    9.     version="%prog " + str(version),
    10.     description="Read FILE and count number of occurences of SUBSTR.")
    11.   opts.set_defaults(verbose=False,flush=False)
    12.   opts.add_option("-v", "--verbose", action="store_true", dest="verbose", help="Print every line containing substr [default: %default]")
    13.   opts.add_option("-f", "--flush", action="store_true", dest="flush", help="When verbose, flush every line [default: %default]")
    14.   (options, args) = opts.parse_args()
    15.  
    16.   if len(args) != 2:
    17.     print "Two arguments required for correct processing"
    18.     opts.print_help()
    19.     sys.exit(2)
    20.  
    21.   infile = args[0]
    22.   substr = args[1]
    23.   lines_count = 0
    24.   substr_count = 0
    25.   lines_substr_count = 0
    26.   if options.verbose and not options.flush:
    27.     msg = ""
    28.  
    29.   f = open(infile, 'r')
    30.   for line in f:
    31.     lines_count += 1
    32.     found = line.count(substr)
    33.     substr_count += found
    34.     if found > 0:
    35.       lines_substr_count += 1
    36.       if options.verbose and not options.flush:
    37.         msg += str(found) + ": " + line
    38.       elif options.verbose and options.flush:
    39.         print (str(found) + ": " + line).replace("n","")
    40.  
    41.   f.close()
    42.  
    43.   if options.verbose and not options.flush:
    44.     print msg
    45.   print "Lines read from file: ", str(lines_count)
    46.   print "Lines with substring found: ", str(lines_substr_count)
    47.   print "Total substrings detected: ", str(substr_count)
    48.  
    49.   return
    50.  
    51. if __name__ == "__main__":  main()
    Share

    Posted in Programming, Python | 4 Comments »