Example #1
0
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     $this->_metadata = null;
     if (!($this->_data = MFile::read($archive))) {
         $this->set('error.message', 'Unable to read archive');
         return MError::raiseWarning(100, $this->get('error.message'));
     }
     if (!$this->_getTarInfo($this->_data)) {
         return MError::raiseWarning(100, $this->get('error.message'));
     }
     for ($i = 0, $n = count($this->_metadata); $i < $n; $i++) {
         $type = strtolower($this->_metadata[$i]['type']);
         if ($type == 'file' || $type == 'unix file') {
             $buffer = $this->_metadata[$i]['data'];
             $path = MPath::clean($destination . '/' . $this->_metadata[$i]['name']);
             // Make sure the destination folder exists
             if (!MFolder::create(dirname($path))) {
                 $this->set('error.message', 'Unable to create destination');
                 return MError::raiseWarning(100, $this->get('error.message'));
             }
             if (MFile::write($path, $buffer) === false) {
                 $this->set('error.message', 'Unable to write entry');
                 return MError::raiseWarning(100, $this->get('error.message'));
             }
         }
     }
     return true;
 }
Example #2
0
 public static function extract($archivename, $extractdir)
 {
     mimport('framework.filesystem.file');
     mimport('framework.filesystem.folder');
     $untar = false;
     $result = false;
     $ext = MFile::getExt(strtolower($archivename));
     // Check if a tar is embedded...gzip/bzip2 can just be plain files!
     if (MFile::getExt(MFile::stripExt(strtolower($archivename))) == 'tar') {
         $untar = true;
     }
     switch ($ext) {
         case 'zip':
             $adapter = MArchive::getAdapter('zip');
             if ($adapter) {
                 $result = $adapter->extract($archivename, $extractdir);
             }
             break;
         case 'tar':
             $adapter = MArchive::getAdapter('tar');
             if ($adapter) {
                 $result = $adapter->extract($archivename, $extractdir);
             }
             break;
         case 'tgz':
             // This format is a tarball gzip'd
             $untar = true;
         case 'gz':
         case 'gzip':
             // This may just be an individual file (e.g. sql script)
             $adapter = MArchive::getAdapter('gzip');
             if ($adapter) {
                 $config = MFactory::getConfig();
                 $tmpfname = $config->get('tmp_path') . '/' . uniqid('gzip');
                 $gzresult = $adapter->extract($archivename, $tmpfname);
                 if ($gzresult instanceof Exception) {
                     @unlink($tmpfname);
                     return false;
                 }
                 if ($untar) {
                     // Try to untar the file
                     $tadapter = MArchive::getAdapter('tar');
                     if ($tadapter) {
                         $result = $tadapter->extract($tmpfname, $extractdir);
                     }
                 } else {
                     $path = MPath::clean($extractdir);
                     MFolder::create($path);
                     $result = MFile::copy($tmpfname, $path . '/' . MFile::stripExt(MFile::getName(strtolower($archivename))), null, 1);
                 }
                 @unlink($tmpfname);
             }
             break;
         case 'tbz2':
             // This format is a tarball bzip2'd
             $untar = true;
         case 'bz2':
         case 'bzip2':
             // This may just be an individual file (e.g. sql script)
             $adapter = MArchive::getAdapter('bzip2');
             if ($adapter) {
                 $config = MFactory::getConfig();
                 $tmpfname = $config->get('tmp_path') . '/' . uniqid('bzip2');
                 $bzresult = $adapter->extract($archivename, $tmpfname);
                 if ($bzresult instanceof Exception) {
                     @unlink($tmpfname);
                     return false;
                 }
                 if ($untar) {
                     // Try to untar the file
                     $tadapter = MArchive::getAdapter('tar');
                     if ($tadapter) {
                         $result = $tadapter->extract($tmpfname, $extractdir);
                     }
                 } else {
                     $path = MPath::clean($extractdir);
                     MFolder::create($path);
                     $result = MFile::copy($tmpfname, $path . '/' . MFile::stripExt(MFile::getName(strtolower($archivename))), null, 1);
                 }
                 @unlink($tmpfname);
             }
             break;
         default:
             MError::raiseWarning(10, MText::_('MLIB_FILESYSTEM_UNKNOWNARCHIVETYPE'));
             return false;
             break;
     }
     if (!$result || $result instanceof Exception) {
         return false;
     }
     return true;
 }
Example #3
0
 public static function upload($src, $dest, $use_streams = false)
 {
     // Ensure that the path is valid and clean
     $dest = MPath::clean($dest);
     // Create the destination directory if it does not exist
     $baseDir = dirname($dest);
     if (!file_exists($baseDir)) {
         mimport('framework.filesystem.folder');
         MFolder::create($baseDir);
     }
     if ($use_streams) {
         $stream = MFactory::getStream();
         if (!$stream->upload($src, $dest)) {
             MError::raiseWarning(21, MText::sprintf('MLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()));
             return false;
         }
         return true;
     } else {
         // Initialise variables.
         $FTPOptions = MClientHelper::getCredentials('ftp');
         $ret = false;
         if ($FTPOptions['enabled'] == 1) {
             // Connect the FTP client
             mimport('framework.client.ftp');
             $ftp = MFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
             // Translate path for the FTP account
             $dest = MPath::clean(str_replace(MPATH_ROOT, $FTPOptions['root'], $dest), '/');
             // Copy the file to the destination directory
             if (is_uploaded_file($src) && $ftp->store($src, $dest)) {
                 unlink($src);
                 $ret = true;
             } else {
                 MError::raiseWarning(21, MText::_('MLIB_FILESYSTEM_ERROR_WARNFS_ERR02'));
             }
         } else {
             if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) {
                 // Short circuit to prevent file permission errors
                 if (MPath::setPermissions($dest)) {
                     $ret = true;
                 } else {
                     MError::raiseWarning(21, MText::_('MLIB_FILESYSTEM_ERROR_WARNFS_ERR01'));
                 }
             } else {
                 MError::raiseWarning(21, MText::_('MLIB_FILESYSTEM_ERROR_WARNFS_ERR02'));
             }
         }
         return $ret;
     }
 }
Example #4
0
 private function _extractNative($archive, $destination, $options)
 {
     $zip = zip_open($archive);
     if (is_resource($zip)) {
         // Make sure the destination folder exists
         if (!MFolder::create($destination)) {
             $this->set('error.message', 'Unable to create destination');
             return false;
         }
         // Read files in the archive
         while ($file = @zip_read($zip)) {
             if (zip_entry_open($zip, $file, "r")) {
                 if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/") {
                     $buffer = zip_entry_read($file, zip_entry_filesize($file));
                     if (MFile::write($destination . '/' . zip_entry_name($file), $buffer) === false) {
                         $this->set('error.message', 'Unable to write entry');
                         return false;
                     }
                     zip_entry_close($file);
                 }
             } else {
                 $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_UNABLE_TO_READ_ENTRY'));
                 return false;
             }
         }
         @zip_close($zip);
     } else {
         $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_UNABLE_TO_OPEN_ARCHIVE'));
         return false;
     }
     return true;
 }
Example #5
0
 protected function initFile()
 {
     // If the file doesn't already exist we need to create it and generate the file header.
     if (!is_file($this->path)) {
         // Make sure the folder exists in which to create the log file.
         MFolder::create(dirname($this->path));
         // Build the log file header.
         $head = $this->generateFileHeader();
     } else {
         $head = false;
     }
     // Open the file for writing (append mode).
     if (!($this->file = fopen($this->path, 'a'))) {
         // Throw exception.
     }
     if ($head) {
         if (!fputs($this->file, $head)) {
             throw new LogException();
         }
     }
 }