| 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::submitItem; | ||
| 31 | |||
| 32 | use CGI; | ||
| 33 | use FAQ::OMatic::Item; | ||
| 34 | use FAQ::OMatic; | ||
| 35 | use FAQ::OMatic::Auth; | ||
| 36 | use FAQ::OMatic::I18N; | ||
| 37 | |||
| 38 | |||
| 39 | sub main { | ||
| 40 | my $cgi = FAQ::OMatic::dispatch::cgi(); | ||
| 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 | FAQ::OMatic::Auth::ensurePerm('-item'=>$item, | ||
| 52 | '-operation'=>'PermEditTitle', | ||
| 53 | '-restart'=>'editItem', | ||
| 54 | '-cgi'=>$cgi, | ||
| 55 | '-extraTime'=>1, | ||
| 56 | '-failexit'=>1); | ||
| 57 | |||
| 58 | # verify that an evil cache hasn't truncated a POST | ||
| 59 | if ($params->{'_zzverify'} ne 'zz') { | ||
| 60 | FAQ::OMatic::gripe('error', | ||
| 61 | gettext("Your browser or WWW cache has truncated your POST.")); | ||
| 62 | } | ||
| 63 | |||
| 64 | $item->checkSequence($params); | ||
| 65 | $item->incrementSequence(); | ||
| 66 | |||
| 67 | my $titleMessage = ''; | ||
| 68 | if (FAQ::OMatic::getParam($params, '_Title') ne '') { | ||
| 69 | my $oldTitle = $item->getProperty('Title'); | ||
| 70 | my $newTitle = FAQ::OMatic::getParam($params, '_Title'); | ||
| 71 | if ($oldTitle ne $newTitle) { | ||
| 72 | $titleMessage = " ".gettexta("Changed the item title, was \"%0\"", $oldTitle); | ||
| 73 | } | ||
| 74 | $item->setProperty('Title', $newTitle); | ||
| 75 | } | ||
| 76 | if (defined $params->{'_partOrder'}) { | ||
| 77 | # get the user's new ordering for the parts | ||
| 78 | #my @newOrder = ($params->{'_partOrder'} =~ | ||
| 79 | # m/([^\s,]+)/sg); | ||
| 80 | # for some reason the previous construct doesn't extract more | ||
| 81 | # than one item from the list on some Perls. | ||
| 82 | # THANKS to Matthew Enger <menger@dhs.org> | ||
| 83 | # for reporting the problem. | ||
| 84 | my @newOrder = split(/[\s,]+/, $params->{'_partOrder'} || ''); | ||
| 85 | |||
| 86 | # verify that there are as many items in the new order as the old: | ||
| 87 | if (scalar @newOrder != $item->numParts()) { | ||
| 88 | FAQ::OMatic::gripe('error', gettexta("Your part order list (%0) ", join(", ", @newOrder)) | ||
| 89 | .gettexta("doesn't have the same number of parts (%0) as the original item.", $item->numParts())); | ||
| 90 | } | ||
| 91 | |||
| 92 | # verify now that every number 0 .. numParts()-1 appears exactly | ||
| 93 | # once in the new list. | ||
| 94 | my %newOrderHash = map { ($_,1) } @newOrder; | ||
| 95 | my $i; | ||
| 96 | for ($i=0; $i<$item->numParts(); $i++) { | ||
| 97 | if (not $newOrderHash{$i}) { | ||
| 98 | FAQ::OMatic::gripe('error', gettexta("Your part order list (%0) ", join(", ", @newOrder)) | ||
| 99 | .gettexta("doesn't say what to do with part %0.", $i)); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | # now we trust the @newOrder array. | ||
| 104 | my $newPartOrder = []; # new anonymous array | ||
| 105 | foreach $i (@newOrder) { | ||
| 106 | push @{$newPartOrder}, $item->getPart($i); | ||
| 107 | } | ||
| 108 | |||
| 109 | # install the new anonymous array | ||
| 110 | $item->{'Parts'} = $newPartOrder; | ||
| 111 | } | ||
| 112 | |||
| 113 | $item->setProperty('AttributionsTogether', | ||
| 114 | defined $params->{'_AttributionsTogether'} ? 1 : ''); | ||
| 115 | |||
| 116 | $item->saveToFile(); | ||
| 117 | |||
| 118 | $item->notifyModerator($cgi, 'edited the item configuration.' | ||
| 119 | .$titleMessage); | ||
| 120 | |||
| 121 | if (FAQ::OMatic::getParam($params, 'isapi')) { | ||
| 122 | # caller is a program; doesn't want a redirect to an HTML file! | ||
| 123 | # provide textual results | ||
| 124 | print FAQ::OMatic::header($cgi, '-type'=>'text/plain') | ||
| 125 | ."isapi=1\n" | ||
| 126 | ."file=".$item->{'filename'}."\n" | ||
| 127 | ."checkSequenceNumber=".$item->{'SequenceNumber'}."\n"; | ||
| 128 | FAQ::OMatic::myExit(0); | ||
| 129 | } | ||
| 130 | |||
| 131 | my $url; | ||
| 132 | if ($params->{'_insert'}) { | ||
| 133 | $url = FAQ::OMatic::makeAref( | ||
| 134 | '-command'=>'editPart', | ||
| 135 | '-params'=>$params, | ||
| 136 | '-changedParams'=>{'_insertpart'=>1, | ||
| 137 | 'partnum'=>'-1', | ||
| 138 | 'checkSequenceNumber'=>$item->{'SequenceNumber'}, | ||
| 139 | '_insert'=>$params->{'_insert'}}, | ||
| 140 | '-refType'=>'url'); | ||
| 141 | } else { | ||
| 142 | $url = FAQ::OMatic::makeAref( | ||
| 143 | '-command'=>'faq', | ||
| 144 | '-params'=>$params, | ||
| 145 | '-changedParams'=>{'checkSequenceNumber'=>''}, | ||
| 146 | '-refType'=>'url'); | ||
| 147 | } | ||
| 148 | |||
| 149 | FAQ::OMatic::redirect($cgi, $url); | ||
| 150 | } | ||
| 151 | |||
| 152 | 1; | ||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 |