Novell Home

Patch Existing PHP Web Server to Make it NetWare Compatible

Novell Cool Solutions: Tip
By Florian Deckert

Digg This - Slashdot This

Posted: 10 Nov 2005
 

In my last two tips I explained how to replicate a Web server and a MySQL database from a Linux box to a NetWare Box:

But what is going to happened if you replicate a LAMP (Linux Apache MySQL PHP) server to a NAMP server (NetWare ...) ?

Even if NetWare PHP is mostly Linux compatible, there are tiny differences in some functions that unless being patched will not render alike on NetWare:

-Path might be different
from /srv/www/htdocs to vol1:/web

-setlocale needs different localisation
from setlocal (LC_ALL, 'fr');
to 
setlocale(LC_ALL, 'fr-FR');
\$loc = setlocale(LC_TIME, NULL);

-strftime displays accents UTF8 encoded where linux displays ISO9660 encoded accents:
from strftime ("%B")
to utf8_decode(strftime ("%B"))

The following scripts will automatically patch an existing PHP web server to make it NetWare compatible.

So if you want to replicate an LAMP server you can now do the 3 following actions:

  • replicate the web files (see my first tips)
  • replicate the mysql database (see my second tip)
  • patch the PHP files

Example:

#------------------------------------------
# CONSTANTS AND GLOBAL VARIABLES
## AVOID any "(" or ")" in the regular expressions, use instead "."

# web server root folder
my $path = "vol1:/web";

#strings to replace in the PHP code
#use a priority replacement policy, 
#the lower the first number, the first being patched
my %replace = (
	"0/srv/www/htdocs" => 
		"vol1:/web",
		
	"0www.rhorga.sopragroup.com" => 
		"rhorga.corp.sopra",
		
	'0function show_menu_top' => 
		'require_once "redirect.inc.php";function  show_menu_top ',
		
	'0method="POST" action="/templates' => 
		'method="POST" action="/templates',
		
	"0setlocale .LC_ALL, 'fr_FR'.;" => 
		"setlocale(LC_ALL, 'fr-FR');   \$loc = setlocale(LC_TIME, NULL);",
		
	'0ucfirst.strftime ."%B"..' => 
		'ucfirst(utf8_decode(strftime ("%B")))',
);

use vars qw(%replace);
use vars qw($path);

#------------------------------------------
# PROGRAM ENTRANCE

Main;

#------------------------------------------
# FUNCTION   fixFile
#
sub fixFile ($$)
{
 	my ($filename, $hr_doit) = @_; 

	my $newfile = "$filename.new"; 
	my $line;
	my $count=0;
	my @new;
	
	#replace keys in the file
 	open (FILE, $filename);
	while ($line = ) {
		#test each key
		foreach my $ka (sort keys %$hr_doit) {
			my $k = substr ($ka, 1);
			my $v = $hr_doit->{$ka};
			
			if ($line =~ /$k/i) {
				$line =~ s/$k/$v/gi;
				$count++;
			}
		}
		push @new, $line;
	}
	close (FILE);
	
	#delete temp file if no modification
	if ($count > 0) {
		open (FILE_NEW, ">$newfile");
		foreach (@new) {
			print FILE_NEW $_;
		}
		close (FILE_NEW);
		
		my $back = "$filename.bak";
		
		unlink ($back);
		rename ($filename, $back);
		rename ($newfile, $filename);

		print "$filename\n";
	}
}

#------------------------------------------
# FUNCTION   fixFile
#
sub fixFiles ($$$)
{
 	my ($folder, $pattern, $hr_doit) = @_; 

	opendir DIR, $folder;
	my @files = readdir(DIR);
	closedir DIR;
	
	foreach my $file (@files) {
		my $p = "$folder/$file";
		
		if (-d $p) {
			#sub-folder
			fixFiles ($p, $pattern, $hr_doit);
		} else {
			next if ($file =~ /\.bak/);
			if ($file =~ /\.$pattern$/) {
				fixFile ($p, $hr_doit);
			}
		}
	}
}

#------------------------------------------
# FUNCTION   Main
#
sub Main 
{
	#patch *.php and *.inc files
	fixFiles ($path, "php", \%replace);
	fixFiles ($path, "inc", \%replace);
}


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell