/** * @throws \Touki\FTP\Exception\ConnectionEstablishedException * @throws \Touki\FTP\Exception\ConnectionException */ private function initializeFtp() { $connection = new Connection($this->host, $this->user, $this->pass, 21); $connection->open(); /** * The wrapper is a simple class which wraps the base PHP ftp_* functions * It needs a Connection instance to get the related stream */ $wrapper = new FTPWrapper($connection); $wrapper->pasv(TRUE); /** * This factory creates Permissions models from a given permission string (rw-) */ $permFactory = new PermissionsFactory(); /** * This factory creates Filesystem models from a given string, ex: * drwxr-x--- 3 vincent vincent 4096 Jul 12 12:16 public_ftp * * It needs the PermissionsFactory so as to instanciate the given permissions in * its model */ $fsFactory = new FilesystemFactory($permFactory); /** * If your server runs on WINDOWS, you can use a Windows filesystem factory instead */ // $fsFactory = new WindowsFilesystemFactory; /** * This manager focuses on operations on remote files and directories * It needs the FTPWrapper so as to do operations on the serveri * It needs the FilesystemFfactory so as to create models */ $manager = new FTPFilesystemManager($wrapper, $fsFactory); /** * This is the downloader voter. It loads multiple DownloaderVotable class and * checks which one is needed on given options */ $dlVoter = new DownloaderVoter(); /** * Loads up default FTP Downloaders * It needs the FTPWrapper to be able to share them with the downloaders */ $dlVoter->addDefaultFTPDownloaders($wrapper); /** * This is the uploader voter. It loads multiple UploaderVotable class and * checks which one is needed on given options */ $ulVoter = new UploaderVoter(); /** * Loads up default FTP Uploaders * It needs the FTPWrapper to be able to share them with the uploaders */ $ulVoter->addDefaultFTPUploaders($wrapper); /** * This is the creator voter. It loads multiple CreatorVotable class and * checks which one is needed on the given options */ $crVoter = new CreatorVoter(); /** * Loads up the default FTP creators. * It needs the FTPWrapper and the FTPFilesystemManager to be able to share * them whith the creators */ $crVoter->addDefaultFTPCreators($wrapper, $manager); /** * This is the deleter voter. It loads multiple DeleterVotable classes and * checks which one is needed on the given options */ $deVoter = new DeleterVoter(); /** * Loads up the default FTP deleters. * It needs the FTPWrapper and the FTPFilesystemManager to be able to share * them with the deleters */ $deVoter->addDefaultFTPDeleters($wrapper, $manager); /** * Finally creates the main FTP * It needs the manager to do operations on files * It needs the download voter to pick-up the right downloader on ->download * It needs the upload voter to pick-up the right uploader on ->upload * It needs the creator voter to pick-up the right creator on ->create * It needs the deleter voter to pick-up the right deleter on ->delete */ $this->ftp = new FTP($manager, $dlVoter, $ulVoter, $crVoter, $deVoter); }
/** * 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)); } }