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 = 'http://box.net/api/1.0/rest';
40 private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
41 private $_error_code = '';
42 private $_error_msg = '';
44 private $debug = false;
47 * @param string $api_key
48 * @param string $auth_token
51 public function __construct($api_key, $auth_token = '', $debug = false) {
52 $this->api_key
= $api_key;
53 $this->auth_token
= $auth_token;
63 * @param string $method
64 * @param array $params
67 function makeRequest($method, $params = array()) {
68 $this->_clearErrors();
69 $c = new curl(array('debug'=>$this->debug
, 'cache'=>true, 'module_cache'=>'repository'));
71 if ($method == 'upload'){
72 $request = $this->_box_api_upload_url
.'/'.
73 $this->auth_token
.'/'.$params['folder_id'];
74 $xml = $c->post($request, $params);
77 $xml = $c->get($this->_box_api_url
, $params);
79 $xml_parser = xml_parser_create();
81 xml_parse_into_struct($xml_parser, $xml, $data);
82 xml_parser_free($xml_parser);
83 } catch (moodle_exception
$e) {
84 $this->setError(0, 'connection time-out or invalid url');
90 * @param array $params
93 function getTicket($params = array()) {
94 $params['api_key'] = $this->api_key
;
95 $params['action'] = 'get_ticket';
97 $data = $this->makeRequest('action=get_ticket', $params);
98 if ($this->_checkForError($data)) {
101 foreach ($data as $a) {
104 $ret_array['status'] = $a['value'];
107 $ret_array['ticket'] = $a['value'];
115 * $options['username'] and $options['password'] must be
116 * given, we will use them to obtain a valid auth_token
117 * To get a token, you should use following code:
120 * $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
122 * $t = $box->getTicket();
123 * $box->getAuthToken($t['ticket'], array(
124 * 'username'=>'dongsheng@moodle.com',
125 * 'password'=>'xxx'));
128 * @param string $ticket
129 * @param string $username
130 * @param string $password
133 function getAuthToken($ticket, $username, $password) {
134 $c = new curl(array('debug'=>$this->debug
));
135 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
139 'password'=>$password,
144 $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param);
145 } catch (moodle_exception
$e) {
146 $this->setError(0, 'connection time-out or invalid url');
149 $header = $c->getResponse();
150 if(empty($header['location'])) {
151 throw new repository_exception('invalidpassword', 'repository_boxnet');
153 $location = $header['location'];
154 preg_match('#auth_token=(.*)$#i', $location, $matches);
155 $auth_token = $matches[1];
156 if(!empty($auth_token)) {
157 $this->auth_token
= $auth_token;
160 throw new repository_exception('invalidtoken', 'repository_boxnet');
164 * @param string $path Unused
165 * @param array $params
168 function getfiletree($path, $params = array()) {
169 $this->_clearErrors();
170 $params['auth_token'] = $this->auth_token
;
171 $params['folder_id'] = 0;
172 $params['api_key'] = $this->api_key
;
173 $params['action'] = 'get_account_tree';
174 $params['onelevel'] = 1;
175 $params['params[]'] = 'nozip';
176 $c = new curl(array('debug'=>$this->debug
, 'cache'=>true, 'module_cache'=>'repository'));
179 $xml = $c->get($this->_box_api_url
, $params);
180 } catch (Exception
$e){
183 $o = simplexml_load_string(trim($xml));
184 if($o->status
== 'listing_ok') {
185 $tree = $o->tree
->folder
;
186 $this->buildtree($tree, $ret);
193 * @param array $tree Passed by reference
195 function buildtree($sax, &$tree){
198 foreach($sax as $k=>$v){
201 foreach($o->folder
as $z){
202 $tmp = array('title'=>(string)$z->attributes()->name
,
203 'size'=>0, 'date'=>userdate(time()),
204 'thumbnail'=>'http://www.box.net/img/small_folder_icon.gif',
205 'path'=>array('name'=>(string)$z->attributes()->name
, 'path'=>(int)$z->attributes()->id
));
206 $tmp['children'] = array();
207 $this->buildtree($z, $tmp['children']);
210 } elseif ($k == 'files') {
211 $val = $sax[$k]->file
;
212 foreach($val as $file){
213 $thumbnail = (string)$file->attributes()->thumbnail
;
214 if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
215 $thumbnail = 'http://www.box.net'.$thumbnail;
217 $tmp = array('title'=>(string)$file->attributes()->file_name
,
218 'size'=>display_size((int)$file->attributes()->size
),
219 'thumbnail'=>$thumbnail,
220 'date'=>userdate((int)$file->attributes()->updated
),
221 'source'=>'http://box.net/api/1.0/download/'
222 .$this->auth_token
.'/'.(string)$file->attributes()->id
,
223 'url'=>(string)$file->attributes()->shared_link
);
231 * @param array $params
232 * @return bool|array Array or false
234 function getAccountTree($params = array()) {
235 $params['auth_token'] = $this->auth_token
;
236 $params['folder_id'] = 0;
237 $params['api_key'] = $this->api_key
;
238 $params['action'] = 'get_account_tree';
239 $params['onelevel'] = 1;
240 $params['params[]'] = 'nozip';
241 $ret_array = array();
242 $data = $this->makeRequest('action=get_account_tree', $params);
243 if ($this->_checkForError($data)) {
246 $tree_count=count($data);
248 for ($i=0; $i<$tree_count; $i++
) {
253 if (@is_array
($a['attributes'])) {
254 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
255 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
256 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
261 if (@is_array
($a['attributes'])) {
262 $ret_array['file_id'][$i] = $a['attributes']['ID'];
263 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
264 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
265 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
266 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
267 if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
268 @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
270 @$ret_array['thumbnail'][$i] = 'http://www.box.net'.$a['attributes']['THUMBNAIL'];
281 * @param string $new_folder_name
282 * @param array $params
283 * @return bool|array Array or false
285 function CreateFolder($new_folder_name, $params = array()) {
286 $params['auth_token'] = $this->auth_token
;
287 $params['api_key'] = $this->api_key
;
288 $params['action'] = 'create_folder';
289 $params['name'] = $new_folder_name;
291 'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
292 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
294 foreach ($defaults as $key => $value) {
295 if (!array_key_exists($key, $params)) {
296 $params[$key] = $value;
300 $ret_array = array();
301 $data = $this->makeRequest('action=create_folder', $params);
302 if ($this->_checkForError($data)) {
305 foreach ($data as $a) {
306 if (!empty($a['value'])) {
310 $ret_array['folder_id'] = $a['value'];
314 $ret_array['folder_name'] = $a['value'];
317 case 'FOLDER_TYPE_ID':
318 $ret_array['folder_type_id'] = $a['value'];
322 $ret_array['shared'] = $a['value'];
326 $ret_array['password'] = $a['value'];
330 $ret_array[strtolower($a['tag'])] = null;
338 * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
339 * @return array|bool Array or false
341 function UploadFile ($params = array()) {
342 $params['auth_token'] = $this->auth_token
;
343 // this param should be the full path of the file
344 $params['new_file1'] = $params['file'];
345 unset($params['file']);
347 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
348 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
350 foreach ($defaults as $key => $value) {
351 if (!array_key_exists($key, $params)) {
352 $params[$key] = $value;
355 $ret_array = array();
357 $data = $this->makeRequest('upload', $params);
358 if ($this->_checkForError($data)) {
361 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++
) {
365 $ret_array['status'] = $a['value'];
369 if (is_array($a['attributes'])) {
370 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
371 @$ret_array['id'][$i] = $a['attributes']['ID'];
372 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
373 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
374 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
384 * @param string $fileid
385 * @param string $newname
388 function RenameFile($fileid, $newname) {
390 'api_key' => $this->api_key
,
391 'auth_token' => $this->auth_token
,
392 'action' => 'rename',
394 'target_id' => $fileid,
395 'new_name' => $newname,
397 $data = $this->makeRequest('action=rename', $params);
398 if ($this->_checkForError($data)) {
401 foreach ($data as $a) {
404 if ($a['value'] == 's_rename_node') {
415 * @param array $params
416 * @return array|bool Outcome Array or false
418 function RegisterUser($params = array()) {
419 $params['api_key'] = $this->api_key
;
420 $params['action'] = 'register_new_user';
421 $params['login'] = $_REQUEST['login'];
422 $params['password'] = $_REQUEST['password'];
423 $ret_array = array();
424 $data = $this->makeRequest('action=register_new_user', $params);
425 if ($this->_checkForError($data)) {
428 foreach ($data as $a) {
431 $ret_array['status'] = $a['value'];
435 $ret_array['auth_token'] = $a['value'];
439 $ret_array['login'] = $a['value'];
442 $ret_array['space_amount'] = $a['value'];
445 $ret_array['space_used'] = $a['value'];
454 * Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
457 * @param string $id Set to ID of file or folder
458 * @param string $target_type File or folder
459 * @param array $params
460 * @return array|bool Outcome Array or false
462 function AddTag($tag, $id, $target_type, $params = array()) {
463 $params['auth_token'] = $this->auth_token
;
464 $params['api_key'] = $this->api_key
;
465 $params['action'] = 'add_to_tag';
466 $params['target'] = $target_type; // File or folder
467 $params['target_id'] = $id; // Set to ID of file or folder
468 $params['tags[]'] = $tag;
469 $ret_array = array();
470 $data = $this->makeRequest('action=add_to_tag', $params);
471 if ($this->_checkForError($data)) {
474 foreach ($data as $a) {
477 $ret_array['status'] = $a['value'];
486 * Public Share (http://enabled.box.net/docs/rest#public_share)
488 * @param string $message
489 * @param string $emails
490 * @param string $id Set to ID of file or folder
491 * @param string $target_type File or folder
492 * @param string $password
493 * @param array $params
494 * @return array|bool Outcome Array or false
496 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
497 $params['auth_token'] = $this->auth_token
;
498 $params['api_key'] = $this->api_key
;
499 $params['action'] = 'public_share';
500 $params['target'] = $target_type;
501 $params['target_id'] = $id;
502 $params['password'] = $password;
503 $params['message'] = $message;
504 $params['emails'] = $emails;
505 $ret_array = array();
506 $data = $this->makeRequest('action=public_share', $params);
507 if ($this->_checkForError($data)) {
510 foreach ($data as $a) {
513 $ret_array['status'] = $a['value'];
516 $ret_array['public_name'] = $a['value'];
524 * Get Friends (http://enabled.box.net/docs/rest#get_friends)
526 * @param array $params
527 * @return array|bool Outcome Array or false
529 function GetFriends ($params = array()) {
530 $params['auth_token'] = $this->auth_token
;
531 $params['action'] = 'get_friends';
532 $params['api_key'] = $this->api_key
;
533 $params['params[]'] = 'nozip';
534 $ret_array = array();
535 $data = $this->makeRequest('action=get_friends', $params);
536 if ($this->_checkForError($data)) {
539 foreach ($data as $a) {
542 $ret_array['name'] = $a['value'];
545 $ret_array['email'] = $a['value'];
548 $ret_array['accepted'] = $a['value'];
551 $ret_array['avatar_url'] = $a['value'];
554 $ret_array['id'] = $a['value'];
557 $ret_array['url'] = $a['value'];
560 $ret_array['status'] = $a['value'];
568 * Logout User (http://enabled.box.net/docs/rest#get_friends)
570 * @param array $params
571 * @return array|bool Outcome Array or false
573 function Logout($params = array()) {
574 $params['auth_token'] = $this->auth_token
;
575 $params['api_key'] = $this->api_key
;
576 $params['action'] = 'logout';
577 $ret_array = array();
578 $data = $this->makeRequest('action=logout', $params);
579 if ($this->_checkForError($data)) {
582 foreach ($data as $a) {
585 $ret_array['logout'] = $a['value'];
596 function _checkForError($data) {
597 if ($this->_error_msg
!= '') {
600 if (@$data[0]['attributes']['STAT'] == 'fail') {
601 $this->_error_code
= $data[1]['attributes']['CODE'];
602 $this->_error_msg
= $data[1]['attributes']['MSG'];
611 public function isError() {
612 if ($this->_error_msg
!= '') {
620 public function setError($code = 0, $msg){
621 $this->_error_code
= $code;
622 $this->_error_msg
= $msg;
627 function getErrorMsg() {
628 return '<p>Error: (' . $this->_error_code
. ') ' . $this->_error_msg
. '</p>';
633 function getErrorCode() {
634 return $this->_error_code
;
639 function _clearErrors() {
640 $this->_error_code
= '';
641 $this->_error_msg
= '';