Дек 282011
 

Примерно так я делаю бекапы :)
Может быть кому-нибудь пригодится.

<?php
unlink('/tmp/dropbox_cookie');
$dropbox = Dropbox::forge();
try {
    $dropbox->login('[email protected]', 'password');
    $dropbox->upload('filename.tar', '/folder');
}
catch (Exception $e) {
    die("Error: " . $e->getMessage());
}
class Dropbox
{
    public static function forge()
    {
        return new Dropbox();
    }
 
    public function login($mail, $pass)
    {
        $page  = curl('https://www.dropbox.com/login');
        $token = pm($page, '/name="t" value="([a-z0-9]*?)"/');
        if (!$token)
            throw new Exception('no token');
        $page = curl('https://www.dropbox.com/login', true, array(
            'login_email' => $mail,
            'login_password' => $pass,
            't' => $token
        ));
        if (!substr_count($page, '/home'))
            throw new Exception('invalid login/password');
    }
 
    public function upload($filename, $dir)
    {
        $page  = curl('https://www.dropbox.com/home');
        $page  = explode('https://dl-web.dropbox.com/upload', $page);
        $token = pm($page[1], '/name="t" value="([a-z0-9]*?)"/');
        if (!$token)
            throw new Exception('no token');
        $page = curl('https://dl-web.dropbox.com/upload', true, array(
            'plain' => 'yes',
            'dest' => $dir,
            't' => $token,
            'file' => '@' . $filename
        ));
        if (!substr_count($page, '302 FOUND'))
            throw new Exception('cant upload file');
    }
}
 
function curl($url, $headers = false, $post = false)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, $headers);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/dropbox_cookie');
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/dropbox_cookie');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1');
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
 
function pm($haystack, $regexp)
{
    return preg_match($regexp, $haystack, $out) ? $out[1] : false;
}
?>