2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Box REST Client Library for PHP5 Developers
22 * @author James Levy <james@box.net>
23 * @link http://enabled.box.net
26 * @copyright copyright Box.net 2007
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 * @copyright copyright Box.net 2007
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 public $auth_token = '';
39 private $_box_api_url = 'https://www.box.com/api/1.0/rest';
40 private $_box_api_upload_url = 'http://upload.box.com/api/1.0/upload';
41 private $_box_api_download_url = 'http://www.box.com/api/1.0/download';
42 private $_box_api_auth_url = 'http://www.box.com/api/1.0/auth';
43 private $_error_code = '';
44 private $_error_msg = '';
46 private $debug = false;
49 * @param string $api_key
50 * @param string $auth_token
53 public function __construct($api_key, $auth_token = '', $debug = false) {
54 $this->api_key
= $api_key;
55 $this->auth_token
= $auth_token;
65 * @param string $method
66 * @param array $params
69 function makeRequest($method, $params = array()) {
70 $this->_clearErrors();
71 $c = new curl(array('debug'=>$this->debug
, 'cache'=>true, 'module_cache'=>'repository'));
72 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
74 if ($method == 'upload'){
75 $request = $this->_box_api_upload_url
.'/'.
76 $this->auth_token
.'/'.$params['folder_id'];
77 $xml = $c->post($request, $params);
80 $xml = $c->get($this->_box_api_url
, $params);
82 $xml_parser = xml_parser_create();
84 xml_parse_into_struct($xml_parser, $xml, $data);
85 xml_parser_free($xml_parser);
86 } catch (moodle_exception
$e) {
87 $this->setError(0, 'connection time-out or invalid url');
93 * @param array $params
96 function getTicket($params = array()) {
97 $params['api_key'] = $this->api_key
;
98 $params['action'] = 'get_ticket';
100 $data = $this->makeRequest('action=get_ticket', $params);
101 if ($this->_checkForError($data)) {
104 foreach ($data as $a) {
107 $ret_array['status'] = $a['value'];
110 $ret_array['ticket'] = $a['value'];
118 * $options['username'] and $options['password'] must be
119 * given, we will use them to obtain a valid auth_token
120 * To get a token, you should use following code:
123 * $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
125 * $t = $box->getTicket();
126 * $box->getAuthToken($t['ticket'], array(
127 * 'username'=>'dongsheng@moodle.com',
128 * 'password'=>'xxx'));
131 * @param string $ticket
132 * @param string $username
133 * @param string $password
136 function getAuthToken($ticket, $username, $password) {
137 $c = new curl(array('debug'=>$this->debug
));
138 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
142 'password'=>$password,
147 $ret = $c->post($this->_box_api_auth_url
.'/'.$ticket, $param);
148 } catch (moodle_exception
$e) {
149 $this->setError(0, 'connection time-out or invalid url');
152 $header = $c->getResponse();
153 if(empty($header['location'])) {
154 throw new repository_exception('invalidpassword', 'repository_boxnet');
156 $location = $header['location'];
157 preg_match('#auth_token=(.*)$#i', $location, $matches);
158 $auth_token = $matches[1];
159 if(!empty($auth_token)) {
160 $this->auth_token
= $auth_token;
163 throw new repository_exception('invalidtoken', 'repository_boxnet');
167 * @param string $path Unused
168 * @param array $params
171 function getfiletree($path, $params = array()) {
172 $this->_clearErrors();
173 $params['auth_token'] = $this->auth_token
;
174 $params['folder_id'] = 0;
175 $params['api_key'] = $this->api_key
;
176 $params['action'] = 'get_account_tree';
177 $params['onelevel'] = 1;
178 $params['params[]'] = 'nozip';
179 $c = new curl(array('debug'=>$this->debug
));
180 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
183 $xml = $c->get($this->_box_api_url
, $params);
184 } catch (Exception
$e){
187 $o = simplexml_load_string(trim($xml));
188 if($o->status
== 'listing_ok') {
189 $tree = $o->tree
->folder
;
190 $this->buildtree($tree, $ret);
196 * Get box.net file info
198 * @param string $fileid
199 * @param int $timeout request timeout in seconds
200 * @return stdClass|null
202 function get_file_info($fileid, $timeout = 0) {
203 $this->_clearErrors();
205 $params['action'] = 'get_file_info';
206 $params['file_id'] = $fileid;
207 $params['auth_token'] = $this->auth_token
;
208 $params['api_key'] = $this->api_key
;
209 $http = new curl(array('debug'=>$this->debug
));
210 $xml = $http->get($this->_box_api_url
, $params, array('timeout' => $timeout));
211 if (!$http->get_errno()) {
212 $o = simplexml_load_string(trim($xml));
213 if ($o->status
== 's_get_file_info') {
222 * @param array $tree Passed by reference
224 function buildtree($sax, &$tree){
227 foreach($sax as $k=>$v){
230 foreach($o->folder
as $z){
231 $tmp = array('title'=>(string)$z->attributes()->name
,
232 'size'=>0, 'date'=>userdate(time()),
233 'thumbnail'=>'https://www.box.com/img/small_folder_icon.gif',
234 'path'=>array('name'=>(string)$z->attributes()->name
, 'path'=>(int)$z->attributes()->id
));
235 $tmp['children'] = array();
236 $this->buildtree($z, $tmp['children']);
239 } elseif ($k == 'files') {
240 $val = $sax[$k]->file
;
241 foreach($val as $file){
242 $thumbnail = (string)$file->attributes()->thumbnail
;
243 if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
244 $thumbnail = 'http://www.box.com'.$thumbnail;
246 $tmp = array('title'=>(string)$file->attributes()->file_name
,
247 'size'=>display_size((int)$file->attributes()->size
),
248 'thumbnail'=>$thumbnail,
249 'date'=>userdate((int)$file->attributes()->updated
),
250 'source'=> $this->_box_api_download_url
.'/'
251 .$this->auth_token
.'/'.(string)$file->attributes()->id
,
252 'url'=>(string)$file->attributes()->shared_link
);
260 * @param array $params
261 * @return bool|array Array or false
263 function getAccountTree($params = array()) {
264 $params['auth_token'] = $this->auth_token
;
265 $params['folder_id'] = 0;
266 $params['api_key'] = $this->api_key
;
267 $params['action'] = 'get_account_tree';
268 $params['onelevel'] = 1;
269 $params['params[]'] = 'nozip';
270 $ret_array = array();
271 $data = $this->makeRequest('action=get_account_tree', $params);
272 if ($this->_checkForError($data)) {
275 $tree_count=count($data);
277 for ($i=0; $i<$tree_count; $i++
) {
282 if (@is_array
($a['attributes'])) {
283 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
284 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
285 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
290 if (@is_array
($a['attributes'])) {
291 $ret_array['file_id'][$i] = $a['attributes']['ID'];
292 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
293 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
294 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
295 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
296 if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
297 @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
299 @$ret_array['thumbnail'][$i] = 'http://www.box.com'.$a['attributes']['THUMBNAIL'];
310 * @param string $new_folder_name
311 * @param array $params
312 * @return bool|array Array or false
314 function CreateFolder($new_folder_name, $params = array()) {
315 $params['auth_token'] = $this->auth_token
;
316 $params['api_key'] = $this->api_key
;
317 $params['action'] = 'create_folder';
318 $params['name'] = $new_folder_name;
320 'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
321 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
323 foreach ($defaults as $key => $value) {
324 if (!array_key_exists($key, $params)) {
325 $params[$key] = $value;
329 $ret_array = array();
330 $data = $this->makeRequest('action=create_folder', $params);
331 if ($this->_checkForError($data)) {
334 foreach ($data as $a) {
335 if (!empty($a['value'])) {
339 $ret_array['folder_id'] = $a['value'];
343 $ret_array['folder_name'] = $a['value'];
346 case 'FOLDER_TYPE_ID':
347 $ret_array['folder_type_id'] = $a['value'];
351 $ret_array['shared'] = $a['value'];
355 $ret_array['password'] = $a['value'];
359 $ret_array[strtolower($a['tag'])] = null;
367 * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
368 * @return array|bool Array or false
370 function UploadFile ($params = array()) {
371 $params['auth_token'] = $this->auth_token
;
372 // this param should be the full path of the file
373 $params['new_file1'] = $params['file'];
374 unset($params['file']);
376 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
377 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
379 foreach ($defaults as $key => $value) {
380 if (!array_key_exists($key, $params)) {
381 $params[$key] = $value;
384 $ret_array = array();
386 $data = $this->makeRequest('upload', $params);
387 if ($this->_checkForError($data)) {
390 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++
) {
394 $ret_array['status'] = $a['value'];
398 if (is_array($a['attributes'])) {
399 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
400 @$ret_array['id'][$i] = $a['attributes']['ID'];
401 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
402 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
403 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
413 * @param string $fileid
414 * @param string $newname
417 function RenameFile($fileid, $newname) {
419 'api_key' => $this->api_key
,
420 'auth_token' => $this->auth_token
,
421 'action' => 'rename',
423 'target_id' => $fileid,
424 'new_name' => $newname,
426 $data = $this->makeRequest('action=rename', $params);
427 if ($this->_checkForError($data)) {
430 foreach ($data as $a) {
433 if ($a['value'] == 's_rename_node') {
444 * @param array $params
445 * @return array|bool Outcome Array or false
447 function RegisterUser($params = array()) {
448 $params['api_key'] = $this->api_key
;
449 $params['action'] = 'register_new_user';
450 $params['login'] = $_REQUEST['login'];
451 $params['password'] = $_REQUEST['password'];
452 $ret_array = array();
453 $data = $this->makeRequest('action=register_new_user', $params);
454 if ($this->_checkForError($data)) {
457 foreach ($data as $a) {
460 $ret_array['status'] = $a['value'];
464 $ret_array['auth_token'] = $a['value'];
468 $ret_array['login'] = $a['value'];
471 $ret_array['space_amount'] = $a['value'];
474 $ret_array['space_used'] = $a['value'];
483 * Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
486 * @param string $id Set to ID of file or folder
487 * @param string $target_type File or folder
488 * @param array $params
489 * @return array|bool Outcome Array or false
491 function AddTag($tag, $id, $target_type, $params = array()) {
492 $params['auth_token'] = $this->auth_token
;
493 $params['api_key'] = $this->api_key
;
494 $params['action'] = 'add_to_tag';
495 $params['target'] = $target_type; // File or folder
496 $params['target_id'] = $id; // Set to ID of file or folder
497 $params['tags[]'] = $tag;
498 $ret_array = array();
499 $data = $this->makeRequest('action=add_to_tag', $params);
500 if ($this->_checkForError($data)) {
503 foreach ($data as $a) {
506 $ret_array['status'] = $a['value'];
515 * Public Share (http://enabled.box.net/docs/rest#public_share)
517 * @param string $message
518 * @param string $emails
519 * @param string $id Set to ID of file or folder
520 * @param string $target_type File or folder
521 * @param string $password
522 * @param array $params
523 * @return array|bool Outcome Array or false
525 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
526 $params['auth_token'] = $this->auth_token
;
527 $params['api_key'] = $this->api_key
;
528 $params['action'] = 'public_share';
529 $params['target'] = $target_type;
530 $params['target_id'] = $id;
531 $params['password'] = $password;
532 $params['message'] = $message;
533 $params['emails'] = $emails;
534 $ret_array = array();
535 $data = $this->makeRequest('action=public_share', $params);
536 if ($this->_checkForError($data)) {
539 foreach ($data as $a) {
542 $ret_array['status'] = $a['value'];
545 $ret_array['public_name'] = $a['value'];
553 * Get Friends (http://enabled.box.net/docs/rest#get_friends)
555 * @param array $params
556 * @return array|bool Outcome Array or false
558 function GetFriends ($params = array()) {
559 $params['auth_token'] = $this->auth_token
;
560 $params['action'] = 'get_friends';
561 $params['api_key'] = $this->api_key
;
562 $params['params[]'] = 'nozip';
563 $ret_array = array();
564 $data = $this->makeRequest('action=get_friends', $params);
565 if ($this->_checkForError($data)) {
568 foreach ($data as $a) {
571 $ret_array['name'] = $a['value'];
574 $ret_array['email'] = $a['value'];
577 $ret_array['accepted'] = $a['value'];
580 $ret_array['avatar_url'] = $a['value'];
583 $ret_array['id'] = $a['value'];
586 $ret_array['url'] = $a['value'];
589 $ret_array['status'] = $a['value'];
597 * Logout User (http://enabled.box.net/docs/rest#get_friends)
599 * @param array $params
600 * @return array|bool Outcome Array or false
602 function Logout($params = array()) {
603 $params['auth_token'] = $this->auth_token
;
604 $params['api_key'] = $this->api_key
;
605 $params['action'] = 'logout';
606 $ret_array = array();
607 $data = $this->makeRequest('action=logout', $params);
608 if ($this->_checkForError($data)) {
611 foreach ($data as $a) {
614 $ret_array['logout'] = $a['value'];
625 function _checkForError($data) {
626 if ($this->_error_msg
!= '') {
629 if (@$data[0]['attributes']['STAT'] == 'fail') {
630 $this->_error_code
= $data[1]['attributes']['CODE'];
631 $this->_error_msg
= $data[1]['attributes']['MSG'];
640 public function isError() {
641 if ($this->_error_msg
!= '') {
649 public function setError($code = 0, $msg){
650 $this->_error_code
= $code;
651 $this->_error_msg
= $msg;
656 function getErrorMsg() {
657 return '<p>Error: (' . $this->_error_code
. ') ' . $this->_error_msg
. '</p>';
662 function getErrorCode() {
663 return $this->_error_code
;
668 function _clearErrors() {
669 $this->_error_code
= '';
670 $this->_error_msg
= '';