Commit | Line | Data |
---|---|---|
6a391ebf | 1 | <?php |
aedb0396 DC |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
6a391ebf | 17 | |
96297ca2 | 18 | /** |
aedb0396 | 19 | * repository_local class is used to browse moodle files |
96297ca2 | 20 | * |
aedb0396 | 21 | * @since 2.0 |
d078f6d3 PS |
22 | * @package repository |
23 | * @subpackage local | |
24 | * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com> | |
25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
96297ca2 | 26 | */ |
aedb0396 | 27 | |
be236ef5 | 28 | class repository_local extends repository { |
6a391ebf | 29 | |
96297ca2 | 30 | /** |
acb70a9b | 31 | * local plugin doesn't require login, so list all files |
cd312233 | 32 | * @return mixed |
96297ca2 | 33 | */ |
5f9087f3 | 34 | public function print_login() { |
6a391ebf | 35 | return $this->get_listing(); |
36 | } | |
5fd3e8f7 | 37 | |
96297ca2 | 38 | /** |
aedb0396 | 39 | * Get file listing |
96297ca2 | 40 | * |
cd312233 | 41 | * @param string $encodedpath |
cd312233 | 42 | * @return mixed |
96297ca2 | 43 | */ |
acb70a9b | 44 | public function get_listing($encodedpath = '') { |
390baf46 | 45 | global $CFG, $USER, $OUTPUT; |
cd312233 | 46 | $ret = array(); |
47 | $ret['dynload'] = true; | |
d1bfc05e | 48 | $ret['nosearch'] = true; |
03896eb7 | 49 | $ret['nologin'] = true; |
cd312233 | 50 | $list = array(); |
21942470 | 51 | |
a6058fed | 52 | if (!empty($encodedpath)) { |
53 | $params = unserialize(base64_decode($encodedpath)); | |
54 | if (is_array($params)) { | |
3136dc16 PS |
55 | $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_ALPHAEXT); |
56 | $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_ALPHAEXT); | |
57 | $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT); | |
58 | $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);; | |
59 | $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE); | |
60 | $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT)); | |
a6058fed | 61 | } |
62 | } else { | |
63 | $itemid = null; | |
64 | $filename = null; | |
65 | $filearea = null; | |
66 | $filepath = null; | |
16f61c70 | 67 | $component = null; |
be85f7ab DC |
68 | if (!empty($this->context)) { |
69 | list($context, $course, $cm) = get_context_info_array($this->context->id); | |
70 | $courseid = is_object($course) ? $course->id : SITEID; | |
71 | $context = get_context_instance(CONTEXT_COURSE, $courseid); | |
72 | } else { | |
73 | $context = get_system_context(); | |
74 | } | |
a6058fed | 75 | } |
76 | ||
955b6e09 DC |
77 | $browser = get_file_browser(); |
78 | ||
16f61c70 | 79 | if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { |
955b6e09 DC |
80 | // build path navigation |
81 | $pathnodes = array(); | |
82 | $encodedpath = base64_encode(serialize($fileinfo->get_params())); | |
83 | $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath); | |
84 | $level = $fileinfo->get_parent(); | |
85 | while ($level) { | |
86 | $encodedpath = base64_encode(serialize($level->get_params())); | |
87 | $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); | |
88 | $level = $level->get_parent(); | |
89 | } | |
90 | if (!empty($pathnodes) && is_array($pathnodes)) { | |
91 | $pathnodes = array_reverse($pathnodes); | |
92 | $ret['path'] = $pathnodes; | |
93 | } | |
94 | // build file tree | |
95 | $children = $fileinfo->get_children(); | |
96 | foreach ($children as $child) { | |
955b6e09 DC |
97 | if ($child->is_directory()) { |
98 | $params = $child->get_params(); | |
99 | $subdir_children = $child->get_children(); | |
100 | //if (empty($subdir_children)) { | |
101 | //continue; | |
102 | //} | |
103 | $encodedpath = base64_encode(serialize($params)); | |
104 | // hide user_private area from local plugin, user should | |
105 | // use private file plugin to access private files | |
106 | //if ($params['filearea'] == 'user_private') { | |
107 | //continue; | |
108 | //} | |
109 | $node = array( | |
110 | 'title' => $child->get_visible_name(), | |
955b6e09 DC |
111 | 'size' => 0, |
112 | 'date' => '', | |
113 | 'path' => $encodedpath, | |
114 | 'children'=>array(), | |
ede72522 | 115 | 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false) |
955b6e09 DC |
116 | ); |
117 | $list[] = $node; | |
118 | } else { | |
119 | $encodedpath = base64_encode(serialize($child->get_params())); | |
955b6e09 DC |
120 | $node = array( |
121 | 'title' => $child->get_visible_name(), | |
955b6e09 DC |
122 | 'size' => 0, |
123 | 'date' => '', | |
124 | 'source'=> $encodedpath, | |
ede72522 | 125 | 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false) |
955b6e09 DC |
126 | ); |
127 | $list[] = $node; | |
cd312233 | 128 | } |
129 | } | |
eed6f2e7 DC |
130 | } else { |
131 | // if file doesn't exist, build path nodes root of current context | |
132 | $pathnodes = array(); | |
133 | $fileinfo = $browser->get_file_info($context, null, null, null, null, null); | |
134 | $encodedpath = base64_encode(serialize($fileinfo->get_params())); | |
135 | $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath); | |
136 | $level = $fileinfo->get_parent(); | |
137 | while ($level) { | |
138 | $encodedpath = base64_encode(serialize($level->get_params())); | |
139 | $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); | |
140 | $level = $level->get_parent(); | |
141 | } | |
142 | if (!empty($pathnodes) && is_array($pathnodes)) { | |
143 | $pathnodes = array_reverse($pathnodes); | |
144 | $ret['path'] = $pathnodes; | |
145 | } | |
146 | $list = array(); | |
6a391ebf | 147 | } |
87628a67 | 148 | $ret['list'] = array_filter($list, array($this, 'filter')); |
cd312233 | 149 | return $ret; |
6a391ebf | 150 | } |
dbc01944 | 151 | |
aedb0396 DC |
152 | /** |
153 | * Local file don't support to link to external links | |
154 | * | |
2f67a9b3 | 155 | * @return int |
aedb0396 | 156 | */ |
41076c58 DC |
157 | public function supported_returntypes() { |
158 | return FILE_INTERNAL; | |
159 | } | |
c7e4621e | 160 | |
acb70a9b | 161 | /** |
c7e4621e | 162 | * Copy a file to file area |
acb70a9b DC |
163 | * |
164 | * @global object $USER | |
165 | * @global object $DB | |
3b61c7e6 PS |
166 | * @param string $encoded The metainfo of file, it is base64 encoded php serialized data |
167 | * @param string $draftitemid itemid | |
acb70a9b | 168 | * @param string $new_filename The intended name of file |
acb70a9b DC |
169 | * @param string $new_filepath the new path in draft area |
170 | * @return array The information of file | |
171 | */ | |
3b61c7e6 | 172 | public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) { |
acb70a9b DC |
173 | global $USER, $DB; |
174 | $info = array(); | |
4317f92f | 175 | |
acb70a9b | 176 | $browser = get_file_browser(); |
cba9ed29 | 177 | $fs = get_file_storage(); |
acb70a9b | 178 | $user_context = get_context_instance(CONTEXT_USER, $USER->id); |
955b6e09 | 179 | |
acb70a9b | 180 | // the final file |
955b6e09 DC |
181 | $params = unserialize(base64_decode($encoded)); |
182 | $contextid = clean_param($params['contextid'], PARAM_INT); | |
183 | $fileitemid = clean_param($params['itemid'], PARAM_INT); | |
184 | $filename = clean_param($params['filename'], PARAM_FILE); | |
185 | $filepath = clean_param($params['filepath'], PARAM_PATH);; | |
186 | $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT); | |
16f61c70 | 187 | $component = clean_param($params['component'], PARAM_ALPHAEXT); |
955b6e09 DC |
188 | $context = get_context_instance_by_id($contextid); |
189 | ||
1311e4dd DC |
190 | if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) { |
191 | throw new moodle_exception('fileexists'); | |
192 | } | |
193 | ||
16f61c70 | 194 | $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); |
3b61c7e6 | 195 | $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename); |
acb70a9b | 196 | |
3b61c7e6 | 197 | $info['itemid'] = $draftitemid; |
acb70a9b DC |
198 | $info['title'] = $new_filename; |
199 | $info['contextid'] = $user_context->id; | |
200 | $info['filesize'] = $file_info->get_filesize(); | |
201 | ||
202 | return $info; | |
203 | } | |
f892da72 DC |
204 | function get_file_count($contextid) { |
205 | global $DB; | |
206 | } | |
acb70a9b | 207 | } |