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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Publication Handler
1.54      albertel    3: #
1.120   ! albertel    4: # $Id: lonpublisher.pm,v 1.119 2003/03/17 16:58:09 www 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.27      www        36: # 05/03,05/05,05/07 Gerd Kortemeyer
1.51      www        37: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
1.58      www        38: # 12/04,12/05 Guy Albertelli
                     39: # 12/05 Gerd Kortemeyer
1.62      www        40: # 12/05 Guy Albertelli
1.64      www        41: # 12/06,12/07 Gerd Kortemeyer
1.67      www        42: # 12/25 Gerd Kortemeyer
1.71      www        43: # YEAR=2002
                     44: # 1/17 Gerd Kortemeyer
1.65      harris41   45: #
                     46: ###
                     47: 
                     48: ###############################################################################
                     49: ##                                                                           ##
                     50: ## ORGANIZATION OF THIS PERL MODULE                                          ##
                     51: ##                                                                           ##
                     52: ## 1. Modules used by this module                                            ##
                     53: ## 2. Various subroutines                                                    ##
                     54: ## 3. Publication Step One                                                   ##
                     55: ## 4. Phase Two                                                              ##
                     56: ## 5. Main Handler                                                           ##
                     57: ##                                                                           ##
                     58: ###############################################################################
1.1       www        59: 
1.90      matthew    60: 
                     61: ######################################################################
                     62: ######################################################################
                     63: 
                     64: =pod 
                     65: 
1.94      harris41   66: =head1 NAME
1.90      matthew    67: 
                     68: lonpublisher - LON-CAPA publishing handler
                     69: 
1.94      harris41   70: =head1 SYNOPSIS
1.90      matthew    71: 
1.94      harris41   72: B<lonpublisher> is used by B<mod_perl> inside B<Apache>.  This is the
                     73: invocation by F<loncapa_apache.conf>:
                     74: 
                     75:   <Location /adm/publish>
                     76:   PerlAccessHandler       Apache::lonacc
                     77:   SetHandler perl-script
                     78:   PerlHandler Apache::lonpublisher
                     79:   ErrorDocument     403 /adm/login
                     80:   ErrorDocument     404 /adm/notfound.html
                     81:   ErrorDocument     406 /adm/unauthorized.html
                     82:   ErrorDocument     500 /adm/errorhandler
                     83:   </Location>
                     84: 
                     85: =head1 DESCRIPTION
                     86: 
                     87: B<lonpublisher> takes the proper steps to add resources to the LON-CAPA
1.90      matthew    88: digital library.  This includes updating the metadata table in the
                     89: LON-CAPA database.
                     90: 
1.94      harris41   91: B<lonpublisher> is many things to many people.  
1.90      matthew    92: 
                     93: This module publishes a file.  This involves gathering metadata,
                     94: versioning the file, copying file from construction space to
                     95: publication space, and copying metadata from construction space
                     96: to publication space.
                     97: 
1.94      harris41   98: =head2 SUBROUTINES
                     99: 
                    100: Many of the undocumented subroutines implement various magical
                    101: parsing shortcuts.
1.90      matthew   102: 
                    103: =over 4
                    104: 
                    105: =cut
                    106: 
                    107: ######################################################################
                    108: ######################################################################
                    109: 
                    110: 
1.1       www       111: package Apache::lonpublisher;
                    112: 
1.65      harris41  113: # ------------------------------------------------- modules used by this module
1.1       www       114: use strict;
                    115: use Apache::File;
1.13      www       116: use File::Copy;
1.2       www       117: use Apache::Constants qw(:common :http :methods);
1.76      albertel  118: use HTML::LCParser;
1.4       www       119: use Apache::lonxml;
1.27      www       120: use Apache::loncacc;
1.24      harris41  121: use DBI;
1.65      harris41  122: use Apache::lonnet();
                    123: use Apache::loncommon();
1.89      matthew   124: use Apache::lonmysql;
1.105     www       125: use vars qw(%metadatafields %metadatakeys);
1.2       www       126: 
1.3       www       127: my %addid;
1.5       www       128: my %nokey;
1.10      www       129: 
1.12      www       130: my $docroot;
                    131: 
1.27      www       132: my $cuname;
                    133: my $cudom;
                    134: 
1.90      matthew   135: =pod
                    136: 
1.94      harris41  137: =item B<metaeval>
                    138: 
                    139: Evaluates a string that contains metadata.  This subroutine
                    140: stores values inside I<%metadatafields> and I<%metadatakeys>.
                    141: The hash key is a I<$unikey> corresponding to a unique id
                    142: that is descriptive of the parser location inside the XML tree.
                    143: 
                    144: Parameters:
                    145: 
                    146: =over 4
1.90      matthew   147: 
1.94      harris41  148: =item I<$metastring>
                    149: 
                    150: A string that contains metadata.
                    151: 
                    152: =back
                    153: 
                    154: Returns:
                    155: 
                    156: nothing
1.90      matthew   157: 
                    158: =cut
                    159: 
                    160: #########################################
                    161: #########################################
1.7       www       162: sub metaeval {
                    163:     my $metastring=shift;
                    164:    
1.76      albertel  165:         my $parser=HTML::LCParser->new(\$metastring);
1.7       www       166:         my $token;
                    167:         while ($token=$parser->get_token) {
                    168:            if ($token->[0] eq 'S') {
                    169: 	      my $entry=$token->[1];
                    170:               my $unikey=$entry;
1.32      www       171:               if (defined($token->[2]->{'package'})) { 
                    172:                   $unikey.='_package_'.$token->[2]->{'package'};
                    173:               } 
1.7       www       174:               if (defined($token->[2]->{'part'})) { 
                    175:                  $unikey.='_'.$token->[2]->{'part'}; 
                    176: 	      }
1.32      www       177:               if (defined($token->[2]->{'id'})) { 
1.49      www       178:                   $unikey.='_'.$token->[2]->{'id'};
1.32      www       179:               } 
1.7       www       180:               if (defined($token->[2]->{'name'})) { 
                    181:                  $unikey.='_'.$token->[2]->{'name'}; 
                    182: 	      }
1.65      harris41  183:               foreach (@{$token->[3]}) {
1.7       www       184: 		  $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
                    185:                   if ($metadatakeys{$unikey}) {
                    186: 		      $metadatakeys{$unikey}.=','.$_;
                    187:                   } else {
                    188:                       $metadatakeys{$unikey}=$_;
                    189:                   }
1.65      harris41  190:               }
1.7       www       191:               if ($metadatafields{$unikey}) {
1.8       www       192: 		  my $newentry=$parser->get_text('/'.$entry);
1.41      www       193:                   unless (($metadatafields{$unikey}=~/$newentry/) ||
                    194:                           ($newentry eq '')) {
1.8       www       195:                      $metadatafields{$unikey}.=', '.$newentry;
                    196: 		  }
1.7       www       197: 	      } else {
                    198:                  $metadatafields{$unikey}=$parser->get_text('/'.$entry);
                    199:               }
                    200:           }
                    201:        }
                    202: }
                    203: 
1.90      matthew   204: #########################################
                    205: #########################################
                    206: 
                    207: =pod
                    208: 
1.94      harris41  209: =item B<metaread>
1.90      matthew   210: 
                    211: Read a metadata file
                    212: 
1.94      harris41  213: Parameters:
                    214: 
                    215: =over
                    216: 
                    217: =item I<$logfile>
                    218: 
                    219: File output stream to output errors and warnings to.
                    220: 
                    221: =item I<$fn>
                    222: 
                    223: File name (including path).
                    224: 
                    225: =back
                    226: 
                    227: Returns:
                    228: 
                    229: =over 4
                    230: 
                    231: =item Scalar string (if successful)
                    232: 
                    233: XHTML text that indicates successful reading of the metadata.
                    234: 
                    235: =back
                    236: 
1.90      matthew   237: =cut
                    238: 
                    239: #########################################
                    240: #########################################
1.7       www       241: sub metaread {
                    242:     my ($logfile,$fn)=@_;
                    243:     unless (-e $fn) {
1.94      harris41  244: 	print($logfile 'No file '.$fn."\n");
1.120   ! albertel  245:         return '<br /><b>No file:</b> <tt>'.$fn.'</tt>';
1.7       www       246:     }
1.94      harris41  247:     print($logfile 'Processing '.$fn."\n");
1.7       www       248:     my $metastring;
                    249:     {
                    250:      my $metafh=Apache::File->new($fn);
                    251:      $metastring=join('',<$metafh>);
                    252:     }
                    253:     &metaeval($metastring);
1.120   ! albertel  254:     return '<br /><b>Processed file:</b> <tt>'.$fn.'</tt>';
1.7       www       255: }
1.12      www       256: 
1.90      matthew   257: #########################################
                    258: #########################################
                    259: 
1.101     www       260: sub coursedependencies {
                    261:     my $url=&Apache::lonnet::declutter(shift);
                    262:     $url=~s/\.meta$//;
                    263:     my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
                    264:     my $regexp=$url;
                    265:     $regexp=~s/(\W)/\\$1/g;
                    266:     $regexp='___'.$regexp.'___course';
                    267:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
                    268: 				       $aauthor,$regexp);
                    269:     my %courses=();
                    270:     foreach (keys %evaldata) {
                    271: 	if ($_=~/^([a-zA-Z0-9]+_[a-zA-Z0-9]+)___.+___course$/) {
                    272: 	    $courses{$1}=1;
                    273:         }
                    274:     }
                    275:     return %courses;
                    276: }
                    277: #########################################
                    278: #########################################
                    279: 
                    280: 
1.90      matthew   281: =pod
                    282: 
1.94      harris41  283: =item Form-field-generating subroutines.
                    284: 
                    285: For input parameters, these subroutines take in values
                    286: such as I<$name>, I<$value> and other form field metadata.
                    287: The output (scalar string that is returned) is an XHTML
                    288: string which presents the form field (foreseeably inside
                    289: <form></form> tags).
1.90      matthew   290: 
                    291: =over 4
                    292: 
1.94      harris41  293: =item B<textfield>
1.90      matthew   294: 
1.94      harris41  295: =item B<hiddenfield>
1.90      matthew   296: 
1.94      harris41  297: =item B<selectbox>
1.90      matthew   298: 
                    299: =back
                    300: 
                    301: =cut
                    302: 
                    303: #########################################
                    304: #########################################
1.8       www       305: sub textfield {
1.10      www       306:     my ($title,$name,$value)=@_;
1.120   ! albertel  307:     return "\n<p><b>$title:</b></p><br />".
1.94      harris41  308:            '<input type="text" name="'.$name.'" size=80 value="'.$value.'" />';
1.11      www       309: }
                    310: 
                    311: sub hiddenfield {
                    312:     my ($name,$value)=@_;
1.94      harris41  313:     return "\n".'<input type="hidden" name="'.$name.'" value="'.$value.'" />';
1.8       www       314: }
                    315: 
1.9       www       316: sub selectbox {
1.65      harris41  317:     my ($title,$name,$value,$functionref,@idlist)=@_;
                    318:     my $uctitle=uc($title);
                    319:     my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
1.120   ! albertel  320: 	"</b></font></p><br />".'<select name="'.$name.'">';
1.65      harris41  321:     foreach (@idlist) {
                    322:         $selout.='<option value=\''.$_.'\'';
                    323:         if ($_ eq $value) {
                    324: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
                    325: 	}
                    326:         else {$selout.='>'.&{$functionref}($_).'</option>';}
                    327:     }
1.10      www       328:     return $selout.'</select>';
1.9       www       329: }
                    330: 
1.90      matthew   331: #########################################
                    332: #########################################
                    333: 
                    334: =pod
                    335: 
1.94      harris41  336: =item B<urlfixup>
1.90      matthew   337: 
                    338: Fix up a url?  First step of publication
1.12      www       339: 
1.90      matthew   340: =cut
                    341: 
                    342: #########################################
                    343: #########################################
1.34      www       344: sub urlfixup {
1.35      www       345:     my ($url,$target)=@_;
1.39      www       346:     unless ($url) { return ''; }
1.68      albertel  347:     #javascript code needs no fixing
                    348:     if ($url =~ /^javascript:/i) { return $url; }
1.69      albertel  349:     if ($url =~ /^mailto:/i) { return $url; }
1.68      albertel  350:     #internal document links need no fixing
                    351:     if ($url =~ /^\#/) { return $url; } 
1.35      www       352:     my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
1.65      harris41  353:     foreach (values %Apache::lonnet::hostname) {
1.35      www       354: 	if ($_ eq $host) {
                    355: 	    $url=~s/^http\:\/\///;
                    356:             $url=~s/^$host//;
                    357:         }
1.65      harris41  358:     }
1.40      www       359:     if ($url=~/^http\:\/\//) { return $url; }
1.35      www       360:     $url=~s/\~$cuname/res\/$cudom\/$cuname/;
1.71      www       361:     return $url;
                    362: }
                    363: 
1.90      matthew   364: #########################################
                    365: #########################################
                    366: 
                    367: =pod
                    368: 
1.94      harris41  369: =item B<absoluteurl>
1.90      matthew   370: 
1.94      harris41  371: Currently undocumented.
1.90      matthew   372: 
                    373: =cut
1.71      www       374: 
1.90      matthew   375: #########################################
                    376: #########################################
1.71      www       377: sub absoluteurl {
                    378:     my ($url,$target)=@_;
                    379:     unless ($url) { return ''; }
1.35      www       380:     if ($target) {
                    381: 	$target=~s/\/[^\/]+$//;
                    382:        $url=&Apache::lonnet::hreflocation($target,$url);
                    383:     }
                    384:     return $url;
1.34      www       385: }
                    386: 
1.90      matthew   387: #########################################
                    388: #########################################
                    389: 
                    390: =pod
                    391: 
1.94      harris41  392: =item B<set_allow>
1.90      matthew   393: 
                    394: Currently undocumented    
                    395: 
                    396: =cut
                    397: 
                    398: #########################################
                    399: #########################################
1.81      albertel  400: sub set_allow {
                    401:     my ($allow,$logfile,$target,$tag,$oldurl)=@_;
                    402:     my $newurl=&urlfixup($oldurl,$target);
                    403:     my $return_url=$oldurl;
                    404:     print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
                    405:     if ($newurl ne $oldurl) {
                    406: 	$return_url=$newurl;
                    407: 	print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
                    408:     }
                    409:     if (($newurl !~ /^javascript:/i) &&
                    410: 	($newurl !~ /^mailto:/i) &&
                    411: 	($newurl !~ /^http:/i) &&
                    412: 	($newurl !~ /^\#/)) {
                    413: 	$$allow{&absoluteurl($newurl,$target)}=1;
                    414:     }
                    415:     return $return_url
                    416: }
                    417: 
1.90      matthew   418: #########################################
                    419: #########################################
                    420: 
                    421: =pod
                    422: 
1.94      harris41  423: =item B<get_subscribed_hosts>
1.90      matthew   424: 
                    425: Currently undocumented    
                    426: 
                    427: =cut
                    428: 
                    429: #########################################
                    430: #########################################
1.85      albertel  431: sub get_subscribed_hosts {
                    432:     my ($target)=@_;
                    433:     my @subscribed;
                    434:     my $filename;
                    435:     $target=~/(.*)\/([^\/]+)$/;
                    436:     my $srcf=$2;
                    437:     opendir(DIR,$1);
                    438:     while ($filename=readdir(DIR)) {
1.118     albertel  439: 	if ($filename=~/\Q$srcf\E\.(\w+)$/) {
1.85      albertel  440: 	    my $subhost=$1;
1.98      www       441: 	    if (($subhost ne 'meta' && $subhost ne 'subscription') &&
                    442:                 ($subhost ne $Apache::lonnet::perlvar{'lonHostID'})) {
1.85      albertel  443: 		push(@subscribed,$subhost);
                    444: 	    }
                    445: 	}
                    446:     }
                    447:     closedir(DIR);
                    448:     my $sh;
                    449:     if ( $sh=Apache::File->new("$target.subscription") ) {
                    450: 	&Apache::lonnet::logthis("opened $target.subscription");
                    451: 	while (my $subline=<$sh>) {
                    452: 	    &Apache::lonnet::logthis("Trying $subline");
1.98      www       453: 	    if ($subline =~ /(^\w+):/) { 
                    454:                 if ($1 ne $Apache::lonnet::perlvar{'lonHostID'}) { 
                    455:                    push(@subscribed,$1);
                    456: 	        }
                    457:             } else {
1.85      albertel  458: 		&Apache::lonnet::logthis("No Match for $subline");
                    459: 	    }
                    460: 	}
                    461:     } else {
1.94      harris41  462: 	&Apache::lonnet::logthis("Unable to open $target.subscription");
1.85      albertel  463:     }
                    464:     return @subscribed;
                    465: }
                    466: 
1.86      albertel  467: 
1.90      matthew   468: #########################################
                    469: #########################################
                    470: 
                    471: =pod
                    472: 
1.94      harris41  473: =item B<get_max_ids_indices>
1.90      matthew   474: 
                    475: Currently undocumented    
                    476: 
                    477: =cut
                    478: 
                    479: #########################################
                    480: #########################################
1.86      albertel  481: sub get_max_ids_indices {
                    482:     my ($content)=@_;
                    483:     my $maxindex=10;
                    484:     my $maxid=10;
                    485:     my $needsfixup=0;
1.106     albertel  486:     my $duplicateids=0;
                    487: 
                    488:     my %allids;
                    489:     my %duplicatedids;
1.86      albertel  490: 
                    491:     my $parser=HTML::LCParser->new($content);
                    492:     my $token;
                    493:     while ($token=$parser->get_token) {
                    494: 	if ($token->[0] eq 'S') {
                    495: 	    my $counter;
                    496: 	    if ($counter=$addid{$token->[1]}) {
                    497: 		if ($counter eq 'id') {
                    498: 		    if (defined($token->[2]->{'id'})) {
                    499: 			$maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
1.106     albertel  500: 			if (exists($allids{$token->[2]->{'id'}})) {
                    501: 			    $duplicateids=1;
                    502: 			    $duplicatedids{$token->[2]->{'id'}}=1;
                    503: 			} else {
                    504: 			    $allids{$token->[2]->{'id'}}=1;
                    505: 			}
1.86      albertel  506: 		    } else {
                    507: 			$needsfixup=1;
                    508: 		    }
                    509: 		} else {
                    510: 		    if (defined($token->[2]->{'index'})) {
                    511: 			$maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
                    512: 		    } else {
                    513: 			$needsfixup=1;
                    514: 		    }
                    515: 		}
                    516: 	    }
                    517: 	}
                    518:     }
1.106     albertel  519:     return ($needsfixup,$maxid,$maxindex,$duplicateids,
                    520: 	    (keys(%duplicatedids)));
1.86      albertel  521: }
                    522: 
1.90      matthew   523: #########################################
                    524: #########################################
                    525: 
                    526: =pod
                    527: 
1.94      harris41  528: =item B<get_all_text_unbalanced>
1.90      matthew   529: 
                    530: Currently undocumented    
                    531: 
                    532: =cut
                    533: 
                    534: #########################################
                    535: #########################################
1.87      albertel  536: sub get_all_text_unbalanced {
                    537:     #there is a copy of this in lonxml.pm
                    538:     my($tag,$pars)= @_;
                    539:     my $token;
                    540:     my $result='';
                    541:     $tag='<'.$tag.'>';
                    542:     while ($token = $$pars[-1]->get_token) {
                    543: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    544: 	    $result.=$token->[1];
                    545: 	} elsif ($token->[0] eq 'PI') {
                    546: 	    $result.=$token->[2];
                    547: 	} elsif ($token->[0] eq 'S') {
                    548: 	    $result.=$token->[4];
                    549: 	} elsif ($token->[0] eq 'E')  {
                    550: 	    $result.=$token->[2];
                    551: 	}
1.107     albertel  552: 	if ($result =~ /(.*)\Q$tag\E(.*)/s) {
1.88      albertel  553: 	    #&Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
                    554: 	    #&Apache::lonnet::logthis('Result is :'.$1);
1.87      albertel  555: 	    $result=$1;
                    556: 	    my $redo=$tag.$2;
                    557: 	    push (@$pars,HTML::LCParser->new(\$redo));
                    558: 	    $$pars[-1]->xml_mode('1');
                    559: 	    last;
                    560: 	}
                    561:     }
                    562:     return $result
                    563: }
                    564: 
1.90      matthew   565: #########################################
                    566: #########################################
                    567: 
                    568: =pod
                    569: 
1.94      harris41  570: =item B<fix_ids_and_indices>
1.90      matthew   571: 
                    572: Currently undocumented    
                    573: 
                    574: =cut
                    575: 
                    576: #########################################
                    577: #########################################
1.87      albertel  578: #Arguably this should all be done as a lonnet::ssi instead
1.86      albertel  579: sub fix_ids_and_indices {
                    580:     my ($logfile,$source,$target)=@_;
                    581: 
                    582:     my %allow;
                    583:     my $content;
                    584:     {
                    585: 	my $org=Apache::File->new($source);
                    586: 	$content=join('',<$org>);
                    587:     }
                    588: 
1.106     albertel  589:     my ($needsfixup,$maxid,$maxindex,$duplicateids,@duplicatedids)=
                    590: 	&get_max_ids_indices(\$content);
1.86      albertel  591: 
1.106     albertel  592:     print $logfile ("Got $needsfixup,$maxid,$maxindex,$duplicateids--".
                    593: 			   join(', ',@duplicatedids));
                    594:     if ($duplicateids) {
                    595: 	print $logfile "Duplicate ID(s) exist, ".join(', ',@duplicatedids)."\n";
                    596: 	my $outstring='<font color="red">Unable to publish file, it contains duplicated ID(s), ID(s) need to be unique. The duplicated ID(s) are: '.join(', ',@duplicatedids).'</font>';
                    597: 	return ($outstring,1);
                    598:     }
1.86      albertel  599:     if ($needsfixup) {
                    600: 	print $logfile "Needs ID and/or index fixup\n".
                    601: 	    "Max ID   : $maxid (min 10)\n".
                    602:                 "Max Index: $maxindex (min 10)\n";
                    603:     }
                    604:     my $outstring='';
                    605:     my @parser;
                    606:     $parser[0]=HTML::LCParser->new(\$content);
                    607:     $parser[-1]->xml_mode(1);
                    608:     my $token;
                    609:     while (@parser) {
                    610: 	while ($token=$parser[-1]->get_token) {
                    611: 	    if ($token->[0] eq 'S') {
                    612: 		my $counter;
                    613: 		my $tag=$token->[1];
                    614: 		my $lctag=lc($tag);
                    615: 		if ($lctag eq 'allow') {
                    616: 		    $allow{$token->[2]->{'src'}}=1;
                    617: 		    next;
                    618: 		}
                    619: 		my %parms=%{$token->[2]};
                    620: 		$counter=$addid{$tag};
                    621: 		if (!$counter) { $counter=$addid{$lctag}; }
                    622: 		if ($counter) {
                    623: 		    if ($counter eq 'id') {
                    624: 			unless (defined($parms{'id'})) {
                    625: 			    $maxid++;
                    626: 			    $parms{'id'}=$maxid;
                    627: 			    print $logfile 'ID: '.$tag.':'.$maxid."\n";
                    628: 			}
                    629: 		    } elsif ($counter eq 'index') {
                    630: 			unless (defined($parms{'index'})) {
                    631: 			    $maxindex++;
                    632: 			    $parms{'index'}=$maxindex;
                    633: 			    print $logfile 'Index: '.$tag.':'.$maxindex."\n";
                    634: 			}
                    635: 		    }
                    636: 		}
                    637: 		foreach my $type ('src','href','background','bgimg') {
                    638: 		    foreach my $key (keys(%parms)) {
                    639: 			if ($key =~ /^$type$/i) {
                    640: 			    $parms{$key}=&set_allow(\%allow,$logfile,
                    641: 						    $target,$tag,
                    642: 						    $parms{$key});
                    643: 			}
                    644: 		    }
                    645: 		}
                    646: 		# probably a <randomlabel> image type <label>
                    647: 		if ($lctag eq 'label' && defined($parms{'description'})) {
                    648: 		    my $next_token=$parser[-1]->get_token();
                    649: 		    if ($next_token->[0] eq 'T') {
                    650: 			$next_token->[1]=&set_allow(\%allow,$logfile,
                    651: 						    $target,$tag,
                    652: 						    $next_token->[1]);
                    653: 		    }
                    654: 		    $parser[-1]->unget_token($next_token);
                    655: 		}
                    656: 		if ($lctag eq 'applet') {
                    657: 		    my $codebase='';
                    658: 		    if (defined($parms{'codebase'})) {
                    659: 			my $oldcodebase=$parms{'codebase'};
                    660: 			unless ($oldcodebase=~/\/$/) {
                    661: 			    $oldcodebase.='/';
                    662: 			}
                    663: 			$codebase=&urlfixup($oldcodebase,$target);
                    664: 			$codebase=~s/\/$//;    
                    665: 			if ($codebase ne $oldcodebase) {
                    666: 			    $parms{'codebase'}=$codebase;
                    667: 			    print $logfile 'URL codebase: '.$tag.':'.
                    668: 				$oldcodebase.' - '.
                    669: 				    $codebase."\n";
                    670: 			}
                    671: 			$allow{&absoluteurl($codebase,$target).'/*'}=1;
                    672: 		    } else {
                    673: 			foreach ('archive','code','object') {
                    674: 			    if (defined($parms{$_})) {
                    675: 				my $oldurl=$parms{$_};
                    676: 				my $newurl=&urlfixup($oldurl,$target);
                    677: 				$newurl=~s/\/[^\/]+$/\/\*/;
                    678: 				print $logfile 'Allow: applet '.$_.':'.
                    679: 				    $oldurl.' allows '.
                    680: 					$newurl."\n";
                    681: 				$allow{&absoluteurl($newurl,$target)}=1;
                    682: 			    }
                    683: 			}
                    684: 		    }
                    685: 		}
                    686: 		my $newparmstring='';
                    687: 		my $endtag='';
                    688: 		foreach (keys %parms) {
                    689: 		    if ($_ eq '/') {
                    690: 			$endtag=' /';
                    691: 		    } else { 
                    692: 			my $quote=($parms{$_}=~/\"/?"'":'"');
                    693: 			$newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
                    694: 		    }
                    695: 		}
                    696: 		if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
                    697: 		$outstring.='<'.$tag.$newparmstring.$endtag.'>';
1.87      albertel  698: 		if ($lctag eq 'm') {
                    699: 		    $outstring.=&get_all_text_unbalanced('/m',\@parser);
                    700: 		}
1.86      albertel  701: 	    } elsif ($token->[0] eq 'E') {
                    702: 		if ($token->[2]) {
                    703: 		    unless ($token->[1] eq 'allow') {
                    704: 			$outstring.='</'.$token->[1].'>';
                    705: 		    }
                    706: 		}
                    707: 	    } else {
                    708: 		$outstring.=$token->[1];
                    709: 	    }
                    710: 	}
                    711: 	pop(@parser);
                    712:     }
                    713: 
                    714:     if ($needsfixup) {
                    715: 	print $logfile "End of ID and/or index fixup\n".
                    716: 	    "Max ID   : $maxid (min 10)\n".
                    717: 		"Max Index: $maxindex (min 10)\n";
                    718:     } else {
                    719: 	print $logfile "Does not need ID and/or index fixup\n";
                    720:     }
                    721: 
1.106     albertel  722:     return ($outstring,0,%allow);
1.86      albertel  723: }
                    724: 
1.89      matthew   725: #########################################
                    726: #########################################
                    727: 
                    728: =pod
                    729: 
1.94      harris41  730: =item B<store_metadata>
1.89      matthew   731: 
                    732: Store the metadata in the metadata table in the loncapa database.
                    733: Uses lonmysql to access the database.
                    734: 
                    735: Inputs: \%metadata
                    736: 
                    737: Returns: (error,status).  error is undef on success, status is undef on error.
                    738: 
                    739: =cut
                    740: 
                    741: #########################################
                    742: #########################################
                    743: sub store_metadata {
                    744:     my %metadata = %{shift()};
                    745:     my $error;
                    746:     # Determine if the table exists
                    747:     my $status = &Apache::lonmysql::check_table('metadata');
                    748:     if (! defined($status)) {
                    749:         $error='<font color="red">WARNING: Cannot connect to '.
                    750:             'database!</font>';
                    751:         &Apache::lonnet::logthis($error);
                    752:         return ($error,undef);
                    753:     }
                    754:     if ($status == 0) {
                    755:         # It would be nice to actually create the table....
                    756:         $error ='<font color="red">WARNING: The metadata table does not '.
                    757:             'exist in the LON-CAPA database.</font>';
                    758:         &Apache::lonnet::logthis($error);
                    759:         return ($error,undef);
                    760:     }
                    761:     # Remove old value from table
                    762:     $status = &Apache::lonmysql::remove_from_table
                    763:         ('metadata','url',$metadata{'url'});
                    764:     if (! defined($status)) {
                    765:         $error = '<font color="red">Error when removing old values from '.
                    766:             'metadata table in LON-CAPA database.</font>';
                    767:         &Apache::lonnet::logthis($error);
                    768:         return ($error,undef);
                    769:     }
                    770:     # Store data in table.
                    771:     $status = &Apache::lonmysql::store_row('metadata',\%metadata);
                    772:     if (! defined($status)) {
                    773:         $error='<font color="red">Error occured storing new values in '.
                    774:             'metadata table in LON-CAPA database</font>';
                    775:         &Apache::lonnet::logthis($error);
                    776:         return ($error,undef);
                    777:     }
                    778:     return (undef,$status);
                    779: }
                    780: 
1.90      matthew   781: #########################################
                    782: #########################################
                    783: 
                    784: =pod
                    785: 
1.94      harris41  786: =item B<publish>
                    787: 
                    788: This is the workhorse function of this module.  This subroutine generates
                    789: backup copies, performs any automatic processing (prior to publication,
                    790: especially for rat and ssi files),
1.90      matthew   791: 
1.113     albertel  792: Returns a 2 element array, the first is the string to be shown to the
                    793: user, the second is an error code, either 1 (an error occured) or 0
                    794: (no error occurred)
                    795: 
1.94      harris41  796: I<Additional documentation needed.>
1.90      matthew   797: 
                    798: =cut
                    799: 
                    800: #########################################
                    801: #########################################
1.2       www       802: sub publish {
1.50      www       803: 
1.97      www       804:     my ($source,$target,$style,$batch)=@_;
1.2       www       805:     my $logfile;
1.4       www       806:     my $scrout='';
1.23      www       807:     my $allmeta='';
                    808:     my $content='';
1.36      www       809:     my %allow=();
1.4       www       810: 
1.2       www       811:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.114     albertel  812: 	return ('<font color="red">No write permission to user directory, FAIL</font>',1);
1.2       www       813:     }
                    814:     print $logfile 
1.11      www       815: "\n\n================= Publish ".localtime()." Phase One  ================\n";
1.2       www       816: 
1.119     www       817:     if (($style eq 'ssi') || ($style eq 'rat') || ($style eq 'prv')) {
1.3       www       818: # ------------------------------------------------------- This needs processing
1.4       www       819: 
                    820: # ----------------------------------------------------------------- Backup Copy
1.3       www       821: 	my $copyfile=$source.'.save';
1.13      www       822:         if (copy($source,$copyfile)) {
1.3       www       823: 	    print $logfile "Copied original file to ".$copyfile."\n";
                    824:         } else {
1.13      www       825: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
1.114     albertel  826: 	    return ("<font color=\"red\">Failed to write backup copy, $!,FAIL</font>",1);
1.3       www       827:         }
1.4       www       828: # ------------------------------------------------------------- IDs and indices
1.86      albertel  829: 	
1.106     albertel  830: 	my ($outstring,$error);
                    831: 	($outstring,$error,%allow)=&fix_ids_and_indices($logfile,$source,
                    832: 							$target);
1.113     albertel  833: 	if ($error) { return ($outstring,$error); }
1.36      www       834: # ------------------------------------------------------------ Construct Allows
1.62      www       835:     
1.44      www       836: 	$scrout.='<h3>Dependencies</h3>';
1.62      www       837:         my $allowstr='';
1.73      albertel  838:         foreach (sort(keys(%allow))) {
1.59      www       839: 	   my $thisdep=$_;
1.73      albertel  840: 	   if ($thisdep !~ /[^\s]/) { next; }
1.62      www       841:            unless ($style eq 'rat') { 
                    842:               $allowstr.="\n".'<allow src="'.$thisdep.'" />';
                    843: 	   }
1.120   ! albertel  844:            $scrout.='<br />';
1.59      www       845:            unless ($thisdep=~/\*/) {
                    846: 	       $scrout.='<a href="'.$thisdep.'">';
1.44      www       847:            }
1.59      www       848:            $scrout.='<tt>'.$thisdep.'</tt>';
                    849:            unless ($thisdep=~/\*/) {
1.44      www       850: 	       $scrout.='</a>';
1.59      www       851:                if (
                    852:        &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                    853:                                             $thisdep.'.meta') eq '-1') {
1.94      harris41  854: 		   $scrout.= ' - <font color="red">Currently not available'.
                    855: 		       '</font>';
1.59      www       856:                } else {
                    857:                    my %temphash=(&Apache::lonnet::declutter($target).'___'.
                    858:                              &Apache::lonnet::declutter($thisdep).'___usage'
                    859:                                  => time);
                    860:                    $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
                    861:                    if ((defined($1)) && (defined($2))) {
1.92      albertel  862:                       &Apache::lonnet::put('nohist_resevaldata',\%temphash,
                    863: 					   $1,$2);
1.59      www       864: 		   }
                    865: 	       }
1.44      www       866:            }
1.65      harris41  867:         }
1.83      www       868:         $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
1.62      www       869: 
1.76      albertel  870: 	#Encode any High ASCII characters
                    871: 	$outstring=&HTML::Entities::encode($outstring,"\200-\377");
1.94      harris41  872: # ------------------------------------------------------------- Write modified.
1.37      www       873: 
1.4       www       874:         {
                    875:           my $org;
                    876:           unless ($org=Apache::File->new('>'.$source)) {
                    877:              print $logfile "No write permit to $source\n";
1.113     albertel  878:              return ('<font color="red">No write permission to '.$source.
                    879: 		     ', FAIL</font>',1);
1.4       www       880: 	  }
1.94      harris41  881:           print($org $outstring);
1.4       www       882:         }
                    883: 	  $content=$outstring;
1.34      www       884: 
1.37      www       885:     }
1.94      harris41  886: # -------------------------------------------- Initial step done, now metadata.
1.7       www       887: 
1.94      harris41  888: # --------------------------------------- Storage for metadata keys and fields.
1.7       www       889: 
1.8       www       890:      %metadatafields=();
                    891:      %metadatakeys=();
                    892:      
                    893:      my %oldparmstores=();
1.44      www       894:      
1.97      www       895:     unless ($batch) {
1.84      bowersj2  896:      $scrout.='<h3>Metadata Information ' .
                    897:        Apache::loncommon::help_open_topic("Metadata_Description")
                    898:        . '</h3>';
1.97      www       899:     }
1.7       www       900: 
                    901: # ------------------------------------------------ First, check out environment
1.8       www       902:      unless (-e $source.'.meta') {
1.7       www       903:         $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
                    904: 	                          $ENV{'environment.middlename'}.' '.
                    905: 		                  $ENV{'environment.lastname'}.' '.
                    906: 		                  $ENV{'environment.generation'};
1.8       www       907:         $metadatafields{'author'}=~s/\s+/ /g;
                    908:         $metadatafields{'author'}=~s/\s+$//;
1.27      www       909:         $metadatafields{'owner'}=$cuname.'@'.$cudom;
1.7       www       910: 
                    911: # ------------------------------------------------ Check out directory hierachy
                    912: 
                    913:         my $thisdisfn=$source;
1.27      www       914:         $thisdisfn=~s/^\/home\/$cuname\///;
1.7       www       915: 
                    916:         my @urlparts=split(/\//,$thisdisfn);
                    917:         $#urlparts--;
                    918: 
1.27      www       919:         my $currentpath='/home/'.$cuname.'/';
1.7       www       920: 
1.65      harris41  921:         foreach (@urlparts) {
1.7       www       922: 	    $currentpath.=$_.'/';
                    923:             $scrout.=&metaread($logfile,$currentpath.'default.meta');
1.65      harris41  924:         }
1.7       www       925: 
                    926: # ------------------- Clear out parameters and stores (there should not be any)
                    927: 
1.65      harris41  928:         foreach (keys %metadatafields) {
1.7       www       929: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
                    930: 		delete $metadatafields{$_};
                    931:             }
1.65      harris41  932:         }
1.7       www       933: 
1.8       www       934:     } else {
1.7       www       935: # ---------------------- Read previous metafile, remember parameters and stores
                    936: 
                    937:         $scrout.=&metaread($logfile,$source.'.meta');
                    938: 
1.65      harris41  939:         foreach (keys %metadatafields) {
1.7       www       940: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
                    941:                 $oldparmstores{$_}=1;
                    942: 		delete $metadatafields{$_};
                    943:             }
1.65      harris41  944:         }
1.7       www       945:         
1.8       www       946:     }
1.7       www       947: 
1.4       www       948: # -------------------------------------------------- Parse content for metadata
1.119     www       949:     if (($style eq 'ssi') || ($style eq 'prv')) {
1.42      www       950:         my $oldenv=$ENV{'request.uri'};
                    951: 
                    952:         $ENV{'request.uri'}=$target;
1.82      albertel  953:         $allmeta=Apache::lonxml::xmlparse(undef,'meta',$content);
1.42      www       954:         $ENV{'request.uri'}=$oldenv;
1.32      www       955: 
1.19      albertel  956:         &metaeval($allmeta);
1.37      www       957:     }
1.7       www       958: # ---------------- Find and document discrepancies in the parameters and stores
                    959: 
1.116     albertel  960:     my $chparms='';
                    961:     foreach (sort keys %metadatafields) {
                    962: 	if (($_=~/^parameter/) || ($_=~/^stores/)) {
                    963: 	    unless ($_=~/\.\w+$/) { 
                    964: 		unless ($oldparmstores{$_}) {
                    965: 		    print $logfile 'New: '.$_."\n";
                    966: 		    $chparms.=$_.' ';
                    967: 		}
                    968: 	    }
                    969: 	}
                    970:     }
                    971:     if ($chparms) {
1.120   ! albertel  972: 	$scrout.='<p><b>New parameters or stored values:</b> '.$chparms.'</p>';
1.116     albertel  973:     }
1.7       www       974: 
1.116     albertel  975:     $chparms='';
                    976:     foreach (sort keys %oldparmstores) {
                    977: 	if (($_=~/^parameter/) || ($_=~/^stores/)) {
                    978: 	    unless (($metadatafields{$_.'.name'}) ||
                    979: 		    ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
                    980: 		print $logfile 'Obsolete: '.$_."\n";
                    981: 		$chparms.=$_.' ';
                    982: 	    }
                    983: 	}
                    984:     }
                    985:     if ($chparms) {
                    986: 	$scrout.='<p><b>Obsolete parameters or stored values:</b> '.
1.120   ! albertel  987: 	    $chparms.'</p>';
1.116     albertel  988:     }
1.37      www       989: 
1.8       www       990: # ------------------------------------------------------- Now have all metadata
1.5       www       991: 
1.116     albertel  992:     my %keywords=();
1.97      www       993:         
1.116     albertel  994:     if (length($content)<500000) {
                    995: 	my $textonly=$content;
                    996: 	$textonly=~s/\<script[^\<]+\<\/script\>//g;
                    997: 	$textonly=~s/\<m\>[^\<]+\<\/m\>//g;
                    998: 	$textonly=~s/\<[^\>]*\>//g;
                    999: 	$textonly=~tr/A-Z/a-z/;
                   1000: 	$textonly=~s/[\$\&][a-z]\w*//g;
                   1001: 	$textonly=~s/[^a-z\s]//g;
                   1002: 	
                   1003: 	foreach ($textonly=~m/(\w+)/g) {
                   1004: 	    unless ($nokey{$_}) {
                   1005: 		$keywords{$_}=1;
                   1006: 	    } 
                   1007: 	}
                   1008:     }
1.97      www      1009: 
                   1010:             
1.116     albertel 1011:     foreach (split(/\W+/,$metadatafields{'keywords'})) {
                   1012: 	$keywords{$_}=1;
                   1013:     }
1.97      www      1014: # --------------------------------------------------- Now we also have keywords
                   1015: # =============================================================================
                   1016: # INTERACTIVE MODE
                   1017: #
1.116     albertel 1018:     unless ($batch) {
1.8       www      1019:         $scrout.=
1.116     albertel 1020: 	    '<form name="pubform" action="/adm/publish" method="post">'.
                   1021:             '<p><input type="submit" value="Finalize Publication" /></p>'.
                   1022:             &hiddenfield('phase','two').
                   1023:             &hiddenfield('filename',$ENV{'form.filename'}).
                   1024: 	    &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
                   1025:             &hiddenfield('dependencies',join(',',keys %allow)).
                   1026:             &textfield('Title','title',$metadatafields{'title'}).
                   1027:             &textfield('Author(s)','author',$metadatafields{'author'}).
                   1028: 	    &textfield('Subject','subject',$metadatafields{'subject'});
1.5       www      1029: 
                   1030: # --------------------------------------------------- Scan content for keywords
1.7       www      1031: 
1.84      bowersj2 1032:         my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
1.77      matthew  1033: 	my $keywordout=<<"END";
                   1034: <script>
1.116     albertel 1035: function checkAll(field) {
1.77      matthew  1036:     for (i = 0; i < field.length; i++)
                   1037:         field[i].checked = true ;
                   1038: }
                   1039: 
1.116     albertel 1040: function uncheckAll(field) {
1.77      matthew  1041:     for (i = 0; i < field.length; i++)
                   1042:         field[i].checked = false ;
                   1043: }
                   1044: </script>
1.84      bowersj2 1045: <p><b>Keywords: $keywords_help</b> 
1.120   ! albertel 1046: <input type="button" value="check all" onclick="javascript:checkAll(document.pubform.keywords)" /> 
        !          1047: <input type="button" value="uncheck all" onclick="javascript:uncheckAll(document.pubform.keywords)" /> 
        !          1048: </p>
1.77      matthew  1049: <br />
1.117     albertel 1050: END
1.120   ! albertel 1051: 	$keywordout.='<table border="2"><tr>';
1.116     albertel 1052: 	my $colcount=0;
                   1053: 
                   1054: 	foreach (sort keys %keywords) {
1.120   ! albertel 1055: 	    $keywordout.='<td><input type="checkbox" name="keywords" value="'.$_.'"';
1.116     albertel 1056: 	    if ($metadatafields{'keywords'}) {
                   1057: 		if ($metadatafields{'keywords'}=~/$_/) {
1.120   ! albertel 1058: 		    $keywordout.=' checked="on"';
1.116     albertel 1059: 		}
                   1060: 	    } elsif (&Apache::loncommon::keyword($_)) {
1.120   ! albertel 1061: 		$keywordout.=' checked="on"';
1.116     albertel 1062: 	    }
1.120   ! albertel 1063: 	    $keywordout.=' />'.$_.'</td>';
1.116     albertel 1064: 	    if ($colcount>10) {
                   1065: 		$keywordout.="</tr><tr>\n";
                   1066: 		$colcount=0;
                   1067: 	    }
                   1068: 	    $colcount++;
                   1069: 	}
                   1070: 
1.51      www      1071: 	$keywordout.='</tr></table>';
                   1072: 
1.116     albertel 1073: 	$scrout.=$keywordout;
1.9       www      1074: 
1.116     albertel 1075: 	$scrout.=&textfield('Additional Keywords','addkey','');
1.12      www      1076: 
1.116     albertel 1077: 	$scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9       www      1078: 
1.116     albertel 1079: 	$scrout.=
1.120   ! albertel 1080: 	    '<p><b>Abstract:</b><br /><textarea cols="80" rows="5" name="abstract">'.
        !          1081: 	    $metadatafields{'abstract'}.'</textarea></p>';
1.9       www      1082: 
1.11      www      1083: 	$source=~/\.(\w+)$/;
                   1084: 
                   1085: 	$scrout.=&hiddenfield('mime',$1);
                   1086: 
1.116     albertel 1087: 	$scrout.=&selectbox('Language','language',
                   1088: 			    $metadatafields{'language'},
1.70      harris41 1089: 			    \&Apache::loncommon::languagedescription,
1.65      harris41 1090: 			    (&Apache::loncommon::languageids),
1.116     albertel 1091: 			   );
1.11      www      1092: 
1.116     albertel 1093: 	unless ($metadatafields{'creationdate'}) {
1.11      www      1094: 	    $metadatafields{'creationdate'}=time;
1.116     albertel 1095: 	}
                   1096: 	$scrout.=&hiddenfield('creationdate',
                   1097: 			      &Apache::loncommon::unsqltime($metadatafields{'creationdate'}));
                   1098: 
                   1099: 	$scrout.=&hiddenfield('lastrevisiondate',time);
1.11      www      1100: 
                   1101: 
1.10      www      1102: 	$scrout.=&textfield('Publisher/Owner','owner',
1.116     albertel 1103: 			    $metadatafields{'owner'});
1.84      bowersj2 1104: 
1.94      harris41 1105: # -------------------------------------------------- Correct copyright for rat.
1.116     albertel 1106: 	unless ($style eq 'prv') {
                   1107: 	    if ($style eq 'rat') {
                   1108: 		if ($metadatafields{'copyright'} eq 'public') { 
                   1109: 		    delete $metadatafields{'copyright'};
                   1110: 		}
                   1111: 		$scrout.=&selectbox('Copyright/Distribution','copyright',
                   1112: 				    $metadatafields{'copyright'},
                   1113: 				    \&Apache::loncommon::copyrightdescription,
                   1114: 				    (grep !/^public$/,(&Apache::loncommon::copyrightids)));
                   1115: 	    } else {
                   1116: 		$scrout.=&selectbox('Copyright/Distribution','copyright',
                   1117: 				    $metadatafields{'copyright'},
                   1118: 				    \&Apache::loncommon::copyrightdescription,
                   1119: 				    (&Apache::loncommon::copyrightids));
                   1120: 	    }
                   1121:     
                   1122: 	    my $copyright_help =
                   1123: 		Apache::loncommon::help_open_topic('Publishing_Copyright');
                   1124: 	    $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
                   1125: 	    $scrout.=&textfield('Custom Distribution File','customdistributionfile',
                   1126: 				$metadatafields{'customdistributionfile'}).
                   1127: 				    $copyright_help;
                   1128: 	} else {
                   1129: 	    $scrout.=&hiddenfield('copyright','private');
1.65      harris41 1130: 	}
1.116     albertel 1131: 	return ($scrout.'<p><input type="submit" value="Finalize Publication" /></p></form>',0);
1.97      www      1132: # =============================================================================
                   1133: # BATCH MODE
                   1134: #
1.116     albertel 1135:     } else {
1.97      www      1136: # Transfer metadata directly to environment for stage 2
1.116     albertel 1137: 	foreach (keys %metadatafields) {
                   1138: 	    $ENV{'form.'.$_}=$metadatafields{$_};
                   1139: 	}
                   1140: 	$ENV{'form.addkey'}='';
                   1141: 	$ENV{'form.keywords'}='';
                   1142: 	foreach (keys %keywords) {
                   1143: 	    if ($metadatafields{'keywords'}) {
                   1144: 		if ($metadatafields{'keywords'}=~/$_/) { 
                   1145: 		    $ENV{'form.keywords'}.=$_.','; 
                   1146: 		}
                   1147: 	    } elsif (&Apache::loncommon::keyword($_)) {
                   1148: 		$ENV{'form.keywords'}.=$_.',';
                   1149: 	    }
                   1150: 	}
                   1151: 	$ENV{'form.keywords'}=~s/\,$//;
                   1152: 	unless ($ENV{'form.creationdate'}) { $ENV{'form.creationdate'}=time; }
                   1153: 	$ENV{'form.lastrevisiondate'}=time;
                   1154: 	if ((($style eq 'rat') && ($ENV{'form.copyright'} eq 'public')) ||
                   1155: 	    (!$ENV{'form.copyright'})) { 
                   1156: 	    $ENV{'form.copyright'}='default';
                   1157: 	}
                   1158: 	$ENV{'form.allmeta'}=&Apache::lonnet::escape($allmeta);
                   1159: 	return ($scrout,0);
1.97      www      1160:     }
1.2       www      1161: }
1.1       www      1162: 
1.90      matthew  1163: #########################################
                   1164: #########################################
                   1165: 
                   1166: =pod 
                   1167: 
1.94      harris41 1168: =item B<phasetwo>
1.90      matthew  1169: 
                   1170: Render second interface showing status of publication steps.
                   1171: This is publication step two.
                   1172: 
1.94      harris41 1173: Parameters:
                   1174: 
                   1175: =over 4
                   1176: 
                   1177: =item I<$source>
                   1178: 
                   1179: =item I<$target>
                   1180: 
                   1181: =item I<$style>
                   1182: 
                   1183: =item I<$distarget>
                   1184: 
                   1185: =back
                   1186: 
                   1187: Returns:
                   1188: 
                   1189: =over 4
                   1190: 
                   1191: =item Scalar string
                   1192: 
                   1193: String contains status (errors and warnings) and information associated with
1.100     matthew  1194: the server's attempts at publication.     
1.94      harris41 1195: 
1.90      matthew  1196: =cut
1.12      www      1197: 
1.100     matthew  1198: #'stupid emacs
1.90      matthew  1199: #########################################
                   1200: #########################################
1.11      www      1201: sub phasetwo {
                   1202: 
1.100     matthew  1203:     my ($r,$source,$target,$style,$distarget,$batch)=@_;
1.102     www      1204:     $source=~s/\/+/\//g;
                   1205:     $target=~s/\/+/\//g;
1.109     www      1206: 
                   1207:     if ($target=~/\_\_\_/) {
1.110     www      1208: 	$r->print(
1.114     albertel 1209:  '<font color="red">Unsupported character combination "<tt>___</tt>" in filename, FAIL</font>');
1.110     www      1210:         return 0;
1.109     www      1211:     }
1.102     www      1212:     $distarget=~s/\/+/\//g;
1.11      www      1213:     my $logfile;
                   1214:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.110     www      1215: 	$r->print(
1.114     albertel 1216:         '<font color="red">No write permission to user directory, FAIL</font>');
1.110     www      1217:         return 0;
1.11      www      1218:     }
                   1219:     print $logfile 
1.100     matthew  1220:         "\n================= Publish ".localtime()." Phase Two  ================\n";
                   1221:     
                   1222:     %metadatafields=();
                   1223:     %metadatakeys=();
                   1224:     
                   1225:     &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
                   1226:     
                   1227:     $metadatafields{'title'}=$ENV{'form.title'};
                   1228:     $metadatafields{'author'}=$ENV{'form.author'};
                   1229:     $metadatafields{'subject'}=$ENV{'form.subject'};
                   1230:     $metadatafields{'notes'}=$ENV{'form.notes'};
                   1231:     $metadatafields{'abstract'}=$ENV{'form.abstract'};
                   1232:     $metadatafields{'mime'}=$ENV{'form.mime'};
                   1233:     $metadatafields{'language'}=$ENV{'form.language'};
1.103     www      1234:     $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
                   1235:     $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
1.100     matthew  1236:     $metadatafields{'owner'}=$ENV{'form.owner'};
                   1237:     $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.115     www      1238:     $metadatafields{'customdistributionfile'}=
                   1239:                                  $ENV{'form.customdistributionfile'};
1.100     matthew  1240:     $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
                   1241:     
                   1242:     my $allkeywords=$ENV{'form.addkey'};
                   1243:     if (exists($ENV{'form.keywords'})) {
                   1244:         if (ref($ENV{'form.keywords'})) {
                   1245:             $allkeywords .= ','.join(',',@{$ENV{'form.keywords'}});
                   1246:         } else {
                   1247:             $allkeywords .= ','.$ENV{'form.keywords'};
                   1248:         }
                   1249:     }
                   1250:     $allkeywords=~s/\W+/\,/;
                   1251:     $allkeywords=~s/^\,//;
                   1252:     $metadatafields{'keywords'}=$allkeywords;
                   1253:     
                   1254:     {
                   1255:         print $logfile "\nWrite metadata file for ".$source;
                   1256:         my $mfh;
                   1257:         unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
                   1258:             return 
1.114     albertel 1259:                 '<font color="red">Could not write metadata, FAIL</font>';
1.100     matthew  1260:         }
                   1261:         foreach (sort keys %metadatafields) {
                   1262:             unless ($_=~/\./) {
                   1263:                 my $unikey=$_;
                   1264:                 $unikey=~/^([A-Za-z]+)/;
                   1265:                 my $tag=$1;
                   1266:                 $tag=~tr/A-Z/a-z/;
                   1267:                 print $mfh "\n\<$tag";
                   1268:                 foreach (split(/\,/,$metadatakeys{$unikey})) {
                   1269:                     my $value=$metadatafields{$unikey.'.'.$_};
                   1270:                     $value=~s/\"/\'\'/g;
                   1271:                     print $mfh ' '.$_.'="'.$value.'"';
                   1272:                 }
                   1273:                 print $mfh '>'.
                   1274:                     &HTML::Entities::encode($metadatafields{$unikey})
                   1275:                         .'</'.$tag.'>';
                   1276:             }
                   1277:         }
1.120   ! albertel 1278:         $r->print('<p>Wrote Metadata</p>');
1.100     matthew  1279:         print $logfile "\nWrote metadata";
                   1280:     }
                   1281:     
                   1282: # -------------------------------- Synchronize entry with SQL metadata database
1.12      www      1283: 
1.89      matthew  1284:     $metadatafields{'url'} = $distarget;
                   1285:     $metadatafields{'version'} = 'current';
                   1286:     unless ($metadatafields{'copyright'} eq 'priv') {
                   1287:         my ($error,$success) = &store_metadata(\%metadatafields);
1.91      matthew  1288:         if ($success) {
1.120   ! albertel 1289:             $r->print('<p>Synchronized SQL metadata database</p>');
1.89      matthew  1290:             print $logfile "\nSynchronized SQL metadata database";
                   1291:         } else {
1.100     matthew  1292:             $r->print($error);
1.89      matthew  1293:             print $logfile "\n".$error;
                   1294:         }
                   1295:     } else {
1.120   ! albertel 1296:         $r->print('<p>Private Publication - did not synchronize database</p>');
1.89      matthew  1297:         print $logfile "\nPrivate: Did not synchronize data into ".
                   1298:             "SQL metadata database";
1.24      harris41 1299:     }
1.12      www      1300: # ----------------------------------------------------------- Copy old versions
                   1301:    
1.100     matthew  1302:     if (-e $target) {
                   1303:         my $filename;
                   1304:         my $maxversion=0;
                   1305:         $target=~/(.*)\/([^\/]+)\.(\w+)$/;
                   1306:         my $srcf=$2;
                   1307:         my $srct=$3;
                   1308:         my $srcd=$1;
                   1309:         unless ($srcd=~/^\/home\/httpd\/html\/res/) {
                   1310:             print $logfile "\nPANIC: Target dir is ".$srcd;
1.114     albertel 1311:             return "<font color=\"red\">Invalid target directory, FAIL</font>";
1.100     matthew  1312:         }
                   1313:         opendir(DIR,$srcd);
                   1314:         while ($filename=readdir(DIR)) {
                   1315:             if (-l $srcd.'/'.$filename) {
                   1316:                 unlink($srcd.'/'.$filename);
                   1317:                 unlink($srcd.'/'.$filename.'.meta');
                   1318:             } else {
1.118     albertel 1319:                 if ($filename=~/\Q$srcf\E\.(\d+)\.\Q$srct\E$/) {
1.100     matthew  1320:                     $maxversion=($1>$maxversion)?$1:$maxversion;
                   1321:                 }
                   1322:             }
                   1323:         }
                   1324:         closedir(DIR);
                   1325:         $maxversion++;
1.120   ! albertel 1326:         $r->print('<p>Creating old version '.$maxversion.'</p>');
1.100     matthew  1327:         print $logfile "\nCreating old version ".$maxversion;
                   1328:         
                   1329:         my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
                   1330:         
1.13      www      1331:         if (copy($target,$copyfile)) {
1.12      www      1332: 	    print $logfile "Copied old target to ".$copyfile."\n";
1.120   ! albertel 1333:             $r->print('<p>Copied old target file</p>');
1.12      www      1334:         } else {
1.13      www      1335: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
1.114     albertel 1336:             return "<font color=\"red\">Failed to copy old target, $!, FAIL</font>";
1.12      www      1337:         }
1.100     matthew  1338:         
1.12      www      1339: # --------------------------------------------------------------- Copy Metadata
                   1340: 
                   1341: 	$copyfile=$copyfile.'.meta';
1.100     matthew  1342:         
1.13      www      1343:         if (copy($target.'.meta',$copyfile)) {
1.14      www      1344: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
1.120   ! albertel 1345:             $r->print('<p>Copied old metadata</p>')
1.12      www      1346:         } else {
1.13      www      1347: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14      www      1348:             if (-e $target.'.meta') {
1.100     matthew  1349:                 return 
1.114     albertel 1350:                     "<font color=\"red\">Failed to write old metadata copy, $!, FAIL</font>";
1.14      www      1351: 	    }
1.12      www      1352:         }
1.100     matthew  1353:         
                   1354:         
                   1355:     } else {
1.120   ! albertel 1356:         $r->print('<p>Initial version</p>');
1.100     matthew  1357:         print $logfile "\nInitial version";
                   1358:     }
1.12      www      1359: 
                   1360: # ---------------------------------------------------------------- Write Source
1.100     matthew  1361:     my $copyfile=$target;
                   1362:     
                   1363:     my @parts=split(/\//,$copyfile);
                   1364:     my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1365:     
                   1366:     my $count;
                   1367:     for ($count=5;$count<$#parts;$count++) {
                   1368:         $path.="/$parts[$count]";
                   1369:         if ((-e $path)!=1) {
                   1370:             print $logfile "\nCreating directory ".$path;
1.120   ! albertel 1371:             $r->print('<p>Created directory '.$parts[$count].'</p>');
1.100     matthew  1372:             mkdir($path,0777);
1.12      www      1373:         }
1.100     matthew  1374:     }
                   1375:     
                   1376:     if (copy($source,$copyfile)) {
                   1377:         print $logfile "\nCopied original source to ".$copyfile."\n";
1.120   ! albertel 1378:         $r->print('<p>Copied source file</p>');
1.100     matthew  1379:     } else {
                   1380:         print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
1.114     albertel 1381:         return "<font color=\"red\">Failed to copy source, $!, FAIL</font>";
1.100     matthew  1382:     }
                   1383:     
1.12      www      1384: # --------------------------------------------------------------- Copy Metadata
                   1385: 
1.100     matthew  1386:     $copyfile=$copyfile.'.meta';
                   1387:     
                   1388:     if (copy($source.'.meta',$copyfile)) {
                   1389:         print $logfile "\nCopied original metadata to ".$copyfile."\n";
1.120   ! albertel 1390:         $r->print('<p>Copied metadata</p>');
1.100     matthew  1391:     } else {
                   1392:         print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";
                   1393:         return 
1.114     albertel 1394:             "<font color=\"red\">Failed to write metadata copy, $!, FAIL</font>";
1.100     matthew  1395:     }
                   1396:     $r->rflush;
1.12      www      1397: # --------------------------------------------------- Send update notifications
                   1398: 
1.85      albertel 1399:     my @subscribed=&get_subscribed_hosts($target);
                   1400:     foreach my $subhost (@subscribed) {
1.100     matthew  1401: 	$r->print('<p>Notifying host '.$subhost.':');$r->rflush;
1.85      albertel 1402: 	print $logfile "\nNotifying host ".$subhost.':';
                   1403: 	my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.120   ! albertel 1404: 	$r->print($reply.'</p><br />');$r->rflush;
1.85      albertel 1405: 	print $logfile $reply;
1.20      www      1406:     }
1.100     matthew  1407:     
1.20      www      1408: # ---------------------------------------- Send update notifications, meta only
                   1409: 
1.85      albertel 1410:     my @subscribedmeta=&get_subscribed_hosts("$target.meta");
                   1411:     foreach my $subhost (@subscribedmeta) {
1.100     matthew  1412: 	$r->print('<p>Notifying host for metadata only '.$subhost.':');$r->rflush;
1.85      albertel 1413: 	print $logfile "\nNotifying host for metadata only ".$subhost.':';
                   1414: 	my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
                   1415: 					    $subhost);
1.120   ! albertel 1416: 	$r->print($reply.'</p><br />');$r->rflush;
1.85      albertel 1417: 	print $logfile $reply;
1.12      www      1418:     }
1.100     matthew  1419:     
1.101     www      1420: # --------------------------------------------------- Notify subscribed courses
                   1421:     my %courses=&coursedependencies($target);
                   1422:     my $now=time;
                   1423:     foreach (keys %courses) {
                   1424: 	$r->print('<p>Notifying course '.$_.':');$r->rflush;
                   1425: 	print $logfile "\nNotifying host ".$_.':';
                   1426:         my ($cdom,$cname)=split(/\_/,$_);
                   1427: 	my $reply=&Apache::lonnet::cput
                   1428:                   ('versionupdate',{$target => $now},$cdom,$cname);
1.120   ! albertel 1429: 	$r->print($reply.'</p><br />');$r->rflush;
1.101     www      1430: 	print $logfile $reply;
                   1431:     }
1.12      www      1432: # ------------------------------------------------ Provide link to new resource
1.100     matthew  1433:     unless ($batch) {
                   1434:         my $thisdistarget=$target;
                   1435:         $thisdistarget=~s/^$docroot//;
                   1436:         
                   1437:         my $thissrc=$source;
                   1438:         $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
                   1439:         
                   1440:         my $thissrcdir=$thissrc;
                   1441:         $thissrcdir=~s/\/[^\/]+$/\//;
                   1442:         
                   1443:         
                   1444:         $r->print(
1.120   ! albertel 1445:            '<hr /><a href="'.$thisdistarget.'"><font size="+2">'.
1.100     matthew  1446:            'View Published Version</font></a>'.
1.120   ! albertel 1447:            '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a></p>'.
1.100     matthew  1448:            '<p><a href="'.$thissrcdir.
1.120   ! albertel 1449:                    '"><font size="+2">Back to Source Directory</font></a></p>');
1.100     matthew  1450:     }
1.11      www      1451: }
                   1452: 
1.95      www      1453: #########################################
                   1454: 
                   1455: sub batchpublish {
1.97      www      1456:     my ($r,$srcfile,$targetfile)=@_;
1.102     www      1457:     $srcfile=~s/\/+/\//g;
                   1458:     $targetfile=~s/\/+/\//g;
1.95      www      1459:     my $thisdisfn=$srcfile;
                   1460:     $thisdisfn=~s/\/home\/korte\/public_html\///;
                   1461:     $srcfile=~s/\/+/\//g;
1.96      www      1462: 
1.97      www      1463:     my $docroot=$r->dir_config('lonDocRoot');
                   1464:     my $thisdistarget=$targetfile;
                   1465:     $thisdistarget=~s/^$docroot//;
                   1466: 
1.96      www      1467: 
                   1468:     undef %metadatafields;
                   1469:     undef %metadatakeys;
                   1470:      %metadatafields=();
                   1471:      %metadatakeys=();
1.97      www      1472:       $srcfile=~/\.(\w+)$/;
                   1473:       my $thistype=$1;
                   1474: 
                   1475: 
                   1476:       my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.96      www      1477:      
1.95      www      1478:     $r->print('<h2>Publishing <tt>'.$thisdisfn.'</tt></h2>');
1.97      www      1479: 
                   1480: # phase one takes
                   1481: #  my ($source,$target,$style,$batch)=@_;
1.113     albertel 1482:     my ($outstring,$error)=&publish($srcfile,$targetfile,$thisembstyle,1);
                   1483:     $r->print('<p>'.$outstring.'</p>');
1.96      www      1484: # phase two takes
                   1485: # my ($source,$target,$style,$distarget,batch)=@_;
1.97      www      1486: # $ENV{'form.allmeta'},$ENV{'form.title'},$ENV{'form.author'},...
1.113     albertel 1487:     if (!$error) {
                   1488: 	$r->print('<p>');
                   1489: 	&phasetwo($r,$srcfile,$targetfile,$thisembstyle,$thisdistarget,1);
                   1490: 	$r->print('</p>');
                   1491:     }
1.97      www      1492:     return '';
1.95      www      1493: }
1.1       www      1494: 
1.90      matthew  1495: #########################################
1.95      www      1496: 
                   1497: sub publishdirectory {
                   1498:     my ($r,$fn,$thisdisfn)=@_;
1.102     www      1499:     $fn=~s/\/+/\//g;
                   1500:     $thisdisfn=~s/\/+/\//g;
1.96      www      1501:     my $resdir=
1.100     matthew  1502:     $Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cudom.'/'.$cuname.'/'.
1.96      www      1503:       $thisdisfn;
1.100     matthew  1504:       $r->print('<h1>Directory <tt>'.$thisdisfn.'</tt></h1>'.
1.96      www      1505:                 'Target: <tt>'.$resdir.'</tt><br />');
1.95      www      1506: 
                   1507:       my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
                   1508: 
                   1509:       opendir(DIR,$fn);
                   1510:       my @files=sort(readdir(DIR));
                   1511:       foreach my $filename (@files) {
                   1512:          my ($cdev,$cino,$cmode,$cnlink,
                   1513:             $cuid,$cgid,$crdev,$csize,
                   1514:             $catime,$cmtime,$cctime,
                   1515:             $cblksize,$cblocks)=stat($fn.'/'.$filename);
                   1516: 
                   1517:          my $extension='';
                   1518:          if ($filename=~/\.(\w+)$/) { $extension=$1; }
                   1519:          if ($cmode&$dirptr) {
                   1520: 	   if (($filename!~/^\./) && ($ENV{'form.pubrec'})) {
                   1521: 	      &publishdirectory($r,$fn.'/'.$filename,$thisdisfn.'/'.$filename);
                   1522: 	   }
                   1523:          } elsif ((&Apache::loncommon::fileembstyle($extension) ne 'hdn') &&
                   1524:                   ($filename!~/^[\#\.]/) && ($filename!~/\~$/)) {
1.96      www      1525: # find out publication status and/or exiting metadata
                   1526: 	     my $publishthis=0;
                   1527:              if (-e $resdir.'/'.$filename) {
                   1528: 	        my ($rdev,$rino,$rmode,$rnlink,
                   1529: 	        $ruid,$rgid,$rrdev,$rsize,
                   1530: 	        $ratime,$rmtime,$rctime,
                   1531: 	        $rblksize,$rblocks)=stat($resdir.'/'.$filename);
                   1532: 	        if ($rmtime<$cmtime) {
                   1533: # previously published, modified now
                   1534: 		    $publishthis=1;
                   1535:                 }
                   1536: 	     } else {
                   1537: # never published
                   1538: 		 $publishthis=1;
                   1539: 	     }
                   1540:              if ($publishthis) {
1.97      www      1541:                 &batchpublish($r,$fn.'/'.$filename,$resdir.'/'.$filename);
1.96      www      1542: 	     } else {
                   1543:                  $r->print('<br />Skipping '.$filename.'<br />');
                   1544:              }
1.95      www      1545:              $r->rflush();
                   1546:          }
                   1547:       }
                   1548:       closedir(DIR);
                   1549: }
1.90      matthew  1550: #########################################
                   1551: 
                   1552: =pod
                   1553: 
1.94      harris41 1554: =item B<handler>
1.90      matthew  1555: 
                   1556: A basic outline of the handler subroutine follows.
                   1557: 
                   1558: =over 4
                   1559: 
1.94      harris41 1560: =item *
                   1561: 
                   1562: Get query string for limited number of parameters.
                   1563: 
                   1564: =item *
                   1565: 
                   1566: Check filename.
                   1567: 
                   1568: =item *
                   1569: 
                   1570: File is there and owned, init lookup tables.
                   1571: 
                   1572: =item *
1.90      matthew  1573: 
1.94      harris41 1574: Start page output.
1.90      matthew  1575: 
1.94      harris41 1576: =item *
1.90      matthew  1577: 
1.94      harris41 1578: Evaluate individual file, and then output information.
1.90      matthew  1579: 
1.94      harris41 1580: =item *
1.90      matthew  1581: 
1.94      harris41 1582: Publishing from $thisfn to $thistarget with $thisembstyle.
1.90      matthew  1583: 
                   1584: =back
                   1585: 
                   1586: =cut
                   1587: 
                   1588: #########################################
                   1589: #########################################
1.1       www      1590: sub handler {
                   1591:   my $r=shift;
1.2       www      1592: 
                   1593:   if ($r->header_only) {
                   1594:      $r->content_type('text/html');
                   1595:      $r->send_http_header;
                   1596:      return OK;
                   1597:   }
                   1598: 
1.43      www      1599: # Get query string for limited number of parameters
                   1600: 
1.80      matthew  1601:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1602:                                             ['filename']);
1.43      www      1603: 
1.2       www      1604: # -------------------------------------------------------------- Check filename
                   1605: 
1.113     albertel 1606:   my $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
1.2       www      1607: 
1.27      www      1608:   
1.2       www      1609:   unless ($fn) { 
1.27      www      1610:      $r->log_reason($cuname.' at '.$cudom.
1.2       www      1611:          ' trying to publish empty filename', $r->filename); 
                   1612:      return HTTP_NOT_FOUND;
                   1613:   } 
1.4       www      1614: 
1.31      www      1615:   ($cuname,$cudom)=
                   1616:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                   1617:   unless (($cuname) && ($cudom)) {
1.27      www      1618:      $r->log_reason($cuname.' at '.$cudom.
1.4       www      1619:          ' trying to publish file '.$ENV{'form.filename'}.
1.27      www      1620:          ' ('.$fn.') - not authorized', 
                   1621:          $r->filename); 
                   1622:      return HTTP_NOT_ACCEPTABLE;
                   1623:   }
                   1624: 
                   1625:   unless (&Apache::lonnet::homeserver($cuname,$cudom) 
                   1626:           eq $r->dir_config('lonHostID')) {
                   1627:      $r->log_reason($cuname.' at '.$cudom.
                   1628:          ' trying to publish file '.$ENV{'form.filename'}.
                   1629:          ' ('.$fn.') - not homeserver ('.
                   1630:          &Apache::lonnet::homeserver($cuname,$cudom).')', 
1.4       www      1631:          $r->filename); 
                   1632:      return HTTP_NOT_ACCEPTABLE;
                   1633:   }
1.2       www      1634: 
1.43      www      1635:   $fn=~s/^http\:\/\/[^\/]+//;
                   1636:   $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1.2       www      1637: 
                   1638:   my $targetdir='';
1.12      www      1639:   $docroot=$r->dir_config('lonDocRoot'); 
1.27      www      1640:   if ($1 ne $cuname) {
                   1641:      $r->log_reason($cuname.' at '.$cudom.
1.2       www      1642:          ' trying to publish unowned file '.$ENV{'form.filename'}.
                   1643:          ' ('.$fn.')', 
                   1644:          $r->filename); 
                   1645:      return HTTP_NOT_ACCEPTABLE;
                   1646:   } else {
1.27      www      1647:       $targetdir=$docroot.'/res/'.$cudom;
1.2       www      1648:   }
                   1649:                                  
                   1650:   
                   1651:   unless (-e $fn) { 
1.27      www      1652:      $r->log_reason($cuname.' at '.$cudom.
1.2       www      1653:          ' trying to publish non-existing file '.$ENV{'form.filename'}.
                   1654:          ' ('.$fn.')', 
                   1655:          $r->filename); 
                   1656:      return HTTP_NOT_FOUND;
                   1657:   } 
                   1658: 
1.11      www      1659: unless ($ENV{'form.phase'} eq 'two') {
                   1660: 
1.94      harris41 1661: # -------------------------------- File is there and owned, init lookup tables.
1.2       www      1662: 
1.3       www      1663:   %addid=();
                   1664: 
                   1665:   {
                   1666:       my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
                   1667:       while (<$fh>=~/(\w+)\s+(\w+)/) {
                   1668:           $addid{$1}=$2;
                   1669:       }
1.5       www      1670:   }
                   1671: 
                   1672:   %nokey=();
                   1673: 
                   1674:   {
                   1675:      my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1.65      harris41 1676:       while (<$fh>) {
1.5       www      1677:           my $word=$_;
                   1678:           chomp($word);
                   1679:           $nokey{$word}=1;
1.65      harris41 1680:       }
1.3       www      1681:   }
1.11      www      1682: 
                   1683: }
                   1684: 
1.94      harris41 1685: # ---------------------------------------------------------- Start page output.
1.2       www      1686: 
1.1       www      1687:   $r->content_type('text/html');
                   1688:   $r->send_http_header;
                   1689: 
                   1690:   $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.95      www      1691:   $r->print(&Apache::loncommon::bodytag('Resource Publication'));
1.101     www      1692: 
                   1693: 
1.2       www      1694:   my $thisfn=$fn;
1.95      www      1695: 
                   1696:   my $thistarget=$thisfn;
                   1697:       
                   1698:   $thistarget=~s/^\/home/$targetdir/;
                   1699:   $thistarget=~s/\/public\_html//;
                   1700: 
                   1701:   my $thisdistarget=$thistarget;
                   1702:   $thisdistarget=~s/^$docroot//;
                   1703: 
                   1704:   my $thisdisfn=$thisfn;
                   1705:   $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
                   1706: 
                   1707:   if ($fn=~/\/$/) {
                   1708: # -------------------------------------------------------- This is a directory
                   1709:       &publishdirectory($r,$fn,$thisdisfn);
                   1710: 
                   1711:   } else {
1.94      harris41 1712: # ---------------------- Evaluate individual file, and then output information.
1.2       www      1713:       $thisfn=~/\.(\w+)$/;
                   1714:       my $thistype=$1;
1.65      harris41 1715:       my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.2       www      1716: 
                   1717:       $r->print('<h2>Publishing '.
1.66      harris41 1718:         &Apache::loncommon::filedescription($thistype).' <tt>'.
1.95      www      1719:         '<a href="/~'.$cuname.'/'.$thisdisfn.'" target="cat">'.$thisdisfn.
1.120   ! albertel 1720:         '</a></tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><br />');
1.27      www      1721:    
1.94      harris41 1722:       if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
                   1723:           $r->print('<h3><font color="red">Co-Author: '.$cuname.' at '.$cudom.
                   1724: 		    '</font></h3>');
1.27      www      1725:       }
1.26      www      1726: 
1.65      harris41 1727:       if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1.94      harris41 1728:           $r->print('<br /><a href="/adm/diff?filename=/~'.$cuname.'/'.
1.28      www      1729:                     $thisdisfn.
1.120   ! albertel 1730:   	  '&versiontwo=priv" target="cat">Diffs with Current Version</a><br />');
1.26      www      1731:       }
1.11      www      1732:   
1.94      harris41 1733: # ------------------ Publishing from $thisfn to $thistarget with $thisembstyle.
1.2       www      1734: 
1.11      www      1735:        unless ($ENV{'form.phase'} eq 'two') {
1.113     albertel 1736: 	   my ($outstring,$error)=&publish($thisfn,$thistarget,$thisembstyle);
                   1737: 	   $r->print('<hr />'.$outstring);
1.11      www      1738:        } else {
1.100     matthew  1739:            $r->print('<hr />');
                   1740:            &phasetwo($r,$thisfn,$thistarget,$thisembstyle,$thisdistarget); 
1.113     albertel 1741:        }
1.11      www      1742:   }
1.1       www      1743:   $r->print('</body></html>');
1.15      www      1744: 
1.1       www      1745:   return OK;
                   1746: }
                   1747: 
                   1748: 1;
                   1749: __END__
                   1750: 
1.89      matthew  1751: =pod
1.66      harris41 1752: 
                   1753: =back
                   1754: 
1.89      matthew  1755: =cut
1.66      harris41 1756: 

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