Blame view

view/config.php 3.41 KB
cf76164e6   Ting Chan   20190709
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  <?php 
  
  //session_start();
  
  class Tcpconnection
  {
   private $buff;
   public function __construct($buff)
   {
    $this->buff=$buff;
   }
    public function connect()
    {
     $data=array();
     $ip=$_SERVER['SERVER_ADDR'];
     ini_set('display_errors', true); error_reporting(E_ALL); // <- for debugging purposes only
     $socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     if ( !$socket ) {
      $errno = socket_last_error();
      $error = sprintf('%s (%d)', socket_strerror($errno), $errno);
      trigger_error($error, E_USER_ERROR);
     }
     if (socket_connect($socket, $ip, 502) ) {
      // $errno = socket_last_error($socket);
      // $error = sprintf('%s (%d)', socket_strerror($errno), $errno);
      // trigger_error($error, E_USER_ERROR);
      //echo "Connected";
     }
  
     $length = strlen($this->buff);
     $sent = socket_write($socket, $this->buff, $length);
  
     // echo $sent;
     
     if ( FALSE===$sent ) {
      $errno = socket_last_error($socket);
      $error = sprintf('%s (%d)', socket_strerror($errno), $errno);
      trigger_error($error, E_USER_ERROR);
     }
     else if($sent>0)
     {
      // echo $sent." the number of socket resources contained "; 
     }
     else if ( $length!==$sent ) {
      $msg = sprintf('only %d of %d bytes sent', $length, $sent);
      trigger_error($msg, E_USER_NOTICE);
     }
    }
    private function textBinASCII($text)
    {
    $bin = array();
    for($i=0; strlen($text)>$i; $i++)
     $bin[] = bindec(ord($text[$i]));
  
    //echo hexdec($text[$i])."
  ";
    return implode(',',$bin);
   } 
   private function ASCIIBinText($bin)
   {
    $text = array();
    $bin = explode(" ", $bin);
    for($i=0; count($bin)>$i; $i++)
     $text[] = bindec($bin[$i]);
    return implode(",",$text);
   }
   public function Connect_Socket()
   {
    $hold=array();
    $ip=$_SERVER['SERVER_ADDR'];
    ini_set('display_errors', true); error_reporting(E_ALL); // <- for debugging purposes only
    $socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ( !$socket ) {
     $errno = socket_last_error();
     $error = sprintf('%s (%d)', socket_strerror($errno), $errno);
     trigger_error($error, E_USER_ERROR);
    }
    if (socket_connect($socket, $ip, 502) ) {
     // $errno = socket_last_error($socket);
     // $error = sprintf('%s (%d)', socket_strerror($errno), $errno);
     // trigger_error($error, E_USER_ERROR);
     //echo "Connected";
    }
    
    $length = strlen($this->buff);
    $sent = socket_write($socket, $this->buff, $length);
  
    // echo $sent;
      
    if ( FALSE===$sent ) {
     $errno = socket_last_error($socket);
     $error = sprintf('%s (%d)', socket_strerror($errno), $errno);
     trigger_error($error, E_USER_ERROR);
    }
    else if($sent>0)
    {
     // echo $sent." the number of socket resources contained "; 
    }
    else if ( $length!==$sent ) {
     $msg = sprintf('only %d of %d bytes sent', $length, $sent);
     trigger_error($msg, E_USER_NOTICE);
    }
  
    if(false !== ($bytes = socket_recv($socket,$buf,12,MSG_PEEK))) {
     //echo "Read $bytes bytes from socket_recv(). Closing socket...";
     //echo "<br>";
     //echo $bytes;
    }
    else {
     echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "
  ";
    }
    socket_close($socket);
     //01 02 00 00 00 05 01 04 02 00 01
  
    for ($i=0; $i <strlen($buf) ; $i++) { 
     $hold[]=hexdec(ord($buf[$i]));
     //printf("%d",hexdec(ord($buf[$i])));
     //echo "
  ";
    }
    $function=$hold[count($hold)-1];
    return $function;
   }
  }
  
   ?>