OMatic/submitMove.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 104 CC 15 SCAR 51.5
CC
15
Cov
0%
main
240.0
BEGIN
2.0
BEGIN
2.0
CRAP 172.0
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::submitMove;
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 sub main {
39 my $cgi = FAQ::OMatic::dispatch::cgi();
40 my $params = FAQ::OMatic::getParams($cgi);
41  
42 FAQ::OMatic::mirrorsCantEdit($cgi, $params);
43  
44 my $movingFilename = $params->{'file'};
45 my $newParentFilename = $params->{'_newParent'};
46  
47 # load up the moving file and the new parent
48 my $movingItem = new FAQ::OMatic::Item($movingFilename);
49 if ($movingItem->isBroken()) {
50 FAQ::OMatic::gripe('error',
51 gettexta("The moving file (%0) is broken or missing.",
52 $movingFilename));
53 }
54 my $newParentItem = new FAQ::OMatic::Item($newParentFilename);
55 if ($newParentItem->isBroken()) {
56 FAQ::OMatic::gripe('error',
57 gettexta("The newParent file (%0) is broken or missing.",
58 $newParentFilename));
59 }
60  
61 # load up the old parent
62 my $oldParentFilename = $movingItem->{'Parent'};
63 my $oldParentItem = new FAQ::OMatic::Item($oldParentFilename);
64 if ($oldParentItem->isBroken()) {
65 FAQ::OMatic::gripe('error',
66 gettexta("The oldParent file (%0) is broken or missing.",
67 $oldParentFilename));
68 }
69  
70 # make sure the new parent isn't the old parent or
71 # the moving item itself
72 if ($newParentFilename eq $oldParentFilename) {
73 FAQ::OMatic::gripe('error',
74 gettexta("The new parent (%0) is the same as the old parent.",
75 $newParentItem->getTitle()));
76 }
77 if ($newParentFilename eq $movingFilename) {
78 FAQ::OMatic::gripe('error',
79 gettexta("The new parent (%0) is the same as the item you want to move.",
80 $newParentItem->getTitle()));
81 }
82
83 # make sure the new parent isn't a child of movingItem
84 if ($newParentItem->hasParent($movingFilename)) {
85 FAQ::OMatic::gripe('error',
86 gettexta("The new parent (%0) is a child of the item being moved (%1).",
87 $newParentItem->getTitle(), $movingItem->getTitle()));
88 }
89  
90 if ($movingItem->{'filename'} eq '1') {
91 FAQ::OMatic::gripe('error',
92 gettext("You can't move the top item."));
93 }
94  
95 # check permissions on the parents to see that the move is legal
96 # we need to check more than a single permission, and want
97 # to return the error corresponding to the greater of the two.
98 my ($url1,$aq1) =
99 FAQ::OMatic::Auth::ensurePerm($oldParentItem, 'PermEditDirectory',
100 'submitMove', $cgi, 1);
101 my ($url2,$aq2) =
102 FAQ::OMatic::Auth::ensurePerm($newParentItem, 'PermAddItem',
103 'submitMove', $cgi, 1);
104  
105 # If both ends of the move are not authorized, demand authentication
106 # at the higher of the two levels
107 if ($url1 and $url2) {
108 FAQ::OMatic::redirect($cgi, ($aq1 > $aq2) ? $url1 : $url2 );
109 } elsif ($url1) {
110 FAQ::OMatic::redirect($cgi, $url1);
111 } elsif ($url2) {
112 FAQ::OMatic::redirect($cgi, $url2);
113 }
114  
115 # don't remove an item from itself if it's a root (own parent)
116 if ($oldParentItem ne $movingItem) {
117 $oldParentItem->removeSubItem($movingFilename);
118 }
119 $newParentItem->addSubItem($movingFilename);
120 $movingItem->setProperty('Parent', $newParentFilename);
121  
122 $oldParentItem->saveToFile();
123 $newParentItem->saveToFile();
124 $movingItem->saveToFile();
125  
126 my $oldModerator =
127 FAQ::OMatic::Auth::getInheritedProperty($oldParentItem, 'Moderator');
128 my $newModerator =
129 FAQ::OMatic::Auth::getInheritedProperty($newParentItem, 'Moderator');
130 $oldParentItem->notifyModerator($cgi, gettexta("moved a sub-item to %0",
131 $newParentItem->getTitle()));
132 if ($newModerator ne $oldModerator) {
133 $newParentItem->notifyModerator($cgi, gettexta("moved a sub-item from %0",
134 $oldParentItem->getTitle()));
135 }
136  
137 # If user clicked "trash this item," they probably don't want
138 # to go see it in the trash.
139 my $target = ($newParentFilename ne 'trash')
140 ? $newParentFilename
141 : $oldParentFilename;
142 my $url = FAQ::OMatic::makeAref('faq', {'file'=>$target}, 'url');
143  
144 FAQ::OMatic::redirect($cgi, $url);
145 }
146  
147 1;