File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.126: download - view: text, annotated - select for diffs
Wed Mar 10 21:26:04 2010 UTC (14 years, 3 months ago) by droeschl
Branches: MAIN
CVS tags: PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD
In process of removing remote control, remote navmap and different icon modes code
Also some minor refactoring.

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

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