Annotation of loncom/publisher/lonpubdir.pm, revision 1.64

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

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