From cf1f7dec902e256e78a6ba765850b49b95bc8043 Mon Sep 17 00:00:00 2001 From: flyskyko Date: Tue, 24 Jul 2012 09:12:18 +0000 Subject: [PATCH] some improve ftp class git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.3.1@10936 201d5d3c-b55e-5fd7-737f-ddc643e51545 --- libs/ftp.class.php | 47 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/libs/ftp.class.php b/libs/ftp.class.php index 417a3a83b..c716bda90 100644 --- a/libs/ftp.class.php +++ b/libs/ftp.class.php @@ -80,6 +80,12 @@ $this->ftp_debug("Error : fsockopen() ".$errstr." (".$errno.")\n"); return FALSE; } + + if(substr($this->ftp_resp, 0, 3) !== '220') + { + return FALSE; + } + $this->ftp_debug("Connected to remote host \"".$server.":".$port."\"\n"); return TRUE; @@ -477,10 +483,43 @@ function ftp_ok() { $this->ftp_resp = ""; - do { - $res = fgets($this->ftp_sock, 512); - $this->ftp_resp .= $res; - } while (substr($res, 3, 1) != " "); + + // 한줄을 읽는다. + $line = ''; + while(($char = fgetc($this->ftp_sock)) !== FALSE) + { + $line .= $char; + if($char === "\n") break; + } + + // 세자리 응답 코드가 나와야 한다. + if(!preg_match('@^[0-9]{3}@', $line)) + { + return FALSE; + } + + $this->ftp_resp = $line; + + // 4번째 문자가 -이면 여러줄인 응답이다. + if($line[3] === '-') + { + $code = substr($line, 0, 3); + + // 한줄 단위로 읽어 나간다. + do + { + $line = ''; + while(($char = fgetc($this->ftp_sock)) !== FALSE) + { + $line .= $char; + if($char === "\n") break; + } + $this->ftp_resp .= $line; + + // 응답 코드와 같은 코드가 나오고 공백이 있으면 끝 + if($code . ' ' === substr($line, 0, 4)) break; + }while($line); + } $this->ftp_debug(str_replace("\r\n", "\n", $this->ftp_resp));