forked from moodle/moodle
/
lib.php
executable file
·270 lines (251 loc) · 8.61 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
require_once($CFG->libdir.'/boxlib.php');
/**
* repository_boxnet class
* This is a subclass of repository class
*
* @since 2.0
* @package repository
* @subpackage boxnet
* @copyright 2009 Dongsheng Cai
* @author Dongsheng Cai <dongsheng@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_boxnet extends repository {
private $box;
/**
* Constructor
* @global object $SESSION
* @param int $repositoryid
* @param object $context
* @param array $options
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
global $SESSION;
$options['username'] = optional_param('boxusername', '', PARAM_RAW);
$options['password'] = optional_param('boxpassword', '', PARAM_RAW);
$options['ticket'] = optional_param('ticket', '', PARAM_RAW);
$sess_name = 'box_token'.$this->id;
$this->sess_name = 'box_token'.$this->id;
parent::__construct($repositoryid, $context, $options);
$this->api_key = $this->get_option('api_key');
// do login
if(!empty($options['username']) && !empty($options['password']) && !empty($options['ticket']) ) {
$this->box = new boxclient($this->api_key);
try {
$SESSION->$sess_name = $this->box->getAuthToken($options['ticket'],
$options['username'], $options['password']);
} catch (repository_exception $e) {
throw $e;
}
}
// already logged
if(!empty($SESSION->$sess_name)) {
if(empty($this->box)) {
$this->box = new boxclient($this->api_key, $SESSION->$sess_name);
}
$this->auth_token = $SESSION->$sess_name;
} else {
$this->box = new boxclient($this->api_key);
}
}
/**
*
* @global object $SESSION
* @return boolean
*/
public function check_login() {
global $SESSION;
return !empty($SESSION->{$this->sess_name});
}
/**
*
* @global object $SESSION
* @return string
*/
public function logout() {
global $SESSION;
unset($SESSION->{$this->sess_name});
return $this->print_login();
}
/**
*
* @param array $options
* @return mixed
*/
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;
}
/**
*
* @param string $config
* @return mixed
*/
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;
}
/**
*
* @global object $SESSION
* @return boolean
*/
public function global_search() {
global $SESSION;
if (empty($SESSION->{$this->sess_name})) {
return false;
} else {
return true;
}
}
/**
*
* @global object $DB
* @return object
*/
public function get_login() {
global $DB;
if ($entry = $DB->get_record('repository_instances', array('id'=>$this->id))) {
$ret->username = $entry->username;
$ret->password = $entry->password;
} else {
$ret->username = '';
$ret->password = '';
}
return $ret;
}
/**
*
* @global object $CFG
* @param string $search_text
* @return mixed
*/
public function search($search_text) {
global $CFG, $OUTPUT;
$list = array();
$ret = array();
$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){
if(strstr(strtolower($v), strtolower($search_text)) !== false) {
$list[] = array('title'=>$v,
'size'=>$filesizes[$n],
'date'=>$filedates[$n],
'source'=>'http://box.net/api/1.0/download/'
.$this->auth_token.'/'.$fileids[$n],
'thumbnail' => $OUTPUT->pix_url(file_extension_icon($v, 32))->out(false));
}
}
}
$ret['list'] = array_filter($list, array($this, 'filter'));
return $ret;
}
/**
*
* @global object $CFG
* @param string $path
* @return mixed
*/
public function get_listing($path = '/', $page = ''){
global $CFG;
$list = array();
$ret = array();
$ret['list'] = array();
$tree = $this->box->getfiletree($path);
$ret['manage'] = 'http://www.box.net/files';
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
if(!empty($tree)) {
$ret['list'] = array_filter($tree, array($this, 'filter'));
}
return $ret;
}
/**
*
* @return array
*/
public function print_login(){
$t = $this->box->getTicket();
$ret = $this->get_login();
if ($this->options['ajax']) {
$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;
$passwd_field->label = get_string('password', 'repository_boxnet').': ';
$passwd_field->id = 'box_password';
$passwd_field->type = 'password';
$passwd_field->name = 'boxpassword';
$ret = array();
$ret['login'] = array($ticket_field, $user_field, $passwd_field);
return $ret;
} 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>';
echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
}
}
/**
* Names of the plugin settings
* @return array
*/
public static function get_type_option_names() {
return array('api_key', 'pluginname');
}
/**
* Add Plugin settings input to Moodle form
* @param object $mform
*/
public function type_config_form($mform) {
parent::type_config_form($mform);
$public_account = get_config('boxnet', 'public_account');
$api_key = get_config('boxnet', 'api_key');
if (empty($api_key)) {
$api_key = '';
}
$strrequired = get_string('required');
$mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'), array('value'=>$api_key,'size' => '40'));
$mform->addRule('api_key', $strrequired, 'required', null, 'client');
$mform->addElement('static', null, '', get_string('information','repository_boxnet'));
}
public function supported_returntypes() {
return FILE_INTERNAL | FILE_EXTERNAL;
}
}