home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / PEAR / Installer.php < prev    next >
PHP Script  |  2001-11-13  |  9KB  |  245 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@fast.no>                                   |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |                                                                      |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Installer.php,v 1.16.2.3 2001/11/13 01:26:48 ssb Exp $
  22.  
  23. require_once 'PEAR/Common.php';
  24. require_once 'PEAR/Registry.php';
  25.  
  26. /**
  27.  * Administration class used to install PEAR packages and maintain the
  28.  * installed package database.
  29.  *
  30.  * TODO:
  31.  *  - finish and test Windows support
  32.  *  - kill FIXME's
  33.  *
  34.  * @since PHP 4.0.2
  35.  * @author Stig Bakken <ssb@fast.no>
  36.  */
  37. class PEAR_Installer extends PEAR_Common
  38. {
  39.     // {{{ properties
  40.  
  41.     /** name of the package directory, for example Foo-1.0 */
  42.     var $pkgdir;
  43.  
  44.     /** directory where PHP code files go */
  45.     var $phpdir;
  46.  
  47.     /** directory where PHP extension files go */
  48.     var $extdir;
  49.  
  50.     /** directory where documentation goes */
  51.     var $docdir;
  52.  
  53.     /** directory where the package wants to put files, relative
  54.      *  to one of the three previous dirs
  55.      */
  56.     var $destdir = '';
  57.  
  58.     /** debug level (integer) */
  59.     var $debug = 1;
  60.  
  61.     /** temporary directory */
  62.     var $tmpdir;
  63.  
  64.     /** PEAR_Registry object used by the installer */
  65.     var $registry;
  66.  
  67.     // }}}
  68.  
  69.     // {{{ constructor
  70.  
  71.     function PEAR_Installer($phpdir = PEAR_INSTALL_DIR,
  72.                             $extdir = PEAR_EXTENSION_DIR,
  73.                             $docdir = '')
  74.     {
  75.         $this->PEAR();
  76.         $this->phpdir = $phpdir;
  77.         $this->extdir = $extdir;
  78.         $this->docdir = $docdir;
  79.         $this->statedir = "/var/lib/php"; // XXX FIXME Windows
  80.     }
  81.  
  82.     // }}}
  83.     // {{{ install()
  84.  
  85.     /**
  86.      * Installs the files within the package file specified.
  87.      *
  88.      * @param $pkgfile path to the package file
  89.      *
  90.      * @return bool true if successful, false if not
  91.      */
  92.  
  93.     function install($pkgfile)
  94.     {
  95.         // XXX FIXME Add here code to manage packages database
  96.         //$this->loadPackageList("$this->statedir/packages.lst");
  97.         if (empty($this->registry)) {
  98.             $this->registry = new PEAR_Registry;
  99.         }
  100.         $oldcwd = getcwd();
  101.         $need_download = false;
  102.         if (preg_match('#^(http|ftp)://#', $pkgfile)) {
  103.             $need_download = true;
  104.         } elseif (!@is_file($pkgfile)) {
  105.             return $this->raiseError("could not open the package file: $pkgfile");
  106.         }
  107.  
  108.         // Download package -----------------------------------------------
  109.         if ($need_download) {
  110.             $file = basename($pkgfile);
  111.             if (PEAR::isError($downloaddir = $this->mkTempDir())) {
  112.                 return $downloaddir;
  113.             }
  114.             $this->log(2, '+ tmp dir created at ' . $downloaddir);
  115.             $downloadfile = $downloaddir . DIRECTORY_SEPARATOR . $file;
  116.             $this->log(1, "downloading $pkgfile ...");
  117.             if (!$fp = @fopen($pkgfile, 'r')) {
  118.                 return $this->raiseError("$pkgfile: failed to download ($php_errormsg)");
  119.             }
  120.             if (!$wp = @fopen($downloadfile, 'w')) {
  121.                 return $this->raiseError("$downloadfile: write failed ($php_errormsg)");
  122.             }
  123.             $bytes = 0;
  124.             while ($data = @fread($fp, 16384)) {
  125.                 $bytes += strlen($data);
  126.                 if (!@fwrite($wp, $data)) {
  127.                     return $this->raiseError("$downloadfile: write failed ($php_errormsg)");
  128.                 }
  129.             }
  130.             $pkgfile = $downloadfile;
  131.             fclose($fp);
  132.             fclose($wp);
  133.             $this->log(1, '...done: ' . number_format($bytes, 0, '', ',') . ' bytes');
  134.         }
  135.  
  136.         // Decompress pack in tmp dir -------------------------------------
  137.  
  138.         // To allow relative package file calls
  139.         if (!chdir(dirname($pkgfile))) {
  140.             return $this->raiseError('unable to chdir to package directory');
  141.         }
  142.         $pkgfile = getcwd() . DIRECTORY_SEPARATOR . basename($pkgfile);
  143.  
  144.         if (PEAR::isError($tmpdir = $this->mkTempDir())) {
  145.             chdir($oldcwd);
  146.             return $tmpdir;
  147.         }
  148.         $this->log(2, '+ tmp dir created at ' . $tmpdir);
  149.  
  150.         $tar = new Archive_Tar($pkgfile, true);
  151.         if (!$tar->extract($tmpdir)) {
  152.             chdir($oldcwd);
  153.             return $this->raiseError("unable to unpack $pkgfile");
  154.         }
  155.         $file = basename($pkgfile);
  156.         // Assume the decompressed dir name
  157.         if (($pos = strrpos($file, '.')) === false) {
  158.             chdir($oldcwd);
  159.             return $this->raiseError("invalid package name");
  160.         }
  161.         $pkgdir = substr($file, 0, $pos);
  162.         $descfile = $tmpdir . DIRECTORY_SEPARATOR . $pkgdir . DIRECTORY_SEPARATOR . 'package.xml';
  163.  
  164.         if (!is_file($descfile)) {
  165.             chdir($oldcwd);
  166.             return $this->raiseError("no package.xml file after extracting the archive");
  167.         }
  168.  
  169.         // Parse xml file -----------------------------------------------
  170.         $pkginfo = $this->infoFromDescriptionFile($descfile);
  171.         if (PEAR::isError($pkginfo)) {
  172.             chdir($oldcwd);
  173.             return $pkginfo;
  174.         }
  175.  
  176.         if ($this->registry->packageExists($pkginfo['package'])) {
  177.             return $this->raiseError("package already installed");
  178.         }
  179.  
  180.         // Copy files to dest dir ---------------------------------------
  181.         if (!is_dir($this->phpdir)) {
  182.             chdir($oldcwd);
  183.             return $this->raiseError("no script destination directory found",
  184.                                      null, PEAR_ERROR_DIE);
  185.         }
  186.         $tmp_path = dirname($descfile);
  187.         foreach ($pkginfo['filelist'] as $fname => $atts) {
  188.             $dest_dir = $this->phpdir . DIRECTORY_SEPARATOR;
  189.             if (isset($atts['baseinstalldir'])) {
  190.                 $dest_dir .= $atts['baseinstalldir'] . DIRECTORY_SEPARATOR;
  191.             }
  192.             if (dirname($fname) != '.') {
  193.                 $dest_dir .= dirname($fname) . DIRECTORY_SEPARATOR;
  194.             }
  195.             $fname = $tmp_path . DIRECTORY_SEPARATOR . $fname;
  196.             $this->_installFile($fname, $dest_dir, $atts);
  197.         }
  198.         $this->registry->addPackage($pkginfo['package'], $pkginfo);
  199.         chdir($oldcwd);
  200.         return true;
  201.     }
  202.  
  203.     // }}}
  204.  
  205.     // {{{ _installFile()
  206.  
  207.     function _installFile($file, $dest_dir, $atts)
  208.     {
  209.         $type = strtolower($atts['role']);
  210.         switch ($type) {
  211.             case 'test':
  212.                 // don't install test files for now
  213.                 $this->log(2, "+ Test file $file won't be installed yet");
  214.                 return true;
  215.                 break;
  216.             case 'doc':
  217.             case 'php':
  218.             default:
  219.                 $dest_file = $dest_dir . basename($file);
  220.                 break;
  221.         }
  222.         if (!@is_dir($dest_dir)) {
  223.             if (!$this->mkDirHier($dest_dir)) {
  224.                 $this->log(0, "failed to mkdir $dest_dir");
  225.                 return false;
  226.             }
  227.             $this->log(2, "+ created dir $dest_dir");
  228.         }
  229.         $orig_perms = fileperms($file);
  230.         if (!@copy($file, $dest_file)) {
  231.             $this->log(0, "failed to copy $file to $dest_file");
  232.             return false;
  233.         }
  234.         chmod($dest_file, $orig_perms);
  235.         $this->log(2, "+ copy $file to $dest_file");
  236.         // FIXME Update Package database here
  237.         //$this->updatePackageListFrom("$d/$file");
  238.         $this->log(1, "installed file $dest_file");
  239.         return true;
  240.     }
  241.  
  242.     // }}}
  243. }
  244.  
  245. ?>