OMatic/submitBag.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 144 CC 18 SCAR 53.0
CC
18
Cov
0%
main
306.0
bagread
6.0
BEGIN
2.0
CRAP 201.1
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::submitBag;
31  
32 use CGI;
33 use FAQ::OMatic::Item;
34 use FAQ::OMatic;
35 use FAQ::OMatic::Auth;
36 use FAQ::OMatic::Bags;
37 use FAQ::OMatic::I18N;
38  
39 sub main {
40 my $cgi = FAQ::OMatic::dispatch::cgi();
41 my $rt = '';
42
43 my $params = FAQ::OMatic::getParams($cgi);
44  
45 FAQ::OMatic::mirrorsCantEdit($cgi, $params);
46
47 #
48 # jpell - Default filename if not specified.
49 # THANKS to Jason Pell <jason.pell@callista.com.au> for submitting
50 # these patches that improve bag handling.
51 #
52 my $clientFname = $params->{'_bagName'};
53 # If _bagName not defined, use the filename instead.
54 if($clientFname eq ''){
55 my $bagData = $cgi->param('_bagData');
56 if ($bagData){
57 # Get the client clientFname here.
58 $clientFname = $cgi->uploadInfo($bagData)->{'Content-Disposition'};
59 if( $clientFname=~/ filename="?([^\";]*)"?/ ){
60 $clientFname = $1;
61  
62 # Replace dos separators, so we can search for / only.
63 $clientFname =~ tr/\\/\//;
64 if($clientFname =~ m#^(.*/)?(.*)#s ){
65 $clientFname = $2;
66 }
67 }
68 }
69 }
70  
71 my $bagName = FAQ::OMatic::Bags::untaintBagName($clientFname);
72  
73 if ($bagName eq '') {
74 FAQ::OMatic::gripe('error', gettext("Bag names may only contain letters, numbers, underscores (_), hyphens (-), and periods (.), and may not end in '.desc'. Yours was")." \"$bagName\".");
75 }
76  
77 if (-f $FAQ::OMatic::Config::bagsDir.$bagName) {
78 # bag exists
79 FAQ::OMatic::Auth::ensurePerm('-item'=>'',
80 '-operation'=>'PermReplaceBag',
81 '-restart'=>FAQ::OMatic::commandName(),
82 '-cgi'=>$cgi,
83 '-extraTime'=>1,
84 '-xreason'=>'replace',
85 '-failexit'=>1);
86 } else {
87 # new bag name
88 FAQ::OMatic::Auth::ensurePerm('-item'=>'',
89 '-operation'=>'PermNewBag',
90 '-restart'=>FAQ::OMatic::commandName(),
91 '-cgi'=>$cgi,
92 '-extraTime'=>1,
93 '-failexit'=>1);
94 }
95  
96 # get the bag contents
97 my $bagTmpName = 'newbag.'.FAQ::OMatic::nonce();
98 if (not open(BAGFILE, ">".$FAQ::OMatic::Config::bagsDir.$bagTmpName)) {
99 FAQ::OMatic::gripe('error', "Couldn't store incoming bag $bagName: $!");
100 }
101 # THANKS: to John Nolan <JNolan@n2k.com> for fixing the reference
102 # to the filehandle returned by CGI.pm in the next line.
103 my $sizeBytes = 0;
104 my $buf;
105 my $formDataHandle = $cgi->param('_bagData');
106  
107 if ($formDataHandle) {
108 # The next line will make the 'use strict' pragma throw an error
109 # if you're using an old CGI.pm. It has been tested and seen
110 # to work with CGI.pm 2.46 and newer, so upgrade.
111 while (bagread($formDataHandle, \$buf, 4096)) {
112 print BAGFILE $buf;
113 $sizeBytes += length($buf);
114 # TODO: should have admin-configurable length limit here
115 # TODO: and in similar submitPart.pm code
116 }
117 close BAGFILE;
118 }
119  
120 if ($sizeBytes > 0) {
121 if (not rename($FAQ::OMatic::Config::bagsDir.$bagTmpName,
122 $FAQ::OMatic::Config::bagsDir.$bagName)) {
123 FAQ::OMatic::gripe('error', "Couldn't rename "
124 .$FAQ::OMatic::Config::bagsDir.$bagTmpName
125 ." to "
126 .$FAQ::OMatic::Config::bagsDir.$bagName);
127 }
128 if (not chmod(0644, $FAQ::OMatic::Config::bagsDir.$bagName)) {
129 FAQ::OMatic::gripe('problem', "chmod("
130 .$FAQ::OMatic::Config::bagsDir.$bagName
131 .") failed: $!");
132 }
133 } else {
134 # no bag file sent; discard bag file
135 if (not unlink($FAQ::OMatic::Config::bagsDir.$bagTmpName)) {
136 FAQ::OMatic::gripe('problem', "Couldn't unlink "
137 .$FAQ::OMatic::Config::bagsDir.$bagTmpName);
138 }
139 }
140  
141 # get bag description info
142 my $bagDesc = FAQ::OMatic::Bags::getBagDesc($bagName);
143 $bagDesc->setProperty('SizeWidth', $params->{'_sizeWidth'} || '');
144 $bagDesc->setProperty('SizeHeight', $params->{'_sizeHeight'} || '');
145 # don't change $sizeBytes property if we don't have a new valid value
146 if ($sizeBytes > 0) {
147 $bagDesc->setProperty('SizeBytes', $sizeBytes);
148 }
149 FAQ::OMatic::Bags::saveBagDesc($bagDesc);
150  
151 # add link to part if info provided
152 my $partnum = $params->{'partnum'};
153 $partnum = -1 if (not defined $partnum);
154 if ($partnum>=0) {
155 my $itemName = $params->{'file'} || '';
156 my $item = new FAQ::OMatic::Item($itemName);
157 my $part = $item->getPart($partnum);
158  
159 # TODO: Check that legal image extension!
160 my $bagInline = $cgi->param('_bagInline');
161 if("$bagInline" eq "y"){
162 $part->{'Text'}.="\n<baginline:$bagName>\n";
163 }else{
164 $part->{'Text'}.="\n<baglink:$bagName>\n";
165 }
166 $item->saveToFile();
167 }
168  
169 # update any items that depend on this bag
170 # This is the only place that bags (or their .descs) can change,
171 # currently, # so it's reasonable to do the dependency-drive
172 # updates here. If it ever becomes the case that we write bags
173 # in another place, factor this code out.
174 foreach my $dependent
175 (FAQ::OMatic::Item::getDependencies("bags.".$bagName)) {
176 my $dependentItem = new FAQ::OMatic::Item($dependent);
177 $dependentItem->writeCacheCopy();
178 }
179  
180 my $url = FAQ::OMatic::makeAref('-command'=>'faq',
181 '-params'=>$params,
182 '-changedParams'=>{'partnum'=>'', 'checkSequenceNumber'=>''},
183 '-refType'=>'url');
184 FAQ::OMatic::redirect($cgi, $url);
185 }
186  
187 sub bagread {
188 my $formDataHandle = shift;
189 my $buf = shift;
190  
191 my $rc;
192 eval { $rc = read($formDataHandle, $$buf, 4096); };
193 if ($@) {
194 die "Error in multipart form code -- probably your version of "
195 ."CGI.pm. Message was:<br>\n"
196 .$@;
197 }
198 return $rc;
199 }
200  
201 1;