28
Aug 14

How to modify columns in a DB with a script

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.

Repository:
Db modifier https://github.com/elvisciotti/DbModifier

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.


27
Nov 13

Recent code

I haven’t posted for a while. The main reason is that almost any technical solution is solved on stackoverflow, and when I find one, I tend to reply in there, in order to contribute to the community. Besides, I’m now actively developing a project (administrative software), that we started developing from the ground at LexisNexis, excellently backed by clear requirements and SCRUM. As a result, most of my efforts and time go into that project, constantly refactoring and improving the design.

You can find some of my recent snippets of code are on github:

  • cache wrapper utility with phpunit test (exhumed from an old post)
  • JQuery plugin to make inputbox with spinner buttons (click and hold with different increase/decrease rate)
  • other small JS scrips here and here

30
Sep 12

How to create a wordpress plugin, a minimal example

If you’ve ever wondered how wordpress plugins work, you’ll be surprised to see how simple they are.

Let’s create a plugin (I’ll call it “capitalizer”) that just modifies the text of the blog posts.
Continue reading →


17
Dec 11

PHP Command line interface, set and read ENV variables

When a variable on the console is set

export VARIABLE=VALUE;

You can get it from PHP Command line (CLI) using

getenv(‘VARIABLE’)

 

Example

#script.php
<?php
var_dump(getenv('VAR'));
?>

From command line

php -f script.php //bool(false)
export VAR=a; php -f script.php //string(1) "a"

 


01
Nov 11

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 php.net 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;
    	}
    }
    fclose($handle);
  • Get options using getopt
    $arg = getopt('ab:c::') // "a" as flag "b" required, "c" optional

14
Jun 11

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
Autocompletition
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)
Refactoring
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
Format
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
Bookmark
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

 


12
Jan 11

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.


19
Sep 10

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)

[xdebug]
;zend_extension=/usr/lib/php5/20090626+lfs/xdebug.so
zend_extension=/usr/lib/php5/20090626/xdebug.so
xdebug.remote_enable=on
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=localhost
xdebug.remote_port=9000

note1: xdebug.so could be elsewhere. use find /usr/lib -name xdebug.so 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.


01
Aug 10

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 :) )


27
Jul 10

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