/**
  * @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);
 }
 function store_backup()
 {
     foreach ($this->b['storage_servers'] as $s) {
         $s = $this->s[$s];
         switch ($s['type']) {
             case 'local':
                 $path = backup__($s['path']) . '/' . $this->b['_dirname'];
                 //ensure directory structure
                 if (!is_dir($path)) {
                     mkdir($path, 0755, true);
                 }
                 //would rather use the native copy() here, but by defualt
                 //php doesnt support files > 2GB
                 //see here for a posible solution:
                 //http://ca3.php.net/manual/en/function.fopen.php#37791
                 $cmd[] = fpbx_which('cp');
                 $cmd[] = $this->b['_tmpfile'];
                 $cmd[] = $path . '/' . $this->b['_file'] . '.tgz';
                 exec(implode(' ', $cmd), $error, $status);
                 unset($cmd, $error);
                 if ($status !== 0) {
                     $this->b['error'] = 'Error copying ' . $this->b['_tmpfile'] . ' to ' . $path . '/' . $this->b['_file'] . '.tgz: ' . $error;
                     backup_log($this->b['error']);
                 }
                 //run maintenance on the directory
                 $this->maintenance($s['type'], $s);
                 break;
             case 'email':
                 //TODO: set agent to something informative, including fpbx & backup versions
                 $email_options = array('useragent' => 'freepbx', 'protocol' => 'mail');
                 $email = new \CI_Email();
                 //Generic email
                 $from = '*****@*****.**';
                 //If we have sysadmin and "from is set"
                 if (function_exists('sysadmin_get_storage_email')) {
                     $emails = sysadmin_get_storage_email();
                     //Check that what we got back above is a email address
                     if (!empty($emails['fromemail']) && filter_var($emails['fromemail'], FILTER_VALIDATE_EMAIL)) {
                         $from = $emails['fromemail'];
                     }
                 }
                 //If the user set an email in advanced settings it wins, otherwise take whatever won above.
                 $from = filter_var($this->amp_conf['AMPBACKUPEMAILFROM'], FILTER_VALIDATE_EMAIL) ? $this->amp_conf['AMPBACKUPEMAILFROM'] : $from;
                 $msg[] = _('Name') . ': ' . $this->b['name'];
                 $msg[] = _('Created') . ': ' . date('r', $this->b['_ctime']);
                 $msg[] = _('Files') . ': ' . $this->manifest['file_count'];
                 $msg[] = _('Mysql Db\'s') . ': ' . $this->manifest['mysql_count'];
                 $msg[] = _('astDb\'s') . ': ' . $this->manifest['astdb_count'];
                 $email->from($from);
                 $email->to(backup__($s['addr']));
                 $email->subject($this->amp_conf['FREEPBX_SYSTEM_IDENT'] . ' ' . _('Backup') . ' ' . $this->b['name']);
                 $body = implode("\n", $msg);
                 // If the backup file is more than 25MB, yell
                 $encodedsize = ceil(filesize($this->b['_tmpfile']) / 3) * 4;
                 if ($encodedsize > 26214400) {
                     $email->subject($this->amp_conf['FREEPBX_SYSTEM_IDENT'] . ' ' . _('Backup ERROR (exceeded SMTP limits)') . ' ' . $this->b['name']);
                     $email->message(_('BACKUP NOT ATTACHED') . "\n" . _('The backup file exceeded the maximum SMTP limits of 25MB. It was not attempted to be sent. Please shrink your backup, or use a different method of transferring your backup.') . "\n{$body}\n");
                 } elseif ($encodedsize > $s['maxsize']) {
                     $email->subject($this->amp_conf['FREEPBX_SYSTEM_IDENT'] . ' ' . _('Backup ERROR (exceeded soft limit)') . ' ' . $this->b['name']);
                     $email->message(_('BACKUP NOT ATTACHED') . "\n" . _('The backup file exceeded the soft limit set in SMTP configuration (%s bytes). It was not attempted to be sent. Please shrink your backup, or use a different method of transferring your backup.') . "\n{$body}\n");
                 } else {
                     $email->message($body);
                     $email->attach($this->b['_tmpfile']);
                 }
                 $email->send();
                 unset($msg);
                 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'] = trim(backup__($s['path']), '/');
                 $fstype = isset($s['fstype']) ? $s['fstype'] : 'auto';
                 $path = $s['path'] . '/' . $this->b['_dirname'];
                 $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();
                 switch ($fstype) {
                     case 'auto':
                         $ftptype = $wrapper->systype();
                         if (strtolower($ftptype) == "unix") {
                             $fsFactory = new FilesystemFactory($permFactory);
                         } else {
                             $fsFactory = new WindowsFilesystemFactory();
                         }
                         break;
                     case 'unix':
                         $fsFactory = new FilesystemFactory($permFactory);
                         break;
                     case 'windows':
                         $fsFactory = new WindowsFilesystemFactory();
                         break;
                 }
                 $manager = new FTPFilesystemManager($wrapper, $fsFactory);
                 $dlVoter = new DownloaderVoter();
                 $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");
                     backup_log($this->b['error']);
                     return;
                 }
                 if (!$ftp->directoryExists(new Directory($path))) {
                     backup_log(sprintf(_("Creating directory '%s'"), $path));
                     try {
                         $ftp->create(new Directory($path), array(FTP::RECURSIVE => true));
                     } catch (\Exception $e) {
                         $this->b['error'] = sprintf(_("Directory '%s' did not exist and we could not create it"), $path);
                         backup_log($this->b['error']);
                         backup_log($e->getMessage());
                         return;
                     }
                 }
                 try {
                     backup_log(_("Saving file to remote ftp"));
                     $ftp->upload(new File($path . '/' . $this->b['_file'] . '.tgz'), $this->b['_tmpfile']);
                 } catch (\Exception $e) {
                     $this->b['error'] = _("Unable to upload file to the remote server");
                     backup_log($this->b['error']);
                     backup_log($e->getMessage());
                     return;
                 }
                 //run maintenance on the directory
                 $this->maintenance($s['type'], $path, $ftp);
                 break;
             case 'awss3':
                 //subsitute variables if nesesary
                 $s['bucket'] = backup__($s['bucket']);
                 $s['awsaccesskey'] = backup__($s['awsaccesskey']);
                 $s['awssecret'] = backup__($s['awssecret']);
                 $awss3 = new \S3($s['awsaccesskey'], $s['awssecret']);
                 // Does this bucket already exist?
                 $buckets = $awss3->listBuckets();
                 if (!in_array($s['bucket'], $buckets)) {
                     // Create the bucket
                     $awss3->putBucket($s['bucket'], \S3::ACL_PUBLIC_READ);
                 }
                 //copy file
                 if ($awss3->putObjectFile($this->b['_tmpfile'], $s['bucket'], $this->b['name'] . "/" . $this->b['_file'] . '.tgz', \S3::ACL_PUBLIC_READ)) {
                     dbug('S3 successfully uploaded your backup file.');
                 } else {
                     dbug('S3 failed to accept your backup file');
                 }
                 //run maintenance on the directory
                 $this->maintenance($s['type'], $s, $awss3);
                 break;
             case 'ssh':
                 //subsitute variables if nesesary
                 $s['path'] = backup__($s['path']);
                 $s['user'] = backup__($s['user']);
                 $s['host'] = backup__($s['host']);
                 $destdir = $s['path'] . '/' . $this->b['_dirname'];
                 //ensure directory structure
                 $cmd = fpbx_which('ssh') . ' -o StrictHostKeyChecking=no -i ';
                 $cmd .= $s['key'] . " -l " . $s['user'] . ' ' . $s['host'] . ' -p ' . $s['port'];
                 $cmd .= " 'mkdir -p {$destdir}'";
                 exec($cmd, $output, $ret);
                 if ($ret !== 0) {
                     backup_log("SSH Error ({$ret}) - Received " . json_encode($output) . " from {$cmd}");
                 }
                 $output = null;
                 //put file
                 // Note that SCP (*unlike SSH*) needs IPv6 addresses in ['s. Consistancy is awesome.
                 if (filter_var($s['host'], \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6)) {
                     $scphost = "[" . $s['host'] . "]";
                 } else {
                     $scphost = $s['host'];
                 }
                 $cmd = fpbx_which('scp') . ' -o StrictHostKeyChecking=no -i ' . $s['key'] . ' -P ' . $s['port'];
                 $cmd .= " " . $this->b['_tmpfile'] . " " . $s['user'] . "@{$scphost}:{$destdir}";
                 exec($cmd, $output, $ret);
                 if ($ret !== 0) {
                     backup_log("SCP Error ({$ret}) - Received " . json_encode($output) . " from {$cmd}");
                 }
                 //run maintenance on the directory
                 $this->maintenance($s['type'], $s);
                 break;
         }
     }
 }
Beispiel #3
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));
    }
}