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