| 1 | #!/usr/local/bin/perl -Tw | ||
| 2 | #!/usr/bin/perl -w | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | fix-link.cgi - allow a user to repair databases | ||
| 7 | |||
| 8 | =head1 DESCRIPTION | ||
| 9 | |||
| 10 | With this cgi, links within an infostructure can be repaired. It | ||
| 11 | accepts a list of fields corresponding to the urls to be changed, and | ||
| 12 | containing the url they are to be changed to. It then calls the | ||
| 13 | correct functions to repair those links. | ||
| 14 | |||
| 15 | Generally, this program will be triggered from a form generated by | ||
| 16 | link-report.cgi | ||
| 17 | |||
| 18 | =head1 ARGUMENTS | ||
| 19 | |||
| 20 | =head2 orig-url | ||
| 21 | |||
| 22 | The url to be fixed | ||
| 23 | |||
| 24 | =head2 canned-suggestion | ||
| 25 | |||
| 26 | the canned-suggestion parameter is used for returning values from a | ||
| 27 | list of choices. If the parameter is given the value "user" then; | ||
| 28 | |||
| 29 | =head2 user-suggestion | ||
| 30 | |||
| 31 | will contain the string typed in by the user. | ||
| 32 | |||
| 33 | =head1 SEE ALSO | ||
| 34 | |||
| 35 | L<verify-link-control(1)>; L<extract-links(1)>; L<build-schedule> | ||
| 36 | L<link-report(1)>; L<fix-link(1)>; L<link-report.cgi(1)>; L<fix-link.cgi> | ||
| 37 | L<suggest(1)>; L<link-report.cgi(1)>; L<configure-link-control> | ||
| 38 | |||
| 39 | The LinkController manual in the distribution in HTML, info, or | ||
| 40 | postscript formats, included in the distribution. | ||
| 41 | |||
| 42 | http://scotclimb.org.uk/software/linkcont/ - the | ||
| 43 | LinkController homepage. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 48 | 1 | $ENV{PATH} = "/bin:/usr/bin"; | |
| 49 | 1 | delete @ENV{qw(HOME IFS CDPATH ENV BASH_ENV)}; # Make %ENV safer | |
| 50 | } | ||
| 51 | |||
| 52 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 53 | |||
| 54 | 1 | ▶ | use Cwd; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 55 | 1 | ▶ | use CDB_File::BiIndex 0.026; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 56 | 0 | ▶ | use CGI::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 57 | use DB_File; | ||
| 58 | use Fcntl; | ||
| 59 | use HTML::Stream; | ||
| 60 | use CGI::Response; | ||
| 61 | use MLDBM qw(DB_File); | ||
| 62 | use WWW::Link; | ||
| 63 | use WWW::Link::Repair; | ||
| 64 | use WWW::Link::Repair::Substitutor; | ||
| 65 | use WWW::Link_Controller::InfoStruc; | ||
| 66 | use WWW::Link_Controller::URL; | ||
| 67 | |||
| 68 | use vars qw($req @oldurls $origuri $newuri $linkdbm $fixed_config); | ||
| 69 | |||
| 70 | #if the config files are to be previously overridden then this must | ||
| 71 | #have already been "used" | ||
| 72 | #use WWW::Link_Controller::ReadConf; | ||
| 73 | |||
| 74 | #FIXME | ||
| 75 | #Configuration - here we bypass tainting | ||
| 76 | # this basically means we trust the directory we are called in.. unwise!! | ||
| 77 | # this should never be an issue in a normal CGI which will be run from | ||
| 78 | # the user's hardwired script.. | ||
| 79 | |||
| 80 | BEGIN { | ||
| 81 | print STDERR "links before $::links\n" if $::verbose; | ||
| 82 | $DB::single = 1; | ||
| 83 | my $icwd=cwd(); | ||
| 84 | print STDERR "icwd: $icwd\n" if $::verbose; | ||
| 85 | (my $cwd) = ($icwd =~ m/(.*)/) ; #force launder | ||
| 86 | print STDERR "cwd: $cwd\n" if $::verbose; | ||
| 87 | if ((! $fixed_config) && -r ".cgi-infostruc.pl") { | ||
| 88 | my $file=$cwd . "/.cgi-infostruc.pl"; | ||
| 89 | defined (do $file) || do { | ||
| 90 | die "parse of $file failed: $@" if $@; | ||
| 91 | die "couldn't open $file: $!" | ||
| 92 | } | ||
| 93 | } else { | ||
| 94 | warn "using default configuration" if $::verbose; | ||
| 95 | } | ||
| 96 | |||
| 97 | print STDERR "links after $::links\n" if $::verbose; | ||
| 98 | }; | ||
| 99 | |||
| 100 | #controls - not really configuration yet. | ||
| 101 | |||
| 102 | $::secret_suggestions=0; #whether the user wants to tell others about changes | ||
| 103 | $::authorised=1; #whether the system has authenticated the user | ||
| 104 | |||
| 105 | $req=new CGI::Request; | ||
| 106 | $::hstr=new HTML::Stream; | ||
| 107 | |||
| 108 | print &CGI::Response::ContentType("text/html"); | ||
| 109 | |||
| 110 | #what infostructure are we fixing? | ||
| 111 | |||
| 112 | #who does the server think this connection is coming from? | ||
| 113 | |||
| 114 | #Get to the owner of that infostructures uid and check the person is authorised. | ||
| 115 | |||
| 116 | |||
| 117 | #what url should we fix? | ||
| 118 | |||
| 119 | |||
| 120 | my ($tmporiguri,$tmpnewuri); | ||
| 121 | |||
| 122 | $tmporiguri=$req->param("orig-url"); | ||
| 123 | die "arguments missing" unless $tmporiguri; | ||
| 124 | warn "got origurl $tmporiguri\n" if $::verbose; | ||
| 125 | $tmpnewuri=$req->param("canned-suggestion"); | ||
| 126 | die "argument missing" unless $tmporiguri; | ||
| 127 | warn "got newuri $tmpnewuri\n" if $::verbose; | ||
| 128 | $tmpnewuri=$req->param("user-suggestion") | ||
| 129 | unless ( defined $tmpnewuri and $tmpnewuri ne "user" ); | ||
| 130 | |||
| 131 | die "called without url to change from\n" unless defined $tmporiguri; | ||
| 132 | die "called without url to change to\n" unless defined $tmpnewuri; | ||
| 133 | |||
| 134 | # we take in the urls untainting them as we go.. this is potentially | ||
| 135 | # important for security if you are using this code to allow users to | ||
| 136 | # change url in HTML code. Nothing should come through which might | ||
| 137 | # screw up the HTML code. | ||
| 138 | |||
| 139 | |||
| 140 | $origuri=WWW::Link_Controller::URL::untaint_url($tmporiguri); | ||
| 141 | $newuri=WWW::Link_Controller::URL::untaint_url($tmpnewuri); | ||
| 142 | die "untaint of origuri failed" unless defined($origuri); | ||
| 143 | die "untaint of newuri failed" unless defined($newuri); | ||
| 144 | |||
| 145 | $::hstr->HTML->HEAD | ||
| 146 | ->TITLE->t("Results of Link Fix")->_TITLE | ||
| 147 | ->_HEAD | ||
| 148 | ->BODY | ||
| 149 | ->H1->t("Results")->_H1; | ||
| 150 | |||
| 151 | #print STDERR "$origuri eq $tmporiguri and $newuri eq $tmpnewuri\n"; | ||
| 152 | unless ($origuri eq $tmporiguri and $newuri eq $tmpnewuri) { | ||
| 153 | $::hstr->P ->t("The url you suggested wasn't a correct, full format," . | ||
| 154 | "url. Aborting. Sorry.") | ||
| 155 | ->_P; | ||
| 156 | exit 1; | ||
| 157 | } | ||
| 158 | |||
| 159 | if ($::authorised) { | ||
| 160 | #we have a known authorised user for this infostructure carry out all of the fixes. | ||
| 161 | |||
| 162 | print STDERR "page index $::page_index, link index $::link_index\n" | ||
| 163 | if $::verbose; | ||
| 164 | |||
| 165 | $::index = new CDB_File::BiIndex $::page_index, $::link_index; | ||
| 166 | |||
| 167 | #FIXME tree options? Relative Links | ||
| 168 | $::file_subs= WWW::Link::Repair::Substitutor::gen_file_substitutor | ||
| 169 | ($origuri, $newuri, ($::tree ? ( tree_mode => 1 ) : () ), | ||
| 170 | ($::relative ? ( relative => 1 , | ||
| 171 | file_to_url => \&WWW::Link_Controller::InfoStruc::file_to_url) | ||
| 172 | : () ), | ||
| 173 | ); | ||
| 174 | |||
| 175 | warn ( "substituting $origuri for $newuri" ) if $::verbose; | ||
| 176 | |||
| 177 | my $trans_sub=\&WWW::Link_Controller::InfoStruc::url_to_file; | ||
| 178 | my $fixed=WWW::Link::Repair::infostructure($origuri, $::index,$trans_sub, | ||
| 179 | $::file_subs); | ||
| 180 | |||
| 181 | $::hstr->P | ||
| 182 | -> t("Your link change of $origuri\n to $newuri\n" . | ||
| 183 | "has been carried out as requested. We made" . | ||
| 184 | "$fixed fixeds in the process\n" ) | ||
| 185 | ->_P->nl; | ||
| 186 | } else { | ||
| 187 | $::hstr->P ->t("you don't seem to be an authorised user so I can't directly" . | ||
| 188 | "carry out your suggested changes. If you should be" . | ||
| 189 | "authorised then try typing your password again" | ||
| 190 | ); | ||
| 191 | } | ||
| 192 | |||
| 193 | unless ($::secret_suggestions) { | ||
| 194 | $::linkdbm = tie %::links, "MLDBM", $::links, O_RDONLY, 0666, $::DB_HASH | ||
| 195 | or die $!; | ||
| 196 | my $link=$::links{$origuri}; | ||
| 197 | accept_suggestion($link, $newuri); | ||
| 198 | $::links{$origuri} = $link; #MLDBMhack | ||
| 199 | } | ||
| 200 | |||
| 201 | $::hstr->_BODY->_HTML; | ||
| 202 | |||
| 203 | exit 0; | ||
| 204 | |||
| 205 | sub accept_suggestion { | ||
| 206 | my $link=shift; | ||
| 207 | my $suggestion=shift; | ||
| 208 | my $origuri = $link->url(); | ||
| 209 | if ( $link->add_suggestion($suggestion) ) { | ||
| 210 | $::hstr->P | ||
| 211 | -> t("Your link change of $origuri\n to $suggestion\n" . | ||
| 212 | "has been added as a suggestion to the database.\n" . | ||
| 213 | "Thankyou.") | ||
| 214 | ->_P->nl; | ||
| 215 | } else { | ||
| 216 | $::hstr->P | ||
| 217 | -> t("Your link change of $origuri\n to $suggestion\n" . | ||
| 218 | "is already recorded in the database.\n" . | ||
| 219 | "Thankyou for offering anyway.") | ||
| 220 | ->_P->nl; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | sub is_tainted { | ||
| 226 | no warnings; | ||
| 227 | return ! eval { | ||
| 228 | join('',@_), kill 0; | ||
| 229 | 1; | ||
| 230 | }; | ||
| 231 | } | ||