]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-13766
authordongsheng <dongsheng>
Mon, 15 Sep 2008 09:31:41 +0000 (09:31 +0000)
committerdongsheng <dongsheng>
Mon, 15 Sep 2008 09:31:41 +0000 (09:31 +0000)
1. create logout() and check_login() function
2. Remove the code use global varibal

repository/boxnet/repository.class.php
repository/flickr/repository.class.php
repository/flickr_public/repository.class.php
repository/lib.php
repository/ws.php

index 6bfe7e2914dab0c4a28482d8a66e858dc34febd4..e5bea87c42112f5ffba3a98784177cedd797f125 100755 (executable)
@@ -13,37 +13,8 @@ require_once($CFG->libdir.'/boxlib.php');
 class repository_boxnet extends repository{
     private $box;
 
-    public function set_option($options = array()){
-        if (!empty($options['api_key'])) {
-            set_config('api_key', trim($options['api_key']), 'boxnet');
-        }
-        unset($options['api_key']);
-        $ret = parent::set_option($options);
-        return $ret;
-    }
-
-    public function get_option($config = ''){
-        if($config==='api_key'){
-            return trim(get_config('boxnet', 'api_key'));
-        } else {
-            $options['api_key'] = trim(get_config('boxnet', 'api_key'));
-        }
-        $options = parent::get_option($config);
-        return $options;
-    }
-
-    public function global_search(){
-        global $SESSION;
-        $sess_name = 'box_token'.$this->id;
-        if (empty($SESSION->$sess_name)) {
-            return false;
-        } else {
-            return true;
-        }
-    }
-
     public function __construct($repositoryid, $context = SITEID, $options = array()){
-        global $SESSION, $action;
+        global $SESSION;
         $options['username']   = optional_param('boxusername', '', PARAM_RAW);
         $options['password']   = optional_param('boxpassword', '', PARAM_RAW);
         $options['ticket']     = optional_param('ticket', '', PARAM_RAW);
@@ -53,10 +24,7 @@ class repository_boxnet extends repository{
         if (empty($this->api_key)) {
         }
         $sess_name = 'box_token'.$this->id;
-        // reset session
-        if(!empty($reset)) {
-            unset($SESSION->$sess_name);
-        }
+        $this->sess_name = 'box_token'.$this->id;
         // do login
         if(!empty($options['username'])
                     && !empty($options['password'])
@@ -69,11 +37,6 @@ class repository_boxnet extends repository{
             } catch (repository_exception $e) {
                 throw $e;
             }
-            if ($SESSION->$sess_name) {
-                $action = 'list';
-            } else {
-                $action = 'login';
-            }
         }
         // already logged
         if(!empty($SESSION->$sess_name)) {
@@ -81,13 +44,48 @@ class repository_boxnet extends repository{
                 $this->box = new boxclient($this->api_key, $SESSION->$sess_name);
             }
             $this->auth_token = $SESSION->$sess_name;
-            if(empty($action)) {
-                $action = 'list';
-            }
         } else {
             $this->box = new boxclient($this->api_key);
-            // print login
-            $action = 'login';
+        }
+    }
+
+    public function check_login(){
+        global $SESSION;
+        return !empty($SESSION->{$this->sess_name});
+    }
+
+    public function logout(){
+        global $SESSION;
+        unset($SESSION->{$this->sess_name});
+        return $this->print_login();
+    }
+
+    public function set_option($options = array()){
+        if (!empty($options['api_key'])) {
+            set_config('api_key', trim($options['api_key']), 'boxnet');
+        }
+        unset($options['api_key']);
+        $ret = parent::set_option($options);
+        return $ret;
+    }
+
+    public function get_option($config = ''){
+        if($config==='api_key'){
+            return trim(get_config('boxnet', 'api_key'));
+        } else {
+            $options['api_key'] = trim(get_config('boxnet', 'api_key'));
+        }
+        $options = parent::get_option($config);
+        return $options;
+    }
+
+    public function global_search(){
+        global $SESSION;
+        $sess_name = 'box_token'.$this->id;
+        if (empty($SESSION->$sess_name)) {
+            return false;
+        } else {
+            return true;
         }
     }
 
@@ -143,56 +141,31 @@ class repository_boxnet extends repository{
     }
 
     public function print_login(){
-        if(!empty($this->options['auth_token'])) {
-            if($this->options['ajax']){
-                return $this->get_listing();
-            } else {
-                // format file list and 
-                // print list
-            }
-        } else {
-            $t = $this->box->getTicket();
-            if(empty($this->options['auth_token'])) {
-                $ret = $this->get_login();
-                $str = '';
-                $str .= '<form id="moodle-repo-login">';
-                $str .= '<input type="hidden" name="ticket" value="'.
-                    $t['ticket'].'" />';
-                $str .= '<input type="hidden" name="id" value="'.$this->id.'" />';
-                $str .= '<label for="box_username">Username: <label><br/>';
-                $str .= '<input type="text" id="box_username" name="username" value="'.$ret->username.'" />';
-                $str .= '<br/>';
-                $str .= '<label for="box_password">Password: <label><br/>';
-                $str .= '<input type="password" value="'.$ret->password.'" id="box_password" name="password" /><br/>';
-                $str .= '<input type="button" onclick="repository_client.login()" value="Go" />';
-                $str .= '</form>';
-                if ($this->options['ajax']) {
-                    $e1->type = 'hidden';
-                    $e1->name = 'ticket';
-                    $e1->value = $t['ticket'];
-
-                    $e2->type = 'hidden';
-                    $e2->name = 'repo_id';
-                    $e2->value = $this->id;
-
-                    $e3->label = get_string('username', 'repository_boxnet').': ';
-                    $e3->id    = 'box_username';
-                    $e3->type  = 'text';
-                    $e3->name  = 'boxusername';
-                    $e3->value = $ret->username;
-                    
-                    $e4->label = get_string('password', 'repository_boxnet').': ';
-                    $e4->id    = 'box_password';
-                    $e4->type  = 'password';
-                    $e4->name  = 'boxpassword';
-
-                    $ret = array();
-                    $ret['login'] = array($e1, $e2, $e3, $e4);
-                    return $ret;
-                } else {
-                    echo $str;
-                }
-            }
+        $t = $this->box->getTicket();
+        $ret = $this->get_login();
+        if ($this->options['ajax']) {
+            $e1->type = 'hidden';
+            $e1->name = 'ticket';
+            $e1->value = $t['ticket'];
+
+            $e2->type = 'hidden';
+            $e2->name = 'repo_id';
+            $e2->value = $this->id;
+
+            $e3->label = get_string('username', 'repository_boxnet').': ';
+            $e3->id    = 'box_username';
+            $e3->type  = 'text';
+            $e3->name  = 'boxusername';
+            $e3->value = $ret->username;
+            
+            $e4->label = get_string('password', 'repository_boxnet').': ';
+            $e4->id    = 'box_password';
+            $e4->type  = 'password';
+            $e4->name  = 'boxpassword';
+
+            $ret = array();
+            $ret['login'] = array($e1, $e2, $e3, $e4);
+            return $ret;
         }
     }
 
index 5a61f07de6a4505e2d81a8c9ef1efc1edc5940cb..99ff7b598b25469b7feaeab0c12349201ed4d0fb 100755 (executable)
@@ -14,6 +14,44 @@ class repository_flickr extends repository{
     private $flickr;
     public $photos;
 
+    public function __construct($repositoryid, $context = SITEID, $options = array()){
+        global $SESSION, $CFG;
+        $options['page']    = optional_param('p', 1, PARAM_INT);
+        parent::__construct($repositoryid, $context, $options);
+
+        $this->setting = 'flickr_';
+
+        $this->api_key = $this->get_option('api_key');
+        $this->secret  = $this->get_option('secret');
+
+        $this->token = get_user_preferences($this->setting, '');
+        $this->nsid  = get_user_preferences($this->setting.'_nsid', '');
+
+        $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
+
+        if(empty($this->token)){
+            $frob  = optional_param('frob', '', PARAM_RAW);
+            if(!empty($frob)){
+                $auth_info = $this->flickr->auth_getToken($frob);
+                $this->token = $auth_info['token'];
+                $this->nsid  = $auth_info['user']['nsid'];
+                set_user_preference($this->setting, $auth_info['token']);
+                set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
+                $this->perm  = $auth_info['token'];
+            }
+        }
+
+    }
+    public function check_login(){
+        return !empty($this->token);
+    }
+    public function logout(){
+        set_user_preference($this->setting, '');
+        set_user_preference($this->setting.'_nsid', '');
+        $this->token = '';
+        $this->nsid  = '';
+        return $this->print_login();
+    }
     public function set_option($options = array()){
         if (!empty($options['api_key'])) {
             set_config('api_key', trim($options['api_key']), 'flickr');
@@ -48,67 +86,15 @@ class repository_flickr extends repository{
             return true;
         }
     }
-
-    public function __construct($repositoryid, $context = SITEID, $options = array()){
-        global $SESSION, $action, $CFG;
-        $options['page']    = optional_param('p', 1, PARAM_INT);
-        parent::__construct($repositoryid, $context, $options);
-
-        $this->setting = 'flickr_';
-
-        $this->api_key = $this->get_option('api_key');
-        $this->secret  = $this->get_option('secret');
-
-        $this->token = get_user_preferences($this->setting, '');
-        $this->nsid  = get_user_preferences($this->setting.'_nsid', '');
-
-        $this->flickr = new phpFlickr($this->api_key, $this->secret, $this->token);
-
-        $reset = optional_param('reset', 0, PARAM_INT);
-        if(empty($this->token)){
-            $frob  = optional_param('frob', '', PARAM_RAW);
-            if(!empty($frob)){
-                $auth_info = $this->flickr->auth_getToken($frob);
-                $this->token = $auth_info['token'];
-                $this->nsid  = $auth_info['user']['nsid'];
-                set_user_preference($this->setting, $auth_info['token']);
-                set_user_preference($this->setting.'_nsid', $auth_info['user']['nsid']);
-                $this->perm  = $auth_info['token'];
-            }else{
-                $action = 'login';
-            }
-        }
-        if(!empty($reset)) {
-            set_user_preference($this->setting, '');
-            set_user_preference($this->setting.'_nsid', '');
-            $this->token = '';
-            $this->nsid  = '';
-            $action = 'login';
-        }
-
-        if(!empty($this->token)) {
-            if(empty($action)){
-                $action = 'list';
-            }
-        } else {
-            $action = 'login';
-        }
-    }
     public function print_login($ajax = true){
         global $SESSION;
-        if(!empty($this->token)){
-        }
-        if(empty($this->token)) {
-            if($ajax){
-                $ret = array();
-                $popup_btn = new stdclass;
-                $popup_btn->type = 'popup';
-                $popup_btn->url = $this->flickr->auth();
-                $ret['login'] = array($popup_btn);
-                return $ret;
-            }
-        } else {
-            return $this->get_listing();
+        if($ajax){
+            $ret = array();
+            $popup_btn = new stdclass;
+            $popup_btn->type = 'popup';
+            $popup_btn->url = $this->flickr->auth();
+            $ret['login'] = array($popup_btn);
+            return $ret;
         }
     }
     public function get_listing($path = '1', $search = ''){
index 6c4e9a593c1617b6e4ff45593df13eff410bc09e..dd4a10684da912a01d297c2b18c33de3382d144f 100644 (file)
@@ -44,7 +44,7 @@ class repository_flickr_public extends repository{
     }
 
     public function __construct($repositoryid, $context = SITEID, $options = array()){
-        global $action, $CFG;
+        global $CFG;
         $options['page']    = optional_param('p', 1, PARAM_INT);
         parent::__construct($repositoryid, $context, $options);
         $this->api_key = $this->get_option('api_key');
@@ -52,44 +52,32 @@ class repository_flickr_public extends repository{
 
         $this->flickr_account = $this->get_option('email_address');
 
-        if(!empty($this->flickr_account)) {
-            if(empty($action)){
-                $action = 'list';
-            }
-        } else {
-            $account = optional_param('flickr_account', '', PARAM_RAW);
-            if(!empty($account)) {
-                $people = $this->flickr->people_findByEmail($account);
-                if(!empty($people)) {
-                    $this->flickr_account = $account;
-                    $action = 'list';
-                } else {
-                    throw new repository_exception('invalidemail', 'repository_flickr_public');
-                }
+        $account = optional_param('flickr_account', '', PARAM_RAW);
+        if(!empty($account)) {
+            $people = $this->flickr->people_findByEmail($account);
+            if(!empty($people)) {
+                $this->flickr_account = $account;
             } else {
-                $action = 'login';
+                throw new repository_exception('invalidemail', 'repository_flickr_public');
             }
         }
     }
+    public function check_login(){
+        return !empty($this->flickr_account);
+    }
     public function print_login($ajax = true){
-        if(empty($this->flickr_account)) {
-            if($ajax){
-                $ret = array();
-                $e1->label = get_string('username', 'repository_flickr_public').': ';
-                $e1->id    = 'account';
-                $e1->type = 'text';
-                $e1->name = 'flickr_account';
-
-                $e2->type = 'hidden';
-                $e2->name = 'repo_id';
-                $e2->value = $this->id;
-                $ret['login'] = array($e1, $e2);
-                return $ret;
-            }else{
-                echo $str;
-            }
-        } else {
-            return $this->get_listing();
+        if($ajax){
+            $ret = array();
+            $e1->label = get_string('username', 'repository_flickr_public').': ';
+            $e1->id    = 'account';
+            $e1->type = 'text';
+            $e1->name = 'flickr_account';
+
+            $e2->type = 'hidden';
+            $e2->name = 'repo_id';
+            $e2->value = $this->id;
+            $ret['login'] = array($e1, $e2);
+            return $ret;
         }
     }
     public function search(){
index 6d6db042e7b91d58e39a4efddf4f8090df71a65d..3ffbfb49b420e6f8ff63027b911135c299391408 100644 (file)
@@ -838,6 +838,25 @@ abstract class repository {
         return $this->get_listing(null, $search);
     }
 
+    /**
+     * Logout from repository instance
+     * By default, this function will return a login form
+     *
+     * @return string
+     */
+    public function logout(){
+        return $this->print_login();
+    }
+
+    /**
+     * To check whether the user is logged in.
+     *
+     * @return boolean
+     */
+    public function check_login(){
+        return true;
+    }
+
 
     /**
      * Show the login screen, if required
index 939ef4a19c64cf562b53942ee9ed960b09a2ad51..24ebc186ede41d86fb0935c58d490b3e3e89898b 100644 (file)
@@ -106,9 +106,24 @@ EOD;
 
 /// These actions all occur on the currently active repository instance
     switch ($action) {
-        case 'searchform':
-            $repo->print_search();
-            break;
+        case 'sign':
+        case 'list':
+            if ($repo->check_login()) {
+                try {
+                    if (!empty($p)) {
+                        echo json_encode($repo->get_listing($p));
+                    } else {
+                        echo json_encode($repo->get_listing());
+                    }
+                } catch (repository_exception $e) {
+                    $err = new stdclass;
+                    $err->e = $e->getMessage();
+                    die(json_encode($err));
+                }
+                break;
+            } else {
+                $action = 'login';
+            }
         case 'login':
             try {
                 echo json_encode($repo->print_login());
@@ -118,6 +133,12 @@ EOD;
                 die(json_encode($err));
             }
             break;
+        case 'logout':
+            echo json_encode($repo->logout());
+            break;
+        case 'searchform':
+            $repo->print_search();
+            break;
         case 'search':
             try {
                 echo json_encode($repo->search());
@@ -127,19 +148,6 @@ EOD;
                 die(json_encode($err));
             }
             break;
-        case 'list':
-            try {
-                if (!empty($p)) {
-                    echo json_encode($repo->get_listing($p));
-                } else {
-                    echo json_encode($repo->get_listing());
-                }
-            } catch (repository_exception $e) {
-                $err = new stdclass;
-                $err->e = $e->getMessage();
-                die(json_encode($err));
-            }
-            break;
         case 'download':
             $path = $repo->get_file($file, $title);
             $itemid = (int)substr(hexdec(uniqid()), 0, 9)+rand(1,100);