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

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

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