Skip to content

Commit

Permalink
Cleaning up javascript and trying to make code less
Browse files Browse the repository at this point in the history
memory consuming.
  • Loading branch information
julmis committed Mar 26, 2004
1 parent 7f6de0e commit cfa3f26
Showing 1 changed file with 53 additions and 66 deletions.
119 changes: 53 additions & 66 deletions lib/editor/coursefiles.php
Expand Up @@ -4,7 +4,7 @@


// This file is a hack to files/index.php that removes // This file is a hack to files/index.php that removes
// the headers and adds some controls so that images // the headers and adds some controls so that images
// can be selected within the Richtext editor. // can be selected within the Richtext editor.


// All the Moodle-specific stuff is in this top section // All the Moodle-specific stuff is in this top section
// Configuration and access control occurs here. // Configuration and access control occurs here.
Expand Down Expand Up @@ -33,44 +33,42 @@
function html_footer() { function html_footer() {
echo "</td></tr></table></body></html>"; echo "</td></tr></table></body></html>";
} }

function html_header($course, $wdir, $formfield=""){ function html_header($course, $wdir, $formfield=""){


global $CFG; global $CFG;


if (! $site = get_site()) { if (! $site = get_site()) {
error("Invalid site!"); error("Invalid site!");
} }

?> ?>
<html> <html>
<head> <head>
<title>coursefiles</title> <title>coursefiles</title>
<script language="javscript" type="text/javascript"> <script language="javscript" type="text/javascript">
<!-- <!--
function set_value(params) { function set_value(params) {
/// function's argument is an array containing necessary values /// function's argument is an object containing necessary values
/// to export parent window (url,isize,itype,iwidth,iheight) /// to export parent window (url,isize,itype,iwidth,iheight, imodified)
/// set values when user click's an image name. /// set values when user click's an image name.
var upper = window.parent; var upper = window.parent;
var insimg = upper.document.getElementById('f_url'); var insimg = upper.document.getElementById('f_url');

try { try {
if(insimg != null) { if(insimg != null) {
if(params.itype.indexOf("image/gif") == -1 && params.itype.indexOf("image/jpeg") == -1 && params.itype.indexOf("image/png") == -1) {
alert("<?php print_string("notimage","editor");?>");
return false;
}
for(field in params) { for(field in params) {
var value = params[field]; var value = params[field];
switch(field) { switch(field) {
case "url" : upper.document.getElementById('f_url').value = value; case "url" : upper.document.getElementById('f_url').value = value;
upper.ipreview.location.replace('popups/preview.php?id='+ <?php print($course->id);?> +'&imageurl='+ value); upper.ipreview.location.replace('popups/preview.php?id='+ <?php print($course->id);?> +'&imageurl='+ value);
break; break;
case "isize" : upper.document.getElementById('isize').value = value; break; case "isize" : upper.document.getElementById('isize').value = value; break;
case "itype" : case "itype" : upper.document.getElementById('itype').value = value; break;
if(params[field].indexOf("image/gif") == -1 && params[field].indexOf("image/jpeg") == -1 && params[field].indexOf("image/png") == -1) {
alert("<?php print_string("notimage","editor");?>");
return false;
} else {
upper.document.getElementById('itype').value = value;
}
break;
case "iwidth": upper.document.getElementById('f_width').value = value; break; case "iwidth": upper.document.getElementById('f_width').value = value; break;
case "iheight": upper.document.getElementById('f_height').value = value; break; case "iheight": upper.document.getElementById('f_height').value = value; break;
} }
Expand All @@ -80,7 +78,7 @@ function set_value(params) {
var value = params[field]; var value = params[field];
switch(field) { switch(field) {
case "url" : upper.document.getElementById('f_href').value = value; break; case "url" : upper.document.getElementById('f_href').value = value; break;
case "imodified" : upper.document.getElementById('imodified').value = value; break; case "imodified" : upper.document.getElementById('imodified').value = value; break;
case "isize" : upper.document.getElementById('isize').value = value; break; case "isize" : upper.document.getElementById('isize').value = value; break;
case "itype" : upper.document.getElementById('itype').value = value; break; case "itype" : upper.document.getElementById('itype').value = value; break;
} }
Expand All @@ -91,7 +89,7 @@ function set_value(params) {
} }
return false; return false;
} }

function set_dir(strdir) { function set_dir(strdir) {
// sets wdir values // sets wdir values
var upper = window.parent.document; var upper = window.parent.document;
Expand All @@ -101,19 +99,19 @@ function set_dir(strdir) {
try { try {
f.wdir.value = strdir; f.wdir.value = strdir;
} catch (e) { } catch (e) {

} }
} }
} }
} }

function set_rename(strfile) { function set_rename(strfile) {
if(window.parent.document.irename != null) { if(window.parent.document.irename != null) {
window.parent.document.irename.file.value = strfile; window.parent.document.irename.file.value = strfile;
} }
return true; return true;
} }

function reset_value() { function reset_value() {
var upper = window.parent.document; var upper = window.parent.document;
for(var i = 0; i < upper.forms.length; i++) { for(var i = 0; i < upper.forms.length; i++) {
Expand All @@ -128,7 +126,7 @@ function reset_value() {
} }
} }
} }

var ren = upper.getElementById('irename'); var ren = upper.getElementById('irename');
if(ren != null) { if(ren != null) {
upper.irename.file.value = ""; upper.irename.file.value = "";
Expand All @@ -143,7 +141,7 @@ function reset_value() {
} }
set_dir('<?php print($_REQUEST['wdir']);?>'); set_dir('<?php print($_REQUEST['wdir']);?>');
return true; return true;
} }
--> -->
</script> </script>
<style type="text/css"> <style type="text/css">
Expand All @@ -158,7 +156,7 @@ function reset_value() {
font-family: Tahoma, sans-serif; font-family: Tahoma, sans-serif;
font-size: 11px; font-size: 11px;
} }
select { select {
position: absolute; position: absolute;
top: -20px; top: -20px;
left: 0px; left: 0px;
Expand All @@ -167,7 +165,7 @@ function reset_value() {
</style> </style>
</head> </head>
<body onload="reset_value();"> <body onload="reset_value();">

<?php <?php
} }


Expand All @@ -181,11 +179,11 @@ function reset_value() {




$regexp="\\.\\."; $regexp="\\.\\.";
if (ereg( $regexp, $file, $regs )| ereg( $regexp, $wdir,$regs )) { if (ereg( $regexp, $file, $regs )| ereg( $regexp, $wdir,$regs )) {
$message = "Error: Directories can not contain \"..\""; $message = "Error: Directories can not contain \"..\"";
$wdir = "/"; $wdir = "/";
$action = ""; $action = "";
} }


if (!$wdir) { if (!$wdir) {
$wdir="/"; $wdir="/";
Expand Down Expand Up @@ -220,7 +218,7 @@ function reset_value() {
} }
} }
displaydir($wdir); displaydir($wdir);

} else { } else {
$upload_max_filesize = get_max_upload_file_size(); $upload_max_filesize = get_max_upload_file_size();
$filesize = display_size($upload_max_filesize); $filesize = display_size($upload_max_filesize);
Expand Down Expand Up @@ -274,7 +272,7 @@ function reset_value() {
printfilelist($USER->filelist); printfilelist($USER->filelist);
print_simple_box_end(); print_simple_box_end();
echo "<br />"; echo "<br />";
notice_yesno (get_string("deletecheckfiles"), notice_yesno (get_string("deletecheckfiles"),
"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=delete&confirm=1", "".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=delete&confirm=1",
"".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=cancel"); "".basename($_SERVER['PHP_SELF'])."?id=$id&wdir=$wdir&action=cancel");
} else { } else {
Expand Down Expand Up @@ -324,7 +322,7 @@ function reset_value() {
echo "Error: could not rename $oldname to $name"; echo "Error: could not rename $oldname to $name";
} }
displaydir($wdir); displaydir($wdir);

} else { } else {
$strrename = get_string("rename"); $strrename = get_string("rename");
$strcancel = get_string("cancel"); $strcancel = get_string("cancel");
Expand Down Expand Up @@ -362,7 +360,7 @@ function reset_value() {
echo "Error: could not create $name"; echo "Error: could not create $name";
} }
displaydir($wdir); displaydir($wdir);

} else { } else {
$strcreate = get_string("create"); $strcreate = get_string("create");
$strcancel = get_string("cancel"); $strcancel = get_string("cancel");
Expand Down Expand Up @@ -396,7 +394,7 @@ function reset_value() {
fputs($fileptr, stripslashes($text)); fputs($fileptr, stripslashes($text));
fclose($fileptr); fclose($fileptr);
displaydir($wdir); displaydir($wdir);

} else { } else {
$streditfile = get_string("edit", "", "<B>$file</B>"); $streditfile = get_string("edit", "", "<B>$file</B>");
$fileptr = fopen($basedir.$file, "r"); $fileptr = fopen($basedir.$file, "r");
Expand Down Expand Up @@ -435,7 +433,7 @@ function reset_value() {
echo "</FORM>"; echo "</FORM>";
echo "</TD></TR></TABLE>"; echo "</TD></TR></TABLE>";


if ($usehtmleditor) { if ($usehtmleditor) {
print_richedit_javascript("form", "text", "yes"); print_richedit_javascript("form", "text", "yes");
} }


Expand Down Expand Up @@ -469,7 +467,7 @@ function reset_value() {
} }
clearfilelist(); clearfilelist();
displaydir($wdir); displaydir($wdir);

} else { } else {
html_header($course, $wdir, "form.name"); html_header($course, $wdir, "form.name");


Expand Down Expand Up @@ -545,7 +543,7 @@ function reset_value() {
} }
echo "</table>"; echo "</table>";
} }

} else { // Use external unzip program } else { // Use external unzip program
print_simple_box_start("center"); print_simple_box_start("center");
echo "<PRE>"; echo "<PRE>";
Expand Down Expand Up @@ -614,7 +612,7 @@ function reset_value() {
} }
html_footer(); html_footer();
break; break;

case "cancel"; case "cancel";
clearfilelist(); clearfilelist();


Expand All @@ -629,23 +627,23 @@ function reset_value() {
/// FILE FUNCTIONS /////////////////////////////////////////////////////////// /// FILE FUNCTIONS ///////////////////////////////////////////////////////////




function fulldelete($location) { function fulldelete($location) {
if (is_dir($location)) { if (is_dir($location)) {
$currdir = opendir($location); $currdir = opendir($location);
while ($file = readdir($currdir)) { while ($file = readdir($currdir)) {
if ($file <> ".." && $file <> ".") { if ($file <> ".." && $file <> ".") {
$fullfile = $location."/".$file; $fullfile = $location."/".$file;
if (is_dir($fullfile)) { if (is_dir($fullfile)) {
if (!fulldelete($fullfile)) { if (!fulldelete($fullfile)) {
return false; return false;
} }
} else { } else {
if (!unlink($fullfile)) { if (!unlink($fullfile)) {
return false; return false;
} }
} }
} }
} }
closedir($currdir); closedir($currdir);
if (! rmdir($location)) { if (! rmdir($location)) {
return false; return false;
Expand Down Expand Up @@ -693,14 +691,14 @@ function printfilelist($filelist) {
echo "<img src=\"$CFG->pixpath/f/folder.gif\" height=\"16\" width=\"16\"> $file<br />"; echo "<img src=\"$CFG->pixpath/f/folder.gif\" height=\"16\" width=\"16\"> $file<br />";
$subfilelist = array(); $subfilelist = array();
$currdir = opendir($basedir.$file); $currdir = opendir($basedir.$file);
while ($subfile = readdir($currdir)) { while ($subfile = readdir($currdir)) {
if ($subfile <> ".." && $subfile <> ".") { if ($subfile <> ".." && $subfile <> ".") {
$subfilelist[] = $file."/".$subfile; $subfilelist[] = $file."/".$subfile;
} }
} }
printfilelist($subfilelist); printfilelist($subfilelist);


} else { } else {
$icon = mimeinfo("icon", $file); $icon = mimeinfo("icon", $file);
echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\"> $file<br />"; echo "<img src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\"> $file<br />";
} }
Expand Down Expand Up @@ -747,7 +745,7 @@ function displaydir ($wdir) {
if ($file == "." || $file == "..") { if ($file == "." || $file == "..") {
continue; continue;
} }

if (is_dir($fullpath."/".$file)) { if (is_dir($fullpath."/".$file)) {
$dirlist[] = $file; $dirlist[] = $file;
} else { } else {
Expand Down Expand Up @@ -776,7 +774,7 @@ function displaydir ($wdir) {




echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=post NAME=dirform>"; echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" METHOD=post NAME=dirform>";
echo "<TABLE BORDER=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"100%\">"; echo "<TABLE BORDER=\"0\" cellspacing=\"2\" cellpadding=\"2\" width=\"100%\">";


if ($wdir == "/") { if ($wdir == "/") {
$wdir = ""; $wdir = "";
Expand All @@ -790,7 +788,7 @@ function displaydir ($wdir) {
print "<img src=\"$CFG->wwwroot/lib/editor/images/folderup.gif\" height=\"14\" width=\"24\" border=\"0\" ALT=\"Move up\">"; print "<img src=\"$CFG->wwwroot/lib/editor/images/folderup.gif\" height=\"14\" width=\"24\" border=\"0\" ALT=\"Move up\">";
print "</a></td></tr>\n"; print "</a></td></tr>\n";
} }

$count = 0; $count = 0;


if (!empty($dirlist)) { if (!empty($dirlist)) {
Expand All @@ -803,7 +801,7 @@ function displaydir ($wdir) {
$fileurl = rawurlencode($wdir."/".$dir); $fileurl = rawurlencode($wdir."/".$dir);
$filesafe = rawurlencode($dir); $filesafe = rawurlencode($dir);
$filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p"); $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");

echo "<TR>"; echo "<TR>";


if ($usecheckboxes) { if ($usecheckboxes) {
Expand All @@ -812,7 +810,7 @@ function displaydir ($wdir) {
print_cell("left", "<a href=\"".basename($_server['php_self'])."?id=$id&wdir=$fileurl\" onclick=\"return reset_value();\"><img src=\"$CFG->pixpath/f/folder.gif\" height=16 width=16 border=0 alt=\"folder\"></a> <a href=\"".basename($_server['php_self'])."?id=$id&wdir=$fileurl&usecheckboxes=$usecheckboxes\" onclick=\"return reset_value();\">".htmlspecialchars($dir)."</a>"); print_cell("left", "<a href=\"".basename($_server['php_self'])."?id=$id&wdir=$fileurl\" onclick=\"return reset_value();\"><img src=\"$CFG->pixpath/f/folder.gif\" height=16 width=16 border=0 alt=\"folder\"></a> <a href=\"".basename($_server['php_self'])."?id=$id&wdir=$fileurl&usecheckboxes=$usecheckboxes\" onclick=\"return reset_value();\">".htmlspecialchars($dir)."</a>");
print_cell("right", "&nbsp;"); print_cell("right", "&nbsp;");
print_cell("right", $filedate); print_cell("right", $filedate);

echo "</TR>"; echo "</TR>";
} }
} }
Expand All @@ -831,7 +829,7 @@ function displaydir ($wdir) {
$filesafe = rawurlencode($file); $filesafe = rawurlencode($file);
$fileurlsafe = rawurlencode($fileurl); $fileurlsafe = rawurlencode($fileurl);
$filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p"); $filedate = userdate(filemtime($filename), "%d %b %Y, %I:%M %p");

$dimensions = get_image_size($filename); $dimensions = get_image_size($filename);
if($dimensions) { if($dimensions) {
$imgwidth = $dimensions[0]; $imgwidth = $dimensions[0];
Expand All @@ -852,25 +850,14 @@ function displaydir ($wdir) {
} else { } else {
$ffurl = "/file.php?file=/$id$fileurl"; $ffurl = "/file.php?file=/$id$fileurl";
} }
link_to_popup_window ($ffurl, "display", link_to_popup_window ($ffurl, "display",
"<img src=\"$CFG->pixpath/f/$icon\" height=16 width=16 border=0 align=\"absmiddle\" alt=\"$strfile\">", "<img src=\"$CFG->pixpath/f/$icon\" height=16 width=16 border=0 align=\"absmiddle\" alt=\"$strfile\">",
480, 640); 480, 640);
$file_size = filesize($filename); $file_size = filesize($filename);
?>
<script language="javascript" type="text/javascript"> echo "<a onclick=\"return set_value(info = {url: '".$CFG->wwwroot.$ffurl."',";
<!-- echo " isize: '".$file_size."', itype: '".$imgtype."', iwidth: '".$imgwidth."',";
var a<?php echo $count; ?> = new Array(); echo " iheight: '".$imgheight."', imodified: '".$filedate."' })\" href=\"#\">$file</a>";
a<?php echo $count; ?>['url'] = "<?php print($CFG->wwwroot.$ffurl);?>";
a<?php echo $count; ?>['isize'] = "<?php print(display_size($file_size));?>";
a<?php echo $count; ?>['itype'] = "<?php echo $imgtype; ?>";
a<?php echo $count; ?>['iwidth'] = "<?php echo $imgwidth; ?>";
a<?php echo $count; ?>['iheight'] = "<?php echo $imgheight; ?>";
a<?php echo $count; ?>['imodified'] = "<?php echo $filedate; ?>";

// -->
</script>
<?php
echo "<a onclick=\"return set_value(a".$count.")\" href=\"#\">$file</a>";
echo "<!-- </font> --></td>"; echo "<!-- </font> --></td>";


if ($icon == "zip.gif") { if ($icon == "zip.gif") {
Expand All @@ -881,7 +868,7 @@ function displaydir ($wdir) {
} }
print_cell("right", "$edittext "); print_cell("right", "$edittext ");
print_cell("right", $filedate); print_cell("right", $filedate);

echo "</TR>"; echo "</TR>";
} }
} }
Expand All @@ -891,7 +878,7 @@ function displaydir ($wdir) {
$wdir = "/"; $wdir = "/";
} }


echo "<TABLE BORDER=0 cellspacing=2 cellpadding=2>"; echo "<TABLE BORDER=0 cellspacing=2 cellpadding=2>";
echo "<TR><TD>"; echo "<TR><TD>";
echo "<INPUT TYPE=hidden NAME=id VALUE=\"$id\">"; echo "<INPUT TYPE=hidden NAME=id VALUE=\"$id\">";
echo "<INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\"> "; echo "<INPUT TYPE=hidden NAME=wdir VALUE=\"$wdir\"> ";
Expand Down

0 comments on commit cfa3f26

Please sign in to comment.