Annotation of loncom/publisher/lonpublisher.pm, revision 1.81

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

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