MDL-15350

1. more clean up to boxclient lib
2. rewrite the getauthtoken function of boxclient
TODO
Penny, could you merge your changes into this lib
This commit is contained in:
dongsheng 2008-07-17 15:25:48 +00:00
parent a9493cbea3
commit 34f210f609
3 changed files with 76 additions and 102 deletions

View file

@ -20,16 +20,12 @@
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;
@ -38,24 +34,14 @@ class boxclient {
// 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);
@ -64,6 +50,7 @@ class boxclient {
}
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)) {
@ -81,42 +68,47 @@ class boxclient {
}
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>';
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['api_key'] = $this->api_key;
$params['action'] = 'get_account_tree';
$params['onelevel'] = 1;
$params['params[]'] = 'nozip';
$ret_array = array();
@ -151,13 +143,17 @@ class boxclient {
}
return $ret_array;
}
// Create New Folder
function CreateFolder($new_folder_name, $params = array()) {
$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['api_key'] = $this->api_key;
$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);
@ -223,8 +219,8 @@ class boxclient {
// Register New User
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();
@ -260,9 +256,9 @@ class boxclient {
// 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['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;
@ -284,11 +280,12 @@ class boxclient {
// 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['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();
@ -311,8 +308,9 @@ class boxclient {
}
// 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['action'] = 'get_friends';
$params['api_key'] = $this->api_key;
$params['params[]'] = 'nozip';
$ret_array = array();
$data = $this->makeRequest('action=get_friends', $params);
@ -349,9 +347,9 @@ class boxclient {
// Logout User
function Logout($params = array()) {
$params['api_key'] = $this->api_key;
$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)) {
@ -376,7 +374,6 @@ class boxclient {
return false;
}
public function isError() {
if ($this->_error_msg != '') {
return true;
@ -384,7 +381,6 @@ class boxclient {
return false;
}
function getErrorMsg() {
return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
}
@ -393,7 +389,6 @@ class boxclient {
return $this->_error_code;
}
function _clearErrors() {
$this->_error_code = '';
$this->_error_msg = '';

View file

@ -112,27 +112,7 @@ class repository_boxnet extends repository{
} else if(!empty($this->box)){
// get a ticket from box.net
$ticket_return = $this->box->getTicket();
if($this->box->isError()) {
if(!$this->options['ajax']){
echo $this->box->getErrorMsg();
}
} else {
$this->ticket = $ticket_return['ticket'];
}
// use the ticket to get a auth_token
// auth_token is the key to access the resources
// of box.net
// WARNING: this function won't return a auth_token
// if auth_token is not existed, this function will
// direct user to authentication page of box.net
// If the user has been authenticated, box.net will
// direct to a callback page (can be set in box.net)
// the call back page will obtain the auth_token
// ===============================================
// Because the authentication process will be done
// in box.net, so we need print a login link in this
// function instead a login screen.
if($this->ticket && empty($this->options['auth_token'])) {
$str = '';
$str .= '<form id="moodle-repo-login">';

View file

@ -372,9 +372,8 @@ class curl {
if (!empty($params)){
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= http_build_query($params);
$url .= http_build_query($params, '', '&');
}
return $this->request($url, $options);
}