File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.106: download - view: text, annotated - select for diffs
Wed Jan 16 12:08:04 2008 UTC (16 years, 4 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
Localization: added missing $mt(...), $lt{...}

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

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