File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.138: download - view: text, annotated - select for diffs
Wed Nov 23 19:13:01 2011 UTC (12 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: language_hyphenation_merge, language_hyphenation, HEAD
- Bug 6530.
  - Additional clean up for bug 1320, following retirement of lonconstruct.pm
    (see: loncapa_apache.conf rev 1.205 vs. 1.204).

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

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