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