Browse Source

remote servers -- writing to another server via SSH

pull/40/head
Savetheinternet 12 years ago
parent
commit
36ee32b38b
  1. 3
      inc/config.php
  2. 48
      inc/functions.php
  3. 47
      inc/remote.php

3
inc/config.php

@ -30,7 +30,8 @@
'wordfilters' => Array(),
'custom_capcode' => Array(),
'custom_tripcode' => Array(),
'dnsbl' => Array()
'dnsbl' => Array(),
'remote' => Array()
);
// Database stuff

48
inc/functions.php

@ -250,38 +250,46 @@
}
}
function file_write($path, $data) {
function file_write($path, $data, $simple = false, $skip_purge = false) {
global $config;
if(preg_match('/^scp:\/\/(.+)$/', $path, $m)) {
// Experimental: secure copy...
$file = tempnam($config['tmp'], 'tinyboard-scp');
// Write to temp file
file_write($file, $data);
// Call `scp` (yes, this is horrible)
$command = 'scp ' . escapeshellarg($file) . ' ' . escapeshellarg($m[1]);
system($command);
// Delete temporary file
file_unlink($file);
return;
if(preg_match('/^remote:\/\/(.+)\:(.+)$/', $path, $m)) {
if(isset($config['remote'][$m[1]])) {
require_once 'inc/remote.php';
$remote = new Remote($config['remote'][$m[1]]);
$remote->write($data, $m[2]);
return;
} else {
error('Invalid remote server: ' . $m[1]);
}
}
if(!$fp = fopen($path, 'c'))
if(!$fp = fopen($path, $simple ? 'w' : 'c'))
error('Unable to open file for writing: ' . $path);
// File locking
if(!flock($fp, LOCK_EX)) {
if(!$simple && !flock($fp, LOCK_EX)) {
error('Unable to lock file: ' . $path);
}
// Truncate file
ftruncate($fp, 0);
if(!$simple && !ftruncate($fp, 0))
error('Unable to truncate file: ' . $path);
// Write data
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
if(fwrite($fp, $data) === false)
error('Unable to write to file: ' . $path);
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
if(basename($path) == $config['file_index']) {
// Index file (/index.html); purge "/" as well

47
inc/remote.php

@ -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…
Cancel
Save