PluginProbe
ManageWP Worker / 3.9.17
ManageWP Worker v3.9.17
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / lib / dropbox.php

dropbox.php in ManageWP Worker 3.9.17, at lib/dropbox.php

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