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

Automatic caching of Zend_Db (or any object) calls

I’m working on a personal Zend framework application that uses Zend_Db_Table_Abstract classes to get data from the database.
Each class has its own methods (getall, getXX, getYYY) etc…

My need was having all those method calls (around the code) automatically cached.
The first solution in my mind was a general wrapper (container) class that uses magic methods to call and cache methods of the inner class (subclass of Zend_db_Table_Abstract in this case).
The cache ID can be calculated by using class name +function name + arguments (md5+serialize).

public function __call($name, $arguments)
{
   $cacheId = get_class($this->wrappedObject) . $name
                . md5(serialize($arguments));
   // ...
   $callBack = array($this->wrappedObject, $name);
   //... cache logic...
   $ret = call_user_func_array($callBack, $arguments);
   // ...
}

I wanted the wrapper to work in a “invisible mode” in order to keep the IDE autocompletition for the model methods. So I’ve made a static method that instantiates the wrapper on the object passed by reference.

public static function createOn(&$wrappedObject, $lifetime = null)
{
    $wrappedObject = new self($wrappedObject);
    //...
}

Here it is an example of use

$modelCategories = new Application_Model_Categories();
CacheWrapper::createOn($modelCategories /*, 7200*/); //2nd param = lifetime
/* $modelCategories is now and instance of CacheWrapper 
 *  but the IDE still thinks is teh model */
$data = $modelCategories ->getAll(); //cached !
$data2 = $modelCategories ->getAll(1,2); //cached !

Cache and expiring time (value “randomized” by 10%) can be set using a static method (or taken from registry if existing).

EDIT 3/9/2013
source code moved to github: https://github.com/elvisciotti/CacheWrapper

Script to download Youtube videos and merge into an AVI file

youtube downloader mergerI had the need to watch a TV program on youtube splitted into 12 videos when I was on holiday at home. Due to my poor connection there, I decided to obtain the merged version in AVI format and watch it on the TV screen via HD player. I did not found any software/script that suited me on linux (apparently on windows nothing similar exists neither) so my developer instinct made me developing something appropriate … and made me go to sleep late as usual :).

I’ve eventually made a PHP script that downloads all the youtube videos (using the already existing python script youtube-dl) from the URLs specified in the Command line, then convert them to AVI (pipeline process with more than one file) and merge together into a final AVI file (FLV of different framerates and size are supported, using mencoder). The script is tested on linux ubuntu 10 (mencoder obtainable from repositories).

Download here (public SVN): http://svn.phpntips.com/projects/youtube-merge/

Example of use with 2 short videos:

youtube-multi "http://www.youtube.com/watch?v=MaZRgTJlN2Y&NR=1" 
 "http://www.youtube.com/watch?v=na_3XoysbCk"

See other options and instruction inside the file (max number of simultaneous processes, AVI bitrate etc..).

copy the file into /usr/bin to have them available evertywhere

PHP float bug with 2.2250738585072011e-308

I’ve recently been informed that a serious PHP bug has been discovered.
Basically, when PHP convert the value 2.2250738585072011e-308 (any manipulation such as conversion to int or sum with another value) it hangs and stop working.

See discussions and more info
http://bugs.php.net/bug.php?id=53632
http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
http://news.ycombinator.com/item?id=2066084

Consequences are trivial to foresee: a site can be hacked by simply passing the value in the URL or by submitting it in a form if the (very frequent) operations above are executed.

It seems that the patch has been committed to the PHP’s SVN but the new release to download is not yet available.

I’ve googled a solution, and at first glance the following one seems OK to me
http://www.aircraft24.com/en/info/php-float-dos-quickfix.htm

code:


// *************************************************************
// QUICK FIX / WORKAROUND FOR PHP FLOATING POINT DOS ATTACK
// provided by AirCraft24.com / www.aircraft24.com
// version 1.5, released 2011-01-06 14:00 GMT+1
// *************************************************************

if (strstr(str_replace('.','',serialize($_REQUEST)), '22250738585072011')) 
{
  header('Status: 422 Unprocessable Entity');
  die ('Script interrupted due to floating point DoS attack.');
}

// *************************************************************
// END QUICK FIX / WORKAROUND FOR PHP FLOATING POINT DOS ATTACK	
// *************************************************************

Post to wordpress using Zend_XmlRpc Client

WordPress supports metaWeblob API standard to post data into the blog from an external service.

WordPress contains xmlrpc.php [source], that is basically a XML RPC server that can be called from a client that posts XML RPC messages.

See here the wordpress codex guide.

Sending XML message is a task very easy with zend framework xml rpc libraries. It automatically creates data types using a straightforward conversion, e.g: php array->xmlrpc array and php associative array->xmlrpc struct.

Here is an example to post data to a wordpress blog

require 'Zend/XmlRpc/Client.php';
$client = new Zend_XmlRpc_Client('http://www.softwareengineeringsolutions.co.uk/xmlrpc.php');
$res = $client->call('metaWeblog.newPost',array(
        0, //blog_id, keep 0 for standard wordpress
        'xx', //username
        'yy', //pass
        array(
            //'post_type' => 'post', //not necessary, 'post' is default
            'title'=>'test xmlrpc',
            'description'=>'description of post',
            'mt_keywords'=>'key1, key2',
        ),
        true //publish
    ))

To see the detailed options, see the API documentation or see the xmlrpc.php source directly to better understand the data supported by wordpress (more than the standard metaBlog API data)

Using PHP Closures to get cached object with one call

Getting caching objects if a fairly frequent task when dealing with performative web applications.

Standard approach

if (cache object is valid)  {
return cached object
} else {
get fresh object (*)
save object into cache
return object
}

I don’t like that approach as there are a lots of LOC that wraps the only meaningful code (to get the fresh object).
Continue reading

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 – php.net

Useful links about regular expressions

Class flags with bitwise operators

bitwise operatorsThere is a group of  PHP features that most of developers don’t usually use: bitwise operators.

These operators are widely used in the C language to make low-level applications (device drivers, protocols etc..). They are very useful when used in OOP developing as a flags for options in functions, classes and general settings.

Brief overview

There are 6 bitwise operators in PHP: And (&), Or (|), Xor (^), not (~), shift left(<<) and right(>>). I won’t to explain here their semantic as it’s not the purpose of this post, read the official documentation [link] if you don’t know how the bitwise operator work.

Use of as a flags : error_reporting

When an array of N binary (on/off) options is needed, the idea is using bitwise operators to store all the options within one integer value, that is a string (or array or word) of bits.
Normally, N constants are set to the values 1,2,4,8, … ,2^(N-1) so that with bitwise operator it’s possible to combine them and obtain any possible configuration of the string.
Continue reading

Simple PHP script to edit front end components

I’ve recently built a very simple web site. The only dynamic requirement was having some parts of the front end editable (WYSIWYG) by an administration area (user and password). Basically, a very simple basic CMS for one user.

Solution
A MySQL database is not needed, saving to files is much more appropriate for these requirements. Also SQLite is not necessary, as we just want to do only basic operations.
To do it, I’ve used one of my scripts that meets all of those requirements using only one file (the script, to place in any position, better a separate “admin” folder) + a config file (basic options: array with the list of the frontend component and their relative paths, user and password to login) + FCK editor files.

The script (protected with a simple session control + form to login) basically lists the components and allows to click on them and change the text inside using FCK editor (free WYSIWYG html editor). Also a simple backup system is provided, by keeping/restoring the previous version saved.

It’s quite easy to implement a script like that.
To whom may be interested in my version (<200 LOC, made long time ago, so PHP4 compliant), let me know.