Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 7 of 7

This is a discussion on phpinimerge - Tool for merging server's php.ini with local overrides in the Open Discussion & Chit-chat forum
As you've probably heard, JPC is moving from the Apache Module of PHP to the CGI version for increased security. With this change we will ...

  1. #1
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003

    phpinimerge - Tool for merging server's php.ini with local overrides

    As you've probably heard, JPC is moving from the Apache Module of PHP to the CGI version for increased security. With this change we will lose the ability to override PHP sttings in .htaccess and will instead have to use our own copies of php.ini instead.

    Here is the php.ini merging script that I said I was writing. It will allow you to keep your php.ini changes separate from those of the server-wide php.ini, which should make it easier to track and apply changes. Feel free to use it on any of your sites.

    Code:
    #!/usr/bin/php -q
    <?php
        /**********************************************************************
         * phpinimerge                                                        *
         * By Jason Pitoniak <jason@interbrite.com>                           *
         * Copyright (c) 2006 Interbrite Communications. All rights reserved. *
         *                                                                    *
         * This program allows you to merge local changes to php.ini with the *
         * server-wide settings.                                              *
         *                                                                    *
         * Usage:                                                             *
         *  phpinimerge [-h] [-s serverfile] localfile outputfile             *
         *                                                                    *
         *   -h          Print help message                                   *
         *   -s          Allows the path to the server-wide php.ini to be     *
         *               specified. If not set the default will be used.      *
         *   localfile   The path to the file containing php.ini directives   *
         *               intended to override server-wide settings            *
         *   outputfile  The new php.ini file that contains the original      *
         *               server-wide settings merged with those of localfile  *
         *                                                                    *
         * This program is distributed in the hope that it will be useful,    *
         * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               *
         **********************************************************************/
         
        //shift off the path used to call the program from the args list
        array_shift($argv);
        
        if(count($argv) < 2) {
            //not enough arguments passed, show help
            showUsage();
        }
        else {
            //collect the output file path and remove it from the args
            $output_ini = $argv[count($argv) - 1];
            unset($argv[count($argv) - 1]);
            
            //collect the localfile path and remove it from the args
            $local_ini = $argv[count($argv) - 1];
            unset($argv[count($argv) - 1]);
            
            while(count($argv) > 0) {
                //search for command line options and handle them
                $cmd = array_shift($argv);
                if($cmd == '-s') {
                    //server-wide path override
                    if(count($argv) > 0) {
                        $server_ini = array_shift($argv);
                    }
                    else {
                        echo("-s option must be followed by a path\n\n");
                        showUsage();
                    } 
                }
                else if($cmd == '-h') {
                    //show help
                    showUsage();
                }
                else {
                    //bad option
                    echo("Unsupported Option: $cmd\n\n");
                    showUsage();
                }
            }
        }
        
        //if no server-wide file path was specified, use th default
        $server_ini = isset($server_ini) ? $server_ini : '/usr/local/Zend/etc/php.ini';
        
        //parse the two ini files into arrays
        $server = parse_ini_file($server_ini);
        $local = parse_ini_file($local_ini);
        
        //check for errors
        if($server === false) {
            echo("Unable parse server php.ini file ($server_ini)\n\n");
            showUsage();
        }
        
        if($local === false) {
            echo("Unable to parse local php.ini file ($local_ini)\n\n");
            showUsage();
        }
        
        //if we get here we should have two arrays with the contents of the two ini files
        $merged = array_merge($server, $local);
        
        //write the new file
        $fp = fopen($output_ini, 'w');
        flock($fp, 2);
        foreach($merged as $key => $value) {
            fputs($fp, "$key=$value\n");
        }
        flock($fp, 3);
        fclose($fp);
        exit();
        
        function showUsage() {
            //show useage instructions
            
            $usage =<<<EOF
    phpinimerge - Merge local changes to php.ini with the server's settings.
    
    Usage:
        phpinimerge [-h] [-s servercopy] localcopy outputfile
    
    Options:
        -h		Print this help message
        -s		Force the use of "servercopy" as the server's php.ini
    EOF;
            echo("$usage\n");
            exit();
        }
    ?>
    Installation:

    1. Copy the above code to a file called phpinimerge (no php extension necessary) somewhere in your site. It should be outside of your public_html directory.
    2. Chmod the file to 700 (executable by you, and only you)

    Use:

    NOTE: This is a command line tool, therefore it requires SSH access.

    1. Create a local php ini file. For simplicity, put it in the same directory as the phpinimerge script. I recommend calling this file php.ini.local, but the name is not important. This file should be in the form of "setting=value" where setting is the PHP setting you want to override and value is the new value. Place each directive on its own line. For example:
    Code:
    register_globals=off
    magic_quotes_gpc=off
    2. Grab the server-wide php.ini file from http://chamaeleon.nocdirect .com/php.ini. The script should be able to use the php.ini file that's installed on your server, but JailShell prevents users from accessing it. I call this file php.ini.server, but again the name is unimportant.

    3. Run the script (in an SSH session).
    Code:
    ./phpinimerge -s php.ini.server php.ini.local php.ini.merged
    If all goes well you should get a new JailShell prompt with no other messages and there should be a new file called php.ini.merged (or whatever you specified for the last argument to the script) in your current directory.

    If you open the new file you will see that it does not contain any sections (in square brackets, ie [PHP]) or comments (delimited by ";"). PHP ignores these, so they aren't necessary.

    To see if your new merged file works, run the command
    Code:
    php -c ./php.ini.merged -i
    which will output the same information as the phpinfo() command, using your new merged php.ini. (Note that you may see some errors because the Zend libraries are not available through JailShell.) You can pipe the results through grep to quickly find your overrides:
    Code:
    php -c ./php.ini.merged -i | magic_quotes
    You will need to change the merged file to php.ini and copy it to any directories inside public_html that have PHP scripts in them for these changes to take effect.

    IMPORTANT: If you make changes to your local php.ini after running phpinimerge, the new changes will NOT take effect automatically. You will need to run phpinimerge and recopy the file to your public_html folder and subfolders each time you change settings.

    This is the first of a few scripts that I plan to write to ease the transition to the CGI version of PHP. The others will come as I have time to write them. Some ideas include a tool to manage php.ini symlinks in public_html subfolders and perhaps a tool to parse .htaccess files for php_value and php_flag directives to build the php.ini.local file. If anyone has any suggestions or comments, I'm all ears.

    I hope everyone finds this useful.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  2. #2
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775
    OMG! Are we gonna have to add hash plings to the top of every PHP file before they'll render?
    Last edited by Vin DSL; 03-14-2006 at 09:15 PM.
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

  3. #3
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Quote Originally Posted by Vin DSL
    OMG! Are we gonna have to add hash plings to the top of every PHP file before they'll render?
    No, its only in this file because it is a shell script intended for the command line, not a web script. The "shebang" here just lets you run the script from the command line without having to call php first.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  4. #4
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775
    Whew!
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

  5. #5
    Apathetic
    Join Date
    Sep 2002
    Posts
    31
    Not to play dumb, but can we simply download http://chamaeleon.nocdirect .com/php.ini, change the entries we want to change locally, then upload the file to our site and symlink it in every folder a php script is located?
    "We have normality. I repeat, we have normality. Anything you still can't cope with is therefore your own problem."
    - Douglas Adams

  6. #6
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    Quote Originally Posted by scarenius
    Not to play dumb, but can we simply download http://chamaeleon.nocdirect .com/php.ini, change the entries we want to change locally, then upload the file to our site and symlink it in every folder a php script is located?
    Yes, the only benefit that this script affords is that it lets you maintain a separate file of your changes. That way if the server version changes and you want the new functionality that it provides you can easily merge your changes into the new version rather than hunting through it manually.

    If you only have one or two settings to change then using this script might be overkill. Even for me, with my five or six changes, this script might be overkill. I'm good at creating overkill scripts--I get ideas and spend two hours writing a script to tackle what would take me a few minutes to do by hand, well...often. I guess I just like to program. Anyway, I thought this tool might be useful to some, so I shared it. I plan to use it for my own site, so I consider it worth my time to create. If someone else alsop finds it useful then all the better, if not that's fine too.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  7. #7
    Apathetic
    Join Date
    Sep 2002
    Posts
    31
    You're preaching to the choir, jason. =)
    "We have normality. I repeat, we have normality. Anything you still can't cope with is therefore your own problem."
    - Douglas Adams

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •