components/php-5_3/Solaris/configure-sun-webserver
changeset 846 fe258446a1ae
equal deleted inserted replaced
845:f38158deaa77 846:fe258446a1ae
       
     1 #!/bin/bash
       
     2 #
       
     3 # CDDL HEADER START
       
     4 #
       
     5 # The contents of this file are subject to the terms of the
       
     6 # Common Development and Distribution License (the "License").
       
     7 # You may not use this file except in compliance with the License.
       
     8 #
       
     9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
    10 # or http://www.opensolaris.org/os/licensing.
       
    11 # See the License for the specific language governing permissions
       
    12 # and limitations under the License.
       
    13 #
       
    14 # When distributing Covered Code, include this CDDL HEADER in each
       
    15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    16 # If applicable, add the following below this CDDL HEADER, with the
       
    17 # fields enclosed by brackets "[]" replaced with your own identifying
       
    18 # information: Portions Copyright [yyyy] [name of copyright owner]
       
    19 #
       
    20 # CDDL HEADER END
       
    21 #
       
    22 # Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
       
    23 #
       
    24 
       
    25 # Helper functions
       
    26 function usage {
       
    27     echo ""
       
    28     echo "Usage: This script will update a given Web Server 7 instance's configuration"
       
    29     echo "       files to be able to execute PHP scripts."
       
    30     echo ""
       
    31     echo "This script recognizes following arguments:"
       
    32     echo " --installroot  : Top level Sun Web Server 7 installation location. "
       
    33     echo ""
       
    34     echo " --instancename : Name of Web Server instance (https-php) which should"
       
    35     echo "                  be configured to execute PHP scripts. "
       
    36     echo ""
       
    37     echo " --sapi         : How should PHP runtime be loaded within Web Server 7"
       
    38     echo "                  fastcgi (Default) or NSAPI (Optional). "
       
    39     echo ""
       
    40     exit 1
       
    41 }
       
    42 
       
    43 function parse_arguments
       
    44 {
       
    45     until [ $# -eq 0 ]
       
    46     do 
       
    47         cur_arg=$1
       
    48         case $cur_arg in 
       
    49             --installroot=*)
       
    50                 install_root="`echo $cur_arg | cut -d= -f2-`"
       
    51                 ;;
       
    52             --instancename=*)
       
    53                 instance_name="`echo $cur_arg | cut -d= -f2-`"
       
    54                 ;;
       
    55             --sapi=*)
       
    56                 sapi="`echo $cur_arg | cut -d= -f2-`"
       
    57                 ;;
       
    58             *)
       
    59                 usage
       
    60                 ;;
       
    61         esac
       
    62         shift;
       
    63     done
       
    64 }
       
    65 
       
    66 function try_interactive
       
    67 {
       
    68     # Get user response on Web Server 7 installation location and instance name.
       
    69     echo -n "Enter your Web Server installation location : "
       
    70     read input
       
    71     if [ -n "$input" ]; then
       
    72         install_root="$input"
       
    73     fi
       
    74 
       
    75     echo -n "Enter your Web Server instance name to configure with PHP runtime: "
       
    76     read input
       
    77     if [ -n "$input" ]; then
       
    78         instance_name="$input"
       
    79     fi
       
    80 
       
    81 	while [ 0 ]; do
       
    82 		echo -n "How you would like Sun Web Server 7 to load PHP engine (fastcgi|nsapi) [fastcgi]: "
       
    83 		read input
       
    84 		if [ -z "$input" ]; then
       
    85 			sapi="fastcgi"
       
    86 		else
       
    87 			sapi=$input
       
    88 		fi
       
    89 		if [ -n "$sapi" ] && [ $sapi = "fastcgi" -o $sapi = "nsapi" ]; then
       
    90 			break
       
    91 		fi
       
    92 	done
       
    93     return 1;
       
    94 }
       
    95 
       
    96 function validate_arguments
       
    97 {
       
    98     if [ ! -d "$install_root" ] || [ ! -x "$install_root/lib/webservd" ]; then
       
    99         echo ""
       
   100         echo "Warning: Unable to find valid Web Server installation under $install_root"
       
   101         echo "Please try again by providing a valid Web Server 7 installation location."
       
   102         usage
       
   103     fi
       
   104 
       
   105     if [ ! -d "$install_root" ] || [ ! -x "$install_root/lib/webservd" ]; then
       
   106         echo ""
       
   107         echo "Warning: Unable to find valid Web Server installation under $install_root"
       
   108         echo "Please try again by providing a valid Web Server 7 installation location."
       
   109         usage
       
   110     fi
       
   111 
       
   112     if [ -n "$sapi" ] && [ $sapi != "fastcgi" -a $sapi != "nsapi" ]; then
       
   113         echo ""
       
   114         echo "Error: Invalid SAPI option is provided.Valid SAPI argument is either 'fastcgi' or 'nsapi'"
       
   115         echo "Please try again by providing a valid SAPI as argument."
       
   116         usage
       
   117     fi
       
   118 
       
   119     return 1;
       
   120 }
       
   121 
       
   122 function generate_tempfile
       
   123 {
       
   124     template="tmp.XXXXXXXXXX";
       
   125     if [ -x "/bin/mktemp" ]; then
       
   126         temp_file="`/bin/mktemp`"
       
   127     elif [ -x "/usr/bin/mktemp" ]; then
       
   128         temp_file="`/usr/bin/mktemp`"
       
   129     fi
       
   130     if [ ! -f $temp_file ]; then
       
   131         temp_file="/tmp/ws7_php_configure.$$"
       
   132         touch $temp_file 
       
   133         chmod 600 $temp_file
       
   134     fi
       
   135 
       
   136     return 1;
       
   137 }
       
   138 
       
   139 function generate_configure
       
   140 {
       
   141 	if [ "$OSNAME" = "SunOS" ]; then
       
   142 		tail +${perl_start_line} $PROGRAM_NAME > $temp_file
       
   143 	elif [ "$OSNAME" = "Linux" ]; then
       
   144         start_line=${perl_start_line}
       
   145         total_line=`wc -l $PROGRAM_NAME | awk '{print $1}' 2>/dev/null`
       
   146         few_lines=$(($total_line - $start_line))
       
   147 		tail -${few_lines} $PROGRAM_NAME > $temp_file
       
   148 	fi
       
   149     chmod 500 $temp_file
       
   150 }
       
   151 
       
   152 function invoke_configure
       
   153 {
       
   154     # Setup environment 
       
   155     if [ -f "$install_root/lib/wsenv" ]; then
       
   156         source  $install_root/lib/wsenv
       
   157         if [ ! -d "${WS_INSTANCEROOT}/${instance_name}" ]; then
       
   158             echo "Warning: Unable to find instance:'$instance_name' under $WS_INSTANCEROOT"
       
   159             echo "         Please try again by providing a valid instance name."
       
   160             exit 1;
       
   161         fi
       
   162     fi
       
   163 
       
   164 	# Special case, if it is NSAPI
       
   165 	if [ "$sapi" = "nsapi" ]; then
       
   166         mkdir -p "$install_root/plugins/webstack-php"
       
   167         ln -sf "<<INSTALL_DIR>>/nsapi/libphp5.so" "$install_root/plugins/webstack-php/libphp5.so"
       
   168 	fi
       
   169 
       
   170     # Invoke script to configure PHP runtime.
       
   171 	if [ "$sapi" = "nsapi" ]; then
       
   172 		PHPROOT="<<INSTALL_DIR>>"
       
   173 		PHPCONFROOT="<<NSAPI_CONF_DIR>>"
       
   174 		PHPCONFROOT_SCANDIR="<<ZTS_MODULES_CONF_DIR>>"
       
   175 	else
       
   176 		PHPROOT="<<INSTALL_DIR>>"
       
   177 		PHPCONFROOT="<<CONF_DIR>>"
       
   178 		PHPCONFROOT_SCANDIR="<<MODULES_CONF_DIR>>"
       
   179 	fi
       
   180 
       
   181     ${WS_PERL}/perl -I ${WS_PERL} -I ${WS_PERL}/lib -I ${WS_PERL}/lib/site_perl $temp_file  \
       
   182 	-installroot=${WS_INSTALLROOT} -instanceroot=${WS_INSTANCEROOT} -instancename=$instance_name \
       
   183 	-sapi=$sapi -phproot="${PHPROOT}" \
       
   184 	-phpconfroot="${PHPCONFROOT}" -phpmodulesconfroot="${PHPCONFROOT_SCANDIR}"
       
   185 
       
   186 	status=$?
       
   187 	if [ $status -eq 0 ]; then
       
   188 		if [ -f $temp_file ]; then
       
   189 			rm -f $temp_file
       
   190 		fi
       
   191     elif [ $status -ne 0 ]; then
       
   192         echo "Unable to successfully setup PHP within Web Server 7"
       
   193         exit 1
       
   194     fi
       
   195 }
       
   196 
       
   197 #-------------------------------------------------------------------------------
       
   198 #####
       
   199 # Main
       
   200 #####
       
   201 PATH=/bin:/usr/bin:/usr/gnu/bin:/usr/sfw/bin
       
   202 export PATH
       
   203 
       
   204 # Global variables.
       
   205 OSNAME="`uname -s`"
       
   206 PHPROOT="<<INSTALL_DIR>>"
       
   207 PHPCONFROOT="<<CONF_DIR>>"
       
   208 PHPCONFROOT_SCANDIR="<<MODULES_CONF_DIR>>"
       
   209 PROGRAM_NAME="$0"
       
   210 install_root=""
       
   211 instance_name=""
       
   212 sapi="fastcgi"
       
   213 temp_file=""
       
   214 
       
   215 # This below line need to point to the start of embedded perl script.
       
   216 perl_start_line=245
       
   217 
       
   218 echo "This script will update a given Web Server 7 instance's configuration"
       
   219 echo "files to be able to execute PHP scripts."
       
   220 echo ""
       
   221 
       
   222 # Verify if the program is called with necessary arguments. 
       
   223 if [ -n "$1" ]; then
       
   224     parse_arguments $@;
       
   225 else
       
   226     # Invoked with no arguments. Try interactive.
       
   227     try_interactive
       
   228 fi
       
   229 
       
   230 validate_arguments
       
   231 
       
   232 generate_tempfile
       
   233 
       
   234 generate_configure
       
   235 
       
   236 invoke_configure
       
   237 
       
   238 exit 0
       
   239 
       
   240 #---------------------EOF-------------------------------------------------------
       
   241 
       
   242 # Helper Script to configure PHP runtime environment within Web Server 7
       
   243 
       
   244 use XML::Simple;
       
   245 use File::Basename;
       
   246 use English;
       
   247 use strict;
       
   248 
       
   249 our $INSTANCE_ROOT = undef;
       
   250 our $INSTALL_ROOT = undef;
       
   251 our $INSTANCE_NAME = undef;
       
   252 our $PHP_ROOT = "";
       
   253 our $PHP_CONF_ROOT = "";
       
   254 our $PHP_MODULES_CONF_ROOT = "";
       
   255 our $SAPI = undef;
       
   256 our $MIME_TYPES_FILES = [];
       
   257 our $OBJ_CONF_FILES = [];
       
   258 our $SERVER_64BIT_MODE = undef;
       
   259 
       
   260 my $phpSoName;
       
   261 if (isWindows()) {
       
   262    $phpSoName = "php5nsapi.dll";
       
   263 } else {
       
   264    $phpSoName = "libphp5.so";
       
   265 }
       
   266 
       
   267 my $fastCGISoName;
       
   268 if (isWindows()) {
       
   269     $fastCGISoName = "fastcgi.dll";
       
   270 } else {
       
   271     $fastCGISoName = "libfastcgi.so";
       
   272 }
       
   273 
       
   274 main();
       
   275 
       
   276 sub main {
       
   277     getCommandOptions();
       
   278     processServerXml();
       
   279     checkFilesWritable();
       
   280     processMagnusConf();
       
   281     processObjConf();
       
   282     processMimeTypes();
       
   283     printResult();
       
   284 }
       
   285 
       
   286 # -----------------------------------------------------------------------------
       
   287 # getCommandOptions
       
   288 # Process the command line options
       
   289 # -----------------------------------------------------------------------------
       
   290 
       
   291 sub getCommandOptions {
       
   292     for (my $counter=0; $#ARGV >= $counter; $counter++) {
       
   293         my $argument    = $ARGV[$counter];
       
   294         
       
   295         if ($argument =~ /^-installroot=/i) {
       
   296             $INSTALL_ROOT = substr($argument, length("-installroot="));
       
   297         }        
       
   298         if ($argument =~ /^-instanceroot=/i) {
       
   299             $INSTANCE_ROOT = substr($argument, length("-instanceroot="));
       
   300         }
       
   301         if ($argument =~ /^-phproot=/i) {
       
   302             $PHP_ROOT = substr($argument, length("-phproot="));
       
   303         }
       
   304         if ($argument =~ /^-phpconfroot=/i) {
       
   305             $PHP_CONF_ROOT = substr($argument, length("-phpconfroot="));
       
   306         }
       
   307         if ($argument =~ /^-phpmodulesconfroot=/i) {
       
   308             $PHP_MODULES_CONF_ROOT = substr($argument, length("-phpmodulesconfroot="));
       
   309         }
       
   310         if ($argument =~ /^-instancename=/i) {
       
   311             $INSTANCE_NAME = substr($argument, length("-instancename="));
       
   312         }
       
   313         if ($argument =~ /^-sapi=/i) {
       
   314             $SAPI = substr($argument, length("-sapi="));
       
   315         }
       
   316     }
       
   317     if ((!defined $PHP_CONF_ROOT) || ($PHP_CONF_ROOT eq "")) {
       
   318         $PHP_CONF_ROOT = $PHP_ROOT;
       
   319     }
       
   320     
       
   321     if ((not defined $INSTANCE_NAME) or ($INSTANCE_NAME !~ m/\S+/)) {
       
   322         printUsage();
       
   323     }
       
   324     
       
   325     exit 1 unless defined isValidFile("$INSTANCE_ROOT/$INSTANCE_NAME/config");
       
   326 }
       
   327 
       
   328 # -----------------------------------------------------------------------------
       
   329 # migrateObjConf
       
   330 # Migrate obj.conf file to php 1.1
       
   331 # -----------------------------------------------------------------------------
       
   332 
       
   333 sub migrateObjConf {
       
   334     my $objConfFile = shift;
       
   335     my $pContents = shift;
       
   336     my $tmpObjConfStatus = 1;
       
   337 
       
   338     return undef unless ($objConfFile);
       
   339     if ((not -f $objConfFile or ref($pContents) != "ARRAY")) {
       
   340         return undef;
       
   341     }
       
   342 
       
   343     my $tmpObjConfFile = "$objConfFile"."tmp";
       
   344     my $update_reqd = undef;
       
   345     local *TMPOBJ;
       
   346 
       
   347     my $newLibPath = "$PHP_ROOT/lib";
       
   348     my $newLibPath64 = "$PHP_ROOT/lib/64";
       
   349 
       
   350     for (my $i = 0; $i < $#{@{$pContents}}; $i++) {
       
   351         my $pLine = \$pContents->[$i];
       
   352         next if ($$pLine =~ /^\#/); #ignore comments;
       
   353         next if (isWindows());
       
   354         if ($$pLine =~ m@(.*\s+app-path=['"])(\S+)\s*(['"].*)@) {
       
   355             my ($tmp, $tmp1, $tmp2, $tmp3);
       
   356             $tmp1 = $1; $tmp2 = $2; $tmp3 = $3;
       
   357 
       
   358             $tmp2 =~ s@$PHP_ROOT/bin/php@$PHP_ROOT/bin/php-cgi@;
       
   359             $tmp = $tmp1.$tmp2.$tmp3."\n";
       
   360             $pContents->[$i] = $tmp;
       
   361             $update_reqd = 1;
       
   362         }
       
   363         elsif ($$pLine =~ m@(.*\s+app-env=['"])(\S+)(=)(\S+)(['"].*)@) {
       
   364             my ($tmp, $tmp1, $tmp2, $tmp3, $tmp4, $tmp5);
       
   365             $tmp1 = $1; $tmp2 = $2; $tmp3 = $3; $tmp4 = $4; $tmp5 = $5;
       
   366 
       
   367             if (($tmp2 =~ m@LD_LIBRARY_PATH@) or ($tmp2 =~ m@LD_LIBRARY_PATH_64@)) {
       
   368                 if ($tmp4 =~ m@$PHP_ROOT/64@) {
       
   369                     $tmp4 =~ s@$PHP_ROOT/64@$newLibPath64@g;
       
   370                 }
       
   371                 elsif ($tmp4 =~ m@$PHP_ROOT/64([:].*)@) {
       
   372                     $tmp4 =~ s@$PHP_ROOT/64(:.*)@$newLibPath64$1@g;
       
   373                 } 
       
   374                 elsif ($tmp4 =~ m@$PHP_ROOT([:].*)@) {
       
   375                     $tmp4 =~ s@$PHP_ROOT(:.*)@$newLibPath$1@g;
       
   376                 }
       
   377                 elsif ($tmp4 =~ m@$PHP_ROOT@) {
       
   378                     $tmp4 =~ s@$PHP_ROOT@$newLibPath@g;
       
   379                 }
       
   380             }
       
   381             elsif ($tmp2 =~ m@PHP_FCGI_MAX_REQUEST@) {
       
   382               $tmp2 =~ s@PHP_FCGI_MAX_REQUEST@PHP_FCGI_MAX_REQUESTS@g;
       
   383               $tmp4 =~ s@200@2000@;
       
   384             }
       
   385             $tmp = $tmp1.$tmp2.$tmp3.$tmp4.$tmp5."\n";
       
   386             $pContents->[$i] = $tmp;
       
   387             $update_reqd = 1;
       
   388         }
       
   389     }
       
   390 
       
   391     if ($update_reqd) {
       
   392         open(TMPOBJ,">$tmpObjConfFile") or $tmpObjConfStatus = undef;
       
   393         if (defined $tmpObjConfStatus) {                
       
   394             for (my $i = 0; $i < $#{@{$pContents}}; $i++) {
       
   395                 my $line = $pContents->[$i];
       
   396                 print TMPOBJ $line;
       
   397             }
       
   398             print "UPDATED: $objConfFile \n";
       
   399             close(TMPOBJ);
       
   400             unlink("$objConfFile");
       
   401             rename("$tmpObjConfFile", "$objConfFile");
       
   402             chmod(0600, "$objConfFile");
       
   403             return 2;
       
   404         }
       
   405     }
       
   406 
       
   407     return 1;
       
   408 }
       
   409 
       
   410 # -----------------------------------------------------------------------------
       
   411 # processServerXml
       
   412 # Parse the server.xml and get all the mime files and object files
       
   413 # -----------------------------------------------------------------------------
       
   414 
       
   415 sub processServerXml {
       
   416     my $file = undef;
       
   417     my $serverXml = "$INSTANCE_ROOT/$INSTANCE_NAME/config/server.xml";
       
   418     
       
   419     my $config = eval{XMLin("$serverXml", forcearray=>1, keyattr=>[])};
       
   420     if ($@) {
       
   421         print("\nERROR: Problem parsing the $serverXml. Not a valid xml file. \n\n");
       
   422         exit 1;
       
   423     }
       
   424     
       
   425     #get the server level mime file
       
   426     $file = $config->{"mime-file"}->[0];
       
   427     if (defined $file) {
       
   428         $file = getValidAbsoluteFilePath($file);
       
   429         if (defined $file) {
       
   430             push (@$MIME_TYPES_FILES, $file); 
       
   431         }
       
   432     }        
       
   433 
       
   434     # get the server platform mode
       
   435     my $mode = $config->{"platform"}->[0];
       
   436     if (defined $mode) {
       
   437         if ($mode == "64") {
       
   438             $SERVER_64BIT_MODE = "64";
       
   439         }
       
   440     }
       
   441         
       
   442     for (my $vsCounter = 0; ${config}->{"virtual-server"}->[$vsCounter]; $vsCounter++) {
       
   443         my $virutalServerElement = ${config}->{"virtual-server"}->[$vsCounter];
       
   444         #get the virtual server level mime files
       
   445         for (my $mimeTypescounter = 0; ${virutalServerElement}->{"mime-file"}->[$mimeTypescounter]; $mimeTypescounter++) {
       
   446             $file = ${virutalServerElement}->{"mime-file"}->[$mimeTypescounter];
       
   447             $file = getValidAbsoluteFilePath($file);
       
   448             if (defined $file) {
       
   449                 push (@$MIME_TYPES_FILES, $file);
       
   450             }
       
   451         }
       
   452         #get the virtual server level object files       
       
   453         for (my $objectFilecounter = 0; ${virutalServerElement}->{"object-file"}->[$objectFilecounter]; $objectFilecounter++) {
       
   454             $file = ${virutalServerElement}->{"object-file"}->[$objectFilecounter];
       
   455             $file = getValidAbsoluteFilePath($file);
       
   456             if (defined $file) {
       
   457                 push (@$OBJ_CONF_FILES, $file);
       
   458             }
       
   459         }        
       
   460     }
       
   461     
       
   462     #Default is mime.types
       
   463     if (@$MIME_TYPES_FILES < 1) {
       
   464         push (@$MIME_TYPES_FILES, "$INSTANCE_ROOT/$INSTANCE_NAME/config/mime.types");
       
   465     }
       
   466     
       
   467     #Default is obj.conf
       
   468     if (@$OBJ_CONF_FILES < 1) {
       
   469         push (@$OBJ_CONF_FILES, "$INSTANCE_ROOT/$INSTANCE_NAME/config/obj.conf");
       
   470     }    
       
   471 }
       
   472 
       
   473 # -----------------------------------------------------------------------------
       
   474 # processMagnusConf
       
   475 # Append the MAGNUS_CONF_APPEND_STRING value at the end of magnus.conf file.
       
   476 # -----------------------------------------------------------------------------
       
   477 
       
   478 sub processMagnusConf {
       
   479     my $magnusConfFile = "$INSTANCE_ROOT/$INSTANCE_NAME/config/magnus.conf";
       
   480     my $magnusConfStatus = 1;
       
   481 
       
   482     if (defined isValidFile($magnusConfFile)) 
       
   483     {
       
   484         # Get the current File Stat.
       
   485         my @statInfo = stat $magnusConfFile;
       
   486 
       
   487         # Verify if the changes already exist.
       
   488         if (open(MAGNUS_R,"<$magnusConfFile")) {
       
   489             my @contents = <MAGNUS_R>;
       
   490             foreach (@contents) {
       
   491                 next if (/^\#/); #ignore comments;
       
   492                 if ((isNSAPI()) and (/shlib(.*)$phpSoName(.*)/g)) {
       
   493                     close(MAGNUS_R);
       
   494                     return 1;
       
   495                 } elsif ((not isNSAPI()) and (/shlib(.*)$fastCGISoName(.*)/g)) {
       
   496                     close(MAGNUS_R);
       
   497                     return 1;
       
   498                 }
       
   499             }
       
   500             close(MAGNUS_R);
       
   501         }
       
   502         open(MAGNUS,">>$magnusConfFile") or $magnusConfStatus = 0;
       
   503 
       
   504         if ($magnusConfStatus == 1) {
       
   505             addToMagnusConf(\*MAGNUS);
       
   506             print "\n\nUPDATED: $magnusConfFile \n";
       
   507         } else {
       
   508             print "\nERROR: Unable to write $magnusConfFile. \n\n";
       
   509             close(MAGNUS);
       
   510             exit 1;
       
   511         }
       
   512         close(MAGNUS);
       
   513         chown $statInfo[4], $statInfo[5], $magnusConfFile;
       
   514     }
       
   515 }
       
   516 
       
   517 # -----------------------------------------------------------------------------
       
   518 # processObjConf
       
   519 # Append the OBJ_CONF_APPEND_STRING value after the <object name="default" 
       
   520 # directive in all object files.
       
   521 # -----------------------------------------------------------------------------
       
   522 
       
   523 sub processObjConf {
       
   524     while(scalar(@$OBJ_CONF_FILES) > 0) {
       
   525         my $objConfFile = pop(@$OBJ_CONF_FILES);
       
   526         my $objConfStatus = 1;
       
   527         my $tmpObjConfStatus = 1;
       
   528 
       
   529         if (defined isValidFile($objConfFile)) 
       
   530         {
       
   531             # Get the current File Stat.
       
   532             my @statInfo = stat $objConfFile;
       
   533 
       
   534             # Verify if the changes already exist.
       
   535             open(OBJ,"<$objConfFile") or $objConfStatus = undef;
       
   536             if (defined $objConfStatus) {
       
   537                 my @lines = <OBJ>;
       
   538                 my $escape_path = $PHP_ROOT;
       
   539                 $escape_path =~ s/\/(\w)/\\\/$1/g;
       
   540                 $escape_path =~ s/\\(\w)/\/$1/g;
       
   541                 my $contents = join("",@lines);
       
   542                 my $already_configured = undef;
       
   543                 if ((isNSAPI()) and ($contents =~ m/
       
   544                         \s*Service\s+type="magnus-internal\/php"\s+fn="php5_execute"
       
   545                         /mx)) {
       
   546                     close(OBJ);
       
   547                     $already_configured = 1;
       
   548                 }
       
   549                 if ((not isNSAPI()) and (($contents =~ m/
       
   550                         \s*Service\s+type=[\"]magnus-internal\/php[\"]
       
   551                         [\r\n]+
       
   552                         \s*fn=[\"]responder-fastcgi[\"]
       
   553                         [\r\n]+
       
   554                         \s*app-path=[\"](.*)[\"]
       
   555                         [\r\n]+
       
   556                         /mx) and ($1 =~ m@$escape_path@))) {
       
   557                     close(OBJ);
       
   558                     $already_configured = 1;
       
   559                     # migrate existing obj.conf configurations.
       
   560                     &migrateObjConf($objConfFile, \@lines);
       
   561                 }
       
   562 
       
   563                 next if ($already_configured);
       
   564 
       
   565                 # Create a new obj.conf
       
   566                 my $tmpObjConfFile = "$objConfFile"."tmp";
       
   567                 open(TMPOBJ,">$tmpObjConfFile") or $tmpObjConfStatus = undef;
       
   568                 if (defined $tmpObjConfStatus) {                
       
   569                     if (@lines) {                
       
   570                         foreach my $line (@lines) {
       
   571                             if (($line =~ /^<Object/i) && 
       
   572                                 (($line =~ /name=default>/i) || 
       
   573                                 ($line =~ /name=default\s/i) || 
       
   574                                 ($line =~ /name="default"/i))) {
       
   575                                 print TMPOBJ $line;
       
   576                                 addToObjConf(\*TMPOBJ);
       
   577                                 print "UPDATED: $objConfFile \n";
       
   578                             } elsif ($line =~ /PathCheck\s+fn\s*=\s*(\S+)\s+(\S+)\s*=\s*(\S+)$/) {
       
   579                                 my $funcName = $1;
       
   580                                 my $valueName = $2;
       
   581                                 my $values = $3;
       
   582                                 if (($funcName =~ /find-index/) and ($valueName =~ /index-names/)) {
       
   583                                     $values =~ s/[\"](\S+)[\"]/$1/;
       
   584                                     $values = "$1".",index.php";
       
   585                                     my $newLine = <<__UP_TO_THIS_POINT_;
       
   586 PathCheck fn=$funcName $valueName=\"$values\"
       
   587 __UP_TO_THIS_POINT_
       
   588                                     print TMPOBJ $newLine;
       
   589                                 }
       
   590                                 else {
       
   591                                     print TMPOBJ $line;
       
   592                                 }
       
   593                             } else {
       
   594                                 print TMPOBJ $line;
       
   595                             }
       
   596                         }
       
   597                     }
       
   598                 } else {
       
   599                     print "\nERROR: Unable to write $objConfFile \n\n";
       
   600                     close(TMPOBJ);
       
   601                     close(OBJ);
       
   602                     unlink("$tmpObjConfFile");
       
   603                     exit 1;
       
   604                 }
       
   605                 close(TMPOBJ);
       
   606                 close(OBJ);
       
   607                 unlink("$objConfFile");
       
   608                 rename("$tmpObjConfFile", "$objConfFile");
       
   609                 chmod(0600, "$objConfFile");
       
   610                 chown $statInfo[4], $statInfo[5], $objConfFile;
       
   611             }
       
   612         }
       
   613     }
       
   614 }
       
   615 
       
   616 # -----------------------------------------------------------------------------
       
   617 # processMimeTypes
       
   618 # Append the MIME_TYPES_APPEND_STRING value at the end of 
       
   619 # all the mime types file.
       
   620 # -----------------------------------------------------------------------------
       
   621 
       
   622 sub processMimeTypes {
       
   623     while(scalar(@$MIME_TYPES_FILES) > 0) {
       
   624         my $mimeTypesFile = pop(@$MIME_TYPES_FILES);
       
   625         my $mimeTypesStatus = 1;
       
   626 
       
   627         if (defined isValidFile($mimeTypesFile)) {
       
   628 
       
   629             # Get the current File Stat.
       
   630             my @statInfo = stat $mimeTypesFile;
       
   631 
       
   632             # Verify if the changes already exist.
       
   633             if (open(MIME_R,"<$mimeTypesFile")) {
       
   634                 my @contents = <MIME_R>;
       
   635                 for (my $i = $#contents; $i > 0; $i--) {
       
   636                     if ($contents[$i] =~ /magnus-internal\/php/g) {
       
   637                         close(MIME_R);
       
   638                         return 1;
       
   639                     }
       
   640                 }
       
   641                 close(MIME_R);
       
   642             }
       
   643             open(MIME,">>$mimeTypesFile") or $mimeTypesStatus = undef;
       
   644             if (defined $mimeTypesStatus) {
       
   645                 addToMimeTypes(\*MIME);
       
   646                 print "UPDATED: $mimeTypesFile \n";
       
   647             } else {
       
   648                 print "\nERROR: Unable to write $mimeTypesFile. \n\n";
       
   649                 close(MIME);
       
   650                 exit 1;
       
   651             }
       
   652             close(MIME);
       
   653             chown $statInfo[4], $statInfo[5], $mimeTypesFile;
       
   654         }        
       
   655     }        
       
   656 }
       
   657 
       
   658 # -----------------------------------------------------------------------------
       
   659 # addMagnusConfEntry
       
   660 # Add the required magnus conf entry
       
   661 # -----------------------------------------------------------------------------
       
   662 sub addToMagnusConf {
       
   663 
       
   664     my $entry;
       
   665     my $FILENAME = shift;
       
   666     my $phpNsapi = $phpSoName; 
       
   667 
       
   668     if (isNSAPI()) {
       
   669 
       
   670         $entry = <<__UP_TO_THIS_POINT_;
       
   671 
       
   672 Init fn="load-modules" shlib="$phpNsapi" shlib_flags="global|now"
       
   673     funcs="php5_init,php5_close,php5_execute,php5_auth_trans"
       
   674 Init fn="php5_init"
       
   675 	php_ini="$PHP_CONF_ROOT"
       
   676 	php_ini_scandir="$PHP_MODULES_CONF_ROOT"
       
   677 	errorString="PHP failed to initialize."
       
   678 
       
   679 __UP_TO_THIS_POINT_
       
   680 
       
   681     } else {    # fastcgi
       
   682 
       
   683         $entry = <<__UP_TO_THIS_POINT_;
       
   684 
       
   685 Init fn="load-modules" shlib="$fastCGISoName"
       
   686 
       
   687 __UP_TO_THIS_POINT_
       
   688 
       
   689     }
       
   690 
       
   691     print $FILENAME $entry;    
       
   692 }
       
   693 
       
   694 # -----------------------------------------------------------------------------
       
   695 # addMimeTypesEntry
       
   696 # Add the required mime types entry
       
   697 # -----------------------------------------------------------------------------
       
   698 sub addToMimeTypes {
       
   699     my $FILENAME = shift;
       
   700     my $entry = <<__UP_TO_THIS_POINT_;
       
   701 
       
   702 type=magnus-internal/php                         exts=php,php3,php4,php5
       
   703 __UP_TO_THIS_POINT_
       
   704     print $FILENAME $entry;    
       
   705 }
       
   706 
       
   707 # -----------------------------------------------------------------------------
       
   708 # addObjConfEntry
       
   709 # Add the required obj conf entry
       
   710 # -----------------------------------------------------------------------------
       
   711 sub addToObjConf {
       
   712     # setup 
       
   713     my $FILENAME = shift;
       
   714     # On windows, replace \ with / in paths
       
   715     my $l_php_root = $PHP_ROOT;
       
   716     if (isWindows()) {
       
   717         $l_php_root =~ s/\\/\//g;
       
   718     }
       
   719 
       
   720     my $newLibPath = "$l_php_root/lib";
       
   721     my $newLibPath64 = "$l_php_root/lib/64";
       
   722     my $childs = &detectDefaultChildren();
       
   723 
       
   724     my $unixFastCGIEntry = <<__UNIX_FASTCGI_ENTRY_
       
   725 <If -f \$path>
       
   726 Service type="magnus-internal/php"
       
   727     fn="responder-fastcgi"
       
   728     app-path="$l_php_root/bin/php-cgi"
       
   729     bind-path="localhost:3101"
       
   730     app-env="PHPRC=$PHP_CONF_ROOT"
       
   731     app-env="PHP_INI_SCANDIR=$PHP_MODULES_CONF_ROOT"
       
   732     app-env="PHP_FCGI_CHILDREN=$childs"
       
   733     app-env="PHP_FCGI_MAX_REQUESTS=2000"
       
   734     app-env="FCGI_WEB_SERVER_ADDRS=127.0.0.1"
       
   735     bucket="php-bucket"
       
   736 </If>
       
   737 <Else>
       
   738 Service type="magnus-internal/php" fn="set-variable" error="404"
       
   739 </Else>
       
   740 
       
   741 __UNIX_FASTCGI_ENTRY_
       
   742 ;
       
   743 
       
   744     my $windowsFastCGIEntry = <<__WINDOWS_FASTCGI_ENTRY_
       
   745 
       
   746 Service type="magnus-internal/php"
       
   747     fn="responder-fastcgi"
       
   748     app-path="$l_php_root/php-cgi.exe"
       
   749     bind-path="$INSTANCE_NAME--php_cgi"
       
   750     app-env="PHPRC=$l_php_root"
       
   751     app-env="PHP_FCGI_CHILDREN=$childs"
       
   752     app-env="PHP_FCGI_MAX_REQUESTS=2000"
       
   753     bucket="php-bucket"
       
   754 
       
   755 __WINDOWS_FASTCGI_ENTRY_
       
   756 ;
       
   757 
       
   758     my $nsapiEntry = <<__NSAPI_ENTRY_
       
   759 
       
   760 Service type="magnus-internal/php" fn="php5_execute"
       
   761 
       
   762 __NSAPI_ENTRY_
       
   763 ;
       
   764 
       
   765     my $entry;
       
   766 
       
   767     if (isNSAPI()) {
       
   768         $entry = $nsapiEntry;
       
   769     } else {
       
   770         if (isWindows()) {
       
   771             $entry = $windowsFastCGIEntry;
       
   772         } else {
       
   773             $entry = $unixFastCGIEntry;
       
   774         }
       
   775     }
       
   776 
       
   777     print $FILENAME $entry;
       
   778 }
       
   779 
       
   780 # -----------------------------------------------------------------------------
       
   781 # isNSAPI
       
   782 # Check if SAPI is nsapi
       
   783 # -----------------------------------------------------------------------------
       
   784 
       
   785 sub isNSAPI() {
       
   786     if ($SAPI =~ m/nsapi/i) {
       
   787         return 1;
       
   788     }
       
   789     return 0;
       
   790 }
       
   791 
       
   792 # -----------------------------------------------------------------------------
       
   793 # isWindows
       
   794 # Check platform
       
   795 # -----------------------------------------------------------------------------
       
   796 
       
   797 sub isWindows() {
       
   798     if ($OSNAME =~ m/WIN/i) {
       
   799         return 1;
       
   800     }
       
   801     return 0;
       
   802 }
       
   803 
       
   804 # -----------------------------------------------------------------------------
       
   805 # checkFilesWritable
       
   806 # Check all the necessary files writable before adding the entries
       
   807 # -----------------------------------------------------------------------------
       
   808 
       
   809 sub checkFilesWritable {
       
   810     exit 1 unless defined isValidFile("$INSTANCE_ROOT/$INSTANCE_NAME/config/magnus.conf");
       
   811     my @TMP_OBJ_CONF_FILES = @$OBJ_CONF_FILES;
       
   812     my @TMP_MIME_TYPES_FILES = @$MIME_TYPES_FILES;
       
   813 
       
   814     while(scalar(@TMP_OBJ_CONF_FILES) > 0) {
       
   815         my $objConfFile = pop(@TMP_OBJ_CONF_FILES);
       
   816         exit 1 unless defined isValidFile("$objConfFile");
       
   817     }
       
   818     while(scalar(@TMP_MIME_TYPES_FILES) > 0) {
       
   819         my $mimeTypesFile = pop(@TMP_MIME_TYPES_FILES);
       
   820         exit 1 unless defined isValidFile("$mimeTypesFile");
       
   821     }
       
   822 }
       
   823 
       
   824 # -----------------------------------------------------------------------------
       
   825 # getValidAbsoluteFilePath
       
   826 # To get the valid absolute file path
       
   827 # -----------------------------------------------------------------------------
       
   828 
       
   829 sub getValidAbsoluteFilePath {
       
   830     my ($file) = @_;
       
   831     
       
   832     if (defined $file) {
       
   833         my ($fileName,$filePath,$fileNameSuffix) = fileparse("$file");
       
   834         if ($fileName eq $file) {
       
   835             $file = "$INSTANCE_ROOT/$INSTANCE_NAME/config/$fileName";
       
   836         }
       
   837         
       
   838         $file = undef unless defined isValidFile($file);
       
   839     }
       
   840     
       
   841     return $file;
       
   842 }  
       
   843 
       
   844 # -----------------------------------------------------------------------------
       
   845 # getValidAbsoluteFilePath
       
   846 # Valid file check
       
   847 # -----------------------------------------------------------------------------
       
   848 
       
   849 sub isValidFile {
       
   850     my ($file) = @_;
       
   851     my $status = undef;
       
   852     
       
   853     if (-e "$file") {
       
   854         if (-w "$file") {
       
   855             $status = 1; 
       
   856         } else {
       
   857             print "\nERROR: $file is not writable. \n\n";
       
   858             exit 1;
       
   859         }
       
   860     } else {
       
   861         print "\nERROR: $file not found, $! \n\n";
       
   862         exit 1;
       
   863     }
       
   864     
       
   865     return $status;
       
   866 }
       
   867 
       
   868 # -----------------------------------------------------------------------------
       
   869 # detectDefaultChildren
       
   870 # detect current architecture and come up with default values.
       
   871 # set default value to a higher value on Niagara based servers.
       
   872 # -----------------------------------------------------------------------------
       
   873 sub detectDefaultChildren {
       
   874     my $default = 2;
       
   875     if ($OSNAME =~ /SOLARIS/i) {
       
   876         if (-x "/bin/uname") {
       
   877             my $type = qx(/bin/uname -m);
       
   878             chomp($type);
       
   879             $default *= 3 if ($type =~ /sun4u/i);
       
   880             $default *= 6 if ($type =~ /sun4v/);
       
   881         }
       
   882     }
       
   883     return $default;
       
   884 }
       
   885 
       
   886 # -----------------------------------------------------------------------------
       
   887 # printUsage
       
   888 # print the usage command
       
   889 # -----------------------------------------------------------------------------
       
   890 
       
   891 sub printUsage {
       
   892     print "This script will configure a web server instance to run PHP scripts\n" .
       
   893           "either as FastCGI or NSAPI \n".
       
   894           "usage : \n" .
       
   895           "     setupPHP -instancename=<instance name> [-sapi=fastcgi|nsapi]\n" .
       
   896           "Examples:\n" .
       
   897           "\n".
       
   898           "This below example configures Web Server to run PHP in FastCGI mode (Default)\n".
       
   899           "     setupPHP -instancename=https-php\n" .
       
   900           "\n".
       
   901           "This below example configures Web Server to run PHP in NSAPI mode\n".
       
   902           "     setupPHP -instancename=https-php -sapi=nsapi\n";
       
   903     exit 1;
       
   904 }
       
   905 
       
   906 # -----------------------------------------------------------------------------
       
   907 # printResult
       
   908 # print the post setup steps
       
   909 # -----------------------------------------------------------------------------
       
   910 
       
   911 sub printResult {
       
   912     #remove "https-" from the instance name and use it as the config name
       
   913     my $configName = undef;
       
   914     $configName = $INSTANCE_NAME;
       
   915     $configName = substr($INSTANCE_NAME, 6) if ($INSTANCE_NAME =~ /^https/);
       
   916     my $result = <<__UP_TO_THIS_POINT_;
       
   917     
       
   918 Setup was sucessful.
       
   919 --------------------
       
   920 
       
   921 The following steps are necessary to make the changes to all the nodes.
       
   922 
       
   923 (1) Start the admin server
       
   924 
       
   925     $INSTALL_ROOT/admin-server/bin/startserv
       
   926 
       
   927 (2) Connect to the admin server using wadm command
       
   928 
       
   929     $INSTALL_ROOT/bin/wadm [--user=admin-user] [--password-file=admin-pswd-file] [--host=admin-host] [--port=admin-port]
       
   930 
       
   931 (3) Pull the modified config from the node to config store 
       
   932     using the following command in wadm console:
       
   933 
       
   934     pull-config --config=$configName nodehost
       
   935     
       
   936     For Example: If the host name for the node is xyz.com then enter the command like,
       
   937     
       
   938     pull-config --config=$configName xyz.com
       
   939 
       
   940 (4) Deploy the new changes to all nodes using 
       
   941     the following command in wadm console:
       
   942 
       
   943     deploy-config $configName 
       
   944 
       
   945 
       
   946 __UP_TO_THIS_POINT_
       
   947     print $result;
       
   948 }