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

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

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