[system] / branches / dg_dev / webwork2 / lib / WeBWorK.pm Repository:
ViewVC logotype

Annotation of /branches/dg_dev/webwork2/lib/WeBWorK.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2368 - (view) (download) (as text)
Original Path: trunk/webwork2/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 2368 # $CVSHeader: webwork-modperl/lib/WeBWorK.pm,v 1.57 2004/06/17 20:10:18 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 1548 BEGIN { $main::VERSION = "2.0"; }
38 :    
39 : sh002i 986 use strict;
40 :     use warnings;
41 : gage 1377 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 : sh002i 2368 use WeBWorK::Utils qw(runtime_use);
58 : sh002i 1836
59 :     use constant AUTHEN_MODULE => "WeBWorK::ContentGenerator::Login";
60 : sh002i 2313 use constant FIXDB_MODULE => "WeBWorK::ContentGenerator::FixDB";
61 : sh002i 1836
62 : sh002i 1879 sub dispatch($) {
63 : sh002i 1836 my ($apache) = @_;
64 :     my $r = new WeBWorK::Request $apache;
65 :    
66 :     my $method = $r->method;
67 :     my $location = $r->location;
68 :     my $uri = $r->uri;
69 :     my $path_info = $r->path_info | "";
70 :     my $args = $r->args || "";
71 :     my $webwork_root = $r->dir_config("webwork_root");
72 :     my $pg_root = $r->dir_config("pg_root");
73 :    
74 :     debug("Hi, I'm the new dispatcher!\n");
75 :     debug(("-" x 80) . "\n");
76 :    
77 :     debug("Okay, I got some basic information:\n");
78 :     debug("The apache location is $location\n");
79 :     debug("The request method is $method\n");
80 :     debug("The URI is $uri\n");
81 :     debug("The path-info is $path_info\n");
82 :     debug("The argument string is $args\n");
83 :     debug("The WeBWorK root directory is $webwork_root\n");
84 :     debug("The PG root directory is $pg_root\n");
85 :     debug(("-" x 80) . "\n");
86 :    
87 :     debug("The first thing we need to do is munge the path a little:\n");
88 :    
89 :     my ($path) = $uri =~ m/$location(.*)/;
90 :     $path = "/" if $path eq ""; # no path at all
91 :    
92 :     debug("We can't trust the path-info, so we make our own path.\n");
93 :     debug("path-info claims: $path_info\n");
94 :     debug("but it's really: $path\n");
95 :     debug("(if it's empty, we set it to \"/\".)\n");
96 :    
97 :     $path =~ s|/+|/|g;
98 :     debug("...and here it is without repeated slashes: $path\n");
99 :    
100 :     # lookbehind assertion for "not a slash"
101 :     # matches the boundary after the last char
102 :     $path =~ s|(?<=[^/])$|/|;
103 :     debug("...and here it is with a trailing slash: $path\n");
104 :    
105 :     debug(("-" x 80) . "\n");
106 :    
107 :     debug("Now we need to look at the path a little to figure out where we are\n");
108 :    
109 :     debug("-------------------- call to WeBWorK::URLPath::newFromPath\n");
110 : sh002i 1885 my $urlPath = WeBWorK::URLPath->newFromPath($path);
111 : sh002i 1836 debug("-------------------- call to WeBWorK::URLPath::newFromPath\n");
112 :    
113 :     unless ($urlPath) {
114 :     debug("This path is invalid... see you later!\n");
115 :     return DECLINED;
116 :     }
117 :    
118 :     my $displayModule = $urlPath->module;
119 :     my %displayArgs = $urlPath->args;
120 :    
121 :     debug("The display module for this path is: $displayModule\n");
122 :     debug("...and here are the arguments we'll pass to it:\n");
123 :     foreach my $key (keys %displayArgs) {
124 :     debug("\t$key => $displayArgs{$key}\n");
125 :     }
126 :    
127 :     unless ($displayModule) {
128 :     debug("The display module is empty, so we can DECLINE here.\n");
129 :     return DECLINED;
130 :     }
131 :    
132 :     my $selfPath = $urlPath->path;
133 :     my $parent = $urlPath->parent;
134 :     my $parentPath = $parent ? $parent->path : "<no parent>";
135 :    
136 :     debug("Reconstructing the original path gets us: $selfPath\n");
137 :     debug("And we can generate the path to our parent, too: $parentPath\n");
138 :     debug("(We could also figure out who our children are, but we'd need to supply additional arguments.)\n");
139 :     debug(("-" x 80) . "\n");
140 :    
141 : sh002i 1885 debug("The URLPath looks good, we'll add it to the request.\n");
142 :     $r->urlpath($urlPath);
143 :    
144 : sh002i 1836 debug("Now we want to look at the parameters we got.\n");
145 :    
146 :     debug("The raw params:\n");
147 :     foreach my $key ($r->param) {
148 :     debug("\t$key\n");
149 :     debug("\t\t$_\n") foreach $r->param($key);
150 :     }
151 :    
152 : sh002i 1895 #mungeParams($r);
153 : sh002i 1836
154 :     debug("The munged params:\n");
155 :     foreach my $key ($r->param) {
156 :     debug("\t$key\n");
157 :     debug("\t\t$_\n") foreach $r->param($key);
158 :     }
159 :    
160 :     debug(("-" x 80) . "\n");
161 :    
162 : sh002i 2346 # create a package-global timing object
163 : sh002i 2368 # FIXME: this is used by other modules!
164 :     # FIXME: this is not thread-safe!
165 :     my $label = defined $displayArgs{courseID} ? $displayArgs{courseID} : "ROOT";
166 :     $WeBWorK::timer = WeBWorK::Timing->new($label);
167 :     $WeBWorK::timer->start;
168 : sh002i 2346
169 : sh002i 1836 debug("We need to get a course environment (with or without a courseID!)\n");
170 :     my $ce = new WeBWorK::CourseEnvironment($webwork_root, $location, $pg_root, $displayArgs{courseID});
171 :     debug("Here's the course environment: $ce\n");
172 : sh002i 1885 $r->ce($ce);
173 : sh002i 1836
174 : sh002i 1879 my @uploads = $r->upload;
175 :     foreach my $u (@uploads) {
176 :     # make sure it's a "real" upload
177 :     next unless $u->filename;
178 :    
179 :     # store the upload
180 :     my $upload = WeBWorK::Upload->store($u,
181 :     dir => $ce->{webworkDirs}->{uploadCache}
182 :     );
183 :    
184 :     # store the upload ID and hash in the file upload field
185 :     my $id = $upload->id;
186 :     my $hash = $upload->hash;
187 :     $r->param($u->name => "$id $hash");
188 :     }
189 : sh002i 1836
190 :     my ($db, $authz);
191 :    
192 :     if ($displayArgs{courseID}) {
193 :     debug("We got a courseID from the URLPath, now we can do some stuff:\n");
194 :     debug("...we can create a database object...\n");
195 :     $db = new WeBWorK::DB($ce->{dbLayout});
196 :     debug("(here's the DB handle: $db)\n");
197 : sh002i 1885 $r->db($db);
198 : sh002i 1836
199 : sh002i 2313 debug("Now we check the database...\n");
200 :     debug("(we can detect if a hash-style database from WW1 has not be converted properly.)\n");
201 :     my ($dbOK, @dbMessages) = $db->hashDatabaseOK(0); # 0 == don't fix
202 :     if (not $dbOK) {
203 :     debug("hashDatabaseOK() returned $dbOK -- looks like trouble...\n");
204 :     $displayModule = FIXDB_MODULE;
205 :     debug("set displayModule to $displayModule\n");
206 :     } else {
207 :     debug("hashDatabaseOK() returned $dbOK -- leaving displayModule as-is\n");
208 :     }
209 :    
210 :     debug("...and now we can authenticate the remote user...\n");
211 : sh002i 1885 my $authen = new WeBWorK::Authen($r);
212 : sh002i 1836 my $authenOK = $authen->verify;
213 :     if ($authenOK) {
214 :     debug("Hi, ", $r->param("user"), ", glad you made it.\n");
215 :    
216 :     debug("Authentication succeeded, so it makes sense to create an authz object...\n");
217 : sh002i 1885 $authz = new WeBWorK::Authz($r, $ce, $db);
218 : sh002i 1836 debug("(here's the authz object: $authz)\n");
219 : sh002i 1885 $r->authz($authz);
220 : sh002i 1836
221 :     debug("Now we deal with the effective user:\n");
222 :     my $userID = $r->param("user");
223 :     my $eUserID = $r->param("effectiveUser") || $userID;
224 :     debug("userID=$userID eUserID=$eUserID\n");
225 :     my $su_authorized = $authz->hasPermissions($userID, "become_student", $eUserID);
226 :     if ($su_authorized) {
227 :     debug("Ok, looks like you're is allowed to become $eUserID. Whoopie!\n");
228 :     } else {
229 :     debug("Uh oh, you're isn't allowed to become $eUserID. Nice try!\n");
230 :     $eUserID = $userID;
231 :     }
232 :     $r->param("effectiveUser" => $eUserID);
233 :     } else {
234 :     debug("Bad news: authentication failed!\n");
235 :     $displayModule = AUTHEN_MODULE;
236 :     debug("set displayModule to $displayModule\n");
237 :     }
238 :     }
239 :    
240 :     debug(("-" x 80) . "\n");
241 :     debug("Finally, we'll load the display module...\n");
242 :    
243 :     runtime_use($displayModule);
244 :    
245 :     debug("...instantiate it...\n");
246 :    
247 :     my $instance = $displayModule->new($r);
248 :    
249 :     debug("...and call it:\n");
250 :     debug("-------------------- call to ${displayModule}::go\n");
251 :    
252 :     my $result = $instance->go();
253 :    
254 :     debug("-------------------- call to ${displayModule}::go\n");
255 :    
256 : sh002i 1910 debug("returning result: " . (defined $result ? $result : "UNDEF") . "\n");
257 : sh002i 2346
258 : sh002i 2368 $WeBWorK::timer->save();
259 : sh002i 2346
260 : gage 2098 return $result;
261 : sh002i 1836
262 :     }
263 :    
264 :     sub mungeParams {
265 :     my ($r) = @_;
266 :    
267 :     my @paramQueue;
268 :    
269 :     # remove all the params from the request, and store them in the param queue
270 :     foreach my $key ($r->param) {
271 :     push @paramQueue, [ $key => [ $r->param($key) ] ];
272 :     $r->parms->unset($key)
273 :     }
274 :    
275 :     # exhaust the param queue, decoding encoded params
276 :     while (@paramQueue) {
277 :     my ($key, $values) = @{ shift @paramQueue };
278 :    
279 :     if ($key =~ m/\,/) {
280 :     # we have multiple params encoded in a single param
281 :     # split them up and add them to the end of the queue
282 :     push @paramQueue, map { [ $_, $values ] } split m/\,/, $key;
283 :     } elsif ($key =~ m/\:/) {
284 :     # we have a whole param encoded in a key
285 :     # split it up and add it to the end of the queue
286 :     my ($newKey, $newValue) = split m/\:/, $key;
287 :     push @paramQueue, [ $newKey, [ $newValue ] ];
288 :     } else {
289 :     # this is a "normal" param
290 :     # add it to the param list
291 :     if (defined $r->param($key)) {
292 :     # the param already exists -- append the values we have
293 :     $r->param($key => [ $r->param($key), @$values ]);
294 :     } else {
295 :     # the param doesn't exist -- create it with the values we have
296 :     $r->param($key => $values);
297 :     }
298 :     }
299 :     }
300 :     }
301 :    
302 :    
303 : sh002i 1565 =head1 AUTHOR
304 :    
305 : sh002i 1616 Written by Dennis Lambe, malsyned at math.rochester.edu. Modified by Sam
306 :     Hathaway, sh002i at math.rochester.edu.
307 : sh002i 1565
308 :     =cut
309 :    
310 : sh002i 986 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9