2008-06-27 06:19:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Box REST Client Library for PHP5 Developers
|
2008-07-14 05:31:01 +00:00
|
|
|
*
|
|
|
|
*
|
2008-06-27 06:19:40 +00:00
|
|
|
* @author James Levy <james@box.net>
|
|
|
|
* @link http://enabled.box.net
|
|
|
|
* @access public
|
|
|
|
* @version 1.0
|
2008-07-14 05:31:01 +00:00
|
|
|
* copyright Box.net 2007
|
2008-06-27 06:19:40 +00:00
|
|
|
* Available for use and distribution under GPL-license
|
|
|
|
* Go to http://www.gnu.org/licenses/gpl-3.0.txt for full text
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modified by Dongsheng Cai <dongsheng@cvs.moodle.org>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
class boxclient {
|
2008-07-17 15:25:48 +00:00
|
|
|
public $auth_token = '';
|
2008-06-27 06:19:40 +00:00
|
|
|
|
2008-07-17 15:25:48 +00:00
|
|
|
private $_box_api_url = 'http://www.box.net/api/1.0/rest';
|
|
|
|
private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
|
|
|
|
private $_error_code = '';
|
|
|
|
private $_error_msg = '';
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
public function __construct($api_key, $auth_token = '') {
|
2008-06-27 06:19:40 +00:00
|
|
|
$this->api_key = $api_key;
|
2008-07-17 13:12:09 +00:00
|
|
|
$this->auth_token = $auth_token;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
// Setup for Functions
|
|
|
|
function makeRequest($method, $params = array()) {
|
|
|
|
$this->_clearErrors();
|
2008-07-17 13:12:09 +00:00
|
|
|
$c = new curl(array('cache'=>true));
|
2008-06-27 06:19:40 +00:00
|
|
|
if ($method == 'upload'){
|
2008-07-17 13:12:09 +00:00
|
|
|
$request = $this->_box_api_upload_url.'/'.
|
|
|
|
$this->auth_token.'/'.$params['folder_id'];
|
|
|
|
$xml = $c->post($request, params);
|
2008-06-27 06:19:40 +00:00
|
|
|
}else{
|
|
|
|
$args = array();
|
2008-07-17 15:25:48 +00:00
|
|
|
$xml = $c->get($this->_box_api_url, $params);
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
$xml_parser = xml_parser_create();
|
|
|
|
xml_parse_into_struct($xml_parser, $xml, $data);
|
|
|
|
xml_parser_free($xml_parser);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
function getTicket($params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['api_key'] = $this->api_key;
|
|
|
|
$params['action'] = 'get_ticket';
|
2008-06-27 06:19:40 +00:00
|
|
|
$ret_array = array();
|
|
|
|
$data = $this->makeRequest('action=get_ticket', $params);
|
|
|
|
if ($this->_checkForError($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($data as $a) {
|
|
|
|
switch ($a['tag']) {
|
2008-07-17 13:12:09 +00:00
|
|
|
case 'STATUS':
|
|
|
|
$ret_array['status'] = $a['value'];
|
|
|
|
break;
|
|
|
|
case 'TICKET':
|
|
|
|
$ret_array['ticket'] = $a['value'];
|
|
|
|
break;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret_array;
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 15:25:48 +00:00
|
|
|
// $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:
|
|
|
|
//
|
|
|
|
// $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
|
|
|
|
// Get a ticket
|
|
|
|
// $t = $box->getTicket();
|
|
|
|
// $box->getAuthToken($t['ticket'], array(
|
|
|
|
// 'username'=>'dongsheng@moodle.com',
|
|
|
|
// 'password'=>'xxx'));
|
|
|
|
//
|
2008-07-18 07:40:12 +00:00
|
|
|
function getAuthToken($ticket, $username, $password) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$c = new curl;
|
|
|
|
$c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
|
|
|
|
$param = array(
|
|
|
|
'login_form1'=>'',
|
2008-07-18 07:40:12 +00:00
|
|
|
'login'=>$username,
|
|
|
|
'password'=>$password,
|
2008-07-17 15:25:48 +00:00
|
|
|
'dologin'=>1,
|
|
|
|
'__login'=>1
|
|
|
|
);
|
|
|
|
$ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param);
|
|
|
|
$header = $c->getResponse();
|
|
|
|
$location = $header['location'];
|
|
|
|
preg_match('#auth_token=(.*)$#i', $location, $matches);
|
|
|
|
$auth_token = $matches[1];
|
|
|
|
if(!empty($auth_token)) {
|
|
|
|
$this->auth_token = $auth_token;
|
2008-07-07 06:34:39 +00:00
|
|
|
return $auth_token;
|
2008-07-17 15:25:48 +00:00
|
|
|
} else {
|
2008-07-07 06:34:39 +00:00
|
|
|
return false;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 15:25:48 +00:00
|
|
|
// Get the file list
|
2008-06-27 06:19:40 +00:00
|
|
|
function getAccountTree($params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$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';
|
2008-06-27 06:19:40 +00:00
|
|
|
$ret_array = array();
|
|
|
|
$data = $this->makeRequest('action=get_account_tree', $params);
|
|
|
|
if ($this->_checkForError($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$tree_count=count($data);
|
|
|
|
global $tree_count;
|
|
|
|
$entry_count = 0;
|
|
|
|
for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
|
|
|
|
$a = $data[$i];
|
2008-07-14 05:31:01 +00:00
|
|
|
switch ($a['tag'])
|
2008-06-27 06:19:40 +00:00
|
|
|
{
|
2008-07-17 13:12:09 +00:00
|
|
|
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'];
|
|
|
|
$entry_count++;
|
|
|
|
}
|
|
|
|
break;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret_array;
|
|
|
|
}
|
2008-07-17 15:25:48 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
// Create New Folder
|
|
|
|
function CreateFolder($new_folder_name, $params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['auth_token'] = $this->auth_token;
|
2008-07-17 13:12:09 +00:00
|
|
|
$params['api_key'] = $this->api_key;
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['action'] = 'create_folder';
|
|
|
|
//Set to '0' by default. Change to create within sub-folder.
|
|
|
|
$params['parent_id'] = 0;
|
2008-07-17 13:12:09 +00:00
|
|
|
$params['name'] = $new_folder_name;
|
2008-07-17 15:25:48 +00:00
|
|
|
//Set to '1' by default. Set to '0' to make folder private.
|
|
|
|
$params['share'] = 1;
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
$ret_array = array();
|
|
|
|
$data = $this->makeRequest('action=create_folder', $params);
|
2008-07-14 05:31:01 +00:00
|
|
|
|
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
if ($this->_checkForError($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($data as $a) {
|
|
|
|
switch ($a['tag']) {
|
|
|
|
case 'FOLDER_ID':
|
|
|
|
$ret_array['folder_id'] = $a['value'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'FOLDER_NAME':
|
|
|
|
$ret_array['folder_type'] = $a['value'];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'SHARED':
|
|
|
|
$ret_array['shared'] = $a['value'];
|
|
|
|
break;
|
|
|
|
case 'PASSWORD':
|
|
|
|
$ret_array['password'] = $a['value'];
|
|
|
|
break;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
2008-07-17 13:12:09 +00:00
|
|
|
}
|
|
|
|
return $ret_array;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
2008-07-17 13:12:09 +00:00
|
|
|
// Upload File
|
|
|
|
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'];
|
|
|
|
$params['folder_id'] = 0; //Set to '0' by default. Change to create within sub-folder.
|
|
|
|
$params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
|
|
|
|
$ret_array = array();
|
|
|
|
$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;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
return $ret_array;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
// Register New User
|
|
|
|
function RegisterUser($params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['api_key'] = $this->api_key;
|
|
|
|
$params['action'] = 'register_new_user';
|
|
|
|
$params['login'] = $_REQUEST['login'];
|
|
|
|
$params['password'] = $_REQUEST['password'];
|
2008-07-17 13:12:09 +00:00
|
|
|
$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;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
return $ret_array;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
// Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-06-27 06:19:40 +00:00
|
|
|
function AddTag($tag, $id, $target_type, $params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$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
|
2008-06-27 06:19:40 +00:00
|
|
|
$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']) {
|
2008-07-17 13:12:09 +00:00
|
|
|
case 'STATUS':
|
|
|
|
$ret_array['status'] = $a['value'];
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
break;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret_array;
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-06-27 06:19:40 +00:00
|
|
|
// Public Share (http://enabled.box.net/docs/rest#public_share)
|
|
|
|
function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$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;
|
2008-06-27 06:19:40 +00:00
|
|
|
$params['message'] = $message;
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['emails'] = $emails;
|
2008-06-27 06:19:40 +00:00
|
|
|
$ret_array = array();
|
|
|
|
$data = $this->makeRequest('action=public_share', $params);
|
|
|
|
if ($this->_checkForError($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($data as $a) {
|
|
|
|
switch ($a['tag']) {
|
2008-07-17 13:12:09 +00:00
|
|
|
case 'STATUS':
|
|
|
|
$ret_array['status'] = $a['value'];
|
|
|
|
break;
|
|
|
|
case 'PUBLIC_NAME':
|
|
|
|
$ret_array['public_name'] = $a['value'];
|
|
|
|
break;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-06-27 06:19:40 +00:00
|
|
|
return $ret_array;
|
|
|
|
}
|
|
|
|
// Get Friends (http://enabled.box.net/docs/rest#get_friends)
|
2008-07-17 13:12:09 +00:00
|
|
|
function GetFriends ($params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['auth_token'] = $this->auth_token;
|
|
|
|
$params['action'] = 'get_friends';
|
|
|
|
$params['api_key'] = $this->api_key;
|
|
|
|
$params['params[]'] = 'nozip';
|
2008-06-27 06:19:40 +00:00
|
|
|
$ret_array = array();
|
|
|
|
$data = $this->makeRequest('action=get_friends', $params);
|
|
|
|
if ($this->_checkForError($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
foreach ($data as $a) {
|
|
|
|
switch ($a['tag']) {
|
2008-07-17 13:12:09 +00:00
|
|
|
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;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret_array;
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-06-27 06:19:40 +00:00
|
|
|
// Logout User
|
2008-07-17 13:12:09 +00:00
|
|
|
function Logout($params = array()) {
|
2008-07-17 15:25:48 +00:00
|
|
|
$params['auth_token'] = $this->auth_token;
|
|
|
|
$params['api_key'] = $this->api_key;
|
|
|
|
$params['action'] = 'logout';
|
2008-07-17 13:12:09 +00:00
|
|
|
$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'];
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
break;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
2008-06-27 06:19:40 +00:00
|
|
|
return $ret_array;
|
|
|
|
}
|
|
|
|
}
|
2008-07-17 13:12:09 +00:00
|
|
|
function _checkForError($data) {
|
|
|
|
if (@$data[0]['attributes']['STAT'] == 'fail') {
|
|
|
|
$this->_error_code = $data[1]['attributes']['CODE'];
|
|
|
|
$this->_error_msg = $data[1]['attributes']['MSG'];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
public function isError() {
|
|
|
|
if ($this->_error_msg != '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2008-07-14 05:31:01 +00:00
|
|
|
}
|
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
function getErrorMsg() {
|
|
|
|
return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
function getErrorCode() {
|
|
|
|
return $this->_error_code;
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-07-17 13:12:09 +00:00
|
|
|
function _clearErrors() {
|
|
|
|
$this->_error_code = '';
|
|
|
|
$this->_error_msg = '';
|
|
|
|
}
|
2008-07-14 05:31:01 +00:00
|
|
|
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
?>
|