forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxlib.php
913 lines (863 loc) · 29.2 KB
/
boxlib.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Box.net client.
*
* @package core
* @author James Levy <[email protected]>
* @link http://enabled.box.net
* @access public
* @version 1.0
* @copyright copyright Box.net 2007
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/oauthlib.php');
/**
* Box.net client class.
*
* @package core
* @copyright 2013 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class boxnet_client extends oauth2_client {
/** @const API URL */
const API = 'https://api.box.com/2.0';
/** @const UPLOAD_API URL */
const UPLOAD_API = 'https://upload.box.com/api/2.0';
/**
* Return authorize URL.
*
* @return string
*/
protected function auth_url() {
return 'https://www.box.com/api/oauth2/authorize';
}
/**
* Create a folder.
*
* @param string $foldername The folder name.
* @param int $parentid The ID of the parent folder.
* @return array Information about the new folder.
*/
public function create_folder($foldername, $parentid = 0) {
$params = array('name' => $foldername, 'parent' => array('id' => (string) $parentid));
$this->reset_state();
$result = $this->post($this->make_url("/folders"), json_encode($params));
$result = json_decode($result);
return $result;
}
/**
* Download the file.
*
* @param int $fileid File ID.
* @param string $path Path to download the file to.
* @return bool Success or not.
*/
public function download_file($fileid, $path) {
$this->reset_state();
$result = $this->download_one($this->make_url("/files/$fileid/content"), array(),
array('filepath' => $path, 'CURLOPT_FOLLOWLOCATION' => true));
return ($result === true && $this->info['http_code'] === 200);
}
/**
* Get info of a file.
*
* @param int $fileid File ID.
* @return object
*/
public function get_file_info($fileid) {
$this->reset_state();
$result = $this->request($this->make_url("/files/$fileid"));
return json_decode($result);
}
/**
* Get a folder content.
*
* @param int $folderid Folder ID.
* @return object
*/
public function get_folder_items($folderid = 0) {
$this->reset_state();
$result = $this->request($this->make_url("/folders/$folderid/items",
array('fields' => 'id,name,type,modified_at,size,owned_by')));
return json_decode($result);
}
/**
* Log out.
*
* @return void
*/
public function log_out() {
if ($accesstoken = $this->get_accesstoken()) {
$params = array(
'client_id' => $this->get_clientid(),
'client_secret' => $this->get_clientsecret(),
'token' => $accesstoken->token
);
$this->reset_state();
$this->post($this->revoke_url(), $params);
}
parent::log_out();
}
/**
* Build a request URL.
*
* @param string $uri The URI to request.
* @param array $params Query string parameters.
* @param bool $uploadapi Whether this works with the upload API or not.
* @return string
*/
protected function make_url($uri, $params = array(), $uploadapi = false) {
$api = $uploadapi ? self::UPLOAD_API : self::API;
$url = new moodle_url($api . '/' . ltrim($uri, '/'), $params);
return $url->out(false);
}
/**
* Rename a file.
*
* @param int $fileid The file ID.
* @param string $newname The new file name.
* @return object Box.net file object.
*/
public function rename_file($fileid, $newname) {
// This requires a PUT request with data within it. We cannot use
// the standard PUT request 'CURLOPT_PUT' because it expects a file.
$data = array('name' => $newname);
$options = array(
'CURLOPT_CUSTOMREQUEST' => 'PUT',
'CURLOPT_POSTFIELDS' => json_encode($data)
);
$url = $this->make_url("/files/$fileid");
$this->reset_state();
$result = $this->request($url, $options);
$result = json_decode($result);
return $result;
}
/**
* Resets curl for multiple requests.
*
* @return void
*/
public function reset_state() {
$this->cleanopt();
$this->resetHeader();
}
/**
* Return the revoke URL.
*
* @return string
*/
protected function revoke_url() {
return 'https://www.box.com/api/oauth2/revoke';
}
/**
* Share a file and return the link to it.
*
* @param string $fileid The file ID.
* @param bool $businesscheck Whether or not to check if the user can share files, has a business account.
* @return object
*/
public function share_file($fileid, $businesscheck = true) {
// Sharing the file, this requires a PUT request with data within it. We cannot use
// the standard PUT request 'CURLOPT_PUT' because it expects a file.
$data = array('shared_link' => array('access' => 'open', 'permissions' =>
array('can_download' => true, 'can_preview' => true)));
$options = array(
'CURLOPT_CUSTOMREQUEST' => 'PUT',
'CURLOPT_POSTFIELDS' => json_encode($data)
);
$this->reset_state();
$result = $this->request($this->make_url("/files/$fileid"), $options);
$result = json_decode($result);
if ($businesscheck) {
// Checks that the user has the right to share the file. If not, throw an exception.
$this->reset_state();
$this->head($result->shared_link->download_url);
$info = $this->get_info();
if ($info['http_code'] == 403) {
throw new moodle_exception('No permission to share the file');
}
}
return $result->shared_link;
}
/**
* Search.
*
* @return object
*/
public function search($query) {
$this->reset_state();
$result = $this->request($this->make_url('/search', array('query' => $query, 'limit' => 50, 'offset' => 0)));
return json_decode($result);
}
/**
* Return token URL.
*
* @return string
*/
protected function token_url() {
return 'https://www.box.com/api/oauth2/token';
}
/**
* Upload a file.
*
* Please note that the file is named on Box.net using the path we are providing, and so
* the file has the name of the stored_file hash.
*
* @param stored_file $storedfile A stored_file.
* @param integer $parentid The ID of the parent folder.
* @return object Box.net file object.
*/
public function upload_file(stored_file $storedfile, $parentid = 0) {
$url = $this->make_url('/files/content', array(), true);
$options = array(
'filename' => $storedfile,
'parent_id' => $parentid
);
$this->reset_state();
$result = $this->post($url, $options);
$result = json_decode($result);
return $result;
}
}
/**
* Box REST Client Library for PHP5 Developers.
*
* Deprecation note: As of the 14th of December 2013 Box.net APIv1, used by this class,
* is reaching its end of life. Please use boxnet_client() instead.
*
* @package core
* @author James Levy <[email protected]>
* @link http://enabled.box.net
* @access public
* @version 1.0
* @copyright copyright Box.net 2007
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @deprecated since 2.6, 2.5.3, 2.4.7
*/
class boxclient {
/** @var string */
public $auth_token = '';
/** @var string */
private $_box_api_url = 'https://www.box.com/api/1.0/rest';
private $_box_api_upload_url = 'http://upload.box.com/api/1.0/upload';
private $_box_api_download_url = 'http://www.box.com/api/1.0/download';
private $_box_api_auth_url = 'http://www.box.com/api/1.0/auth';
private $_error_code = '';
private $_error_msg = '';
/** @var bool */
private $debug = false;
/**
* @param string $api_key
* @param string $auth_token
* @param bool $debug
*/
public function __construct($api_key, $auth_token = '', $debug = false) {
$this->api_key = $api_key;
$this->auth_token = $auth_token;
if (!empty($debug)) {
$this->debug = true;
} else {
$this->debug = false;
}
}
/**
* Setup for Functions
*
* @param string $method
* @param array $params
* @return array
*/
function makeRequest($method, $params = array()) {
$this->_clearErrors();
$c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
try {
if ($method == 'upload'){
$request = $this->_box_api_upload_url.'/'.
$this->auth_token.'/'.$params['folder_id'];
$xml = $c->post($request, $params);
}else{
$args = array();
$xml = $c->get($this->_box_api_url, $params);
}
$xml_parser = xml_parser_create();
// set $data here
xml_parse_into_struct($xml_parser, $xml, $data);
xml_parser_free($xml_parser);
} catch (moodle_exception $e) {
$this->setError(0, 'connection time-out or invalid url');
return false;
}
return $data;
}
/**
* @param array $params
* @return array
*/
function getTicket($params = array()) {
$params['api_key'] = $this->api_key;
$params['action'] = 'get_ticket';
$ret_array = array();
$data = $this->makeRequest('action=get_ticket', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'STATUS':
$ret_array['status'] = $a['value'];
break;
case 'TICKET':
$ret_array['ticket'] = $a['value'];
break;
}
}
return $ret_array;
}
/**
* $options['username'] and $options['password'] must be
* given, we will use them to obtain a valid auth_token
* To get a token, you should use following code:
*
* <code>
* $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
* Get a ticket
* $t = $box->getTicket();
* $box->getAuthToken($t['ticket'], array(
* 'username'=>'[email protected]',
* 'password'=>'xxx'));
* </code>
*
* @param string $ticket
* @param string $username
* @param string $password
* @return mixed
*/
function getAuthToken($ticket, $username, $password) {
$c = new curl(array('debug'=>$this->debug));
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
$param = array(
'login_form1'=>'',
'login'=>$username,
'password'=>$password,
'dologin'=>1,
'__login'=>1
);
try {
$ret = $c->post($this->_box_api_auth_url.'/'.$ticket, $param);
} catch (moodle_exception $e) {
$this->setError(0, 'connection time-out or invalid url');
return false;
}
$header = $c->getResponse();
if(empty($header['location'])) {
throw new repository_exception('invalidpassword', 'repository_boxnet');
}
$location = $header['location'];
preg_match('#auth_token=(.*)$#i', $location, $matches);
$auth_token = $matches[1];
if(!empty($auth_token)) {
$this->auth_token = $auth_token;
return $auth_token;
} else {
throw new repository_exception('invalidtoken', 'repository_boxnet');
}
}
/**
* @param string $path Unused
* @param array $params
* @return array
*/
function getfiletree($path, $params = array()) {
$this->_clearErrors();
$params['auth_token'] = $this->auth_token;
$params['folder_id'] = 0;
$params['api_key'] = $this->api_key;
$params['action'] = 'get_account_tree';
$params['onelevel'] = 1;
$params['params[]'] = 'nozip';
$c = new curl(array('debug'=>$this->debug));
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
try {
$args = array();
$xml = $c->get($this->_box_api_url, $params);
} catch (Exception $e){
}
$ret = array();
$o = simplexml_load_string(trim($xml));
if($o->status == 'listing_ok') {
$tree = $o->tree->folder;
$this->buildtree($tree, $ret);
}
return $ret;
}
/**
* Get box.net file info
*
* @param string $fileid
* @param int $timeout request timeout in seconds
* @return stdClass|null
*/
function get_file_info($fileid, $timeout = 0) {
$this->_clearErrors();
$params = array();
$params['action'] = 'get_file_info';
$params['file_id'] = $fileid;
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$http = new curl(array('debug'=>$this->debug));
$xml = $http->get($this->_box_api_url, $params, array('timeout' => $timeout));
if (!$http->get_errno()) {
$o = simplexml_load_string(trim($xml));
if ($o->status == 's_get_file_info') {
return $o->info;
}
}
return null;
}
/**
* @param array $sax
* @param array $tree Passed by reference
*/
function buildtree($sax, &$tree){
$sax = (array)$sax;
$count = 0;
foreach($sax as $k=>$v){
if($k == 'folders'){
$o = $sax[$k];
foreach($o->folder as $z){
$tmp = array('title'=>(string)$z->attributes()->name,
'size'=>0, 'date'=>userdate(time()),
'thumbnail'=>'https://www.box.com/img/small_folder_icon.gif',
'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
$tmp['children'] = array();
$this->buildtree($z, $tmp['children']);
$tree[] = $tmp;
}
} elseif ($k == 'files') {
$val = $sax[$k]->file;
foreach($val as $file){
$thumbnail = (string)$file->attributes()->thumbnail;
if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
$thumbnail = 'http://www.box.com'.$thumbnail;
}
$tmp = array('title'=>(string)$file->attributes()->file_name,
'size'=>display_size((int)$file->attributes()->size),
'thumbnail'=>$thumbnail,
'date'=>userdate((int)$file->attributes()->updated),
'source'=> $this->_box_api_download_url .'/'
.$this->auth_token.'/'.(string)$file->attributes()->id,
'url'=>(string)$file->attributes()->shared_link);
$tree[] = $tmp;
}
}
$count++;
}
}
/**
* @param array $params
* @return bool|array Array or false
*/
function getAccountTree($params = array()) {
$params['auth_token'] = $this->auth_token;
$params['folder_id'] = 0;
$params['api_key'] = $this->api_key;
$params['action'] = 'get_account_tree';
$params['onelevel'] = 1;
$params['params[]'] = 'nozip';
$ret_array = array();
$data = $this->makeRequest('action=get_account_tree', $params);
if ($this->_checkForError($data)) {
return false;
}
$tree_count=count($data);
$entry_count = 0;
for ($i=0; $i<$tree_count; $i++) {
$a = $data[$i];
switch ($a['tag'])
{
case 'FOLDER':
if (@is_array($a['attributes'])) {
$ret_array['folder_id'][$i] = $a['attributes']['ID'];
$ret_array['folder_name'][$i] = $a['attributes']['NAME'];
$ret_array['shared'][$i] = $a['attributes']['SHARED'];
}
break;
case 'FILE':
if (@is_array($a['attributes'])) {
$ret_array['file_id'][$i] = $a['attributes']['ID'];
@$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
@$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
@$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
@$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
@$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
} else {
@$ret_array['thumbnail'][$i] = 'http://www.box.com'.$a['attributes']['THUMBNAIL'];
}
$entry_count++;
}
break;
}
}
return $ret_array;
}
/**
* @param string $new_folder_name
* @param array $params
* @return bool|array Array or false
*/
function CreateFolder($new_folder_name, $params = array()) {
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$params['action'] = 'create_folder';
$params['name'] = $new_folder_name;
$defaults = array(
'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
);
foreach ($defaults as $key => $value) {
if (!array_key_exists($key, $params)) {
$params[$key] = $value;
}
}
$ret_array = array();
$data = $this->makeRequest('action=create_folder', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
if (!empty($a['value'])) {
switch ($a['tag']) {
case 'FOLDER_ID':
$ret_array['folder_id'] = $a['value'];
break;
case 'FOLDER_NAME':
$ret_array['folder_name'] = $a['value'];
break;
case 'FOLDER_TYPE_ID':
$ret_array['folder_type_id'] = $a['value'];
break;
case 'SHARED':
$ret_array['shared'] = $a['value'];
break;
case 'PASSWORD':
$ret_array['password'] = $a['value'];
break;
}
} else {
$ret_array[strtolower($a['tag'])] = null;
}
}
return $ret_array;
}
/**
* Upload a File
* @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
* @return array|bool Array or false
*/
function UploadFile ($params = array()) {
$params['auth_token'] = $this->auth_token;
// this param should be the full path of the file
$params['new_file1'] = $params['file'];
unset($params['file']);
$defaults = array(
'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
);
foreach ($defaults as $key => $value) {
if (!array_key_exists($key, $params)) {
$params[$key] = $value;
}
}
$ret_array = array();
$entry_count = 0;
$data = $this->makeRequest('upload', $params);
if ($this->_checkForError($data)) {
return false;
}
for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
$a = $data[$i];
switch ($a['tag']) {
case 'STATUS':
$ret_array['status'] = $a['value'];
break;
case 'FILE':
if (is_array($a['attributes'])) {
@$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
@$ret_array['id'][$i] = $a['attributes']['ID'];
@$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
@$ret_array['error'][$i] = $a['attributes']['ERROR'];
@$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
$entry_count++;
}
break;
}
}
return $ret_array;
}
/**
* @param string $fileid
* @param string $newname
* @return bool
*/
function RenameFile($fileid, $newname) {
$params = array(
'api_key' => $this->api_key,
'auth_token' => $this->auth_token,
'action' => 'rename',
'target' => 'file',
'target_id' => $fileid,
'new_name' => $newname,
);
$data = $this->makeRequest('action=rename', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'STATUS':
if ($a['value'] == 's_rename_node') {
return true;
}
}
}
return false;
}
/**
* Register New User
*
* @param array $params
* @return array|bool Outcome Array or false
*/
function RegisterUser($params = array()) {
$params['api_key'] = $this->api_key;
$params['action'] = 'register_new_user';
$params['login'] = $_REQUEST['login'];
$params['password'] = $_REQUEST['password'];
$ret_array = array();
$data = $this->makeRequest('action=register_new_user', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'STATUS':
$ret_array['status'] = $a['value'];
break;
case 'AUTH_TOKEN':
$ret_array['auth_token'] = $a['value'];
break;
case 'LOGIN':
$ret_array['login'] = $a['value'];
break;
case 'SPACE_AMOUNT':
$ret_array['space_amount'] = $a['value'];
break;
case 'SPACE_USED':
$ret_array['space_used'] = $a['value'];
break;
}
}
return $ret_array;
}
/**
* Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
*
* @param string $tag
* @param string $id Set to ID of file or folder
* @param string $target_type File or folder
* @param array $params
* @return array|bool Outcome Array or false
*/
function AddTag($tag, $id, $target_type, $params = array()) {
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$params['action'] = 'add_to_tag';
$params['target'] = $target_type; // File or folder
$params['target_id'] = $id; // Set to ID of file or folder
$params['tags[]'] = $tag;
$ret_array = array();
$data = $this->makeRequest('action=add_to_tag', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'STATUS':
$ret_array['status'] = $a['value'];
break;
}
}
return $ret_array;
}
/**
* Public Share (http://enabled.box.net/docs/rest#public_share)
*
* @param string $message
* @param string $emails
* @param string $id Set to ID of file or folder
* @param string $target_type File or folder
* @param string $password
* @param array $params
* @return array|bool Outcome Array or false
*/
function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$params['action'] = 'public_share';
$params['target'] = $target_type;
$params['target_id'] = $id;
$params['password'] = $password;
$params['message'] = $message;
$params['emails'] = $emails;
$ret_array = array();
$data = $this->makeRequest('action=public_share', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'STATUS':
$ret_array['status'] = $a['value'];
break;
case 'PUBLIC_NAME':
$ret_array['public_name'] = $a['value'];
break;
}
}
return $ret_array;
}
/**
* Get Friends (http://enabled.box.net/docs/rest#get_friends)
*
* @param array $params
* @return array|bool Outcome Array or false
*/
function GetFriends ($params = array()) {
$params['auth_token'] = $this->auth_token;
$params['action'] = 'get_friends';
$params['api_key'] = $this->api_key;
$params['params[]'] = 'nozip';
$ret_array = array();
$data = $this->makeRequest('action=get_friends', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'NAME':
$ret_array['name'] = $a['value'];
break;
case 'EMAIL':
$ret_array['email'] = $a['value'];
break;
case 'ACCEPTED':
$ret_array['accepted'] = $a['value'];
break;
case 'AVATAR_URL':
$ret_array['avatar_url'] = $a['value'];
break;
case 'ID':
$ret_array['id'] = $a['value'];
break;
case 'URL':
$ret_array['url'] = $a['value'];
break;
case 'STATUS':
$ret_array['status'] = $a['value'];
break;
}
}
return $ret_array;
}
/**
* Logout User (http://enabled.box.net/docs/rest#get_friends)
*
* @param array $params
* @return array|bool Outcome Array or false
*/
function Logout($params = array()) {
$params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
$params['action'] = 'logout';
$ret_array = array();
$data = $this->makeRequest('action=logout', $params);
if ($this->_checkForError($data)) {
return false;
}
foreach ($data as $a) {
switch ($a['tag']) {
case 'ACTION':
$ret_array['logout'] = $a['value'];
break;
}
return $ret_array;
}
}
/**
* @param array $data
* @return bool
*/
function _checkForError($data) {
if ($this->_error_msg != '') {
return true;
}
if (@$data[0]['attributes']['STAT'] == 'fail') {
$this->_error_code = $data[1]['attributes']['CODE'];
$this->_error_msg = $data[1]['attributes']['MSG'];
return true;
}
return false;
}
/**
* @return bool
*/
public function isError() {
if ($this->_error_msg != '') {
return true;
}
return false;
}
/**
*
*/
public function setError($code = 0, $msg){
$this->_error_code = $code;
$this->_error_msg = $msg;
}
/**
* @return string
*/
function getErrorMsg() {
return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
}
/**
* @return string
*/
function getErrorCode() {
return $this->_error_code;
}
/**
*
*/
function _clearErrors() {
$this->_error_code = '';
$this->_error_msg = '';
}
}