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

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Publication Handler
1.54      albertel    3: #
1.236   ! www         4: # $Id: lonpublisher.pm,v 1.235 2008/05/17 12:08:45 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.65      harris41   28: ###
                     29: 
                     30: ###############################################################################
                     31: ##                                                                           ##
                     32: ## ORGANIZATION OF THIS PERL MODULE                                          ##
                     33: ##                                                                           ##
                     34: ## 1. Modules used by this module                                            ##
                     35: ## 2. Various subroutines                                                    ##
                     36: ## 3. Publication Step One                                                   ##
                     37: ## 4. Phase Two                                                              ##
                     38: ## 5. Main Handler                                                           ##
                     39: ##                                                                           ##
                     40: ###############################################################################
1.1       www        41: 
1.90      matthew    42: 
                     43: ######################################################################
                     44: ######################################################################
                     45: 
                     46: =pod 
                     47: 
1.94      harris41   48: =head1 NAME
1.90      matthew    49: 
                     50: lonpublisher - LON-CAPA publishing handler
                     51: 
1.94      harris41   52: =head1 SYNOPSIS
1.90      matthew    53: 
1.94      harris41   54: B<lonpublisher> is used by B<mod_perl> inside B<Apache>.  This is the
                     55: invocation by F<loncapa_apache.conf>:
                     56: 
                     57:   <Location /adm/publish>
                     58:   PerlAccessHandler       Apache::lonacc
                     59:   SetHandler perl-script
                     60:   PerlHandler Apache::lonpublisher
                     61:   ErrorDocument     403 /adm/login
                     62:   ErrorDocument     404 /adm/notfound.html
                     63:   ErrorDocument     406 /adm/unauthorized.html
                     64:   ErrorDocument     500 /adm/errorhandler
                     65:   </Location>
1.127     bowersj2   66: 
                     67: =head1 OVERVIEW
                     68: 
                     69: Authors can only write-access the C</~authorname/> space. They can
                     70: copy resources into the resource area through the publication step,
                     71: and move them back through a recover step. Authors do not have direct
                     72: write-access to their resource space.
                     73: 
                     74: During the publication step, several events will be
                     75: triggered. Metadata is gathered, where a wizard manages default
                     76: entries on a hierarchical per-directory base: The wizard imports the
                     77: metadata (including access privileges and royalty information) from
                     78: the most recent published resource in the current directory, and if
                     79: that is not available, from the next directory above, etc. The Network
                     80: keeps all previous versions of a resource and makes them available by
                     81: an explicit version number, which is inserted between the file name
                     82: and extension, for example C<foo.2.html>, while the most recent
                     83: version does not carry a version number (C<foo.html>). Servers
                     84: subscribing to a changed resource are notified that a new version is
                     85: available.
1.94      harris41   86: 
                     87: =head1 DESCRIPTION
                     88: 
                     89: B<lonpublisher> takes the proper steps to add resources to the LON-CAPA
1.90      matthew    90: digital library.  This includes updating the metadata table in the
                     91: LON-CAPA database.
                     92: 
1.94      harris41   93: B<lonpublisher> is many things to many people.  
1.90      matthew    94: 
                     95: This module publishes a file.  This involves gathering metadata,
                     96: versioning the file, copying file from construction space to
                     97: publication space, and copying metadata from construction space
                     98: to publication space.
                     99: 
1.94      harris41  100: =head2 SUBROUTINES
                    101: 
                    102: Many of the undocumented subroutines implement various magical
                    103: parsing shortcuts.
1.90      matthew   104: 
                    105: =over 4
                    106: 
                    107: =cut
                    108: 
                    109: ######################################################################
                    110: ######################################################################
                    111: 
                    112: 
1.1       www       113: package Apache::lonpublisher;
                    114: 
1.65      harris41  115: # ------------------------------------------------- modules used by this module
1.1       www       116: use strict;
                    117: use Apache::File;
1.13      www       118: use File::Copy;
1.2       www       119: use Apache::Constants qw(:common :http :methods);
1.76      albertel  120: use HTML::LCParser;
1.4       www       121: use Apache::lonxml;
1.27      www       122: use Apache::loncacc;
1.24      harris41  123: use DBI;
1.192     albertel  124: use Apache::lonnet;
1.65      harris41  125: use Apache::loncommon();
1.89      matthew   126: use Apache::lonmysql;
1.134     www       127: use Apache::lonlocal;
1.145     albertel  128: use Apache::loncfile;
1.166     matthew   129: use LONCAPA::lonmetadata;
1.159     www       130: use Apache::lonmsg;
1.105     www       131: use vars qw(%metadatafields %metadatakeys);
1.215     albertel  132: use LONCAPA qw(:DEFAULT :match);
1.209     www       133:  
1.2       www       134: 
1.3       www       135: my %addid;
1.5       www       136: my %nokey;
1.10      www       137: 
1.12      www       138: my $docroot;
                    139: 
1.27      www       140: my $cuname;
                    141: my $cudom;
                    142: 
1.182     www       143: my $registered_cleanup;
1.183     www       144: my $modified_urls;
1.182     www       145: 
1.233     www       146: my $lock;
                    147: 
1.90      matthew   148: =pod
                    149: 
1.94      harris41  150: =item B<metaeval>
                    151: 
                    152: Evaluates a string that contains metadata.  This subroutine
                    153: stores values inside I<%metadatafields> and I<%metadatakeys>.
                    154: The hash key is a I<$unikey> corresponding to a unique id
                    155: that is descriptive of the parser location inside the XML tree.
                    156: 
                    157: Parameters:
                    158: 
                    159: =over 4
1.90      matthew   160: 
1.94      harris41  161: =item I<$metastring>
                    162: 
                    163: A string that contains metadata.
                    164: 
                    165: =back
                    166: 
                    167: Returns:
                    168: 
                    169: nothing
1.90      matthew   170: 
                    171: =cut
                    172: 
                    173: #########################################
                    174: #########################################
1.144     www       175: #
                    176: # Modifies global %metadatafields %metadatakeys 
                    177: #
                    178: 
1.7       www       179: sub metaeval {
1.140     albertel  180:     my ($metastring,$prefix)=@_;
1.7       www       181:    
1.139     albertel  182:     my $parser=HTML::LCParser->new(\$metastring);
                    183:     my $token;
                    184:     while ($token=$parser->get_token) {
                    185: 	if ($token->[0] eq 'S') {
                    186: 	    my $entry=$token->[1];
                    187: 	    my $unikey=$entry;
1.219     albertel  188: 	    next if ($entry =~ m/^(?:parameter|stores)_/);
1.139     albertel  189: 	    if (defined($token->[2]->{'package'})) { 
1.219     albertel  190: 		$unikey.="\0package\0".$token->[2]->{'package'};
1.139     albertel  191: 	    } 
                    192: 	    if (defined($token->[2]->{'part'})) { 
1.219     albertel  193: 		$unikey.="\0".$token->[2]->{'part'}; 
1.139     albertel  194: 	    }
                    195: 	    if (defined($token->[2]->{'id'})) { 
1.219     albertel  196: 		$unikey.="\0".$token->[2]->{'id'};
1.139     albertel  197: 	    } 
                    198: 	    if (defined($token->[2]->{'name'})) { 
1.219     albertel  199: 		$unikey.="\0".$token->[2]->{'name'}; 
1.139     albertel  200: 	    }
                    201: 	    foreach (@{$token->[3]}) {
                    202: 		$metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
                    203: 		if ($metadatakeys{$unikey}) {
                    204: 		    $metadatakeys{$unikey}.=','.$_;
                    205: 		} else {
                    206: 		    $metadatakeys{$unikey}=$_;
                    207: 		}
                    208: 	    }
1.140     albertel  209: 	    my $newentry=$parser->get_text('/'.$entry);
1.174     www       210: 	    if (($entry eq 'customdistributionfile') ||
                    211: 		($entry eq 'sourcerights')) {
1.140     albertel  212: 		$newentry=~s/^\s*//;
                    213: 		if ($newentry !~m|^/res|) { $newentry=$prefix.$newentry; }
                    214: 	    }
1.149     www       215: # actually store
1.162     albertel  216: 	    if ( $entry eq 'rule' && exists($metadatafields{$unikey})) {
                    217: 		$metadatafields{$unikey}.=','.$newentry;
                    218: 	    } else {
                    219: 		$metadatafields{$unikey}=$newentry;
                    220: 	    }
1.139     albertel  221: 	}
                    222:     }
1.7       www       223: }
                    224: 
1.90      matthew   225: #########################################
                    226: #########################################
                    227: 
                    228: =pod
                    229: 
1.94      harris41  230: =item B<metaread>
1.90      matthew   231: 
                    232: Read a metadata file
                    233: 
1.94      harris41  234: Parameters:
                    235: 
                    236: =over
                    237: 
                    238: =item I<$logfile>
                    239: 
                    240: File output stream to output errors and warnings to.
                    241: 
                    242: =item I<$fn>
                    243: 
                    244: File name (including path).
                    245: 
                    246: =back
                    247: 
                    248: Returns:
                    249: 
                    250: =over 4
                    251: 
                    252: =item Scalar string (if successful)
                    253: 
                    254: XHTML text that indicates successful reading of the metadata.
                    255: 
                    256: =back
                    257: 
1.90      matthew   258: =cut
                    259: 
                    260: #########################################
                    261: #########################################
1.7       www       262: sub metaread {
1.140     albertel  263:     my ($logfile,$fn,$prefix)=@_;
1.7       www       264:     unless (-e $fn) {
1.94      harris41  265: 	print($logfile 'No file '.$fn."\n");
1.146     sakharuk  266:         return '<br /><b>'.&mt('No file').':</b> <tt>'.
1.145     albertel  267: 	    &Apache::loncfile::display($fn).'</tt>';
1.7       www       268:     }
1.94      harris41  269:     print($logfile 'Processing '.$fn."\n");
1.7       www       270:     my $metastring;
                    271:     {
1.140     albertel  272: 	my $metafh=Apache::File->new($fn);
                    273: 	$metastring=join('',<$metafh>);
1.7       www       274:     }
1.140     albertel  275:     &metaeval($metastring,$prefix);
1.147     sakharuk  276:     return '<br /><b>'.&mt('Processed file').':</b> <tt>'.
1.145     albertel  277: 	&Apache::loncfile::display($fn).'</tt>';
1.7       www       278: }
1.12      www       279: 
1.90      matthew   280: #########################################
                    281: #########################################
                    282: 
1.101     www       283: sub coursedependencies {
                    284:     my $url=&Apache::lonnet::declutter(shift);
                    285:     $url=~s/\.meta$//;
1.215     albertel  286:     my ($adomain,$aauthor)=($url=~ m{^($match_domain)/($match_username)/});
                    287:     my $regexp=quotemeta($url);
1.101     www       288:     $regexp='___'.$regexp.'___course';
                    289:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
                    290: 				       $aauthor,$regexp);
                    291:     my %courses=();
                    292:     foreach (keys %evaldata) {
                    293: 	if ($_=~/^([a-zA-Z0-9]+_[a-zA-Z0-9]+)___.+___course$/) {
                    294: 	    $courses{$1}=1;
                    295:         }
                    296:     }
                    297:     return %courses;
                    298: }
                    299: #########################################
                    300: #########################################
                    301: 
                    302: 
1.90      matthew   303: =pod
                    304: 
1.94      harris41  305: =item Form-field-generating subroutines.
                    306: 
                    307: For input parameters, these subroutines take in values
                    308: such as I<$name>, I<$value> and other form field metadata.
                    309: The output (scalar string that is returned) is an XHTML
                    310: string which presents the form field (foreseeably inside
                    311: <form></form> tags).
1.90      matthew   312: 
                    313: =over 4
                    314: 
1.94      harris41  315: =item B<textfield>
1.90      matthew   316: 
1.94      harris41  317: =item B<hiddenfield>
1.90      matthew   318: 
1.94      harris41  319: =item B<selectbox>
1.90      matthew   320: 
                    321: =back
                    322: 
                    323: =cut
                    324: 
                    325: #########################################
                    326: #########################################
1.8       www       327: sub textfield {
1.10      www       328:     my ($title,$name,$value)=@_;
1.141     www       329:     $value=~s/^\s+//gs;
                    330:     $value=~s/\s+$//gs;
                    331:     $value=~s/\s+/ /gs;
1.134     www       332:     $title=&mt($title);
1.192     albertel  333:     $env{'form.'.$name}=$value;
1.157     www       334:     return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$title:".
1.123     albertel  335:            "</b></font></p><br />".
1.94      harris41  336:            '<input type="text" name="'.$name.'" size=80 value="'.$value.'" />';
1.11      www       337: }
                    338: 
1.180     albertel  339: sub text_with_browse_field {
                    340:     my ($title,$name,$value,$restriction)=@_;
                    341:     $value=~s/^\s+//gs;
                    342:     $value=~s/\s+$//gs;
                    343:     $value=~s/\s+/ /gs;
                    344:     $title=&mt($title);
1.192     albertel  345:     $env{'form.'.$name}=$value;
1.180     albertel  346:     return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$title:".
                    347:            "</b></font></p><br />".
                    348:            '<input type="text" name="'.$name.'" size=80 value="'.$value.'" />'.
1.230     bisitz    349: 	   '<a href="javascript:openbrowser(\'pubform\',\''.$name.'\',\''.$restriction.'\');">'.&mt('Select').'</a>&nbsp;'.
                    350: 	   '<a href="javascript:opensearcher(\'pubform\',\''.$name.'\');">'.&mt('Search').'</a>';
1.180     albertel  351: 	   
                    352: }
                    353: 
1.11      www       354: sub hiddenfield {
                    355:     my ($name,$value)=@_;
1.192     albertel  356:     $env{'form.'.$name}=$value;
1.94      harris41  357:     return "\n".'<input type="hidden" name="'.$name.'" value="'.$value.'" />';
1.8       www       358: }
                    359: 
1.193     www       360: sub checkbox {
                    361:     my ($name,$text)=@_;
1.201     albertel  362:     return "\n<br /><label><input type='checkbox' name='$name' /> ".
                    363: 	&mt($text)."</label>";
1.193     www       364: }
                    365: 
1.9       www       366: sub selectbox {
1.65      harris41  367:     my ($title,$name,$value,$functionref,@idlist)=@_;
1.134     www       368:     $title=&mt($title);
1.123     albertel  369:     $value=(split(/\s*,\s*/,$value))[-1];
1.167     albertel  370:     if (defined($value)) {
1.192     albertel  371: 	$env{'form.'.$name}=$value;
1.167     albertel  372:     } else {
1.192     albertel  373: 	$env{'form.'.$name}=$idlist[0];
1.167     albertel  374:     }
1.157     www       375:     my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$title:".
1.123     albertel  376: 	'</b></font></p><br /><select name="'.$name.'">';
1.65      harris41  377:     foreach (@idlist) {
                    378:         $selout.='<option value=\''.$_.'\'';
                    379:         if ($_ eq $value) {
                    380: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
                    381: 	}
                    382:         else {$selout.='>'.&{$functionref}($_).'</option>';}
                    383:     }
1.10      www       384:     return $selout.'</select>';
1.9       www       385: }
                    386: 
1.167     albertel  387: sub select_level_form {
                    388:     my ($value,$name)=@_;
1.192     albertel  389:     $env{'form.'.$name}=$value;
                    390:     if (!defined($value)) { $env{'form.'.$name}=0; }
1.167     albertel  391:     return  &Apache::loncommon::select_level_form($value,$name);
                    392: }
1.90      matthew   393: #########################################
                    394: #########################################
                    395: 
                    396: =pod
                    397: 
1.94      harris41  398: =item B<urlfixup>
1.90      matthew   399: 
                    400: Fix up a url?  First step of publication
1.12      www       401: 
1.90      matthew   402: =cut
                    403: 
                    404: #########################################
                    405: #########################################
1.34      www       406: sub urlfixup {
1.35      www       407:     my ($url,$target)=@_;
1.39      www       408:     unless ($url) { return ''; }
1.68      albertel  409:     #javascript code needs no fixing
                    410:     if ($url =~ /^javascript:/i) { return $url; }
1.69      albertel  411:     if ($url =~ /^mailto:/i) { return $url; }
1.68      albertel  412:     #internal document links need no fixing
                    413:     if ($url =~ /^\#/) { return $url; } 
1.223     albertel  414:     my ($host)=($url=~m{(?:(?:http|https|ftp)://)*([^/]+)});
                    415:     my @lonids = &Apache::lonnet::machine_ids($host);
                    416:     if (@lonids) {
                    417: 	$url=~s{^(?:http|https|ftp)://}{};
                    418: 	$url=~s/^\Q$host\E//;
1.65      harris41  419:     }
1.223     albertel  420:     if ($url=~m{^(?:http|https|ftp)://}) { return $url; }
1.222     albertel  421:     $url=~s{\Q~$cuname\E}{res/$cudom/$cuname};
1.71      www       422:     return $url;
                    423: }
                    424: 
1.90      matthew   425: #########################################
                    426: #########################################
                    427: 
                    428: =pod
                    429: 
1.94      harris41  430: =item B<absoluteurl>
1.90      matthew   431: 
1.94      harris41  432: Currently undocumented.
1.90      matthew   433: 
                    434: =cut
1.71      www       435: 
1.90      matthew   436: #########################################
                    437: #########################################
1.71      www       438: sub absoluteurl {
                    439:     my ($url,$target)=@_;
                    440:     unless ($url) { return ''; }
1.35      www       441:     if ($target) {
                    442: 	$target=~s/\/[^\/]+$//;
                    443:        $url=&Apache::lonnet::hreflocation($target,$url);
                    444:     }
                    445:     return $url;
1.34      www       446: }
                    447: 
1.90      matthew   448: #########################################
                    449: #########################################
                    450: 
                    451: =pod
                    452: 
1.94      harris41  453: =item B<set_allow>
1.90      matthew   454: 
                    455: Currently undocumented    
                    456: 
                    457: =cut
                    458: 
                    459: #########################################
                    460: #########################################
1.81      albertel  461: sub set_allow {
                    462:     my ($allow,$logfile,$target,$tag,$oldurl)=@_;
                    463:     my $newurl=&urlfixup($oldurl,$target);
                    464:     my $return_url=$oldurl;
                    465:     print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
                    466:     if ($newurl ne $oldurl) {
                    467: 	$return_url=$newurl;
                    468: 	print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
                    469:     }
                    470:     if (($newurl !~ /^javascript:/i) &&
                    471: 	($newurl !~ /^mailto:/i) &&
1.220     albertel  472: 	($newurl !~ /^(?:http|https|ftp):/i) &&
1.81      albertel  473: 	($newurl !~ /^\#/)) {
                    474: 	$$allow{&absoluteurl($newurl,$target)}=1;
                    475:     }
1.218     raeburn   476:     return $return_url;
1.81      albertel  477: }
                    478: 
1.90      matthew   479: #########################################
                    480: #########################################
                    481: 
                    482: =pod
                    483: 
1.94      harris41  484: =item B<get_subscribed_hosts>
1.90      matthew   485: 
                    486: Currently undocumented    
                    487: 
                    488: =cut
                    489: 
                    490: #########################################
                    491: #########################################
1.85      albertel  492: sub get_subscribed_hosts {
                    493:     my ($target)=@_;
                    494:     my @subscribed;
                    495:     my $filename;
                    496:     $target=~/(.*)\/([^\/]+)$/;
                    497:     my $srcf=$2;
                    498:     opendir(DIR,$1);
1.225     albertel  499:     # cycle through listed files, subscriptions used to exist
                    500:     # as "filename.lonid"
1.85      albertel  501:     while ($filename=readdir(DIR)) {
1.216     albertel  502: 	if ($filename=~/\Q$srcf\E\.($match_lonid)$/) {
1.85      albertel  503: 	    my $subhost=$1;
1.225     albertel  504: 	    if (($subhost ne 'meta' 
                    505: 		 && $subhost ne 'subscription' 
                    506: 		 && $subhost ne 'meta.subscription'
                    507: 		 && $subhost ne 'tmp') &&
1.98      www       508:                 ($subhost ne $Apache::lonnet::perlvar{'lonHostID'})) {
1.85      albertel  509: 		push(@subscribed,$subhost);
                    510: 	    }
                    511: 	}
                    512:     }
                    513:     closedir(DIR);
                    514:     my $sh;
                    515:     if ( $sh=Apache::File->new("$target.subscription") ) {
                    516: 	while (my $subline=<$sh>) {
1.216     albertel  517: 	    if ($subline =~ /^($match_lonid):/) { 
1.98      www       518:                 if ($1 ne $Apache::lonnet::perlvar{'lonHostID'}) { 
                    519:                    push(@subscribed,$1);
                    520: 	        }
1.85      albertel  521: 	    }
                    522: 	}
                    523:     }
                    524:     return @subscribed;
                    525: }
                    526: 
1.86      albertel  527: 
1.90      matthew   528: #########################################
                    529: #########################################
                    530: 
                    531: =pod
                    532: 
1.94      harris41  533: =item B<get_max_ids_indices>
1.90      matthew   534: 
                    535: Currently undocumented    
                    536: 
                    537: =cut
                    538: 
                    539: #########################################
                    540: #########################################
1.86      albertel  541: sub get_max_ids_indices {
                    542:     my ($content)=@_;
                    543:     my $maxindex=10;
                    544:     my $maxid=10;
                    545:     my $needsfixup=0;
1.106     albertel  546:     my $duplicateids=0;
                    547: 
                    548:     my %allids;
                    549:     my %duplicatedids;
1.86      albertel  550: 
                    551:     my $parser=HTML::LCParser->new($content);
1.207     albertel  552:     $parser->xml_mode(1);
1.86      albertel  553:     my $token;
                    554:     while ($token=$parser->get_token) {
                    555: 	if ($token->[0] eq 'S') {
                    556: 	    my $counter;
                    557: 	    if ($counter=$addid{$token->[1]}) {
                    558: 		if ($counter eq 'id') {
1.186     albertel  559: 		    if (defined($token->[2]->{'id'}) &&
                    560: 			$token->[2]->{'id'} !~ /^\s*$/) {
1.86      albertel  561: 			$maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
1.106     albertel  562: 			if (exists($allids{$token->[2]->{'id'}})) {
                    563: 			    $duplicateids=1;
                    564: 			    $duplicatedids{$token->[2]->{'id'}}=1;
                    565: 			} else {
                    566: 			    $allids{$token->[2]->{'id'}}=1;
                    567: 			}
1.86      albertel  568: 		    } else {
                    569: 			$needsfixup=1;
                    570: 		    }
                    571: 		} else {
1.186     albertel  572: 		    if (defined($token->[2]->{'index'}) &&
                    573: 			$token->[2]->{'index'} !~ /^\s*$/) {
1.86      albertel  574: 			$maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
                    575: 		    } else {
                    576: 			$needsfixup=1;
                    577: 		    }
                    578: 		}
                    579: 	    }
                    580: 	}
                    581:     }
1.106     albertel  582:     return ($needsfixup,$maxid,$maxindex,$duplicateids,
                    583: 	    (keys(%duplicatedids)));
1.86      albertel  584: }
                    585: 
1.90      matthew   586: #########################################
                    587: #########################################
                    588: 
                    589: =pod
                    590: 
1.94      harris41  591: =item B<get_all_text_unbalanced>
1.90      matthew   592: 
                    593: Currently undocumented    
                    594: 
                    595: =cut
                    596: 
                    597: #########################################
                    598: #########################################
1.87      albertel  599: sub get_all_text_unbalanced {
                    600:     #there is a copy of this in lonxml.pm
                    601:     my($tag,$pars)= @_;
                    602:     my $token;
                    603:     my $result='';
                    604:     $tag='<'.$tag.'>';
                    605:     while ($token = $$pars[-1]->get_token) {
                    606: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    607: 	    $result.=$token->[1];
                    608: 	} elsif ($token->[0] eq 'PI') {
                    609: 	    $result.=$token->[2];
                    610: 	} elsif ($token->[0] eq 'S') {
                    611: 	    $result.=$token->[4];
                    612: 	} elsif ($token->[0] eq 'E')  {
                    613: 	    $result.=$token->[2];
                    614: 	}
1.177     albertel  615: 	if ($result =~ /\Q$tag\E/s) {
1.176     albertel  616: 	    ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
1.88      albertel  617: 	    #&Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
                    618: 	    #&Apache::lonnet::logthis('Result is :'.$1);
1.176     albertel  619: 	    $redo=$tag.$redo;
1.87      albertel  620: 	    push (@$pars,HTML::LCParser->new(\$redo));
                    621: 	    $$pars[-1]->xml_mode('1');
                    622: 	    last;
                    623: 	}
                    624:     }
                    625:     return $result
                    626: }
                    627: 
1.90      matthew   628: #########################################
                    629: #########################################
                    630: 
                    631: =pod
                    632: 
1.94      harris41  633: =item B<fix_ids_and_indices>
1.90      matthew   634: 
                    635: Currently undocumented    
                    636: 
                    637: =cut
                    638: 
                    639: #########################################
                    640: #########################################
1.87      albertel  641: #Arguably this should all be done as a lonnet::ssi instead
1.86      albertel  642: sub fix_ids_and_indices {
                    643:     my ($logfile,$source,$target)=@_;
                    644: 
                    645:     my %allow;
                    646:     my $content;
                    647:     {
                    648: 	my $org=Apache::File->new($source);
                    649: 	$content=join('',<$org>);
                    650:     }
                    651: 
1.106     albertel  652:     my ($needsfixup,$maxid,$maxindex,$duplicateids,@duplicatedids)=
                    653: 	&get_max_ids_indices(\$content);
1.86      albertel  654: 
1.106     albertel  655:     print $logfile ("Got $needsfixup,$maxid,$maxindex,$duplicateids--".
                    656: 			   join(', ',@duplicatedids));
                    657:     if ($duplicateids) {
                    658: 	print $logfile "Duplicate ID(s) exist, ".join(', ',@duplicatedids)."\n";
1.226     albertel  659: 	my $outstring='<span class="LC_error">'.&mt('Unable to publish file, it contains duplicated ID(s), ID(s) need to be unique. The duplicated ID(s) are').': '.join(', ',@duplicatedids).'</span>';
1.106     albertel  660: 	return ($outstring,1);
                    661:     }
1.86      albertel  662:     if ($needsfixup) {
                    663: 	print $logfile "Needs ID and/or index fixup\n".
                    664: 	    "Max ID   : $maxid (min 10)\n".
                    665:                 "Max Index: $maxindex (min 10)\n";
                    666:     }
                    667:     my $outstring='';
1.236   ! www       668:     my $responsecounter=1;
1.86      albertel  669:     my @parser;
                    670:     $parser[0]=HTML::LCParser->new(\$content);
                    671:     $parser[-1]->xml_mode(1);
                    672:     my $token;
                    673:     while (@parser) {
                    674: 	while ($token=$parser[-1]->get_token) {
                    675: 	    if ($token->[0] eq 'S') {
                    676: 		my $counter;
                    677: 		my $tag=$token->[1];
                    678: 		my $lctag=lc($tag);
                    679: 		if ($lctag eq 'allow') {
                    680: 		    $allow{$token->[2]->{'src'}}=1;
                    681: 		    next;
                    682: 		}
1.202     albertel  683: 		if ($lctag eq 'base') { next; }
1.236   ! www       684:                 if (($lctag eq 'part') || ($lctag eq 'problem')) {
        !           685:                     $responsecounter=0;
        !           686:                 }
        !           687:                 if ($lctag=~/response$/) { $responsecounter++; }
1.86      albertel  688: 		my %parms=%{$token->[2]};
                    689: 		$counter=$addid{$tag};
                    690: 		if (!$counter) { $counter=$addid{$lctag}; }
                    691: 		if ($counter) {
                    692: 		    if ($counter eq 'id') {
1.186     albertel  693: 			unless (defined($parms{'id'}) &&
                    694: 				$parms{'id'}!~/^\s*$/) {
1.86      albertel  695: 			    $maxid++;
                    696: 			    $parms{'id'}=$maxid;
1.205     albertel  697: 			    print $logfile 'ID(new) : '.$tag.':'.$maxid."\n";
                    698: 			} else {
                    699: 			    print $logfile 'ID(kept): '.$tag.':'.$parms{'id'}."\n";
1.86      albertel  700: 			}
                    701: 		    } elsif ($counter eq 'index') {
1.186     albertel  702: 			unless (defined($parms{'index'}) &&
                    703: 				$parms{'index'}!~/^\s*$/) {
1.86      albertel  704: 			    $maxindex++;
                    705: 			    $parms{'index'}=$maxindex;
                    706: 			    print $logfile 'Index: '.$tag.':'.$maxindex."\n";
                    707: 			}
                    708: 		    }
                    709: 		}
1.203     www       710:                 unless ($parms{'type'} eq 'zombie') {
                    711: 		    foreach my $type ('src','href','background','bgimg') {
                    712: 			foreach my $key (keys(%parms)) {
                    713: 			    if ($key =~ /^$type$/i) {
                    714: 				$parms{$key}=&set_allow(\%allow,$logfile,
                    715: 							$target,$tag,
                    716: 							$parms{$key});
                    717: 			    }
1.86      albertel  718: 			}
                    719: 		    }
                    720: 		}
                    721: 		# probably a <randomlabel> image type <label>
1.135     albertel  722: 		# or a <image> tag inside <imageresponse>
                    723: 		if (($lctag eq 'label' && defined($parms{'description'}))
                    724: 		    ||
                    725: 		    ($lctag eq 'image')) {
1.86      albertel  726: 		    my $next_token=$parser[-1]->get_token();
                    727: 		    if ($next_token->[0] eq 'T') {
1.218     raeburn   728:                         $next_token->[1] =~ s/[\n\r\f]+//g;
1.86      albertel  729: 			$next_token->[1]=&set_allow(\%allow,$logfile,
                    730: 						    $target,$tag,
                    731: 						    $next_token->[1]);
                    732: 		    }
                    733: 		    $parser[-1]->unget_token($next_token);
                    734: 		}
                    735: 		if ($lctag eq 'applet') {
                    736: 		    my $codebase='';
1.148     albertel  737: 		    my $havecodebase=0;
                    738: 		    foreach my $key (keys(%parms)) {
                    739: 			if (lc($key) eq 'codebase') { 
                    740: 			    $codebase=$parms{$key};
                    741: 			    $havecodebase=1; 
                    742: 			}
                    743: 		    }
                    744: 		    if ($havecodebase) {
                    745: 			my $oldcodebase=$codebase;
1.86      albertel  746: 			unless ($oldcodebase=~/\/$/) {
                    747: 			    $oldcodebase.='/';
                    748: 			}
                    749: 			$codebase=&urlfixup($oldcodebase,$target);
                    750: 			$codebase=~s/\/$//;    
                    751: 			if ($codebase ne $oldcodebase) {
                    752: 			    $parms{'codebase'}=$codebase;
                    753: 			    print $logfile 'URL codebase: '.$tag.':'.
                    754: 				$oldcodebase.' - '.
                    755: 				    $codebase."\n";
                    756: 			}
                    757: 			$allow{&absoluteurl($codebase,$target).'/*'}=1;
                    758: 		    } else {
1.148     albertel  759: 			foreach my $key (keys(%parms)) {
                    760: 			    if ($key =~ /(archive|code|object)/i) {
                    761: 				my $oldurl=$parms{$key};
1.86      albertel  762: 				my $newurl=&urlfixup($oldurl,$target);
                    763: 				$newurl=~s/\/[^\/]+$/\/\*/;
1.148     albertel  764: 				print $logfile 'Allow: applet '.lc($key).':'.
                    765: 				    $oldurl.' allows '.$newurl."\n";
1.86      albertel  766: 				$allow{&absoluteurl($newurl,$target)}=1;
                    767: 			    }
                    768: 			}
                    769: 		    }
                    770: 		}
                    771: 		my $newparmstring='';
                    772: 		my $endtag='';
                    773: 		foreach (keys %parms) {
                    774: 		    if ($_ eq '/') {
                    775: 			$endtag=' /';
                    776: 		    } else { 
                    777: 			my $quote=($parms{$_}=~/\"/?"'":'"');
                    778: 			$newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
                    779: 		    }
                    780: 		}
                    781: 		if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
                    782: 		$outstring.='<'.$tag.$newparmstring.$endtag.'>';
1.235     www       783: 		if ($lctag eq 'm' || $lctag eq 'script' || $lctag eq 'answer' 
1.131     albertel  784:                     || $lctag eq 'display' || $lctag eq 'tex') {
1.130     albertel  785: 		    $outstring.=&get_all_text_unbalanced('/'.$lctag,\@parser);
1.87      albertel  786: 		}
1.86      albertel  787: 	    } elsif ($token->[0] eq 'E') {
                    788: 		if ($token->[2]) {
                    789: 		    unless ($token->[1] eq 'allow') {
                    790: 			$outstring.='</'.$token->[1].'>';
                    791: 		    }
1.236   ! www       792:                 }
        !           793:                 if ((($token->[1] eq 'part') || ($token->[1] eq 'problem'))
        !           794:                     && (!$responsecounter)) {
        !           795:                     my $outstring='<span class="LC_error">'.&mt('Found [_1] without responses',$token->[1]).'</span>';
        !           796:                     return ($outstring,1);
        !           797:                 }
1.86      albertel  798: 	    } else {
                    799: 		$outstring.=$token->[1];
                    800: 	    }
                    801: 	}
                    802: 	pop(@parser);
                    803:     }
                    804: 
                    805:     if ($needsfixup) {
                    806: 	print $logfile "End of ID and/or index fixup\n".
                    807: 	    "Max ID   : $maxid (min 10)\n".
                    808: 		"Max Index: $maxindex (min 10)\n";
                    809:     } else {
                    810: 	print $logfile "Does not need ID and/or index fixup\n";
                    811:     }
                    812: 
1.106     albertel  813:     return ($outstring,0,%allow);
1.86      albertel  814: }
                    815: 
1.89      matthew   816: #########################################
                    817: #########################################
                    818: 
                    819: =pod
                    820: 
1.94      harris41  821: =item B<store_metadata>
1.89      matthew   822: 
                    823: Store the metadata in the metadata table in the loncapa database.
                    824: Uses lonmysql to access the database.
                    825: 
                    826: Inputs: \%metadata
                    827: 
                    828: Returns: (error,status).  error is undef on success, status is undef on error.
                    829: 
                    830: =cut
                    831: 
                    832: #########################################
                    833: #########################################
                    834: sub store_metadata {
1.151     www       835:     my %metadata = @_;
1.89      matthew   836:     my $error;
                    837:     # Determine if the table exists
                    838:     my $status = &Apache::lonmysql::check_table('metadata');
                    839:     if (! defined($status)) {
1.226     albertel  840:         $error='<span class="LC_error">WARNING: Cannot connect to '.
                    841:             'database!</span>';
1.89      matthew   842:         &Apache::lonnet::logthis($error);
                    843:         return ($error,undef);
                    844:     }
                    845:     if ($status == 0) {
                    846:         # It would be nice to actually create the table....
1.226     albertel  847:         $error ='<span class="LC_error">WARNING: The metadata table does not '.
                    848:             'exist in the LON-CAPA database.</span>';
1.89      matthew   849:         &Apache::lonnet::logthis($error);
                    850:         return ($error,undef);
                    851:     }
1.172     matthew   852:     my $dbh = &Apache::lonmysql::get_dbh();
1.152     www       853:     if (($metadata{'obsolete'}) || ($metadata{'copyright'} eq 'priv') ||
                    854: 	($metadata{'copyright'} eq 'custom')) {
1.172     matthew   855:         # remove this entry
1.228     albertel  856: 	my $delitem = 'url = '.$dbh->quote($metadata{'url'});
                    857: 	$status = &LONCAPA::lonmetadata::delete_metadata($dbh,undef,$delitem);
                    858:                                                        
1.152     www       859:     } else {
1.213     albertel  860:         $status = &LONCAPA::lonmetadata::update_metadata($dbh,undef,undef,
1.172     matthew   861:                                                          \%metadata);
1.152     www       862:     }
1.172     matthew   863:     if (defined($status) && $status ne '') {
1.226     albertel  864:         $error='<span class="LC_error">Error occured saving new values in '.
                    865:             'metadata table in LON-CAPA database</span>';
1.89      matthew   866:         &Apache::lonnet::logthis($error);
1.172     matthew   867:         &Apache::lonnet::logthis($status);
1.89      matthew   868:         return ($error,undef);
                    869:     }
1.213     albertel  870:     return (undef,'success');
1.89      matthew   871: }
                    872: 
1.142     www       873: 
1.185     www       874: # ========================================== Parse file for errors and warnings
                    875: 
                    876: sub checkonthis {
                    877:     my ($r,$source)=@_;
1.187     www       878:     my $uri=&Apache::lonnet::hreflocation($source);
                    879:     $uri=~s/\/$//;
1.190     albertel  880:     my $result=&Apache::lonnet::ssi_body($uri,
                    881: 					 ('grade_target'=>'web',
                    882: 					  'return_only_error_and_warning_counts' => 1));
                    883:     my ($errorcount,$warningcount)=split(':',$result);
1.187     www       884:     if (($errorcount) || ($warningcount)) {
                    885:         $r->print('<br /><tt>'.$uri.'</tt>: ');
                    886: 	if ($errorcount) {
1.226     albertel  887: 	    $r->print('<img src="/adm/lonMisc/bomb.gif" /><span class="LC_error"><b>'.
1.187     www       888: 		      $errorcount.' '.
1.226     albertel  889: 		      &mt('error(s)').'</b></span> ');
1.185     www       890: 	}
1.187     www       891: 	if ($warningcount) {
1.185     www       892: 	    $r->print('<font color="blue">'.
1.187     www       893: 		      $warningcount.' '.
1.185     www       894: 		      &mt('warning(s)').'</font>');
                    895: 	}
                    896:     } else {
1.190     albertel  897: 	#$r->print('<font color="green">'.&mt('ok').'</font>');
1.185     www       898:     }
                    899:     $r->rflush();
1.187     www       900:     return ($warningcount,$errorcount);
1.185     www       901: }
                    902: 
1.142     www       903: # ============================================== Parse file itself for metadata
1.144     www       904: #
                    905: # parses a file with target meta, sets global %metadatafields %metadatakeys 
1.142     www       906: 
                    907: sub parseformeta {
                    908:     my ($source,$style)=@_;
1.143     www       909:     my $allmeta='';
1.142     www       910:     if (($style eq 'ssi') || ($style eq 'prv')) {
                    911: 	my $dir=$source;
                    912: 	$dir=~s-/[^/]*$--;
                    913: 	my $file=$source;
                    914: 	$file=(split('/',$file))[-1];
                    915:         $source=&Apache::lonnet::hreflocation($dir,$file);
1.143     www       916: 	$allmeta=&Apache::lonnet::ssi_body($source,('grade_target' => 'meta'));
1.142     www       917:         &metaeval($allmeta);
                    918:     }
1.143     www       919:     return $allmeta;
1.142     www       920: }
                    921: 
1.90      matthew   922: #########################################
                    923: #########################################
                    924: 
                    925: =pod
                    926: 
1.94      harris41  927: =item B<publish>
                    928: 
                    929: This is the workhorse function of this module.  This subroutine generates
                    930: backup copies, performs any automatic processing (prior to publication,
                    931: especially for rat and ssi files),
1.90      matthew   932: 
1.113     albertel  933: Returns a 2 element array, the first is the string to be shown to the
                    934: user, the second is an error code, either 1 (an error occured) or 0
                    935: (no error occurred)
                    936: 
1.94      harris41  937: I<Additional documentation needed.>
1.90      matthew   938: 
                    939: =cut
                    940: 
                    941: #########################################
                    942: #########################################
1.2       www       943: sub publish {
1.50      www       944: 
1.97      www       945:     my ($source,$target,$style,$batch)=@_;
1.2       www       946:     my $logfile;
1.4       www       947:     my $scrout='';
1.23      www       948:     my $allmeta='';
                    949:     my $content='';
1.36      www       950:     my %allow=();
1.4       www       951: 
1.2       www       952:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.226     albertel  953: 	return ('<span class="LC_error">'.&mt('No write permission to user directory, FAIL').'</span>',1);
1.2       www       954:     }
                    955:     print $logfile 
1.211     albertel  956: "\n\n================= Publish ".localtime()." Phase One  ================\n".$env{'user.name'}.':'.$env{'user.domain'}."\n";
1.2       www       957: 
1.119     www       958:     if (($style eq 'ssi') || ($style eq 'rat') || ($style eq 'prv')) {
1.3       www       959: # ------------------------------------------------------- This needs processing
1.4       www       960: 
                    961: # ----------------------------------------------------------------- Backup Copy
1.3       www       962: 	my $copyfile=$source.'.save';
1.13      www       963:         if (copy($source,$copyfile)) {
1.3       www       964: 	    print $logfile "Copied original file to ".$copyfile."\n";
                    965:         } else {
1.13      www       966: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
1.226     albertel  967: 	    return ("<span class=\"LC_error\">Failed to write backup copy, $!,FAIL</span>",1);
1.3       www       968:         }
1.4       www       969: # ------------------------------------------------------------- IDs and indices
1.86      albertel  970: 	
1.106     albertel  971: 	my ($outstring,$error);
                    972: 	($outstring,$error,%allow)=&fix_ids_and_indices($logfile,$source,
                    973: 							$target);
1.113     albertel  974: 	if ($error) { return ($outstring,$error); }
1.36      www       975: # ------------------------------------------------------------ Construct Allows
1.62      www       976:     
1.146     sakharuk  977: 	$scrout.='<h3>'.&mt('Dependencies').'</h3>';
1.62      www       978:         my $allowstr='';
1.232     raeburn   979:         foreach my $thisdep (sort(keys(%allow))) {
1.73      albertel  980: 	   if ($thisdep !~ /[^\s]/) { next; }
1.231     www       981:            if ($thisdep =~/\$/) {
1.232     raeburn   982:               $scrout.='<br /><span class="LC_warning">'
                    983:                        .&mt('The resource depends on another resource with variable filename, i.e., [_1].','<tt>'.$thisdep.'</tt>').'<br />'
                    984:                        .&mt('You likely need to explicitly allow access to all possible dependencies using the [_1]-tag.','<tt>&lt;allow&gt;</tt>')
                    985:                        .'</span><br />';
1.231     www       986:            }
1.62      www       987:            unless ($style eq 'rat') { 
                    988:               $allowstr.="\n".'<allow src="'.$thisdep.'" />';
                    989: 	   }
1.120     albertel  990:            $scrout.='<br />';
1.231     www       991:            if ($thisdep!~/[\*\$]/ && $thisdep!~m|^/adm/|) {
1.59      www       992: 	       $scrout.='<a href="'.$thisdep.'">';
1.44      www       993:            }
1.59      www       994:            $scrout.='<tt>'.$thisdep.'</tt>';
1.231     www       995:            if ($thisdep!~/[\*\$]/ && $thisdep!~m|^/adm/|) {
1.44      www       996: 	       $scrout.='</a>';
1.59      www       997:                if (
                    998:        &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                    999:                                             $thisdep.'.meta') eq '-1') {
1.226     albertel 1000: 		   $scrout.= ' - <span class="LC_error">'.&mt('Currently not available').
                   1001: 		       '</span>';
1.59      www      1002:                } else {
                   1003:                    my %temphash=(&Apache::lonnet::declutter($target).'___'.
                   1004:                              &Apache::lonnet::declutter($thisdep).'___usage'
                   1005:                                  => time);
1.215     albertel 1006:                    $thisdep=~m{^/res/($match_domain)/($match_username)/};
1.59      www      1007:                    if ((defined($1)) && (defined($2))) {
1.92      albertel 1008:                       &Apache::lonnet::put('nohist_resevaldata',\%temphash,
                   1009: 					   $1,$2);
1.59      www      1010: 		   }
                   1011: 	       }
1.44      www      1012:            }
1.65      harris41 1013:         }
1.175     albertel 1014:         $outstring=~s/\n*(\<\/[^\>]+\>[^<]*)$/$allowstr\n$1\n/s;
1.62      www      1015: 
1.94      harris41 1016: # ------------------------------------------------------------- Write modified.
1.37      www      1017: 
1.4       www      1018:         {
                   1019:           my $org;
                   1020:           unless ($org=Apache::File->new('>'.$source)) {
                   1021:              print $logfile "No write permit to $source\n";
1.226     albertel 1022:              return ('<span class="LC_error">'.&mt('No write permission to').
1.136     www      1023: 		     ' '.$source.
1.226     albertel 1024: 		     ', '.&mt('FAIL').'</span>',1);
1.4       www      1025: 	  }
1.94      harris41 1026:           print($org $outstring);
1.4       www      1027:         }
                   1028: 	  $content=$outstring;
1.34      www      1029: 
1.37      www      1030:     }
1.94      harris41 1031: # -------------------------------------------- Initial step done, now metadata.
1.7       www      1032: 
1.94      harris41 1033: # --------------------------------------- Storage for metadata keys and fields.
1.144     www      1034: # these are globals
                   1035: #
1.8       www      1036:      %metadatafields=();
                   1037:      %metadatakeys=();
                   1038:      
                   1039:      my %oldparmstores=();
1.44      www      1040:      
1.97      www      1041:     unless ($batch) {
1.136     www      1042:      $scrout.='<h3>'.&mt('Metadata Information').' ' .
1.84      bowersj2 1043:        Apache::loncommon::help_open_topic("Metadata_Description")
                   1044:        . '</h3>';
1.97      www      1045:     }
1.7       www      1046: 
                   1047: # ------------------------------------------------ First, check out environment
1.195     www      1048:      if ((!(-e $source.'.meta')) || ($env{'form.forceoverride'})) {
1.192     albertel 1049:         $metadatafields{'author'}=$env{'environment.firstname'}.' '.
                   1050: 	                          $env{'environment.middlename'}.' '.
                   1051: 		                  $env{'environment.lastname'}.' '.
                   1052: 		                  $env{'environment.generation'};
1.8       www      1053:         $metadatafields{'author'}=~s/\s+/ /g;
                   1054:         $metadatafields{'author'}=~s/\s+$//;
1.211     albertel 1055:         $metadatafields{'owner'}=$cuname.':'.$cudom;
1.7       www      1056: 
                   1057: # ------------------------------------------------ Check out directory hierachy
                   1058: 
                   1059:         my $thisdisfn=$source;
1.122     albertel 1060:         $thisdisfn=~s/^\/home\/\Q$cuname\E\///;
1.7       www      1061: 
                   1062:         my @urlparts=split(/\//,$thisdisfn);
                   1063:         $#urlparts--;
                   1064: 
1.27      www      1065:         my $currentpath='/home/'.$cuname.'/';
1.7       www      1066: 
1.140     albertel 1067: 	my $prefix='../'x($#urlparts);
1.65      harris41 1068:         foreach (@urlparts) {
1.7       www      1069: 	    $currentpath.=$_.'/';
1.140     albertel 1070:             $scrout.=&metaread($logfile,$currentpath.'default.meta',$prefix);
                   1071: 	    $prefix=~s|^\.\./||;
1.65      harris41 1072:         }
1.185     www      1073: 
1.149     www      1074: # ----------------------------------------------------------- Parse file itself
                   1075: # read %metadatafields from file itself
                   1076:  
                   1077: 	$allmeta=&parseformeta($source,$style);
1.7       www      1078: 
                   1079: # ------------------- Clear out parameters and stores (there should not be any)
                   1080: 
1.65      harris41 1081:         foreach (keys %metadatafields) {
1.7       www      1082: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1083: 		delete $metadatafields{$_};
                   1084:             }
1.65      harris41 1085:         }
1.7       www      1086: 
1.8       www      1087:     } else {
1.7       www      1088: # ---------------------- Read previous metafile, remember parameters and stores
                   1089: 
                   1090:         $scrout.=&metaread($logfile,$source.'.meta');
                   1091: 
1.65      harris41 1092:         foreach (keys %metadatafields) {
1.7       www      1093: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1094:                 $oldparmstores{$_}=1;
                   1095: 		delete $metadatafields{$_};
                   1096:             }
1.65      harris41 1097:         }
1.195     www      1098: # ------------------------------------------------------------- Save some stuff
                   1099:         my %savemeta=();
                   1100:         foreach ('title') {
                   1101:             $savemeta{$_}=$metadatafields{$_};
                   1102: 	}
1.161     albertel 1103: # ------------------------------------------ See if anything new in file itself
                   1104:  
                   1105: 	$allmeta=&parseformeta($source,$style);
1.195     www      1106: # ----------------------------------------------------------- Restore the stuff
                   1107:         foreach (keys %savemeta) {
                   1108: 	    $metadatafields{$_}=$savemeta{$_};
                   1109: 	}
1.144     www      1110:    }
1.7       www      1111: 
1.144     www      1112:        
1.7       www      1113: # ---------------- Find and document discrepancies in the parameters and stores
                   1114: 
1.116     albertel 1115:     my $chparms='';
                   1116:     foreach (sort keys %metadatafields) {
                   1117: 	if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1118: 	    unless ($_=~/\.\w+$/) { 
                   1119: 		unless ($oldparmstores{$_}) {
1.219     albertel 1120: 		    my $disp_key = $_;
                   1121: 		    $disp_key =~ tr/\0/_/;
                   1122: 		    print $logfile ('New: '.$disp_key."\n");
                   1123: 		    $chparms .= $disp_key.' ';
1.116     albertel 1124: 		}
                   1125: 	    }
                   1126: 	}
                   1127:     }
                   1128:     if ($chparms) {
1.224     albertel 1129: 	$scrout.='<p><b>'.&mt('New parameters or saved values').
1.136     www      1130: 	    ':</b> '.$chparms.'</p>';
1.116     albertel 1131:     }
1.7       www      1132: 
1.116     albertel 1133:     $chparms='';
                   1134:     foreach (sort keys %oldparmstores) {
                   1135: 	if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1136: 	    unless (($metadatafields{$_.'.name'}) ||
                   1137: 		    ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
1.219     albertel 1138: 		my $disp_key = $_;
                   1139: 		$disp_key =~ tr/\0/_/;
                   1140: 		print $logfile ('Obsolete: '.$disp_key."\n");
                   1141: 		$chparms.=$disp_key.' ';
1.116     albertel 1142: 	    }
                   1143: 	}
                   1144:     }
                   1145:     if ($chparms) {
1.224     albertel 1146: 	$scrout.='<p><b>'.&mt('Obsolete parameters or saved values').':</b> '.
1.219     albertel 1147: 	    $chparms.'</p><h1><span class="LC_warning">'.&mt('Warning!').
                   1148: 	    '</span></h1><p><span class="LC_warning">'.
                   1149: 	    &mt('If this resource is in active use, student performance data from the previous version may become inaccessible.').'</span></p><hr />';
1.116     albertel 1150:     }
1.229     www      1151:     if ($metadatafields{'copyright'} eq 'priv') {
                   1152:         $scrout.='</p><h1><span class="LC_warning">'.&mt('Warning!').
                   1153:             '</span></h1><p><span class="LC_warning">'.
                   1154:             &mt('Copyright/distribution option "Private" is no longer supported. Select another option from below. Consider "Custom Rights" for maximum control over the usage of your resource.').'</span></p><hr />';
                   1155:     }
1.37      www      1156: 
1.8       www      1157: # ------------------------------------------------------- Now have all metadata
1.5       www      1158: 
1.116     albertel 1159:     my %keywords=();
1.97      www      1160:         
1.116     albertel 1161:     if (length($content)<500000) {
                   1162: 	my $textonly=$content;
                   1163: 	$textonly=~s/\<script[^\<]+\<\/script\>//g;
                   1164: 	$textonly=~s/\<m\>[^\<]+\<\/m\>//g;
                   1165: 	$textonly=~s/\<[^\>]*\>//g;
                   1166: 	$textonly=~tr/A-Z/a-z/;
                   1167: 	$textonly=~s/[\$\&][a-z]\w*//g;
                   1168: 	$textonly=~s/[^a-z\s]//g;
                   1169: 	
                   1170: 	foreach ($textonly=~m/(\w+)/g) {
                   1171: 	    unless ($nokey{$_}) {
                   1172: 		$keywords{$_}=1;
                   1173: 	    } 
                   1174: 	}
                   1175:     }
1.97      www      1176: 
                   1177:             
1.168     www      1178:     foreach my $addkey (split(/[\"\'\,\;]/,$metadatafields{'keywords'})) {
                   1179: 	$addkey=~s/\s+/ /g;
                   1180: 	$addkey=~s/^\s//;
                   1181: 	$addkey=~s/\s$//;
                   1182: 	if ($addkey=~/\w/) {
                   1183: 	    $keywords{$addkey}=1;
                   1184: 	}
1.116     albertel 1185:     }
1.97      www      1186: # --------------------------------------------------- Now we also have keywords
                   1187: # =============================================================================
1.167     albertel 1188: # interactive mode html goes into $intr_scrout
                   1189: # batch mode throws away this HTML
                   1190: # additionally all of the field functions have a by product of setting
1.192     albertel 1191: #   $env{'from.'..} so that it can be used by the phase two handler in
1.167     albertel 1192: #    batch mode
                   1193: 
                   1194:     my $intr_scrout.=
                   1195: 	'<form name="pubform" action="/adm/publish" method="post">'.
1.194     www      1196: 	'<p>'.($env{'form.makeobsolete'}?'':'<input type="submit" value="'.&mt('Finalize Publication').'" />').'</p>'.
1.167     albertel 1197: 	&hiddenfield('phase','two').
1.192     albertel 1198: 	&hiddenfield('filename',$env{'form.filename'}).
1.209     www      1199: 	&hiddenfield('allmeta',&escape($allmeta)).
1.194     www      1200: 	&hiddenfield('dependencies',join(',',keys %allow));
                   1201:     unless ($env{'form.makeobsolete'}) {
                   1202:        $intr_scrout.=
1.167     albertel 1203: 	&textfield('Title','title',$metadatafields{'title'}).
                   1204: 	&textfield('Author(s)','author',$metadatafields{'author'}).
                   1205: 	&textfield('Subject','subject',$metadatafields{'subject'});
1.194     www      1206:  # --------------------------------------------------- Scan content for keywords
1.7       www      1207: 
1.167     albertel 1208:     my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
                   1209:     my $KEYWORDS=&mt('Keywords');
                   1210:     my $CheckAll=&mt('check all');
                   1211:     my $UncheckAll=&mt('uncheck all');
                   1212:     my $keywordout=<<"END";
1.77      matthew  1213: <script>
1.116     albertel 1214: function checkAll(field) {
1.77      matthew  1215:     for (i = 0; i < field.length; i++)
                   1216:         field[i].checked = true ;
                   1217: }
                   1218: 
1.116     albertel 1219: function uncheckAll(field) {
1.77      matthew  1220:     for (i = 0; i < field.length; i++)
                   1221:         field[i].checked = false ;
                   1222: }
                   1223: </script>
1.146     sakharuk 1224: <p><font color="#800000" face="helvetica"><b>$KEYWORDS:</b></font>
1.123     albertel 1225:  $keywords_help</b>
1.146     sakharuk 1226: <input type="button" value="$CheckAll" onclick="javascript:checkAll(document.pubform.keywords)" /> 
                   1227: <input type="button" value="$UncheckAll" onclick="javascript:uncheckAll(document.pubform.keywords)" /> 
1.120     albertel 1228: </p>
1.77      matthew  1229: <br />
1.117     albertel 1230: END
1.167     albertel 1231:     $keywordout.='<table border="2"><tr>';
                   1232:     my $colcount=0;
1.116     albertel 1233: 
1.167     albertel 1234:     foreach (sort keys %keywords) {
1.179     matthew  1235: 	$keywordout.='<td><label><input type="checkbox" name="keywords" value="'.$_.'"';
1.167     albertel 1236: 	if ($metadatafields{'keywords'}) {
                   1237: 	    if ($metadatafields{'keywords'}=~/\Q$_\E/) {
1.120     albertel 1238: 		$keywordout.=' checked="on"';
1.192     albertel 1239: 		$env{'form.keywords'}.=$_.',';
1.116     albertel 1240: 	    }
1.167     albertel 1241: 	} elsif (&Apache::loncommon::keyword($_)) {
                   1242: 	    $keywordout.=' checked="on"';
1.192     albertel 1243: 	    $env{'form.keywords'}.=$_.',';
1.167     albertel 1244: 	}
1.179     matthew  1245: 	$keywordout.=' />'.$_.'</label></td>';
1.167     albertel 1246: 	if ($colcount>10) {
                   1247: 	    $keywordout.="</tr><tr>\n";
                   1248: 	    $colcount=0;
1.116     albertel 1249: 	}
1.167     albertel 1250: 	$colcount++;
                   1251:     }
1.192     albertel 1252:     $env{'form.keywords'}=~s/\,$//;
1.116     albertel 1253: 
1.167     albertel 1254:     $keywordout.='</tr></table>';
1.51      www      1255: 
1.167     albertel 1256:     $intr_scrout.=$keywordout;
1.9       www      1257: 
1.167     albertel 1258:     $intr_scrout.=&textfield('Additional Keywords','addkey','');
1.12      www      1259: 
1.167     albertel 1260:     $intr_scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9       www      1261: 
1.167     albertel 1262:     $intr_scrout.=
                   1263: 	"\n<p><font color=\"#800000\" face=\"helvetica\"><b>".&mt('Abstract').":".
                   1264: 	"</b></font></p><br />".
                   1265: 	'<textarea cols="80" rows="5" name="abstract">'.
                   1266: 	$metadatafields{'abstract'}.'</textarea></p>';
1.9       www      1267: 
1.167     albertel 1268:     $source=~/\.(\w+)$/;
1.150     www      1269: 
                   1270: 
1.167     albertel 1271:     $intr_scrout.=
                   1272: 	"\n<p><font color=\"#800000\" face=\"helvetica\"><b>".
                   1273: 	&mt('Lowest Grade Level').':'.
                   1274: 	"</b></font></p><br />".
                   1275: 	&select_level_form($metadatafields{'lowestgradelevel'},'lowestgradelevel').
                   1276: 	"\n<p><font color=\"#800000\" face=\"helvetica\"><b>".
                   1277: 	&mt('Highest Grade Level').':'.
                   1278: 	"</b></font></p><br />".
                   1279: 	&select_level_form($metadatafields{'highestgradelevel'},'highestgradelevel').
                   1280: 	&textfield('Standards','standards',$metadatafields{'standards'});
1.150     www      1281: 
                   1282: 
                   1283: 
1.11      www      1284: 
1.167     albertel 1285:     $intr_scrout.=&hiddenfield('mime',$1);
1.11      www      1286: 
1.167     albertel 1287:     my $defaultlanguage=$metadatafields{'language'};
                   1288:     $defaultlanguage =~ s/\s*notset\s*//g;
                   1289:     $defaultlanguage =~ s/^,\s*//g;
                   1290:     $defaultlanguage =~ s/,\s*$//g;
1.123     albertel 1291: 
1.167     albertel 1292:     $intr_scrout.=&selectbox('Language','language',
                   1293: 			     $defaultlanguage,
                   1294: 			     \&Apache::loncommon::languagedescription,
                   1295: 			     (&Apache::loncommon::languageids),
                   1296: 			     );
1.11      www      1297: 
1.167     albertel 1298:     unless ($metadatafields{'creationdate'}) {
                   1299: 	$metadatafields{'creationdate'}=time;
                   1300:     }
                   1301:     $intr_scrout.=&hiddenfield('creationdate',
                   1302: 			       &Apache::lonmysql::unsqltime($metadatafields{'creationdate'}));
1.116     albertel 1303: 
1.167     albertel 1304:     $intr_scrout.=&hiddenfield('lastrevisiondate',time);
1.11      www      1305: 
                   1306: 
1.167     albertel 1307:     $intr_scrout.=&textfield('Publisher/Owner','owner',
                   1308: 			     $metadatafields{'owner'});
1.84      bowersj2 1309: 
1.173     www      1310: # ---------------------------------------------- Retrofix for unused copyright
                   1311:     if ($metadatafields{'copyright'} eq 'free') {
                   1312: 	$metadatafields{'copyright'}='default';
                   1313: 	$metadatafields{'sourceavail'}='open';
                   1314:     }
1.229     www      1315:     if ($metadatafields{'copyright'} eq 'priv') {
                   1316:         $metadatafields{'copyright'}='domain';
                   1317:     }
1.174     www      1318: # ------------------------------------------------ Dial in reasonable defaults
1.167     albertel 1319:     my $defaultoption=$metadatafields{'copyright'};
                   1320:     unless ($defaultoption) { $defaultoption='default'; }
1.174     www      1321:     my $defaultsourceoption=$metadatafields{'sourceavail'};
                   1322:     unless ($defaultsourceoption) { $defaultsourceoption='closed'; }
1.167     albertel 1323:     unless ($style eq 'prv') {
1.174     www      1324: # -------------------------------------------------- Correct copyright for rat.
1.167     albertel 1325: 	if ($style eq 'rat') {
1.174     www      1326: # -------------------------------------- Retrofix for non-applicable copyright
1.167     albertel 1327: 	    if ($metadatafields{'copyright'} eq 'public') { 
                   1328: 		delete $metadatafields{'copyright'};
                   1329: 		$defaultoption='default';
                   1330: 	    }
                   1331: 	    $intr_scrout.=&selectbox('Copyright/Distribution','copyright',
                   1332: 				     $defaultoption,
                   1333: 				     \&Apache::loncommon::copyrightdescription,
1.229     www      1334: 				    (grep !/^(public|priv)$/,(&Apache::loncommon::copyrightids)));
1.116     albertel 1335: 	} else {
1.174     www      1336: 	    $intr_scrout.=&selectbox('Copyright/Distribution','copyright',
                   1337: 				     $defaultoption,
                   1338: 				     \&Apache::loncommon::copyrightdescription,
1.229     www      1339: 				     (grep !/^priv$/,(&Apache::loncommon::copyrightids)));
1.65      harris41 1340: 	}
1.174     www      1341: 	my $copyright_help =
                   1342: 	    Apache::loncommon::help_open_topic('Publishing_Copyright');
1.188     www      1343: 	$intr_scrout =~ s/Distribution:/'Distribution: ' . $copyright_help/ge;
1.180     albertel 1344: 	$intr_scrout.=&text_with_browse_field('Custom Distribution File','customdistributionfile',$metadatafields{'customdistributionfile'},'rights').$copyright_help;
1.174     www      1345: 	$intr_scrout.=&selectbox('Source Distribution','sourceavail',
                   1346: 				 $defaultsourceoption,
                   1347: 				 \&Apache::loncommon::source_copyrightdescription,
                   1348: 				 (&Apache::loncommon::source_copyrightids));
1.198     www      1349: #	$intr_scrout.=&text_with_browse_field('Source Custom Distribution File','sourcerights',$metadatafields{'sourcerights'},'rights');
1.174     www      1350: 	my $uctitle=&mt('Obsolete');
                   1351: 	$intr_scrout.=
1.201     albertel 1352: 	    "\n<p><label><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
1.174     www      1353: 	    '</b></font> <input type="checkbox" name="obsolete" ';
                   1354: 	if ($metadatafields{'obsolete'}) {
                   1355: 	    $intr_scrout.=' checked="1" ';
                   1356: 	}
1.201     albertel 1357: 	$intr_scrout.='/ ></label></p>'.
1.180     albertel 1358: 	    &text_with_browse_field('Suggested Replacement for Obsolete File',
                   1359: 				    'obsoletereplacement',
                   1360: 				    $metadatafields{'obsoletereplacement'});
1.174     www      1361:     } else {
                   1362: 	$intr_scrout.=&hiddenfield('copyright','private');
                   1363:     }
1.194     www      1364:    } else {
                   1365:        $intr_scrout.=
                   1366: 	&hiddenfield('title',$metadatafields{'title'}).
                   1367: 	&hiddenfield('author',$metadatafields{'author'}).
                   1368: 	&hiddenfield('subject',$metadatafields{'subject'}).
                   1369: 	&hiddenfield('keywords',$metadatafields{'keywords'}).
                   1370: 	&hiddenfield('abstract',$metadatafields{'abstract'}).
                   1371: 	&hiddenfield('notes',$metadatafields{'notes'}).
                   1372: 	&hiddenfield('mime',$metadatafields{'mime'}).
                   1373: 	&hiddenfield('creationdate',$metadatafields{'creationdate'}).
                   1374: 	&hiddenfield('lastrevisiondate',time).
                   1375: 	&hiddenfield('owner',$metadatafields{'owner'}).
                   1376: 	&hiddenfield('lowestgradelevel',$metadatafields{'lowestgradelevel'}).
                   1377: 	&hiddenfield('standards',$metadatafields{'standards'}).
                   1378: 	&hiddenfield('highestgradelevel',$metadatafields{'highestgradelevel'}).
                   1379: 	&hiddenfield('language',$metadatafields{'language'}).
                   1380: 	&hiddenfield('copyright',$metadatafields{'copyright'}).
                   1381: 	&hiddenfield('sourceavail',$metadatafields{'sourceavail'}).
                   1382: 	&hiddenfield('customdistributionfile',$metadatafields{'customdistributionfile'}).
1.195     www      1383: 	&hiddenfield('obsolete',1).
1.194     www      1384: 	&text_with_browse_field('Suggested Replacement for Obsolete File',
                   1385: 				    'obsoletereplacement',
                   1386: 				    $metadatafields{'obsoletereplacement'});
                   1387:    }
1.167     albertel 1388:     if (!$batch) {
                   1389: 	$scrout.=$intr_scrout.'<p><input type="submit" value="'.
1.194     www      1390: 	    &mt($env{'form.makeobsolete'}?'Make Obsolete':'Finalize Publication').'" /></p></form>';
1.97      www      1391:     }
1.167     albertel 1392:     return($scrout,0);
1.2       www      1393: }
1.1       www      1394: 
1.90      matthew  1395: #########################################
                   1396: #########################################
                   1397: 
                   1398: =pod 
                   1399: 
1.94      harris41 1400: =item B<phasetwo>
1.90      matthew  1401: 
                   1402: Render second interface showing status of publication steps.
                   1403: This is publication step two.
                   1404: 
1.94      harris41 1405: Parameters:
                   1406: 
                   1407: =over 4
                   1408: 
                   1409: =item I<$source>
                   1410: 
                   1411: =item I<$target>
                   1412: 
                   1413: =item I<$style>
                   1414: 
                   1415: =item I<$distarget>
                   1416: 
                   1417: =back
                   1418: 
                   1419: Returns:
                   1420: 
                   1421: =over 4
                   1422: 
1.197     www      1423: =item integer
1.94      harris41 1424: 
1.197     www      1425: 0: fail
                   1426: 1: success
1.94      harris41 1427: 
1.90      matthew  1428: =cut
1.12      www      1429: 
1.100     matthew  1430: #'stupid emacs
1.90      matthew  1431: #########################################
                   1432: #########################################
1.11      www      1433: sub phasetwo {
                   1434: 
1.100     matthew  1435:     my ($r,$source,$target,$style,$distarget,$batch)=@_;
1.102     www      1436:     $source=~s/\/+/\//g;
                   1437:     $target=~s/\/+/\//g;
1.196     www      1438: #
                   1439: # Unless trying to get rid of something, check name validity
                   1440: #
                   1441:     unless ($env{'form.obsolete'}) {
                   1442: 	if ($target=~/(\_\_\_|\&\&\&|\:\:\:)/) {
1.226     albertel 1443: 	    $r->print('<span class="LC_error">'.
                   1444: 		      &mt('Unsupported character combination [_1] in filename, FAIL.',"<tt>'.$1.'</tt>").
                   1445: 		      '</span>');
1.196     www      1446: 	    return 0;
                   1447: 	}
                   1448: 	unless ($target=~/\.(\w+)$/) {
1.226     albertel 1449: 	    $r->print('<span class="LC_error">'.&mt('No valid extension found in filename, FAIL').'</span>');
1.196     www      1450: 	    return 0;
                   1451: 	}
                   1452: 	if ($target=~/\.(\d+)\.(\w+)$/) {
1.226     albertel 1453: 	    $r->print('<span class="LC_error">'.&mt('Cannot publish versioned resource, FAIL').'</span>');
1.196     www      1454: 	    return 0;
                   1455: 	}
                   1456:     }
1.109     www      1457: 
1.196     www      1458: #
                   1459: # End name check
                   1460: #
1.102     www      1461:     $distarget=~s/\/+/\//g;
1.11      www      1462:     my $logfile;
                   1463:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.110     www      1464: 	$r->print(
1.226     albertel 1465:         '<span class="LC_error">'.
                   1466: 		&mt('No write permission to user directory, FAIL').'</span>');
1.110     www      1467:         return 0;
1.11      www      1468:     }
1.227     albertel 1469:     
                   1470:     if ($source =~ /\.rights$/) {
                   1471: 	$r->print('<p><span class="LC_warning">'.&mt('Warning: It can take up to 1 hour for rights changes to fully propagate.').'</span></p>');
                   1472:     }
                   1473: 
1.11      www      1474:     print $logfile 
1.211     albertel 1475:         "\n================= Publish ".localtime()." Phase Two  ================\n".$env{'user.name'}.':'.$env{'user.domain'}."\n";
1.100     matthew  1476:     
                   1477:     %metadatafields=();
                   1478:     %metadatakeys=();
1.167     albertel 1479: 
1.209     www      1480:     &metaeval(&unescape($env{'form.allmeta'}));
1.100     matthew  1481:     
1.192     albertel 1482:     $metadatafields{'title'}=$env{'form.title'};
                   1483:     $metadatafields{'author'}=$env{'form.author'};
                   1484:     $metadatafields{'subject'}=$env{'form.subject'};
                   1485:     $metadatafields{'notes'}=$env{'form.notes'};
                   1486:     $metadatafields{'abstract'}=$env{'form.abstract'};
                   1487:     $metadatafields{'mime'}=$env{'form.mime'};
                   1488:     $metadatafields{'language'}=$env{'form.language'};
                   1489:     $metadatafields{'creationdate'}=$env{'form.creationdate'};
                   1490:     $metadatafields{'lastrevisiondate'}=$env{'form.lastrevisiondate'};
                   1491:     $metadatafields{'owner'}=$env{'form.owner'};
                   1492:     $metadatafields{'copyright'}=$env{'form.copyright'};
                   1493:     $metadatafields{'standards'}=$env{'form.standards'};
                   1494:     $metadatafields{'lowestgradelevel'}=$env{'form.lowestgradelevel'};
                   1495:     $metadatafields{'highestgradelevel'}=$env{'form.highestgradelevel'};
1.115     www      1496:     $metadatafields{'customdistributionfile'}=
1.192     albertel 1497:                                  $env{'form.customdistributionfile'};
                   1498:     $metadatafields{'sourceavail'}=$env{'form.sourceavail'};
                   1499:     $metadatafields{'obsolete'}=$env{'form.obsolete'};
1.138     www      1500:     $metadatafields{'obsoletereplacement'}=
1.192     albertel 1501: 	                        $env{'form.obsoletereplacement'};
                   1502:     $metadatafields{'dependencies'}=$env{'form.dependencies'};
1.211     albertel 1503:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
1.192     albertel 1504: 	                                 $env{'user.domain'};
1.211     albertel 1505:     $metadatafields{'authorspace'}=$cuname.':'.$cudom;
1.214     albertel 1506:     $metadatafields{'domain'}=$cudom;
1.100     matthew  1507:     
1.192     albertel 1508:     my $allkeywords=$env{'form.addkey'};
                   1509:     if (exists($env{'form.keywords'})) {
                   1510:         if (ref($env{'form.keywords'})) {
                   1511:             $allkeywords .= ','.join(',',@{$env{'form.keywords'}});
1.100     matthew  1512:         } else {
1.192     albertel 1513:             $allkeywords .= ','.$env{'form.keywords'};
1.100     matthew  1514:         }
                   1515:     }
1.168     www      1516:     $allkeywords=~s/[\"\']//g;
1.170     www      1517:     $allkeywords=~s/\s*[\;\,]\s*/\,/g;
1.168     www      1518:     $allkeywords=~s/\s+/ /g;
                   1519:     $allkeywords=~s/^[ \,]//;
                   1520:     $allkeywords=~s/[ \,]$//;
1.100     matthew  1521:     $metadatafields{'keywords'}=$allkeywords;
                   1522:     
1.149     www      1523: # check if custom distribution file is specified
                   1524:     if ($metadatafields{'copyright'} eq 'custom') {
                   1525: 	my $file=$metadatafields{'customdistributionfile'};
                   1526: 	unless ($file=~/\.rights$/) {
1.197     www      1527:             $r->print(
1.226     albertel 1528:                 '<span class="LC_error">'.&mt('No valid custom distribution rights file specified, FAIL').
                   1529: 		'</span>');
1.197     www      1530: 	    return 0;
1.149     www      1531:         }
                   1532:     }
1.100     matthew  1533:     {
                   1534:         print $logfile "\nWrite metadata file for ".$source;
                   1535:         my $mfh;
                   1536:         unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
1.197     www      1537:             $r->print( 
1.226     albertel 1538:                 '<span class="LC_error">'.&mt('Could not write metadata, FAIL').
                   1539: 		'</span>');
1.197     www      1540: 	    return 0;
1.100     matthew  1541:         }
                   1542:         foreach (sort keys %metadatafields) {
                   1543:             unless ($_=~/\./) {
                   1544:                 my $unikey=$_;
                   1545:                 $unikey=~/^([A-Za-z]+)/;
                   1546:                 my $tag=$1;
                   1547:                 $tag=~tr/A-Z/a-z/;
                   1548:                 print $mfh "\n\<$tag";
                   1549:                 foreach (split(/\,/,$metadatakeys{$unikey})) {
                   1550:                     my $value=$metadatafields{$unikey.'.'.$_};
                   1551:                     $value=~s/\"/\'\'/g;
                   1552:                     print $mfh ' '.$_.'="'.$value.'"';
                   1553:                 }
                   1554:                 print $mfh '>'.
1.165     albertel 1555:                     &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
1.100     matthew  1556:                         .'</'.$tag.'>';
                   1557:             }
                   1558:         }
1.136     www      1559:         $r->print('<p>'.&mt('Wrote Metadata').'</p>');
1.100     matthew  1560:         print $logfile "\nWrote metadata";
                   1561:     }
                   1562:     
                   1563: # -------------------------------- Synchronize entry with SQL metadata database
1.12      www      1564: 
1.89      matthew  1565:     $metadatafields{'url'} = $distarget;
                   1566:     $metadatafields{'version'} = 'current';
1.152     www      1567: 
                   1568:     my ($error,$success) = &store_metadata(%metadatafields);
                   1569:     if ($success) {
                   1570: 	$r->print('<p>'.&mt('Synchronized SQL metadata database').'</p>');
                   1571: 	print $logfile "\nSynchronized SQL metadata database";
1.89      matthew  1572:     } else {
1.152     www      1573: 	$r->print($error);
                   1574: 	print $logfile "\n".$error;
1.24      harris41 1575:     }
1.159     www      1576: # --------------------------------------------- Delete author resource messages
                   1577:     my $delresult=&Apache::lonmsg::del_url_author_res_msg($target); 
                   1578:     $r->print('<p>'.&mt('Removing error messages:').' '.$delresult.'</p>');
                   1579:     print $logfile "\nRemoving error messages: $delresult";
1.12      www      1580: # ----------------------------------------------------------- Copy old versions
                   1581:    
1.100     matthew  1582:     if (-e $target) {
                   1583:         my $filename;
                   1584:         my $maxversion=0;
                   1585:         $target=~/(.*)\/([^\/]+)\.(\w+)$/;
                   1586:         my $srcf=$2;
                   1587:         my $srct=$3;
                   1588:         my $srcd=$1;
                   1589:         unless ($srcd=~/^\/home\/httpd\/html\/res/) {
                   1590:             print $logfile "\nPANIC: Target dir is ".$srcd;
1.197     www      1591:             $r->print(
1.226     albertel 1592: 	 "<span class=\"LC_error\">Invalid target directory, FAIL</span>");
1.197     www      1593: 	    return 0;
1.100     matthew  1594:         }
                   1595:         opendir(DIR,$srcd);
                   1596:         while ($filename=readdir(DIR)) {
                   1597:             if (-l $srcd.'/'.$filename) {
                   1598:                 unlink($srcd.'/'.$filename);
                   1599:                 unlink($srcd.'/'.$filename.'.meta');
                   1600:             } else {
1.118     albertel 1601:                 if ($filename=~/\Q$srcf\E\.(\d+)\.\Q$srct\E$/) {
1.100     matthew  1602:                     $maxversion=($1>$maxversion)?$1:$maxversion;
                   1603:                 }
                   1604:             }
                   1605:         }
                   1606:         closedir(DIR);
                   1607:         $maxversion++;
1.120     albertel 1608:         $r->print('<p>Creating old version '.$maxversion.'</p>');
1.125     www      1609:         print $logfile "\nCreating old version ".$maxversion."\n";
1.100     matthew  1610:         
                   1611:         my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
                   1612:         
1.13      www      1613:         if (copy($target,$copyfile)) {
1.12      www      1614: 	    print $logfile "Copied old target to ".$copyfile."\n";
1.136     www      1615:             $r->print('<p>'.&mt('Copied old target file').'</p>');
1.12      www      1616:         } else {
1.13      www      1617: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
1.226     albertel 1618:             $r->print("<span class=\"LC_error\">".&mt('Failed to copy old target').
                   1619: 		", $!, ".&mt('FAIL')."</span>");
1.197     www      1620: 	    return 0;
1.12      www      1621:         }
1.100     matthew  1622:         
1.12      www      1623: # --------------------------------------------------------------- Copy Metadata
                   1624: 
                   1625: 	$copyfile=$copyfile.'.meta';
1.100     matthew  1626:         
1.13      www      1627:         if (copy($target.'.meta',$copyfile)) {
1.14      www      1628: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
1.136     www      1629:             $r->print('<p>'.&mt('Copied old metadata').'</p>')
1.12      www      1630:         } else {
1.13      www      1631: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14      www      1632:             if (-e $target.'.meta') {
1.197     www      1633:                 $r->print( 
1.226     albertel 1634:                     "<span class=\"LC_error\">".
                   1635: &mt('Failed to write old metadata copy').", $!, ".&mt('FAIL')."</span>");
1.197     www      1636: 		return 0;
1.14      www      1637: 	    }
1.12      www      1638:         }
1.100     matthew  1639:         
                   1640:         
                   1641:     } else {
1.138     www      1642:         $r->print('<p>'.&mt('Initial version').'</p>');
1.100     matthew  1643:         print $logfile "\nInitial version";
                   1644:     }
1.12      www      1645: 
                   1646: # ---------------------------------------------------------------- Write Source
1.100     matthew  1647:     my $copyfile=$target;
                   1648:     
                   1649:     my @parts=split(/\//,$copyfile);
                   1650:     my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1651:     
                   1652:     my $count;
                   1653:     for ($count=5;$count<$#parts;$count++) {
                   1654:         $path.="/$parts[$count]";
                   1655:         if ((-e $path)!=1) {
                   1656:             print $logfile "\nCreating directory ".$path;
1.136     www      1657:             $r->print('<p>'.&mt('Created directory').' '.$parts[$count].'</p>');
1.100     matthew  1658:             mkdir($path,0777);
1.12      www      1659:         }
1.100     matthew  1660:     }
                   1661:     
                   1662:     if (copy($source,$copyfile)) {
                   1663:         print $logfile "\nCopied original source to ".$copyfile."\n";
1.136     www      1664:         $r->print('<p>'.&mt('Copied source file').'</p>');
1.100     matthew  1665:     } else {
                   1666:         print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
1.226     albertel 1667:         $r->print("<span class=\"LC_error\">".
                   1668: 	    &mt('Failed to copy source').", $!, ".&mt('FAIL')."</span>");
1.197     www      1669: 	return 0;
1.100     matthew  1670:     }
                   1671:     
1.12      www      1672: # --------------------------------------------------------------- Copy Metadata
                   1673: 
1.100     matthew  1674:     $copyfile=$copyfile.'.meta';
                   1675:     
                   1676:     if (copy($source.'.meta',$copyfile)) {
                   1677:         print $logfile "\nCopied original metadata to ".$copyfile."\n";
1.136     www      1678:         $r->print('<p>'.&mt('Copied metadata').'</p>');
1.100     matthew  1679:     } else {
                   1680:         print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";
1.197     www      1681:         $r->print(
1.226     albertel 1682:             "<span class=\"LC_error\">".&mt('Failed to write metadata copy').", $!, ".&mt('FAIL')."</span>");
1.197     www      1683: 	return 0;
1.100     matthew  1684:     }
                   1685:     $r->rflush;
1.12      www      1686: 
1.181     www      1687: # ------------------------------------------------------------- Trigger updates
1.183     www      1688:     push(@{$modified_urls},[$target,$source]);
1.182     www      1689:     unless ($registered_cleanup) {
                   1690: 	$r->register_cleanup(\&notify);
                   1691: 	$registered_cleanup=1;
                   1692:     }
1.199     www      1693: 
                   1694: # ---------------------------------------------------------- Clear local caches
                   1695:     my $thisdistarget=$target;
                   1696:     $thisdistarget=~s/^\Q$docroot\E//;
                   1697:     &Apache::lonnet::devalidate_cache_new('resversion',$target);
                   1698:     &Apache::lonnet::devalidate_cache_new('meta',
                   1699: 			 &Apache::lonnet::declutter($thisdistarget));
                   1700: 
1.12      www      1701: # ------------------------------------------------ Provide link to new resource
1.100     matthew  1702:     unless ($batch) {
                   1703:         
                   1704:         my $thissrc=$source;
1.215     albertel 1705:         $thissrc=~s{^/home/($match_username)/public_html}{/priv/$1};
1.100     matthew  1706:         
                   1707:         my $thissrcdir=$thissrc;
                   1708:         $thissrcdir=~s/\/[^\/]+$/\//;
                   1709:         
                   1710:         
                   1711:         $r->print(
1.120     albertel 1712:            '<hr /><a href="'.$thisdistarget.'"><font size="+2">'.
1.138     www      1713:            &mt('View Published Version').'</font></a>'.
                   1714:            '<p><a href="'.$thissrc.'"><font size=+2>'.
                   1715: 		  &mt('Back to Source').'</font></a></p>'.
1.100     matthew  1716:            '<p><a href="'.$thissrcdir.
1.138     www      1717:                    '"><font size="+2">'.
                   1718: 		  &mt('Back to Source Directory').'</font></a></p>');
1.100     matthew  1719:     }
1.181     www      1720:     $logfile->close();
1.197     www      1721:     $r->print('<p><font color="green">'.&mt('Done').'</font></p>');
                   1722:     return 1;
1.11      www      1723: }
                   1724: 
1.181     www      1725: # =============================================================== Notifications
                   1726: sub notify {  
                   1727: # --------------------------------------------------- Send update notifications
1.183     www      1728:     foreach my $targetsource (@{$modified_urls}){
1.182     www      1729: 	my ($target,$source)=@{$targetsource};
                   1730: 	my $logfile=Apache::File->new('>>'.$source.'.log');
                   1731: 	print $logfile "\nCleanup phase: Notifications\n";
                   1732: 	my @subscribed=&get_subscribed_hosts($target);
                   1733: 	foreach my $subhost (@subscribed) {
                   1734: 	    print $logfile "\nNotifying host ".$subhost.':';
                   1735: 	    my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
                   1736: 	    print $logfile $reply;
                   1737: 	}
1.181     www      1738: # ---------------------------------------- Send update notifications, meta only
1.182     www      1739: 	my @subscribedmeta=&get_subscribed_hosts("$target.meta");
                   1740: 	foreach my $subhost (@subscribedmeta) {
                   1741: 	    print $logfile "\nNotifying host for metadata only ".$subhost.':';
                   1742: 	    my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
                   1743: 						$subhost);
                   1744: 	    print $logfile $reply;
                   1745: 	} 
1.181     www      1746: # --------------------------------------------------- Notify subscribed courses
1.182     www      1747: 	my %courses=&coursedependencies($target);
                   1748: 	my $now=time;
                   1749: 	foreach (keys %courses) {
                   1750: 	    print $logfile "\nNotifying course ".$_.':';
                   1751: 	    my ($cdom,$cname)=split(/\_/,$_);
                   1752: 	    my $reply=&Apache::lonnet::cput
                   1753: 		('versionupdate',{$target => $now},$cdom,$cname);
                   1754: 	    print $logfile $reply;
                   1755: 	}
                   1756: 	print $logfile "\n============ Done ============\n";
                   1757: 	$logfile->close();
1.181     www      1758:     }
1.233     www      1759:     if ($lock) { &Apache::lonnet::remove_lock($lock); }
1.182     www      1760:     return OK;
1.181     www      1761: }
                   1762: 
1.95      www      1763: #########################################
                   1764: 
                   1765: sub batchpublish {
1.97      www      1766:     my ($r,$srcfile,$targetfile)=@_;
1.192     albertel 1767:     #publication pollutes %env with form.* values
                   1768:     my %oldenv=%env;
1.102     www      1769:     $srcfile=~s/\/+/\//g;
                   1770:     $targetfile=~s/\/+/\//g;
1.95      www      1771:     my $thisdisfn=$srcfile;
                   1772:     $thisdisfn=~s/\/home\/korte\/public_html\///;
                   1773:     $srcfile=~s/\/+/\//g;
1.96      www      1774: 
1.97      www      1775:     my $docroot=$r->dir_config('lonDocRoot');
                   1776:     my $thisdistarget=$targetfile;
1.122     albertel 1777:     $thisdistarget=~s/^\Q$docroot\E//;
1.97      www      1778: 
1.96      www      1779: 
1.139     albertel 1780:     %metadatafields=();
                   1781:     %metadatakeys=();
                   1782:     $srcfile=~/\.(\w+)$/;
                   1783:     my $thistype=$1;
1.97      www      1784: 
                   1785: 
1.139     albertel 1786:     my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.96      www      1787:      
1.138     www      1788:     $r->print('<h2>'.&mt('Publishing').' <tt>'.$thisdisfn.'</tt></h2>');
1.97      www      1789: 
                   1790: # phase one takes
                   1791: #  my ($source,$target,$style,$batch)=@_;
1.113     albertel 1792:     my ($outstring,$error)=&publish($srcfile,$targetfile,$thisembstyle,1);
                   1793:     $r->print('<p>'.$outstring.'</p>');
1.96      www      1794: # phase two takes
                   1795: # my ($source,$target,$style,$distarget,batch)=@_;
1.192     albertel 1796: # $env{'form.allmeta'},$env{'form.title'},$env{'form.author'},...
1.113     albertel 1797:     if (!$error) {
                   1798: 	$r->print('<p>');
                   1799: 	&phasetwo($r,$srcfile,$targetfile,$thisembstyle,$thisdistarget,1);
                   1800: 	$r->print('</p>');
                   1801:     }
1.192     albertel 1802:     %env=%oldenv;
1.97      www      1803:     return '';
1.95      www      1804: }
1.1       www      1805: 
1.90      matthew  1806: #########################################
1.95      www      1807: 
                   1808: sub publishdirectory {
                   1809:     my ($r,$fn,$thisdisfn)=@_;
1.102     www      1810:     $fn=~s/\/+/\//g;
                   1811:     $thisdisfn=~s/\/+/\//g;
1.96      www      1812:     my $resdir=
1.139     albertel 1813: 	$Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cudom.'/'.$cuname.'/'.
                   1814: 	$thisdisfn;
1.156     www      1815:     $r->print('<h1>'.&mt('Directory').' <tt>'.$thisdisfn.'</tt></h1>'.
                   1816: 	      &mt('Target').': <tt>'.$resdir.'</tt><br />');
1.139     albertel 1817: 
                   1818:     my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
1.193     www      1819:     unless ($env{'form.phase'} eq 'two') {
                   1820: # ask user what they want
                   1821:         $r->print('<form name="pubdirpref" method="post">'.
                   1822: 		  &hiddenfield('phase','two').
                   1823: 		  &hiddenfield('filename',$env{'form.filename'}).
                   1824: 		  &checkbox('pubrec','include subdirectories').
1.194     www      1825: 		  &checkbox('forcerepub','force republication of previously published files').
1.206     albertel 1826:                   &checkbox('obsolete','make file(s) obsolete').
1.195     www      1827: 		  &checkbox('forceoverride','force directory level catalog information over existing').
                   1828: 		  '<br /><input type="submit" value="'.&mt('Publish Directory').'" /></form>');
1.233     www      1829:         $lock=0;
1.193     www      1830:     } else {
1.234     www      1831:         unless ($lock) { $lock=&Apache::lonnet::set_lock(&mt('Publishing [_1]',$fn)); }
1.193     www      1832: # actually publish things
                   1833: 	opendir(DIR,$fn);
                   1834: 	my @files=sort(readdir(DIR));
                   1835: 	foreach my $filename (@files) {
                   1836: 	    my ($cdev,$cino,$cmode,$cnlink,
                   1837: 		$cuid,$cgid,$crdev,$csize,
                   1838: 		$catime,$cmtime,$cctime,
                   1839: 		$cblksize,$cblocks)=stat($fn.'/'.$filename);
                   1840: 	    
                   1841: 	    my $extension='';
                   1842: 	    if ($filename=~/\.(\w+)$/) { $extension=$1; }
                   1843: 	    if ($cmode&$dirptr) {
                   1844: 		if (($filename!~/^\./) && ($env{'form.pubrec'})) {
                   1845: 		    &publishdirectory($r,$fn.'/'.$filename,$thisdisfn.'/'.$filename);
                   1846: 		}
                   1847: 	    } elsif ((&Apache::loncommon::fileembstyle($extension) ne 'hdn') &&
                   1848: 		     ($filename!~/^[\#\.]/) && ($filename!~/\~$/)) {
1.96      www      1849: # find out publication status and/or exiting metadata
1.193     www      1850: 		my $publishthis=0;
                   1851: 		if (-e $resdir.'/'.$filename) {
                   1852: 		    my ($rdev,$rino,$rmode,$rnlink,
                   1853: 			$ruid,$rgid,$rrdev,$rsize,
                   1854: 			$ratime,$rmtime,$rctime,
                   1855: 			$rblksize,$rblocks)=stat($resdir.'/'.$filename);
                   1856: 		    if (($rmtime<$cmtime) || ($env{'form.forcerepub'})) {
1.96      www      1857: # previously published, modified now
1.193     www      1858: 			$publishthis=1;
                   1859: 		    }
1.212     albertel 1860: 		    my $meta_cmtime = (stat($fn.'/'.$filename.'.meta'))[9];
                   1861: 		    my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
                   1862: 		    if ( $meta_rmtime<$meta_cmtime ) {
                   1863: 			$publishthis=1;
                   1864: 		    }
1.193     www      1865: 		} else {
                   1866: # never published
1.96      www      1867: 		    $publishthis=1;
1.193     www      1868: 		}
1.212     albertel 1869: 		
1.193     www      1870: 		if ($publishthis) {
                   1871: 		    &batchpublish($r,$fn.'/'.$filename,$resdir.'/'.$filename);
                   1872: 		} else {
                   1873: 		    $r->print('<br />'.&mt('Skipping').' '.$filename.'<br />');
                   1874: 		}
                   1875: 		$r->rflush();
1.139     albertel 1876: 	    }
                   1877: 	}
1.193     www      1878: 	closedir(DIR);
1.139     albertel 1879:     }
1.95      www      1880: }
1.160     www      1881: 
                   1882: #########################################
                   1883: # publish a default.meta file
                   1884: 
                   1885: sub defaultmetapublish {
                   1886:     my ($r,$fn,$cuname,$cudom)=@_;
                   1887:     $fn=~s/^\/\~$cuname\//\/home\/$cuname\/public_html\//;
                   1888:     unless (-e $fn) {
                   1889:        return HTTP_NOT_FOUND;
                   1890:     }
                   1891:     my $target=$fn;
                   1892:     $target=~s/^\/home\/$cuname\/public_html\//$Apache::lonnet::perlvar{'lonDocRoot'}\/res\/$cudom\/$cuname\//;
                   1893: 
                   1894: 
                   1895:     &Apache::loncommon::content_type($r,'text/html');
                   1896:     $r->send_http_header;
                   1897: 
1.208     albertel 1898:     $r->print(&Apache::loncommon::start_page('Catalog Information Publication'));
1.160     www      1899: 
                   1900: # ---------------------------------------------------------------- Write Source
                   1901:     my $copyfile=$target;
                   1902:     
                   1903:     my @parts=split(/\//,$copyfile);
                   1904:     my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1905:     
                   1906:     my $count;
                   1907:     for ($count=5;$count<$#parts;$count++) {
                   1908:         $path.="/$parts[$count]";
                   1909:         if ((-e $path)!=1) {
                   1910:             $r->print('<p>'.&mt('Created directory').' '.$parts[$count].'</p>');
                   1911:             mkdir($path,0777);
                   1912:         }
                   1913:     }
                   1914:     
                   1915:     if (copy($fn,$copyfile)) {
                   1916:         $r->print('<p>'.&mt('Copied source file').'</p>');
                   1917:     } else {
1.226     albertel 1918:         return "<span class=\"LC_error\">".
                   1919: 	    &mt('Failed to copy source').", $!, ".&mt('FAIL')."</span>";
1.160     www      1920:     }
                   1921: 
                   1922: # --------------------------------------------------- Send update notifications
                   1923: 
                   1924:     my @subscribed=&get_subscribed_hosts($target);
                   1925:     foreach my $subhost (@subscribed) {
                   1926: 	$r->print('<p>'.&mt('Notifying host').' '.$subhost.':');$r->rflush;
                   1927: 	my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
                   1928: 	$r->print($reply.'</p><br />');$r->rflush;
                   1929:     }
                   1930: # ------------------------------------------------------------------- Link back
                   1931:     my $link=$fn;
                   1932:     $link=~s/^\/home\/$cuname\/public_html\//\/priv\/$cuname\//;
                   1933:     $r->print("<a href='$link'>".&mt('Back to Catalog Information').'</a>');
1.208     albertel 1934:     $r->print(&Apache::loncommon::end_page());
1.160     www      1935:     return OK;
                   1936: }
1.90      matthew  1937: #########################################
                   1938: 
                   1939: =pod
                   1940: 
1.94      harris41 1941: =item B<handler>
1.90      matthew  1942: 
                   1943: A basic outline of the handler subroutine follows.
                   1944: 
                   1945: =over 4
                   1946: 
1.94      harris41 1947: =item *
                   1948: 
                   1949: Get query string for limited number of parameters.
                   1950: 
                   1951: =item *
                   1952: 
                   1953: Check filename.
                   1954: 
                   1955: =item *
                   1956: 
                   1957: File is there and owned, init lookup tables.
                   1958: 
                   1959: =item *
1.90      matthew  1960: 
1.94      harris41 1961: Start page output.
1.90      matthew  1962: 
1.94      harris41 1963: =item *
1.90      matthew  1964: 
1.94      harris41 1965: Evaluate individual file, and then output information.
1.90      matthew  1966: 
1.94      harris41 1967: =item *
1.90      matthew  1968: 
1.94      harris41 1969: Publishing from $thisfn to $thistarget with $thisembstyle.
1.90      matthew  1970: 
                   1971: =back
                   1972: 
                   1973: =cut
                   1974: 
                   1975: #########################################
                   1976: #########################################
1.1       www      1977: sub handler {
1.139     albertel 1978:     my $r=shift;
1.2       www      1979: 
1.139     albertel 1980:     if ($r->header_only) {
                   1981: 	&Apache::loncommon::content_type($r,'text/html');
                   1982: 	$r->send_http_header;
                   1983: 	return OK;
                   1984:     }
1.2       www      1985: 
1.43      www      1986: # Get query string for limited number of parameters
                   1987: 
1.80      matthew  1988:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1989:                                             ['filename']);
1.43      www      1990: 
1.183     www      1991: # -------------------------------------- Flag and buffer for registered cleanup
1.182     www      1992:     $registered_cleanup=0;
1.183     www      1993:     @{$modified_urls}=();
1.2       www      1994: # -------------------------------------------------------------- Check filename
                   1995: 
1.209     www      1996:     my $fn=&unescape($env{'form.filename'});
1.160     www      1997: 
                   1998:     ($cuname,$cudom)=
                   1999: 	&Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                   2000: 
                   2001: # special publication: default.meta file
                   2002:     if ($fn=~/\/default.meta$/) {
                   2003: 	return &defaultmetapublish($r,$fn,$cuname,$cudom); 
                   2004:     }
1.159     www      2005:     $fn=~s/\.meta$//;
1.27      www      2006:   
1.139     albertel 2007:     unless ($fn) { 
                   2008: 	$r->log_reason($cuname.' at '.$cudom.
                   2009: 		       ' trying to publish empty filename', $r->filename); 
                   2010: 	return HTTP_NOT_FOUND;
                   2011:     } 
                   2012: 
                   2013:     unless (($cuname) && ($cudom)) {
                   2014: 	$r->log_reason($cuname.' at '.$cudom.
1.192     albertel 2015: 		       ' trying to publish file '.$env{'form.filename'}.
1.139     albertel 2016: 		       ' ('.$fn.') - not authorized', 
                   2017: 		       $r->filename); 
                   2018: 	return HTTP_NOT_ACCEPTABLE;
                   2019:     }
                   2020: 
1.163     albertel 2021:     my $home=&Apache::lonnet::homeserver($cuname,$cudom);
                   2022:     my $allowed=0;
                   2023:     my @ids=&Apache::lonnet::current_machine_ids();
                   2024:     foreach my $id (@ids) { if ($id eq $home) { $allowed = 1; }  }
                   2025:     unless ($allowed) {
1.139     albertel 2026: 	$r->log_reason($cuname.' at '.$cudom.
1.192     albertel 2027: 		       ' trying to publish file '.$env{'form.filename'}.
1.163     albertel 2028: 		       ' ('.$fn.') - not homeserver ('.$home.')', 
1.139     albertel 2029: 		       $r->filename); 
                   2030: 	return HTTP_NOT_ACCEPTABLE;
                   2031:     }
                   2032: 
1.215     albertel 2033:     $fn=~s{^http://[^/]+}{};
                   2034:     $fn=~s{^/~($match_username)}{/home/$1/public_html};
1.139     albertel 2035: 
                   2036:     my $targetdir='';
                   2037:     $docroot=$r->dir_config('lonDocRoot'); 
                   2038:     if ($1 ne $cuname) {
                   2039: 	$r->log_reason($cuname.' at '.$cudom.
                   2040: 		       ' trying to publish unowned file '.
1.192     albertel 2041: 		       $env{'form.filename'}.' ('.$fn.')', 
1.139     albertel 2042: 		       $r->filename); 
                   2043: 	return HTTP_NOT_ACCEPTABLE;
                   2044:     } else {
                   2045: 	$targetdir=$docroot.'/res/'.$cudom;
                   2046:     }
1.2       www      2047:                                  
                   2048:   
1.139     albertel 2049:     unless (-e $fn) { 
                   2050: 	$r->log_reason($cuname.' at '.$cudom.
                   2051: 		       ' trying to publish non-existing file '.
1.192     albertel 2052: 		       $env{'form.filename'}.' ('.$fn.')', 
1.139     albertel 2053: 		       $r->filename); 
                   2054: 	return HTTP_NOT_FOUND;
                   2055:     } 
1.2       www      2056: 
1.94      harris41 2057: # -------------------------------- File is there and owned, init lookup tables.
1.2       www      2058: 
1.205     albertel 2059:     %addid=();
                   2060:     
                   2061:     {
                   2062: 	my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
                   2063: 	while (<$fh>=~/(\w+)\s+(\w+)/) {
                   2064: 	    $addid{$1}=$2;
1.139     albertel 2065: 	}
1.205     albertel 2066:     }
1.3       www      2067: 
1.205     albertel 2068:     %nokey=();
1.11      www      2069: 
1.205     albertel 2070:     {
                   2071: 	my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
                   2072: 	while (<$fh>) {
                   2073: 	    my $word=$_;
                   2074: 	    chomp($word);
                   2075: 	    $nokey{$word}=1;
1.139     albertel 2076: 	}
                   2077:     }
1.11      www      2078: 
1.94      harris41 2079: # ---------------------------------------------------------- Start page output.
1.2       www      2080: 
1.139     albertel 2081:     &Apache::loncommon::content_type($r,'text/html');
                   2082:     $r->send_http_header;
1.180     albertel 2083:     
1.208     albertel 2084:     my $js='<script type="text/javascript">'.
                   2085: 	&Apache::loncommon::browser_and_searcher_javascript().
                   2086: 	'</script>';
1.210     albertel 2087:     $r->print(&Apache::loncommon::start_page('Resource Publication',$js));
1.101     www      2088: 
                   2089: 
1.139     albertel 2090:     my $thisfn=$fn;
1.95      www      2091: 
1.139     albertel 2092:     my $thistarget=$thisfn;
1.95      www      2093:       
1.139     albertel 2094:     $thistarget=~s/^\/home/$targetdir/;
                   2095:     $thistarget=~s/\/public\_html//;
1.95      www      2096: 
1.139     albertel 2097:     my $thisdistarget=$thistarget;
                   2098:     $thisdistarget=~s/^\Q$docroot\E//;
1.95      www      2099: 
1.139     albertel 2100:     my $thisdisfn=$thisfn;
                   2101:     $thisdisfn=~s/^\/home\/\Q$cuname\E\/public_html\///;
1.95      www      2102: 
1.139     albertel 2103:     if ($fn=~/\/$/) {
1.95      www      2104: # -------------------------------------------------------- This is a directory
1.139     albertel 2105: 	&publishdirectory($r,$fn,$thisdisfn);
1.193     www      2106: 	$r->print('<hr /><a href="/priv/'
1.139     albertel 2107: 		  .$cuname.'/'.$thisdisfn
                   2108: 		  .'">'.&mt('Return to Directory').'</a>');
1.128     www      2109: 
1.95      www      2110: 
1.139     albertel 2111:     } else {
1.94      harris41 2112: # ---------------------- Evaluate individual file, and then output information.
1.139     albertel 2113: 	$thisfn=~/\.(\w+)$/;
                   2114: 	my $thistype=$1;
                   2115: 	my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.200     raeburn  2116:         if ($thistype eq 'page') {  $thisembstyle = 'rat'; }
1.139     albertel 2117: 	$r->print('<h2>'.&mt('Publishing').' '.
                   2118: 		  &Apache::loncommon::filedescription($thistype).' <tt>');
1.2       www      2119: 
1.139     albertel 2120: 	$r->print(<<ENDCAPTION);
1.129     www      2121: <a href='javascript:void(window.open("/~$cuname/$thisdisfn","cat","height=300,width=500,scrollbars=1,resizable=1,menubar=0,location=1"))'>
                   2122: $thisdisfn</a>
                   2123: ENDCAPTION
1.139     albertel 2124:         $r->print('</tt></h2><b>'.&mt('Target').':</b> <tt>'.
                   2125: 		  $thisdistarget.'</tt><br />');
1.27      www      2126:    
1.192     albertel 2127: 	if (($cuname ne $env{'user.name'})||($cudom ne $env{'user.domain'})) {
1.139     albertel 2128: 	    $r->print('<h3><font color="red">'.&mt('Co-Author').': '.
                   2129: 		      $cuname.&mt(' at ').$cudom.'</font></h3>');
                   2130: 	}
1.26      www      2131: 
1.139     albertel 2132: 	if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
                   2133: 	    $r->print(<<ENDDIFF);
1.129     www      2134: <br />
1.136     www      2135: <a href='javascript:void(window.open("/adm/diff?filename=/~$cuname/$thisdisfn&versiontwo=priv","cat","height=300,width=500,scrollbars=1,resizable=1,menubar=0,location=1"))'>
1.129     www      2136: ENDDIFF
1.139     albertel 2137:             $r->print(&mt('Diffs with Current Version').'</a><br />');
                   2138: 	}
1.11      www      2139:   
1.94      harris41 2140: # ------------------ Publishing from $thisfn to $thistarget with $thisembstyle.
1.2       www      2141: 
1.192     albertel 2142: 	unless ($env{'form.phase'} eq 'two') {
1.185     www      2143: # ---------------------------------------------------------- Parse for problems
1.189     albertel 2144: 	    my ($warningcount,$errorcount);
                   2145: 	    if ($thisembstyle eq 'ssi') {
                   2146: 		($warningcount,$errorcount)=&checkonthis($r,$thisfn);
                   2147: 	    }
                   2148: 	    unless ($errorcount) {
1.187     www      2149: 		my ($outstring,$error)=
                   2150: 		    &publish($thisfn,$thistarget,$thisembstyle);
                   2151: 		$r->print('<hr />'.$outstring);
                   2152: 	    } else {
1.189     albertel 2153: 		$r->print('<h3>'.
                   2154: 			  &mt('The document contains errors and cannot be published.').
1.187     www      2155: 			  '</h3>');
                   2156: 	    }
1.139     albertel 2157: 	} else {
1.197     www      2158: 	    &phasetwo($r,$thisfn,$thistarget,$thisembstyle,$thisdistarget); 
                   2159: 	    $r->print('<hr />');
1.139     albertel 2160: 	}
                   2161:     }
1.208     albertel 2162:     $r->print(&Apache::loncommon::end_page());
1.15      www      2163: 
1.139     albertel 2164:     return OK;
1.1       www      2165: }
                   2166: 
                   2167: 1;
                   2168: __END__
                   2169: 
1.89      matthew  2170: =pod
1.126     bowersj2 2171: 
                   2172: =back
1.66      harris41 2173: 
                   2174: =back
                   2175: 
1.89      matthew  2176: =cut
1.66      harris41 2177: 

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