$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';
<?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;
* 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]);
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;
$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 = "";
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;
}
/*******************************
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;
}
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;
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();
}
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();
} 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');
$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(){
}
public static function has_multiple_instances() {
- return true;
+ return false;
}
public static function has_instance_config() {
$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 = '';
.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}
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 = '';
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;
}
$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);
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 {
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 {