123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- namespace OSInet\Finite_State_Machine;
- $_ftpEr = error_reporting(E_ALL | E_STRICT);
- class FtpClient extends Machine {
-
- private $fRemoteFile;
- private $fRemoteHost;
- private $fRemotePass;
- private $fRemoteUser;
- private $fRemoteWd;
-
- private $fLocalWd;
- private $fLocalFile;
- private $fLocalFp;
-
- private $fFtpCallback;
- private $fFtpGoal;
- private $fFtpConnection;
-
- public function __construct($values = array()) {
- if (!isset($values['source'])) {
- throw \Exception('Unspecified machine description.');
- }
- $this->loadFsm($values['source']);
-
- parent::__construct();
-
- }
-
- public function setParameters($params) {
- $r = new \ReflectionClass(get_class($this));
- $dp = $r->getDefaultProperties();
- foreach ($params as $name => $value) {
- $field_name = "f$name";
- if (array_key_exists($field_name, $dp)) {
- $this->$field_name = $value;
- }
- else {
- echo "Skipping invalid parameter $name.\n";
- }
- }
- $ret = $this->applyEvent('CheckParameters');
- return $ret;
- }
-
-
- static function stringFromBoolean($val)
- {
- return $val ? 'true' : 'false';
- }
-
- static function stringFromFtp($status)
- {
- switch ($status)
- {
- case FTP_FINISHED: $ret = 'FTP_FINISHED'; break;
- case FTP_MOREDATA: $ret = 'FTP_MOREDATA'; break;
- case FTP_FAILED: $ret = 'FTP_MOREDATA'; break;
- default: $ret = 'FTP_INVALID_STATUS'; break;
- }
- return $ret;
- }
-
-
- protected function eventChdir()
- {
- $ret = ftp_chdir($this->fFtpConnection, $this->fRemoteWd);
- return self::stringFromBoolean($ret);
- }
-
- public function eventCheckParameters()
- {
- $ret = isset($this->fRemoteHost)
- && isset($this->fRemoteUser)
- && isset($this->fRemotePass)
- && isset($this->fRemoteWd)
- && isset($this->fRemoteFile)
- && isset($this->fLocalWd)
- && isset($this->fLocalFile)
- && isset($this->fFtpCallback)
- ;
- $ret = self::stringFromBoolean($ret);
-
- return $ret;
- }
-
- protected function eventClose()
- {
- $ret = 'true';
-
- if (is_resource($this->fFtpConnection))
- {
- try
- {
-
- ftp_close($this->fFtpConnection);
-
- }
- catch (Exception $e)
- {
- print_r($e);
- $ret = 'false';
- }
- }
-
- return $ret;
- }
-
- protected function eventConnect()
- {
-
- $this->fFtpConnection = ftp_connect($this->fRemoteHost);
- $ret = is_resource($this->fFtpConnection);
- return self::stringFromBoolean($ret);
- }
-
- protected function eventContinue()
- {
-
- if ($this->fFtpCallback)
- {
- call_user_func($this->fFtpCallback, $this, 'pre');
- }
- $ret = ftp_nb_continue($this->fFtpConnection);
- if ($ret == FTP_FINISHED)
- {
- fclose($this->fLocalFp);
- }
- if ($this->fFtpCallback)
- call_user_func($this->fFtpCallback, $this, 'post');
- $ret = self::stringFromFtp($ret);
- return $ret;
- }
-
- protected function eventGet()
- {
-
- $this->fLocalFp = fopen($this->fLocalFile, "wb");
- if (!is_resource($this->fLocalFp))
- {
- $ret = self::stringFromFtp(FTP_FAILED);
-
- return $ret;
- }
- $this->fFtpGoal = ftp_size($this->fFtpConnection, $this->fRemoteFile);
- $ret = ftp_nb_fget($this->fFtpConnection, $this->fLocalFp,
- $this->fRemoteFile, FTP_BINARY);
- if ($ret == FTP_FINISHED)
- {
- fclose($this->fLocalFp);
- }
- call_user_func($this->fFtpCallback, $this, 'post');
-
- return self::stringFromFtp($ret);
- }
-
- protected function eventLogin()
- {
-
- $ret = ftp_login($this->fFtpConnection, $this->fRemoteUser, $this->fRemotePass);
- return self::stringFromBoolean($ret);
- }
-
- protected function eventProgress()
- {
- return;
- }
-
- public function __destruct()
- {
-
- if ($this->isEventAllowed('Close'))
- {
- $this->applyEvent('Close');
- }
- if(is_resource($this->fLocalFp))
- try
- {
- echo "Trying to close file...";
- fclose($this->fLocalFp);
- echo "Done\n";
- }
- catch (Exception $e)
- {
- print_r($e);
- }
-
- }
-
- public function isHostValid($host = null)
- {
- $ret = is_array(gethostbynamel($host));
- return $ret;
- }
-
- public function getProgress()
- {
- if ((!$this->fState == 'active') || (!is_resource($this->fLocalFp)))
- {
- $ret = 0.0;
- }
- else
- {
- $pos = ftell($this->fLocalFp);
- $ret = $pos / $this->fFtpGoal;
- }
- return $ret;
- }
- }
- error_reporting($_ftpEr);
|