Example #1
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $res = true;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $dir = !empty($cmdParams[1]) ? $cmdParams[1] : '';
     $mode = !empty($cmdParams[2]) ? $cmdParams[2] : -1;
     $recursive = !empty($cmdParams[3]) ? $cmdParams[3] : true;
     if (empty($connectionId) || empty($dir)) {
         Log::throwException('sftpMkdir: Please specify a valid connection id and directory');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Creating directory ');
     $controller->stdout($dir, Console::FG_CYAN);
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         $res = $sftpHelper->mkdir($dir, $mode, $recursive);
         $sftpHelper->flushCache();
     } else {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $controller->stdout("\n");
     return $res;
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $res = true;
     $list = [];
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $dir = !empty($cmdParams[1]) ? $cmdParams[1] : '.';
     $recursive = !empty($cmdParams[2]) ? $cmdParams[2] : false;
     $varName = !empty($cmdParams[3]) ? $cmdParams[3] : $connectionId . '.list';
     if (empty($connectionId)) {
         Log::throwException('sftpList: Please specify a valid connection id');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Listing directory ');
     $controller->stdout($dir, Console::FG_CYAN);
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         $list = $sftpHelper->nlist($dir, $recursive, true);
         // TODO: use SftpHelper to store the list
     } else {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $params[$varName] = $list;
     $controller->stdout("\n");
     return $res;
 }
Example #3
0
 private function _get($remotePath, $destRelPathArr = [])
 {
     // Remove the first element from the $destRelPathArr since it corresponds to the destination path
     $destSubFoldersArr = $destRelPathArr;
     array_shift($destSubFoldersArr);
     $destRelPath = implode(DIRECTORY_SEPARATOR, $destSubFoldersArr);
     $localFolderName = basename($this->_destPath);
     $localFolderName .= !empty($destRelPath) ? DIRECTORY_SEPARATOR . $destRelPath : '';
     $destPath = $this->_destPath;
     $destPath .= !empty($destSubFoldersArr) ? DIRECTORY_SEPARATOR . $destRelPath : '';
     $this->controller->stdout("\n - {$localFolderName}\t <= {$remotePath}");
     if (!$this->controller->dryRun) {
         if ($this->_sftpHelper->isDir($remotePath)) {
             if (!is_dir($this->_destPath)) {
                 $this->controller->stdout("\n");
                 Log::throwException('sftpGet: remotePath is a directory, therefore destination has to be a directory too');
             }
             $list = $this->_sftpHelper->nlist($remotePath);
             $remoteDirName = basename($remotePath);
             $newSubDir = $destPath . DIRECTORY_SEPARATOR . $remoteDirName;
             $destRelPathArr[] = $remoteDirName;
             // Creating the sub-directory before recursion to allow creation of empty directories
             // (excluding the first level as it corresponds to the destination path)
             if (count($destRelPathArr) > 1 && !is_dir($newSubDir)) {
                 FileHelper::createDirectory($newSubDir);
             }
             foreach ($list as $item) {
                 if ($item !== '.' && $item !== '..') {
                     $this->_get($remotePath . '/' . $item, $destRelPathArr);
                 }
             }
         } elseif (!empty($destSubFoldersArr) || $this->_sftpHelper->fileExists($remotePath)) {
             // if $destSubFoldersArr is not empty it means that we've got the list of files
             // from the nlist command on the remote directory; therefore we don't need to check
             // if the file exists
             $destFile = $destPath;
             if (is_dir($destFile)) {
                 $destFile = $destPath . DIRECTORY_SEPARATOR . basename($remotePath);
             }
             if ($this->_overwrite || !file_exists($destFile)) {
                 $this->_sftpHelper->get($remotePath, $destFile);
             } else {
                 $this->controller->stdout(' [skipped]', Console::FG_PURPLE);
             }
         } else {
             $this->controller->stdout("\n");
             $this->controller->warn('Not found: ' . $remotePath);
         }
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
 }
Example #4
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $taskRunner = $this->taskRunner;
     $controller = $this->controller;
     $res = true;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $execCommand = !empty($cmdParams[1]) ? $cmdParams[1] : '';
     $execParams = !empty($cmdParams[2]) ? $taskRunner->parseStringParams($cmdParams[2]) : '';
     $execHiddenParams = !empty($cmdParams[3]) ? $cmdParams[3] : '';
     // not printed out
     if (empty($connectionId) || empty($execCommand)) {
         Log::throwException('sftpExec: Please specify a valid connection id and directory');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     $cmdString = trim($execCommand . ' ' . $execParams);
     $cmdFull = trim($execCommand . ' ' . $execParams . ' ' . $execHiddenParams);
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         $execOutput = $sftpHelper->exec($cmdFull);
         $execResult = $sftpHelper->getExecExitStatus();
     } else {
         $execResult = 0;
         $execOutput = '';
     }
     $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(" ");
     if ($execResult !== 0) {
         $this->controller->stderr("Error running " . $cmdString . " ({$execResult})\n", Console::FG_RED);
     } else {
         $this->controller->stdout('Running shell command: ');
         $this->controller->stdout($execCommand . ' ', Console::FG_YELLOW);
         $this->controller->stdout($execParams, Console::FG_BLUE);
         if (!empty($execOutput)) {
             $this->controller->stdout("\n" . '---------------------------------------------------------------' . "\n");
             $this->controller->stdout(trim($execOutput));
             $this->controller->stdout("\n" . '---------------------------------------------------------------' . "\n");
         } elseif ($this->controller->dryRun) {
             $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
         }
     }
     $controller->stdout("\n");
     return $res;
 }
Example #5
0
 private function _put($srcFullPath, $srcRelPath)
 {
     $destRelPath = $this->_destDir . '/' . $srcRelPath;
     $this->controller->stdout("\n - " . $srcRelPath . "\t => " . $destRelPath);
     if (!$this->controller->dryRun) {
         if (is_dir($srcFullPath)) {
             $files = FileHelper::findFiles($srcFullPath, $this->_options);
             foreach ($files as $foundPath) {
                 $relativePath = substr($foundPath, strlen($this->_srcBaseDir) + 1);
                 $this->_sftpHelper->mkdir($destRelPath);
                 $this->_put($foundPath, $relativePath, $this->_srcBaseDir, $this->_destDir, $this->_options);
             }
         } elseif (FileHelper::filterPath($srcFullPath, $this->_options)) {
             $this->_sftpHelper->mkdir(dirname($destRelPath), -1, true);
             if ($this->_overwrite || !$this->_sftpHelper->fileExists($destRelPath)) {
                 $res = $this->_sftpHelper->put($destRelPath, $srcFullPath);
                 if (!$res) {
                     Log::logger()->addError('sftpPut: error uploading file {from} to {to}', ['from' => $srcFullPath, 'to' => $destRelPath]);
                 }
             } else {
                 $this->controller->stdout(' [skipped]', Console::FG_PURPLE);
             }
         }
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
 }
Example #6
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $res = true;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $permList = !empty($cmdParams[1]) ? $cmdParams[1] : [];
     if (empty($connectionId)) {
         Log::throwException('sftpChmod: Please specify a valid connection id');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     foreach ($permList as $mode => $pathList) {
         $mode = is_string($mode) ? octdec((int) $mode) : $mode;
         foreach ($pathList as $path) {
             //                $path = $taskRunner->parsePath($path); // TODO: parse parameters
             $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
             $controller->stdout(" Changing permissions of {$path} to ");
             $controller->stdout('0' . decoct($mode), Console::FG_CYAN);
             if (!$this->controller->dryRun) {
                 // the getConnection method is provided by the SftpConnectReqs Behavior
                 /** @noinspection PhpUndefinedMethodInspection */
                 /** @var $connection Net_SFTP|resource */
                 $connection = $controller->getConnection($connectionId);
                 $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
                 $res = $sftpHelper->chmod($mode, $path, false);
             } else {
                 $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
             }
             $this->controller->stdout("\n");
         }
     }
     if (!$this->controller->dryRun) {
         /** @noinspection PhpUndefinedVariableInspection */
         $sftpHelper->flushCache('stat');
     }
     return $res;
 }
Example #7
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $res = true;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $dir = !empty($cmdParams[1]) ? $cmdParams[1] : '';
     $recursive = !empty($cmdParams[2]) ? $cmdParams[2] : false;
     if (empty($connectionId) || empty($dir)) {
         Log::throwException('sftpRmdir: Please specify a valid connection id and directory');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Removing directory ' . ($recursive ? '(recursive) ' : ''));
     $controller->stdout($dir, Console::FG_CYAN);
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if (!$recursive) {
             $res = $sftpHelper->rmdir($dir);
         } else {
             $res = $sftpHelper->delete($dir, true);
         }
         if (!$res) {
             Log::logger()->addError('sftpRmdir: error removing directory {dir} (recursive: {recursive})', ['dir' => $dir, 'recursive' => $recursive ? 'yes' : 'no']);
         }
         $sftpHelper->flushCache();
     } else {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $controller->stdout("\n");
     return $res;
 }
Example #8
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $res = true;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $file = !empty($cmdParams[1]) ? $cmdParams[1] : '';
     if (empty($connectionId) || empty($file)) {
         Log::throwException('sftpRm: Please specify a valid connection id and file');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($connectionId);
     $controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Removing file ');
     $controller->stdout($file, Console::FG_CYAN);
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if ($sftpHelper->isFile($file)) {
             $res = $sftpHelper->delete($file, false);
             if (!$res) {
                 Log::logger()->addError('sftpRm: error removing file {file}', ['file' => $file]);
             }
         } else {
             $controller->stdout("\n");
             $controller->warn('sftpRm: ' . $file . ' is not a file');
         }
         $sftpHelper->flushCache();
     } else {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $controller->stdout("\n");
     return $res;
 }
Example #9
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $res = true;
     $taskRunner = $this->taskRunner;
     $connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : '';
     $pathFrom = !empty($cmdParams[1]) ? $taskRunner->parsePath($cmdParams[1]) : '';
     $pathTo = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : '';
     $overwrite = !empty($cmdParams[3]) ? $cmdParams[3] : false;
     // TODO: check if overwrite is needed after implementing the FTP support
     if (empty($pathFrom) || empty($pathTo)) {
         throw new Exception('sftpMv: Origin and destination cannot be empty');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $this->controller->getConnectionParams($connectionId);
     $this->controller->stdout(" " . $connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $this->controller->stdout(" Move (overwrite: " . ($overwrite ? 'yes' : 'no') . ") \n " . $pathFrom . " to \t " . $pathTo);
     $insidePathTo = $pathTo . '/' . basename($pathFrom);
     if (!$this->controller->dryRun) {
         /** @noinspection PhpUndefinedMethodInspection */
         /** @var $connection Net_SFTP|resource */
         $connection = $this->controller->getConnection($connectionId);
         $sftpHelper = new SftpHelper($connectionId, $connection, $connParams);
         if (!$sftpHelper->fileExists($pathFrom)) {
             $this->controller->stdout("\n");
             $this->controller->stderr("Not found: {$pathFrom}", Console::FG_RED);
         } else {
             if ($sftpHelper->isFile($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 $pathTo = $insidePathTo;
             }
             if (!$overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo)) {
                 if (!$sftpHelper->fileExists($insidePathTo)) {
                     // not overwriting; copy the source directory into the destination folder:
                     $res = $sftpHelper->rename($pathFrom, $insidePathTo);
                 } else {
                     $this->controller->stdout("\n");
                     $this->controller->warn("Destination directory {$insidePathTo} already exists; not overwriting");
                 }
             } elseif (!$overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->warn("Destination file {$pathTo} already exists; not overwriting");
             } elseif ($sftpHelper->isDir($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 $this->controller->stdout("\n");
                 $this->controller->stderr("Trying to move a directory to a file: {$pathTo}", Console::FG_RED);
             } elseif (!$sftpHelper->fileExists($pathTo) || $overwrite && $sftpHelper->isDir($pathFrom) && $sftpHelper->isDir($pathTo) || $overwrite && $sftpHelper->isFile($pathFrom) && $sftpHelper->isFile($pathTo)) {
                 // if destination exists, overwrite it with the source file/dir
                 // note: if pathTo is a directory, it has to be empty in order to be overwritten
                 try {
                     $res = $sftpHelper->rename($pathFrom, $pathTo);
                 } catch (ErrorException $e) {
                     $this->controller->stdout("\n");
                     $this->controller->warn($e->getMessage());
                 }
                 if (!$res) {
                     // Note: using sftp, renaming a directory to another does not work even if destination is empty
                     $this->controller->stdout("\n");
                     $this->controller->stderr("Failed moving {$pathFrom} to {$pathTo}", Console::FG_RED);
                 }
             }
         }
         $sftpHelper->flushCache();
     } else {
         $this->controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     $this->controller->stdout("\n");
     return $res;
 }