nanoserv
[ class tree: nanoserv ] [ index: nanoserv ] [ all elements ]

Source for file NS_SMTP_Service_Handler.php

Documentation is available at NS_SMTP_Service_Handler.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * nanoserv handlers - SMTP server
  6.  * 
  7.  * Copyright (C) 2004-2010 Vincent Negrier aka. sIX <six@aegis-corp.org>
  8.  * 
  9.  * This library is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU Lesser General Public
  11.  * License as published by the Free Software Foundation; either
  12.  * version 2.1 of the License, or (at your option) any later version.
  13.  * 
  14.  * This library is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.  * Lesser General Public License for more details.
  18.  * 
  19.  * You should have received a copy of the GNU Lesser General Public
  20.  * License along with this library; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA *
  22.  *
  23.  * @package nanoserv
  24.  * @subpackage Handlers
  25.  */
  26.  
  27. /**
  28.  * Require the line input connection handler
  29.  */
  30. require_once "nanoserv-compat/handlers/NS_Line_Input_Connection_Handler.php";
  31.  
  32. /**
  33.  * SMTP Service handler class
  34.  *
  35.  * @package nanoserv
  36.  * @subpackage Handlers
  37.  */
  38.  
  39.     /**
  40.      * Server string
  41.      */
  42.     const SERVER_STRING = "";
  43.     
  44.     /**
  45.      * Hostname
  46.      * @var string 
  47.      */
  48.     public $hostname;
  49.     
  50.     /**
  51.      * HELO message
  52.      * @var string 
  53.      */
  54.     protected $helo_message = "";
  55.     
  56.     /**
  57.      * Enveloppe sender
  58.      * @var string 
  59.      */
  60.     protected $env_from = "";
  61.  
  62.     /**
  63.      * Enveloppe recipents
  64.      * @var array 
  65.      */
  66.     protected $env_rcpt = array();
  67.  
  68.     /**
  69.      * Mail data buffer
  70.      * @var string 
  71.      */
  72.     protected $data_buffer = "";
  73.     
  74.     /**
  75.      * State indicator
  76.      * @var bool 
  77.      */
  78.     private $indata false;
  79.     
  80.     /**
  81.      * SMTP Service Handler constructor
  82.      */
  83.     public function __construct({
  84.  
  85.         $this->hostname = php_uname("n");
  86.     
  87.     }
  88.     
  89.     public function on_Accept({
  90.  
  91.         $this->Write("200 ".$this->hostname." SMTP ".(static::SERVER_STRING static::SERVER_STRING "nanoserv/2.1.1-dev")."\n");
  92.     
  93.     }
  94.     
  95.     final public function on_Read_Line($data{
  96.  
  97.         if (!$this->indata{
  98.  
  99.             $updata strtoupper($data);
  100.  
  101.             if (strpos($updata"HELO"=== 0{
  102.  
  103.                 strtok($data" ");
  104.                 $this->helo_message = trim(strtok(""));
  105.  
  106.                 if (!$this->on_SMTP_HELO($this->helo_message)) {
  107.                     
  108.                     $this->Disconnect();
  109.                     break;
  110.                 
  111.                 }
  112.                 
  113.                 $this->Write("250 ".$this->hostname." Hello\n");
  114.  
  115.             else if (strpos($updata"MAIL FROM"=== 0{
  116.  
  117.                 strtok($data":");
  118.                 $this->env_from = trim(strtok(""));
  119.             
  120.                 if (!$this->on_SMTP_MAIL_FROM($this->env_from)) break;
  121.  
  122.                 $this->Write("250 ".$this->env_from."... Sender ok\n");
  123.             
  124.             else if (strpos($updata"RCPT TO"=== 0{
  125.  
  126.                 strtok($data":");
  127.                 $this->env_rcpt[$rcpt trim(strtok(""));
  128.  
  129.                 if (!$this->on_SMTP_RCPT_TO($rcpt)) break;
  130.  
  131.                 $this->Write("250 ".$rcpt."... Recipient ok\n");
  132.             
  133.             else if (strpos($updata"DATA"=== 0{
  134.  
  135.                 $this->Write("354 Enter mail, end with '.' on a line by itself\n");
  136.                 $this->indata true;
  137.             
  138.             else if (strpos($updata"QUIT"=== 0{
  139.  
  140.                 $this->Write("251 ".$this->hostname." closing connection\n"array($this"Disconnect"));
  141.  
  142.             else {
  143.  
  144.                 if (!$this->on_SMTP_Unhandled(trim($data))) break;
  145.  
  146.             }
  147.         
  148.         else {
  149.  
  150.             if (rtrim($data!== "."{
  151.             
  152.                 $this->data_buffer .= $data;
  153.  
  154.             else {
  155.  
  156.                 if ($this->on_Mail($this->env_from$this->env_rcpt$this->data_buffer)) {
  157.  
  158.                     $this->Write("250 Message accepted\n");
  159.                 
  160.                 else {
  161.  
  162.                     $this->Write("554 Message rejected\n");
  163.  
  164.                 }
  165.             
  166.                 $this->indata false;
  167.                 $this->env_from = "";
  168.                 $this->env_rcpt = array();
  169.             
  170.             }
  171.         
  172.         }
  173.     
  174.     }
  175.  
  176.     /**
  177.      * Event called on SMTP HELO reception
  178.      *
  179.      * Extend this method to return the boolean status of the session (false = disconnect client)
  180.      *
  181.      * @param string $data remote HELO message
  182.      * @return bool 
  183.      */
  184.     public function on_SMTP_HELO($data{
  185.  
  186.         return true;
  187.     
  188.     }
  189.  
  190.     /**
  191.      * Event called on SMTP MAIL FROM reception
  192.      *
  193.      * Extend this method to return the boolean status of the session (false = disconnect client)
  194.      *
  195.      * @param string $data remote MAIL FROM message
  196.      * @return bool 
  197.      */
  198.     public function on_SMTP_MAIL_FROM($data{
  199.  
  200.         return true;
  201.     
  202.     }
  203.  
  204.     /**
  205.      * Event called on SMTP RCPT TO reception
  206.      *
  207.      * Extend this method to return the boolean status of the session (false = disconnect client)
  208.      *
  209.      * @param string $data remote RCPT TO message
  210.      * @return bool 
  211.      */
  212.     public function on_SMTP_RCPT_TO($data{
  213.  
  214.         return true;
  215.     
  216.     }
  217.     
  218.     /**
  219.      * Event called on unknown SMTP command reception
  220.      *
  221.      * Extend this method to return the boolean status of the session (false = disconnect client)
  222.      *
  223.      * @param string $data entire command line
  224.      * @return bool 
  225.      */
  226.     public function on_SMTP_Unhandled($data{
  227.  
  228.         $this->Write("500 Unknown command : '$data'\n");
  229.         
  230.         return true;
  231.     
  232.     }
  233.     
  234.     /**
  235.      * Event called on mail reception
  236.      *
  237.      * if true is returned, a "message accepted" reply will be sent, and "message rejected" for false
  238.      *
  239.      * @param string $env_from 
  240.      * @param array $env_to 
  241.      * @param string $data this includes mail headers and content
  242.      * @return bool 
  243.      */
  244.     public function on_Mail($env_from$env_to$data{
  245.  
  246.     }
  247.  
  248. }
  249.  
  250. ?>

Documentation generated on Wed, 30 Nov 2011 22:03:25 +0100 by phpDocumentor 1.4.3