lainchan/inc/remote.php

65 lines
1.7 KiB
PHP
Raw Normal View History

<?php
2012-04-11 12:49:22 -04:00
/*
2013-01-20 05:23:46 -05:00
* Copyright (c) 2010-2013 Tinyboard Development Group
2012-04-11 12:49:22 -04:00
*/
defined('TINYBOARD') or exit;
2012-04-11 12:49:22 -04:00
class Remote {
public function __construct($config) {
2012-04-12 10:18:19 -04:00
foreach ($config as $name => $value) {
2012-04-11 12:49:22 -04:00
$this->{$name} = $value;
}
2012-08-27 07:50:15 -04:00
$methods = array();
2012-04-11 12:49:22 -04:00
2012-04-12 10:18:19 -04:00
if (!isset($this->auth['method']))
2012-04-11 12:49:22 -04:00
error('Unspecified authentication method.');
// Connect
$this->connection = ssh2_connect($this->host, isset($this->port) ? $this->port : 22, $methods);
2012-04-12 10:18:19 -04:00
switch ($this->auth['method']) {
2012-04-11 12:49:22 -04:00
case 'pubkey':
2012-04-12 10:18:19 -04:00
if (!isset($this->auth['public']))
2012-04-11 12:49:22 -04:00
error('Public key filename not specified.');
2012-04-12 10:18:19 -04:00
if (!isset($this->auth['private']))
2012-04-11 12:49:22 -04:00
error('Private key filename not specified.');
2012-04-12 10:18:19 -04:00
if (!ssh2_auth_pubkey_file($this->connection, $this->auth['username'], $this->auth['public'], $this->auth['private'], isset($this->auth['passphrase']) ? $this->auth['passphrase']: null))
2012-04-11 12:49:22 -04:00
error('Public key authentication failed.');
break;
case 'plain':
2012-04-12 10:18:19 -04:00
if (!ssh2_auth_password($this->connection, $this->auth['username'], $this->auth['password']))
2012-04-11 12:49:22 -04:00
error('Plain-text authentication failed.');
break;
default:
error('Unknown authentication method: "' . $this->auth['method'] . '".');
}
}
public function write($data, $remote_path) {
global $config;
2012-04-12 10:18:19 -04:00
switch ($this->type) {
2012-04-11 12:49:22 -04:00
case 'sftp':
$sftp = ssh2_sftp($this->connection);
file_write('ssh2.sftp://' . $sftp . $remote_path, $data, true);
break;
case 'scp':
$file = tempnam($config['tmp'], 'tinyboard-scp');
// Write to temp file
file_write($file, $data);
ssh2_scp_send($this->connection, $file, $remote_path, 0755);
break;
default:
error('Unknown send method.');
}
2012-04-11 12:49:22 -04:00
}
};