require_once($CFG->dirroot.'/repository/'.'curl.class.php');
class boxclient {
- var $_box_api_url = 'http://www.box.net/api/1.0/rest';
- var $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
- var $_error_code = '';
- var $_error_msg = '';
- var $auth_token = '';
+ public $auth_token = '';
- public function setAuth($str){
- $this->auth_token = $str;
-
- }
+ 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 = '';
public function __construct($api_key, $auth_token = '') {
$this->api_key = $api_key;
// Setup for Functions
function makeRequest($method, $params = array()) {
$this->_clearErrors();
-
$c = new curl(array('cache'=>true));
- $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>true));
-
if ($method == 'upload'){
$request = $this->_box_api_upload_url.'/'.
$this->auth_token.'/'.$params['folder_id'];
- var_dump($request);
- var_dump($params);
$xml = $c->post($request, params);
}else{
$args = array();
- foreach($params as $k => $v){
- array_push($args, urlencode($k).'='.urlencode($v));
- $query_str = implode('&', $args);
- }
- $request = $this->_box_api_url .'?'. $method . '&' . $query_str;
- $xml = $c->get($request);
+ $xml = $c->get($this->_box_api_url, $params);
}
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml, $data);
return $data;
}
function getTicket($params = array()) {
- $params['api_key'] = $this->api_key;
+ $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 $ret_array;
}
- // Get Auth Token
- function getAuthToken($ticket) {
- $params['api_key'] = $this->api_key;
- $params['ticket'] = $ticket;
- $ret_array = array();
- $data = $this->makeRequest('action=get_auth_token', $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;
- }
- }
- if ($ret_array['status'] == 'get_auth_token_ok'){
- $this->auth_token = $ret_array['auth_token'];
- $auth_token = $ret_array['auth_token'];
+ // $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'));
+ //
+ function getAuthToken($ticket, $options = array()) {
+ $c = new curl;
+ $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
+ $param = array(
+ 'login_form1'=>'',
+ 'login'=>$options['username'],
+ 'password'=>$options['password'],
+ '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;
return $auth_token;
- }else{
- echo '<a href="http://www.box.net/api/1.0/auth/'.$ticket.'">Login</a>';
+ } else {
return false;
}
}
- // Retrieve Account Tree (http://enabled.box.net/docs/rest#get_account_tree)
+ // Get the file list
function getAccountTree($params = array()) {
- $params['api_key'] = $this->api_key;
- $params['auth_token'] = $this->auth_token;
- $params['folder_id'] = 0;
- $params['onelevel'] = 1;
- $params['params[]'] = 'nozip';
+ $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 $ret_array;
}
+
// Create New Folder
function CreateFolder($new_folder_name, $params = array()) {
+ $params['auth_token'] = $this->auth_token;
$params['api_key'] = $this->api_key;
- $params['auth_token'] = $this->auth_token;
- $params['parent_id'] = 0; //Set to '0' by default. Change to create within sub-folder.
+ $params['action'] = 'create_folder';
+ //Set to '0' by default. Change to create within sub-folder.
+ $params['parent_id'] = 0;
$params['name'] = $new_folder_name;
- $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
+ //Set to '1' by default. Set to '0' to make folder private.
+ $params['share'] = 1;
$ret_array = array();
$data = $this->makeRequest('action=create_folder', $params);
// Register New User
function RegisterUser($params = array()) {
-
- $params['api_key'] = $this->api_key;
- $params['login'] = $_REQUEST['login'];
- $params['password'] = $_REQUEST['password'];
+ $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)) {
// Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
function AddTag($tag, $id, $target_type, $params = array()) {
-
- $params['api_key'] = $this->api_key;
- $params['auth_token'] = $this->auth_token;
- $params['target'] = $target_type; // File or folder
- $params['target_id'] = $id; // Set to ID of file or folder
+ $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);
// Public Share (http://enabled.box.net/docs/rest#public_share)
function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
- $params['api_key'] = $this->api_key;
- $params['auth_token'] = $this->auth_token;
- $params['target'] = $target_type; // File or folder
- $params['target_id'] = $id; // Set to ID of file or folder
- $params['password'] = $password; //optional
+ $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;
+ $params['emails'] = $emails;
$ret_array = array();
$data = $this->makeRequest('action=public_share', $params);
if ($this->_checkForError($data)) {
}
// Get Friends (http://enabled.box.net/docs/rest#get_friends)
function GetFriends ($params = array()) {
- $params['api_key'] = $this->api_key;
- $params['auth_token'] = $this->auth_token;
- $params['params[]'] = 'nozip';
+ $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)) {
// Logout User
function Logout($params = array()) {
-
- $params['api_key'] = $this->api_key;
- $params['auth_token'] = $this->auth_token;
+ $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;
}
-
public function isError() {
if ($this->_error_msg != '') {
return true;
return false;
}
-
function getErrorMsg() {
return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
}
return $this->_error_code;
}
-
function _clearErrors() {
$this->_error_code = '';
$this->_error_msg = '';