Merge branch 'MDL-57791_master-fix3' of git://github.com/dmonllao/moodle
[moodle.git] / repository / local / lib.php
blob6c8be642ba414668738106d8e0569873fc7612a2
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This plugin is used to access local files
20 * @since Moodle 2.0
21 * @package repository_local
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
27 /**
28 * repository_local class is used to browse moodle files
30 * @since Moodle 2.0
31 * @package repository_local
32 * @copyright 2012 Marina Glancy
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class repository_local extends repository {
37 /**
38 * Get file listing
40 * @param string $encodedpath
41 * @param string $page no paging is used in repository_local
42 * @return mixed
44 public function get_listing($encodedpath = '', $page = '') {
45 global $CFG, $USER, $OUTPUT;
46 $ret = array();
47 $ret['dynload'] = true;
48 $ret['nosearch'] = false;
49 $ret['nologin'] = true;
50 $ret['list'] = array();
52 $itemid = null;
53 $filename = null;
54 $filearea = null;
55 $filepath = null;
56 $component = null;
58 if (!empty($encodedpath)) {
59 $params = json_decode(base64_decode($encodedpath), true);
60 if (is_array($params) && isset($params['contextid'])) {
61 $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT);
62 $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA);
63 $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT);
64 $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);
65 $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE);
66 $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT));
69 if (empty($context) && !empty($this->context)) {
70 $context = $this->context->get_course_context(false);
72 if (empty($context)) {
73 $context = context_system::instance();
76 // prepare list of allowed extensions: $extensions is either string '*'
77 // or array of lowercase extensions, i.e. array('.gif','.jpg')
78 $extensions = optional_param_array('accepted_types', '', PARAM_RAW);
79 if (empty($extensions) || $extensions === '*' || (is_array($extensions) && in_array('*', $extensions))) {
80 $extensions = '*';
81 } else {
82 if (!is_array($extensions)) {
83 $extensions = array($extensions);
85 $extensions = array_map('core_text::strtolower', $extensions);
88 // build file tree
89 $browser = get_file_browser();
90 if (!($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename))) {
91 // if file doesn't exist, build path nodes root of current context
92 $fileinfo = $browser->get_file_info($context, null, null, null, null, null);
94 $ret['list'] = $this->get_non_empty_children($fileinfo, $extensions);
96 // build path navigation
97 $path = array();
98 for ($level = $fileinfo; $level; $level = $level->get_parent()) {
99 array_unshift($path, $level);
101 array_unshift($path, null);
102 $ret['path'] = array();
103 for ($i=1; $i<count($path); $i++) {
104 if ($path[$i] == $fileinfo || !$this->can_skip($path[$i], $extensions, $path[$i-1])) {
105 $ret['path'][] = $this->get_node_path($path[$i]);
108 return $ret;
112 * Tells how the file can be picked from this repository
114 * @return int
116 public function supported_returntypes() {
117 return FILE_INTERNAL | FILE_REFERENCE;
121 * Does this repository used to browse moodle files?
123 * @return boolean
125 public function has_moodle_files() {
126 return true;
130 * Returns all children elements that have one of the specified extensions
132 * This function may skip subfolders and recursively add their children
133 * {@link repository_local::can_skip()}
135 * @param file_info $fileinfo
136 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
137 * @return array array of file_info elements
139 private function get_non_empty_children(file_info $fileinfo, $extensions) {
140 $nonemptychildren = $fileinfo->get_non_empty_children($extensions);
141 $list = array();
142 foreach ($nonemptychildren as $child) {
143 if ($this->can_skip($child, $extensions, $fileinfo)) {
144 $list = array_merge($list, $this->get_non_empty_children($child, $extensions));
145 } else {
146 $list[] = $this->get_node($child);
149 return $list;
153 * Whether this folder may be skipped in folder hierarchy
155 * 1. Skip the name of a single filearea in a module
156 * 2. Skip course categories for non-admins who do not have navshowmycoursecategories setting
158 * @param file_info $fileinfo
159 * @param string|array $extensions, for example '*' or array('.gif','.jpg')
160 * @param file_info|int $parent specify parent here if we know it to avoid creating extra objects
161 * @return bool
163 private function can_skip(file_info $fileinfo, $extensions, $parent = -1) {
164 global $CFG;
165 if (!$fileinfo->is_directory()) {
166 // do not skip files
167 return false;
169 if ($fileinfo instanceof file_info_context_coursecat) {
170 // This is a course category. For non-admins we do not display categories
171 return empty($CFG->navshowmycoursecategories) &&
172 !has_capability('moodle/course:update', context_system::instance());
173 } else if ($fileinfo instanceof file_info_context_course ||
174 $fileinfo instanceof file_info_context_user ||
175 $fileinfo instanceof file_info_area_course_legacy ||
176 $fileinfo instanceof file_info_context_module ||
177 $fileinfo instanceof file_info_context_system) {
178 // these instances can never be filearea inside an activity, they will never be skipped
179 return false;
180 } else {
181 $params = $fileinfo->get_params();
182 if (strlen($params['filearea']) &&
183 ($params['filepath'] === '/' || empty($params['filepath'])) &&
184 ($params['filename'] === '.' || empty($params['filename'])) &&
185 context::instance_by_id($params['contextid'])->contextlevel == CONTEXT_MODULE) {
186 if ($parent === -1) {
187 $parent = $fileinfo->get_parent();
189 // This is a filearea inside an activity, it can be skipped if it has no non-empty siblings
190 if ($parent && ($parent instanceof file_info_context_module)) {
191 if ($parent->count_non_empty_children($extensions, 2) <= 1) {
192 return true;
197 return false;
201 * Converts file_info object to element of repository return list
203 * @param file_info $fileinfo
204 * @return array
206 private function get_node(file_info $fileinfo) {
207 global $OUTPUT;
208 $encodedpath = base64_encode(json_encode($fileinfo->get_params()));
209 $node = array(
210 'title' => $fileinfo->get_visible_name(),
211 'datemodified' => $fileinfo->get_timemodified(),
212 'datecreated' => $fileinfo->get_timecreated()
214 if ($fileinfo->is_directory()) {
215 $node['path'] = $encodedpath;
216 $node['thumbnail'] = $OUTPUT->image_url(file_folder_icon(90))->out(false);
217 $node['children'] = array();
218 } else {
219 $node['size'] = $fileinfo->get_filesize();
220 $node['author'] = $fileinfo->get_author();
221 $node['license'] = $fileinfo->get_license();
222 $node['isref'] = $fileinfo->is_external_file();
223 if ($fileinfo->get_status() == 666) {
224 $node['originalmissing'] = true;
226 $node['source'] = $encodedpath;
227 $node['thumbnail'] = $OUTPUT->image_url(file_file_icon($fileinfo, 90))->out(false);
228 $node['icon'] = $OUTPUT->image_url(file_file_icon($fileinfo, 24))->out(false);
229 if ($imageinfo = $fileinfo->get_imageinfo()) {
230 // what a beautiful picture, isn't it
231 $fileurl = new moodle_url($fileinfo->get_url());
232 $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $fileinfo->get_timemodified()));
233 $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $fileinfo->get_timemodified()));
234 $node['image_width'] = $imageinfo['width'];
235 $node['image_height'] = $imageinfo['height'];
238 return $node;
242 * Converts file_info object to element of repository return path
244 * @param file_info $fileinfo
245 * @return array
247 private function get_node_path(file_info $fileinfo) {
248 $encodedpath = base64_encode(json_encode($fileinfo->get_params()));
249 return array(
250 'path' => $encodedpath,
251 'name' => $fileinfo->get_visible_name()
256 * Search through all the files.
258 * This method will do a raw search through the database, then will try
259 * to match with files that a user can access. A maximum of 50 files will be
260 * returned at a time, excluding possible duplicates found along the way.
262 * Queries are done in chunk of 100 files to prevent too many records to be fetched
263 * at once. When too many files are not included, or a maximum of 10 queries are
264 * performed we consider that this was the last page.
266 * @param String $q The query string.
267 * @param integer $page The page number.
268 * @return array of results.
270 public function search($q, $page = 1) {
271 global $DB, $SESSION;
273 // Because the repository API is weird, the first page is 0, but it should be 1.
274 if (!$page) {
275 $page = 1;
278 if (!isset($SESSION->repository_local_search)) {
279 $SESSION->repository_local_search = array();
282 $fs = get_file_storage();
283 $fb = get_file_browser();
285 $max = 50;
286 $limit = 100;
287 if ($page <= 1) {
288 $SESSION->repository_local_search['query'] = $q;
289 $SESSION->repository_local_search['from'] = 0;
290 $from = 0;
291 } else {
292 // Yes, the repository does not send the query again...
293 $q = $SESSION->repository_local_search['query'];
294 $from = (int) $SESSION->repository_local_search['from'];
297 $count = $fs->search_server_files('%' . $DB->sql_like_escape($q) . '%', null, null, true);
298 $remaining = $count - $from;
299 $maxloops = 3000;
300 $loops = 0;
302 $results = array();
303 while (count($results) < $max && $maxloops > 0 && $remaining > 0) {
304 if (empty($files)) {
305 $files = $fs->search_server_files('%' . $DB->sql_like_escape($q) . '%', $from, $limit);
306 $from += $limit;
309 $remaining--;
310 $maxloops--;
311 $loops++;
313 $file = array_shift($files);
314 if (!$file) {
315 // This should not happen.
316 throw new coding_exception('Unexpected end of files list.');
319 $key = $file->get_contenthash() . ':' . $file->get_filename();
320 if (isset($results[$key])) {
321 // We found the file with same content and same name, let's skip it.
322 continue;
325 $ctx = context::instance_by_id($file->get_contextid());
326 $fileinfo = $fb->get_file_info($ctx, $file->get_component(), $file->get_filearea(), $file->get_itemid(),
327 $file->get_filepath(), $file->get_filename());
328 if ($fileinfo) {
329 $results[$key] = $this->get_node($fileinfo);
334 // Save the position for the paging to work.
335 if ($maxloops > 0 && $remaining > 0) {
336 $SESSION->repository_local_search['from'] += $loops;
337 $pages = -1;
338 } else {
339 $SESSION->repository_local_search['from'] = 0;
340 $pages = 0;
343 $return = array(
344 'list' => array_values($results),
345 'dynload' => true,
346 'pages' => $pages,
347 'page' => $page
350 return $return;
354 * Is this repository accessing private data?
356 * @return bool
358 public function contains_private_data() {
359 return false;