File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.158: download - view: text, annotated - select for diffs
Sun Jun 29 03:33:23 2014 UTC (9 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0, HEAD
- Exclude . and .. from directory listing when determining if directory
  is empty. Correction to regexps added in rev. 1.157

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

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