File:  [LON-CAPA] / loncom / publisher / lonpublisher.pm
Revision 1.87: download - view: text, annotated - select for diffs
Wed Aug 7 19:50:22 2002 UTC (21 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- should handle < and > inside <m> when publishing

    1: # The LearningOnline Network with CAPA
    2: # Publication Handler
    3: #
    4: # $Id: lonpublisher.pm,v 1.87 2002/08/07 19:50:22 albertel 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: # (TeX Content Handler
   30: #
   31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   32: #
   33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   34: # 03/23 Guy Albertelli
   35: # 03/24,03/29,04/03 Gerd Kortemeyer
   36: # 04/16/2001 Scott Harrison
   37: # 05/03,05/05,05/07 Gerd Kortemeyer
   38: # 05/28/2001 Scott Harrison
   39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
   40: # 12/04,12/05 Guy Albertelli
   41: # 12/05 Gerd Kortemeyer
   42: # 12/05 Guy Albertelli
   43: # 12/06,12/07 Gerd Kortemeyer
   44: # 12/15,12/16 Scott Harrison
   45: # 12/25 Gerd Kortemeyer
   46: # YEAR=2002
   47: # 1/16,1/17 Scott Harrison
   48: # 1/17 Gerd Kortemeyer
   49: #
   50: ###
   51: 
   52: ###############################################################################
   53: ##                                                                           ##
   54: ## ORGANIZATION OF THIS PERL MODULE                                          ##
   55: ##                                                                           ##
   56: ## 1. Modules used by this module                                            ##
   57: ## 2. Various subroutines                                                    ##
   58: ## 3. Publication Step One                                                   ##
   59: ## 4. Phase Two                                                              ##
   60: ## 5. Main Handler                                                           ##
   61: ##                                                                           ##
   62: ###############################################################################
   63: 
   64: package Apache::lonpublisher;
   65: 
   66: # ------------------------------------------------- modules used by this module
   67: use strict;
   68: use Apache::File;
   69: use File::Copy;
   70: use Apache::Constants qw(:common :http :methods);
   71: use HTML::LCParser;
   72: use Apache::lonxml;
   73: use Apache::lonhomework;
   74: use Apache::loncacc;
   75: use DBI;
   76: use Apache::lonnet();
   77: use Apache::loncommon();
   78: 
   79: my %addid;
   80: my %nokey;
   81: 
   82: my %metadatafields;
   83: my %metadatakeys;
   84: 
   85: my $docroot;
   86: 
   87: my $cuname;
   88: my $cudom;
   89: 
   90: # ----------------------------------------------- Evaluate string with metadata
   91: sub metaeval {
   92:     my $metastring=shift;
   93:    
   94:         my $parser=HTML::LCParser->new(\$metastring);
   95:         my $token;
   96:         while ($token=$parser->get_token) {
   97:            if ($token->[0] eq 'S') {
   98: 	      my $entry=$token->[1];
   99:               my $unikey=$entry;
  100:               if (defined($token->[2]->{'package'})) { 
  101:                   $unikey.='_package_'.$token->[2]->{'package'};
  102:               } 
  103:               if (defined($token->[2]->{'part'})) { 
  104:                  $unikey.='_'.$token->[2]->{'part'}; 
  105: 	      }
  106:               if (defined($token->[2]->{'id'})) { 
  107:                   $unikey.='_'.$token->[2]->{'id'};
  108:               } 
  109:               if (defined($token->[2]->{'name'})) { 
  110:                  $unikey.='_'.$token->[2]->{'name'}; 
  111: 	      }
  112:               foreach (@{$token->[3]}) {
  113: 		  $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
  114:                   if ($metadatakeys{$unikey}) {
  115: 		      $metadatakeys{$unikey}.=','.$_;
  116:                   } else {
  117:                       $metadatakeys{$unikey}=$_;
  118:                   }
  119:               }
  120:               if ($metadatafields{$unikey}) {
  121: 		  my $newentry=$parser->get_text('/'.$entry);
  122:                   unless (($metadatafields{$unikey}=~/$newentry/) ||
  123:                           ($newentry eq '')) {
  124:                      $metadatafields{$unikey}.=', '.$newentry;
  125: 		  }
  126: 	      } else {
  127:                  $metadatafields{$unikey}=$parser->get_text('/'.$entry);
  128:               }
  129:           }
  130:        }
  131: }
  132: 
  133: # -------------------------------------------------------- Read a metadata file
  134: sub metaread {
  135:     my ($logfile,$fn)=@_;
  136:     unless (-e $fn) {
  137: 	print $logfile 'No file '.$fn."\n";
  138:         return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
  139:     }
  140:     print $logfile 'Processing '.$fn."\n";
  141:     my $metastring;
  142:     {
  143:      my $metafh=Apache::File->new($fn);
  144:      $metastring=join('',<$metafh>);
  145:     }
  146:     &metaeval($metastring);
  147:     return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
  148: }
  149: 
  150: # ---------------------------- convert 'time' format into a datetime sql format
  151: sub sqltime {
  152:     my $timef=shift @_;
  153:     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  154: 	localtime($timef);
  155:     $mon++; $year+=1900;
  156:     return "$year-$mon-$mday $hour:$min:$sec";
  157: }
  158: 
  159: # --------------------------------------------------------- Various form fields
  160: 
  161: sub textfield {
  162:     my ($title,$name,$value)=@_;
  163:     return "\n<p><b>$title:</b><br>".
  164:            '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
  165: }
  166: 
  167: sub hiddenfield {
  168:     my ($name,$value)=@_;
  169:     return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
  170: }
  171: 
  172: sub selectbox {
  173:     my ($title,$name,$value,$functionref,@idlist)=@_;
  174:     my $uctitle=uc($title);
  175:     my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
  176: 	"</b></font><br />".'<select name="'.$name.'">';
  177:     foreach (@idlist) {
  178:         $selout.='<option value=\''.$_.'\'';
  179:         if ($_ eq $value) {
  180: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
  181: 	}
  182:         else {$selout.='>'.&{$functionref}($_).'</option>';}
  183:     }
  184:     return $selout.'</select>';
  185: }
  186: 
  187: # -------------------------------------------------------- Publication Step One
  188: 
  189: sub urlfixup {
  190:     my ($url,$target)=@_;
  191:     unless ($url) { return ''; }
  192:     #javascript code needs no fixing
  193:     if ($url =~ /^javascript:/i) { return $url; }
  194:     if ($url =~ /^mailto:/i) { return $url; }
  195:     #internal document links need no fixing
  196:     if ($url =~ /^\#/) { return $url; } 
  197:     my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
  198:     foreach (values %Apache::lonnet::hostname) {
  199: 	if ($_ eq $host) {
  200: 	    $url=~s/^http\:\/\///;
  201:             $url=~s/^$host//;
  202:         }
  203:     }
  204:     if ($url=~/^http\:\/\//) { return $url; }
  205:     $url=~s/\~$cuname/res\/$cudom\/$cuname/;
  206:     return $url;
  207: }
  208: 
  209: 
  210: sub absoluteurl {
  211:     my ($url,$target)=@_;
  212:     unless ($url) { return ''; }
  213:     if ($target) {
  214: 	$target=~s/\/[^\/]+$//;
  215:        $url=&Apache::lonnet::hreflocation($target,$url);
  216:     }
  217:     return $url;
  218: }
  219: 
  220: sub set_allow {
  221:     my ($allow,$logfile,$target,$tag,$oldurl)=@_;
  222:     my $newurl=&urlfixup($oldurl,$target);
  223:     my $return_url=$oldurl;
  224:     print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
  225:     if ($newurl ne $oldurl) {
  226: 	$return_url=$newurl;
  227: 	print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
  228:     }
  229:     if (($newurl !~ /^javascript:/i) &&
  230: 	($newurl !~ /^mailto:/i) &&
  231: 	($newurl !~ /^http:/i) &&
  232: 	($newurl !~ /^\#/)) {
  233: 	$$allow{&absoluteurl($newurl,$target)}=1;
  234:     }
  235:     return $return_url
  236: }
  237: 
  238: sub get_subscribed_hosts {
  239:     my ($target)=@_;
  240:     my @subscribed;
  241:     my $filename;
  242:     $target=~/(.*)\/([^\/]+)$/;
  243:     my $srcf=$2;
  244:     opendir(DIR,$1);
  245:     while ($filename=readdir(DIR)) {
  246: 	if ($filename=~/$srcf\.(\w+)$/) {
  247: 	    my $subhost=$1;
  248: 	    if ($subhost ne 'meta' && $subhost ne 'subscription') {
  249: 		push(@subscribed,$subhost);
  250: 	    }
  251: 	}
  252:     }
  253:     closedir(DIR);
  254:     my $sh;
  255:     if ( $sh=Apache::File->new("$target.subscription") ) {
  256: 	&Apache::lonnet::logthis("opened $target.subscription");
  257: 	while (my $subline=<$sh>) {
  258: 	    &Apache::lonnet::logthis("Trying $subline");
  259: 	    if ($subline =~ /(^\w+):/) { push(@subscribed,$1); } else {
  260: 		&Apache::lonnet::logthis("No Match for $subline");
  261: 	    }
  262: 	}
  263:     } else {
  264: 	&Apache::lonnet::logthis("Un able to open $target.subscription");
  265:     }
  266:     &Apache::lonnet::logthis("Got list of ".join(':',@subscribed));
  267:     return @subscribed;
  268: }
  269: 
  270: 
  271: sub get_max_ids_indices {
  272:     my ($content)=@_;
  273:     my $maxindex=10;
  274:     my $maxid=10;
  275:     my $needsfixup=0;
  276: 
  277:     my $parser=HTML::LCParser->new($content);
  278:     my $token;
  279:     while ($token=$parser->get_token) {
  280: 	if ($token->[0] eq 'S') {
  281: 	    my $counter;
  282: 	    if ($counter=$addid{$token->[1]}) {
  283: 		if ($counter eq 'id') {
  284: 		    if (defined($token->[2]->{'id'})) {
  285: 			$maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
  286: 		    } else {
  287: 			$needsfixup=1;
  288: 		    }
  289: 		} else {
  290: 		    if (defined($token->[2]->{'index'})) {
  291: 			$maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
  292: 		    } else {
  293: 			$needsfixup=1;
  294: 		    }
  295: 		}
  296: 	    }
  297: 	}
  298:     }
  299:     return ($needsfixup,$maxid,$maxindex);
  300: }
  301: 
  302: sub get_all_text_unbalanced {
  303:     #there is a copy of this in lonxml.pm
  304:     my($tag,$pars)= @_;
  305:     my $token;
  306:     my $result='';
  307:     $tag='<'.$tag.'>';
  308:     while ($token = $$pars[-1]->get_token) {
  309: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
  310: 	    $result.=$token->[1];
  311: 	} elsif ($token->[0] eq 'PI') {
  312: 	    $result.=$token->[2];
  313: 	} elsif ($token->[0] eq 'S') {
  314: 	    $result.=$token->[4];
  315: 	} elsif ($token->[0] eq 'E')  {
  316: 	    $result.=$token->[2];
  317: 	}
  318: 	if ($result =~ /(.*)$tag(.*)/) {
  319: 	    &Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
  320: 	    &Apache::lonnet::logthis('Result is :'.$1);
  321: 	    $result=$1;
  322: 	    my $redo=$tag.$2;
  323: 	    push (@$pars,HTML::LCParser->new(\$redo));
  324: 	    $$pars[-1]->xml_mode('1');
  325: 	    last;
  326: 	}
  327:     }
  328:     return $result
  329: }
  330: 
  331: #Arguably this should all be done as a lonnet::ssi instead
  332: sub fix_ids_and_indices {
  333:     my ($logfile,$source,$target)=@_;
  334: 
  335:     my %allow;
  336:     my $content;
  337:     {
  338: 	my $org=Apache::File->new($source);
  339: 	$content=join('',<$org>);
  340:     }
  341: 
  342:     my ($needsfixup,$maxid,$maxindex)=&get_max_ids_indices(\$content);
  343: 
  344:     if ($needsfixup) {
  345: 	print $logfile "Needs ID and/or index fixup\n".
  346: 	    "Max ID   : $maxid (min 10)\n".
  347:                 "Max Index: $maxindex (min 10)\n";
  348:     }
  349:     my $outstring='';
  350:     my @parser;
  351:     $parser[0]=HTML::LCParser->new(\$content);
  352:     $parser[-1]->xml_mode(1);
  353:     my $token;
  354:     while (@parser) {
  355: 	while ($token=$parser[-1]->get_token) {
  356: 	    if ($token->[0] eq 'S') {
  357: 		my $counter;
  358: 		my $tag=$token->[1];
  359: 		my $lctag=lc($tag);
  360: 		if ($lctag eq 'allow') {
  361: 		    $allow{$token->[2]->{'src'}}=1;
  362: 		    next;
  363: 		}
  364: 		my %parms=%{$token->[2]};
  365: 		$counter=$addid{$tag};
  366: 		if (!$counter) { $counter=$addid{$lctag}; }
  367: 		if ($counter) {
  368: 		    if ($counter eq 'id') {
  369: 			unless (defined($parms{'id'})) {
  370: 			    $maxid++;
  371: 			    $parms{'id'}=$maxid;
  372: 			    print $logfile 'ID: '.$tag.':'.$maxid."\n";
  373: 			}
  374: 		    } elsif ($counter eq 'index') {
  375: 			unless (defined($parms{'index'})) {
  376: 			    $maxindex++;
  377: 			    $parms{'index'}=$maxindex;
  378: 			    print $logfile 'Index: '.$tag.':'.$maxindex."\n";
  379: 			}
  380: 		    }
  381: 		}
  382: 		foreach my $type ('src','href','background','bgimg') {
  383: 		    foreach my $key (keys(%parms)) {
  384: 			print $logfile "for $type, and $key\n";
  385: 			if ($key =~ /^$type$/i) {
  386: 			    print $logfile "calling set_allow\n";
  387: 			    $parms{$key}=&set_allow(\%allow,$logfile,
  388: 						    $target,$tag,
  389: 						    $parms{$key});
  390: 			}
  391: 		    }
  392: 		}
  393: 		# probably a <randomlabel> image type <label>
  394: 		if ($lctag eq 'label' && defined($parms{'description'})) {
  395: 		    my $next_token=$parser[-1]->get_token();
  396: 		    if ($next_token->[0] eq 'T') {
  397: 			$next_token->[1]=&set_allow(\%allow,$logfile,
  398: 						    $target,$tag,
  399: 						    $next_token->[1]);
  400: 		    }
  401: 		    $parser[-1]->unget_token($next_token);
  402: 		}
  403: 		if ($lctag eq 'applet') {
  404: 		    my $codebase='';
  405: 		    if (defined($parms{'codebase'})) {
  406: 			my $oldcodebase=$parms{'codebase'};
  407: 			unless ($oldcodebase=~/\/$/) {
  408: 			    $oldcodebase.='/';
  409: 			}
  410: 			$codebase=&urlfixup($oldcodebase,$target);
  411: 			$codebase=~s/\/$//;    
  412: 			if ($codebase ne $oldcodebase) {
  413: 			    $parms{'codebase'}=$codebase;
  414: 			    print $logfile 'URL codebase: '.$tag.':'.
  415: 				$oldcodebase.' - '.
  416: 				    $codebase."\n";
  417: 			}
  418: 			$allow{&absoluteurl($codebase,$target).'/*'}=1;
  419: 		    } else {
  420: 			foreach ('archive','code','object') {
  421: 			    if (defined($parms{$_})) {
  422: 				my $oldurl=$parms{$_};
  423: 				my $newurl=&urlfixup($oldurl,$target);
  424: 				$newurl=~s/\/[^\/]+$/\/\*/;
  425: 				print $logfile 'Allow: applet '.$_.':'.
  426: 				    $oldurl.' allows '.
  427: 					$newurl."\n";
  428: 				$allow{&absoluteurl($newurl,$target)}=1;
  429: 			    }
  430: 			}
  431: 		    }
  432: 		}
  433: 		my $newparmstring='';
  434: 		my $endtag='';
  435: 		foreach (keys %parms) {
  436: 		    if ($_ eq '/') {
  437: 			$endtag=' /';
  438: 		    } else { 
  439: 			my $quote=($parms{$_}=~/\"/?"'":'"');
  440: 			$newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
  441: 		    }
  442: 		}
  443: 		if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
  444: 		$outstring.='<'.$tag.$newparmstring.$endtag.'>';
  445: 		if ($lctag eq 'm') {
  446: 		    $outstring.=&get_all_text_unbalanced('/m',\@parser);
  447: 		}
  448: 	    } elsif ($token->[0] eq 'E') {
  449: 		if ($token->[2]) {
  450: 		    unless ($token->[1] eq 'allow') {
  451: 			$outstring.='</'.$token->[1].'>';
  452: 		    }
  453: 		}
  454: 	    } else {
  455: 		$outstring.=$token->[1];
  456: 	    }
  457: 	}
  458: 	pop(@parser);
  459:     }
  460: 
  461:     if ($needsfixup) {
  462: 	print $logfile "End of ID and/or index fixup\n".
  463: 	    "Max ID   : $maxid (min 10)\n".
  464: 		"Max Index: $maxindex (min 10)\n";
  465:     } else {
  466: 	print $logfile "Does not need ID and/or index fixup\n";
  467:     }
  468: 
  469:     return ($outstring,%allow);
  470: }
  471: 
  472: sub publish {
  473: 
  474:     my ($source,$target,$style)=@_;
  475:     my $logfile;
  476:     my $scrout='';
  477:     my $allmeta='';
  478:     my $content='';
  479:     my %allow=();
  480: 
  481:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  482: 	return 
  483:          '<font color=red>No write permission to user directory, FAIL</font>';
  484:     }
  485:     print $logfile 
  486: "\n\n================= Publish ".localtime()." Phase One  ================\n";
  487: 
  488:     if (($style eq 'ssi') || ($style eq 'rat')) {
  489: # ------------------------------------------------------- This needs processing
  490: 
  491: # ----------------------------------------------------------------- Backup Copy
  492: 	my $copyfile=$source.'.save';
  493:         if (copy($source,$copyfile)) {
  494: 	    print $logfile "Copied original file to ".$copyfile."\n";
  495:         } else {
  496: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
  497:           return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
  498:         }
  499: # ------------------------------------------------------------- IDs and indices
  500: 	
  501: 	my $outstring;
  502: 	($outstring,%allow)=&fix_ids_and_indices($logfile,$source,$target);
  503: # ------------------------------------------------------------ Construct Allows
  504:     
  505: 	$scrout.='<h3>Dependencies</h3>';
  506:         my $allowstr='';
  507:         foreach (sort(keys(%allow))) {
  508: 	   my $thisdep=$_;
  509: 	   if ($thisdep !~ /[^\s]/) { next; }
  510:            unless ($style eq 'rat') { 
  511:               $allowstr.="\n".'<allow src="'.$thisdep.'" />';
  512: 	   }
  513:            $scrout.='<br>';
  514:            unless ($thisdep=~/\*/) {
  515: 	       $scrout.='<a href="'.$thisdep.'">';
  516:            }
  517:            $scrout.='<tt>'.$thisdep.'</tt>';
  518:            unless ($thisdep=~/\*/) {
  519: 	       $scrout.='</a>';
  520:                if (
  521:        &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
  522:                                             $thisdep.'.meta') eq '-1') {
  523: 		   $scrout.=
  524:                            ' - <font color=red>Currently not available</font>';
  525:                } else {
  526:                    my %temphash=(&Apache::lonnet::declutter($target).'___'.
  527:                              &Apache::lonnet::declutter($thisdep).'___usage'
  528:                                  => time);
  529:                    $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
  530:                    if ((defined($1)) && (defined($2))) {
  531:                       &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
  532: 		   }
  533: 	       }
  534:            }
  535:         }
  536:         $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
  537: 
  538: 	#Encode any High ASCII characters
  539: 	$outstring=&HTML::Entities::encode($outstring,"\200-\377");
  540: # ------------------------------------------------------------- Write modified
  541: 
  542:         {
  543:           my $org;
  544:           unless ($org=Apache::File->new('>'.$source)) {
  545:              print $logfile "No write permit to $source\n";
  546:              return 
  547:               "<font color=red>No write permission to $source, FAIL</font>";
  548: 	  }
  549:           print $org $outstring;
  550:         }
  551: 	  $content=$outstring;
  552: 
  553:     }
  554: # --------------------------------------------- Initial step done, now metadata
  555: 
  556: # ---------------------------------------- Storage for metadata keys and fields
  557: 
  558:      %metadatafields=();
  559:      %metadatakeys=();
  560:      
  561:      my %oldparmstores=();
  562:      
  563:      
  564:      $scrout.='<h3>Metadata Information ' .
  565:        Apache::loncommon::help_open_topic("Metadata_Description")
  566:        . '</h3>';
  567: 
  568: # ------------------------------------------------ First, check out environment
  569:      unless (-e $source.'.meta') {
  570:         $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
  571: 	                          $ENV{'environment.middlename'}.' '.
  572: 		                  $ENV{'environment.lastname'}.' '.
  573: 		                  $ENV{'environment.generation'};
  574:         $metadatafields{'author'}=~s/\s+/ /g;
  575:         $metadatafields{'author'}=~s/\s+$//;
  576:         $metadatafields{'owner'}=$cuname.'@'.$cudom;
  577: 
  578: # ------------------------------------------------ Check out directory hierachy
  579: 
  580:         my $thisdisfn=$source;
  581:         $thisdisfn=~s/^\/home\/$cuname\///;
  582: 
  583:         my @urlparts=split(/\//,$thisdisfn);
  584:         $#urlparts--;
  585: 
  586:         my $currentpath='/home/'.$cuname.'/';
  587: 
  588:         foreach (@urlparts) {
  589: 	    $currentpath.=$_.'/';
  590:             $scrout.=&metaread($logfile,$currentpath.'default.meta');
  591:         }
  592: 
  593: # ------------------- Clear out parameters and stores (there should not be any)
  594: 
  595:         foreach (keys %metadatafields) {
  596: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  597: 		delete $metadatafields{$_};
  598:             }
  599:         }
  600: 
  601:     } else {
  602: # ---------------------- Read previous metafile, remember parameters and stores
  603: 
  604:         $scrout.=&metaread($logfile,$source.'.meta');
  605: 
  606:         foreach (keys %metadatafields) {
  607: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  608:                 $oldparmstores{$_}=1;
  609: 		delete $metadatafields{$_};
  610:             }
  611:         }
  612:         
  613:     }
  614: 
  615: # -------------------------------------------------- Parse content for metadata
  616:     if ($style eq 'ssi') {
  617:         my $oldenv=$ENV{'request.uri'};
  618: 
  619:         $ENV{'request.uri'}=$target;
  620:         $allmeta=Apache::lonxml::xmlparse(undef,'meta',$content);
  621:         $ENV{'request.uri'}=$oldenv;
  622: 
  623:         &metaeval($allmeta);
  624:     }
  625: # ---------------- Find and document discrepancies in the parameters and stores
  626: 
  627:         my $chparms='';
  628:         foreach (sort keys %metadatafields) {
  629: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  630:                 unless ($_=~/\.\w+$/) { 
  631:                    unless ($oldparmstores{$_}) {
  632: 		      print $logfile 'New: '.$_."\n";
  633:                       $chparms.=$_.' ';
  634:                    }
  635: 	        }
  636:             }
  637:         }
  638:         if ($chparms) {
  639: 	    $scrout.='<p><b>New parameters or stored values:</b> '.
  640:                      $chparms;
  641:         }
  642: 
  643:         $chparms='';
  644:         foreach (sort keys %oldparmstores) {
  645: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
  646:                 unless (($metadatafields{$_.'.name'}) ||
  647:                         ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
  648: 		    print $logfile 'Obsolete: '.$_."\n";
  649:                     $chparms.=$_.' ';
  650:                 }
  651:             }
  652:         }
  653:         if ($chparms) {
  654: 	    $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
  655:                      $chparms;
  656:         }
  657: 
  658: # ------------------------------------------------------- Now have all metadata
  659: 
  660:         $scrout.=
  661:      '<form name="pubform" action="/adm/publish" method="post">'.
  662:        '<p><input type="submit" value="Finalize Publication" /></p>'.
  663:           &hiddenfield('phase','two').
  664:           &hiddenfield('filename',$ENV{'form.filename'}).
  665: 	  &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
  666:           &hiddenfield('dependencies',join(',',keys %allow)).
  667:           &textfield('Title','title',$metadatafields{'title'}).
  668:           &textfield('Author(s)','author',$metadatafields{'author'}).
  669: 	  &textfield('Subject','subject',$metadatafields{'subject'});
  670: 
  671: # --------------------------------------------------- Scan content for keywords
  672: 
  673:         my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
  674: 	my $keywordout=<<"END";
  675: <script>
  676: function checkAll(field)
  677: {
  678:     for (i = 0; i < field.length; i++)
  679:         field[i].checked = true ;
  680: }
  681: 
  682: function uncheckAll(field)
  683: {
  684:     for (i = 0; i < field.length; i++)
  685:         field[i].checked = false ;
  686: }
  687: </script>
  688: <p><b>Keywords: $keywords_help</b> 
  689: <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)"> 
  690: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)"> 
  691: <br />
  692: END
  693:         $keywordout.='<table border=2><tr>';
  694:         my $colcount=0;
  695:         my %keywords=();
  696:         
  697: 	if (length($content)<500000) {
  698: 	    my $textonly=$content;
  699:             $textonly=~s/\<script[^\<]+\<\/script\>//g;
  700:             $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
  701:             $textonly=~s/\<[^\>]*\>//g;
  702:             $textonly=~tr/A-Z/a-z/;
  703:             $textonly=~s/[\$\&][a-z]\w*//g;
  704:             $textonly=~s/[^a-z\s]//g;
  705: 
  706:             foreach ($textonly=~m/(\w+)/g) {
  707: 		unless ($nokey{$_}) {
  708:                    $keywords{$_}=1;
  709:                 } 
  710:             }
  711:         }
  712: 
  713:             
  714:             foreach (split(/\W+/,$metadatafields{'keywords'})) {
  715: 		$keywords{$_}=1;
  716:             }
  717: 
  718:             foreach (sort keys %keywords) {
  719:                 $keywordout.='<td><input type=checkbox name="keywords" value="'.$_.'"';
  720:                 if ($metadatafields{'keywords'}) {
  721:                    if ($metadatafields{'keywords'}=~/$_/) { 
  722:                       $keywordout.=' checked'; 
  723:                    }
  724: 	        } elsif (&Apache::loncommon::keyword($_)) {
  725: 	            $keywordout.=' checked';
  726:                 } 
  727:                 $keywordout.='>'.$_.'</td>';
  728:                 if ($colcount>10) {
  729: 		    $keywordout.="</tr><tr>\n";
  730:                     $colcount=0;
  731:                 }
  732:                 $colcount++;
  733:             }
  734:         
  735: 	$keywordout.='</tr></table>';
  736: 
  737:         $scrout.=$keywordout;
  738: 
  739:         $scrout.=&textfield('Additional Keywords','addkey','');
  740: 
  741:         $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
  742: 
  743:         $scrout.=
  744:              '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
  745:               $metadatafields{'abstract'}.'</textarea>';
  746: 
  747: 	$source=~/\.(\w+)$/;
  748: 
  749: 	$scrout.=&hiddenfield('mime',$1);
  750: 
  751:         $scrout.=&selectbox('Language','language',
  752:                             $metadatafields{'language'},
  753: 			    \&Apache::loncommon::languagedescription,
  754: 			    (&Apache::loncommon::languageids),
  755: 			     );
  756: 
  757:         unless ($metadatafields{'creationdate'}) {
  758: 	    $metadatafields{'creationdate'}=time;
  759:         }
  760:         $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
  761: 
  762:         $scrout.=&hiddenfield('lastrevisiondate',time);
  763: 
  764: 			   
  765: 	$scrout.=&textfield('Publisher/Owner','owner',
  766:                             $metadatafields{'owner'});
  767: # --------------------------------------------------- Correct copyright for rat        
  768: 
  769:     if ($style eq 'rat') {
  770: 	if ($metadatafields{'copyright'} eq 'public') { 
  771: 	    delete $metadatafields{'copyright'};
  772: 	}
  773:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  774:                             $metadatafields{'copyright'},
  775: 			    \&Apache::loncommon::copyrightdescription,
  776: 		     (grep !/^public$/,(&Apache::loncommon::copyrightids)));
  777:     }
  778:     else {
  779:         $scrout.=&selectbox('Copyright/Distribution','copyright',
  780:                             $metadatafields{'copyright'},
  781: 			    \&Apache::loncommon::copyrightdescription,
  782: 			     (&Apache::loncommon::copyrightids));
  783:     }
  784: 
  785:     my $copyright_help = Apache::loncommon::help_open_topic("Publishing_Copyright");
  786:     $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
  787:     return $scrout.
  788:       '<p><input type="submit" value="Finalize Publication" /></p></form>';
  789: }
  790: 
  791: # -------------------------------------------------------- Publication Step Two
  792: 
  793: sub phasetwo {
  794: 
  795:     my ($source,$target,$style,$distarget)=@_;
  796:     my $logfile;
  797:     my $scrout='';
  798:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
  799: 	return 
  800:          '<font color=red>No write permission to user directory, FAIL</font>';
  801:     }
  802:     print $logfile 
  803: "\n================= Publish ".localtime()." Phase Two  ================\n";
  804: 
  805:      %metadatafields=();
  806:      %metadatakeys=();
  807: 
  808:      &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
  809: 
  810:      $metadatafields{'title'}=$ENV{'form.title'};
  811:      $metadatafields{'author'}=$ENV{'form.author'};
  812:      $metadatafields{'subject'}=$ENV{'form.subject'};
  813:      $metadatafields{'notes'}=$ENV{'form.notes'};
  814:      $metadatafields{'abstract'}=$ENV{'form.abstract'};
  815:      $metadatafields{'mime'}=$ENV{'form.mime'};
  816:      $metadatafields{'language'}=$ENV{'form.language'};
  817:      $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
  818:      $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
  819:      $metadatafields{'owner'}=$ENV{'form.owner'};
  820:      $metadatafields{'copyright'}=$ENV{'form.copyright'};
  821:      $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
  822: 
  823:      my $allkeywords=$ENV{'form.addkey'};
  824:      if (exists($ENV{'form.keywords'}) && (ref($ENV{'form.keywords'}))) {
  825:          my @Keywords = @{$ENV{'form.keywords'}};
  826:          foreach (@Keywords) {
  827:              $allkeywords.=','.$_;
  828:          }
  829:      }
  830:      $allkeywords=~s/\W+/\,/;
  831:      $allkeywords=~s/^\,//;
  832:      $metadatafields{'keywords'}=$allkeywords;
  833:  
  834:      {
  835:        print $logfile "\nWrite metadata file for ".$source;
  836:        my $mfh;
  837:        unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
  838: 	return 
  839:          '<font color=red>Could not write metadata, FAIL</font>';
  840:        }
  841:        foreach (sort keys %metadatafields) {
  842: 	 unless ($_=~/\./) {
  843:            my $unikey=$_;
  844:            $unikey=~/^([A-Za-z]+)/;
  845:            my $tag=$1;
  846:            $tag=~tr/A-Z/a-z/;
  847:            print $mfh "\n\<$tag";
  848:            foreach (split(/\,/,$metadatakeys{$unikey})) {
  849:                my $value=$metadatafields{$unikey.'.'.$_};
  850:                $value=~s/\"/\'\'/g;
  851:                print $mfh ' '.$_.'="'.$value.'"';
  852:            }
  853: 	   print $mfh '>'.
  854: 	     &HTML::Entities::encode($metadatafields{$unikey})
  855: 	       .'</'.$tag.'>';
  856:          }
  857:        }
  858:        $scrout.='<p>Wrote Metadata';
  859:        print $logfile "\nWrote metadata";
  860:      }
  861: 
  862: # -------------------------------- Synchronize entry with SQL metadata database
  863:   my $warning;
  864: 
  865:   unless ($metadatafields{'copyright'} eq 'priv') {
  866: 
  867:     my $dbh;
  868:     {
  869: 	unless (
  870: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",
  871:     $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
  872: 		) { 
  873: 	    $warning='<font color=red>WARNING: Cannot connect to '.
  874: 		'database!</font>';
  875: 	}
  876: 	else {
  877: 	    my %sqldatafields;
  878: 	    $sqldatafields{'url'}=$distarget;
  879: 	    my $sth=$dbh->prepare(
  880: 				  'delete from metadata where url like binary'.
  881: 				  '"'.$sqldatafields{'url'}.'"');
  882: 	    $sth->execute();
  883: 	    foreach ('title','author','subject','keywords','notes','abstract',
  884: 	     'mime','language','creationdate','lastrevisiondate','owner',
  885: 	     'copyright') {
  886: 		my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g; 
  887: 		$sqldatafields{$_}=$field;
  888: 	    }
  889: 	    
  890: 	    $sth=$dbh->prepare('insert into metadata values ('.
  891: 			       '"'.delete($sqldatafields{'title'}).'"'.','.
  892: 			       '"'.delete($sqldatafields{'author'}).'"'.','.
  893: 			       '"'.delete($sqldatafields{'subject'}).'"'.','.
  894: 			       '"'.delete($sqldatafields{'url'}).'"'.','.
  895: 			       '"'.delete($sqldatafields{'keywords'}).'"'.','.
  896: 			       '"'.'current'.'"'.','.
  897: 			       '"'.delete($sqldatafields{'notes'}).'"'.','.
  898: 			       '"'.delete($sqldatafields{'abstract'}).'"'.','.
  899: 			       '"'.delete($sqldatafields{'mime'}).'"'.','.
  900: 			       '"'.delete($sqldatafields{'language'}).'"'.','.
  901: 			       '"'.
  902: 			       sqltime(delete($sqldatafields{'creationdate'}))
  903: 			       .'"'.','.
  904: 			       '"'.
  905: 			       sqltime(delete(
  906: 			       $sqldatafields{'lastrevisiondate'})).'"'.','.
  907: 			       '"'.delete($sqldatafields{'owner'}).'"'.','.
  908: 			       '"'.delete(
  909: 			       $sqldatafields{'copyright'}).'"'.')');
  910: 	    $sth->execute();
  911: 	    $dbh->disconnect;
  912: 	    $scrout.='<p>Synchronized SQL metadata database';
  913: 	    print $logfile "\nSynchronized SQL metadata database";
  914: 	}
  915:     }
  916: 
  917: } else {
  918:     $scrout.='<p>Private Publication - did not synchronize database';
  919:     print $logfile "\nPrivate: Did not synchronize data into ".
  920: 	"SQL metadata database";
  921: }
  922: # ----------------------------------------------------------- Copy old versions
  923:    
  924: if (-e $target) {
  925:     my $filename;
  926:     my $maxversion=0;
  927:     $target=~/(.*)\/([^\/]+)\.(\w+)$/;
  928:     my $srcf=$2;
  929:     my $srct=$3;
  930:     my $srcd=$1;
  931:     unless ($srcd=~/^\/home\/httpd\/html\/res/) {
  932: 	print $logfile "\nPANIC: Target dir is ".$srcd;
  933:         return "<font color=red>Invalid target directory, FAIL</font>";
  934:     }
  935:     opendir(DIR,$srcd);
  936:     while ($filename=readdir(DIR)) {
  937:        if ($filename=~/$srcf\.(\d+)\.$srct$/) {
  938: 	   $maxversion=($1>$maxversion)?$1:$maxversion;
  939:        }
  940:     }
  941:     closedir(DIR);
  942:     $maxversion++;
  943:     $scrout.='<p>Creating old version '.$maxversion;
  944:     print $logfile "\nCreating old version ".$maxversion;
  945: 
  946:     my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
  947: 
  948:         if (copy($target,$copyfile)) {
  949: 	    print $logfile "Copied old target to ".$copyfile."\n";
  950:             $scrout.='<p>Copied old target file';
  951:         } else {
  952: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  953:            return "<font color=red>Failed to copy old target, $!, FAIL</font>";
  954:         }
  955: 
  956: # --------------------------------------------------------------- Copy Metadata
  957: 
  958: 	$copyfile=$copyfile.'.meta';
  959: 
  960:         if (copy($target.'.meta',$copyfile)) {
  961: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
  962:             $scrout.='<p>Copied old metadata';
  963:         } else {
  964: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
  965:             if (-e $target.'.meta') {
  966:                return 
  967:        "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
  968: 	    }
  969:         }
  970: 
  971: 
  972: } else {
  973:     $scrout.='<p>Initial version';
  974:     print $logfile "\nInitial version";
  975: }
  976: 
  977: # ---------------------------------------------------------------- Write Source
  978: 	my $copyfile=$target;
  979: 
  980:            my @parts=split(/\//,$copyfile);
  981:            my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
  982: 
  983:            my $count;
  984:            for ($count=5;$count<$#parts;$count++) {
  985:                $path.="/$parts[$count]";
  986:                if ((-e $path)!=1) {
  987:                    print $logfile "\nCreating directory ".$path;
  988:                    $scrout.='<p>Created directory '.$parts[$count];
  989: 		   mkdir($path,0777);
  990:                }
  991:            }
  992: 
  993:         if (copy($source,$copyfile)) {
  994: 	    print $logfile "Copied original source to ".$copyfile."\n";
  995:             $scrout.='<p>Copied source file';
  996:         } else {
  997: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
  998:             return "<font color=red>Failed to copy source, $!, FAIL</font>";
  999:         }
 1000: 
 1001: # --------------------------------------------------------------- Copy Metadata
 1002: 
 1003:         $copyfile=$copyfile.'.meta';
 1004: 
 1005:         if (copy($source.'.meta',$copyfile)) {
 1006: 	    print $logfile "Copied original metadata to ".$copyfile."\n";
 1007:             $scrout.='<p>Copied metadata';
 1008:         } else {
 1009: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 1010:             return 
 1011:           "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
 1012:         }
 1013: 
 1014: # --------------------------------------------------- Send update notifications
 1015: 
 1016:     my @subscribed=&get_subscribed_hosts($target);
 1017:     foreach my $subhost (@subscribed) {
 1018: 	$scrout.='<p>Notifying host '.$subhost.':';
 1019: 	print $logfile "\nNotifying host ".$subhost.':';
 1020: 	my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 1021: 	$scrout.=$reply;
 1022: 	print $logfile $reply;
 1023:     }
 1024: 
 1025: # ---------------------------------------- Send update notifications, meta only
 1026: 
 1027:     my @subscribedmeta=&get_subscribed_hosts("$target.meta");
 1028:     foreach my $subhost (@subscribedmeta) {
 1029: 	$scrout.='<p>Notifying host for metadata only '.$subhost.':';
 1030: 	print $logfile "\nNotifying host for metadata only ".$subhost.':';
 1031: 	my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 1032: 					    $subhost);
 1033: 	$scrout.=$reply;
 1034: 	print $logfile $reply;
 1035:     }
 1036: 
 1037: # ------------------------------------------------ Provide link to new resource
 1038: 
 1039:     my $thisdistarget=$target;
 1040:     $thisdistarget=~s/^$docroot//;
 1041: 
 1042:     my $thissrc=$source;
 1043:     $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
 1044: 
 1045:     my $thissrcdir=$thissrc;
 1046:     $thissrcdir=~s/\/[^\/]+$/\//;
 1047: 
 1048: 
 1049:     return $warning.$scrout.
 1050:       '<hr><a href="'.$thisdistarget.'"><font size=+2>View Published Version</font></a>'.
 1051:       '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
 1052:       '<p><a href="'.$thissrcdir.
 1053:       '"><font size=+2>Back to Source Directory</font></a>';
 1054: 
 1055: }
 1056: 
 1057: # ================================================================ Main Handler
 1058: 
 1059: sub handler {
 1060:   my $r=shift;
 1061: 
 1062:   if ($r->header_only) {
 1063:      $r->content_type('text/html');
 1064:      $r->send_http_header;
 1065:      return OK;
 1066:   }
 1067: 
 1068: # Get query string for limited number of parameters
 1069: 
 1070:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1071:                                             ['filename']);
 1072: 
 1073: # -------------------------------------------------------------- Check filename
 1074: 
 1075:   my $fn=$ENV{'form.filename'};
 1076: 
 1077:   
 1078:   unless ($fn) { 
 1079:      $r->log_reason($cuname.' at '.$cudom.
 1080:          ' trying to publish empty filename', $r->filename); 
 1081:      return HTTP_NOT_FOUND;
 1082:   } 
 1083: 
 1084:   ($cuname,$cudom)=
 1085:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
 1086:   unless (($cuname) && ($cudom)) {
 1087:      $r->log_reason($cuname.' at '.$cudom.
 1088:          ' trying to publish file '.$ENV{'form.filename'}.
 1089:          ' ('.$fn.') - not authorized', 
 1090:          $r->filename); 
 1091:      return HTTP_NOT_ACCEPTABLE;
 1092:   }
 1093: 
 1094:   unless (&Apache::lonnet::homeserver($cuname,$cudom) 
 1095:           eq $r->dir_config('lonHostID')) {
 1096:      $r->log_reason($cuname.' at '.$cudom.
 1097:          ' trying to publish file '.$ENV{'form.filename'}.
 1098:          ' ('.$fn.') - not homeserver ('.
 1099:          &Apache::lonnet::homeserver($cuname,$cudom).')', 
 1100:          $r->filename); 
 1101:      return HTTP_NOT_ACCEPTABLE;
 1102:   }
 1103: 
 1104:   $fn=~s/^http\:\/\/[^\/]+//;
 1105:   $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
 1106: 
 1107:   my $targetdir='';
 1108:   $docroot=$r->dir_config('lonDocRoot'); 
 1109:   if ($1 ne $cuname) {
 1110:      $r->log_reason($cuname.' at '.$cudom.
 1111:          ' trying to publish unowned file '.$ENV{'form.filename'}.
 1112:          ' ('.$fn.')', 
 1113:          $r->filename); 
 1114:      return HTTP_NOT_ACCEPTABLE;
 1115:   } else {
 1116:       $targetdir=$docroot.'/res/'.$cudom;
 1117:   }
 1118:                                  
 1119:   
 1120:   unless (-e $fn) { 
 1121:      $r->log_reason($cuname.' at '.$cudom.
 1122:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
 1123:          ' ('.$fn.')', 
 1124:          $r->filename); 
 1125:      return HTTP_NOT_FOUND;
 1126:   } 
 1127: 
 1128: unless ($ENV{'form.phase'} eq 'two') {
 1129: 
 1130: # --------------------------------- File is there and owned, init lookup tables
 1131: 
 1132:   %addid=();
 1133: 
 1134:   {
 1135:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
 1136:       while (<$fh>=~/(\w+)\s+(\w+)/) {
 1137:           $addid{$1}=$2;
 1138:       }
 1139:   }
 1140: 
 1141:   %nokey=();
 1142: 
 1143:   {
 1144:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
 1145:       while (<$fh>) {
 1146:           my $word=$_;
 1147:           chomp($word);
 1148:           $nokey{$word}=1;
 1149:       }
 1150:   }
 1151: 
 1152: }
 1153: 
 1154: # ----------------------------------------------------------- Start page output
 1155: 
 1156:   $r->content_type('text/html');
 1157:   $r->send_http_header;
 1158: 
 1159:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
 1160:   $r->print(
 1161:    '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
 1162:   my $thisfn=$fn;
 1163:    
 1164: # ------------------------------------------------------------- Individual file
 1165:   {
 1166:       $thisfn=~/\.(\w+)$/;
 1167:       my $thistype=$1;
 1168:       my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
 1169: 
 1170:       my $thistarget=$thisfn;
 1171:       
 1172:       $thistarget=~s/^\/home/$targetdir/;
 1173:       $thistarget=~s/\/public\_html//;
 1174: 
 1175:       my $thisdistarget=$thistarget;
 1176:       $thisdistarget=~s/^$docroot//;
 1177: 
 1178:       my $thisdisfn=$thisfn;
 1179:       $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
 1180: 
 1181:       $r->print('<h2>Publishing '.
 1182:         &Apache::loncommon::filedescription($thistype).' <tt>'.
 1183:         $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
 1184:    
 1185:        if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
 1186:           $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
 1187:                '</font></h3>');
 1188:       }
 1189: 
 1190:       if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
 1191:           $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
 1192:                     $thisdisfn.
 1193:   	  '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
 1194:       }
 1195:   
 1196: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
 1197: 
 1198:        unless ($ENV{'form.phase'} eq 'two') {
 1199:          $r->print(
 1200:           '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
 1201:        } else {
 1202:          $r->print(
 1203:           '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget)); 
 1204:        }  
 1205: 
 1206:   }
 1207:   $r->print('</body></html>');
 1208: 
 1209:   return OK;
 1210: }
 1211: 
 1212: 1;
 1213: __END__
 1214: 
 1215: =head1 NAME
 1216: 
 1217: Apache::lonpublisher - Publication Handler
 1218: 
 1219: =head1 SYNOPSIS
 1220: 
 1221: Invoked by /etc/httpd/conf/srm.conf:
 1222: 
 1223:  <Location /adm/publish>
 1224:  PerlAccessHandler       Apache::lonacc
 1225:  SetHandler perl-script
 1226:  PerlHandler Apache::lonpublisher
 1227:  ErrorDocument     403 /adm/login
 1228:  ErrorDocument     404 /adm/notfound.html
 1229:  ErrorDocument     406 /adm/unauthorized.html
 1230:  ErrorDocument	  500 /adm/errorhandler
 1231:  </Location>
 1232: 
 1233: =head1 INTRODUCTION
 1234: 
 1235: This module publishes a file.  This involves gathering metadata,
 1236: versioning the file, copying file from construction space to
 1237: publication space, and copying metadata from construction space
 1238: to publication space.
 1239: 
 1240: This is part of the LearningOnline Network with CAPA project
 1241: described at http://www.lon-capa.org.
 1242: 
 1243: =head1 HANDLER SUBROUTINE
 1244: 
 1245: This routine is called by Apache and mod_perl.
 1246: 
 1247: =over 4
 1248: 
 1249: =item *
 1250: 
 1251: Get query string for limited number of parameters
 1252: 
 1253: =item *
 1254: 
 1255: Check filename
 1256: 
 1257: =item *
 1258: 
 1259: File is there and owned, init lookup tables
 1260: 
 1261: =item *
 1262: 
 1263: Start page output
 1264: 
 1265: =item *
 1266: 
 1267: Individual file
 1268: 
 1269: =item *
 1270: 
 1271: publish from $thisfn to $thistarget with $thisembstyle
 1272: 
 1273: =back
 1274: 
 1275: =head1 OTHER SUBROUTINES
 1276: 
 1277: =over 4
 1278: 
 1279: =item *
 1280: 
 1281: metaeval() : Evaluate string with metadata
 1282: 
 1283: =item *
 1284: 
 1285: metaread() : Read a metadata file
 1286: 
 1287: =item *
 1288: 
 1289: sqltime() : convert 'time' format into a datetime sql format
 1290: 
 1291: =item *
 1292: 
 1293: textfield() : form field
 1294: 
 1295: =item *
 1296: 
 1297: hiddenfield() : form field
 1298: 
 1299: =item *
 1300: 
 1301: selectbox() : form field
 1302: 
 1303: =item *
 1304: 
 1305: urlfixup() : fixup URL (Publication Step One)
 1306: 
 1307: =item *
 1308: 
 1309: publish() : publish (Publication Step One)
 1310: 
 1311: =item *
 1312: 
 1313: phasetwo() : render second interface showing status of publication steps
 1314: (Publication Step Two)
 1315: 
 1316: =back
 1317: 
 1318: =cut

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