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);
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'])
} catch (repository_exception $e) {
throw $e;
}
- if ($SESSION->$sess_name) {
- $action = 'list';
- } else {
- $action = 'login';
- }
}
// already logged
if(!empty($SESSION->$sess_name)) {
$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;
}
}
}
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;
}
}
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');
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 = ''){
}
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');
$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(){
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
/// 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());
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());
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);