<?php
/**
 * UpgradeWizard_prepare
 *
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004-2005 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 */

// $Id: UpgradeWizard_prepare.php,v 1.15 2005/09/23 08:26:02 andrew Exp $

require_once('modules/Administration/UpgradeWizardCommon.php');

// process commands
if( !isset($_REQUEST['install_file']) || ($_REQUEST['install_file'] == "") ){
    die( "File to install not specified." );
}
if( !isset($_REQUEST['mode']) || ($_REQUEST['mode'] == "") ){
    die( "No mode specified." );
}

$hidden_fields = "";
$new_lang_name = "";
$new_lang_desc = "";

$mode           = $_REQUEST['mode'];
$hidden_fields .= "<input type=hidden name=\"mode\" value=\"$mode\"/><br>\n";

$install_file   = urldecode( $_REQUEST['install_file'] );
$install_type   = getInstallType( $install_file );

$version        = "";
$show_files     = true;
$unzip_dir      = mk_temp_dir( $base_tmp_upgrade_dir );
$zip_from_dir   = ".";
$zip_to_dir     = ".";
$zip_force_copy = array();

unzip( $install_file, $unzip_dir );

// assumption -- already validated manifest.php at time of upload
require_once( "$unzip_dir/manifest.php" );

if( isset( $manifest['copy_files']['from_dir'] ) && $manifest['copy_files']['from_dir'] != "" ){
    $zip_from_dir   = $manifest['copy_files']['from_dir'];
}
if( isset( $manifest['copy_files']['to_dir'] ) && $manifest['copy_files']['to_dir'] != "" ){
    $zip_to_dir     = $manifest['copy_files']['to_dir'];
}
if( isset( $manifest['copy_files']['force_copy'] ) && $manifest['copy_files']['force_copy'] != "" ){
    $zip_force_copy     = $manifest['copy_files']['force_copy'];
}
if( isset( $manifest['version'] ) ){
    $version    = $manifest['version'];
}


switch( $install_type ){
    case "full":
    case "patch":
        if( !is_writable( "config.php" ) ){
            die( "Please make your config.php file writable and reload this page." );
        }
        break;
    case "theme":
        break;
    case "langpack":
        // find name of language pack: find single file in include/language/xx_xx.lang.php
        $d = dir( "$unzip_dir/$zip_from_dir/include/language" );
        while( $f = $d->read() ){
            if( $f == "." || $f == ".." ){
                continue;
            }
            else if( preg_match("/(.*)\.lang\.php\$/", $f, $match) ){
                $new_lang_name = $match[1];
            }
        }
        if( $new_lang_name == "" ){
            die( "No language pack file found at include/language inside $install_file." );
        }
        $hidden_fields .= "<input type=hidden name=\"new_lang_name\" value=\"$new_lang_name\"/><br>\n";

        $new_lang_desc = getLanguagePackName( "$unzip_dir/$zip_from_dir/include/language/$new_lang_name.lang.php" );
        if( $new_lang_desc == "" ){
            die( "No language pack description found at include/language/$new_lang_name.lang.php inside $install_file." );
        }
        $hidden_fields .= "<input type=hidden name=\"new_lang_desc\" value=\"$new_lang_desc\"/><br>\n";

        if( !is_writable( "config.php" ) ){
            die( "Please make your config.php file writable and reload this page." );
        }
        break;
    case "module":
        $show_files = false;
        break;
    default:
        die( "This page is not for running $install_type." );
}


$new_files      = findAllFilesRelative( "$unzip_dir/$zip_from_dir", array() );
$hidden_fields .= "<input type=hidden name=\"version\" value=\"$version\"/><br>\n";

// present list to user
?>
<form action="<?php print( $form_action . "_commit" ); ?>" method="post">
<?php
    switch( $mode ){
        case "Install":
            if( $install_type == "module" ){
                print( "Module is ready to be installed.  Click \"Commit\" to proceed with installation." );
            }
            else{
?>
The following is a list of files that are included in the upgrade.






If the checkbox to the left of a file name is not clickable, then it is identical to the existing version and does not need to be updated.
All other files contain changes.
Only checked files will be copied.
<?php
            }
            break;
        case "Uninstall":
            if( $install_type == "module" ){
                print( "Module is ready to be uninstalled.  Click \"Commit\" to proceed with installation.<br>\n" );
            }
            else{
                print( "The following files will be removed from the system:<br>\n" );
            }
            break;
    }

echo "<br><br>";
foreach ($script_files as $script_name => $script_filename) {
	if (is_file("{$unzip_dir}/{$script_filename}")) {
   		echo "Found {$script_name} script: <i>{$script_filename}</i><br>";
   	}
}
?>

<p>

<?php
$count = 0;
    if( $show_files == true ){
        $count = 0;
        
        foreach( $new_files as $the_file ){
            $highlight_start    = "";
            $highlight_end      = "";
            $checked            = "";
            $disabled           = "";
            $unzip_file = "$unzip_dir/$zip_from_dir/$the_file";
            $new_file   = clean_path( "$zip_to_dir/$the_file" );
            $forced_copy    = false;

            if( $mode == "Install" ){
                $checked = "checked";
                foreach( $zip_force_copy as $pattern ){
                    if( preg_match("#" . $pattern . "#", $unzip_file) ){
                        $disabled = "disabled=\"true\"";
                        $forced_copy = true;
                    }
                }
                if( !$forced_copy && is_file( $new_file ) && (md5_file( $unzip_file ) == md5_file( $new_file )) ){
                    $disabled = "disabled=\"true\"";
                    $checked = "";
                }







                if( $checked != "" && $disabled != "" ){    // need to put a hidden field
                    print( "<input name=\"copy_$count\" type=\"hidden\" value=\"" . $the_file . "\">\n" );
                }
                print( "<li><input name=\"copy_$count\" type=\"checkbox\" value=\"" . $the_file . "\" $checked $disabled > " . $highlight_start . $new_file . $highlight_end );
                if( $checked == "" && $disabled != "" ){    // need to explain this file hasn't changed
                    print( " (no changes)" );
                }
                print( "<br>\n" );
            }
            else if( $mode == "Uninstall" && file_exists( $new_file ) ){
                if( md5_file( $unzip_file ) == md5_file( $new_file ) ){
                    $checked = "checked=\"true\"";
                }
                else{
                    $highlight_start    = "<font color=red>";
                    $highlight_end      = "</font>";
                }
                print( "<li><input name=\"copy_$count\" type=\"checkbox\" value=\"" . $the_file . "\" $checked $disabled > " . $highlight_start . $new_file . $highlight_end . "<br>\n" );
            }
            $count++;
        }
        print( "</ul>\n" );
    }
?>
	<br>
    <input type=submit value="Commit" />
    <?php print( $hidden_fields ); ?>
    <input type=hidden name="copy_count" value="<?php print( $count );?>"/>
    <input type=hidden name="run" value="commit" />
    <input type=hidden name="install_file"  value="<?php print( urlencode( $install_file ) ); ?>" />
    <input type=hidden name="unzip_dir"     value="<?php echo $unzip_dir; ?>" />
    <input type=hidden name="zip_from_dir"  value="<?php echo $zip_from_dir; ?>" />
    <input type=hidden name="zip_to_dir"    value="<?php echo $zip_to_dir; ?>" />
</form>

<?php
    $GLOBALS['log']->info( "Upgrade Wizard patches" );
?>
