| 1 | # Apache::WeBWorK - The WeBWorK dispatcher module |
1 | ################################################################################ |
| 2 | # Place something like the following in your Apache configuration to load the |
2 | # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project |
| 3 | # WeBWorK module and install it as a handler for the WeBWorK system |
3 | # $Id$ |
| 4 | |
4 | ################################################################################ |
| 5 | # PerlRequire /path/to/webwork/conf/init.pl |
|
|
| 6 | # PerlSetVar webwork_root /path/to/webwork |
|
|
| 7 | # <Location /webwork> |
|
|
| 8 | # SetHandler perl-script |
|
|
| 9 | # PerlHandler Apache::WeBWorK |
|
|
| 10 | # </Location> |
|
|
| 11 | |
|
|
| 12 | # In addition, you will have to edit init.pl in what should be obvious ways. |
|
|
| 13 | |
5 | |
| 14 | package Apache::WeBWorK; |
6 | package Apache::WeBWorK; |
| 15 | |
7 | |
|
|
8 | =head1 NAME |
|
|
9 | |
|
|
10 | Apache::WeBWorK - The WeBWorK dispatcher module. |
|
|
11 | |
|
|
12 | =cut |
|
|
13 | |
| 16 | use strict; |
14 | use strict; |
|
|
15 | use warnings; |
| 17 | use Apache::Constants qw(:common REDIRECT); |
16 | use Apache::Constants qw(:common REDIRECT); |
| 18 | use Apache::Request; |
17 | use Apache::Request; |
| 19 | use WeBWorK::CourseEnvironment; |
|
|
| 20 | use WeBWorK::Authen; |
18 | use WeBWorK::Authen; |
| 21 | use WeBWorK::Authz; |
19 | use WeBWorK::Authz; |
|
|
20 | use WeBWorK::ContentGenerator::Feedback; |
|
|
21 | use WeBWorK::ContentGenerator::Login; |
|
|
22 | use WeBWorK::ContentGenerator::Logout; |
|
|
23 | use WeBWorK::ContentGenerator::Hardcopy; |
|
|
24 | use WeBWorK::ContentGenerator::Options; |
|
|
25 | use WeBWorK::ContentGenerator::Problem; |
|
|
26 | use WeBWorK::ContentGenerator::ProblemSet; |
|
|
27 | use WeBWorK::ContentGenerator::ProblemSets; |
|
|
28 | use WeBWorK::ContentGenerator::Professor; |
| 22 | use WeBWorK::ContentGenerator::Test; |
29 | use WeBWorK::ContentGenerator::Test; |
| 23 | use WeBWorK::ContentGenerator::Login; |
30 | use WeBWorK::CourseEnvironment; |
| 24 | use WeBWorK::ContentGenerator::ProblemSets; |
31 | use WeBWorK::DB; |
| 25 | use WeBWorK::ContentGenerator::ProblemSet; |
|
|
| 26 | use WeBWorK::ContentGenerator::Problem; |
|
|
| 27 | |
32 | |
| 28 | # Sets up the common environment needed for every subsystem and then dispatches |
33 | # This module should be installed as a Handler for the location selected for |
| 29 | # the page request to the appropriate content generator. |
34 | # WeBWorK on your webserver. Here is an example of a stanza that can be added |
| 30 | |
35 | # to your httpd.conf file to achieve this: |
| 31 | # This function has MANY MANY points of exit (return statements)! woo! |
36 | # |
| 32 | # call it a quirk of my coding style. I think it makes it easier to read in this case. |
37 | # <IfModule mod_perl.c> |
|
|
38 | # PerlFreshRestart On |
|
|
39 | # <Location /webwork> |
|
|
40 | # SetHandler perl-script |
|
|
41 | # PerlHandler Apache::WeBWorK |
|
|
42 | # PerlSetVar webwork_root /path/to/webwork-modperl |
|
|
43 | # <Perl> |
|
|
44 | # use lib '/path/to/webwork-modperl/lib'; |
|
|
45 | # use lib '/path/to/webwork-modperl/pglib'; |
|
|
46 | # </Perl> |
|
|
47 | # </Location> |
|
|
48 | # </IfModule> |
| 33 | |
49 | |
| 34 | sub handler() { |
50 | sub handler() { |
| 35 | my $r = Apache::Request->new(shift); # have to deal with unpredictable GET or POST data, and sift through it for the key. So use Apache::Request |
51 | my $r = Apache::Request->new(shift); # have to deal with unpredictable GET or POST data, and sift through it for the key. So use Apache::Request |
| 36 | |
52 | |
| 37 | # This stuff is pretty much copied out of the O'Reilly mod_perl book. |
53 | # This stuff is pretty much copied out of the O'Reilly mod_perl book. |
| 38 | # It's for figuring out the basepath. I may change this up if I |
54 | # It's for figuring out the basepath. I may change this up if I |
| 39 | # find a better way to do it. |
55 | # find a better way to do it. |
| 40 | my $path_info = $r->path_info; |
56 | my $path_info = $r->path_info || ""; |
| 41 | my $path_translated = $r->lookup_uri($path_info)->filename; |
|
|
| 42 | my $current_uri = $r->uri; |
57 | my $current_uri = $r->uri; |
| 43 | my $args = $r->args; |
58 | my $args = $r->args; |
|
|
59 | |
|
|
60 | $current_uri =~ m/^(.*)$path_info/; |
|
|
61 | my $urlRoot = $1; |
| 44 | |
62 | |
| 45 | # If it's a valid WeBWorK URI, it ends in a /. This is assumed |
63 | # If it's a valid WeBWorK URI, it ends in a /. This is assumed |
| 46 | # alllll over the place. |
64 | # alllll over the place. |
| 47 | unless (substr($current_uri,-1) eq '/') { |
65 | unless (substr($current_uri,-1) eq '/') { |
| 48 | $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : "")); |
66 | $r->header_out(Location => "$current_uri/" . ($args ? "?$args" : "")); |
| 49 | return REDIRECT; |
67 | return REDIRECT; |
|
|
68 | # *** any post data gets lost here -- fix that. |
| 50 | } |
69 | } |
| 51 | |
70 | |
| 52 | # Create the @components array, which contains the path specified in the URL |
71 | # Create the @components array, which contains the path specified in the URL |
| 53 | my($junk, @components) = split "/", $path_info; |
72 | my($junk, @components) = split "/", $path_info; |
| 54 | my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf |
73 | my $webwork_root = $r->dir_config('webwork_root'); # From a PerlSetVar in httpd.conf |
| 55 | my $course = shift @components; |
74 | my $course = shift @components; |
| 56 | |
75 | |
| 57 | # Try to get the course environment. |
76 | # Try to get the course environment. |
| 58 | my $course_env = eval {WeBWorK::CourseEnvironment->new($webwork_root, $course);}; |
77 | my $ce = eval {WeBWorK::CourseEnvironment->new($webwork_root, $urlRoot, $course);}; |
| 59 | if ($@) { # If there was an error getting the requested course |
78 | if ($@) { # If there was an error getting the requested course |
| 60 | # TODO: display an error page. For now, 404 it. |
79 | # TODO: display an error page. For now, 404 it. |
| 61 | warn $@; |
80 | warn $@; |
| 62 | return DECLINED; |
81 | return DECLINED; |
| 63 | } |
82 | } |
| 64 | |
83 | |
|
|
84 | # If no course was specified, redirect to the home URL |
|
|
85 | unless (defined $course) { |
|
|
86 | $r->header_out(Location => $ce->{webworkURLs}->{home}); |
|
|
87 | return REDIRECT; |
|
|
88 | } |
|
|
89 | |
| 65 | # Freak out if the requested course doesn't exist. For now, this is just a |
90 | # Freak out if the requested course doesn't exist. For now, this is just a |
| 66 | # check to see if the course directory exists. |
91 | # check to see if the course directory exists. |
| 67 | if (!-e $course_env->{webworkDirs}->{courses} . "/$course") { |
92 | if (!-e $ce->{webworkDirs}->{courses} . "/$course") { |
|
|
93 | warn "Course directory for $course not found at " |
|
|
94 | . $ce->{webworkDirs}->{courses} . "/$course" ."\n"; |
| 68 | return DECLINED; |
95 | return DECLINED; |
| 69 | } |
96 | } |
|
|
97 | |
|
|
98 | # Bring up a connection to the database (for Authen/Authz, and eventually |
|
|
99 | # to be passed to content generators, when we clean this file up). |
|
|
100 | my $db = WeBWorK::DB->new($ce); |
| 70 | |
101 | |
| 71 | ### Begin dispatching ### |
102 | ### Begin dispatching ### |
| 72 | |
103 | |
| 73 | # WeBWorK::Authen::verify erases the passwd field and sets the key field |
104 | # WeBWorK::Authen::verify erases the passwd field and sets the key field |
| 74 | # if login is successful. |
105 | # if login is successful. |
| 75 | if (!WeBWorK::Authen->new($r, $course_env)->verify) { |
106 | if (!WeBWorK::Authen->new($r, $ce, $db)->verify) { |
| 76 | return WeBWorK::ContentGenerator::Login->new($r, $course_env)->go; |
107 | return WeBWorK::ContentGenerator::Login->new($r, $ce, $db)->go; |
| 77 | } else { |
108 | } else { |
| 78 | # After we are authenticated, there are some things that need to be |
109 | # After we are authenticated, there are some things that need to be |
| 79 | # sorted out, Authorization-wize, before we start dispatching to individual |
110 | # sorted out, Authorization-wize, before we start dispatching to individual |
| 80 | # content generators. |
111 | # content generators. |
| 81 | my $effectiveUser = $r->param("effectiveUser"); |
|
|
| 82 | my $user = $r->param("user"); |
112 | my $user = $r->param("user"); |
|
|
113 | my $effectiveUser = $r->param("effectiveUser") || $user; |
| 83 | my $su_authorized = WeBWorK::Authz->new($r, $course_env)->hasPermissions($user, "become_student", $effectiveUser); |
114 | my $su_authorized = WeBWorK::Authz->new($r, $ce, $db)->hasPermissions($user, "become_student", $effectiveUser); |
| 84 | # This hoary statement has the effect of forcing effectiveUser to equal user unless |
115 | $effectiveUser = $user unless $su_authorized; |
| 85 | # the user is otherwise authorized. |
|
|
| 86 | if (!($user ne $effectiveUser && $su_authorized) || !defined $effectiveUser) { |
|
|
| 87 | $r->param("effectiveUser",$user); |
116 | $r->param("effectiveUser", $effectiveUser); |
| 88 | } |
|
|
| 89 | |
117 | |
| 90 | my $arg = shift @components; |
118 | my $arg = shift @components; |
| 91 | if (!defined $arg) { # We want the list of problem sets |
119 | if (!defined $arg) { # We want the list of problem sets |
| 92 | return WeBWorK::ContentGenerator::ProblemSets->new($r, $course_env)->go; |
120 | return WeBWorK::ContentGenerator::ProblemSets->new($r, $ce, $db)->go; |
|
|
121 | } elsif ($arg eq "hardcopy") { |
|
|
122 | my $hardcopyArgument = shift @components; |
|
|
123 | $hardcopyArgument = "" unless defined $hardcopyArgument; |
|
|
124 | return WeBWorK::ContentGenerator::Hardcopy->new($r, $ce, $db)->go($hardcopyArgument); |
| 93 | } elsif ($arg eq "prof") { |
125 | } elsif ($arg eq "prof") { |
| 94 | ### |
126 | return WeBWorK::ContentGenerator::Professor->new($r, $ce, $db)->go; |
| 95 | } elsif ($arg eq "prefs") { |
127 | } elsif ($arg eq "options") { |
| 96 | ### |
128 | return WeBWorK::ContentGenerator::Options->new($r, $ce, $db)->go; |
|
|
129 | } elsif ($arg eq "feedback") { |
|
|
130 | return WeBWorK::ContentGenerator::Feedback->new($r, $ce, $db)->go; |
|
|
131 | } elsif ($arg eq "logout") { |
|
|
132 | return WeBWorK::ContentGenerator::Logout->new($r, $ce, $db)->go; |
| 97 | } elsif ($arg eq "test") { |
133 | } elsif ($arg eq "test") { |
| 98 | return WeBWorK::ContentGenerator::Test->new($r, $course_env)->go; |
134 | return WeBWorK::ContentGenerator::Test->new($r, $ce, $db)->go; |
| 99 | } else { # We've got the name of a problem set. |
135 | } else { # We've got the name of a problem set. |
| 100 | my $problem_set = $arg; |
136 | my $problem_set = $arg; |
| 101 | my $ps_arg = shift @components; |
137 | my $ps_arg = shift @components; |
| 102 | |
138 | |
| 103 | if (!defined $ps_arg) { |
139 | if (!defined $ps_arg) { |
| 104 | # list the problems in the problem set |
140 | # list the problems in the problem set |
| 105 | return WeBWorK::ContentGenerator::ProblemSet->new($r, $course_env)->go($problem_set); |
141 | return WeBWorK::ContentGenerator::ProblemSet->new($r, $ce, $db)->go($problem_set); |
| 106 | } elsif ($ps_arg eq "hardcopy") { |
|
|
| 107 | ### |
|
|
| 108 | } |
|
|
| 109 | else { |
142 | } else { |
| 110 | # We've got the name of a problem |
143 | # We've got the name of a problem |
| 111 | my $problem = $ps_arg; |
144 | my $problem = $ps_arg; |
| 112 | return WeBWorK::ContentGenerator::Problem->new($r, $course_env)->go($problem_set, $problem); |
145 | return WeBWorK::ContentGenerator::Problem->new($r, $ce, $db)->go($problem_set, $problem); |
| 113 | } |
146 | } |
| 114 | } |
147 | } |
| 115 | |
148 | |
| 116 | } |
149 | } |
| 117 | |
150 | |
| 118 | # If the dispatcher doesn't know any modules that want to handle |
151 | # If the dispatcher doesn't know any modules that want to handle |
| 119 | # the current path, it'll claim that the path does not exist by |
152 | # the current path, it'll claim that the path does not exist by |
| 120 | # declining the request. |
153 | # declining the request. |
| 121 | return DECLINED; |
154 | return DECLINED; |
| 122 | } |
155 | } |