123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- <?php
- namespace ZN\Services;
- class __USE_STATIC_ACCESS__FTP implements FTPInterface
- {
- //----------------------------------------------------------------------------------------------------
- //
- // 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:ftp';
-
- //----------------------------------------------------------------------------------------------------
- // Protected $connect
- //----------------------------------------------------------------------------------------------------
- //
- // @const resource
- //
- //----------------------------------------------------------------------------------------------------
- protected $connect = NULL;
-
- //----------------------------------------------------------------------------------------------------
- // Protected $login
- //----------------------------------------------------------------------------------------------------
- //
- // @const resource
- //
- //----------------------------------------------------------------------------------------------------
- protected $login = NULL;
-
- //----------------------------------------------------------------------------------------------------
- // __construct()
- //----------------------------------------------------------------------------------------------------
- //
- // @param array $config: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function __construct($config = [])
- {
- $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) )
- {
- return \Errors::set('Error', 'arrayParameter', 'config');
- }
-
- if( ! empty($config) )
- {
- $this->config($config);
- }
-
- // Config/Ftp.php dosyasından ftp ayarları alınıyor.
- $config = $this->config;
-
- // ----------------------------------------------------------------------------
- // FTP BAĞLANTI AYARLARI YAPILANDIRILIYOR
- // ----------------------------------------------------------------------------
- $host = $config['host'];
- $port = $config['port'];
- $timeout = $config['timeout'];
- $user = $config['user'];
- $password = $config['password'];
- $ssl = $config['sslConnect'];
- // ----------------------------------------------------------------------------
-
- // Bağlantı türü ayarına göre ssl veya normal
- // bağlatı yapılıp yapılmayacağı belirlenir.
- $this->connect = ( $ssl === false )
- ? @ftp_connect($host, $port, $timeout)
- : @ftp_ssl_connect($host, $port, $timeout);
-
- if( empty($this->connect) )
- {
- return \Errors::set('Error', 'emptyVariable', '@this->connect');
- }
-
- $this->login = @ftp_login($this->connect, $user, $password);
-
- if( empty($this->login) )
- {
- return false;
- }
-
- return $this;
- }
-
- //----------------------------------------------------------------------------------------------------
- // close()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function close()
- {
- if( ! empty($this->connect) )
- {
- @ftp_close($this->connect);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // createFolder()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function createFolder($path = '')
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( @ftp_mkdir($this->connect, $path) )
- {
- 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( @ftp_rmdir($this->connect, $path) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Folder', 'notFoundError', $path);
- }
-
- }
-
- //----------------------------------------------------------------------------------------------------
- // changeFolder()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function changeFolder($path = '')
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( @ftp_chdir($this->connect, $path) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Folder', 'changeFolderError', $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( @ftp_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( @ftp_delete($this->connect, $path) )
- {
- return true;
- }
- else
- {
- return \Errors::set('File', 'notFoundError', $path);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // upload()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $localPath : empty
- // @param string $remotePath: empty
- // @param string $type : binary, ascii
- //
- //----------------------------------------------------------------------------------------------------
- public function upload($localPath = '', $remotePath = '', $type = 'ascii')
- {
- if( ! is_string($localPath) || ! is_string($remotePath) )
- {
- \Errors::set('Error', 'stringParameter', 'localPath');
- \Errors::set('Error', 'stringParameter', 'remotePath');
-
- return false;
- }
-
- if( ! is_string($type) )
- {
- $type = 'ascii';
- }
-
- if( @ftp_put($this->connect, $remotePath, $localPath, \Convert::toConstant($type, 'FTP_')) )
- {
- return true;
- }
- else
- {
- return \Errors::set('File', 'remoteUploadError', $localPath);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // dowload()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $remotePath: empty
- // @param string $localPath : empty
- // @param string $type : binary, ascii
- //
- //----------------------------------------------------------------------------------------------------
- public function download($remotePath = '', $localPath = '', $type = 'ascii')
- {
- if( ! is_string($localPath) || ! is_string($remotePath) )
- {
- \Errors::set('Error', 'stringParameter', 'remotePath');
- \Errors::set('Error', 'stringParameter', 'localPath');
-
- return false;
- }
-
- if( ! is_string($type) )
- {
- $type = 'ascii';
- }
-
- if( @ftp_get($this->connect, $localPath, $remotePath, \Convert::toConstant($type, 'FTP_')) )
- {
- return true;
- }
- else
- {
- return \Errors::set('File', 'remoteDownloadError', $localPath);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // 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( @ftp_chmod($this->connect, $type, $path) )
- {
- return true;
- }
- else
- {
- return \Errors::set('Error', 'emptyVariable', '@this->connect');
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // files()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path : empty
- // @param string $extension: empty
- //
- //----------------------------------------------------------------------------------------------------
- public function files($path = '', $extension = '')
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
- $list = @ftp_nlist($this->connect, $path);
-
- if( ! empty($list) ) foreach( $list as $file )
- {
- if( $file !== '.' && $file !== '..' )
- {
- if( ! empty($extension) && $extension !== 'dir' )
- {
- if( extension($file) === $extension )
- {
- $files[] = $file;
- }
- }
- else
- {
- if( $extension === 'dir' )
- {
- $extens = extension($file);
-
- if( empty($extens) )
- {
- $files[] = $file;
- }
- }
- else
- {
- $files[] = $file;
- }
- }
- }
- }
-
- if( ! empty($files) )
- {
- return $files;
- }
- else
- {
- return \Errors::set('Error', 'emptyVariable', '@files');
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // fileSize()
- //----------------------------------------------------------------------------------------------------
- //
- // @param string $path : empty
- // @param string $type : b, kb, mb, gb
- // @param int $decimal: 2
- //
- //----------------------------------------------------------------------------------------------------
- public function fileSize($path = '', $type = 'b', $decimal = 2)
- {
- if( ! is_string($path) )
- {
- return \Errors::set('Error', 'stringParameter', 'path');
- }
-
- if( ! is_string($type) )
- {
- $type = 'b';
- }
-
- $size = 0;
-
- $extension = extension($path);
-
- if( ! empty($extension) )
- {
- $size = @ftp_size($this->connect, $path);
- }
- else
- {
- if( $this->files($path) )
- {
- foreach($this->files($path) as $val)
- {
- $size += @ftp_size($this->connect, $path."/".$val);
- }
- $size += @ftp_size($this->connect, $path);
- }
- else
- {
- $size += @ftp_size($this->connect, $path);
- }
- }
-
- if( $type === "b" )
- {
- return $size;
- }
- if( $type === "kb" )
- {
- return round($size / 1024, $decimal);
- }
- if( $type === "mb" )
- {
- return round($size / (1024 * 1024), $decimal);
- }
- if( $type === "gb" )
- {
- return round($size / (1024 * 1024 * 1024), $decimal);
- }
- }
-
- //----------------------------------------------------------------------------------------------------
- // __destruct()
- //----------------------------------------------------------------------------------------------------
- //
- // @param void
- //
- //----------------------------------------------------------------------------------------------------
- public function __destruct()
- {
- $this->close();
- }
- }
|