]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15488
authordongsheng <dongsheng>
Thu, 11 Sep 2008 03:18:54 +0000 (03:18 +0000)
committerdongsheng <dongsheng>
Thu, 11 Sep 2008 03:18:54 +0000 (03:18 +0000)
Use a popup login window intead inline login form
TODO:
Find a grace way to verify token

lang/en_utf8/repository.php
repository/flickr/phpFlickr.php
repository/flickr/repository.class.php
repository/lib.php
repository/ws.php

index 96965ea31dc144face318647131f7cfda7aa4480..988fcb37b0e06ecb19d50007da6b9f677dc7b185 100644 (file)
@@ -60,6 +60,7 @@ $string['openpicker'] = 'Choose a file...';
 $string['personalrepositories'] = 'Personal repositories';
 $string['plugin'] = 'Repository plug-ins';
 $string['preview'] = 'Preview';
+$string['popup'] = 'Open a popup window to login';
 $string['refresh'] = 'Refresh';
 $string['removed'] = 'Repository removed';
 $string['repository'] = 'Repository';
index 63b198cde4ff0b6b178f7d0cf3f8f00d319e6370..e4efd4cbe901edbe1d77fa7e9747921b4d5729c6 100755 (executable)
@@ -1,4 +1,15 @@
 <?php
+/**
+ * Modified by Dongsheng Cai <dongsheng@cvs.moodle.org>
+ * ChangeLog:
+ *   1. Remove PEAR HTTP LIB, use curl.class.php (created by myself)
+ *   2. Remove PEAR DB LIB
+ *   3. Remove all cache code, it will implement in curl class.
+ *   4. Clean up session code
+ *
+ * @version $Id$
+ *
+ */
 /* phpFlickr Class 2.2.0
  * Written by Dan Coulter (dan@dancoulter.com)
  * Sourceforge Project Page: http://www.sourceforge.net/projects/phpflickr/
  *     http://sourceforge.net/forum/forum.php?forum_id=469652
  *
  */
-/**
- * Modified by Dongsheng Cai <dongsheng@cvs.moodle.org>
- * ChangeLog:
- *   1. Remove PEAR HTTP LIB, use curl.class.php (created by myself)
- *   2. Remove PEAR DB LIB
- *   3. Remove all cache code, it will implement in curl class.
- *   4. Clean up session code
- *
- */
 
 class phpFlickr {
     var $api_key;
@@ -50,15 +52,16 @@ class phpFlickr {
      * of your table.
      */
 
-    function __construct ($api_key, $secret = NULL, $die_on_error = false)
+    function __construct ($api_key, $secret = NULL, $token = '')
     {
         global $CFG;
         //The API Key must be set before any calls can be made.  You can
         //get your own at http://www.flickr.com/services/api/misc.api_keys.html
         $this->api_key = $api_key;
         $this->secret = $secret;
-        $this->die_on_error = $die_on_error;
+        $this->die_on_error = false;
         $this->service = "flickr";
+        $this->token = $token;
         //Find the PHP version and store it for future reference
         $this->php_version = explode("-", phpversion());
         $this->php_version = explode(".", $this->php_version[0]);
@@ -67,7 +70,6 @@ class phpFlickr {
 
     function request ($command, $args = array())
     {
-        global $SESSION;
         //Sends a request to Flickr's REST endpoint via POST.
         if (substr($command,0,7) != "flickr.") {
             $command = "flickr." . $command;
@@ -77,8 +79,8 @@ class phpFlickr {
         $args = array_merge(array("method" => $command, "format" => "php_serial", "api_key" => $this->api_key), $args);
         if (!empty($this->token)) {
             $args = array_merge($args, array("auth_token" => $this->token));
-        } elseif (!empty($SESSION->phpFlickr_auth_token)) {
-            $args = array_merge($args, array("auth_token" => $SESSION->phpFlickr_auth_token));
+        } elseif (!empty($this->token)) {
+            $args = array_merge($args, array("auth_token" => $this->token));
         }
         ksort($args);
         $auth_sig = "";
@@ -187,35 +189,15 @@ class phpFlickr {
 
     function auth ($perms = "read", $remember_uri = true)
     {
-        global $SESSION;
         // Redirects to Flickr's authentication piece if there is no valid token.
         // If remember_uri is set to false, the callback script (included) will
         // redirect to its default page.
-
-        if (empty($SESSION->phpFlickr_auth_token) && empty($this->token)) {
-            if ($remember_uri) {
-                $redirect = $_SERVER['REQUEST_URI'];
-            }
-            //$api_sig = md5($this->secret . "api_key" . $this->api_key . "extra" . $redirect . "perms" . $perms);
-            $api_sig = md5($this->secret . "api_key" . $this->api_key . "perms" . $perms);
-            if ($this->service == "23") {
-                header("Location: http://www.23hq.com/services/auth/?api_key=" . $this->api_key . "&extra=" . $redirect . "&perms=" . $perms . "&api_sig=". $api_sig);
-            } else {
-                $url = 'http://www.flickr.com/services/auth/?api_key=' . $this->api_key . "&perms=" .  $perms . '&api_sig='. $api_sig;
-                echo '<a href="'.$url.'">Authentication</a>';
-            }
-            //exit;
-        } else {
-            $tmp = $this->die_on_error;
-            $this->die_on_error = false;
-            $rsp = $this->auth_checkToken();
-            if ($this->error_code !== false) {
-                unset($SESSION->phpFlickr_auth_token);
-                $this->auth($perms, $remember_uri);
-            }
-            $this->die_on_error = $tmp;
-            return $rsp['perms'];
+        if ($remember_uri) {
+            $redirect = $_SERVER['REQUEST_URI'];
         }
+        $api_sig = md5($this->secret . "api_key" . $this->api_key . "perms" . $perms);
+        $url = 'http://www.flickr.com/services/auth/?api_key=' . $this->api_key . "&perms=" .  $perms . '&api_sig='. $api_sig;
+        return $url;
     }
 
     /*******************************
@@ -279,10 +261,9 @@ class phpFlickr {
 
     function auth_getToken ($frob)
     {
-        global $SESSION;
         /* http://www.flickr.com/services/api/flickr.auth.getToken.html */
         $this->request('flickr.auth.getToken', array('frob'=>$frob));
-        $SESSION->phpFlickr_auth_token = $this->parsed_response['auth']['token'];
+        $this->token = $this->parsed_response['auth']['token'];
         return $this->parsed_response ? $this->parsed_response['auth'] : false;
     }
 
index bb410e06cadb687c9142239db271cfa63d878617..8f176c6687ba2f93ccc693b602933362b279aeb4 100755 (executable)
@@ -35,8 +35,7 @@ class repository_flickr extends repository{
 
     public function global_search(){
         global $SESSION;
-        $sess_name = 'flickrmail'.$this->id;
-        if (empty($SESSION->$sess_name)) {
+        if (empty($this->token)) {
             return false;
         } else {
             return true;
@@ -47,86 +46,60 @@ class repository_flickr extends repository{
         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');
-        if (empty($this->api_key)) {
-        }
-        $this->flickr = new phpFlickr($this->api_key);
+        //TODO: put secret into database
+        $this->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);
-        $sess_name = 'flickrmail'.$this->id;
+        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)) {
-            // logout from flickr
-            unset($SESSION->$sess_name);
-            set_user_preference('flickrmail'.$this->id, '');
+            set_user_preference($this->setting, '');
+            set_user_preference($this->setting.'_nsid', '');
+            $this->token = '';
+            $this->nsid  = '';
+            $action = 'login';
         }
 
-        if(!empty($SESSION->$sess_name)) {
-            if(empty($action)) {
+        if(!empty($this->token)) {
+            if(empty($action)){
                 $action = 'list';
             }
         } else {
-            // get flickr account
-            $account = optional_param('flickrmail', '', PARAM_RAW);
-            if(!empty($account)) {
-                $people = $this->flickr->people_findByEmail($account);
-                if(!empty($people)) {
-                    $remember = optional_param('remember', '', PARAM_RAW);
-                    if(!empty($remember)) {
-                        set_user_preference('flickrmail'.$this->id, $account);
-                    }
-                    $SESSION->$sess_name = $account;
-                    if (empty($account)) {
-                        $action = 'list';
-                    } else {
-                        $action = 'login';
-                    }
-                } else {
-                    throw new repository_exception('invalidemail', 'repository_flickr');
-                }
-            } else {
-                if($account = get_user_preferences('flickrmail'.$this->id, '')){
-                    $SESSION->$sess_name = $account;
-                    if(empty($action)) {
-                        $action = 'list';
-                    }
-                } else {
-                    $action = 'login';
-                }
-            }
+            $action = 'login';
         }
     }
     public function print_login($ajax = true){
         global $SESSION;
-        $sess_name = 'flickrmail'.$this->id;
-        if(empty($SESSION->$sess_name)) {
-        $str =<<<EOD
-<form id="moodle-repo-login">
-<label for="account">Account (Email)</label><br/>
-<input type='text' name='flickrmail' id='account' />
-<input type='hidden' name='id' value='$this->id' /><br/>
-<input type='checkbox' name='remember' id="keepid" value='true' /> <label for="keepid">Remember? </label>
-<p><input type='button' onclick="repository_client.login()" value="Go" /></p>
-</form>
-EOD;
+        if(!empty($this->token)){
+        }
+        if(empty($this->token)) {
             if($ajax){
                 $ret = array();
-                $e1->label = get_string('username', 'repository_flickr').': ';
-                $e1->id    = 'account';
-                $e1->type = 'text';
-                $e1->name = 'flickrmail';
-
-                $e2->id   = 'keepid';
-                $e2->label = get_string('remember', 'repository_flickr').' ';
-                $e2->type = 'checkbox';
-                $e2->name = 'remember';
-
-                $e3->type = 'hidden';
-                $e3->name = 'repo_id';
-                $e3->value = $this->id;
-                $ret['login'] = array($e1, $e2, $e3);
+                $popup_btn = new stdclass;
+                $popup_btn->type = 'popup';
+                $popup_btn->url = $this->flickr->auth();
+                $ret['login'] = array($popup_btn);
                 return $ret;
-            }else{
-                echo $str;
             }
         } else {
             return $this->get_listing();
@@ -134,17 +107,24 @@ EOD;
     }
     public function get_listing($path = '1', $search = ''){
         global $SESSION;
-        $sess_name = 'flickrmail'.$this->id;
-        $people = $this->flickr->people_findByEmail($SESSION->$sess_name);
-        $photos_url = $this->flickr->urls_getUserPhotos($people['nsid']);
+        $nsid = get_user_preferences($this->setting.'_nsid', '');
+        $photos_url = $this->flickr->urls_getUserPhotos($nsid);
 
         if(!empty($search)) {
-            // do searching, if $path is not empty, ignore it.
-            $photos = $this->flickr->photos_search(array('user_id'=>$people['nsid'], 'text'=>$search));
+            $photos = $this->flickr->photos_search(array(
+                'per_page'=>25,
+                'page'=>$path,
+                'extras'=>'original_format',
+                'text'=>$search
+                ));
         } elseif(!empty($path) && empty($search)) {
-            $photos = $this->flickr->people_getPublicPhotos($people['nsid'], 'original_format', 25, $path);
+            $photos = $this->flickr->photos_search(array(
+                'user_id'=>$nsid,
+                'per_page'=>25,
+                'page'=>$path,
+                'extras'=>'original_format'
+                ));
         }
-
         $ret = array();
         $ret['manage'] = $photos_url;
         $ret['list']  = array();
@@ -154,17 +134,19 @@ EOD;
         } else {
             $ret['page'] = 1;
         }
-        foreach ($photos['photo'] as $p) {
-            if(empty($p['title'])) {
-                $p['title'] = get_string('notitle', 'repository_flickr');
-            }
-            if (isset($p['originalformat'])) {
-                $format = $p['originalformat'];
-            } else {
-                $format = 'jpg';
+        if(!empty($photos['photo'])){
+            foreach ($photos['photo'] as $p) {
+                if(empty($p['title'])) {
+                    $p['title'] = get_string('notitle', 'repository_flickr');
+                }
+                if (isset($p['originalformat'])) {
+                    $format = $p['originalformat'];
+                } else {
+                    $format = 'jpg';
+                }
+                $ret['list'][] =
+                    array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
             }
-            $ret['list'][] =
-                array('title'=>$p['title'].'.'.$format,'source'=>$p['id'],'id'=>$p['id'],'thumbnail'=>$this->flickr->buildPhotoURL($p, 'Square'), 'date'=>'', 'size'=>'unknown', 'url'=>$photos_url.$p['id']);
         }
         if(empty($ret)) {
             throw new repository_exception('nullphotolist', 'repository_flickr');
@@ -189,19 +171,6 @@ EOD;
                 $str .= "<br/>";
             }
         }
-        $str .= <<<EOD
-<style type='text/css'>
-#paging{margin-top: 10px; clear:both}
-#paging a{padding: 4px; border: 1px solid gray}
-</style>
-EOD;
-        $str .= '<div id="paging">';
-        for($i=1; $i <= $this->photos['pages']; $i++) {
-            $str .= '<a href="###" onclick="cr('.$this->id.', '.$i.', 0)">';
-            $str .= $i;
-            $str .= '</a> ';
-        }
-        $str .= '</div>';
         echo $str;
     }
     public function print_search(){
@@ -244,7 +213,7 @@ EOD;
     }
 
     public static function has_multiple_instances() {
-        return true;
+        return false;
     }
 
     public static function has_instance_config() {
index da57f4c48309960ba5581711e7bf0c6c86e1ae06..fabaad325e81a2153fd9641b2753a4410bb38849 100644 (file)
@@ -1209,6 +1209,7 @@ function repository_get_client($context){
     $strsearching = get_string('searching', 'repository');
     $strsubmit    = get_string('submit', 'repository');
     $strpreview   = get_string('preview', 'repository');
+    $strpopup     = get_string('popup', 'repository');
     $strupload    = get_string('upload', 'repository');
     $struploading = get_string('uploading', 'repository');
     $css = '';
@@ -1243,6 +1244,8 @@ function repository_get_client($context){
 .fp-upload-btn a:hover {background: grey;color:white}
 .fp-paging{margin:10px 5px; clear:both;text-align:center}
 .fp-paging a{padding: 4px;border: 1px solid #CCC}
+.fp-popup{text-align:center}
+.fp-popup a{font-size: 24px}
 .fp-grid{width:80px; float:left;text-align:center;}
 .fp-grid div{width: 80px; overflow: hidden}
 .fp-grid p{margin:0;padding:0;background: #FFFFCC}
@@ -1508,11 +1511,13 @@ _client.print_login = function(){
     var panel = new YAHOO.util.Element('panel-$suffix');
     var data = _client.ds.login;
     var str = '';
+    var has_pop = false;
     for(var k in data){
-        str += '<p>';
         if(data[k].type=='popup'){
-            str += '<a href="###" onclick="repository_client_$suffix.popup(\''+data[k].url+'\')">test</a>';
+            str += '<p class="fp-popup"><a href="###" onclick="repository_client_$suffix.popup(\''+data[k].url+'\')">$strpopup</a></p>';
+            has_pop = true;
         }else{
+            str += '<p>';
             var lable_id = '';
             var field_id = '';
             var field_value = '';
@@ -1527,10 +1532,12 @@ _client.print_login = function(){
                 field_value = ' value="'+data[k].value+'"';
             }
             str += '<input type="'+data[k].type+'"'+' name="'+data[k].name+'"'+field_id+field_value+' />';
+            str += '</p>';
         }
-        str += '</p>';
     }
-    str += '<p><input type="button" onclick="repository_client_$suffix.login()" value="$strsubmit" /></p>';
+    if(!has_pop){
+        str += '<p><input type="button" onclick="repository_client_$suffix.login()" value="$strsubmit" /></p>';
+    }
     panel.get('element').innerHTML = str;
 }
 
index a77e49962cb6972075d91c38e2995d8ca9d68384..2dba41965ae49530a5c021fcf43f70e1ded1b91d 100644 (file)
@@ -15,6 +15,7 @@ $file  = optional_param('file', '', PARAM_RAW);
 $title = optional_param('title', '', PARAM_FILE);
 $action = optional_param('action', '', PARAM_ALPHA);
 $search = optional_param('s', '', PARAM_CLEANHTML);
+$callback = optional_param('callback', '', PARAM_CLEANHTML);
 // repository ID
 $repo_id = optional_param('repo_id', 1, PARAM_INT);
 $ctx_id  = optional_param('ctx_id', SITEID, PARAM_INT);
@@ -95,6 +96,21 @@ if(file_exists($CFG->dirroot.'/repository/'.
     die(json_encode($err));
 }
 
+if (!empty($callback)) {
+    // call opener window to refresh repository
+    // the callback url should be something like this:
+    // http://xx.moodle.com/repository/ws.php?callback=yes&repo_id=1&sid=xxx
+    // sid is the attached auth token from external source
+    $js  =<<<EOD
+<html><head><script type="text/javascript">
+window.opener.repository_callback($repo_id);
+window.close();
+</script><body></body></html>
+EOD;
+    echo $js;
+    die;
+}
+
 switch ($action) {
 case 'login':
     try {
@@ -105,17 +121,6 @@ case 'login':
         die(json_encode($err));
     }
     break;
-case 'callback':
-    // call opener window to refresh repository
-    // the callback url should be something like this:
-    // http://xx.moodle.com/repository/ws.php?action=callback&repo_id=1&sid=xxx
-    // sid is the attached auth token from external source
-    $js  =<<<EOD
-<html><head><script type="text/javascript">
-window.opener.repository_callback($repo_id);
-</script><body></body></html>
-EOD;
-    break;
 case 'list':
 case 'search':
     try {