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

Source for file NS_Telnet_Handler.php

Documentation is available at NS_Telnet_Handler.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * nanoserv handlers - Telnet protocol handler
  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 nanoserv core
  29.  */
  30. require_once "nanoserv-compat/nanoserv.php";
  31.  
  32. /**
  33.  * Telnet protocol handler handler class
  34.  *
  35.  * @package nanoserv
  36.  * @subpackage Handlers
  37.  */
  38. abstract class NS_Telnet_Handler extends NS_Connection_Handler {
  39.  
  40.     const IAC = "\xff";
  41.  
  42.     const TYPE_WILL = "\xfb";
  43.     const TYPE_WONT = "\xfc";
  44.     const TYPE_DO = "\xfd";
  45.     const TYPE_DONT = "\xfe";
  46.  
  47.     const OPT_8BIT = "\0";
  48.     const OPT_ECHO = "\1";
  49.     const OPT_SUPPRESS_GO_AHEAD = "\3";
  50.     const OPT_LINE_MODE = "\x22";
  51.     
  52.     /**
  53.      * Protocol options set by the remote party
  54.      */
  55.     public $remote_options = array();
  56.  
  57.     private function Set_Remote_Option($type$option{
  58.  
  59.         $this->remote_options[array($type$option);
  60.  
  61.     }
  62.     
  63.     public function Send_Option($type$option{
  64.  
  65.         $this->Raw_Write(self::IAC $type $option);
  66.     
  67.     }
  68.     
  69.     protected function Raw_Write($data$callback=false{
  70.  
  71.         return parent::Write($data$callback);
  72.  
  73.     }
  74.     
  75.     public function Write($data$callback=false{
  76.  
  77.         $data str_replace(self::IACself::IAC self::IAC$data);
  78.  
  79.         return parent::Write($data$callback);
  80.  
  81.     }
  82.  
  83.     public function on_Read($data{
  84.  
  85.         if (strpos($dataself::IAC!== false{
  86.  
  87.             $tmp "";
  88.             $len strlen($data);
  89.             
  90.             for ($a 0$a $len$a++{
  91.  
  92.                 if ($data[$a=== self::IAC{
  93.  
  94.                     switch ($data[$a 1]{
  95.  
  96.                         case self::TYPE_WILL:
  97.                         case self::TYPE_WONT:
  98.                         case self::TYPE_DO:
  99.                         case self::TYPE_DONT:
  100.                         $this->Set_Remote_Option($data[$a 1]$data[$a 2]);
  101.                         $a += 2;
  102.                         break;
  103.                         
  104.                         case self::IAC:
  105.                         $tmp .= self::IAC;
  106.                         $a++;
  107.                         break;
  108.                         
  109.                     
  110.                     }
  111.                 
  112.                 else {
  113.  
  114.                     $tmp .= $data[$a];
  115.                 
  116.                 }
  117.             
  118.             }
  119.         
  120.             if (strlen($tmp)) {
  121.             
  122.                 $data $tmp;
  123.  
  124.             else {
  125.  
  126.                 return;
  127.  
  128.             }
  129.         
  130.         }
  131.  
  132.         $this->on_Telnet_Read($data);
  133.  
  134.     }
  135.  
  136.     /**
  137.      * Telnet read event
  138.      *
  139.      * This event is called every time there is data received
  140.      *
  141.      * @param string $s 
  142.      */
  143.     abstract public function on_Telnet_Read($s);
  144.  
  145. }
  146.  
  147. ?>

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