1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # (TeX Content Handler
5: #
6: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
7: #
8: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
9: # 03/23 Guy Albertelli
10: # 03/24,03/29,04/03 Gerd Kortemeyer
11: # 04/16/2001 Scott Harrison
12: # 05/03,05/05,05/07 Gerd Kortemeyer
13: # 05/28/2001 Scott Harrison
14: # 06/23,08/07,08/11,8/13 Gerd Kortemeyer
15:
16: package Apache::lonpublisher;
17:
18: use strict;
19: use Apache::File;
20: use File::Copy;
21: use Apache::Constants qw(:common :http :methods);
22: use HTML::TokeParser;
23: use Apache::lonxml;
24: use Apache::lonhomework;
25: use Apache::loncacc;
26: use DBI;
27:
28: my %addid;
29: my %nokey;
30: my %language;
31: my %cprtag;
32:
33: my %metadatafields;
34: my %metadatakeys;
35:
36: my $docroot;
37:
38: my $cuname;
39: my $cudom;
40:
41: # ----------------------------------------------- Evaluate string with metadata
42:
43: sub metaeval {
44: my $metastring=shift;
45:
46: my $parser=HTML::TokeParser->new(\$metastring);
47: my $token;
48: while ($token=$parser->get_token) {
49: if ($token->[0] eq 'S') {
50: my $entry=$token->[1];
51: my $unikey=$entry;
52: if (defined($token->[2]->{'package'})) {
53: $unikey.='_package_'.$token->[2]->{'package'};
54: }
55: if (defined($token->[2]->{'part'})) {
56: $unikey.='_'.$token->[2]->{'part'};
57: }
58: if (defined($token->[2]->{'id'})) {
59: $unikey.='_'.$token->[2]->{'id'};
60: }
61: if (defined($token->[2]->{'name'})) {
62: $unikey.='_'.$token->[2]->{'name'};
63: }
64: map {
65: $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
66: if ($metadatakeys{$unikey}) {
67: $metadatakeys{$unikey}.=','.$_;
68: } else {
69: $metadatakeys{$unikey}=$_;
70: }
71: } @{$token->[3]};
72: if ($metadatafields{$unikey}) {
73: my $newentry=$parser->get_text('/'.$entry);
74: unless ($metadatafields{$unikey}=~/$newentry/) {
75: $metadatafields{$unikey}.=', '.$newentry;
76: }
77: } else {
78: $metadatafields{$unikey}=$parser->get_text('/'.$entry);
79: }
80: }
81: }
82: }
83:
84: # -------------------------------------------------------- Read a metadata file
85:
86: sub metaread {
87: my ($logfile,$fn)=@_;
88: unless (-e $fn) {
89: print $logfile 'No file '.$fn."\n";
90: return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
91: }
92: print $logfile 'Processing '.$fn."\n";
93: my $metastring;
94: {
95: my $metafh=Apache::File->new($fn);
96: $metastring=join('',<$metafh>);
97: }
98: &metaeval($metastring);
99: return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
100: }
101:
102: # ---------------------------- convert 'time' format into a datetime sql format
103: sub sqltime {
104: my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
105: localtime(@_[0]);
106: $mon++; $year+=1900;
107: return "$year-$mon-$mday $hour:$min:$sec";
108: }
109:
110: # --------------------------------------------------------- Various form fields
111:
112: sub textfield {
113: my ($title,$name,$value)=@_;
114: return "\n<p><b>$title:</b><br>".
115: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
116: }
117:
118: sub hiddenfield {
119: my ($name,$value)=@_;
120: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
121: }
122:
123: sub selectbox {
124: my ($title,$name,$value,%options)=@_;
125: my $selout="\n<p><b>$title:</b><br>".'<select name="'.$name.'">';
126: map {
127: $selout.='<option value="'.$_.'"';
128: if ($_ eq $value) { $selout.=' selected'; }
129: $selout.='>'.$options{$_}.'</option>';
130: } sort keys %options;
131: return $selout.'</select>';
132: }
133:
134: # -------------------------------------------------------- Publication Step One
135:
136: sub urlfixup {
137: my ($url,$target)=@_;
138: my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
139: map {
140: if ($_ eq $host) {
141: $url=~s/^http\:\/\///;
142: $url=~s/^$host//;
143: }
144: } values %Apache::lonnet::hostname;
145: $url=~s/\~$cuname/res\/$cudom\/$cuname/;
146: if ($target) {
147: $target=~s/\/[^\/]+$//;
148: $url=&Apache::lonnet::hreflocation($target,$url);
149: }
150: return $url;
151: }
152:
153: sub publish {
154:
155: my ($source,$target,$style)=@_;
156: my $logfile;
157: my $scrout='';
158: my $allmeta='';
159: my $content='';
160: my %allow=();
161: undef %allow;
162:
163: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
164: return
165: '<font color=red>No write permission to user directory, FAIL</font>';
166: }
167: print $logfile
168: "\n\n================= Publish ".localtime()." Phase One ================\n";
169:
170: if (($style eq 'ssi') || ($style eq 'rat')) {
171: # ------------------------------------------------------- This needs processing
172:
173: # ----------------------------------------------------------------- Backup Copy
174: my $copyfile=$source.'.save';
175: if (copy($source,$copyfile)) {
176: print $logfile "Copied original file to ".$copyfile."\n";
177: } else {
178: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
179: return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
180: }
181: # ------------------------------------------------------------- IDs and indices
182:
183: my $maxindex=10;
184: my $maxid=10;
185:
186: my $needsfixup=0;
187:
188: {
189: my $org=Apache::File->new($source);
190: $content=join('',<$org>);
191: }
192: {
193: my $parser=HTML::TokeParser->new(\$content);
194: my $token;
195: while ($token=$parser->get_token) {
196: if ($token->[0] eq 'S') {
197: my $counter;
198: if ($counter=$addid{$token->[1]}) {
199: if ($counter eq 'id') {
200: if (defined($token->[2]->{'id'})) {
201: $maxid=
202: ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
203: } else {
204: $needsfixup=1;
205: }
206: } else {
207: if (defined($token->[2]->{'index'})) {
208: $maxindex=
209: ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
210: } else {
211: $needsfixup=1;
212: }
213: }
214: }
215: }
216: }
217: }
218: if ($needsfixup) {
219: print $logfile "Needs ID and/or index fixup\n".
220: "Max ID : $maxid (min 10)\n".
221: "Max Index: $maxindex (min 10)\n";
222: }
223: my $outstring='';
224: my $parser=HTML::TokeParser->new(\$content);
225: my $token;
226: while ($token=$parser->get_token) {
227: if ($token->[0] eq 'S') {
228: my $counter;
229: my $tag=$token->[1];
230: unless ($tag eq 'allow') {
231: my %parms=%{$token->[2]};
232: if ($counter=$addid{$tag}) {
233: if ($counter eq 'id') {
234: unless (defined($parms{'id'})) {
235: $maxid++;
236: $parms{'id'}=$maxid;
237: print $logfile 'ID: '.$tag.':'.$maxid."\n";
238: }
239: } elsif ($counter eq 'index') {
240: unless (defined($parms{'index'})) {
241: $maxindex++;
242: $parms{'index'}=$maxindex;
243: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
244: }
245: }
246: }
247:
248: map {
249: if (defined($parms{$_})) {
250: my $oldurl=$parms{$_};
251: my $newurl=&urlfixup($oldurl,$target);
252: if ($newurl ne $oldurl) {
253: $parms{$_}=$newurl;
254: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.
255: $newurl."\n";
256: }
257: $allow{$newurl}=1;
258: }
259: } ('src','href','codebase');
260:
261: my $newparmstring='';
262: my $endtag='';
263: map {
264: if ($_ eq '/') {
265: $endtag=' /';
266: } else {
267: my $quote=($parms{$_}=~/\"/?"'":'"');
268: $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
269: }
270: } keys %parms;
271:
272: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
273: } else {
274: $allow{$token->[2]->{'src'}}=1;
275: }
276: } elsif ($token->[0] eq 'E') {
277: unless ($token->[1] eq 'allow') {
278: $outstring.=$token->[2];
279: }
280: } else {
281: $outstring.=$token->[1];
282: }
283: }
284: # ------------------------------------------------------------ Construct Allows
285: unless ($style eq 'rat') {
286: my $allowstr="\n";
287: map {
288: $allowstr.='<allow src="'.$_.'" />'."\n";
289: } keys %allow;
290: $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;
291: }
292: # ------------------------------------------------------------- Write modified
293:
294: {
295: my $org;
296: unless ($org=Apache::File->new('>'.$source)) {
297: print $logfile "No write permit to $source\n";
298: return
299: "<font color=red>No write permission to $source, FAIL</font>";
300: }
301: print $org $outstring;
302: }
303: $content=$outstring;
304:
305: if ($needsfixup) {
306: print $logfile "End of ID and/or index fixup\n".
307: "Max ID : $maxid (min 10)\n".
308: "Max Index: $maxindex (min 10)\n";
309: } else {
310: print $logfile "Does not need ID and/or index fixup\n";
311: }
312: }
313: # --------------------------------------------- Initial step done, now metadata
314:
315: # ---------------------------------------- Storage for metadata keys and fields
316:
317: %metadatafields=();
318: %metadatakeys=();
319:
320: my %oldparmstores=();
321:
322: # ------------------------------------------------ First, check out environment
323: unless (-e $source.'.meta') {
324: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
325: $ENV{'environment.middlename'}.' '.
326: $ENV{'environment.lastname'}.' '.
327: $ENV{'environment.generation'};
328: $metadatafields{'author'}=~s/\s+/ /g;
329: $metadatafields{'author'}=~s/\s+$//;
330: $metadatafields{'owner'}=$cuname.'@'.$cudom;
331:
332: # ------------------------------------------------ Check out directory hierachy
333:
334: my $thisdisfn=$source;
335: $thisdisfn=~s/^\/home\/$cuname\///;
336:
337: my @urlparts=split(/\//,$thisdisfn);
338: $#urlparts--;
339:
340: my $currentpath='/home/'.$cuname.'/';
341:
342: map {
343: $currentpath.=$_.'/';
344: $scrout.=&metaread($logfile,$currentpath.'default.meta');
345: } @urlparts;
346:
347: # ------------------- Clear out parameters and stores (there should not be any)
348:
349: map {
350: if (($_=~/^parameter/) || ($_=~/^stores/)) {
351: delete $metadatafields{$_};
352: }
353: } keys %metadatafields;
354:
355: } else {
356: # ---------------------- Read previous metafile, remember parameters and stores
357:
358: $scrout.=&metaread($logfile,$source.'.meta');
359:
360: map {
361: if (($_=~/^parameter/) || ($_=~/^stores/)) {
362: $oldparmstores{$_}=1;
363: delete $metadatafields{$_};
364: }
365: } keys %metadatafields;
366:
367: }
368:
369: # -------------------------------------------------- Parse content for metadata
370: if ($style eq 'ssi') {
371: $allmeta=Apache::lonxml::xmlparse('meta',$content);
372:
373: &metaeval($allmeta);
374: }
375: # ---------------- Find and document discrepancies in the parameters and stores
376:
377: my $chparms='';
378: map {
379: if (($_=~/^parameter/) || ($_=~/^stores/)) {
380: unless ($_=~/\.\w+$/) {
381: unless ($oldparmstores{$_}) {
382: print $logfile 'New: '.$_."\n";
383: $chparms.=$_.' ';
384: }
385: }
386: }
387: } sort keys %metadatafields;
388: if ($chparms) {
389: $scrout.='<p><b>New parameters or stored values:</b> '.
390: $chparms;
391: }
392:
393: my $chparms='';
394: map {
395: if (($_=~/^parameter/) || ($_=~/^stores/)) {
396: unless (($metadatafields{$_.'.name'}) ||
397: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
398: print $logfile 'Obsolete: '.$_."\n";
399: $chparms.=$_.' ';
400: }
401: }
402: } sort keys %oldparmstores;
403: if ($chparms) {
404: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
405: $chparms;
406: }
407:
408: # ------------------------------------------------------- Now have all metadata
409:
410: $scrout.=
411: '<form action="/adm/publish" method="post">'.
412: &hiddenfield('phase','two').
413: &hiddenfield('filename',$ENV{'form.filename'}).
414: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
415: &textfield('Title','title',$metadatafields{'title'}).
416: &textfield('Author(s)','author',$metadatafields{'author'}).
417: &textfield('Subject','subject',$metadatafields{'subject'});
418:
419: # --------------------------------------------------- Scan content for keywords
420:
421: my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
422: my $colcount=0;
423:
424: {
425: my $textonly=$content;
426: $textonly=~s/\<script[^\<]+\<\/script\>//g;
427: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
428: $textonly=~s/\<[^\>]*\>//g;
429: $textonly=~tr/A-Z/a-z/;
430: $textonly=~s/[\$\&][a-z]\w*//g;
431: $textonly=~s/[^a-z\s]//g;
432:
433: my %keywords=();
434: map {
435: unless ($nokey{$_}) {
436: $keywords{$_}=1;
437: }
438: } ($textonly=~m/(\w+)/g);
439:
440: map {
441: $keywords{$_}=1;
442: } split(/\W+/,$metadatafields{'keywords'});
443:
444: map {
445: $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
446: if ($metadatafields{'keywords'}=~/$_/) {
447: $keywordout.=' checked';
448: }
449: $keywordout.='>'.$_.'</td>';
450: if ($colcount>10) {
451: $keywordout.="</tr><tr>\n";
452: $colcount=0;
453: }
454: $colcount++;
455: } sort keys %keywords;
456: $keywordout.='</tr></table>';
457:
458: }
459:
460: $scrout.=$keywordout;
461:
462: $scrout.=&textfield('Additional Keywords','addkey','');
463:
464: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
465:
466: $scrout.=
467: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
468: $metadatafields{'abstract'}.'</textarea>';
469:
470: $source=~/\.(\w+)$/;
471:
472: $scrout.=&hiddenfield('mime',$1);
473:
474: $scrout.=&selectbox('Language','language',
475: $metadatafields{'language'},%language);
476:
477: unless ($metadatafields{'creationdate'}) {
478: $metadatafields{'creationdate'}=time;
479: }
480: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
481:
482: $scrout.=&hiddenfield('lastrevisiondate',time);
483:
484:
485: $scrout.=&textfield('Publisher/Owner','owner',
486: $metadatafields{'owner'});
487:
488: $scrout.=&selectbox('Copyright/Distribution','copyright',
489: $metadatafields{'copyright'},%cprtag);
490:
491: return $scrout.
492: '<p><input type="submit" value="Finalize Publication"></form>';
493: }
494:
495: # -------------------------------------------------------- Publication Step Two
496:
497: sub phasetwo {
498:
499: my ($source,$target,$style,$distarget)=@_;
500: my $logfile;
501: my $scrout='';
502:
503: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
504: return
505: '<font color=red>No write permission to user directory, FAIL</font>';
506: }
507: print $logfile
508: "\n================= Publish ".localtime()." Phase Two ================\n";
509:
510: %metadatafields=();
511: %metadatakeys=();
512:
513: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
514:
515: $metadatafields{'title'}=$ENV{'form.title'};
516: $metadatafields{'author'}=$ENV{'form.author'};
517: $metadatafields{'subject'}=$ENV{'form.subject'};
518: $metadatafields{'notes'}=$ENV{'form.notes'};
519: $metadatafields{'abstract'}=$ENV{'form.abstract'};
520: $metadatafields{'mime'}=$ENV{'form.mime'};
521: $metadatafields{'language'}=$ENV{'form.language'};
522: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
523: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
524: $metadatafields{'owner'}=$ENV{'form.owner'};
525: $metadatafields{'copyright'}=$ENV{'form.copyright'};
526:
527: my $allkeywords=$ENV{'form.addkey'};
528: map {
529: if ($_=~/^form\.key\.(\w+)/) {
530: $allkeywords.=','.$1;
531: }
532: } keys %ENV;
533: $allkeywords=~s/\W+/\,/;
534: $allkeywords=~s/^\,//;
535: $metadatafields{'keywords'}=$allkeywords;
536:
537: {
538: print $logfile "\nWrite metadata file for ".$source;
539: my $mfh;
540: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
541: return
542: '<font color=red>Could not write metadata, FAIL</font>';
543: }
544: map {
545: unless ($_=~/\./) {
546: my $unikey=$_;
547: $unikey=~/^([A-Za-z]+)/;
548: my $tag=$1;
549: $tag=~tr/A-Z/a-z/;
550: print $mfh "\n\<$tag";
551: map {
552: my $value=$metadatafields{$unikey.'.'.$_};
553: $value=~s/\"/\'\'/g;
554: print $mfh ' '.$_.'="'.$value.'"';
555: } split(/\,/,$metadatakeys{$unikey});
556: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
557: }
558: } sort keys %metadatafields;
559: $scrout.='<p>Wrote Metadata';
560: print $logfile "\nWrote metadata";
561: }
562:
563: # -------------------------------- Synchronize entry with SQL metadata database
564: my %perlvar;
565: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
566: my $configline;
567: while ($configline=<CONFIG>) {
568: if ($configline =~ /PerlSetVar/) {
569: my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
570: chomp($varvalue);
571: $perlvar{$varname}=$varvalue;
572: }
573: }
574: close(CONFIG);
575:
576: my $warning;
577: my $dbh;
578: {
579: unless (
580: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
581: ) {
582: $warning='<font color=red>WARNING: Cannot connect to '.
583: 'database!</font>';
584: }
585: else {
586: my %sqldatafields;
587: $sqldatafields{'url'}=$distarget;
588: my $sth=$dbh->prepare(
589: 'delete from metadata where url like binary'.
590: '"'.$sqldatafields{'url'}.'"');
591: $sth->execute();
592: map {my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
593: $sqldatafields{$_}=$field;}
594: ('title','author','subject','keywords','notes','abstract',
595: 'mime','language','creationdate','lastrevisiondate','owner',
596: 'copyright');
597:
598: $sth=$dbh->prepare('insert into metadata values ('.
599: '"'.delete($sqldatafields{'title'}).'"'.','.
600: '"'.delete($sqldatafields{'author'}).'"'.','.
601: '"'.delete($sqldatafields{'subject'}).'"'.','.
602: '"'.delete($sqldatafields{'url'}).'"'.','.
603: '"'.delete($sqldatafields{'keywords'}).'"'.','.
604: '"'.'current'.'"'.','.
605: '"'.delete($sqldatafields{'notes'}).'"'.','.
606: '"'.delete($sqldatafields{'abstract'}).'"'.','.
607: '"'.delete($sqldatafields{'mime'}).'"'.','.
608: '"'.delete($sqldatafields{'language'}).'"'.','.
609: '"'.
610: sqltime(delete($sqldatafields{'creationdate'}))
611: .'"'.','.
612: '"'.
613: sqltime(delete(
614: $sqldatafields{'lastrevisiondate'})).'"'.','.
615: '"'.delete($sqldatafields{'owner'}).'"'.','.
616: '"'.delete(
617: $sqldatafields{'copyright'}).'"'.')');
618: $sth->execute();
619: $dbh->disconnect;
620: $scrout.='<p>Synchronized SQL metadata database';
621: print $logfile "\nSynchronized SQL metadata database";
622: }
623: }
624:
625:
626: # ----------------------------------------------------------- Copy old versions
627:
628: if (-e $target) {
629: my $filename;
630: my $maxversion=0;
631: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
632: my $srcf=$2;
633: my $srct=$3;
634: my $srcd=$1;
635: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
636: print $logfile "\nPANIC: Target dir is ".$srcd;
637: return "<font color=red>Invalid target directory, FAIL</font>";
638: }
639: opendir(DIR,$srcd);
640: while ($filename=readdir(DIR)) {
641: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
642: $maxversion=($1>$maxversion)?$1:$maxversion;
643: }
644: }
645: closedir(DIR);
646: $maxversion++;
647: $scrout.='<p>Creating old version '.$maxversion;
648: print $logfile "\nCreating old version ".$maxversion;
649:
650: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
651:
652: if (copy($target,$copyfile)) {
653: print $logfile "Copied old target to ".$copyfile."\n";
654: $scrout.='<p>Copied old target file';
655: } else {
656: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
657: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
658: }
659:
660: # --------------------------------------------------------------- Copy Metadata
661:
662: $copyfile=$copyfile.'.meta';
663:
664: if (copy($target.'.meta',$copyfile)) {
665: print $logfile "Copied old target metadata to ".$copyfile."\n";
666: $scrout.='<p>Copied old metadata';
667: } else {
668: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
669: if (-e $target.'.meta') {
670: return
671: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
672: }
673: }
674:
675:
676: } else {
677: $scrout.='<p>Initial version';
678: print $logfile "\nInitial version";
679: }
680:
681: # ---------------------------------------------------------------- Write Source
682: my $copyfile=$target;
683:
684: my @parts=split(/\//,$copyfile);
685: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
686:
687: my $count;
688: for ($count=5;$count<$#parts;$count++) {
689: $path.="/$parts[$count]";
690: if ((-e $path)!=1) {
691: print $logfile "\nCreating directory ".$path;
692: $scrout.='<p>Created directory '.$parts[$count];
693: mkdir($path,0777);
694: }
695: }
696:
697: if (copy($source,$copyfile)) {
698: print $logfile "Copied original source to ".$copyfile."\n";
699: $scrout.='<p>Copied source file';
700: } else {
701: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
702: return "<font color=red>Failed to copy source, $!, FAIL</font>";
703: }
704:
705: # --------------------------------------------------------------- Copy Metadata
706:
707: $copyfile=$copyfile.'.meta';
708:
709: if (copy($source.'.meta',$copyfile)) {
710: print $logfile "Copied original metadata to ".$copyfile."\n";
711: $scrout.='<p>Copied metadata';
712: } else {
713: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
714: return
715: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
716: }
717:
718: # --------------------------------------------------- Send update notifications
719:
720: {
721:
722: my $filename;
723:
724: $target=~/(.*)\/([^\/]+)$/;
725: my $srcf=$2;
726: opendir(DIR,$1);
727: while ($filename=readdir(DIR)) {
728: if ($filename=~/$srcf\.(\w+)$/) {
729: my $subhost=$1;
730: if ($subhost ne 'meta') {
731: $scrout.='<p>Notifying host '.$subhost.':';
732: print $logfile "\nNotifying host '.$subhost.':'";
733: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
734: $scrout.=$reply;
735: print $logfile $reply;
736: }
737: }
738: }
739: closedir(DIR);
740:
741: }
742:
743: # ---------------------------------------- Send update notifications, meta only
744:
745: {
746:
747: my $filename;
748:
749: $target=~/(.*)\/([^\/]+)$/;
750: my $srcf=$2.'.meta';
751: opendir(DIR,$1);
752: while ($filename=readdir(DIR)) {
753: if ($filename=~/$srcf\.(\w+)$/) {
754: my $subhost=$1;
755: if ($subhost ne 'meta') {
756: $scrout.=
757: '<p>Notifying host for metadata only '.$subhost.':';
758: print $logfile
759: "\nNotifying host for metadata only '.$subhost.':'";
760: my $reply=&Apache::lonnet::critical(
761: 'update:'.$target.'.meta',$subhost);
762: $scrout.=$reply;
763: print $logfile $reply;
764: }
765: }
766: }
767: closedir(DIR);
768:
769: }
770:
771: # ------------------------------------------------ Provide link to new resource
772:
773: my $thisdistarget=$target;
774: $thisdistarget=~s/^$docroot//;
775:
776: my $thissrc=$source;
777: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
778:
779: my $thissrcdir=$thissrc;
780: $thissrcdir=~s/\/[^\/]+$/\//;
781:
782:
783: return $warning.$scrout.
784: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
785: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
786: '<p><a href="'.$thissrcdir.
787: '"><font size=+2>Back to Source Directory</font></a>';
788:
789: }
790:
791: # ================================================================ Main Handler
792:
793: sub handler {
794: my $r=shift;
795:
796: if ($r->header_only) {
797: $r->content_type('text/html');
798: $r->send_http_header;
799: return OK;
800: }
801:
802: # -------------------------------------------------------------- Check filename
803:
804: my $fn=$ENV{'form.filename'};
805:
806:
807: unless ($fn) {
808: $r->log_reason($cuname.' at '.$cudom.
809: ' trying to publish empty filename', $r->filename);
810: return HTTP_NOT_FOUND;
811: }
812:
813: ($cuname,$cudom)=
814: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
815: unless (($cuname) && ($cudom)) {
816: $r->log_reason($cuname.' at '.$cudom.
817: ' trying to publish file '.$ENV{'form.filename'}.
818: ' ('.$fn.') - not authorized',
819: $r->filename);
820: return HTTP_NOT_ACCEPTABLE;
821: }
822:
823: unless (&Apache::lonnet::homeserver($cuname,$cudom)
824: eq $r->dir_config('lonHostID')) {
825: $r->log_reason($cuname.' at '.$cudom.
826: ' trying to publish file '.$ENV{'form.filename'}.
827: ' ('.$fn.') - not homeserver ('.
828: &Apache::lonnet::homeserver($cuname,$cudom).')',
829: $r->filename);
830: return HTTP_NOT_ACCEPTABLE;
831: }
832:
833: $fn=~s/^http\:\/\/[^\/]+\/\~(\w+)/\/home\/$1\/public_html/;
834:
835: my $targetdir='';
836: $docroot=$r->dir_config('lonDocRoot');
837: if ($1 ne $cuname) {
838: $r->log_reason($cuname.' at '.$cudom.
839: ' trying to publish unowned file '.$ENV{'form.filename'}.
840: ' ('.$fn.')',
841: $r->filename);
842: return HTTP_NOT_ACCEPTABLE;
843: } else {
844: $targetdir=$docroot.'/res/'.$cudom;
845: }
846:
847:
848: unless (-e $fn) {
849: $r->log_reason($cuname.' at '.$cudom.
850: ' trying to publish non-existing file '.$ENV{'form.filename'}.
851: ' ('.$fn.')',
852: $r->filename);
853: return HTTP_NOT_FOUND;
854: }
855:
856: unless ($ENV{'form.phase'} eq 'two') {
857:
858: # --------------------------------- File is there and owned, init lookup tables
859:
860: %addid=();
861:
862: {
863: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
864: while (<$fh>=~/(\w+)\s+(\w+)/) {
865: $addid{$1}=$2;
866: }
867: }
868:
869: %nokey=();
870:
871: {
872: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
873: map {
874: my $word=$_;
875: chomp($word);
876: $nokey{$word}=1;
877: } <$fh>;
878: }
879:
880: %language=();
881:
882: {
883: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/language.tab');
884: map {
885: $_=~/(\w+)\s+([\w\s\-]+)/;
886: $language{$1}=$2;
887: } <$fh>;
888: }
889:
890: %cprtag=();
891:
892: {
893: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/copyright.tab');
894: map {
895: $_=~/(\w+)\s+([\w\s\-]+)/;
896: $cprtag{$1}=$2;
897: } <$fh>;
898: }
899:
900: }
901:
902: # ----------------------------------------------------------- Start page output
903:
904: $r->content_type('text/html');
905: $r->send_http_header;
906:
907: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
908: $r->print(
909: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
910: my $thisfn=$fn;
911:
912: # ------------------------------------------------------------- Individual file
913: {
914: $thisfn=~/\.(\w+)$/;
915: my $thistype=$1;
916: my $thisembstyle=&Apache::lonnet::fileembstyle($thistype);
917:
918: my $thistarget=$thisfn;
919:
920: $thistarget=~s/^\/home/$targetdir/;
921: $thistarget=~s/\/public\_html//;
922:
923: my $thisdistarget=$thistarget;
924: $thisdistarget=~s/^$docroot//;
925:
926: my $thisdisfn=$thisfn;
927: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
928:
929: $r->print('<h2>Publishing '.
930: &Apache::lonnet::filedescription($thistype).' <tt>'.
931: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
932:
933: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
934: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
935: '</font></h3>');
936: }
937:
938: if (&Apache::lonnet::fileembstyle($thistype) eq 'ssi') {
939: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
940: $thisdisfn.
941: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
942: }
943:
944: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
945:
946: unless ($ENV{'form.phase'} eq 'two') {
947: $r->print(
948: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
949: } else {
950: $r->print(
951: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
952: }
953:
954: }
955: $r->print('</body></html>');
956:
957: return OK;
958: }
959:
960: 1;
961: __END__
962:
963:
964:
965:
966:
967:
968:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>