Location: PHPKode > projects > ZZ/OSS Installer > zic-1.1.0dev1/installer/packages_download.php
<?php   
    /*
    Copyright (C) 2001-2004 ZZOSS GbR, http://www.zzoss.com

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */
    
    /**
    @version $Id: packages_download.php,v 1.12 2004/04/07 14:21:54 ordnas Exp $
    @copyright Copyright &copy; 2001-2004 ZZ/OSS GbR, http://www.zzoss.com
    @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
    */
	  
	// execute init file
	require_once 'inc/init.php';
    
	function downloadPackages($packages_selection)
    {    
        global $process;
        $process = false;
		$error = false;
   
		if(!is_array($packages_selection)) {
            $result['error'] = true;
            $result['log'] = 'No actions defined.';
        } else {
            global $zi_registry;
            
            $pkg_type = 'package';
            if(isset($_REQUEST['type'])){
                $pkg_type = $_REQUEST['type'];
            }
            $zi_registry->setPackageType($pkg_type);
            
            $items = array();
            
            $packages_resolve = $zi_registry->getPackagesResolve();
            $packages = $zi_registry->getPackages();
            
            foreach($packages_selection as $key => $val) {
                $name = $packages_resolve[$key];
                $package = $packages[$name];
                if( ($val=='I') && ($package["install"]["type"] == 'remote') ) {
                    $items[] = $package['name'].'-'.$package['release']['version'];
                    // Do we also download the remote package XML ?
                    if(strlen($package['filelist']['descriptor']['#'])>2) {
                        $items[] = 'Package XML of '.$package['name'].'-'.$package['release']['version'];
                    }
                }
			}
            
            if(!count($items)){
                $result['error'] = true;
                $result['log'] = 'Nothing to download';
            } else {
                require_once 'ZZOSS_Html/Progress.php';
                $bar = new ZZOSS_HtmlProgress();
                $bar->prepareListHorizontal($items, 'Downloading');
                
                require_once 'ZZOSS_File/Download.php';
                $download = new ZZOSS_FileDownload();
                $download->setCallback(array(&$bar, '_downloadCallback'));
                
                $log = '';
                
                foreach($packages_selection as $key=>$val) {
                    if(!$error) {
                        $pkg_name = $packages_resolve[$key];
                        $package = $packages[$pkg_name];
                        if($val == 'I' && $package["install"]["type"] == 'remote') {
                            // create directory
                            $dir_packages = $zi_registry->getApplicationPath().'packages'.DIRECTORY_SEPARATOR;
                            $dir_downloads = $zi_registry->getApplicationPath().'downloads'.DIRECTORY_SEPARATOR;
                            $dir_package = $dir_packages.$package['name'].'-'.$package['release']['version'].DIRECTORY_SEPARATOR;
                            
                            //print_r($package['filelist']['file']);
                            
                            $pkg_remote = NULL;
                            if(isset($package['filelist']['file'][0]['#']) && strlen($package['filelist']['file'][0]['#'])>2) {
                                $pkg_remote = $package['filelist']['file'][0]['#'];
                            } elseif(isset($package['filelist']['file']['#']) && strlen($package['filelist']['file']['#'])>2) {
                                $pkg_remote = $package['filelist']['file']['#'];
                            }
                            $log .= "[".$package['name'].'-'.$package['release']['version']."]\n";
                            if(!is_null($pkg_remote)) {
                                // download archive and extract it
                                $pkg_dl = $dir_downloads.basename($pkg_remote);
                                
                                $log .= "Downloaded remote package archive:\n".$pkg_remote."\n";
                                
                                $download->download($pkg_remote, $pkg_dl);
                                //$bar->executeListHorizontal();
                                    
                                $log .= $download->getLog();
    
                                if(file_exists($pkg_dl)) {
                                    //$log .= 'downloaded package '.$package['name'].'-'.$package['release']['version']."\n";
                                    $tar = new Archive_Tar($pkg_dl);
                                    if (!$tar->extract($dir_packages)) {
                                        return PEAR::raiseError("unable to unpack $pkg_dl");
                                    }
                                    //unlink($pkg_dl);
                                }
                            } else {
                                PEAR::raiseError("No download location specified for the archive (.tgz).");
                            }
                            
                            $xml_dest = $dir_package.'package.xml';
                            $xml_dl = $dir_packages.'package.xml';
                            // move package XML included in package
                            if(file_exists($xml_dl)) {
                                if(!is_dir(dirname($xml_dest))){
                                    System::mkdir(array('-p', dirname($xml_dest)));
                                }
                                copy($xml_dl, $xml_dest);
                                //unlink($xml_dl);
                            }                        
                            //print_r($package['filelist']);
                            if(isset($package['filelist']['descriptor']['#']) && strlen($package['filelist']['descriptor']['#'])>2) {
                                $xml_dl = $dir_downloads.$package['name'].'-'.$package['release']['version'].'.xml';
                                
                                // download specified remote package XML
                                $log .= "Downloaded remote package XML:\n".$package['filelist']['descriptor']['#']."\n";
                                $download->download($package['filelist']['descriptor']['#'], $xml_dl);
                                $log .= $download->getLog();
                                if(file_exists($xml_dl)) {
                                    if(!is_dir(dirname($xml_dest))){
                                        System::mkdir(array('-p', dirname($xml_dest)));
                                    }
                                    copy($xml_dl, $xml_dest);
                                    unlink($xml_dl);
                                }
                            }
                            
                            // register new package
                            if(file_exists($xml_dest)) {
                                $zi_registry->replacePackage($package['id'], $xml_dest, $dir_package);
                                $log .= 'Registered new package '.$package['name']."-".$package['release']['version'].".\n";
                            } else {
                                $log .= 'Error: no package.xml in package '.$package['name'].'-'.$package['release']['version'].".\n";
                                $error = true;
                            }
                            
                        }
                    }
                }
                $result['error'] = $error;
                $result['log'] = $log;
            }
        }
        
        return $result;
	}
	
	/***************************************************************************   
    * STARTUP                                                             	   *   
    ***************************************************************************/   
	
    $pkg_type = 'package';
    if(isset($_REQUEST['type'])){
        $pkg_type = $_REQUEST['type'];
    }
    $zi_registry->setPackageType($pkg_type);
    
	// load previous selection
	$a =  $zi_registry->getPackagesActions();
    
    $packages_resolve = $zi_registry->getPackagesResolve();
    $packages = $zi_registry->getPackages();
    
    // check if there are packages for downlaod, otherwise we procede
    $has_downloads = false;
    
   foreach($a as $key => $val) {
        $name = $packages_resolve[$key];
        $package = $packages[$name];
        if(
            $val=='I' &&
            $package["install"]["type"] == 'remote' && 
            !file_exists($zi_registry->getApplicationPath().'downloads'.DIRECTORY_SEPARATOR.$name.'.tgz')
            ) {
            $has_downloads = true;
            break;
        }
    }
    
    if(!$has_downloads){
        header('Location: packages_process.php'.zi_form_querystr());
        exit;
    }
    
    /*
    echo '<h3>Actions</h3>';
    echo '<pre>';
    echo print_r($a);
    echo '</pre>';
    */
	// default next link
	$link_next = 'packages_dependencies.php';
	// default do not process
	$process = false;
	
    /***************************************************************************   
    * PAGE CONTENT                                                             *   
    ***************************************************************************/   
	
	include 'themes/'.$GLOBALS['ZI']['theme'].'/header.php';
	
    $application = $zi_registry->getApplication();
    ?>
    <h1>Download Packages</h1>
<h2><a href="info.php?mode=<?php echo $_REQUEST['mode']; ?>&type=application&distribution=<?php echo $_REQUEST['distribution']; ?>" onclick="javascript:window.open('info.php?mode=<?php echo $_REQUEST['mode']; ?>&type=application&distribution=<?php echo $_REQUEST['distribution']; ?>', 'ApplicationInformation', 'width=500,height=500,resizable=1,status=0,scrollbars=1,toolbar=0,location=0,directories=0,menubar=0,dependent=0');void(0);javascript:return(false);" target="_blank" title="<?php echo $application["summary"].' '.$application['release']['version']; ?>"><?php echo $application["summary"].' '.$application['release']['version']; ?></a></h2>

<h2>Download Process</h2>
<?php
$result = downloadPackages($a);
?>
<h2>Download Log</h2>
<pre><?php echo $result['log']; ?></pre>
<?php
	$zi_buttons['back'] = "packages_confirm.php".zi_form_querystr();
    
	if(!$result['error']) {
		$zi_buttons['next'] = 'packages_dependencies.php'.zi_form_querystr();
	}
	include 'themes/'.$GLOBALS['ZI']['theme'].'/footer.php';
?>
Return current item: ZZ/OSS Installer