mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
lib.php
181 lines (168 loc) · 6.59 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
<?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/>.
/**
* This plugin is used to access user's private files
*
* @since 2.0
* @package repository_user
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/repository/lib.php');
/**
* repository_user class is used to browse user private files
*
* @since 2.0
* @package repository_user
* @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_user extends repository {
/**
* user plugin doesn't require login
*
* @return mixed
*/
public function print_login() {
return $this->get_listing();
}
/**
* Get file listing
*
* @param string $encodedpath
* @return mixed
*/
public function get_listing($encodedpath = '', $page = '') {
global $CFG, $USER, $OUTPUT;
$ret = array();
$ret['dynload'] = true;
$ret['nosearch'] = true;
$ret['nologin'] = true;
$manageurl = new moodle_url('/user/files.php');
$ret['manage'] = $manageurl->out();
$list = array();
if (!empty($encodedpath)) {
$params = unserialize(base64_decode($encodedpath));
if (is_array($params)) {
$filepath = clean_param($params['filepath'], PARAM_PATH);
$filename = clean_param($params['filename'], PARAM_FILE);
}
} else {
$itemid = 0;
$filepath = '/';
$filename = null;
}
$filearea = 'private';
$component = 'user';
$itemid = 0;
$context = context_user::instance($USER->id);
try {
$browser = get_file_browser();
if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) {
$pathnodes = array();
$level = $fileinfo;
$params = $fileinfo->get_params();
while ($level && $params['component'] == 'user' && $params['filearea'] == 'private') {
$encodedpath = base64_encode(serialize($level->get_params()));
$pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath);
$level = $level->get_parent();
$params = $level->get_params();
}
$ret['path'] = array_reverse($pathnodes);
// build file tree
$children = $fileinfo->get_children();
foreach ($children as $child) {
if ($child->is_directory()) {
$encodedpath = base64_encode(serialize($child->get_params()));
$node = array(
'title' => $child->get_visible_name(),
'datemodified' => $child->get_timemodified(),
'datecreated' => $child->get_timecreated(),
'path' => $encodedpath,
'children'=>array(),
'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)
);
$list[] = $node;
} else {
$encodedpath = base64_encode(serialize($child->get_params()));
$node = array(
'title' => $child->get_visible_name(),
'size' => $child->get_filesize(),
'datemodified' => $child->get_timemodified(),
'datecreated' => $child->get_timecreated(),
'author' => $child->get_author(),
'license' => $child->get_license(),
'isref' => $child->is_external_file(),
'source'=> $encodedpath,
'icon' => $OUTPUT->pix_url(file_file_icon($child, 24))->out(false),
'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)
);
if ($child->get_status() == 666) {
$node['originalmissing'] = true;
}
if ($imageinfo = $child->get_imageinfo()) {
$fileurl = new moodle_url($child->get_url());
$node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified()));
$node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified()));
$node['image_width'] = $imageinfo['width'];
$node['image_height'] = $imageinfo['height'];
}
$list[] = $node;
}
}
}
} catch (Exception $e) {
throw new repository_exception('emptyfilelist', 'repository_user');
}
$ret['list'] = $list;
$ret['list'] = array_filter($list, array($this, 'filter'));
return $ret;
}
/**
* Does this repository used to browse moodle files?
*
* @return boolean
*/
public function has_moodle_files() {
return true;
}
/**
* User cannot use the external link to dropbox
*
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL | FILE_REFERENCE;
}
/**
* Return reference file life time
*
* @param string $ref
* @return int
*/
public function get_reference_file_lifetime($ref) {
// this should be realtime
return 0;
}
/**
* Is this repository accessing private data?
*
* @return bool
*/
public function contains_private_data() {
return false;
}
}