1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # $Id: lonpublisher.pm,v 1.72 2002/01/18 16:48:14 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 (keys %allow) {
396: my $thisdep=$_;
397: unless ($style eq 'rat') {
398: $allowstr.="\n".'<allow src="'.$thisdep.'" />';
399: }
400: $scrout.='<br>';
401: unless ($thisdep=~/\*/) {
402: $scrout.='<a href="'.$thisdep.'">';
403: }
404: $scrout.='<tt>'.$thisdep.'</tt>';
405: unless ($thisdep=~/\*/) {
406: $scrout.='</a>';
407: if (
408: &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
409: $thisdep.'.meta') eq '-1') {
410: $scrout.=
411: ' - <font color=red>Currently not available</font>';
412: } else {
413: my %temphash=(&Apache::lonnet::declutter($target).'___'.
414: &Apache::lonnet::declutter($thisdep).'___usage'
415: => time);
416: $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
417: if ((defined($1)) && (defined($2))) {
418: &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
419: }
420: }
421: }
422: }
423: $allowstr=~s/\n+/\n/g;
424: $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;
425:
426: # ------------------------------------------------------------- Write modified
427:
428: {
429: my $org;
430: unless ($org=Apache::File->new('>'.$source)) {
431: print $logfile "No write permit to $source\n";
432: return
433: "<font color=red>No write permission to $source, FAIL</font>";
434: }
435: print $org $outstring;
436: }
437: $content=$outstring;
438:
439: if ($needsfixup) {
440: print $logfile "End of ID and/or index fixup\n".
441: "Max ID : $maxid (min 10)\n".
442: "Max Index: $maxindex (min 10)\n";
443: } else {
444: print $logfile "Does not need ID and/or index fixup\n";
445: }
446: }
447: # --------------------------------------------- Initial step done, now metadata
448:
449: # ---------------------------------------- Storage for metadata keys and fields
450:
451: %metadatafields=();
452: %metadatakeys=();
453:
454: my %oldparmstores=();
455:
456: $scrout.='<h3>Metadata Information</h3>';
457:
458: # ------------------------------------------------ First, check out environment
459: unless (-e $source.'.meta') {
460: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
461: $ENV{'environment.middlename'}.' '.
462: $ENV{'environment.lastname'}.' '.
463: $ENV{'environment.generation'};
464: $metadatafields{'author'}=~s/\s+/ /g;
465: $metadatafields{'author'}=~s/\s+$//;
466: $metadatafields{'owner'}=$cuname.'@'.$cudom;
467:
468: # ------------------------------------------------ Check out directory hierachy
469:
470: my $thisdisfn=$source;
471: $thisdisfn=~s/^\/home\/$cuname\///;
472:
473: my @urlparts=split(/\//,$thisdisfn);
474: $#urlparts--;
475:
476: my $currentpath='/home/'.$cuname.'/';
477:
478: foreach (@urlparts) {
479: $currentpath.=$_.'/';
480: $scrout.=&metaread($logfile,$currentpath.'default.meta');
481: }
482:
483: # ------------------- Clear out parameters and stores (there should not be any)
484:
485: foreach (keys %metadatafields) {
486: if (($_=~/^parameter/) || ($_=~/^stores/)) {
487: delete $metadatafields{$_};
488: }
489: }
490:
491: } else {
492: # ---------------------- Read previous metafile, remember parameters and stores
493:
494: $scrout.=&metaread($logfile,$source.'.meta');
495:
496: foreach (keys %metadatafields) {
497: if (($_=~/^parameter/) || ($_=~/^stores/)) {
498: $oldparmstores{$_}=1;
499: delete $metadatafields{$_};
500: }
501: }
502:
503: }
504:
505: # -------------------------------------------------- Parse content for metadata
506: if ($style eq 'ssi') {
507: my $oldenv=$ENV{'request.uri'};
508:
509: $ENV{'request.uri'}=$target;
510: $allmeta=Apache::lonxml::xmlparse('meta',$content);
511: $ENV{'request.uri'}=$oldenv;
512:
513: &metaeval($allmeta);
514: }
515: # ---------------- Find and document discrepancies in the parameters and stores
516:
517: my $chparms='';
518: foreach (sort keys %metadatafields) {
519: if (($_=~/^parameter/) || ($_=~/^stores/)) {
520: unless ($_=~/\.\w+$/) {
521: unless ($oldparmstores{$_}) {
522: print $logfile 'New: '.$_."\n";
523: $chparms.=$_.' ';
524: }
525: }
526: }
527: }
528: if ($chparms) {
529: $scrout.='<p><b>New parameters or stored values:</b> '.
530: $chparms;
531: }
532:
533: $chparms='';
534: foreach (sort keys %oldparmstores) {
535: if (($_=~/^parameter/) || ($_=~/^stores/)) {
536: unless (($metadatafields{$_.'.name'}) ||
537: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
538: print $logfile 'Obsolete: '.$_."\n";
539: $chparms.=$_.' ';
540: }
541: }
542: }
543: if ($chparms) {
544: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
545: $chparms;
546: }
547:
548: # ------------------------------------------------------- Now have all metadata
549:
550: $scrout.=
551: '<form action="/adm/publish" method="post">'.
552: '<p><input type="submit" value="Finalize Publication" /></p>'.
553: &hiddenfield('phase','two').
554: &hiddenfield('filename',$ENV{'form.filename'}).
555: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
556: &hiddenfield('dependencies',join(',',keys %allow)).
557: &textfield('Title','title',$metadatafields{'title'}).
558: &textfield('Author(s)','author',$metadatafields{'author'}).
559: &textfield('Subject','subject',$metadatafields{'subject'});
560:
561: # --------------------------------------------------- Scan content for keywords
562:
563: my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
564: my $colcount=0;
565: my %keywords=();
566:
567: if (length($content)<500000) {
568: my $textonly=$content;
569: $textonly=~s/\<script[^\<]+\<\/script\>//g;
570: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
571: $textonly=~s/\<[^\>]*\>//g;
572: $textonly=~tr/A-Z/a-z/;
573: $textonly=~s/[\$\&][a-z]\w*//g;
574: $textonly=~s/[^a-z\s]//g;
575:
576: foreach ($textonly=~m/(\w+)/g) {
577: unless ($nokey{$_}) {
578: $keywords{$_}=1;
579: }
580: }
581: }
582:
583:
584: foreach (split(/\W+/,$metadatafields{'keywords'})) {
585: $keywords{$_}=1;
586: }
587:
588: foreach (sort keys %keywords) {
589: $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
590: if ($metadatafields{'keywords'}) {
591: if ($metadatafields{'keywords'}=~/$_/) {
592: $keywordout.=' checked';
593: }
594: } elsif (&Apache::loncommon::keyword($_)) {
595: $keywordout.=' checked';
596: }
597: $keywordout.='>'.$_.'</td>';
598: if ($colcount>10) {
599: $keywordout.="</tr><tr>\n";
600: $colcount=0;
601: }
602: $colcount++;
603: }
604:
605: $keywordout.='</tr></table>';
606:
607: $scrout.=$keywordout;
608:
609: $scrout.=&textfield('Additional Keywords','addkey','');
610:
611: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
612:
613: $scrout.=
614: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
615: $metadatafields{'abstract'}.'</textarea>';
616:
617: $source=~/\.(\w+)$/;
618:
619: $scrout.=&hiddenfield('mime',$1);
620:
621: $scrout.=&selectbox('Language','language',
622: $metadatafields{'language'},
623: \&Apache::loncommon::languagedescription,
624: (&Apache::loncommon::languageids),
625: );
626:
627: unless ($metadatafields{'creationdate'}) {
628: $metadatafields{'creationdate'}=time;
629: }
630: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
631:
632: $scrout.=&hiddenfield('lastrevisiondate',time);
633:
634:
635: $scrout.=&textfield('Publisher/Owner','owner',
636: $metadatafields{'owner'});
637: # --------------------------------------------------- Correct copyright for rat
638: if ($style eq 'rat') {
639: if ($metadatafields{'copyright'} eq 'public') {
640: delete $metadatafields{'copyright'};
641: }
642: $scrout.=&selectbox('Copyright/Distribution','copyright',
643: $metadatafields{'copyright'},
644: \&Apache::loncommon::copyrightdescription,
645: (grep !/^public$/,(&Apache::loncommon::copyrightids)));
646: }
647: else {
648: $scrout.=&selectbox('Copyright/Distribution','copyright',
649: $metadatafields{'copyright'},
650: \&Apache::loncommon::copyrightdescription,
651: (&Apache::loncommon::copyrightids));
652: }
653: return $scrout.
654: '<p><input type="submit" value="Finalize Publication" /></p></form>';
655: }
656:
657: # -------------------------------------------------------- Publication Step Two
658:
659: sub phasetwo {
660:
661: my ($source,$target,$style,$distarget)=@_;
662: my $logfile;
663: my $scrout='';
664:
665: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
666: return
667: '<font color=red>No write permission to user directory, FAIL</font>';
668: }
669: print $logfile
670: "\n================= Publish ".localtime()." Phase Two ================\n";
671:
672: %metadatafields=();
673: %metadatakeys=();
674:
675: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
676:
677: $metadatafields{'title'}=$ENV{'form.title'};
678: $metadatafields{'author'}=$ENV{'form.author'};
679: $metadatafields{'subject'}=$ENV{'form.subject'};
680: $metadatafields{'notes'}=$ENV{'form.notes'};
681: $metadatafields{'abstract'}=$ENV{'form.abstract'};
682: $metadatafields{'mime'}=$ENV{'form.mime'};
683: $metadatafields{'language'}=$ENV{'form.language'};
684: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
685: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
686: $metadatafields{'owner'}=$ENV{'form.owner'};
687: $metadatafields{'copyright'}=$ENV{'form.copyright'};
688: $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
689:
690: my $allkeywords=$ENV{'form.addkey'};
691: foreach (keys %ENV) {
692: if ($_=~/^form\.key\.(\w+)/) {
693: $allkeywords.=','.$1;
694: }
695: }
696: $allkeywords=~s/\W+/\,/;
697: $allkeywords=~s/^\,//;
698: $metadatafields{'keywords'}=$allkeywords;
699:
700: {
701: print $logfile "\nWrite metadata file for ".$source;
702: my $mfh;
703: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
704: return
705: '<font color=red>Could not write metadata, FAIL</font>';
706: }
707: foreach (sort keys %metadatafields) {
708: unless ($_=~/\./) {
709: my $unikey=$_;
710: $unikey=~/^([A-Za-z]+)/;
711: my $tag=$1;
712: $tag=~tr/A-Z/a-z/;
713: print $mfh "\n\<$tag";
714: foreach (split(/\,/,$metadatakeys{$unikey})) {
715: my $value=$metadatafields{$unikey.'.'.$_};
716: $value=~s/\"/\'\'/g;
717: print $mfh ' '.$_.'="'.$value.'"';
718: }
719: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
720: }
721: }
722: $scrout.='<p>Wrote Metadata';
723: print $logfile "\nWrote metadata";
724: }
725:
726: # -------------------------------- Synchronize entry with SQL metadata database
727: my $warning;
728:
729: unless ($metadatafields{'copyright'} eq 'priv') {
730:
731: my $dbh;
732: {
733: unless (
734: $dbh = DBI->connect("DBI:mysql:loncapa","www",
735: $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
736: ) {
737: $warning='<font color=red>WARNING: Cannot connect to '.
738: 'database!</font>';
739: }
740: else {
741: my %sqldatafields;
742: $sqldatafields{'url'}=$distarget;
743: my $sth=$dbh->prepare(
744: 'delete from metadata where url like binary'.
745: '"'.$sqldatafields{'url'}.'"');
746: $sth->execute();
747: foreach ('title','author','subject','keywords','notes','abstract',
748: 'mime','language','creationdate','lastrevisiondate','owner',
749: 'copyright') {
750: my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
751: $sqldatafields{$_}=$field;
752: }
753:
754: $sth=$dbh->prepare('insert into metadata values ('.
755: '"'.delete($sqldatafields{'title'}).'"'.','.
756: '"'.delete($sqldatafields{'author'}).'"'.','.
757: '"'.delete($sqldatafields{'subject'}).'"'.','.
758: '"'.delete($sqldatafields{'url'}).'"'.','.
759: '"'.delete($sqldatafields{'keywords'}).'"'.','.
760: '"'.'current'.'"'.','.
761: '"'.delete($sqldatafields{'notes'}).'"'.','.
762: '"'.delete($sqldatafields{'abstract'}).'"'.','.
763: '"'.delete($sqldatafields{'mime'}).'"'.','.
764: '"'.delete($sqldatafields{'language'}).'"'.','.
765: '"'.
766: sqltime(delete($sqldatafields{'creationdate'}))
767: .'"'.','.
768: '"'.
769: sqltime(delete(
770: $sqldatafields{'lastrevisiondate'})).'"'.','.
771: '"'.delete($sqldatafields{'owner'}).'"'.','.
772: '"'.delete(
773: $sqldatafields{'copyright'}).'"'.')');
774: $sth->execute();
775: $dbh->disconnect;
776: $scrout.='<p>Synchronized SQL metadata database';
777: print $logfile "\nSynchronized SQL metadata database";
778: }
779: }
780:
781: } else {
782: $scrout.='<p>Private Publication - did not synchronize database';
783: print $logfile "\nPrivate: Did not synchronize data into ".
784: "SQL metadata database";
785: }
786: # ----------------------------------------------------------- Copy old versions
787:
788: if (-e $target) {
789: my $filename;
790: my $maxversion=0;
791: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
792: my $srcf=$2;
793: my $srct=$3;
794: my $srcd=$1;
795: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
796: print $logfile "\nPANIC: Target dir is ".$srcd;
797: return "<font color=red>Invalid target directory, FAIL</font>";
798: }
799: opendir(DIR,$srcd);
800: while ($filename=readdir(DIR)) {
801: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
802: $maxversion=($1>$maxversion)?$1:$maxversion;
803: }
804: }
805: closedir(DIR);
806: $maxversion++;
807: $scrout.='<p>Creating old version '.$maxversion;
808: print $logfile "\nCreating old version ".$maxversion;
809:
810: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
811:
812: if (copy($target,$copyfile)) {
813: print $logfile "Copied old target to ".$copyfile."\n";
814: $scrout.='<p>Copied old target file';
815: } else {
816: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
817: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
818: }
819:
820: # --------------------------------------------------------------- Copy Metadata
821:
822: $copyfile=$copyfile.'.meta';
823:
824: if (copy($target.'.meta',$copyfile)) {
825: print $logfile "Copied old target metadata to ".$copyfile."\n";
826: $scrout.='<p>Copied old metadata';
827: } else {
828: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
829: if (-e $target.'.meta') {
830: return
831: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
832: }
833: }
834:
835:
836: } else {
837: $scrout.='<p>Initial version';
838: print $logfile "\nInitial version";
839: }
840:
841: # ---------------------------------------------------------------- Write Source
842: my $copyfile=$target;
843:
844: my @parts=split(/\//,$copyfile);
845: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
846:
847: my $count;
848: for ($count=5;$count<$#parts;$count++) {
849: $path.="/$parts[$count]";
850: if ((-e $path)!=1) {
851: print $logfile "\nCreating directory ".$path;
852: $scrout.='<p>Created directory '.$parts[$count];
853: mkdir($path,0777);
854: }
855: }
856:
857: if (copy($source,$copyfile)) {
858: print $logfile "Copied original source to ".$copyfile."\n";
859: $scrout.='<p>Copied source file';
860: } else {
861: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
862: return "<font color=red>Failed to copy source, $!, FAIL</font>";
863: }
864:
865: # --------------------------------------------------------------- Copy Metadata
866:
867: $copyfile=$copyfile.'.meta';
868:
869: if (copy($source.'.meta',$copyfile)) {
870: print $logfile "Copied original metadata to ".$copyfile."\n";
871: $scrout.='<p>Copied metadata';
872: } else {
873: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
874: return
875: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
876: }
877:
878: # --------------------------------------------------- Send update notifications
879:
880: {
881:
882: my $filename;
883:
884: $target=~/(.*)\/([^\/]+)$/;
885: my $srcf=$2;
886: opendir(DIR,$1);
887: while ($filename=readdir(DIR)) {
888: if ($filename=~/$srcf\.(\w+)$/) {
889: my $subhost=$1;
890: if ($subhost ne 'meta') {
891: $scrout.='<p>Notifying host '.$subhost.':';
892: print $logfile "\nNotifying host '.$subhost.':'";
893: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
894: $scrout.=$reply;
895: print $logfile $reply;
896: }
897: }
898: }
899: closedir(DIR);
900:
901: }
902:
903: # ---------------------------------------- Send update notifications, meta only
904:
905: {
906:
907: my $filename;
908:
909: $target=~/(.*)\/([^\/]+)$/;
910: my $srcf=$2.'.meta';
911: opendir(DIR,$1);
912: while ($filename=readdir(DIR)) {
913: if ($filename=~/$srcf\.(\w+)$/) {
914: my $subhost=$1;
915: if ($subhost ne 'meta') {
916: $scrout.=
917: '<p>Notifying host for metadata only '.$subhost.':';
918: print $logfile
919: "\nNotifying host for metadata only '.$subhost.':'";
920: my $reply=&Apache::lonnet::critical(
921: 'update:'.$target.'.meta',$subhost);
922: $scrout.=$reply;
923: print $logfile $reply;
924: }
925: }
926: }
927: closedir(DIR);
928:
929: }
930:
931: # ------------------------------------------------ Provide link to new resource
932:
933: my $thisdistarget=$target;
934: $thisdistarget=~s/^$docroot//;
935:
936: my $thissrc=$source;
937: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
938:
939: my $thissrcdir=$thissrc;
940: $thissrcdir=~s/\/[^\/]+$/\//;
941:
942:
943: return $warning.$scrout.
944: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
945: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
946: '<p><a href="'.$thissrcdir.
947: '"><font size=+2>Back to Source Directory</font></a>';
948:
949: }
950:
951: # ================================================================ Main Handler
952:
953: sub handler {
954: my $r=shift;
955:
956: if ($r->header_only) {
957: $r->content_type('text/html');
958: $r->send_http_header;
959: return OK;
960: }
961:
962: # Get query string for limited number of parameters
963:
964: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
965: my ($name, $value) = split(/=/,$_);
966: $value =~ tr/+/ /;
967: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
968: if ($name eq 'filename') {
969: unless ($ENV{'form.'.$name}) {
970: $ENV{'form.'.$name}=$value;
971: }
972: }
973: }
974:
975:
976: # -------------------------------------------------------------- Check filename
977:
978: my $fn=$ENV{'form.filename'};
979:
980:
981: unless ($fn) {
982: $r->log_reason($cuname.' at '.$cudom.
983: ' trying to publish empty filename', $r->filename);
984: return HTTP_NOT_FOUND;
985: }
986:
987: ($cuname,$cudom)=
988: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
989: unless (($cuname) && ($cudom)) {
990: $r->log_reason($cuname.' at '.$cudom.
991: ' trying to publish file '.$ENV{'form.filename'}.
992: ' ('.$fn.') - not authorized',
993: $r->filename);
994: return HTTP_NOT_ACCEPTABLE;
995: }
996:
997: unless (&Apache::lonnet::homeserver($cuname,$cudom)
998: eq $r->dir_config('lonHostID')) {
999: $r->log_reason($cuname.' at '.$cudom.
1000: ' trying to publish file '.$ENV{'form.filename'}.
1001: ' ('.$fn.') - not homeserver ('.
1002: &Apache::lonnet::homeserver($cuname,$cudom).')',
1003: $r->filename);
1004: return HTTP_NOT_ACCEPTABLE;
1005: }
1006:
1007: $fn=~s/^http\:\/\/[^\/]+//;
1008: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1009:
1010: my $targetdir='';
1011: $docroot=$r->dir_config('lonDocRoot');
1012: if ($1 ne $cuname) {
1013: $r->log_reason($cuname.' at '.$cudom.
1014: ' trying to publish unowned file '.$ENV{'form.filename'}.
1015: ' ('.$fn.')',
1016: $r->filename);
1017: return HTTP_NOT_ACCEPTABLE;
1018: } else {
1019: $targetdir=$docroot.'/res/'.$cudom;
1020: }
1021:
1022:
1023: unless (-e $fn) {
1024: $r->log_reason($cuname.' at '.$cudom.
1025: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1026: ' ('.$fn.')',
1027: $r->filename);
1028: return HTTP_NOT_FOUND;
1029: }
1030:
1031: unless ($ENV{'form.phase'} eq 'two') {
1032:
1033: # --------------------------------- File is there and owned, init lookup tables
1034:
1035: %addid=();
1036:
1037: {
1038: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1039: while (<$fh>=~/(\w+)\s+(\w+)/) {
1040: $addid{$1}=$2;
1041: }
1042: }
1043:
1044: %nokey=();
1045:
1046: {
1047: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1048: while (<$fh>) {
1049: my $word=$_;
1050: chomp($word);
1051: $nokey{$word}=1;
1052: }
1053: }
1054:
1055: }
1056:
1057: # ----------------------------------------------------------- Start page output
1058:
1059: $r->content_type('text/html');
1060: $r->send_http_header;
1061:
1062: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1063: $r->print(
1064: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1065: my $thisfn=$fn;
1066:
1067: # ------------------------------------------------------------- Individual file
1068: {
1069: $thisfn=~/\.(\w+)$/;
1070: my $thistype=$1;
1071: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1072:
1073: my $thistarget=$thisfn;
1074:
1075: $thistarget=~s/^\/home/$targetdir/;
1076: $thistarget=~s/\/public\_html//;
1077:
1078: my $thisdistarget=$thistarget;
1079: $thisdistarget=~s/^$docroot//;
1080:
1081: my $thisdisfn=$thisfn;
1082: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1083:
1084: $r->print('<h2>Publishing '.
1085: &Apache::loncommon::filedescription($thistype).' <tt>'.
1086: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1087:
1088: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1089: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1090: '</font></h3>');
1091: }
1092:
1093: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1094: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1095: $thisdisfn.
1096: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1097: }
1098:
1099: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1100:
1101: unless ($ENV{'form.phase'} eq 'two') {
1102: $r->print(
1103: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1104: } else {
1105: $r->print(
1106: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1107: }
1108:
1109: }
1110: $r->print('</body></html>');
1111:
1112: return OK;
1113: }
1114:
1115: 1;
1116: __END__
1117:
1118: =head1 NAME
1119:
1120: Apache::lonpublisher - Publication Handler
1121:
1122: =head1 SYNOPSIS
1123:
1124: Invoked by /etc/httpd/conf/srm.conf:
1125:
1126: <Location /adm/publish>
1127: PerlAccessHandler Apache::lonacc
1128: SetHandler perl-script
1129: PerlHandler Apache::lonpublisher
1130: ErrorDocument 403 /adm/login
1131: ErrorDocument 404 /adm/notfound.html
1132: ErrorDocument 406 /adm/unauthorized.html
1133: ErrorDocument 500 /adm/errorhandler
1134: </Location>
1135:
1136: =head1 INTRODUCTION
1137:
1138: This module publishes a file. This involves gathering metadata,
1139: versioning the file, copying file from construction space to
1140: publication space, and copying metadata from construction space
1141: to publication space.
1142:
1143: This is part of the LearningOnline Network with CAPA project
1144: described at http://www.lon-capa.org.
1145:
1146: =head1 HANDLER SUBROUTINE
1147:
1148: This routine is called by Apache and mod_perl.
1149:
1150: =over 4
1151:
1152: =item *
1153:
1154: Get query string for limited number of parameters
1155:
1156: =item *
1157:
1158: Check filename
1159:
1160: =item *
1161:
1162: File is there and owned, init lookup tables
1163:
1164: =item *
1165:
1166: Start page output
1167:
1168: =item *
1169:
1170: Individual file
1171:
1172: =item *
1173:
1174: publish from $thisfn to $thistarget with $thisembstyle
1175:
1176: =back
1177:
1178: =head1 OTHER SUBROUTINES
1179:
1180: =over 4
1181:
1182: =item *
1183:
1184: metaeval() : Evaluate string with metadata
1185:
1186: =item *
1187:
1188: metaread() : Read a metadata file
1189:
1190: =item *
1191:
1192: sqltime() : convert 'time' format into a datetime sql format
1193:
1194: =item *
1195:
1196: textfield() : form field
1197:
1198: =item *
1199:
1200: hiddenfield() : form field
1201:
1202: =item *
1203:
1204: selectbox() : form field
1205:
1206: =item *
1207:
1208: urlfixup() : fixup URL (Publication Step One)
1209:
1210: =item *
1211:
1212: publish() : publish (Publication Step One)
1213:
1214: =item *
1215:
1216: phasetwo() : render second interface showing status of publication steps
1217: (Publication Step Two)
1218:
1219: =back
1220:
1221: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>