[system] / trunk / webwork / system / cgi / cgi-scripts / profEditCourseFiles.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/cgi/cgi-scripts/profEditCourseFiles.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 151 - (download) (as text) (annotate)
Fri Aug 17 21:48:01 2001 UTC (11 years, 9 months ago) by sh002i
File size: 10627 byte(s)
Added recursiveFindFiles subroutine (duplicate!), expanded pattern match
to only show *header*.pg files rather than *.pg files.

    1 #!/usr/local/bin/webwork-perl
    2 
    3 ## This file is profEditSetDef.pl
    4 ## It provides a utility for professors to edit set definition files
    5 ## and their course's webworkCourse.ph
    6 
    7 use lib '.'; use webworkInit; # WeBWorKInitLine
    8 
    9 use Global;
   10 use CGI qw(:standard);
   11 use Auth;
   12 use Net::SMTP;
   13 use HTML::Entities;
   14 use strict;
   15 
   16 my $cgi = new CGI;
   17 my %inputs = $cgi->Vars();
   18 
   19 #for use in strings where $cgi->param is not interpolated correctly or at all
   20 my $course = $cgi->param('course');
   21 my $user = $cgi->param('user');
   22 my $session_key = $cgi->param('key');
   23 
   24 my $currentFile = $cgi->param('filename');
   25 my $currentDir = $cgi->param('dir'); # relative to extDir below
   26 $currentDir ='' unless defined $currentDir;
   27 my $currentExt = $cgi->param('ext'); # this is the expected extension
   28 
   29 # edit these to add functionality for other file types
   30 my %validExt = ('def' => 1,
   31     'ph' => 1,
   32     'pg' => 1);
   33 
   34 my %extName = ( 'def' => "Set Definition",
   35     'ph' => "Course Environment",
   36     'pg' => "Set Header");
   37 
   38 my %extDir = (  'def' => "/templates/",
   39     'ph' => "/",
   40     'pg' => "/templates/");
   41 
   42 &Global::getCourseEnvironment($course);
   43 
   44 my $scriptDirectory   = $Global::scriptDirectory;
   45 my $databaseDirectory     = $Global::databaseDirectory;
   46 my $htmlURL     = $Global::htmlURL;
   47 my $cgiURL      = $Global::cgiWebworkURL;
   48 my $courseDirectory   = $Global::coursesDirectory . $course;
   49 my $courseScriptsDirectory  = $Global::courseScriptsDirectory;
   50 my $templateDirectory     = getCourseTemplateDirectory;
   51 my $feedbackAddress   = $Global::feedbackAddress;
   52 
   53 require "${scriptDirectory}$Global::FILE_pl";
   54 require "${scriptDirectory}$Global::classlist_DBglue_pl";
   55 require "${scriptDirectory}$Global::HTMLglue_pl";
   56 require "${scriptDirectory}$Global::FILE_pl";
   57 
   58 # log access
   59 &Global::log_info('', query_string);
   60 
   61 my $permissionsFile = &Global::getCoursePermissionsFile($course);
   62 my $permissions = &get_permissions($user, $permissionsFile);
   63 my $keyFile = &Global::getCourseKeyFile($course);
   64 
   65 
   66 #verify session key
   67 &verify_key($user, $session_key, "$keyFile", $course);
   68 
   69 #verify permissions are correct
   70 if (($permissions != $Global::instructor_permissions) and ($permissions != $Global::TA_permissions) ) {
   71   print "permissions = $permissions instructor_permissions = $Global::instructor_permissions\n";
   72   print &html_NO_PERMISSION;
   73   exit(0);
   74   }
   75 
   76 # verify that the file type is valid
   77 if (!($currentFile =~ /.*\.(.*)/) || ($1 ne $currentExt) ) {
   78   &user_error("The file $currentFile is not valid because it does not contain the expected ending: $currentExt");
   79 }
   80 
   81 # consolidate relative directory paths into one hard path
   82 $currentDir = $courseDirectory . $extDir{$currentExt} . $currentDir;
   83 
   84 #wwerror("current Dir: $currentDir<P>current File:$currentFile <P>current Ext: $currentExt");
   85 # make sure requested file exists and is a valid file
   86   if (defined($currentFile) && -e "$currentDir$currentFile") {
   87     unless ( -R "$currentDir$currentFile") {
   88       wwerror ('File is not readable', "The file $currentFile is not readable by the webserver. Check that it's permissions are set correctly.");
   89     }
   90   } else {
   91     wwerror('File does not exist', "The file $currentDir$currentFile does appear to exist, please check the filename and try again.");
   92   }
   93 
   94 # get row and column info if submitted
   95   my $rows = (defined($cgi->param('rows'))) ? $cgi->param('rows') : $Global::editor_window_rows;
   96   my $columns = (defined($cgi->param('columns'))) ? $cgi->param('columns') : $Global::editor_window_columns;
   97 
   98 # Deal with filled out forms and various actions resulting from different buttons
   99   if ( defined($cgi->param('action')) ) {
  100     if (defined($cgi->param('savefilename'))) {
  101       &user_error("For security reasons, you cannot save a file in any directory higher than the current directory.  Please specify a file name to save under.") if ($cgi->param('savefilename') =~ /^[~.]/ || $cgi->param('savefilename') =~ /\.\./);
  102     }
  103 
  104     #if Save button was clicked
  105     if (( $cgi->param('action') eq 'Save') && defined($cgi->param('body')) && defined($cgi->param('savefilename'))) {
  106 
  107       my $temp_body = $cgi->param('body');
  108       $temp_body =~ s/\r\n/\n/g; #html textareas tend to leave \r
  109 
  110 #     saveProblem($temp_body, $currentDir . $cgi->param('savefilename'));
  111       saveProblem($temp_body, $cgi->param('savefilename'));
  112       $currentFile = $cgi->param('savefilename');
  113 
  114     #if Save As button was clicked
  115     } elsif (( $cgi->param('action') eq 'Save as') && defined($cgi->param('body')) && defined($cgi->param('savefilename'))) {
  116 
  117       $currentFile = $cgi->param('savefilename');
  118 
  119 #     if ($currentFile =~ /^[~.]/ || $currentFile =~ /\.\./) {
  120 #       &user_error("For security reasons, you cannot specify a merge file from a directory higher than the email directory (you can't use ../blah/blah).  Please specify a different file or move the needed file to the email directory");
  121 #     }
  122 
  123 
  124       my $temp_body = $cgi->param('body');
  125       $temp_body =~ s/\r\n/\n/g;
  126 
  127       saveNewProblem($temp_body, $currentFile);
  128 
  129     }
  130   }
  131 
  132 
  133 
  134 #Begin Page
  135   print &htmlTOP("Edit $extName{$currentExt} File(s) for $course"),
  136     $cgi->a( { -href=>"${cgiURL}login.pl?user=$user&key=$session_key&course=$course" },
  137 
  138       $cgi->img({ -name=>'upImg',
  139             -src=>"${Global::upImgUrl}",
  140             -align=>'right',
  141             -border=>'1',
  142             -alt=>'[Up]'
  143           })
  144       ),
  145   $cgi->p;
  146 
  147   print "\n",
  148    $cgi->hr, $cgi->br,
  149    "\n\n", $cgi->h3({ -align=>'left' }, "WeBWorK $extName{$currentExt} File(s) Editor for $course"), "\n",
  150    $cgi->p,
  151    "From this page, you can edit your $extName{$currentExt} file(s).  You can both save over
  152    old files and create new ones.  You can even open one file, make changes and then save those
  153    changes under a different file name.",
  154    $cgi->hr, "\n";
  155 
  156 
  157 # start form with hidden entries to pass info back to profEditCourseFiles.pl
  158 # the last two are very circular but are necessary to make sure the script
  159 # continues to know which kind of file it is dealing with
  160 # They should be set by the original call (most likely in profLogin.pl
  161   print $cgi->startform(-action=>"${cgiURL}profEditCourseFiles.pl"), "\n",
  162      $cgi->hidden(-name=>"user", -value=>$user), "\n",
  163      $cgi->hidden(-name=>"key", -value=>$session_key), "\n",
  164      $cgi->hidden(-name=>"course", -value=>$course), "\n",
  165 
  166      $cgi->hidden(-name=>"dir", -value=>$cgi->param("dir")), "\n",
  167      $cgi->hidden(-name=>"ext", -value=>$cgi->param("ext")), "\n";
  168 
  169 #get current file
  170   my ($text, @text);
  171   if (-e "$currentDir$currentFile") {
  172     open FILE, "$currentDir$currentFile";
  173     @text = <FILE>;
  174     $text = join "", @text;
  175   } else {
  176     # wwerror($0, "Message File $currentDir$currentFile does not exist!");
  177   }
  178 
  179 #print actual body of message
  180   print "\n", $cgi->br, $cgi->textarea(-name=>'body', -default=>$text, -rows=>$rows, -columns=>$columns, -override=>1);
  181 
  182 
  183 #get all message files and create a list
  184 #   opendir CURDIR, $currentDir; # or wwerror($0, "Can't open directory $currentDir","","");
  185 #     my @filenames = grep /\.$currentExt$/, readdir CURDIR;
  186 #   closedir CURDIR;
  187 #
  188 #   @filenames = sort @filenames;
  189 
  190   my @filenames = recursiveFindFiles($currentDir);
  191   if($currentExt eq "pg") { @filenames = grep /\.*header\.*.pg$/i, @filenames; }
  192   else { @filenames = grep /\.$currentExt$/, @filenames; }
  193   map s|$templateDirectory/?||, @filenames;
  194   @filenames = sort @filenames;
  195 
  196   print $cgi->p, $cgi->popup_menu(-name=>'filename', -values=>\@filenames, -default=>$currentFile);
  197 
  198 
  199 #create all necessary action buttons
  200   print $cgi->submit(-name=>'action', -value=>'Open'), "\n", $cgi->br,
  201      $cgi->textfield(-name=>'savefilename', -size => 20, -value=> "$currentFile", -override=>1), ' ',
  202      $cgi->submit(-name=>'action', -value=>'Save'), " \n",
  203      $cgi->submit(-name=>'action', -value=>'Save as'), " \n",
  204      'For "Save As" choose a new filename.', $cgi->br, $cgi->br,
  205      $cgi->p, $cgi->submit(-name=>'action', -value=>'Revert to original and Resize message window'),
  206      " Rows: ", $cgi->textfield(-name=>'rows', -size=>3, -value=>$rows),
  207      " Columns: ", $cgi->textfield(-name=>'columns', -size=>3, -value=>$columns),
  208      $cgi->br, "If you resize the message window, you will lose all unsaved changes and the students you have selected may be unselected.",
  209      $cgi->end_form,
  210      &htmlBOTTOM("profEditCourseFiles", \%inputs);
  211 
  212 # End of HTML
  213 
  214 
  215 ###### SUBROUTINES ######
  216 sub saveProblem {
  217   my ($body, $probFileName)= @_;
  218 
  219   open (PROBLEM, ">$currentDir$probFileName") ||
  220     wwerror($0, "Could not open $currentDir$probFileName for writing.
  221     Check that the  permissions for this problem are 660 (-rw-rw----)");
  222   print PROBLEM $body;
  223   close PROBLEM;
  224   chmod 0660, "$currentDir$probFileName" ||
  225                print "Content-type: text/html\n\n
  226                       CAN'T CHANGE PERMISSIONS ON FILE $currentDir$probFileName";
  227 
  228 }
  229 
  230 sub saveNewProblem {
  231   my ($body, $new_file_name)= @_;
  232  #######check that the new file name doesn't exist
  233   if (-e "$currentDir$new_file_name" ) {
  234     wwerror("Can not use this file name", "The file\n".
  235     "$new_file_name\n".
  236     "already exists.\n" .
  237     "<b>The new version was not saved.</b>\n" .
  238     "Go back and choose a different file name or\, if you really want to edit\n".
  239     "$new_file_name\,\n".
  240     "go back and hit the \&quot;Save updated version\&quot; button.");
  241   }
  242 
  243   wwerror ("Invalid file name", "The file name \"$new_file_name\" does not have a \".$currentExt\" extension.
  244 <b>The file was not saved.</b>
  245 Go back and choose a file name with a \".$currentExt\" extension.") unless
  246     $new_file_name =~ m|\.$currentExt$|;
  247  #######copy new version to the file new_file_name
  248   open (PROBLEM, ">$currentDir$new_file_name") ||
  249     wwerror($0, "Could not open $currentDir$new_file_name for writing.
  250     Check that the  permissions for the directory $currentDir are 770 (drwxrwx---)");
  251   print PROBLEM $body;
  252   close PROBLEM;
  253   chmod 0660, "$currentDir$new_file_name" ||
  254                print "Content-type: text/html\n\n
  255                       CAN'T CHANGE PERMISSIONS ON FILE $currentDir$new_file_name";
  256 
  257 }
  258 
  259 sub user_error {
  260     my $msg = join " ", @_;
  261     print $cgi->header,
  262   $cgi->start_html('-title' => 'User error'),
  263   $cgi->h1('User error'),
  264   $cgi->p,
  265   $cgi->b(HTML::Entities::encode($msg)),
  266   $cgi->p,
  267         "Please hit the &quot;<B>Back</B>&quot; button on your browser to ",
  268   "try again, or notify ", $cgi->br,
  269   "&lt;", $cgi->a({href=>"mailto:$Global::webmaster"}, $Global::webmaster), "&gt; ",
  270   "if you believe this message is in error.",
  271   $cgi->end_html;
  272     exit(1);
  273 }
  274 
  275 
  276 sub recursiveFindFiles
  277 {
  278   my $dir = shift;
  279   $dir .= '/' unless $dir =~ m|/$|;
  280   opendir DIR, $dir;
  281   my @items = readdir DIR;
  282   closedir DIR;
  283   my @result;
  284   foreach (@items) {
  285     /^\./ and next;
  286     -f "$dir$_" and push @result, "$dir$_";
  287     -d "$dir$_" and push @result, recursiveFindFiles("$dir$_");
  288   }
  289   return @result;
  290 }

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9