This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDropboxUploader.php
167 lines (138 loc) · 6.13 KB
/
DropboxUploader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
// https://github.com/hakre/DropboxUploader
namespace Kachkaev\DropboxBackupBundle;
use \Exception;
/**
* Dropbox Uploader
*
* Copyright (c) 2009 Jaka Jancar
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Jaka Jancar [jaka@kubje.org] [http://jaka.kubje.org/]
* @version 1.1.5
*/
class DropboxUploader {
protected $email;
protected $password;
protected $caCertSourceType = self::CACERT_SOURCE_SYSTEM;
const CACERT_SOURCE_SYSTEM = 0;
const CACERT_SOURCE_FILE = 1;
const CACERT_SOURCE_DIR = 2;
protected $caCertSource;
protected $loggedIn = false;
protected $cookies = array();
/**
* Constructor
*
* @param string $email
* @param string|null $password
*/
public function __construct($email, $password) {
// Check requirements
if (!extension_loaded('curl'))
throw new Exception('DropboxUploader requires the cURL extension.');
$this->email = $email;
$this->password = $password;
}
public function setCaCertificateFile($file)
{
$this->caCertSourceType = self::CACERT_SOURCE_FILE;
$this->caCertSource = $file;
}
public function setCaCertificateDir($dir)
{
$this->caCertSourceType = self::CACERT_SOURCE_DIR;
$this->caCertSource = $dir;
}
public function upload($source, $remoteDir='/', $remoteName=null) {
if (!file_exists($source) or !is_file($source) or !is_readable($source))
throw new Exception("File '$source' does not exist or is not readable.");
if (!is_string($remoteDir))
throw new Exception("Remote directory must be a string, is ".gettype($remoteDir)." instead.");
if (is_null($remoteName)) {
$remoteName = $source;
} else if (!is_string($remoteName)) {
throw new Exception("Remote filename must be a string, is ".gettype($remoteDir)." instead.");
}
if (!$this->loggedIn)
$this->login();
$data = $this->request('https://www.dropbox.com/home');
$token = $this->extractToken($data, 'https://dl-web.dropbox.com/upload');
$postdata = array('plain'=>'yes', 'file'=>'@'.$source.';filename='.$remoteName, 'dest'=>$remoteDir, 't'=>$token);
$data = $this->request('https://dl-web.dropbox.com/upload', true, $postdata);
if (strpos($data, 'HTTP/1.1 302 FOUND') === false)
throw new Exception('Upload failed!');
}
protected function login() {
$data = $this->request('https://www.dropbox.com/login');
$token = $this->extractTokenFromLoginForm($data);
$postdata = array('login_email'=>$this->email, 'login_password'=>$this->password, 't'=>$token);
$data = $this->request('https://www.dropbox.com/login', true, $postdata);
if (stripos($data, 'location: /home') === false)
throw new Exception('Login unsuccessful.');
$this->loggedIn = true;
}
protected function request($url, $post=false, $postData=array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
switch ($this->caCertSourceType) {
case self::CACERT_SOURCE_FILE:
curl_setopt($ch, CURLOPT_CAINFO, $this->caCertSource);
break;
case self::CACERT_SOURCE_DIR:
curl_setopt($ch, CURLOPT_CAPATH, $this->caCertSource);
break;
}
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post) {
curl_setopt($ch, CURLOPT_POST, $post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
// Send cookies
$rawCookies = array();
foreach ($this->cookies as $k=>$v)
$rawCookies[] = "$k=$v";
$rawCookies = implode(';', $rawCookies);
curl_setopt($ch, CURLOPT_COOKIE, $rawCookies);
$data = curl_exec($ch);
if ($data === false)
throw new Exception('Cannot execute request: '.curl_error($ch));
// Store received cookies
preg_match_all('/Set-Cookie: ([^=]+)=(.*?);/i', $data, $matches, PREG_SET_ORDER);
foreach ($matches as $match)
$this->cookies[$match[1]] = $match[2];
curl_close($ch);
return $data;
}
protected function extractTokenFromLoginForm($html) {
// <input type="hidden" name="t" value="UJygzfv9DLLCS-is7cLwgG7z" />
if (!preg_match('#<input type="hidden" name="t" value="([A-Za-z0-9_-]+)" />#', $html, $matches))
throw new Exception('Cannot extract login CSRF token.');
return $matches[1];
}
protected function extractToken($html, $formAction) {
if (!preg_match('/<form [^>]*'.preg_quote($formAction, '/').'[^>]*>.*?(<input [^>]*name="t" [^>]*value="(.*?)"[^>]*>).*?<\/form>/is', $html, $matches) || !isset($matches[2]))
throw new Exception("Cannot extract token! (form action=$formAction)");
return $matches[2];
}
}