File:  [LON-CAPA] / loncom / interface / lonsource.pm
Revision 1.8: download - view: text, annotated - select for diffs
Thu Jul 8 20:17:59 2004 UTC (19 years, 9 months ago) by taceyjo1
Branches: MAIN
CVS tags: version_1_3_X, version_1_3_3, version_1_3_2, version_1_3_1, version_1_3_0, version_1_2_X, version_1_2_99_1, version_1_2_99_0, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, HEAD
Cuddled else's AND elsif's

    1: # The LearningOnline Network with CAPA
    2: # Souce Code handler
    3: #
    4: # $Id: lonsource.pm,v 1.8 2004/07/08 20:17:59 taceyjo1 Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: 
   31: package Apache::lonsource;
   32: 
   33: use strict;
   34: use Apache::lonnet();
   35: use Apache::loncommon();
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonsequence();
   38: use Apache::Constants qw(:common :http);
   39: use Apache::lonmeta;
   40: use Apache::File;
   41: use Apache::lonlocal;
   42: use HTML::Entities;
   43: 
   44: sub make_link {
   45:     my ($filename, $listname) = @_;
   46:     my $sourcelink = "http://".$ENV{'SERVER_NAME'}.
   47:                      "/adm/source/?filename=".$filename."&listname=".$listname;
   48: 
   49:     return $sourcelink;
   50: }
   51: 
   52: sub stage_2 {
   53:     my ($r, $filename, $author, $listname) = @_;
   54:     $filename = $filename;
   55:     &Apache::loncommon::content_type($r,'text/html');
   56:     my ($uname, $udom) = &Apache::loncacc::constructaccess('/~'.$author.'/',$r->dir_config('lonDefDomain'));
   57:     $r->send_http_header;
   58:     $r->print('<html><head><title>LON-CAPA Move source to construction space</title>');
   59:     $r->print(&Apache::loncommon::bodytag('Problem source code moving operation'));
   60:     $r->print("Please enter the directory that you would like the source code to go into, a default has also been 			provided <br />");
   61:     $r->print("Also note, the path is in reference to the root of your construction space, and new directories will be 			automatically created. <br /><br />");
   62:     $r->print('<form name="copy" action="/adm/source/" target="_parent" method="post">
   63:               <input type="hidden" name="filename" value="'.$filename.'" />
   64:               <input type="hidden" name="listname" value="'.$listname.'" />
   65:               <input type="hidden" name="action" value="copy_stage" />
   66:               <input type="text" name="newpath" value="/shared_source/'.$author.'" />&nbsp;
   67:               <input type="submit" value="Copy" />
   68:               </form>');
   69:     return OK;
   70: }
   71: 
   72: 
   73: sub copy_stage {
   74:     my ($r, $filename, $listname, $newpath) = @_;
   75:     my $role;
   76:     my $domain;
   77:     my $author_name;
   78: #Figure out if we are author or co-author
   79: 
   80:     if($ENV{'request.role'} =~ m|ca.|) {
   81:         ($role, $domain, $author_name) = split(/\//,$ENV{'request.role'});
   82:     } else {
   83:         $role = "au.";
   84:         $domain = $ENV{'user.domain'};
   85:         $author_name = $ENV{'user.name'};
   86:     }
   87: 
   88:     my $path_to_new_file = '/home/'.$author_name.'/public_html/'.$newpath.'/'.$listname;
   89: #Just checking again for access as we want to make sure that it is really ok now that we have the real path
   90: 
   91:     my ($uname,$udom)= &Apache::loncacc::constructaccess($path_to_new_file,$domain);
   92:     unless (($uname) && ($udom)) {
   93:         return HTTP_NOT_ACCEPTABLE;
   94:     }
   95:     &Apache::loncommon::content_type($r,'text/html');
   96:     $r->send_http_header;
   97:     $r->print('<html><head><title>LON-CAPA Move source to construction space</title>');
   98:     $r->print(&Apache::loncommon::bodytag('Copying Source'));
   99:     my $result = &Apache::loncfile::exists($uname, $udom, $path_to_new_file);
  100:     $r->print($result);
  101:     if(($result) && ($result =~ m|published|) ) {
  102:         &delete_copy_file($r, $author_name, $newpath, $filename, $path_to_new_file, '1');
  103:     } elsif(($result) && ($result =~ m|exists!|)) {
  104:         &confirm($r, $author_name, $newpath, $filename, $path_to_new_file);
  105:     } else {
  106:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
  107:     }
  108: 
  109:     return OK;
  110: 
  111: }
  112: 
  113: sub confirm {
  114:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
  115:     $r->print("<b>Press delete to remove file and replace it with a copy of the source you are viewing</b><br /><br /	>");
  116:     $r->print('<form name="delete_confirm" action="/adm/source/" target="_parent" method="post">
  117:               <input type="hidden" name="filename" value="'.$filename.'" />
  118:               <input type="hidden" name="path" value="'.$path_to_new_file.'" />
  119:               <input type="hidden" name="author" value="'.$author_name.'" />
  120:               <input type="hidden" name="newpath" value="'.$newpath.'" />
  121:               <input type="hidden" name="action" value="delete_confirm" />
  122: 
  123:               <input type="submit" value="Delete" />
  124:               </form>');
  125: }
  126: 
  127: sub delete_copy_file {
  128:     my ($r, $author_name, $newpath, $filename, $path_to_new_file, $type) = @_;
  129:     if($type eq '1') {
  130:         $r->print("<b>Cannot delete non-obsolete published file</b><br />Please
  131:               use the code view in previous window to use shared code<br /><br />");
  132:         $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()" />');
  133:     } else {
  134:         if(-e $path_to_new_file) {
  135:             unless(unlink($path_to_new_file)) {
  136:                 $r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
  137:                 return 0;
  138:             }
  139:         } else {
  140:             $r->print('<p> '.&mt('No such file').'. </p></form>');
  141:             return 0;
  142:         }
  143:         &copy_file($r, $author_name, $newpath, $filename, $path_to_new_file);
  144:     }
  145: }
  146: 
  147: sub copy_file {
  148:     my ($r, $author_name, $newpath, $filename, $path_to_new_file) = @_;
  149:     $r->print("<b>Creating directories</b>");
  150:     my $path = '/home/'.$author_name.'/public_html/';
  151:     my @directories = split(/\//,$newpath);
  152:     foreach my $now_checking (@directories) {
  153:         if($now_checking ne '') {
  154:             $path = $path.'/'.$now_checking;
  155:             if(-e $path) {} #More moving along, isn't recursion fun'
  156: 
  157:             else {
  158:                 unless(mkdir($path, 02770)) {
  159:                     $r->print('<font color="red">'.&mt('Error').': '.$!.'</font>');
  160:                     return 0;
  161:                 }
  162:                 unless(chmod(02770, ($path))) {
  163:                     $r->print('<font color="red"> '.&mt('Error').': '.$!.'</font>');
  164:                     return 0;
  165:                 }
  166:             }
  167:         } else { } #Just move along
  168: 
  169:     }
  170:     $r->print("<br /><b>Copying File</b>");
  171:     my $problem_filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  172:     my $file_output = &Apache::lonnet::getfile($problem_filename);
  173:     my $fs=Apache::File->new(">$path_to_new_file");
  174:     if (defined($fs)) {
  175:         print $fs $file_output;
  176:     }
  177:     $r->print("<br /><br />");
  178:     $r->print('<input type="button" value="Close Window" name="close" onClick="window.close()">');
  179:     #Some 1.3'ish feature is to include the derivative feature, will go here..'
  180: }
  181: 
  182: sub print_item {
  183:     my ($r, $filename) = @_;
  184:     $filename = $Apache::lonnet::perlvar{'lonDocRoot'}.$filename;
  185:     &Apache::lonnet::logthis("print_item filename = $filename");
  186:     my $file_output = &Apache::lonnet::getfile($filename);
  187:     my $count=0;
  188:     my $maxlength=-1;
  189:     foreach (split ("\n", $file_output)) {
  190:         $count+=int(length($_)/79);
  191:         $count++;
  192:         if (length($_) > $maxlength) {
  193:             $maxlength = length($_);
  194:         }
  195:     }
  196:     my $rows = $count;
  197:     my $cols = $maxlength;
  198:     $r->print('<textarea rows="'.$rows.'" cols="'.$cols.'" name="editxmltext">'.
  199:               &HTML::Entities::encode($file_output,'<>&"').'</textarea>');
  200:     return OK;
  201: 
  202: }
  203: 
  204: 
  205: sub handler {
  206:     my $r=shift;
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['filename','listname']);
  209:     my $filename = $ENV{'form.filename'};
  210:     my $listname = $ENV{'form.listname'};
  211:     my $source = &Apache::lonnet::metadata($filename,'sourceavail');
  212:     if ($source ne 'open') {
  213:         $ENV{'user.error.msg'}="$filename:cre:1:1:Source code not available";
  214:         return HTTP_NOT_ACCEPTABLE;
  215:     } 
  216:     if ((!&Apache::lonnet::allowed('cre',$filename)) ||
  217:             (!&Apache::lonnet::allowed('bre',$filename))) {
  218:         $ENV{'user.error.msg'}="$filename:bre:1:1:Access to resource denied";
  219:         return HTTP_NOT_ACCEPTABLE;
  220:     } 
  221:     if ($ENV{'form.action'} eq 'stage2') {
  222:         my $author = &Apache::lonnet::metadata($filename,'authorspace');
  223:         $author =~ m|@|;
  224:         $author = $`; #This just tells who the author name is for later processing.
  225:         &stage_2($r, $ENV{'form.filename'}, $author, $listname);
  226:     } elsif($ENV{'form.action'} eq 'copy_stage') {
  227:         &copy_stage($r, $filename,$ENV{'form.listname'},$ENV{'form.newpath'});
  228:     } elsif($ENV{'form.action'} eq 'delete_confirm') {
  229:         &Apache::loncommon::content_type($r,'text/html');
  230:         $r->send_http_header;
  231:         &delete_copy_file($r, $ENV{'form.author'}, $ENV{'form.newpath'}, $ENV{'form.filename'}, $ENV{'form.path'}, '0');
  232:     } else {
  233:         &Apache::loncommon::content_type($r,'text/html');
  234:         $r->send_http_header;
  235:         $r->print('<form name="copy" action="/adm/source/" target="_parent" method="post">
  236:                   <input type="button" value="Close Window" name="close" onClick="window.close()" />
  237:                   <input type="hidden" name="filename" value="'.$filename.'" />
  238:                   <input type="hidden" name="listname" value="'.$listname.'" />
  239:                   <input type="hidden" name="action" value="stage2" />
  240:                   <input type="submit" value="Copy to CSTR" />
  241:                   </form>');
  242:         $r->print('<hr />');
  243:         &print_item($r, $ENV{'form.filename'});
  244:     }
  245:     return OK;
  246: }
  247: 
  248: 1;
  249: 
  250: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>