File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.159: download - view: text, annotated - select for diffs
Thu Jul 31 19:25:39 2014 UTC (9 years, 10 months ago) by musolffc
Branches: MAIN
CVS tags: HEAD
Excluding display of .DAV files

    1: # The LearningOnline Network with CAPA
    2: # Authoring Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.159 2014/07/31 19:25:39 musolffc 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: package Apache::lonpubdir;
   31: 
   32: use strict;
   33: use Apache::File;
   34: use File::Copy;
   35: use Apache::Constants qw(:common :http :methods);
   36: use Apache::loncommon();
   37: use Apache::lonhtmlcommon();
   38: use Apache::londiff();
   39: use Apache::lonlocal;
   40: use Apache::lonmsg;
   41: use Apache::lonmenu;
   42: use Apache::lonnet;
   43: use LONCAPA qw(:DEFAULT :match);
   44: 
   45: sub handler {
   46: 
   47:     my $r=shift;
   48: 
   49:     # Validate access to the construction space and get username:domain.
   50: 
   51:     my ($uname,$udom)=&Apache::lonnet::constructaccess($r->uri); 
   52:     unless (($uname) && ($udom)) {
   53:         return HTTP_NOT_ACCEPTABLE;
   54:     }
   55: 
   56: # ----------------------------------------------------------- Start page output
   57: 
   58:     my $fn=$r->filename;
   59:     $fn=~s/\/$//;
   60:     my $thisdisfn=$fn;
   61: 
   62:     my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
   63:     if ($thisdisfn eq "$docroot/priv/$udom") {
   64:         if ((-d "/home/$uname/public_html/") && (!-e "$docroot/priv/$udom/$uname")) {
   65:             my ($version) = ($r->dir_config('lonVersion') =~ /^\'?(\d+\.\d+)\./);
   66:             &Apache::loncommon::content_type($r,'text/html');
   67:             $r->send_http_header;
   68: 
   69:             &Apache::lonhtmlcommon::clear_breadcrumbs();
   70:             $r->print(&Apache::loncommon::start_page('Authoring Space').
   71:                       '<div class="LC_error">'.
   72:                       '<br /><p>'.
   73:                       &mt('Your Authoring Space is currently in the location used by LON-CAPA version 2.10 and older, but your domain is using a newer LON-CAPA version ([_1]).',$version).'</p>'.
   74:                       '<p>'.
   75:                       &mt('Please ask your Domain Coordinator to move your Authoring Space to the new location.').
   76:                       '</p>'.
   77:                       '</div>'.
   78:                       &Apache::loncommon::end_page());
   79:             return OK;
   80:         }
   81:     }
   82:     $thisdisfn=~s/^\Q$docroot\E\/priv//;
   83:     
   84:     my $resdir=$docroot.'/res'.$thisdisfn; # Resource directory
   85:     my $targetdir='/res'.$thisdisfn; # Publication target directory.
   86:     my $linkdir='/priv'.$thisdisfn;      # Full URL name of constr space.
   87: 
   88:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   89: 
   90:     &startpage($r, $uname, $udom, $thisdisfn);  # Put out the start of page.
   91: 
   92:     if (!-d $fn) {
   93:         if (-e $fn) {
   94:             $r->print('<p class="LC_info">'.&mt('Requested item is a file not a directory.').'</p>');
   95:         } else {
   96:             $r->print('<p class="LC_info">'.&mt('The requested subdirectory does not exist.').'</p>');
   97:         }
   98:         $r->print(&Apache::loncommon::end_page());
   99:         return OK;
  100:     }
  101:     my @files;
  102:     if (opendir(DIR,$fn)) {
  103:         @files = grep(!/^\.+$/,readdir(DIR));
  104:         closedir(DIR);
  105:     } else {
  106:         $r->print('<p class="LC_error">'.&mt('Could not open directory.').'</p>');
  107:         $r->print(&Apache::loncommon::end_page());
  108:         return OK;
  109:     }
  110: 
  111:     &dircontrols($r,$uname,$udom,$thisdisfn);   # Put out actions for directory, 
  112:                                                 # browse/upload + new file page.
  113:     &resourceactions($r,$uname,$udom,$thisdisfn); # Put out form used for printing/deletion etc.
  114: 
  115:     my $numdir = 0;
  116:     my $numres = 0;
  117:   
  118:     # Retrieving value for "sortby" and "sortorder" from QUERY_STRING
  119:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  120:         ['sortby','sortorder']);
  121: 
  122:     # Sort by name as default, not reversed
  123:     if (! exists($env{'form.sortby'})) { $env{'form.sortby'} = 'filename' }
  124:     if (! exists($env{'form.sortorder'})) { $env{'form.sortorder'} = '' }
  125: 
  126:     my $sortby = $env{'form.sortby'};
  127:     my $sortorder = $env{'form.sortorder'};
  128: 
  129:     if ((@files == 0) && ($thisdisfn =~ m{^/$match_domain/$match_username})) {
  130:         if ($thisdisfn =~ m{^/$match_domain/$match_username$}) {
  131:             $r->print('<p class="LC_info">'.&mt('This Authoring Space is currently empty.').'</p>');
  132:         } else {
  133:             $r->print('<p class="LC_info">'.&mt('This subdirectory is currently empty.').'</p>');
  134:         }
  135:         $r->print(&Apache::loncommon::end_page());
  136:         return OK;
  137:     }
  138: 
  139:     # Start off the directory table.
  140:     $r->print(&Apache::loncommon::start_data_table()
  141:         .&Apache::loncommon::start_data_table_header_row()
  142:         .'<th><a href="'.$linkdir.'/?sortby=filetype&sortorder='
  143:             .((($sortby eq "filetype") && ($sortorder ne 'rev')) ? 'rev' : '') 
  144:             .'">'.&mt('Type')
  145:             .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  146:         .'<th>'.&mt('Actions').'</th>'
  147:         .'<th><a href="'.$linkdir.'/?sortby=filename&sortorder='
  148:             .((($sortby eq "filename") && ($sortorder ne 'rev')) ? 'rev' : '') 
  149:             .'">'.&mt('Name')
  150:             .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  151:         .'<th><a href="'.$linkdir.'/?sortby=title&sortorder='
  152:             .((($sortby eq "title") && ($sortorder ne 'rev')) ? 'rev' : '') 
  153:             .'">'.&mt('Title')
  154:             .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  155:         .'<th colspan="2"><a href="'.$linkdir.'/?sortby=pubstatus&sortorder='
  156:             .((($sortby eq "pubstatus") && ($sortorder ne 'rev')) ? 'rev' : '') 
  157:             .'">'.&mt('Status')
  158:             .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  159:         .'<th><a href="'.$linkdir.'/?sortby=cmtime&sortorder='
  160:             .((($sortby eq "cmtime") && ($sortorder ne 'rev')) ? 'rev' : '') 
  161:             .'">'.&mt('Last Modified')
  162:             .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  163:         .'<th><a href="'.$linkdir.'/?sortby=size&sortorder='
  164:             .((($sortby eq "size") && ($sortorder ne 'rev')) ? 'rev' : '') 
  165:             .'">'.&mt('Size').' (kB)'
  166:             .'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
  167:         .&Apache::loncommon::end_data_table_header_row()
  168:     );
  169: 
  170:     my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
  171:     my $filehash = {};
  172:     foreach my $filename (@files) {
  173:         # Skip .DS_Store, .DAV and hidden files
  174:         my ($extension) = ($filename=~/\.(\w+)$/);
  175:         next if (($filename eq '.DS_Store')
  176:                 || ($filename eq '.DAV')
  177:                 || (&Apache::loncommon::fileembstyle($extension) eq 'hdn')
  178:                 || ($filename =~ /^\._/));
  179: 
  180:         my ($cmode,$csize,$cmtime)=(stat($fn.'/'.$filename))[2,7,9];
  181:         my $linkfilename = &HTML::Entities::encode('/priv'.$thisdisfn.'/'.$filename,'<>&"');
  182:         # Identify type of file according to icon used
  183:         my ($filetype) = (&Apache::loncommon::icon($filename) =~ m{/(\w+).gif$}); 
  184:         my $cstr_dir = $r->dir_config('lonDocRoot').'/priv'.$thisdisfn;
  185:         my $meta_same = &isMetaSame($cstr_dir, $resdir, $filename);
  186:         
  187:         # Store size, title, and status for files but not directories
  188:         my $size = (!($cmode&$dirptr)) ? $csize/1024. : 0;
  189:         my ($status, $pubstatus, $title, $fulltitle);
  190:         if (!($cmode&$dirptr)) {
  191:             ($status, $pubstatus) = &getStatus($resdir, $targetdir, $cstr_dir, 
  192:                 $filename, $linkfilename, $cmtime, $meta_same);
  193:             ($fulltitle, $title) = &getTitle($resdir, $targetdir, $filename, 
  194:                                         $linkfilename, $meta_same, \%bombs);
  195:         } else {
  196:             ($status, $pubstatus) = ('','');
  197:             ($fulltitle, $title) = ('','');
  198:         }
  199: 
  200:         # This hash will allow sorting
  201:         $filehash->{ $filename } = {
  202:             "cmtime"            => $cmtime,
  203:             "size"              => $size,
  204:             "cmode"             => $cmode,
  205:             "filetype"          => $filetype,
  206:             "title"             => $title,
  207:             "fulltitle"         => $fulltitle,
  208:             "status"            => $status,
  209:             "pubstatus"         => $pubstatus,
  210:             "linkfilename"      => $linkfilename,
  211:         }
  212:     }
  213:    
  214:     my @sorted_files;
  215:     # Sorting by something other than "Name".  Name is the secondary key.
  216:     if ($sortby =~ m{cmtime|size}) {    # Numeric fields
  217:         # First check if order should be reversed
  218:         if ($sortorder eq "rev") {
  219:             @sorted_files = sort {
  220:                 $filehash->{$a}->{$sortby} <=> $filehash->{$b}->{$sortby}
  221:                     or
  222:                 uc($a) cmp uc($b)
  223:             } (keys(%{$filehash}));
  224:         } else {
  225:             @sorted_files = sort {
  226:                 $filehash->{$b}->{$sortby} <=> $filehash->{$a}->{$sortby}
  227:                     or
  228:                 uc($a) cmp uc($b)
  229:             } (keys(%{$filehash}));
  230:         }
  231:     } elsif ($sortby =~ m{filetype|title|status}) {     # String fields
  232:         if ($sortorder eq "rev") {
  233:             @sorted_files = sort {
  234:                 $filehash->{$b}->{$sortby} cmp $filehash->{$a}->{$sortby}
  235:                     or
  236:                 uc($a) cmp uc($b)
  237:             } (keys(%{$filehash}));
  238:         } else {
  239:             @sorted_files = sort {
  240:                 $filehash->{$a}->{$sortby} cmp $filehash->{$b}->{$sortby}
  241:                     or
  242:                 uc($a) cmp uc($b)
  243:             } (keys(%{$filehash}));
  244:         }
  245: 
  246:     # Sort by "Name" is the default
  247:     } else { 
  248:         if ($sortorder eq "rev") {
  249:             @sorted_files = sort {uc($b) cmp uc($a)} (keys(%{$filehash}));
  250:         } else {
  251:             @sorted_files = sort {uc($a) cmp uc($b)} (keys(%{$filehash}));
  252:         }
  253:     }
  254: 
  255:     # Print the sorted resources
  256:     foreach my $filename (@sorted_files) {
  257:         if ($filehash->{$filename}->{"cmode"}&$dirptr) {        # Directories
  258:             &putdirectory($r, $thisdisfn, $linkdir, $filename, 
  259:                 $filehash->{$filename}->{"cmtime"}, 
  260:                 $targetdir, \%bombs, \$numdir);
  261:         } else {                                                # Files
  262:             &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
  263:                 $targetdir, $linkdir, $filehash->{$filename}->{"cmtime"}, 
  264:                 $filehash->{$filename}->{"size"}, \$numres, 
  265:                 $filehash->{$filename}->{"linkfilename"},
  266:                 $filehash->{$filename}->{"fulltitle"},
  267:                 $filehash->{$filename}->{"status"},
  268:                 $filehash->{$filename}->{"pubstatus"});
  269:         }
  270:     }
  271: 
  272:     $r->print( &Apache::loncommon::end_data_table()
  273:         .&Apache::loncommon::end_page() );
  274: 
  275:     return OK;
  276: }
  277: 
  278: 
  279: 
  280: #   Output the header of the page.  This includes:
  281: #   - The HTML header 
  282: #   - The H1/H3  stuff which includes the directory.
  283: #
  284: #     startpage($r, $uame, $udom, $thisdisfn);
  285: #      $r     - The apache request object.
  286: #      $uname - User name.
  287: #      $udom  - Domain name the user is logged in under.
  288: #      $thisdisfn - Displayable version of the filename.
  289: 
  290: sub startpage {
  291:     my ($r, $uname, $udom, $thisdisfn) = @_;
  292:     &Apache::loncommon::content_type($r,'text/html');
  293:     $r->send_http_header;
  294: 
  295:     my $formaction='/priv'.$thisdisfn.'/';
  296:     $formaction=~s|/+|/|g;
  297:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
  298: 
  299:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  300:     &Apache::lonhtmlcommon::add_breadcrumb({
  301:         'text'  => 'Authoring Space',
  302:         'href'  => &Apache::loncommon::authorspace($formaction),
  303:     });
  304:     # breadcrumbs (and tools) will be created 
  305:     # in start_page->bodytag->innerregister
  306: 
  307:     $env{'request.noversionuri'}=$formaction;
  308:     $r->print(&Apache::loncommon::start_page('Authoring Space'));
  309: 
  310:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  311:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
  312:     my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author'); #expressed in MB
  313:     $disk_quota = 1000 * $disk_quota; # convert from MB to kB
  314: 
  315:     $r->print(&Apache::loncommon::head_subbox(
  316:                      '<div style="float:right;padding-top:0;margin-top;0">'
  317:                     .&Apache::lonhtmlcommon::display_usage($current_disk_usage,$disk_quota)
  318:                     .'</div>'
  319:                     .&Apache::loncommon::CSTR_pageheader()));
  320: 
  321:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
  322:     my $doctitle = 'LON-CAPA '.&mt('Authoring Space');
  323:     my $newname = &mt('New Name');
  324:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  325: <script type="text/javascript">
  326: top.document.title = '$esc_thisdisfn/ - $doctitle';
  327: // Store directory location for menu bar to find
  328: 
  329: parent.lastknownpriv='/priv$esc_thisdisfn/';
  330: 
  331: // Confirmation dialogues
  332: 
  333:     function currdiract(theform) {
  334:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  335:             document.publishdir.filename.value = theform.filename.value;
  336: 	    document.publishdir.submit();
  337:         }
  338:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
  339:             top.location=theform.filename.value+'default.meta'
  340:         }
  341:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  342:             document.printdir.postdata.value=theform.filename.value
  343:             document.printdir.submit();
  344:         }
  345:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
  346:               var delform = document.delresource
  347:               delform.filename.value = theform.filename.value
  348:               delform.submit()
  349:         }
  350:     }
  351:   
  352:     function checkUpload(theform) {
  353:         if (theform.file == '') {
  354:             alert("Please use 'Browse..' to choose a file first, before uploading")
  355:             return 
  356:         }
  357:         theform.submit()  
  358:     }
  359: 
  360:     function SetPubDir(theform,printForm) {
  361:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  362:             top.location = theform.openname.value
  363:             return
  364:         }
  365:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  366:             theform.submit();
  367:         }
  368:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
  369:             top.location=theform.filename.value+'default.meta'
  370:         }
  371:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  372:             theform.action = '/adm/printout'
  373:             theform.postdata.value = theform.filename.value
  374:             theform.submit()
  375:         }
  376:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
  377:               var delform = document.delresource
  378:               delform.filename.value = theform.filename.value
  379:               delform.submit()
  380:         }
  381:         return
  382:     }
  383:     function SetResChoice(theform) {
  384:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  385:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  386:           changename(theform,activity)
  387:       }
  388:       if (activity == 'publish') {
  389:           var pubform = document.pubresource
  390:           pubform.filename.value = theform.filename.value
  391:           pubform.submit()
  392:       }
  393:       if (activity == 'delete') {
  394:           var delform = document.delresource
  395:           delform.filename.value = theform.filename.value
  396:           delform.submit()
  397:       }
  398:       if (activity == 'obsolete') {
  399:           var pubform = document.pubresource
  400:           pubform.filename.value = theform.filename.value
  401:           pubform.makeobsolete.value=1;
  402:           pubform.submit()
  403:       }
  404:       if (activity == 'print') {
  405:           document.printresource.postdata.value = theform.filename.value
  406:           document.printresource.submit()
  407:       }
  408:       if (activity == 'retrieve') {
  409:           document.retrieveres.filename.value = theform.filename.value
  410:           document.retrieveres.submit()
  411:       }
  412:       if (activity == 'cleanup') {
  413:           document.cleanup.filename.value = theform.filename.value
  414:           document.cleanup.submit()
  415:       }
  416:       return
  417:     }
  418:     function changename(theform,activity) {
  419:         var oldname=theform.dispfilename.value;
  420:         var newname=prompt('$newname',oldname);
  421:         if (newname == "" || !newname || newname == oldname)  {
  422:             return
  423:         }
  424:         document.moveresource.newfilename.value = newname
  425:         document.moveresource.filename.value = theform.filename.value
  426:         document.moveresource.action.value = activity
  427:         document.moveresource.submit();
  428:     }
  429: </script>
  430: ENDPUBDIRSCRIPT
  431:     $r->print($pubdirscript);
  432: }
  433: 
  434: sub dircontrols {
  435:     my ($r,$uname,$udom,$thisdisfn) = @_;
  436:     my %lt=&Apache::lonlocal::texthash(
  437:                                        cnpd => 'Cannot publish directory',
  438:                                        cnrd => 'Cannot retrieve directory',
  439:                                        mcdi => 'Must create new subdirectory inside a directory',
  440:                                        pubr => 'Publish this Resource',
  441:                                        pubd => 'Publish this Directory',
  442:                                        dedr => 'Delete Directory',
  443:                                        rtrv => 'Retrieve Old Version',
  444:                                        list => 'List Directory',
  445:                                        uplo => 'Upload file',  
  446:                                        dele => 'Delete',
  447:                                        edit => 'Edit Metadata', 
  448:                                        sela => 'Select Action',
  449:                                        nfil => 'New file',
  450:                                        nhtm => 'New HTML file',
  451:                                        nprb => 'New problem',
  452:                                        npag => 'New assembled page',
  453:                                        nseq => 'New assembled sequence',
  454:                                        ncrf => 'New custom rights file',
  455:                                        nsty => 'New style file',
  456:                                        nlib => 'New library file',
  457:                                        nbt  => 'New bridgetask file',
  458:                                        nsub => 'New subdirectory',
  459:                                        renm => 'Rename current file to',
  460:                                        move => 'Move current file to',
  461:                                        copy => 'Copy current file to',
  462:                                        type => 'Type Name Here',
  463:                                        go   => 'Go',
  464:                                        prnt => 'Print contents of directory',
  465:                                        crea => 'Create a new directory or LON-CAPA document',
  466: 				       acti => 'Actions for current directory',
  467: 				       updc => 'Upload a new document',
  468: 				       pick => 'Please select an action to perform using the new filename',
  469:                                       );
  470:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
  471:     $r->print(<<END);
  472: <div class="LC_columnSection">
  473:   <div>
  474:     <form name="curractions" method="post" action="">
  475:       <fieldset>
  476:         <legend>$lt{'acti'}</legend>
  477:         <select name="dirtask" onchange="currdiract(this.form)">
  478:             <option>$lt{'sela'}</option>
  479:             <option value="publish">$lt{'pubd'}</option>
  480:             <option value="editmeta">$lt{'edit'}</option>
  481:             <option value="printdir">$lt{'prnt'}</option>
  482:             <option value="delete">$lt{'dedr'}</option>
  483:         </select>
  484:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
  485:       </fieldset>
  486:     </form>
  487:     <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  488:       <input type="hidden" name="pubrec" value="" />
  489:       <input type="hidden" name="filename" value="" />
  490:     </form>
  491:     <form name="printdir" method="post" action="/adm/printout" target="_parent">
  492:       <input type="hidden" name="postdata" value="" />
  493:     </form>
  494:   </div>
  495: 
  496:   <div>
  497:     <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  498:       <fieldset>
  499:         <legend>$lt{'updc'}</legend>
  500:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
  501:         <input type="file" name="upfile" size="20" />
  502:         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
  503:       </fieldset>
  504:     </form>
  505:   </div>
  506: 
  507:   <div>
  508:     <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  509:       <fieldset>
  510:               <legend>$lt{'crea'}</legend>
  511: 	      <span class="LC_nobreak">
  512: 		<input type="hidden" name="filename" value="/priv$thisdisfn/" />
  513:                   <script type="text/javascript">
  514:                     function validate_go() {
  515:                         var selected = document.fileaction.action.selectedIndex;
  516:                         if (selected == 0) {
  517:                             alert('$lt{'pick'}');
  518:                         } else {
  519:                             document.fileaction.submit();
  520:                         }
  521:                     }
  522:                   </script>
  523: 		  <select name="action">
  524: 		    <option value="none">$lt{'sela'}</option>
  525: 		    <option value="newfile">$lt{'nfil'}:</option>
  526: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
  527: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
  528:                     <option value="newpagefile">$lt{'npag'}:</option>
  529:                     <option value="newsequencefile">$lt{'nseq'}:</option>
  530:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
  531:                     <option value="newstyfile">$lt{'nsty'}:</option>
  532:                     <option value="newtaskfile">$lt{'nbt'}:</option>
  533:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
  534: 	            <option value="newdir">$lt{'nsub'}:</option>
  535: 		  </select>&nbsp;<input type="text" name="newfilename" value="$lt{'type'}" onfocus="if (this.value == '$mytype') this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
  536: 		 </span>
  537:       </fieldset>
  538:     </form>
  539:   </div>
  540: </div>
  541: END
  542: }
  543: 
  544: sub resourceactions {
  545:     my ($r,$uname,$udom,$thisdisfn) = @_;
  546:     $r->print(<<END);
  547:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  548:          <input type="hidden" name="filename" value="" />
  549:          <input type="hidden" name="newfilename" value="" />
  550:          <input type="hidden" name="action" value="" />
  551:        </form>
  552:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  553:          <input type="hidden" name="filename" value="" />
  554:          <input type="hidden" name="action" value="delete" />
  555:        </form>
  556:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  557:          <input type="hidden" name="filename" value="" />
  558:          <input type="hidden" name="makeobsolete" value="0" />
  559:        </form>
  560:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  561:            <input type="hidden" name="postdata" value="" />
  562:        </form>
  563:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  564:            <input type="hidden" name="filename" value="" />
  565:        </form>
  566:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
  567:            <input type="hidden" name="filename" value="" />
  568:        </form>
  569: END
  570: }
  571: 
  572: #
  573: #   Get the title string or "[untitled]" if the file has no title metadata:
  574: #   Without the latter substitution, it's impossible to examine metadata for
  575: #   untitled resources.  Resources may be legitimately untitled, to prevent
  576: #   searches from locating them.
  577: #
  578: #   $str = getTitleString($fullname);
  579: #       $fullname - Fully qualified filename to check.
  580: #
  581: sub getTitleString {
  582:     my $fullname = shift;
  583:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  584: 
  585:     unless ($title) {
  586: 	$title = "[".&mt('untitled')."]";
  587:     }
  588:     return $title;
  589: }
  590: 
  591: sub getCopyRightString {
  592:     my $fullname = shift;
  593:     return &Apache::lonnet::metadata($fullname, 'copyright');
  594: }
  595: 
  596: sub getSourceRightString {
  597:     my $fullname = shift;
  598:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  599: }
  600: #
  601: #  Put out a directory table row:
  602: #    putdirectory(r, base, here, dirname, modtime, targetdir, bombs, numdir)
  603: #      r         - Apache request object.
  604: #      reqfile   - File in request.
  605: #      here      - Where we are in directory tree.
  606: #      dirname   - Name of directory special file.
  607: #      modtime   - Encoded modification time.
  608: #      targetdir - Publication target directory.
  609: #      bombs     - Reference to hash of URLs with runtime error messages.
  610: #      numdir    - Reference to scalar used to track number of sub-directories
  611: #                  in directory (used in form name for each "actions" dropdown).
  612: #
  613: sub putdirectory {
  614:     my ($r, $reqfile, $here, $dirname, $modtime, $targetdir, $bombs, $numdir) = @_;
  615: 
  616: # construct the display filename: the directory name unless ..:
  617:    
  618:     my $actionitem;
  619:  
  620:     my $disfilename = $dirname;
  621: # Don't display directory itself, and there is no way up from root directory
  622:     unless ((($dirname eq '..') && ($reqfile=~/^\/[^\/]+\/[^\/]+$/)) || ($dirname eq '.')) {
  623:         my $kaputt=0;
  624:         if (ref($bombs) eq 'HASH') {
  625:             foreach my $key (keys(%{$bombs})) {
  626:                 my $currentdir = &Apache::lonnet::declutter("$targetdir/$disfilename");
  627:                 if (($key) =~ m{^\Q$currentdir\E/}) { $kaputt=1; last; }
  628:             }
  629:         }
  630: #
  631: # Get the metadata from that directory's default.meta to display titles
  632: #
  633: 	%Apache::lonpublisher::metadatafields=();
  634: 	%Apache::lonpublisher::metadatakeys=();
  635: 	&Apache::lonpublisher::metaeval(
  636:                  &Apache::lonnet::getfile($r->dir_config('lonDocRoot').$here.'/'.$dirname.'/default.meta')
  637:                                        );
  638:         if ($dirname eq '..') {
  639:             $actionitem = &mt('Go to ...');
  640:             $disfilename = '<i>'.&mt('Parent Directory').'</i>';
  641:         } else {
  642:             $actionitem = 
  643:                     '<form name="dirselect_'.$$numdir.
  644:                     '" action="/adm/publish" target="_parent">'.
  645:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
  646:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  647:                       '<option value="open">'.&mt('Open').'</option>'.
  648:                       '<option value="publish">'.&mt('Publish').'</option>'.
  649:                       '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
  650:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  651:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
  652:                     '</select>'.
  653:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" />'.
  654:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  655:                      '<input type="hidden" name="postdata" value="" />'.
  656:                    '</form>';
  657:             $$numdir ++;
  658:         }
  659: 	$r->print('<tr class="LC_browser_folder">'.
  660: 		  '<td><img src="'.
  661: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/navmap.folder.closed.gif" alt="folder" /></td>'.
  662: 		  '<td>'.$actionitem.'</td>'.
  663: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
  664: 		  $disfilename.'</a></span></td>'.
  665: 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($targetdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
  666: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
  667: 	    $r->print(' <i>'.
  668: 		      $Apache::lonpublisher::metadatafields{'subject'}.
  669: 		      '</i> ');
  670: 	}
  671: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  672: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  673: 	          '<td></td>'.
  674: 		  "</tr>\n");
  675:     }
  676:     return;
  677: }
  678: 
  679: sub getTitle {
  680:     my ($resdir, $targetdir, $filename, $linkfilename, $meta_same, $bombs) = @_;
  681:     my $title='';
  682:     my $titleString = &getTitleString($targetdir.'/'.$filename);
  683:     if (-e $resdir.'/'.$filename) {
  684: 	$title = '<a href="'.$targetdir.'/'.$filename.
  685: 	    '.meta" target="cat">'.$titleString.'</a>';
  686:         if (!$meta_same) {
  687: 	    $title = &mt('Metadata Modified').'<br />'.$title.
  688: 		'<br />'.
  689:                 &Apache::loncommon::modal_link(
  690:                     '/adm/diff?filename='.$linkfilename.'.meta'.'&amp;versiontwo=priv',
  691:                     &mt('Metadata Diffs'),600,500);
  692: 	    $title.="\n".'<br />'.
  693:                 &Apache::loncommon::modal_link(
  694:                     '/adm/retrieve?filename='.$linkfilename.'.meta&amp;inhibitmenu=yes&amp;add_modal=yes',
  695:                     &mt('Retrieve Metadata'),600,500);
  696:         } 
  697:     }
  698:     # Allow editing metadata of published and unpublished resources
  699:     $title .= "\n".'<br />' if ($title);
  700:     $title .= '<a href="'.$linkfilename.'.meta">'.
  701:               ($$bombs{&Apache::lonnet::declutter($targetdir.'/'.$filename)}?
  702:                   '<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':
  703:                   &mt('Edit Metadata')).
  704:               '</a>';
  705: 
  706:     return ($title, $titleString);
  707: }
  708: 
  709: 
  710: sub isMetaSame {
  711:     my ($cstr_dir, $resdir, $filename) = @_;
  712:     my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
  713:     my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
  714:     return (&Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
  715:             $cstr_dir.'/'.$filename.'.meta') && $meta_rmtime < $meta_cmtime) 
  716:         ? 0 : 1;
  717: }
  718:     
  719: 
  720: sub getStatus {    
  721:     my ($resdir, $targetdir, $cstr_dir, $filename,  
  722:             $linkfilename, $cmtime, $meta_same) = @_;
  723:     my $pubstatus = 'unpublished';
  724:     my $status = &mt('Unpublished');
  725: 
  726:     if (-e $resdir.'/'.$filename) {
  727:         my $same = 0;
  728:         if ((stat($resdir.'/'.$filename))[9] >= $cmtime) {
  729:             $same = 1;
  730:         } else {
  731:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
  732: 						     $cstr_dir.'/'.$filename)) {
  733:               $same = 0;
  734:            } else {
  735:               $same = 1;
  736:            }
  737:         }
  738: 
  739:         my $rights_status =
  740:             &mt(&getCopyRightString($targetdir.'/'.$filename)).', ';
  741: 
  742:         my %lt_SourceRight = &Apache::lonlocal::texthash(
  743:                'open'   => 'Source: open',
  744:                'closed' => 'Source: closed',
  745:         );
  746:         $rights_status .=
  747:             $lt_SourceRight{&getSourceRightString($targetdir.'/'.$filename)};
  748: 
  749: 	if ($same) {
  750: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  751:                 $pubstatus = 'obsolete';
  752: 		$status=&mt('Obsolete');
  753:             } else {
  754: 		if (!$meta_same) {
  755: 		    $pubstatus = 'metamodified';
  756: 		} else {
  757: 		    $pubstatus = 'published';
  758: 		}
  759: 		$status=&mt('Published').
  760: 		    '<br />'. $rights_status;
  761: 	    }
  762: 	} else {
  763:             $pubstatus = 'modified';
  764: 	    $status=&mt('Modified').
  765: 		'<br />'. $rights_status;
  766: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  767: 		$status.='<br />'.
  768:                          &Apache::loncommon::modal_link(
  769:                              '/adm/diff?filename='.$linkfilename.'&amp;versiontwo=priv',
  770:                              &mt('Diffs'),600,500);
  771: 	    }
  772: 	} 
  773: 
  774: 	$status.="\n".'<br />'.
  775:              &Apache::loncommon::modal_link(
  776:                  '/adm/retrieve?filename='.$linkfilename.'&amp;inhibitmenu=yes&amp;add_modal=yes',&mt('Retrieve'),600,500);
  777:     }
  778: 
  779:     return ($status, $pubstatus);
  780: }
  781: 
  782: 
  783: #
  784: #   Put a table row for a file resource.
  785: #
  786: sub putresource {
  787:     my ($r, $udom, $uname, $filename, $thisdisfn, $resdir, $targetdir, 
  788:             $linkdir, $cmtime, $size, $numres, $linkfilename, $title, 
  789:             $status, $pubstatus) = @_;
  790:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
  791: 
  792:     my $editlink='';
  793:     my $editlink2='';
  794:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  795: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
  796:     }
  797:     if ($filename=~/$LONCAPA::assess_re/) {
  798: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=editxml">'.&mt('EditXML').'</a>)';
  799: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
  800:     }
  801:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
  802: 	$editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'" target="_parent">'.&mt('Clean Up').')</a>';
  803:     }
  804:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  805: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
  806:     }
  807:     my $publish_button = (-e $resdir.'/'.$filename) ? &mt('Re-publish') : &mt('Publish');
  808:     my $pub_select = '';
  809:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  810:     $r->print(&Apache::loncommon::start_data_table_row().
  811: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  812: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
  813:               '<td>'.$pub_select.'</td>'.
  814: 	      '<td><span class="LC_filename">'.
  815: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  816:                $filename.'</a></span>'.$editlink2.$editlink.
  817: 	      '</td>'.
  818: 	      '<td>'.$title.'</td>'.
  819:               '<td class="LC_browser_file_'.$pubstatus.'">&nbsp;&nbsp;</td>'. # Display publication status
  820:               '<td>'.$status.'</td>'.
  821: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  822: 	      '<td>'.sprintf("%.1f",$size).'</td>'.
  823: 	      &Apache::loncommon::end_data_table_row()
  824:     );
  825:     return;
  826: }
  827: 
  828: sub create_pubselect {
  829:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  830:     $$pub_select = '
  831: <form name="resselect_'.$$numres.'" action="">
  832: <select name="reschoice"  onchange="SetResChoice(this.form)">
  833: <option>'.&mt('Select action').'</option>'.
  834: '<option value="copy">'.&mt('Copy').'</option>';
  835:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  836:         $$pub_select .= 
  837: '<option value="rename">'.&mt('Rename').'</option>'.
  838: '<option value="move">'.&mt('Move').'</option>'.
  839: '<option value="delete">'.&mt('Delete').'</option>';
  840:     } else {
  841:         $$pub_select .= '
  842: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  843:     }
  844: # check for versions
  845:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  846:     if ($versions > 0) {
  847:         $$pub_select .='
  848: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  849:     }
  850:     $$pub_select .= '
  851: <option value="publish">'.$publish_button.'</option>'.
  852: '<option value="cleanup">'.&mt('Clean up').'</option>'.
  853: '<option value="print">'.&mt('Print').'</option>'.
  854: '</select>
  855: <input type="hidden" name="filename" value="/priv'.
  856:  &HTML::Entities::encode($thisdisfn.'/'.$filename,'<>&"').'" />
  857:  <input type="hidden" name="dispfilename" value="'.
  858:  &HTML::Entities::encode($filename).'" /></form>';
  859:     $$numres ++;
  860: }
  861: 
  862: sub check_for_versions {
  863:     my ($r,$fn,$udom,$uname) = @_;
  864:     my $versions = 0;
  865:     my $docroot=$r->dir_config('lonDocRoot');
  866:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  867:     my $resdir=$resfn;
  868:     $resdir=~s/\/[^\/]+$/\//;
  869:     $fn=~/\/([^\/]+)\.(\w+)$/;
  870:     my $main=$1;
  871:     my $suffix=$2;
  872:     opendir(DIR,$resdir);
  873:     while (my $filename=readdir(DIR)) {
  874:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  875:             $versions ++;        
  876:         }
  877:     }
  878:     closedir(DIR);
  879:     return $versions;
  880: }
  881: 
  882: 1;
  883: __END__
  884: 
  885: 
  886: =head1 NAME
  887: 
  888: Apache::lonpubdir - Authoring space directory lister
  889: 
  890: =head1 SYNOPSIS
  891: 
  892: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  893: 
  894:  <LocationMatch "^/+priv.*/$">
  895:  PerlAccessHandler       Apache::loncacc
  896:  SetHandler perl-script
  897:  PerlHandler Apache::lonpubdir
  898:  ErrorDocument     403 /adm/login
  899:  ErrorDocument     404 /adm/notfound.html
  900:  ErrorDocument     406 /adm/unauthorized.html
  901:  ErrorDocument	  500 /adm/errorhandler
  902:  </LocationMatch>
  903: 
  904:  <Location /adm/pubdir>
  905:  PerlAccessHandler       Apache::lonacc
  906:  SetHandler perl-script
  907:  PerlHandler Apache::lonpubdir
  908:  ErrorDocument     403 /adm/login
  909:  ErrorDocument     404 /adm/notfound.html
  910:  ErrorDocument     406 /adm/unauthorized.html
  911:  ErrorDocument	  500 /adm/errorhandler
  912:  </Location>
  913: 
  914: =head1 INTRODUCTION
  915: 
  916: This module publishes a directory of files.
  917: 
  918: This is part of the LearningOnline Network with CAPA project
  919: described at http://www.lon-capa.org.
  920: 
  921: =head1 HANDLER SUBROUTINE
  922: 
  923: This routine is called by Apache and mod_perl.
  924: 
  925: =over 4
  926: 
  927: =item *
  928: 
  929: read in information
  930: 
  931: =item *
  932: 
  933: start page output
  934: 
  935: =item *
  936: 
  937: run through list of files and attempt to publish unhidden files
  938: 
  939: =back
  940: 
  941: =head1 SUBROUTINES:
  942: 
  943: =over
  944: 
  945: =item startpage($r, $uame, $udom, $thisdisfn)
  946: 
  947: Output the header of the page.  This includes:
  948:  - The HTML header 
  949:  - The H1/H3  stuff which includes the directory.
  950:  
  951:     startpage($r, $uame, $udom, $thisdisfn);
  952:         $r     - The apache request object.
  953:         $uname - User name.
  954:         $udom  - Domain name the user is logged in under.
  955:         $thisdisfn - Displayable version of the filename.
  956: 
  957: =item getTitleString($fullname)
  958: 
  959:     Get the title string or "[untitled]" if the file has no title metadata:
  960:     Without the latter substitution, it's impossible to examine metadata for
  961:     untitled resources.  Resources may be legitimately untitled, to prevent
  962:     searches from locating them.
  963:     
  964:     $str = getTitleString($fullname);
  965:         $fullname - Fully qualified filename to check.
  966: 
  967: =item putdirectory($r, $base, $here, $dirname, $modtime, $targetdir, $bombs,
  968:                    $numdir)
  969: 
  970:     Put out a directory table row:
  971:     
  972:         $r        - Apache request object.
  973:         $reqfile  - File in request.
  974:         $here     - Where we are in directory tree.
  975:         $dirname  - Name of directory special file.
  976:         $modtime  - Encoded modification time.
  977:         targetdir - Publication target directory.
  978:         bombs     - Reference to hash of URLs with runtime error messages.
  979:         numdir    - Reference to scalar used to track number of sub-directories
  980:                     in directory (used in form name for each "actions" dropdown).
  981: 
  982: =back
  983: 
  984: =cut

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