123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- namespace ZN\Services;
- class __USE_STATIC_ACCESS__SSH implements SSHInterface
- {
- //----------------------------------------------------------------------------------------------------
- //
- // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
- // Site : www.zntr.net
- // Lisans : The MIT License
- // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
- //
- //----------------------------------------------------------------------------------------------------
-
- //----------------------------------------------------------------------------------------------------
- // Const CONFIG_NAME
- //----------------------------------------------------------------------------------------------------
- //
- // @const string
- //
- //----------------------------------------------------------------------------------------------------
- const CONFIG_NAME = 'Services:ssh';
-
- //----------------------------------------------------------------------------------------------------
- // Protected $connect
- //----------------------------------------------------------------------------------------------------
- //
- // @const resource
- //
- //----------------------------------------------------------------------------------------------------
- protected $connect = NULL;
-
- //----------------------------------------------------------------------------------------------------
- // Protected $login
- //----------------------------------------------------------------------------------------------------
- //
- // @const resource
- //
- //----------------------------------------------------------------------------------------------------
- protected $login = NULL;
-
- //----------------------------------------------------------------------------------------------------
- // Protected $stream
- //----------------------------------------------------------------------------------------------------
- //
- // @const resource
- //
- //----------------------------------------------------------------------------------------------------
- protected $stream = NULL;
-
- //----------------------------------------------------------------------------------------------------
- // Protected $command
- //----------------------------------------------------------------------------------------------------
- //
- // @const string
- //
- //----------------------------------------------------------------------------------------------------
- protected $command = '';
-
- //----------------------------------------------------------------------------------------------------
- // __construct()
- //----------------------------------------------------------------------------------------------------
- //
- // @param array $config: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function __construct($config = [])
- {
- if( ! function_exists('ssh2_connect') )
- {
- die(getErrorMessage('Error', 'undefinedFunctionExtension', 'SSH(Secure Shell)'));
- }
-
- $this->config($config);
- $this->connect();
- }
-
- //----------------------------------------------------------------------------------------------------
- // Call Undefined Method
- //----------------------------------------------------------------------------------------------------
- //
- // __call()
- //
- //----------------------------------------------------------------------------------------------------
- use \CallUndefinedMethodTrait;
-
- //----------------------------------------------------------------------------------------------------
- // Config Method
- //----------------------------------------------------------------------------------------------------
- //
- // config()
- //
- //----------------------------------------------------------------------------------------------------
- use \ConfigMethodTrait;
-
- //----------------------------------------------------------------------------------------------------
- // Error Control
- //----------------------------------------------------------------------------------------------------
- //
- // $error
- // $success
- //
- // error()
- // success()
- //
- //----------------------------------------------------------------------------------------------------
- use \ErrorControlTrait;
-
- //----------------------------------------------------------------------------------------------------
- // connect()
- //----------------------------------------------------------------------------------------------------
- //
- // @param array $config: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function connect($config = [])
- {
- if( ! is_array($config) )
- {
- \Errors::set('Error', 'arrayParameter', 'config');
-
- return $this;
- }
-
- if( ! empty($config) )
- {
- $this->config($config);
- }
-
- // Config/Ftp.php dosyasından ftp ayarları alınıyor.
- $config = $this->config;
-
- // ----------------------------------------------------------------------------
- // SSH BAĞLANTI AYARLARI YAPILANDIRILIYOR
- // ----------------------------------------------------------------------------
- $host = $config['host'];
- $port = $config['port'];
- $user = $config['user'];
- $password = $config['password'];
- $methods = $config['methods'];
- $callbacks = $config['callbacks'];
- // ----------------------------------------------------------------------------
-
- // Bağlantı türü ayarına göre ssl veya normal
- // bağlatı yapılıp yapılmayacağı belirlenir.
- if( ! empty($methods) && ! empty($callbacks))
- {
- $this->connect = ssh2_connect($host, $port, $methods, $callbacks);
- }
- elseif( ! empty($methods) )
- {
- $this->connect = ssh2_connect($host, $port, $methods);
- }
- else
- {
- $this->connect = ssh2_connect($host, $port);
- }
-
- if( empty($this->connect) )
- {
- \Errors::set('Error', 'emptyVariable', '@this->connect');
-
- return $this;
- }
-
- if( ! empty($user) )
- {
- $this->login = ssh2_auth_password($this->connect, $user, $password);
- }
-
- if( empty($this->login) )
- {
- \Errors::set('Error', 'emptyVariable', '@this->login');
- }
-
- return $this;
- }
-
- //----------------------------------------------------------------------------------------------------
- // close()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function close()
- {
- if( ! empty($this->connect) )
- {
- ssh2_exec($this->connect, 'exit');
- $this->connect = NULL;
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // command()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function command($command = '')
- {
- $this->command .= $command.' ';
-
- return $this;
- }
-
- //----------------------------------------------------------------------------------------------------
- // run()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function run($command = '')
- {
- if( ! empty($this->connect) )
- {
- if( ! empty($this->command) )
- {
- $command = rtrim($this->command);
- }
-
- $this->_defaultVariables();
-
- $this->stream = ssh2_exec($this->connect, $command);
-
- return $this->stream;
- }
-
- return false;
- }
-
- //----------------------------------------------------------------------------------------------------
- // output()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function output($length = 4096)
- {
- $stream = $this->stream;
-
- stream_set_blocking($stream, true);
-
- $data = "";
-
- while( $buffer = fread($stream, $length) )
- {
- $data .= $buffer;
- }
-
- fclose($stream);
-
- return $data;
- }
-
- //----------------------------------------------------------------------------------------------------
- // upload()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $localPath : empty
- // @param string $remotePath: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function upload($localPath = '', $remotePath = '')
- {
- if( ! is_string($localPath) || ! is_string($remotePath) )
- {
- \Errors::set('Error', 'stringParameter', 'localPath');
- \Errors::set('Error', 'stringParameter', 'remotePath');
-
- return false;
- }
-
- if( @ssh2_scp_send($this->connect, $localPath, $remotePath) )
- {
- return true;
- }
- else
- {
- return \Errors::set('File', 'remoteUploadError', $localPath);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // dowload()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $remotePath: empty
- // @param string $localPath : empty
- //
- //----------------------------------------------------------------------------------------------------
- public function download($remotePath = '', $localPath = '')
- {
- if( ! is_string($localPath) || ! is_string($remotePath) )
- {
- \Errors::set('Error', 'stringParameter', 'remotePath');
- \Errors::set('Error', 'stringParameter', 'localPath');
-
- return false;
- }
-
- if( @ssh2_scp_recv($this->connect, $remotePath, $localPath) )
- {
- return true;
- }
- else
- {
- return \Errors::set('File', 'remoteDownloadError', $localPath);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // createFolder()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function createFolder($path = '', $mode = 0777, $recursive = true)
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( @ssh2_sftp_mkdir($this->connect, $path, $mode, $recursive) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Folder', 'alreadyFileError', $path);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // deleteFolder()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function deleteFolder($path = '')
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( @ssh2_sftp_rmdir($this->connect, $path) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Folder', 'notFoundError', $path);
- }
-
- }
-
- //----------------------------------------------------------------------------------------------------
- // rename()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $oldName: empty
- // @param string $newName: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function rename($oldName = '', $newName = '')
- {
- if( ! is_string($oldName) || ! is_string($newName) )
- {
- \Errors::set('Error', 'stringParameter', 'oldName');
- \Errors::set('Error', 'stringParameter', 'newName');
-
- return false;
- }
-
- if( @ssh2_sftp_rename($this->connect, $oldName, $newName) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Folder', 'changeFolderNameError', $oldName);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // deleteFile()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function deleteFile($path = '')
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( @ssh2_sftp_unlink($this->connect, $path) )
- {
- return true;
- }
- else
- {
- return \Errors::set('File', 'notFoundError', $path);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // permission()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path: empty
- // @param int $type : 0755
- //
- //----------------------------------------------------------------------------------------------------
- public function permission($path = '', $type = 0755)
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( ! is_numeric($type) )
- {
- $type = 0755;
- }
-
- if( @ssh2_sftp_chmod($this->connect, $path, $type) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Error', 'emptyVariable', '@this->connect');
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // Protected _defaultVariables()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function _defaultVariables()
- {
- $this->command = '';
- }
-
- //----------------------------------------------------------------------------------------------------
- // __destruct()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function __destruct()
- {
- $this->close();
- }
- }
|