[system] / branches / rel-2-4-patches / webwork-modperl / lib / WeBWorK.pm Repository:
ViewVC logotype

Annotation of /branches/rel-2-4-patches/webwork-modperl/lib/WeBWorK.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2973 - (view) (download) (as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK.pm

1 : sh002i 986 ################################################################################
2 : sh002i 1663 # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
4 : sh002i 2973 # $CVSHeader: webwork2/lib/WeBWorK.pm,v 1.67 2004/11/02 19:58:31 sh002i Exp $
5 : sh002i 1663 #
6 :     # This program is free software; you can redistribute it and/or modify it under
7 :     # the terms of either: (a) the GNU General Public License as published by the
8 :     # Free Software Foundation; either version 2, or (at your option) any later
9 :     # version, or (b) the "Artistic License" which comes with this package.
10 :     #
11 :     # This program is distributed in the hope that it will be useful, but WITHOUT
12 :     # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 :     # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
14 :     # Artistic License for more details.
15 : sh002i 986 ################################################################################
16 :    
17 :     package WeBWorK;
18 :    
19 :     =head1 NAME
20 :    
21 : sh002i 1565 WeBWorK - Dispatch requests to the appropriate content generator.
22 : sh002i 986
23 : sh002i 1565 =head1 SYNOPSIS
24 :    
25 :     my $r = Apache->request;
26 :     my $result = eval { WeBWorK::dispatch($r) };
27 :     die "something bad happened: $@" if $@;
28 :    
29 :     =head1 DESCRIPTION
30 :    
31 :     C<WeBWorK> is the dispatcher for the WeBWorK system. Given an Apache request
32 :     object, it performs authentication and determines which subclass of
33 :     C<WeBWorK::ContentGenerator> to call.
34 :    
35 : sh002i 986 =cut
36 :    
37 : sh002i 2973 BEGIN { $main::VERSION = "2.1"; }
38 : sh002i 1548
39 : sh002i 986 use strict;
40 :     use warnings;
41 : sh002i 2434 use Apache::Constants qw(:common REDIRECT DONE);
42 : sh002i 2368
43 :     # load WeBWorK::Constants before anything else
44 :     # this sets package variables in several packages
45 :     use WeBWorK::Constants;
46 :    
47 :     # the rest of these are modules that are acutally used by this one
48 : sh002i 986 use WeBWorK::Authen;
49 :     use WeBWorK::Authz;
50 :     use WeBWorK::CourseEnvironment;
51 :     use WeBWorK::DB;
52 : sh002i 2368 use WeBWorK::Debug;
53 :     use WeBWorK::Request;
54 : gage 2098 use WeBWorK::Timing;
55 : sh002i 1616 use WeBWorK::Upload;
56 : sh002i 1836 use WeBWorK::URLPath;
57 : gage 2930 use WeBWorK::Utils qw(runtime_use writeTimingLogEntry);
58 :     use Date::Format;
59 : sh002i 1836
60 :     use constant AUTHEN_MODULE => "WeBWorK::ContentGenerator::Login";
61 : sh002i 2313 use constant FIXDB_MODULE => "WeBWorK::ContentGenerator::FixDB";
62 : sh002i 1836
63 : sh002i 2491 our %SeedCE;
64 :    
65 : sh002i 1879 sub dispatch($) {
66 : sh002i 1836 my ($apache) = @_;
67 :     my $r = new WeBWorK::Request $apache;
68 :    
69 :     my $method = $r->method;
70 :     my $location = $r->location;
71 :     my $uri = $r->uri;
72 :     my $path_info = $r->path_info | "";
73 :     my $args = $r->args || "";
74 : sh002i 2491 #my $webwork_root = $r->dir_config("webwork_root");
75 :     #my $pg_root = $r->dir_config("pg_root");
76 : sh002i 1836
77 : sh002i 2638 debug("\n\n===> Begin " . __PACKAGE__ . "::dispatch() <===\n\n");
78 : sh002i 1836 debug("Hi, I'm the new dispatcher!\n");
79 :     debug(("-" x 80) . "\n");
80 :    
81 :     debug("Okay, I got some basic information:\n");
82 :     debug("The apache location is $location\n");
83 :     debug("The request method is $method\n");
84 :     debug("The URI is $uri\n");
85 :     debug("The path-info is $path_info\n");
86 :     debug("The argument string is $args\n");
87 : sh002i 2491 #debug("The WeBWorK root directory is $webwork_root\n");
88 :     #debug("The PG root directory is $pg_root\n");
89 : sh002i 1836 debug(("-" x 80) . "\n");
90 :    
91 :     debug("The first thing we need to do is munge the path a little:\n");
92 :    
93 :     my ($path) = $uri =~ m/$location(.*)/;
94 :     $path = "/" if $path eq ""; # no path at all
95 :    
96 :     debug("We can't trust the path-info, so we make our own path.\n");
97 :     debug("path-info claims: $path_info\n");
98 :     debug("but it's really: $path\n");
99 :     debug("(if it's empty, we set it to \"/\".)\n");
100 :    
101 :     $path =~ s|/+|/|g;
102 :     debug("...and here it is without repeated slashes: $path\n");
103 :    
104 :     # lookbehind assertion for "not a slash"
105 :     # matches the boundary after the last char
106 :     $path =~ s|(?<=[^/])$|/|;
107 :     debug("...and here it is with a trailing slash: $path\n");
108 :    
109 :     debug(("-" x 80) . "\n");
110 :    
111 :     debug("Now we need to look at the path a little to figure out where we are\n");
112 :    
113 :     debug("-------------------- call to WeBWorK::URLPath::newFromPath\n");
114 : sh002i 1885 my $urlPath = WeBWorK::URLPath->newFromPath($path);
115 : sh002i 1836 debug("-------------------- call to WeBWorK::URLPath::newFromPath\n");
116 :    
117 :     unless ($urlPath) {
118 :     debug("This path is invalid... see you later!\n");
119 : sh002i 2434 die "The path '$path' is not valid.\n";
120 : sh002i 1836 }
121 :    
122 :     my $displayModule = $urlPath->module;
123 :     my %displayArgs = $urlPath->args;
124 : sh002i 2434
125 : sh002i 2970 unless ($displayModule) {
126 :     debug("The display module is empty, so we can DECLINE here.\n");
127 :     die "No display module found for path '$path'.";
128 :     }
129 :    
130 : sh002i 1836 debug("The display module for this path is: $displayModule\n");
131 :     debug("...and here are the arguments we'll pass to it:\n");
132 :     foreach my $key (keys %displayArgs) {
133 :     debug("\t$key => $displayArgs{$key}\n");
134 :     }
135 :    
136 :     my $selfPath = $urlPath->path;
137 :     my $parent = $urlPath->parent;
138 :     my $parentPath = $parent ? $parent->path : "<no parent>";
139 :    
140 :     debug("Reconstructing the original path gets us: $selfPath\n");
141 :     debug("And we can generate the path to our parent, too: $parentPath\n");
142 :     debug("(We could also figure out who our children are, but we'd need to supply additional arguments.)\n");
143 :     debug(("-" x 80) . "\n");
144 :    
145 : sh002i 1885 debug("The URLPath looks good, we'll add it to the request.\n");
146 :     $r->urlpath($urlPath);
147 :    
148 : sh002i 1836 debug("Now we want to look at the parameters we got.\n");
149 :    
150 :     debug("The raw params:\n");
151 :     foreach my $key ($r->param) {
152 :     debug("\t$key\n");
153 :     debug("\t\t$_\n") foreach $r->param($key);
154 :     }
155 :    
156 : sh002i 1895 #mungeParams($r);
157 : sh002i 1836
158 :     debug("The munged params:\n");
159 :     foreach my $key ($r->param) {
160 :     debug("\t$key\n");
161 :     debug("\t\t$_\n") foreach $r->param($key);
162 :     }
163 :    
164 :     debug(("-" x 80) . "\n");
165 :    
166 : sh002i 2346 # create a package-global timing object
167 : sh002i 2368 # FIXME: this is used by other modules!
168 :     # FIXME: this is not thread-safe!
169 :     my $label = defined $displayArgs{courseID} ? $displayArgs{courseID} : "ROOT";
170 :     $WeBWorK::timer = WeBWorK::Timing->new($label);
171 :     $WeBWorK::timer->start;
172 : sh002i 2346
173 : sh002i 1836 debug("We need to get a course environment (with or without a courseID!)\n");
174 : sh002i 2491 my $ce = eval { new WeBWorK::CourseEnvironment({
175 :     #webworkRoot => $r->dir_config("webwork_root"),
176 :     #webworkURLRoot => $location,
177 :     #pgRoot => $r->dir_config("pg_root"),
178 :     %SeedCE,
179 :     courseName => $displayArgs{courseID},
180 :     }) };
181 : sh002i 2437 $@ and die "Failed to initialize course environment: $@\n";
182 : sh002i 1836 debug("Here's the course environment: $ce\n");
183 : sh002i 1885 $r->ce($ce);
184 : sh002i 1836
185 : sh002i 1879 my @uploads = $r->upload;
186 :     foreach my $u (@uploads) {
187 :     # make sure it's a "real" upload
188 :     next unless $u->filename;
189 :    
190 :     # store the upload
191 :     my $upload = WeBWorK::Upload->store($u,
192 :     dir => $ce->{webworkDirs}->{uploadCache}
193 :     );
194 :    
195 :     # store the upload ID and hash in the file upload field
196 :     my $id = $upload->id;
197 :     my $hash = $upload->hash;
198 :     $r->param($u->name => "$id $hash");
199 :     }
200 : sh002i 1836
201 :     my ($db, $authz);
202 : sh002i 2434
203 : sh002i 2437 if ($displayArgs{courseID}) {
204 : sh002i 1836 debug("We got a courseID from the URLPath, now we can do some stuff:\n");
205 : sh002i 2437
206 :     unless (-e $ce->{courseDirs}->{root}) {
207 :     die "Course '$displayArgs{courseID}' not found: $!";
208 :     }
209 :    
210 : sh002i 1836 debug("...we can create a database object...\n");
211 :     $db = new WeBWorK::DB($ce->{dbLayout});
212 :     debug("(here's the DB handle: $db)\n");
213 : sh002i 1885 $r->db($db);
214 : sh002i 1836
215 : sh002i 2313 debug("Now we check the database...\n");
216 :     debug("(we can detect if a hash-style database from WW1 has not be converted properly.)\n");
217 :     my ($dbOK, @dbMessages) = $db->hashDatabaseOK(0); # 0 == don't fix
218 :     if (not $dbOK) {
219 :     debug("hashDatabaseOK() returned $dbOK -- looks like trouble...\n");
220 :     $displayModule = FIXDB_MODULE;
221 :     debug("set displayModule to $displayModule\n");
222 :     } else {
223 :     debug("hashDatabaseOK() returned $dbOK -- leaving displayModule as-is\n");
224 :     }
225 :    
226 :     debug("...and now we can authenticate the remote user...\n");
227 : sh002i 1885 my $authen = new WeBWorK::Authen($r);
228 : sh002i 1836 my $authenOK = $authen->verify;
229 :     if ($authenOK) {
230 :     debug("Hi, ", $r->param("user"), ", glad you made it.\n");
231 :    
232 :     debug("Authentication succeeded, so it makes sense to create an authz object...\n");
233 : sh002i 1885 $authz = new WeBWorK::Authz($r, $ce, $db);
234 : sh002i 1836 debug("(here's the authz object: $authz)\n");
235 : sh002i 1885 $r->authz($authz);
236 : sh002i 1836
237 :     debug("Now we deal with the effective user:\n");
238 :     my $userID = $r->param("user");
239 :     my $eUserID = $r->param("effectiveUser") || $userID;
240 :     debug("userID=$userID eUserID=$eUserID\n");
241 :     my $su_authorized = $authz->hasPermissions($userID, "become_student", $eUserID);
242 :     if ($su_authorized) {
243 : toenail 2375 debug("Ok, looks like you're allowed to become $eUserID. Whoopie!\n");
244 : sh002i 1836 } else {
245 : toenail 2375 debug("Uh oh, you're not allowed to become $eUserID. Nice try!\n");
246 : sh002i 1836 $eUserID = $userID;
247 :     }
248 :     $r->param("effectiveUser" => $eUserID);
249 :     } else {
250 :     debug("Bad news: authentication failed!\n");
251 :     $displayModule = AUTHEN_MODULE;
252 :     debug("set displayModule to $displayModule\n");
253 :     }
254 :     }
255 :    
256 : sh002i 2437 ## if a course ID was given in the URL and resulted in an error (as stored in $!)
257 :     ## it probably means that the course does not exist or was misspelled
258 :     #if ($displayArgs{courseID} and $ce->{'!'}) {
259 :     # debug("Something was wrong with the courseID: \n");
260 :     # debug("\t\t" . $ce->{'!'} . "\n");
261 :     # debug("Time to bail!\n");
262 :     # die "An error occured while accessing '$displayArgs{courseID}': '", $ce->{'!'}, "'.\n";
263 :     #}
264 : sh002i 2434
265 : sh002i 1836 debug(("-" x 80) . "\n");
266 :     debug("Finally, we'll load the display module...\n");
267 :    
268 : gage 2930 my $localStartTime = time;
269 :    
270 : sh002i 1836 runtime_use($displayModule);
271 :    
272 :     debug("...instantiate it...\n");
273 :    
274 :     my $instance = $displayModule->new($r);
275 :    
276 :     debug("...and call it:\n");
277 :     debug("-------------------- call to ${displayModule}::go\n");
278 :    
279 :     my $result = $instance->go();
280 :    
281 :     debug("-------------------- call to ${displayModule}::go\n");
282 :    
283 : sh002i 1910 debug("returning result: " . (defined $result ? $result : "UNDEF") . "\n");
284 : gage 2930 #$WeBWorK::timer->continue("[" . time2str("%a %b %d %H:%M:%S %Y", time) . "]" . "[" . $r->uri . "]");
285 :     #$WeBWorK::timer->stop();
286 :     #$WeBWorK::timer->save();
287 : sh002i 2346
288 : gage 2930 my $localStopTime = time;
289 :     my $timeDiff = $localStopTime - $localStartTime;
290 : gage 2931 writeTimingLogEntry($ce,"[".$r->uri."]", sprintf("runTime = %.1f sec", $timeDiff)." ".$ce->{dbLayoutName},"" );
291 : gage 2098 return $result;
292 : sh002i 1836
293 :     }
294 :    
295 :     sub mungeParams {
296 :     my ($r) = @_;
297 :    
298 :     my @paramQueue;
299 :    
300 :     # remove all the params from the request, and store them in the param queue
301 :     foreach my $key ($r->param) {
302 :     push @paramQueue, [ $key => [ $r->param($key) ] ];
303 :     $r->parms->unset($key)
304 :     }
305 :    
306 :     # exhaust the param queue, decoding encoded params
307 :     while (@paramQueue) {
308 :     my ($key, $values) = @{ shift @paramQueue };
309 :    
310 :     if ($key =~ m/\,/) {
311 :     # we have multiple params encoded in a single param
312 :     # split them up and add them to the end of the queue
313 :     push @paramQueue, map { [ $_, $values ] } split m/\,/, $key;
314 :     } elsif ($key =~ m/\:/) {
315 :     # we have a whole param encoded in a key
316 :     # split it up and add it to the end of the queue
317 :     my ($newKey, $newValue) = split m/\:/, $key;
318 :     push @paramQueue, [ $newKey, [ $newValue ] ];
319 :     } else {
320 :     # this is a "normal" param
321 :     # add it to the param list
322 :     if (defined $r->param($key)) {
323 :     # the param already exists -- append the values we have
324 :     $r->param($key => [ $r->param($key), @$values ]);
325 :     } else {
326 :     # the param doesn't exist -- create it with the values we have
327 :     $r->param($key => $values);
328 :     }
329 :     }
330 :     }
331 :     }
332 :    
333 :    
334 : sh002i 1565 =head1 AUTHOR
335 :    
336 : sh002i 1616 Written by Dennis Lambe, malsyned at math.rochester.edu. Modified by Sam
337 :     Hathaway, sh002i at math.rochester.edu.
338 : sh002i 1565
339 :     =cut
340 :    
341 : sh002i 986 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9