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

1.1       www         1: # The LearningOnline Network with CAPA
1.32      www         2: # Construction Space Directory Lister
1.16      albertel    3: #
1.121   ! bisitz      4: # $Id: lonpubdir.pm,v 1.120 2009/05/19 10:16:02 bisitz 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.120     bisitz     65:      $r->log_reason($uname.':'.$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.120     bisitz     77:      $r->log_reason($env{'user.name'}.':'.$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.115     bisitz    108:   $r->print(&Apache::loncommon::start_data_table()
                    109:            .&Apache::loncommon::start_data_table_header_row()
                    110:            .'<th>'.&mt('Type').'</th>'
                    111:            .'<th>'.&mt('Actions').'</th>'
                    112:            .'<th>'.&mt('Name').'</th>'
                    113:            .'<th>'.&mt('Title').'</th>'
                    114:            .'<th colspan="2">'.&mt('Status').'</th>'
                    115:            .'<th>'.&mt('Last Modified').'</th>'
                    116:            .&Apache::loncommon::end_data_table_header_row()
                    117:   );
1.1       www       118: 
1.2       www       119:   my $filename;
1.23      foxr      120:   my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
1.1       www       121: 
1.2       www       122:   opendir(DIR,$fn);
1.44      albertel  123:   my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
1.11      albertel  124:   foreach my $filename (@files) {
1.2       www       125:      my ($cdev,$cino,$cmode,$cnlink,
                    126:          $cuid,$cgid,$crdev,$csize,
                    127:          $catime,$cmtime,$cctime,
                    128:          $cblksize,$cblocks)=stat($fn.'/'.$filename);
1.12      www       129: 
1.10      albertel  130:      my $extension='';
                    131:      if ($filename=~/\.(\w+)$/) { $extension=$1; }
1.15      matthew   132:      if ($cmode&$dirptr) {
1.64      raeburn   133: 	 putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
1.17      harris41  134:      } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
1.64      raeburn   135: 	 putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir, 
                    136: 		     $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
1.14      albertel  137:      } else {
1.15      matthew   138: 	# "hidden" extension and not a directory, so hide it away.
1.2       www       139:      }
                    140:   }
                    141:   closedir(DIR);
                    142: 
1.115     bisitz    143:   $r->print(&Apache::loncommon::end_data_table()
                    144:            .&Apache::loncommon::end_page()
                    145:   );
1.1       www       146:   return OK;  
                    147: }
1.21      foxr      148: #
1.23      foxr      149: #  Gets the effective URL of the request and returns it:
                    150: #    $effn = getEffectiveUrl($r);
                    151: #       $r  - The Apache Request object.
                    152: sub getEffectiveUrl {
                    153:     my $r = shift;
                    154:     my $fn;
                    155:     
1.78      albertel  156:     if ($env{'form.filename'}) {	# If a form filename is defined.
                    157: 	$fn=$env{'form.filename'};
1.23      foxr      158: 	#
                    159: 	#   Replace the ~username of the URL with /home/username/public_html
                    160: 	#   so that we don't have to worry about ~ expansion internally.
                    161: 	#
1.118     raeburn   162: 	$fn=~s/^https?\:\/\/[^\/]+\///;
1.32      www       163:         $fn=~s/^\///;
1.98      albertel  164:         $fn=~s{~($LONCAPA::username_re)}{/home/$1/public_html};
1.23      foxr      165: 	
                    166: 	#  Remove trailing / strings (?) 
                    167: 	
                    168: 	$fn=~s/\/[^\/]+$//;
1.24      albertel  169:     } else {
                    170: 	#   If no form is defined, use request filename.
                    171: 	$fn = $r->filename();
                    172: 	my $lonDocRoot=$r->dir_config('lonDocRoot');
                    173: 	if ( $fn =~ /$lonDocRoot/ ) {
                    174: 	    #internal authentication, needs fixup.
                    175: 	    $fn = $r->uri(); # non users do not get the full path request
                    176:                              # through SCRIPT_FILENAME
1.98      albertel  177: 	    $fn=~s{^/~($LONCAPA::username_re)}{/home/$1/public_html};
1.24      albertel  178: 	}
1.23      foxr      179:     }
1.37      www       180:     $fn=~s/\/+/\//g;
1.23      foxr      181:     return $fn;
                    182: }
                    183: #
                    184: #   Output the header of the page.  This includes:
                    185: #   - The HTML header 
                    186: #   - The H1/H3  stuff which includes the directory.
                    187: #
                    188: #     startpage($r, $uame, $udom, $thisdisfn);
                    189: #      $r     - The apache request object.
                    190: #      $uname - User name.
                    191: #      $udom  - Domain name the user is logged in under.
                    192: #      $thisdisfn - Displayable version of the filename.
1.26      www       193: 
1.23      foxr      194: sub startpage {
                    195:     my ($r, $uname, $udom, $thisdisfn) = @_;
1.64      raeburn   196:     my $currdir = '/priv/'.$uname.$thisdisfn;
1.39      www       197:     &Apache::loncommon::content_type($r,'text/html');
1.23      foxr      198:     $r->send_http_header;
1.64      raeburn   199: 
1.68      raeburn   200:     my $formaction='/priv/'.$uname.$thisdisfn.'/';
1.89      albertel  201:     $formaction=~s|/+|/|g;
1.66      raeburn   202:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
1.121   ! bisitz    203: 
1.78      albertel  204:     if ($env{'environment.remote'} eq 'off') {
1.121   ! bisitz    205:         $env{'request.noversionuri'}=$currdir.'/';
        !           206:         $r->print(&Apache::loncommon::start_page('Construction Space',undef));
1.65      albertel  207:     } else {
1.121   ! bisitz    208:         $r->print(&Apache::loncommon::start_page('Construction Space',undef,
        !           209:                                                  { 'only_body' => 1,}));
1.65      albertel  210:     }
1.90      albertel  211: 
1.121   ! bisitz    212:     $r->print(&Apache::lonhtmlcommon::breadcrumbs()); # FIXME add breadcrumbs
        !           213: 
        !           214:     $r->print(&Apache::loncommon::head_subbox(
        !           215:                 &Apache::loncommon::CSTR_pageheader(1)));
        !           216: 
1.101     albertel  217:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
1.108     raeburn   218:     my $doctitle = 'LON-CAPA '.&mt('Construction Space');
1.109     bisitz    219:     my $newname = &mt('New Name');
1.29      www       220:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
1.77      albertel  221: <script type="text/javascript">
1.107     bisitz    222: top.document.title = '$esc_thisdisfn/ - $doctitle';
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:         }
1.113     schafran  234:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
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:         }
1.113     schafran  264:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
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;
1.109     bisitz    316:         var newname=prompt('$newname',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.120     bisitz    331:         $r->print('<h3>'.&mt('Co-Author [_1]',$uname.':'.$udom).'</h3>');
1.23      foxr      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',
1.113     schafran  348:                                        edit => 'Edit Metadata', 
1.84      www       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',
1.103     albertel  358:                                        nbt  => 'New bridgetask file',
1.84      www       359:                                        nsub => 'New subdirectory',
                    360:                                        renm => 'Rename current file to',
                    361:                                        move => 'Move current file to',
                    362:                                        copy => 'Copy current file to',
                    363:                                        type => 'Type Name Here',
1.105     albertel  364:                                        go   => 'Go',
1.84      www       365:                                        prnt => 'Print contents of directory',
                    366:                                        crea => 'Create a new directory or LON-CAPA document',
                    367: 				       acti => 'Actions for current directory',
1.105     albertel  368: 				       updc => 'Upload a new document',
                    369: 				       pick => 'Please select an action to perform using the new filename',
1.84      www       370:                                       );
1.106     bisitz    371:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
1.64      raeburn   372:     $r->print(<<END);
1.117     harmsja   373: <div class="LC_columnSection">
1.116     bisitz    374:   <div>
                    375:     <form name="curractions" method="post" action="">
                    376:       <fieldset>
                    377:         <legend>$lt{'acti'}</legend>
                    378:         <select name="dirtask" onchange="currdiract(this.form)">
1.84      www       379:             <option>$lt{'sela'}</option>
                    380:             <option value="publish">$lt{'pubd'}</option>
1.113     schafran  381:             <option value="editmeta">$lt{'edit'}</option>
1.84      www       382:             <option value="printdir">$lt{'prnt'}</option>
1.88      raeburn   383:             <option value="delete">$lt{'dedr'}</option>
1.116     bisitz    384:         </select>
                    385:         <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
                    386:       </fieldset>
                    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:   </div>
                    396: 
                    397:   <div>
                    398:     <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
                    399:       <fieldset>
                    400:         <legend>$lt{'updc'}</legend>
                    401:         <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
                    402:         <input type="file" name="upfile" size="20" />
                    403:         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
                    404:       </fieldset>
                    405:     </form>
                    406:   </div>
                    407: 
                    408:   <div>
                    409:     <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
                    410:       <fieldset>
                    411:               <legend>$lt{'crea'}</legend>
                    412: 	      <span class="LC_nobreak">
1.64      raeburn   413: 		<input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
1.105     albertel  414:                   <script type="text/javascript">
                    415:                     function validate_go() {
                    416:                         var selected = document.fileaction.action.selectedIndex;
                    417:                         if (selected == 0) {
                    418:                             alert('$lt{'pick'}');
                    419:                         } else {
                    420:                             document.fileaction.submit();
                    421:                         }
                    422:                     }
                    423:                   </script>
                    424: 		  <select name="action">
                    425: 		    <option value="none">$lt{'sela'}</option>
                    426: 		    <option value="newfile">$lt{'nfil'}:</option>
                    427: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
                    428: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
                    429:                     <option value="newpagefile">$lt{'npag'}:</option>
                    430:                     <option value="newsequencefile">$lt{'nseq'}:</option>
                    431:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
                    432:                     <option value="newstyfile">$lt{'nsty'}:</option>
                    433:                     <option value="newtaskfile">$lt{'nbt'}:</option>
                    434:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
                    435: 	            <option value="newdir">$lt{'nsub'}:</option>
1.106     bisitz    436: 		  </select>&nbsp;<input type="text" name="newfilename" value="$lt{'type'}" onfocus="if (this.value == '$mytype') this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
1.92      albertel  437: 		 </span>
1.116     bisitz    438:       </fieldset>
                    439:     </form>
                    440:   </div>
                    441: </div>
1.64      raeburn   442: END
                    443: }
                    444: 
1.72      raeburn   445: sub pubbuttons {
                    446:     my ($r,$uname,$thisdisfn) = @_;
                    447:     $r->print('<form method="post" action="/adm/publish" target="_parent">'.
                    448:               '<table><tr><td><input type="hidden" name="filename" value="/~'.
                    449:                $uname.$thisdisfn.'/" />'.
1.79      www       450:               '<input type="submit" value="'.&mt('Publish Directory').'" /></td><td>'.
1.85      albertel  451: '<input type="button" onclick="window.location='."'/~".
1.72      raeburn   452:                $uname.$thisdisfn."/default.meta'".'" value="'.
1.119     schafran  453: &mt('Edit Directory Metadata').'" /></td></tr></table></form>');
1.72      raeburn   454: }
                    455: 
1.64      raeburn   456: sub resourceactions {
                    457:     my ($r,$uname,$udom,$thisdisfn) = @_;
                    458:     $r->print(<<END);
                    459:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
                    460:          <input type="hidden" name="filename" value="" />
                    461:          <input type="hidden" name="newfilename" value="" />
                    462:          <input type="hidden" name="action" value="" />
                    463:        </form>
                    464:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
                    465:          <input type="hidden" name="filename" value="" />
                    466:          <input type="hidden" name="action" value="delete" />
                    467:        </form>
                    468:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
                    469:          <input type="hidden" name="filename" value="" />
1.80      www       470:          <input type="hidden" name="makeobsolete" value="0" />
1.64      raeburn   471:        </form>
                    472:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
                    473:            <input type="hidden" name="postdata" value="" />
                    474:        </form>
                    475:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
                    476:            <input type="hidden" name="filename" value="" />
                    477:        </form>
1.82      www       478:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
                    479:            <input type="hidden" name="filename" value="" />
                    480:        </form>
1.64      raeburn   481: END
1.23      foxr      482: }
                    483: 
                    484: #
                    485: #   Get the title string or "[untitled]" if the file has no title metadata:
                    486: #   Without the latter substitution, it's impossible to examine metadata for
                    487: #   untitled resources.  Resources may be legitimately untitled, to prevent
                    488: #   searches from locating them.
                    489: #
                    490: #   $str = getTitleString($fullname);
                    491: #       $fullname - Fully qualified filename to check.
                    492: #
                    493: sub getTitleString {
                    494:     my $fullname = shift;
                    495:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
                    496: 
                    497:     unless ($title) {
1.40      www       498: 	$title = "[".&mt('untitled')."]";
1.23      foxr      499:     }
                    500:     return $title;
                    501: }
                    502: 
1.55      www       503: sub getCopyRightString {
                    504:     my $fullname = shift;
                    505:     return &Apache::lonnet::metadata($fullname, 'copyright');
                    506: }
1.61      www       507: 
                    508: sub getSourceRightString {
                    509:     my $fullname = shift;
                    510:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
                    511: }
1.23      foxr      512: #
1.21      foxr      513: #  Put out a directory table row:
                    514: #    putdirectory(r, base, here, dirname, modtime)
                    515: #      r       - Apache request object.
                    516: #      reqfile - File in request.
                    517: #      here    - Where we are in directory tree.
                    518: #      dirname - Name of directory special file.
                    519: #      modtime - Encoded modification time.
                    520: # 
                    521: sub putdirectory {
1.64      raeburn   522:     my ($r, $reqfile, $here, $dirname, $modtime, $resdir, $bombs, $numdir) = @_;
1.21      foxr      523:     # construct the display filename: the directory name unless ..:
                    524:     
                    525:     my $disfilename = $dirname;
                    526:     if ($dirname eq '..') {
1.39      www       527: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
1.21      foxr      528:     }
1.64      raeburn   529:     unless ( (($dirname eq '..') && ($reqfile eq '')) || ($dirname eq '.')) {
1.50      www       530: 	my $kaputt=0;
                    531: 	foreach (keys %{$bombs}) {
1.56      albertel  532: 	    if ($_=~m:^\Q$resdir\E/\Q$disfilename\E/:) { $kaputt=1; last; }
1.50      www       533: 	}
1.52      www       534: 	%Apache::lonpublisher::metadatafields=();
                    535: 	%Apache::lonpublisher::metadatakeys=();
                    536: 	my $construct=$here;
1.98      albertel  537: 	$construct=~s{^/priv/($LONCAPA::username_re)$}{/home/$1/public_html};
1.67      raeburn   538:         my $dirpath = $here;
1.98      albertel  539:         $dirpath=~s{^/priv/}{/~};
1.52      www       540: 	&Apache::lonpublisher::metaeval(&Apache::lonnet::getfile(
                    541:        				 $construct.'/'.$dirname.'/default.meta'
                    542: 								 ));
1.64      raeburn   543:         my $actionitem = '';
                    544:         if ($dirname eq '..') {
1.109     bisitz    545:             $actionitem = &mt('Go to ...');
1.64      raeburn   546:         } else {
                    547:             $actionitem = 
                    548:                     '<form name="dirselect_'.$$numdir.
                    549:                     '" action="/adm/publish" target="_parent">'.
1.85      albertel  550:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
1.64      raeburn   551:                       '<option selected="selected">'.&mt('Select action').'</option>'.
                    552:                       '<option value="open">'.&mt('Open').'</option>'.
                    553:                       '<option value="publish">'.&mt('Publish').'</option>'.
1.113     schafran  554:                       '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
1.77      albertel  555:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
1.88      raeburn   556:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
1.64      raeburn   557:                     '</select>'.
1.86      albertel  558:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($dirpath.'/'.$dirname,'<>&"').'/" />'.
1.75      albertel  559:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
1.64      raeburn   560:                      '<input type="hidden" name="postdata" value="" />'.
                    561:                    '</form>';
                    562:             $$numdir ++;
                    563:         }
1.92      albertel  564: 	$r->print('<tr class="LC_browser_folder">'.
1.53      www       565: 		  '<td><img src="'.
1.86      albertel  566: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/folder_closed.gif" alt="folder" /></td>'.
1.64      raeburn   567: 		  '<td>'.$actionitem.'</td>'.
1.92      albertel  568: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
                    569: 		  $disfilename.'</a></span></td>'.
1.115     bisitz    570: 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($resdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
1.92      albertel  571: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
                    572: 	    $r->print(' <i>'.
                    573: 		      $Apache::lonpublisher::metadatafields{'subject'}.
                    574: 		      '</i> ');
                    575: 	}
                    576: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
1.42      www       577: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
1.25      www       578: 		  "</tr>\n");
1.64      raeburn   579:     }
1.21      foxr      580:     return OK;
                    581: }
1.22      foxr      582: #
                    583: #   Put a table row for a file resource.
                    584: #
                    585: sub putresource {
1.64      raeburn   586:     my ($r, $udom, $uname, $filename, $thisdisfn, 
1.22      foxr      587: 	$resdir, $targetdir, $linkdir,
1.64      raeburn   588: 	$cmtime,$bombs,$numres) = @_;
1.81      www       589:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
1.64      raeburn   590:     my $pubstatus = 'unpublished';
1.47      sakharuk  591:     my $status=&mt('Unpublished');
1.92      albertel  592:     my $css_class='LC_browser_file';
1.22      foxr      593:     my $title='&nbsp;';
1.60      albertel  594:     my $publish_button=&mt('Publish');
1.95      albertel  595:     my $cstr_dir = '/home/'.$uname.'/public_html/'.$thisdisfn.'/';
1.64      raeburn   596: #    my $action_buttons=
                    597: #        '<br /><a target="_parent" href="/adm/cfile?action=delete&filename=/~'.
                    598: #	$uname.'/'.$thisdisfn.'/'.$filename.'">'.
                    599: #	&mt('Delete').'</a>';
1.22      foxr      600:     if (-e $resdir.'/'.$filename) {
1.91      www       601:         my $same=0;
1.22      foxr      602: 	my ($rdev,$rino,$rmode,$rnlink,
                    603: 	    $ruid,$rgid,$rrdev,$rsize,
                    604: 	    $ratime,$rmtime,$rctime,
                    605: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
1.91      www       606:         if ($rmtime>=$cmtime) {
                    607:            $same=1;
                    608:         } else {
                    609:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
1.95      albertel  610: 						     $cstr_dir.'/'.$filename)) {
1.91      www       611:               $same=0;
                    612:            } else {
                    613:               $same=1;
                    614:            }
                    615:         }
1.95      albertel  616: 	my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
                    617: 	my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
                    618: 	my $meta_same = 1;
                    619: 	if ($meta_rmtime < $meta_cmtime
                    620: 	    && &Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
                    621: 						     $cstr_dir.'/'.$filename.'.meta')) {
                    622: 	    $meta_same = 0;
                    623: 	}
1.64      raeburn   624: 	$publish_button=&mt('Re-publish');
1.95      albertel  625: 	my $rights_status =
                    626: 	    &mt(&getCopyRightString($targetdir.'/'.$filename)).' '.
                    627: 	    &mt(&getSourceRightString($targetdir.'/'.$filename));
                    628: 	$title = '<a href="/res/'.$targetdir.'/'.$filename.
                    629: 	    '.meta" target="cat">'.
                    630: 	    &getTitleString($targetdir.'/'.$filename).'</a>';
1.91      www       631: 	if ($same) {
1.40      www       632: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
1.64      raeburn   633:                 $pubstatus = 'obsolete';
1.41      www       634: 		$status=&mt('Obsolete');
1.95      albertel  635:             } else {
                    636: 		if (!$meta_same) {
                    637: 		    $pubstatus = 'metamodified';
                    638: 		} else {
                    639: 		    $pubstatus = 'published';
                    640: 		}
                    641: 		$status=&mt('Published').
                    642: 		    '<br />'. $rights_status;
                    643: 	    }
1.64      raeburn   644: #	    } else {
                    645: #		$action_buttons='';
                    646: #	    }
1.22      foxr      647: 	} else {
1.64      raeburn   648:             $pubstatus = 'modified';
1.95      albertel  649: 	    $status=&mt('Modified').
                    650: 		'<br />'. $rights_status;
1.64      raeburn   651: #	    $action_buttons='';
1.22      foxr      652: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
1.57      www       653: 		$status.='<br /><a href="/adm/diff?filename=/~'.$uname.
1.22      foxr      654: 		    $thisdisfn.'/'.$filename.
1.77      albertel  655: 		    '&amp;versiontwo=priv" target="cat">'.&mt('Diffs').'</a>';
1.22      foxr      656: 	    }
1.95      albertel  657: 	} 
                    658: 
1.77      albertel  659: 	$title.="\n".'<br /><a href="/~'.$uname.$thisdisfn.'/'.$filename.'.meta">'. 
1.110     bisitz    660: 	    ($$bombs{$targetdir.'/'.$filename}?'<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':&mt('Edit Metadata')).'</a>';
1.95      albertel  661: 
                    662: 	if (!$meta_same) {
                    663: 	    $title = &mt('Metadata Modified').'<br />'.$title.
                    664: 		'<br /><a href="/adm/diff?filename=/~'.$uname.
                    665: 		$thisdisfn.'/'.$filename.'.meta'.
                    666: 		'&amp;versiontwo=priv" target="cat">'.&mt('Metadata Diffs').'</a>';
                    667: 	    $title.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
                    668: 		$thisdisfn.'/'.$filename.'.meta" target="_parent">'.&mt('Retrieve Metadata').'</a>';
                    669: 	}
1.77      albertel  670: 	$status.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
1.64      raeburn   671: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Retrieve').'</a>';
1.22      foxr      672:     }
1.33      www       673:     my $editlink='';
1.38      taceyjo1  674:     my $editlink2='';
1.36      www       675:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
1.64      raeburn   676: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('Edit').'</a>)';
1.34      www       677:     }
                    678:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
1.64      raeburn   679: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('EditXML').'</a>)';
                    680: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_parent">'.&mt('Edit').'</a>)';
1.43      taceyjo1  681:     }
1.82      www       682:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
1.83      www       683: 	$editlink.=' (<a href="/adm/cleanup?filename=/~'.$uname.
                    684: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Clean Up').')</a>';
1.82      www       685:     }
1.43      taceyjo1  686:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
1.46      taceyjo1  687: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress=/~'.
                    688: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Decompress').'</a>)';
1.33      www       689:     }
1.64      raeburn   690:     my $pub_select = '';
                    691:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
1.115     bisitz    692:     $r->print(&Apache::loncommon::start_data_table_row().
1.53      www       693: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
1.86      albertel  694: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
1.64      raeburn   695:               '<td>'.$pub_select.'</td>'.
1.104     albertel  696: 	      '<td><span class="LC_filename">'.
1.64      raeburn   697: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
1.92      albertel  698:                $filename.'</a></span>'.$editlink2.$editlink.
1.22      foxr      699: 	      '</td>'.
                    700: 	      '<td>'.$title.'</td>'.
1.115     bisitz    701:               '<td class="LC_browser_file_'.$pubstatus.'">&nbsp;&nbsp;</td>'. # Display publication status
                    702:               '<td>'.$status.'</td>'.
1.42      www       703: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
1.115     bisitz    704: 	      &Apache::loncommon::end_data_table_row()
                    705:     );
1.22      foxr      706:     return OK;
1.23      foxr      707: }
1.64      raeburn   708: 
                    709: sub create_pubselect {
                    710:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
                    711:     $$pub_select = '
                    712: <form name="resselect_'.$$numres.'" action="">
1.85      albertel  713: <select name="reschoice"  onchange="SetResChoice(this.form)">
1.77      albertel  714: <option>'.&mt('Select action').'</option>'.
                    715: '<option value="copy">'.&mt('Copy').'</option>';
1.68      raeburn   716:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
                    717:         $$pub_select .= 
1.77      albertel  718: '<option value="rename">'.&mt('Rename').'</option>'.
                    719: '<option value="move">'.&mt('Move').'</option>'.
                    720: '<option value="delete">'.&mt('Delete').'</option>';
1.64      raeburn   721:     } else {
                    722:         $$pub_select .= '
1.77      albertel  723: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
1.64      raeburn   724:     }
                    725: # check for versions
                    726:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
                    727:     if ($versions > 0) {
                    728:         $$pub_select .='
1.77      albertel  729: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
1.64      raeburn   730:     }
                    731:     $$pub_select .= '
1.77      albertel  732: <option value="publish">'.$publish_button.'</option>'.
1.82      www       733: '<option value="cleanup">'.&mt('Clean up').'</option>'.
1.77      albertel  734: '<option value="print">'.&mt('Print').'</option>'.
1.68      raeburn   735: '</select>
1.64      raeburn   736: <input type="hidden" name="filename" value="/~'.
1.96      banghart  737:  &HTML::Entities::encode($uname.$thisdisfn.'/'.$filename,'<>&"').'" />
                    738:  <input type="hidden" name="dispfilename" value="'.
1.99      albertel  739:  &HTML::Entities::encode($filename).'" /></form>';
1.64      raeburn   740:     $$numres ++;
                    741: }
                    742: 
                    743: sub check_for_versions {
                    744:     my ($r,$fn,$udom,$uname) = @_;
                    745:     my $versions = 0;
                    746:     my $docroot=$r->dir_config('lonDocRoot');
                    747:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                    748:     my $resdir=$resfn;
                    749:     $resdir=~s/\/[^\/]+$/\//;
                    750:     $fn=~/\/([^\/]+)\.(\w+)$/;
                    751:     my $main=$1;
                    752:     my $suffix=$2;
                    753:     opendir(DIR,$resdir);
                    754:     while (my $filename=readdir(DIR)) {
                    755:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
                    756:             $versions ++;        
                    757:         }
                    758:     }
                    759:     return $versions;
                    760: }
                    761: 
1.23      foxr      762: #
                    763: #   Categorize files in the directory.
                    764: #   For each file in a list of files in a file directory, 
                    765: #   the  file categorized as one of:
                    766: #    - directory  
                    767: #    - sequence
                    768: #    - problem 
                    769: #    - Other resource.
                    770: #
                    771: #   For each file the modification date is determined as well.
                    772: #   Returned is a list of sublists:
                    773: #    (directories, sequences, problems, other)
                    774: #   each of the sublists contains entries of the following form (sorted by
                    775: #   filename):
                    776: #     (filename, typecode, lastmodtime)
                    777: #
                    778: #   $list = CategorizeFiles($location, $files)
                    779: #       $location   - Directory in which the files live (relative to our
                    780: #                     execution.
                    781: #       $files      - list of files.
                    782: #
                    783: sub CategorizeFiles {
                    784:     my $location = shift;
                    785:     my $files    = shift;
1.22      foxr      786: }
                    787: 
1.4       www       788: 1;
                    789: __END__
1.17      harris41  790: 
                    791: 
1.114     jms       792: =head1 NAME
                    793: 
                    794: Apache::lonpubdir - Construction space directory lister
                    795: 
                    796: =head1 SYNOPSIS
                    797: 
                    798: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    799: 
                    800:  <LocationMatch "^/\~.*/$">
                    801:  PerlAccessHandler       Apache::loncacc
                    802:  SetHandler perl-script
                    803:  PerlHandler Apache::lonpubdir
                    804:  ErrorDocument     403 /adm/login
                    805:  ErrorDocument     404 /adm/notfound.html
                    806:  ErrorDocument     406 /adm/unauthorized.html
                    807:  ErrorDocument	  500 /adm/errorhandler
                    808:  </LocationMatch>
                    809: 
                    810:  <Location /adm/pubdir>
                    811:  PerlAccessHandler       Apache::lonacc
                    812:  SetHandler perl-script
                    813:  PerlHandler Apache::lonpubdir
                    814:  ErrorDocument     403 /adm/login
                    815:  ErrorDocument     404 /adm/notfound.html
                    816:  ErrorDocument     406 /adm/unauthorized.html
                    817:  ErrorDocument	  500 /adm/errorhandler
                    818:  </Location>
                    819: 
                    820: =head1 INTRODUCTION
                    821: 
                    822: This module publishes a directory of files.
                    823: 
                    824: This is part of the LearningOnline Network with CAPA project
                    825: described at http://www.lon-capa.org.
                    826: 
                    827: =head1 HANDLER SUBROUTINE
                    828: 
                    829: This routine is called by Apache and mod_perl.
                    830: 
                    831: =over 4
                    832: 
                    833: =item *
                    834: 
                    835: read in information
                    836: 
                    837: =item *
                    838: 
                    839: start page output
                    840: 
                    841: =item *
                    842: 
                    843: run through list of files and attempt to publish unhidden files
                    844: 
                    845: =back
                    846: 
                    847: =head1 SUBROUTINES:
                    848: 
                    849: =over
                    850: 
                    851: =item startpage($r, $uame, $udom, $thisdisfn)
                    852: 
                    853: Output the header of the page.  This includes:
                    854:  - The HTML header 
                    855:  - The H1/H3  stuff which includes the directory.
                    856:  
                    857:     startpage($r, $uame, $udom, $thisdisfn);
                    858:         $r     - The apache request object.
                    859:         $uname - User name.
                    860:         $udom  - Domain name the user is logged in under.
                    861:         $thisdisfn - Displayable version of the filename.
                    862: 
                    863: =item getTitleString($fullname)
                    864: 
                    865:     Get the title string or "[untitled]" if the file has no title metadata:
                    866:     Without the latter substitution, it's impossible to examine metadata for
                    867:     untitled resources.  Resources may be legitimately untitled, to prevent
                    868:     searches from locating them.
                    869:     
                    870:     $str = getTitleString($fullname);
                    871:         $fullname - Fully qualified filename to check.
                    872: 
                    873: =item putdirectory(r, base, here, dirname, modtime)
                    874: 
                    875:     Put out a directory table row:
                    876:     
                    877:     putdirectory($r, $base, $here, $dirname, $modtime)
                    878:         $r       - Apache request object.
                    879:         $reqfile - File in request.
                    880:         $here    - Where we are in directory tree.
                    881:         $dirname - Name of directory special file.
                    882:         $modtime - Encoded modification time.
                    883: 
                    884: =item CategorizeFiles($location, $files)
                    885:     
                    886:     Categorize files in the directory.
                    887:     For each file in a list of files in a file directory, 
                    888:     the  file categorized as one of:
                    889:         - directory  
                    890:         - sequence
                    891:         - problem 
                    892:         - Other resource.
                    893: 
                    894:     For each file the modification date is determined as well.
                    895:     Returned is a list of sublists:
                    896:         (directories, sequences, problems, other)
                    897:     each of the sublists contains entries of the following form (sorted by filename):
                    898:     (filename, typecode, lastmodtime)
                    899:     
                    900:         $list = CategorizeFiles($location, $files)
                    901:         $location   - Directory in which the files live (relative to our execution)
                    902:         $files      - list of files.
                    903: 
                    904: =back
                    905: 
                    906: =cut
                    907: 

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