/[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.9 - (show annotations)
Wed Sep 24 20:35:48 2003 UTC (20 years, 6 months ago) by dpavlin
Branch: MAIN
Changes since 1.8: +8 -0 lines
File MIME type: text/plain
added without_invitation option

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

  ViewVC Help
Powered by ViewVC 1.1.26