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 DC |
21 | * @since 2.0 |
22 | * @package moodlecore | |
23 | * @subpackage repository | |
955b6e09 | 24 | * @copyright 2009 Dongsheng Cai <dongsheng@moodle.com> |
aedb0396 | 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)) { | |
955b6e09 DC |
55 | $itemid = clean_param($params['itemid'], PARAM_INT); |
56 | $filename = clean_param($params['filename'], PARAM_FILE); | |
57 | $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT); | |
58 | $filepath = clean_param($params['filepath'], PARAM_PATH);; | |
16f61c70 | 59 | $component = clean_param($params['component'], PARAM_ALPHAEXT); |
955b6e09 | 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) { | |
97 | $shorttitle = $this->get_short_filename($child->get_visible_name(), 12); | |
98 | if ($child->is_directory()) { | |
99 | $params = $child->get_params(); | |
100 | $subdir_children = $child->get_children(); | |
101 | //if (empty($subdir_children)) { | |
102 | //continue; | |
103 | //} | |
104 | $encodedpath = base64_encode(serialize($params)); | |
105 | // hide user_private area from local plugin, user should | |
106 | // use private file plugin to access private files | |
107 | //if ($params['filearea'] == 'user_private') { | |
108 | //continue; | |
109 | //} | |
110 | $node = array( | |
111 | 'title' => $child->get_visible_name(), | |
112 | 'shorttitle'=>$shorttitle, | |
113 | 'size' => 0, | |
114 | 'date' => '', | |
115 | 'path' => $encodedpath, | |
116 | 'children'=>array(), | |
117 | 'thumbnail' => $OUTPUT->pix_url('f/folder-32') . '' | |
118 | ); | |
119 | $list[] = $node; | |
120 | } else { | |
121 | $encodedpath = base64_encode(serialize($child->get_params())); | |
122 | $icon = 'f/'.str_replace('.gif', '', mimeinfo('icon', $child->get_visible_name())).'-32'; | |
123 | $node = array( | |
124 | 'title' => $child->get_visible_name(), | |
125 | 'shorttitle'=>$shorttitle, | |
126 | 'size' => 0, | |
127 | 'date' => '', | |
128 | 'source'=> $encodedpath, | |
129 | 'thumbnail' => $OUTPUT->pix_url($icon) . '', | |
130 | ); | |
131 | $list[] = $node; | |
cd312233 | 132 | } |
133 | } | |
eed6f2e7 DC |
134 | } else { |
135 | // if file doesn't exist, build path nodes root of current context | |
136 | $pathnodes = array(); | |
137 | $fileinfo = $browser->get_file_info($context, null, null, null, null, null); | |
138 | $encodedpath = base64_encode(serialize($fileinfo->get_params())); | |
139 | $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath); | |
140 | $level = $fileinfo->get_parent(); | |
141 | while ($level) { | |
142 | $encodedpath = base64_encode(serialize($level->get_params())); | |
143 | $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); | |
144 | $level = $level->get_parent(); | |
145 | } | |
146 | if (!empty($pathnodes) && is_array($pathnodes)) { | |
147 | $pathnodes = array_reverse($pathnodes); | |
148 | $ret['path'] = $pathnodes; | |
149 | } | |
150 | $list = array(); | |
6a391ebf | 151 | } |
87628a67 | 152 | $ret['list'] = array_filter($list, array($this, 'filter')); |
cd312233 | 153 | return $ret; |
6a391ebf | 154 | } |
dbc01944 | 155 | |
aedb0396 DC |
156 | /** |
157 | * Local file don't support to link to external links | |
158 | * | |
2f67a9b3 | 159 | * @return int |
aedb0396 | 160 | */ |
41076c58 DC |
161 | public function supported_returntypes() { |
162 | return FILE_INTERNAL; | |
163 | } | |
c7e4621e | 164 | |
acb70a9b | 165 | /** |
c7e4621e | 166 | * Copy a file to file area |
acb70a9b DC |
167 | * |
168 | * @global object $USER | |
169 | * @global object $DB | |
3b61c7e6 PS |
170 | * @param string $encoded The metainfo of file, it is base64 encoded php serialized data |
171 | * @param string $draftitemid itemid | |
acb70a9b | 172 | * @param string $new_filename The intended name of file |
acb70a9b DC |
173 | * @param string $new_filepath the new path in draft area |
174 | * @return array The information of file | |
175 | */ | |
3b61c7e6 | 176 | public function copy_to_area($encoded, $draftitemid, $new_filepath, $new_filename) { |
acb70a9b DC |
177 | global $USER, $DB; |
178 | $info = array(); | |
4317f92f | 179 | |
acb70a9b | 180 | $browser = get_file_browser(); |
cba9ed29 | 181 | $fs = get_file_storage(); |
acb70a9b | 182 | $user_context = get_context_instance(CONTEXT_USER, $USER->id); |
955b6e09 | 183 | |
acb70a9b | 184 | // the final file |
955b6e09 DC |
185 | $params = unserialize(base64_decode($encoded)); |
186 | $contextid = clean_param($params['contextid'], PARAM_INT); | |
187 | $fileitemid = clean_param($params['itemid'], PARAM_INT); | |
188 | $filename = clean_param($params['filename'], PARAM_FILE); | |
189 | $filepath = clean_param($params['filepath'], PARAM_PATH);; | |
190 | $filearea = clean_param($params['filearea'], PARAM_ALPHAEXT); | |
16f61c70 | 191 | $component = clean_param($params['component'], PARAM_ALPHAEXT); |
955b6e09 DC |
192 | $context = get_context_instance_by_id($contextid); |
193 | ||
1311e4dd DC |
194 | if ($existingfile = $fs->get_file($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename)) { |
195 | throw new moodle_exception('fileexists'); | |
196 | } | |
197 | ||
16f61c70 | 198 | $file_info = $browser->get_file_info($context, $component, $filearea, $fileitemid, $filepath, $filename); |
3b61c7e6 | 199 | $file_info->copy_to_storage($user_context->id, 'user', 'draft', $draftitemid, $new_filepath, $new_filename); |
acb70a9b | 200 | |
3b61c7e6 | 201 | $info['itemid'] = $draftitemid; |
acb70a9b DC |
202 | $info['title'] = $new_filename; |
203 | $info['contextid'] = $user_context->id; | |
204 | $info['filesize'] = $file_info->get_filesize(); | |
205 | ||
206 | return $info; | |
207 | } | |
208 | } |