]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/user/fo_posting.pl
3 ################################################################################
5 # File: user/fo_posting.pl #
7 # Authors: André Malo <nd@o3media.de>, 2001-04-08 #
9 # Description: Accept new postings, display "Neue Nachricht" page #
11 ################################################################################
25 my $null = $0; $null =~ s/\\/\//g
; # for win :-(
26 $Bin = ($null =~ /^(.*)\/.*$/)?
$1 : '.';
27 $Shared = "$Bin/../shared";
28 $Config = "$Bin/config";
29 $Script = ($null =~ /^.*\/(.*)$/)?
$1 : $null;
32 # $Bin = ($null =~ /^(.*)\/.*$/)? $1 : '.';
33 # $Config = "$Bin/../../daten/forum/config";
34 # $Shared = "$Bin/../../cgi-shared";
35 # $Script = ($null =~ /^.*\/(.*)$/)? $1 : $null;
38 # setting umask, remove or comment it, if you don't need
43 use CGI
::Carp
qw(fatalsToBrowser);
51 $VERSION = do { my @r =(q
$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x
$#r, @r };
53 # load script configuration and admin default conf.
55 my $conf = read_script_conf
($Config, $Shared, $Script);
56 my $adminDefault = read_admin_conf
($conf -> {files
} -> {adminDefault
});
58 # Initialize the request
60 my $request = new Posting
::Request
($conf, $adminDefault);
62 # fetch and parse the cgi-params
64 $request -> parse_cgi
;
66 # handle errors or save the posting
68 $request -> handle_error
or $request -> save
;
74 # shorten the main file?
76 $request -> severance
;
80 ### main end ###################################################################
82 ################################################################################
83 ### Posting::Request ###########################################################
84 package Posting
::Request
;
88 use Encode
::Plain
; $Encode::Plain
::utf8
= 1; # generally convert from UTF-8
102 use Template
::Posting
;
106 ### sub new ####################################################################
108 # initialising the Posting::Request object
109 # check parameters and fill in object properties
112 my ($class, $conf, $adminDefault) = @_;
114 my $sp = $conf -> {show
} -> {Posting
};
119 admin
=> $adminDefault,
121 message_path
=> $conf -> {files
} -> {messagePath
},
122 forum_file_name
=> $conf -> {files
} -> {forum
},
125 assign
=> $sp -> {assign
},
126 template
=> $conf -> {template
},
127 form_must
=> $sp -> {form
} -> {must
},
128 form_data
=> $sp -> {form
} -> {data
},
129 form_action
=> $sp -> {form
} -> {action
},
132 template
=> new Template
$sp -> {templateFile
},
144 my $stat = cut_tail
({
145 forumFile
=> $self -> {conf
} -> {forum_file_name
},
146 messagePath
=> $self -> {conf
} -> {message_path
},
147 archivePath
=> $self -> {conf
} -> {original
} -> {files
} -> {archivePath
},
148 lockFile
=> $self -> {conf
} -> {original
} -> {files
} -> {sev_lock
},
149 adminDefault
=> $self -> {conf
} -> {admin
},
150 cachePath
=> $self -> {conf
} -> {original
} -> {files
} -> {cachePath
}
152 # die $stat->{(keys %$stat)[0]} if (%$stat);
156 ### sub response ###############################################################
158 # print the response to STDOUT
164 my $formdata = $self -> {conf
} -> {form_data
};
165 my $formact = $self -> {conf
} -> {form_action
};
166 my $template = $self -> {template
};
167 my $assign = $self -> {conf
} -> {assign
};
168 my $q = $self -> {cgi_object
};
170 # fill out the form field names
173 for (keys %$formdata) {
174 $pars -> {$formdata -> {$_} -> {assign
} -> {name
}} = plain
($formdata -> {$_} -> {name
}) if (
175 exists($formdata -> {$_} -> {name
})
176 and exists ($formdata -> {$_} -> {assign
})
177 and exists ($formdata -> {$_} -> {assign
} -> {name
})
181 # response the 'new message' page
183 if ($self -> {response
} -> {new_thread
}) {
185 # fill in the default form data
189 for (keys %$formdata) {
190 unless (exists ($formdata -> {$_} -> {type
}) and $formdata -> {$_} -> {type
} eq 'internal') {
191 if (exists ($formdata -> {$_} -> {default}) and exists ($formdata -> {$_} -> {assign
} -> {value
})) {
192 $default -> {$formdata -> {$_} -> {assign
} -> {value
}}
193 = $formdata -> {$_} -> {default};
195 elsif (exists($formdata -> {$_} -> {values})) {
196 my ($_name, $val) = $_;
197 $val = exists ($formdata -> {$_} -> {default})
198 ?
$formdata -> {$_} -> {default}
200 $default -> {$formdata -> {$_} -> {assign
} -> {value
}}
201 = $self -> {template
} -> list
(
204 { $assign -> {optval
} => plain
($_),
205 ((defined $val and $_ eq $val)
206 ?
($assign -> {optsel
} => 1)
210 } @
{$formdata -> {$_name} -> {values}}
217 print $q -> header
(-type
=> 'text/html');
218 print ${$template -> scrap
(
220 { $formdata->{uniqueID
} ->{assign
}->{value
} => plain
(unique_id
),
221 $formdata->{quoteChar
} ->{assign
}->{value
} => 'ÿ'.plain
($self -> {conf
} -> {admin
} -> {View
} -> {quoteChars
}),
222 $formact->{post
}->{assign
} => $formact->{post
}->{url
},
230 # check the response -> doc
232 unless ($self -> {response
} -> {doc
}) {
234 spec
=> 'unknown_error',
238 $self -> handle_error
;
240 unless ($self -> {response
} -> {doc
}) {
241 $self -> jerk
('While producing the HTML response an unknown error has occurred.');
246 # ok, print the response document to STDOUT
248 print $q -> header
(-type
=> 'text/html');
249 print ${$template -> scrap
(
250 $self -> {response
} -> {doc
},
252 $self -> {response
} -> {pars
}
259 ### sub handle_error ###########################################################
261 # analyze error data and create content for the response method
263 # Return: true if error detected
269 my $spec = $self -> {error
} -> {spec
};
271 return unless ($spec);
273 my $assign = $self -> {conf
} -> {assign
};
274 my $formdata = $self -> {conf
} -> {form_data
};
276 my $desc = $self -> {error
} -> {desc
} || '';
277 my $type = $self -> {error
} -> {type
};
280 if (exists ($formdata -> {$desc})
281 and exists ($formdata -> {$desc} -> {assign
} -> {$spec})) {
282 $emsg = $formdata -> {$desc} -> {assign
} -> {$spec};
285 $emsg = $assign -> {$spec} || '';
290 if ($type eq 'fatal') {
291 $self -> {response
} -> {doc
} = $assign -> {docFatal
};
292 $self -> {response
} -> {pars
} = {
293 $assign -> {errorMessage
} => $self -> {template
} -> insert
($emsg)
298 # user is able to repair his request
300 elsif ($type eq 'repeat' or $type eq 'fetch') {
301 $self -> {response
} -> {doc
} = $assign -> {docError
};
302 $self -> fillout_form
;
303 $self -> {response
} -> {pars
} -> {$assign -> {errorMessage
}} = $self -> {template
} -> insert
($emsg);
304 my $num = $spec eq 'too_long'
305 ?
$formdata -> {$desc} -> {maxlength
}
306 : ($spec eq 'too_short'
307 ?
$formdata -> {$desc} -> {minlength
}
311 $self -> {response
} -> {pars
} -> {$assign -> {charNum
}} = $num
318 ### sub fillout_form ###########################################################
320 # fill out the form using available form data
327 my $assign = $self -> {conf
} -> {assign
};
328 my $formdata = $self -> {conf
} -> {form_data
};
329 my $formact = $self -> {conf
} -> {form_action
};
330 my $q = $self -> {cgi_object
};
335 $pars -> {$formact -> {post
} -> {assign
}} = $formact -> {post
} -> {url
};
337 for (keys %$formdata) {
338 if ($_ eq 'quoteChar') {
339 $pars -> {$formdata->{$_}->{assign
}->{value
}}
340 = 'ÿ'.plain
($q -> param
($formdata -> {quoteChar
} -> {name
}) or '');
342 elsif (exists ($formdata -> {$_} -> {name
})) {
343 unless (exists ($formdata -> {$_} -> {values})) {
344 $pars -> {$formdata -> {$_} -> {assign
} -> {value
}}
345 = plain
($q -> param
($formdata -> {$_} -> {name
}));
349 $pars -> {$formdata -> {$_} -> {assign
} -> {value
}}
350 = $self -> {template
} -> list
(
353 { $assign -> {optval
} => plain
($_),
354 (( $_ eq $q -> param
($formdata -> {$_name} -> {name
}))
355 ?
($assign -> {optsel
} => 1)
359 } @
{$formdata -> {$_name} -> {values}}
366 $self -> {response
} -> {pars
} = $pars;
370 ### sub save ###################################################################
373 # check on legal reply or dupe is released here
380 # if an empty 'new message' document, there's nothing to save
382 return if ($self -> {response
} -> {new_thread
});
384 $self -> {check_success
} = 0;
386 # lock and load the forum main file
388 if ($self -> load_main_file
) {
390 # if a reply - is it legal?
393 if ($self -> check_reply_dupe
) {
395 unless ($self -> {response
} -> {reply
} or $self -> {response
} -> {new
}) {
396 # don't know, if we any time come to this branch
397 # the script is probably broken
400 spec
=> 'unknown_error',
406 my $formdata = $self -> {conf
} -> {form_data
};
407 my $q = $self -> {cgi_object
};
408 my $f = $self -> {forum
};
410 quoteChars
=> $q -> param
($formdata -> {quoteChar
} -> {name
}),
411 uniqueID
=> $q -> param
($formdata -> {uniqueID
} -> {name
}),
413 ip
=> $q -> remote_addr
,
414 forumFile
=> $self -> {conf
} -> {forum_file_name
},
415 messagePath
=> $self -> {conf
} -> {message_path
},
416 lastThread
=> $f -> {last_thread
},
417 lastMessage
=> $f -> {last_message
},
418 parsedThreads
=> $f -> {threads
},
420 messages
=> $self -> {conf
} -> {template
} -> {messages
} || {},
421 base_uri
=> $self -> {conf
} -> {original
} -> {files
} -> {forum_base
}
424 # set the variables if defined..
427 author
=> 'posterName',
428 email
=> 'posterEmail',
429 category
=> 'posterCategory',
430 subject
=> 'posterSubject',
431 body
=> 'posterBody',
432 homepage
=> 'posterURL',
433 image
=> 'posterImage'
437 $pars -> {$_} = $q -> param
($formdata -> {$may{$_}} -> {name
})
438 if (defined $q -> param
($formdata -> {$may{$_}} -> {name
}));
441 my ($stat, $xml, $mid, $tid);
443 # we've got a fup if it's a reply
445 if ($self -> {response
} -> {reply
}) {
446 $pars -> {parentMessage
} = $self -> {fup_mid
};
447 $pars -> {thread
} = $self -> {fup_tid
};
448 ($stat, $xml, $mid, $tid) = write_reply_posting
($pars);
451 ($stat, $xml, $mid, $tid) = write_new_thread
($pars);
462 my $cache = new Posting
::Cache
($self->{conf
}->{original
}->{files
}->{cachePath
});
463 $cache -> add_posting
(
464 { thread
=> ($tid =~ /(\d+)/)[0],
465 posting
=> ($mid =~ /(\d+)/)[0]
469 $self -> {check_success
} = 1;
470 my $thx = $self -> {conf
} -> {show_posting
} -> {thanx
};
472 # define special response data
474 $self -> {response
} -> {doc
} = $self -> {conf
} -> {assign
} -> {docThx
};
475 $self -> {response
} -> {pars
} = {
476 $thx -> {time} => plain
(hr_time
($time)),
477 $thx -> {body
} => message_as_HTML
(
481 assign
=> $self -> {conf
} -> {assign
},
482 quoteChars
=> $q -> param
($formdata -> {quoteChar
} -> {name
}),
483 quoting
=> $self -> {conf
} -> {admin
} -> {View
} -> {quoting
}
487 # set the variables if defined..
490 author
=> 'posterName',
491 email
=> 'posterEmail',
492 category
=> 'posterCategory',
493 subject
=> 'posterSubject',
494 homepage
=> 'posterURL',
495 image
=> 'posterImage'
499 my $x = $q -> param
($formdata -> {$may{$_}} -> {name
});
500 $x = '' unless (defined $x);
501 $self -> {response
} -> {pars
} -> {$thx -> {$_}} = plain
($x)
502 if (defined $thx -> {$_});
509 # unlock forum main file
511 if ($self -> {forum
} -> {flocked
}) {
512 $self -> {forum
} -> {flocked
} -> unlock
;
513 $self -> {forum
} -> {flocked
} = 0;
516 $self -> handle_error
unless $self -> {check_success
};
521 ### sub parse_cgi ##############################################################
523 # fetch and decode cgi-parameters,
524 # find out the kind of response requested by the user (new message, reply)
531 # create the CGI object
533 $self -> {cgi_object
} = new CGI
;
537 $self -> {check_success
} = $self -> check_cgi
;
542 ### sub load_main_file #########################################################
544 # load and parse the forum main file
546 # Return: Success (true/false)
550 my $forum = new Lock
($self -> {conf
} -> {forum_file_name
});
552 unless ($forum -> lock(LH_EXCL
)) {
553 unless ($forum -> masterlocked
) {
554 # occupied or no w-bit set for the directory..., hmmm
566 spec
=> 'master_lock',
573 $self -> {forum
} -> {flocked
} = $forum;
574 ( $self -> {forum
} -> {threads
},
575 $self -> {forum
} -> {last_thread
},
576 $self -> {forum
} -> {last_message
},
577 $self -> {forum
} -> {dtd
},
578 $self -> {forum
} -> {unids
}
579 ) = get_all_threads
($self -> {conf
} -> {forum_file_name
}, KEEP_DELETED
);
586 ### sub check_reply_dupe #######################################################
588 # check whether a reply is legal
589 # (followup posting must exists)
591 # check whether this form request is a dupe
592 # (unique id already exists)
594 # Return: Status Code (Bool)
596 sub check_reply_dupe
{
600 # return true unless it's not a reply
604 $self -> {response
} -> {reply
}
605 or $self -> {response
} -> {new
}
608 if ($self -> {response
} -> {reply
}) {
610 my ($threads, $ftid, $fmid, $i, %msg) = (
611 $self -> {forum
} -> {threads
},
616 # thread doesn't exist
618 unless (exists($threads -> {$ftid})) {
626 # build a reverse lookup hash (mid => number in array)
627 # and ignore invisible messages
628 # (users can't reply to "deleted" msg)
630 for ($i=0; $i < @
{$threads -> {$ftid}}; $i++) {
632 if ($threads -> {$ftid} -> [$i] -> {deleted
}) {
633 $i+=$threads -> {$ftid} -> [$i] -> {answers
};
636 $msg{$threads -> {$ftid} -> [$i] -> {mid
}}=$i;
640 # message doesn't exist
642 unless (exists($msg{$fmid})) {
650 # build a unique id lookup hash
651 # use the unids of parent message's kids
653 %unids = map {$_ => 1} @
{$threads -> {$ftid} -> [$msg{$fmid}] -> {unids
}};
656 # build a unique id lookup hash, too
657 # but use only the level-zero-messages
659 %unids = map {$_ => 1} @
{$self -> {forum
} -> {unids
}};
665 $self -> {cgi_object
} -> param
(
666 $self -> {conf
} -> {form_data
} -> {uniqueID
} -> {name
})})) {
678 ### sub check_cgi ##############################################################
680 # cgi params are like raw eggs...
682 # Return: Status Code (Bool)
683 # creates content for the handle_error method if anything fails
688 # count the submitted keys and get the keys themselves
690 my %got_keys = map {($_ => 1)} $self -> {cgi_object
} -> param
;
691 my $cnt_got_keys = keys %got_keys;
692 my $formdata = $self -> {conf
} -> {form_data
};
693 my $formmust = $self -> {conf
} -> {form_must
};
695 # user requested the 'new thread' page
696 # (no params but perhaps the user-ID have been submitted)
698 if ($cnt_got_keys == 0 or (
699 exists ($formdata -> {userID
})
700 and $cnt_got_keys == 1
701 and $got_keys{$formdata -> {userID
} -> {name
}}
703 $self -> {response
} -> {new_thread
} = 1;
704 $self -> {check_success
} = 1;
708 # now we know, we've got a filled out form
709 # we do the following steps to check it:
711 # 1st: create a reverse Hash (CGI-key - identifier)
712 # 2nd: did we get _all_ must-keys?
713 # check whether reply or new message request
714 # 3rd: did we get too many keys?
715 # 4th: do _all_ submitted values accord to
717 # fetch the "missing" keys
723 exists($formdata -> {$_} -> {name
})
724 ?
($formdata -> {$_} -> {name
} => $_)
730 $self -> {response
} -> {reply
} = $got_keys {$formdata -> {followUp
} -> {name
}}?
1 : 0;
731 $self -> {response
} -> {new
} = not $self -> {response
} -> {reply
};
733 # define the fetch array (values to fetch from parent message)
735 $self -> {fetch
} = [];
737 for ( @
{$formmust -> {$self -> {response
} -> {reply
}?
'reply':'new'}} ) {
739 unless ($got_keys {$formdata -> {$_} -> {name
}}) {
741 # only miss the key unless we're able to fetch it from parent posting
744 not $self -> {response
} -> {reply
}
745 or $formdata -> {$_} -> {errorType
} eq 'fetch') {
748 spec
=> 'missing_key',
755 # keep in mind to fetch the value later
757 push @
{$self -> {fetch
}} => $_;
762 # I'm lazy - I know...
763 my $q = $self -> {cgi_object
};
768 unless (exists ($name {$_})) {
770 spec
=> 'unexpected_key',
780 unless ($self -> decode_param
) {
782 spec
=> 'unknown_encoding',
788 if ($self -> {response
} -> {reply
}) {
790 # get the parent-identifiers if we got a reply request
792 my ($ftid, $fmid) = split /;/ => $q -> param
($formdata -> {followUp
} -> {name
}) => 2;
794 unless ($ftid =~ /^\d+$/ and $fmid =~ /^\d+$/) {
796 spec
=> 'unknown_followup',
801 $self -> {fup_tid
} = $ftid;
802 $self -> {fup_mid
} = $fmid;
804 # fetch the missing keys
805 # if it fails, they're too short, too... ;)
808 $got_keys{$formdata -> {$_} -> {name
}} = 1 for (@
{$self -> {fetch
}});
811 # now we can check on length, type etc.
813 for (keys %got_keys) {
815 # we are sure, we've got only one value for one key
817 my $val = $q -> param
($_);
819 $val =~ s/\302\240/ /g; # convert nbsp (UTF-8 encoded) into normal spaces
820 $val =~ s/\015\012|\015|\012/ /g # convert \n into spaces unless it's a multiline field
822 exists ($formdata -> {$name {$_}} -> {type
})
823 and $formdata -> {$name {$_}} -> {type
} eq 'multiline-text'
826 $q -> param
($_ => $val); # write it back
830 if (length $val > $formdata -> {$name {$_}} -> {maxlength
}) {
834 type
=> $formdata -> {$name {$_}} -> {errorType
}
836 $self -> kill_param
or return;
840 # (only check if there's defined a minimum length)
842 if (exists ($formdata -> {$name {$_}} -> {minlength
})) {
844 # kill the whitespaces to get only the visible characters...
846 (my $val_ww = $val) =~ s/\s+//g;
848 if (exists ($formdata -> {$name {$_}} -> {type
}) and $formdata -> {$name {$_}} -> {type
} eq 'name') {
849 $val_ww =~ y/a-zA-Z//cd;
852 # my @badlist = map {qr/\Q$_/i} qw (
853 # # insert badmatchlist here
856 # push @badlist => map {qr/\b\Q$_\E\b/i} qw(
857 # # insert badwordlist here
861 if ($val_ww =~ /$_/) {
872 if (length $val_ww < $formdata -> {$name {$_}} -> {minlength
}) {
876 type
=> $formdata -> {$name {$_}} -> {errorType
}
878 $self -> kill_param
or return;
882 # check the values on expected kinds of content
883 # (email, http-url, url, option)
885 if (exists ($formdata -> {$name {$_}} -> {type
}) and length $val) {
886 if ($formdata -> {$name {$_}} -> {type
} eq 'email' and not is_email
$val) {
888 spec
=> 'wrong_mail',
890 type
=> $formdata -> {$name {$_}} -> {errorType
}
892 $self -> kill_param
or return;
895 elsif ($formdata -> {$name {$_}} -> {type
} eq 'http-url' and not is_URL
$val => 'http') {
897 spec
=> 'wrong_http_url',
899 type
=> $formdata -> {$name {$_}} -> {errorType
}
901 $self -> kill_param
or return;
904 elsif ($formdata -> {$name {$_}} -> {type
} eq 'url' and not is_URL
$val => ':ALL') {
908 type
=> $formdata -> {$name {$_}} -> {errorType
}
910 $self -> kill_param
or return;
913 elsif ($formdata -> {$name {$_}} -> {type
} eq 'unique-id' and not may_id
$val) {
915 spec
=> 'wrong_unique_id',
917 type
=> $formdata -> {$name {$_}} -> {errorType
}
919 print STDERR
"Manipuliert!";
920 $self -> kill_param
or return;
924 if (exists ($formdata -> {$name {$_}} -> {values})
925 and not exists ({map {$_ => undef} @
{$formdata -> {$name {$_}} -> {values}}} -> {$val})) {
929 type
=> $formdata -> {$name {$_}} -> {errorType
}
931 $self -> kill_param
or return;
938 ### sub kill_param #############################################################
940 # kill the param (set it on '') if wrong and declared as 'kill' in config file
942 # Return: true if killed
948 if ($self -> {conf
} -> {form_data
} -> {$self -> {error
} -> {desc
}} -> {errorType
} eq 'kill') {
949 $self -> {cgi_object
} -> param
($self -> {conf
} -> {form_data
} -> {$self -> {error
} -> {desc
}} -> {name
} => '');
950 $self -> {error
} = {};
957 ### sub fetch ##################################################################
959 # fetch "missing" keys from parent posting
963 my $q = $self -> {cgi_object
};
964 my $formdata = $self -> {conf
} -> {form_data
};
966 if (@
{$self -> {fetch
}}) {
967 my $thread = new Lock
($self -> {conf
} -> {message_path
}.'t'.$self -> {fup_tid
}.'.xml');
969 if ($thread -> lock (LH_SHARED
)) {
970 my $xml = parse_xml_file
($thread -> filename
);
974 my $mnode = get_message_node
($xml, 't'.$self -> {fup_tid
}, 'm'.$self -> {fup_mid
});
976 my $header = get_message_header
($mnode);
978 $q -> param
($formdata -> {$_} -> {name
} => $header -> {$formdata -> {$_} -> {header
}})
979 for (@
{$self -> {fetch
}});
988 # fillout the values with an empty string
990 $q -> param
($formdata -> {$_} -> {name
} => '')
991 for (@
{$self -> {fetch
}});
996 ### sub decode_param ###########################################################
998 # convert submitted form data into UTF-8
999 # unless it's not encoded yet
1001 # Return: Status Code (Bool)
1002 # false if unknown encoding (like UTF-7 for instance)
1007 my $q = $self -> {cgi_object
};
1008 my $formdata = $self -> {conf
} -> {form_data
};
1010 my $code = $q -> param
($formdata -> {quoteChar
} -> {name
});
1013 # Latin 1 (we hope so - there's no real way to find out :-( )
1014 if ($code =~ /^\377/) {
1015 $q -> param
($_ => map {toUTF8
($_)} $q -> param
($_)) for ($q -> param
);
1018 # UTF-8 is (probably) correct,
1019 # other encodings we don't know and fail
1020 return unless $code =~ /^\303\277/;
1023 # remove the ÿ (encoded as UTF-8) from quotechars
1024 $q -> param
($formdata -> {quoteChar
} -> {name
}
1025 => substr $q -> param
($formdata -> {quoteChar
} -> {name
}),2);
1027 # ok, params now should be UTF-8 encoded
1032 my $text = $_[1] || 'An error has occurred.';
1034 Content-type: text/plain
1041 We will fix it as soon as possible. Thank you for your patience.
1050 ### end of fo_posting.pl #######################################################
patrick-canterino.de