Changeset 887 for trunk/Modules/NetworkConfigRouterOS/RouterboardAPI.php
- Timestamp:
- Nov 20, 2020, 12:08:12 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/NetworkConfigRouterOS/RouterboardAPI.php
r874 r887 25 25 } 26 26 27 function EncodeLength($Length) 28 { 29 if ($Length < 0x80) { 27 function EncodeLength(int $Length): int 28 { 29 if ($Length < 0x80) 30 { 30 31 $Length = chr($Length); 31 } else if ($Length < 0x4000) { 32 } else if ($Length < 0x4000) 33 { 32 34 $Length |= 0x8000; 33 35 $Length = chr(($Length >> 8) & 0xFF).chr($Length & 0xFF); 34 } else if ($Length < 0x200000) { 36 } else if ($Length < 0x200000) 37 { 35 38 $Length |= 0xC00000; 36 39 $Length = chr(($Length >> 16) & 0xFF).chr(($Length >> 8) & 0xFF).chr($Length & 0xFF); 37 } else if ($Length < 0x10000000) { 40 } else if ($Length < 0x10000000) 41 { 38 42 $Length |= 0xE0000000; 39 43 $Length = chr(($Length >> 24) & 0xFF).chr(($Length >> 16) & 0xFF).chr(($Length >> 8) & 0xFF).chr($Length & 0xFF); … … 43 47 } 44 48 45 function ConnectOnce( $IP, $Login, $Password)49 function ConnectOnce(string $IP, string $Login, string $Password): void 46 50 { 47 51 if ($this->Connected) $this->Disconnect(); … … 63 67 } 64 68 65 function Connect( $IP, $Login, $Password)69 function Connect(string $IP, string $Login, string $Password): bool 66 70 { 67 71 for ($Attempt = 1; $Attempt <= $this->Attempts; $Attempt++) … … 74 78 } 75 79 76 function Disconnect() 80 function Disconnect(): void 77 81 { 78 82 if ($this->Connected) … … 83 87 } 84 88 85 function ParseResponse($Response) 86 { 87 if (is_array($Response)) { 89 function ParseResponse(array $Response): array 90 { 91 if (is_array($Response)) 92 { 88 93 $Parsed = array(); 89 94 $Current = null; 90 95 $SingleValue = null; 91 96 $count = 0; 92 foreach ($Response as $x) { 97 foreach ($Response as $x) 98 { 93 99 if (in_array($x, array( 94 100 '!fatal', 95 101 '!re', 96 102 '!trap' 97 ))) { 98 if ($x == '!re') { 103 ))) 104 { 105 if ($x == '!re') 106 { 99 107 $Current =& $Parsed[]; 100 108 } else 101 109 $Current =& $Parsed[$x][]; 102 } else if ($x != '!done') { 103 if (preg_match_all('/[^=]+/i', $x, $Matches)) { 110 } else if ($x != '!done') 111 { 112 if (preg_match_all('/[^=]+/i', $x, $Matches)) 113 { 104 114 if ($Matches[0][0] == 'ret') { 105 115 $SingleValue = $Matches[0][1]; … … 109 119 } 110 120 } 111 if (empty($Parsed) && !is_null($SingleValue)) { 121 if (empty($Parsed) && !is_null($SingleValue)) 122 { 112 123 $Parsed = $SingleValue; 113 124 } … … 117 128 } 118 129 119 function ArrayChangeKeyName(&$array) 120 { 121 if (is_array($array)) { 122 foreach ($array as $k => $v) { 130 function ArrayChangeKeyName(array &$array): array 131 { 132 if (is_array($array)) 133 { 134 foreach ($array as $k => $v) 135 { 123 136 $tmp = str_replace("-", "_", $k); 124 137 $tmp = str_replace("/", "_", $tmp); 125 if ($tmp) { 138 if ($tmp) 139 { 126 140 $array_new[$tmp] = $v; 127 } else { 141 } else 142 { 128 143 $array_new[$k] = $v; 129 144 } 130 145 } 131 146 return $array_new; 132 } else { 147 } else 148 { 133 149 return $array; 134 150 } 135 151 } 136 152 137 function Read( $Parse = true)153 function Read(bool $Parse = true): array 138 154 { 139 155 $Line = ''; 140 156 $Response = array(); 141 while (true) { 157 while (true) 158 { 142 159 // Read the first byte of input which gives us some or all of the length 143 160 // of the remaining reply. … … 149 166 // If the fourth bit is set, we need to remove anything left in the first byte 150 167 // and then read in yet another byte. 151 if ($Byte & 0x80) { 152 if (($Byte & 0xc0) == 0x80) { 168 if ($Byte & 0x80) 169 { 170 if (($Byte & 0xc0) == 0x80) 171 { 153 172 $Length = (($Byte & 63) << 8) + ord(fread($this->Socket, 1)); 154 } else { 155 if (($Byte & 0xe0) == 0xc0) { 173 } else 174 { 175 if (($Byte & 0xe0) == 0xc0) 176 { 156 177 $Length = (($Byte & 31) << 8) + ord(fread($this->Socket, 1)); 157 178 $Length = ($Length << 8) + ord(fread($this->Socket, 1)); 158 } else { 159 if (($Byte & 0xf0) == 0xe0) { 179 } else 180 { 181 if (($Byte & 0xf0) == 0xe0) 182 { 160 183 $Length = (($Byte & 15) << 8) + ord(fread($this->Socket, 1)); 161 184 $Length = ($Length << 8) + ord(fread($this->Socket, 1)); 162 185 $Length = ($Length << 8) + ord(fread($this->Socket, 1)); 163 } else { 186 } else 187 { 164 188 $Length = ord(fread($this->Socket, 1)); 165 189 $Length = ($Length << 8) + ord(fread($this->Socket, 1)); … … 169 193 } 170 194 } 171 } else { 195 } else 196 { 172 197 $Length = $Byte; 173 198 } 174 199 // If we have got more characters to read, read them in. 175 if ($Length > 0) { 200 if ($Length > 0) 201 { 176 202 $Line = ''; 177 203 $RetLen = 0; 178 while ($RetLen < $Length) { 204 while ($RetLen < $Length) 205 { 179 206 $ToRead = $Length - $RetLen; 180 207 $Line .= fread($this->Socket, $ToRead); … … 196 223 } 197 224 198 function Write( $Command, $Param2 = true)225 function Write(string $Command, bool $Param2 = true): bool 199 226 { 200 227 if ($Command) 201 228 { 202 229 $Data = explode("\n", $Command); 203 foreach ($Data as $Com) { 230 foreach ($Data as $Com) 231 { 204 232 $Com = trim($Com); 205 233 fwrite($this->Socket, $this->EncodeLength(strlen($Com)).$Com); 206 234 } 207 if (gettype($Param2) == 'integer') { 235 if (gettype($Param2) == 'integer') 236 { 208 237 fwrite($this->Socket, $this->EncodeLength(strlen('.tag='.$Param2)).'.tag='.$Param2.chr(0)); 209 238 } else if (gettype($Param2) == 'boolean') … … 214 243 } 215 244 216 function Comm( $Com, $Arr = array())245 function Comm(string $Com, array $Arr = array()): array 217 246 { 218 247 $Count = count($Arr); 219 248 $this->write($Com, !$Arr); 220 249 $i = 0; 221 foreach ($Arr as $k => $v) { 222 switch ($k[0]) { 250 foreach ($Arr as $k => $v) 251 { 252 switch ($k[0]) 253 { 223 254 case "?": 224 255 $el = "$k=$v";
Note:
See TracChangeset
for help on using the changeset viewer.