File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.108: download - view: text, annotated - select for diffs
Thu Jan 17 04:50:57 2008 UTC (16 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, HEAD
- loncommon::start_page() does its own &mt() on the title passed to it as the first argument.
- $doctitle - change &mt() to reuse existing translation.

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

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