3 * repository_boxnet class
4 * This is a subclass of repository class
6 * @author Dongsheng Cai
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 require_once($CFG->dirroot.'/repository/boxnet/'.'boxlibphp5.php');
13 class repository_boxnet extends repository{
16 public function __construct($repositoryid, $context = SITEID, $options = array()){
17 global $SESSION, $action;
18 $options['username'] = optional_param('username', '', PARAM_RAW);
19 $options['password'] = optional_param('password', '', PARAM_RAW);
20 $options['ticket'] = optional_param('ticket', '', PARAM_RAW);
21 $options['api_key'] = 'dmls97d8j3i9tn7av8y71m9eb55vrtj4';
23 $reset = optional_param('reset', 0, PARAM_INT);
25 unset($SESSION->box_token);
28 if(!empty($options['username'])
29 && !empty($options['password'])
30 && !empty($options['ticket']) )
32 $this->box = new boxclient($options['api_key']);
34 $SESSION->box_token = $this->box->getAuthToken($options['ticket'],
35 $options['username'], $options['password']);
36 } catch (repository_exception $e) {
41 if(!empty($SESSION->box_token)) {
42 if(empty($this->box)) {
43 $this->box = new boxclient($options['api_key'], $SESSION->box_token);
45 $options['auth_token'] = $SESSION->box_token;
50 $this->box = new boxclient($options['api_key']);
55 parent::__construct($repositoryid, $context, $options);
58 public function get_listing($path = '/', $search = ''){
62 $tree = $this->box->getAccountTree();
64 $filenames = $tree['file_name'];
65 $fileids = $tree['file_id'];
66 $filesizes = $tree['file_size'];
67 $filedates = $tree['file_date'];
68 foreach ($filenames as $n=>$v){
71 if(strstr($v, $search) !== false) {
72 $list[] = array('title'=>$v,
73 'size'=>$filesizes[$n],
74 'date'=>$filedates[$n],
75 'source'=>'http://box.net/api/1.0/download/'
76 .$this->options['auth_token'].'/'.$fileids[$n],
77 'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
80 $list[] = array('title'=>$v,
81 'size'=>$filesizes[$n],
82 'date'=>$filedates[$n],
83 'source'=>'http://box.net/api/1.0/download/'
84 .$this->options['auth_token'].'/'.$fileids[$n],
85 'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', $v));
88 $this->listing = $list;
92 throw new repository_exception('nullfilelist', 'repository');
96 public function print_login(){
97 if(!empty($this->options['auth_token'])) {
98 if($this->options['ajax']){
99 return $this->get_listing();
101 // format file list and
105 $t = $this->box->getTicket();
106 if(empty($this->options['auth_token'])) {
108 $str .= '<form id="moodle-repo-login">';
109 $str .= '<input type="hidden" name="ticket" value="'.
111 $str .= '<input type="hidden" name="id" value="'.$this->repositoryid.'" />';
112 $str .= '<label for="box_username">Username: <label><br/>';
113 $str .= '<input type="text" id="box_username" name="username" />';
115 $str .= '<label for="box_password">Password: <label><br/>';
116 $str .= '<input type="password" id="box_password" name="password" /><br/>';
117 $str .= '<input type="button" onclick="repository_client.login()" value="Go" />';
119 if($this->options['ajax']){
130 public function print_search(){