/**
  * @param $localPathAndFilename
  * @param $remotePathAndFilename
  * @throws \Touki\FTP\Exception\DirectoryException
  */
 public function download($localPathAndFilename, $remotePathAndFilename)
 {
     if (!$this->ftp) {
         $this->initializeFtp();
     }
     $remotePathAndFilename = '/' . $this->zone . '/' . $remotePathAndFilename;
     $file = $this->ftp->findFileByName($remotePathAndFilename);
     $this->ftp->download($localPathAndFilename, $file);
 }
Exemplo n.º 2
0
/**
 * make sure backup file is local, download it and make it local if necessary
 */
function backup_restore_locate_file($id, $path)
{
    global $amp_conf;
    $path = trim($path, '/');
    $path = str_replace(array('..', ':'), '', trim($path, '/'));
    $path = escapeshellcmd($path);
    $s = backup_get_server($id);
    if (!$s) {
        return array('error_msg' => _('Backup Server not found!'));
    }
    //dest is where we gona put backup files pulled infrom other servers
    $dest = $amp_conf['ASTSPOOLDIR'] . '/tmp/' . 'backuptmp-s' . $id . '-' . time() . '-' . basename($path);
    switch ($s['type']) {
        case 'local':
            $s['path'] = backup__($s['path']);
            $path = $s['path'] . '/' . $path;
            break;
        case 'ftp':
            //subsitute variables if nesesary
            $s['host'] = backup__($s['host']);
            $s['port'] = backup__($s['port']);
            $s['user'] = backup__($s['user']);
            $s['password'] = backup__($s['password']);
            $s['path'] = backup__($s['path']);
            $path = ltrim($path, '/');
            $connection = new Connection($s['host'], $s['user'], $s['password'], $s['port'], 90, $s['transfer'] == 'passive');
            try {
                $connection->open();
            } catch (\Exception $e) {
                $this->b['error'] = $e->getMessage();
                backup_log($this->b['error']);
                return;
            }
            $wrapper = new FTPWrapper($connection);
            $permFactory = new PermissionsFactory();
            $ftptype = $wrapper->systype();
            if (strtolower($ftptype) == "unix") {
                $fsFactory = new FilesystemFactory($permFactory);
            } else {
                $fsFactory = new WindowsFilesystemFactory();
            }
            $manager = new FTPFilesystemManager($wrapper, $fsFactory);
            $dlVoter = new DownloaderVoter();
            $dlVoter->addDefaultFTPDownloaders($wrapper);
            $ulVoter = new UploaderVoter();
            $ulVoter->addDefaultFTPUploaders($wrapper);
            $crVoter = new CreatorVoter();
            $crVoter->addDefaultFTPCreators($wrapper, $manager);
            $deVoter = new DeleterVoter();
            $deVoter->addDefaultFTPDeleters($wrapper, $manager);
            $ftp = new FTP($manager, $dlVoter, $ulVoter, $crVoter, $deVoter);
            if (!$ftp) {
                $this->b['error'] = _("Error creating the FTP object");
            }
            $ftpdirs = $ftp->findFilesystems(new \Touki\FTP\Model\Directory($s['path']));
            $file = null;
            foreach ($ftpdirs as $thisdir) {
                if ($ftp->fileExists(new \Touki\FTP\Model\File($thisdir->getRealPath() . '/' . $path))) {
                    $file = $ftp->findFileByName($thisdir->getRealPath() . '/' . $path);
                }
            }
            try {
                $options = array(FTP::NON_BLOCKING => false, FTP::TRANSFER_MODE => FTP_BINARY);
                $ftp->download($dest, $file, $options);
                $path = $dest;
            } catch (\Exception $e) {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
        case 'ssh':
            $s['path'] = backup__($s['path']);
            $s['user'] = backup__($s['user']);
            $s['host'] = backup__($s['host']);
            $cmd[] = fpbx_which('scp');
            $cmd[] = '-o StrictHostKeyChecking=no -i';
            $cmd[] = $s['key'];
            $cmd[] = '-P ' . $s['port'];
            $cmd[] = $s['user'] . '\\@' . $s['host'] . ':' . $s['path'] . '/' . $path;
            $cmd[] = $dest;
            exec(implode(' ', $cmd), $foo, $ret);
            unset($cmd);
            if ($ret === 0) {
                $path = $dest;
            } else {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
        case 'awss3':
            $s['bucket'] = backup__($s['bucket']);
            $s['awsaccesskey'] = backup__($s['awsaccesskey']);
            $s['awssecret'] = backup__($s['awssecret']);
            $awss3 = new S3($s['awsaccesskey'], $s['awssecret']);
            dbug('S3 Path: ' . $path);
            dbug('S3 Dest: ' . $dest);
            if ($awss3->getObject($s['bucket'], $path, $dest) !== false) {
                $path = $dest;
            } else {
                return array('error_msg' => _('Failed to retrieve file from server!'));
            }
            break;
    }
    if (file_exists($path)) {
        return $path;
    } else {
        return array('error_msg' => _('File not found! ' . $path));
    }
}