예제 #1
0
 public function testParse2()
 {
     $str = new StringParser('arg1 arg2 arg3', 10);
     $args = $str->parse();
     #\Doctrine\Common\Util\Debug::dump($args);
     #$this->assertEquals(array('arg1', 'arg2'), $args);
     $this->assertTrue(true);
 }
예제 #2
0
파일: Client.php 프로젝트: thefox/smtpd
 public function msgHandle($msgRaw)
 {
     #$this->log('debug', 'client '.$this->id.' raw: /'.$msgRaw.'/');
     $rv = '';
     $str = new StringParser($msgRaw);
     $args = $str->parse();
     #ve($args);
     $command = array_shift($args);
     $commandcmp = strtolower($command);
     if ($commandcmp == 'helo') {
         #$this->log('debug', 'client '.$this->id.' helo');
         $this->setStatus('hasHello', true);
         return $this->sendOk($this->getHostname());
     } elseif ($commandcmp == 'ehlo') {
         #$this->log('debug', 'client '.$this->id.' helo');
         $this->setStatus('hasHello', true);
         $msg = '250-' . $this->getHostname() . static::MSG_SEPARATOR;
         $count = count($this->extendedCommands) - 1;
         for ($i = 0; $i < $count; $i++) {
             $msg .= '250-' . $this->extendedCommands[$i] . static::MSG_SEPARATOR;
         }
         $msg .= '250 ' . end($this->extendedCommands);
         return $this->dataSend($msg);
     } elseif ($commandcmp == 'mail') {
         #$this->log('debug', 'client '.$this->id.' mail');
         #ve($args);
         if ($this->getStatus('hasHello')) {
             if (isset($args[0]) && $args[0]) {
                 $this->setStatus('hasMail', true);
                 $from = $args[0];
                 if (substr(strtolower($from), 0, 6) == 'from:<') {
                     $from = substr(substr($from, 6), 0, -1);
                 }
                 #$this->log('debug', 'client '.$this->id.' from: /'.$from.'/');
                 $this->from = $from;
                 $this->mail = '';
                 return $this->sendOk();
             } else {
                 return $this->sendSyntaxErrorInParameters();
             }
         } else {
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     } elseif ($commandcmp == 'rcpt') {
         #$this->log('debug', 'client '.$this->id.' rcpt');
         #ve($args);
         if ($this->getStatus('hasHello')) {
             if (isset($args[0]) && $args[0]) {
                 $this->setStatus('hasMail', true);
                 $rcpt = $args[0];
                 if (substr(strtolower($rcpt), 0, 4) == 'to:<') {
                     $rcpt = substr(substr($rcpt, 4), 0, -1);
                     $this->rcpt[] = $rcpt;
                 }
                 #$this->log('debug', 'client '.$this->id.' rcpt: /'.$rcpt.'/');
                 return $this->sendOk();
             } else {
                 return $this->sendSyntaxErrorInParameters();
             }
         } else {
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     } elseif ($commandcmp == 'data') {
         #$this->log('debug', 'client '.$this->id.' data');
         if ($this->getStatus('hasHello')) {
             $this->setStatus('hasData', true);
             return $this->sendDataResponse();
         } else {
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     } elseif ($commandcmp == 'noop') {
         return $this->sendOk();
     } elseif ($commandcmp == 'quit') {
         $rv .= $this->sendQuit();
         $this->shutdown();
     } elseif ($commandcmp == 'auth') {
         $this->setStatus('hasAuth', true);
         if (empty($args)) {
             return $this->sendSyntaxErrorInParameters();
         }
         $authentication = strtolower($args[0]);
         if ($authentication == 'plain') {
             $this->setStatus('hasAuthPlain', true);
             if (isset($args[1])) {
                 $this->setStatus('hasAuthPlainUser', true);
                 $this->setCredentials(array($args[1]));
                 if ($this->authenticate('plain')) {
                     return $this->sendAuthSuccessResponse();
                 }
                 return $this->sendAuthInvalid();
             }
             return $this->sendAuthPlainResponse();
         } elseif ($authentication == 'login') {
             $this->setStatus('hasAuthLogin', true);
             return $this->sendAskForUserResponse();
         } elseif ($authentication == 'cram-md5') {
             return $this->sendCommandNotImplemented();
         } else {
             return $this->sendSyntaxErrorInParameters();
         }
     } elseif ($commandcmp == 'starttls') {
         if (!empty($args)) {
             return $this->sendSyntaxErrorInParameters();
         }
         $this->sendReadyStartTls();
         try {
             return $this->getSocket()->enableEncryption();
         } catch (RuntimeException $e) {
             return $this->sendTemporaryErrorStartTls();
         }
     } elseif ($commandcmp == 'help') {
         return $this->sendOk('HELO, EHLO, MAIL FROM, RCPT TO, DATA, NOOP, QUIT');
     } else {
         if ($this->getStatus('hasAuth')) {
             if ($this->getStatus('hasAuthPlain')) {
                 $this->setStatus('hasAuthPlainUser', true);
                 $this->setCredentials(array($command));
                 if ($this->authenticate('plain')) {
                     return $this->sendAuthSuccessResponse();
                 }
                 return $this->sendAuthInvalid();
             } elseif ($this->getStatus('hasAuthLogin')) {
                 $credentials = $this->getCredentials();
                 if ($this->getStatus('hasAuthLoginUser')) {
                     $credentials['password'] = $command;
                     $this->setCredentials($credentials);
                     if ($this->authenticate('login')) {
                         return $this->sendAuthSuccessResponse();
                     }
                     return $this->sendAuthInvalid();
                 }
                 $this->setStatus('hasAuthLoginUser', true);
                 $credentials['user'] = $command;
                 $this->setCredentials($credentials);
                 return $this->sendAskForPasswordResponse();
             }
         } elseif ($this->getStatus('hasData')) {
             if ($msgRaw == '.') {
                 $this->mail = substr($this->mail, 0, -strlen(static::MSG_SEPARATOR));
                 $zmail = Message::fromString($this->mail);
                 $this->getServer()->mailNew($this->from, $this->rcpt, $zmail);
                 $this->from = '';
                 $this->rcpt = array();
                 $this->mail = '';
                 return $this->sendOk();
             } else {
                 $this->mail .= $msgRaw . static::MSG_SEPARATOR;
             }
         } else {
             $this->log('debug', 'client ' . $this->id . ' not implemented: /' . $command . '/ - /' . join('/ /', $args) . '/');
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     }
     return $rv;
 }
예제 #3
0
파일: Client.php 프로젝트: mambaru/smtpd
 public function msgHandle($msgRaw)
 {
     #$this->log('debug', 'client '.$this->id.' raw: /'.$msgRaw.'/');
     $rv = '';
     $str = new StringParser($msgRaw);
     $args = $str->parse();
     //print_r($args);
     $command = array_shift($args);
     $commandcmp = strtolower($command);
     if ($commandcmp == 'helo') {
         #$this->log('debug', 'client '.$this->id.' helo');
         $this->setStatus('hasHello', true);
         return $this->sendOk('localhost.localdomain');
     } elseif ($commandcmp == 'ehlo') {
         #$this->log('debug', 'client '.$this->id.' helo');
         return $this->sendCommandNotImplemented();
     } elseif ($commandcmp == 'mail') {
         #$this->log('debug', 'client '.$this->id.' mail');
         #ve($args);
         if ($this->getStatus('hasHello')) {
             if (isset($args[0]) && $args[0]) {
                 $this->setStatus('hasMail', true);
                 $from = count($args) > 1 ? $args[1] : $args[0];
                 if (substr(strtolower($from), 0, 6) == 'from:<') {
                     $from = substr(substr($from, 6), 0, -1);
                 } elseif (substr(strtolower($from), 0, 1) == '<') {
                     $from = substr(substr($from, 1), 0, -1);
                 }
                 #$this->log('debug', 'client '.$this->id.' from: /'.$from.'/');
                 $this->from = $from;
                 $this->mail = '';
                 return $this->sendOk();
             } else {
                 return $this->sendSyntaxErrorInParameters();
             }
         } else {
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     } elseif ($commandcmp == 'rcpt') {
         #$this->log('debug', 'client '.$this->id.' rcpt');
         #ve($args);
         if ($this->getStatus('hasHello')) {
             if (isset($args[0]) && $args[0]) {
                 $this->setStatus('hasMail', true);
                 $rcpt = count($args) > 1 ? $args[1] : $args[0];
                 if (substr(strtolower($rcpt), 0, 4) == 'to:<') {
                     $rcpt = substr(substr($rcpt, 4), 0, -1);
                 } elseif (substr(strtolower($rcpt), 0, 1) == '<') {
                     $rcpt = substr(substr($rcpt, 1), 0, -1);
                 }
                 $this->rcpt[] = $rcpt;
                 #$this->log('debug', 'client '.$this->id.' rcpt: /'.$rcpt.'/');
                 return $this->sendOk();
             } else {
                 return $this->sendSyntaxErrorInParameters();
             }
         } else {
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     } elseif ($commandcmp == 'data') {
         #$this->log('debug', 'client '.$this->id.' data');
         if ($this->getStatus('hasHello')) {
             $this->setStatus('hasData', true);
             return $this->sendDataResponse();
         } else {
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     } elseif ($commandcmp == 'noop') {
         return $this->sendOk();
     } elseif ($commandcmp == 'quit') {
         $rv .= $this->sendQuit();
         $this->shutdown();
     } else {
         if ($this->getStatus('hasData')) {
             if ($msgRaw == '.') {
                 $this->mail = substr($this->mail, 0, -strlen(static::MSG_SEPARATOR));
                 $parser = new Parser();
                 $parser->setText($this->mail);
                 //$parser = Message::fromString($this->mail);
                 $this->getServer()->mailNew($this->from, $this->rcpt, $parser);
                 $this->from = '';
                 $this->rcpt = array();
                 $this->mail = '';
                 return $this->sendOk();
             } else {
                 $this->mail .= $msgRaw . static::MSG_SEPARATOR;
             }
         } else {
             $this->log('debug', 'client ' . $this->id . ' not implemented: /' . $command . '/ - /' . join('/ /', $args) . '/');
             return $this->sendSyntaxErrorCommandUnrecognized();
         }
     }
     return $rv;
 }