forked from AriTheElk/BTSync-PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtsync.php
377 lines (331 loc) · 9.49 KB
/
btsync.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* BTSync - PHP Wrapper for the BitTorrent Sync API
* @author Garet McKinley <[email protected]>
*/
class BTSync {
/**
* IP address to access HTTP API
* @var string
*/
public $ip;
/**
* Port to access HTTP API
* @var integer
*/
public $port;
/**
* IP:Port for access HTTP API
* @var string
*/
private $baseURL;
/**
* Initialize the class
*
* @param string $ip (Optional) IP address to access HTTP API (default is 127.0.0.1)
* @param string $port (Optional) Port to access HTTP API (default is 8888)
*/
public function __construct($ip = '127.0.0.1', $port = '8888', $username = 'api', $password = 'secret')
{
$this->ip = $ip;
$this->port = $port;
$this->username = $username;
$this->password = $password;
if ($this->username == 'api')
$this->baseURL = sprintf('http://%s:%s/api', $this->ip, $this->port);
else
$this->baseURL = sprintf('http://%s:%s@%s:%s/api', $this->username, $this->password, $this->ip, $this->port);
}
/**
* Makes an API call and returns the result
*
* @param string $params Parameters for the API call
*
* @return string API Result
*/
private function request($params) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, sprintf('%s?%s', $this->baseURL, $params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Returns an array with folders info. If a secret is specified,
* will return info about the folder with this secret.
*
* @param string $secret (Optional) If a secret is specified, will return info about the folder with this secret
*
* @return array Result
*/
public function getFolders($secret = null)
{
if (!is_null($secret))
$result = $this->request(sprintf('method=get_folders&secret=%s', $secret));
else
$result = $this->request('method=get_folders');
$array = json_decode($result);
if (empty($array))
return false;
return $array;
}
/**
* Adds a folder to Sync. If a secret is not specified, it will
* be generated automatically. The folder will have to pre-exist
* on the disk and Sync will add it into a list of syncing folders
*
* @param string $dir (Required) Path to the sync folder
* @param string $secret (Optional) Folder secret
* @param boolean $selective (Optional)
*
* @return object Result
*/
public function addFolder($dir, $secret = null, $selective = false)
{
if (!is_null($secret))
$result = $this->request(sprintf('method=add_folder&dir=%s&secret=%s&selective_sync=%s', $dir, $secret, intval($selective)));
else
$result = $this->request(sprintf('method=add_folder&dir=%s&selective_sync=%s', $dir, intval($selective)));
$array = json_decode($result);
print_r(gettype($array));
return $array;
}
/**
* Removes folder from Sync while leaving actual folder and files
* on disk. It will remove a folder from the Sync list of folders
* and does not touch any files or folders on disk.
*
* @param string $secret (Required) Folder secret
*
* @return object Result
*/
public function removeFolder($secret)
{
$result = $this->request(sprintf('method=remove_folder&secret=%s', $secret));
$array = json_decode($result);
return $array;
}
/**
* Returns list of files within the specified directory. If a
* directory is not specified, will return list of files and
* folders within the root folder.
*
* @param string $secret (Required) Folder secret
* @param string $path (Optional) Path to a subfolder inside of the sync folder
*
* @return array Result
*/
public function getFiles($secret, $path = null)
{
if (!is_null($path))
$result = $this->request(sprintf('method=get_files&secret=%s&path=%s', $secret, $path));
else
$result = $this->request(sprintf('method=get_files&secret=%s', $secret));
$array = json_decode($result);
return $array;
}
/**
* Selects file for download for selective sync folders.
* Returns file information with applied preferences.
*
* @param string $secret (Required) Folder secret
* @param string $path (Required) Path to a subfolder inside of the sync folder
* @param boolean $download (Required) Specify if the file should be downloaded
*
* @return array Result
*/
public function setFilePrefs($secret, $path, $download)
{
$result = $this->request(sprintf('method=set_file_prefs&secret=%s&path=%s&download=%s', $secret, $path, intval($download)));
$array = json_decode($result);
return $array;
}
/**
* Returns list of peers connected to the specified folder.
*
* @param string $secret (Required) Folder secret
*
* @return array Result
*/
public function getFolderPeers($secret)
{
$result = $this->request(sprintf('method=get_folder_peers&secret=%s', $secret));
$array = json_decode($result);
return $array;
}
/**
* Generates read-write, read-only and encryption read-only secrets.
* If ‘secret’ parameter is specified, will return secrets available
* for sharing under this secret.
*
* @param string $secret (Required) Folder secret
* @param string $secret (Optional) If type=encrypted, generate secret with support of encrypted peer
*
* @return object Secrets
*/
public function getSecrets($secret, $type = null)
{
if ($type == 'encrypted')
$result = $this->request(sprintf('method=get_secrets&secret=%s&type=%s', $secret, $type));
else
$result = $this->request(sprintf('method=get_secrets&secret=%s', $secret));
$array = json_decode($result);
return $array;
}
/**
* Returns preferences for the specified sync folder.
*
* @param string $secret (Required) Folder secret
*
* @return object Returns current settings.
*/
public function getFolderPrefs($secret)
{
$result = $this->request(sprintf('method=get_folder_prefs&secret=%s', $secret));
$array = json_decode($result);
return $array;
}
/**
* Returns preferences for the specified sync folder.
*
* @param string $secret (Required) Folder secret
* @param array $params (Required) { use_dht, use_hosts, search_lan, use_relay_server, use_tracker, use_sync_trash }
*
* @return object Returns current settings.
*/
public function setFolderPrefs($secret, $params)
{
foreach ($params as $pref => $value) {
$prefs .= sprintf('%s=%s', $pref, intval($value));
if ($value !== end($params))
$prefs .= '&';
}
$result = $this->request(sprintf('method=set_folder_prefs&secret=%s&%s', $secret, $prefs));
$array = json_decode($result);
return $array;
}
/**
* Returns list of predefined hosts for the folder,
* or error code if a secret is not specified.
*
* @param string $secret (Required) Folder secret
*
* @return object Hosts
*/
public function getFolderHosts($secret)
{
$result = $this->request(sprintf('method=get_folder_hosts&secret=%s', $secret));
$array = json_decode($result);
return $array;
}
/**
* Sets one or several predefined hosts for the specified
* sync folder. Existing list of hosts will be replaced.
* Hosts should be added as values of the ‘host’ parameter
* and separated by commas. Returns current hosts if set
* successfully, error code otherwise.
*
* @param string $secret (Required) Folder secret
* @param array $hosts (Required) List of hosts. Host should be represented as “[address]:[port]”
*
* @return object Hosts
*/
public function setFolderHosts($secret, $hosts)
{
foreach ($hosts as $index => $host) {
$hostString .= $host;
if ($host !== end($hosts))
$hostString .= ',';
}
$result = $this->request(sprintf('method=set_folder_hosts&secret=%s&hosts=%s', $secret, $hostString));
$array = json_decode($result);
return $array;
}
/**
* Returns BitTorrent Sync preferences. Contains dictionary
* with advanced preferences. Please see Sync user guide for
* description of each option.
*
* @return object Current settings
*/
public function getPrefs()
{
$result = $this->request('method=get_prefs');
$array = json_decode($result);
return $array;
}
/**
* Sets BitTorrent Sync preferences. Parameters are the same
* as in ‘Get preferences’. Advanced preferences are set as
* general settings. Returns current settings.
*
* @param array (Required) { device_name, download_limit, lang, listening_port, upload_limit, use_upnp }
*
* @return object Current settings
*/
public function setPrefs($params)
{
foreach ($params as $pref => $value) {
$prefs .= sprintf('%s=%s', $pref, intval($value));
if ($value !== end($params))
$prefs .= '&';
}
return $prefs;
$result = $this->request(sprintf('method=set_prefs&%s', $prefs));
$array = json_decode($result);
return $array;
}
/**
* Gets the OS name where BitTorrent Sync is running.
*
* @return object OS
*/
public function getOS()
{
$result = $this->request('method=get_os');
$array = json_decode($result);
return $array;
}
/**
* Gets the BitTorrent Sync version.
*
* @return object Version
*/
public function getVersion()
{
$result = $this->request('method=get_version');
$array = json_decode($result);
return $array;
}
/**
* Gets the current upload and download speed.
*
* @return object Version
*/
public function getSpeed()
{
$result = $this->request('method=get_speed');
$array = json_decode($result);
return $array;
}
/**
* Gracefully stops Sync.
*
* @return object Result
*/
public function shutdown()
{
$result = $this->request('method=shutdown');
$array = json_decode($result);
return $array;
}
/**
* Destroys the class
*/
function __destruct() {
}
}
?>