]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15350
authordongsheng <dongsheng>
Thu, 17 Jul 2008 15:25:48 +0000 (15:25 +0000)
committerdongsheng <dongsheng>
Thu, 17 Jul 2008 15:25:48 +0000 (15:25 +0000)
1. more clean up to boxclient lib
2. rewrite the getauthtoken function of boxclient
TODO
Penny, could you merge your changes into this lib

repository/boxnet/boxlibphp5.php
repository/boxnet/repository.class.php
repository/curl.class.php

index 98d76db9db143b39e297bc2b7ec1432f7743636d..0a80a602d1bc44b4ad2849010b46cf1368c844d4 100755 (executable)
 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);
@@ -63,7 +49,8 @@ class boxclient {
         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)) {
@@ -81,44 +68,49 @@ 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>';
+        } 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)) {
@@ -151,13 +143,17 @@ class boxclient {
         }
         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);
@@ -223,10 +219,10 @@ class boxclient {
 
     // 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)) {
@@ -260,11 +256,11 @@ 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['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);
@@ -284,13 +280,14 @@ 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['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)) {
@@ -311,9 +308,10 @@ 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['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)) {
@@ -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['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 = '';
index 85997617c503b761ddf58dff96d70c1d8e89441a..2282895ca539cae2851e3d09289fe8caa4ff4957 100755 (executable)
@@ -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.
-
+            $this->ticket = $ticket_return['ticket'];
             if($this->ticket && empty($this->options['auth_token'])) {
                 $str = '';
                 $str .= '<form id="moodle-repo-login">';
index b42f0ad11e0562fe55e74428f6cb879890f285e0..fa4ce524a23ba74faa8047b129403485583d3668 100644 (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);
     }