/[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.5 - (show annotations)
Thu Apr 24 17:55:17 2003 UTC (21 years ago) by dpavlin
Branch: MAIN
Changes since 1.4: +1 -10 lines
File MIME type: text/plain
moved common code to common.pl; added script to send invitation e-mails

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

  ViewVC Help
Powered by ViewVC 1.1.26