moodle/files/index.php

313 lines
12 KiB
PHP
Raw Normal View History

<?php // $Id$
2001-11-22 06:23:56 +00:00
require('../config.php');
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/adminlib.php');
2001-11-22 06:23:56 +00:00
$courseid = optional_param('id', 0, PARAM_INT);
2001-11-22 06:23:56 +00:00
$contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT);
$filearea = optional_param('filearea', '', PARAM_ALPHAEXT);
$itemid = optional_param('itemid', -1, PARAM_INT);
$filepath = optional_param('filepath', '', PARAM_PATH);
$filename = optional_param('filename', '', PARAM_FILE);
$newdirname = optional_param('newdirname', '', PARAM_FILE);
$delete = optional_param('delete', 0, PARAM_BOOL);
if ($courseid) {
if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
print_error('invalidcourseid');
}
if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
print_error('invalidcontext');
}
2008-08-03 21:08:15 +00:00
redirect('index.php?contextid='.$context->id.'&amp;itemid=0&amp;filearea=course_content');
2001-11-22 06:23:56 +00:00
}
if (!$context = get_context_instance_by_id($contextid)) {
print_error('invalidcontext');
2001-11-22 06:23:56 +00:00
}
require_login();
require_capability('moodle/course:managefiles', $context);
2001-11-22 06:23:56 +00:00
if ($filearea === '') {
$filearea = null;
}
if ($itemid < 0) {
$itemid = null;
2001-11-22 06:23:56 +00:00
}
if ($filepath === '') {
$filepath = null;
}
if ($filename === '') {
$filename = null;
}
2001-11-22 06:23:56 +00:00
$error = '';
2001-11-22 06:23:56 +00:00
$browser = get_file_browser();
$file_info = $browser->get_file_info($context, $filearea, $itemid, $filepath, $filename);
/// process actions
if ($file_info and $file_info->is_directory() and $file_info->is_writable() and $newdirname !== '' and data_submitted() and confirm_sesskey()) {
if ($newdir_info = $file_info->create_directory($newdirname, $USER->id)) {
$params = $newdir_info->get_params_rawencoded();
$params = implode('&amp;', $params);
redirect("index.php?$params");
} else {
$error = "Could not create new dir"; // TODO: localise
}
}
if ($file_info and $file_info->is_directory() and $file_info->is_writable() and isset($_FILES['newfile']) and data_submitted() and confirm_sesskey()) {
$file = $_FILES['newfile'];
$newfilename = clean_param($file['name'], PARAM_FILE);
if (is_uploaded_file($_FILES['newfile']['tmp_name'])) {
try {
if ($newfile = $file_info->create_file_from_pathname($newfilename, $_FILES['newfile']['tmp_name'], $USER->id)) {
$params = $file_info->get_params_rawencoded();
$params = implode('&amp;', $params);
redirect("index.php?$params");
2001-11-22 06:23:56 +00:00
} else {
$error = "Could not create upload file"; // TODO: localise
2001-11-22 06:23:56 +00:00
}
} catch (file_exception $e) {
$error = "Exception: Could not create upload file"; // TODO: localise
2001-11-22 06:23:56 +00:00
}
}
}
if ($file_info and $delete) {
if (!data_submitted() or !confirm_sesskey()) {
print_header();
notify(get_string('deletecheckwarning').': '.$file_info->get_visible_name());
$parent_info = $file_info->get_parent();
$optionsno = $parent_info->get_params();
$optionsyes = $file_info->get_params();
$optionsyes['delete'] = 1;
$optionsyes['sesskey'] = sesskey();
notice_yesno (get_string('deletecheckfiles'), 'index.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
print_footer();
die;
}
if ($parent_info = $file_info->get_parent() and $parent_info->is_writable()) {
if (!$file_info->delete()) {
$error = "Could not delete file!"; // TODO: localise
}
$params = $parent_info->get_params_rawencoded();
$params = implode('&amp;', $params);
redirect("index.php?$params", $error);
}
}
2001-11-22 06:23:56 +00:00
/// print dir listing
html_header($context, $file_info);
2001-11-22 06:23:56 +00:00
if ($error !== '') {
notify($error);
}
2001-11-22 06:23:56 +00:00
displaydir($file_info);
if ($file_info and $file_info->is_directory() and $file_info->is_writable()) {
echo '<br />';
echo '<form action="index.php" method="post"><div>';
echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input type="text" name="newdirname" value="" />';
echo '<input type="submit" value="'.get_string('makeafolder').'" />';
echo '</div></form>';
echo '<br />';
echo '<form enctype="multipart/form-data" method="post" action="index.php"><div>';
echo '<input type="hidden" name="contextid" value="'.$contextid.'" />';
echo '<input type="hidden" name="filearea" value="'.$filearea.'" />';
echo '<input type="hidden" name="itemid" value="'.$itemid.'" />';
echo '<input type="hidden" name="filepath" value="'.s($filepath).'" />';
echo '<input type="hidden" name="filename" value="'.s($filename).'" />';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<input name="newfile" type="file" />';
echo '<input type="submit" value="'.get_string('uploadafile').'" />';
echo '</div></form>';
2001-11-22 06:23:56 +00:00
}
html_footer();
2001-11-22 06:23:56 +00:00
/// UI functions /////////////////////////
function html_footer() {
echo '</td></tr></table>';
print_footer();
2001-11-22 06:23:56 +00:00
}
function html_header($context, $file_info){
global $CFG, $SITE;
2001-11-22 06:23:56 +00:00
$navlinks = array();
$strfiles = get_string("files");
2001-11-22 06:23:56 +00:00
$navlinks[] = array('name' => $strfiles, 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header("$SITE->shortname: $strfiles", '', $navigation);
2001-11-22 06:23:56 +00:00
echo "<table border=\"0\" style=\"margin-left:auto;margin-right:auto\" cellspacing=\"3\" cellpadding=\"3\" width=\"740\">";
echo "<tr>";
echo "<td colspan=\"2\">";
2001-11-22 06:23:56 +00:00
}
/// FILE FUNCTIONS ///////////////////////////////////////////////////////////
2001-11-22 06:23:56 +00:00
2005-03-13 17:16:26 +00:00
function print_cell($alignment='center', $text='&nbsp;', $class='') {
if ($class) {
$class = ' class="'.$class.'"';
}
2007-02-07 07:34:53 +00:00
echo '<td align="'.$alignment.'" style="white-space:nowrap "'.$class.'>'.$text.'</td>';
2001-11-22 06:23:56 +00:00
}
function displaydir($file_info) {
global $CFG;
$children = $file_info->get_children();
$parent_info = $file_info->get_parent();
$strname = get_string('name');
$strsize = get_string('size');
$strmodified = get_string('modified');
$strfolder = get_string('folder');
$strfile = get_string('file');
$strdownload = get_string('download');
$strdelete = get_string('delete');
$straction = get_string('action');
$path = array();
$params = $file_info->get_params_rawencoded();
$params = implode('&amp;', $params);
$path[] = $file_info->get_visible_name();
$level = $parent_info;
while ($level) {
$params = $level->get_params_rawencoded();
$params = implode('&amp;', $params);
$path[] = '<a href="index.php?'.$params.'">'.$level->get_visible_name().'</a>';
$level = $level->get_parent();
}
2001-11-22 06:23:56 +00:00
$path = array_reverse($path);
2001-11-22 06:23:56 +00:00
$path = implode (' / ', $path);
echo $path. ' /';
2001-11-22 06:23:56 +00:00
2007-02-07 07:34:53 +00:00
echo "<div>";
echo "<hr/>";
echo "<table border=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"740\" class=\"files\">";
2004-09-12 21:43:59 +00:00
echo "<tr>";
echo "<th class=\"header\" scope=\"col\"></th>";
2007-02-07 07:34:53 +00:00
echo "<th class=\"header name\" scope=\"col\">$strname</th>";
echo "<th class=\"header size\" scope=\"col\">$strsize</th>";
echo "<th class=\"header date\" scope=\"col\">$strmodified</th>";
echo "<th class=\"header commands\" scope=\"col\">$straction</th>";
2004-09-12 21:43:59 +00:00
echo "</tr>\n";
2001-11-22 06:23:56 +00:00
$parentwritable = $file_info->is_writable();
2001-11-22 06:23:56 +00:00
if ($parent_info) {
$params = $parent_info->get_params_rawencoded();
$params = implode('&amp;', $params);
2005-03-13 17:16:26 +00:00
echo "<tr class=\"folder\">";
print_cell();
print_cell('left', '<a href="index.php?'.$params.'"><img src="'.$CFG->pixpath.'/f/parent.gif" class="icon" alt="" />&nbsp;'.get_string('parentfolder').'</a>', 'name');
print_cell();
print_cell();
print_cell();
echo "</tr>";
2001-11-22 06:23:56 +00:00
}
if ($children) {
foreach ($children as $child_info) {
$filename = $child_info->get_visible_name();
$filesize = $child_info->get_filesize();
$filesize = $filesize ? display_size($filesize) : '';
$filedate = $child_info->get_timemodified();
$filedate = $filedate ? userdate($filedate) : '';
2001-11-22 06:23:56 +00:00
$mimetype = $child_info->get_mimetype();
2001-11-22 06:23:56 +00:00
$params = $child_info->get_params_rawencoded();
$params = implode('&amp;', $params);
2001-11-22 06:23:56 +00:00
if ($child_info->is_directory()) {
2004-11-02 12:45:04 +00:00
echo "<tr class=\"folder\">";
print_cell();
print_cell("left", "<a href=\"index.php?$params\"><img src=\"$CFG->pixpath/f/folder.gif\" class=\"icon\" alt=\"$strfolder\" />&nbsp;".s($filename)."</a>", 'name');
print_cell("right", $filesize, 'size');
print_cell("right", $filedate, 'date');
if ($parentwritable) {
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');
} else {
print_cell();
}
echo "</tr>";
2001-11-22 06:23:56 +00:00
} else {
$icon = mimeinfo_from_type("icon", $mimetype);
if ($downloadurl = $child_info->get_url(true)) {
$downloadurl = "&nbsp;<a href=\"$downloadurl\" title=\"" . get_string('downloadfile') . "\"><img src=\"$CFG->pixpath/t/down.gif\" class=\"iconsmall\" alt=\"$strdownload\" /></a>";
} else {
$downloadurl = '';
}
2001-11-22 06:23:56 +00:00
if ($viewurl = $child_info->get_url()) {
$viewurl = "&nbsp;".link_to_popup_window ($viewurl, "display",
"<img src=\"$CFG->pixpath/t/preview.gif\" class=\"iconsmall\" alt=\"$strfile\" />&nbsp;",
480, 640, get_string('viewfileinpopup'), null, true);
} else {
$viewurl = '';
}
2004-11-02 12:45:04 +00:00
echo "<tr class=\"file\">";
print_cell();
print_cell("left", "<img src=\"$CFG->pixpath/f/$icon\" class=\"icon\" alt=\"$strfile\" />&nbsp;".s($filename).$downloadurl.$viewurl, 'name');
print_cell("right", $filesize, 'size');
print_cell("right", $filedate, 'date');
if ($parentwritable) {
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');
} else {
print_cell();
}
echo "</tr>";
2001-11-22 06:23:56 +00:00
}
}
}
2004-09-12 21:43:59 +00:00
echo "</table>";
echo "</div>";
2007-02-07 07:34:53 +00:00
echo "<hr/>";
2001-11-22 06:23:56 +00:00
}
?>