| 1 | ############################################################################## | ||
| 2 | # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. # | ||
| 3 | # # | ||
| 4 | # This program is free software; you can redistribute it and/or # | ||
| 5 | # modify it under the terms of the GNU General Public License # | ||
| 6 | # as published by the Free Software Foundation; either version 2 # | ||
| 7 | # of the License, or (at your option) any later version. # | ||
| 8 | # # | ||
| 9 | # This program is distributed in the hope that it will be useful, # | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
| 12 | # GNU General Public License for more details. # | ||
| 13 | # # | ||
| 14 | # You should have received a copy of the GNU General Public License # | ||
| 15 | # along with this program; if not, write to the Free Software # | ||
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.# | ||
| 17 | # # | ||
| 18 | # Jon Howell can be contacted at: # | ||
| 19 | # 6211 Sudikoff Lab, Dartmouth College # | ||
| 20 | # Hanover, NH 03755-3510 # | ||
| 21 | # jonh@cs.dartmouth.edu # | ||
| 22 | # # | ||
| 23 | # An electronic copy of the GPL is available at: # | ||
| 24 | # http://www.gnu.org/copyleft/gpl.html # | ||
| 25 | # # | ||
| 26 | ############################################################################## | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | |||
| 30 | package FAQ::OMatic::submitPart; | ||
| 31 | |||
| 32 | use CGI; | ||
| 33 | use FAQ::OMatic::Item; | ||
| 34 | use FAQ::OMatic::I18N; | ||
| 35 | use FAQ::OMatic; | ||
| 36 | use FAQ::OMatic::Auth; | ||
| 37 | |||
| 38 | sub main { | ||
| 39 | my $cgi = FAQ::OMatic::dispatch::cgi(); | ||
| 40 | my $removed = 0; | ||
| 41 | |||
| 42 | my $params = FAQ::OMatic::getParams($cgi); | ||
| 43 | |||
| 44 | FAQ::OMatic::mirrorsCantEdit($cgi, $params); | ||
| 45 | |||
| 46 | my $item = new FAQ::OMatic::Item($params->{'file'}); | ||
| 47 | if ($item->isBroken()) { | ||
| 48 | FAQ::OMatic::gripe('error', gettexta("The file (%0) doesn't exist.", $params->{'file'})); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $insertpart = $params->{'_insertpart'} || $params->{'s_insertpart'}; | ||
| 52 | # have to check the s_ case, because we don't extract the s_-encoded | ||
| 53 | # parameters until later. (Any reason not to do them here?) | ||
| 54 | |||
| 55 | if (not $insertpart) { | ||
| 56 | # if inserting a part, we don't have to check -- inserts can | ||
| 57 | # come out of order and it's not too bad. | ||
| 58 | $item->checkSequence($params); | ||
| 59 | } | ||
| 60 | $item->incrementSequence(); | ||
| 61 | |||
| 62 | my $partnum = $params->{'partnum'}; | ||
| 63 | my $part; | ||
| 64 | if ($partnum >= 0) { | ||
| 65 | $part = $item->getPart($partnum); | ||
| 66 | if (not $part) { | ||
| 67 | FAQ::OMatic::gripe('error', "Part number \"$partnum\" in \"" | ||
| 68 | .$params->{'file'}."\" doesn't exist."); | ||
| 69 | } | ||
| 70 | } else { | ||
| 71 | $partnum = -1; | ||
| 72 | } | ||
| 73 | if (($partnum < 0) and (not $insertpart)) { | ||
| 74 | FAQ::OMatic::gripe('error', "Part number \"$partnum\" in \"" | ||
| 75 | .$params->{'file'}."\" doesn't exist."); | ||
| 76 | } | ||
| 77 | |||
| 78 | # if we're inserting a part, create a new one to hold the data | ||
| 79 | # and stuff it into the item. (Don't worry about the permissions | ||
| 80 | # check being later -- we're only modifying the in-memory copy; | ||
| 81 | # we haven't written the item out yet.) | ||
| 82 | if ($insertpart) { | ||
| 83 | $part = new FAQ::OMatic::Part(); | ||
| 84 | splice @{$item->{'Parts'}}, $partnum+1, 0, $part; | ||
| 85 | $item->updateDirectoryHint(); | ||
| 86 | } | ||
| 87 | |||
| 88 | # verify that an evil cache hasn't truncated a POST | ||
| 89 | if ((($params->{'_zzverify'}||'') ne 'zz') | ||
| 90 | and (not $params->{'s_textInFile'})) { | ||
| 91 | FAQ::OMatic::gripe('error', | ||
| 92 | "Your browser or WWW cache has truncated your POST."); | ||
| 93 | } | ||
| 94 | |||
| 95 | # select source of data: file or textarea | ||
| 96 | if (($params->{'_inputType'}||'') eq 'file') { | ||
| 97 | # THANKS: John Nolan's fix applies here, too. | ||
| 98 | my $formFileHandle = $cgi->param('_newTextFile'); | ||
| 99 | $params->{'_newText'} = ''; # scrap <textarea> text and load file | ||
| 100 | my $sizesum = 0; | ||
| 101 | # if the nextline gives you an error, update your CGI.pm. | ||
| 102 | while (defined(my $line = <$formFileHandle>)) { | ||
| 103 | $sizesum += length($line); | ||
| 104 | if ($sizesum > 64*1024) { | ||
| 105 | FAQ::OMatic::gripe('error', | ||
| 106 | "Your file was greater than 64K long."); | ||
| 107 | } | ||
| 108 | $line =~ s/[^ -~\t\r\n\x80-\xff]//gs; # limit to printable characters | ||
| 109 | $params->{'_newText'} .= $line; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | # verify permissions | ||
| 114 | my $authFailed = ''; | ||
| 115 | my $perm; | ||
| 116 | if ($insertpart or ($part->{'Text'} =~ m/^\s*$/s)) { | ||
| 117 | # if the part is currently empty, the user is doing no more than | ||
| 118 | # an add. But we always send users back to editPart, since the | ||
| 119 | # only way to get this far and fail is by cheating, so a weird | ||
| 120 | # error message ain't my fault. :v) | ||
| 121 | $perm = 'PermAddPart'; | ||
| 122 | } else { | ||
| 123 | $perm = 'PermEditPart'; | ||
| 124 | } | ||
| 125 | $authFailed = FAQ::OMatic::Auth::checkPerm($item, $perm); | ||
| 126 | |||
| 127 | if ((!$authFailed) && ($part->{'Type'} eq 'HTML')) { | ||
| 128 | $authFailed = FAQ::OMatic::Auth::checkPerm($item, 'PermUseHTML'); | ||
| 129 | } | ||
| 130 | |||
| 131 | my @rcvFields = ('_HideAttributions', '_Type', '_insertpart'); | ||
| 132 | my $fn; | ||
| 133 | if ($authFailed) { | ||
| 134 | # There was a permission problem. Write the user's data to | ||
| 135 | # a file, and send them to the authentication page. | ||
| 136 | # That way, they can come right back here and submit without | ||
| 137 | # having to type their data again, which would bite. | ||
| 138 | if ($params->{'s_textInFile'}) { | ||
| 139 | # hey, already been here once -- just let the (now "permanent") | ||
| 140 | # arguments pass through to authenticate again. | ||
| 141 | } else { | ||
| 142 | # write the _newText out to a file, instead of passing through | ||
| 143 | # as a variable, since we don't trust browsers not to abuse it. | ||
| 144 | # And it could make a very big URL. | ||
| 145 | for ($fn = FAQ::OMatic::nonce(); | ||
| 146 | -f "$FAQ::OMatic::Config::metaDir/submitTmp.$fn"; | ||
| 147 | $fn++) {} # skip until we find an unused filename | ||
| 148 | if (not open(TMPF, ">$FAQ::OMatic::Config::metaDir/submitTmp.$fn")) { | ||
| 149 | # shoot -- this trick isn't working! Just send the | ||
| 150 | # user through the usual channels, and they'll have to retype. | ||
| 151 | FAQ::OMatic::Auth::ensurePerm('-item'=>$item, | ||
| 152 | '-operation'=>$perm, | ||
| 153 | '-restart'=>'editPart', | ||
| 154 | '-cgi'=>$cgi, | ||
| 155 | '-failexit'=>1); | ||
| 156 | } | ||
| 157 | print TMPF $params->{'_newText'}; | ||
| 158 | close TMPF; | ||
| 159 | $params->{'s_textInFile'} = $fn; | ||
| 160 | |||
| 161 | # turn input _transient params into s_notSoTransient args, so | ||
| 162 | # they make the round-trip through authenticate. We'll delete | ||
| 163 | # them from the params list when we finally succeed. | ||
| 164 | my $i; | ||
| 165 | foreach $i (@rcvFields) { | ||
| 166 | $params->{"s$i"} = $params->{$i}; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | # attach all the _params | ||
| 170 | my $url = FAQ::OMatic::makeAref('authenticate', | ||
| 171 | {'_restart'=>FAQ::OMatic::commandName(), '_reason'=>$authFailed}, | ||
| 172 | 'url'); | ||
| 173 | FAQ::OMatic::redirect($cgi, $url); | ||
| 174 | } | ||
| 175 | |||
| 176 | # check for args coming from an authentication detour, and convert | ||
| 177 | # back to the ones we expect | ||
| 178 | if ($params->{'s_textInFile'}) { | ||
| 179 | $params->{'s_textInFile'} =~ m#([^/]*)#gs; | ||
| 180 | $fn = "$FAQ::OMatic::Config::metaDir/submitTmp.".$1; | ||
| 181 | if (not open(TMPF, $fn)) { | ||
| 182 | # shoot, the save-file has disappeared ... send the user | ||
| 183 | # back to editPart, poor slob. | ||
| 184 | killS_Params($params); | ||
| 185 | my $url = FAQ::OMatic::makeAref('editPart', {}, | ||
| 186 | 'url'); | ||
| 187 | FAQ::OMatic::redirect($cgi, $url); | ||
| 188 | } | ||
| 189 | my @lines = <TMPF>; | ||
| 190 | close TMPF; | ||
| 191 | unlink $fn; | ||
| 192 | $params->{'_newText'} = join('', @lines); | ||
| 193 | my $i; | ||
| 194 | foreach $i (@rcvFields) { | ||
| 195 | $params->{$i} = $params->{"s$i"}; | ||
| 196 | } | ||
| 197 | killS_Params($params); # get rid of these vestigial params | ||
| 198 | } | ||
| 199 | |||
| 200 | # Finally, input the arguments we originally expected into the part. | ||
| 201 | $part->setProperty('HideAttributions', | ||
| 202 | defined $params->{'_HideAttributions'} ? 1 : ''); | ||
| 203 | # remove extra title info from faqomatic: link | ||
| 204 | $params->{'_newText'} =~ s/faqomatic\[[^\[\]]*\]:/faqomatic:/sg; | ||
| 205 | if ($part->{'Type'} eq 'directory') { | ||
| 206 | if ($params->{'_Type'} ne 'directory') { | ||
| 207 | FAQ::OMatic::gripe('error', "Can't change Type from directory."); | ||
| 208 | } | ||
| 209 | # verify that new and old directory text contain identical set of | ||
| 210 | # faqomatic: links | ||
| 211 | my @oldLinks = sort($part->getLinks()); | ||
| 212 | my @newLinks = sort( | ||
| 213 | FAQ::OMatic::Part::getLinksFromText($params->{'_newText'})); | ||
| 214 | # Perl sucks sometimes. If you leave the parens of the sort() | ||
| 215 | # in the previous statement, the secret magical Perl bit | ||
| 216 | # 'wantarray' does not get set, and so the return value from | ||
| 217 | # getLinks...() gets all globbed into a single scalar, glued | ||
| 218 | # together with spaces. Aaaargh! I hate how Perl second-guesses me. | ||
| 219 | |||
| 220 | my $error = 0; | ||
| 221 | if (scalar @oldLinks != scalar @newLinks) { | ||
| 222 | $error = 1; | ||
| 223 | } else { | ||
| 224 | my $i; | ||
| 225 | for ($i=0; $i<scalar(@oldLinks); $i++) { | ||
| 226 | if ($oldLinks[$i] ne $newLinks[$i]) { | ||
| 227 | $error = 1; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | if ($error) { | ||
| 232 | FAQ::OMatic::gripe('error', "When editing a directory, you " | ||
| 233 | ."may not alter the set of faqomatic:<i>item</i> links in " | ||
| 234 | ."directory." | ||
| 235 | ."<p>".join('',(map {"<br>old: $_\n"} @oldLinks)) | ||
| 236 | ."<p>".join('',(map {"<br>new: $_\n"} @newLinks)) | ||
| 237 | ); | ||
| 238 | } | ||
| 239 | |||
| 240 | # the new directory passes the test | ||
| 241 | $part->setText($params->{'_newText'}); | ||
| 242 | |||
| 243 | # all the children in the list may now have different siblings, | ||
| 244 | # which means we need to recompute their dependencies and | ||
| 245 | # regenerate their cached html. | ||
| 246 | $item->updateAllChildren(); | ||
| 247 | } else { | ||
| 248 | if ($params->{'_Type'} eq 'directory') { | ||
| 249 | FAQ::OMatic::gripe('error', "Can't force Type to directory."); | ||
| 250 | } | ||
| 251 | $part->setText($params->{'_newText'}); | ||
| 252 | $part->setProperty('Type', $params->{'_Type'} || ''); | ||
| 253 | } | ||
| 254 | $part->touch(); # update modification date | ||
| 255 | |||
| 256 | # in any case, the user has co-authored the document | ||
| 257 | my ($id,$aq) = FAQ::OMatic::Auth::getID(); | ||
| 258 | $part->addAuthor($id) if ($id); | ||
| 259 | $item->saveToFile(); | ||
| 260 | |||
| 261 | # partnum may be invalid now, if removeSubItem() happened | ||
| 262 | if ($removed) { | ||
| 263 | $item->notifyModerator($cgi, "removed the directory, making an " | ||
| 264 | ."answer item from a category item."); | ||
| 265 | } elsif ($insertpart) { | ||
| 266 | $item->notifyModerator($cgi, 'inserted a part', $partnum+1); | ||
| 267 | $item->notifyNotifier($cgi, 'inserted a part', $partnum+1); | ||
| 268 | } else { | ||
| 269 | $item->notifyModerator($cgi, 'edited a part', $partnum); | ||
| 270 | $item->notifyNotifier($cgi, 'edited a part', $partnum); | ||
| 271 | } | ||
| 272 | |||
| 273 | if (FAQ::OMatic::getParam($params, 'isapi')) { | ||
| 274 | # caller is a program; doesn't want a redirect to an HTML file! | ||
| 275 | # provide textual results | ||
| 276 | print FAQ::OMatic::header($cgi, '-type'=>'text/plain') | ||
| 277 | ."isapi=1\n" | ||
| 278 | ."file=".$item->{'filename'}."\n" | ||
| 279 | ."checkSequenceNumber=".$item->{'SequenceNumber'}."\n"; | ||
| 280 | FAQ::OMatic::myExit(0); | ||
| 281 | } | ||
| 282 | |||
| 283 | my $url = FAQ::OMatic::makeAref('-command'=>'faq', | ||
| 284 | '-params'=>$params, | ||
| 285 | '-changedParams'=>{'partnum'=>'', 'checkSequenceNumber'=>''}, | ||
| 286 | '-refType'=>'url'); | ||
| 287 | # eliminate things that were in our input form that weren't | ||
| 288 | # automatically transient (_ prefix) | ||
| 289 | FAQ::OMatic::redirect($cgi, $url); | ||
| 290 | } | ||
| 291 | |||
| 292 | # when auth fails between editPart and submitPart, we call through | ||
| 293 | # the authenticate script. To keep params from getting lost along the | ||
| 294 | # way, we convert them from "_param" transient form to "s_param" form, | ||
| 295 | # which survives makeAref(). This sub kills off those s_* guys once we're | ||
| 296 | # done with 'em. | ||
| 297 | sub killS_Params { | ||
| 298 | my $params = shift; | ||
| 299 | foreach my $i (keys %{$params}) { | ||
| 300 | next if (not $i =~ m/^s_/); | ||
| 301 | delete $params->{$i}; | ||
| 302 | } | ||
| 303 | } | ||
| 304 | |||
| 305 | 1; |