script to modify DB values filtering by column and rows

One of my client recently needed to implement an application-level encryption for some existing applications.

Unfortunately, the design pattern used for the database layer obliged me to write an independent script to browse the database and encrypt the data for each user using a user-specific two-way encryption key.

So, I ended up writing a simple utility class that applies a user-defined function (modifies) to the defined columns and records (via a filter query) of each database table.

It can be used for any relational database server supported by PDO (MySQL, SQL Server, MySQL, Oracle). I’ve only used it with MySQL but it should work smoothly with all the others.

Db modifier

An example of usage is in the README, hope it’s clear enough.

You can basically use this to mass editing Db records with a function that is NOT easily implementable with a simple MySQL Query, e.g. encrypt the data with a custom PHP function.

Useful options for PHP Command line interface

I find PHP command line interface very useful, for CRON jobs, testing and whenever apache is not necessary. Basic commands here: CLI manual. In this article I’m writing some CL flags and few lines of related functions / PHP code I find useful

  • To make script interactive and read the line from the console
    $var = readline("text");
  • Prints reflection of a function (params and required values). Can be used as a guide
    php -rf json_encode
  • save highlited code into a html file
    php -s file.php > fileWithCodeHighlited.phtml
  • Display a ini setting containing “log_” (e.g. error_log)
    php -i | grep "log_"
  • Run one command (without entering in interactive mode with php -a)
    php -r "echo time();";
  • Set a INI option before executing
    php -d max_execution_time=20 ...
  • Read from STDIN (when piped)
    $handle = fopen('php://stind', 'r');
    while (!feof($handle)) {
    	$line = trim(fgets($handle));
    	if(strlen($line) > 0){
    	echo strrev($line).PHP_EOL;
  • Get options using getopt
    $arg = getopt('ab:c::') // "a" as flag "b" required, "c" optional

Netbeans 7.0 for PHP – Keyboard shortcuts

Netbeans 7.0 PHP IDE EDITORNetbeans PHP 7.0 includes a powerful refactoring tool, support for PHP aliases and PHPDoc generation (not tested yet). I think when xdebug watching feature will become stable, Netbeans  – currently my favourite – will be a fully-featured PHP IDE, probably the best free alternative to Zend Studio.

Netbeans PHP was initially created for Java and afterwards  extended for PHP development, so most of the code OOP features are unknown by PHP community. I haven’t found a page containing a full description of them, so I’ll list here some shortcuts/features I found very useful.

Be smart ! Spend some time learn and experiment them, and you will save lots of time later doing more proper developing, less bug fixing, and less click/search actions. Depending on the tasks, even one of them can considerably increase your performances.

Code navigation
Ctrl+Click on a method/class/function go into the class (if it’s a dependent project/libraries, add those in the project properties)
Alt+Left, Alt+Right navigate back/forward when using the ctrl+click feature
Ctrl+Space when typing code (variable, method, function, class name,…) => show autocompletition with PHP Docs
Ctrl+ [mouse over] shows the PHP Doc of the method (another reason why it’s necessary to write PHP Docs for our methods/classes)
Ctrl+K/L immediate autocomplete with first match (rotate)
Ctrl+R on a selected variable : automatically highlights all the occurrences in the scope and when typing the changes, it changes all of them
Ctrl+R on a method/class :show a dialog with the new name, and refactors all the occurrences around the code (Implemented in NB 7.0). Of course netbeans does a refactoring using names, it cannot consider dynamic instantiation of classes.
Code generator
Alt+Ins : autcompletition of class constructor, setter and getter methods using existing class fields
Lines editing
The following commands are extremely useful when dealing with zend configuration INI files, chain calls to methods or array options)
Ctrl+E delete line
Alt+Shift+Up / Down move the current line or selection one line up/down
Ctrl+Shift+Down duplicate line
Ctrl+G go to line number
Shift+Tab / Tab on group of line, move tabulation of all the selected line to the left/right (obvious but not everyone knows)
Alt+Shift+F Format/Ident the selected code (not code-sniffer-proof but an order of magnitude better than doing it manually)
Windows Management
Ctrl+w close current window
Menu: Windows->Reset Windows restore windows
Shift+Esc or Double click on a file tab: expand to fullscreen (double click again to go back)
Ctrl+Tab shows opened files list (keep Ctrl button pushed and push Tab to rotate)
Ctrl+Page up/down rotate opened file tabs
Code suggestions
Netbeans highlights syntax errors, and also suggest tips on the line number column, use Alt+Enter to see and select automatic fixes
Ctrl+M set/remove a bookmark on the current line (an icon is displayed)
Ctrl+, move to the next bookmark (rotate)


You can see all the shortcut list in Netbeans options, but it’s not clear what they do and most of them are not really useful. An interesting feature is assigning shortcuts for custom commands (e.g. git commit).

I’ll keep this post updated


Eclipse hangs on SVN commit

I’m currently using Eclipse PDT at work (build 20100917-0705). I usually prefer Netbeans as more stable and with more or less the same features, but the application I’m working on contains a huge amount of classes and the eclipse quick search (open resource) is extremely useful to navigate and open files (currently better and faster than Netbeans file search).

The most annoying bug of Eclipse I’ve found happens when the IDE hangs when commiting with SVN. It needs to kill the process, restart, clean svn, reinsert SVN password and commit again (after having lost the SVN comment inserted), enjoyable isn’t it ?

After some googling (read here) it seems that the problem is due to a limit on the console output.

Untick the option Limit Console Output under Eclipse Preferences ->Team -> SVN -> Console and the problem should be solved.

Xdebug debug with Netbeans on Linux

XDebug is a free PHP extension to debug variables of your applications. Combined with Netbeans for PHP debug features (break points, variable watching and flow execution navigation, see screenshot on the left), it allows to have a complete debug environment.

What it’s really interesting is the possibility to place breakpoints in any file, even though not called directly in the local URL. That is required when working with web applications with URL routing/rewriting sudh as MVC applications (Zend Framework, Symfony etc..)  where the PHP code in a controller action does not match with its path in the local URL.

Here are the easy steps to follow in order to configure Xdebug for Linux Ubuntu 10.04 LTS. I’ll write the full commands to make it easier by copy & paste.

-) download the xdebug extension

sudo apt-get install php5-xdebug

-) check where the extension is installed

sudo find /usr/lib/php5 | grep xdebug

-) open the php.ini file (inside apache)

sudo vim /etc/php5/apache2/php.ini

and add these lines at the end of the file (replace with the right module path)


note1: could be elsewhere. use find /usr/lib -name to find the correct path
note2: see other xdebug settings

-) restart apache

sudo /etc/init.d/apache2 restart

-) Netbeans: tools->options->PHP
Tick ”Watches and Baloon Evaluation” (*), Untick “Stop at first Line” (not really useful except when debugging the application bootstrap).

(*): Xdebug is unstable with this options, however I found it very useful and enough stable for my purposes. It’s possible that Netbeans crashes with the current version, restart it in case.

5) Netbeans: right click on the project ->properties->Run configuration -> run as local website. Insert the (virtualhost) local URL. Do not insert any index file if the application uses URL routing, or Netbeans will launch the relative script path in the URL.

6) Netbeans: place your breakpoints by clicking on the line number, then start the debug (Under Debug->debug or Ctrl+F5). Netbeans will open the browser at the local URL, and when reaching the breakpoitns, netbeans will show a red icon on the line.
Depending on the browser, the focus will return to Netbeans (go manually if it does not).
Select the code / expression to watch/evaluate. Netbeans will show a baloon tip with the value. Alternatively, right click and select “new watch” to have the expression and the values appearing in the debug panel.
Use F5 to go to the next breakpoint, F7/F8 to step into/over.

As mentioned, Xdebug is unstable with the option set in the step 4. If watching some expressions crashes netbeans, use the “new watch” option instead.

Dropbox, a swiss-knife tool for developers

dropbox Among the tools I consider useful, Dropbox is one ! It offers a free online storage space of 2 Gb with automatic synchronization with local directory and versioning ! I personally use to keep my digital books, guides, code snippets, software (php, apache, mysql, portable graphic software, IDEs, etc…) between two houses and workplace.

Nice features are the possibility to share contents with other dropbox users and get public links of the selected files (to let anyone to download that file). The second feature can be used to easily download our compressed files (custom libraries, frameworks) into our new hosting (wget via ssh).

Another feature I like is the “immediate upload“: if the same file (thanks probably to a hash of the file) is already on the dropbox servers (as uploaded by another dropbox user I suppose), the file is automatically copied online-to-online without the need to upload it.  Practically, any file downloaded online is probably already on dropbox, of course except personal files and compressed files. When saving the OpenOffice installer, it will be helpful.

Download and enjoy it ! (that’s an affiliate link, if you download from that link, I get  additional 250Mb of space, thanks 🙂 )

Free web-based software for project management

project managementAfter being part of a new team that works with outsourcing team with dynamic allocation of resources (developers) without using a software to plan and schedule the project, I’m now interested in experimenting some free web-based software for project management [project magement wiki].

As expected, the awesome wikipedia contains a page about the software used for project management, as well as a comparison of the  project management software.

Among the open source, web-based, here is the list of the ones that seem more insteresting, with some notes. I’m making this list in order to having them read to try.

See in this article how to manage developers

The power of the Regular expression on the web

Regular Expression are Swiss-knife tools developers should know, extremely useful for  web-purposes and unavoidable when dealing with content scraping.
A single call of a preg_match_all with a medium complex regular expression could save hundres of LOC and time as well as obtaining much more maintainable code.

There  are advanced features like subpatterns and conditional expressions. They might be useful to extract some patterns, task  otherwise not easily feasible.

Pattern Syntax guide –

Useful links about regular expressions