/**
  * Fetch a file or if no FileName set all files found at remote location.
  *
  * @SideEffects:
  *  Writes files to local filesystem
  *
  * If all files then don't overwrite existing files, otherwise if a single file then overwrite it every time.
  *
  * @return int number of files fetched
  */
 public function execute()
 {
     $this->checkPerm();
     $transport = Replicant::transportFactory($this->Protocol, $this->RemoteHost, $this->Proxy, $this->UserName, $this->Password);
     // if we have a FileName then only enqueue that file, otherwise get a list of files from remote host and enqueue all for fetching (existing files won't be refetched in this case).
     if (!$this->FileName) {
         $this->step("Fetching file list from '{$this->Protocol}://{$this->UserName}@{$this->RemoteHost}/{$this->Path}'");
         try {
             $files = $transport->fetchFileList($this->Path);
         } catch (Exception $e) {
             $this->failed("Failed to get file list: " . $e->getMessage());
             return 0;
         }
     } else {
         $fullPath = FileSystemTools::build_path($this->Path, $this->FileName);
         $this->step("Enqueuing file '{$fullPath}'");
         // create the files array as a single entry with path and name
         $files = array(array('Path' => $this->Path, 'FileName' => $this->FileName));
     }
     $numFiles = count($files);
     $numFetched = 0;
     $this->step("Fetching #{$numFiles} files");
     foreach ($files as $fileInfo) {
         // strip off extension here or alpha will reject request
         $fileName = $fileInfo['FileName'];
         $remotePathName = FileSystemTools::build_path(Replicant::config()->get('remote_path'), basename($fileName));
         $localPathName = FileSystemTools::build_path(Replicant::asset_path(), $fileName);
         $overwrite = $this->FileName != '';
         $this->step("Fetching file '{$remotePathName}' with overwrite (" . ($overwrite ? "set" : "not set") . ")");
         try {
             if (false !== $transport->fetchFile($remotePathName, $localPathName, $overwrite)) {
                 $numFetched++;
             }
         } catch (Exception $e) {
             $this->failed("Failed to fetch file '{$remotePathName}': " . $e->getMessage());
             // EARLY EXIT!
             return false;
         }
     }
     $this->success("Fetched #{$numFetched} files of #{$numFiles}");
     return $numFetched;
 }
 /**
  * Update with some sensible defaults.
  * @param array $data
  * @return DataObject
  */
 public function update($data)
 {
     return parent::update(CollectionTools::options_from_array($data, array('RemoteHost' => $_SERVER['SERVER_NAME'], 'Protocol' => $_SERVER['SERVER_PROTOCOL'], 'Proxy' => '', 'Path' => Replicant::asset_path(), 'UserName' => Member::currentUser()->Email)));
 }
 /**
  * Update with some sensible defaults.
  * @param array $data
  * @return DataObject
  */
 public function update($data)
 {
     return parent::update(CollectionTools::options_from_array($data, array('RemoteHost' => $_SERVER['SERVER_NAME'], 'Protocol' => $_SERVER['SERVER_PROTOCOL'], 'Proxy' => '', 'Path' => Replicant::asset_path(), 'FileName' => null, 'Database' => DatabaseTools::getDBCredential('Database'))));
 }
 /**
  * Override update with some usefull defaults and checks
  * @param array $data
  * @return DataObject
  */
 public function update($data)
 {
     return parent::update(CollectionTools::options_from_array($data, array('RemoteHost' => null, 'Protocol' => 'http', 'Proxy' => '', 'Database' => DatabaseTools::getDBCredential('Database'), 'UserName' => Member::currentUser() ? Member::currentUser()->Email : $_SERVER['PHP_AUTH_USER'], 'Password' => '', 'Path' => Replicant::asset_path(), 'FileName' => FileSystemTools::filename_from_timestamp('.sql'))));
 }
 /**
  * Fetch one or all remote dump files and writes to local filesystem.
  *
  * If filename is supplied as getVar then only that file will be retrieved, otherwise all files which don't exist locally will be retrieved up to number getVar.
  *
  * If filename is supplied as getVar then file will overwrite existing file.
  *
  * SideEffects:
  *  Reads files from remote system.
  *  Writes files to local filesystem.
  *  Outputs results
  *
  * @param SS_HTTPRequest $request
  * @return int number of files fetched
  * @throws PermissionFailureException
  */
 public function fetch(SS_HTTPRequest $request)
 {
     $options = CollectionTools::options_from_array($request->getVars(), array('RemoteHost' => $request->getIP(), 'Path' => Replicant::asset_path(), 'FileName' => '', 'UserName' => null, 'Password' => null));
     $action = ReplicantActionFetch::create();
     $action->checkPerm()->update($options)->execute();
     return $action->format();
 }