MDL-19235 updated license header in installer - completely rewritten for 2.0 + more...
[moodle.git] / files / index.php
CommitLineData
ad4e0fcc 1<?php // $Id$
f9903ed0 2
f1e0649c 3 require('../config.php');
172dd12c 4 require_once($CFG->libdir.'/filelib.php');
5 require_once($CFG->libdir.'/adminlib.php');
f9903ed0 6
172dd12c 7 $courseid = optional_param('id', 0, PARAM_INT);
f9903ed0 8
172dd12c 9 $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
6c0e2d08 10 $filearea = optional_param('filearea', '', PARAM_ALPHAEXT);
172dd12c 11 $itemid = optional_param('itemid', -1, PARAM_INT);
12 $filepath = optional_param('filepath', '', PARAM_PATH);
13 $filename = optional_param('filename', '', PARAM_FILE);
70cedbfd 14
172dd12c 15 $newdirname = optional_param('newdirname', '', PARAM_FILE);
16 $delete = optional_param('delete', 0, PARAM_BOOL);
8ce53f0f 17
172dd12c 18 if ($courseid) {
19 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
20 print_error('invalidcourseid');
55e4b5f9 21 }
172dd12c 22 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
23 print_error('invalidcontext');
65c59e30 24 }
1ba7534c 25 redirect('index.php?contextid='.$context->id.'&amp;itemid=0&amp;filearea=course_content');
f9903ed0 26 }
5875b7d6 27
172dd12c 28 if (!$context = get_context_instance_by_id($contextid)) {
29 print_error('invalidcontext');
f9903ed0 30 }
f9903ed0 31
172dd12c 32 require_login();
33 require_capability('moodle/course:managefiles', $context);
f9903ed0 34
172dd12c 35 if ($filearea === '') {
36 $filearea = null;
ad4e0fcc 37 }
38
172dd12c 39 if ($itemid < 0) {
40 $itemid = null;
f9903ed0 41 }
42
172dd12c 43 if ($filepath === '') {
44 $filepath = null;
fdfc6269 45 }
46
172dd12c 47 if ($filename === '') {
48 $filename = null;
70cedbfd 49 }
f9903ed0 50
172dd12c 51 $error = '';
f9903ed0 52
172dd12c 53 $browser = get_file_browser();
1a11dfd1 54
172dd12c 55 $file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename);
1a11dfd1 56
172dd12c 57/// process actions
58 if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) {
59 if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) {
60 $params = $newdir_info->get_params_rawencoded();
61 $params = implode('&amp;', $params);
62 redirect("index.php?$params");
63 } else {
64 $error = "Could not create new dir"; // TODO: localise
65 }
66 }
1a11dfd1 67
172dd12c 68 if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) {
69 $file = $_FILES['newfile'];
70 $newfilename = clean_param($file['name'], PARAM_FILE);
71 if (is_uploaded_file($_FILES['newfile']['tmp_name'])) {
72 try {
73 if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) {
74 $params = $file_info->get_params_rawencoded();
75 $params = implode('&amp;', $params);
76 redirect("index.php?$params");
70cedbfd 77
f9903ed0 78 } else {
172dd12c 79 $error = "Could not create upload file"; // TODO: localise
f9903ed0 80 }
172dd12c 81 } catch (file_exception $e) {
82 $error = "Exception: Could not create upload file"; // TODO: localise
f9903ed0 83 }
172dd12c 84 }
85 }
e64984a0 86
172dd12c 87 if ($file_info and $delete) {
88 if (!data_submitted() or !confirm_sesskey()) {
89 print_header();
90 notify(get_string('deletecheckwarning').': '.$file_info->get_visible_name());
91 $parent_info = $file_info->get_parent();
e64984a0 92
172dd12c 93 $optionsno = $parent_info->get_params();
94 $optionsyes = $file_info->get_params();
95 $optionsyes['delete'] = 1;
96 $optionsyes['sesskey'] = sesskey();
e64984a0 97
172dd12c 98 notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
99 print_footer();
100 die;
101 }
e64984a0 102
172dd12c 103 if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) {
104 if (!$file_info->delete()) {
105 $error = "Could not delete file!"; // TODO: localise
e64984a0 106 }
172dd12c 107 $params = $parent_info->get_params_rawencoded();
108 $params = implode('&amp;', $params);
109 redirect("index.php?$params", $error);
110 }
111 }
f9903ed0 112
f9903ed0 113
172dd12c 114/// print dir listing
115 html_header($context, $file_info);
f9903ed0 116
172dd12c 117 if ($error !== '') {
118 notify($error);
119 }
f9903ed0 120
172dd12c 121 displaydir($file_info);
122
123 if ($file_info and $file_info->is_directory() and $file_info->is_writable()) {
124 echo '<br />';
125
126 echo '<form action="index.php" method="post"><div>';
127 echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
128 echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
129 echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
130 echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
131 echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
132 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
133 echo '<input type="text" name="newdirname" value="" />';
134 echo '<input type="submit" value="'.get_string('makeafolder').'" />';
135 echo '</div></form>';
136
137 echo '<br />';
138
7caa6bbd 139 echo '<form enctype="multipart/form-data" method="post" action="index.php"><div>';
172dd12c 140 echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
141 echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
142 echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
143 echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
144 echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
145 echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
146 echo '<input name="newfile" type="file" />';
147 echo '<input type="submit" value="'.get_string('uploadafile').'" />';
148 echo '</div></form>';
f9903ed0 149 }
f9903ed0 150
172dd12c 151 html_footer();
f9903ed0 152
172dd12c 153/// UI functions /////////////////////////
154
155function html_footer() {
156 echo '</td></tr></table>';
157 print_footer();
f9903ed0 158}
159
172dd12c 160function html_header($context, $file_info){
161 global $CFG, $SITE;
f9903ed0 162
172dd12c 163 $navlinks = array();
164 $strfiles = get_string("files");
f9903ed0 165
172dd12c 166 $navlinks[] = array('name' => $strfiles, 'link' => null, 'type' => 'misc');
2bbaf749 167
172dd12c 168 $navigation = build_navigation($navlinks);
169 print_header("$SITE->shortname: $strfiles", '', $navigation);
f9903ed0 170
172dd12c 171 echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"740\">";
172 echo "<tr>";
173 echo "<td colspan=\"2\">";
f9903ed0 174}
175
172dd12c 176/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
f9903ed0 177
6b091c94 178function print_cell($alignment='center', $text='&nbsp;', $class='') {
179 if ($class) {
180 $class = ' class="'.$class.'"';
181 }
60a9a6ea 182 echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
f9903ed0 183}
184
172dd12c 185function displaydir($file_info) {
186 global $CFG;
187
188 $children = $file_info->get_children();
189 $parent_info = $file_info->get_parent();
190
191 $strname = get_string('name');
192 $strsize = get_string('size');
193 $strmodified = get_string('modified');
194 $strfolder = get_string('folder');
195 $strfile = get_string('file');
196 $strdownload = get_string('download');
197 $strdelete = get_string('delete');
198 $straction = get_string('action');
199
200 $path = array();
201 $params = $file_info->get_params_rawencoded();
202 $params = implode('&amp;', $params);
203 $path[] = $file_info->get_visible_name();
204
205 $level = $parent_info;
206 while ($level) {
207 $params = $level->get_params_rawencoded();
208 $params = implode('&amp;', $params);
209 $path[] = '<a href="index.php?'.$params.'">'.$level->get_visible_name().'</a>';
210 $level = $level->get_parent();
211 }
f9903ed0 212
172dd12c 213 $path = array_reverse($path);
f9903ed0 214
172dd12c 215 $path = implode (' / ', $path);
216 echo $path. ' /';
f9903ed0 217
60a9a6ea 218 echo "<div>";
60a9a6ea 219 echo "<hr/>";
172dd12c 220 echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"740\" class=\"files\">";
9a58f7cb 221 echo "<tr>";
2eb467b3 222 echo "<th class=\"header\" scope=\"col\"></th>";
60a9a6ea 223 echo "<th class=\"header name\" scope=\"col\">$strname</th>";
224 echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
225 echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
226 echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
9a58f7cb 227 echo "</tr>\n";
f9903ed0 228
172dd12c 229 $parentwritable = $file_info->is_writable();
f9903ed0 230
172dd12c 231 if ($parent_info) {
232 $params = $parent_info->get_params_rawencoded();
233 $params = implode('&amp;', $params);
6b091c94 234
172dd12c 235 echo "<tr class=\"folder\">";
236 print_cell();
237 print_cell('left', '<a href="index.php?'.$params.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>', 'name');
238 print_cell();
239 print_cell();
240 print_cell();
70cedbfd 241
172dd12c 242 echo "</tr>";
f9903ed0 243 }
244
172dd12c 245 if ($children) {
246 foreach ($children as $child_info) {
247 $filename = $child_info->get_visible_name();
248 $filesize = $child_info->get_filesize();
249 $filesize = $filesize ? display_size($filesize) : '';
250 $filedate = $child_info->get_timemodified();
251 $filedate = $filedate ? userdate($filedate) : '';
f9903ed0 252
172dd12c 253 $mimetype = $child_info->get_mimetype();
f9903ed0 254
172dd12c 255 $params = $child_info->get_params_rawencoded();
256 $params = implode('&amp;', $params);
f9903ed0 257
172dd12c 258 if ($child_info->is_directory()) {
8e73aa97 259
172dd12c 260 echo "<tr class=\"folder\">";
261 print_cell();
262 print_cell("left", "<a href=\"index.php?$params\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".s($filename)."</a>", 'name');
263 print_cell("right", $filesize, 'size');
264 print_cell("right", $filedate, 'date');
265 if ($parentwritable) {
266 print_cell("right", "<a href=\"index.php?$params&amp;sesskey=".sesskey()."&amp;delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>", 'command');
267 } else {
268 print_cell();
269 }
270 echo "</tr>";
f9903ed0 271
172dd12c 272 } else {
5a254a29 273
172dd12c 274 $icon = mimeinfo_from_type("icon", $mimetype);
275 if ($downloadurl = $child_info->get_url(true)) {
7caa6bbd 276 $downloadurl = "&nbsp;<a href=\"$downloadurl\" title=\"" . get_string('downloadfile') . "\"><img src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strdownload\" /></a>";
172dd12c 277 } else {
278 $downloadurl = '';
279 }
f9903ed0 280
172dd12c 281 if ($viewurl = $child_info->get_url()) {
282 $viewurl = "&nbsp;".link_to_popup_window ($viewurl, "display",
283 "<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strfile\" />&nbsp;",
7caa6bbd 284 480, 640, get_string('viewfileinpopup'), null, true);
172dd12c 285 } else {
286 $viewurl = '';
287 }
8e73aa97 288
8e73aa97 289
290
172dd12c 291 echo "<tr class=\"file\">";
292 print_cell();
293 print_cell("left", "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".s($filename).$downloadurl.$viewurl, 'name');
294 print_cell("right", $filesize, 'size');
295 print_cell("right", $filedate, 'date');
296 if ($parentwritable) {
297 print_cell("right", "<a href=\"index.php?$params&amp;sesskey=".sesskey()."&amp;delete=1\"><img src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a>", 'command');
298 } else {
299 print_cell();
476fd9c7 300 }
172dd12c 301 echo "</tr>";
f9903ed0 302 }
f9903ed0 303 }
304 }
f98e18e6 305
9a58f7cb 306 echo "</table>";
172dd12c 307 echo "</div>";
60a9a6ea 308 echo "<hr/>";
f9903ed0 309
310}
311
312?>