diff --git a/lang/en_utf8/repository.php b/lang/en_utf8/repository.php index 96965ea31dc..988fcb37b0e 100644 --- a/lang/en_utf8/repository.php +++ b/lang/en_utf8/repository.php @@ -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'; diff --git a/repository/flickr/phpFlickr.php b/repository/flickr/phpFlickr.php index 63b198cde4f..e4efd4cbe90 100755 --- a/repository/flickr/phpFlickr.php +++ b/repository/flickr/phpFlickr.php @@ -1,4 +1,15 @@ + * 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/ @@ -14,15 +25,6 @@ * http://sourceforge.net/forum/forum.php?forum_id=469652 * */ -/** - * Modified by Dongsheng Cai - * 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 'Authentication'; - } - //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; } diff --git a/repository/flickr/repository.class.php b/repository/flickr/repository.class.php index bb410e06cad..8f176c6687b 100755 --- a/repository/flickr/repository.class.php +++ b/repository/flickr/repository.class.php @@ -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; + 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(!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']); } - 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']); } if(empty($ret)) { throw new repository_exception('nullphotolist', 'repository_flickr'); @@ -189,19 +171,6 @@ EOD; $str .= "
"; } } - $str .= << -#paging{margin-top: 10px; clear:both} -#paging a{padding: 4px; border: 1px solid gray} - -EOD; - $str .= '
'; - for($i=1; $i <= $this->photos['pages']; $i++) { - $str .= ''; - $str .= $i; - $str .= ' '; - } - $str .= '
'; 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() { diff --git a/repository/lib.php b/repository/lib.php index da57f4c4830..fabaad325e8 100644 --- a/repository/lib.php +++ b/repository/lib.php @@ -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 += '

'; if(data[k].type=='popup'){ - str += 'test'; + str += '

$strpopup

'; + has_pop = true; }else{ + str += '

'; var lable_id = ''; var field_id = ''; var field_value = ''; @@ -1527,10 +1532,12 @@ _client.print_login = function(){ field_value = ' value="'+data[k].value+'"'; } str += ''; + str += '

'; } - str += '

'; } - str += '

'; + if(!has_pop){ + str += '

'; + } panel.get('element').innerHTML = str; } diff --git a/repository/ws.php b/repository/ws.php index a77e49962cb..2dba41965ae 100644 --- a/repository/ws.php +++ b/repository/ws.php @@ -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; + 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; - break; case 'list': case 'search': try {