File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.148: download - view: text, annotated - select for diffs
Sat Jul 27 21:38:54 2013 UTC (10 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_11_0_RC2, version_2_11_0_RC1, HEAD
- Don't list .DS_Store file saved when viewing directory via webDAV on MacOS.

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

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