PHP DBA extension: DBM handler support in FreeBSD (patch)

Last update: Dec 22, 2008

Introduction:

This patch enables PHP to utilize the FreeBSD DBOPEN(3) built-in library
for building up the DBM handler (DB 1.85 db-format) in DBA extension.

For example, after applying this patch,
you can access '/etc/pwd.db' or '/etc/mail/aliases.db' by PHP script in FreeBSD system.

Related information:

Release history:

v1.0 - [2005/01/24] - Initial public release.
v1.1 - [2005/01/26] - Fix the dba_replace() bug
v1.2 - [2005/02/25] - Using diff to create this patch
v1.3 - [2005/09/13] - Fixed for the PHP 5.0.5 builds
v1.4 - [2006/07/14] - Fixed for the latest ports PHP 5.1.4 & PHP 4.4.2
  [2006/08/15] - Support for PHP 4.4.3
  [2006/08/19] - Support for PHP 4.4.4 & PHP 5.1.5
  [2006/09/01] - Support for PHP 5.1.6
  [2006/11/07] - Support for PHP 5.2.0
  [2007/02/10] - Support for PHP 5.2.1
  [2007/06/23] - Support for PHP 4.4.7 & PHP 5.2.3
  [2007/09/21] - Support for PHP 5.2.4
  [2008/11/11] - Support for PHP 4.4.9 & PHP 5.2.6
  [2008/12/22] - Support for PHP 5.2.8

Downloads

This patch is only available against the following FreeBSD ports:

Installation:

To support DBM handler in FreeBSD for your PHP DBA extension, follow these steps:

(Please retrieve the patch according to your PHP version,
  for example, fetch phpdbm-5.2.8.patch if your PHP version is 5.2.8)


# cd /usr/ports/databases/php5-dba/
# make clean
...

# make patch
===>  Vulnerability check disabled, database not found
===>  Found saved configuration for php5-dba-5.2.8 
===>  Extracting for php5-dba-5.2.8 
=> MD5 Checksum OK for php-5.2.8.tar.bz2.
=> SHA256 Checksum OK for php-5.2.8.tar.bz2.
===>  Patching for php5-dba-5.2.8 
===>  Applying FreeBSD patches for php5-dba-5.2.8 
# fetch http://www.csie.nctu.edu.tw/~cdsheen/codings/phpdbm-5.2.8.patch
phpdbm-5.2.8.patch                            100% of 6159  B   10 MBps
# patch -p0 < phpdbm-5.2.8.patch
Hmm...  Looks like a unified diff to me...
The text leading up to this was:
--------------------------
|--- Makefile.orig      2008-11-11 15:14:48.000000000 +0800
|+++ Makefile   2008-11-11 15:17:42.000000000 +0800
--------------------------
Patching file Makefile using Plan A...
Hunk #1 succeeded at 11.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|--- work/php-5.2.8/ext/dba/config.m4.orig      2008-11-11 15:16:01.000000000 +0800
|+++ work/php-5.2.8/ext/dba/config.m4   2008-11-11 15:17:42.000000000 +0800
--------------------------
Patching file work/php-5.2.8/ext/dba/config.m4 using Plan A...
Hunk #1 succeeded at 473.
Hmm...  The next patch looks like a unified diff to me...
The text leading up to this was:
--------------------------
|--- work/php-5.2.8/ext/dba/dba_dbm.c.orig      2007-12-31 15:20:05.000000000 +0800
|+++ work/php-5.2.8/ext/dba/dba_dbm.c   2008-11-11 15:17:42.000000000 +0800
--------------------------
Patching file work/php-5.2.8/ext/dba/dba_dbm.c using Plan A...
Hunk #1 succeeded at 40.
done
# make
...

# make deinstall
===>  Deinstalling for databases/php5-dba
===>   Deinstalling php5-dba-5.2.8 
# make install
===>  Installing for php5-dba-5.2.8 
===>   php5-dba-5.2.8 depends on file: /usr/local/include/php/main/php.h - found
===>   php5-dba-5.2.8 depends on shared library: db-4.3.0 - found
===>   php5-dba-5.2.8 depends on shared library: gdbm.3 - found
===>   Generating temporary packing list
===>  Checking if databases/php5-dba already installed
===>   Registering installation for php5-dba-5.2.8 
****************************************************************************

The following line has been added to your /usr/local/etc/php/extensions.ini
configuration file to automatically load the installed extension:

extension=dba.so

****************************************************************************
# /usr/local/etc/rc.d/apache2.sh stop
Stopping apache2.
Waiting for PIDS: 71197.
# /usr/local/etc/rc.d/apache2.sh start
Starting apache2.

All commands are collected here: (for PHP 5.2.8)

cd   /usr/ports/databases/php5-dba/
make   clean
make   patch
fetch   http://www.csie.nctu.edu.tw/~cdsheen/codings/phpdbm-5.2.8.patch
patch   -p0   <   phpdbm-5.2.8.patch
make
make   deinstall
make   install
/usr/local/etc/rc.d/apache2.sh   stop
/usr/local/etc/rc.d/apache2.sh   start

Finally, you can check the supported handlers of DBA extension by phpinfo():



You may also check the supported handlers from command line, for example:
# php -i | grep 'Supported handlers'
Supported handlers => dbm cdb cdb_make db4 inifile flatfile

Applications:

After applying this patch, you can now access DBM format db-file by DBA's "dbm" handler,
for example in PHP:
<?
    $db = dba_open( '/etc/mail/aliases.db', 'r', 'dbm' );
    print "alias for root is: " . dba_fetch( "root\0", $db ) . "\n";
    dba_close($db);
?>
By the way, this patch use HASH(3) as the internal db format,
so you must use DB_HASH as the fourth argument of dbopen (C language)
to access the dbm file created by PHP:
db = dbopen("/tmp/test.db", O_RDWR, 0644, DB_HASH, NULL);
Enjoy it!


Shen Cheng-Da ()