My favorites
|
Sign in
boxnetsuit
Box.net utilities
Project Home
Downloads
Wiki
Issues
Source
Checkout
|
Browse
|
Changes
|
r2
Source path:
svn
/
trunk
/
uploader.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
<?php
/**
* Box.net Uploader
*
* @version 1.0
* @author martin maly <author@php-suit.com>
* @copyright (C) 2008 martin maly
*/
/**
* Prerequisities:
* 1. Register as developer at enabled.box.net
* 2. Register your application and obtain the API key
* 3. Copy your api key at the appropriate place below
* 4. Prepare regular box.net account for data storage
*
* Usage:
* $b=new boxnetsuit();
$b->authorize('box.net user name', 'box.net password');
$b->upload('./anyfile.zip');
*/
class boxnetsuit {
private $return;
private $status;
private $token;
private $auth;
//YOUR API KEY GOES HERE!
const apikey='2r96gpqhg6f0ukqbcmqetldzfjaut30s';
function __construct(){
$this->auth = false;
}
private function _send($action, $getdata=array()){
$get = http_build_query($getdata);
$host='ssl://www.box.net';
$path = '/api/1.0/rest?api_key='.self::apikey.'&action='.$action;
if ($this->auth) $path .= '&auth_token='.$this->token;
if ($get) $path.='&'.$get;
$fp = fsockopen($host, 443);
// then just
fputs($fp, "GET $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Connection: close\r\n");
fputs($fp, "\r\n");
$return='';
while (!feof($fp)) {
$return .= fgets($fp, 128);
}
fclose($fp);
$p=strpos($return,"\r\n\r\n");
$this->return = trim(substr($return,$p));
if (!$return) return false;
if (stripos($return, '200 OK')) return true;
return false;
}
/**
* Get authentification token from box.net site (i.e. "login")
* @param string $user
* @param string $password
* @return boolean
*/
public function authorize($user, $password){
if ($this->_send('authorization',array('login'=>$user,'password'=>$password,'method'=>'1'))){
$s=$this->return;
$status = '';
if (preg_match('/\<status\>(.*)\<\/status\>/is', $s, $arr)){
$status = $arr[1];
}
$token = '';
if (preg_match('/\<auth\_token\>(.*)\<\/auth\_token\>/is', $s, $arr)){
$token = $arr[1];
}
$this->status = $status;
$this->token = $token;
$this->auth = ($status == 'logged')?true:false;
return true;
} else {
return false;
}
}
/**
* just for fun
*/
function gettree(){
if (!$this->auth) return false;
$this->_send('get_account_tree',array('folder_id'=>0,'params[]'=>'nozip'));
echo htmlspecialchars(nl2br(print_r($this->return,true)));
}
/**
* Main backup uploader
*/
function upload($file, $remote='', $folder_id = 0){
if (!file_exists($file)) return false;
if (!$this->auth) return false;
if (!$remote)
$remote_filename = 'backup' . date("Y-m-d-H-i-s") . '.tgz';
else
$remote_filename = $remote;
$host = '/api/1.0/upload/'.$this->token.'/'.$folder_id;
srand((double)microtime()*1000000);
$boundary='---------------------------'.substr(md5(rand(0,32000)),0,10);
$header = "POST $host HTTP/1.1\r\n";
$header .= "Host: upload.box.net\r\n";
$header .= "Content-type: multipart/form-data, boundary=$boundary\r\n";
$data = '';
$data .="--$boundary\r\n";
$data .= "Content-Disposition: form-data; name=\"share\"\r\n";
$data .= "\r\n".'1'."\r\n";
$data .="--$boundary\r\n";
$data .="--$boundary\r\n";
$data .= "Content-Disposition: form-data; name=\"file\"; filename=\"$remote_filename\"\r\n";
$data .= "Content-Type: application/octet-stream;\r\n";
$data .= "\r\n".(file_get_contents($file))."\r\n";
$data .="--$boundary--\r\n";
$header .= "Content-length: " . strlen($data) . "\r\n\r\n";
$fp = fsockopen('upload.box.net', 80);
// then just
fputs($fp, $header.$data);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
echo htmlspecialchars(nl2br(print_r($this->return,true)));
}
}
?>
Show details
Hide details
Change log
r2
by martin.maly on Jul 27, 2008
Diff
FIRST
Go to:
/trunk/uploader.php
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 3831 bytes, 148 lines
View raw file
File properties
svn:mime-type
text/x-php
svn:keywords
Id Rev Date
Hosted by