mtWeb > Download > Remote SQL

#!/usr/local/bin/php -q
<?php
    
// substitute the correct path to PHP in the line above
    // -q suppresses HTTP headers produced by the PHP interpreter

    
require_once 'salt.php';

    
// all files in this directory ending with sql (case sensitive)
    // will be sent to the remote host for execution
    
$sqldir = "/home/foobar/sql";
    
$host = "localhost";
    
$sql = "# a random SQL comment ;-) just to make sure, yes do not use this one\n \n";

    if (
$dir = @opendir($sqldir)) {
         while ((
$file = readdir($dir)) !== false) {
             if (
$file != '.' and $file != '..' and substr($file, -3, strlen($file)) == 'sql') {

                echo
"Opening $file\n";
                
$filename = "$sqldir/$file";
                
$fp = fopen($filename, 'r');
                
$sql .= fread($fp, filesize($filename));
                
fclose($fp);

            }
        }

    
closedir($dir);
    }

    
post($host, $sql);


    
/////////////////////////////////////////////////////////////////////////////////////////////////////////


    
function post($host, &$sql) {
        
$hash = md5($sql . $GLOBALS['salt']);
        
$size = strlen($sql);
        
$data = serialize(array($hash, $size, base64_encode($sql)));

        
$post = "data=" . urlencode($data);
        echo
"SQL size: $size\n";
        echo
"Post size: " . strlen($post) . "\n";

        
$headers = "POST /remote-sql/serv.php HTTP/1.0\r\n";
        
$headers .= "Host: $host\r\n";
        
$headers .= "Content-type: application/x-www-form-urlencoded\r\n";
        
$headers .= "Content-length: " . strlen($post) . "\r\n";

        echo
"Connecting to $host\n\n";
        
$fp = fsockopen($host, 80, $errno, $errstr, 30);

        if (!
$fp) {
            echo
"Error: $errstr ($errno)\n";
        } else {
            echo
"Sending headers\n";
            
fputs($fp, "$headers\r\n");

            echo
"Posting data\n";
            
fputs($fp, $post);

            echo
"Awaiting response\n";
            while (!
feof($fp)) {
                echo
fgets ($fp,128);
            }
            
fclose ($fp);
        }
    }


?>