pssht  latest
SSH server library written in PHP
REQUEST.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
17 class REQUEST extends Base
18 {
19  // SSH_MSG_CHANNEL_REQUEST = 98
20  public function handle(
21  $msgType,
22  \fpoirotte\Pssht\Wire\Decoder $decoder,
23  \fpoirotte\Pssht\Transport $transport,
24  array &$context
25  ) {
26  $encoder = new \fpoirotte\Pssht\Wire\Encoder();
27  $channel = $decoder->decodeUint32();
28  $type = $decoder->decodeString();
29  $wantsReply = $decoder->decodeBoolean();
30 
31  $encoder->encodeUint32($channel);
32  $encoder->encodeString($type);
33  $encoder->encodeBoolean($wantsReply);
34  $decoder->getBuffer()->unget($encoder->getBuffer()->get(0));
35  $remoteChannel = $this->connection->getChannel($channel);
36 
37  switch ($type) {
38  case 'exec':
39  case 'shell':
40  case 'pty-req':
41  // Normalize the name.
42  // Eg. "pty-req" becomes "PtyReq".
43  $cls = str_replace(' ', '', ucwords(str_replace('-', ' ', $type)));
44  $cls = '\\fpoirotte\\Pssht\\Messages\\CHANNEL\\REQUEST\\' . $cls;
45  $message = $cls::unserialize($decoder);
46  break;
47 
48  default:
49  if ($wantsReply) {
50  $response = new \fpoirotte\Pssht\Messages\CHANNEL\FAILURE($remoteChannel);
51  $transport->writeMessage($response);
52  }
53  return true;
54  }
55 
56  if (!$wantsReply) {
57  return true;
58  }
59 
60  if (in_array($type, array('shell', 'exec'), true)) {
61  $response = new \fpoirotte\Pssht\Messages\CHANNEL\SUCCESS($remoteChannel);
62  } else {
63  $response = new \fpoirotte\Pssht\Messages\CHANNEL\FAILURE($remoteChannel);
64  }
65  $transport->writeMessage($response);
66 
67  if (in_array($type, array('shell', 'exec'), true)) {
68  $callable = $transport->getApplicationFactory();
69  if ($callable !== null) {
70  call_user_func($callable, $transport, $this->connection, $message);
71  }
72  }
73 
74  return true;
75  }
76 }
handle($msgType,\fpoirotte\Pssht\Wire\Decoder $decoder,\fpoirotte\Pssht\Transport $transport, array &$context)
Definition: REQUEST.php:20