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

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

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