updated to phpseclib 0.3.6
This commit is contained in:
parent
9bfd16cbf9
commit
0e5583298b
|
@ -0,0 +1,560 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Pure-PHP ANSI Decoder
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* If you call read() in Net_SSH2 you may get {@link http://en.wikipedia.org/wiki/ANSI_escape_code ANSI escape codes} back.
|
||||
* They'd look like chr(0x1B) . '[00m' or whatever (0x1B = ESC). They tell a
|
||||
* {@link http://en.wikipedia.org/wiki/Terminal_emulator terminal emulator} how to format the characters, what
|
||||
* color to display them in, etc. File_ANSI is a {@link http://en.wikipedia.org/wiki/VT100 VT100} terminal emulator.
|
||||
*
|
||||
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @category File
|
||||
* @package File_ANSI
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @copyright MMXII Jim Wigginton
|
||||
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||
* @link http://phpseclib.sourceforge.net
|
||||
*/
|
||||
|
||||
/**
|
||||
* Pure-PHP ANSI Decoder
|
||||
*
|
||||
* @package File_ANSI
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @version 0.3.0
|
||||
* @access public
|
||||
*/
|
||||
class File_ANSI
|
||||
{
|
||||
/**
|
||||
* Max Width
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $max_x;
|
||||
|
||||
/**
|
||||
* Max Height
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $max_y;
|
||||
|
||||
/**
|
||||
* Max History
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $max_history;
|
||||
|
||||
/**
|
||||
* History
|
||||
*
|
||||
* @var Array
|
||||
* @access private
|
||||
*/
|
||||
var $history;
|
||||
|
||||
/**
|
||||
* History Attributes
|
||||
*
|
||||
* @var Array
|
||||
* @access private
|
||||
*/
|
||||
var $history_attrs;
|
||||
|
||||
/**
|
||||
* Current Column
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $x;
|
||||
|
||||
/**
|
||||
* Current Row
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $y;
|
||||
|
||||
/**
|
||||
* Old Column
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $old_x;
|
||||
|
||||
/**
|
||||
* Old Row
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $old_y;
|
||||
|
||||
/**
|
||||
* An empty attribute row
|
||||
*
|
||||
* @var Array
|
||||
* @access private
|
||||
*/
|
||||
var $attr_row;
|
||||
|
||||
/**
|
||||
* The current screen text
|
||||
*
|
||||
* @var Array
|
||||
* @access private
|
||||
*/
|
||||
var $screen;
|
||||
|
||||
/**
|
||||
* The current screen attributes
|
||||
*
|
||||
* @var Array
|
||||
* @access private
|
||||
*/
|
||||
var $attrs;
|
||||
|
||||
/**
|
||||
* The current foreground color
|
||||
*
|
||||
* @var String
|
||||
* @access private
|
||||
*/
|
||||
var $foreground;
|
||||
|
||||
/**
|
||||
* The current background color
|
||||
*
|
||||
* @var String
|
||||
* @access private
|
||||
*/
|
||||
var $background;
|
||||
|
||||
/**
|
||||
* Bold flag
|
||||
*
|
||||
* @var Boolean
|
||||
* @access private
|
||||
*/
|
||||
var $bold;
|
||||
|
||||
/**
|
||||
* Underline flag
|
||||
*
|
||||
* @var Boolean
|
||||
* @access private
|
||||
*/
|
||||
var $underline;
|
||||
|
||||
/**
|
||||
* Blink flag
|
||||
*
|
||||
* @var Boolean
|
||||
* @access private
|
||||
*/
|
||||
var $blink;
|
||||
|
||||
/**
|
||||
* Reverse flag
|
||||
*
|
||||
* @var Boolean
|
||||
* @access private
|
||||
*/
|
||||
var $reverse;
|
||||
|
||||
/**
|
||||
* Color flag
|
||||
*
|
||||
* @var Boolean
|
||||
* @access private
|
||||
*/
|
||||
var $color;
|
||||
|
||||
/**
|
||||
* Current ANSI code
|
||||
*
|
||||
* @var String
|
||||
* @access private
|
||||
*/
|
||||
var $ansi;
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
*
|
||||
* @return File_ANSI
|
||||
* @access public
|
||||
*/
|
||||
function File_ANSI()
|
||||
{
|
||||
$this->setHistory(200);
|
||||
$this->setDimensions(80, 24);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set terminal width and height
|
||||
*
|
||||
* Resets the screen as well
|
||||
*
|
||||
* @param Integer $x
|
||||
* @param Integer $y
|
||||
* @access public
|
||||
*/
|
||||
function setDimensions($x, $y)
|
||||
{
|
||||
$this->max_x = $x - 1;
|
||||
$this->max_y = $y - 1;
|
||||
$this->x = $this->y = 0;
|
||||
$this->history = $this->history_attrs = array();
|
||||
$this->attr_row = array_fill(0, $this->max_x + 1, '');
|
||||
$this->screen = array_fill(0, $this->max_y + 1, '');
|
||||
$this->attrs = array_fill(0, $this->max_y + 1, $this->attr_row);
|
||||
$this->foreground = 'white';
|
||||
$this->background = 'black';
|
||||
$this->bold = false;
|
||||
$this->underline = false;
|
||||
$this->blink = false;
|
||||
$this->reverse = false;
|
||||
$this->color = false;
|
||||
|
||||
$this->ansi = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the number of lines that should be logged past the terminal height
|
||||
*
|
||||
* @param Integer $x
|
||||
* @param Integer $y
|
||||
* @access public
|
||||
*/
|
||||
function setHistory($history)
|
||||
{
|
||||
$this->max_history = $history;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a string
|
||||
*
|
||||
* @param String $source
|
||||
* @access public
|
||||
*/
|
||||
function loadString($source)
|
||||
{
|
||||
$this->setDimensions($this->max_x + 1, $this->max_y + 1);
|
||||
$this->appendString($source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appdend a string
|
||||
*
|
||||
* @param String $source
|
||||
* @access public
|
||||
*/
|
||||
function appendString($source)
|
||||
{
|
||||
for ($i = 0; $i < strlen($source); $i++) {
|
||||
if (strlen($this->ansi)) {
|
||||
$this->ansi.= $source[$i];
|
||||
$chr = ord($source[$i]);
|
||||
// http://en.wikipedia.org/wiki/ANSI_escape_code#Sequence_elements
|
||||
// single character CSI's not currently supported
|
||||
switch (true) {
|
||||
case $this->ansi == "\x1B=":
|
||||
$this->ansi = '';
|
||||
continue 2;
|
||||
case strlen($this->ansi) == 2 && $chr >= 64 && $chr <= 95 && $chr != ord('['):
|
||||
case strlen($this->ansi) > 2 && $chr >= 64 && $chr <= 126:
|
||||
break;
|
||||
default:
|
||||
continue 2;
|
||||
}
|
||||
// http://ascii-table.com/ansi-escape-sequences-vt-100.php
|
||||
switch ($this->ansi) {
|
||||
case "\x1B[H": // Move cursor to upper left corner
|
||||
$this->old_x = $this->x;
|
||||
$this->old_y = $this->y;
|
||||
$this->x = $this->y = 0;
|
||||
break;
|
||||
case "\x1B[J": // Clear screen from cursor down
|
||||
$this->history = array_merge($this->history, array_slice(array_splice($this->screen, $this->y + 1), 0, $this->old_y));
|
||||
$this->screen = array_merge($this->screen, array_fill($this->y, $this->max_y, ''));
|
||||
|
||||
$this->history_attrs = array_merge($this->history_attrs, array_slice(array_splice($this->attrs, $this->y + 1), 0, $this->old_y));
|
||||
$this->attrs = array_merge($this->attrs, array_fill($this->y, $this->max_y, $this->attr_row));
|
||||
|
||||
if (count($this->history) == $this->max_history) {
|
||||
array_shift($this->history);
|
||||
array_shift($this->history_attrs);
|
||||
}
|
||||
case "\x1B[K": // Clear screen from cursor right
|
||||
$this->screen[$this->y] = substr($this->screen[$this->y], 0, $this->x);
|
||||
|
||||
array_splice($this->attrs[$this->y], $this->x + 1);
|
||||
break;
|
||||
case "\x1B[2K": // Clear entire line
|
||||
$this->screen[$this->y] = str_repeat(' ', $this->x);
|
||||
$this->attrs[$this->y] = $this->attr_row;
|
||||
break;
|
||||
case "\x1B[?1h": // set cursor key to application
|
||||
case "\x1B[?25h": // show the cursor
|
||||
break;
|
||||
case "\x1BE": // Move to next line
|
||||
$this->_newLine();
|
||||
$this->x = 0;
|
||||
break;
|
||||
default:
|
||||
switch (true) {
|
||||
case preg_match('#\x1B\[(\d+);(\d+)H#', $this->ansi, $match): // Move cursor to screen location v,h
|
||||
$this->old_x = $this->x;
|
||||
$this->old_y = $this->y;
|
||||
$this->x = $match[2] - 1;
|
||||
$this->y = $match[1] - 1;
|
||||
break;
|
||||
case preg_match('#\x1B\[(\d+)C#', $this->ansi, $match): // Move cursor right n lines
|
||||
$this->old_x = $this->x;
|
||||
$x = $match[1] - 1;
|
||||
break;
|
||||
case preg_match('#\x1B\[(\d+);(\d+)r#', $this->ansi, $match): // Set top and bottom lines of a window
|
||||
break;
|
||||
case preg_match('#\x1B\[(\d*(?:;\d*)*)m#', $this->ansi, $match): // character attributes
|
||||
$mods = explode(';', $match[1]);
|
||||
foreach ($mods as $mod) {
|
||||
switch ($mod) {
|
||||
case 0: // Turn off character attributes
|
||||
$this->attrs[$this->y][$this->x] = '';
|
||||
|
||||
if ($this->bold) $this->attrs[$this->y][$this->x].= '</b>';
|
||||
if ($this->underline) $this->attrs[$this->y][$this->x].= '</u>';
|
||||
if ($this->blink) $this->attrs[$this->y][$this->x].= '</blink>';
|
||||
if ($this->color) $this->attrs[$this->y][$this->x].= '</span>';
|
||||
|
||||
if ($this->reverse) {
|
||||
$temp = $this->background;
|
||||
$this->background = $this->foreground;
|
||||
$this->foreground = $temp;
|
||||
}
|
||||
|
||||
$this->bold = $this->underline = $this->blink = $this->color = $this->reverse = false;
|
||||
break;
|
||||
case 1: // Turn bold mode on
|
||||
if (!$this->bold) {
|
||||
$this->attrs[$this->y][$this->x] = '<b>';
|
||||
$this->bold = true;
|
||||
}
|
||||
break;
|
||||
case 4: // Turn underline mode on
|
||||
if (!$this->underline) {
|
||||
$this->attrs[$this->y][$this->x] = '<u>';
|
||||
$this->underline = true;
|
||||
}
|
||||
break;
|
||||
case 5: // Turn blinking mode on
|
||||
if (!$this->blink) {
|
||||
$this->attrs[$this->y][$this->x] = '<blink>';
|
||||
$this->blink = true;
|
||||
}
|
||||
break;
|
||||
case 7: // Turn reverse video on
|
||||
$this->reverse = !$this->reverse;
|
||||
$temp = $this->background;
|
||||
$this->background = $this->foreground;
|
||||
$this->foreground = $temp;
|
||||
$this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
|
||||
if ($this->color) {
|
||||
$this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
|
||||
}
|
||||
$this->color = true;
|
||||
break;
|
||||
default: // set colors
|
||||
//$front = $this->reverse ? &$this->background : &$this->foreground;
|
||||
$front = &$this->{ $this->reverse ? 'background' : 'foreground' };
|
||||
//$back = $this->reverse ? &$this->foreground : &$this->background;
|
||||
$back = &$this->{ $this->reverse ? 'foreground' : 'background' };
|
||||
switch ($mod) {
|
||||
case 30: $front = 'black'; break;
|
||||
case 31: $front = 'red'; break;
|
||||
case 32: $front = 'green'; break;
|
||||
case 33: $front = 'yellow'; break;
|
||||
case 34: $front = 'blue'; break;
|
||||
case 35: $front = 'magenta'; break;
|
||||
case 36: $front = 'cyan'; break;
|
||||
case 37: $front = 'white'; break;
|
||||
|
||||
case 40: $back = 'black'; break;
|
||||
case 41: $back = 'red'; break;
|
||||
case 42: $back = 'green'; break;
|
||||
case 43: $back = 'yellow'; break;
|
||||
case 44: $back = 'blue'; break;
|
||||
case 45: $back = 'magenta'; break;
|
||||
case 46: $back = 'cyan'; break;
|
||||
case 47: $back = 'white'; break;
|
||||
|
||||
default:
|
||||
user_error('Unsupported attribute: ' . $mod);
|
||||
$this->ansi = '';
|
||||
break 2;
|
||||
}
|
||||
|
||||
unset($temp);
|
||||
$this->attrs[$this->y][$this->x] = '<span style="color: ' . $this->foreground . '; background: ' . $this->background . '">';
|
||||
if ($this->color) {
|
||||
$this->attrs[$this->y][$this->x] = '</span>' . $this->attrs[$this->y][$this->x];
|
||||
}
|
||||
$this->color = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
user_error("{$this->ansi} unsupported\r\n");
|
||||
}
|
||||
}
|
||||
$this->ansi = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
switch ($source[$i]) {
|
||||
case "\r":
|
||||
$this->x = 0;
|
||||
break;
|
||||
case "\n":
|
||||
$this->_newLine();
|
||||
break;
|
||||
case "\x0F": // shift
|
||||
break;
|
||||
case "\x1B": // start ANSI escape code
|
||||
$this->ansi.= "\x1B";
|
||||
break;
|
||||
default:
|
||||
$this->screen[$this->y] = substr_replace(
|
||||
$this->screen[$this->y],
|
||||
$source[$i],
|
||||
$this->x,
|
||||
1
|
||||
);
|
||||
|
||||
if ($this->x > $this->max_x) {
|
||||
$this->x = 0;
|
||||
$this->y++;
|
||||
} else {
|
||||
$this->x++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new line
|
||||
*
|
||||
* Also update the $this->screen and $this->history buffers
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
function _newLine()
|
||||
{
|
||||
//if ($this->y < $this->max_y) {
|
||||
// $this->y++;
|
||||
//}
|
||||
|
||||
while ($this->y >= $this->max_y) {
|
||||
$this->history = array_merge($this->history, array(array_shift($this->screen)));
|
||||
$this->screen[] = '';
|
||||
|
||||
$this->history_attrs = array_merge($this->history_attrs, array(array_shift($this->attrs)));
|
||||
$this->attrs[] = $this->attr_row;
|
||||
|
||||
if (count($this->history) >= $this->max_history) {
|
||||
array_shift($this->history);
|
||||
array_shift($this->history_attrs);
|
||||
}
|
||||
|
||||
$this->y--;
|
||||
}
|
||||
$this->y++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current screen without preformating
|
||||
*
|
||||
* @access private
|
||||
* @return String
|
||||
*/
|
||||
function _getScreen()
|
||||
{
|
||||
$output = '';
|
||||
for ($i = 0; $i <= $this->max_y; $i++) {
|
||||
for ($j = 0; $j <= $this->max_x + 1; $j++) {
|
||||
if (isset($this->attrs[$i][$j])) {
|
||||
$output.= $this->attrs[$i][$j];
|
||||
}
|
||||
if (isset($this->screen[$i][$j])) {
|
||||
$output.= htmlspecialchars($this->screen[$i][$j]);
|
||||
}
|
||||
}
|
||||
$output.= "\r\n";
|
||||
}
|
||||
return rtrim($output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current screen
|
||||
*
|
||||
* @access public
|
||||
* @return String
|
||||
*/
|
||||
function getScreen()
|
||||
{
|
||||
return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $this->_getScreen() . '</pre>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current screen and the x previous lines
|
||||
*
|
||||
* @access public
|
||||
* @return String
|
||||
*/
|
||||
function getHistory()
|
||||
{
|
||||
$scrollback = '';
|
||||
for ($i = 0; $i < count($this->history); $i++) {
|
||||
for ($j = 0; $j <= $this->max_x + 1; $j++) {
|
||||
if (isset($this->history_attrs[$i][$j])) {
|
||||
$scrollback.= $this->history_attrs[$i][$j];
|
||||
}
|
||||
if (isset($this->history[$i][$j])) {
|
||||
$scrollback.= htmlspecialchars($this->history[$i][$j]);
|
||||
}
|
||||
}
|
||||
$scrollback.= "\r\n";
|
||||
}
|
||||
$scrollback.= $this->_getScreen();
|
||||
|
||||
return '<pre style="color: white; background: black" width="' . ($this->max_x + 1) . '">' . $scrollback . '</pre>';
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,775 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* SFTP Stream Wrapper
|
||||
*
|
||||
* Creates an sftp:// protocol handler that can be used with, for example, fopen(), dir(), etc.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @category Net
|
||||
* @package Net_SFTP_Stream
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @copyright MMXIII Jim Wigginton
|
||||
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||
* @link http://phpseclib.sourceforge.net
|
||||
*/
|
||||
|
||||
/**
|
||||
* SFTP Stream Wrapper
|
||||
*
|
||||
* @package Net_SFTP_Stream
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @version 0.3.2
|
||||
* @access public
|
||||
*/
|
||||
class Net_SFTP_Stream
|
||||
{
|
||||
/**
|
||||
* SFTP instances
|
||||
*
|
||||
* Rather than re-create the connection we re-use instances if possible
|
||||
*
|
||||
* @var Array
|
||||
* @access static
|
||||
*/
|
||||
static $instances;
|
||||
|
||||
/**
|
||||
* SFTP instance
|
||||
*
|
||||
* @var Object
|
||||
* @access private
|
||||
*/
|
||||
var $sftp;
|
||||
|
||||
/**
|
||||
* Path
|
||||
*
|
||||
* @var String
|
||||
* @access private
|
||||
*/
|
||||
var $path;
|
||||
|
||||
/**
|
||||
* Mode
|
||||
*
|
||||
* @var String
|
||||
* @access private
|
||||
*/
|
||||
var $mode;
|
||||
|
||||
/**
|
||||
* Position
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $pos;
|
||||
|
||||
/**
|
||||
* Size
|
||||
*
|
||||
* @var Integer
|
||||
* @access private
|
||||
*/
|
||||
var $size;
|
||||
|
||||
/**
|
||||
* Directory entries
|
||||
*
|
||||
* @var Array
|
||||
* @access private
|
||||
*/
|
||||
var $entries;
|
||||
|
||||
/**
|
||||
* EOF flag
|
||||
*
|
||||
* @var Boolean
|
||||
* @access private
|
||||
*/
|
||||
var $eof;
|
||||
|
||||
/**
|
||||
* Context resource
|
||||
*
|
||||
* Technically this needs to be publically accessible so PHP can set it directly
|
||||
*
|
||||
* @var Resource
|
||||
* @access public
|
||||
*/
|
||||
var $context;
|
||||
|
||||
/**
|
||||
* Notification callback function
|
||||
*
|
||||
* @var Callable
|
||||
* @access public
|
||||
*/
|
||||
var $notification;
|
||||
|
||||
/**
|
||||
* The Constructor
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function Net_SFTP_Stream()
|
||||
{
|
||||
if (defined('NET_SFTP_STREAM_LOGGING')) {
|
||||
echo "__construct()\r\n";
|
||||
}
|
||||
|
||||
if (!class_exists('Net_SFTP')) {
|
||||
include_once 'Net/SFTP.php';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Path Parser
|
||||
*
|
||||
* Extract a path from a URI and actually connect to an SSH server if appropriate
|
||||
*
|
||||
* If "notification" is set as a context parameter the message code for successful login is
|
||||
* NET_SSH2_MSG_USERAUTH_SUCCESS. For a failed login it's NET_SSH2_MSG_USERAUTH_FAILURE.
|
||||
*
|
||||
* @param String $path
|
||||
* @return String
|
||||
* @access private
|
||||
*/
|
||||
function _parse_path($path)
|
||||
{
|
||||
extract(parse_url($path) + array('port' => 22));
|
||||
|
||||
if (!isset($host)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($this->context)) {
|
||||
$context = stream_context_get_params($this->context);
|
||||
if (isset($context['notification'])) {
|
||||
$this->notification = $context['notification'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($host[0] == '$') {
|
||||
$host = substr($host, 1);
|
||||
global $$host;
|
||||
if (!is_object($$host) || get_class($$host) != 'Net_SFTP') {
|
||||
return false;
|
||||
}
|
||||
$this->sftp = $$host;
|
||||
} else {
|
||||
if (isset($this->context)) {
|
||||
$context = stream_context_get_options($this->context);
|
||||
}
|
||||
if (isset($context['sftp']['session'])) {
|
||||
$sftp = $context['sftp']['session'];
|
||||
}
|
||||
if (isset($context['sftp']['sftp'])) {
|
||||
$sftp = $context['sftp']['sftp'];
|
||||
}
|
||||
if (isset($sftp) && is_object($sftp) && get_class($sftp) == 'Net_SFTP') {
|
||||
$this->sftp = $sftp;
|
||||
return $path;
|
||||
}
|
||||
if (isset($context['sftp']['username'])) {
|
||||
$user = $context['sftp']['username'];
|
||||
}
|
||||
if (isset($context['sftp']['password'])) {
|
||||
$pass = $context['sftp']['password'];
|
||||
}
|
||||
if (isset($context['sftp']['privkey']) && is_object($context['sftp']['privkey']) && get_Class($context['sftp']['privkey']) == 'Crypt_RSA') {
|
||||
$pass = $context['sftp']['privkey'];
|
||||
}
|
||||
|
||||
if (!isset($user) || !isset($pass)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// casting $pass to a string is necessary in the event that it's a Crypt_RSA object
|
||||
if (isset(self::$instances[$host][$port][$user][(string) $pass])) {
|
||||
$this->sftp = self::$instances[$host][$port][$user][(string) $pass];
|
||||
} else {
|
||||
$this->sftp = new Net_SFTP($host, $port);
|
||||
if (isset($this->notification) && is_callable($this->notification)) {
|
||||
/* if !is_callable($this->notification) we could do this:
|
||||
|
||||
user_error('fopen(): failed to call user notifier', E_USER_WARNING);
|
||||
|
||||
the ftp wrapper gives errors like that when the notifier isn't callable.
|
||||
i've opted not to do that, however, since the ftp wrapper gives the line
|
||||
on which the fopen occurred as the line number - not the line that the
|
||||
user_error is on.
|
||||
*/
|
||||
call_user_func($this->notification, STREAM_NOTIFY_CONNECT, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0);
|
||||
call_user_func($this->notification, STREAM_NOTIFY_AUTH_REQUIRED, STREAM_NOTIFY_SEVERITY_INFO, '', 0, 0, 0);
|
||||
if (!$this->sftp->login($user, $pass)) {
|
||||
call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_ERR, 'Login Failure', NET_SSH2_MSG_USERAUTH_FAILURE, 0, 0);
|
||||
return false;
|
||||
}
|
||||
call_user_func($this->notification, STREAM_NOTIFY_AUTH_RESULT, STREAM_NOTIFY_SEVERITY_INFO, 'Login Success', NET_SSH2_MSG_USERAUTH_SUCCESS, 0, 0);
|
||||
} else {
|
||||
if (!$this->sftp->login($user, $pass)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self::$instances[$host][$port][$user][(string) $pass] = $this->sftp;
|
||||
}
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens file or URL
|
||||
*
|
||||
* @param String $path
|
||||
* @param String $mode
|
||||
* @param Integer $options
|
||||
* @param String $opened_path
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_open($path, $mode, $options, &$opened_path)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
$this->path = $path;
|
||||
|
||||
$this->size = $this->sftp->size($path);
|
||||
$this->mode = preg_replace('#[bt]$#', '', $mode);
|
||||
$this->eof = false;
|
||||
|
||||
if ($this->size === false) {
|
||||
if ($this->mode[0] == 'r') {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
switch ($this->mode[0]) {
|
||||
case 'x':
|
||||
return false;
|
||||
case 'w':
|
||||
case 'c':
|
||||
$this->sftp->truncate($path, 0);
|
||||
}
|
||||
}
|
||||
|
||||
$this->pos = $this->mode[0] != 'a' ? 0 : $this->size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from stream
|
||||
*
|
||||
* @param Integer $count
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function _stream_read($count)
|
||||
{
|
||||
switch ($this->mode) {
|
||||
case 'w':
|
||||
case 'a':
|
||||
case 'x':
|
||||
case 'c':
|
||||
return false;
|
||||
}
|
||||
|
||||
// commented out because some files - eg. /dev/urandom - will say their size is 0 when in fact it's kinda infinite
|
||||
//if ($this->pos >= $this->size) {
|
||||
// $this->eof = true;
|
||||
// return false;
|
||||
//}
|
||||
|
||||
$result = $this->sftp->get($this->path, false, $this->pos, $count);
|
||||
if (isset($this->notification) && is_callable($this->notification)) {
|
||||
if ($result === false) {
|
||||
call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
|
||||
return 0;
|
||||
}
|
||||
// seems that PHP calls stream_read in 8k chunks
|
||||
call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size);
|
||||
}
|
||||
|
||||
if (empty($result)) { // ie. false or empty string
|
||||
$this->eof = true;
|
||||
return false;
|
||||
}
|
||||
$this->pos+= strlen($result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to stream
|
||||
*
|
||||
* @param String $data
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function _stream_write($data)
|
||||
{
|
||||
switch ($this->mode) {
|
||||
case 'r':
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->sftp->put($this->path, $data, NET_SFTP_STRING, $this->pos);
|
||||
if (isset($this->notification) && is_callable($this->notification)) {
|
||||
if (!$result) {
|
||||
call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
|
||||
return 0;
|
||||
}
|
||||
// seems that PHP splits up strings into 8k blocks before calling stream_write
|
||||
call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($data), strlen($data));
|
||||
}
|
||||
|
||||
if ($result === false) {
|
||||
return false;
|
||||
}
|
||||
$this->pos+= strlen($data);
|
||||
if ($this->pos > $this->size) {
|
||||
$this->size = $this->pos;
|
||||
}
|
||||
$this->eof = false;
|
||||
return strlen($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the current position of a stream
|
||||
*
|
||||
* @return Integer
|
||||
* @access public
|
||||
*/
|
||||
function _stream_tell()
|
||||
{
|
||||
return $this->pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for end-of-file on a file pointer
|
||||
*
|
||||
* In my testing there are four classes functions that normally effect the pointer:
|
||||
* fseek, fputs / fwrite, fgets / fread and ftruncate.
|
||||
*
|
||||
* Only fgets / fread, however, results in feof() returning true. do fputs($fp, 'aaa') on a blank file and feof()
|
||||
* will return false. do fread($fp, 1) and feof() will then return true. do fseek($fp, 10) on ablank file and feof()
|
||||
* will return false. do fread($fp, 1) and feof() will then return true.
|
||||
*
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_eof()
|
||||
{
|
||||
return $this->eof;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seeks to specific location in a stream
|
||||
*
|
||||
* @param Integer $offset
|
||||
* @param Integer $whence
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_seek($offset, $whence)
|
||||
{
|
||||
switch ($whence) {
|
||||
case SEEK_SET:
|
||||
if ($offset >= $this->size || $offset < 0) {
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case SEEK_CUR:
|
||||
$offset+= $this->pos;
|
||||
break;
|
||||
case SEEK_END:
|
||||
$offset+= $this->size;
|
||||
}
|
||||
|
||||
$this->pos = $offset;
|
||||
$this->eof = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change stream options
|
||||
*
|
||||
* @param String $path
|
||||
* @param Integer $option
|
||||
* @param Mixed $var
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_metadata($path, $option, $var)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// stream_metadata was introduced in PHP 5.4.0 but as of 5.4.11 the constants haven't been defined
|
||||
// see http://www.php.net/streamwrapper.stream-metadata and https://bugs.php.net/64246
|
||||
// and https://github.com/php/php-src/blob/master/main/php_streams.h#L592
|
||||
switch ($option) {
|
||||
case 1: // PHP_STREAM_META_TOUCH
|
||||
return $this->sftp->touch($path, $var[0], $var[1]);
|
||||
case 2: // PHP_STREAM_OWNER_NAME
|
||||
case 3: // PHP_STREAM_GROUP_NAME
|
||||
return false;
|
||||
case 4: // PHP_STREAM_META_OWNER
|
||||
return $this->sftp->chown($path, $var);
|
||||
case 5: // PHP_STREAM_META_GROUP
|
||||
return $this->sftp->chgrp($path, $var);
|
||||
case 6: // PHP_STREAM_META_ACCESS
|
||||
return $this->sftp->chmod($path, $var) !== false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the underlaying resource
|
||||
*
|
||||
* @param Integer $cast_as
|
||||
* @return Resource
|
||||
* @access public
|
||||
*/
|
||||
function _stream_cast($cast_as)
|
||||
{
|
||||
return $this->sftp->fsock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Advisory file locking
|
||||
*
|
||||
* @param Integer $operation
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_lock($operation)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renames a file or directory
|
||||
*
|
||||
* Attempts to rename oldname to newname, moving it between directories if necessary.
|
||||
* If newname exists, it will be overwritten. This is a departure from what Net_SFTP
|
||||
* does.
|
||||
*
|
||||
* @param String $path_from
|
||||
* @param String $path_to
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _rename($path_from, $path_to)
|
||||
{
|
||||
$path1 = parse_url($path_from);
|
||||
$path2 = parse_url($path_to);
|
||||
unset($path1['path'], $path2['path']);
|
||||
if ($path1 != $path2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path_from = $this->_parse_path($path_from);
|
||||
$path_to = parse_url($path_to);
|
||||
if ($path_from == false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path_to = $path_to['path']; // the $component part of parse_url() was added in PHP 5.1.2
|
||||
// "It is an error if there already exists a file with the name specified by newpath."
|
||||
// -- http://tools.ietf.org/html/draft-ietf-secsh-filexfer-02#section-6.5
|
||||
if (!$this->sftp->rename($path_from, $path_to)) {
|
||||
if ($this->sftp->stat($path_to)) {
|
||||
return $this->sftp->delete($path_to, true) && $this->sftp->rename($path_from, $path_to);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open directory handle
|
||||
*
|
||||
* The only $options is "whether or not to enforce safe_mode (0x04)". Since safe mode was deprecated in 5.3 and
|
||||
* removed in 5.4 I'm just going to ignore it
|
||||
*
|
||||
* @param String $path
|
||||
* @param Integer $options
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _dir_opendir($path, $options)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
$this->pos = 0;
|
||||
$this->entries = $this->sftp->nlist($path);
|
||||
return $this->entries !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read entry from directory handle
|
||||
*
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function _dir_readdir()
|
||||
{
|
||||
if (isset($this->entries[$this->pos])) {
|
||||
return $this->entries[$this->pos++];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind directory handle
|
||||
*
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _dir_rewinddir()
|
||||
{
|
||||
$this->pos = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close directory handle
|
||||
*
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _dir_closedir()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a directory
|
||||
*
|
||||
* Only valid $options is STREAM_MKDIR_RECURSIVE
|
||||
*
|
||||
* @param String $path
|
||||
* @param Integer $mode
|
||||
* @param Integer $options
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _mkdir($path, $mode, $options)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->sftp->mkdir($path, $mode, $options & STREAM_MKDIR_RECURSIVE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a directory
|
||||
*
|
||||
* Only valid $options is STREAM_MKDIR_RECURSIVE per <http://php.net/streamwrapper.rmdir>, however,
|
||||
* <http://php.net/rmdir> does not have a $recursive parameter as mkdir() does so I don't know how
|
||||
* STREAM_MKDIR_RECURSIVE is supposed to be set. Also, when I try it out with rmdir() I get 8 as
|
||||
* $options. What does 8 correspond to?
|
||||
*
|
||||
* @param String $path
|
||||
* @param Integer $mode
|
||||
* @param Integer $options
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _rmdir($path, $options)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->sftp->rmdir($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes the output
|
||||
*
|
||||
* See <http://php.net/fflush>. Always returns true because Net_SFTP doesn't cache stuff before writing
|
||||
*
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_flush()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve information about a file resource
|
||||
*
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function _stream_stat()
|
||||
{
|
||||
$results = $this->sftp->stat($this->path);
|
||||
if ($results === false) {
|
||||
return false;
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a file
|
||||
*
|
||||
* @param String $path
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _unlink($path)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->sftp->delete($path, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve information about a file
|
||||
*
|
||||
* Ignores the STREAM_URL_STAT_QUIET flag because the entirety of Net_SFTP_Stream is quiet by default
|
||||
* might be worthwhile to reconstruct bits 12-16 (ie. the file type) if mode doesn't have them but we'll
|
||||
* cross that bridge when and if it's reached
|
||||
*
|
||||
* @param String $path
|
||||
* @param Integer $flags
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function _url_stat($path, $flags)
|
||||
{
|
||||
$path = $this->_parse_path($path);
|
||||
if ($path === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$results = $flags & STREAM_URL_STAT_LINK ? $this->sftp->lstat($path) : $this->sftp->stat($path);
|
||||
if ($results === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncate stream
|
||||
*
|
||||
* @param Integer $new_size
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_truncate($new_size)
|
||||
{
|
||||
if (!$this->sftp->truncate($this->path, $new_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->eof = false;
|
||||
$this->size = $new_size;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change stream options
|
||||
*
|
||||
* STREAM_OPTION_WRITE_BUFFER isn't supported for the same reason stream_flush isn't.
|
||||
* The other two aren't supported because of limitations in Net_SFTP.
|
||||
*
|
||||
* @param Integer $option
|
||||
* @param Integer $arg1
|
||||
* @param Integer $arg2
|
||||
* @return Boolean
|
||||
* @access public
|
||||
*/
|
||||
function _stream_set_option($option, $arg1, $arg2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close an resource
|
||||
*
|
||||
* @access public
|
||||
*/
|
||||
function _stream_close()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* __call Magic Method
|
||||
*
|
||||
* When you're utilizing an SFTP stream you're not calling the methods in this class directly - PHP is calling them for you.
|
||||
* Which kinda begs the question... what methods is PHP calling and what parameters is it passing to them? This function
|
||||
* lets you figure that out.
|
||||
*
|
||||
* If NET_SFTP_STREAM_LOGGING is defined all calls will be output on the screen and then (regardless of whether or not
|
||||
* NET_SFTP_STREAM_LOGGING is enabled) the parameters will be passed through to the appropriate method.
|
||||
*
|
||||
* @param String
|
||||
* @param Array
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function __call($name, $arguments)
|
||||
{
|
||||
if (defined('NET_SFTP_STREAM_LOGGING')) {
|
||||
echo $name . '(';
|
||||
$last = count($arguments) - 1;
|
||||
foreach ($arguments as $i => $argument) {
|
||||
var_export($argument);
|
||||
if ($i != $last) {
|
||||
echo ',';
|
||||
}
|
||||
}
|
||||
echo ")\r\n";
|
||||
}
|
||||
$name = '_' . $name;
|
||||
if (!method_exists($this, $name)) {
|
||||
return false;
|
||||
}
|
||||
return call_user_func_array(array($this, $name), $arguments);
|
||||
}
|
||||
}
|
||||
|
||||
if (function_exists('stream_wrapper_register')) {
|
||||
stream_wrapper_register('sftp', 'Net_SFTP_Stream');
|
||||
}
|
|
@ -0,0 +1,315 @@
|
|||
<?php
|
||||
/**
|
||||
* Pure-PHP ssh-agent client.
|
||||
*
|
||||
* PHP versions 4 and 5
|
||||
*
|
||||
* Here are some examples of how to use this library:
|
||||
* <code>
|
||||
* <?php
|
||||
* include('System/SSH_Agent.php');
|
||||
* include('Net/SSH2.php');
|
||||
*
|
||||
* $agent = new System_SSH_Agent();
|
||||
*
|
||||
* $ssh = new Net_SSH2('www.domain.tld');
|
||||
* if (!$ssh->login('username', $agent)) {
|
||||
* exit('Login Failed');
|
||||
* }
|
||||
*
|
||||
* echo $ssh->exec('pwd');
|
||||
* echo $ssh->exec('ls -la');
|
||||
* ?>
|
||||
* </code>
|
||||
*
|
||||
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* @category System
|
||||
* @package System_SSH_Agent
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @copyright MMXIV Jim Wigginton
|
||||
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||
* @link http://phpseclib.sourceforge.net
|
||||
* @internal See http://api.libssh.org/rfc/PROTOCOL.agent
|
||||
*/
|
||||
|
||||
/**#@+
|
||||
* Message numbers
|
||||
*
|
||||
* @access private
|
||||
*/
|
||||
// to request SSH1 keys you have to use SSH_AGENTC_REQUEST_RSA_IDENTITIES (1)
|
||||
define('SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES', 11);
|
||||
// this is the SSH2 response; the SSH1 response is SSH_AGENT_RSA_IDENTITIES_ANSWER (2).
|
||||
define('SYSTEM_SSH_AGENT_IDENTITIES_ANSWER', 12);
|
||||
define('SYSTEM_SSH_AGENT_FAILURE', 5);
|
||||
// the SSH1 request is SSH_AGENTC_RSA_CHALLENGE (3)
|
||||
define('SYSTEM_SSH_AGENTC_SIGN_REQUEST', 13);
|
||||
// the SSH1 response is SSH_AGENT_RSA_RESPONSE (4)
|
||||
define('SYSTEM_SSH_AGENT_SIGN_RESPONSE', 14);
|
||||
/**#@-*/
|
||||
|
||||
/**
|
||||
* Pure-PHP ssh-agent client identity object
|
||||
*
|
||||
* Instantiation should only be performed by System_SSH_Agent class.
|
||||
* This could be thought of as implementing an interface that Crypt_RSA
|
||||
* implements. ie. maybe a Net_SSH_Auth_PublicKey interface or something.
|
||||
* The methods in this interface would be getPublicKey, setSignatureMode
|
||||
* and sign since those are the methods phpseclib looks for to perform
|
||||
* public key authentication.
|
||||
*
|
||||
* @package System_SSH_Agent
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @version 0.1.0
|
||||
* @access internal
|
||||
*/
|
||||
class System_SSH_Agent_Identity
|
||||
{
|
||||
/**
|
||||
* Key Object
|
||||
*
|
||||
* @var Crypt_RSA
|
||||
* @access private
|
||||
* @see System_SSH_Agent_Identity::getPublicKey()
|
||||
*/
|
||||
var $key;
|
||||
|
||||
/**
|
||||
* Key Blob
|
||||
*
|
||||
* @var String
|
||||
* @access private
|
||||
* @see System_SSH_Agent_Identity::sign()
|
||||
*/
|
||||
var $key_blob;
|
||||
|
||||
/**
|
||||
* Socket Resource
|
||||
*
|
||||
* @var Resource
|
||||
* @access private
|
||||
* @see System_SSH_Agent_Identity::sign()
|
||||
*/
|
||||
var $fsock;
|
||||
|
||||
/**
|
||||
* Default Constructor.
|
||||
*
|
||||
* @param Resource $fsock
|
||||
* @return System_SSH_Agent_Identity
|
||||
* @access private
|
||||
*/
|
||||
function System_SSH_Agent_Identity($fsock)
|
||||
{
|
||||
$this->fsock = $fsock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Public Key
|
||||
*
|
||||
* Called by System_SSH_Agent::requestIdentities()
|
||||
*
|
||||
* @param Crypt_RSA $key
|
||||
* @access private
|
||||
*/
|
||||
function setPublicKey($key)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->key->setPublicKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Public Key
|
||||
*
|
||||
* Called by System_SSH_Agent::requestIdentities(). The key blob could be extracted from $this->key
|
||||
* but this saves a small amount of computation.
|
||||
*
|
||||
* @param String $key_blob
|
||||
* @access private
|
||||
*/
|
||||
function setPublicKeyBlob($key_blob)
|
||||
{
|
||||
$this->key_blob = $key_blob;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Public Key
|
||||
*
|
||||
* Wrapper for $this->key->getPublicKey()
|
||||
*
|
||||
* @param Integer $format optional
|
||||
* @return Mixed
|
||||
* @access public
|
||||
*/
|
||||
function getPublicKey($format = null)
|
||||
{
|
||||
return !isset($format) ? $this->key->getPublicKey() : $this->key->getPublicKey($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Signature Mode
|
||||
*
|
||||
* Doesn't do anything as ssh-agent doesn't let you pick and choose the signature mode. ie.
|
||||
* ssh-agent's only supported mode is CRYPT_RSA_SIGNATURE_PKCS1
|
||||
*
|
||||
* @param Integer $mode
|
||||
* @access public
|
||||
*/
|
||||
function setSignatureMode($mode)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a signature
|
||||
*
|
||||
* See "2.6.2 Protocol 2 private key signature request"
|
||||
*
|
||||
* @param String $message
|
||||
* @return String
|
||||
* @access public
|
||||
*/
|
||||
function sign($message)
|
||||
{
|
||||
// the last parameter (currently 0) is for flags and ssh-agent only defines one flag (for ssh-dss): SSH_AGENT_OLD_SIGNATURE
|
||||
$packet = pack('CNa*Na*N', SYSTEM_SSH_AGENTC_SIGN_REQUEST, strlen($this->key_blob), $this->key_blob, strlen($message), $message, 0);
|
||||
$packet = pack('Na*', strlen($packet), $packet);
|
||||
if (strlen($packet) != fputs($this->fsock, $packet)) {
|
||||
user_error('Connection closed during signing');
|
||||
}
|
||||
|
||||
$length = current(unpack('N', fread($this->fsock, 4)));
|
||||
$type = ord(fread($this->fsock, 1));
|
||||
if ($type != SYSTEM_SSH_AGENT_SIGN_RESPONSE) {
|
||||
user_error('Unable to retreive signature');
|
||||
}
|
||||
|
||||
$signature_blob = fread($this->fsock, $length - 1);
|
||||
// the only other signature format defined - ssh-dss - is the same length as ssh-rsa
|
||||
// the + 12 is for the other various SSH added length fields
|
||||
return substr($signature_blob, strlen('ssh-rsa') + 12);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure-PHP ssh-agent client identity factory
|
||||
*
|
||||
* requestIdentities() method pumps out System_SSH_Agent_Identity objects
|
||||
*
|
||||
* @package System_SSH_Agent
|
||||
* @author Jim Wigginton <terrafrost@php.net>
|
||||
* @version 0.1.0
|
||||
* @access internal
|
||||
*/
|
||||
class System_SSH_Agent
|
||||
{
|
||||
/**
|
||||
* Socket Resource
|
||||
*
|
||||
* @var Resource
|
||||
* @access private
|
||||
*/
|
||||
var $fsock;
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
*
|
||||
* @return System_SSH_Agent
|
||||
* @access public
|
||||
*/
|
||||
function System_SSH_Agent()
|
||||
{
|
||||
switch (true) {
|
||||
case isset($_SERVER['SSH_AUTH_SOCK']):
|
||||
$address = $_SERVER['SSH_AUTH_SOCK'];
|
||||
break;
|
||||
case isset($_ENV['SSH_AUTH_SOCK']):
|
||||
$address = $_ENV['SSH_AUTH_SOCK'];
|
||||
break;
|
||||
default:
|
||||
user_error('SSH_AUTH_SOCK not found');
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->fsock = fsockopen('unix://' . $address, 0, $errno, $errstr);
|
||||
if (!$this->fsock) {
|
||||
user_error("Unable to connect to ssh-agent (Error $errno: $errstr)");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request Identities
|
||||
*
|
||||
* See "2.5.2 Requesting a list of protocol 2 keys"
|
||||
* Returns an array containing zero or more System_SSH_Agent_Identity objects
|
||||
*
|
||||
* @return Array
|
||||
* @access public
|
||||
*/
|
||||
function requestIdentities()
|
||||
{
|
||||
if (!$this->fsock) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$packet = pack('NC', 1, SYSTEM_SSH_AGENTC_REQUEST_IDENTITIES);
|
||||
if (strlen($packet) != fputs($this->fsock, $packet)) {
|
||||
user_error('Connection closed while requesting identities');
|
||||
}
|
||||
|
||||
$length = current(unpack('N', fread($this->fsock, 4)));
|
||||
$type = ord(fread($this->fsock, 1));
|
||||
if ($type != SYSTEM_SSH_AGENT_IDENTITIES_ANSWER) {
|
||||
user_error('Unable to request identities');
|
||||
}
|
||||
|
||||
$identities = array();
|
||||
$keyCount = current(unpack('N', fread($this->fsock, 4)));
|
||||
for ($i = 0; $i < $keyCount; $i++) {
|
||||
$length = current(unpack('N', fread($this->fsock, 4)));
|
||||
$key_blob = fread($this->fsock, $length);
|
||||
$length = current(unpack('N', fread($this->fsock, 4)));
|
||||
$key_comment = fread($this->fsock, $length);
|
||||
$length = current(unpack('N', substr($key_blob, 0, 4)));
|
||||
$key_type = substr($key_blob, 4, $length);
|
||||
switch ($key_type) {
|
||||
case 'ssh-rsa':
|
||||
if (!class_exists('Crypt_RSA')) {
|
||||
include_once 'Crypt/RSA.php';
|
||||
}
|
||||
$key = new Crypt_RSA();
|
||||
$key->loadKey('ssh-rsa ' . base64_encode($key_blob) . ' ' . $key_comment);
|
||||
break;
|
||||
case 'ssh-dss':
|
||||
// not currently supported
|
||||
break;
|
||||
}
|
||||
// resources are passed by reference by default
|
||||
if (isset($key)) {
|
||||
$identity = new System_SSH_Agent_Identity($this->fsock);
|
||||
$identity->setPublicKey($key);
|
||||
$identity->setPublicKeyBlob($key_blob);
|
||||
$identities[] = $identity;
|
||||
unset($key);
|
||||
}
|
||||
}
|
||||
|
||||
return $identities;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue