File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.97: download - view: text, annotated - select for diffs
Wed Nov 22 20:50:44 2006 UTC (17 years, 6 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Don't bother renaming to the same name. Jeesh.

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

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