Beispiel #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;
 }
Beispiel #2
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;
 }
Beispiel #3
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;
 }
Beispiel #4
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;
 }
Beispiel #5
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;
 }
Beispiel #6
0
 /**
  * @inheritdoc
  */
 public function run(&$cmdParams, &$params)
 {
     $controller = $this->controller;
     $taskRunner = $this->taskRunner;
     $res = true;
     $toBeUploaded = [];
     $this->_connectionId = !empty($cmdParams[0]) ? $cmdParams[0] : $this->_connectionId;
     $srcList = !empty($cmdParams[1]) ? $cmdParams[1] : [];
     $this->_destDir = !empty($cmdParams[2]) ? $taskRunner->parsePath($cmdParams[2]) : '';
     $this->_srcBaseDir = !empty($cmdParams[3]) ? $taskRunner->parsePath($cmdParams[3]) : '';
     $this->_overwrite = !empty($cmdParams[4]) ? $cmdParams[4] : $this->_overwrite;
     $this->_options = !empty($cmdParams[5]) ? $cmdParams[5] : $this->_options;
     if (empty($this->_connectionId) || empty($this->_destDir)) {
         Log::throwException('sftpPut: Please specify a valid connection id and directory');
     }
     if (empty($srcList) || empty($this->_srcBaseDir)) {
         Log::throwException('sftpPut: srcList and srcBaseDir cannot be empty');
     }
     /** @noinspection PhpUndefinedMethodInspection (provided by the SftpConnectReqs Behavior) */
     $connParams = $controller->getConnectionParams($this->_connectionId);
     $controller->stdout(" " . $this->_connectionId . " ", $connParams['sftpLabelColor'], Console::FG_BLACK);
     $controller->stdout(' Uploading files to ');
     $controller->stdout($this->_destDir, Console::FG_CYAN);
     if ($controller->dryRun) {
         $controller->stdout(' [dry run]', Console::FG_YELLOW);
     }
     if (is_string($srcList) && $srcList === '*') {
         $list = glob($this->_srcBaseDir . DIRECTORY_SEPARATOR . '*');
         foreach ($list as $srcFullPath) {
             $srcRelPath = substr($srcFullPath, strlen($this->_srcBaseDir) + 1);
             $toBeUploaded[$srcRelPath] = $srcFullPath;
         }
     } else {
         // if $srcList is specified but it is a string, we convert it to an array
         if (!empty($srcList) && is_string($srcList)) {
             $srcList = explode(',', $srcList);
         }
         foreach ($srcList as $srcPath) {
             $parsedPath = $taskRunner->parseStringParams(trim($srcPath));
             $toBeUploaded[$parsedPath] = $this->_srcBaseDir . DIRECTORY_SEPARATOR . $parsedPath;
         }
     }
     if (!$controller->dryRun) {
         // the getConnection method is provided by the SftpConnectReqs Behavior
         /** @noinspection PhpUndefinedMethodInspection */
         $this->_connection = $controller->getConnection($this->_connectionId);
         $this->_sftpHelper = new SftpHelper($this->_connectionId, $this->_connection, $connParams);
         if (!is_dir($this->_srcBaseDir) || !$this->_sftpHelper->isDir($this->_destDir)) {
             $this->controller->stdout("\n");
             Log::throwException('sftpPut: Base and destination have to be directories');
         }
     }
     foreach ($toBeUploaded as $srcRelPath => $srcFullPath) {
         if (file_exists($srcFullPath)) {
             $this->_put($srcFullPath, $srcRelPath);
         } else {
             $this->controller->stdout("\n");
             $this->controller->stderr("{$srcFullPath} does not exists!\n", Console::FG_RED);
         }
     }
     if (!$controller->dryRun) {
         $this->_sftpHelper->flushCache();
     }
     $controller->stdout("\n");
     return $res;
 }