/[wopi]/make_poll.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /make_poll.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (show annotations)
Thu Apr 24 17:25:44 2003 UTC (21 years ago) by dpavlin
Branch: MAIN
Changes since 1.3: +50 -68 lines
File MIME type: text/plain
parse some of config

1 #!/usr/bin/perl -w
2 #
3 # Dobrica Pavlinusic <dpavlin@rot13.org>
4 #
5 # Originally made for proof. during April 2001; later released under GPL v2
6 #
7 # 2003-04-dd general cleanup in preparation of release
8
9 use strict;
10
11 use XML::Parser;
12 use Text::Iconv;
13
14 # output charset
15 my $charset='ISO-8859-2';
16
17 Text::Iconv->raise_error(0); # Conversion errors raise exceptions
18 my $from_utf8 = Text::Iconv->new('UTF8', $charset);
19 sub x {
20 return $from_utf8->convert($_[0]);
21 }
22
23 $|=1;
24
25 my $Usage =<<'End_of_Usage;';
26 I will write usage information here. I promise!
27 End_of_Usage;
28
29 my @Modes = qw(object pass skip);
30
31 my $dir;
32 my $dowarn = 1;
33
34 my $pitanje_nr = 0; # curr. pitanje
35 my $pitanje_tag = ""; # originalni oblik broja pitanja
36 my $page_nr = 1; # prvo pitanje na strani
37
38 my $p_suffix=""; # if more than one box per question
39
40 my $curr_suffix=""; # trenutni suffix
41
42 my @stack_pit; # stack pitanja (pitanje, suffix)
43
44 my @sql_create = ("id serial",
45 "http_referer character varying(500)",
46 "remote_addr character varying(15)",
47 "user_agent character varying(300)",
48 "unesen timestamp DEFAULT now()",
49 "member_id int4 NOT NULL"
50 );
51 my @sql_update;
52 my @last_sql_update;
53 my @prelast_sql_update;
54
55 my @php_addon; # php code to add on page header
56
57 my ($last_fn,$last_page);
58
59 # this is unique prefix for this installation
60 my $prefix="wopi_";
61
62 # this is usename in database
63 my $db_user="dpavlin";
64
65 #------------------------------------------------------------------
66
67 sub suck_file {
68 my $file = shift @_;
69 open(H,$file) || die "can't open '$file': $!";
70 my $content;
71 while (<H>) { $content .= $_; } ;
72 close(H);
73 return $content;
74 }
75
76 my $html_header=suck_file("header.html");
77 my $html_separator=suck_file("separator.html");
78 my $html_next=suck_file("next.html");
79 my $html_footer=suck_file("footer.html");
80
81 #------------------------------------------------------------------
82
83 sub php_header {
84 my ($page_nr,@sql_update) = @_;
85 my $out='<?php
86 include("common.php");
87 if (isset($update)) {
88 $member_id=id_decode($a);
89 ';
90 $out.=$php_addon[$page_nr-2] if (defined $php_addon[$page_nr-2]);
91 $out.='
92 $sql="update '.$dir.' set '.join(",\n",@sql_update).',
93 do_stranice=\'$PHP_SELF\'
94 where id=$id";
95 # print "<pre>$sql</pre>";
96 $result=pg_Exec($conn,fix_sql($sql));
97 } elseif($do_stranice != $PHP_SELF) {
98 Header("Location: $do_uri?a=$a");
99 exit;
100 }
101 ?>';
102 return $out;
103 }
104
105 #------------------------------------------------------------------
106
107 # first, define some constants
108 my $common_php = suck_file("common.php");
109
110 #------------------------------------------------------------------
111
112 my $head_php=suck_file("head.php");
113
114 #------------------------------------------------------------------
115
116 my $html_kraj=suck_file("thanks.html");
117
118 #------------------------------------------------------------------
119
120 while (defined($ARGV[0]) and $ARGV[0] =~ /^-/) {
121 my $opt = shift;
122
123 if ($opt eq '-h') {
124 print $Usage;
125 exit;
126 }
127 } # End of option processing
128
129 my $xmlfile = shift;
130
131 die "No poll xml file provided!\n$Usage" unless defined $xmlfile;
132
133 die "Can't read $xmlfile" unless -r $xmlfile;
134
135 if (defined $dir) {
136 die "$dir isn't a directory" unless -d $dir;
137 }
138 else {
139 $xmlfile =~ m!([^/.]+)(?:\.[^/.]*)?$!;
140 $dir = $1;
141 if (-e $dir) {
142 die "$dir exists but isn't a directory"
143 unless -d $dir;
144 }
145 else {
146 mkdir $dir, 0755;
147 }
148 }
149
150 my $in_poll = 0;
151 my $after_head = 0;
152
153 my $Mode = 0;
154 my $Mode_level = 0;
155
156 my $Text;
157 my $Markedup_Text;
158 my $Object;
159 my @Ostack = ();
160
161 #my $intext = 0;
162 my $closure;
163 my @closure_stack = ();
164
165 #my $style_link = '';
166
167 #my $index = 'index.html';
168 #my @slidetitle;
169 my $body;
170 #my $inlist = 0;
171
172 #my @Titles;
173
174 my $header;
175
176 my $page_number = 0;
177
178 my $p = new XML::Parser(ErrorContext => 3,
179 Handlers => {Start => \&starthndl,
180 End => \&endhndl,
181 Char => \&text});
182 $p->parsefile($xmlfile);
183
184 #----------------------------------------------------------
185
186 # dump last php page....
187
188 print "p[$page_nr] ";
189
190 open(PAGE, ">$dir/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";
191 print PAGE php_header($page_nr,@prelast_sql_update);
192 my $next_fn=sprintf("%02d.php",$page_nr);
193 $last_page=~s/##NEXTPAGE##/$next_fn/;
194 print PAGE $last_page;
195 close(PAGE);
196
197 $page_nr++;
198 open(PAGE, ">$dir/$next_fn") or die "Couldn't open $next_fn for writing:\n$!";
199 print PAGE php_header($page_nr,@last_sql_update);
200 print PAGE "$html_header $html_kraj $html_footer";
201 close(PAGE);
202
203 # dump sql structure
204
205 open(SQL,">$dir/$dir.sql") || die "$dir.sql: $!";
206 print SQL "create table poslani ( member_id int4 not null, unesen timestamp default now() );\n";
207 print SQL "create table $dir (do_stranice text default null, ",join(",\n",@sql_create),");\n";
208 close(SQL);
209
210 # dump common.php
211
212 open(PHP,">$dir/common.php") || die "common.php: $!";
213 $common_php =~ s/##DB##/$dir/g;
214 my $db_name = $prefix.$dir;
215 $common_php =~ s/##DB_NAME##/$db_name/g;
216 $common_php =~ s/##PREFIX##/$prefix/g;
217 $common_php =~ s/##DB_USER##/$db_user/g;
218 $common_php =~ s/##PREFIX##/$prefix/g;
219 my $members_db = $prefix."members";
220 $common_php =~ s/##MEMBERS_DB##/$members_db/g;
221
222 print PHP $common_php;
223 close(PHP);
224
225 open(PHP,">$dir/head.php") || die "head.php: $!";
226 my $max_page = $page_nr - 1;
227 $head_php=~ s/##MAXPAGE##/$max_page/;
228 $head_php=~ s/##TEXT##/Ispunili ste %02d%% ankete/;
229 print PHP $head_php;
230 close(PHP);
231
232 # 01.php -> index.php
233 rename "$dir/01.php","$dir/index.php" || die "can't rename '$dir/01.php' to index.php";
234
235 ################
236 ## End of main
237 ################
238
239 # return unique name of pitanje
240 sub new_pit {
241 my $out="p".$pitanje_nr.$p_suffix;
242 $curr_suffix=$p_suffix;
243 $p_suffix++;
244 return $out;
245 }
246
247 # current pitanje
248 sub curr_pit {
249 return "p".$pitanje_nr.$curr_suffix;
250 }
251
252 #----------------------------------------------------------
253
254 sub starthndl {
255 my ($xp, $el, %atts) = @_;
256
257 # return unless ($in_poll or $el eq 'slideshow');
258
259 unless ($in_poll) {
260 $in_poll = $xp->depth + 1;
261 return;
262 }
263
264 if ($Mode) {
265
266 if ($Mode eq 'pass') {
267 $Markedup_Text .= "\n" . $xp->recognized_string;
268 }
269 elsif ($Mode eq 'object') {
270 push(@Ostack, $Object);
271
272 $Object = {_Atts => \%atts,
273 _Text => ''
274 };
275 bless $Object, "Slideobj::$el";
276 }
277
278 # skip does nothing
279 return;
280 }
281
282 unless ($after_head) {
283 if ($el eq 'head') {
284 $after_head = 1;
285 start_mode($xp, 'object');
286
287 push(@closure_stack, $closure);
288 $closure =
289 sub {
290 my ($xp, $text) = @_;
291
292 unless (defined $text) {
293
294 $header = $Object;
295 }
296 };
297
298 return;
299 }
300
301 # die "The head element must be the first thing in the slideshow";
302 }
303
304
305 my $new_closure;
306
307 my $subname = "Poll::$el";
308
309 if (defined &$subname) {
310 no strict 'refs';
311
312 &$subname($xp, $el, \%atts, \$new_closure);
313 }
314 else {
315 $body .= $xp->recognized_string;
316 $new_closure =
317 sub {
318 my ($xp, $text) = @_;
319
320 if (defined $text) {
321 $body .= $text;
322 }
323 else {
324 $body .= "</$el>";
325 }
326 };
327 }
328
329 push(@closure_stack, $closure);
330 $closure = $new_closure;
331 } # End starthndl
332
333 sub endhndl {
334 my ($xp, $el) = @_;
335
336 return unless $in_poll;
337
338 my $lev = $xp->depth;
339
340 if ($lev == $in_poll - 1) {
341 $in_poll = 0;
342 $xp->finish;
343 return;
344 }
345
346 if ($Mode_level == $lev) {
347
348 if ($Mode eq 'pass') {
349 &$closure($xp, $Markedup_Text)
350 if (defined $closure);
351 }
352
353 $Mode = $Mode_level = 0;
354 }
355
356 if ($Mode) {
357 if ($Mode eq 'pass') {
358 $Markedup_Text .= "</$el>";
359 }
360 elsif ($Mode eq 'object') {
361 my $this = $Object;
362 if (2 == keys %$this) {
363 $this = $this->{_Text};
364 }
365
366 $Object = pop(@Ostack);
367
368 my $slot = $Object->{$el};
369 if (defined $slot) {
370 if (ref($slot) eq 'ARRAY') {
371 push(@$slot, $this);
372 }
373 else {
374 $Object->{$el} = [$slot, $this];
375 }
376 }
377 else {
378 $Object->{$el} = $this;
379 }
380 }
381
382 return;
383 }
384
385 &$closure($xp)
386 if defined $closure;
387
388 $closure = pop(@closure_stack);
389 } # End endhndl
390
391 #----------------------------------------------------------
392
393 sub text {
394 my ($xp, $data) = @_;
395
396 return unless $in_poll;
397
398 if ($Mode ) {
399
400 if ($Mode eq 'pass') {
401 my $safe = sgml_escape($data);
402
403 $Text .= $safe;
404 $Markedup_Text .= $safe;
405 }
406 elsif ($Mode eq 'object') {
407 $Object->{_Text} .= $data
408 if $data =~ /\S/;
409 }
410
411 return;
412 }
413
414 &$closure($xp, sgml_escape($data))
415 if (defined $closure);
416
417 } # End text
418
419 sub start_mode {
420 my ($xp, $mode) = @_;
421
422 if ($mode eq 'pass') {
423 $Text = '';
424 $Markedup_Text = '';
425 }
426 elsif ($mode eq 'object') {
427 $Object = {_Atts => undef,
428 _Text => undef
429 };
430 }
431
432 $Mode = $mode;
433 $Mode_level = $xp->depth;
434 } # End start_mode
435
436 sub sgml_escape {
437 my ($str) = @_;
438
439 $str =~ s/\&/\&amp;/g;
440 $str =~ s/</\&lt;/g;
441 $str =~ s/>/\&gt;/g;
442
443 $str;
444 } # End sgml_escape
445
446 ################################################################
447
448 package Poll;
449
450 sub page {
451 package main;
452
453 my ($xp, $el, $attref, $ncref) = @_;
454
455 $$ncref = sub {
456 my ($xp, $text) = @_;
457
458 if (! defined $text) {
459
460 print "p[$page_nr] ";
461
462 if (defined $last_fn) {
463 open(PAGE, ">$dir/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";
464 if ($page_nr == 2) {
465 print PAGE '<?php
466 include("common.php");
467 if (isset($do_stranice) && $do_stranice !="") {
468 Header("Location: $do_uri?a=$a");
469 exit;
470 }
471 $member_id=id_decode($a);
472 $sql="insert into '.$dir.' ( http_referer,remote_addr,user_agent, member_id ) values (\'$HTTP_REFERER\',\'$REMOTE_ADDR\',\'$HTTP_USER_AGENT\',$member_id)";
473
474 # print "<pre>$sql</pre>";
475 $result=pg_Exec($conn,fix_sql($sql));
476 $lastoid=pg_getlastoid($result);
477 $result = pg_Exec($conn,fix_sql("select id from '.$dir.' where oid=$lastoid"));
478 $row=pg_fetch_row($result,0);
479 $id=$row[0];
480 ?>';
481
482 } else {
483 print PAGE php_header($page_nr,@prelast_sql_update);
484 } # last_sql_update
485
486
487 my $next_fn=sprintf("%02d.php",$page_nr);
488 $last_page=~s/##NEXTPAGE##/$next_fn/;
489 print PAGE $last_page;
490 close(PAGE);
491
492 }
493 @prelast_sql_update=@last_sql_update;
494 @last_sql_update=@sql_update;
495 @sql_update = ();
496
497 $last_fn=sprintf("%02d.php",$page_nr);
498 $last_page="$html_header $body $html_next $html_footer";
499 # delete vars for next page
500 $page_nr++;
501 $body="";
502 }
503 }
504 } # page
505
506 sub nr {
507 package main;
508
509 my ($xp, $el, $attref, $ncref) = @_;
510
511 $pitanje_tag="";
512
513 $$ncref = sub {
514 my ($xp, $text) = @_;
515 if (defined($text)) {
516 $body.=x($text);
517 chomp $text;
518 $pitanje_tag .= x($text);
519 } else {
520 $pitanje_nr = $pitanje_tag;
521 $pitanje_nr =~ s/[^0-9a-zA-Z]//g;
522 print "$pitanje_nr ";
523 }
524 $p_suffix="";
525 };
526 } # nr
527
528
529 sub hr {
530 $body .= "<br></td></tr>$html_separator<tr><td></td><td><br>";
531 }
532
533 sub br {
534 $body .= "<br>\n";
535 }
536
537 sub pit {
538 package main;
539
540 my ($xp, $el, $attref, $ncref) = @_;
541
542 $body.="<p>";
543
544 $$ncref = sub {
545 my ($xp, $text) = @_;
546
547 if (defined $text) {
548 $body.=x($text);
549 } else {
550 $body.="</p>";
551 }
552 }
553 }
554
555 sub podpit {
556 package main;
557
558 my ($xp, $el, $attref, $ncref) = @_;
559
560 $body.='<table width="100%" cellspacing="0" cellpadding="2" border="0">';
561 $$ncref = sub {
562 my ($xp, $text) = @_;
563
564 if (defined $text) {
565 $body.=x($text);
566 } else {
567 $body.="</table>";
568 }
569 }
570 }
571
572
573 sub odg {
574 package main;
575
576 my ($xp, $el, $attref, $ncref) = @_;
577
578 $body .= "<p>";
579
580 $$ncref = sub {
581 my ($xp, $text) = @_;
582
583 if (defined $text) {
584 $body .= x($text);
585 } else {
586 $body .= "</p>";
587 }
588 }
589 }
590
591 sub php {
592 package main;
593 my ($xp, $el, $attref, $ncref) = @_;
594
595 $body.="<?php\n";
596
597 $$ncref = sub {
598 my ($xp, $text) = @_;
599
600 if (defined $text) {
601 $text=~s/ lt / < /g;
602 $text=~s/ le / <= /g;
603 $text=~s/ gt / > /g;
604 $text=~s/ ge / >= /g;
605 $body.=x($text);
606 } else {
607 $body.="\n?>\n";
608 }
609 }
610 }
611
612 sub dropdown {
613 package main;
614
615 my ($xp, $el, $attref, $ncref) = @_;
616
617 my @dropdown_data;
618
619 $$ncref = sub {
620 my ($xp, $text) = @_;
621
622 if (defined $text) {
623 chomp $text;
624 $text=~s/^\s*//g;
625 $text=~s/^[\d\.\s]+//g;
626 $text=~s/\s*$//g;
627 push @dropdown_data,x($text) if ($text ne "");
628 } else {
629 my $opt;
630 my $id=1;
631 my $p=new_pit();
632 $body.="<select name=$p >\n";
633 $body.="<option value=null>-</option>\n";
634 foreach $opt (@dropdown_data) {
635 if (defined($opt) && $opt ne "") {
636 $body.="<option value=$id>$opt</option>\n";
637 $id++;
638 }
639 }
640 $body.="</select>\n";
641
642 push @sql_create,"$p int4";
643 push @sql_update,"$p=\$$p";
644 }
645 }
646 }
647
648 sub textbox {
649 package main;
650 my ($xp, $el, $attref, $ncref) = @_;
651
652 $$ncref = sub {
653 my ($xp, $text) = @_;
654 my $size=$attref->{size};
655 $size = 25 if (! defined $size || $size == 0); # default
656 my $p=new_pit();
657 $body.="<input type=text name=$p size=".x($size)." >\n";
658 push @sql_create,"$p text";
659 push @sql_update,"$p='\$$p'";
660 }
661 }
662
663 sub radiobuttons_tab {
664 package main;
665 my ($xp, $el, $attref, $ncref) = @_;
666
667 $$ncref = sub {
668 my ($xp, $text) = @_;
669 if (! defined $text) {
670 my $nr=$attref->{nr};
671 my $p=new_pit();
672 for (my $i=1; $i<=$nr; $i++) {
673 $body.="<td><input type=radio name=$p value=$i></td> ";
674 }
675 push @sql_create,"$p int4";
676 push @sql_update,"$p=\$$p";
677 }
678 }
679 }
680
681 sub radiobuttons {
682 package main;
683 my ($xp, $el, $attref, $ncref) = @_;
684
685 my @radiobuttons_data;
686
687 $$ncref = sub {
688 my ($xp, $text) = @_;
689
690 if (defined $text) {
691 chomp $text;
692 $text=~s/^\s*//g;
693 $text=~s/^[\d\.\s]+//g;
694 $text=~s/\s*$//g;
695 push @radiobuttons_data,x($text) if ($text ne "");
696 } else {
697 my $opt;
698 my $p=new_pit();
699 my $id=1;
700 foreach $opt (@radiobuttons_data) {
701 if (defined($opt) && $opt ne "") {
702 $body.="<input type=radio name=$p value=$id> $opt<br>\n";
703 $id++;
704 }
705 }
706 push @sql_create,"$p int4";
707 push @sql_update,"$p=\$$p";
708 }
709 }
710 }
711 sub checkbox {
712 package main;
713 my ($xp, $el, $attref, $ncref) = @_;
714
715 $$ncref = sub {
716 my ($xp, $text) = @_;
717 my $p=new_pit();
718 $body.="<input type=checkbox name=$p >\n";
719 push @sql_create,"$p text";
720 push @sql_update,"$p='\$$p'";
721 }
722 }
723
724 sub checkboxes {
725 package main;
726
727 my ($xp, $el, $attref, $ncref) = @_;
728
729 my @checkboxes_data;
730
731 $$ncref = sub {
732 my ($xp, $text) = @_;
733
734
735 if (defined $text) {
736 chomp $text;
737 $text=~s/^\s*//g;
738 $text=~s/^[\d\.\s]+//g;
739 $text=~s/\s*$//g;
740 push @checkboxes_data,x($text) if ($text ne "");
741 } else {
742 my $opt;
743 my $base_p=new_pit();
744 my $id=1;
745
746 my $before=$attref->{before};
747 my $after=$attref->{after};
748 my $middle=$attref->{middle};
749 if (! $before && ! $after && ! $middle) {
750 $middle="&nbsp;";
751 $after="<br>";
752 }
753 my $hide_description=$attref->{hide_description};
754
755 foreach $opt (@checkboxes_data) {
756 if (defined($opt) && $opt ne "") {
757 $p=$base_p."_".$id;
758 $id++;
759 $body .= x($before) if ($before);
760 $body.="<input type=checkbox name=$p>";
761 $body .= x($middle) if ($middle);
762 $body .= "$opt" if (! $hide_description);
763 $body .= x($after) if ($after);
764 $body.="\n";
765
766 push @sql_create,"$p boolean";
767 push @sql_update,"$p=\$$p";
768 }
769 }
770 $php_addon[$page_nr].="fix_checkboxes($base_p,".($id-1).");";
771
772 }
773 }
774 }
775
776 # read configuration data
777 #
778 # FIX: write actually this :-)
779 sub config {
780 package main;
781 my ($xp, $el, $attref, $ncref) = @_;
782
783 $$ncref = sub {
784 my ($xp, $text) = @_;
785 $db_user=x($attref->{db_user});
786 $prefix=x($attref->{prefix});
787 }
788 }
789
790 #---------------------------------------------------------------

  ViewVC Help
Powered by ViewVC 1.1.26