mirror of
https://github.com/vichan-devel/vichan.git
synced 2024-11-30 18:24:29 +01:00
remote servers -- writing to another server via SSH
This commit is contained in:
parent
2511dc447c
commit
36ee32b38b
@ -30,7 +30,8 @@
|
|||||||
'wordfilters' => Array(),
|
'wordfilters' => Array(),
|
||||||
'custom_capcode' => Array(),
|
'custom_capcode' => Array(),
|
||||||
'custom_tripcode' => Array(),
|
'custom_tripcode' => Array(),
|
||||||
'dnsbl' => Array()
|
'dnsbl' => Array(),
|
||||||
|
'remote' => Array()
|
||||||
);
|
);
|
||||||
// Database stuff
|
// Database stuff
|
||||||
|
|
||||||
|
@ -250,38 +250,46 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function file_write($path, $data) {
|
function file_write($path, $data, $simple = false, $skip_purge = false) {
|
||||||
global $config;
|
global $config;
|
||||||
if(preg_match('/^scp:\/\/(.+)$/', $path, $m)) {
|
|
||||||
// Experimental: secure copy...
|
if(preg_match('/^remote:\/\/(.+)\:(.+)$/', $path, $m)) {
|
||||||
$file = tempnam($config['tmp'], 'tinyboard-scp');
|
if(isset($config['remote'][$m[1]])) {
|
||||||
// Write to temp file
|
require_once 'inc/remote.php';
|
||||||
file_write($file, $data);
|
|
||||||
// Call `scp` (yes, this is horrible)
|
$remote = new Remote($config['remote'][$m[1]]);
|
||||||
$command = 'scp ' . escapeshellarg($file) . ' ' . escapeshellarg($m[1]);
|
$remote->write($data, $m[2]);
|
||||||
system($command);
|
return;
|
||||||
// Delete temporary file
|
} else {
|
||||||
file_unlink($file);
|
error('Invalid remote server: ' . $m[1]);
|
||||||
return;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!$fp = fopen($path, 'c'))
|
if(!$fp = fopen($path, $simple ? 'w' : 'c'))
|
||||||
error('Unable to open file for writing: ' . $path);
|
error('Unable to open file for writing: ' . $path);
|
||||||
|
|
||||||
// File locking
|
// File locking
|
||||||
if(!flock($fp, LOCK_EX)) {
|
if(!$simple && !flock($fp, LOCK_EX)) {
|
||||||
error('Unable to lock file: ' . $path);
|
error('Unable to lock file: ' . $path);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Truncate file
|
// Truncate file
|
||||||
ftruncate($fp, 0);
|
if(!$simple && !ftruncate($fp, 0))
|
||||||
|
error('Unable to truncate file: ' . $path);
|
||||||
|
|
||||||
// Write data
|
// Write data
|
||||||
fwrite($fp, $data);
|
if(fwrite($fp, $data) === false)
|
||||||
flock($fp, LOCK_UN);
|
error('Unable to write to file: ' . $path);
|
||||||
|
|
||||||
fclose($fp);
|
|
||||||
|
|
||||||
if(isset($config['purge']) && isset($_SERVER['HTTP_HOST'])) {
|
// Unlock
|
||||||
|
if(!$simple)
|
||||||
|
flock($fp, LOCK_UN);
|
||||||
|
|
||||||
|
// Close
|
||||||
|
if(!fclose($fp))
|
||||||
|
error('Unable to close file: ' . $path);
|
||||||
|
|
||||||
|
if(!$skip_purge && isset($config['purge']) && isset($_SERVER['HTTP_HOST'])) {
|
||||||
// Purge cache
|
// Purge cache
|
||||||
if(basename($path) == $config['file_index']) {
|
if(basename($path) == $config['file_index']) {
|
||||||
// Index file (/index.html); purge "/" as well
|
// Index file (/index.html); purge "/" as well
|
||||||
|
47
inc/remote.php
Normal file
47
inc/remote.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
class Remote {
|
||||||
|
public function __construct($config) {
|
||||||
|
foreach($config as $name => $value) {
|
||||||
|
$this->{$name} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$methods = Array();
|
||||||
|
|
||||||
|
if(!isset($this->auth['method']))
|
||||||
|
error('Unspecified authentication method.');
|
||||||
|
|
||||||
|
// Connect
|
||||||
|
$this->connection = ssh2_connect($this->host, isset($this->port) ? $this->port : 22, $methods);
|
||||||
|
|
||||||
|
switch($this->auth['method']) {
|
||||||
|
case 'plain':
|
||||||
|
if(!ssh2_auth_password($this->connection, $this->auth['username'], $this->auth['password']))
|
||||||
|
error('Plain-text authentication failed.');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
error('Unknown authentication method.');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function write($data, $remote_path) {
|
||||||
|
global $config;
|
||||||
|
|
||||||
|
switch($this->type) {
|
||||||
|
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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
?>
|
Loading…
Reference in New Issue
Block a user