![]() |
Xataface 2.0
Xataface Application Framework
|
00001 <?php 00002 require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'ftp.api.php'; 00003 require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'ftp.class.php'; 00004 require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lib'.DIRECTORY_SEPARATOR.'Archive'.DIRECTORY_SEPARATOR.'Tar.php'; 00005 00006 class FTPExtractor { 00007 var $archive; 00008 var $ftp; 00009 var $destination; 00010 var $source; 00011 00012 function FTPExtractor(&$archive){ 00013 $this->archive =& $archive; 00014 } 00015 00016 function connect($host, $user, $password ){ 00017 $this->ftp = ftp_connect($host); 00018 if ( !$this->ftp ){ 00019 return PEAR::raiseError("Failed to connect to FTP host '$host'"); 00020 } 00021 00022 $res = ftp_login($this->ftp, $user, $password); 00023 if ( !$res ){ 00024 return PEAR::raiseError("Incorrect username or password while trying to connect to FTP host '$host'"); 00025 } 00026 00027 return true; 00028 } 00029 00030 function extract($destination, $sourcePath){ 00031 $log = array(); 00032 $res = ftp_chdir($this->ftp, $destination); 00033 00034 if ( !$res ) return PEAR::raiseError("Directory $destination does not exist on the server."); 00035 //print_r($this->archive->listContent());exit; 00036 foreach ( $this->archive->listContent() as $item ){ 00037 //if ( !$item['filename'] ) continue; 00038 //if ( $item['filename']{ strlen($item['filename'])-1 } == '/' ) $item['filename'] = substr($item['filename'],0,strlen($item['filename'])-1); 00039 00040 $log[] = "Extracting ".$item['filename']." to $destination"; 00041 //print_r($item); 00042 if ( $item['typeflag'] == 5){ 00043 //directory 00044 //print_r(); 00045 //$size = ftp_size($this->ftp, $item['filename']); 00046 //echo "Size: $size"; 00047 if ( !ftp_nlist($this->ftp, $item['filename']) ){ 00048 $res = ftp_mkdir($this->ftp, $item['filename']); 00049 if ( !$res ) return PEAR::raiseError("Extraction failed. Failed to create directory ".$item['filename']); 00050 ftp_chmod($this->ftp, $item['mode'], $item['filename']); 00051 } 00052 00053 } else { 00054 // a file 00055 $fpath = tempnam(null, $item['filename']); 00056 file_put_contents($fpath, $this->archive->extractInString($item['filename'])); 00057 $res = ftp_put($this->ftp, $item['filename'], $fpath, FTP_BINARY); 00058 if ( !$res ){ 00059 $dirname = dirname($item['filename']); 00060 if ( $dirname and $dirname{strlen($dirname)-1} == '/' ) $dirname = substr($dirname,0,strlen($dirname)-1); 00061 00062 $dirstack = array(); 00063 00064 while ( !($ls = ftp_nlist($this->ftp, $dirname)) or (is_array($ls) and count($ls) < 2) ){ 00065 00066 $dirstack[] = $dirname; 00067 $dirname = dirname($dirname); 00068 if ( $dirname and $dirname{strlen($dirname)-1} == '/' ) $dirname = substr($dirname,0,strlen($dirname)-1); 00069 if ( dirname($dirname) == $dirname) break; 00070 } 00071 00072 while ( count($dirstack) > 0 ){ 00073 $dirname = array_pop($dirstack); 00074 $dres = ftp_mkdir($this->ftp, $dirname); 00075 //if ( !$dres ) return PEAR::raiseError("Failed to create directory $dirname"); 00076 ftp_chmod($this->ftp, 0755, $dirname); 00077 } 00078 $res = ftp_put($this->ftp, $item['filename'], $fpath, FTP_BINARY); 00079 } 00080 00081 if ( !$res ) return PEAR::raiseError("Failed to put file ".$item['filename']); 00082 ftp_chmod($this->ftp, $item['mode'], $item['filename']); 00083 00084 } 00085 } 00086 $result = "Successfully installed at $destination"; 00087 return array('log'=>$log, 'result'=>$result); 00088 } 00089 00090 }
1.7.4