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));