2008-06-27 06:19:40 +00:00
|
|
|
<?php
|
2009-04-22 02:19:24 +00:00
|
|
|
require_once($CFG->libdir.'/boxlib.php');
|
|
|
|
|
2008-06-27 06:19:40 +00:00
|
|
|
/**
|
2008-07-17 03:54:20 +00:00
|
|
|
* repository_boxnet class
|
2008-06-27 06:19:40 +00:00
|
|
|
* This is a subclass of repository class
|
|
|
|
*
|
|
|
|
* @author Dongsheng Cai
|
2008-09-10 05:20:08 +00:00
|
|
|
* @version $Id$
|
2008-06-27 06:19:40 +00:00
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
class repository_boxnet extends repository {
|
2008-07-18 07:40:12 +00:00
|
|
|
private $box;
|
2008-06-27 06:19:40 +00:00
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Constructor
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $SESSION
|
|
|
|
* @param int $repositoryid
|
|
|
|
* @param object $context
|
|
|
|
* @param array $options
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function __construct($repositoryid, $context = SITEID, $options = array()) {
|
2008-09-15 09:31:41 +00:00
|
|
|
global $SESSION;
|
2008-08-02 15:52:14 +00:00
|
|
|
$options['username'] = optional_param('boxusername', '', PARAM_RAW);
|
|
|
|
$options['password'] = optional_param('boxpassword', '', PARAM_RAW);
|
2008-07-07 06:34:39 +00:00
|
|
|
$options['ticket'] = optional_param('ticket', '', PARAM_RAW);
|
2008-08-13 08:35:18 +00:00
|
|
|
$sess_name = 'box_token'.$this->id;
|
2008-09-15 09:31:41 +00:00
|
|
|
$this->sess_name = 'box_token'.$this->id;
|
2009-05-29 09:17:51 +00:00
|
|
|
parent::__construct($repositoryid, $context, $options);
|
|
|
|
$this->api_key = $this->get_option('api_key');
|
2008-07-14 05:31:01 +00:00
|
|
|
// do login
|
2008-09-16 03:11:17 +00:00
|
|
|
if(!empty($options['username']) && !empty($options['password']) && !empty($options['ticket']) ) {
|
2008-08-13 08:35:18 +00:00
|
|
|
$this->box = new boxclient($this->api_key);
|
2008-09-16 02:09:35 +00:00
|
|
|
try {
|
2008-08-13 08:35:18 +00:00
|
|
|
$SESSION->$sess_name = $this->box->getAuthToken($options['ticket'],
|
2008-09-16 03:11:17 +00:00
|
|
|
$options['username'], $options['password']);
|
2008-07-23 04:43:53 +00:00
|
|
|
} catch (repository_exception $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
2008-07-07 06:34:39 +00:00
|
|
|
// already logged
|
2008-09-16 03:11:17 +00:00
|
|
|
if(!empty($SESSION->$sess_name)) {
|
|
|
|
if(empty($this->box)) {
|
2008-08-13 08:35:18 +00:00
|
|
|
$this->box = new boxclient($this->api_key, $SESSION->$sess_name);
|
2008-07-18 07:40:12 +00:00
|
|
|
}
|
2008-08-13 08:35:18 +00:00
|
|
|
$this->auth_token = $SESSION->$sess_name;
|
2008-06-27 06:19:40 +00:00
|
|
|
} else {
|
2008-08-13 08:35:18 +00:00
|
|
|
$this->box = new boxclient($this->api_key);
|
2008-09-15 09:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $SESSION
|
|
|
|
* @return boolean
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function check_login() {
|
2008-09-15 09:31:41 +00:00
|
|
|
global $SESSION;
|
|
|
|
return !empty($SESSION->{$this->sess_name});
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $SESSION
|
|
|
|
* @return string
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function logout() {
|
2008-09-15 09:31:41 +00:00
|
|
|
global $SESSION;
|
|
|
|
unset($SESSION->{$this->sess_name});
|
|
|
|
return $this->print_login();
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @param array $options
|
|
|
|
* @return mixed
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function set_option($options = array()) {
|
2008-09-15 09:31:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @param string $config
|
|
|
|
* @return mixed
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function get_option($config = '') {
|
2008-09-16 03:11:17 +00:00
|
|
|
if($config==='api_key') {
|
2008-09-15 09:31:41 +00:00
|
|
|
return trim(get_config('boxnet', 'api_key'));
|
|
|
|
} else {
|
|
|
|
$options['api_key'] = trim(get_config('boxnet', 'api_key'));
|
|
|
|
}
|
|
|
|
$options = parent::get_option($config);
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $SESSION
|
|
|
|
* @return boolean
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function global_search() {
|
2008-09-15 09:31:41 +00:00
|
|
|
global $SESSION;
|
2008-09-16 03:11:17 +00:00
|
|
|
if (empty($SESSION->{$this->sess_name})) {
|
2008-09-15 09:31:41 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $DB
|
|
|
|
* @return object
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 02:09:35 +00:00
|
|
|
public function get_login() {
|
2008-07-28 08:24:55 +00:00
|
|
|
global $DB;
|
2008-08-27 04:17:35 +00:00
|
|
|
if ($entry = $DB->get_record('repository_instances', array('id'=>$this->id))) {
|
2008-07-28 08:24:55 +00:00
|
|
|
$ret->username = $entry->username;
|
|
|
|
$ret->password = $entry->password;
|
2008-07-31 02:51:28 +00:00
|
|
|
} else {
|
|
|
|
$ret->username = '';
|
|
|
|
$ret->password = '';
|
2008-07-28 08:24:55 +00:00
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $CFG
|
|
|
|
* @param string $search_text
|
|
|
|
* @return mixed
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 03:11:17 +00:00
|
|
|
public function search($search_text) {
|
|
|
|
global $CFG;
|
2008-07-07 06:34:39 +00:00
|
|
|
$list = array();
|
|
|
|
$ret = array();
|
2008-09-16 03:11:17 +00:00
|
|
|
$tree = $this->box->getAccountTree();
|
|
|
|
if (!empty($tree)) {
|
|
|
|
$filenames = $tree['file_name'];
|
|
|
|
$fileids = $tree['file_id'];
|
|
|
|
$filesizes = $tree['file_size'];
|
|
|
|
$filedates = $tree['file_date'];
|
|
|
|
$fileicon = $tree['thumbnail'];
|
|
|
|
foreach ($filenames as $n=>$v){
|
2009-04-22 04:53:26 +00:00
|
|
|
if(strstr(strtolower($v), strtolower($search_text)) !== false) {
|
2008-09-16 03:11:17 +00:00
|
|
|
$list[] = array('title'=>$v,
|
|
|
|
'size'=>$filesizes[$n],
|
|
|
|
'date'=>$filedates[$n],
|
|
|
|
'source'=>'http://box.net/api/1.0/download/'
|
|
|
|
.$this->options['auth_token'].'/'.$fileids[$n],
|
2009-06-04 09:33:25 +00:00
|
|
|
'thumbnail'=>$CFG->httpswwwroot.'/pix/f/'.mimeinfo('icon32', $v));
|
2008-09-08 06:20:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-25 03:46:49 +00:00
|
|
|
$ret['list'] = array_filter($list, array($this, 'filter'));
|
2008-09-16 03:11:17 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
2008-09-18 05:33:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @global object $CFG
|
|
|
|
* @param string $path
|
|
|
|
* @return mixed
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2009-02-18 06:52:54 +00:00
|
|
|
public function get_listing($path = '/', $page = ''){
|
2008-09-16 03:11:17 +00:00
|
|
|
global $CFG;
|
|
|
|
$list = array();
|
|
|
|
$ret = array();
|
|
|
|
$ret['list'] = array();
|
2008-09-01 03:07:16 +00:00
|
|
|
$tree = $this->box->getfiletree($path);
|
2008-09-16 03:11:17 +00:00
|
|
|
$ret['manage'] = 'http://www.box.net/files';
|
|
|
|
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
|
|
|
|
if(!empty($tree)) {
|
2009-02-25 03:46:49 +00:00
|
|
|
$ret['list'] = array_filter($tree, array($this, 'filter'));
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
2008-09-16 03:11:17 +00:00
|
|
|
return $ret;
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
|
|
|
*
|
2009-04-22 02:19:24 +00:00
|
|
|
* @return array
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-16 03:11:17 +00:00
|
|
|
public function print_login(){
|
2008-09-15 09:31:41 +00:00
|
|
|
$t = $this->box->getTicket();
|
|
|
|
$ret = $this->get_login();
|
|
|
|
if ($this->options['ajax']) {
|
2008-09-15 09:42:38 +00:00
|
|
|
$ticket_field->type = 'hidden';
|
|
|
|
$ticket_field->name = 'ticket';
|
|
|
|
$ticket_field->value = $t['ticket'];
|
|
|
|
|
|
|
|
$user_field->label = get_string('username', 'repository_boxnet').': ';
|
|
|
|
$user_field->id = 'box_username';
|
|
|
|
$user_field->type = 'text';
|
|
|
|
$user_field->name = 'boxusername';
|
|
|
|
$user_field->value = $ret->username;
|
2008-09-15 09:31:41 +00:00
|
|
|
|
2008-09-15 09:42:38 +00:00
|
|
|
$passwd_field->label = get_string('password', 'repository_boxnet').': ';
|
|
|
|
$passwd_field->id = 'box_password';
|
|
|
|
$passwd_field->type = 'password';
|
|
|
|
$passwd_field->name = 'boxpassword';
|
2008-09-15 09:31:41 +00:00
|
|
|
|
|
|
|
$ret = array();
|
2008-09-15 09:42:38 +00:00
|
|
|
$ret['login'] = array($ticket_field, $user_field, $passwd_field);
|
2008-09-15 09:31:41 +00:00
|
|
|
return $ret;
|
2009-06-04 09:33:25 +00:00
|
|
|
} else {
|
|
|
|
echo '<table>';
|
|
|
|
echo '<tr><td><label>'.get_string('username', 'repository_boxnet').'</label></td>';
|
|
|
|
echo '<td><input type="text" name="boxusername" /></td></tr>';
|
|
|
|
echo '<tr><td><label>'.get_string('password', 'repository_boxnet').'</label></td>';
|
|
|
|
echo '<td><input type="password" name="boxpassword" /></td></tr>';
|
|
|
|
echo '<input type="hidden" name="ticket" value="'.$t['ticket'].'" />';
|
|
|
|
echo '</table>';
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Names of the plugin settings
|
2009-04-22 02:19:24 +00:00
|
|
|
* @return array
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-18 03:19:52 +00:00
|
|
|
public static function get_type_option_names() {
|
2008-09-02 08:58:53 +00:00
|
|
|
return array('api_key');
|
|
|
|
}
|
|
|
|
|
2008-09-18 05:33:44 +00:00
|
|
|
/**
|
2008-09-18 05:43:38 +00:00
|
|
|
* Add Plugin settings input to Moodle form
|
2009-04-22 02:19:24 +00:00
|
|
|
* @param object $mform
|
2008-09-18 05:33:44 +00:00
|
|
|
*/
|
2008-09-18 03:06:10 +00:00
|
|
|
public function type_config_form(&$mform) {
|
2008-09-02 08:58:53 +00:00
|
|
|
$public_account = get_config('boxnet', 'public_account');
|
2008-08-20 07:26:10 +00:00
|
|
|
$api_key = get_config('boxnet', 'api_key');
|
|
|
|
if (empty($api_key)) {
|
|
|
|
$api_key = '';
|
|
|
|
}
|
2008-08-13 04:09:13 +00:00
|
|
|
$strrequired = get_string('required');
|
2008-09-02 08:58:53 +00:00
|
|
|
$mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'), array('value'=>$api_key,'size' => '40'));
|
2008-08-13 04:09:13 +00:00
|
|
|
$mform->addRule('api_key', $strrequired, 'required', null, 'client');
|
2008-09-17 06:09:10 +00:00
|
|
|
$mform->addElement('static', null, '', get_string('information','repository_boxnet'));
|
2008-08-13 04:09:13 +00:00
|
|
|
}
|
2008-06-27 06:19:40 +00:00
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
?>
|