File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.98: download - view: text, annotated - select for diffs
Wed Dec 6 22:22:39 2006 UTC (17 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: version_2_3_2, version_2_3_1, version_2_3_0, version_2_2_99_1, version_2_2_99_0, HEAD
- more re fix ups

    1: # The LearningOnline Network with CAPA
    2: # Construction Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.98 2006/12/06 22:22:39 albertel 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>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>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('Construction Space',undef,
  211: 						 {'body_title' =>
  212: 						      $pagetitle,}));
  213:     } else {
  214: 	$r->print(&Apache::loncommon::start_page('Construction Space',undef));
  215: 	$r->print($pagetitle);
  216:     }
  217: 
  218:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  219: <script type="text/javascript">
  220: top.document.title = '$thisdisfn/ - LON-CAPA Construction Space';
  221: // Store directory location for menu bar to find
  222: 
  223: parent.lastknownpriv='/~$uname$thisdisfn/';
  224: 
  225: // Confirmation dialogues
  226: 
  227:     function currdiract(theform) {
  228:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  229:             document.publishdir.filename.value = theform.filename.value;
  230: 	    document.publishdir.submit();
  231:         }
  232:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editcat') {
  233:             top.location=theform.filename.value+'default.meta'
  234:         }
  235:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  236:             document.printdir.postdata.value=theform.filename.value
  237:             document.printdir.submit();
  238:         }
  239:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
  240:               var delform = document.delresource
  241:               delform.filename.value = theform.filename.value
  242:               delform.submit()
  243:         }
  244:     }
  245:   
  246:     function checkUpload(theform) {
  247:         if (theform.file == '') {
  248:             alert("Please use 'Browse..' to choose a file first, before uploading")
  249:             return 
  250:         }
  251:         theform.submit()  
  252:     }
  253: 
  254:     function SetPubDir(theform,printForm) {
  255:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  256:             top.location = theform.openname.value
  257:             return
  258:         }
  259:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  260:             theform.submit();
  261:         }
  262:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editcat") {
  263:             top.location=theform.filename.value+'default.meta'
  264:         }
  265:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  266:             theform.action = '/adm/printout'
  267:             theform.postdata.value = theform.filename.value
  268:             theform.submit()
  269:         }
  270:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
  271:               var delform = document.delresource
  272:               delform.filename.value = theform.filename.value
  273:               delform.submit()
  274:         }
  275:         return
  276:     }
  277:     function SetResChoice(theform) {
  278:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  279:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  280:           changename(theform,activity)
  281:       }
  282:       if (activity == 'publish') {
  283:           var pubform = document.pubresource
  284:           pubform.filename.value = theform.filename.value
  285:           pubform.submit()
  286:       }
  287:       if (activity == 'delete') {
  288:           var delform = document.delresource
  289:           delform.filename.value = theform.filename.value
  290:           delform.submit()
  291:       }
  292:       if (activity == 'obsolete') {
  293:           var pubform = document.pubresource
  294:           pubform.filename.value = theform.filename.value
  295:           pubform.makeobsolete.value=1;
  296:           pubform.submit()
  297:       }
  298:       if (activity == 'print') {
  299:           document.printresource.postdata.value = theform.filename.value
  300:           document.printresource.submit()
  301:       }
  302:       if (activity == 'retrieve') {
  303:           document.retrieveres.filename.value = theform.filename.value
  304:           document.retrieveres.submit()
  305:       }
  306:       if (activity == 'cleanup') {
  307:           document.cleanup.filename.value = theform.filename.value
  308:           document.cleanup.submit()
  309:       }
  310:       return
  311:     }
  312:     function changename(theform,activity) {
  313:         var oldname=theform.dispfilename.value;
  314:         var newname=prompt('New Name',oldname);
  315:         if (newname == "" || !newname || newname == oldname)  {
  316:             return
  317:         }
  318:         document.moveresource.newfilename.value = newname
  319:         document.moveresource.filename.value = theform.filename.value
  320:         document.moveresource.action.value = activity
  321:         document.moveresource.submit();
  322:     }
  323: </script>
  324: ENDPUBDIRSCRIPT
  325:     $r->print($pubdirscript);
  326: 
  327:     if ((($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) &&
  328: 	$env{'environment.remote'} ne 'off') {
  329: 	$r->print('<h3>'.&mt('Co-Author').': '.$uname.' at '.$udom.
  330: 		  '</h3>');
  331:     }
  332: }
  333: 
  334: sub dircontrols {
  335:     my ($r,$uname,$udom,$thisdisfn) = @_;
  336:     my %lt=&Apache::lonlocal::texthash(
  337:                                        cnpd => 'Cannot publish directory',
  338:                                        cnrd => 'Cannot retrieve directory',
  339:                                        mcdi => 'Must create new subdirectory inside a directory',
  340:                                        pubr => 'Publish this Resource',
  341:                                        pubd => 'Publish this Directory',
  342:                                        dedr => 'Delete Directory',
  343:                                        rtrv => 'Retrieve Old Version',
  344:                                        list => 'List Directory',
  345:                                        uplo => 'Upload file',  
  346:                                        dele => 'Delete',
  347:                                        edit => 'Edit Catalog Information', 
  348:                                        sela => 'Select Action',
  349:                                        nfil => 'New file',
  350:                                        nhtm => 'New HTML file',
  351:                                        nprb => 'New problem',
  352:                                        npag => 'New assembled page',
  353:                                        nseq => 'New assembled sequence',
  354:                                        ncrf => 'New custom rights file',
  355:                                        nsty => 'New style file',
  356:                                        nlib => 'New library file',
  357:                                        nsub => 'New subdirectory',
  358:                                        renm => 'Rename current file to',
  359:                                        move => 'Move current file to',
  360:                                        copy => 'Copy current file to',
  361:                                        type => 'Type Name Here',
  362:                                        go => 'Go',
  363:                                        prnt => 'Print contents of directory',
  364:                                        crea => 'Create a new directory or LON-CAPA document',
  365: 				       acti => 'Actions for current directory',
  366: 				       updc => 'Upload a new document'
  367:                                       );
  368:     $r->print(<<END);
  369:         <table id="LC_cstr_controls">
  370:          <tr>
  371:           <th>$lt{'acti'}</th>
  372:           <th>$lt{'updc'}</th>
  373:           <th>$lt{'crea'}</th>
  374:         </tr>
  375:         <tr>
  376:          <td>
  377:           <form name="curractions" method="post" action="">
  378:            <select name="dirtask" onchange="currdiract(this.form)">
  379:             <option>$lt{'sela'}</option>
  380:             <option value="publish">$lt{'pubd'}</option>
  381:             <option value="editcat">$lt{'edit'}</option>
  382:             <option value="printdir">$lt{'prnt'}</option>
  383:             <option value="delete">$lt{'dedr'}</option>
  384:            </select>
  385:            <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  386:           </form>
  387:           <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  388:            <input type="hidden" name="pubrec" value="" />
  389:            <input type="hidden" name="filename" value="" />
  390:           </form>
  391:           <form name="printdir" method="post" action="/adm/printout" target="_parent">
  392:            <input type="hidden" name="postdata" value="" />
  393:           </form>
  394:          </td>
  395:          <td>
  396: 	    <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  397: 	      <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  398: 	      <input type="file" name="upfile" size="20" />
  399: 	      <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
  400: 	    </form>
  401: 	 </td>
  402: 	 <td>
  403: 	    <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  404: 	      <span style="white-space: nowrap">
  405: 		<input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  406: 			  <select name="action">
  407: 			    <option value="Select Action">$lt{'sela'}</option>
  408: 			    <option value="newfile">$lt{'nfil'}:</option>
  409: 			    <option value="newhtmlfile">$lt{'nhtm'}:</option>
  410: 			    <option value="newproblemfile">$lt{'nprb'}:</option>
  411:                             <option value="newpagefile">$lt{'npag'}:</option>
  412:                             <option value="newsequencefile">$lt{'nseq'}:</option>
  413:                             <option value="newrightsfile">$lt{'ncrf'}:</option>
  414:                             <option value="newstyfile">$lt{'nsty'}:</option>
  415:                             <option value="newlibraryfile">$lt{'nlib'}:</option>
  416: 			    <option value="newdir">$lt{'nsub'}:</option>
  417: 		  </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()" />
  418: 		 </span>
  419: 		</form>
  420: 	  </td>
  421:          </tr>
  422:         </table>
  423: END
  424: }
  425: 
  426: sub pubbuttons {
  427:     my ($r,$uname,$thisdisfn) = @_;
  428:     $r->print('<form method="post" action="/adm/publish" target="_parent">'.
  429:               '<table><tr><td><input type="hidden" name="filename" value="/~'.
  430:                $uname.$thisdisfn.'/" />'.
  431:               '<input type="submit" value="'.&mt('Publish Directory').'" /></td><td>'.
  432: '<input type="button" onclick="window.location='."'/~".
  433:                $uname.$thisdisfn."/default.meta'".'" value="'.
  434: &mt('Edit Directory Catalog Information').'" /></td></tr></table></form>');
  435: }
  436: 
  437: sub resourceactions {
  438:     my ($r,$uname,$udom,$thisdisfn) = @_;
  439:     $r->print(<<END);
  440:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  441:          <input type="hidden" name="filename" value="" />
  442:          <input type="hidden" name="newfilename" value="" />
  443:          <input type="hidden" name="action" value="" />
  444:        </form>
  445:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  446:          <input type="hidden" name="filename" value="" />
  447:          <input type="hidden" name="action" value="delete" />
  448:        </form>
  449:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  450:          <input type="hidden" name="filename" value="" />
  451:          <input type="hidden" name="makeobsolete" value="0" />
  452:        </form>
  453:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  454:            <input type="hidden" name="postdata" value="" />
  455:        </form>
  456:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  457:            <input type="hidden" name="filename" value="" />
  458:        </form>
  459:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
  460:            <input type="hidden" name="filename" value="" />
  461:        </form>
  462: END
  463: }
  464: 
  465: #
  466: #   Get the title string or "[untitled]" if the file has no title metadata:
  467: #   Without the latter substitution, it's impossible to examine metadata for
  468: #   untitled resources.  Resources may be legitimately untitled, to prevent
  469: #   searches from locating them.
  470: #
  471: #   $str = getTitleString($fullname);
  472: #       $fullname - Fully qualified filename to check.
  473: #
  474: sub getTitleString {
  475:     my $fullname = shift;
  476:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  477: 
  478:     unless ($title) {
  479: 	$title = "[".&mt('untitled')."]";
  480:     }
  481:     return $title;
  482: }
  483: 
  484: sub getCopyRightString {
  485:     my $fullname = shift;
  486:     return &Apache::lonnet::metadata($fullname, 'copyright');
  487: }
  488: 
  489: sub getSourceRightString {
  490:     my $fullname = shift;
  491:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  492: }
  493: #
  494: #  Put out a directory table row:
  495: #    putdirectory(r, base, here, dirname, modtime)
  496: #      r       - Apache request object.
  497: #      reqfile - File in request.
  498: #      here    - Where we are in directory tree.
  499: #      dirname - Name of directory special file.
  500: #      modtime - Encoded modification time.
  501: # 
  502: sub putdirectory {
  503:     my ($r, $reqfile, $here, $dirname, $modtime, $resdir, $bombs, $numdir) = @_;
  504:     # construct the display filename: the directory name unless ..:
  505:     
  506:     my $disfilename = $dirname;
  507:     if ($dirname eq '..') {
  508: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
  509:     }
  510:     unless ( (($dirname eq '..') && ($reqfile eq '')) || ($dirname eq '.')) {
  511: 	my $kaputt=0;
  512: 	foreach (keys %{$bombs}) {
  513: 	    if ($_=~m:^\Q$resdir\E/\Q$disfilename\E/:) { $kaputt=1; last; }
  514: 	}
  515: 	%Apache::lonpublisher::metadatafields=();
  516: 	%Apache::lonpublisher::metadatakeys=();
  517: 	my $construct=$here;
  518: 	$construct=~s{^/priv/($LONCAPA::username_re)$}{/home/$1/public_html};
  519:         my $dirpath = $here;
  520:         $dirpath=~s{^/priv/}{/~};
  521: 	&Apache::lonpublisher::metaeval(&Apache::lonnet::getfile(
  522:        				 $construct.'/'.$dirname.'/default.meta'
  523: 								 ));
  524:         my $actionitem = '';
  525:         if ($dirname eq '..') {
  526:             $actionitem = 'Go to ...';
  527:         } else {
  528:             $actionitem = 
  529:                     '<form name="dirselect_'.$$numdir.
  530:                     '" action="/adm/publish" target="_parent">'.
  531:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
  532:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  533:                       '<option value="open">'.&mt('Open').'</option>'.
  534:                       '<option value="publish">'.&mt('Publish').'</option>'.
  535:                       '<option value="editcat">'.&mt('Edit catalog information').'</option>'.
  536:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  537:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
  538:                     '</select>'.
  539:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($dirpath.'/'.$dirname,'<>&"').'/" />'.
  540:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  541:                      '<input type="hidden" name="postdata" value="" />'.
  542:                    '</form>';
  543:             $$numdir ++;
  544:         }
  545: 	$r->print('<tr class="LC_browser_folder">'.
  546: 		  '<td><img src="'.
  547: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/folder_closed.gif" alt="folder" /></td>'.
  548: 		  '<td>'.$actionitem.'</td>'.
  549: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
  550: 		  $disfilename.'</a></span></td>'.
  551: 		        '<td colspan="2">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($resdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
  552: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
  553: 	    $r->print(' <i>'.
  554: 		      $Apache::lonpublisher::metadatafields{'subject'}.
  555: 		      '</i> ');
  556: 	}
  557: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  558: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  559: 		  "</tr>\n");
  560:     }
  561:     return OK;
  562: }
  563: #
  564: #   Put a table row for a file resource.
  565: #
  566: sub putresource {
  567:     my ($r, $udom, $uname, $filename, $thisdisfn, 
  568: 	$resdir, $targetdir, $linkdir,
  569: 	$cmtime,$bombs,$numres) = @_;
  570:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
  571:     my $pubstatus = 'unpublished';
  572:     my $status=&mt('Unpublished');
  573:     my $css_class='LC_browser_file';
  574:     my $title='&nbsp;';
  575:     my $publish_button=&mt('Publish');
  576:     my $cstr_dir = '/home/'.$uname.'/public_html/'.$thisdisfn.'/';
  577: #    my $action_buttons=
  578: #        '<br /><a target="_parent" href="/adm/cfile?action=delete&filename=/~'.
  579: #	$uname.'/'.$thisdisfn.'/'.$filename.'">'.
  580: #	&mt('Delete').'</a>';
  581:     if (-e $resdir.'/'.$filename) {
  582:         my $same=0;
  583: 	my ($rdev,$rino,$rmode,$rnlink,
  584: 	    $ruid,$rgid,$rrdev,$rsize,
  585: 	    $ratime,$rmtime,$rctime,
  586: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  587:         if ($rmtime>=$cmtime) {
  588:            $same=1;
  589:         } else {
  590:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
  591: 						     $cstr_dir.'/'.$filename)) {
  592:               $same=0;
  593:            } else {
  594:               $same=1;
  595:            }
  596:         }
  597: 	my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
  598: 	my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
  599: 	my $meta_same = 1;
  600: 	if ($meta_rmtime < $meta_cmtime
  601: 	    && &Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
  602: 						     $cstr_dir.'/'.$filename.'.meta')) {
  603: 	    $meta_same = 0;
  604: 	}
  605: 	$publish_button=&mt('Re-publish');
  606: 	my $rights_status =
  607: 	    &mt(&getCopyRightString($targetdir.'/'.$filename)).' '.
  608: 	    &mt(&getSourceRightString($targetdir.'/'.$filename));
  609: 	$title = '<a href="/res/'.$targetdir.'/'.$filename.
  610: 	    '.meta" target="cat">'.
  611: 	    &getTitleString($targetdir.'/'.$filename).'</a>';
  612: 	&Apache::lonnet::logthis("same $same meta $meta_same");
  613: 	if ($same) {
  614: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  615:                 $pubstatus = 'obsolete';
  616: 		$status=&mt('Obsolete');
  617:             } else {
  618: 		if (!$meta_same) {
  619: 		    $pubstatus = 'metamodified';
  620: 		} else {
  621: 		    $pubstatus = 'published';
  622: 		}
  623: 		$status=&mt('Published').
  624: 		    '<br />'. $rights_status;
  625: 	    }
  626: #	    } else {
  627: #		$action_buttons='';
  628: #	    }
  629: 	} else {
  630:             $pubstatus = 'modified';
  631: 	    $status=&mt('Modified').
  632: 		'<br />'. $rights_status;
  633: #	    $action_buttons='';
  634: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  635: 		$status.='<br /><a href="/adm/diff?filename=/~'.$uname.
  636: 		    $thisdisfn.'/'.$filename.
  637: 		    '&amp;versiontwo=priv" target="cat">'.&mt('Diffs').'</a>';
  638: 	    }
  639: 	} 
  640: 
  641: 	$title.="\n".'<br /><a href="/~'.$uname.$thisdisfn.'/'.$filename.'.meta">'. 
  642: 	    ($$bombs{$targetdir.'/'.$filename}?'<img src="/adm/lonMisc/bomb.gif" border="0" alt="bomb" />':'Edit Metadata').'</a>';
  643: 
  644: 	if (!$meta_same) {
  645: 	    $title = &mt('Metadata Modified').'<br />'.$title.
  646: 		'<br /><a href="/adm/diff?filename=/~'.$uname.
  647: 		$thisdisfn.'/'.$filename.'.meta'.
  648: 		'&amp;versiontwo=priv" target="cat">'.&mt('Metadata Diffs').'</a>';
  649: 	    $title.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  650: 		$thisdisfn.'/'.$filename.'.meta" target="_parent">'.&mt('Retrieve Metadata').'</a>';
  651: 	}
  652: 	$status.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  653: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Retrieve').'</a>';
  654:     }
  655:     my $editlink='';
  656:     my $editlink2='';
  657:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  658: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('Edit').'</a>)';
  659:     }
  660:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  661: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('EditXML').'</a>)';
  662: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_parent">'.&mt('Edit').'</a>)';
  663:     }
  664:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
  665: 	$editlink.=' (<a href="/adm/cleanup?filename=/~'.$uname.
  666: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Clean Up').')</a>';
  667:     }
  668:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  669: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress=/~'.
  670: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Decompress').'</a>)';
  671:     }
  672:     my $pub_select = '';
  673:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  674:     $r->print('<tr class="LC_browser_file_'.$pubstatus.'">'.
  675: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  676: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
  677:               '<td>'.$pub_select.'</td>'.
  678: 	      '<td><span class="LC_filename"> '.
  679: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  680:                $filename.'</a></span>'.$editlink2.$editlink.
  681: 	      '</td>'.
  682: 	      '<td>'.$title.'</td>'.
  683: 	      '<td>'.$status.'</td>'.
  684: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  685: 	      "</tr>\n");
  686:     return OK;
  687: }
  688: 
  689: sub create_pubselect {
  690:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  691:     $$pub_select = '
  692: <form name="resselect_'.$$numres.'" action="">
  693: <select name="reschoice"  onchange="SetResChoice(this.form)">
  694: <option>'.&mt('Select action').'</option>'.
  695: '<option value="copy">'.&mt('Copy').'</option>';
  696:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  697:         $$pub_select .= 
  698: '<option value="rename">'.&mt('Rename').'</option>'.
  699: '<option value="move">'.&mt('Move').'</option>'.
  700: '<option value="delete">'.&mt('Delete').'</option>';
  701:     } else {
  702:         $$pub_select .= '
  703: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  704:     }
  705: # check for versions
  706:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  707:     if ($versions > 0) {
  708:         $$pub_select .='
  709: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  710:     }
  711:     $$pub_select .= '
  712: <option value="publish">'.$publish_button.'</option>'.
  713: '<option value="cleanup">'.&mt('Clean up').'</option>'.
  714: '<option value="print">'.&mt('Print').'</option>'.
  715: '</select>
  716: <input type="hidden" name="filename" value="/~'.
  717:  &HTML::Entities::encode($uname.$thisdisfn.'/'.$filename,'<>&"').'" />
  718:  <input type="hidden" name="dispfilename" value="'.
  719:  &HTML::Entities::encode($filename).'"</form>';
  720:     $$numres ++;
  721: }
  722: 
  723: sub check_for_versions {
  724:     my ($r,$fn,$udom,$uname) = @_;
  725:     my $versions = 0;
  726:     my $docroot=$r->dir_config('lonDocRoot');
  727:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  728:     my $resdir=$resfn;
  729:     $resdir=~s/\/[^\/]+$/\//;
  730:     $fn=~/\/([^\/]+)\.(\w+)$/;
  731:     my $main=$1;
  732:     my $suffix=$2;
  733:     opendir(DIR,$resdir);
  734:     while (my $filename=readdir(DIR)) {
  735:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  736:             $versions ++;        
  737:         }
  738:     }
  739:     return $versions;
  740: }
  741: 
  742: #
  743: #   Categorize files in the directory.
  744: #   For each file in a list of files in a file directory, 
  745: #   the  file categorized as one of:
  746: #    - directory  
  747: #    - sequence
  748: #    - problem 
  749: #    - Other resource.
  750: #
  751: #   For each file the modification date is determined as well.
  752: #   Returned is a list of sublists:
  753: #    (directories, sequences, problems, other)
  754: #   each of the sublists contains entries of the following form (sorted by
  755: #   filename):
  756: #     (filename, typecode, lastmodtime)
  757: #
  758: #   $list = CategorizeFiles($location, $files)
  759: #       $location   - Directory in which the files live (relative to our
  760: #                     execution.
  761: #       $files      - list of files.
  762: #
  763: sub CategorizeFiles {
  764:     my $location = shift;
  765:     my $files    = shift;
  766: }
  767: 
  768: 1;
  769: __END__
  770: 
  771: =head1 NAME
  772: 
  773: Apache::lonpubdir - Construction space directory lister
  774: 
  775: =head1 SYNOPSIS
  776: 
  777: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  778: 
  779:  <LocationMatch "^/\~.*/$">
  780:  PerlAccessHandler       Apache::loncacc
  781:  SetHandler perl-script
  782:  PerlHandler Apache::lonpubdir
  783:  ErrorDocument     403 /adm/login
  784:  ErrorDocument     404 /adm/notfound.html
  785:  ErrorDocument     406 /adm/unauthorized.html
  786:  ErrorDocument	  500 /adm/errorhandler
  787:  </LocationMatch>
  788: 
  789:  <Location /adm/pubdir>
  790:  PerlAccessHandler       Apache::lonacc
  791:  SetHandler perl-script
  792:  PerlHandler Apache::lonpubdir
  793:  ErrorDocument     403 /adm/login
  794:  ErrorDocument     404 /adm/notfound.html
  795:  ErrorDocument     406 /adm/unauthorized.html
  796:  ErrorDocument	  500 /adm/errorhandler
  797:  </Location>
  798: 
  799: =head1 INTRODUCTION
  800: 
  801: This module publishes a directory of files.
  802: 
  803: This is part of the LearningOnline Network with CAPA project
  804: described at http://www.lon-capa.org.
  805: 
  806: =head1 HANDLER SUBROUTINE
  807: 
  808: This routine is called by Apache and mod_perl.
  809: 
  810: =over 4
  811: 
  812: =item *
  813: 
  814: read in information
  815: 
  816: =item *
  817: 
  818: start page output
  819: 
  820: =item *
  821: 
  822: run through list of files and attempt to publish unhidden files
  823: 
  824: =back
  825: 
  826: =cut

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